"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 2 | /* signalpipe.c |
| 3 | * |
| 4 | * Signal pipe infrastructure. A reliable way of delivering signals. |
| 5 | * |
| 6 | * Russ Dill <Russ.Dill@asu.edu> December 2003 |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 21 | */ |
| 22 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 23 | #include "common.h" |
| 24 | |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 25 | |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame] | 26 | static struct fd_pair signal_pipe; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 27 | |
| 28 | static void signal_handler(int sig) |
| 29 | { |
Denis Vlasenko | 5a6aedd | 2007-05-26 16:44:20 +0000 | [diff] [blame] | 30 | unsigned char ch = sig; /* use char, avoid dealing with partial writes */ |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame] | 31 | if (write(signal_pipe.wr, &ch, 1) != 1) |
Denis Vlasenko | ea62077 | 2006-10-14 02:23:43 +0000 | [diff] [blame] | 32 | bb_perror_msg("cannot send signal"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | |
| 36 | /* Call this before doing anything else. Sets up the socket pair |
| 37 | * and installs the signal handler */ |
| 38 | void udhcp_sp_setup(void) |
| 39 | { |
Denis Vlasenko | 5a6aedd | 2007-05-26 16:44:20 +0000 | [diff] [blame] | 40 | /* was socketpair, but it needs AF_UNIX in kernel */ |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame] | 41 | xpiped_pair(signal_pipe); |
| 42 | close_on_exec_on(signal_pipe.rd); |
| 43 | close_on_exec_on(signal_pipe.wr); |
| 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 | |
| 52 | |
| 53 | /* Quick little function to setup the rfds. Will return the |
| 54 | * max_fd for use with select. Limited in that you can only pass |
| 55 | * one extra fd */ |
| 56 | int udhcp_sp_fd_set(fd_set *rfds, int extra_fd) |
| 57 | { |
| 58 | FD_ZERO(rfds); |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame] | 59 | FD_SET(signal_pipe.rd, rfds); |
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); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 62 | FD_SET(extra_fd, rfds); |
| 63 | } |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame] | 64 | return signal_pipe.rd > extra_fd ? signal_pipe.rd : extra_fd; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | |
| 68 | /* Read a signal from the signal pipe. Returns 0 if there is |
| 69 | * no signal, -1 on error (and sets errno appropriately), and |
| 70 | * your signal on success */ |
Denis Vlasenko | 223bc97 | 2007-11-22 00:58:49 +0000 | [diff] [blame] | 71 | int udhcp_sp_read(const fd_set *rfds) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 72 | { |
Denis Vlasenko | 5a6aedd | 2007-05-26 16:44:20 +0000 | [diff] [blame] | 73 | unsigned char sig; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 74 | |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame] | 75 | if (!FD_ISSET(signal_pipe.rd, rfds)) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 76 | return 0; |
| 77 | |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame] | 78 | if (safe_read(signal_pipe.rd, &sig, 1) != 1) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 79 | return -1; |
| 80 | |
| 81 | return sig; |
| 82 | } |