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