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