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 <linux/kd.h> |
| 27 | |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/wait.h> |
| 30 | #include <sys/ioctl.h> |
| 31 | #include <sys/reboot.h> |
| 32 | #include <sys/resource.h> |
| 33 | |
| 34 | #include <signal.h> |
Scott James Remnant | 94d0098 | 2006-08-25 15:38:22 +0200 | [diff] [blame] | 35 | #include <stdlib.h> |
Scott James Remnant | 12a330f | 2006-08-24 02:19:09 +0200 | [diff] [blame] | 36 | #include <syslog.h> |
Scott James Remnant | 027dd7b | 2006-08-21 09:01:25 +0200 | [diff] [blame] | 37 | #include <unistd.h> |
Scott James Remnant | ff0d26a | 2006-08-31 20:49:43 +0100 | [diff] [blame^] | 38 | #include <termios.h> |
Scott James Remnant | 027dd7b | 2006-08-21 09:01:25 +0200 | [diff] [blame] | 39 | |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 40 | #include <nih/macros.h> |
| 41 | #include <nih/alloc.h> |
| 42 | #include <nih/list.h> |
| 43 | #include <nih/timer.h> |
| 44 | #include <nih/signal.h> |
| 45 | #include <nih/child.h> |
| 46 | #include <nih/main.h> |
| 47 | #include <nih/logging.h> |
| 48 | |
| 49 | #include "process.h" |
| 50 | #include "job.h" |
| 51 | #include "event.h" |
| 52 | #include "control.h" |
| 53 | #include "cfgfile.h" |
Scott James Remnant | 5074884 | 2006-05-16 21:02:31 +0100 | [diff] [blame] | 54 | |
| 55 | |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 56 | /* Prototypes for static functions */ |
Scott James Remnant | ff0d26a | 2006-08-31 20:49:43 +0100 | [diff] [blame^] | 57 | static void reset_console (void); |
| 58 | static void segv_handler (int signum); |
| 59 | static void cad_handler (void *data, NihSignal *signal); |
| 60 | static void kbd_handler (void *data, NihSignal *signal); |
| 61 | static void stop_handler (void *data, NihSignal *signal); |
| 62 | |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 63 | |
Scott James Remnant | 5074884 | 2006-05-16 21:02:31 +0100 | [diff] [blame] | 64 | int |
| 65 | main (int argc, |
| 66 | char *argv[]) |
| 67 | { |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 68 | int ret, i; |
Scott James Remnant | 5074884 | 2006-05-16 21:02:31 +0100 | [diff] [blame] | 69 | |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 70 | nih_main_init (argv[0]); |
| 71 | |
Scott James Remnant | 12a330f | 2006-08-24 02:19:09 +0200 | [diff] [blame] | 72 | openlog (program_name, LOG_CONS, LOG_DAEMON); |
| 73 | |
Scott James Remnant | 12a330f | 2006-08-24 02:19:09 +0200 | [diff] [blame] | 74 | nih_log_set_logger (nih_logger_syslog); |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 75 | |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 76 | /* Close any file descriptors we inherited, and open the console |
| 77 | * device instead |
| 78 | */ |
| 79 | for (i = 0; i < 3; i++) |
| 80 | close (i); |
| 81 | |
| 82 | process_setup_console (CONSOLE_OUTPUT); |
Scott James Remnant | ff0d26a | 2006-08-31 20:49:43 +0100 | [diff] [blame^] | 83 | reset_console (); |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 84 | |
Scott James Remnant | f497081 | 2006-08-27 18:27:19 +0100 | [diff] [blame] | 85 | /* Check we're root */ |
| 86 | if (getuid ()) { |
| 87 | nih_error (_("Need to be root")); |
| 88 | exit (1); |
| 89 | } |
| 90 | |
| 91 | /* Check we're process #1 */ |
| 92 | if (getpid () > 1) { |
| 93 | nih_error (_("Not being executed as init")); |
| 94 | exit (1); |
| 95 | } |
| 96 | |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 97 | /* Reset the signal state and install the signal handler for those |
| 98 | * signals we actually want to catch; this also sets those that |
| 99 | * can be sent to us, because we're special |
| 100 | */ |
| 101 | nih_signal_reset (); |
| 102 | nih_signal_set_handler (SIGALRM, nih_signal_handler); |
| 103 | nih_signal_set_handler (SIGHUP, nih_signal_handler); |
Scott James Remnant | eabb780 | 2006-08-31 15:39:04 +0100 | [diff] [blame] | 104 | nih_signal_set_handler (SIGTSTP, nih_signal_handler); |
| 105 | nih_signal_set_handler (SIGCONT, nih_signal_handler); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 106 | nih_signal_set_handler (SIGINT, nih_signal_handler); |
| 107 | nih_signal_set_handler (SIGWINCH, nih_signal_handler); |
| 108 | nih_signal_set_handler (SIGSEGV, segv_handler); |
| 109 | |
Scott James Remnant | eabb780 | 2006-08-31 15:39:04 +0100 | [diff] [blame] | 110 | /* Ensure that we don't process events while paused */ |
Scott James Remnant | eabb780 | 2006-08-31 15:39:04 +0100 | [diff] [blame] | 111 | nih_signal_add_callback (NULL, SIGTSTP, stop_handler, NULL); |
| 112 | nih_signal_add_callback (NULL, SIGCONT, stop_handler, NULL); |
| 113 | |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 114 | /* Ask the kernel to send us SIGINT when control-alt-delete is |
| 115 | * pressed; generate an event with the same name. |
| 116 | */ |
| 117 | reboot (RB_DISABLE_CAD); |
| 118 | nih_signal_add_callback (NULL, SIGINT, cad_handler, NULL); |
| 119 | |
| 120 | /* Ask the kernel to send us SIGWINCH when alt-uparrow is pressed; |
| 121 | * generate a kbdrequest event. |
| 122 | */ |
| 123 | ioctl (0, KDSIGACCEPT, SIGWINCH); |
| 124 | nih_signal_add_callback (NULL, SIGWINCH, kbd_handler, NULL); |
| 125 | |
| 126 | |
| 127 | /* Reap all children that die */ |
| 128 | nih_child_add_watch (NULL, -1, job_child_reaper, NULL); |
| 129 | |
Scott James Remnant | dc8877d | 2006-08-29 16:56:48 +0100 | [diff] [blame] | 130 | /* Process the event queue and check the jobs for idleness |
| 131 | * every time through the main loop */ |
Scott James Remnant | 0c7e72a | 2006-08-27 22:22:53 +0100 | [diff] [blame] | 132 | nih_main_loop_add_func (NULL, (NihMainLoopCb)event_queue_run, NULL); |
Scott James Remnant | dc8877d | 2006-08-29 16:56:48 +0100 | [diff] [blame] | 133 | nih_main_loop_add_func (NULL, (NihMainLoopCb)job_detect_idle, NULL); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 134 | |
Scott James Remnant | 94d0098 | 2006-08-25 15:38:22 +0200 | [diff] [blame] | 135 | |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 136 | /* Become session and process group leader (should be already, |
| 137 | * but you never know what initramfs did |
| 138 | */ |
| 139 | setsid (); |
| 140 | |
| 141 | /* Open control socket */ |
| 142 | control_open (); |
| 143 | |
Scott James Remnant | ceaaf4d | 2006-08-24 01:40:10 +0200 | [diff] [blame] | 144 | /* Read configuration */ |
| 145 | cfg_watch_dir (NULL, CFG_DIR, NULL); |
| 146 | |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 147 | /* Set the PATH environment variable */ |
| 148 | setenv ("PATH", PATH, TRUE); |
| 149 | |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 150 | |
| 151 | /* Generate and run the startup event */ |
Scott James Remnant | ff5efb9 | 2006-08-31 01:45:19 +0100 | [diff] [blame] | 152 | event_queue ("startup"); |
Scott James Remnant | 0c7e72a | 2006-08-27 22:22:53 +0100 | [diff] [blame] | 153 | event_queue_run (); |
Scott James Remnant | 32de922 | 2006-08-31 04:34:27 +0100 | [diff] [blame] | 154 | job_detect_idle (); |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 155 | |
| 156 | /* Go! */ |
| 157 | ret = nih_main_loop (); |
| 158 | |
| 159 | return ret; |
Scott James Remnant | 5074884 | 2006-05-16 21:02:31 +0100 | [diff] [blame] | 160 | } |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 161 | |
| 162 | |
| 163 | /** |
Scott James Remnant | ff0d26a | 2006-08-31 20:49:43 +0100 | [diff] [blame^] | 164 | * reset_console: |
| 165 | * |
| 166 | * Set up the console flags to something sensible. Cribbed from sysvinit, |
| 167 | * initng, etc. |
| 168 | **/ |
| 169 | static void |
| 170 | reset_console (void) |
| 171 | { |
| 172 | tcgetattr (0, &tty); |
| 173 | |
| 174 | tty.c_cflag &= (CBAUD | CBAUDEX | CSIZE | CSTOPB | PARENB | PARODD); |
| 175 | tty.c_cflag |= (HUPCL | CLOCAL | CREAD); |
| 176 | |
| 177 | /* Set up usual keys */ |
| 178 | tty.c_cc[VINTR] = 3; /* ^C */ |
| 179 | tty.c_cc[VQUIT] = 28; /* ^\ */ |
| 180 | tty.c_cc[VERASE] = 127; |
| 181 | tty.c_cc[VKILL] = 24; /* ^X */ |
| 182 | tty.c_cc[VEOF] = 4; /* ^D */ |
| 183 | tty.c_cc[VTIME] = 0; |
| 184 | tty.c_cc[VMIN] = 1; |
| 185 | tty.c_cc[VSTART] = 17; /* ^Q */ |
| 186 | tty.c_cc[VSTOP] = 19; /* ^S */ |
| 187 | tty.c_cc[VSUSP] = 26; /* ^Z */ |
| 188 | |
| 189 | /* Pre and post processing */ |
| 190 | tty.c_iflag = (IGNPAR | ICRNL | IXON | IXANY); |
| 191 | tty.c_oflag = (OPOST | ONLCR); |
| 192 | tty.c_lflag = (ISIG | ICANON | ECHO | ECHOCTL | ECHOPRT | ECHOKE); |
| 193 | |
| 194 | /* Set the terminal line and flush it */ |
| 195 | tcsetattr (0, TCSANOW, &tty); |
| 196 | tcflush (0, TCIOFLUSH); |
| 197 | } |
| 198 | |
| 199 | |
| 200 | /** |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 201 | * segv_handler: |
| 202 | * @signum: |
| 203 | * |
| 204 | * Handle receiving the SEGV signal, usually caused by one of our own |
| 205 | * mistakes. We deal with it by dumping core in a child process and |
| 206 | * just carrying on in the parent. |
| 207 | **/ |
| 208 | static void |
| 209 | segv_handler (int signum) |
| 210 | { |
| 211 | pid_t pid; |
| 212 | |
| 213 | pid = fork (); |
| 214 | if (pid == 0) { |
| 215 | struct sigaction act; |
| 216 | struct rlimit limit; |
| 217 | sigset_t mask; |
| 218 | |
| 219 | /* Mask out all signals */ |
| 220 | sigfillset (&mask); |
| 221 | sigprocmask (SIG_SETMASK, &mask, NULL); |
| 222 | |
| 223 | /* Set the SEGV handler to the default so core is dumped */ |
| 224 | act.sa_handler = SIG_DFL; |
| 225 | act.sa_flags = 0; |
| 226 | sigemptyset (&act.sa_mask); |
| 227 | sigaction (SIGSEGV, &act, NULL); |
| 228 | |
| 229 | /* Dump in the root directory */ |
| 230 | chdir ("/"); |
| 231 | |
| 232 | /* Don't limit the core dump size */ |
| 233 | limit.rlim_cur = RLIM_INFINITY; |
| 234 | limit.rlim_max = RLIM_INFINITY; |
| 235 | setrlimit (RLIMIT_CORE, &limit); |
| 236 | |
| 237 | /* Raise the signal */ |
| 238 | raise (SIGSEGV); |
| 239 | |
| 240 | /* Unmask so that we receive it */ |
| 241 | sigdelset (&mask, SIGSEGV); |
| 242 | sigprocmask (SIG_SETMASK, &mask, NULL); |
| 243 | |
| 244 | /* Wait for death */ |
| 245 | pause (); |
| 246 | exit (0); |
| 247 | } else if (pid > 0) { |
| 248 | /* Wait for the core to be generated */ |
| 249 | waitpid (pid, NULL, 0); |
| 250 | |
| 251 | nih_error (_("Caught segmentation fault, core dumped")); |
| 252 | } else { |
| 253 | nih_error (_("Caught segmentation fault, unable to dump core")); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * cad_handler: |
| 259 | * @data: unused, |
| 260 | * @signal: signal that called this handler. |
| 261 | * |
| 262 | * Handle having recieved the SIGINT signal, sent to us when somebody |
| 263 | * presses Ctrl-Alt-Delete on the console. We just generate a |
| 264 | * control-alt-delete event. |
| 265 | **/ |
| 266 | static void |
| 267 | cad_handler (void *data, |
| 268 | NihSignal *signal) |
| 269 | { |
Scott James Remnant | ff5efb9 | 2006-08-31 01:45:19 +0100 | [diff] [blame] | 270 | event_queue ("control-alt-delete"); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /** |
| 274 | * kbd_handler: |
| 275 | * @data: unused, |
| 276 | * @signal: signal that called this handler. |
| 277 | * |
| 278 | * Handle having recieved the SIGWINCH signal, sent to us when somebody |
| 279 | * presses Alt-UpArrow on the console. We just generate a |
| 280 | * kbdrequest event. |
| 281 | **/ |
| 282 | static void |
| 283 | kbd_handler (void *data, |
| 284 | NihSignal *signal) |
| 285 | { |
Scott James Remnant | ff5efb9 | 2006-08-31 01:45:19 +0100 | [diff] [blame] | 286 | event_queue ("kbdrequest"); |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame] | 287 | } |
Scott James Remnant | eabb780 | 2006-08-31 15:39:04 +0100 | [diff] [blame] | 288 | |
| 289 | /** |
| 290 | * stop_handler: |
| 291 | * @data: unused, |
| 292 | * @signal: signal caught. |
| 293 | * |
| 294 | * This is called when we receive the STOP, TSTP or CONT signals; we |
| 295 | * adjust the paused variable appropriately so that the event queue and |
| 296 | * job idle detection is not run. |
| 297 | **/ |
| 298 | static void |
| 299 | stop_handler (void *data, |
| 300 | NihSignal *signal) |
| 301 | { |
| 302 | nih_assert (signal != NULL); |
| 303 | |
| 304 | if (signal->signum == SIGCONT) { |
| 305 | nih_info (_("Event queue resumed")); |
| 306 | paused = FALSE; |
| 307 | } else { |
| 308 | nih_info (_("Event queue paused")); |
| 309 | paused = TRUE; |
| 310 | } |
| 311 | } |