23 |
#include "common.h" |
#include "common.h" |
24 |
|
|
25 |
|
|
26 |
static int signal_pipe[2]; |
static struct fd_pair signal_pipe; |
27 |
|
|
28 |
static void signal_handler(int sig) |
static void signal_handler(int sig) |
29 |
{ |
{ |
30 |
if (send(signal_pipe[1], &sig, sizeof(sig), MSG_DONTWAIT) < 0) |
unsigned char ch = sig; /* use char, avoid dealing with partial writes */ |
31 |
|
if (write(signal_pipe.wr, &ch, 1) != 1) |
32 |
bb_perror_msg("cannot send signal"); |
bb_perror_msg("cannot send signal"); |
33 |
} |
} |
34 |
|
|
35 |
|
|
36 |
/* Call this before doing anything else. Sets up the socket pair |
/* Call this before doing anything else. Sets up the socket pair |
37 |
* and installs the signal handler */ |
* and installs the signal handler */ |
38 |
void udhcp_sp_setup(void) |
void FAST_FUNC udhcp_sp_setup(void) |
39 |
{ |
{ |
40 |
socketpair(AF_UNIX, SOCK_STREAM, 0, signal_pipe); |
/* was socketpair, but it needs AF_UNIX in kernel */ |
41 |
fcntl(signal_pipe[0], F_SETFD, FD_CLOEXEC); |
xpiped_pair(signal_pipe); |
42 |
fcntl(signal_pipe[1], F_SETFD, FD_CLOEXEC); |
close_on_exec_on(signal_pipe.rd); |
43 |
signal(SIGUSR1, signal_handler); |
close_on_exec_on(signal_pipe.wr); |
44 |
signal(SIGUSR2, signal_handler); |
ndelay_on(signal_pipe.wr); |
45 |
signal(SIGTERM, signal_handler); |
bb_signals(0 |
46 |
|
+ (1 << SIGUSR1) |
47 |
|
+ (1 << SIGUSR2) |
48 |
|
+ (1 << SIGTERM) |
49 |
|
, signal_handler); |
50 |
} |
} |
51 |
|
|
52 |
|
|
53 |
/* Quick little function to setup the rfds. Will return the |
/* Quick little function to setup the rfds. Will return the |
54 |
* max_fd for use with select. Limited in that you can only pass |
* max_fd for use with select. Limited in that you can only pass |
55 |
* one extra fd */ |
* one extra fd */ |
56 |
int udhcp_sp_fd_set(fd_set *rfds, int extra_fd) |
int FAST_FUNC udhcp_sp_fd_set(fd_set *rfds, int extra_fd) |
57 |
{ |
{ |
58 |
FD_ZERO(rfds); |
FD_ZERO(rfds); |
59 |
FD_SET(signal_pipe[0], rfds); |
FD_SET(signal_pipe.rd, rfds); |
60 |
if (extra_fd >= 0) { |
if (extra_fd >= 0) { |
61 |
fcntl(extra_fd, F_SETFD, FD_CLOEXEC); |
close_on_exec_on(extra_fd); |
62 |
FD_SET(extra_fd, rfds); |
FD_SET(extra_fd, rfds); |
63 |
} |
} |
64 |
return signal_pipe[0] > extra_fd ? signal_pipe[0] : extra_fd; |
return signal_pipe.rd > extra_fd ? signal_pipe.rd : extra_fd; |
65 |
} |
} |
66 |
|
|
67 |
|
|
68 |
/* Read a signal from the signal pipe. Returns 0 if there is |
/* Read a signal from the signal pipe. Returns 0 if there is |
69 |
* no signal, -1 on error (and sets errno appropriately), and |
* no signal, -1 on error (and sets errno appropriately), and |
70 |
* your signal on success */ |
* your signal on success */ |
71 |
int udhcp_sp_read(fd_set *rfds) |
int FAST_FUNC udhcp_sp_read(const fd_set *rfds) |
72 |
{ |
{ |
73 |
int sig; |
unsigned char sig; |
74 |
|
|
75 |
if (!FD_ISSET(signal_pipe[0], rfds)) |
if (!FD_ISSET(signal_pipe.rd, rfds)) |
76 |
return 0; |
return 0; |
77 |
|
|
78 |
if (read(signal_pipe[0], &sig, sizeof(sig)) < 0) |
if (safe_read(signal_pipe.rd, &sig, 1) != 1) |
79 |
return -1; |
return -1; |
80 |
|
|
81 |
return sig; |
return sig; |