root/src/noitd.c

Revision 13c6915acb2b7e36b5abdf279d6a43532977854a, 9.3 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 4 years ago)

make boot fail with invalid config

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007-2009, 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 "eventer/eventer.h"
47 #include "utils/noit_log.h"
48 #include "utils/noit_hash.h"
49 #include "utils/noit_security.h"
50 #include "utils/noit_watchdog.h"
51 #include "noit_listener.h"
52 #include "noit_console.h"
53 #include "noit_jlog_listener.h"
54 #include "noit_rest.h"
55 #include "noit_check_rest.h"
56 #include "noit_livestream_listener.h"
57 #include "noit_capabilities_listener.h"
58 #include "noit_module.h"
59 #include "noit_conf.h"
60 #include "noit_conf_checks.h"
61 #include "noit_filters.h"
62
63 #define APPNAME "noit"
64 #define CHILD_WATCHDOG_TIMEOUT 5 /*seconds*/
65
66 static char *config_file = ETC_DIR "/" APPNAME ".conf";
67 static const char *droptouser = NULL;
68 static const char *droptogroup = NULL;
69 static const char *chrootpath = NULL;
70 static int foreground = 0;
71 static int debug = 0;
72 static int strict_module_load = 0;
73 static char **enable_logs;
74 static int enable_logs_cnt = 0;
75 static char **disable_logs;
76 static int disable_logs_cnt = 0;
77
78 #include "man/noitd.usage.h"
79 static void usage(const char *progname) {
80   printf("Usage for %s:\n", progname);
81 #ifdef NOITD_USAGE
82   assert(write(STDOUT_FILENO,
83                NOITD_USAGE,
84                sizeof(NOITD_USAGE)-1) == sizeof(NOITD_USAGE)-1);
85 #else
86   printf("\nError in usage, build problem.\n");
87 #endif
88   return;
89 }
90
91 void cli_log_switches() {
92   int i;
93   noit_log_stream_t ls;
94   for(i=0; i<enable_logs_cnt; i++) {
95     ls = noit_log_stream_find(enable_logs[i]);
96     if(!ls) noitL(noit_error, "No such log: '%s'\n", enable_logs[i]);
97     if(ls && !ls->enabled) {
98       noitL(noit_error, "Enabling %s\n", enable_logs[i]);
99       ls->enabled = 1;
100     }
101   }
102   for(i=0; i<disable_logs_cnt; i++) {
103     ls = noit_log_stream_find(disable_logs[i]);
104     if(!ls) noitL(noit_error, "No such log: '%s'\n", enable_logs[i]);
105     if(ls && ls->enabled) {
106       noitL(noit_error, "Disabling %s\n", disable_logs[i]);
107       ls->enabled = 0;
108     }
109   }
110 }
111 void parse_clargs(int argc, char **argv) {
112   int c;
113   enable_logs = calloc(argc, sizeof(*enable_logs));
114   disable_logs = calloc(argc, sizeof(*disable_logs));
115   while((c = getopt(argc, argv, "Mhc:dDu:g:t:l:L:")) != EOF) {
116     switch(c) {
117       case 'M':
118         strict_module_load = 1;
119         break;
120       case 'h':
121         usage(argv[0]);
122         exit(1);
123         break;
124       case 'l':
125         enable_logs[enable_logs_cnt++] = strdup(optarg);
126         break;
127       case 'L':
128         disable_logs[disable_logs_cnt++] = strdup(optarg);
129         break;
130       case 'u':
131         droptouser = strdup(optarg);
132         break;
133       case 'g':
134         droptogroup = strdup(optarg);
135         break;
136       case 't':
137         chrootpath = strdup(optarg);
138         break;
139       case 'c':
140         config_file = strdup(optarg);
141         break;
142       case 'D':
143         foreground = 1;
144         break;
145       case 'd':
146         debug++;
147         break;
148       default:
149         break;
150     }
151   }
152 }
153
154 static
155 int configure_eventer() {
156   int rv = 0;
157   noit_hash_table *table;
158   table = noit_conf_get_hash(NULL, "/" APPNAME "/eventer/config");
159   if(table) {
160     noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
161     const char *key, *value;
162     int klen;
163     while(noit_hash_next_str(table, &iter, &key, &klen, &value)) {
164       int subrv;
165       if((subrv = eventer_propset(key, value)) != 0)
166         rv = subrv;
167     }
168     noit_hash_destroy(table, free, free);
169     free(table);
170   }
171   return rv;
172 }
173
174 static int __reload_needed = 0;
175 static void request_conf_reload(int sig) {
176   if(sig == SIGHUP) {
177     __reload_needed = 1;
178   }
179 }
180 static int noitice_hup(eventer_t e, int mask, void *unused, struct timeval *now) {
181   if(__reload_needed) {
182     noitL(noit_error, "SIGHUP received, performing reload\n");
183     if(noit_conf_load(config_file) == -1) {
184       noitL(noit_error, "Cannot load config: '%s'\n", config_file);
185       exit(-1);
186     }
187     noit_poller_reload(NULL);
188     __reload_needed = 0;
189   }
190   return 0;
191 }
192 static int child_main() {
193   eventer_t e;
194
195   /* Load our config...
196    * to ensure it is current w.r.t. to this child starting */
197   if(noit_conf_load(config_file) == -1) {
198     noitL(noit_error, "Cannot load config: '%s'\n", config_file);
199     exit(2);
200   }
201
202   noit_log_reopen_all();
203
204   signal(SIGHUP, request_conf_reload);
205
206   /* initialize the eventer */
207   if(eventer_init() == -1) {
208     noitL(noit_stderr, "Cannot initialize eventer\n");
209     exit(-1);
210   }
211
212   /* Setup our heartbeat */
213   noit_watchdog_child_eventer_heartbeat();
214
215   e = eventer_alloc();
216   e->mask = EVENTER_RECURRENT;
217   e->callback = noitice_hup;
218   eventer_add_recurrent(e);
219
220   /* Initialize all of our listeners */
221   noit_console_init(APPNAME);
222   noit_console_conf_init();
223   noit_console_conf_checks_init();
224   noit_capabilities_listener_init();
225   noit_jlog_listener_init();
226   noit_http_rest_init();
227   noit_check_rest_init();
228   noit_filters_rest_init();
229   noit_livestream_listener_init();
230
231   noit_module_init();
232   if(strict_module_load && noit_module_load_failures() > 0) {
233     noitL(noit_stderr, "Failed to load some modules and -M given.\n");
234     exit(2);
235   }
236
237   /* Drop privileges */
238   if(chrootpath && noit_security_chroot(chrootpath)) {
239     noitL(noit_stderr, "Failed to chroot(), exiting.\n");
240     exit(2);
241   }
242   if(noit_security_usergroup(droptouser, droptogroup, noit_false)) {
243     noitL(noit_stderr, "Failed to drop privileges, exiting.\n");
244     exit(2);
245   }
246
247   /* Prepare for launch... */
248   noit_filters_init();
249   noit_poller_init();
250   noit_listener_init(APPNAME);
251
252   /* Write our log out, and setup a watchdog to write it out on change. */
253   noit_conf_write_log(NULL);
254   noit_conf_coalesce_changes(10); /* 10 seconds of no changes before we write */
255   noit_conf_watch_and_journal_watchdog(noit_conf_write_log, NULL);
256
257   eventer_loop();
258   return 0;
259 }
260
261 int main(int argc, char **argv) {
262   char conf_str[1024];
263   char user[32], group[32];
264
265   parse_clargs(argc, argv);
266
267   /* First initialize logging, so we can log errors */
268   noit_log_init();
269   noit_log_stream_add_stream(noit_debug, noit_stderr);
270   noit_log_stream_add_stream(noit_error, noit_stderr);
271
272   /* Next load the configs */
273   noit_conf_init(APPNAME);
274   if(noit_conf_load(config_file) == -1) {
275     fprintf(stderr, "Cannot load config: '%s'\n", config_file);
276     exit(-1);
277   }
278
279   /* Reinitialize the logging system now that we have a config */
280   snprintf(user, sizeof(user), "%d", getuid());
281   snprintf(group, sizeof(group), "%d", getgid());
282   if(noit_security_usergroup(droptouser, droptogroup, noit_true)) {
283     noitL(noit_stderr, "Failed to drop privileges, exiting.\n");
284     exit(-1);
285   }
286   noit_conf_log_init(APPNAME);
287   cli_log_switches();
288   if(noit_security_usergroup(user, group, noit_true)) {
289     noitL(noit_stderr, "Failed to regain privileges, exiting.\n");
290     exit(-1);
291   }
292   if(debug)
293     noit_debug->enabled = 1;
294
295   /* Lastly, run through all other system inits */
296   if(!noit_conf_get_stringbuf(NULL, "/" APPNAME "/eventer/@implementation",
297                               conf_str, sizeof(conf_str))) {
298     noitL(noit_stderr, "Cannot find '%s' in configuration\n",
299           "/" APPNAME "/eventer/@implementation");
300     exit(-1);
301   }
302   if(eventer_choose(conf_str) == -1) {
303     noitL(noit_stderr, "Cannot choose eventer %s\n", conf_str);
304     exit(-1);
305   }
306   if(configure_eventer() != 0) {
307     noitL(noit_stderr, "Cannot configure eventer\n");
308     exit(-1);
309   }
310
311   noit_watchdog_prefork_init();
312
313   if(chdir("/") != 0) {
314     noitL(noit_stderr, "Failed chdir(\"/\"): %s\n", strerror(errno));
315     exit(-1);
316   }
317   if(foreground) return child_main();
318
319   close(STDIN_FILENO);
320   close(STDOUT_FILENO);
321   close(STDERR_FILENO);
322   if(fork()) exit(0);
323   setsid();
324   if(fork()) exit(0);
325
326   signal(SIGHUP, SIG_IGN);
327   return noit_watchdog_start_child("noitd", child_main, 0);
328 }
Note: See TracBrowser for help on using the browser.