blob: aa9ab85894d1b07a62afc6e94fe99cc4b190bac1 [file] [log] [blame]
Scott James Remnant50748842006-05-16 21:02:31 +01001/* upstart
2 *
Scott James Remnantb6270dd2006-07-13 02:16:38 +01003 * Copyright © 2006 Canonical Ltd.
4 * Author: Scott James Remnant <scott@ubuntu.com>.
Scott James Remnant50748842006-05-16 21:02:31 +01005 *
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 Remnantf43bdf32006-08-27 18:20:29 +010026#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 Remnant94d00982006-08-25 15:38:22 +020035#include <stdlib.h>
Scott James Remnant12a330f2006-08-24 02:19:09 +020036#include <syslog.h>
Scott James Remnant027dd7b2006-08-21 09:01:25 +020037#include <unistd.h>
38
Scott James Remnant77e8db32006-08-21 08:47:50 +020039#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 Remnant50748842006-05-16 21:02:31 +010053
54
Scott James Remnantf43bdf32006-08-27 18:20:29 +010055/* Prototypes for static functions */
56static void segv_handler (int signum);
57static void cad_handler (void *data, NihSignal *signal);
58static void kbd_handler (void *data, NihSignal *signal);
59
60
Scott James Remnant50748842006-05-16 21:02:31 +010061int
62main (int argc,
63 char *argv[])
64{
Scott James Remnant77e8db32006-08-21 08:47:50 +020065 int ret, i;
Scott James Remnant50748842006-05-16 21:02:31 +010066
Scott James Remnant77e8db32006-08-21 08:47:50 +020067 nih_main_init (argv[0]);
68
Scott James Remnant12a330f2006-08-24 02:19:09 +020069 openlog (program_name, LOG_CONS, LOG_DAEMON);
70
Scott James Remnant1cd8d862006-08-31 15:17:16 +010071 nih_log_set_priority (NIH_LOG_DEBUG);
Scott James Remnant12a330f2006-08-24 02:19:09 +020072 nih_log_set_logger (nih_logger_syslog);
Scott James Remnant77e8db32006-08-21 08:47:50 +020073
Scott James Remnant77e8db32006-08-21 08:47:50 +020074 /* 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 Remnantf4970812006-08-27 18:27:19 +010082 /* Check we're root */
83 if (getuid ()) {
84 nih_error (_("Need to be root"));
85 exit (1);
86 }
87
88 /* Check we're process #1 */
89 if (getpid () > 1) {
90 nih_error (_("Not being executed as init"));
91 exit (1);
92 }
93
Scott James Remnantf43bdf32006-08-27 18:20:29 +010094
95 /* Reset the signal state and install the signal handler for those
96 * signals we actually want to catch; this also sets those that
97 * can be sent to us, because we're special
98 */
99 nih_signal_reset ();
100 nih_signal_set_handler (SIGALRM, nih_signal_handler);
101 nih_signal_set_handler (SIGHUP, nih_signal_handler);
102 nih_signal_set_handler (SIGINT, nih_signal_handler);
103 nih_signal_set_handler (SIGWINCH, nih_signal_handler);
104 nih_signal_set_handler (SIGSEGV, segv_handler);
105
106 /* Ask the kernel to send us SIGINT when control-alt-delete is
107 * pressed; generate an event with the same name.
108 */
109 reboot (RB_DISABLE_CAD);
110 nih_signal_add_callback (NULL, SIGINT, cad_handler, NULL);
111
112 /* Ask the kernel to send us SIGWINCH when alt-uparrow is pressed;
113 * generate a kbdrequest event.
114 */
115 ioctl (0, KDSIGACCEPT, SIGWINCH);
116 nih_signal_add_callback (NULL, SIGWINCH, kbd_handler, NULL);
117
118
119 /* Reap all children that die */
120 nih_child_add_watch (NULL, -1, job_child_reaper, NULL);
121
Scott James Remnantdc8877d2006-08-29 16:56:48 +0100122 /* Process the event queue and check the jobs for idleness
123 * every time through the main loop */
Scott James Remnant0c7e72a2006-08-27 22:22:53 +0100124 nih_main_loop_add_func (NULL, (NihMainLoopCb)event_queue_run, NULL);
Scott James Remnantdc8877d2006-08-29 16:56:48 +0100125 nih_main_loop_add_func (NULL, (NihMainLoopCb)job_detect_idle, NULL);
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100126
Scott James Remnant94d00982006-08-25 15:38:22 +0200127
Scott James Remnant77e8db32006-08-21 08:47:50 +0200128 /* Become session and process group leader (should be already,
129 * but you never know what initramfs did
130 */
131 setsid ();
132
133 /* Open control socket */
134 control_open ();
135
Scott James Remnantceaaf4d2006-08-24 01:40:10 +0200136 /* Read configuration */
137 cfg_watch_dir (NULL, CFG_DIR, NULL);
138
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100139 /* Set the PATH environment variable */
140 setenv ("PATH", PATH, TRUE);
141
Scott James Remnant77e8db32006-08-21 08:47:50 +0200142
143 /* Generate and run the startup event */
Scott James Remnantff5efb92006-08-31 01:45:19 +0100144 event_queue ("startup");
Scott James Remnant0c7e72a2006-08-27 22:22:53 +0100145 event_queue_run ();
Scott James Remnant32de9222006-08-31 04:34:27 +0100146 job_detect_idle ();
Scott James Remnant77e8db32006-08-21 08:47:50 +0200147
148 /* Go! */
149 ret = nih_main_loop ();
150
151 return ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100152}
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100153
154
155/**
156 * segv_handler:
157 * @signum:
158 *
159 * Handle receiving the SEGV signal, usually caused by one of our own
160 * mistakes. We deal with it by dumping core in a child process and
161 * just carrying on in the parent.
162 **/
163static void
164segv_handler (int signum)
165{
166 pid_t pid;
167
168 pid = fork ();
169 if (pid == 0) {
170 struct sigaction act;
171 struct rlimit limit;
172 sigset_t mask;
173
174 /* Mask out all signals */
175 sigfillset (&mask);
176 sigprocmask (SIG_SETMASK, &mask, NULL);
177
178 /* Set the SEGV handler to the default so core is dumped */
179 act.sa_handler = SIG_DFL;
180 act.sa_flags = 0;
181 sigemptyset (&act.sa_mask);
182 sigaction (SIGSEGV, &act, NULL);
183
184 /* Dump in the root directory */
185 chdir ("/");
186
187 /* Don't limit the core dump size */
188 limit.rlim_cur = RLIM_INFINITY;
189 limit.rlim_max = RLIM_INFINITY;
190 setrlimit (RLIMIT_CORE, &limit);
191
192 /* Raise the signal */
193 raise (SIGSEGV);
194
195 /* Unmask so that we receive it */
196 sigdelset (&mask, SIGSEGV);
197 sigprocmask (SIG_SETMASK, &mask, NULL);
198
199 /* Wait for death */
200 pause ();
201 exit (0);
202 } else if (pid > 0) {
203 /* Wait for the core to be generated */
204 waitpid (pid, NULL, 0);
205
206 nih_error (_("Caught segmentation fault, core dumped"));
207 } else {
208 nih_error (_("Caught segmentation fault, unable to dump core"));
209 }
210}
211
212/**
213 * cad_handler:
214 * @data: unused,
215 * @signal: signal that called this handler.
216 *
217 * Handle having recieved the SIGINT signal, sent to us when somebody
218 * presses Ctrl-Alt-Delete on the console. We just generate a
219 * control-alt-delete event.
220 **/
221static void
222cad_handler (void *data,
223 NihSignal *signal)
224{
Scott James Remnantff5efb92006-08-31 01:45:19 +0100225 event_queue ("control-alt-delete");
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100226}
227
228/**
229 * kbd_handler:
230 * @data: unused,
231 * @signal: signal that called this handler.
232 *
233 * Handle having recieved the SIGWINCH signal, sent to us when somebody
234 * presses Alt-UpArrow on the console. We just generate a
235 * kbdrequest event.
236 **/
237static void
238kbd_handler (void *data,
239 NihSignal *signal)
240{
Scott James Remnantff5efb92006-08-31 01:45:19 +0100241 event_queue ("kbdrequest");
Scott James Remnantf43bdf32006-08-27 18:20:29 +0100242}