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