blob: b236a6ba576bc2278942211ae48a0c5447e13f43 [file] [log] [blame]
Scott James Remnant50748842006-05-16 21:02:31 +01001/* upstart
2 *
Scott James Remnantb6270dd2006-07-13 02:16:38 +01003 * Copyright © 2006 Canonical Ltd.
4 * 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 <linux/kd.h>
27
28#include <sys/types.h>
29#include <sys/wait.h>
30#include <sys/ioctl.h>
31#include <sys/reboot.h>
32#include <sys/resource.h>
33
Scott James Remnant3401ab72006-09-01 02:14:47 +010034#include <errno.h>
35#include <stdio.h>
Scott James Remnantf43bdf32006-08-27 18:20:29 +010036#include <signal.h>
Scott James Remnant94d00982006-08-25 15:38:22 +020037#include <stdlib.h>
Scott James Remnant3401ab72006-09-01 02:14:47 +010038#include <string.h>
Scott James Remnant12a330f2006-08-24 02:19:09 +020039#include <syslog.h>
Scott James Remnant027dd7b2006-08-21 09:01:25 +020040#include <unistd.h>
Scott James Remnantff0d26a2006-08-31 20:49:43 +010041#include <termios.h>
Scott James Remnant027dd7b2006-08-21 09:01:25 +020042
Scott James Remnant77e8db32006-08-21 08:47:50 +020043#include <nih/macros.h>
44#include <nih/alloc.h>
45#include <nih/list.h>
46#include <nih/timer.h>
47#include <nih/signal.h>
48#include <nih/child.h>
Scott James Remnant3401ab72006-09-01 02:14:47 +010049#include <nih/option.h>
Scott James Remnant77e8db32006-08-21 08:47:50 +020050#include <nih/main.h>
51#include <nih/logging.h>
52
53#include "process.h"
54#include "job.h"
55#include "event.h"
56#include "control.h"
57#include "cfgfile.h"
Scott James Remnant50748842006-05-16 21:02:31 +010058
59
Scott James Remnant1db88042006-09-01 03:14:19 +010060/**
61 * STATE_FD:
62 *
63 * File descriptor we read our state from.
64 **/
65#define STATE_FD 101
66
67
Scott James Remnantf43bdf32006-08-27 18:20:29 +010068/* Prototypes for static functions */
Scott James Remnant3401ab72006-09-01 02:14:47 +010069static void reset_console (void);
70static void segv_handler (int signum);
71static void cad_handler (void *data, NihSignal *signal);
72static void kbd_handler (void *data, NihSignal *signal);
73static void stop_handler (void *data, NihSignal *signal);
Scott James Remnant1db88042006-09-01 03:14:19 +010074static void term_handler (const char *prog, NihSignal *signal);
Scott James Remnant3401ab72006-09-01 02:14:47 +010075static void read_state (int fd);
76static void write_state (int fd);
77
Scott James Remnant1db88042006-09-01 03:14:19 +010078
Scott James Remnant3401ab72006-09-01 02:14:47 +010079/**
Scott James Remnant1db88042006-09-01 03:14:19 +010080 * restart:
Scott James Remnant3401ab72006-09-01 02:14:47 +010081 *
Scott James Remnant1db88042006-09-01 03:14:19 +010082 * This is set to %TRUE if we're being re-exec'd by an existing init
83 * process.
Scott James Remnant3401ab72006-09-01 02:14:47 +010084 **/
Scott James Remnant1db88042006-09-01 03:14:19 +010085static int restart = FALSE;
Scott James Remnant3401ab72006-09-01 02:14:47 +010086
87
88/**
89 * options:
90 *
91 * Command-line options we accept.
92 **/
93static NihOption options[] = {
Scott James Remnant1db88042006-09-01 03:14:19 +010094 { 0, "restart", NULL, NULL, NULL, &restart, NULL },
Scott James Remnant3401ab72006-09-01 02:14:47 +010095
96 /* Ignore invalid options */
97 { '-', "--", NULL, NULL, NULL, NULL, NULL },
98
99 NIH_OPTION_LAST
100};
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100101
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100102
Scott James Remnant50748842006-05-16 21:02:31 +0100103int
104main (int argc,
105 char *argv[])
106{
Scott James Remnant3401ab72006-09-01 02:14:47 +0100107 char **args;
108 int ret, i;
Scott James Remnant50748842006-05-16 21:02:31 +0100109
Scott James Remnant77e8db32006-08-21 08:47:50 +0200110 nih_main_init (argv[0]);
111
Scott James Remnant3401ab72006-09-01 02:14:47 +0100112 args = nih_option_parser (NULL, argc, argv, options, FALSE);
113 if (! args)
114 exit (1);
Scott James Remnant12a330f2006-08-24 02:19:09 +0200115
Scott James Remnant3401ab72006-09-01 02:14:47 +0100116 /* Send all logging output to syslog */
117 openlog (program_name, LOG_CONS, LOG_DAEMON);
Scott James Remnant12a330f2006-08-24 02:19:09 +0200118 nih_log_set_logger (nih_logger_syslog);
Scott James Remnant77e8db32006-08-21 08:47:50 +0200119
Scott James Remnant77e8db32006-08-21 08:47:50 +0200120 /* Close any file descriptors we inherited, and open the console
Scott James Remnant3401ab72006-09-01 02:14:47 +0100121 * device instead. Normally we reset the console, unless we're
122 * inheriting one from another init process.
Scott James Remnant77e8db32006-08-21 08:47:50 +0200123 */
124 for (i = 0; i < 3; i++)
125 close (i);
126
127 process_setup_console (CONSOLE_OUTPUT);
Scott James Remnant1db88042006-09-01 03:14:19 +0100128 if (! restart)
Scott James Remnant3401ab72006-09-01 02:14:47 +0100129 reset_console ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200130
Scott James Remnantf4970812006-08-27 18:27:19 +0100131 /* Check we're root */
132 if (getuid ()) {
133 nih_error (_("Need to be root"));
134 exit (1);
135 }
136
137 /* Check we're process #1 */
138 if (getpid () > 1) {
139 nih_error (_("Not being executed as init"));
140 exit (1);
141 }
142
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100143 /* Reset the signal state and install the signal handler for those
144 * signals we actually want to catch; this also sets those that
145 * can be sent to us, because we're special
146 */
147 nih_signal_reset ();
148 nih_signal_set_handler (SIGALRM, nih_signal_handler);
149 nih_signal_set_handler (SIGHUP, nih_signal_handler);
Scott James Remnanteabb7802006-08-31 15:39:04 +0100150 nih_signal_set_handler (SIGTSTP, nih_signal_handler);
151 nih_signal_set_handler (SIGCONT, nih_signal_handler);
Scott James Remnant1db88042006-09-01 03:14:19 +0100152 nih_signal_set_handler (SIGTERM, nih_signal_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100153 nih_signal_set_handler (SIGINT, nih_signal_handler);
154 nih_signal_set_handler (SIGWINCH, nih_signal_handler);
155 nih_signal_set_handler (SIGSEGV, segv_handler);
156
Scott James Remnanteabb7802006-08-31 15:39:04 +0100157 /* Ensure that we don't process events while paused */
Scott James Remnanteabb7802006-08-31 15:39:04 +0100158 nih_signal_add_callback (NULL, SIGTSTP, stop_handler, NULL);
159 nih_signal_add_callback (NULL, SIGCONT, stop_handler, NULL);
160
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100161 /* Ask the kernel to send us SIGINT when control-alt-delete is
162 * pressed; generate an event with the same name.
163 */
164 reboot (RB_DISABLE_CAD);
165 nih_signal_add_callback (NULL, SIGINT, cad_handler, NULL);
166
167 /* Ask the kernel to send us SIGWINCH when alt-uparrow is pressed;
168 * generate a kbdrequest event.
169 */
170 ioctl (0, KDSIGACCEPT, SIGWINCH);
171 nih_signal_add_callback (NULL, SIGWINCH, kbd_handler, NULL);
172
Scott James Remnant1db88042006-09-01 03:14:19 +0100173 /* SIGTERM instructs us to re-exec ourselves */
174 nih_signal_add_callback (NULL, SIGTERM,
175 (NihSignalCb)term_handler, argv[0]);
176
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100177
178 /* Reap all children that die */
179 nih_child_add_watch (NULL, -1, job_child_reaper, NULL);
180
Scott James Remnantdc8877d2006-08-29 16:56:48 +0100181 /* Process the event queue and check the jobs for idleness
182 * every time through the main loop */
Scott James Remnant0c7e72a2006-08-27 22:22:53 +0100183 nih_main_loop_add_func (NULL, (NihMainLoopCb)event_queue_run, NULL);
Scott James Remnantdc8877d2006-08-29 16:56:48 +0100184 nih_main_loop_add_func (NULL, (NihMainLoopCb)job_detect_idle, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100185
Scott James Remnant94d00982006-08-25 15:38:22 +0200186
Scott James Remnant77e8db32006-08-21 08:47:50 +0200187 /* Become session and process group leader (should be already,
188 * but you never know what initramfs did
189 */
190 setsid ();
191
192 /* Open control socket */
193 control_open ();
194
Scott James Remnantceaaf4d2006-08-24 01:40:10 +0200195 /* Read configuration */
196 cfg_watch_dir (NULL, CFG_DIR, NULL);
197
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100198 /* Set the PATH environment variable */
199 setenv ("PATH", PATH, TRUE);
200
Scott James Remnant77e8db32006-08-21 08:47:50 +0200201
Scott James Remnant3401ab72006-09-01 02:14:47 +0100202 /* Generate and run the startup event or read the state from the
203 * init daemon that exec'd us
204 */
Scott James Remnant1db88042006-09-01 03:14:19 +0100205 if (! restart) {
Scott James Remnant3401ab72006-09-01 02:14:47 +0100206 event_queue ("startup");
207 } else {
Scott James Remnant1db88042006-09-01 03:14:19 +0100208 sigset_t mask;
209
210 /* State file descriptor is fixed */
211 read_state (STATE_FD);
212
213 /* We're ok to receive signals again */
214 sigemptyset (&mask);
215 sigprocmask (SIG_SETMASK, &mask, NULL);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100216 }
217
218 /* Run the event queue once, and detect anything idle */
Scott James Remnant0c7e72a2006-08-27 22:22:53 +0100219 event_queue_run ();
Scott James Remnant32de9222006-08-31 04:34:27 +0100220 job_detect_idle ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200221
222 /* Go! */
223 ret = nih_main_loop ();
224
225 return ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100226}
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100227
228
229/**
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100230 * reset_console:
231 *
232 * Set up the console flags to something sensible. Cribbed from sysvinit,
233 * initng, etc.
234 **/
235static void
236reset_console (void)
237{
Scott James Remnant4bc5fe32006-08-31 21:48:33 +0100238 struct termios tty;
239
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100240 tcgetattr (0, &tty);
241
242 tty.c_cflag &= (CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD);
243 tty.c_cflag |= (HUPCL | CLOCAL | CREAD);
244
245 /* Set up usual keys */
246 tty.c_cc[VINTR] = 3; /* ^C */
247 tty.c_cc[VQUIT] = 28; /* ^\ */
248 tty.c_cc[VERASE] = 127;
249 tty.c_cc[VKILL] = 24; /* ^X */
250 tty.c_cc[VEOF] = 4; /* ^D */
251 tty.c_cc[VTIME] = 0;
252 tty.c_cc[VMIN] = 1;
253 tty.c_cc[VSTART] = 17; /* ^Q */
254 tty.c_cc[VSTOP] = 19; /* ^S */
255 tty.c_cc[VSUSP] = 26; /* ^Z */
256
257 /* Pre and post processing */
258 tty.c_iflag = (IGNPAR | ICRNL | IXON | IXANY);
259 tty.c_oflag = (OPOST | ONLCR);
260 tty.c_lflag = (ISIG | ICANON | ECHO | ECHOCTL | ECHOPRT | ECHOKE);
261
262 /* Set the terminal line and flush it */
263 tcsetattr (0, TCSANOW, &tty);
264 tcflush (0, TCIOFLUSH);
265}
266
267
268/**
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100269 * segv_handler:
270 * @signum:
271 *
272 * Handle receiving the SEGV signal, usually caused by one of our own
273 * mistakes. We deal with it by dumping core in a child process and
274 * just carrying on in the parent.
275 **/
276static void
277segv_handler (int signum)
278{
279 pid_t pid;
280
281 pid = fork ();
282 if (pid == 0) {
283 struct sigaction act;
284 struct rlimit limit;
285 sigset_t mask;
286
287 /* Mask out all signals */
288 sigfillset (&mask);
289 sigprocmask (SIG_SETMASK, &mask, NULL);
290
291 /* Set the SEGV handler to the default so core is dumped */
292 act.sa_handler = SIG_DFL;
293 act.sa_flags = 0;
294 sigemptyset (&act.sa_mask);
295 sigaction (SIGSEGV, &act, NULL);
296
297 /* Dump in the root directory */
298 chdir ("/");
299
300 /* Don't limit the core dump size */
301 limit.rlim_cur = RLIM_INFINITY;
302 limit.rlim_max = RLIM_INFINITY;
303 setrlimit (RLIMIT_CORE, &limit);
304
305 /* Raise the signal */
306 raise (SIGSEGV);
307
308 /* Unmask so that we receive it */
309 sigdelset (&mask, SIGSEGV);
310 sigprocmask (SIG_SETMASK, &mask, NULL);
311
312 /* Wait for death */
313 pause ();
314 exit (0);
315 } else if (pid > 0) {
316 /* Wait for the core to be generated */
317 waitpid (pid, NULL, 0);
318
319 nih_error (_("Caught segmentation fault, core dumped"));
320 } else {
321 nih_error (_("Caught segmentation fault, unable to dump core"));
322 }
323}
324
325/**
326 * cad_handler:
327 * @data: unused,
328 * @signal: signal that called this handler.
329 *
330 * Handle having recieved the SIGINT signal, sent to us when somebody
331 * presses Ctrl-Alt-Delete on the console. We just generate a
332 * control-alt-delete event.
333 **/
334static void
335cad_handler (void *data,
336 NihSignal *signal)
337{
Scott James Remnantff5efb92006-08-31 01:45:19 +0100338 event_queue ("control-alt-delete");
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100339}
340
341/**
342 * kbd_handler:
343 * @data: unused,
344 * @signal: signal that called this handler.
345 *
346 * Handle having recieved the SIGWINCH signal, sent to us when somebody
347 * presses Alt-UpArrow on the console. We just generate a
348 * kbdrequest event.
349 **/
350static void
351kbd_handler (void *data,
352 NihSignal *signal)
353{
Scott James Remnantff5efb92006-08-31 01:45:19 +0100354 event_queue ("kbdrequest");
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100355}
Scott James Remnanteabb7802006-08-31 15:39:04 +0100356
357/**
358 * stop_handler:
359 * @data: unused,
360 * @signal: signal caught.
361 *
362 * This is called when we receive the STOP, TSTP or CONT signals; we
363 * adjust the paused variable appropriately so that the event queue and
364 * job idle detection is not run.
365 **/
366static void
367stop_handler (void *data,
368 NihSignal *signal)
369{
370 nih_assert (signal != NULL);
371
372 if (signal->signum == SIGCONT) {
373 nih_info (_("Event queue resumed"));
374 paused = FALSE;
375 } else {
376 nih_info (_("Event queue paused"));
377 paused = TRUE;
378 }
379}
Scott James Remnant3401ab72006-09-01 02:14:47 +0100380
381
382/**
Scott James Remnant1db88042006-09-01 03:14:19 +0100383 * term_handler:
Scott James Remnant3401ab72006-09-01 02:14:47 +0100384 * @argv0: program to run,
385 * @signal: signal caught.
386 *
Scott James Remnant1db88042006-09-01 03:14:19 +0100387 * This is called when we receive the TERM signal, which instructs us
Scott James Remnant3401ab72006-09-01 02:14:47 +0100388 * to reexec ourselves.
389 **/
390static void
Scott James Remnant1db88042006-09-01 03:14:19 +0100391term_handler (const char *argv0,
Scott James Remnant3401ab72006-09-01 02:14:47 +0100392 NihSignal *signal)
393{
394 sigset_t mask, oldmask;
395 int fds[2] = { -1, -1 };
396 pid_t pid;
Scott James Remnant3401ab72006-09-01 02:14:47 +0100397
398 nih_assert (argv0 != NULL);
399 nih_assert (signal != NULL);
400
401 nih_warn (_("Re-executing %s"), argv0);
402
Scott James Remnant1db88042006-09-01 03:14:19 +0100403 /* Block signals while we work. We're the last signal handler
404 * installed so this should mean that they're all handled now.
405 *
406 * The child must make sure that it unblocks these again when
407 * it's ready.
408 */
Scott James Remnant3401ab72006-09-01 02:14:47 +0100409 sigfillset (&mask);
410 sigprocmask (SIG_BLOCK, &mask, &oldmask);
411
412 /* Create pipe */
413 if (pipe (fds) < 0)
414 goto error;
415
416 /* Fork a child that can send the state to the new init process */
417 pid = fork ();
418 if (pid < 0) {
419 close (fds[1]);
420 goto error;
421 } else if (pid == 0) {
Scott James Remnant1db88042006-09-01 03:14:19 +0100422 dup2 (fds[1], STATE_FD);
423 write_state (STATE_FD);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100424 exit (0);
425 } else {
426 close (fds[1]);
427 }
428
429 /* Argument list */
Scott James Remnant1db88042006-09-01 03:14:19 +0100430 execl (argv0, argv0, "--restart", NULL);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100431
432error:
433 /* Gnargh! */
434 nih_error (_("Failed to re-execute %s: %s"), argv0, strerror (errno));
435 close (fds[0]);
436 sigprocmask (SIG_SETMASK, &oldmask, NULL);
437}
438
439/**
Scott James Remnant3401ab72006-09-01 02:14:47 +0100440 * read_state:
441 * @fd: file descriptor to read from.
442 *
443 * Read event and job state from @fd, which is a trivial line-based
444 * protocol that we can keep the same without too much difficultly. It's
445 * tempting to use the control sockets for this, but they break too often.
446 **/
447static void
448read_state (int fd)
449{
450 Job *job = NULL;
451 Event *event = NULL;
452 FILE *state;
453 char buf[80];
454
455 nih_debug ("Reading state");
456
457 /* Use stdio as it's a light-weight thing that won't change */
458 state = fdopen (fd, "r");
459 if (! state) {
460 nih_warn (_("Unable to read from state descriptor: %s"),
461 strerror (errno));
462 return;
463 }
464
465 /* It's just a series of simple lines; if one begins Job then it
466 * indicates the start of a Job description, otherwise if it
467 * begins Event then it's the start of an Event description.
468 *
469 * Lines beginning "." are assumed to belong to the current job
470 * or event.
471 */
472 while (fgets (buf, sizeof (buf), state)) {
473 char *ptr;
474
475 /* Strip newline */
476 ptr = strchr (buf, '\n');
477 if (ptr)
478 *ptr = '\0';
479
480 if (! strncmp (buf, "Job ", 4)) {
481 job = job_read_state (NULL, buf);
482 event = NULL;
483 } else if (! strncmp (buf, "Event ", 5)) {
484 event = event_read_state (NULL, buf);
485 job = NULL;
486 } else if (buf[0] == '.') {
487 if (job) {
488 job = job_read_state (job, buf);
489 } else if (event) {
490 event = event_read_state (event, buf);
491 }
492 } else {
493 event = NULL;
494 job = NULL;
495 }
496 }
497
498 if (fclose (state))
499 nih_warn (_("Error after reading state: %s"),
500 strerror (errno));
501
502 nih_debug ("State read from parent");
503}
504
505/**
506 * write_state:
507 * @fd: file descriptor to write to.
508 *
509 * Write event and job state to @fd, which is a trivial line-based
510 * protocol that we can keep the same without too much difficultly. It's
511 * tempting to use the control sockets for this, but they break too often.
512 **/
513static void
514write_state (int fd)
515{
516 FILE *state;
517
518 /* Use stdio as it's a light-weight thing that won't change */
519 state = fdopen (fd, "w");
520 if (! state) {
521 nih_warn (_("Unable to write to state descriptor: %s"),
522 strerror (errno));
523 return;
524 }
525
526 event_write_state (state);
527 job_write_state (state);
528
529 if (fclose (state))
530 nih_warn (_("Error after writing state: %s"),
531 strerror (errno));
532}