• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * dhcpcd - DHCP client daemon
3  * Copyright 2006-2008 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 	SIGHUP,
44 	SIGALRM,
45 	SIGTERM,
46 	SIGINT
47 };
48 
49 static void
signal_handler(int sig)50 signal_handler(int sig)
51 {
52 	int serrno = errno;
53 
54 	if (write(signal_pipe[1], &sig, sizeof(sig)) != sizeof(sig))
55 		syslog(LOG_ERR, "write signal %d: %s", sig, strerror(errno));
56 	/* Restore errno */
57 	errno = serrno;
58 }
59 
60 int
signal_fd(void)61 signal_fd(void)
62 {
63 	return (signal_pipe[0]);
64 }
65 
66 /* Read a signal from the signal pipe. Returns 0 if there is
67  * no signal, -1 on error (and sets errno appropriately), and
68  * your signal on success */
69 int
signal_read(void)70 signal_read(void)
71 {
72 	int sig = -1;
73 	char buf[16];
74 	ssize_t bytes;
75 
76 	memset(buf, 0, sizeof(buf));
77 	bytes = read(signal_pipe[0], buf, sizeof(buf));
78 	if (bytes >= 0 && (size_t)bytes >= sizeof(sig))
79 		memcpy(&sig, buf, sizeof(sig));
80 	return sig;
81 }
82 
83 /* Call this before doing anything else. Sets up the socket pair
84  * and installs the signal handler */
85 int
signal_init(void)86 signal_init(void)
87 {
88 	if (pipe(signal_pipe) == -1)
89 		return -1;
90 	/* Don't block on read */
91 	if (set_nonblock(signal_pipe[0]) == -1)
92 		return -1;
93 	/* Stop any scripts from inheriting us */
94 	if (set_cloexec(signal_pipe[0]) == -1)
95 		return -1;
96 	if (set_cloexec(signal_pipe[1]) == -1)
97 		return -1;
98 	return 0;
99 }
100 
101 static int
signal_handle(void (* func)(int))102 signal_handle(void (*func)(int))
103 {
104 	unsigned int i;
105 	struct sigaction sa;
106 
107 	memset(&sa, 0, sizeof(sa));
108 	sa.sa_handler = func;
109 	sigemptyset(&sa.sa_mask);
110 
111 	for (i = 0; i < sizeof(handle_sigs) / sizeof(handle_sigs[0]); i++)
112 		if (sigaction(handle_sigs[i], &sa, NULL) == -1)
113 			return -1;
114 	return 0;
115 }
116 
117 int
signal_setup(void)118 signal_setup(void)
119 {
120 	return signal_handle(signal_handler);
121 }
122 
123 int
signal_reset(void)124 signal_reset(void)
125 {
126 	return signal_handle(SIG_DFL);
127 }
128