"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Denys Vlasenko | 385b456 | 2010-03-26 10:09:34 +0100 | [diff] [blame] | 2 | /* |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 3 | * Signal pipe infrastructure. A reliable way of delivering signals. |
| 4 | * |
| 5 | * Russ Dill <Russ.Dill@asu.edu> December 2003 |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 21 | #include "common.h" |
| 22 | |
Denys Vlasenko | 385b456 | 2010-03-26 10:09:34 +0100 | [diff] [blame] | 23 | /* Global variable: we access it from signal handler */ |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame] | 24 | static struct fd_pair signal_pipe; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 25 | |
| 26 | static void signal_handler(int sig) |
| 27 | { |
Denys Vlasenko | dc207f6 | 2017-02-16 20:04:19 +0100 | [diff] [blame] | 28 | int sv = errno; |
Denis Vlasenko | 5a6aedd | 2007-05-26 16:44:20 +0000 | [diff] [blame] | 29 | unsigned char ch = sig; /* use char, avoid dealing with partial writes */ |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame] | 30 | if (write(signal_pipe.wr, &ch, 1) != 1) |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 31 | bb_perror_msg("can't send signal"); |
Denys Vlasenko | dc207f6 | 2017-02-16 20:04:19 +0100 | [diff] [blame] | 32 | errno = sv; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 35 | /* Call this before doing anything else. Sets up the socket pair |
| 36 | * and installs the signal handler */ |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 37 | void FAST_FUNC udhcp_sp_setup(void) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 38 | { |
Denis Vlasenko | 5a6aedd | 2007-05-26 16:44:20 +0000 | [diff] [blame] | 39 | /* was socketpair, but it needs AF_UNIX in kernel */ |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame] | 40 | xpiped_pair(signal_pipe); |
| 41 | close_on_exec_on(signal_pipe.rd); |
| 42 | close_on_exec_on(signal_pipe.wr); |
Denys Vlasenko | 3293bc1 | 2018-03-10 19:01:48 +0100 | [diff] [blame] | 43 | ndelay_on(signal_pipe.rd); |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame] | 44 | ndelay_on(signal_pipe.wr); |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 45 | bb_signals(0 |
| 46 | + (1 << SIGUSR1) |
| 47 | + (1 << SIGUSR2) |
| 48 | + (1 << SIGTERM) |
| 49 | , signal_handler); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Denys Vlasenko | 52a515d | 2017-02-16 23:25:44 +0100 | [diff] [blame] | 52 | /* Quick little function to setup the pfds. |
| 53 | * Limited in that you can only pass one extra fd. |
| 54 | */ |
| 55 | void FAST_FUNC udhcp_sp_fd_set(struct pollfd pfds[2], int extra_fd) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 56 | { |
Denys Vlasenko | 52a515d | 2017-02-16 23:25:44 +0100 | [diff] [blame] | 57 | pfds[0].fd = signal_pipe.rd; |
| 58 | pfds[0].events = POLLIN; |
| 59 | pfds[1].fd = -1; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 60 | if (extra_fd >= 0) { |
Denis Vlasenko | 96e1b38 | 2007-09-30 23:50:48 +0000 | [diff] [blame] | 61 | close_on_exec_on(extra_fd); |
Denys Vlasenko | 52a515d | 2017-02-16 23:25:44 +0100 | [diff] [blame] | 62 | pfds[1].fd = extra_fd; |
| 63 | pfds[1].events = POLLIN; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 64 | } |
Denys Vlasenko | 3293bc1 | 2018-03-10 19:01:48 +0100 | [diff] [blame] | 65 | /* this simplifies "is extra_fd ready?" tests elsewhere: */ |
| 66 | pfds[1].revents = 0; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 69 | /* Read a signal from the signal pipe. Returns 0 if there is |
| 70 | * no signal, -1 on error (and sets errno appropriately), and |
| 71 | * your signal on success */ |
Denys Vlasenko | 3293bc1 | 2018-03-10 19:01:48 +0100 | [diff] [blame] | 72 | int FAST_FUNC udhcp_sp_read(void) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 73 | { |
Denis Vlasenko | 5a6aedd | 2007-05-26 16:44:20 +0000 | [diff] [blame] | 74 | unsigned char sig; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 75 | |
Denys Vlasenko | 3293bc1 | 2018-03-10 19:01:48 +0100 | [diff] [blame] | 76 | /* Can't block here, fd is in nonblocking mode */ |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame] | 77 | if (safe_read(signal_pipe.rd, &sig, 1) != 1) |
Denys Vlasenko | 3293bc1 | 2018-03-10 19:01:48 +0100 | [diff] [blame] | 78 | return 0; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 79 | |
| 80 | return sig; |
| 81 | } |