root/src/stratcond.c

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

dead assignments, refs #283

  • 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
33 #include "noit_defines.h"
34
35 #include <assert.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <limits.h>
42
43 #include "eventer/eventer.h"
44 #include "utils/noit_log.h"
45 #include "utils/noit_hash.h"
46 #include "utils/noit_security.h"
47 #include "utils/noit_watchdog.h"
48 #include "utils/noit_lockfile.h"
49 #include "noit_listener.h"
50 #include "noit_console.h"
51 #include "noit_module.h"
52 #include "noit_conf.h"
53 #include "noit_rest.h"
54 #include "noit_capabilities_listener.h"
55 #include "stratcon_jlog_streamer.h"
56 #include "stratcon_datastore.h"
57 #include "stratcon_iep.h"
58 #include "stratcon_realtime_http.h"
59
60 #define APPNAME "stratcon"
61 static char *config_file = ETC_DIR "/" APPNAME ".conf";
62 static const char *droptouser = NULL;
63 static const char *droptogroup = NULL;
64 static const char *chrootpath = NULL;
65 static int foreground = 0;
66 static int debug = 0;
67 static int strict_module_load = 0;
68 static char **enable_logs;
69 static int enable_logs_cnt = 0;
70 static char **disable_logs;
71 static int disable_logs_cnt = 0;
72
73 #include "man/stratcond.usage.h"
74 static void usage(const char *progname) {
75   printf("Usage for %s:\n", progname);
76 #ifdef STRATCOND_USAGE
77   assert(write(STDOUT_FILENO,
78               STRATCOND_USAGE,
79               sizeof(STRATCOND_USAGE)-1) == sizeof(STRATCOND_USAGE)-1);
80 #else
81   printf("\nError in usage, build problem.\n");
82 #endif
83   return;
84 }
85
86 void cli_log_switches() {
87   int i;
88   noit_log_stream_t ls;
89   for(i=0; i<enable_logs_cnt; i++) {
90     ls = noit_log_stream_find(enable_logs[i]);
91     if(!ls) noitL(noit_error, "No such log: '%s'\n", enable_logs[i]);
92     if(ls && !ls->enabled) {
93       noitL(noit_error, "Enabling %s\n", enable_logs[i]);
94       ls->enabled = 1;
95     }
96   }
97   for(i=0; i<disable_logs_cnt; i++) {
98     ls = noit_log_stream_find(disable_logs[i]);
99     if(!ls) noitL(noit_error, "No such log: '%s'\n", enable_logs[i]);
100     if(ls && ls->enabled) {
101       noitL(noit_error, "Disabling %s\n", disable_logs[i]);
102       ls->enabled = 0;
103     }
104   }
105 }
106 void parse_clargs(int argc, char **argv) {
107   int c;
108   enable_logs = calloc(argc, sizeof(*enable_logs));
109   disable_logs = calloc(argc, sizeof(*disable_logs));
110   while((c = getopt(argc, argv, "Mrshc:dDu:g:t:l:L:")) != EOF) {
111     switch(c) {
112       case 'M':
113         strict_module_load = 1;
114         break;
115       case 'l':
116         enable_logs[enable_logs_cnt++] = strdup(optarg);
117         break;
118       case 'L':
119         disable_logs[disable_logs_cnt++] = strdup(optarg);
120         break;
121       case 'r':
122         stratcon_iep_set_enabled(0);
123         break;
124       case 's':
125         stratcon_datastore_set_enabled(0);
126         break;
127       case 'h':
128         usage(argv[0]);
129         exit(1);
130         break;
131       case 'u':
132         droptouser = strdup(optarg);
133         break;
134       case 'g':
135         droptogroup = strdup(optarg);
136         break;
137       case 't':
138         chrootpath = strdup(optarg);
139         break;
140       case 'c':
141         config_file = strdup(optarg);
142         break;
143       case 'd':
144         debug++;
145         break;
146       case 'D':
147         foreground = 1;
148         break;
149       default:
150         break;
151     }
152   }
153 }
154
155 static
156 int configure_eventer() {
157   int rv = 0;
158   noit_hash_table *table;
159   table = noit_conf_get_hash(NULL, "/" APPNAME "/eventer/config");
160   if(table) {
161     noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
162     const char *key, *value;
163     int klen;
164     while(noit_hash_next_str(table, &iter, &key, &klen, &value)) {
165       int subrv;
166       if((subrv = eventer_propset(key, value)) != 0)
167         rv = subrv;
168     }
169     noit_hash_destroy(table, free, free);
170     free(table);
171   }
172   return rv;
173 }
174
175 static int child_main() {
176   char lockfile[PATH_MAX];
177   char conf_str[1024];
178   char user[32], group[32];
179
180   /* First initialize logging, so we can log errors */
181   noit_log_init();
182   noit_log_stream_add_stream(noit_debug, noit_stderr);
183   noit_log_stream_add_stream(noit_error, noit_stderr);
184
185   /* Next load the configs */
186   noit_conf_init(APPNAME);
187   if(noit_conf_load(config_file) == -1) {
188     fprintf(stderr, "Cannot load config: '%s'\n", config_file);
189     exit(2);
190   }
191
192   /* Acquire the lock so that we can throw an error if it doesn't work.
193    * If we've started -D, we'll have the lock.
194    * If not we will daemon and must reacquire the lock.
195    */
196   lockfile[0] = '\0';
197   if(noit_conf_get_stringbuf(NULL, "/" APPNAME "/@lockfile",
198                              lockfile, sizeof(lockfile))) {
199     if(noit_lockfile_acquire(lockfile) < 0) {
200       noitL(noit_stderr, "Failed to acquire lock: %s\n", lockfile);
201       exit(-1);
202     }
203   }
204
205   /* Reinitialize the logging system now that we have a config */
206   snprintf(user, sizeof(user), "%d", getuid());
207   snprintf(group, sizeof(group), "%d", getgid());
208   if(noit_security_usergroup(droptouser, droptogroup, noit_true)) {
209     noitL(noit_stderr, "Failed to drop privileges, exiting.\n");
210     exit(2);
211   }
212   noit_conf_log_init(APPNAME);
213   cli_log_switches();
214   if(noit_security_usergroup(user, group, noit_true)) {
215     noitL(noit_stderr, "Failed to regain privileges, exiting.\n");
216     exit(2);
217   }
218   if(debug)
219     noit_debug->enabled = 1;
220
221   /* Lastly, run through all other system inits */
222   if(!noit_conf_get_stringbuf(NULL, "/" APPNAME "/eventer/@implementation",
223                               conf_str, sizeof(conf_str))) {
224     noitL(noit_stderr, "Cannot find '%s' in configuration\n",
225           "/" APPNAME "/eventer/@implementation");
226     exit(2);
227   }
228   if(eventer_choose(conf_str) == -1) {
229     noitL(noit_stderr, "Cannot choose eventer %s\n", conf_str);
230     exit(2);
231   }
232   if(configure_eventer() != 0) {
233     noitL(noit_stderr, "Cannot configure eventer\n");
234     exit(2);
235   }
236   if(eventer_init() == -1) {
237     noitL(noit_stderr, "Cannot init eventer %s\n", conf_str);
238     exit(2);
239   }
240
241   noit_watchdog_child_eventer_heartbeat();
242
243   noit_console_init(APPNAME);
244   noit_console_conf_init();
245   noit_http_rest_init();
246   stratcon_realtime_http_init(APPNAME);
247   noit_capabilities_listener_init();
248   noit_listener_init(APPNAME);
249
250   noit_module_init();
251   if(strict_module_load && noit_module_load_failures() > 0) {
252     noitL(noit_stderr, "Failed to load some modules and -M given.\n");
253     exit(2);
254   }
255
256   if(stratcon_datastore_get_enabled())
257     stratcon_datastore_init();
258
259   /* Drop privileges */
260   if(chrootpath && noit_security_chroot(chrootpath)) {
261     noitL(noit_stderr, "Failed to chroot(), exiting.\n");
262     exit(-1);
263   }
264   if(noit_security_usergroup(droptouser, droptogroup, noit_false)) {
265     noitL(noit_stderr, "Failed to drop privileges, exiting.\n");
266     exit(-1);
267   }
268
269   stratcon_jlog_streamer_init(APPNAME);
270
271   if(stratcon_iep_get_enabled())
272     stratcon_iep_init();
273   if(stratcon_datastore_get_enabled()) {
274     /* Write our log out, and setup a watchdog to write it out on change. */
275     stratcon_datastore_saveconfig(NULL);
276     noit_conf_coalesce_changes(10); /* 10 seconds of no changes before we write */
277   }
278   else
279     noit_conf_coalesce_changes(INT_MAX);
280
281   noit_conf_watch_and_journal_watchdog(stratcon_datastore_saveconfig, NULL);
282
283   eventer_loop();
284   return 0;
285 }
286
287 int main(int argc, char **argv) {
288   int fd;
289   parse_clargs(argc, argv);
290
291   if(chdir("/") != 0) {
292     fprintf(stderr, "cannot chdir(\"/\"): %s\n", strerror(errno));
293     exit(2);
294   }
295
296   noit_watchdog_prefork_init();
297
298   if(foreground) exit(child_main());
299
300   fd = open("/dev/null", O_RDWR);
301   dup2(fd, STDIN_FILENO);
302   dup2(fd, STDOUT_FILENO);
303   dup2(fd, STDERR_FILENO);
304   if(fork()) exit(0);
305   setsid();
306   if(fork()) exit(0);
307
308   return noit_watchdog_start_child("stratcond", child_main, 0);
309 }
Note: See TracBrowser for help on using the browser.