2 * radiusd.c Main loop of the radius server.
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.
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.
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
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>
28 #include <freeradius-devel/ident.h>
31 #include <freeradius-devel/radiusd.h>
32 #include <freeradius-devel/modules.h>
33 #include <freeradius-devel/rad_assert.h>
46 #ifdef HAVE_SYS_WAIT_H
47 # include <sys/wait.h>
50 # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
53 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
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;
66 int check_config = FALSE;
68 const char *radiusd_version = "FreeRADIUS Version " RADIUSD_VERSION ", for host " HOSTINFO ", built on " __DATE__ " at " __TIME__;
72 static int debug_memory = 0;
75 * Configuration items.
81 static void usage(int);
83 static void sig_fatal (int);
85 static void sig_hup (int);
91 int main(int argc, char *argv[])
95 int spawn_flag = TRUE;
96 int dont_fork = FALSE;
100 struct sigaction act;
104 set_auth_parameters(argc,argv);
107 if ((progname = strrchr(argv[0], FR_DIR_SEP)) == NULL)
115 if (WSAStartup(MAKEWORD(2, 0), &wsaData)) {
116 fprintf(stderr, "%s: Unable to initialize socket library.\n");
124 radius_dir = strdup(RADIUS_DIR);
127 * Ensure that the configuration is initialized.
129 memset(&mainconfig, 0, sizeof(mainconfig));
130 mainconfig.myip.af = AF_UNSPEC;
131 mainconfig.port = -1;
132 mainconfig.name = "radiusd";
134 #ifdef HAVE_SIGACTION
135 memset(&act, 0, sizeof(act));
137 sigemptyset( &act.sa_mask ) ;
141 * Don't put output anywhere until we get told a little
144 mainconfig.radlog_dest = RADLOG_NULL;
145 mainconfig.radlog_fd = -1;
146 mainconfig.log_file = NULL;
148 /* Process the options. */
149 while ((argval = getopt(argc, argv, "Cd:fhi:l:mn:p:stvxX")) != EOF) {
159 if (radius_dir) free(radius_dir);
160 radius_dir = strdup(optarg);
172 if (strcmp(optarg, "stdout") == 0) {
175 mainconfig.log_file = strdup(optarg);
176 mainconfig.radlog_dest = RADLOG_FILES;
177 mainconfig.radlog_fd = open(mainconfig.log_file,
178 O_WRONLY | O_APPEND | O_CREAT, 0640);
179 if (mainconfig.radlog_fd < 0) {
180 fprintf(stderr, "radiusd: Failed to open log file %s: %s\n", mainconfig.log_file, strerror(errno));
186 if (ip_hton(optarg, AF_UNSPEC, &mainconfig.myip) < 0) {
187 fprintf(stderr, "radiusd: Invalid IP Address or hostname \"%s\"\n", optarg);
194 mainconfig.name = optarg;
202 mainconfig.port = atoi(optarg);
203 if ((mainconfig.port <= 0) ||
204 (mainconfig.port >= 65536)) {
205 fprintf(stderr, "radiusd: Invalid port number %s\n", optarg);
211 case 's': /* Single process mode */
216 case 't': /* no child threads */
228 mainconfig.log_auth = TRUE;
229 mainconfig.log_auth_badpass = TRUE;
230 mainconfig.log_auth_goodpass = TRUE;
232 mainconfig.radlog_dest = RADLOG_STDOUT;
233 mainconfig.radlog_fd = STDOUT_FILENO;
247 if (flag && (flag != 0x03)) {
248 fprintf(stderr, "radiusd: The options -i and -p cannot be used individually.\n");
253 radlog(L_INFO, "%s", radiusd_version);
254 radlog(L_INFO, "Copyright (C) 1999-2009 The FreeRADIUS server project and contributors.\n");
255 radlog(L_INFO, "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n");
256 radlog(L_INFO, "PARTICULAR PURPOSE.\n");
257 radlog(L_INFO, "You may redistribute copies of FreeRADIUS under the terms of the\n");
258 radlog(L_INFO, "GNU General Public License v2.\n");
262 /* Read the configuration files, BEFORE doing anything else. */
263 if (read_mainconfig(0) < 0) {
269 * Disconnect from session
271 if (dont_fork == FALSE) {
275 radlog(L_ERR, "Couldn't fork: %s", strerror(errno));
280 * The parent exits, so the child can run in the background.
292 * Ensure that we're using the CORRECT pid after forking,
293 * NOT the one we started with.
295 radius_pid = getpid();
298 * If we're running as a daemon, close the default file
299 * descriptors, AFTER forking.
304 devnull = open("/dev/null", O_RDWR);
306 radlog(L_ERR|L_CONS, "Failed opening /dev/null: %s\n",
310 dup2(devnull, STDIN_FILENO);
311 if (mainconfig.radlog_dest == RADLOG_STDOUT) {
312 mainconfig.radlog_fd = dup(STDOUT_FILENO);
315 dup2(devnull, STDOUT_FILENO);
316 if (mainconfig.radlog_dest == RADLOG_STDERR) {
317 mainconfig.radlog_fd = dup(STDERR_FILENO);
320 dup2(devnull, STDERR_FILENO);
324 setlinebuf(stdout); /* unbuffered output */
328 * Initialize the event pool, including threads.
330 radius_event_init(mainconfig.config, spawn_flag);
333 * Now that we've set everything up, we can install the signal
334 * handlers. Before this, if we get any signal, we don't know
335 * what to do, so we might as well do the default, and die.
338 signal(SIGPIPE, SIG_IGN);
340 #ifdef HAVE_SIGACTION
341 act.sa_handler = sig_hup;
342 sigaction(SIGHUP, &act, NULL);
343 act.sa_handler = sig_fatal;
344 sigaction(SIGTERM, &act, NULL);
347 signal(SIGHUP, sig_hup);
349 signal(SIGTERM, sig_fatal);
352 * If we're debugging, then a CTRL-C will cause the
353 * server to die immediately. Use SIGTERM to shut down
354 * the server cleanly in that case.
356 if ((debug_memory == 1) || (debug_flag == 0)) {
357 #ifdef HAVE_SIGACTION
358 act.sa_handler = sig_fatal;
359 sigaction(SIGINT, &act, NULL);
360 sigaction(SIGQUIT, &act, NULL);
362 signal(SIGINT, sig_fatal);
364 signal(SIGQUIT, sig_fatal);
370 * Everything seems to have loaded OK, exit gracefully.
373 DEBUG("Configuration appears to be OK.");
377 radius_stats_init(0);
380 * Only write the PID file if we're running as a daemon.
382 * And write it AFTER we've forked, so that we write the
385 if (dont_fork == FALSE) {
388 fp = fopen(mainconfig.pid_file, "w");
391 * FIXME: What about following symlinks,
392 * and having it over-write a normal file?
394 fprintf(fp, "%d\n", (int) radius_pid);
397 radlog(L_ERR|L_CONS, "Failed creating PID file %s: %s\n",
398 mainconfig.pid_file, strerror(errno));
404 * Process requests until HUP or exit.
406 while ((rcode = radius_event_process()) == 0x80) {
407 radius_stats_init(1);
411 radlog(L_INFO, "Exiting normally.");
414 * Ignore the TERM signal: we're
417 signal(SIGTERM, SIG_IGN);
420 * Send a TERM signal to all
421 * associated processes
422 * (including us, which gets
426 if (spawn_flag) kill(-radius_pid, SIGTERM);
430 * We're exiting, so we can delete the PID
431 * file. (If it doesn't exist, we can ignore
432 * the error returned by unlink)
434 if (dont_fork == FALSE) {
435 unlink(mainconfig.pid_file);
441 * Free the configuration items.
446 * Detach any modules.
450 xlat_free(); /* modules may have xlat's */
463 * Display the syntax for starting this program.
465 static void NEVER_RETURNS usage(int status)
467 FILE *output = status?stderr:stdout;
470 "Usage: %s [-d db_dir] [-l log_dir] [-i address] [-n name] [-fsvXx]\n", progname);
471 fprintf(output, "Options:\n\n");
472 fprintf(output, " -C Check configuration and exit.\n");
473 fprintf(output, " -d raddb_dir Configuration files are in \"raddbdir/*\".\n");
474 fprintf(output, " -f Run as a foreground process, not a daemon.\n");
475 fprintf(output, " -h Print this help message.\n");
476 fprintf(output, " -i ipaddr Listen on ipaddr ONLY\n");
477 fprintf(output, " -n name Read raddb/name.conf instead of raddb/radiusd.conf\n");
478 fprintf(output, " -p port Listen on port ONLY\n");
479 fprintf(output, " -s Do not spawn child processes to handle requests.\n");
480 fprintf(output, " -v Print server version information.\n");
481 fprintf(output, " -X Turn on full debugging.\n");
482 fprintf(output, " -x Turn on additional debugging. (-xx gives more debugging).\n");
488 * We got a fatal signal.
490 static void sig_fatal(int sig)
494 radius_signal_self(RADIUS_SIGNAL_SELF_TERM);
502 radius_signal_self(RADIUS_SIGNAL_SELF_TERM);
514 * We got the hangup signal.
515 * Re-read the configuration files.
517 static void sig_hup(int sig)
519 sig = sig; /* -Wunused */
521 reset_signal(SIGHUP, sig_hup);
523 radius_signal_self(RADIUS_SIGNAL_SELF_HUP);