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