blob: 906bb48f7791fc962c05b614bdf1f7028b9d675a [file] [log] [blame]
Scott James Remnant50748842006-05-16 21:02:31 +01001/* upstart
2 *
Scott James Remnant3d6bb792007-01-05 22:33:43 +00003 * Copyright © 2007 Canonical Ltd.
Scott James Remnantb6270dd2006-07-13 02:16:38 +01004 * Author: Scott James Remnant <scott@ubuntu.com>.
Scott James Remnant50748842006-05-16 21:02:31 +01005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#ifdef HAVE_CONFIG_H
22# include <config.h>
23#endif /* HAVE_CONFIG_H */
24
25
Scott James Remnantf43bdf32006-08-27 18:20:29 +010026#include <sys/types.h>
Scott James Remnantea806b72006-10-18 15:01:00 +010027#include <sys/time.h>
Scott James Remnantf43bdf32006-08-27 18:20:29 +010028#include <sys/wait.h>
29#include <sys/ioctl.h>
30#include <sys/reboot.h>
31#include <sys/resource.h>
32
Scott James Remnant3401ab72006-09-01 02:14:47 +010033#include <errno.h>
34#include <stdio.h>
Scott James Remnantf43bdf32006-08-27 18:20:29 +010035#include <signal.h>
Scott James Remnant94d00982006-08-25 15:38:22 +020036#include <stdlib.h>
Scott James Remnant3401ab72006-09-01 02:14:47 +010037#include <string.h>
Scott James Remnant12a330f2006-08-24 02:19:09 +020038#include <syslog.h>
Scott James Remnant027dd7b2006-08-21 09:01:25 +020039#include <unistd.h>
Scott James Remnantff0d26a2006-08-31 20:49:43 +010040#include <termios.h>
Scott James Remnant027dd7b2006-08-21 09:01:25 +020041
Scott James Remnant097b2a92006-09-01 19:30:37 +010042#include <linux/kd.h>
43
Scott James Remnant77e8db32006-08-21 08:47:50 +020044#include <nih/macros.h>
45#include <nih/alloc.h>
46#include <nih/list.h>
47#include <nih/timer.h>
48#include <nih/signal.h>
49#include <nih/child.h>
Scott James Remnant3401ab72006-09-01 02:14:47 +010050#include <nih/option.h>
Scott James Remnant77e8db32006-08-21 08:47:50 +020051#include <nih/main.h>
Scott James Remnant28fcc922006-09-01 04:15:57 +010052#include <nih/error.h>
Scott James Remnant77e8db32006-08-21 08:47:50 +020053#include <nih/logging.h>
54
55#include "process.h"
56#include "job.h"
57#include "event.h"
58#include "control.h"
59#include "cfgfile.h"
Scott James Remnante0d0dd12006-09-14 10:51:05 +010060#include "paths.h"
Scott James Remnant50748842006-05-16 21:02:31 +010061
62
Scott James Remnant1db88042006-09-01 03:14:19 +010063/**
64 * STATE_FD:
65 *
66 * File descriptor we read our state from.
67 **/
68#define STATE_FD 101
69
70
Scott James Remnantf43bdf32006-08-27 18:20:29 +010071/* Prototypes for static functions */
Scott James Remnant3401ab72006-09-01 02:14:47 +010072static void reset_console (void);
Scott James Remnant16a286f2007-01-10 15:38:33 +000073static void crash_handler (int signum);
Scott James Remnant3401ab72006-09-01 02:14:47 +010074static void cad_handler (void *data, NihSignal *signal);
75static void kbd_handler (void *data, NihSignal *signal);
76static void stop_handler (void *data, NihSignal *signal);
Scott James Remnant1db88042006-09-01 03:14:19 +010077static void term_handler (const char *prog, NihSignal *signal);
Scott James Remnant3401ab72006-09-01 02:14:47 +010078static void read_state (int fd);
79static void write_state (int fd);
80
Scott James Remnant1db88042006-09-01 03:14:19 +010081
Scott James Remnant3401ab72006-09-01 02:14:47 +010082/**
Scott James Remnant1db88042006-09-01 03:14:19 +010083 * restart:
Scott James Remnant3401ab72006-09-01 02:14:47 +010084 *
Scott James Remnant8b227402006-10-11 17:55:27 +010085 * This is set to TRUE if we're being re-exec'd by an existing init
Scott James Remnant1db88042006-09-01 03:14:19 +010086 * process.
Scott James Remnant3401ab72006-09-01 02:14:47 +010087 **/
Scott James Remnant1db88042006-09-01 03:14:19 +010088static int restart = FALSE;
Scott James Remnant3401ab72006-09-01 02:14:47 +010089
90
91/**
92 * options:
93 *
94 * Command-line options we accept.
95 **/
96static NihOption options[] = {
Scott James Remnant1db88042006-09-01 03:14:19 +010097 { 0, "restart", NULL, NULL, NULL, &restart, NULL },
Scott James Remnant3401ab72006-09-01 02:14:47 +010098
99 /* Ignore invalid options */
100 { '-', "--", NULL, NULL, NULL, NULL, NULL },
101
102 NIH_OPTION_LAST
103};
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100104
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100105
Scott James Remnant50748842006-05-16 21:02:31 +0100106int
107main (int argc,
108 char *argv[])
109{
Scott James Remnant3401ab72006-09-01 02:14:47 +0100110 char **args;
Scott James Remnant2e204b72007-02-03 23:15:28 +0000111 int ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100112
Scott James Remnant77e8db32006-08-21 08:47:50 +0200113 nih_main_init (argv[0]);
114
Scott James Remnant930e25a2006-10-13 12:28:05 +0100115 nih_option_set_synopsis (_("Process management daemon."));
Scott James Remnant462734c2006-10-13 13:36:00 +0100116 nih_option_set_help (
117 _("This daemon is normally executed by the kernel and given "
118 "process id 1 to denote its special status. When executed "
119 "by a user process, it will actually run /sbin/telinit."));
Scott James Remnanta6ed7eb2006-10-13 12:14:45 +0100120
Scott James Remnant3401ab72006-09-01 02:14:47 +0100121 args = nih_option_parser (NULL, argc, argv, options, FALSE);
122 if (! args)
123 exit (1);
Scott James Remnant12a330f2006-08-24 02:19:09 +0200124
Scott James Remnanta17917d2006-09-07 00:18:28 +0100125 /* Check we're root */
126 if (getuid ()) {
127 nih_error (_("Need to be root"));
128 exit (1);
129 }
130
131 /* Check we're process #1 */
132 if (getpid () > 1) {
Scott James Remnante0d0dd12006-09-14 10:51:05 +0100133 execv (TELINIT, argv);
Scott James Remnanta17917d2006-09-07 00:18:28 +0100134 /* Ignore failure, probably just that telinit doesn't exist */
135
136 nih_error (_("Not being executed as init"));
137 exit (1);
138 }
139
Scott James Remnant2e204b72007-02-03 23:15:28 +0000140 /* Open control socket */
141 if (! control_open ()) {
142 NihError *err;
143
144 err = nih_error_get ();
145 nih_error ("%s: %s", _("Unable to open control socket"),
146 err->message);
147 nih_free (err);
148
149 exit (1);
150 }
151
152 /* Read configuration */
153 if (cfg_watch_dir (CFG_DIR) < 0) {
154 NihError *err;
155
156 err = nih_error_get ();
157 nih_error ("%s: %s", _("Error parsing configuration"),
158 err->message);
159 nih_free (err);
160
161 exit (1);
162 }
163
Scott James Remnant78626fb2007-01-10 16:48:10 +0000164
165 /* Clear our arguments from the command-line, so that we show up in
166 * ps or top output as /sbin/init, with no extra flags.
167 *
168 * This is a very Linux-specific trick; by deleting the NULL
169 * terminator at the end of the last argument, we fool the kernel
170 * into believing we used a setproctitle()-a-like to extend the
171 * argument space into the environment space, and thus make it use
172 * strlen() instead of its own assumed length. In fact, we've done
173 * the exact opposite, and shrunk the command line length to just that
174 * of whatever is in argv[0].
175 *
176 * If we don't do this, and just write \0 over the rest of argv, for
177 * example; the command-line length still includes those \0s, and ps
178 * will show whitespace in their place.
179 */
180 if (argc > 1) {
Scott James Remnant505f9282007-01-10 18:44:38 +0000181 char *arg_end;
Scott James Remnant78626fb2007-01-10 16:48:10 +0000182
Scott James Remnant505f9282007-01-10 18:44:38 +0000183 arg_end = argv[argc-1] + strlen (argv[argc-1]);
184 *arg_end = ' ';
Scott James Remnant78626fb2007-01-10 16:48:10 +0000185 }
Scott James Remnant7f4db422007-01-09 20:51:08 +0000186
Scott James Remnant2e204b72007-02-03 23:15:28 +0000187 /* Become session and process group leader (should be already,
188 * but you never know what initramfs did
189 */
190 setsid ();
Scott James Remnanta17917d2006-09-07 00:18:28 +0100191
Scott James Remnant3401ab72006-09-01 02:14:47 +0100192 /* Send all logging output to syslog */
193 openlog (program_name, LOG_CONS, LOG_DAEMON);
Scott James Remnant12a330f2006-08-24 02:19:09 +0200194 nih_log_set_logger (nih_logger_syslog);
Scott James Remnant77e8db32006-08-21 08:47:50 +0200195
Scott James Remnant77e8db32006-08-21 08:47:50 +0200196 /* Close any file descriptors we inherited, and open the console
Scott James Remnant3401ab72006-09-01 02:14:47 +0100197 * device instead. Normally we reset the console, unless we're
198 * inheriting one from another init process.
Scott James Remnant77e8db32006-08-21 08:47:50 +0200199 */
Scott James Remnant2e204b72007-02-03 23:15:28 +0000200 for (int i = 0; i < 3; i++)
Scott James Remnant77e8db32006-08-21 08:47:50 +0200201 close (i);
202
Scott James Remnant5d702952007-01-05 17:21:34 +0000203 if (process_setup_console (NULL, CONSOLE_OUTPUT) < 0)
204 nih_free (nih_error_get ());
Scott James Remnant1db88042006-09-01 03:14:19 +0100205 if (! restart)
Scott James Remnant3401ab72006-09-01 02:14:47 +0100206 reset_console ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200207
Scott James Remnant2e204b72007-02-03 23:15:28 +0000208 /* Set the PATH environment variable */
209 setenv ("PATH", PATH, TRUE);
210
211
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100212 /* Reset the signal state and install the signal handler for those
213 * signals we actually want to catch; this also sets those that
214 * can be sent to us, because we're special
215 */
Scott James Remnant2e204b72007-02-03 23:15:28 +0000216 if (! restart)
217 nih_signal_reset ();
218
219 nih_signal_set_handler (SIGCHLD, nih_signal_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100220 nih_signal_set_handler (SIGALRM, nih_signal_handler);
221 nih_signal_set_handler (SIGHUP, nih_signal_handler);
Scott James Remnanteabb7802006-08-31 15:39:04 +0100222 nih_signal_set_handler (SIGTSTP, nih_signal_handler);
223 nih_signal_set_handler (SIGCONT, nih_signal_handler);
Scott James Remnant1db88042006-09-01 03:14:19 +0100224 nih_signal_set_handler (SIGTERM, nih_signal_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100225 nih_signal_set_handler (SIGINT, nih_signal_handler);
226 nih_signal_set_handler (SIGWINCH, nih_signal_handler);
Scott James Remnant16a286f2007-01-10 15:38:33 +0000227 nih_signal_set_handler (SIGSEGV, crash_handler);
228 nih_signal_set_handler (SIGABRT, crash_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100229
Scott James Remnanteabb7802006-08-31 15:39:04 +0100230 /* Ensure that we don't process events while paused */
Scott James Remnant5d702952007-01-05 17:21:34 +0000231 NIH_MUST (nih_signal_add_handler (NULL, SIGTSTP, stop_handler, NULL));
232 NIH_MUST (nih_signal_add_handler (NULL, SIGCONT, stop_handler, NULL));
Scott James Remnanteabb7802006-08-31 15:39:04 +0100233
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100234 /* Ask the kernel to send us SIGINT when control-alt-delete is
235 * pressed; generate an event with the same name.
236 */
237 reboot (RB_DISABLE_CAD);
Scott James Remnant5d702952007-01-05 17:21:34 +0000238 NIH_MUST (nih_signal_add_handler (NULL, SIGINT, cad_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100239
240 /* Ask the kernel to send us SIGWINCH when alt-uparrow is pressed;
241 * generate a kbdrequest event.
242 */
Scott James Remnant5d702952007-01-05 17:21:34 +0000243 if (ioctl (0, KDSIGACCEPT, SIGWINCH) == 0)
244 NIH_MUST (nih_signal_add_handler (NULL, SIGWINCH,
245 kbd_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100246
Scott James Remnant1db88042006-09-01 03:14:19 +0100247 /* SIGTERM instructs us to re-exec ourselves */
Scott James Remnant5d702952007-01-05 17:21:34 +0000248 NIH_MUST (nih_signal_add_handler (NULL, SIGTERM,
249 (NihSignalHandler)term_handler,
250 argv[0]));
Scott James Remnant1db88042006-09-01 03:14:19 +0100251
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100252 /* Reap all children that die */
Scott James Remnant5d702952007-01-05 17:21:34 +0000253 NIH_MUST (nih_child_add_watch (NULL, -1, job_child_reaper, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100254
Scott James Remnantdc8877d2006-08-29 16:56:48 +0100255 /* Process the event queue and check the jobs for idleness
256 * every time through the main loop */
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000257 NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)event_poll,
Scott James Remnant5d702952007-01-05 17:21:34 +0000258 NULL));
259 NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)job_detect_idle,
260 NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100261
Scott James Remnant94d00982006-08-25 15:38:22 +0200262
Scott James Remnant3401ab72006-09-01 02:14:47 +0100263 /* Generate and run the startup event or read the state from the
264 * init daemon that exec'd us
265 */
Scott James Remnant1db88042006-09-01 03:14:19 +0100266 if (! restart) {
Scott James Remnantb4a1c2b2006-09-09 01:28:26 +0100267 Job *logd;
268
269 /* FIXME this is a bit of a hack, should have a list of
270 * essential services or something
271 */
272 logd = job_find_by_name ("logd");
Scott James Remnant515b2b92006-09-09 03:41:27 +0100273 if (logd) {
Scott James Remnantb4a1c2b2006-09-09 01:28:26 +0100274 job_start (logd);
Scott James Remnant515b2b92006-09-09 03:41:27 +0100275 if (logd->state == JOB_RUNNING) {
276 /* Hang around until logd signals that it's
277 * listening ... but not too long
278 */
279 alarm (5);
280 waitpid (logd->pid, NULL, WUNTRACED);
281 kill (logd->pid, SIGCONT);
282 alarm (0);
283 }
284 }
Scott James Remnantb4a1c2b2006-09-09 01:28:26 +0100285
Scott James Remnantea204d42006-09-07 00:30:53 +0100286 event_queue (STARTUP_EVENT);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100287 } else {
Scott James Remnant1db88042006-09-01 03:14:19 +0100288 sigset_t mask;
289
290 /* State file descriptor is fixed */
291 read_state (STATE_FD);
292
293 /* We're ok to receive signals again */
294 sigemptyset (&mask);
295 sigprocmask (SIG_SETMASK, &mask, NULL);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100296 }
297
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000298 /* Run through the loop at least once */
299 nih_main_loop_interrupt ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200300 ret = nih_main_loop ();
301
302 return ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100303}
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100304
305
306/**
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100307 * reset_console:
308 *
309 * Set up the console flags to something sensible. Cribbed from sysvinit,
310 * initng, etc.
311 **/
312static void
313reset_console (void)
314{
Scott James Remnant4bc5fe32006-08-31 21:48:33 +0100315 struct termios tty;
316
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100317 tcgetattr (0, &tty);
318
319 tty.c_cflag &= (CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD);
320 tty.c_cflag |= (HUPCL | CLOCAL | CREAD);
321
322 /* Set up usual keys */
323 tty.c_cc[VINTR] = 3; /* ^C */
324 tty.c_cc[VQUIT] = 28; /* ^\ */
325 tty.c_cc[VERASE] = 127;
326 tty.c_cc[VKILL] = 24; /* ^X */
327 tty.c_cc[VEOF] = 4; /* ^D */
328 tty.c_cc[VTIME] = 0;
329 tty.c_cc[VMIN] = 1;
330 tty.c_cc[VSTART] = 17; /* ^Q */
331 tty.c_cc[VSTOP] = 19; /* ^S */
332 tty.c_cc[VSUSP] = 26; /* ^Z */
333
334 /* Pre and post processing */
335 tty.c_iflag = (IGNPAR | ICRNL | IXON | IXANY);
336 tty.c_oflag = (OPOST | ONLCR);
337 tty.c_lflag = (ISIG | ICANON | ECHO | ECHOCTL | ECHOPRT | ECHOKE);
338
339 /* Set the terminal line and flush it */
340 tcsetattr (0, TCSANOW, &tty);
341 tcflush (0, TCIOFLUSH);
342}
343
344
345/**
Scott James Remnant16a286f2007-01-10 15:38:33 +0000346 * crash_handler:
Scott James Remnant8b227402006-10-11 17:55:27 +0100347 * @signum: signal number received.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100348 *
Scott James Remnant16a286f2007-01-10 15:38:33 +0000349 * Handle receiving the SEGV or ABRT signal, usually caused by one of
350 * our own mistakes. We deal with it by dumping core in a child process
351 * and just carrying on in the parent.
352 *
353 * This may or may not work, but the only alternative would be sigjmp()ing
354 * to somewhere "safe" leaving inconsistent state everywhere (like dangling
355 * lists pointers) or exec'ing another process (which we couldn't transfer
356 * our state to anyway). This just hopes that the kernel resumes on the
357 * next instruction.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100358 **/
359static void
Scott James Remnant16a286f2007-01-10 15:38:33 +0000360crash_handler (int signum)
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100361{
362 pid_t pid;
363
364 pid = fork ();
365 if (pid == 0) {
366 struct sigaction act;
367 struct rlimit limit;
368 sigset_t mask;
369
370 /* Mask out all signals */
371 sigfillset (&mask);
372 sigprocmask (SIG_SETMASK, &mask, NULL);
373
Scott James Remnant16a286f2007-01-10 15:38:33 +0000374 /* Set the handler to the default so core is dumped */
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100375 act.sa_handler = SIG_DFL;
376 act.sa_flags = 0;
377 sigemptyset (&act.sa_mask);
Scott James Remnant16a286f2007-01-10 15:38:33 +0000378 sigaction (signum, &act, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100379
380 /* Dump in the root directory */
381 chdir ("/");
382
383 /* Don't limit the core dump size */
384 limit.rlim_cur = RLIM_INFINITY;
385 limit.rlim_max = RLIM_INFINITY;
386 setrlimit (RLIMIT_CORE, &limit);
387
Scott James Remnant16a286f2007-01-10 15:38:33 +0000388 /* Raise the signal again */
389 raise (signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100390
391 /* Unmask so that we receive it */
Scott James Remnant16a286f2007-01-10 15:38:33 +0000392 sigdelset (&mask, signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100393 sigprocmask (SIG_SETMASK, &mask, NULL);
394
395 /* Wait for death */
396 pause ();
397 exit (0);
398 } else if (pid > 0) {
399 /* Wait for the core to be generated */
400 waitpid (pid, NULL, 0);
401
Scott James Remnant16a286f2007-01-10 15:38:33 +0000402 nih_error (_("Caught %s, core dumped"),
Scott James Remnantde443012007-01-10 18:45:40 +0000403 (signum == SIGSEGV
404 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100405 } else {
Scott James Remnant16a286f2007-01-10 15:38:33 +0000406 nih_error (_("Caught %s, unable to dump core"),
Scott James Remnantde443012007-01-10 18:45:40 +0000407 (signum == SIGSEGV
408 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100409 }
410}
411
412/**
413 * cad_handler:
414 * @data: unused,
415 * @signal: signal that called this handler.
416 *
417 * Handle having recieved the SIGINT signal, sent to us when somebody
418 * presses Ctrl-Alt-Delete on the console. We just generate a
Scott James Remnantbb3cc3f2006-09-08 17:17:47 +0100419 * ctrlaltdel event.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100420 **/
421static void
422cad_handler (void *data,
423 NihSignal *signal)
424{
Scott James Remnantbb3cc3f2006-09-08 17:17:47 +0100425 event_queue (CTRLALTDEL_EVENT);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100426}
427
428/**
429 * kbd_handler:
430 * @data: unused,
431 * @signal: signal that called this handler.
432 *
433 * Handle having recieved the SIGWINCH signal, sent to us when somebody
434 * presses Alt-UpArrow on the console. We just generate a
435 * kbdrequest event.
436 **/
437static void
438kbd_handler (void *data,
439 NihSignal *signal)
440{
Scott James Remnantbb3cc3f2006-09-08 17:17:47 +0100441 event_queue (KBDREQUEST_EVENT);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100442}
Scott James Remnanteabb7802006-08-31 15:39:04 +0100443
444/**
445 * stop_handler:
446 * @data: unused,
447 * @signal: signal caught.
448 *
449 * This is called when we receive the STOP, TSTP or CONT signals; we
450 * adjust the paused variable appropriately so that the event queue and
451 * job idle detection is not run.
452 **/
453static void
454stop_handler (void *data,
455 NihSignal *signal)
456{
457 nih_assert (signal != NULL);
458
459 if (signal->signum == SIGCONT) {
460 nih_info (_("Event queue resumed"));
461 paused = FALSE;
462 } else {
463 nih_info (_("Event queue paused"));
464 paused = TRUE;
465 }
466}
Scott James Remnant3401ab72006-09-01 02:14:47 +0100467
468
469/**
Scott James Remnant1db88042006-09-01 03:14:19 +0100470 * term_handler:
Scott James Remnant3401ab72006-09-01 02:14:47 +0100471 * @argv0: program to run,
472 * @signal: signal caught.
473 *
Scott James Remnant1db88042006-09-01 03:14:19 +0100474 * This is called when we receive the TERM signal, which instructs us
Scott James Remnant3401ab72006-09-01 02:14:47 +0100475 * to reexec ourselves.
476 **/
477static void
Scott James Remnant1db88042006-09-01 03:14:19 +0100478term_handler (const char *argv0,
Scott James Remnant3401ab72006-09-01 02:14:47 +0100479 NihSignal *signal)
480{
Scott James Remnant6f464962006-09-01 04:10:20 +0100481 NihError *err;
Scott James Remnant3401ab72006-09-01 02:14:47 +0100482 sigset_t mask, oldmask;
483 int fds[2] = { -1, -1 };
484 pid_t pid;
Scott James Remnant3401ab72006-09-01 02:14:47 +0100485
486 nih_assert (argv0 != NULL);
487 nih_assert (signal != NULL);
488
489 nih_warn (_("Re-executing %s"), argv0);
490
Scott James Remnant1db88042006-09-01 03:14:19 +0100491 /* Block signals while we work. We're the last signal handler
492 * installed so this should mean that they're all handled now.
493 *
494 * The child must make sure that it unblocks these again when
495 * it's ready.
496 */
Scott James Remnant3401ab72006-09-01 02:14:47 +0100497 sigfillset (&mask);
498 sigprocmask (SIG_BLOCK, &mask, &oldmask);
499
500 /* Create pipe */
Scott James Remnant6f464962006-09-01 04:10:20 +0100501 if (pipe (fds) < 0) {
502 nih_error_raise_system ();
Scott James Remnant3401ab72006-09-01 02:14:47 +0100503 goto error;
Scott James Remnant6f464962006-09-01 04:10:20 +0100504 }
Scott James Remnant3401ab72006-09-01 02:14:47 +0100505
506 /* Fork a child that can send the state to the new init process */
507 pid = fork ();
508 if (pid < 0) {
Scott James Remnant6f464962006-09-01 04:10:20 +0100509 nih_error_raise_system ();
Scott James Remnant3401ab72006-09-01 02:14:47 +0100510 goto error;
511 } else if (pid == 0) {
Scott James Remnant6f464962006-09-01 04:10:20 +0100512 close (fds[0]);
513
Scott James Remnant2e204b72007-02-03 23:15:28 +0000514 /* Close the control socket so the new init process won't
515 * get EADDRINUSE when it tries to open it.
516 */
517 control_close ();
518 write (fds[1], "\n", 1);
519
Scott James Remnant6f464962006-09-01 04:10:20 +0100520 write_state (fds[1]);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100521 exit (0);
522 } else {
Scott James Remnant2e204b72007-02-03 23:15:28 +0000523 char buf[1];
524
525 /* Make sure the child is ready to send its state */
526 read (fds[0], buf, sizeof (buf));
527
Scott James Remnant6f464962006-09-01 04:10:20 +0100528 if (dup2 (fds[0], STATE_FD) < 0) {
529 nih_error_raise_system ();
530 goto error;
531 }
532
533 close (fds[0]);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100534 close (fds[1]);
Scott James Remnant6f464962006-09-01 04:10:20 +0100535 fds[0] = fds[1] = -1;
Scott James Remnant3401ab72006-09-01 02:14:47 +0100536 }
537
538 /* Argument list */
Scott James Remnant1db88042006-09-01 03:14:19 +0100539 execl (argv0, argv0, "--restart", NULL);
Scott James Remnant6f464962006-09-01 04:10:20 +0100540 nih_error_raise_system ();
Scott James Remnant3401ab72006-09-01 02:14:47 +0100541
542error:
Scott James Remnant6f464962006-09-01 04:10:20 +0100543 err = nih_error_get ();
544 nih_error (_("Failed to re-execute %s: %s"), argv0, err->message);
Scott James Remnant28fcc922006-09-01 04:15:57 +0100545 nih_free (err);
Scott James Remnant6f464962006-09-01 04:10:20 +0100546
Scott James Remnant3401ab72006-09-01 02:14:47 +0100547 close (fds[0]);
Scott James Remnant6f464962006-09-01 04:10:20 +0100548 close (fds[1]);
549
Scott James Remnant3401ab72006-09-01 02:14:47 +0100550 sigprocmask (SIG_SETMASK, &oldmask, NULL);
551}
552
553/**
Scott James Remnant3401ab72006-09-01 02:14:47 +0100554 * read_state:
555 * @fd: file descriptor to read from.
556 *
557 * Read event and job state from @fd, which is a trivial line-based
558 * protocol that we can keep the same without too much difficultly. It's
559 * tempting to use the control sockets for this, but they break too often.
560 **/
561static void
562read_state (int fd)
563{
564 Job *job = NULL;
565 Event *event = NULL;
566 FILE *state;
567 char buf[80];
568
569 nih_debug ("Reading state");
570
571 /* Use stdio as it's a light-weight thing that won't change */
572 state = fdopen (fd, "r");
573 if (! state) {
574 nih_warn (_("Unable to read from state descriptor: %s"),
575 strerror (errno));
576 return;
577 }
578
579 /* It's just a series of simple lines; if one begins Job then it
580 * indicates the start of a Job description, otherwise if it
581 * begins Event then it's the start of an Event description.
582 *
583 * Lines beginning "." are assumed to belong to the current job
584 * or event.
585 */
586 while (fgets (buf, sizeof (buf), state)) {
587 char *ptr;
588
589 /* Strip newline */
590 ptr = strchr (buf, '\n');
591 if (ptr)
592 *ptr = '\0';
593
594 if (! strncmp (buf, "Job ", 4)) {
595 job = job_read_state (NULL, buf);
596 event = NULL;
597 } else if (! strncmp (buf, "Event ", 5)) {
598 event = event_read_state (NULL, buf);
599 job = NULL;
600 } else if (buf[0] == '.') {
601 if (job) {
602 job = job_read_state (job, buf);
603 } else if (event) {
604 event = event_read_state (event, buf);
605 }
606 } else {
607 event = NULL;
608 job = NULL;
609 }
610 }
611
612 if (fclose (state))
613 nih_warn (_("Error after reading state: %s"),
614 strerror (errno));
615
616 nih_debug ("State read from parent");
617}
618
619/**
620 * write_state:
621 * @fd: file descriptor to write to.
622 *
623 * Write event and job state to @fd, which is a trivial line-based
624 * protocol that we can keep the same without too much difficultly. It's
625 * tempting to use the control sockets for this, but they break too often.
626 **/
627static void
628write_state (int fd)
629{
630 FILE *state;
631
632 /* Use stdio as it's a light-weight thing that won't change */
633 state = fdopen (fd, "w");
634 if (! state) {
635 nih_warn (_("Unable to write to state descriptor: %s"),
636 strerror (errno));
637 return;
638 }
639
640 event_write_state (state);
641 job_write_state (state);
642
643 if (fclose (state))
644 nih_warn (_("Error after writing state: %s"),
645 strerror (errno));
646}