Restore interface compatibility with libtr_tid.so.2
[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 <jansson.h>
38 #include <argp.h>
39 #include <event2/event.h>
40 #include <talloc.h>
41 #include <sys/time.h>
42 #include <signal.h>
43 #include <pthread.h>
44
45 #include <tid_internal.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 /* Strip trailing / from a path name.*/
59 static void remove_trailing_slash(char *s) {
60   size_t n;
61
62   n=strlen(s);
63   if(s[n-1]=='/') {
64     s[n-1]='\0';
65   }
66 }
67
68 /* argp global parameters */
69 const char *argp_program_bug_address=PACKAGE_BUGREPORT; /* bug reporting address */
70
71 /* doc strings */
72 static const char doc[]=PACKAGE_NAME " - Moonshot Trust Router";
73 static const char arg_doc[]=""; /* string describing arguments, if any */
74
75 /* define the options here. Fields are:
76  * { long-name, short-name, variable name, options, help description } */
77 static const struct argp_option cmdline_options[] = {
78     { "config-dir", 'c', "DIR", 0, "Specify configuration file location (default is current directory)"},
79     { NULL }
80 };
81
82 /* structure for communicating with option parser */
83 struct cmdline_args {
84   char *config_dir;
85 };
86
87 /* parser for individual options - fills in a struct cmdline_args */
88 static error_t parse_option(int key, char *arg, struct argp_state *state)
89 {
90   /* get a shorthand to the command line argument structure, part of state */
91   struct cmdline_args *arguments=state->input;
92
93   switch (key) {
94   case 'c':
95     if (arg == NULL) {
96       /* somehow we got called without an argument */
97       return ARGP_ERR_UNKNOWN;
98     }
99     arguments->config_dir=arg;
100     break;
101
102   default:
103     return ARGP_ERR_UNKNOWN;
104   }
105
106   return 0; /* success */
107 }
108
109 /* assemble the argp parser */
110 static struct argp argp = {cmdline_options, parse_option, arg_doc, doc};
111
112
113 /***** talloc error handling *****/
114 /* called when talloc tries to abort */
115 static void tr_abort(const char *reason)
116 {
117   tr_crit("tr_abort: Critical error, talloc aborted. Reason: %s", reason);
118   abort();
119 }
120
121 #if TALLOC_DEBUG_ENABLE
122 static void tr_talloc_log(const char *msg)
123 {
124   tr_debug("talloc: %s", msg);
125 }
126 #endif /* TALLOC_DEBUG_ENABLE */
127
128 static void configure_signals(void)
129 {
130   sigset_t signals;
131   /* ignore SIGPIPE */
132   sigemptyset(&signals);
133   sigaddset(&signals, SIGPIPE);
134   pthread_sigmask(SIG_BLOCK, &signals, NULL);
135 }
136
137 int main(int argc, char *argv[])
138 {
139   TALLOC_CTX *main_ctx=NULL;
140
141   TR_INSTANCE *tr = NULL;
142   struct cmdline_args opts;
143   struct event_base *ev_base;
144   struct tr_socket_event tids_ev = {0};
145   struct event *cfgwatch_ev;
146
147   configure_signals();
148
149   /* we're going to be multithreaded, so disable null context tracking */
150   talloc_set_abort_fn(tr_abort);
151   talloc_disable_null_tracking();
152 #if TALLOC_DEBUG_ENABLE
153   talloc_set_log_fn(tr_talloc_log);
154 #endif /* TALLOC_DEBUG_ENABLE */
155   main_ctx=talloc_new(NULL);
156
157   /* Use standalone logging */
158   tr_log_open();
159
160   /***** parse command-line arguments *****/
161   /* set defaults */
162   opts.config_dir=".";
163
164   /* parse the command line*/
165   argp_parse(&argp, argc, argv, 0, 0, &opts);
166
167   /* process options */
168   remove_trailing_slash(opts.config_dir);
169
170   /***** create a Trust Router instance *****/
171   if (NULL == (tr = tr_create(main_ctx))) {
172     tr_crit("Unable to create Trust Router instance, exiting.");
173     return 1;
174   }
175
176   /***** initialize the trust path query server instance *****/
177   if (NULL == (tr->tids = tids_create())) {
178     tr_crit("Error initializing Trust Path Query Server instance.");
179     return 1;
180   }
181   talloc_steal(tr, tr->tids);
182
183   /***** initialize the trust router protocol server instance *****/
184   if (NULL == (tr->trps = trps_new(tr))) {
185     tr_crit("Error initializing Trust Router Protocol Server instance.");
186     return 1;
187   }
188
189   /***** process configuration *****/
190   tr->cfgwatch=tr_cfgwatch_create(tr);
191   if (tr->cfgwatch == NULL) {
192     tr_crit("Unable to create configuration watcher object, exiting.");
193     return 1;
194   }
195   tr->cfgwatch->config_dir=opts.config_dir;
196   tr->cfgwatch->cfg_mgr=tr->cfg_mgr;
197   tr->cfgwatch->update_cb=tr_config_changed; /* handle configuration changes */
198   tr->cfgwatch->update_cookie=(void *)tr;
199   if (0 != tr_read_and_apply_config(tr->cfgwatch)) {
200     tr_crit("Error reading configuration, exiting.");
201     return 1;
202   }
203
204   /***** Set up the event loop *****/
205   ev_base=tr_event_loop_init(); /* Set up the event loop */
206   if (ev_base==NULL) {
207     tr_crit("Error initializing event loop.");
208     return 1;
209   }
210
211   /* already set config_dir, fstat_list and n_files earlier */
212   if (0 != tr_cfgwatch_event_init(ev_base, tr->cfgwatch, &cfgwatch_ev)) {
213     tr_crit("Error initializing configuration file watcher.");
214     return 1;
215   }
216
217   /*tr_status_event_init();*/ /* install status reporting events */
218
219   /* install TID server events */
220   tr_debug("Initializing TID server events.");
221   if (0 != tr_tids_event_init(ev_base,
222                               tr->tids,
223                               tr->cfg_mgr,
224                               tr->trps,
225                              &tids_ev)) {
226     tr_crit("Error initializing Trust Path Query Server instance.");
227     return 1;
228   }
229
230   /* install TRP handler events */
231   tr_debug("Initializing Dynamic Trust Router Protocol events.");
232   if (TRP_SUCCESS != tr_trps_event_init(ev_base, tr)) {
233     tr_crit("Error initializing Trust Path Query Server instance.");
234     return 1;
235   }
236
237   tr_debug("Starting event loop.");
238   tr_event_loop_run(ev_base); /* does not return until we are done */
239
240   tr_destroy(tr); /* thanks to talloc, should destroy everything */
241
242   talloc_free(main_ctx);
243   return 0;
244 }