blob: 81eb2a3769fe10463ca9b9bb2fe0ebe7dd27c481 [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 <sys/types.h>
27#include <sys/wait.h>
28#include <sys/ioctl.h>
29#include <sys/reboot.h>
30#include <sys/resource.h>
31
Scott James Remnant3401ab72006-09-01 02:14:47 +010032#include <errno.h>
33#include <stdio.h>
Scott James Remnantf43bdf32006-08-27 18:20:29 +010034#include <signal.h>
Scott James Remnant94d00982006-08-25 15:38:22 +020035#include <stdlib.h>
Scott James Remnant3401ab72006-09-01 02:14:47 +010036#include <string.h>
Scott James Remnant12a330f2006-08-24 02:19:09 +020037#include <syslog.h>
Scott James Remnant027dd7b2006-08-21 09:01:25 +020038#include <unistd.h>
Scott James Remnantff0d26a2006-08-31 20:49:43 +010039#include <termios.h>
Scott James Remnant027dd7b2006-08-21 09:01:25 +020040
Scott James Remnant097b2a92006-09-01 19:30:37 +010041#include <linux/kd.h>
42
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>
Scott James Remnant28fcc922006-09-01 04:15:57 +010051#include <nih/error.h>
Scott James Remnant77e8db32006-08-21 08:47:50 +020052#include <nih/logging.h>
53
54#include "process.h"
55#include "job.h"
56#include "event.h"
57#include "control.h"
58#include "cfgfile.h"
Scott James Remnante0d0dd12006-09-14 10:51:05 +010059#include "paths.h"
Scott James Remnant50748842006-05-16 21:02:31 +010060
61
Scott James Remnant1db88042006-09-01 03:14:19 +010062/**
63 * STATE_FD:
64 *
65 * File descriptor we read our state from.
66 **/
67#define STATE_FD 101
68
69
Scott James Remnantf43bdf32006-08-27 18:20:29 +010070/* Prototypes for static functions */
Scott James Remnant3401ab72006-09-01 02:14:47 +010071static void reset_console (void);
72static void segv_handler (int signum);
73static void cad_handler (void *data, NihSignal *signal);
74static void kbd_handler (void *data, NihSignal *signal);
75static void stop_handler (void *data, NihSignal *signal);
Scott James Remnant1db88042006-09-01 03:14:19 +010076static void term_handler (const char *prog, NihSignal *signal);
Scott James Remnant3401ab72006-09-01 02:14:47 +010077static void read_state (int fd);
78static void write_state (int fd);
79
Scott James Remnant1db88042006-09-01 03:14:19 +010080
Scott James Remnant3401ab72006-09-01 02:14:47 +010081/**
Scott James Remnant1db88042006-09-01 03:14:19 +010082 * restart:
Scott James Remnant3401ab72006-09-01 02:14:47 +010083 *
Scott James Remnant8b227402006-10-11 17:55:27 +010084 * This is set to TRUE if we're being re-exec'd by an existing init
Scott James Remnant1db88042006-09-01 03:14:19 +010085 * process.
Scott James Remnant3401ab72006-09-01 02:14:47 +010086 **/
Scott James Remnant1db88042006-09-01 03:14:19 +010087static int restart = FALSE;
Scott James Remnant3401ab72006-09-01 02:14:47 +010088
89
90/**
91 * options:
92 *
93 * Command-line options we accept.
94 **/
95static NihOption options[] = {
Scott James Remnant1db88042006-09-01 03:14:19 +010096 { 0, "restart", NULL, NULL, NULL, &restart, NULL },
Scott James Remnant3401ab72006-09-01 02:14:47 +010097
98 /* Ignore invalid options */
99 { '-', "--", NULL, NULL, NULL, NULL, NULL },
100
101 NIH_OPTION_LAST
102};
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100103
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100104
Scott James Remnant50748842006-05-16 21:02:31 +0100105int
106main (int argc,
107 char *argv[])
108{
Scott James Remnant3401ab72006-09-01 02:14:47 +0100109 char **args;
110 int ret, i;
Scott James Remnant50748842006-05-16 21:02:31 +0100111
Scott James Remnant77e8db32006-08-21 08:47:50 +0200112 nih_main_init (argv[0]);
113
Scott James Remnant930e25a2006-10-13 12:28:05 +0100114 nih_option_set_synopsis (_("Process management daemon."));
Scott James Remnant462734c2006-10-13 13:36:00 +0100115 nih_option_set_help (
116 _("This daemon is normally executed by the kernel and given "
117 "process id 1 to denote its special status. When executed "
118 "by a user process, it will actually run /sbin/telinit."));
Scott James Remnanta6ed7eb2006-10-13 12:14:45 +0100119
Scott James Remnant3401ab72006-09-01 02:14:47 +0100120 args = nih_option_parser (NULL, argc, argv, options, FALSE);
121 if (! args)
122 exit (1);
Scott James Remnant12a330f2006-08-24 02:19:09 +0200123
Scott James Remnanta17917d2006-09-07 00:18:28 +0100124 /* Check we're root */
125 if (getuid ()) {
126 nih_error (_("Need to be root"));
127 exit (1);
128 }
129
130 /* Check we're process #1 */
131 if (getpid () > 1) {
Scott James Remnante0d0dd12006-09-14 10:51:05 +0100132 execv (TELINIT, argv);
Scott James Remnanta17917d2006-09-07 00:18:28 +0100133 /* Ignore failure, probably just that telinit doesn't exist */
134
135 nih_error (_("Not being executed as init"));
136 exit (1);
137 }
138
139
Scott James Remnant3401ab72006-09-01 02:14:47 +0100140 /* Send all logging output to syslog */
141 openlog (program_name, LOG_CONS, LOG_DAEMON);
Scott James Remnant12a330f2006-08-24 02:19:09 +0200142 nih_log_set_logger (nih_logger_syslog);
Scott James Remnant77e8db32006-08-21 08:47:50 +0200143
Scott James Remnant77e8db32006-08-21 08:47:50 +0200144 /* Close any file descriptors we inherited, and open the console
Scott James Remnant3401ab72006-09-01 02:14:47 +0100145 * device instead. Normally we reset the console, unless we're
146 * inheriting one from another init process.
Scott James Remnant77e8db32006-08-21 08:47:50 +0200147 */
148 for (i = 0; i < 3; i++)
149 close (i);
150
Scott James Remnant9fb20d42006-09-09 01:22:03 +0100151 process_setup_console (NULL, CONSOLE_OUTPUT);
Scott James Remnant1db88042006-09-01 03:14:19 +0100152 if (! restart)
Scott James Remnant3401ab72006-09-01 02:14:47 +0100153 reset_console ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200154
Scott James Remnantf4970812006-08-27 18:27:19 +0100155
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100156 /* Reset the signal state and install the signal handler for those
157 * signals we actually want to catch; this also sets those that
158 * can be sent to us, because we're special
159 */
160 nih_signal_reset ();
161 nih_signal_set_handler (SIGALRM, nih_signal_handler);
162 nih_signal_set_handler (SIGHUP, nih_signal_handler);
Scott James Remnanteabb7802006-08-31 15:39:04 +0100163 nih_signal_set_handler (SIGTSTP, nih_signal_handler);
164 nih_signal_set_handler (SIGCONT, nih_signal_handler);
Scott James Remnant1db88042006-09-01 03:14:19 +0100165 nih_signal_set_handler (SIGTERM, nih_signal_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100166 nih_signal_set_handler (SIGINT, nih_signal_handler);
167 nih_signal_set_handler (SIGWINCH, nih_signal_handler);
168 nih_signal_set_handler (SIGSEGV, segv_handler);
169
Scott James Remnanteabb7802006-08-31 15:39:04 +0100170 /* Ensure that we don't process events while paused */
Scott James Remnantc34c4be2006-10-11 17:56:34 +0100171 nih_signal_add_handler (NULL, SIGTSTP, stop_handler, NULL);
172 nih_signal_add_handler (NULL, SIGCONT, stop_handler, NULL);
Scott James Remnanteabb7802006-08-31 15:39:04 +0100173
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100174 /* Ask the kernel to send us SIGINT when control-alt-delete is
175 * pressed; generate an event with the same name.
176 */
177 reboot (RB_DISABLE_CAD);
Scott James Remnantc34c4be2006-10-11 17:56:34 +0100178 nih_signal_add_handler (NULL, SIGINT, cad_handler, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100179
180 /* Ask the kernel to send us SIGWINCH when alt-uparrow is pressed;
181 * generate a kbdrequest event.
182 */
183 ioctl (0, KDSIGACCEPT, SIGWINCH);
Scott James Remnantc34c4be2006-10-11 17:56:34 +0100184 nih_signal_add_handler (NULL, SIGWINCH, kbd_handler, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100185
Scott James Remnant1db88042006-09-01 03:14:19 +0100186 /* SIGTERM instructs us to re-exec ourselves */
Scott James Remnantc34c4be2006-10-11 17:56:34 +0100187 nih_signal_add_handler (NULL, SIGTERM,
188 (NihSignalHandler)term_handler, argv[0]);
Scott James Remnant1db88042006-09-01 03:14:19 +0100189
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100190
191 /* Reap all children that die */
192 nih_child_add_watch (NULL, -1, job_child_reaper, NULL);
193
Scott James Remnantdc8877d2006-08-29 16:56:48 +0100194 /* Process the event queue and check the jobs for idleness
195 * every time through the main loop */
Scott James Remnant0c7e72a2006-08-27 22:22:53 +0100196 nih_main_loop_add_func (NULL, (NihMainLoopCb)event_queue_run, NULL);
Scott James Remnantdc8877d2006-08-29 16:56:48 +0100197 nih_main_loop_add_func (NULL, (NihMainLoopCb)job_detect_idle, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100198
Scott James Remnant94d00982006-08-25 15:38:22 +0200199
Scott James Remnant77e8db32006-08-21 08:47:50 +0200200 /* Become session and process group leader (should be already,
201 * but you never know what initramfs did
202 */
203 setsid ();
204
205 /* Open control socket */
206 control_open ();
207
Scott James Remnantceaaf4d2006-08-24 01:40:10 +0200208 /* Read configuration */
Scott James Remnant987bcc42006-09-07 00:38:24 +0100209 cfg_watch_dir (NULL, CFG_DIR, NULL);
Scott James Remnantceaaf4d2006-08-24 01:40:10 +0200210
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100211 /* Set the PATH environment variable */
212 setenv ("PATH", PATH, TRUE);
213
Scott James Remnant77e8db32006-08-21 08:47:50 +0200214
Scott James Remnant3401ab72006-09-01 02:14:47 +0100215 /* Generate and run the startup event or read the state from the
216 * init daemon that exec'd us
217 */
Scott James Remnant1db88042006-09-01 03:14:19 +0100218 if (! restart) {
Scott James Remnantb4a1c2b2006-09-09 01:28:26 +0100219 Job *logd;
220
221 /* FIXME this is a bit of a hack, should have a list of
222 * essential services or something
223 */
224 logd = job_find_by_name ("logd");
Scott James Remnant515b2b92006-09-09 03:41:27 +0100225 if (logd) {
Scott James Remnantb4a1c2b2006-09-09 01:28:26 +0100226 job_start (logd);
Scott James Remnant515b2b92006-09-09 03:41:27 +0100227 if (logd->state == JOB_RUNNING) {
228 /* Hang around until logd signals that it's
229 * listening ... but not too long
230 */
231 alarm (5);
232 waitpid (logd->pid, NULL, WUNTRACED);
233 kill (logd->pid, SIGCONT);
234 alarm (0);
235 }
236 }
Scott James Remnantb4a1c2b2006-09-09 01:28:26 +0100237
Scott James Remnantea204d42006-09-07 00:30:53 +0100238 event_queue (STARTUP_EVENT);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100239 } else {
Scott James Remnant1db88042006-09-01 03:14:19 +0100240 sigset_t mask;
241
242 /* State file descriptor is fixed */
243 read_state (STATE_FD);
244
245 /* We're ok to receive signals again */
246 sigemptyset (&mask);
247 sigprocmask (SIG_SETMASK, &mask, NULL);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100248 }
249
250 /* Run the event queue once, and detect anything idle */
Scott James Remnant0c7e72a2006-08-27 22:22:53 +0100251 event_queue_run ();
Scott James Remnant32de9222006-08-31 04:34:27 +0100252 job_detect_idle ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200253
254 /* Go! */
255 ret = nih_main_loop ();
256
257 return ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100258}
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100259
260
261/**
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100262 * reset_console:
263 *
264 * Set up the console flags to something sensible. Cribbed from sysvinit,
265 * initng, etc.
266 **/
267static void
268reset_console (void)
269{
Scott James Remnant4bc5fe32006-08-31 21:48:33 +0100270 struct termios tty;
271
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100272 tcgetattr (0, &tty);
273
274 tty.c_cflag &= (CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD);
275 tty.c_cflag |= (HUPCL | CLOCAL | CREAD);
276
277 /* Set up usual keys */
278 tty.c_cc[VINTR] = 3; /* ^C */
279 tty.c_cc[VQUIT] = 28; /* ^\ */
280 tty.c_cc[VERASE] = 127;
281 tty.c_cc[VKILL] = 24; /* ^X */
282 tty.c_cc[VEOF] = 4; /* ^D */
283 tty.c_cc[VTIME] = 0;
284 tty.c_cc[VMIN] = 1;
285 tty.c_cc[VSTART] = 17; /* ^Q */
286 tty.c_cc[VSTOP] = 19; /* ^S */
287 tty.c_cc[VSUSP] = 26; /* ^Z */
288
289 /* Pre and post processing */
290 tty.c_iflag = (IGNPAR | ICRNL | IXON | IXANY);
291 tty.c_oflag = (OPOST | ONLCR);
292 tty.c_lflag = (ISIG | ICANON | ECHO | ECHOCTL | ECHOPRT | ECHOKE);
293
294 /* Set the terminal line and flush it */
295 tcsetattr (0, TCSANOW, &tty);
296 tcflush (0, TCIOFLUSH);
297}
298
299
300/**
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100301 * segv_handler:
Scott James Remnant8b227402006-10-11 17:55:27 +0100302 * @signum: signal number received.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100303 *
304 * Handle receiving the SEGV signal, usually caused by one of our own
305 * mistakes. We deal with it by dumping core in a child process and
306 * just carrying on in the parent.
307 **/
308static void
309segv_handler (int signum)
310{
311 pid_t pid;
312
313 pid = fork ();
314 if (pid == 0) {
315 struct sigaction act;
316 struct rlimit limit;
317 sigset_t mask;
318
319 /* Mask out all signals */
320 sigfillset (&mask);
321 sigprocmask (SIG_SETMASK, &mask, NULL);
322
323 /* Set the SEGV handler to the default so core is dumped */
324 act.sa_handler = SIG_DFL;
325 act.sa_flags = 0;
326 sigemptyset (&act.sa_mask);
327 sigaction (SIGSEGV, &act, NULL);
328
329 /* Dump in the root directory */
330 chdir ("/");
331
332 /* Don't limit the core dump size */
333 limit.rlim_cur = RLIM_INFINITY;
334 limit.rlim_max = RLIM_INFINITY;
335 setrlimit (RLIMIT_CORE, &limit);
336
337 /* Raise the signal */
338 raise (SIGSEGV);
339
340 /* Unmask so that we receive it */
341 sigdelset (&mask, SIGSEGV);
342 sigprocmask (SIG_SETMASK, &mask, NULL);
343
344 /* Wait for death */
345 pause ();
346 exit (0);
347 } else if (pid > 0) {
348 /* Wait for the core to be generated */
349 waitpid (pid, NULL, 0);
350
351 nih_error (_("Caught segmentation fault, core dumped"));
352 } else {
353 nih_error (_("Caught segmentation fault, unable to dump core"));
354 }
355}
356
357/**
358 * cad_handler:
359 * @data: unused,
360 * @signal: signal that called this handler.
361 *
362 * Handle having recieved the SIGINT signal, sent to us when somebody
363 * presses Ctrl-Alt-Delete on the console. We just generate a
Scott James Remnantbb3cc3f2006-09-08 17:17:47 +0100364 * ctrlaltdel event.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100365 **/
366static void
367cad_handler (void *data,
368 NihSignal *signal)
369{
Scott James Remnantbb3cc3f2006-09-08 17:17:47 +0100370 event_queue (CTRLALTDEL_EVENT);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100371}
372
373/**
374 * kbd_handler:
375 * @data: unused,
376 * @signal: signal that called this handler.
377 *
378 * Handle having recieved the SIGWINCH signal, sent to us when somebody
379 * presses Alt-UpArrow on the console. We just generate a
380 * kbdrequest event.
381 **/
382static void
383kbd_handler (void *data,
384 NihSignal *signal)
385{
Scott James Remnantbb3cc3f2006-09-08 17:17:47 +0100386 event_queue (KBDREQUEST_EVENT);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100387}
Scott James Remnanteabb7802006-08-31 15:39:04 +0100388
389/**
390 * stop_handler:
391 * @data: unused,
392 * @signal: signal caught.
393 *
394 * This is called when we receive the STOP, TSTP or CONT signals; we
395 * adjust the paused variable appropriately so that the event queue and
396 * job idle detection is not run.
397 **/
398static void
399stop_handler (void *data,
400 NihSignal *signal)
401{
402 nih_assert (signal != NULL);
403
404 if (signal->signum == SIGCONT) {
405 nih_info (_("Event queue resumed"));
406 paused = FALSE;
407 } else {
408 nih_info (_("Event queue paused"));
409 paused = TRUE;
410 }
411}
Scott James Remnant3401ab72006-09-01 02:14:47 +0100412
413
414/**
Scott James Remnant1db88042006-09-01 03:14:19 +0100415 * term_handler:
Scott James Remnant3401ab72006-09-01 02:14:47 +0100416 * @argv0: program to run,
417 * @signal: signal caught.
418 *
Scott James Remnant1db88042006-09-01 03:14:19 +0100419 * This is called when we receive the TERM signal, which instructs us
Scott James Remnant3401ab72006-09-01 02:14:47 +0100420 * to reexec ourselves.
421 **/
422static void
Scott James Remnant1db88042006-09-01 03:14:19 +0100423term_handler (const char *argv0,
Scott James Remnant3401ab72006-09-01 02:14:47 +0100424 NihSignal *signal)
425{
Scott James Remnant6f464962006-09-01 04:10:20 +0100426 NihError *err;
Scott James Remnant3401ab72006-09-01 02:14:47 +0100427 sigset_t mask, oldmask;
428 int fds[2] = { -1, -1 };
429 pid_t pid;
Scott James Remnant3401ab72006-09-01 02:14:47 +0100430
431 nih_assert (argv0 != NULL);
432 nih_assert (signal != NULL);
433
434 nih_warn (_("Re-executing %s"), argv0);
435
Scott James Remnant1db88042006-09-01 03:14:19 +0100436 /* Block signals while we work. We're the last signal handler
437 * installed so this should mean that they're all handled now.
438 *
439 * The child must make sure that it unblocks these again when
440 * it's ready.
441 */
Scott James Remnant3401ab72006-09-01 02:14:47 +0100442 sigfillset (&mask);
443 sigprocmask (SIG_BLOCK, &mask, &oldmask);
444
Scott James Remnant28fcc922006-09-01 04:15:57 +0100445 /* Close the control connection */
446 control_close ();
447
Scott James Remnant3401ab72006-09-01 02:14:47 +0100448 /* Create pipe */
Scott James Remnant6f464962006-09-01 04:10:20 +0100449 if (pipe (fds) < 0) {
450 nih_error_raise_system ();
Scott James Remnant3401ab72006-09-01 02:14:47 +0100451 goto error;
Scott James Remnant6f464962006-09-01 04:10:20 +0100452 }
Scott James Remnant3401ab72006-09-01 02:14:47 +0100453
454 /* Fork a child that can send the state to the new init process */
455 pid = fork ();
456 if (pid < 0) {
Scott James Remnant6f464962006-09-01 04:10:20 +0100457 nih_error_raise_system ();
Scott James Remnant3401ab72006-09-01 02:14:47 +0100458 goto error;
459 } else if (pid == 0) {
Scott James Remnant6f464962006-09-01 04:10:20 +0100460 close (fds[0]);
461
462 write_state (fds[1]);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100463 exit (0);
464 } else {
Scott James Remnant6f464962006-09-01 04:10:20 +0100465 if (dup2 (fds[0], STATE_FD) < 0) {
466 nih_error_raise_system ();
467 goto error;
468 }
469
470 close (fds[0]);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100471 close (fds[1]);
Scott James Remnant6f464962006-09-01 04:10:20 +0100472 fds[0] = fds[1] = -1;
Scott James Remnant3401ab72006-09-01 02:14:47 +0100473 }
474
475 /* Argument list */
Scott James Remnant1db88042006-09-01 03:14:19 +0100476 execl (argv0, argv0, "--restart", NULL);
Scott James Remnant6f464962006-09-01 04:10:20 +0100477 nih_error_raise_system ();
Scott James Remnant3401ab72006-09-01 02:14:47 +0100478
479error:
Scott James Remnant6f464962006-09-01 04:10:20 +0100480 err = nih_error_get ();
481 nih_error (_("Failed to re-execute %s: %s"), argv0, err->message);
Scott James Remnant28fcc922006-09-01 04:15:57 +0100482 nih_free (err);
Scott James Remnant6f464962006-09-01 04:10:20 +0100483
Scott James Remnant3401ab72006-09-01 02:14:47 +0100484 close (fds[0]);
Scott James Remnant6f464962006-09-01 04:10:20 +0100485 close (fds[1]);
486
Scott James Remnant28fcc922006-09-01 04:15:57 +0100487 control_open ();
488
Scott James Remnant3401ab72006-09-01 02:14:47 +0100489 sigprocmask (SIG_SETMASK, &oldmask, NULL);
490}
491
492/**
Scott James Remnant3401ab72006-09-01 02:14:47 +0100493 * read_state:
494 * @fd: file descriptor to read from.
495 *
496 * Read event and job state from @fd, which is a trivial line-based
497 * protocol that we can keep the same without too much difficultly. It's
498 * tempting to use the control sockets for this, but they break too often.
499 **/
500static void
501read_state (int fd)
502{
503 Job *job = NULL;
504 Event *event = NULL;
505 FILE *state;
506 char buf[80];
507
508 nih_debug ("Reading state");
509
510 /* Use stdio as it's a light-weight thing that won't change */
511 state = fdopen (fd, "r");
512 if (! state) {
513 nih_warn (_("Unable to read from state descriptor: %s"),
514 strerror (errno));
515 return;
516 }
517
518 /* It's just a series of simple lines; if one begins Job then it
519 * indicates the start of a Job description, otherwise if it
520 * begins Event then it's the start of an Event description.
521 *
522 * Lines beginning "." are assumed to belong to the current job
523 * or event.
524 */
525 while (fgets (buf, sizeof (buf), state)) {
526 char *ptr;
527
528 /* Strip newline */
529 ptr = strchr (buf, '\n');
530 if (ptr)
531 *ptr = '\0';
532
533 if (! strncmp (buf, "Job ", 4)) {
534 job = job_read_state (NULL, buf);
535 event = NULL;
536 } else if (! strncmp (buf, "Event ", 5)) {
537 event = event_read_state (NULL, buf);
538 job = NULL;
539 } else if (buf[0] == '.') {
540 if (job) {
541 job = job_read_state (job, buf);
542 } else if (event) {
543 event = event_read_state (event, buf);
544 }
545 } else {
546 event = NULL;
547 job = NULL;
548 }
549 }
550
551 if (fclose (state))
552 nih_warn (_("Error after reading state: %s"),
553 strerror (errno));
554
555 nih_debug ("State read from parent");
556}
557
558/**
559 * write_state:
560 * @fd: file descriptor to write to.
561 *
562 * Write event and job state to @fd, which is a trivial line-based
563 * protocol that we can keep the same without too much difficultly. It's
564 * tempting to use the control sockets for this, but they break too often.
565 **/
566static void
567write_state (int fd)
568{
569 FILE *state;
570
571 /* Use stdio as it's a light-weight thing that won't change */
572 state = fdopen (fd, "w");
573 if (! state) {
574 nih_warn (_("Unable to write to state descriptor: %s"),
575 strerror (errno));
576 return;
577 }
578
579 event_write_state (state);
580 job_write_state (state);
581
582 if (fclose (state))
583 nih_warn (_("Error after writing state: %s"),
584 strerror (errno));
585}