Initialize mainconfig.radlog_dest.
Keep log FP open, unless it's been deleted. This allows
libfreeradius to log to the FP, too
#include <freeradius-devel/radiusd.h>
+#ifdef HAVE_SYS_STAT_H
+#include <sys/stat.h>
+#endif
+
#ifdef HAVE_SYSLOG_H
# include <syslog.h>
/* keep track of whether we've run openlog() */
static int openlog_run = 0;
#endif
- /*
+static FILE *log_fp = NULL;
+
+/*
* Logging facility names
*/
static const FR_NAME_NUMBER levels[] = {
print_timestamp = 1;
}
+ if ((fd != -1) &&
+ (myconfig->radlog_dest != RADLOG_STDOUT) &&
+ (myconfig->radlog_dest != RADLOG_STDERR)) {
+ myconfig->radlog_fd = -1;
+ fd = -1;
+ }
+
*buffer = '\0';
len = 0;
if (fd != -1) {
return 0;
}
- } else if ((fp = fopen(myconfig->log_file, "a")) == NULL) {
- fprintf(stderr, "%s: Couldn't open %s for logging: %s\n",
- progname, myconfig->log_file, strerror(errno));
-
- fprintf(stderr, " (");
- vfprintf(stderr, fmt, ap); /* the message that caused the log */
- fprintf(stderr, ")\n");
- return -1;
+ } else if (log_fp) {
+ struct stat buf;
+
+ if ((stat(myconfig->log_file, &buf) < 0) ||
+ (buf.st_ino == 0)) {
+ fclose(log_fp);
+ log_fp = fr_log_fp = NULL;
+ }
+ }
+
+ if (log_fp) {
+ fclose(log_fp);
+ log_fp = fr_log_fp = NULL;
+ }
+
+ if (!log_fp) {
+ log_fp = fopen(myconfig->log_file, "a");
+ if (!log_fp) {
+ fprintf(stderr, "%s: Couldn't open %s for logging: %s\n",
+ progname, myconfig->log_file, strerror(errno));
+
+ fprintf(stderr, " (");
+ vfprintf(stderr, fmt, ap); /* the message that caused the log */
+ fprintf(stderr, ")\n");
+ return -1;
+ }
+ fr_log_fp = log_fp;
+ setlinebuf(log_fp);
}
}
syslog(lvl, "%s", buffer);
} else
#endif
- if (fp != NULL) {
- fputs(buffer, fp);
- fflush(fp);
- fclose(fp);
+ if (log_fp != NULL) {
+ fputs(buffer, log_fp);
} else if (fd >= 0) {
write(fd, buffer, strlen(buffer));
}
}
/*
+ * Don't over-ride a destination set on the command-line.
+ */
+ if (mainconfig.radlog_dest != RADLOG_NULL) goto read_dict;
+
+ /*
* Debug flag 1 MAY go to files.
* Debug flag 2 ALWAYS goes to stdout
*
cf_section_free(&cs);
return -1;
}
-
+
mainconfig.radlog_dest = fr_str2int(str2dest, radlog_dest,
RADLOG_NUM_DEST);
if (mainconfig.radlog_dest == RADLOG_NUM_DEST) {
cf_section_free(&cs);
return -1;
}
-
+
if (mainconfig.radlog_dest == RADLOG_SYSLOG) {
/*
* Make sure syslog_facility isn't NULL
return -1;
}
}
- } else {
+
+ } else if (mainconfig.radlog_dest != RADLOG_NULL) {
mainconfig.radlog_dest = RADLOG_STDOUT;
mainconfig.radlog_fd = STDOUT_FILENO;
}
+ read_dict:
/* Initialize the dictionary */
cp = cf_pair_find(cs, "dictionary");
if (cp) p = cf_pair_value(cp);
* Don't put output anywhere until we get told a little
* more.
*/
+ mainconfig.radlog_dest = RADLOG_NULL;
mainconfig.radlog_fd = -1;
mainconfig.log_file = NULL;
/* Process the options. */
- while ((argval = getopt(argc, argv, "Cd:fhi:mn:p:svxX")) != EOF) {
+ while ((argval = getopt(argc, argv, "Cd:fhi:l:mn:p:svxX")) != EOF) {
switch(argval) {
case 'C':
usage(0);
break;
+ case 'l':
+ mainconfig.log_file = strdup(optarg);
+ mainconfig.radlog_dest = RADLOG_FILES;
+ break;
+
case 'i':
if (ip_hton(optarg, AF_UNSPEC, &mainconfig.myip) < 0) {
fprintf(stderr, "radiusd: Invalid IP Address or hostname \"%s\"\n", optarg);
/*
* Disconnect from session
*/
- if (debug_flag == 0 && dont_fork == FALSE) {
+ if (dont_fork == FALSE) {
pid_t pid = fork();
if (pid < 0) {