root/src/noitd.c

Revision 1736a5cea51dadc9d19682cfc5ccb714a9f817ce, 6.9 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 6 months ago)

json endpoints for /eventer/{sockets,timers,jobq}.json

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