root/src/stratcond.c

Revision c57081bfe551401db8874b81cce5b1f06b7dbdfd, 8.1 kB (checked in by Phil Maddox <philip.maddox@circonus.com>, 2 months ago)

Add Some mtev_hash_inits That Were Missing

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007-2010, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  * Copyright (c) 2015, Circonus, Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above
13  *       copyright notice, this list of conditions and the following
14  *       disclaimer in the documentation and/or other materials provided
15  *       with the distribution.
16  *     * Neither the name OmniTI Computer Consulting, Inc. nor the names
17  *       of its contributors may be used to endorse or promote products
18  *       derived from this software without specific prior written
19  *       permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <mtev_defines.h>
35 #include "noit_version.h"
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <limits.h>
43
44 #include <eventer/eventer.h>
45 #include <mtev_memory.h>
46 #include <mtev_log.h>
47 #include <mtev_hash.h>
48 #include <mtev_security.h>
49 #include <mtev_watchdog.h>
50 #include <mtev_lockfile.h>
51 #include <mtev_main.h>
52 #include <mtev_listener.h>
53 #include <mtev_console.h>
54 #include <mtev_conf.h>
55 #include <mtev_rest.h>
56 #include <mtev_reverse_socket.h>
57 #include <mtev_events_rest.h>
58 #include <mtev_capabilities_listener.h>
59
60 #include "noit_mtev_bridge.h"
61 #include "noit_config.h"
62 #include "noit_module.h"
63 #include "noit_check_tools.h"
64 #include "stratcon_jlog_streamer.h"
65 #include "stratcon_datastore.h"
66 #include "stratcon_iep.h"
67 #include "stratcon_realtime_http.h"
68
69 #define APPNAME "stratcon"
70 static char *config_file = ETC_DIR "/" APPNAME ".conf";
71 static const char *droptouser = NULL;
72 static const char *droptogroup = NULL;
73 static const char *chrootpath = NULL;
74 static int foreground = 0;
75 static int debug = 0;
76 static int strict_module_load = 0;
77 static char *glider = NULL;
78
79 #include "man/stratcond.usage.h"
80 static void usage(const char *progname) {
81   printf("Usage for %s:\n", progname);
82 #ifdef STRATCOND_USAGE
83   mtevAssert(write(STDOUT_FILENO,
84               STRATCOND_USAGE,
85               sizeof(STRATCOND_USAGE)-1) == sizeof(STRATCOND_USAGE)-1);
86 #else
87   printf("\nError in usage, build problem.\n");
88 #endif
89   return;
90 }
91
92 void parse_clargs(int argc, char **argv) {
93   int c;
94   while((c = getopt(argc, argv, "Mrshc:dDu:g:n:t:l:L:G:")) != EOF) {
95     switch(c) {
96       case 'M':
97         strict_module_load = 1;
98         break;
99       case 'G':
100         glider = strdup(optarg);
101         break;
102       case 'l':
103         mtev_main_enable_log(optarg);
104         break;
105       case 'L':
106         mtev_main_disable_log(optarg);
107         break;
108       case 'n':
109         {
110           char *cp = strchr(optarg, ':');
111           if(!cp) mtev_listener_skip(optarg, 0);
112           else {
113             if(cp == optarg) {
114               *cp++ = '\0';
115               mtev_listener_skip(NULL, atoi(cp));
116             }
117             else {
118               *cp++ = '\0';
119               mtev_listener_skip(optarg, atoi(cp));
120             }
121           }
122         }
123         break;
124       case 'r':
125         stratcon_iep_set_enabled(0);
126         break;
127       case 's':
128         stratcon_datastore_set_enabled(0);
129         break;
130       case 'h':
131         usage(argv[0]);
132         exit(1);
133         break;
134       case 'u':
135         droptouser = strdup(optarg);
136         break;
137       case 'g':
138         droptogroup = strdup(optarg);
139         break;
140       case 't':
141         chrootpath = strdup(optarg);
142         break;
143       case 'c':
144         config_file = strdup(optarg);
145         break;
146       case 'd':
147         debug++;
148         break;
149       case 'D':
150         foreground++;
151         break;
152       default:
153         break;
154     }
155   }
156 }
157
158 static
159 int configure_eventer() {
160   int rv = 0;
161   mtev_hash_table *table;
162   table = mtev_conf_get_hash(NULL, "/" APPNAME "/eventer/config");
163   if(table) {
164     mtev_hash_iter iter = MTEV_HASH_ITER_ZERO;
165     const char *key, *value;
166     int klen;
167     while(mtev_hash_next_str(table, &iter, &key, &klen, &value)) {
168       int subrv;
169       if((subrv = eventer_propset(key, value)) != 0)
170         rv = subrv;
171     }
172     mtev_hash_destroy(table, free, free);
173     free(table);
174   }
175   return rv;
176 }
177
178 const char *reverse_prefix = "noit/"; /* namespace out connections */
179 const char *reverse_prefix_cns[] = { "noit/", NULL };
180
181 static int child_main() {
182   char conf_str[1024];
183   char stratcon_version[80];
184
185   mtev_watchdog_child_heartbeat();
186   noit_mtev_bridge_init();
187
188   /* Next (re)load the configs */
189   if(mtev_conf_load(config_file) == -1) {
190     fprintf(stderr, "Cannot load config: '%s'\n", config_file);
191     exit(2);
192   }
193
194   mtev_log_reopen_all();
195   mtevL(noit_notice, "process starting: %d\n", (int)getpid());
196   mtev_log_go_asynch();
197
198   /* Lastly, run through all other system inits */
199   if(!mtev_conf_get_stringbuf(NULL, "/" APPNAME "/eventer/@implementation",
200                               conf_str, sizeof(conf_str))) {
201     mtevL(noit_stderr, "Cannot find '%s' in configuration\n",
202           "/" APPNAME "/eventer/@implementation");
203     exit(2);
204   }
205   if(eventer_choose(conf_str) == -1) {
206     mtevL(noit_stderr, "Cannot choose eventer %s\n", conf_str);
207     exit(2);
208   }
209   if(configure_eventer() != 0) {
210     mtevL(noit_stderr, "Cannot configure eventer\n");
211     exit(2);
212   }
213   if(eventer_init() == -1) {
214     mtevL(noit_stderr, "Cannot init eventer %s\n", conf_str);
215     exit(2);
216   }
217   /* rotation init requires, eventer_init() */
218   mtev_conf_log_init_rotate(APPNAME, mtev_false);
219
220   mtev_watchdog_child_eventer_heartbeat();
221
222   mtev_console_init(APPNAME);
223   mtev_console_conf_init();
224   mtev_http_rest_init();
225   mtev_reverse_socket_init(reverse_prefix, reverse_prefix_cns);
226   mtev_reverse_socket_acl(mtev_reverse_socket_denier);
227   mtev_events_rest_init();
228   stratcon_realtime_http_init(APPNAME);
229   mtev_capabilities_listener_init();
230   noit_build_version(stratcon_version, sizeof(stratcon_version));
231   mtev_capabilities_add_feature("stratcon", stratcon_version);
232   mtev_listener_init(APPNAME);
233
234   mtev_dso_init();
235   mtev_dso_post_init();
236   if(strict_module_load && mtev_dso_load_failures() > 0) {
237     mtevL(noit_stderr, "Failed to load some modules and -M given.\n");
238     exit(2);
239   }
240
241   if(stratcon_datastore_get_enabled())
242     stratcon_datastore_init();
243
244   /* Drop privileges */
245   mtev_conf_security_init(APPNAME, droptouser, droptogroup, chrootpath);
246
247   stratcon_jlog_streamer_init(APPNAME);
248
249   if(stratcon_iep_get_enabled())
250     stratcon_iep_init();
251   if(stratcon_datastore_get_enabled()) {
252     /* Write our log out, and setup a watchdog to write it out on change. */
253     stratcon_datastore_saveconfig(NULL);
254     mtev_conf_coalesce_changes(10); /* 10 seconds of no changes before we write */
255   }
256   else
257     mtev_conf_coalesce_changes(INT_MAX);
258
259   mtev_conf_watch_and_journal_watchdog(stratcon_datastore_saveconfig, NULL);
260
261   eventer_loop();
262   return 0;
263 }
264
265 void
266 stratcond_init_globals(void) {
267   noit_check_tools_shared_init_globals();
268   stratcon_datastore_init_globals();
269   stratcon_iep_init_globals();
270   stratcon_jlog_streamer_init_globals();
271 }
272
273 int main(int argc, char **argv) {
274   mtev_memory_init();
275   parse_clargs(argc, argv);
276   stratcond_init_globals();
277   return mtev_main(APPNAME, config_file, debug, foreground,
278                    MTEV_LOCK_OP_LOCK, glider, droptouser, droptogroup,
279                    child_main);
280 }
Note: See TracBrowser for help on using the browser.