root/src/noitd.c

Revision 6e0e4ebc94a88b13dd060d72ad9e43a3cc32ec9e, 6.5 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 3 years ago)

remove lurkers

  • 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 *glider = NULL;
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 parse_clargs(int argc, char **argv) {
91   int c;
92   while((c = getopt(argc, argv, "Mhc:dDu:g:t:l:L:G:")) != EOF) {
93     switch(c) {
94       case 'G':
95         glider = strdup(optarg);
96         break;
97       case 'M':
98         strict_module_load = 1;
99         break;
100       case 'h':
101         usage(argv[0]);
102         exit(1);
103         break;
104       case 'l':
105         noit_main_enable_log(optarg);
106         break;
107       case 'L':
108         noit_main_disable_log(optarg);
109         break;
110       case 'u':
111         droptouser = strdup(optarg);
112         break;
113       case 'g':
114         droptogroup = strdup(optarg);
115         break;
116       case 't':
117         chrootpath = strdup(optarg);
118         break;
119       case 'c':
120         config_file = strdup(optarg);
121         break;
122       case 'D':
123         foreground = 1;
124         break;
125       case 'd':
126         debug++;
127         break;
128       default:
129         break;
130     }
131   }
132 }
133
134 static int __reload_needed = 0;
135 static void request_conf_reload(int sig) {
136   if(sig == SIGHUP) {
137     __reload_needed = 1;
138   }
139 }
140 static int noitice_hup(eventer_t e, int mask, void *unused, struct timeval *now) {
141   if(__reload_needed) {
142     noitL(noit_error, "SIGHUP received, performing reload\n");
143     if(noit_conf_load(config_file) == -1) {
144       noitL(noit_error, "Cannot load config: '%s'\n", config_file);
145       exit(-1);
146     }
147     noit_poller_reload(NULL);
148     __reload_needed = 0;
149   }
150   return 0;
151 }
152 static int child_main() {
153   eventer_t e;
154
155   /* Load our config...
156    * to ensure it is current w.r.t. to this child starting */
157   if(noit_conf_load(config_file) == -1) {
158     noitL(noit_error, "Cannot load config: '%s'\n", config_file);
159     exit(2);
160   }
161
162   noit_log_reopen_all();
163
164   signal(SIGHUP, request_conf_reload);
165
166   /* initialize the eventer */
167   if(eventer_init() == -1) {
168     noitL(noit_stderr, "Cannot initialize eventer\n");
169     exit(-1);
170   }
171   /* rotation init requires, eventer_init() */
172   noit_conf_log_init_rotate(APPNAME, noit_false);
173
174   /* Setup our heartbeat */
175   noit_watchdog_child_eventer_heartbeat();
176
177   e = eventer_alloc();
178   e->mask = EVENTER_RECURRENT;
179   e->callback = noitice_hup;
180   eventer_add_recurrent(e);
181
182   /* Initialize all of our listeners */
183   noit_console_init(APPNAME);
184   noit_console_conf_init();
185   noit_console_conf_checks_init();
186   noit_capabilities_listener_init();
187   noit_jlog_listener_init();
188   noit_http_rest_init();
189   noit_check_rest_init();
190   noit_filters_rest_init();
191   noit_livestream_listener_init();
192
193   noit_module_init();
194   if(strict_module_load && noit_module_load_failures() > 0) {
195     noitL(noit_stderr, "Failed to load some modules and -M given.\n");
196     exit(2);
197   }
198
199   /* Drop privileges */
200   if(chrootpath && noit_security_chroot(chrootpath)) {
201     noitL(noit_stderr, "Failed to chroot(), exiting.\n");
202     exit(2);
203   }
204   if(noit_security_usergroup(droptouser, droptogroup, noit_false)) {
205     noitL(noit_stderr, "Failed to drop privileges, exiting.\n");
206     exit(2);
207   }
208
209   /* Prepare for launch... */
210   noit_filters_init();
211   noit_poller_init();
212   noit_listener_init(APPNAME);
213
214   /* Write our log out, and setup a watchdog to write it out on change. */
215   noit_conf_write_log(NULL);
216   noit_conf_coalesce_changes(10); /* 10 seconds of no changes before we write */
217   noit_conf_watch_and_journal_watchdog(noit_conf_write_log, NULL);
218
219   eventer_loop();
220   return 0;
221 }
222
223 int main(int argc, char **argv) {
224   parse_clargs(argc, argv);
225   return noit_main(APPNAME, config_file, debug, foreground,
226                    glider, droptouser, droptogroup, child_main);
227 }
Note: See TracBrowser for help on using the browser.