root/src/stratcond.c

Revision 8ad126b7390d75e24ecc859efb01570b01bbcdc1, 6.3 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 5 years ago)

This is pretty intrusive.

Here we refactor the console initalization in noit_conf_checks to have
the reusable parts pulled into the noit_conf directly so that stratcon
can make use of them. noit_conf_checks is retooled to extend that and
override the 'ls' command to have it's old form and function.

stratcon_jlog_streamer extends the noit_conf console syntax to allow
adding and removing of noits within the 'configure terminal' mode.

# conf t
# noit 10.1.2.3
# noit 10.1.5.5:12345
# no noit 10.1.2.3
# no noit 10.1.5.5:12345

This should not change the operation of noitd's console in any way;
however, the changes were quite substantial, so we should keep an eye
out for collateral breakage.

refs #195

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007-2009, 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
42 #include "eventer/eventer.h"
43 #include "utils/noit_log.h"
44 #include "utils/noit_hash.h"
45 #include "utils/noit_security.h"
46 #include "utils/noit_watchdog.h"
47 #include "noit_listener.h"
48 #include "noit_console.h"
49 #include "noit_module.h"
50 #include "noit_conf.h"
51 #include "noit_rest.h"
52 #include "noit_capabilities_listener.h"
53 #include "stratcon_jlog_streamer.h"
54 #include "stratcon_datastore.h"
55 #include "stratcon_iep.h"
56 #include "stratcon_realtime_http.h"
57
58 #define APPNAME "stratcon"
59 static char *config_file = ETC_DIR "/" APPNAME ".conf";
60 static const char *droptouser = NULL;
61 static const char *droptogroup = NULL;
62 static const char *chrootpath = NULL;
63 static int foreground = 0;
64 static int debug = 0;
65
66 #include "man/stratcond.usage.h"
67 static void usage(const char *progname) {
68   printf("Usage for %s:\n", progname);
69 #ifdef STRATCOND_USAGE
70   assert(write(STDOUT_FILENO,
71               STRATCOND_USAGE,
72               sizeof(STRATCOND_USAGE)-1) == sizeof(STRATCOND_USAGE)-1);
73 #else
74   printf("\nError in usage, build problem.\n");
75 #endif
76   return;
77 }
78
79 void parse_clargs(int argc, char **argv) {
80   int c;
81   while((c = getopt(argc, argv, "hc:dDu:g:t:")) != EOF) {
82     switch(c) {
83       case 'h':
84         usage(argv[0]);
85         exit(1);
86         break;
87       case 'u':
88         droptouser = strdup(optarg);
89         break;
90       case 'g':
91         droptogroup = strdup(optarg);
92         break;
93       case 't':
94         chrootpath = strdup(optarg);
95         break;
96       case 'c':
97         config_file = strdup(optarg);
98         break;
99       case 'd':
100         debug++;
101         break;
102       case 'D':
103         foreground = 1;
104         break;
105       default:
106         break;
107     }
108   }
109 }
110
111 static
112 int configure_eventer() {
113   int rv = 0;
114   noit_hash_table *table;
115   table = noit_conf_get_hash(NULL, "/" APPNAME "/eventer/config");
116   if(table) {
117     noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
118     const char *key, *value;
119     int klen;
120     while(noit_hash_next_str(table, &iter, &key, &klen, &value)) {
121       int subrv;
122       if((subrv = eventer_propset(key, value)) != 0)
123         rv = subrv;
124     }
125     noit_hash_destroy(table, free, free);
126     free(table);
127   }
128   return rv;
129 }
130
131 static int child_main() {
132   char conf_str[1024];
133
134   /* First initialize logging, so we can log errors */
135   noit_log_init();
136   noit_log_stream_add_stream(noit_debug, noit_stderr);
137   noit_log_stream_add_stream(noit_error, noit_stderr);
138
139   /* Next load the configs */
140   noit_conf_init(APPNAME);
141   if(noit_conf_load(config_file) == -1) {
142     fprintf(stderr, "Cannot load config: '%s'\n", config_file);
143     exit(2);
144   }
145
146   /* Reinitialize the logging system now that we have a config */
147   noit_conf_log_init(APPNAME);
148   if(debug)
149     noit_debug->enabled = 1;
150
151   /* Lastly, run through all other system inits */
152   if(!noit_conf_get_stringbuf(NULL, "/" APPNAME "/eventer/@implementation",
153                               conf_str, sizeof(conf_str))) {
154     noitL(noit_stderr, "Cannot find '%s' in configuration\n",
155           "/" APPNAME "/eventer/@implementation");
156     exit(-1);
157   }
158   if(eventer_choose(conf_str) == -1) {
159     noitL(noit_stderr, "Cannot choose eventer %s\n", conf_str);
160     exit(-1);
161   }
162   if(configure_eventer() != 0) {
163     noitL(noit_stderr, "Cannot configure eventer\n");
164     exit(-1);
165   }
166   if(eventer_init() == -1) {
167     noitL(noit_stderr, "Cannot init eventer %s\n", conf_str);
168     exit(-1);
169   }
170
171   noit_watchdog_child_eventer_heartbeat();
172
173   noit_console_init(APPNAME);
174   noit_console_conf_init();
175   noit_http_rest_init();
176   stratcon_realtime_http_init(APPNAME);
177   noit_capabilities_listener_init();
178   noit_listener_init(APPNAME);
179
180   /* Drop privileges */
181   if(chrootpath && noit_security_chroot(chrootpath)) {
182     noitL(noit_stderr, "Failed to chroot(), exiting.\n");
183     exit(-1);
184   }
185   if(noit_security_usergroup(droptouser, droptogroup)) {
186     noitL(noit_stderr, "Failed to drop privileges, exiting.\n");
187     exit(-1);
188   }
189
190   stratcon_iep_init();
191   stratcon_jlog_streamer_init(APPNAME);
192
193   /* Write our log out, and setup a watchdog to write it out on change. */
194   stratcon_datastore_saveconfig(NULL);
195   noit_conf_coalesce_changes(10); /* 10 seconds of no changes before we write */
196   noit_conf_watch_and_journal_watchdog(stratcon_datastore_saveconfig, NULL);
197
198   eventer_loop();
199   return 0;
200 }
201
202 int main(int argc, char **argv) {
203   parse_clargs(argc, argv);
204
205   if(chdir("/") != 0) {
206     fprintf(stderr, "cannot chdir(\"/\"): %s\n", strerror(errno));
207     exit(2);
208   }
209
210   noit_watchdog_prefork_init();
211
212   if(foreground) exit(child_main());
213
214   close(STDIN_FILENO);
215   close(STDOUT_FILENO);
216   close(STDERR_FILENO);
217   if(fork()) exit(0);
218   setsid();
219   if(fork()) exit(0);
220
221   return noit_watchdog_start_child("stratcond", child_main, 0);
222 }
Note: See TracBrowser for help on using the browser.