06b78880b203b1219e0caed5b83ef27d515308de
[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         /*  Read the configuration files, BEFORE doing anything else.  */
276         if (read_mainconfig(0) < 0) {
277                 exit(1);
278         }
279
280         /*
281          *  Disconnect from session
282          */
283         if (debug_flag == 0 && dont_fork == FALSE) {
284                 pid = fork();
285                 if(pid < 0) {
286                         radlog(L_ERR|L_CONS, "Couldn't fork");
287                         exit(1);
288                 }
289
290                 /*
291                  *  The parent exits, so the child can run in the background.
292                  */
293                 if (pid > 0) {
294                         exit(0);
295                 }
296 #ifdef HAVE_SETSID
297                 setsid();
298 #endif
299         }
300
301         /*
302          *      If we're NOT debugging, trap fatal signals, so we can
303          *      easily clean up after ourselves.
304          *
305          *      If we ARE debugging, don't trap them, so we can
306          *      dump core.
307          */
308         if ((mainconfig.allow_core_dumps == FALSE) && (debug_flag == 0)) {
309 #ifdef SIGSEGV
310 #ifdef HAVE_SIGACTION
311                 act.sa_handler = sig_fatal;
312                 sigaction(SIGSEGV, &act, NULL);
313 #else
314                 signal(SIGSEGV, sig_fatal);
315 #endif
316 #endif
317         }
318
319         /*
320          *  Ensure that we're using the CORRECT pid after forking,
321          *  NOT the one we started with.
322          */
323         radius_pid = getpid();
324
325         /*
326          *  Only write the PID file if we're running as a daemon.
327          *
328          *  And write it AFTER we've forked, so that we write the
329          *  correct PID.
330          */
331         if (dont_fork == FALSE) {
332                 FILE *fp;
333
334                 fp = fopen(mainconfig.pid_file, "w");
335                 if (fp != NULL) {
336                         /*
337                          *      FIXME: What about following symlinks,
338                          *      and having it over-write a normal file?
339                          */
340                         fprintf(fp, "%d\n", (int) radius_pid);
341                         fclose(fp);
342                 } else {
343                         radlog(L_ERR|L_CONS, "Failed creating PID file %s: %s\n",
344                                mainconfig.pid_file, strerror(errno));
345                         exit(1);
346                 }
347         }
348
349         /*
350          *      Child threads need a pipe to signal us, as do the
351          *      signal handlers.
352          */
353         if (pipe(radius_self_pipe) < 0) {
354                 radlog(L_ERR, "radiusd: Error opening internal pipe: %s",
355                        strerror(errno));
356                 exit(1);
357         }
358         if (fcntl(radius_self_pipe[0], F_SETFL, O_NONBLOCK | FD_CLOEXEC) < 0) {
359                 radlog(L_ERR, "radiusd: Error setting internal flags: %s",
360                        strerror(errno));
361                 exit(1);
362         }
363         if (fcntl(radius_self_pipe[1], F_SETFL, O_NONBLOCK | FD_CLOEXEC) < 0) {
364                 radlog(L_ERR, "radiusd: Error setting internal flags: %s",
365                        strerror(errno));
366                 exit(1);
367         }
368
369         /*
370          *      If we're running as a daemon, close the default file
371          *      descriptors, AFTER forking.
372          */
373         if (!debug_flag) {
374                 int devnull;
375
376                 devnull = open("/dev/null", O_RDWR);
377                 if (devnull < 0) {
378                         radlog(L_ERR|L_CONS, "Failed opening /dev/null: %s\n",
379                                strerror(errno));
380                         exit(1);
381                 }
382                 dup2(devnull, STDIN_FILENO);
383                 if (mainconfig.radlog_dest == RADLOG_STDOUT) {
384                         mainconfig.radlog_fd = dup(STDOUT_FILENO);
385                 }
386                 dup2(devnull, STDOUT_FILENO);
387                 if (mainconfig.radlog_dest == RADLOG_STDERR) {
388                         mainconfig.radlog_fd = dup(STDERR_FILENO);
389                 }
390                 dup2(devnull, STDERR_FILENO);
391                 close(devnull);
392         }
393
394         /*
395          *      It's called the thread pool, but it does a little
396          *      more than that.
397          */
398         radius_event_init(spawn_flag);
399
400         /*
401          *  Use linebuffered or unbuffered stdout if
402          *  the debug flag is on.
403          */
404         if (debug_flag == TRUE)
405                 setlinebuf(stdout);
406
407         /*
408          *      Print out which ports we're listening on.
409          */
410         for (listener = mainconfig.listen;
411              listener != NULL;
412              listener = listener->next) {
413                 listener->print(listener, buffer, sizeof(buffer));
414                 switch (listener->type) {
415                 case RAD_LISTEN_AUTH:
416                         DEBUG("Listening on authentication address %s", buffer);
417                         break;
418
419                 case RAD_LISTEN_ACCT:
420                         DEBUG("Listening on accounting address %s", buffer);
421                         break;
422
423                 case RAD_LISTEN_PROXY:
424                         DEBUG("Listening on proxy address %s", buffer);
425                         break;
426
427                 case RAD_LISTEN_DETAIL:
428                         has_detail_listener = TRUE;
429                         read_from_detail = TRUE;
430                         DEBUG("Listening on detail file %s", buffer);
431                         break;
432
433                 case RAD_LISTEN_SNMP:
434                         DEBUG("Listening on SNMP %s", buffer);
435                         break;
436
437                 default:
438                         break;
439                 }
440         }
441
442         /*
443          *      Now that we've set everything up, we can install the signal
444          *      handlers.  Before this, if we get any signal, we don't know
445          *      what to do, so we might as well do the default, and die.
446          */
447         signal(SIGPIPE, SIG_IGN);
448 #ifdef HAVE_SIGACTION
449         act.sa_handler = sig_hup;
450         sigaction(SIGHUP, &act, NULL);
451         act.sa_handler = sig_fatal;
452         sigaction(SIGTERM, &act, NULL);
453 #else
454         signal(SIGHUP, sig_hup);
455         signal(SIGTERM, sig_fatal);
456 #endif
457         /*
458          *      If we're debugging, then a CTRL-C will cause the
459          *      server to die immediately.  Use SIGTERM to shut down
460          *      the server cleanly in that case.
461          */
462         if ((debug_memory == 1) || (debug_flag == 0)) {
463 #ifdef HAVE_SIGACTION
464                 act.sa_handler = sig_fatal;
465                 sigaction(SIGINT, &act, NULL);
466                 sigaction(SIGQUIT, &act, NULL);
467 #else
468                 signal(SIGINT, sig_fatal);
469                 signal(SIGQUIT, sig_fatal);
470 #endif
471         }
472
473  restart:
474         radlog(L_INFO, "Ready to process requests.");
475
476         /*
477          *  Receive user requests
478          */
479         for (;;) {
480                 ssize_t rcode;
481
482                 FD_ZERO(&readfds);
483                 max_fd = 0;
484
485                 /*
486                  *      Loop over all the listening FD's.
487                  */
488                 for (listener = mainconfig.listen;
489                      listener != NULL;
490                      listener = listener->next) {
491                         if (listener->fd < 0) continue;
492
493                         if (!read_from_detail &&
494                             (listener->type == RAD_LISTEN_DETAIL)) continue;
495
496                         FD_SET(listener->fd, &readfds);
497                         if (listener->fd > max_fd) max_fd = listener->fd;
498                 }
499
500                 /*
501                  *      Add in the self pipe FD, for signals or
502                  *      notices from the child threads.
503                  */
504                 FD_SET(radius_self_pipe[0], &readfds);
505                 if (radius_self_pipe[0] > max_fd) {
506                         max_fd = radius_self_pipe[0];
507                 }
508
509                 if (!ptv) {
510                         DEBUG2("Nothing to do.  Sleeping until we see a request.");
511                 } else if (tv.tv_sec) {
512                                 DEBUG2("Waking up in %d seconds...",
513                                        (int) tv.tv_sec);
514                 }
515
516                 status = select(max_fd + 1, &readfds, NULL, NULL, ptv);
517                 if ((status < 0) && (errno != EINTR)) {
518                         radlog(L_ERR, "Unexpected error in select(): %s",
519                                strerror(errno));
520                                 exit(1);
521                 }
522                 time_now = time(NULL);
523
524 #ifndef HAVE_PTHREAD_H
525                 /*
526                  *      If there are no child threads, then there may
527                  *      be child processes.  In that case, wait for
528                  *      their exit status, and throw that exit status
529                  *      away.  This helps get rid of zxombie children.
530                  */
531                 while (waitpid(-1, &argval, WNOHANG) > 0) {
532                         /* do nothing */
533                 }
534 #endif
535
536                 /*
537                  *      Before doing anything else, check the self pipe.
538                  */
539                 if (FD_ISSET(radius_self_pipe[0], &readfds) &&
540                     (rcode = read(radius_self_pipe[0], buffer, sizeof(buffer))) > 0) {
541                         ssize_t i;
542
543                         for (i = 1; i < rcode; i++) {
544                                 buffer[0] |= buffer[i];
545                         }
546
547                         if ((buffer[0] & (RADIUS_SIGNAL_SELF_EXIT | RADIUS_SIGNAL_SELF_TERM)) != 0) {
548                                 DEBUG("Exiting...");
549
550                                 /*
551                                  *      Ignore the TERM signal: we're
552                                  *      about to die.
553                                  */
554                                 signal(SIGTERM, SIG_IGN);
555
556                                 /*
557                                  *      Send a TERM signal to all
558                                  *      associated processes
559                                  *      (including us, which gets
560                                  *      ignored.)
561                                  */
562                                 kill(-radius_pid, SIGTERM);
563
564                                 /*
565                                  *      FIXME: Kill child threads, and
566                                  *      clean up?
567                                  */
568
569                                 /*
570                                  *      FIXME: clean up any active REQUEST
571                                  *      handles.
572                                  */
573
574                                 /*
575                                  *      We're exiting, so we can delete the PID
576                                  *      file.  (If it doesn't exist, we can ignore
577                                  *      the error returned by unlink)
578                                  */
579                                 if (dont_fork == FALSE) {
580                                         unlink(mainconfig.pid_file);
581                                 }
582
583                                 radius_event_free();
584
585                                 /*
586                                  *      Free the configuration items.
587                                  */
588                                 free_mainconfig();
589
590                                 /*
591                                  *      Detach any modules.
592                                  */
593                                 detach_modules();
594
595                                 free(radius_dir);
596
597                                 /*
598                                  *      SIGTERM gets do_exit=0,
599                                  *      and we want to exit cleanly.
600                                  *
601                                  *      Other signals make us exit
602                                  *      with an error status.
603                                  */
604                                 if ((buffer[0] & RADIUS_SIGNAL_SELF_EXIT) != 0) {
605                                         exit(1);
606                                 }
607                                 exit(0);
608                         } /* else exit/term flags weren't set */
609
610                         if ((buffer[0] & RADIUS_SIGNAL_SELF_HUP) != 0) {
611                                 DEBUG("Received HUP signal");
612 #ifdef HAVE_PTHREAD_H
613                                 /*
614                                  *      Threads: wait for all threads to stop
615                                  *      processing before re-loading the
616                                  *      config, so we don't pull the rug out
617                                  *      from under them.
618                                  */
619                                 int max_wait = 0;
620                                 if (spawn_flag) for(;;) {
621                                         /*
622                                          *      Block until there are
623                                          *      zero threads with a
624                                          *      REQUEST handle.
625                                          *
626                                          *      FIXME: Wait for
627                                          *      threads to send us a
628                                          *      signal in the pipe?
629                                          */
630                                         sig_hup_block = TRUE;
631
632                                         if( (total_active_threads() == 0) ||
633                                             (max_wait >= 5) ) {
634                                                 sig_hup_block = FALSE;
635                                                 break;
636                                         }
637                                         sleep(1);
638                                         max_wait++;
639                                 }
640 #endif
641
642 #ifdef WITH_SNMP
643                                 if (mainconfig.do_snmp) {
644                                         rad_snmp.smux_failures = 0;
645                                         rad_snmp.smux_event = SMUX_CONNECT;
646                                 }
647 #endif
648                                 if (read_mainconfig(TRUE) < 0) {
649                                         exit(1);
650                                 }
651
652                                 goto restart;
653                         }
654
655                         /*
656                          *      Check if we can read from the detail file
657                          */
658                         if (has_detail_listener &&
659                             (buffer[0] & RADIUS_SIGNAL_SELF_DETAIL) != 0) {
660                                 DEBUG3("Can now read from detail file(s)");
661                                 read_from_detail = TRUE;
662                         }
663                 } /* else the self pipe didn't have data ready */
664
665                 /*
666                  *      Loop over the open socket FD's, reading any data.
667                  */
668                 for (listener = mainconfig.listen;
669                      listener != NULL;
670                      listener = listener->next) {
671                         RAD_REQUEST_FUNP fun;
672
673                         if ((listener->fd >= 0) &&
674                             !FD_ISSET(listener->fd, &readfds))
675                                 continue;
676
677                         /*
678                          *  Receive the packet.
679                          */
680                         if (sig_hup_block != FALSE) {
681                                 continue;
682                         }
683
684                         /*
685                          *      Do per-socket receive processing of
686                          *      the packet.  This also takes care of
687                          *      inserting the request into the event
688                          *      tree, and adding it to the queue for
689                          *      threads.
690                          */
691                         if (!listener->recv(listener, &fun, &request)) {
692                                 continue;
693                         }
694
695                         /*
696                          *      Drop the request into the thread pool,
697                          *      and let the thread pool take care of
698                          *      doing something with it.
699                          */
700                         if (!thread_pool_addrequest(request, fun)) {
701                                 request->child_state = REQUEST_DONE;
702                         }
703
704                         /*
705                          *      If we've read a packet from a socket
706                          *      OTHER than the detail file, we start
707                          *      ignoring the detail file.
708                          *
709                          *      When the child thread pulls the
710                          *      request off of the queues, it will
711                          *      check if the queues are empty.  Once
712                          *      all queues are empty, it will signal
713                          *      us to read the detail file again.
714                          */
715                         if (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         buffer[0] |= flag;
844
845         write(radius_self_pipe[1], buffer, 1);
846 }