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> |
| 38 | |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 39 | #include <nih/macros.h> |
| 40 | #include <nih/alloc.h> |
| 41 | #include <nih/list.h> |
| 42 | #include <nih/timer.h> |
| 43 | #include <nih/signal.h> |
| 44 | #include <nih/child.h> |
| 45 | #include <nih/main.h> |
| 46 | #include <nih/logging.h> |
| 47 | |
| 48 | #include "process.h" |
| 49 | #include "job.h" |
| 50 | #include "event.h" |
| 51 | #include "control.h" |
| 52 | #include "cfgfile.h" |
Scott James Remnant | 5074884 | 2006-05-16 21:02:31 +0100 | [diff] [blame] | 53 | |
| 54 | |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame^] | 55 | /* Prototypes for static functions */ |
| 56 | static void segv_handler (int signum); |
| 57 | static void cad_handler (void *data, NihSignal *signal); |
| 58 | static void kbd_handler (void *data, NihSignal *signal); |
| 59 | |
| 60 | |
Scott James Remnant | 5074884 | 2006-05-16 21:02:31 +0100 | [diff] [blame] | 61 | int |
| 62 | main (int argc, |
| 63 | char *argv[]) |
| 64 | { |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 65 | int ret, i; |
Scott James Remnant | 5074884 | 2006-05-16 21:02:31 +0100 | [diff] [blame] | 66 | |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 67 | nih_main_init (argv[0]); |
| 68 | |
Scott James Remnant | 12a330f | 2006-08-24 02:19:09 +0200 | [diff] [blame] | 69 | openlog (program_name, LOG_CONS, LOG_DAEMON); |
| 70 | |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 71 | nih_log_set_priority (NIH_LOG_DEBUG); |
Scott James Remnant | 12a330f | 2006-08-24 02:19:09 +0200 | [diff] [blame] | 72 | nih_log_set_logger (nih_logger_syslog); |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 73 | |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 74 | /* Close any file descriptors we inherited, and open the console |
| 75 | * device instead |
| 76 | */ |
| 77 | for (i = 0; i < 3; i++) |
| 78 | close (i); |
| 79 | |
| 80 | process_setup_console (CONSOLE_OUTPUT); |
| 81 | |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame^] | 82 | |
| 83 | /* Reset the signal state and install the signal handler for those |
| 84 | * signals we actually want to catch; this also sets those that |
| 85 | * can be sent to us, because we're special |
| 86 | */ |
| 87 | nih_signal_reset (); |
| 88 | nih_signal_set_handler (SIGALRM, nih_signal_handler); |
| 89 | nih_signal_set_handler (SIGHUP, nih_signal_handler); |
| 90 | nih_signal_set_handler (SIGINT, nih_signal_handler); |
| 91 | nih_signal_set_handler (SIGWINCH, nih_signal_handler); |
| 92 | nih_signal_set_handler (SIGSEGV, segv_handler); |
| 93 | |
| 94 | /* Ask the kernel to send us SIGINT when control-alt-delete is |
| 95 | * pressed; generate an event with the same name. |
| 96 | */ |
| 97 | reboot (RB_DISABLE_CAD); |
| 98 | nih_signal_add_callback (NULL, SIGINT, cad_handler, NULL); |
| 99 | |
| 100 | /* Ask the kernel to send us SIGWINCH when alt-uparrow is pressed; |
| 101 | * generate a kbdrequest event. |
| 102 | */ |
| 103 | ioctl (0, KDSIGACCEPT, SIGWINCH); |
| 104 | nih_signal_add_callback (NULL, SIGWINCH, kbd_handler, NULL); |
| 105 | |
| 106 | |
| 107 | /* Reap all children that die */ |
| 108 | nih_child_add_watch (NULL, -1, job_child_reaper, NULL); |
| 109 | |
| 110 | /* Process the event queue every time through the main loop */ |
| 111 | nih_main_loop_add_func (NULL, event_queue_run, NULL); |
| 112 | |
Scott James Remnant | 94d0098 | 2006-08-25 15:38:22 +0200 | [diff] [blame] | 113 | |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 114 | /* Become session and process group leader (should be already, |
| 115 | * but you never know what initramfs did |
| 116 | */ |
| 117 | setsid (); |
| 118 | |
| 119 | /* Open control socket */ |
| 120 | control_open (); |
| 121 | |
Scott James Remnant | ceaaf4d | 2006-08-24 01:40:10 +0200 | [diff] [blame] | 122 | /* Read configuration */ |
| 123 | cfg_watch_dir (NULL, CFG_DIR, NULL); |
| 124 | |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame^] | 125 | /* Set the PATH environment variable */ |
| 126 | setenv ("PATH", PATH, TRUE); |
| 127 | |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 128 | |
| 129 | /* Generate and run the startup event */ |
| 130 | event_queue_edge ("startup"); |
| 131 | event_queue_run (NULL, NULL); |
| 132 | |
| 133 | /* Go! */ |
| 134 | ret = nih_main_loop (); |
| 135 | |
| 136 | return ret; |
Scott James Remnant | 5074884 | 2006-05-16 21:02:31 +0100 | [diff] [blame] | 137 | } |
Scott James Remnant | f43bdf3 | 2006-08-27 18:20:29 +0100 | [diff] [blame^] | 138 | |
| 139 | |
| 140 | /** |
| 141 | * segv_handler: |
| 142 | * @signum: |
| 143 | * |
| 144 | * Handle receiving the SEGV signal, usually caused by one of our own |
| 145 | * mistakes. We deal with it by dumping core in a child process and |
| 146 | * just carrying on in the parent. |
| 147 | **/ |
| 148 | static void |
| 149 | segv_handler (int signum) |
| 150 | { |
| 151 | pid_t pid; |
| 152 | |
| 153 | pid = fork (); |
| 154 | if (pid == 0) { |
| 155 | struct sigaction act; |
| 156 | struct rlimit limit; |
| 157 | sigset_t mask; |
| 158 | |
| 159 | /* Mask out all signals */ |
| 160 | sigfillset (&mask); |
| 161 | sigprocmask (SIG_SETMASK, &mask, NULL); |
| 162 | |
| 163 | /* Set the SEGV handler to the default so core is dumped */ |
| 164 | act.sa_handler = SIG_DFL; |
| 165 | act.sa_flags = 0; |
| 166 | sigemptyset (&act.sa_mask); |
| 167 | sigaction (SIGSEGV, &act, NULL); |
| 168 | |
| 169 | /* Dump in the root directory */ |
| 170 | chdir ("/"); |
| 171 | |
| 172 | /* Don't limit the core dump size */ |
| 173 | limit.rlim_cur = RLIM_INFINITY; |
| 174 | limit.rlim_max = RLIM_INFINITY; |
| 175 | setrlimit (RLIMIT_CORE, &limit); |
| 176 | |
| 177 | /* Raise the signal */ |
| 178 | raise (SIGSEGV); |
| 179 | |
| 180 | /* Unmask so that we receive it */ |
| 181 | sigdelset (&mask, SIGSEGV); |
| 182 | sigprocmask (SIG_SETMASK, &mask, NULL); |
| 183 | |
| 184 | /* Wait for death */ |
| 185 | pause (); |
| 186 | exit (0); |
| 187 | } else if (pid > 0) { |
| 188 | /* Wait for the core to be generated */ |
| 189 | waitpid (pid, NULL, 0); |
| 190 | |
| 191 | nih_error (_("Caught segmentation fault, core dumped")); |
| 192 | } else { |
| 193 | nih_error (_("Caught segmentation fault, unable to dump core")); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * cad_handler: |
| 199 | * @data: unused, |
| 200 | * @signal: signal that called this handler. |
| 201 | * |
| 202 | * Handle having recieved the SIGINT signal, sent to us when somebody |
| 203 | * presses Ctrl-Alt-Delete on the console. We just generate a |
| 204 | * control-alt-delete event. |
| 205 | **/ |
| 206 | static void |
| 207 | cad_handler (void *data, |
| 208 | NihSignal *signal) |
| 209 | { |
| 210 | event_queue_edge ("control-alt-delete"); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * kbd_handler: |
| 215 | * @data: unused, |
| 216 | * @signal: signal that called this handler. |
| 217 | * |
| 218 | * Handle having recieved the SIGWINCH signal, sent to us when somebody |
| 219 | * presses Alt-UpArrow on the console. We just generate a |
| 220 | * kbdrequest event. |
| 221 | **/ |
| 222 | static void |
| 223 | kbd_handler (void *data, |
| 224 | NihSignal *signal) |
| 225 | { |
| 226 | event_queue_edge ("kbdrequest"); |
| 227 | } |