blob: 774c4beeef716e9a3366842ebf58f9f631bc1ef4 [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 Vlasenko65c34c52019-05-31 23:39:22 +020023#define READ_FD 3
24#define WRITE_FD 4
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 */
Denys Vlasenko65c34c52019-05-31 23:39:22 +020030 if (write(WRITE_FD, &ch, 1) != 1)
James Byrne69374872019-07-02 11:35:03 +020031 bb_simple_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{
Denys Vlasenko65c34c52019-05-31 23:39:22 +020039 struct fd_pair signal_pipe;
40
41 /* All callers also want this, so... */
42 bb_sanitize_stdio();
43
Denis Vlasenko5a6aedd2007-05-26 16:44:20 +000044 /* was socketpair, but it needs AF_UNIX in kernel */
Denis Vlasenko37188322008-02-16 13:20:56 +000045 xpiped_pair(signal_pipe);
Denys Vlasenko65c34c52019-05-31 23:39:22 +020046
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);
52
53 close_on_exec_on(READ_FD);
54 close_on_exec_on(WRITE_FD);
55 ndelay_on(READ_FD);
56 ndelay_on(WRITE_FD);
57
Denis Vlasenko25591c32008-02-16 22:58:56 +000058 bb_signals(0
59 + (1 << SIGUSR1)
60 + (1 << SIGUSR2)
61 + (1 << SIGTERM)
62 , signal_handler);
Mike Frysinger7031f622006-05-08 03:20:50 +000063}
64
Denys Vlasenko52a515d2017-02-16 23:25:44 +010065/* Quick little function to setup the pfds.
66 * Limited in that you can only pass one extra fd.
67 */
Denys Vlasenkofe2d8062021-04-14 17:52:18 +020068void FAST_FUNC udhcp_sp_fd_set(struct pollfd *pfds, int extra_fd)
Mike Frysinger7031f622006-05-08 03:20:50 +000069{
Denys Vlasenko65c34c52019-05-31 23:39:22 +020070 pfds[0].fd = READ_FD;
Denys Vlasenko52a515d2017-02-16 23:25:44 +010071 pfds[0].events = POLLIN;
72 pfds[1].fd = -1;
Mike Frysinger7031f622006-05-08 03:20:50 +000073 if (extra_fd >= 0) {
Denis Vlasenko96e1b382007-09-30 23:50:48 +000074 close_on_exec_on(extra_fd);
Denys Vlasenko52a515d2017-02-16 23:25:44 +010075 pfds[1].fd = extra_fd;
76 pfds[1].events = POLLIN;
Mike Frysinger7031f622006-05-08 03:20:50 +000077 }
Denys Vlasenko3293bc12018-03-10 19:01:48 +010078 /* this simplifies "is extra_fd ready?" tests elsewhere: */
79 pfds[1].revents = 0;
Mike Frysinger7031f622006-05-08 03:20:50 +000080}
81
Mike Frysinger7031f622006-05-08 03:20:50 +000082/* 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 */
Denys Vlasenko3293bc12018-03-10 19:01:48 +010085int FAST_FUNC udhcp_sp_read(void)
Mike Frysinger7031f622006-05-08 03:20:50 +000086{
Denis Vlasenko5a6aedd2007-05-26 16:44:20 +000087 unsigned char sig;
Mike Frysinger7031f622006-05-08 03:20:50 +000088
Denys Vlasenko3293bc12018-03-10 19:01:48 +010089 /* Can't block here, fd is in nonblocking mode */
Denys Vlasenko65c34c52019-05-31 23:39:22 +020090 if (safe_read(READ_FD, &sig, 1) != 1)
Denys Vlasenko3293bc12018-03-10 19:01:48 +010091 return 0;
Mike Frysinger7031f622006-05-08 03:20:50 +000092
93 return sig;
94}