1 /* vi: set sw=4 ts=4: */
4 * Signal pipe infrastructure. A reliable way of delivering signals.
6 * Russ Dill <Russ.Dill@asu.edu> December 2003
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.
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.
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.
26 static struct fd_pair signal_pipe;
28 static void signal_handler(int sig)
30 unsigned char ch = sig; /* use char, avoid dealing with partial writes */
31 if (write(signal_pipe.wr, &ch, 1) != 1)
32 bb_perror_msg("can't send signal");
36 /* Call this before doing anything else. Sets up the socket pair
37 * and installs the signal handler */
38 void FAST_FUNC udhcp_sp_setup(void)
40 /* was socketpair, but it needs AF_UNIX in kernel */
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);
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
56 int FAST_FUNC udhcp_sp_fd_set(fd_set *rfds, int extra_fd)
59 FD_SET(signal_pipe.rd, rfds);
61 close_on_exec_on(extra_fd);
62 FD_SET(extra_fd, rfds);
64 return signal_pipe.rd > extra_fd ? signal_pipe.rd : extra_fd;
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 */
71 int FAST_FUNC udhcp_sp_read(const fd_set *rfds)
75 if (!FD_ISSET(signal_pipe.rd, rfds))
78 if (safe_read(signal_pipe.rd, &sig, 1) != 1)