blob: 845aa3a9a522bfdb9a60574e805af2b9d1bf0265 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger7031f622006-05-08 03:20:50 +00002/* 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 Frysinger7031f622006-05-08 03:20:50 +000023#include "common.h"
24
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000025
Mike Frysinger7031f622006-05-08 03:20:50 +000026static int signal_pipe[2];
27
28static void signal_handler(int sig)
29{
Denis Vlasenko5a6aedd2007-05-26 16:44:20 +000030 unsigned char ch = sig; /* use char, avoid dealing with partial writes */
31 if (write(signal_pipe[1], &ch, 1) != 1)
Denis Vlasenkoea620772006-10-14 02:23:43 +000032 bb_perror_msg("cannot send signal");
Mike Frysinger7031f622006-05-08 03:20:50 +000033}
34
35
36/* Call this before doing anything else. Sets up the socket pair
37 * and installs the signal handler */
38void udhcp_sp_setup(void)
39{
Denis Vlasenko5a6aedd2007-05-26 16:44:20 +000040 /* was socketpair, but it needs AF_UNIX in kernel */
41 xpipe(signal_pipe);
Denis Vlasenko96e1b382007-09-30 23:50:48 +000042 close_on_exec_on(signal_pipe[0]);
43 close_on_exec_on(signal_pipe[1]);
44 ndelay_on(signal_pipe[1]);
Mike Frysinger7031f622006-05-08 03:20:50 +000045 signal(SIGUSR1, signal_handler);
46 signal(SIGUSR2, signal_handler);
47 signal(SIGTERM, signal_handler);
48}
49
50
51/* Quick little function to setup the rfds. Will return the
52 * max_fd for use with select. Limited in that you can only pass
53 * one extra fd */
54int udhcp_sp_fd_set(fd_set *rfds, int extra_fd)
55{
56 FD_ZERO(rfds);
57 FD_SET(signal_pipe[0], rfds);
58 if (extra_fd >= 0) {
Denis Vlasenko96e1b382007-09-30 23:50:48 +000059 close_on_exec_on(extra_fd);
Mike Frysinger7031f622006-05-08 03:20:50 +000060 FD_SET(extra_fd, rfds);
61 }
62 return signal_pipe[0] > extra_fd ? signal_pipe[0] : extra_fd;
63}
64
65
66/* Read a signal from the signal pipe. Returns 0 if there is
67 * no signal, -1 on error (and sets errno appropriately), and
68 * your signal on success */
Denis Vlasenko223bc972007-11-22 00:58:49 +000069int udhcp_sp_read(const fd_set *rfds)
Mike Frysinger7031f622006-05-08 03:20:50 +000070{
Denis Vlasenko5a6aedd2007-05-26 16:44:20 +000071 unsigned char sig;
Mike Frysinger7031f622006-05-08 03:20:50 +000072
73 if (!FD_ISSET(signal_pipe[0], rfds))
74 return 0;
75
Denis Vlasenko5a6aedd2007-05-26 16:44:20 +000076 if (read(signal_pipe[0], &sig, 1) != 1)
Mike Frysinger7031f622006-05-08 03:20:50 +000077 return -1;
78
79 return sig;
80}