blob: 0f389fb46d98c40c70ae2bfe13ef9276e5f35eb4 [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 Remnant50748842006-05-16 21:02:31 +010059
60
Scott James Remnant1db88042006-09-01 03:14:19 +010061/**
62 * STATE_FD:
63 *
64 * File descriptor we read our state from.
65 **/
66#define STATE_FD 101
67
68
Scott James Remnantf43bdf32006-08-27 18:20:29 +010069/* Prototypes for static functions */
Scott James Remnant3401ab72006-09-01 02:14:47 +010070static void reset_console (void);
71static void segv_handler (int signum);
72static void cad_handler (void *data, NihSignal *signal);
73static void kbd_handler (void *data, NihSignal *signal);
74static void stop_handler (void *data, NihSignal *signal);
Scott James Remnant1db88042006-09-01 03:14:19 +010075static void term_handler (const char *prog, NihSignal *signal);
Scott James Remnant3401ab72006-09-01 02:14:47 +010076static void read_state (int fd);
77static void write_state (int fd);
78
Scott James Remnant1db88042006-09-01 03:14:19 +010079
Scott James Remnant3401ab72006-09-01 02:14:47 +010080/**
Scott James Remnant1db88042006-09-01 03:14:19 +010081 * restart:
Scott James Remnant3401ab72006-09-01 02:14:47 +010082 *
Scott James Remnant1db88042006-09-01 03:14:19 +010083 * This is set to %TRUE if we're being re-exec'd by an existing init
84 * process.
Scott James Remnant3401ab72006-09-01 02:14:47 +010085 **/
Scott James Remnant1db88042006-09-01 03:14:19 +010086static int restart = FALSE;
Scott James Remnant3401ab72006-09-01 02:14:47 +010087
88
89/**
90 * options:
91 *
92 * Command-line options we accept.
93 **/
94static NihOption options[] = {
Scott James Remnant1db88042006-09-01 03:14:19 +010095 { 0, "restart", NULL, NULL, NULL, &restart, NULL },
Scott James Remnant3401ab72006-09-01 02:14:47 +010096
97 /* Ignore invalid options */
98 { '-', "--", NULL, NULL, NULL, NULL, NULL },
99
100 NIH_OPTION_LAST
101};
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100102
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100103
Scott James Remnant50748842006-05-16 21:02:31 +0100104int
105main (int argc,
106 char *argv[])
107{
Scott James Remnant3401ab72006-09-01 02:14:47 +0100108 char **args;
109 int ret, i;
Scott James Remnant50748842006-05-16 21:02:31 +0100110
Scott James Remnant77e8db32006-08-21 08:47:50 +0200111 nih_main_init (argv[0]);
112
Scott James Remnant3401ab72006-09-01 02:14:47 +0100113 args = nih_option_parser (NULL, argc, argv, options, FALSE);
114 if (! args)
115 exit (1);
Scott James Remnant12a330f2006-08-24 02:19:09 +0200116
Scott James Remnanta17917d2006-09-07 00:18:28 +0100117 /* Check we're root */
118 if (getuid ()) {
119 nih_error (_("Need to be root"));
120 exit (1);
121 }
122
123 /* Check we're process #1 */
124 if (getpid () > 1) {
125 execv ("/sbin/telinit", argv);
126 /* Ignore failure, probably just that telinit doesn't exist */
127
128 nih_error (_("Not being executed as init"));
129 exit (1);
130 }
131
132
Scott James Remnant3401ab72006-09-01 02:14:47 +0100133 /* Send all logging output to syslog */
134 openlog (program_name, LOG_CONS, LOG_DAEMON);
Scott James Remnant12a330f2006-08-24 02:19:09 +0200135 nih_log_set_logger (nih_logger_syslog);
Scott James Remnant77e8db32006-08-21 08:47:50 +0200136
Scott James Remnant77e8db32006-08-21 08:47:50 +0200137 /* Close any file descriptors we inherited, and open the console
Scott James Remnant3401ab72006-09-01 02:14:47 +0100138 * device instead. Normally we reset the console, unless we're
139 * inheriting one from another init process.
Scott James Remnant77e8db32006-08-21 08:47:50 +0200140 */
141 for (i = 0; i < 3; i++)
142 close (i);
143
Scott James Remnant9fb20d42006-09-09 01:22:03 +0100144 process_setup_console (NULL, CONSOLE_OUTPUT);
Scott James Remnant1db88042006-09-01 03:14:19 +0100145 if (! restart)
Scott James Remnant3401ab72006-09-01 02:14:47 +0100146 reset_console ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200147
Scott James Remnantf4970812006-08-27 18:27:19 +0100148
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100149 /* Reset the signal state and install the signal handler for those
150 * signals we actually want to catch; this also sets those that
151 * can be sent to us, because we're special
152 */
153 nih_signal_reset ();
154 nih_signal_set_handler (SIGALRM, nih_signal_handler);
155 nih_signal_set_handler (SIGHUP, nih_signal_handler);
Scott James Remnanteabb7802006-08-31 15:39:04 +0100156 nih_signal_set_handler (SIGTSTP, nih_signal_handler);
157 nih_signal_set_handler (SIGCONT, nih_signal_handler);
Scott James Remnant1db88042006-09-01 03:14:19 +0100158 nih_signal_set_handler (SIGTERM, nih_signal_handler);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100159 nih_signal_set_handler (SIGINT, nih_signal_handler);
160 nih_signal_set_handler (SIGWINCH, nih_signal_handler);
161 nih_signal_set_handler (SIGSEGV, segv_handler);
162
Scott James Remnanteabb7802006-08-31 15:39:04 +0100163 /* Ensure that we don't process events while paused */
Scott James Remnanteabb7802006-08-31 15:39:04 +0100164 nih_signal_add_callback (NULL, SIGTSTP, stop_handler, NULL);
165 nih_signal_add_callback (NULL, SIGCONT, stop_handler, NULL);
166
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100167 /* Ask the kernel to send us SIGINT when control-alt-delete is
168 * pressed; generate an event with the same name.
169 */
170 reboot (RB_DISABLE_CAD);
171 nih_signal_add_callback (NULL, SIGINT, cad_handler, NULL);
172
173 /* Ask the kernel to send us SIGWINCH when alt-uparrow is pressed;
174 * generate a kbdrequest event.
175 */
176 ioctl (0, KDSIGACCEPT, SIGWINCH);
177 nih_signal_add_callback (NULL, SIGWINCH, kbd_handler, NULL);
178
Scott James Remnant1db88042006-09-01 03:14:19 +0100179 /* SIGTERM instructs us to re-exec ourselves */
180 nih_signal_add_callback (NULL, SIGTERM,
181 (NihSignalCb)term_handler, argv[0]);
182
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100183
184 /* Reap all children that die */
185 nih_child_add_watch (NULL, -1, job_child_reaper, NULL);
186
Scott James Remnantdc8877d2006-08-29 16:56:48 +0100187 /* Process the event queue and check the jobs for idleness
188 * every time through the main loop */
Scott James Remnant0c7e72a2006-08-27 22:22:53 +0100189 nih_main_loop_add_func (NULL, (NihMainLoopCb)event_queue_run, NULL);
Scott James Remnantdc8877d2006-08-29 16:56:48 +0100190 nih_main_loop_add_func (NULL, (NihMainLoopCb)job_detect_idle, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100191
Scott James Remnant94d00982006-08-25 15:38:22 +0200192
Scott James Remnant77e8db32006-08-21 08:47:50 +0200193 /* Become session and process group leader (should be already,
194 * but you never know what initramfs did
195 */
196 setsid ();
197
198 /* Open control socket */
199 control_open ();
200
Scott James Remnantceaaf4d2006-08-24 01:40:10 +0200201 /* Read configuration */
Scott James Remnant987bcc42006-09-07 00:38:24 +0100202 cfg_watch_dir (NULL, CFG_DIR, NULL);
Scott James Remnantceaaf4d2006-08-24 01:40:10 +0200203
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100204 /* Set the PATH environment variable */
205 setenv ("PATH", PATH, TRUE);
206
Scott James Remnant77e8db32006-08-21 08:47:50 +0200207
Scott James Remnant3401ab72006-09-01 02:14:47 +0100208 /* Generate and run the startup event or read the state from the
209 * init daemon that exec'd us
210 */
Scott James Remnant1db88042006-09-01 03:14:19 +0100211 if (! restart) {
Scott James Remnantb4a1c2b2006-09-09 01:28:26 +0100212 Job *logd;
213
214 /* FIXME this is a bit of a hack, should have a list of
215 * essential services or something
216 */
217 logd = job_find_by_name ("logd");
Scott James Remnant515b2b92006-09-09 03:41:27 +0100218 if (logd) {
Scott James Remnantb4a1c2b2006-09-09 01:28:26 +0100219 job_start (logd);
Scott James Remnant515b2b92006-09-09 03:41:27 +0100220 if (logd->state == JOB_RUNNING) {
221 /* Hang around until logd signals that it's
222 * listening ... but not too long
223 */
224 alarm (5);
225 waitpid (logd->pid, NULL, WUNTRACED);
226 kill (logd->pid, SIGCONT);
227 alarm (0);
228 }
229 }
Scott James Remnantb4a1c2b2006-09-09 01:28:26 +0100230
Scott James Remnantea204d42006-09-07 00:30:53 +0100231 event_queue (STARTUP_EVENT);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100232 } else {
Scott James Remnant1db88042006-09-01 03:14:19 +0100233 sigset_t mask;
234
235 /* State file descriptor is fixed */
236 read_state (STATE_FD);
237
238 /* We're ok to receive signals again */
239 sigemptyset (&mask);
240 sigprocmask (SIG_SETMASK, &mask, NULL);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100241 }
242
243 /* Run the event queue once, and detect anything idle */
Scott James Remnant0c7e72a2006-08-27 22:22:53 +0100244 event_queue_run ();
Scott James Remnant32de9222006-08-31 04:34:27 +0100245 job_detect_idle ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200246
247 /* Go! */
248 ret = nih_main_loop ();
249
250 return ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100251}
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100252
253
254/**
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100255 * reset_console:
256 *
257 * Set up the console flags to something sensible. Cribbed from sysvinit,
258 * initng, etc.
259 **/
260static void
261reset_console (void)
262{
Scott James Remnant4bc5fe32006-08-31 21:48:33 +0100263 struct termios tty;
264
Scott James Remnantff0d26a2006-08-31 20:49:43 +0100265 tcgetattr (0, &tty);
266
267 tty.c_cflag &= (CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD);
268 tty.c_cflag |= (HUPCL | CLOCAL | CREAD);
269
270 /* Set up usual keys */
271 tty.c_cc[VINTR] = 3; /* ^C */
272 tty.c_cc[VQUIT] = 28; /* ^\ */
273 tty.c_cc[VERASE] = 127;
274 tty.c_cc[VKILL] = 24; /* ^X */
275 tty.c_cc[VEOF] = 4; /* ^D */
276 tty.c_cc[VTIME] = 0;
277 tty.c_cc[VMIN] = 1;
278 tty.c_cc[VSTART] = 17; /* ^Q */
279 tty.c_cc[VSTOP] = 19; /* ^S */
280 tty.c_cc[VSUSP] = 26; /* ^Z */
281
282 /* Pre and post processing */
283 tty.c_iflag = (IGNPAR | ICRNL | IXON | IXANY);
284 tty.c_oflag = (OPOST | ONLCR);
285 tty.c_lflag = (ISIG | ICANON | ECHO | ECHOCTL | ECHOPRT | ECHOKE);
286
287 /* Set the terminal line and flush it */
288 tcsetattr (0, TCSANOW, &tty);
289 tcflush (0, TCIOFLUSH);
290}
291
292
293/**
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100294 * segv_handler:
295 * @signum:
296 *
297 * Handle receiving the SEGV signal, usually caused by one of our own
298 * mistakes. We deal with it by dumping core in a child process and
299 * just carrying on in the parent.
300 **/
301static void
302segv_handler (int signum)
303{
304 pid_t pid;
305
306 pid = fork ();
307 if (pid == 0) {
308 struct sigaction act;
309 struct rlimit limit;
310 sigset_t mask;
311
312 /* Mask out all signals */
313 sigfillset (&mask);
314 sigprocmask (SIG_SETMASK, &mask, NULL);
315
316 /* Set the SEGV handler to the default so core is dumped */
317 act.sa_handler = SIG_DFL;
318 act.sa_flags = 0;
319 sigemptyset (&act.sa_mask);
320 sigaction (SIGSEGV, &act, NULL);
321
322 /* Dump in the root directory */
323 chdir ("/");
324
325 /* Don't limit the core dump size */
326 limit.rlim_cur = RLIM_INFINITY;
327 limit.rlim_max = RLIM_INFINITY;
328 setrlimit (RLIMIT_CORE, &limit);
329
330 /* Raise the signal */
331 raise (SIGSEGV);
332
333 /* Unmask so that we receive it */
334 sigdelset (&mask, SIGSEGV);
335 sigprocmask (SIG_SETMASK, &mask, NULL);
336
337 /* Wait for death */
338 pause ();
339 exit (0);
340 } else if (pid > 0) {
341 /* Wait for the core to be generated */
342 waitpid (pid, NULL, 0);
343
344 nih_error (_("Caught segmentation fault, core dumped"));
345 } else {
346 nih_error (_("Caught segmentation fault, unable to dump core"));
347 }
348}
349
350/**
351 * cad_handler:
352 * @data: unused,
353 * @signal: signal that called this handler.
354 *
355 * Handle having recieved the SIGINT signal, sent to us when somebody
356 * presses Ctrl-Alt-Delete on the console. We just generate a
Scott James Remnantbb3cc3f2006-09-08 17:17:47 +0100357 * ctrlaltdel event.
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100358 **/
359static void
360cad_handler (void *data,
361 NihSignal *signal)
362{
Scott James Remnantbb3cc3f2006-09-08 17:17:47 +0100363 event_queue (CTRLALTDEL_EVENT);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100364}
365
366/**
367 * kbd_handler:
368 * @data: unused,
369 * @signal: signal that called this handler.
370 *
371 * Handle having recieved the SIGWINCH signal, sent to us when somebody
372 * presses Alt-UpArrow on the console. We just generate a
373 * kbdrequest event.
374 **/
375static void
376kbd_handler (void *data,
377 NihSignal *signal)
378{
Scott James Remnantbb3cc3f2006-09-08 17:17:47 +0100379 event_queue (KBDREQUEST_EVENT);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100380}
Scott James Remnanteabb7802006-08-31 15:39:04 +0100381
382/**
383 * stop_handler:
384 * @data: unused,
385 * @signal: signal caught.
386 *
387 * This is called when we receive the STOP, TSTP or CONT signals; we
388 * adjust the paused variable appropriately so that the event queue and
389 * job idle detection is not run.
390 **/
391static void
392stop_handler (void *data,
393 NihSignal *signal)
394{
395 nih_assert (signal != NULL);
396
397 if (signal->signum == SIGCONT) {
398 nih_info (_("Event queue resumed"));
399 paused = FALSE;
400 } else {
401 nih_info (_("Event queue paused"));
402 paused = TRUE;
403 }
404}
Scott James Remnant3401ab72006-09-01 02:14:47 +0100405
406
407/**
Scott James Remnant1db88042006-09-01 03:14:19 +0100408 * term_handler:
Scott James Remnant3401ab72006-09-01 02:14:47 +0100409 * @argv0: program to run,
410 * @signal: signal caught.
411 *
Scott James Remnant1db88042006-09-01 03:14:19 +0100412 * This is called when we receive the TERM signal, which instructs us
Scott James Remnant3401ab72006-09-01 02:14:47 +0100413 * to reexec ourselves.
414 **/
415static void
Scott James Remnant1db88042006-09-01 03:14:19 +0100416term_handler (const char *argv0,
Scott James Remnant3401ab72006-09-01 02:14:47 +0100417 NihSignal *signal)
418{
Scott James Remnant6f464962006-09-01 04:10:20 +0100419 NihError *err;
Scott James Remnant3401ab72006-09-01 02:14:47 +0100420 sigset_t mask, oldmask;
421 int fds[2] = { -1, -1 };
422 pid_t pid;
Scott James Remnant3401ab72006-09-01 02:14:47 +0100423
424 nih_assert (argv0 != NULL);
425 nih_assert (signal != NULL);
426
427 nih_warn (_("Re-executing %s"), argv0);
428
Scott James Remnant1db88042006-09-01 03:14:19 +0100429 /* Block signals while we work. We're the last signal handler
430 * installed so this should mean that they're all handled now.
431 *
432 * The child must make sure that it unblocks these again when
433 * it's ready.
434 */
Scott James Remnant3401ab72006-09-01 02:14:47 +0100435 sigfillset (&mask);
436 sigprocmask (SIG_BLOCK, &mask, &oldmask);
437
Scott James Remnant28fcc922006-09-01 04:15:57 +0100438 /* Close the control connection */
439 control_close ();
440
Scott James Remnant3401ab72006-09-01 02:14:47 +0100441 /* Create pipe */
Scott James Remnant6f464962006-09-01 04:10:20 +0100442 if (pipe (fds) < 0) {
443 nih_error_raise_system ();
Scott James Remnant3401ab72006-09-01 02:14:47 +0100444 goto error;
Scott James Remnant6f464962006-09-01 04:10:20 +0100445 }
Scott James Remnant3401ab72006-09-01 02:14:47 +0100446
447 /* Fork a child that can send the state to the new init process */
448 pid = fork ();
449 if (pid < 0) {
Scott James Remnant6f464962006-09-01 04:10:20 +0100450 nih_error_raise_system ();
Scott James Remnant3401ab72006-09-01 02:14:47 +0100451 goto error;
452 } else if (pid == 0) {
Scott James Remnant6f464962006-09-01 04:10:20 +0100453 close (fds[0]);
454
455 write_state (fds[1]);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100456 exit (0);
457 } else {
Scott James Remnant6f464962006-09-01 04:10:20 +0100458 if (dup2 (fds[0], STATE_FD) < 0) {
459 nih_error_raise_system ();
460 goto error;
461 }
462
463 close (fds[0]);
Scott James Remnant3401ab72006-09-01 02:14:47 +0100464 close (fds[1]);
Scott James Remnant6f464962006-09-01 04:10:20 +0100465 fds[0] = fds[1] = -1;
Scott James Remnant3401ab72006-09-01 02:14:47 +0100466 }
467
468 /* 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
472error:
Scott James Remnant6f464962006-09-01 04:10:20 +0100473 err = nih_error_get ();
474 nih_error (_("Failed to re-execute %s: %s"), argv0, err->message);
Scott James Remnant28fcc922006-09-01 04:15:57 +0100475 nih_free (err);
Scott James Remnant6f464962006-09-01 04:10:20 +0100476
Scott James Remnant3401ab72006-09-01 02:14:47 +0100477 close (fds[0]);
Scott James Remnant6f464962006-09-01 04:10:20 +0100478 close (fds[1]);
479
Scott James Remnant28fcc922006-09-01 04:15:57 +0100480 control_open ();
481
Scott James Remnant3401ab72006-09-01 02:14:47 +0100482 sigprocmask (SIG_SETMASK, &oldmask, NULL);
483}
484
485/**
Scott James Remnant3401ab72006-09-01 02:14:47 +0100486 * read_state:
487 * @fd: file descriptor to read from.
488 *
489 * Read event and job state from @fd, which is a trivial line-based
490 * protocol that we can keep the same without too much difficultly. It's
491 * tempting to use the control sockets for this, but they break too often.
492 **/
493static void
494read_state (int fd)
495{
496 Job *job = NULL;
497 Event *event = NULL;
498 FILE *state;
499 char buf[80];
500
501 nih_debug ("Reading state");
502
503 /* Use stdio as it's a light-weight thing that won't change */
504 state = fdopen (fd, "r");
505 if (! state) {
506 nih_warn (_("Unable to read from state descriptor: %s"),
507 strerror (errno));
508 return;
509 }
510
511 /* It's just a series of simple lines; if one begins Job then it
512 * indicates the start of a Job description, otherwise if it
513 * begins Event then it's the start of an Event description.
514 *
515 * Lines beginning "." are assumed to belong to the current job
516 * or event.
517 */
518 while (fgets (buf, sizeof (buf), state)) {
519 char *ptr;
520
521 /* Strip newline */
522 ptr = strchr (buf, '\n');
523 if (ptr)
524 *ptr = '\0';
525
526 if (! strncmp (buf, "Job ", 4)) {
527 job = job_read_state (NULL, buf);
528 event = NULL;
529 } else if (! strncmp (buf, "Event ", 5)) {
530 event = event_read_state (NULL, buf);
531 job = NULL;
532 } else if (buf[0] == '.') {
533 if (job) {
534 job = job_read_state (job, buf);
535 } else if (event) {
536 event = event_read_state (event, buf);
537 }
538 } else {
539 event = NULL;
540 job = NULL;
541 }
542 }
543
544 if (fclose (state))
545 nih_warn (_("Error after reading state: %s"),
546 strerror (errno));
547
548 nih_debug ("State read from parent");
549}
550
551/**
552 * write_state:
553 * @fd: file descriptor to write to.
554 *
555 * Write event and job state to @fd, which is a trivial line-based
556 * protocol that we can keep the same without too much difficultly. It's
557 * tempting to use the control sockets for this, but they break too often.
558 **/
559static void
560write_state (int fd)
561{
562 FILE *state;
563
564 /* Use stdio as it's a light-weight thing that won't change */
565 state = fdopen (fd, "w");
566 if (! state) {
567 nih_warn (_("Unable to write to state descriptor: %s"),
568 strerror (errno));
569 return;
570 }
571
572 event_write_state (state);
573 job_write_state (state);
574
575 if (fclose (state))
576 nih_warn (_("Error after writing state: %s"),
577 strerror (errno));
578}