[Midnightbsd-cvs] src [9452] trunk/usr.sbin/newsyslog/newsyslog.c: Some filesystems (NFS in particular) do not fill out the d_type field when

laffer1 at midnightbsd.org laffer1 at midnightbsd.org
Sun Mar 5 14:32:41 EST 2017


Revision: 9452
          http://svnweb.midnightbsd.org/src/?rev=9452
Author:   laffer1
Date:     2017-03-05 14:32:41 -0500 (Sun, 05 Mar 2017)
Log Message:
-----------
  Some filesystems (NFS in particular) do not fill out the d_type field when
  returning directory entries through readdir(3). In this case we need to
  obtain the file type ourselves; otherwise newsyslog -t will not be able to
  find archived log files and will fail to both delete old log files and to
  do interval-based rotations properly.

Modified Paths:
--------------
    trunk/usr.sbin/newsyslog/newsyslog.c

Modified: trunk/usr.sbin/newsyslog/newsyslog.c
===================================================================
--- trunk/usr.sbin/newsyslog/newsyslog.c	2017-03-05 19:32:12 UTC (rev 9451)
+++ trunk/usr.sbin/newsyslog/newsyslog.c	2017-03-05 19:32:41 UTC (rev 9452)
@@ -1450,8 +1450,10 @@
  * tm if this is the case; otherwise return false.
  */
 static int
-validate_old_timelog(const struct dirent *dp, const char *logfname, struct tm *tm)
+validate_old_timelog(int fd, const struct dirent *dp, const char *logfname,
+    struct tm *tm)
 {
+	struct stat sb;
 	size_t logfname_len;
 	char *s;
 	int c;
@@ -1458,8 +1460,17 @@
 
 	logfname_len = strlen(logfname);
 
-	if (dp->d_type != DT_REG)
-		return (0);
+	if (dp->d_type != DT_REG) {
+		/*
+		 * Some filesystems (e.g. NFS) don't fill out the d_type field
+		 * and leave it set to DT_UNKNOWN; in this case we must obtain
+		 * the file type ourselves.
+		 */
+		if (dp->d_type != DT_UNKNOWN ||
+		    fstatat(fd, dp->d_name, &sb, AT_SYMLINK_NOFOLLOW) != 0 ||
+		    !S_ISREG(sb.st_mode))
+			return (0);
+	}
 	/* Ignore everything but files with our logfile prefix. */
 	if (strncmp(dp->d_name, logfname, logfname_len) != 0)
 		return (0);
@@ -1545,7 +1556,7 @@
 		err(1, "Cannot open log directory '%s'", dir);
 	dirfd = dirfd(dirp);
 	while ((dp = readdir(dirp)) != NULL) {
-		if (validate_old_timelog(dp, logfname, &tm) == 0)
+		if (validate_old_timelog(dir_fd, dp, logfname, &tm) == 0)
 			continue;
 
 		/*
@@ -2310,10 +2321,10 @@
 	dir_fd = dirfd(dirp);
 	/* Open the archive dir and find the most recent archive of logfname. */
 	while ((dp = readdir(dirp)) != NULL) {
-		if (validate_old_timelog(dp, logfname, &tm) == 0)
+		if (validate_old_timelog(dir_fd, dp, logfname, &tm) == 0)
 			continue;
 
-		if (fstatat(dir_fd, logfname, &sb, 0) == -1) {
+		if (fstatat(dir_fd, logfname, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
 			warn("Cannot stat '%s'", file);
 			continue;
 		}



More information about the Midnightbsd-cvs mailing list