Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/networking/udhcp/signalpipe.c
- Timestamp:
- Feb 25, 2011, 9:26:54 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.2.9/mindi-busybox/networking/udhcp/signalpipe.c
r1765 r2725 1 1 /* vi: set sw=4 ts=4: */ 2 /* signalpipe.c 3 * 2 /* 4 3 * Signal pipe infrastructure. A reliable way of delivering signals. 5 4 * … … 20 19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 21 20 */ 22 23 21 #include "common.h" 24 22 25 26 static int signal_pipe[2];23 /* Global variable: we access it from signal handler */ 24 static struct fd_pair signal_pipe; 27 25 28 26 static void signal_handler(int sig) 29 27 { 30 28 unsigned char ch = sig; /* use char, avoid dealing with partial writes */ 31 if (write(signal_pipe [1], &ch, 1) != 1)32 bb_perror_msg("can not send signal");29 if (write(signal_pipe.wr, &ch, 1) != 1) 30 bb_perror_msg("can't send signal"); 33 31 } 34 35 32 36 33 /* Call this before doing anything else. Sets up the socket pair 37 34 * and installs the signal handler */ 38 void udhcp_sp_setup(void)35 void FAST_FUNC udhcp_sp_setup(void) 39 36 { 40 37 /* was socketpair, but it needs AF_UNIX in kernel */ 41 xpipe(signal_pipe); 42 fcntl(signal_pipe[0], F_SETFD, FD_CLOEXEC); 43 fcntl(signal_pipe[1], F_SETFD, FD_CLOEXEC); 44 fcntl(signal_pipe[1], F_SETFL, O_NONBLOCK); 45 signal(SIGUSR1, signal_handler); 46 signal(SIGUSR2, signal_handler); 47 signal(SIGTERM, signal_handler); 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); 48 47 } 49 50 48 51 49 /* Quick little function to setup the rfds. Will return the 52 50 * max_fd for use with select. Limited in that you can only pass 53 51 * one extra fd */ 54 int udhcp_sp_fd_set(fd_set *rfds, int extra_fd)52 int FAST_FUNC udhcp_sp_fd_set(fd_set *rfds, int extra_fd) 55 53 { 56 54 FD_ZERO(rfds); 57 FD_SET(signal_pipe [0], rfds);55 FD_SET(signal_pipe.rd, rfds); 58 56 if (extra_fd >= 0) { 59 fcntl(extra_fd, F_SETFD, FD_CLOEXEC);57 close_on_exec_on(extra_fd); 60 58 FD_SET(extra_fd, rfds); 61 59 } 62 return signal_pipe [0] > extra_fd ? signal_pipe[0]: extra_fd;60 return signal_pipe.rd > extra_fd ? signal_pipe.rd : extra_fd; 63 61 } 64 65 62 66 63 /* Read a signal from the signal pipe. Returns 0 if there is 67 64 * no signal, -1 on error (and sets errno appropriately), and 68 65 * your signal on success */ 69 int udhcp_sp_read(fd_set *rfds)66 int FAST_FUNC udhcp_sp_read(const fd_set *rfds) 70 67 { 71 68 unsigned char sig; 72 69 73 if (!FD_ISSET(signal_pipe [0], rfds))70 if (!FD_ISSET(signal_pipe.rd, rfds)) 74 71 return 0; 75 72 76 if ( read(signal_pipe[0], &sig, 1) != 1)73 if (safe_read(signal_pipe.rd, &sig, 1) != 1) 77 74 return -1; 78 75
Note:
See TracChangeset
for help on using the changeset viewer.