SSID: Respect ASCII character Label.
[tomato.git] / release / src / router / busybox / networking / udhcp / signalpipe.c
blob6355c5e90208b5c5f7b70ba74d20bcfd1b510dd5
1 /* vi: set sw=4 ts=4: */
2 /*
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.
21 #include "common.h"
23 /* Global variable: we access it from signal handler */
24 static struct fd_pair signal_pipe;
26 static void signal_handler(int sig)
28 unsigned char ch = sig; /* use char, avoid dealing with partial writes */
29 if (write(signal_pipe.wr, &ch, 1) != 1)
30 bb_perror_msg("can't send signal");
33 /* Call this before doing anything else. Sets up the socket pair
34 * and installs the signal handler */
35 void FAST_FUNC udhcp_sp_setup(void)
37 /* was socketpair, but it needs AF_UNIX in kernel */
38 xpiped_pair(signal_pipe);
39 close_on_exec_on(signal_pipe.rd);
40 close_on_exec_on(signal_pipe.wr);
41 ndelay_on(signal_pipe.wr);
42 bb_signals(0
43 + (1 << SIGUSR1)
44 + (1 << SIGUSR2)
45 + (1 << SIGTERM)
46 , signal_handler);
49 /* Quick little function to setup the rfds. Will return the
50 * max_fd for use with select. Limited in that you can only pass
51 * one extra fd */
52 int FAST_FUNC udhcp_sp_fd_set(fd_set *rfds, int extra_fd)
54 FD_ZERO(rfds);
55 FD_SET(signal_pipe.rd, rfds);
56 if (extra_fd >= 0) {
57 close_on_exec_on(extra_fd);
58 FD_SET(extra_fd, rfds);
60 return signal_pipe.rd > extra_fd ? signal_pipe.rd : extra_fd;
63 /* Read a signal from the signal pipe. Returns 0 if there is
64 * no signal, -1 on error (and sets errno appropriately), and
65 * your signal on success */
66 int FAST_FUNC udhcp_sp_read(const fd_set *rfds)
68 unsigned char sig;
70 if (!FD_ISSET(signal_pipe.rd, rfds))
71 return 0;
73 if (safe_read(signal_pipe.rd, &sig, 1) != 1)
74 return -1;
76 return sig;