blob: c4ecbb49548f958fb341dacfe14ace541f6a00c2 [file] [log] [blame]
Brian Waters13d96012017-12-08 16:53:31 -06001/*********************************************************************************************************
2* Software License Agreement (BSD License) *
3* Author: Sebastien Decugis <sdecugis@freediameter.net> *
4* *
5* Copyright (c) 2015, WIDE Project and NICT *
6* All rights reserved. *
7* *
8* Redistribution and use of this software in source and binary forms, with or without modification, are *
9* permitted provided that the following conditions are met: *
10* *
11* * Redistributions of source code must retain the above *
12* copyright notice, this list of conditions and the *
13* following disclaimer. *
14* *
15* * Redistributions in binary form must reproduce the above *
16* copyright notice, this list of conditions and the *
17* following disclaimer in the documentation and/or other *
18* materials provided with the distribution. *
19* *
20* * Neither the name of the WIDE Project or NICT nor the *
21* names of its contributors may be used to endorse or *
22* promote products derived from this software without *
23* specific prior written permission of WIDE Project and *
24* NICT. *
25* *
26* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
27* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
28* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
29* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
30* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *
31* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
32* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF *
33* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
34*********************************************************************************************************/
35
36#include <freeDiameter/freeDiameter-host.h>
37#include <freeDiameter/libfdcore.h>
38
39#include <signal.h>
40#include <getopt.h>
41#include <locale.h>
42
43
44/* forward declarations */
45static int main_cmdline(int argc, char *argv[]);
46static void * catch_signals(void * arg);
47static pthread_t signals_thr;
48
49static char *conffile = NULL;
50static int gnutls_debug = 0;
51
52/* gnutls debug */
53static void fd_gnutls_debug(int level, const char * str) {
54 fd_log_debug(" [gnutls:%d] %s", level, str);
55}
56
57
58/* freeDiameter starting point */
59int main(int argc, char * argv[])
60{
61 int ret;
62 sigset_t sig_all;
63
64 /* Block all signals from the current thread and all its future children -- we will catch everything in catch_signals */
65 sigfillset(&sig_all);
66 ret = pthread_sigmask(SIG_BLOCK, &sig_all, NULL);
67 ASSERT(ret == 0);
68
69 /* Parse the command-line */
70 ret = main_cmdline(argc, argv);
71 if (ret != 0) {
72 return ret;
73 }
74
75 /* Initialize the core library */
76 ret = fd_core_initialize();
77 if (ret != 0) {
78 fprintf(stderr, "An error occurred during freeDiameter core library initialization.\n");
79 return ret;
80 }
81
82 /* Set gnutls debug level ? */
83 if (gnutls_debug) {
84 gnutls_global_set_log_function((gnutls_log_func)fd_gnutls_debug);
85 gnutls_global_set_log_level (gnutls_debug);
86 TRACE_DEBUG(INFO, "Enabled GNUTLS debug at level %d", gnutls_debug);
87 }
88
89 /* Parse the configuration file */
90 CHECK_FCT_DO( fd_core_parseconf(conffile), goto error );
91
92 /* Start the servers */
93 CHECK_FCT_DO( fd_core_start(), goto error );
94
95 /* Allow SIGINT and SIGTERM from this point to terminate the application */
96 CHECK_POSIX_DO( pthread_create(&signals_thr, NULL, catch_signals, NULL), goto error );
97
98 TRACE_DEBUG(INFO, FD_PROJECT_BINARY " daemon initialized.");
99
100 /* Now, just wait for termination */
101 CHECK_FCT( fd_core_wait_shutdown_complete() );
102
103 /* Just in case it was not the result of a signal, we cancel signals_thr */
104 fd_thr_term(&signals_thr);
105
106 return 0;
107error:
108 CHECK_FCT_DO( fd_core_shutdown(), );
109 CHECK_FCT( fd_core_wait_shutdown_complete() );
110 fd_thr_term(&signals_thr);
111 return -1;
112}
113
114
115/* Display package version */
116static void main_version_core(void)
117{
118 printf("%s, version %s\n", FD_PROJECT_NAME, fd_core_version);
119}
120
121/* Display package version and general info */
122static void main_version(void)
123{
124 main_version_core();
125 printf( "%s\n", FD_PROJECT_COPYRIGHT);
126 printf( "\nSee " FD_PROJECT_NAME " homepage at http://www.freediameter.net/\n"
127 " for information, updates and bug reports on this software.\n");
128}
129
130/* Print command-line options */
131static void main_help( void )
132{
133 main_version_core();
134 printf( " This daemon is an implementation of the Diameter protocol\n"
135 " used for Authentication, Authorization, and Accounting (AAA).\n");
136 printf("\nUsage: " FD_PROJECT_BINARY " [OPTIONS]...\n");
137 printf( " -h, --help Print help and exit\n"
138 " -V, --version Print version and exit\n"
139 " -c, --config=filename Read configuration from this file instead of the \n"
140 " default location (" DEFAULT_CONF_PATH "/" FD_DEFAULT_CONF_FILENAME ").\n");
141 printf( "\nDebug:\n"
142 " These options are mostly useful for developers\n"
143 " -l, --dbglocale Set the locale for error messages\n"
144 " -d, --debug Increase verbosity of debug messages if default logger is used\n"
145 " -q, --quiet Decrease verbosity if default logger is used\n"
146 " -f, --dbg_func <func> Enable all traces within the function <func>\n"
147 " -F, --dbg_file <file.c> Enable all traces within the file <file.c> (basename match)\n"
148 " --dbg_gnutls <int> Enable GNU TLS debug at level <int>\n"
149 );
150}
151
152/* Parse the command-line */
153static int main_cmdline(int argc, char *argv[])
154{
155 int c;
156 int option_index = 0;
157 char * locale;
158
159 struct option long_options[] = {
160 { "help", no_argument, NULL, 'h' },
161 { "version", no_argument, NULL, 'V' },
162 { "config", required_argument, NULL, 'c' },
163 { "debug", no_argument, NULL, 'd' },
164 { "quiet", no_argument, NULL, 'q' },
165 { "dbglocale", optional_argument, NULL, 'l' },
166 { "dbg_func", required_argument, NULL, 'f' },
167 { "dbg_file", required_argument, NULL, 'F' },
168 { "dbg_gnutls", required_argument, NULL, 'g' },
169 { NULL, 0, NULL, 0 }
170 };
171
172 /* Loop on arguments */
173 while (1) {
174 c = getopt_long (argc, argv, "hVc:dql:f:F:g:", long_options, &option_index);
175 if (c == -1)
176 break; /* Exit from the loop. */
177
178 switch (c) {
179 case 'h': /* Print help and exit. */
180 main_help();
181 exit(0);
182
183 case 'V': /* Print version and exit. */
184 main_version();
185 exit(0);
186
187 case 'c': /* Read configuration from this file instead of the default location.. */
188 if (optarg == NULL ) {
189 fprintf(stderr, "Missing argument with --config directive\n");
190 return EINVAL;
191 }
192 conffile = optarg;
193 break;
194
195 case 'l': /* Change the locale. */
196 locale = setlocale(LC_ALL, optarg?:"");
197 if (!locale) {
198 fprintf(stderr, "Unable to set locale (%s)\n", optarg);
199 return EINVAL;
200 }
201 break;
202
203 case 'd': /* Increase verbosity of debug messages. */
204 fd_g_debug_lvl--;
205 break;
206
207 case 'f': /* Full debug for the function with this name. */
208 #ifdef DEBUG
209 fd_debug_one_function = optarg;
210 fd_g_debug_lvl = FD_LOG_DEBUG;
211 #else /* DEBUG */
212 fprintf(stderr, "Error: must compile with DEBUG support to use --dbg_func feature!\n");
213 return EINVAL;
214 #endif /* DEBUG */
215 break;
216
217 case 'F': /* Full debug for the file with this name. */
218 #ifdef DEBUG
219 fd_debug_one_file = basename(optarg);
220 fd_g_debug_lvl = FD_LOG_DEBUG;
221 #else /* DEBUG */
222 fprintf(stderr, "Error: must compile with DEBUG support to use --dbg_file feature!\n");
223 return EINVAL;
224 #endif /* DEBUG */
225 break;
226
227 case 'g': /* Set a debug level and function for GNU TLS calls. */
228 gnutls_debug = (int)atoi(optarg);
229 break;
230
231 case 'q': /* Decrease verbosity then remove debug messages. */
232 fd_g_debug_lvl++;
233 break;
234
235 case '?': /* Invalid option. */
236 /* `getopt_long' already printed an error message. */
237 fprintf(stderr, "getopt_long found an invalid character\n");
238 return EINVAL;
239
240 default: /* bug: option not considered. */
241 fprintf(stderr, "A command-line option is missing in parser: %c\n", c);
242 ASSERT(0);
243 return EINVAL;
244 }
245 }
246
247 return 0;
248}
249
250/* Handle some signals */
251static void * catch_signals(void * arg)
252{
253 sigset_t ss;
254 fd_log_threadname ( "signals catcher" );
255
256 sigemptyset(&ss);
257
258 /* Signals that terminate the daemon */
259 sigaddset(&ss, SIGTERM);
260 sigaddset(&ss, SIGINT);
261
262 /* Signals that send an event */
263 sigaddset(&ss, SIGUSR1);
264 sigaddset(&ss, SIGUSR2);
265
266 /* We unblock all other signals, so that their default handler is used (such as SIGTSTP) */
267 CHECK_SYS_DO( pthread_sigmask( SIG_SETMASK, &ss, NULL ), goto out );
268
269 /* Now loop on the reception of the signal */
270 while (1) {
271 int sig, *ps;
272
273 /* Wait to receive the next signal */
274 CHECK_POSIX_DO( sigwait(&ss, &sig), break );
275
276 TRACE_DEBUG(FULL, "Signal %d caught", sig);
277
278 switch (sig) {
279 case SIGUSR1:
280 case SIGUSR2:
281 CHECK_MALLOC_DO( ps = malloc(sizeof(int)), goto out);
282 *ps = sig;
283 CHECK_FCT_DO( fd_event_send(fd_g_config->cnf_main_ev, FDEV_TRIGGER, sizeof(int), ps), goto out );
284 break;
285
286 case SIGINT:
287 case SIGTERM:
288 CHECK_FCT_DO( fd_core_shutdown(), goto out );
289
290 }
291 }
292out:
293 /* Better way to handle this ? */
294 ASSERT(0);
295 return NULL;
296}