Periodically call tids_sweep_procs() during trust router operation
[trust_router.git] / tr / tr_main.c
1 /*
2  * Copyright (c) 2012, 2015, JANET(UK)
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
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written 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
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31  * OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <argp.h>
38 #include <event2/event.h>
39 #include <talloc.h>
40 #include <signal.h>
41 #include <time.h>
42
43 #include <tid_internal.h>
44 #include <mon_internal.h>
45 #include <tr_mon.h>
46 #include <tr_tid.h>
47 #include <tr_trp.h>
48 #include <tr_config.h>
49 #include <tr_event.h>
50 #include <tr_cfgwatch.h>
51 #include <tr.h>
52 #include <tr_debug.h>
53
54 #define TALLOC_DEBUG_ENABLE 1
55
56 /***** command-line option handling / setup *****/
57
58 static void print_version_info(void)
59 {
60   printf("Moonshot Trust Router %s\n\n", PACKAGE_VERSION);
61 }
62
63 /* Strip trailing / from a path name.*/
64 static void remove_trailing_slash(char *s) {
65   size_t n;
66
67   n=strlen(s);
68   if(s[n-1]=='/') {
69     s[n-1]='\0';
70   }
71 }
72
73 /* argp global parameters */
74 const char *argp_program_bug_address=PACKAGE_BUGREPORT; /* bug reporting address */
75
76 /* doc strings */
77 static const char doc[]=PACKAGE_NAME " - Moonshot Trust Router " PACKAGE_VERSION;
78 static const char arg_doc[]=""; /* string describing arguments, if any */
79
80 /* define the options here. Fields are:
81  * { long-name, short-name, variable name, options, help description } */
82 static const struct argp_option cmdline_options[] = {
83     { "config-dir", 'c', "DIR", 0, "Specify configuration file location (default is current directory)"},
84     { "version", 'v', NULL, 0, "Print version information and exit"},
85     { NULL }
86 };
87
88 /* structure for communicating with option parser */
89 struct cmdline_args {
90     int version_requested;
91     char *config_dir;
92 };
93
94 /* parser for individual options - fills in a struct cmdline_args */
95 static error_t parse_option(int key, char *arg, struct argp_state *state)
96 {
97   /* get a shorthand to the command line argument structure, part of state */
98   struct cmdline_args *arguments=state->input;
99
100   switch (key) {
101     case 'c':
102       if (arg == NULL) {
103         /* somehow we got called without an argument */
104         return ARGP_ERR_UNKNOWN;
105       }
106       arguments->config_dir=arg;
107       break;
108
109     case 'v':
110       arguments->version_requested=1;
111       break;
112
113     default:
114       return ARGP_ERR_UNKNOWN;
115   }
116
117   return 0; /* success */
118 }
119
120 /* assemble the argp parser */
121 static struct argp argp = {cmdline_options, parse_option, arg_doc, doc};
122
123
124 /***** talloc error handling *****/
125 /* called when talloc tries to abort */
126 static void tr_abort(const char *reason)
127 {
128   tr_crit("tr_abort: Critical error, talloc aborted. Reason: %s", reason);
129   abort();
130 }
131
132 #if TALLOC_DEBUG_ENABLE
133 static void tr_talloc_log(const char *msg)
134 {
135   tr_debug("talloc: %s", msg);
136 }
137 #endif /* TALLOC_DEBUG_ENABLE */
138
139 static void configure_signals(void)
140 {
141   sigset_t signals;
142   /* ignore SIGPIPE */
143   sigemptyset(&signals);
144   sigaddset(&signals, SIGPIPE);
145   pthread_sigmask(SIG_BLOCK, &signals, NULL);
146 }
147
148 /* TODO move this function */
149 static MON_RC tr_mon_handle_version(void *cookie, json_t **result_ptr)
150 {
151   *result_ptr = json_string(PACKAGE_VERSION);
152   return (*result_ptr == NULL) ? MON_NOMEM : MON_SUCCESS;
153 }
154
155 static MON_RC tr_mon_handle_uptime(void *cookie, json_t **result_ptr)
156 {
157   time_t *start_time = cookie;
158   *result_ptr = json_integer(time(NULL) - (*start_time));
159   return (*result_ptr == NULL) ? MON_NOMEM : MON_SUCCESS;
160 }
161
162 int main(int argc, char *argv[])
163 {
164   TALLOC_CTX *main_ctx=NULL;
165
166   TR_INSTANCE *tr = NULL;
167   struct cmdline_args opts;
168   struct event_base *ev_base;
169   struct tr_socket_event tids_ev = {0};
170   struct event *tids_sweep_ev;
171   struct tr_socket_event mon_ev = {0};
172   struct event *cfgwatch_ev;
173
174   time_t start_time = time(NULL); /* TODO move this? */
175
176   configure_signals();
177
178   /* we're going to be multithreaded, so disable null context tracking */
179   talloc_set_abort_fn(tr_abort);
180   talloc_disable_null_tracking();
181 #if TALLOC_DEBUG_ENABLE
182   talloc_set_log_fn(tr_talloc_log);
183 #endif /* TALLOC_DEBUG_ENABLE */
184   main_ctx=talloc_new(NULL);
185
186   /* Use standalone logging */
187   tr_log_open();
188
189   /***** parse command-line arguments *****/
190   /* set defaults */
191   opts.version_requested=0;
192   opts.config_dir=".";
193
194   /* parse the command line*/
195   argp_parse(&argp, argc, argv, 0, 0, &opts);
196
197   /* process options */
198   remove_trailing_slash(opts.config_dir);
199
200
201   /***** Print version info *****/
202   print_version_info();
203   if (opts.version_requested)
204     return 0; /* requested that we print version and exit */
205
206   /***** create a Trust Router instance *****/
207   if (NULL == (tr = tr_create(main_ctx))) {
208     tr_crit("Unable to create Trust Router instance, exiting.");
209     return 1;
210   }
211
212   /***** initialize the trust path query server instance *****/
213   if (NULL == (tr->tids = tids_new(tr))) {
214     tr_crit("Error initializing Trust Path Query Server instance.");
215     return 1;
216   }
217
218   /***** initialize the trust router protocol server instance *****/
219   if (NULL == (tr->trps = trps_new(tr))) {
220     tr_crit("Error initializing Trust Router Protocol Server instance.");
221     return 1;
222   }
223
224   /***** initialize the monitoring interface instance *****/
225   if (NULL == (tr->mons = mons_new(tr))) {
226     tr_crit("Error initializing monitoring interface instance.");
227     return 1;
228   }
229   /* Monitor our tids/trps instances */
230   tr->mons->tids = tr->tids;
231   tr->mons->trps = tr->trps;
232
233   /* TODO do this more systematically */
234   mons_register_handler(tr->mons, MON_CMD_SHOW, OPT_TYPE_SHOW_VERSION, tr_mon_handle_version, NULL);
235   mons_register_handler(tr->mons, MON_CMD_SHOW, OPT_TYPE_SHOW_UPTIME, tr_mon_handle_uptime, &start_time);
236   tr_tid_register_mons_handlers(tr->tids, tr->mons);
237   
238   /***** process configuration *****/
239   tr->cfgwatch=tr_cfgwatch_create(tr);
240   if (tr->cfgwatch == NULL) {
241     tr_crit("Unable to create configuration watcher object, exiting.");
242     return 1;
243   }
244   tr->cfgwatch->config_dir=opts.config_dir;
245   tr->cfgwatch->cfg_mgr=tr->cfg_mgr;
246   tr->cfgwatch->update_cb=tr_config_changed; /* handle configuration changes */
247   tr->cfgwatch->update_cookie=(void *)tr;
248   if (0 != tr_read_and_apply_config(tr->cfgwatch)) {
249     tr_crit("Error reading configuration, exiting.");
250     return 1;
251   }
252
253   /***** Set up the event loop *****/
254   ev_base=tr_event_loop_init(); /* Set up the event loop */
255   if (ev_base==NULL) {
256     tr_crit("Error initializing event loop.");
257     return 1;
258   }
259
260   /* already set config_dir, fstat_list and n_files earlier */
261   if (0 != tr_cfgwatch_event_init(ev_base, tr->cfgwatch, &cfgwatch_ev)) {
262     tr_crit("Error initializing configuration file watcher.");
263     return 1;
264   }
265
266   /* install monitoring interface events */
267   tr_debug("Initializing monitoring interface events.");
268   if (0 != tr_mons_event_init(ev_base, tr->mons, tr->cfg_mgr, &mon_ev)) {
269     tr_crit("Error initializing monitoring interface.");
270     return 1;
271   }
272
273   /* install TID server events */
274   tr_debug("Initializing TID server events.");
275   if (0 != tr_tids_event_init(ev_base, tr->tids, tr->cfg_mgr, tr->trps, &tids_ev, &tids_sweep_ev)) {
276     tr_crit("Error initializing Trust Path Query Server instance.");
277     return 1;
278   }
279
280   /* install TRP handler events */
281   tr_debug("Initializing Dynamic Trust Router Protocol events.");
282   if (TRP_SUCCESS != tr_trps_event_init(ev_base, tr)) {
283     tr_crit("Error initializing Trust Path Query Server instance.");
284     return 1;
285   }
286
287   tr_debug("Starting event loop.");
288   tr_event_loop_run(ev_base); /* does not return until we are done */
289
290   tr_destroy(tr); /* thanks to talloc, should destroy everything */
291
292   talloc_free(main_ctx);
293   return 0;
294 }