blob: 51f022adc58bd29c5c73431063b2b7399f9e1d2c [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 Remnantf43bdf32006-08-27 18:20:29 +010063/* Prototypes for static functions */
Scott James Remnant3401ab72006-09-01 02:14:47 +010064static void reset_console (void);
Scott James Remnant16a286f2007-01-10 15:38:33 +000065static void crash_handler (int signum);
Scott James Remnant3401ab72006-09-01 02:14:47 +010066static void cad_handler (void *data, NihSignal *signal);
67static void kbd_handler (void *data, NihSignal *signal);
68static void stop_handler (void *data, NihSignal *signal);
Scott James Remnant1db88042006-09-01 03:14:19 +010069static void term_handler (const char *prog, NihSignal *signal);
Scott James Remnant3401ab72006-09-01 02:14:47 +010070
Scott James Remnant1db88042006-09-01 03:14:19 +010071
Scott James Remnant3401ab72006-09-01 02:14:47 +010072/**
Scott James Remnant1db88042006-09-01 03:14:19 +010073 * restart:
Scott James Remnant3401ab72006-09-01 02:14:47 +010074 *
Scott James Remnant8b227402006-10-11 17:55:27 +010075 * This is set to TRUE if we're being re-exec'd by an existing init
Scott James Remnant1db88042006-09-01 03:14:19 +010076 * process.
Scott James Remnant3401ab72006-09-01 02:14:47 +010077 **/
Scott James Remnant1db88042006-09-01 03:14:19 +010078static int restart = FALSE;
Scott James Remnant3401ab72006-09-01 02:14:47 +010079
80
81/**
82 * options:
83 *
84 * Command-line options we accept.
85 **/
86static NihOption options[] = {
Scott James Remnant1db88042006-09-01 03:14:19 +010087 { 0, "restart", NULL, NULL, NULL, &restart, NULL },
Scott James Remnant3401ab72006-09-01 02:14:47 +010088
89 /* Ignore invalid options */
90 { '-', "--", NULL, NULL, NULL, NULL, NULL },
91
92 NIH_OPTION_LAST
93};
Scott James Remnantff0d26a2006-08-31 20:49:43 +010094
Scott James Remnantf43bdf32006-08-27 18:20:29 +010095
Scott James Remnant50748842006-05-16 21:02:31 +010096int
97main (int argc,
98 char *argv[])
99{
Scott James Remnant3401ab72006-09-01 02:14:47 +0100100 char **args;
Scott James Remnant2e204b72007-02-03 23:15:28 +0000101 int ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100102
Scott James Remnant77e8db32006-08-21 08:47:50 +0200103 nih_main_init (argv[0]);
104
Scott James Remnant930e25a2006-10-13 12:28:05 +0100105 nih_option_set_synopsis (_("Process management daemon."));
Scott James Remnant462734c2006-10-13 13:36:00 +0100106 nih_option_set_help (
107 _("This daemon is normally executed by the kernel and given "
108 "process id 1 to denote its special status. When executed "
109 "by a user process, it will actually run /sbin/telinit."));
Scott James Remnanta6ed7eb2006-10-13 12:14:45 +0100110
Scott James Remnant3401ab72006-09-01 02:14:47 +0100111 args = nih_option_parser (NULL, argc, argv, options, FALSE);
112 if (! args)
113 exit (1);
Scott James Remnant12a330f2006-08-24 02:19:09 +0200114
Scott James Remnanta17917d2006-09-07 00:18:28 +0100115 /* Check we're root */
116 if (getuid ()) {
117 nih_error (_("Need to be root"));
118 exit (1);
119 }
120
121 /* Check we're process #1 */
122 if (getpid () > 1) {
Scott James Remnante0d0dd12006-09-14 10:51:05 +0100123 execv (TELINIT, argv);
Scott James Remnanta17917d2006-09-07 00:18:28 +0100124 /* Ignore failure, probably just that telinit doesn't exist */
125
126 nih_error (_("Not being executed as init"));
127 exit (1);
128 }
129
Scott James Remnant2e204b72007-02-03 23:15:28 +0000130 /* Open control socket */
131 if (! control_open ()) {
132 NihError *err;
133
134 err = nih_error_get ();
135 nih_error ("%s: %s", _("Unable to open control socket"),
136 err->message);
137 nih_free (err);
138
139 exit (1);
140 }
141
142 /* Read configuration */
143 if (cfg_watch_dir (CFG_DIR) < 0) {
144 NihError *err;
145
146 err = nih_error_get ();
147 nih_error ("%s: %s", _("Error parsing configuration"),
148 err->message);
149 nih_free (err);
150
151 exit (1);
152 }
153
Scott James Remnant78626fb2007-01-10 16:48:10 +0000154
155 /* Clear our arguments from the command-line, so that we show up in
156 * ps or top output as /sbin/init, with no extra flags.
157 *
158 * This is a very Linux-specific trick; by deleting the NULL
159 * terminator at the end of the last argument, we fool the kernel
160 * into believing we used a setproctitle()-a-like to extend the
161 * argument space into the environment space, and thus make it use
162 * strlen() instead of its own assumed length. In fact, we've done
163 * the exact opposite, and shrunk the command line length to just that
164 * of whatever is in argv[0].
165 *
166 * If we don't do this, and just write \0 over the rest of argv, for
167 * example; the command-line length still includes those \0s, and ps
168 * will show whitespace in their place.
169 */
170 if (argc > 1) {
Scott James Remnant505f9282007-01-10 18:44:38 +0000171 char *arg_end;
Scott James Remnant78626fb2007-01-10 16:48:10 +0000172
Scott James Remnant505f9282007-01-10 18:44:38 +0000173 arg_end = argv[argc-1] + strlen (argv[argc-1]);
174 *arg_end = ' ';
Scott James Remnant78626fb2007-01-10 16:48:10 +0000175 }
Scott James Remnant7f4db422007-01-09 20:51:08 +0000176
Scott James Remnant2e204b72007-02-03 23:15:28 +0000177 /* Become session and process group leader (should be already,
178 * but you never know what initramfs did
179 */
180 setsid ();
Scott James Remnanta17917d2006-09-07 00:18:28 +0100181
Scott James Remnant3401ab72006-09-01 02:14:47 +0100182 /* Send all logging output to syslog */
183 openlog (program_name, LOG_CONS, LOG_DAEMON);
Scott James Remnant12a330f2006-08-24 02:19:09 +0200184 nih_log_set_logger (nih_logger_syslog);
Scott James Remnant77e8db32006-08-21 08:47:50 +0200185
Scott James Remnant77e8db32006-08-21 08:47:50 +0200186 /* Close any file descriptors we inherited, and open the console
Scott James Remnant3401ab72006-09-01 02:14:47 +0100187 * device instead. Normally we reset the console, unless we're
188 * inheriting one from another init process.
Scott James Remnant77e8db32006-08-21 08:47:50 +0200189 */
Scott James Remnant2e204b72007-02-03 23:15:28 +0000190 for (int i = 0; i < 3; i++)
Scott James Remnant77e8db32006-08-21 08:47:50 +0200191 close (i);
192
Scott James Remnant5d702952007-01-05 17:21:34 +0000193 if (process_setup_console (NULL, CONSOLE_OUTPUT) < 0)
194 nih_free (nih_error_get ());
Scott James Remnant1db88042006-09-01 03:14:19 +0100195 if (! restart)
Scott James Remnant3401ab72006-09-01 02:14:47 +0100196 reset_console ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200197
Scott James Remnant2e204b72007-02-03 23:15:28 +0000198 /* Set the PATH environment variable */
199 setenv ("PATH", PATH, TRUE);
200
201
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100202 /* Reset the signal state and install the signal handler for those
203 * signals we actually want to catch; this also sets those that
204 * can be sent to us, because we're special
205 */
Scott James Remnant2e204b72007-02-03 23:15:28 +0000206 if (! restart)
207 nih_signal_reset ();
208
209 nih_signal_set_handler (SIGCHLD, nih_signal_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100210 nih_signal_set_handler (SIGALRM, nih_signal_handler);
211 nih_signal_set_handler (SIGHUP, nih_signal_handler);
Scott James Remnanteabb7802006-08-31 15:39:04 +0100212 nih_signal_set_handler (SIGTSTP, nih_signal_handler);
213 nih_signal_set_handler (SIGCONT, nih_signal_handler);
Scott James Remnant1db88042006-09-01 03:14:19 +0100214 nih_signal_set_handler (SIGTERM, nih_signal_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100215 nih_signal_set_handler (SIGINT, nih_signal_handler);
216 nih_signal_set_handler (SIGWINCH, nih_signal_handler);
Scott James Remnant16a286f2007-01-10 15:38:33 +0000217 nih_signal_set_handler (SIGSEGV, crash_handler);
218 nih_signal_set_handler (SIGABRT, crash_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100219
Scott James Remnanteabb7802006-08-31 15:39:04 +0100220 /* Ensure that we don't process events while paused */
Scott James Remnant5d702952007-01-05 17:21:34 +0000221 NIH_MUST (nih_signal_add_handler (NULL, SIGTSTP, stop_handler, NULL));
222 NIH_MUST (nih_signal_add_handler (NULL, SIGCONT, stop_handler, NULL));
Scott James Remnanteabb7802006-08-31 15:39:04 +0100223
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100224 /* Ask the kernel to send us SIGINT when control-alt-delete is
225 * pressed; generate an event with the same name.
226 */
227 reboot (RB_DISABLE_CAD);
Scott James Remnant5d702952007-01-05 17:21:34 +0000228 NIH_MUST (nih_signal_add_handler (NULL, SIGINT, cad_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100229
230 /* Ask the kernel to send us SIGWINCH when alt-uparrow is pressed;
231 * generate a kbdrequest event.
232 */
Scott James Remnant5d702952007-01-05 17:21:34 +0000233 if (ioctl (0, KDSIGACCEPT, SIGWINCH) == 0)
234 NIH_MUST (nih_signal_add_handler (NULL, SIGWINCH,
235 kbd_handler, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100236
Scott James Remnant1db88042006-09-01 03:14:19 +0100237 /* SIGTERM instructs us to re-exec ourselves */
Scott James Remnant5d702952007-01-05 17:21:34 +0000238 NIH_MUST (nih_signal_add_handler (NULL, SIGTERM,
239 (NihSignalHandler)term_handler,
240 argv[0]));
Scott James Remnant1db88042006-09-01 03:14:19 +0100241
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100242 /* Reap all children that die */
Scott James Remnant5d702952007-01-05 17:21:34 +0000243 NIH_MUST (nih_child_add_watch (NULL, -1, job_child_reaper, NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100244
Scott James Remnant65906602007-02-08 02:51:39 +0000245 /* Process the event queue and check the jobs to make sure we
246 * haven't stalled, every time through the main loop */
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000247 NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)event_poll,
Scott James Remnant5d702952007-01-05 17:21:34 +0000248 NULL));
Scott James Remnantc6740472007-02-09 21:16:28 +0000249 NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)job_detect_stalled,
250 NULL));
251 NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)job_free_deleted,
Scott James Remnant5d702952007-01-05 17:21:34 +0000252 NULL));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100253
Scott James Remnant94d00982006-08-25 15:38:22 +0200254
Scott James Remnant3401ab72006-09-01 02:14:47 +0100255 /* Generate and run the startup event or read the state from the
256 * init daemon that exec'd us
257 */
Scott James Remnant1db88042006-09-01 03:14:19 +0100258 if (! restart) {
Scott James Remnant71b91fc2007-02-08 01:07:56 +0000259 event_emit (STARTUP_EVENT, NULL, NULL);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100260 } else {
Scott James Remnant1db88042006-09-01 03:14:19 +0100261 sigset_t mask;
262
Scott James Remnant1db88042006-09-01 03:14:19 +0100263 /* We're ok to receive signals again */
264 sigemptyset (&mask);
265 sigprocmask (SIG_SETMASK, &mask, NULL);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100266 }
267
Scott James Remnanta39da0f2007-02-07 13:52:42 +0000268 /* Run through the loop at least once */
269 nih_main_loop_interrupt ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200270 ret = nih_main_loop ();
271
272 return ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100273}
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100274
275
276/**
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100277 * reset_console:
278 *
279 * Set up the console flags to something sensible. Cribbed from sysvinit,
280 * initng, etc.
281 **/
282static void
283reset_console (void)
284{
Scott James Remnant4bc5fe32006-08-31 21:48:33 +0100285 struct termios tty;
286
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100287 tcgetattr (0, &tty);
288
289 tty.c_cflag &= (CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD);
290 tty.c_cflag |= (HUPCL | CLOCAL | CREAD);
291
292 /* Set up usual keys */
293 tty.c_cc[VINTR] = 3; /* ^C */
294 tty.c_cc[VQUIT] = 28; /* ^\ */
295 tty.c_cc[VERASE] = 127;
296 tty.c_cc[VKILL] = 24; /* ^X */
297 tty.c_cc[VEOF] = 4; /* ^D */
298 tty.c_cc[VTIME] = 0;
299 tty.c_cc[VMIN] = 1;
300 tty.c_cc[VSTART] = 17; /* ^Q */
301 tty.c_cc[VSTOP] = 19; /* ^S */
302 tty.c_cc[VSUSP] = 26; /* ^Z */
303
304 /* Pre and post processing */
305 tty.c_iflag = (IGNPAR | ICRNL | IXON | IXANY);
306 tty.c_oflag = (OPOST | ONLCR);
307 tty.c_lflag = (ISIG | ICANON | ECHO | ECHOCTL | ECHOPRT | ECHOKE);
308
309 /* Set the terminal line and flush it */
310 tcsetattr (0, TCSANOW, &tty);
311 tcflush (0, TCIOFLUSH);
312}
313
314
315/**
Scott James Remnant16a286f2007-01-10 15:38:33 +0000316 * crash_handler:
Scott James Remnant8b227402006-10-11 17:55:27 +0100317 * @signum: signal number received.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100318 *
Scott James Remnant16a286f2007-01-10 15:38:33 +0000319 * Handle receiving the SEGV or ABRT signal, usually caused by one of
320 * our own mistakes. We deal with it by dumping core in a child process
321 * and just carrying on in the parent.
322 *
323 * This may or may not work, but the only alternative would be sigjmp()ing
324 * to somewhere "safe" leaving inconsistent state everywhere (like dangling
325 * lists pointers) or exec'ing another process (which we couldn't transfer
326 * our state to anyway). This just hopes that the kernel resumes on the
327 * next instruction.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100328 **/
329static void
Scott James Remnant16a286f2007-01-10 15:38:33 +0000330crash_handler (int signum)
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100331{
332 pid_t pid;
333
334 pid = fork ();
335 if (pid == 0) {
336 struct sigaction act;
337 struct rlimit limit;
338 sigset_t mask;
339
340 /* Mask out all signals */
341 sigfillset (&mask);
342 sigprocmask (SIG_SETMASK, &mask, NULL);
343
Scott James Remnant16a286f2007-01-10 15:38:33 +0000344 /* Set the handler to the default so core is dumped */
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100345 act.sa_handler = SIG_DFL;
346 act.sa_flags = 0;
347 sigemptyset (&act.sa_mask);
Scott James Remnant16a286f2007-01-10 15:38:33 +0000348 sigaction (signum, &act, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100349
350 /* Dump in the root directory */
351 chdir ("/");
352
353 /* Don't limit the core dump size */
354 limit.rlim_cur = RLIM_INFINITY;
355 limit.rlim_max = RLIM_INFINITY;
356 setrlimit (RLIMIT_CORE, &limit);
357
Scott James Remnant16a286f2007-01-10 15:38:33 +0000358 /* Raise the signal again */
359 raise (signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100360
361 /* Unmask so that we receive it */
Scott James Remnant16a286f2007-01-10 15:38:33 +0000362 sigdelset (&mask, signum);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100363 sigprocmask (SIG_SETMASK, &mask, NULL);
364
365 /* Wait for death */
366 pause ();
367 exit (0);
368 } else if (pid > 0) {
369 /* Wait for the core to be generated */
370 waitpid (pid, NULL, 0);
371
Scott James Remnant16a286f2007-01-10 15:38:33 +0000372 nih_error (_("Caught %s, core dumped"),
Scott James Remnantde443012007-01-10 18:45:40 +0000373 (signum == SIGSEGV
374 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100375 } else {
Scott James Remnant16a286f2007-01-10 15:38:33 +0000376 nih_error (_("Caught %s, unable to dump core"),
Scott James Remnantde443012007-01-10 18:45:40 +0000377 (signum == SIGSEGV
378 ? "segmentation fault" : "abort"));
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100379 }
380}
381
382/**
383 * cad_handler:
384 * @data: unused,
385 * @signal: signal that called this handler.
386 *
387 * Handle having recieved the SIGINT signal, sent to us when somebody
388 * presses Ctrl-Alt-Delete on the console. We just generate a
Scott James Remnantbb3cc3f2006-09-08 17:17:47 +0100389 * ctrlaltdel event.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100390 **/
391static void
392cad_handler (void *data,
393 NihSignal *signal)
394{
Scott James Remnant71b91fc2007-02-08 01:07:56 +0000395 event_emit (CTRLALTDEL_EVENT, NULL, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100396}
397
398/**
399 * kbd_handler:
400 * @data: unused,
401 * @signal: signal that called this handler.
402 *
403 * Handle having recieved the SIGWINCH signal, sent to us when somebody
404 * presses Alt-UpArrow on the console. We just generate a
405 * kbdrequest event.
406 **/
407static void
408kbd_handler (void *data,
409 NihSignal *signal)
410{
Scott James Remnant71b91fc2007-02-08 01:07:56 +0000411 event_emit (KBDREQUEST_EVENT, NULL, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100412}
Scott James Remnanteabb7802006-08-31 15:39:04 +0100413
414/**
415 * stop_handler:
416 * @data: unused,
417 * @signal: signal caught.
418 *
419 * This is called when we receive the STOP, TSTP or CONT signals; we
420 * adjust the paused variable appropriately so that the event queue and
Scott James Remnant65906602007-02-08 02:51:39 +0000421 * job stalled detection is not run.
Scott James Remnanteabb7802006-08-31 15:39:04 +0100422 **/
423static void
424stop_handler (void *data,
425 NihSignal *signal)
426{
427 nih_assert (signal != NULL);
428
429 if (signal->signum == SIGCONT) {
430 nih_info (_("Event queue resumed"));
431 paused = FALSE;
432 } else {
433 nih_info (_("Event queue paused"));
434 paused = TRUE;
435 }
436}
Scott James Remnant3401ab72006-09-01 02:14:47 +0100437
438
439/**
Scott James Remnant1db88042006-09-01 03:14:19 +0100440 * term_handler:
Scott James Remnant3401ab72006-09-01 02:14:47 +0100441 * @argv0: program to run,
442 * @signal: signal caught.
443 *
Scott James Remnant1db88042006-09-01 03:14:19 +0100444 * This is called when we receive the TERM signal, which instructs us
Scott James Remnant3401ab72006-09-01 02:14:47 +0100445 * to reexec ourselves.
446 **/
447static void
Scott James Remnant1db88042006-09-01 03:14:19 +0100448term_handler (const char *argv0,
Scott James Remnant3401ab72006-09-01 02:14:47 +0100449 NihSignal *signal)
450{
Scott James Remnant6f464962006-09-01 04:10:20 +0100451 NihError *err;
Scott James Remnant3401ab72006-09-01 02:14:47 +0100452 sigset_t mask, oldmask;
Scott James Remnant3401ab72006-09-01 02:14:47 +0100453
454 nih_assert (argv0 != NULL);
455 nih_assert (signal != NULL);
456
457 nih_warn (_("Re-executing %s"), argv0);
458
Scott James Remnant1db88042006-09-01 03:14:19 +0100459 /* Block signals while we work. We're the last signal handler
460 * installed so this should mean that they're all handled now.
461 *
462 * The child must make sure that it unblocks these again when
463 * it's ready.
464 */
Scott James Remnant3401ab72006-09-01 02:14:47 +0100465 sigfillset (&mask);
466 sigprocmask (SIG_BLOCK, &mask, &oldmask);
467
Scott James Remnant3401ab72006-09-01 02:14:47 +0100468 /* Argument list */
Scott James Remnant1db88042006-09-01 03:14:19 +0100469 execl (argv0, argv0, "--restart", NULL);
Scott James Remnant6f464962006-09-01 04:10:20 +0100470 nih_error_raise_system ();
Scott James Remnant3401ab72006-09-01 02:14:47 +0100471
Scott James Remnant6f464962006-09-01 04:10:20 +0100472 err = nih_error_get ();
473 nih_error (_("Failed to re-execute %s: %s"), argv0, err->message);
Scott James Remnant28fcc922006-09-01 04:15:57 +0100474 nih_free (err);
Scott James Remnant6f464962006-09-01 04:10:20 +0100475
Scott James Remnant3401ab72006-09-01 02:14:47 +0100476 sigprocmask (SIG_SETMASK, &oldmask, NULL);
477}