root/src/stratcond.c

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

closes #53

  • 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 "noit_listener.h"
14 #include "noit_console.h"
15 #include "noit_module.h"
16 #include "noit_conf.h"
17 #include "stratcon_jlog_streamer.h"
18 #include "stratcon_datastore.h"
19
20 #define APPNAME "stratcon"
21 static char *config_file = ETC_DIR "/" APPNAME ".conf";
22 static int debug = 0;
23 static int foreground = 0;
24
25 void parse_clargs(int argc, char **argv) {
26   int c;
27   while((c = getopt(argc, argv, "c:dD")) != EOF) {
28     switch(c) {
29       case 'c':
30         config_file = strdup(optarg);
31         break;
32       case 'd':
33         debug++;
34         break;
35       case 'D':
36         foreground = 1;
37         break;
38       default:
39         break;
40     }
41   }
42 }
43
44 static
45 int configure_eventer() {
46   int rv = 0;
47   noit_hash_table *table;
48   table = noit_conf_get_hash(NULL, "/" APPNAME "/eventer/config");
49   if(table) {
50     noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
51     const char *key, *value;
52     int klen;
53     while(noit_hash_next(table, &iter, &key, &klen, (void **)&value)) {
54       int subrv;
55       if((subrv = eventer_propset(key, value)) != 0)
56         rv = subrv;
57     }
58     noit_hash_destroy(table, free, free);
59     free(table);
60   }
61   return rv;
62 }
63
64 int main(int argc, char **argv) {
65   char conf_str[1024];
66
67   parse_clargs(argc, argv);
68
69   chdir("/");
70   if(!foreground) {
71     close(STDIN_FILENO);
72     close(STDOUT_FILENO);
73     close(STDERR_FILENO);
74     if(fork()) exit(0);
75     setsid();
76     if(fork()) exit(0);
77   }
78
79   /* First initialize logging, so we can log errors */
80   noit_log_init();
81   noit_log_stream_add_stream(noit_debug, noit_stderr);
82   noit_log_stream_add_stream(noit_error, noit_stderr);
83
84   /* Next load the configs */
85   noit_conf_init(APPNAME);
86   if(noit_conf_load(config_file) == -1) {
87     fprintf(stderr, "Cannot load config: '%s'\n", config_file);
88   }
89
90   /* Reinitialize the logging system now that we have a config */
91   noit_conf_log_init(APPNAME);
92   if(debug)
93     noit_debug->enabled = 1;
94
95   /* Lastly, run through all other system inits */
96   if(!noit_conf_get_stringbuf(NULL, "/" APPNAME "/eventer/@implementation",
97                               conf_str, sizeof(conf_str))) {
98     noitL(noit_stderr, "Cannot find '%s' in configuration\n",
99           "/" APPNAME "/eventer/@implementation");
100     exit(-1);
101   }
102   if(eventer_choose(conf_str) == -1) {
103     noitL(noit_stderr, "Cannot choose eventer %s\n", conf_str);
104     exit(-1);
105   }
106   if(configure_eventer() != 0) {
107     noitL(noit_stderr, "Cannot configure eventer\n");
108     exit(-1);
109   }
110   if(eventer_init() == -1) {
111     noitL(noit_stderr, "Cannot init eventer %s\n", conf_str);
112     exit(-1);
113   }
114   noit_console_init();
115   noit_listener_init(APPNAME);
116   stratcon_jlog_streamer_init(APPNAME);
117
118   /* Write our log out, and setup a watchdog to write it out on change. */
119   stratcon_datastore_saveconfig(NULL);
120   noit_conf_coalesce_changes(10); /* 10 seconds of no changes before we write */
121   noit_conf_watch_and_journal_watchdog(stratcon_datastore_saveconfig, NULL);
122
123   eventer_loop();
124   return 0;
125 }
Note: See TracBrowser for help on using the browser.