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 | 12a330f | 2006-08-24 02:19:09 +0200 | [diff] [blame^] | 26 | #include <syslog.h> |
Scott James Remnant | 027dd7b | 2006-08-21 09:01:25 +0200 | [diff] [blame] | 27 | #include <unistd.h> |
| 28 | |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 29 | #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 Remnant | 5074884 | 2006-05-16 21:02:31 +0100 | [diff] [blame] | 43 | |
| 44 | |
| 45 | int |
| 46 | main (int argc, |
| 47 | char *argv[]) |
| 48 | { |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 49 | int ret, i; |
Scott James Remnant | 5074884 | 2006-05-16 21:02:31 +0100 | [diff] [blame] | 50 | |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 51 | nih_main_init (argv[0]); |
| 52 | |
Scott James Remnant | 12a330f | 2006-08-24 02:19:09 +0200 | [diff] [blame^] | 53 | openlog (program_name, LOG_CONS, LOG_DAEMON); |
| 54 | |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 55 | nih_log_set_priority (NIH_LOG_DEBUG); |
Scott James Remnant | 12a330f | 2006-08-24 02:19:09 +0200 | [diff] [blame^] | 56 | nih_log_set_logger (nih_logger_syslog); |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 57 | |
| 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 Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 69 | |
| 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 Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 85 | /* 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 Remnant | ceaaf4d | 2006-08-24 01:40:10 +0200 | [diff] [blame] | 93 | /* Read configuration */ |
| 94 | cfg_watch_dir (NULL, CFG_DIR, NULL); |
| 95 | |
Scott James Remnant | 77e8db3 | 2006-08-21 08:47:50 +0200 | [diff] [blame] | 96 | |
| 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 Remnant | 5074884 | 2006-05-16 21:02:31 +0100 | [diff] [blame] | 105 | } |