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