root/src/noitd.c

Revision 60f61acb95b034095cb82701e6786a3139e97cb6, 7.4 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 1 month ago)

Support Illumos privileges.

  • 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_memory.h"
49 #include "utils/noit_log.h"
50 #include "utils/noit_hash.h"
51 #include "utils/noit_security.h"
52 #include "utils/noit_watchdog.h"
53 #include "utils/noit_lockfile.h"
54 #include "noit_listener.h"
55 #include "noit_console.h"
56 #include "noit_jlog_listener.h"
57 #include "noit_rest.h"
58 #include "noit_reverse_socket.h"
59 #include "noit_check_rest.h"
60 #include "noit_events_rest.h"
61 #include "noit_livestream_listener.h"
62 #include "noit_capabilities_listener.h"
63 #include "noit_module.h"
64 #include "noit_conf.h"
65 #include "noit_conf_checks.h"
66 #include "noit_filters.h"
67
68 #define APPNAME "noit"
69 #define CHILD_WATCHDOG_TIMEOUT 5 /*seconds*/
70
71 static char *config_file = ETC_DIR "/" APPNAME ".conf";
72 static char *xpath = NULL;
73 static const char *droptouser = NULL;
74 static const char *droptogroup = NULL;
75 static const char *chrootpath = NULL;
76 static int foreground = 0;
77 static int debug = 0;
78 static int strict_module_load = 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   while((c = getopt(argc, argv, "x:Mhc:dDu:g:n:t:l:L:G:")) != EOF) {
97     switch(c) {
98       case 'x':
99         xpath = strdup(optarg);
100         foreground = 1;
101         break;
102       case 'G':
103         glider = strdup(optarg);
104         break;
105       case 'M':
106         strict_module_load = 1;
107         break;
108       case 'h':
109         usage(argv[0]);
110         exit(1);
111         break;
112       case 'l':
113         noit_main_enable_log(optarg);
114         break;
115       case 'L':
116         noit_main_disable_log(optarg);
117         break;
118       case 'n':
119         {
120           char *cp = optarg ? strchr(optarg, ':') : NULL;
121           if(!cp) noit_listener_skip(optarg, 0);
122           else {
123             if(cp == optarg) optarg = NULL;
124             *cp++ = '\0';
125             noit_listener_skip(optarg, atoi(cp));
126           }
127         }
128         break;
129       case 'u':
130         droptouser = strdup(optarg);
131         break;
132       case 'g':
133         droptogroup = strdup(optarg);
134         break;
135       case 't':
136         chrootpath = strdup(optarg);
137         break;
138       case 'c':
139         config_file = strdup(optarg);
140         break;
141       case 'D':
142         foreground++;
143         break;
144       case 'd':
145         debug++;
146         break;
147       default:
148         break;
149     }
150   }
151 }
152
153 static int __reload_needed = 0;
154 static void request_conf_reload(int sig) {
155   if(sig == SIGHUP) {
156     __reload_needed = 1;
157   }
158 }
159 static int noitice_hup(eventer_t e, int mask, void *unused, struct timeval *now) {
160   if(__reload_needed) {
161     noitL(noit_error, "SIGHUP received, performing reload\n");
162     if(noit_conf_load(config_file) == -1) {
163       noitL(noit_error, "Cannot load config: '%s'\n", config_file);
164       exit(-1);
165     }
166     noit_poller_reload(NULL);
167     __reload_needed = 0;
168   }
169   return 0;
170 }
171 static int child_main() {
172   eventer_t e;
173
174   /* Send out a birth notice. */
175   noit_watchdog_child_heartbeat();
176
177   /* Load our config...
178    * to ensure it is current w.r.t. to this child starting */
179   if(noit_conf_load(config_file) == -1) {
180     noitL(noit_error, "Cannot load config: '%s'\n", config_file);
181     exit(2);
182   }
183   if(xpath) {
184     int cnt, i;
185     noit_conf_section_t *parts = NULL;
186     parts = noit_conf_get_sections(NULL, xpath, &cnt);
187     if(cnt == 0) exit(2);
188     for(i=0; i<cnt; i++) {
189       fprintf(stdout, "%d: ", i); fflush(stdout);
190       noit_conf_write_section(parts[i], 1);
191     }
192     free(parts);
193     exit(0);
194   }
195
196   noit_log_reopen_all();
197   noitL(noit_notice, "process starting: %d\n", (int)getpid());
198   noit_log_go_asynch();
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   /* rotation init requires, eventer_init() */
208   noit_conf_log_init_rotate(APPNAME, noit_false);
209
210   /* Setup our heartbeat */
211   noit_watchdog_child_eventer_heartbeat();
212
213   e = eventer_alloc();
214   e->mask = EVENTER_RECURRENT;
215   e->callback = noitice_hup;
216   eventer_add_recurrent(e);
217
218   /* Initialize all of our listeners */
219   noit_console_init(APPNAME);
220   noit_console_conf_init();
221   noit_console_conf_checks_init();
222   noit_capabilities_listener_init();
223   noit_jlog_listener_init();
224   noit_http_rest_init();
225   noit_reverse_socket_init();
226   noit_events_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   noit_listener_init(APPNAME);
238
239   /* Drop privileges */
240   noit_conf_security_init(APPNAME, droptouser, droptogroup, chrootpath);
241
242   /* Prepare for launch... */
243   noit_filters_init();
244   noit_poller_init();
245
246   /* Write our log out, and setup a watchdog to write it out on change. */
247   noit_conf_write_log(NULL);
248   noit_conf_coalesce_changes(10); /* 10 seconds of no changes before we write */
249   noit_conf_watch_and_journal_watchdog(noit_conf_write_log, NULL);
250
251   eventer_loop();
252   return 0;
253 }
254
255 int main(int argc, char **argv) {
256   int lock = NOIT_LOCK_OP_LOCK;
257   noit_memory_init();
258   parse_clargs(argc, argv);
259   if (xpath) lock = NOIT_LOCK_OP_NONE;
260   return noit_main(APPNAME, config_file, debug, foreground,
261                    lock, glider, droptouser, droptogroup,
262                    child_main);
263 }
Note: See TracBrowser for help on using the browser.