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