--- common/com-syslog.h +++ common/com-syslog.h 2000/11/03 13:58:03 @@ -52,6 +52,7 @@ void syslog_open (char *name); void syslog_write (int level, char *fmt, ...); void syslog_error (char *fmt, ...); +int syslog_rename(char *new_name, char *log_name, size_t len); void syslog_rotate(void); void syslog_close (void); --- common/com-syslog.c +++ common/com-syslog.c 2000/11/03 14:49:24 @@ -71,6 +71,9 @@ # endif #endif +#include +#include + #include "com-debug.h" #include "com-misc.h" #include "com-syslog.h" @@ -186,12 +189,16 @@ ** So we do have a destination now ... */ if (*name == '/') { + char tmp_name[MAX_PATH_SIZE + 64]; /* ** Logging to a regular file */ - if (unlink(name) != 0 && errno != ENOENT) { - misc_die(FL, "can't remove logfile '%.*s'", + if(syslog_rename(tmp_name, name, sizeof(tmp_name))<0) + { + if (unlink(name) != 0 && errno != ENOENT) { + misc_die(FL, "can't remove logfile '%.*s'", MAX_PATH_SIZE, name); + } } if ((fd = open(name, O_RDWR | O_CREAT | O_EXCL, 0640)) < 0) { @@ -404,6 +411,53 @@ /* ------------------------------------------------------------ ** ** +** Function......: syslog_rename +** +** Parameters....: new_name, log_name, len +** +** Return........: -1 on error, 0 on success, +** 1 if log_name does not exists +** +** Purpose.......: if file specified as log_name exists, +** rename it to new_name with a maximal +** length given in len. +** +** ------------------------------------------------------------ */ + +int syslog_rename(char *new_name, char *log_name, size_t len) +{ + time_t now; + struct tm *t; + struct stat st; + + if( !(len > 0 && new_name && log_name)) + return -1; + + time(&now); + t = localtime(&now); + sprintf(new_name, "%.*s.%d%02d%02d-%02d%02d%02d", + len, log_name, t->tm_year + 1900, + t->tm_mon + 1, t->tm_mday, + t->tm_hour, t->tm_min, t->tm_sec); + + if( stat(log_name, &st)) + return 1; + + if( !stat(new_name, &st)) { + if( !(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))) { + return -1; + } + unlink(new_name); + } + + if (rename(log_name, new_name)) { + return -1; + } + return 0; +} + +/* ------------------------------------------------------------ ** +** ** Function......: syslog_rotate ** ** Parameters....: (none) @@ -418,8 +472,6 @@ { char tmp_name[MAX_PATH_SIZE + 64]; int fd; - time_t now; - struct tm *t; if (log_file == NULL) /* Only useful if logging to file */ return; @@ -431,16 +483,9 @@ fclose(log_file); log_file = NULL; - time(&now); - t = localtime(&now); - sprintf(tmp_name, "%s.%d%02d%02d-%02d%02d%02d", - log_name, t->tm_year + 1900, - t->tm_mon + 1, t->tm_mday, - t->tm_hour, t->tm_min, t->tm_sec); - if (rename(log_name, tmp_name)) { - misc_die(FL, "can't rename '%.*s' to '%.*s'", - MAX_PATH_SIZE, log_name, - MAX_PATH_SIZE, tmp_name); + if(syslog_rename(tmp_name, log_name, sizeof(tmp_name))<0) { + misc_die(FL, "can't rotate logfile '%.*s'", + MAX_PATH_SIZE, log_name); } if ((fd = open(log_name, O_RDWR | O_CREAT | O_EXCL, 0640)) < 0) {