root/src/stratcond.c

Revision 6453a67b4303e1da90f053764144109fef67c505, 4.2 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 5 years ago)

first step on interface into OpenESB IEP. No serf stuff yet, I'll let Umar take a whack at that. This has a double free issue that I'll troubleshoot on valgrind when I get more time. refs #119

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