blob: e58df405193264f523339b3759a8eff134121c78 [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;
111 int ret, i;
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 Remnant7f4db422007-01-09 20:51:08 +0000140 /* Clear arguments, so we just show up as init */
141 for (i = 1; i < argc; i++)
142 memset (argv[i], '\0', strlen (argv[i]));
143
Scott James Remnanta17917d2006-09-07 00:18:28 +0100144
Scott James Remnant3401ab72006-09-01 02:14:47 +0100145 /* Send all logging output to syslog */
146 openlog (program_name, LOG_CONS, LOG_DAEMON);
Scott James Remnant12a330f2006-08-24 02:19:09 +0200147 nih_log_set_logger (nih_logger_syslog);
Scott James Remnant77e8db32006-08-21 08:47:50 +0200148
Scott James Remnant77e8db32006-08-21 08:47:50 +0200149 /* Close any file descriptors we inherited, and open the console
Scott James Remnant3401ab72006-09-01 02:14:47 +0100150 * device instead. Normally we reset the console, unless we're
151 * inheriting one from another init process.
Scott James Remnant77e8db32006-08-21 08:47:50 +0200152 */
153 for (i = 0; i < 3; i++)
154 close (i);
155
Scott James Remnant5d702952007-01-05 17:21:34 +0000156 if (process_setup_console (NULL, CONSOLE_OUTPUT) < 0)
157 nih_free (nih_error_get ());
Scott James Remnant1db88042006-09-01 03:14:19 +0100158 if (! restart)
Scott James Remnant3401ab72006-09-01 02:14:47 +0100159 reset_console ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200160
Scott James Remnantf4970812006-08-27 18:27:19 +0100161
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100162 /* Reset the signal state and install the signal handler for those
163 * signals we actually want to catch; this also sets those that
164 * can be sent to us, because we're special
165 */
166 nih_signal_reset ();
167 nih_signal_set_handler (SIGALRM, nih_signal_handler);
168 nih_signal_set_handler (SIGHUP, nih_signal_handler);
Scott James Remnanteabb7802006-08-31 15:39:04 +0100169 nih_signal_set_handler (SIGTSTP, nih_signal_handler);
170 nih_signal_set_handler (SIGCONT, nih_signal_handler);
Scott James Remnant1db88042006-09-01 03:14:19 +0100171 nih_signal_set_handler (SIGTERM, nih_signal_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100172 nih_signal_set_handler (SIGINT, nih_signal_handler);
173 nih_signal_set_handler (SIGWINCH, nih_signal_handler);
Scott James Remnant16a286f2007-01-10 15:38:33 +0000174 nih_signal_set_handler (SIGSEGV, crash_handler);
175 nih_signal_set_handler (SIGABRT, crash_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100176
Scott James Remnanteabb7802006-08-31 15:39:04 +0100177 /* Ensure that we don't process events while paused */
Scott James Remnant5d702952007-01-05 17:21:34 +0000178 NIH_MUST (nih_signal_add_handler (NULL, SIGTSTP, stop_handler, NULL));
179 NIH_MUST (nih_signal_add_handler (NULL, SIGCONT, stop_handler, NULL));
Scott James Remnanteabb7802006-08-31 15:39:04 +0100180
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100181 /* Ask the kernel to send us SIGINT when control-alt-delete is
182 * pressed; generate an event with the same name.
183 */
184 reboot (RB_DISABLE_CAD);
Scott James Remnant5d702952007-01-05 17:21:34 +0000185 NIH_MUST (nih_signal_add_handler (NULL, SIGINT, cad_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100186
187 /* Ask the kernel to send us SIGWINCH when alt-uparrow is pressed;
188 * generate a kbdrequest event.
189 */
Scott James Remnant5d702952007-01-05 17:21:34 +0000190 if (ioctl (0, KDSIGACCEPT, SIGWINCH) == 0)
191 NIH_MUST (nih_signal_add_handler (NULL, SIGWINCH,
192 kbd_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100193
Scott James Remnant1db88042006-09-01 03:14:19 +0100194 /* SIGTERM instructs us to re-exec ourselves */
Scott James Remnant5d702952007-01-05 17:21:34 +0000195 NIH_MUST (nih_signal_add_handler (NULL, SIGTERM,
196 (NihSignalHandler)term_handler,
197 argv[0]));
Scott James Remnant1db88042006-09-01 03:14:19 +0100198
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100199
200 /* Reap all children that die */
Scott James Remnant5d702952007-01-05 17:21:34 +0000201 NIH_MUST (nih_child_add_watch (NULL, -1, job_child_reaper, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100202
Scott James Remnantdc8877d2006-08-29 16:56:48 +0100203 /* Process the event queue and check the jobs for idleness
204 * every time through the main loop */
Scott James Remnant5d702952007-01-05 17:21:34 +0000205 NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)event_queue_run,
206 NULL));
207 NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)job_detect_idle,
208 NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100209
Scott James Remnant94d00982006-08-25 15:38:22 +0200210
Scott James Remnant77e8db32006-08-21 08:47:50 +0200211 /* Become session and process group leader (should be already,
212 * but you never know what initramfs did
213 */
214 setsid ();
215
216 /* Open control socket */
Scott James Remnant5d702952007-01-05 17:21:34 +0000217 if (! control_open ()) {
218 NihError *err;
219
220 err = nih_error_get ();
221 nih_error ("%s: %s", _("Unable to open control socket"),
222 err->message);
223 nih_free (err);
224 }
Scott James Remnant77e8db32006-08-21 08:47:50 +0200225
Scott James Remnantceaaf4d2006-08-24 01:40:10 +0200226 /* Read configuration */
Scott James Remnant5d702952007-01-05 17:21:34 +0000227 if (cfg_watch_dir (NULL, CFG_DIR, NULL) < 0) {
228 NihError *err;
229
230 err = nih_error_get ();
231 nih_error ("%s: %s",
232 _("Unable to watch configuration directory"),
233 err->message);
234 nih_free (err);
235 }
Scott James Remnantceaaf4d2006-08-24 01:40:10 +0200236
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100237 /* Set the PATH environment variable */
238 setenv ("PATH", PATH, TRUE);
239
Scott James Remnant77e8db32006-08-21 08:47:50 +0200240
Scott James Remnant3401ab72006-09-01 02:14:47 +0100241 /* Generate and run the startup event or read the state from the
242 * init daemon that exec'd us
243 */
Scott James Remnant1db88042006-09-01 03:14:19 +0100244 if (! restart) {
Scott James Remnantb4a1c2b2006-09-09 01:28:26 +0100245 Job *logd;
246
247 /* FIXME this is a bit of a hack, should have a list of
248 * essential services or something
249 */
250 logd = job_find_by_name ("logd");
Scott James Remnant515b2b92006-09-09 03:41:27 +0100251 if (logd) {
Scott James Remnantb4a1c2b2006-09-09 01:28:26 +0100252 job_start (logd);
Scott James Remnant515b2b92006-09-09 03:41:27 +0100253 if (logd->state == JOB_RUNNING) {
254 /* Hang around until logd signals that it's
255 * listening ... but not too long
256 */
257 alarm (5);
258 waitpid (logd->pid, NULL, WUNTRACED);
259 kill (logd->pid, SIGCONT);
260 alarm (0);
261 }
262 }
Scott James Remnantb4a1c2b2006-09-09 01:28:26 +0100263
Scott James Remnantea204d42006-09-07 00:30:53 +0100264 event_queue (STARTUP_EVENT);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100265 } else {
Scott James Remnant1db88042006-09-01 03:14:19 +0100266 sigset_t mask;
267
268 /* State file descriptor is fixed */
269 read_state (STATE_FD);
270
271 /* We're ok to receive signals again */
272 sigemptyset (&mask);
273 sigprocmask (SIG_SETMASK, &mask, NULL);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100274 }
275
276 /* Run the event queue once, and detect anything idle */
Scott James Remnant0c7e72a2006-08-27 22:22:53 +0100277 event_queue_run ();
Scott James Remnant32de9222006-08-31 04:34:27 +0100278 job_detect_idle ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200279
280 /* Go! */
281 ret = nih_main_loop ();
282
283 return ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100284}
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100285
286
287/**
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100288 * reset_console:
289 *
290 * Set up the console flags to something sensible. Cribbed from sysvinit,
291 * initng, etc.
292 **/
293static void
294reset_console (void)
295{
Scott James Remnant4bc5fe32006-08-31 21:48:33 +0100296 struct termios tty;
297
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100298 tcgetattr (0, &tty);
299
300 tty.c_cflag &= (CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD);
301 tty.c_cflag |= (HUPCL | CLOCAL | CREAD);
302
303 /* Set up usual keys */
304 tty.c_cc[VINTR] = 3; /* ^C */
305 tty.c_cc[VQUIT] = 28; /* ^\ */
306 tty.c_cc[VERASE] = 127;
307 tty.c_cc[VKILL] = 24; /* ^X */
308 tty.c_cc[VEOF] = 4; /* ^D */
309 tty.c_cc[VTIME] = 0;
310 tty.c_cc[VMIN] = 1;
311 tty.c_cc[VSTART] = 17; /* ^Q */
312 tty.c_cc[VSTOP] = 19; /* ^S */
313 tty.c_cc[VSUSP] = 26; /* ^Z */
314
315 /* Pre and post processing */
316 tty.c_iflag = (IGNPAR | ICRNL | IXON | IXANY);
317 tty.c_oflag = (OPOST | ONLCR);
318 tty.c_lflag = (ISIG | ICANON | ECHO | ECHOCTL | ECHOPRT | ECHOKE);
319
320 /* Set the terminal line and flush it */
321 tcsetattr (0, TCSANOW, &tty);
322 tcflush (0, TCIOFLUSH);
323}
324
325
326/**
Scott James Remnant16a286f2007-01-10 15:38:33 +0000327 * crash_handler:
Scott James Remnant8b227402006-10-11 17:55:27 +0100328 * @signum: signal number received.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100329 *
Scott James Remnant16a286f2007-01-10 15:38:33 +0000330 * Handle receiving the SEGV or ABRT signal, usually caused by one of
331 * our own mistakes. We deal with it by dumping core in a child process
332 * and just carrying on in the parent.
333 *
334 * This may or may not work, but the only alternative would be sigjmp()ing
335 * to somewhere "safe" leaving inconsistent state everywhere (like dangling
336 * lists pointers) or exec'ing another process (which we couldn't transfer
337 * our state to anyway). This just hopes that the kernel resumes on the
338 * next instruction.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100339 **/
340static void
Scott James Remnant16a286f2007-01-10 15:38:33 +0000341crash_handler (int signum)
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100342{
343 pid_t pid;
344
345 pid = fork ();
346 if (pid == 0) {
347 struct sigaction act;
348 struct rlimit limit;
349 sigset_t mask;
350
351 /* Mask out all signals */
352 sigfillset (&mask);
353 sigprocmask (SIG_SETMASK, &mask, NULL);
354
Scott James Remnant16a286f2007-01-10 15:38:33 +0000355 /* Set the handler to the default so core is dumped */
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100356 act.sa_handler = SIG_DFL;
357 act.sa_flags = 0;
358 sigemptyset (&act.sa_mask);
Scott James Remnant16a286f2007-01-10 15:38:33 +0000359 sigaction (signum, &act, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100360
361 /* Dump in the root directory */
362 chdir ("/");
363
364 /* Don't limit the core dump size */
365 limit.rlim_cur = RLIM_INFINITY;
366 limit.rlim_max = RLIM_INFINITY;
367 setrlimit (RLIMIT_CORE, &limit);
368
Scott James Remnant16a286f2007-01-10 15:38:33 +0000369 /* Raise the signal again */
370 raise (signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100371
372 /* Unmask so that we receive it */
Scott James Remnant16a286f2007-01-10 15:38:33 +0000373 sigdelset (&mask, signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100374 sigprocmask (SIG_SETMASK, &mask, NULL);
375
376 /* Wait for death */
377 pause ();
378 exit (0);
379 } else if (pid > 0) {
380 /* Wait for the core to be generated */
381 waitpid (pid, NULL, 0);
382
Scott James Remnant16a286f2007-01-10 15:38:33 +0000383 nih_error (_("Caught %s, core dumped"),
384 (signum == SEGV ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100385 } else {
Scott James Remnant16a286f2007-01-10 15:38:33 +0000386 nih_error (_("Caught %s, unable to dump core"),
387 (signum == SEGV ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100388 }
389}
390
391/**
392 * cad_handler:
393 * @data: unused,
394 * @signal: signal that called this handler.
395 *
396 * Handle having recieved the SIGINT signal, sent to us when somebody
397 * presses Ctrl-Alt-Delete on the console. We just generate a
Scott James Remnantbb3cc3f2006-09-08 17:17:47 +0100398 * ctrlaltdel event.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100399 **/
400static void
401cad_handler (void *data,
402 NihSignal *signal)
403{
Scott James Remnantbb3cc3f2006-09-08 17:17:47 +0100404 event_queue (CTRLALTDEL_EVENT);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100405}
406
407/**
408 * kbd_handler:
409 * @data: unused,
410 * @signal: signal that called this handler.
411 *
412 * Handle having recieved the SIGWINCH signal, sent to us when somebody
413 * presses Alt-UpArrow on the console. We just generate a
414 * kbdrequest event.
415 **/
416static void
417kbd_handler (void *data,
418 NihSignal *signal)
419{
Scott James Remnantbb3cc3f2006-09-08 17:17:47 +0100420 event_queue (KBDREQUEST_EVENT);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100421}
Scott James Remnanteabb7802006-08-31 15:39:04 +0100422
423/**
424 * stop_handler:
425 * @data: unused,
426 * @signal: signal caught.
427 *
428 * This is called when we receive the STOP, TSTP or CONT signals; we
429 * adjust the paused variable appropriately so that the event queue and
430 * job idle detection is not run.
431 **/
432static void
433stop_handler (void *data,
434 NihSignal *signal)
435{
436 nih_assert (signal != NULL);
437
438 if (signal->signum == SIGCONT) {
439 nih_info (_("Event queue resumed"));
440 paused = FALSE;
441 } else {
442 nih_info (_("Event queue paused"));
443 paused = TRUE;
444 }
445}
Scott James Remnant3401ab72006-09-01 02:14:47 +0100446
447
448/**
Scott James Remnant1db88042006-09-01 03:14:19 +0100449 * term_handler:
Scott James Remnant3401ab72006-09-01 02:14:47 +0100450 * @argv0: program to run,
451 * @signal: signal caught.
452 *
Scott James Remnant1db88042006-09-01 03:14:19 +0100453 * This is called when we receive the TERM signal, which instructs us
Scott James Remnant3401ab72006-09-01 02:14:47 +0100454 * to reexec ourselves.
455 **/
456static void
Scott James Remnant1db88042006-09-01 03:14:19 +0100457term_handler (const char *argv0,
Scott James Remnant3401ab72006-09-01 02:14:47 +0100458 NihSignal *signal)
459{
Scott James Remnant6f464962006-09-01 04:10:20 +0100460 NihError *err;
Scott James Remnant3401ab72006-09-01 02:14:47 +0100461 sigset_t mask, oldmask;
462 int fds[2] = { -1, -1 };
463 pid_t pid;
Scott James Remnant3401ab72006-09-01 02:14:47 +0100464
465 nih_assert (argv0 != NULL);
466 nih_assert (signal != NULL);
467
468 nih_warn (_("Re-executing %s"), argv0);
469
Scott James Remnant1db88042006-09-01 03:14:19 +0100470 /* Block signals while we work. We're the last signal handler
471 * installed so this should mean that they're all handled now.
472 *
473 * The child must make sure that it unblocks these again when
474 * it's ready.
475 */
Scott James Remnant3401ab72006-09-01 02:14:47 +0100476 sigfillset (&mask);
477 sigprocmask (SIG_BLOCK, &mask, &oldmask);
478
Scott James Remnant28fcc922006-09-01 04:15:57 +0100479 /* Close the control connection */
480 control_close ();
481
Scott James Remnant3401ab72006-09-01 02:14:47 +0100482 /* Create pipe */
Scott James Remnant6f464962006-09-01 04:10:20 +0100483 if (pipe (fds) < 0) {
484 nih_error_raise_system ();
Scott James Remnant3401ab72006-09-01 02:14:47 +0100485 goto error;
Scott James Remnant6f464962006-09-01 04:10:20 +0100486 }
Scott James Remnant3401ab72006-09-01 02:14:47 +0100487
488 /* Fork a child that can send the state to the new init process */
489 pid = fork ();
490 if (pid < 0) {
Scott James Remnant6f464962006-09-01 04:10:20 +0100491 nih_error_raise_system ();
Scott James Remnant3401ab72006-09-01 02:14:47 +0100492 goto error;
493 } else if (pid == 0) {
Scott James Remnant6f464962006-09-01 04:10:20 +0100494 close (fds[0]);
495
496 write_state (fds[1]);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100497 exit (0);
498 } else {
Scott James Remnant6f464962006-09-01 04:10:20 +0100499 if (dup2 (fds[0], STATE_FD) < 0) {
500 nih_error_raise_system ();
501 goto error;
502 }
503
504 close (fds[0]);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100505 close (fds[1]);
Scott James Remnant6f464962006-09-01 04:10:20 +0100506 fds[0] = fds[1] = -1;
Scott James Remnant3401ab72006-09-01 02:14:47 +0100507 }
508
509 /* Argument list */
Scott James Remnant1db88042006-09-01 03:14:19 +0100510 execl (argv0, argv0, "--restart", NULL);
Scott James Remnant6f464962006-09-01 04:10:20 +0100511 nih_error_raise_system ();
Scott James Remnant3401ab72006-09-01 02:14:47 +0100512
513error:
Scott James Remnant6f464962006-09-01 04:10:20 +0100514 err = nih_error_get ();
515 nih_error (_("Failed to re-execute %s: %s"), argv0, err->message);
Scott James Remnant28fcc922006-09-01 04:15:57 +0100516 nih_free (err);
Scott James Remnant6f464962006-09-01 04:10:20 +0100517
Scott James Remnant3401ab72006-09-01 02:14:47 +0100518 close (fds[0]);
Scott James Remnant6f464962006-09-01 04:10:20 +0100519 close (fds[1]);
520
Scott James Remnant28fcc922006-09-01 04:15:57 +0100521 control_open ();
522
Scott James Remnant3401ab72006-09-01 02:14:47 +0100523 sigprocmask (SIG_SETMASK, &oldmask, NULL);
524}
525
526/**
Scott James Remnant3401ab72006-09-01 02:14:47 +0100527 * read_state:
528 * @fd: file descriptor to read from.
529 *
530 * Read event and job state from @fd, which is a trivial line-based
531 * protocol that we can keep the same without too much difficultly. It's
532 * tempting to use the control sockets for this, but they break too often.
533 **/
534static void
535read_state (int fd)
536{
537 Job *job = NULL;
538 Event *event = NULL;
539 FILE *state;
540 char buf[80];
541
542 nih_debug ("Reading state");
543
544 /* Use stdio as it's a light-weight thing that won't change */
545 state = fdopen (fd, "r");
546 if (! state) {
547 nih_warn (_("Unable to read from state descriptor: %s"),
548 strerror (errno));
549 return;
550 }
551
552 /* It's just a series of simple lines; if one begins Job then it
553 * indicates the start of a Job description, otherwise if it
554 * begins Event then it's the start of an Event description.
555 *
556 * Lines beginning "." are assumed to belong to the current job
557 * or event.
558 */
559 while (fgets (buf, sizeof (buf), state)) {
560 char *ptr;
561
562 /* Strip newline */
563 ptr = strchr (buf, '\n');
564 if (ptr)
565 *ptr = '\0';
566
567 if (! strncmp (buf, "Job ", 4)) {
568 job = job_read_state (NULL, buf);
569 event = NULL;
570 } else if (! strncmp (buf, "Event ", 5)) {
571 event = event_read_state (NULL, buf);
572 job = NULL;
573 } else if (buf[0] == '.') {
574 if (job) {
575 job = job_read_state (job, buf);
576 } else if (event) {
577 event = event_read_state (event, buf);
578 }
579 } else {
580 event = NULL;
581 job = NULL;
582 }
583 }
584
585 if (fclose (state))
586 nih_warn (_("Error after reading state: %s"),
587 strerror (errno));
588
589 nih_debug ("State read from parent");
590}
591
592/**
593 * write_state:
594 * @fd: file descriptor to write to.
595 *
596 * Write event and job state to @fd, which is a trivial line-based
597 * protocol that we can keep the same without too much difficultly. It's
598 * tempting to use the control sockets for this, but they break too often.
599 **/
600static void
601write_state (int fd)
602{
603 FILE *state;
604
605 /* Use stdio as it's a light-weight thing that won't change */
606 state = fdopen (fd, "w");
607 if (! state) {
608 nih_warn (_("Unable to write to state descriptor: %s"),
609 strerror (errno));
610 return;
611 }
612
613 event_write_state (state);
614 job_write_state (state);
615
616 if (fclose (state))
617 nih_warn (_("Error after writing state: %s"),
618 strerror (errno));
619}