2 * dhcpcd - DHCP client daemon
3 * Copyright (c) 2006-2009 Roy Marples <roy@marples.name>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/types.h>
29 #include <sys/socket.h>
40 static int signal_pipe
[2];
42 static const int handle_sigs
[] = {
52 signal_handler(int sig
)
56 if (write(signal_pipe
[1], &sig
, sizeof(sig
)) != sizeof(sig
))
57 syslog(LOG_ERR
, "failed to write signal %d: %m", sig
);
62 /* Read a signal from the signal pipe. Returns 0 if there is
63 * no signal, -1 on error (and sets errno appropriately), and
64 * your signal on success */
72 memset(buf
, 0, sizeof(buf
));
73 bytes
= read(signal_pipe
[0], buf
, sizeof(buf
));
74 if (bytes
>= 0 && (size_t)bytes
>= sizeof(sig
))
75 memcpy(&sig
, buf
, sizeof(sig
));
79 /* Call this before doing anything else. Sets up the socket pair
80 * and installs the signal handler */
84 if (pipe(signal_pipe
) == -1)
86 /* Don't block on read */
87 if (set_nonblock(signal_pipe
[0]) == -1)
89 /* Stop any scripts from inheriting us */
90 if (set_cloexec(signal_pipe
[0]) == -1)
92 if (set_cloexec(signal_pipe
[1]) == -1)
94 return signal_pipe
[0];
98 signal_handle(void (*func
)(int))
103 memset(&sa
, 0, sizeof(sa
));
104 sa
.sa_handler
= func
;
105 sigemptyset(&sa
.sa_mask
);
107 for (i
= 0; i
< sizeof(handle_sigs
) / sizeof(handle_sigs
[0]); i
++)
108 if (sigaction(handle_sigs
[i
], &sa
, NULL
) == -1)
116 return signal_handle(signal_handler
);
122 return signal_handle(SIG_DFL
);