]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
debug: detect logrotation by checking inode number
authorRalph Boehme <slow@samba.org>
Mon, 23 Nov 2020 15:04:03 +0000 (16:04 +0100)
committerKarolin Seeger <kseeger@samba.org>
Wed, 9 Dec 2020 10:44:15 +0000 (10:44 +0000)
BUG: https://bugzilla.samba.org/show_bug.cgi?id=14248

Signed-off-by: Ralph Boehme <slow@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
(cherry picked from commit 3651a51e93b45104323d5db1d5ea704d4f71acf1)

lib/util/debug.c

index 6764578096add3b57d59cde3826ad3ffda755bc3..692e97e3390b9689ef4d425c735e8e99c4bea1ab 100644 (file)
@@ -113,6 +113,8 @@ struct debug_class {
         */
        char *logfile;
        int fd;
+       /* inode number of the logfile to detect logfile rotation */
+       ino_t ino;
 };
 
 static const char *default_classname_table[] = {
@@ -1086,7 +1088,9 @@ static bool reopen_one_log(struct debug_class *config)
 {
        int old_fd = config->fd;
        const char *logfile = config->logfile;
+       struct stat st;
        int new_fd;
+       int ret;
 
        if (logfile == NULL) {
                debug_close_fd(old_fd);
@@ -1107,6 +1111,16 @@ static bool reopen_one_log(struct debug_class *config)
        smb_set_close_on_exec(new_fd);
        config->fd = new_fd;
 
+       ret = fstat(new_fd, &st);
+       if (ret != 0) {
+               log_overflow = true;
+               DBG_ERR("Unable to fstat() new log file '%s': %s\n",
+                       logfile, strerror(errno));
+               log_overflow = false;
+               return false;
+       }
+
+       config->ino = st.st_ino;
        return true;
 }
 
@@ -1254,17 +1268,26 @@ static void do_one_check_log_size(off_t maxlog, struct debug_class *config)
        char name[strlen(config->logfile) + 5];
        struct stat st;
        int ret;
+       bool reopen = false;
        bool ok;
 
        if (maxlog == 0) {
                return;
        }
 
-       ret = fstat(config->fd, &st);
+       ret = stat(config->logfile, &st);
        if (ret != 0) {
                return;
        }
-       if (st.st_size < maxlog ) {
+       if (st.st_size >= maxlog ) {
+               reopen = true;
+       }
+
+       if (st.st_ino != config->ino) {
+               reopen = true;
+       }
+
+       if (!reopen) {
                return;
        }
 
@@ -1276,8 +1299,12 @@ static void do_one_check_log_size(off_t maxlog, struct debug_class *config)
        }
        ret = fstat(config->fd, &st);
        if (ret != 0) {
+               config->ino = (ino_t)0;
                return;
        }
+
+       config->ino = st.st_ino;
+
        if (st.st_size < maxlog) {
                return;
        }