root/src/stratcond.c

Revision cb0464fd269dd989e20f86700d81579090bf8cc8, 4.1 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 6 years ago)

integrate command line docs as usage, refs #21

  • Property mode set to 100644
Line 
1 #include "noit_defines.h"
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <errno.h>
7 #include <sys/ioctl.h>
8 #include <fcntl.h>
9
10 #include "eventer/eventer.h"
11 #include "utils/noit_log.h"
12 #include "utils/noit_hash.h"
13 #include "utils/noit_security.h"
14 #include "noit_listener.h"
15 #include "noit_console.h"
16 #include "noit_module.h"
17 #include "noit_conf.h"
18 #include "stratcon_jlog_streamer.h"
19 #include "stratcon_datastore.h"
20
21 #define APPNAME "stratcon"
22 static char *config_file = ETC_DIR "/" APPNAME ".conf";
23 static const char *droptouser = NULL;
24 static const char *droptogroup = NULL;
25 static const char *chrootpath = NULL;
26 static int foreground = 0;
27 static int debug = 0;
28
29 #include "man/stratcond.usage.h"
30 static void usage(const char *progname) {
31   printf("Usage for %s:\n", progname);
32 #ifdef STRATCOND_USAGE
33   write(STDOUT_FILENO, STRATCOND_USAGE, sizeof(STRATCOND_USAGE)-1);
34 #else
35   printf("\nError in usage, build problem.\n");
36 #endif
37   return;
38 }
39
40 void parse_clargs(int argc, char **argv) {
41   int c;
42   while((c = getopt(argc, argv, "hc:dDu:g:t:")) != EOF) {
43     switch(c) {
44       case 'h':
45         usage(argv[0]);
46         exit(1);
47         break;
48       case 'u':
49         droptouser = strdup(optarg);
50         break;
51       case 'g':
52         droptogroup = strdup(optarg);
53         break;
54       case 't':
55         chrootpath = strdup(optarg);
56         break;
57       case 'c':
58         config_file = strdup(optarg);
59         break;
60       case 'd':
61         debug++;
62         break;
63       case 'D':
64         foreground = 1;
65         break;
66       default:
67         break;
68     }
69   }
70 }
71
72 static
73 int configure_eventer() {
74   int rv = 0;
75   noit_hash_table *table;
76   table = noit_conf_get_hash(NULL, "/" APPNAME "/eventer/config");
77   if(table) {
78     noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
79     const char *key, *value;
80     int klen;
81     while(noit_hash_next(table, &iter, &key, &klen, (void **)&value)) {
82       int subrv;
83       if((subrv = eventer_propset(key, value)) != 0)
84         rv = subrv;
85     }
86     noit_hash_destroy(table, free, free);
87     free(table);
88   }
89   return rv;
90 }
91
92 int main(int argc, char **argv) {
93   char conf_str[1024];
94
95   parse_clargs(argc, argv);
96
97   chdir("/");
98   if(!foreground) {
99     close(STDIN_FILENO);
100     close(STDOUT_FILENO);
101     close(STDERR_FILENO);
102     if(fork()) exit(0);
103     setsid();
104     if(fork()) exit(0);
105   }
106
107   /* First initialize logging, so we can log errors */
108   noit_log_init();
109   noit_log_stream_add_stream(noit_debug, noit_stderr);
110   noit_log_stream_add_stream(noit_error, noit_stderr);
111
112   /* Next load the configs */
113   noit_conf_init(APPNAME);
114   if(noit_conf_load(config_file) == -1) {
115     fprintf(stderr, "Cannot load config: '%s'\n", config_file);
116   }
117
118   /* Reinitialize the logging system now that we have a config */
119   noit_conf_log_init(APPNAME);
120   if(debug)
121     noit_debug->enabled = 1;
122
123   /* Lastly, run through all other system inits */
124   if(!noit_conf_get_stringbuf(NULL, "/" APPNAME "/eventer/@implementation",
125                               conf_str, sizeof(conf_str))) {
126     noitL(noit_stderr, "Cannot find '%s' in configuration\n",
127           "/" APPNAME "/eventer/@implementation");
128     exit(-1);
129   }
130   if(eventer_choose(conf_str) == -1) {
131     noitL(noit_stderr, "Cannot choose eventer %s\n", conf_str);
132     exit(-1);
133   }
134   if(configure_eventer() != 0) {
135     noitL(noit_stderr, "Cannot configure eventer\n");
136     exit(-1);
137   }
138   if(eventer_init() == -1) {
139     noitL(noit_stderr, "Cannot init eventer %s\n", conf_str);
140     exit(-1);
141   }
142   noit_console_init();
143   noit_listener_init(APPNAME);
144
145   /* Drop privileges */
146   if(chrootpath && noit_security_chroot(chrootpath)) {
147     noitL(noit_stderr, "Failed to chroot(), exiting.\n");
148     exit(-1);
149   }
150   if(noit_security_usergroup(droptouser, droptogroup)) {
151     noitL(noit_stderr, "Failed to drop privileges, exiting.\n");
152     exit(-1);
153   }
154
155   stratcon_jlog_streamer_init(APPNAME);
156
157   /* Write our log out, and setup a watchdog to write it out on change. */
158   stratcon_datastore_saveconfig(NULL);
159   noit_conf_coalesce_changes(10); /* 10 seconds of no changes before we write */
160   noit_conf_watch_and_journal_watchdog(stratcon_datastore_saveconfig, NULL);
161
162   eventer_loop();
163   return 0;
164 }
Note: See TracBrowser for help on using the browser.