Refactor to move task code out of tr_main.c.
[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
43 #include <tr.h>
44 #include <tid_internal.h>
45 #include <tr_tid.h>
46 #include <tr_config.h>
47 #include <tr_event.h>
48 #include <tr_cfgwatch.h>
49 #include <tr_debug.h>
50
51 /***** command-line option handling / setup *****/
52
53 /* Strip trailing / from a path name.*/
54 static void remove_trailing_slash(char *s) {
55   size_t n;
56
57   n=strlen(s);
58   if(s[n-1]=='/') {
59     s[n-1]='\0';
60   }
61 }
62
63 /* argp global parameters */
64 const char *argp_program_bug_address=PACKAGE_BUGREPORT; /* bug reporting address */
65
66 /* doc strings */
67 static const char doc[]=PACKAGE_NAME " - Moonshot Trust Router";
68 static const char arg_doc[]=""; /* string describing arguments, if any */
69
70 /* define the options here. Fields are:
71  * { long-name, short-name, variable name, options, help description } */
72 static const struct argp_option cmdline_options[] = {
73     { "config-dir", 'c', "DIR", 0, "Specify configuration file location (default is current directory)"},
74     { NULL }
75 };
76
77 /* structure for communicating with option parser */
78 struct cmdline_args {
79   char *config_dir;
80 };
81
82 /* parser for individual options - fills in a struct cmdline_args */
83 static error_t parse_option(int key, char *arg, struct argp_state *state)
84 {
85   /* get a shorthand to the command line argument structure, part of state */
86   struct cmdline_args *arguments=state->input;
87
88   switch (key) {
89   case 'c':
90     if (arg == NULL) {
91       /* somehow we got called without an argument */
92       return ARGP_ERR_UNKNOWN;
93     }
94     arguments->config_dir=arg;
95     break;
96
97   default:
98     return ARGP_ERR_UNKNOWN;
99   }
100
101   return 0; /* success */
102 }
103
104 /* assemble the argp parser */
105 static struct argp argp = {cmdline_options, parse_option, arg_doc, doc};
106
107
108 int main (int argc, char *argv[])
109 {
110   TALLOC_CTX *main_ctx=talloc_new(NULL);
111
112   TR_INSTANCE *tr = NULL;
113   struct cmdline_args opts;
114   struct event_base *ev_base;
115   struct tr_socket_event tids_ev;
116   struct event *cfgwatch_ev;
117   TR_CFGWATCH *cfgwatch; /* config file watcher status */
118
119   /* Use standalone logging */
120   tr_log_open();
121
122   /***** parse command-line arguments *****/
123   /* set defaults */
124   opts.config_dir=".";
125
126   /* parse the command line*/
127   argp_parse(&argp, argc, argv, 0, 0, &opts);
128
129   /* process options */
130   remove_trailing_slash(opts.config_dir);
131
132   /* Get a configuration status object */
133   cfgwatch=tr_cfgwatch_create(main_ctx);
134   if (cfgwatch == NULL) {
135     tr_crit("Unable to create configuration watcher object, exiting.");
136     return 1;
137   }
138   
139   /***** create a Trust Router instance *****/
140   if (NULL == (tr = tr_create())) {
141     tr_crit("Unable to create Trust Router instance, exiting.");
142     return 1;
143   }
144
145   /***** process configuration *****/
146   cfgwatch->config_dir=opts.config_dir;
147   cfgwatch->ctx=main_ctx;
148   cfgwatch->tr=tr;
149   if (0 != tr_read_and_apply_config(cfgwatch)) {
150     tr_crit("Error reading configuration, exiting.");
151     return 1;
152   }
153
154   /***** initialize the trust path query server instance *****/
155   if (0 == (tr->tids = tids_create ())) {
156     tr_crit("Error initializing Trust Path Query Server instance.");
157     return 1;
158   }
159
160   /***** Set up the event loop *****/
161   ev_base=tr_event_loop_init(); /* Set up the event loop */
162   if (ev_base==NULL) {
163     tr_crit("Error initializing event loop.");
164     return 1;
165   }
166
167   /* install configuration file watching events */
168   cfgwatch->poll_interval=(struct timeval) {1,0}; /* set poll interval in {sec, usec} */
169   cfgwatch->settling_time=(struct timeval) {5,0}; /* delay for changes to settle before updating */
170   
171   /* already set config_dir, fstat_list and n_files earlier */
172   if (0 != tr_cfgwatch_event_init(ev_base, cfgwatch, &cfgwatch_ev)) {
173     tr_crit("Error initializing configuration file watcher.");
174     return 1;
175   }
176
177   /*tr_status_event_init();*/ /* install status reporting events */
178
179   /* install TID server events */
180   if (0 != tr_tids_event_init(ev_base, tr, &tids_ev)) {
181     tr_crit("Error initializing Trust Path Query Server instance.");
182     return 1;
183   }
184
185   /*tr_trp_event_init();*/ /* install TRP handler events */
186
187   fflush(stdout); fflush(stderr);
188   tr_event_loop_run(ev_base); /* does not return until we are done */
189
190   /* TODO: update the cleanup code */
191   tids_destroy(tr->tids);
192   tr_destroy(tr);
193
194   talloc_free(main_ctx);
195   return 0;
196 }