Add '-l logfile' back again (it's good for something)
authoraland <aland>
Thu, 6 Mar 2008 10:28:36 +0000 (10:28 +0000)
committeraland <aland>
Thu, 6 Mar 2008 10:28:36 +0000 (10:28 +0000)
Initialize mainconfig.radlog_dest.
Keep log FP open, unless it's been deleted.  This allows
libfreeradius to log to the FP, too

src/main/log.c
src/main/mainconfig.c
src/main/radiusd.c

index a3ac002..d3ac8f7 100644 (file)
@@ -28,13 +28,19 @@ RCSID("$Id$")
 
 #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[] = {
@@ -89,6 +95,13 @@ int vradlog(int lvl, const char *fmt, va_list ap)
                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) {
@@ -125,14 +138,34 @@ int vradlog(int lvl, const char *fmt, va_list ap)
                                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);
                }
        }
 
@@ -188,10 +221,8 @@ int vradlog(int lvl, const char *fmt, va_list ap)
                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));
        }
index f2a8618..0db4a9b 100644 (file)
@@ -636,6 +636,11 @@ int read_mainconfig(int reload)
        }
 
        /*
+        *      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
         *
@@ -655,7 +660,7 @@ int read_mainconfig(int reload)
                        cf_section_free(&cs);
                        return -1;
                }
-
+               
                mainconfig.radlog_dest = fr_str2int(str2dest, radlog_dest,
                                                    RADLOG_NUM_DEST);
                if (mainconfig.radlog_dest == RADLOG_NUM_DEST) {
@@ -664,7 +669,7 @@ int read_mainconfig(int reload)
                        cf_section_free(&cs);
                        return -1;
                }
-
+               
                if (mainconfig.radlog_dest == RADLOG_SYSLOG) {
                        /*
                         *      Make sure syslog_facility isn't NULL
@@ -683,11 +688,13 @@ int read_mainconfig(int reload)
                                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);
index c85f857..856db32 100644 (file)
@@ -148,11 +148,12 @@ int main(int argc, char *argv[])
         *      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':
@@ -174,6 +175,11 @@ int main(int argc, char *argv[])
                                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);
@@ -254,7 +260,7 @@ int main(int argc, char *argv[])
        /*
         *  Disconnect from session
         */
-       if (debug_flag == 0 && dont_fork == FALSE) {
+       if (dont_fork == FALSE) {
                pid_t pid = fork();
 
                if (pid < 0) {