blob: b101b4ce4e1040abf2cff45f066139774fafad21 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Denys Vlasenko385b4562010-03-26 10:09:34 +01002/*
Mike Frysinger7031f622006-05-08 03:20:50 +00003 * Signal pipe infrastructure. A reliable way of delivering signals.
4 *
5 * Russ Dill <Russ.Dill@asu.edu> December 2003
6 *
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.
11 *
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.
16 *
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.
20 */
Mike Frysinger7031f622006-05-08 03:20:50 +000021#include "common.h"
22
Denys Vlasenko385b4562010-03-26 10:09:34 +010023/* Global variable: we access it from signal handler */
Denis Vlasenko37188322008-02-16 13:20:56 +000024static struct fd_pair signal_pipe;
Mike Frysinger7031f622006-05-08 03:20:50 +000025
26static void signal_handler(int sig)
27{
Denys Vlasenkodc207f62017-02-16 20:04:19 +010028 int sv = errno;
Denis Vlasenko5a6aedd2007-05-26 16:44:20 +000029 unsigned char ch = sig; /* use char, avoid dealing with partial writes */
Denis Vlasenko37188322008-02-16 13:20:56 +000030 if (write(signal_pipe.wr, &ch, 1) != 1)
Denys Vlasenko6331cf02009-11-13 09:08:27 +010031 bb_perror_msg("can't send signal");
Denys Vlasenkodc207f62017-02-16 20:04:19 +010032 errno = sv;
Mike Frysinger7031f622006-05-08 03:20:50 +000033}
34
Mike Frysinger7031f622006-05-08 03:20:50 +000035/* Call this before doing anything else. Sets up the socket pair
36 * and installs the signal handler */
Denis Vlasenkof1980f62008-09-26 09:34:59 +000037void FAST_FUNC udhcp_sp_setup(void)
Mike Frysinger7031f622006-05-08 03:20:50 +000038{
Denis Vlasenko5a6aedd2007-05-26 16:44:20 +000039 /* was socketpair, but it needs AF_UNIX in kernel */
Denis Vlasenko37188322008-02-16 13:20:56 +000040 xpiped_pair(signal_pipe);
41 close_on_exec_on(signal_pipe.rd);
42 close_on_exec_on(signal_pipe.wr);
43 ndelay_on(signal_pipe.wr);
Denis Vlasenko25591c32008-02-16 22:58:56 +000044 bb_signals(0
45 + (1 << SIGUSR1)
46 + (1 << SIGUSR2)
47 + (1 << SIGTERM)
48 , signal_handler);
Mike Frysinger7031f622006-05-08 03:20:50 +000049}
50
Denys Vlasenko52a515d2017-02-16 23:25:44 +010051/* Quick little function to setup the pfds.
52 * Limited in that you can only pass one extra fd.
53 */
54void FAST_FUNC udhcp_sp_fd_set(struct pollfd pfds[2], int extra_fd)
Mike Frysinger7031f622006-05-08 03:20:50 +000055{
Denys Vlasenko52a515d2017-02-16 23:25:44 +010056 pfds[0].fd = signal_pipe.rd;
57 pfds[0].events = POLLIN;
58 pfds[1].fd = -1;
Mike Frysinger7031f622006-05-08 03:20:50 +000059 if (extra_fd >= 0) {
Denis Vlasenko96e1b382007-09-30 23:50:48 +000060 close_on_exec_on(extra_fd);
Denys Vlasenko52a515d2017-02-16 23:25:44 +010061 pfds[1].fd = extra_fd;
62 pfds[1].events = POLLIN;
Mike Frysinger7031f622006-05-08 03:20:50 +000063 }
Mike Frysinger7031f622006-05-08 03:20:50 +000064}
65
Mike Frysinger7031f622006-05-08 03:20:50 +000066/* 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 */
Denys Vlasenko52a515d2017-02-16 23:25:44 +010069int FAST_FUNC udhcp_sp_read(struct pollfd pfds[2])
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
Denys Vlasenko52a515d2017-02-16 23:25:44 +010073 if (!pfds[0].revents)
Mike Frysinger7031f622006-05-08 03:20:50 +000074 return 0;
75
Denis Vlasenko37188322008-02-16 13:20:56 +000076 if (safe_read(signal_pipe.rd, &sig, 1) != 1)
Mike Frysinger7031f622006-05-08 03:20:50 +000077 return -1;
78
79 return sig;
80}