[PATCH] syslogd: Possible endless loop when reinitializing after power loss

Christian Engelmayer christian.engelmayer at frequentis.com
Mon Oct 24 22:52:42 UTC 2011


Function log_locally() within the syslogd can potentially lock up when
restarting the daemon after a power loss in case the unplanned shutdown hit the
rename operation during logfile rotation.

While POSIX requires the rename operation to be atomic, many file systems such
as JFFS2 implement the rename operation in 2 steps by linking the new name
followed by unlinking the original name. In case of a power loss during the
rename the system can end up with /var/log/messages and /var/log/messages.0
being 2 hard links to the same file.

When the syslog daemon restarts in such a situation it will rediscover the need
to rotate the log files, however, POSIX also requires that rename does nothing
and reports success in case oldpath and newpath are existing hard links to the
same file. Looping through reopen: by (O_CREAT | O_APPEND), the daemon
eternally reopens the same file without succeeding to rotate.

Signed-off-by: Christian Engelmayer <christian.engelmayer at frequentis.com>
--

diff -upNr -x .svn 1.19.2.orig/sysklogd/syslogd.c 1.19.2/sysklogd/syslogd.c
--- 1.19.2.orig/sysklogd/syslogd.c	2011-10-24 23:56:45.000000000 +0200
+++ 1.19.2/sysklogd/syslogd.c	2011-10-25 00:02:27.000000000 +0200
@@ -594,6 +594,11 @@ static void log_locally(time_t now, char
 			}
 			/* newFile == "f.0" now */
 			rename(log_file->path, newFile);
+
+			/* POSIX requires that rename does nothing and reports
+			 * success if oldpath and newpath are existing hard
+			 * links referring to the same file. */
+			unlink(log_file->path);
 #ifdef SYSLOGD_WRLOCK
 			fl.l_type = F_UNLCK;
 			fcntl(log_file->fd, F_SETLKW, &fl);


More information about the busybox mailing list