blob: 7d01f1dbb78b930faba394822573ee58ec625a8f [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 Remnantb1db7a32007-02-07 16:33:27 +000077#if 0
Scott James Remnant1db88042006-09-01 03:14:19 +010078static void term_handler (const char *prog, NihSignal *signal);
Scott James Remnant3401ab72006-09-01 02:14:47 +010079static void read_state (int fd);
80static void write_state (int fd);
Scott James Remnantb1db7a32007-02-07 16:33:27 +000081#endif
Scott James Remnant3401ab72006-09-01 02:14:47 +010082
Scott James Remnant1db88042006-09-01 03:14:19 +010083
Scott James Remnant3401ab72006-09-01 02:14:47 +010084/**
Scott James Remnant1db88042006-09-01 03:14:19 +010085 * restart:
Scott James Remnant3401ab72006-09-01 02:14:47 +010086 *
Scott James Remnant8b227402006-10-11 17:55:27 +010087 * This is set to TRUE if we're being re-exec'd by an existing init
Scott James Remnant1db88042006-09-01 03:14:19 +010088 * process.
Scott James Remnant3401ab72006-09-01 02:14:47 +010089 **/
Scott James Remnant1db88042006-09-01 03:14:19 +010090static int restart = FALSE;
Scott James Remnant3401ab72006-09-01 02:14:47 +010091
92
93/**
94 * options:
95 *
96 * Command-line options we accept.
97 **/
98static NihOption options[] = {
Scott James Remnantb1db7a32007-02-07 16:33:27 +000099#if 0
Scott James Remnant1db88042006-09-01 03:14:19 +0100100 { 0, "restart", NULL, NULL, NULL, &restart, NULL },
Scott James Remnantb1db7a32007-02-07 16:33:27 +0000101#endif
Scott James Remnant3401ab72006-09-01 02:14:47 +0100102
103 /* Ignore invalid options */
104 { '-', "--", NULL, NULL, NULL, NULL, NULL },
105
106 NIH_OPTION_LAST
107};
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100108
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100109
Scott James Remnant50748842006-05-16 21:02:31 +0100110int
111main (int argc,
112 char *argv[])
113{
Scott James Remnant3401ab72006-09-01 02:14:47 +0100114 char **args;
Scott James Remnant2e204b72007-02-03 23:15:28 +0000115 int ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100116
Scott James Remnant77e8db32006-08-21 08:47:50 +0200117 nih_main_init (argv[0]);
118
Scott James Remnant930e25a2006-10-13 12:28:05 +0100119 nih_option_set_synopsis (_("Process management daemon."));
Scott James Remnant462734c2006-10-13 13:36:00 +0100120 nih_option_set_help (
121 _("This daemon is normally executed by the kernel and given "
122 "process id 1 to denote its special status. When executed "
123 "by a user process, it will actually run /sbin/telinit."));
Scott James Remnanta6ed7eb2006-10-13 12:14:45 +0100124
Scott James Remnant3401ab72006-09-01 02:14:47 +0100125 args = nih_option_parser (NULL, argc, argv, options, FALSE);
126 if (! args)
127 exit (1);
Scott James Remnant12a330f2006-08-24 02:19:09 +0200128
Scott James Remnanta17917d2006-09-07 00:18:28 +0100129 /* Check we're root */
130 if (getuid ()) {
131 nih_error (_("Need to be root"));
132 exit (1);
133 }
134
135 /* Check we're process #1 */
136 if (getpid () > 1) {
Scott James Remnante0d0dd12006-09-14 10:51:05 +0100137 execv (TELINIT, argv);
Scott James Remnanta17917d2006-09-07 00:18:28 +0100138 /* Ignore failure, probably just that telinit doesn't exist */
139
140 nih_error (_("Not being executed as init"));
141 exit (1);
142 }
143
Scott James Remnant2e204b72007-02-03 23:15:28 +0000144 /* Open control socket */
145 if (! control_open ()) {
146 NihError *err;
147
148 err = nih_error_get ();
149 nih_error ("%s: %s", _("Unable to open control socket"),
150 err->message);
151 nih_free (err);
152
153 exit (1);
154 }
155
156 /* Read configuration */
157 if (cfg_watch_dir (CFG_DIR) < 0) {
158 NihError *err;
159
160 err = nih_error_get ();
161 nih_error ("%s: %s", _("Error parsing configuration"),
162 err->message);
163 nih_free (err);
164
165 exit (1);
166 }
167
Scott James Remnant78626fb2007-01-10 16:48:10 +0000168
169 /* Clear our arguments from the command-line, so that we show up in
170 * ps or top output as /sbin/init, with no extra flags.
171 *
172 * This is a very Linux-specific trick; by deleting the NULL
173 * terminator at the end of the last argument, we fool the kernel
174 * into believing we used a setproctitle()-a-like to extend the
175 * argument space into the environment space, and thus make it use
176 * strlen() instead of its own assumed length. In fact, we've done
177 * the exact opposite, and shrunk the command line length to just that
178 * of whatever is in argv[0].
179 *
180 * If we don't do this, and just write \0 over the rest of argv, for
181 * example; the command-line length still includes those \0s, and ps
182 * will show whitespace in their place.
183 */
184 if (argc > 1) {
Scott James Remnant505f9282007-01-10 18:44:38 +0000185 char *arg_end;
Scott James Remnant78626fb2007-01-10 16:48:10 +0000186
Scott James Remnant505f9282007-01-10 18:44:38 +0000187 arg_end = argv[argc-1] + strlen (argv[argc-1]);
188 *arg_end = ' ';
Scott James Remnant78626fb2007-01-10 16:48:10 +0000189 }
Scott James Remnant7f4db422007-01-09 20:51:08 +0000190
Scott James Remnant2e204b72007-02-03 23:15:28 +0000191 /* Become session and process group leader (should be already,
192 * but you never know what initramfs did
193 */
194 setsid ();
Scott James Remnanta17917d2006-09-07 00:18:28 +0100195
Scott James Remnant3401ab72006-09-01 02:14:47 +0100196 /* Send all logging output to syslog */
197 openlog (program_name, LOG_CONS, LOG_DAEMON);
Scott James Remnant12a330f2006-08-24 02:19:09 +0200198 nih_log_set_logger (nih_logger_syslog);
Scott James Remnant77e8db32006-08-21 08:47:50 +0200199
Scott James Remnant77e8db32006-08-21 08:47:50 +0200200 /* Close any file descriptors we inherited, and open the console
Scott James Remnant3401ab72006-09-01 02:14:47 +0100201 * device instead. Normally we reset the console, unless we're
202 * inheriting one from another init process.
Scott James Remnant77e8db32006-08-21 08:47:50 +0200203 */
Scott James Remnant2e204b72007-02-03 23:15:28 +0000204 for (int i = 0; i < 3; i++)
Scott James Remnant77e8db32006-08-21 08:47:50 +0200205 close (i);
206
Scott James Remnant5d702952007-01-05 17:21:34 +0000207 if (process_setup_console (NULL, CONSOLE_OUTPUT) < 0)
208 nih_free (nih_error_get ());
Scott James Remnant1db88042006-09-01 03:14:19 +0100209 if (! restart)
Scott James Remnant3401ab72006-09-01 02:14:47 +0100210 reset_console ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200211
Scott James Remnant2e204b72007-02-03 23:15:28 +0000212 /* Set the PATH environment variable */
213 setenv ("PATH", PATH, TRUE);
214
215
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100216 /* Reset the signal state and install the signal handler for those
217 * signals we actually want to catch; this also sets those that
218 * can be sent to us, because we're special
219 */
Scott James Remnant2e204b72007-02-03 23:15:28 +0000220 if (! restart)
221 nih_signal_reset ();
222
223 nih_signal_set_handler (SIGCHLD, nih_signal_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100224 nih_signal_set_handler (SIGALRM, nih_signal_handler);
225 nih_signal_set_handler (SIGHUP, nih_signal_handler);
Scott James Remnanteabb7802006-08-31 15:39:04 +0100226 nih_signal_set_handler (SIGTSTP, nih_signal_handler);
227 nih_signal_set_handler (SIGCONT, nih_signal_handler);
Scott James Remnant1db88042006-09-01 03:14:19 +0100228 nih_signal_set_handler (SIGTERM, nih_signal_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100229 nih_signal_set_handler (SIGINT, nih_signal_handler);
230 nih_signal_set_handler (SIGWINCH, nih_signal_handler);
Scott James Remnant16a286f2007-01-10 15:38:33 +0000231 nih_signal_set_handler (SIGSEGV, crash_handler);
232 nih_signal_set_handler (SIGABRT, crash_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100233
Scott James Remnanteabb7802006-08-31 15:39:04 +0100234 /* Ensure that we don't process events while paused */
Scott James Remnant5d702952007-01-05 17:21:34 +0000235 NIH_MUST (nih_signal_add_handler (NULL, SIGTSTP, stop_handler, NULL));
236 NIH_MUST (nih_signal_add_handler (NULL, SIGCONT, stop_handler, NULL));
Scott James Remnanteabb7802006-08-31 15:39:04 +0100237
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100238 /* Ask the kernel to send us SIGINT when control-alt-delete is
239 * pressed; generate an event with the same name.
240 */
241 reboot (RB_DISABLE_CAD);
Scott James Remnant5d702952007-01-05 17:21:34 +0000242 NIH_MUST (nih_signal_add_handler (NULL, SIGINT, cad_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100243
244 /* Ask the kernel to send us SIGWINCH when alt-uparrow is pressed;
245 * generate a kbdrequest event.
246 */
Scott James Remnant5d702952007-01-05 17:21:34 +0000247 if (ioctl (0, KDSIGACCEPT, SIGWINCH) == 0)
248 NIH_MUST (nih_signal_add_handler (NULL, SIGWINCH,
249 kbd_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100250
Scott James Remnantb1db7a32007-02-07 16:33:27 +0000251#if 0
Scott James Remnant1db88042006-09-01 03:14:19 +0100252 /* SIGTERM instructs us to re-exec ourselves */
Scott James Remnant5d702952007-01-05 17:21:34 +0000253 NIH_MUST (nih_signal_add_handler (NULL, SIGTERM,
254 (NihSignalHandler)term_handler,
255 argv[0]));
Scott James Remnantb1db7a32007-02-07 16:33:27 +0000256#endif
Scott James Remnant1db88042006-09-01 03:14:19 +0100257
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100258 /* Reap all children that die */
Scott James Remnant5d702952007-01-05 17:21:34 +0000259 NIH_MUST (nih_child_add_watch (NULL, -1, job_child_reaper, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100260
Scott James Remnantdc8877d2006-08-29 16:56:48 +0100261 /* Process the event queue and check the jobs for idleness
262 * every time through the main loop */
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000263 NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)event_poll,
Scott James Remnant5d702952007-01-05 17:21:34 +0000264 NULL));
265 NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)job_detect_idle,
266 NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100267
Scott James Remnant94d00982006-08-25 15:38:22 +0200268
Scott James Remnant3401ab72006-09-01 02:14:47 +0100269 /* Generate and run the startup event or read the state from the
270 * init daemon that exec'd us
271 */
Scott James Remnant1db88042006-09-01 03:14:19 +0100272 if (! restart) {
Scott James Remnantb4a1c2b2006-09-09 01:28:26 +0100273 Job *logd;
274
275 /* FIXME this is a bit of a hack, should have a list of
276 * essential services or something
277 */
278 logd = job_find_by_name ("logd");
Scott James Remnant515b2b92006-09-09 03:41:27 +0100279 if (logd) {
Scott James Remnantb4a1c2b2006-09-09 01:28:26 +0100280 job_start (logd);
Scott James Remnant515b2b92006-09-09 03:41:27 +0100281 if (logd->state == JOB_RUNNING) {
282 /* Hang around until logd signals that it's
283 * listening ... but not too long
284 */
285 alarm (5);
286 waitpid (logd->pid, NULL, WUNTRACED);
287 kill (logd->pid, SIGCONT);
288 alarm (0);
289 }
290 }
Scott James Remnantb4a1c2b2006-09-09 01:28:26 +0100291
Scott James Remnantea204d42006-09-07 00:30:53 +0100292 event_queue (STARTUP_EVENT);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100293 } else {
Scott James Remnant1db88042006-09-01 03:14:19 +0100294 sigset_t mask;
295
Scott James Remnantb1db7a32007-02-07 16:33:27 +0000296#if 0
Scott James Remnant1db88042006-09-01 03:14:19 +0100297 /* State file descriptor is fixed */
298 read_state (STATE_FD);
Scott James Remnantb1db7a32007-02-07 16:33:27 +0000299#endif
Scott James Remnant1db88042006-09-01 03:14:19 +0100300
301 /* We're ok to receive signals again */
302 sigemptyset (&mask);
303 sigprocmask (SIG_SETMASK, &mask, NULL);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100304 }
305
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000306 /* Run through the loop at least once */
307 nih_main_loop_interrupt ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200308 ret = nih_main_loop ();
309
310 return ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100311}
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100312
313
314/**
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100315 * reset_console:
316 *
317 * Set up the console flags to something sensible. Cribbed from sysvinit,
318 * initng, etc.
319 **/
320static void
321reset_console (void)
322{
Scott James Remnant4bc5fe32006-08-31 21:48:33 +0100323 struct termios tty;
324
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100325 tcgetattr (0, &tty);
326
327 tty.c_cflag &= (CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD);
328 tty.c_cflag |= (HUPCL | CLOCAL | CREAD);
329
330 /* Set up usual keys */
331 tty.c_cc[VINTR] = 3; /* ^C */
332 tty.c_cc[VQUIT] = 28; /* ^\ */
333 tty.c_cc[VERASE] = 127;
334 tty.c_cc[VKILL] = 24; /* ^X */
335 tty.c_cc[VEOF] = 4; /* ^D */
336 tty.c_cc[VTIME] = 0;
337 tty.c_cc[VMIN] = 1;
338 tty.c_cc[VSTART] = 17; /* ^Q */
339 tty.c_cc[VSTOP] = 19; /* ^S */
340 tty.c_cc[VSUSP] = 26; /* ^Z */
341
342 /* Pre and post processing */
343 tty.c_iflag = (IGNPAR | ICRNL | IXON | IXANY);
344 tty.c_oflag = (OPOST | ONLCR);
345 tty.c_lflag = (ISIG | ICANON | ECHO | ECHOCTL | ECHOPRT | ECHOKE);
346
347 /* Set the terminal line and flush it */
348 tcsetattr (0, TCSANOW, &tty);
349 tcflush (0, TCIOFLUSH);
350}
351
352
353/**
Scott James Remnant16a286f2007-01-10 15:38:33 +0000354 * crash_handler:
Scott James Remnant8b227402006-10-11 17:55:27 +0100355 * @signum: signal number received.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100356 *
Scott James Remnant16a286f2007-01-10 15:38:33 +0000357 * Handle receiving the SEGV or ABRT signal, usually caused by one of
358 * our own mistakes. We deal with it by dumping core in a child process
359 * and just carrying on in the parent.
360 *
361 * This may or may not work, but the only alternative would be sigjmp()ing
362 * to somewhere "safe" leaving inconsistent state everywhere (like dangling
363 * lists pointers) or exec'ing another process (which we couldn't transfer
364 * our state to anyway). This just hopes that the kernel resumes on the
365 * next instruction.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100366 **/
367static void
Scott James Remnant16a286f2007-01-10 15:38:33 +0000368crash_handler (int signum)
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100369{
370 pid_t pid;
371
372 pid = fork ();
373 if (pid == 0) {
374 struct sigaction act;
375 struct rlimit limit;
376 sigset_t mask;
377
378 /* Mask out all signals */
379 sigfillset (&mask);
380 sigprocmask (SIG_SETMASK, &mask, NULL);
381
Scott James Remnant16a286f2007-01-10 15:38:33 +0000382 /* Set the handler to the default so core is dumped */
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100383 act.sa_handler = SIG_DFL;
384 act.sa_flags = 0;
385 sigemptyset (&act.sa_mask);
Scott James Remnant16a286f2007-01-10 15:38:33 +0000386 sigaction (signum, &act, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100387
388 /* Dump in the root directory */
389 chdir ("/");
390
391 /* Don't limit the core dump size */
392 limit.rlim_cur = RLIM_INFINITY;
393 limit.rlim_max = RLIM_INFINITY;
394 setrlimit (RLIMIT_CORE, &limit);
395
Scott James Remnant16a286f2007-01-10 15:38:33 +0000396 /* Raise the signal again */
397 raise (signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100398
399 /* Unmask so that we receive it */
Scott James Remnant16a286f2007-01-10 15:38:33 +0000400 sigdelset (&mask, signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100401 sigprocmask (SIG_SETMASK, &mask, NULL);
402
403 /* Wait for death */
404 pause ();
405 exit (0);
406 } else if (pid > 0) {
407 /* Wait for the core to be generated */
408 waitpid (pid, NULL, 0);
409
Scott James Remnant16a286f2007-01-10 15:38:33 +0000410 nih_error (_("Caught %s, core dumped"),
Scott James Remnantde443012007-01-10 18:45:40 +0000411 (signum == SIGSEGV
412 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100413 } else {
Scott James Remnant16a286f2007-01-10 15:38:33 +0000414 nih_error (_("Caught %s, unable to dump core"),
Scott James Remnantde443012007-01-10 18:45:40 +0000415 (signum == SIGSEGV
416 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100417 }
418}
419
420/**
421 * cad_handler:
422 * @data: unused,
423 * @signal: signal that called this handler.
424 *
425 * Handle having recieved the SIGINT signal, sent to us when somebody
426 * presses Ctrl-Alt-Delete on the console. We just generate a
Scott James Remnantbb3cc3f2006-09-08 17:17:47 +0100427 * ctrlaltdel event.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100428 **/
429static void
430cad_handler (void *data,
431 NihSignal *signal)
432{
Scott James Remnantbb3cc3f2006-09-08 17:17:47 +0100433 event_queue (CTRLALTDEL_EVENT);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100434}
435
436/**
437 * kbd_handler:
438 * @data: unused,
439 * @signal: signal that called this handler.
440 *
441 * Handle having recieved the SIGWINCH signal, sent to us when somebody
442 * presses Alt-UpArrow on the console. We just generate a
443 * kbdrequest event.
444 **/
445static void
446kbd_handler (void *data,
447 NihSignal *signal)
448{
Scott James Remnantbb3cc3f2006-09-08 17:17:47 +0100449 event_queue (KBDREQUEST_EVENT);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100450}
Scott James Remnanteabb7802006-08-31 15:39:04 +0100451
452/**
453 * stop_handler:
454 * @data: unused,
455 * @signal: signal caught.
456 *
457 * This is called when we receive the STOP, TSTP or CONT signals; we
458 * adjust the paused variable appropriately so that the event queue and
459 * job idle detection is not run.
460 **/
461static void
462stop_handler (void *data,
463 NihSignal *signal)
464{
465 nih_assert (signal != NULL);
466
467 if (signal->signum == SIGCONT) {
468 nih_info (_("Event queue resumed"));
469 paused = FALSE;
470 } else {
471 nih_info (_("Event queue paused"));
472 paused = TRUE;
473 }
474}
Scott James Remnant3401ab72006-09-01 02:14:47 +0100475
476
Scott James Remnantb1db7a32007-02-07 16:33:27 +0000477#if 0
Scott James Remnant3401ab72006-09-01 02:14:47 +0100478/**
Scott James Remnant1db88042006-09-01 03:14:19 +0100479 * term_handler:
Scott James Remnant3401ab72006-09-01 02:14:47 +0100480 * @argv0: program to run,
481 * @signal: signal caught.
482 *
Scott James Remnant1db88042006-09-01 03:14:19 +0100483 * This is called when we receive the TERM signal, which instructs us
Scott James Remnant3401ab72006-09-01 02:14:47 +0100484 * to reexec ourselves.
485 **/
486static void
Scott James Remnant1db88042006-09-01 03:14:19 +0100487term_handler (const char *argv0,
Scott James Remnant3401ab72006-09-01 02:14:47 +0100488 NihSignal *signal)
489{
Scott James Remnant6f464962006-09-01 04:10:20 +0100490 NihError *err;
Scott James Remnant3401ab72006-09-01 02:14:47 +0100491 sigset_t mask, oldmask;
492 int fds[2] = { -1, -1 };
493 pid_t pid;
Scott James Remnant3401ab72006-09-01 02:14:47 +0100494
495 nih_assert (argv0 != NULL);
496 nih_assert (signal != NULL);
497
498 nih_warn (_("Re-executing %s"), argv0);
499
Scott James Remnant1db88042006-09-01 03:14:19 +0100500 /* Block signals while we work. We're the last signal handler
501 * installed so this should mean that they're all handled now.
502 *
503 * The child must make sure that it unblocks these again when
504 * it's ready.
505 */
Scott James Remnant3401ab72006-09-01 02:14:47 +0100506 sigfillset (&mask);
507 sigprocmask (SIG_BLOCK, &mask, &oldmask);
508
509 /* Create pipe */
Scott James Remnant6f464962006-09-01 04:10:20 +0100510 if (pipe (fds) < 0) {
511 nih_error_raise_system ();
Scott James Remnant3401ab72006-09-01 02:14:47 +0100512 goto error;
Scott James Remnant6f464962006-09-01 04:10:20 +0100513 }
Scott James Remnant3401ab72006-09-01 02:14:47 +0100514
515 /* Fork a child that can send the state to the new init process */
516 pid = fork ();
517 if (pid < 0) {
Scott James Remnant6f464962006-09-01 04:10:20 +0100518 nih_error_raise_system ();
Scott James Remnant3401ab72006-09-01 02:14:47 +0100519 goto error;
520 } else if (pid == 0) {
Scott James Remnant6f464962006-09-01 04:10:20 +0100521 close (fds[0]);
522
Scott James Remnant2e204b72007-02-03 23:15:28 +0000523 /* Close the control socket so the new init process won't
524 * get EADDRINUSE when it tries to open it.
525 */
526 control_close ();
527 write (fds[1], "\n", 1);
528
Scott James Remnant6f464962006-09-01 04:10:20 +0100529 write_state (fds[1]);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100530 exit (0);
531 } else {
Scott James Remnant2e204b72007-02-03 23:15:28 +0000532 char buf[1];
533
534 /* Make sure the child is ready to send its state */
535 read (fds[0], buf, sizeof (buf));
536
Scott James Remnant6f464962006-09-01 04:10:20 +0100537 if (dup2 (fds[0], STATE_FD) < 0) {
538 nih_error_raise_system ();
539 goto error;
540 }
541
542 close (fds[0]);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100543 close (fds[1]);
Scott James Remnant6f464962006-09-01 04:10:20 +0100544 fds[0] = fds[1] = -1;
Scott James Remnant3401ab72006-09-01 02:14:47 +0100545 }
546
547 /* Argument list */
Scott James Remnant1db88042006-09-01 03:14:19 +0100548 execl (argv0, argv0, "--restart", NULL);
Scott James Remnant6f464962006-09-01 04:10:20 +0100549 nih_error_raise_system ();
Scott James Remnant3401ab72006-09-01 02:14:47 +0100550
551error:
Scott James Remnant6f464962006-09-01 04:10:20 +0100552 err = nih_error_get ();
553 nih_error (_("Failed to re-execute %s: %s"), argv0, err->message);
Scott James Remnant28fcc922006-09-01 04:15:57 +0100554 nih_free (err);
Scott James Remnant6f464962006-09-01 04:10:20 +0100555
Scott James Remnant3401ab72006-09-01 02:14:47 +0100556 close (fds[0]);
Scott James Remnant6f464962006-09-01 04:10:20 +0100557 close (fds[1]);
558
Scott James Remnant3401ab72006-09-01 02:14:47 +0100559 sigprocmask (SIG_SETMASK, &oldmask, NULL);
560}
561
562/**
Scott James Remnant3401ab72006-09-01 02:14:47 +0100563 * read_state:
564 * @fd: file descriptor to read from.
565 *
566 * Read event and job state from @fd, which is a trivial line-based
567 * protocol that we can keep the same without too much difficultly. It's
568 * tempting to use the control sockets for this, but they break too often.
569 **/
570static void
571read_state (int fd)
572{
Scott James Remnantfcc9a0d2007-02-07 15:41:57 +0000573 Job *job = NULL;
574 EventEmission *emission = NULL;
575 FILE *state;
576 char buf[80];
Scott James Remnant3401ab72006-09-01 02:14:47 +0100577
578 nih_debug ("Reading state");
579
580 /* Use stdio as it's a light-weight thing that won't change */
581 state = fdopen (fd, "r");
582 if (! state) {
583 nih_warn (_("Unable to read from state descriptor: %s"),
584 strerror (errno));
585 return;
586 }
587
588 /* It's just a series of simple lines; if one begins Job then it
589 * indicates the start of a Job description, otherwise if it
590 * begins Event then it's the start of an Event description.
591 *
592 * Lines beginning "." are assumed to belong to the current job
593 * or event.
594 */
595 while (fgets (buf, sizeof (buf), state)) {
596 char *ptr;
597
598 /* Strip newline */
599 ptr = strchr (buf, '\n');
600 if (ptr)
601 *ptr = '\0';
602
603 if (! strncmp (buf, "Job ", 4)) {
604 job = job_read_state (NULL, buf);
Scott James Remnantfcc9a0d2007-02-07 15:41:57 +0000605 emission = NULL;
606 } else if (! strncmp (buf, "Event ", 6)) {
607 emission = event_read_state (NULL, buf);
608 job = NULL;
609 } else if (! strncmp (buf, "Emission ", 9)) {
Scott James Remnantab132da2007-02-07 15:48:02 +0000610 emission = event_read_state (NULL, buf);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100611 job = NULL;
612 } else if (buf[0] == '.') {
613 if (job) {
614 job = job_read_state (job, buf);
Scott James Remnantfcc9a0d2007-02-07 15:41:57 +0000615 } else if (emission) {
616 emission = event_read_state (emission, buf);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100617 }
618 } else {
Scott James Remnantfcc9a0d2007-02-07 15:41:57 +0000619 emission = NULL;
Scott James Remnant3401ab72006-09-01 02:14:47 +0100620 job = NULL;
621 }
622 }
623
624 if (fclose (state))
625 nih_warn (_("Error after reading state: %s"),
626 strerror (errno));
627
628 nih_debug ("State read from parent");
629}
630
631/**
632 * write_state:
633 * @fd: file descriptor to write to.
634 *
635 * Write event and job state to @fd, which is a trivial line-based
636 * protocol that we can keep the same without too much difficultly. It's
637 * tempting to use the control sockets for this, but they break too often.
638 **/
639static void
640write_state (int fd)
641{
642 FILE *state;
643
644 /* Use stdio as it's a light-weight thing that won't change */
645 state = fdopen (fd, "w");
646 if (! state) {
647 nih_warn (_("Unable to write to state descriptor: %s"),
648 strerror (errno));
649 return;
650 }
651
652 event_write_state (state);
653 job_write_state (state);
654
655 if (fclose (state))
656 nih_warn (_("Error after writing state: %s"),
657 strerror (errno));
658}
Scott James Remnantb1db7a32007-02-07 16:33:27 +0000659#endif