root/src/stratcond.c

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

Update copyright as changes were made this year.

  • 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   int lockfd;
177   char lockfile[PATH_MAX];
178   char conf_str[1024];
179   char user[32], group[32];
180
181   /* First initialize logging, so we can log errors */
182   noit_log_init();
183   noit_log_stream_add_stream(noit_debug, noit_stderr);
184   noit_log_stream_add_stream(noit_error, noit_stderr);
185
186   /* Next load the configs */
187   noit_conf_init(APPNAME);
188   if(noit_conf_load(config_file) == -1) {
189     fprintf(stderr, "Cannot load config: '%s'\n", config_file);
190     exit(2);
191   }
192
193   /* Acquire the lock so that we can throw an error if it doesn't work.
194    * If we've started -D, we'll have the lock.
195    * If not we will daemon and must reacquire the lock.
196    */
197   lockfd = -1;
198   lockfile[0] = '\0';
199   if(noit_conf_get_stringbuf(NULL, "/" APPNAME "/@lockfile",
200                              lockfile, sizeof(lockfile))) {
201     if((lockfd = noit_lockfile_acquire(lockfile)) < 0) {
202       noitL(noit_stderr, "Failed to acquire lock: %s\n", lockfile);
203       exit(-1);
204     }
205   }
206
207   /* Reinitialize the logging system now that we have a config */
208   snprintf(user, sizeof(user), "%d", getuid());
209   snprintf(group, sizeof(group), "%d", getgid());
210   if(noit_security_usergroup(droptouser, droptogroup, noit_true)) {
211     noitL(noit_stderr, "Failed to drop privileges, exiting.\n");
212     exit(2);
213   }
214   noit_conf_log_init(APPNAME);
215   cli_log_switches();
216   if(noit_security_usergroup(user, group, noit_true)) {
217     noitL(noit_stderr, "Failed to regain privileges, exiting.\n");
218     exit(2);
219   }
220   if(debug)
221     noit_debug->enabled = 1;
222
223   /* Lastly, run through all other system inits */
224   if(!noit_conf_get_stringbuf(NULL, "/" APPNAME "/eventer/@implementation",
225                               conf_str, sizeof(conf_str))) {
226     noitL(noit_stderr, "Cannot find '%s' in configuration\n",
227           "/" APPNAME "/eventer/@implementation");
228     exit(2);
229   }
230   if(eventer_choose(conf_str) == -1) {
231     noitL(noit_stderr, "Cannot choose eventer %s\n", conf_str);
232     exit(2);
233   }
234   if(configure_eventer() != 0) {
235     noitL(noit_stderr, "Cannot configure eventer\n");
236     exit(2);
237   }
238   if(eventer_init() == -1) {
239     noitL(noit_stderr, "Cannot init eventer %s\n", conf_str);
240     exit(2);
241   }
242
243   noit_watchdog_child_eventer_heartbeat();
244
245   noit_console_init(APPNAME);
246   noit_console_conf_init();
247   noit_http_rest_init();
248   stratcon_realtime_http_init(APPNAME);
249   noit_capabilities_listener_init();
250   noit_listener_init(APPNAME);
251
252   noit_module_init();
253   if(strict_module_load && noit_module_load_failures() > 0) {
254     noitL(noit_stderr, "Failed to load some modules and -M given.\n");
255     exit(2);
256   }
257
258   if(stratcon_datastore_get_enabled())
259     stratcon_datastore_init();
260
261   /* Drop privileges */
262   if(chrootpath && noit_security_chroot(chrootpath)) {
263     noitL(noit_stderr, "Failed to chroot(), exiting.\n");
264     exit(-1);
265   }
266   if(noit_security_usergroup(droptouser, droptogroup, noit_false)) {
267     noitL(noit_stderr, "Failed to drop privileges, exiting.\n");
268     exit(-1);
269   }
270
271   stratcon_jlog_streamer_init(APPNAME);
272
273   if(stratcon_iep_get_enabled())
274     stratcon_iep_init();
275   if(stratcon_datastore_get_enabled()) {
276     /* Write our log out, and setup a watchdog to write it out on change. */
277     stratcon_datastore_saveconfig(NULL);
278     noit_conf_coalesce_changes(10); /* 10 seconds of no changes before we write */
279   }
280   else
281     noit_conf_coalesce_changes(INT_MAX);
282
283   noit_conf_watch_and_journal_watchdog(stratcon_datastore_saveconfig, NULL);
284
285   eventer_loop();
286   return 0;
287 }
288
289 int main(int argc, char **argv) {
290   int fd;
291   parse_clargs(argc, argv);
292
293   if(chdir("/") != 0) {
294     fprintf(stderr, "cannot chdir(\"/\"): %s\n", strerror(errno));
295     exit(2);
296   }
297
298   noit_watchdog_prefork_init();
299
300   if(foreground) exit(child_main());
301
302   fd = open("/dev/null", O_RDWR);
303   dup2(fd, STDIN_FILENO);
304   dup2(fd, STDOUT_FILENO);
305   dup2(fd, STDERR_FILENO);
306   if(fork()) exit(0);
307   setsid();
308   if(fork()) exit(0);
309
310   return noit_watchdog_start_child("stratcond", child_main, 0);
311 }
Note: See TracBrowser for help on using the browser.