blob: a0ce22709ce65c660fc0d036eb481deaabb15dc2 [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 Remnant12a330f2006-08-24 02:19:09 +020026#include <syslog.h>
Scott James Remnant027dd7b2006-08-21 09:01:25 +020027#include <unistd.h>
28
Scott James Remnant77e8db32006-08-21 08:47:50 +020029#include <nih/macros.h>
30#include <nih/alloc.h>
31#include <nih/list.h>
32#include <nih/timer.h>
33#include <nih/signal.h>
34#include <nih/child.h>
35#include <nih/main.h>
36#include <nih/logging.h>
37
38#include "process.h"
39#include "job.h"
40#include "event.h"
41#include "control.h"
42#include "cfgfile.h"
Scott James Remnant50748842006-05-16 21:02:31 +010043
44
45int
46main (int argc,
47 char *argv[])
48{
Scott James Remnant77e8db32006-08-21 08:47:50 +020049 int ret, i;
Scott James Remnant50748842006-05-16 21:02:31 +010050
Scott James Remnant77e8db32006-08-21 08:47:50 +020051 nih_main_init (argv[0]);
52
Scott James Remnant12a330f2006-08-24 02:19:09 +020053 openlog (program_name, LOG_CONS, LOG_DAEMON);
54
Scott James Remnant77e8db32006-08-21 08:47:50 +020055 nih_log_set_priority (NIH_LOG_DEBUG);
Scott James Remnant12a330f2006-08-24 02:19:09 +020056 nih_log_set_logger (nih_logger_syslog);
Scott James Remnant77e8db32006-08-21 08:47:50 +020057
58
59 /* Reset the signal state and install the signal handler for those
60 * signals we actually want to catch; this also sets those that
61 * can be sent to us, because we're special
62 */
63 nih_signal_reset ();
64 nih_signal_set_handler (SIGALRM, nih_signal_handler);
65 nih_signal_set_handler (SIGTERM, nih_signal_handler);
66 nih_signal_set_handler (SIGHUP, nih_signal_handler);
67
68 nih_signal_add_callback (NULL, SIGTERM, nih_main_term_signal, NULL);
Scott James Remnant77e8db32006-08-21 08:47:50 +020069
70 /* Reap all children that die */
71 nih_child_add_watch (NULL, -1, job_child_reaper, NULL);
72
73 /* Process the event queue every time through the main loop */
74 nih_main_loop_add_func (NULL, event_queue_run, NULL);
75
76
77 /* Close any file descriptors we inherited, and open the console
78 * device instead
79 */
80 for (i = 0; i < 3; i++)
81 close (i);
82
83 process_setup_console (CONSOLE_OUTPUT);
84
Scott James Remnant77e8db32006-08-21 08:47:50 +020085 /* Become session and process group leader (should be already,
86 * but you never know what initramfs did
87 */
88 setsid ();
89
90 /* Open control socket */
91 control_open ();
92
Scott James Remnantceaaf4d2006-08-24 01:40:10 +020093 /* Read configuration */
94 cfg_watch_dir (NULL, CFG_DIR, NULL);
95
Scott James Remnant77e8db32006-08-21 08:47:50 +020096
97 /* Generate and run the startup event */
98 event_queue_edge ("startup");
99 event_queue_run (NULL, NULL);
100
101 /* Go! */
102 ret = nih_main_loop ();
103
104 return ret;
Scott James Remnant50748842006-05-16 21:02:31 +0100105}