2006-07-03 01:17:05 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2010-03-26 14:39:34 +05:30
|
|
|
/*
|
2006-05-08 08:50:50 +05:30
|
|
|
* Signal pipe infrastructure. A reliable way of delivering signals.
|
|
|
|
*
|
|
|
|
* Russ Dill <Russ.Dill@asu.edu> December 2003
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
#include "common.h"
|
|
|
|
|
2010-03-26 14:39:34 +05:30
|
|
|
/* Global variable: we access it from signal handler */
|
2008-02-16 18:50:56 +05:30
|
|
|
static struct fd_pair signal_pipe;
|
2006-05-08 08:50:50 +05:30
|
|
|
|
|
|
|
static void signal_handler(int sig)
|
|
|
|
{
|
2017-02-17 00:34:19 +05:30
|
|
|
int sv = errno;
|
2007-05-26 22:14:20 +05:30
|
|
|
unsigned char ch = sig; /* use char, avoid dealing with partial writes */
|
2008-02-16 18:50:56 +05:30
|
|
|
if (write(signal_pipe.wr, &ch, 1) != 1)
|
2009-11-13 13:38:27 +05:30
|
|
|
bb_perror_msg("can't send signal");
|
2017-02-17 00:34:19 +05:30
|
|
|
errno = sv;
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Call this before doing anything else. Sets up the socket pair
|
|
|
|
* and installs the signal handler */
|
2008-09-26 15:04:59 +05:30
|
|
|
void FAST_FUNC udhcp_sp_setup(void)
|
2006-05-08 08:50:50 +05:30
|
|
|
{
|
2007-05-26 22:14:20 +05:30
|
|
|
/* was socketpair, but it needs AF_UNIX in kernel */
|
2008-02-16 18:50:56 +05:30
|
|
|
xpiped_pair(signal_pipe);
|
|
|
|
close_on_exec_on(signal_pipe.rd);
|
|
|
|
close_on_exec_on(signal_pipe.wr);
|
|
|
|
ndelay_on(signal_pipe.wr);
|
2008-02-17 04:28:56 +05:30
|
|
|
bb_signals(0
|
|
|
|
+ (1 << SIGUSR1)
|
|
|
|
+ (1 << SIGUSR2)
|
|
|
|
+ (1 << SIGTERM)
|
|
|
|
, signal_handler);
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|
|
|
|
|
2017-02-17 03:55:44 +05:30
|
|
|
/* Quick little function to setup the pfds.
|
|
|
|
* Limited in that you can only pass one extra fd.
|
|
|
|
*/
|
|
|
|
void FAST_FUNC udhcp_sp_fd_set(struct pollfd pfds[2], int extra_fd)
|
2006-05-08 08:50:50 +05:30
|
|
|
{
|
2017-02-17 03:55:44 +05:30
|
|
|
pfds[0].fd = signal_pipe.rd;
|
|
|
|
pfds[0].events = POLLIN;
|
|
|
|
pfds[1].fd = -1;
|
2006-05-08 08:50:50 +05:30
|
|
|
if (extra_fd >= 0) {
|
2007-10-01 05:20:48 +05:30
|
|
|
close_on_exec_on(extra_fd);
|
2017-02-17 03:55:44 +05:30
|
|
|
pfds[1].fd = extra_fd;
|
|
|
|
pfds[1].events = POLLIN;
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read a signal from the signal pipe. Returns 0 if there is
|
|
|
|
* no signal, -1 on error (and sets errno appropriately), and
|
|
|
|
* your signal on success */
|
2017-02-17 03:55:44 +05:30
|
|
|
int FAST_FUNC udhcp_sp_read(struct pollfd pfds[2])
|
2006-05-08 08:50:50 +05:30
|
|
|
{
|
2007-05-26 22:14:20 +05:30
|
|
|
unsigned char sig;
|
2006-05-08 08:50:50 +05:30
|
|
|
|
2017-02-17 03:55:44 +05:30
|
|
|
if (!pfds[0].revents)
|
2006-05-08 08:50:50 +05:30
|
|
|
return 0;
|
|
|
|
|
2008-02-16 18:50:56 +05:30
|
|
|
if (safe_read(signal_pipe.rd, &sig, 1) != 1)
|
2006-05-08 08:50:50 +05:30
|
|
|
return -1;
|
|
|
|
|
|
|
|
return sig;
|
|
|
|
}
|