1 /* vi: set sw=4 ts=4: */
3 * Signal pipe infrastructure. A reliable way of delivering signals.
5 * Russ Dill <Russ.Dill@asu.edu> December 2003
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.
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.
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.
26 static void signal_handler(int sig)
29 unsigned char ch = sig; /* use char, avoid dealing with partial writes */
30 if (write(WRITE_FD, &ch, 1) != 1)
31 bb_simple_perror_msg("can't send signal");
35 /* Call this before doing anything else. Sets up the socket pair
36 * and installs the signal handler */
37 void FAST_FUNC udhcp_sp_setup(void)
39 struct fd_pair signal_pipe;
41 /* All callers also want this, so... */
44 /* was socketpair, but it needs AF_UNIX in kernel */
45 xpiped_pair(signal_pipe);
47 /* usually we get fds 3 and 4, but if we get higher ones... */
48 if (signal_pipe.rd != READ_FD)
49 xmove_fd(signal_pipe.rd, READ_FD);
50 if (signal_pipe.wr != WRITE_FD)
51 xmove_fd(signal_pipe.wr, WRITE_FD);
53 close_on_exec_on(READ_FD);
54 close_on_exec_on(WRITE_FD);
65 /* Quick little function to setup the pfds.
66 * Limited in that you can only pass one extra fd.
68 void FAST_FUNC udhcp_sp_fd_set(struct pollfd pfds[2], int extra_fd)
71 pfds[0].events = POLLIN;
74 close_on_exec_on(extra_fd);
75 pfds[1].fd = extra_fd;
76 pfds[1].events = POLLIN;
78 /* this simplifies "is extra_fd ready?" tests elsewhere: */
82 /* Read a signal from the signal pipe. Returns 0 if there is
83 * no signal, -1 on error (and sets errno appropriately), and
84 * your signal on success */
85 int FAST_FUNC udhcp_sp_read(void)
89 /* Can't block here, fd is in nonblocking mode */
90 if (safe_read(READ_FD, &sig, 1) != 1)