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