]> jfr.im git - solanum.git/blobdiff - src/logger.c
Merge pull request #53 from ShadowNinja/clarify_U+R
[solanum.git] / src / logger.c
index 2246f25a501a8365c99e46f4bb60caa98e4ed3c0..e7f14f61f6b6263673c7ba97e6ff7f98befcdf1c 100644 (file)
@@ -38,7 +38,6 @@
 #include "ircd_defs.h"
 #include "logger.h"
 #include "s_conf.h"
-#include "sprintf_irc.h"
 #include "send.h"
 #include "client.h"
 #include "s_serv.h"
@@ -74,11 +73,56 @@ static struct log_struct log_table[LAST_LOGFILE] =
        { &ConfigFileEntry.fname_ioerrorlog,    &log_ioerror    }
 };
 
+static void
+verify_logfile_access(const char *filename)
+{
+       char *dirname, *d;
+       char buf[512];
+       d = rb_dirname(filename);
+       dirname = LOCAL_COPY(d);
+       rb_free(d);
+
+       if(access(dirname, F_OK) == -1)
+       {
+               rb_snprintf(buf, sizeof(buf), "WARNING: Unable to access logfile %s - parent directory %s does not exist", filename, dirname);
+               if(testing_conf || server_state_foreground)
+                       fprintf(stderr, "%s\n", buf);
+               sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s", buf);
+               return;
+       }
+
+       if(access(filename, F_OK) == -1)
+       {
+               if(access(dirname, W_OK) == -1)
+               {
+                       rb_snprintf(buf, sizeof(buf), "WARNING: Unable to access logfile %s - access to parent directory %s failed: %s",
+                                   filename, dirname, strerror(errno));
+                       if(testing_conf || server_state_foreground)
+                               fprintf(stderr, "%s\n", buf);
+                       sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s", buf);
+               }
+               return;
+       }
+
+       if(access(filename, W_OK) == -1)
+       {
+               rb_snprintf(buf, sizeof(buf), "WARNING: Access denied for logfile %s: %s", filename, strerror(errno));
+               if(testing_conf || server_state_foreground)
+                       fprintf(stderr, "%s\n", buf);
+               sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s", buf);
+               return;
+       }
+       return;
+}
+
 void
 init_main_logfile(void)
 {
+       verify_logfile_access(logFileName);
        if(log_main == NULL)
+       {
                log_main = fopen(logFileName, "a");
+       }
 }
 
 void
@@ -95,9 +139,12 @@ open_logfiles(void)
        {
                /* reopen those with paths */
                if(!EmptyString(*log_table[i].name))
+               {
+                       verify_logfile_access(*log_table[i].name);
                        *log_table[i].logfile = fopen(*log_table[i].name, "a");
+               }
        }
-}                      
+}
 
 void
 close_logfiles(void)
@@ -133,12 +180,14 @@ ilog(ilogfile dest, const char *format, ...)
        rb_vsnprintf(buf, sizeof(buf), format, args);
        va_end(args);
 
-       rb_snprintf(buf2, sizeof(buf2), "%s %s\n", smalldate(), buf);
+       rb_snprintf(buf2, sizeof(buf2), "%s %s\n",
+                       smalldate(rb_current_time()), buf);
 
        if(fputs(buf2, logfile) < 0)
        {
                fclose(logfile);
                *log_table[dest].logfile = NULL;
+               return;
        }
 
        fflush(logfile);
@@ -218,11 +267,10 @@ report_operspy(struct Client *source_p, const char *token, const char *arg)
 }
 
 const char *
-smalldate(void)
+smalldate(time_t ltime)
 {
        static char buf[MAX_DATE_STRING];
        struct tm *lt;
-       time_t ltime = rb_current_time();
 
        lt = localtime(&ltime);
 
@@ -236,7 +284,13 @@ smalldate(void)
 void
 ilog_error(const char *error)
 {
-       ilog(L_IOERROR, "%s: %d (%s)", error, errno, strerror(errno));
+       int e;
+       const char *errstr;
+
+       e = errno;
+       errstr = strerror(e);
 
-       sendto_realops_snomask(SNO_DEBUG, L_ALL, "%s: %d (%s)", error, errno, strerror(errno));
+       ilog(L_IOERROR, "%s: %d (%s)", error, e, errstr);
+       sendto_realops_snomask(SNO_DEBUG, L_ALL, "%s: %d (%s)",
+                       error, e, errstr);
 }