root/src/noitd.c

Revision 9267e43ddfd7b4b47ee778995e83ee06f228731a, 6.7 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 3 years ago)

refactor this into something reusable

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007-2010, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  *       notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  *       copyright notice, this list of conditions and the following
13  *       disclaimer in the documentation and/or other materials provided
14  *       with the distribution.
15  *     * Neither the name OmniTI Computer Consulting, Inc. nor the names
16  *       of its contributors may be used to endorse or promote products
17  *       derived from this software without specific prior written
18  *       permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 #include "noit_defines.h"
33
34 #include <assert.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <sys/mman.h>
41 #include <signal.h>
42 #ifdef HAVE_SYS_WAIT_H
43 #include <sys/wait.h>
44 #endif
45
46 #include "noit_main.h"
47 #include "eventer/eventer.h"
48 #include "utils/noit_log.h"
49 #include "utils/noit_hash.h"
50 #include "utils/noit_security.h"
51 #include "utils/noit_watchdog.h"
52 #include "utils/noit_lockfile.h"
53 #include "noit_listener.h"
54 #include "noit_console.h"
55 #include "noit_jlog_listener.h"
56 #include "noit_rest.h"
57 #include "noit_check_rest.h"
58 #include "noit_livestream_listener.h"
59 #include "noit_capabilities_listener.h"
60 #include "noit_module.h"
61 #include "noit_conf.h"
62 #include "noit_conf_checks.h"
63 #include "noit_filters.h"
64
65 #define APPNAME "noit"
66 #define CHILD_WATCHDOG_TIMEOUT 5 /*seconds*/
67
68 static char *config_file = ETC_DIR "/" APPNAME ".conf";
69 static const char *droptouser = NULL;
70 static const char *droptogroup = NULL;
71 static const char *chrootpath = NULL;
72 static int foreground = 0;
73 static int debug = 0;
74 static int strict_module_load = 0;
75 static char **enable_logs;
76 static int enable_logs_cnt = 0;
77 static char **disable_logs;
78 static int disable_logs_cnt = 0;
79 static char *glider = NULL;
80
81 #include "man/noitd.usage.h"
82 static void usage(const char *progname) {
83   printf("Usage for %s:\n", progname);
84 #ifdef NOITD_USAGE
85   assert(write(STDOUT_FILENO,
86                NOITD_USAGE,
87                sizeof(NOITD_USAGE)-1) == sizeof(NOITD_USAGE)-1);
88 #else
89   printf("\nError in usage, build problem.\n");
90 #endif
91   return;
92 }
93
94 void parse_clargs(int argc, char **argv) {
95   int c;
96   enable_logs = calloc(argc, sizeof(*enable_logs));
97   disable_logs = calloc(argc, sizeof(*disable_logs));
98   while((c = getopt(argc, argv, "Mhc:dDu:g:t:l:L:G:")) != EOF) {
99     switch(c) {
100       case 'G':
101         glider = strdup(optarg);
102         break;
103       case 'M':
104         strict_module_load = 1;
105         break;
106       case 'h':
107         usage(argv[0]);
108         exit(1);
109         break;
110       case 'l':
111         noit_main_enable_log(optarg);
112         break;
113       case 'L':
114         noit_main_disable_log(optarg);
115         break;
116       case 'u':
117         droptouser = strdup(optarg);
118         break;
119       case 'g':
120         droptogroup = strdup(optarg);
121         break;
122       case 't':
123         chrootpath = strdup(optarg);
124         break;
125       case 'c':
126         config_file = strdup(optarg);
127         break;
128       case 'D':
129         foreground = 1;
130         break;
131       case 'd':
132         debug++;
133         break;
134       default:
135         break;
136     }
137   }
138 }
139
140 static int __reload_needed = 0;
141 static void request_conf_reload(int sig) {
142   if(sig == SIGHUP) {
143     __reload_needed = 1;
144   }
145 }
146 static int noitice_hup(eventer_t e, int mask, void *unused, struct timeval *now) {
147   if(__reload_needed) {
148     noitL(noit_error, "SIGHUP received, performing reload\n");
149     if(noit_conf_load(config_file) == -1) {
150       noitL(noit_error, "Cannot load config: '%s'\n", config_file);
151       exit(-1);
152     }
153     noit_poller_reload(NULL);
154     __reload_needed = 0;
155   }
156   return 0;
157 }
158 static int child_main() {
159   eventer_t e;
160
161   /* Load our config...
162    * to ensure it is current w.r.t. to this child starting */
163   if(noit_conf_load(config_file) == -1) {
164     noitL(noit_error, "Cannot load config: '%s'\n", config_file);
165     exit(2);
166   }
167
168   noit_log_reopen_all();
169
170   signal(SIGHUP, request_conf_reload);
171
172   /* initialize the eventer */
173   if(eventer_init() == -1) {
174     noitL(noit_stderr, "Cannot initialize eventer\n");
175     exit(-1);
176   }
177   /* rotation init requires, eventer_init() */
178   noit_conf_log_init_rotate(APPNAME, noit_false);
179
180   /* Setup our heartbeat */
181   noit_watchdog_child_eventer_heartbeat();
182
183   e = eventer_alloc();
184   e->mask = EVENTER_RECURRENT;
185   e->callback = noitice_hup;
186   eventer_add_recurrent(e);
187
188   /* Initialize all of our listeners */
189   noit_console_init(APPNAME);
190   noit_console_conf_init();
191   noit_console_conf_checks_init();
192   noit_capabilities_listener_init();
193   noit_jlog_listener_init();
194   noit_http_rest_init();
195   noit_check_rest_init();
196   noit_filters_rest_init();
197   noit_livestream_listener_init();
198
199   noit_module_init();
200   if(strict_module_load && noit_module_load_failures() > 0) {
201     noitL(noit_stderr, "Failed to load some modules and -M given.\n");
202     exit(2);
203   }
204
205   /* Drop privileges */
206   if(chrootpath && noit_security_chroot(chrootpath)) {
207     noitL(noit_stderr, "Failed to chroot(), exiting.\n");
208     exit(2);
209   }
210   if(noit_security_usergroup(droptouser, droptogroup, noit_false)) {
211     noitL(noit_stderr, "Failed to drop privileges, exiting.\n");
212     exit(2);
213   }
214
215   /* Prepare for launch... */
216   noit_filters_init();
217   noit_poller_init();
218   noit_listener_init(APPNAME);
219
220   /* Write our log out, and setup a watchdog to write it out on change. */
221   noit_conf_write_log(NULL);
222   noit_conf_coalesce_changes(10); /* 10 seconds of no changes before we write */
223   noit_conf_watch_and_journal_watchdog(noit_conf_write_log, NULL);
224
225   eventer_loop();
226   return 0;
227 }
228
229 int main(int argc, char **argv) {
230   parse_clargs(argc, argv);
231   return noit_main(APPNAME, config_file, debug, foreground,
232                    glider, droptouser, droptogroup, child_main);
233 }
Note: See TracBrowser for help on using the browser.