• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * dhcpcd - DHCP client daemon
3  * Copyright (c) 2006-2009 Roy Marples <roy@marples.name>
4  * All rights reserved
5 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 
31 #include <errno.h>
32 #include <signal.h>
33 #include <string.h>
34 #include <syslog.h>
35 #include <unistd.h>
36 
37 #include "common.h"
38 #include "signals.h"
39 
40 static int signal_pipe[2];
41 
42 static const int handle_sigs[] = {
43 	SIGALRM,
44 	SIGHUP,
45 	SIGINT,
46 	SIGPIPE,
47 	SIGTERM,
48 	SIGUSR1,
49 };
50 
51 static void
signal_handler(int sig)52 signal_handler(int sig)
53 {
54 	int serrno = errno;
55 
56 	if (write(signal_pipe[1], &sig, sizeof(sig)) != sizeof(sig))
57 		syslog(LOG_ERR, "failed to write signal %d: %m", sig);
58 	/* Restore errno */
59 	errno = serrno;
60 }
61 
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 */
65 int
signal_read(void)66 signal_read(void)
67 {
68 	int sig = -1;
69 	char buf[16];
70 	ssize_t bytes;
71 
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));
76 	return sig;
77 }
78 
79 /* Call this before doing anything else. Sets up the socket pair
80  * and installs the signal handler */
81 int
signal_init(void)82 signal_init(void)
83 {
84 	if (pipe(signal_pipe) == -1)
85 		return -1;
86 	/* Don't block on read */
87 	if (set_nonblock(signal_pipe[0]) == -1)
88 		return -1;
89 	/* Stop any scripts from inheriting us */
90 	if (set_cloexec(signal_pipe[0]) == -1)
91 		return -1;
92 	if (set_cloexec(signal_pipe[1]) == -1)
93 		return -1;
94 	return signal_pipe[0];
95 }
96 
97 static int
signal_handle(void (* func)(int))98 signal_handle(void (*func)(int))
99 {
100 	unsigned int i;
101 	struct sigaction sa;
102 
103 	memset(&sa, 0, sizeof(sa));
104 	sa.sa_handler = func;
105 	sigemptyset(&sa.sa_mask);
106 
107 	for (i = 0; i < sizeof(handle_sigs) / sizeof(handle_sigs[0]); i++)
108 		if (sigaction(handle_sigs[i], &sa, NULL) == -1)
109 			return -1;
110 	return 0;
111 }
112 
113 int
signal_setup(void)114 signal_setup(void)
115 {
116 	return signal_handle(signal_handler);
117 }
118 
119 int
signal_reset(void)120 signal_reset(void)
121 {
122 	return signal_handle(SIG_DFL);
123 }
124 
125