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 | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 63 | /* Prototypes for static functions */ |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 64 | static void reset_console (void); |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 65 | static void crash_handler (int signum); |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 66 | static void cad_handler (void *data, NihSignal *signal); |
| 67 | static void kbd_handler (void *data, NihSignal *signal); |
Scott James Remnant | 2c95069 | 2007-02-25 09:13:38 +0000 | [diff] [blame] | 68 | static void pwr_handler (void *data, NihSignal *signal); |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 69 | static void stop_handler (void *data, NihSignal *signal); |
Scott James Remnant | 06abbec | 2007-03-09 13:02:38 +0000 | [diff] [blame^] | 70 | static void term_handler (void *data, NihSignal *signal); |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 71 | |
Scott James Remnant | 1db8804 | 2006-09-01 03:14:19 +0100 | [diff] [blame] | 72 | |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 73 | /** |
Scott James Remnant | 06abbec | 2007-03-09 13:02:38 +0000 | [diff] [blame^] | 74 | * argv0: |
| 75 | * |
| 76 | * Path to program executed, used for re-executing the init binary from the |
| 77 | * same location we were executed from. |
| 78 | **/ |
| 79 | static const char *argv0 = NULL; |
| 80 | |
| 81 | /** |
Scott James Remnant | 1db8804 | 2006-09-01 03:14:19 +0100 | [diff] [blame] | 82 | * restart: |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 83 | * |
Scott James Remnant | 8b22740 | 2006-10-11 17:55:27 +0100 | [diff] [blame] | 84 | * 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] | 85 | * process. |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 86 | **/ |
Scott James Remnant | 1db8804 | 2006-09-01 03:14:19 +0100 | [diff] [blame] | 87 | static int restart = FALSE; |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 88 | |
Scott James Remnant | 502ea70 | 2007-03-05 20:47:18 +0000 | [diff] [blame] | 89 | /** |
| 90 | * rescue: |
| 91 | * |
| 92 | * This is set to TRUE if we're being re-exec'd by an existing init process |
| 93 | * that has crashed. |
| 94 | **/ |
| 95 | static int rescue = FALSE; |
| 96 | |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 97 | |
| 98 | /** |
| 99 | * options: |
| 100 | * |
| 101 | * Command-line options we accept. |
| 102 | **/ |
| 103 | static NihOption options[] = { |
Scott James Remnant | 1db8804 | 2006-09-01 03:14:19 +0100 | [diff] [blame] | 104 | { 0, "restart", NULL, NULL, NULL, &restart, NULL }, |
Scott James Remnant | 502ea70 | 2007-03-05 20:47:18 +0000 | [diff] [blame] | 105 | { 0, "rescue", NULL, NULL, NULL, &rescue, NULL }, |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 106 | |
| 107 | /* Ignore invalid options */ |
| 108 | { '-', "--", NULL, NULL, NULL, NULL, NULL }, |
| 109 | |
| 110 | NIH_OPTION_LAST |
| 111 | }; |
Scott James Remnant | ff0d26a | 2006-08-31 20:49:43 +0100 | [diff] [blame] | 112 | |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 113 | |
Scott James Remnant | 5074884 | 2006-05-16 21:02:31 +0100 | [diff] [blame] | 114 | int |
| 115 | main (int argc, |
| 116 | char *argv[]) |
| 117 | { |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 118 | char **args; |
Scott James Remnant | 2e204b7 | 2007-02-03 23:15:28 +0000 | [diff] [blame] | 119 | int ret; |
Scott James Remnant | 5074884 | 2006-05-16 21:02:31 +0100 | [diff] [blame] | 120 | |
Scott James Remnant | 06abbec | 2007-03-09 13:02:38 +0000 | [diff] [blame^] | 121 | argv0 = argv[0]; |
| 122 | nih_main_init (argv0); |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 123 | |
Scott James Remnant | 930e25a | 2006-10-13 12:28:05 +0100 | [diff] [blame] | 124 | nih_option_set_synopsis (_("Process management daemon.")); |
Scott James Remnant | 462734c | 2006-10-13 13:36:00 +0100 | [diff] [blame] | 125 | nih_option_set_help ( |
| 126 | _("This daemon is normally executed by the kernel and given " |
| 127 | "process id 1 to denote its special status. When executed " |
| 128 | "by a user process, it will actually run /sbin/telinit.")); |
Scott James Remnant | a6ed7eb | 2006-10-13 12:14:45 +0100 | [diff] [blame] | 129 | |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 130 | args = nih_option_parser (NULL, argc, argv, options, FALSE); |
| 131 | if (! args) |
| 132 | exit (1); |
Scott James Remnant | 12a330f | 2006-08-24 02:19:09 +0200 | [diff] [blame] | 133 | |
Scott James Remnant | a17917d | 2006-09-07 00:18:28 +0100 | [diff] [blame] | 134 | /* Check we're root */ |
| 135 | if (getuid ()) { |
Scott James Remnant | 31421b7 | 2007-03-08 23:53:05 +0000 | [diff] [blame] | 136 | nih_fatal (_("Need to be root")); |
Scott James Remnant | a17917d | 2006-09-07 00:18:28 +0100 | [diff] [blame] | 137 | exit (1); |
| 138 | } |
| 139 | |
| 140 | /* Check we're process #1 */ |
| 141 | if (getpid () > 1) { |
Scott James Remnant | e0d0dd1 | 2006-09-14 10:51:05 +0100 | [diff] [blame] | 142 | execv (TELINIT, argv); |
Scott James Remnant | a17917d | 2006-09-07 00:18:28 +0100 | [diff] [blame] | 143 | /* Ignore failure, probably just that telinit doesn't exist */ |
| 144 | |
Scott James Remnant | 31421b7 | 2007-03-08 23:53:05 +0000 | [diff] [blame] | 145 | nih_fatal (_("Not being executed as init")); |
Scott James Remnant | a17917d | 2006-09-07 00:18:28 +0100 | [diff] [blame] | 146 | exit (1); |
| 147 | } |
| 148 | |
Scott James Remnant | 2e204b7 | 2007-02-03 23:15:28 +0000 | [diff] [blame] | 149 | /* Open control socket */ |
| 150 | if (! control_open ()) { |
| 151 | NihError *err; |
| 152 | |
| 153 | err = nih_error_get (); |
Scott James Remnant | 31421b7 | 2007-03-08 23:53:05 +0000 | [diff] [blame] | 154 | nih_fatal ("%s: %s", _("Unable to open control socket"), |
Scott James Remnant | 2e204b7 | 2007-02-03 23:15:28 +0000 | [diff] [blame] | 155 | err->message); |
| 156 | nih_free (err); |
| 157 | |
| 158 | exit (1); |
| 159 | } |
| 160 | |
| 161 | /* Read configuration */ |
Scott James Remnant | 4a104b5 | 2007-03-02 12:21:45 +0000 | [diff] [blame] | 162 | if (cfg_watch_dir (CFG_DIR, NULL) == (void *)-1) { |
Scott James Remnant | 2e204b7 | 2007-02-03 23:15:28 +0000 | [diff] [blame] | 163 | NihError *err; |
| 164 | |
| 165 | err = nih_error_get (); |
Scott James Remnant | 31421b7 | 2007-03-08 23:53:05 +0000 | [diff] [blame] | 166 | nih_fatal ("%s: %s", _("Error parsing configuration"), |
Scott James Remnant | 2e204b7 | 2007-02-03 23:15:28 +0000 | [diff] [blame] | 167 | err->message); |
| 168 | nih_free (err); |
| 169 | |
| 170 | exit (1); |
| 171 | } |
| 172 | |
Scott James Remnant | 78626fb | 2007-01-10 16:48:10 +0000 | [diff] [blame] | 173 | |
| 174 | /* Clear our arguments from the command-line, so that we show up in |
| 175 | * ps or top output as /sbin/init, with no extra flags. |
| 176 | * |
| 177 | * This is a very Linux-specific trick; by deleting the NULL |
| 178 | * terminator at the end of the last argument, we fool the kernel |
| 179 | * into believing we used a setproctitle()-a-like to extend the |
| 180 | * argument space into the environment space, and thus make it use |
| 181 | * strlen() instead of its own assumed length. In fact, we've done |
| 182 | * the exact opposite, and shrunk the command line length to just that |
| 183 | * of whatever is in argv[0]. |
| 184 | * |
| 185 | * If we don't do this, and just write \0 over the rest of argv, for |
| 186 | * example; the command-line length still includes those \0s, and ps |
| 187 | * will show whitespace in their place. |
| 188 | */ |
| 189 | if (argc > 1) { |
Scott James Remnant | 505f928 | 2007-01-10 18:44:38 +0000 | [diff] [blame] | 190 | char *arg_end; |
Scott James Remnant | 78626fb | 2007-01-10 16:48:10 +0000 | [diff] [blame] | 191 | |
Scott James Remnant | 505f928 | 2007-01-10 18:44:38 +0000 | [diff] [blame] | 192 | arg_end = argv[argc-1] + strlen (argv[argc-1]); |
| 193 | *arg_end = ' '; |
Scott James Remnant | 78626fb | 2007-01-10 16:48:10 +0000 | [diff] [blame] | 194 | } |
Scott James Remnant | 7f4db42 | 2007-01-09 20:51:08 +0000 | [diff] [blame] | 195 | |
Scott James Remnant | 2e204b7 | 2007-02-03 23:15:28 +0000 | [diff] [blame] | 196 | /* Become session and process group leader (should be already, |
| 197 | * but you never know what initramfs did |
| 198 | */ |
| 199 | setsid (); |
Scott James Remnant | a17917d | 2006-09-07 00:18:28 +0100 | [diff] [blame] | 200 | |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 201 | /* Send all logging output to syslog */ |
| 202 | openlog (program_name, LOG_CONS, LOG_DAEMON); |
Scott James Remnant | 12a330f | 2006-08-24 02:19:09 +0200 | [diff] [blame] | 203 | nih_log_set_logger (nih_logger_syslog); |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 204 | |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 205 | /* Close any file descriptors we inherited, and open the console |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 206 | * device instead. Normally we reset the console, unless we're |
| 207 | * inheriting one from another init process. |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 208 | */ |
Scott James Remnant | 2e204b7 | 2007-02-03 23:15:28 +0000 | [diff] [blame] | 209 | for (int i = 0; i < 3; i++) |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 210 | close (i); |
| 211 | |
Scott James Remnant | 5d70295 | 2007-01-05 17:21:34 +0000 | [diff] [blame] | 212 | if (process_setup_console (NULL, CONSOLE_OUTPUT) < 0) |
| 213 | nih_free (nih_error_get ()); |
Scott James Remnant | 502ea70 | 2007-03-05 20:47:18 +0000 | [diff] [blame] | 214 | if (! (restart || rescue)) |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 215 | reset_console (); |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 216 | |
Scott James Remnant | 2e204b7 | 2007-02-03 23:15:28 +0000 | [diff] [blame] | 217 | /* Set the PATH environment variable */ |
| 218 | setenv ("PATH", PATH, TRUE); |
| 219 | |
| 220 | |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 221 | /* Reset the signal state and install the signal handler for those |
| 222 | * signals we actually want to catch; this also sets those that |
| 223 | * can be sent to us, because we're special |
| 224 | */ |
Scott James Remnant | 502ea70 | 2007-03-05 20:47:18 +0000 | [diff] [blame] | 225 | if (! (restart || rescue)) |
Scott James Remnant | 2e204b7 | 2007-02-03 23:15:28 +0000 | [diff] [blame] | 226 | nih_signal_reset (); |
| 227 | |
| 228 | nih_signal_set_handler (SIGCHLD, nih_signal_handler); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 229 | nih_signal_set_handler (SIGALRM, nih_signal_handler); |
| 230 | nih_signal_set_handler (SIGHUP, nih_signal_handler); |
Scott James Remnant | eabb780 | 2006-08-31 15:39:04 +0100 | [diff] [blame] | 231 | nih_signal_set_handler (SIGTSTP, nih_signal_handler); |
| 232 | nih_signal_set_handler (SIGCONT, nih_signal_handler); |
Scott James Remnant | 1db8804 | 2006-09-01 03:14:19 +0100 | [diff] [blame] | 233 | nih_signal_set_handler (SIGTERM, nih_signal_handler); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 234 | nih_signal_set_handler (SIGINT, nih_signal_handler); |
| 235 | nih_signal_set_handler (SIGWINCH, nih_signal_handler); |
Scott James Remnant | 2c95069 | 2007-02-25 09:13:38 +0000 | [diff] [blame] | 236 | nih_signal_set_handler (SIGPWR, nih_signal_handler); |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 237 | nih_signal_set_handler (SIGSEGV, crash_handler); |
| 238 | nih_signal_set_handler (SIGABRT, crash_handler); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 239 | |
Scott James Remnant | eabb780 | 2006-08-31 15:39:04 +0100 | [diff] [blame] | 240 | /* Ensure that we don't process events while paused */ |
Scott James Remnant | 5d70295 | 2007-01-05 17:21:34 +0000 | [diff] [blame] | 241 | NIH_MUST (nih_signal_add_handler (NULL, SIGTSTP, stop_handler, NULL)); |
| 242 | NIH_MUST (nih_signal_add_handler (NULL, SIGCONT, stop_handler, NULL)); |
Scott James Remnant | eabb780 | 2006-08-31 15:39:04 +0100 | [diff] [blame] | 243 | |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 244 | /* Ask the kernel to send us SIGINT when control-alt-delete is |
| 245 | * pressed; generate an event with the same name. |
| 246 | */ |
| 247 | reboot (RB_DISABLE_CAD); |
Scott James Remnant | 5d70295 | 2007-01-05 17:21:34 +0000 | [diff] [blame] | 248 | NIH_MUST (nih_signal_add_handler (NULL, SIGINT, cad_handler, NULL)); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 249 | |
| 250 | /* Ask the kernel to send us SIGWINCH when alt-uparrow is pressed; |
| 251 | * generate a kbdrequest event. |
| 252 | */ |
Scott James Remnant | 5d70295 | 2007-01-05 17:21:34 +0000 | [diff] [blame] | 253 | if (ioctl (0, KDSIGACCEPT, SIGWINCH) == 0) |
| 254 | NIH_MUST (nih_signal_add_handler (NULL, SIGWINCH, |
| 255 | kbd_handler, NULL)); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 256 | |
Scott James Remnant | 2c95069 | 2007-02-25 09:13:38 +0000 | [diff] [blame] | 257 | /* powstatd sends us SIGPWR when it changes /etc/powerstatus */ |
| 258 | NIH_MUST (nih_signal_add_handler (NULL, SIGPWR, pwr_handler, NULL)); |
| 259 | |
Scott James Remnant | 1db8804 | 2006-09-01 03:14:19 +0100 | [diff] [blame] | 260 | /* SIGTERM instructs us to re-exec ourselves */ |
Scott James Remnant | 5d70295 | 2007-01-05 17:21:34 +0000 | [diff] [blame] | 261 | NIH_MUST (nih_signal_add_handler (NULL, SIGTERM, |
| 262 | (NihSignalHandler)term_handler, |
Scott James Remnant | 06abbec | 2007-03-09 13:02:38 +0000 | [diff] [blame^] | 263 | NULL)); |
Scott James Remnant | 1db8804 | 2006-09-01 03:14:19 +0100 | [diff] [blame] | 264 | |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 265 | /* Reap all children that die */ |
Scott James Remnant | 5d70295 | 2007-01-05 17:21:34 +0000 | [diff] [blame] | 266 | 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] | 267 | |
Scott James Remnant | 6590660 | 2007-02-08 02:51:39 +0000 | [diff] [blame] | 268 | /* Process the event queue and check the jobs to make sure we |
| 269 | * haven't stalled, every time through the main loop */ |
Scott James Remnant | a39da0f | 2007-02-07 13:52:42 +0000 | [diff] [blame] | 270 | NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)event_poll, |
Scott James Remnant | 5d70295 | 2007-01-05 17:21:34 +0000 | [diff] [blame] | 271 | NULL)); |
Scott James Remnant | c674047 | 2007-02-09 21:16:28 +0000 | [diff] [blame] | 272 | NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)job_detect_stalled, |
| 273 | NULL)); |
| 274 | NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)job_free_deleted, |
Scott James Remnant | 5d70295 | 2007-01-05 17:21:34 +0000 | [diff] [blame] | 275 | NULL)); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 276 | |
Scott James Remnant | 94d0098 | 2006-08-25 15:38:22 +0200 | [diff] [blame] | 277 | |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 278 | /* Generate and run the startup event or read the state from the |
| 279 | * init daemon that exec'd us |
| 280 | */ |
Scott James Remnant | 502ea70 | 2007-03-05 20:47:18 +0000 | [diff] [blame] | 281 | if (! (restart || rescue)) { |
Scott James Remnant | 71b91fc | 2007-02-08 01:07:56 +0000 | [diff] [blame] | 282 | event_emit (STARTUP_EVENT, NULL, NULL); |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 283 | } else { |
Scott James Remnant | 1db8804 | 2006-09-01 03:14:19 +0100 | [diff] [blame] | 284 | sigset_t mask; |
| 285 | |
Scott James Remnant | 1db8804 | 2006-09-01 03:14:19 +0100 | [diff] [blame] | 286 | /* We're ok to receive signals again */ |
| 287 | sigemptyset (&mask); |
| 288 | sigprocmask (SIG_SETMASK, &mask, NULL); |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 289 | } |
| 290 | |
Scott James Remnant | a39da0f | 2007-02-07 13:52:42 +0000 | [diff] [blame] | 291 | /* Run through the loop at least once */ |
| 292 | nih_main_loop_interrupt (); |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 293 | ret = nih_main_loop (); |
| 294 | |
| 295 | return ret; |
Scott James Remnant | 5074884 | 2006-05-16 21:02:31 +0100 | [diff] [blame] | 296 | } |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 297 | |
| 298 | |
| 299 | /** |
Scott James Remnant | ff0d26a | 2006-08-31 20:49:43 +0100 | [diff] [blame] | 300 | * reset_console: |
| 301 | * |
| 302 | * Set up the console flags to something sensible. Cribbed from sysvinit, |
| 303 | * initng, etc. |
| 304 | **/ |
| 305 | static void |
| 306 | reset_console (void) |
| 307 | { |
Scott James Remnant | 4bc5fe3 | 2006-08-31 21:48:33 +0100 | [diff] [blame] | 308 | struct termios tty; |
| 309 | |
Scott James Remnant | ff0d26a | 2006-08-31 20:49:43 +0100 | [diff] [blame] | 310 | tcgetattr (0, &tty); |
| 311 | |
| 312 | tty.c_cflag &= (CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD); |
| 313 | tty.c_cflag |= (HUPCL | CLOCAL | CREAD); |
| 314 | |
| 315 | /* Set up usual keys */ |
| 316 | tty.c_cc[VINTR] = 3; /* ^C */ |
| 317 | tty.c_cc[VQUIT] = 28; /* ^\ */ |
| 318 | tty.c_cc[VERASE] = 127; |
| 319 | tty.c_cc[VKILL] = 24; /* ^X */ |
| 320 | tty.c_cc[VEOF] = 4; /* ^D */ |
| 321 | tty.c_cc[VTIME] = 0; |
| 322 | tty.c_cc[VMIN] = 1; |
| 323 | tty.c_cc[VSTART] = 17; /* ^Q */ |
| 324 | tty.c_cc[VSTOP] = 19; /* ^S */ |
| 325 | tty.c_cc[VSUSP] = 26; /* ^Z */ |
| 326 | |
| 327 | /* Pre and post processing */ |
| 328 | tty.c_iflag = (IGNPAR | ICRNL | IXON | IXANY); |
| 329 | tty.c_oflag = (OPOST | ONLCR); |
| 330 | tty.c_lflag = (ISIG | ICANON | ECHO | ECHOCTL | ECHOPRT | ECHOKE); |
| 331 | |
| 332 | /* Set the terminal line and flush it */ |
| 333 | tcsetattr (0, TCSANOW, &tty); |
| 334 | tcflush (0, TCIOFLUSH); |
| 335 | } |
| 336 | |
| 337 | |
| 338 | /** |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 339 | * crash_handler: |
Scott James Remnant | 8b22740 | 2006-10-11 17:55:27 +0100 | [diff] [blame] | 340 | * @signum: signal number received. |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 341 | * |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 342 | * Handle receiving the SEGV or ABRT signal, usually caused by one of |
| 343 | * our own mistakes. We deal with it by dumping core in a child process |
| 344 | * and just carrying on in the parent. |
| 345 | * |
| 346 | * This may or may not work, but the only alternative would be sigjmp()ing |
| 347 | * to somewhere "safe" leaving inconsistent state everywhere (like dangling |
| 348 | * lists pointers) or exec'ing another process (which we couldn't transfer |
| 349 | * our state to anyway). This just hopes that the kernel resumes on the |
| 350 | * next instruction. |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 351 | **/ |
| 352 | static void |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 353 | crash_handler (int signum) |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 354 | { |
Scott James Remnant | 06abbec | 2007-03-09 13:02:38 +0000 | [diff] [blame^] | 355 | NihError *err; |
| 356 | const char *loglevel = NULL; |
| 357 | sigset_t mask, oldmask; |
| 358 | pid_t pid; |
| 359 | |
| 360 | nih_assert (argv0 != NULL); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 361 | |
| 362 | pid = fork (); |
| 363 | if (pid == 0) { |
| 364 | struct sigaction act; |
| 365 | struct rlimit limit; |
| 366 | sigset_t mask; |
| 367 | |
| 368 | /* Mask out all signals */ |
| 369 | sigfillset (&mask); |
| 370 | sigprocmask (SIG_SETMASK, &mask, NULL); |
| 371 | |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 372 | /* Set the handler to the default so core is dumped */ |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 373 | act.sa_handler = SIG_DFL; |
| 374 | act.sa_flags = 0; |
| 375 | sigemptyset (&act.sa_mask); |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 376 | sigaction (signum, &act, NULL); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 377 | |
| 378 | /* Dump in the root directory */ |
| 379 | chdir ("/"); |
| 380 | |
| 381 | /* Don't limit the core dump size */ |
| 382 | limit.rlim_cur = RLIM_INFINITY; |
| 383 | limit.rlim_max = RLIM_INFINITY; |
| 384 | setrlimit (RLIMIT_CORE, &limit); |
| 385 | |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 386 | /* Raise the signal again */ |
| 387 | raise (signum); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 388 | |
| 389 | /* Unmask so that we receive it */ |
Scott James Remnant | 16a286f | 2007-01-10 15:38:33 +0000 | [diff] [blame] | 390 | sigdelset (&mask, signum); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 391 | sigprocmask (SIG_SETMASK, &mask, NULL); |
| 392 | |
| 393 | /* Wait for death */ |
| 394 | pause (); |
| 395 | exit (0); |
| 396 | } else if (pid > 0) { |
| 397 | /* Wait for the core to be generated */ |
| 398 | waitpid (pid, NULL, 0); |
| 399 | |
Scott James Remnant | 31421b7 | 2007-03-08 23:53:05 +0000 | [diff] [blame] | 400 | nih_fatal (_("Caught %s, core dumped"), |
Scott James Remnant | de44301 | 2007-01-10 18:45:40 +0000 | [diff] [blame] | 401 | (signum == SIGSEGV |
| 402 | ? "segmentation fault" : "abort")); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 403 | } else { |
Scott James Remnant | 31421b7 | 2007-03-08 23:53:05 +0000 | [diff] [blame] | 404 | nih_fatal (_("Caught %s, unable to dump core"), |
Scott James Remnant | de44301 | 2007-01-10 18:45:40 +0000 | [diff] [blame] | 405 | (signum == SIGSEGV |
| 406 | ? "segmentation fault" : "abort")); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 407 | } |
Scott James Remnant | 502ea70 | 2007-03-05 20:47:18 +0000 | [diff] [blame] | 408 | |
| 409 | /* There's no point carrying on from here; our state is almost |
| 410 | * likely in tatters, so we'd just end up core-dumping again and |
| 411 | * writing over the one that contains the real bug. We can't |
| 412 | * even re-exec properly, since we'd probably core dump while |
| 413 | * trying to transfer the state. |
| 414 | * |
| 415 | * So we just do the only thing we can, block out all signals |
| 416 | * and try and start again from scratch. |
| 417 | */ |
| 418 | sigfillset (&mask); |
| 419 | sigprocmask (SIG_BLOCK, &mask, &oldmask); |
| 420 | |
| 421 | /* Argument list */ |
Scott James Remnant | 06abbec | 2007-03-09 13:02:38 +0000 | [diff] [blame^] | 422 | if (nih_log_priority <= NIH_LOG_DEBUG) { |
| 423 | loglevel = "--debug"; |
| 424 | } else if (nih_log_priority <= NIH_LOG_INFO) { |
| 425 | loglevel = "--verbose"; |
| 426 | } else if (nih_log_priority >= NIH_LOG_ERROR) { |
| 427 | loglevel = "--error"; |
| 428 | } else { |
| 429 | loglevel = NULL; |
| 430 | } |
| 431 | execl (argv0, argv0, "--rescue", loglevel, NULL); |
Scott James Remnant | 502ea70 | 2007-03-05 20:47:18 +0000 | [diff] [blame] | 432 | nih_error_raise_system (); |
| 433 | |
| 434 | err = nih_error_get (); |
Scott James Remnant | 31421b7 | 2007-03-08 23:53:05 +0000 | [diff] [blame] | 435 | nih_fatal (_("Failed to re-execute %s: %s"), |
Scott James Remnant | 502ea70 | 2007-03-05 20:47:18 +0000 | [diff] [blame] | 436 | "/sbin/init", err->message); |
| 437 | nih_free (err); |
| 438 | |
| 439 | sigprocmask (SIG_SETMASK, &oldmask, NULL); |
| 440 | |
| 441 | /* Oh Bugger */ |
| 442 | exit (1); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /** |
| 446 | * cad_handler: |
| 447 | * @data: unused, |
| 448 | * @signal: signal that called this handler. |
| 449 | * |
| 450 | * Handle having recieved the SIGINT signal, sent to us when somebody |
| 451 | * presses Ctrl-Alt-Delete on the console. We just generate a |
Scott James Remnant | bb3cc3f | 2006-09-08 17:17:47 +0100 | [diff] [blame] | 452 | * ctrlaltdel event. |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 453 | **/ |
| 454 | static void |
| 455 | cad_handler (void *data, |
| 456 | NihSignal *signal) |
| 457 | { |
Scott James Remnant | 71b91fc | 2007-02-08 01:07:56 +0000 | [diff] [blame] | 458 | event_emit (CTRLALTDEL_EVENT, NULL, NULL); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /** |
| 462 | * kbd_handler: |
| 463 | * @data: unused, |
| 464 | * @signal: signal that called this handler. |
| 465 | * |
| 466 | * Handle having recieved the SIGWINCH signal, sent to us when somebody |
| 467 | * presses Alt-UpArrow on the console. We just generate a |
| 468 | * kbdrequest event. |
| 469 | **/ |
| 470 | static void |
| 471 | kbd_handler (void *data, |
| 472 | NihSignal *signal) |
| 473 | { |
Scott James Remnant | 71b91fc | 2007-02-08 01:07:56 +0000 | [diff] [blame] | 474 | event_emit (KBDREQUEST_EVENT, NULL, NULL); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 475 | } |
Scott James Remnant | eabb780 | 2006-08-31 15:39:04 +0100 | [diff] [blame] | 476 | |
| 477 | /** |
Scott James Remnant | 2c95069 | 2007-02-25 09:13:38 +0000 | [diff] [blame] | 478 | * pwr_handler: |
| 479 | * @data: unused, |
| 480 | * @signal: signal that called this handler. |
| 481 | * |
| 482 | * Handle having recieved the SIGPWR signal, sent to us when powstatd |
| 483 | * changes the /etc/powerstatus file. We just generate a |
| 484 | * power-status-changed event and jobs read the file. |
| 485 | **/ |
| 486 | static void |
| 487 | pwr_handler (void *data, |
| 488 | NihSignal *signal) |
| 489 | { |
| 490 | event_emit (PWRSTATUS_EVENT, NULL, NULL); |
| 491 | } |
| 492 | |
| 493 | /** |
Scott James Remnant | eabb780 | 2006-08-31 15:39:04 +0100 | [diff] [blame] | 494 | * stop_handler: |
| 495 | * @data: unused, |
| 496 | * @signal: signal caught. |
| 497 | * |
| 498 | * This is called when we receive the STOP, TSTP or CONT signals; we |
| 499 | * adjust the paused variable appropriately so that the event queue and |
Scott James Remnant | 6590660 | 2007-02-08 02:51:39 +0000 | [diff] [blame] | 500 | * job stalled detection is not run. |
Scott James Remnant | eabb780 | 2006-08-31 15:39:04 +0100 | [diff] [blame] | 501 | **/ |
| 502 | static void |
| 503 | stop_handler (void *data, |
| 504 | NihSignal *signal) |
| 505 | { |
| 506 | nih_assert (signal != NULL); |
| 507 | |
| 508 | if (signal->signum == SIGCONT) { |
| 509 | nih_info (_("Event queue resumed")); |
| 510 | paused = FALSE; |
| 511 | } else { |
| 512 | nih_info (_("Event queue paused")); |
| 513 | paused = TRUE; |
| 514 | } |
| 515 | } |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 516 | |
| 517 | |
| 518 | /** |
Scott James Remnant | 1db8804 | 2006-09-01 03:14:19 +0100 | [diff] [blame] | 519 | * term_handler: |
Scott James Remnant | 06abbec | 2007-03-09 13:02:38 +0000 | [diff] [blame^] | 520 | * @data: unused, |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 521 | * @signal: signal caught. |
| 522 | * |
Scott James Remnant | 1db8804 | 2006-09-01 03:14:19 +0100 | [diff] [blame] | 523 | * 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] | 524 | * to reexec ourselves. |
| 525 | **/ |
| 526 | static void |
Scott James Remnant | 06abbec | 2007-03-09 13:02:38 +0000 | [diff] [blame^] | 527 | term_handler (void *data, |
| 528 | NihSignal *signal) |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 529 | { |
Scott James Remnant | 06abbec | 2007-03-09 13:02:38 +0000 | [diff] [blame^] | 530 | NihError *err; |
| 531 | const char *loglevel; |
| 532 | sigset_t mask, oldmask; |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 533 | |
| 534 | nih_assert (argv0 != NULL); |
| 535 | nih_assert (signal != NULL); |
| 536 | |
| 537 | nih_warn (_("Re-executing %s"), argv0); |
| 538 | |
Scott James Remnant | 1db8804 | 2006-09-01 03:14:19 +0100 | [diff] [blame] | 539 | /* Block signals while we work. We're the last signal handler |
| 540 | * installed so this should mean that they're all handled now. |
| 541 | * |
| 542 | * The child must make sure that it unblocks these again when |
| 543 | * it's ready. |
| 544 | */ |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 545 | sigfillset (&mask); |
| 546 | sigprocmask (SIG_BLOCK, &mask, &oldmask); |
| 547 | |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 548 | /* Argument list */ |
Scott James Remnant | 06abbec | 2007-03-09 13:02:38 +0000 | [diff] [blame^] | 549 | if (nih_log_priority <= NIH_LOG_DEBUG) { |
| 550 | loglevel = "--debug"; |
| 551 | } else if (nih_log_priority <= NIH_LOG_INFO) { |
| 552 | loglevel = "--verbose"; |
| 553 | } else if (nih_log_priority >= NIH_LOG_ERROR) { |
| 554 | loglevel = "--error"; |
| 555 | } else { |
| 556 | loglevel = NULL; |
| 557 | } |
| 558 | execl (argv0, argv0, "--restart", loglevel, NULL); |
Scott James Remnant | 6f46496 | 2006-09-01 04:10:20 +0100 | [diff] [blame] | 559 | nih_error_raise_system (); |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 560 | |
Scott James Remnant | 6f46496 | 2006-09-01 04:10:20 +0100 | [diff] [blame] | 561 | err = nih_error_get (); |
| 562 | nih_error (_("Failed to re-execute %s: %s"), argv0, err->message); |
Scott James Remnant | 28fcc92 | 2006-09-01 04:15:57 +0100 | [diff] [blame] | 563 | nih_free (err); |
Scott James Remnant | 6f46496 | 2006-09-01 04:10:20 +0100 | [diff] [blame] | 564 | |
Scott James Remnant | 3401ab7 | 2006-09-01 02:14:47 +0100 | [diff] [blame] | 565 | sigprocmask (SIG_SETMASK, &oldmask, NULL); |
| 566 | } |