1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
/**
* Copyright © 2015 Mattias Andrée <maandree@member.fsf.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "daemon.h"
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <stdint.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <sys/timerfd.h>
/**
* Macro that adds the common beginning of for
* all daemon pathnames to a string literal.
*
* @param name:string literal The unique part of the pathname.
*/
#define DAEMON_IMAGE(name) LIBEXECDIR "/" PACKAGE "/satd-" name
/**
* The value on `timer_pid` when there is not "timer" image running.
*/
#define NO_TIMER_SPAWED -2 /* Must be negative, but not -1. */
/**
* Signal that has been received, 0 if none.
*/
static volatile sig_atomic_t received_signo = SIGCHLD; /* Forces the "timer" image to run. */
/**
* The PID the "timer" fork, `NO_TIMER_SPAWED` if not running.
*
* It does not matter that this is lost on a successful SIGHUP.
*/
static volatile pid_t timer_pid = NO_TIMER_SPAWED;
/**
* Invoked when a signal is received.
*
* @param int The signal.
*/
static void
sighandler(int signo)
{
int saved_errno = errno;
if ((signo == SIGCHLD) && (waitpid(-1, NULL, WNOHANG) == timer_pid))
timer_pid = NO_TIMER_SPAWED;
else
received_signo = (sig_atomic_t)signo;
errno = saved_errno;
}
/**
* Spawn a libexec.
*
* @param command The command to spawn, -1 for "timer".
* @param fd File descriptor to the socket, -1 iff `command` is -1.
* @param argv `argv` from `main`.
* @param envp `envp` from `main`.
* @return 0 on success, -1 on error.
*/
static int
spawn(int command, int fd, char *argv[], char *envp[])
{
const char *image;
pid_t pid;
fork_again:
switch ((pid = fork())) {
case -1:
if (errno != EAGAIN)
return -1;
(void) sleep(1); /* Possibly shorter because of SIGCHLD. */
goto fork_again;
case 0:
switch (command) {
case SAT_QUEUE: image = DAEMON_IMAGE("add"); break;
case SAT_REMOVE: image = DAEMON_IMAGE("rm"); break;
case SAT_PRINT: image = DAEMON_IMAGE("list"); break;
case SAT_RUN: image = DAEMON_IMAGE("run"); break;
case -1: image = DAEMON_IMAGE("timer"); break;
default:
fprintf(stderr, "%s: invalid command received.\n", argv[0]);
goto child_fail;
}
if (command < 0) {
close(SOCK_FILENO);
execve(image, argv, envp);
} else {
close(BOOT_FILENO), close(REAL_FILENO);
if (dup2(fd, SOCK_FILENO) != -1)
close(fd), fd = SOCK_FILENO, execve(image, argv, envp);
}
perror(argv[0]);
child_fail:
close(fd);
exit(1);
default:
if (command < 0)
timer_pid = pid;
return 0;
}
}
/**
* Determine whether a timer is set.
*
* @param fd The file descriptor of the timer.
* @return 1 if set, 0 if unset, -1 on error.
*/
static int
is_timer_set(int fd)
{
struct itimerspec spec;
if (timerfd_gettime(fd, &spec))
return -1;
return (spec.it_interval.tv_sec || spec.it_value.tv_sec ||
spec.it_interval.tv_nsec || spec.it_value.tv_nsec);
}
/**
* The sat daemon.
*
* @param argc Should be 3.
* @param argv The name of the process, the pathname of the socket,
* and the pathname to the state file.
* @param envp The environment.
* @return 0 The process was successful.
* @return 1 The process failed queuing the job.
*/
int
main(int argc, char *argv[], char *envp[])
{
int fd = -1, rc = 0, accepted = 0, r;
unsigned char type;
fd_set fdset;
int64_t _overrun;
/* Set up signal handlers. */
t (signal(SIGHUP, sighandler) == SIG_ERR);
t (signal(SIGCHLD, sighandler) == SIG_ERR);
/* The magnificent loop. */
again:
if (accepted && (timer_pid == NO_TIMER_SPAWED)) {
t (r = is_timer_set(BOOT_FILENO), r < 0); if (r) goto not_done;
t (r = is_timer_set(REAL_FILENO), r < 0); if (r) goto not_done;
goto done;
}
not_done:
if (received_signo == SIGHUP) {
execve(DAEMON_IMAGE("diminished"), argv, envp);
perror(argv[0]);
} else if (received_signo == SIGCHLD) {
t (spawn(-1, -1, argv, envp));
}
received_signo = 0;
FD_ZERO(&fdset);
FD_SET(SOCK_FILENO, &fdset);
FD_SET(BOOT_FILENO, &fdset);
FD_SET(REAL_FILENO, &fdset); /* This is the highest one. */
if (select(REAL_FILENO + 1, &fdset, NULL, NULL, NULL) == -1) {
t (errno != EINTR);
}
if (FD_ISSET(BOOT_FILENO, &fdset)) t (read(BOOT_FILENO, &_overrun, (size_t)8) < 8);
if (FD_ISSET(REAL_FILENO, &fdset)) t (read(REAL_FILENO, &_overrun, (size_t)8) < 8);
if (!FD_ISSET(SOCK_FILENO, &fdset))
goto again;
if (fd = accept(SOCK_FILENO, NULL, NULL), fd == -1) {
switch (errno) {
case ECONNABORTED:
case EINTR:
goto again;
default:
/* Including EMFILE, ENFILE, and ENOMEM
* because of potential resource leak. */
goto fail;
}
}
accepted = 1;
if (read(fd, &type, sizeof(type)) <= 0)
perror(argv[0]);
else
t (spawn((int)type, fd, argv, envp));
close(fd), fd = -1;
goto again;
fail:
perror(argv[0]);
if (fd >= 0)
close(fd);
rc = 1;
done:
while (waitpid(-1, NULL, 0) > 0);
unlink(argv[1]);
if (!rc)
unlink(argv[2]);
close(SOCK_FILENO);
close(STATE_FILENO);
return rc;
(void) argc;
}
|