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