blob: e6e7112e3d1cd139fadfcc13a16defe36796f543 [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 Remnant027dd7b2006-08-21 09:01:25 +020026#include <unistd.h>
27
Scott James Remnant77e8db32006-08-21 08:47:50 +020028#include <nih/macros.h>
29#include <nih/alloc.h>
30#include <nih/list.h>
31#include <nih/timer.h>
32#include <nih/signal.h>
33#include <nih/child.h>
34#include <nih/main.h>
35#include <nih/logging.h>
36
37#include "process.h"
38#include "job.h"
39#include "event.h"
40#include "control.h"
41#include "cfgfile.h"
Scott James Remnant50748842006-05-16 21:02:31 +010042
43
44int
45main (int argc,
46 char *argv[])
47{
Scott James Remnant77e8db32006-08-21 08:47:50 +020048 int ret, i;
Scott James Remnant50748842006-05-16 21:02:31 +010049
Scott James Remnant77e8db32006-08-21 08:47:50 +020050 nih_main_init (argv[0]);
51
52 nih_log_set_priority (NIH_LOG_DEBUG);
53
54
55 /* Reset the signal state and install the signal handler for those
56 * signals we actually want to catch; this also sets those that
57 * can be sent to us, because we're special
58 */
59 nih_signal_reset ();
60 nih_signal_set_handler (SIGALRM, nih_signal_handler);
61 nih_signal_set_handler (SIGTERM, nih_signal_handler);
62 nih_signal_set_handler (SIGHUP, nih_signal_handler);
63
64 nih_signal_add_callback (NULL, SIGTERM, nih_main_term_signal, NULL);
65 nih_signal_add_callback (NULL, SIGHUP, (NihSignalCb)cfg_read, NULL);
66
67 /* Reap all children that die */
68 nih_child_add_watch (NULL, -1, job_child_reaper, NULL);
69
70 /* Process the event queue every time through the main loop */
71 nih_main_loop_add_func (NULL, event_queue_run, NULL);
72
73
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
82
83 /* Become session and process group leader (should be already,
84 * but you never know what initramfs did
85 */
86 setsid ();
87
88 /* Open control socket */
89 control_open ();
90
91 /* Read configuration */
92 cfg_read ();
93
94
95 /* Generate and run the startup event */
96 event_queue_edge ("startup");
97 event_queue_run (NULL, NULL);
98
99 /* Go! */
100 ret = nih_main_loop ();
101
102 return ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100103}