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