1 /***
2 This file is part of libdaemon.
3
4 Copyright 2003-2008 Lennart Poettering
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23
24 ***/
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <stdarg.h>
35 #include <signal.h>
36
37 #include "dsignal.h"
38 #include "dlog.h"
39 #include "dnonblock.h"
40
41 static int _signal_pipe[2] = { -1, -1 };
42
_sigfunc(int s)43 static void _sigfunc(int s) {
44 int saved_errno = errno;
45 write(_signal_pipe[1], &s, sizeof(s));
46 errno = saved_errno;
47 }
48
_init(void)49 static int _init(void) {
50
51 if (_signal_pipe[0] < 0 || _signal_pipe[1] < 0) {
52 if (pipe(_signal_pipe) < 0) {
53 daemon_log(LOG_ERR, "pipe(): %s", strerror(errno));
54 return -1;
55 }
56
57 if (daemon_nonblock(_signal_pipe[0], 1) < 0 || daemon_nonblock(_signal_pipe[1], 1) < 0) {
58 daemon_signal_done();
59 return -1;
60 }
61 }
62
63 return 0;
64 }
65
daemon_signal_install(int s)66 int daemon_signal_install(int s){
67 sigset_t ss;
68 struct sigaction sa;
69
70 if (_init() < 0)
71 return -1;
72
73 if (sigemptyset(&ss) < 0) {
74 daemon_log(LOG_ERR, "sigemptyset(): %s", strerror(errno));
75 return -1;
76 }
77
78 if (sigaddset(&ss, s) < 0) {
79 daemon_log(LOG_ERR, "sigaddset(): %s", strerror(errno));
80 return -1;
81 }
82
83 if (sigprocmask(SIG_UNBLOCK, &ss, NULL) < 0) {
84 daemon_log(LOG_ERR, "sigprocmask(): %s", strerror(errno));
85 return -1;
86 }
87
88 memset(&sa, 0, sizeof(sa));
89 sa.sa_handler = _sigfunc;
90 sigemptyset(&sa.sa_mask);
91 sa.sa_flags = SA_RESTART;
92
93 if (sigaction(s, &sa, NULL) < 0) {
94 daemon_log(LOG_ERR, "sigaction(%s, ...) failed: %s", strsignal(s), strerror(errno));
95 return -1;
96 }
97
98 return 0;
99 }
100
daemon_signal_init(int s,...)101 int daemon_signal_init(int s, ...) {
102 int sig, r = 0;
103 va_list ap;
104
105 if (_init() < 0)
106 return -1;
107
108 va_start(ap, s);
109
110 sig = s;
111 while (sig > 0) {
112 if ((r = daemon_signal_install(sig)) < 0)
113 break;
114
115 sig = va_arg(ap, int);
116 }
117
118 va_end(ap);
119
120 return r;
121 }
122
daemon_signal_done(void)123 void daemon_signal_done(void) {
124 int saved_errno = errno;
125
126 if (_signal_pipe[0] != -1)
127 close(_signal_pipe[0]);
128
129 if (_signal_pipe[1] != -1)
130 close(_signal_pipe[1]);
131
132 _signal_pipe[0] = _signal_pipe[1] = -1;
133
134 errno = saved_errno;
135 }
136
daemon_signal_next(void)137 int daemon_signal_next(void) {
138 int s;
139 ssize_t r;
140
141 if ((r = read(_signal_pipe[0], &s, sizeof(s))) == sizeof(s))
142 return s;
143
144 if (r < 0) {
145
146 if (errno == EAGAIN)
147 return 0;
148 else {
149 daemon_log(LOG_ERR, "read(signal_pipe[0], ...): %s", strerror(errno));
150 return -1;
151 }
152 }
153
154 daemon_log(LOG_ERR, "Short read() on signal pipe.");
155 return -1;
156 }
157
daemon_signal_fd(void)158 int daemon_signal_fd(void) {
159 return _signal_pipe[0];
160 }
161