Pass the top-level configuration to the thread pool, so that
[freeradius.git] / src / main / radiusd.c
1 /*
2  * radiusd.c    Main loop of the radius server.
3  *
4  * Version:     $Id$
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * Copyright 2000-2004,2006  The FreeRADIUS server project
21  * Copyright 1999,2000  Miquel van Smoorenburg <miquels@cistron.nl>
22  * Copyright 2000  Alan DeKok <aland@ox.org>
23  * Copyright 2000  Alan Curry <pacman-radius@cqc.com>
24  * Copyright 2000  Jeff Carneal <jeff@apex.net>
25  * Copyright 2000  Chad Miller <cmiller@surfsouth.com>
26  */
27
28 #include <freeradius-devel/ident.h>
29 RCSID("$Id$")
30
31 #include <freeradius-devel/radiusd.h>
32 #include <freeradius-devel/radius_snmp.h>
33 #include <freeradius-devel/rad_assert.h>
34
35 #include <sys/file.h>
36
37 #include <fcntl.h>
38 #include <ctype.h>
39
40 #include <signal.h>
41
42 #ifdef HAVE_GETOPT_H
43 #       include <getopt.h>
44 #endif
45
46 #ifdef HAVE_SYS_WAIT_H
47 #       include <sys/wait.h>
48 #endif
49 #ifndef WEXITSTATUS
50 #       define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
51 #endif
52 #ifndef WIFEXITED
53 #       define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
54 #endif
55
56 /*
57  *  Global variables.
58  */
59 const char *progname = NULL;
60 const char *radius_dir = NULL;
61 const char *radacct_dir = NULL;
62 const char *radlog_dir = NULL;
63 const char *radlib_dir = NULL;
64 int log_stripped_names;
65 int debug_flag = 0;
66 int log_auth_detail = FALSE;
67
68
69 const char *radiusd_version = "FreeRADIUS Version " RADIUSD_VERSION ", for host " HOSTINFO ", built on " __DATE__ " at " __TIME__;
70
71 time_t time_now;
72 static pid_t radius_pid;
73 static int debug_memory = 0;
74 static int has_detail_listener = FALSE;
75
76 static int radius_self_pipe[2];
77
78 /*
79  *  Configuration items.
80  */
81
82 /*
83  *      Static functions.
84  */
85 static void usage(int);
86
87 static void sig_fatal (int);
88 static void sig_hup (int);
89
90 /*
91  *      The main guy.
92  */
93 int main(int argc, char *argv[])
94 {
95         REQUEST *request;
96         unsigned char buffer[4096];
97         fd_set readfds;
98         int argval;
99         int pid;
100         int max_fd;
101         int status;
102         int dont_fork = FALSE;
103         int spawn_flag = TRUE;
104         int sig_hup_block = FALSE;
105         struct timeval tv, *ptv = NULL;
106         int read_from_detail = FALSE;
107         int fd_flags;
108
109 #ifdef HAVE_SIGACTION
110         struct sigaction act;
111 #endif
112         rad_listen_t *listener;
113
114 #ifdef OSFC2
115         set_auth_parameters(argc,argv);
116 #endif
117
118         if ((progname = strrchr(argv[0], '/')) == NULL)
119                 progname = argv[0];
120         else
121                 progname++;
122
123         debug_flag = 0;
124         spawn_flag = TRUE;
125         radius_dir = strdup(RADIUS_DIR);
126
127         /*
128          *      Ensure that the configuration is initialized.
129          */
130         memset(&mainconfig, 0, sizeof(mainconfig));
131         mainconfig.myip.af = AF_UNSPEC;
132         mainconfig.port = -1;
133         mainconfig.radiusd_conf = strdup("radiusd.conf");
134
135 #ifdef HAVE_SIGACTION
136         memset(&act, 0, sizeof(act));
137         act.sa_flags = 0 ;
138         sigemptyset( &act.sa_mask ) ;
139 #endif
140
141         /*
142          *      Don't put output anywhere until we get told a little
143          *      more.
144          */
145         mainconfig.radlog_fd = -1;
146         mainconfig.log_file = NULL;
147
148         /*  Process the options.  */
149         while ((argval = getopt(argc, argv, "Aa:bcd:fg:hi:l:mn:p:sSvxXyz")) != EOF) {
150
151                 switch(argval) {
152
153                         case 'A':
154                                 log_auth_detail = TRUE;
155                                 break;
156
157                         case 'a':
158                                 if (radacct_dir) free(radacct_dir);
159                                 radacct_dir = strdup(optarg);
160                                 break;
161
162                         case 'c':
163                                 /* ignore for backwards compatibility with Cistron */
164                                 break;
165
166                         case 'd':
167                                 if (radius_dir) free(radius_dir);
168                                 radius_dir = strdup(optarg);
169                                 break;
170
171                         case 'f':
172                                 dont_fork = TRUE;
173                                 break;
174
175                         case 'h':
176                                 usage(0);
177                                 break;
178
179                         case 'i':
180                                 if (ip_hton(optarg, AF_UNSPEC, &mainconfig.myip) < 0) {
181                                         fprintf(stderr, "radiusd: Invalid IP Address or hostname \"%s\"\n", optarg);
182                                         exit(1);
183                                 }
184                                 break;
185
186                         case 'l':
187                                 if ((strcmp(optarg, "stdout") == 0) ||
188                                     (strcmp(optarg, "stderr") == 0) ||
189                                     (strcmp(optarg, "syslog") == 0)) {
190                                         fprintf(stderr, "radiusd: -l %s is unsupported.  Use log_destination in radiusd.conf\n", optarg);
191                                         exit(1);
192                                 }
193                                 if (radlog_dir) free(radlog_dir);
194                                 radlog_dir = strdup(optarg);
195                                 break;
196
197                         case 'g':
198                                 fprintf(stderr, "radiusd: -g is unsupported.  Use log_destination in radiusd.conf.\n");
199                                 exit(1);
200                                 break;
201
202                         case 'm':
203                                 debug_memory = 1;
204                                 break;
205
206                         case 'n':
207                                 if ((strchr(optarg, '/') != NULL) ||
208                                     (strchr(optarg, '.') != NULL) ||
209                                     (strlen(optarg) > 45)) usage(1);
210
211                                 snprintf(buffer, sizeof(buffer), "%s.conf",
212                                          optarg);
213                                 if (mainconfig.radiusd_conf)
214                                         free(mainconfig.radiusd_conf);
215                                 mainconfig.radiusd_conf = strdup(buffer);
216                                 break;
217
218                         case 'S':
219                                 log_stripped_names++;
220                                 break;
221
222                         case 'p':
223                                 mainconfig.port = atoi(optarg);
224                                 if ((mainconfig.port <= 0) ||
225                                     (mainconfig.port >= 65536)) {
226                                         fprintf(stderr, "radiusd: Invalid port number %s\n", optarg);
227                                         exit(1);
228                                 }
229                                 break;
230
231                         case 's':       /* Single process mode */
232                                 spawn_flag = FALSE;
233                                 dont_fork = TRUE;
234                                 break;
235
236                         case 'v':
237                                 version();
238                                 break;
239
240                                 /*
241                                  *  BIG debugging mode for users who are
242                                  *  TOO LAZY to type '-sfxxyz -l stdout' themselves.
243                                  */
244                         case 'X':
245                                 spawn_flag = FALSE;
246                                 dont_fork = TRUE;
247                                 debug_flag += 2;
248                                 mainconfig.log_auth = TRUE;
249                                 mainconfig.log_auth_badpass = TRUE;
250                                 mainconfig.log_auth_goodpass = TRUE;
251                                 mainconfig.radlog_dest = RADLOG_STDOUT;
252                                 mainconfig.radlog_fd = STDOUT_FILENO;
253                                 break;
254
255                         case 'x':
256                                 debug_flag++;
257                                 break;
258
259                         case 'y':
260                                 mainconfig.log_auth = TRUE;
261                                 mainconfig.log_auth_badpass = TRUE;
262                                 break;
263
264                         case 'z':
265                                 mainconfig.log_auth_badpass = TRUE;
266                                 mainconfig.log_auth_goodpass = TRUE;
267                                 break;
268
269                         default:
270                                 usage(1);
271                                 break;
272                 }
273         }
274
275         if (debug_flag) {
276                 radlog(L_INFO, "%s", radiusd_version);
277                 radlog(L_INFO, "Copyright (C) 2000-2007 The FreeRADIUS server project.\n");
278                 radlog(L_INFO, "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n");
279                 radlog(L_INFO, "PARTICULAR PURPOSE.\n");
280                 radlog(L_INFO, "You may redistribute copies of FreeRADIUS under the terms of the\n");
281                 radlog(L_INFO, "GNU General Public License.\n");
282                 fflush(NULL);
283         }
284
285         /*  Read the configuration files, BEFORE doing anything else.  */
286         if (read_mainconfig(0) < 0) {
287                 exit(1);
288         }
289
290         /*
291          *  Disconnect from session
292          */
293         if (debug_flag == 0 && dont_fork == FALSE) {
294                 pid = fork();
295                 if(pid < 0) {
296                         radlog(L_ERR|L_CONS, "Couldn't fork");
297                         exit(1);
298                 }
299
300                 /*
301                  *  The parent exits, so the child can run in the background.
302                  */
303                 if (pid > 0) {
304                         exit(0);
305                 }
306 #ifdef HAVE_SETSID
307                 setsid();
308 #endif
309         }
310
311         /*
312          *      If we're NOT debugging, trap fatal signals, so we can
313          *      easily clean up after ourselves.
314          *
315          *      If we ARE debugging, don't trap them, so we can
316          *      dump core.
317          */
318         if ((mainconfig.allow_core_dumps == FALSE) && (debug_flag == 0)) {
319 #ifdef SIGSEGV
320 #ifdef HAVE_SIGACTION
321                 act.sa_handler = sig_fatal;
322                 sigaction(SIGSEGV, &act, NULL);
323 #else
324                 signal(SIGSEGV, sig_fatal);
325 #endif
326 #endif
327         }
328
329         /*
330          *  Ensure that we're using the CORRECT pid after forking,
331          *  NOT the one we started with.
332          */
333         radius_pid = getpid();
334
335         /*
336          *  Only write the PID file if we're running as a daemon.
337          *
338          *  And write it AFTER we've forked, so that we write the
339          *  correct PID.
340          */
341         if (dont_fork == FALSE) {
342                 FILE *fp;
343
344                 fp = fopen(mainconfig.pid_file, "w");
345                 if (fp != NULL) {
346                         /*
347                          *      FIXME: What about following symlinks,
348                          *      and having it over-write a normal file?
349                          */
350                         fprintf(fp, "%d\n", (int) radius_pid);
351                         fclose(fp);
352                 } else {
353                         radlog(L_ERR|L_CONS, "Failed creating PID file %s: %s\n",
354                                mainconfig.pid_file, strerror(errno));
355                         exit(1);
356                 }
357         }
358
359         /*
360          *      Child threads need a pipe to signal us, as do the
361          *      signal handlers.
362          */
363         if (pipe(radius_self_pipe) < 0) {
364                 radlog(L_ERR, "radiusd: Error opening internal pipe: %s",
365                        strerror(errno));
366                 exit(1);
367         }
368         if (fcntl(radius_self_pipe[0], F_SETFL, O_NONBLOCK | FD_CLOEXEC) < 0) {
369                 radlog(L_ERR, "radiusd: Error setting internal flags: %s",
370                        strerror(errno));
371                 exit(1);
372         }
373         if (fcntl(radius_self_pipe[1], F_SETFL, O_NONBLOCK | FD_CLOEXEC) < 0) {
374                 radlog(L_ERR, "radiusd: Error setting internal flags: %s",
375                        strerror(errno));
376                 exit(1);
377         }
378
379         /*
380          *      If we're running as a daemon, close the default file
381          *      descriptors, AFTER forking.
382          */
383         if (!debug_flag) {
384                 int devnull;
385
386                 devnull = open("/dev/null", O_RDWR);
387                 if (devnull < 0) {
388                         radlog(L_ERR|L_CONS, "Failed opening /dev/null: %s\n",
389                                strerror(errno));
390                         exit(1);
391                 }
392                 dup2(devnull, STDIN_FILENO);
393                 if (mainconfig.radlog_dest == RADLOG_STDOUT) {
394                         mainconfig.radlog_fd = dup(STDOUT_FILENO);
395                 }
396                 dup2(devnull, STDOUT_FILENO);
397                 if (mainconfig.radlog_dest == RADLOG_STDERR) {
398                         mainconfig.radlog_fd = dup(STDERR_FILENO);
399                 }
400                 dup2(devnull, STDERR_FILENO);
401                 close(devnull);
402         }
403
404         /*
405          *      It's called the thread pool, but it does a little
406          *      more than that.
407          */
408         radius_event_init(mainconfig.config, spawn_flag);
409
410         /*
411          *  Use linebuffered or unbuffered stdout if
412          *  the debug flag is on.
413          */
414         if (debug_flag == TRUE)
415                 setlinebuf(stdout);
416
417         /*
418          *      Print out which ports we're listening on.
419          */
420         for (listener = mainconfig.listen;
421              listener != NULL;
422              listener = listener->next) {
423                 listener->print(listener, buffer, sizeof(buffer));
424                 switch (listener->type) {
425                 case RAD_LISTEN_DETAIL:
426                         has_detail_listener = TRUE;
427                         read_from_detail = TRUE;
428                         break;
429
430                 case RAD_LISTEN_SNMP:
431                         DEBUG("Listening on SNMP %s", buffer);
432                         break;
433
434                 default:
435                         break;
436                 }
437
438                 DEBUG("Listening on %s", buffer);
439         }
440
441         /*
442          *      Now that we've set everything up, we can install the signal
443          *      handlers.  Before this, if we get any signal, we don't know
444          *      what to do, so we might as well do the default, and die.
445          */
446         signal(SIGPIPE, SIG_IGN);
447 #ifdef HAVE_SIGACTION
448         act.sa_handler = sig_hup;
449         sigaction(SIGHUP, &act, NULL);
450         act.sa_handler = sig_fatal;
451         sigaction(SIGTERM, &act, NULL);
452 #else
453         signal(SIGHUP, sig_hup);
454         signal(SIGTERM, sig_fatal);
455 #endif
456         /*
457          *      If we're debugging, then a CTRL-C will cause the
458          *      server to die immediately.  Use SIGTERM to shut down
459          *      the server cleanly in that case.
460          */
461         if ((debug_memory == 1) || (debug_flag == 0)) {
462 #ifdef HAVE_SIGACTION
463                 act.sa_handler = sig_fatal;
464                 sigaction(SIGINT, &act, NULL);
465                 sigaction(SIGQUIT, &act, NULL);
466 #else
467                 signal(SIGINT, sig_fatal);
468                 signal(SIGQUIT, sig_fatal);
469 #endif
470         }
471
472  restart:
473         radlog(L_INFO, "Ready to process requests.");
474
475         /*
476          *  Receive user requests
477          */
478         for (;;) {
479                 ssize_t rcode;
480
481                 FD_ZERO(&readfds);
482                 max_fd = 0;
483
484                 /*
485                  *      Loop over all the listening FD's.
486                  */
487                 for (listener = mainconfig.listen;
488                      listener != NULL;
489                      listener = listener->next) {
490                         if (listener->fd < 0) continue;
491
492                         if (!read_from_detail &&
493                             (listener->type == RAD_LISTEN_DETAIL)) continue;
494
495                         FD_SET(listener->fd, &readfds);
496                         if (listener->fd > max_fd) max_fd = listener->fd;
497                 }
498
499                 /*
500                  *      Add in the self pipe FD, for signals or
501                  *      notices from the child threads.
502                  */
503                 FD_SET(radius_self_pipe[0], &readfds);
504                 if (radius_self_pipe[0] > max_fd) {
505                         max_fd = radius_self_pipe[0];
506                 }
507
508                 if (!ptv) {
509                         DEBUG2("Nothing to do.  Sleeping until we see a request.");
510                 } else if (tv.tv_sec) {
511                                 DEBUG2("Waking up in %d seconds...",
512                                        (int) tv.tv_sec);
513                 }
514
515                 status = select(max_fd + 1, &readfds, NULL, NULL, ptv);
516                 if ((status < 0) && (errno != EINTR)) {
517                         radlog(L_ERR, "Unexpected error in select(): %s",
518                                strerror(errno));
519                                 exit(1);
520                 }
521                 time_now = time(NULL);
522
523 #ifndef HAVE_PTHREAD_H
524                 /*
525                  *      If there are no child threads, then there may
526                  *      be child processes.  In that case, wait for
527                  *      their exit status, and throw that exit status
528                  *      away.  This helps get rid of zxombie children.
529                  */
530                 while (waitpid(-1, &argval, WNOHANG) > 0) {
531                         /* do nothing */
532                 }
533 #endif
534
535                 /*
536                  *      Before doing anything else, check the self pipe.
537                  */
538                 if (FD_ISSET(radius_self_pipe[0], &readfds) &&
539                     (rcode = read(radius_self_pipe[0], buffer, sizeof(buffer))) > 0) {
540                         ssize_t i;
541
542                         for (i = 1; i < rcode; i++) {
543                                 buffer[0] |= buffer[i];
544                         }
545
546                         if ((buffer[0] & (RADIUS_SIGNAL_SELF_EXIT | RADIUS_SIGNAL_SELF_TERM)) != 0) {
547                                 DEBUG("Exiting...");
548
549                                 /*
550                                  *      Ignore the TERM signal: we're
551                                  *      about to die.
552                                  */
553                                 signal(SIGTERM, SIG_IGN);
554
555                                 /*
556                                  *      Send a TERM signal to all
557                                  *      associated processes
558                                  *      (including us, which gets
559                                  *      ignored.)
560                                  */
561                                 kill(-radius_pid, SIGTERM);
562
563                                 /*
564                                  *      FIXME: Kill child threads, and
565                                  *      clean up?
566                                  */
567
568                                 /*
569                                  *      FIXME: clean up any active REQUEST
570                                  *      handles.
571                                  */
572
573                                 /*
574                                  *      We're exiting, so we can delete the PID
575                                  *      file.  (If it doesn't exist, we can ignore
576                                  *      the error returned by unlink)
577                                  */
578                                 if (dont_fork == FALSE) {
579                                         unlink(mainconfig.pid_file);
580                                 }
581
582                                 radius_event_free();
583
584                                 /*
585                                  *      Free the configuration items.
586                                  */
587                                 free_mainconfig();
588
589                                 /*
590                                  *      Detach any modules.
591                                  */
592                                 detach_modules();
593
594                                 free(radius_dir);
595
596                                 /*
597                                  *      SIGTERM gets do_exit=0,
598                                  *      and we want to exit cleanly.
599                                  *
600                                  *      Other signals make us exit
601                                  *      with an error status.
602                                  */
603                                 if ((buffer[0] & RADIUS_SIGNAL_SELF_EXIT) != 0) {
604                                         exit(1);
605                                 }
606                                 exit(0);
607                         } /* else exit/term flags weren't set */
608
609                         if ((buffer[0] & RADIUS_SIGNAL_SELF_HUP) != 0) {
610                                 DEBUG("Received HUP signal");
611 #ifdef HAVE_PTHREAD_H
612                                 /*
613                                  *      Threads: wait for all threads to stop
614                                  *      processing before re-loading the
615                                  *      config, so we don't pull the rug out
616                                  *      from under them.
617                                  */
618                                 int max_wait = 0;
619                                 if (spawn_flag) for(;;) {
620                                         /*
621                                          *      Block until there are
622                                          *      zero threads with a
623                                          *      REQUEST handle.
624                                          *
625                                          *      FIXME: Wait for
626                                          *      threads to send us a
627                                          *      signal in the pipe?
628                                          */
629                                         sig_hup_block = TRUE;
630
631                                         if( (total_active_threads() == 0) ||
632                                             (max_wait >= 5) ) {
633                                                 sig_hup_block = FALSE;
634                                                 break;
635                                         }
636                                         sleep(1);
637                                         max_wait++;
638                                 }
639 #endif
640
641 #ifdef WITH_SNMP
642                                 if (mainconfig.do_snmp) {
643                                         rad_snmp.smux_failures = 0;
644                                         rad_snmp.smux_event = SMUX_CONNECT;
645                                 }
646 #endif
647                                 if (read_mainconfig(TRUE) < 0) {
648                                         exit(1);
649                                 }
650
651                                 goto restart;
652                         }
653
654                         /*
655                          *      Check if we can read from the detail file
656                          */
657                         if (has_detail_listener &&
658                             (buffer[0] & RADIUS_SIGNAL_SELF_DETAIL) != 0) {
659                                 DEBUG3("Can now read from detail file(s)");
660                                 read_from_detail = TRUE;
661                         }
662                 } /* else the self pipe didn't have data ready */
663
664                 /*
665                  *      Loop over the open socket FD's, reading any data.
666                  */
667                 for (listener = mainconfig.listen;
668                      listener != NULL;
669                      listener = listener->next) {
670                         RAD_REQUEST_FUNP fun;
671
672                         if ((listener->fd >= 0) &&
673                             !FD_ISSET(listener->fd, &readfds))
674                                 continue;
675
676                         /*
677                          *  Receive the packet.
678                          */
679                         if (sig_hup_block != FALSE) {
680                                 continue;
681                         }
682
683                         /*
684                          *      Do per-socket receive processing of
685                          *      the packet.  This also takes care of
686                          *      inserting the request into the event
687                          *      tree, and adding it to the queue for
688                          *      threads.
689                          */
690                         if (!listener->recv(listener, &fun, &request)) {
691                                 continue;
692                         }
693
694                         /*
695                          *      Drop the request into the thread pool,
696                          *      and let the thread pool take care of
697                          *      doing something with it.
698                          */
699                         if (!thread_pool_addrequest(request, fun)) {
700                                 request->child_state = REQUEST_DONE;
701                         }
702
703                         /*
704                          *      If we've read a packet from a socket
705                          *      OTHER than the detail file, we start
706                          *      ignoring the detail file.
707                          *
708                          *      When the child thread pulls the
709                          *      request off of the queues, it will
710                          *      check if the queues are empty.  Once
711                          *      all queues are empty, it will signal
712                          *      us to read the detail file again.
713                          */
714                         if (spawn_flag &&
715                             (listener->type != RAD_LISTEN_DETAIL)) {
716                                 read_from_detail = FALSE;
717                         }
718                 } /* loop over listening sockets*/
719
720                 ptv = &tv;
721                 radius_event_process(&ptv);
722
723 #ifdef HAVE_PTHREAD_H
724
725                 /*
726                  *      Only clean the thread pool if we're spawning
727                  *      child threads.
728                  *
729                  *      FIXME: Move this to the event handler!
730                  */
731                 if (spawn_flag) {
732                         thread_pool_clean(time_now);
733                 }
734 #endif
735         } /* loop forever */
736 }
737
738
739 /*
740  *  Display the syntax for starting this program.
741  */
742 static void NEVER_RETURNS usage(int status)
743 {
744         FILE *output = status?stderr:stdout;
745
746         fprintf(output,
747                         "Usage: %s [-a acct_dir] [-d db_dir] [-l log_dir] [-i address] [-AcfnsSvXxyz]\n", progname);
748         fprintf(output, "Options:\n\n");
749         fprintf(output, "  -a acct_dir     use accounting directory 'acct_dir'.\n");
750         fprintf(output, "  -A              Log auth detail.\n");
751         fprintf(output, "  -d raddb_dir    Configuration files are in \"raddbdir/*\".\n");
752         fprintf(output, "  -f              Run as a foreground process, not a daemon.\n");
753         fprintf(output, "  -h              Print this help message.\n");
754         fprintf(output, "  -i ipaddr       Listen on ipaddr ONLY\n");
755         fprintf(output, "  -l log_dir      Log file is \"log_dir/radius.log\" (not used in debug mode)\n");
756         fprintf(output, "  -p port         Listen on port ONLY\n");
757         fprintf(output, "  -s              Do not spawn child processes to handle requests.\n");
758         fprintf(output, "  -S              Log stripped names.\n");
759         fprintf(output, "  -v              Print server version information.\n");
760         fprintf(output, "  -X              Turn on full debugging.\n");
761         fprintf(output, "  -x              Turn on additional debugging. (-xx gives more debugging).\n");
762         fprintf(output, "  -y              Log authentication failures, with password.\n");
763         fprintf(output, "  -z              Log authentication successes, with password.\n");
764         exit(status);
765 }
766
767
768 /*
769  *      We got a fatal signal.
770  */
771 static void sig_fatal(int sig)
772 {
773         switch(sig) {
774                 case SIGSEGV:
775                         /*
776                          *      We can't really do anything
777                          *      intelligent here so just die
778                          */
779                         _exit(1);
780
781                 case SIGTERM:
782                         radius_signal_self(RADIUS_SIGNAL_SELF_TERM);
783                         break;
784
785                 case SIGINT:
786                 case SIGQUIT:
787                         if (debug_memory) {
788                                 radius_signal_self(RADIUS_SIGNAL_SELF_TERM);
789                                 break;
790                         }
791                         /* FALL-THROUGH */
792
793                 default:
794                         radius_signal_self(RADIUS_SIGNAL_SELF_EXIT);
795                         break;
796         }
797 }
798
799
800 /*
801  *  We got the hangup signal.
802  *  Re-read the configuration files.
803  */
804 static void sig_hup(int sig)
805 {
806         sig = sig; /* -Wunused */
807
808         reset_signal(SIGHUP, sig_hup);
809
810         write(STDOUT_FILENO, "STUFF\n", 6);
811
812         radius_signal_self(RADIUS_SIGNAL_SELF_HUP);
813 }
814
815 void radius_signal_self(int flag)
816 {
817         ssize_t rcode;
818         uint8_t buffer[16];
819
820         /*
821          *      The caller is telling us it's OK to read from the
822          *      detail files.  However, we're not even listening on
823          *      the detail files.  Therefore, suppress the flag to
824          *      avoid bothering the mail worker thread.
825          */
826         if ((flag == RADIUS_SIGNAL_SELF_DETAIL) &&
827             !has_detail_listener) {
828                 return;
829         }
830
831         /*
832          *      The read MUST be non-blocking for this to work.
833          */
834         rcode = read(radius_self_pipe[0], buffer, sizeof(buffer));
835         if (rcode > 0) {
836                 ssize_t i;
837
838                 for (i = 1; i < rcode; i++) {
839                         buffer[0] |= buffer[i];
840                 }
841         }
842
843 #ifndef NDEBUG
844         memset(buffer + 1, 0, sizeof(buffer) - 1);
845 #endif
846
847         buffer[0] |= flag;
848
849
850         write(radius_self_pipe[1], buffer, 1);
851 }