| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 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 |
|
| 45 |
static int main_cmdline(int argc, char *argv[]); |
| 46 |
static void * catch_signals(void * arg); |
| 47 |
static pthread_t signals_thr; |
| 48 |
|
| 49 |
static char *conffile = NULL; |
| 50 |
static int gnutls_debug = 0; |
| 51 |
|
| 52 |
|
| 53 |
static void fd_gnutls_debug(int level, const char * str) { |
| 54 |
fd_log_debug(" [gnutls:%d] %s", level, str); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
int main(int argc, char * argv[]) |
| 60 |
{ |
| 61 |
int ret; |
| 62 |
sigset_t sig_all; |
| 63 |
|
| 64 |
|
| 65 |
sigfillset(&sig_all); |
| 66 |
ret = pthread_sigmask(SIG_BLOCK, &sig_all, NULL); |
| 67 |
ASSERT(ret == 0); |
| 68 |
|
| 69 |
|
| 70 |
ret = main_cmdline(argc, argv); |
| 71 |
if (ret != 0) { |
| 72 |
return ret; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 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 |
|
| 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 |
|
| 90 |
CHECK_FCT_DO( fd_core_parseconf(conffile), goto error ); |
| 91 |
|
| 92 |
|
| 93 |
CHECK_FCT_DO( fd_core_start(), goto error ); |
| 94 |
|
| 95 |
|
| 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 |
|
| 101 |
CHECK_FCT( fd_core_wait_shutdown_complete() ); |
| 102 |
|
| 103 |
|
| 104 |
fd_thr_term(&signals_thr); |
| 105 |
|
| 106 |
return 0; |
| 107 |
error: |
| 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 |
|
| 116 |
static void main_version_core(void) |
| 117 |
{ |
| 118 |
printf("%s, version %s\n", FD_PROJECT_NAME, fd_core_version); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
static 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 |
|
| 131 |
static 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 |
|
| 153 |
static 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 |
|
| 173 |
while (1) { |
| 174 |
c = getopt_long (argc, argv, "hVc:dql:f:F:g:", long_options, &option_index); |
| 175 |
if (c == -1) |
| 176 |
break; |
| 177 |
|
| 178 |
switch (c) { |
| 179 |
case 'h': |
| 180 |
main_help(); |
| 181 |
exit(0); |
| 182 |
|
| 183 |
case 'V': |
| 184 |
main_version(); |
| 185 |
exit(0); |
| 186 |
|
| 187 |
case 'c': |
| 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': |
| 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': |
| 204 |
fd_g_debug_lvl--; |
| 205 |
break; |
| 206 |
|
| 207 |
case 'f': |
| 208 |
#ifdef DEBUG |
| 209 |
fd_debug_one_function = optarg; |
| 210 |
fd_g_debug_lvl = FD_LOG_DEBUG; |
| 211 |
#else |
| 212 |
fprintf(stderr, "Error: must compile with DEBUG support to use --dbg_func feature!\n"); |
| 213 |
return EINVAL; |
| 214 |
#endif |
| 215 |
break; |
| 216 |
|
| 217 |
case 'F': |
| 218 |
#ifdef DEBUG |
| 219 |
fd_debug_one_file = basename(optarg); |
| 220 |
fd_g_debug_lvl = FD_LOG_DEBUG; |
| 221 |
#else |
| 222 |
fprintf(stderr, "Error: must compile with DEBUG support to use --dbg_file feature!\n"); |
| 223 |
return EINVAL; |
| 224 |
#endif |
| 225 |
break; |
| 226 |
|
| 227 |
case 'g': |
| 228 |
gnutls_debug = (int)atoi(optarg); |
| 229 |
break; |
| 230 |
|
| 231 |
case 'q': |
| 232 |
fd_g_debug_lvl++; |
| 233 |
break; |
| 234 |
|
| 235 |
case '?': |
| 236 |
|
| 237 |
fprintf(stderr, "getopt_long found an invalid character\n"); |
| 238 |
return EINVAL; |
| 239 |
|
| 240 |
default: |
| 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 |
|
| 251 |
static void * catch_signals(void * arg) |
| 252 |
{ |
| 253 |
sigset_t ss; |
| 254 |
fd_log_threadname ( "signals catcher" ); |
| 255 |
|
| 256 |
sigemptyset(&ss); |
| 257 |
|
| 258 |
|
| 259 |
sigaddset(&ss, SIGTERM); |
| 260 |
sigaddset(&ss, SIGINT); |
| 261 |
|
| 262 |
|
| 263 |
sigaddset(&ss, SIGUSR1); |
| 264 |
sigaddset(&ss, SIGUSR2); |
| 265 |
|
| 266 |
|
| 267 |
CHECK_SYS_DO( pthread_sigmask( SIG_SETMASK, &ss, NULL ), goto out ); |
| 268 |
|
| 269 |
|
| 270 |
while (1) { |
| 271 |
int sig, *ps; |
| 272 |
|
| 273 |
|
| 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 |
} |
| 292 |
out: |
| 293 |
|
| 294 |
ASSERT(0); |
| 295 |
return NULL; |
| 296 |
} |