root/src/noitd.c

Revision 48647bb29c7c49cc39e67ce571de2d505a3e44e0, 9.0 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 5 years ago)

enable -l/-L for noitd, just like we did for stratcond

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