svn commit: r251533 - stable/8/usr.sbin/newsyslog

Mark Johnston markj at FreeBSD.org
Sat Jun 8 15:46:09 UTC 2013


Author: markj
Date: Sat Jun  8 15:46:09 2013
New Revision: 251533
URL: http://svnweb.freebsd.org/changeset/base/251533

Log:
  MFC r235647 for newsyslog (by gleb):
  Hide DIR definition by making it an opaque struct typedef.
  
  Introduce dirfd() libc exported symbol replacing macro with same name,
  preserve _dirfd() macro for internal use.
  
  Replace dirp->dd_fd with dirfd() call. Avoid using dirfd as variable
  name to prevent shadowing global symbol.
  
  MFC r250545:
  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.
  
  MFC r251240:
  We want to stat the archived log file rather than the logfile itself.

Modified:
  stable/8/usr.sbin/newsyslog/newsyslog.c
Directory Properties:
  stable/8/usr.sbin/newsyslog/   (props changed)

Modified: stable/8/usr.sbin/newsyslog/newsyslog.c
==============================================================================
--- stable/8/usr.sbin/newsyslog/newsyslog.c	Sat Jun  8 15:45:01 2013	(r251532)
+++ stable/8/usr.sbin/newsyslog/newsyslog.c	Sat Jun  8 15:46:09 2013	(r251533)
@@ -1447,16 +1447,27 @@ oldlog_entry_compare(const void *a, cons
  * 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;
 
 	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);
@@ -1508,7 +1519,7 @@ static void
 delete_oldest_timelog(const struct conf_entry *ent, const char *archive_dir)
 {
 	char *logfname, *s, *dir, errbuf[80];
-	int dirfd, i, logcnt, max_logcnt;
+	int dir_fd, i, logcnt, max_logcnt;
 	struct oldlog_entry *oldlogs;
 	struct dirent *dp;
 	const char *cdir;
@@ -1540,9 +1551,9 @@ delete_oldest_timelog(const struct conf_
 	/* First we create a 'list' of all archived logfiles */
 	if ((dirp = opendir(dir)) == NULL)
 		err(1, "Cannot open log directory '%s'", dir);
-	dirfd = dirfd(dirp);
+	dir_fd = 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;
 
 		/*
@@ -1590,7 +1601,7 @@ delete_oldest_timelog(const struct conf_
 			if (noaction)
 				printf("\trm -f %s/%s\n", dir,
 				    oldlogs[i].fname);
-			else if (unlinkat(dirfd, oldlogs[i].fname, 0) != 0) {
+			else if (unlinkat(dir_fd, oldlogs[i].fname, 0) != 0) {
 				snprintf(errbuf, sizeof(errbuf),
 				    "Could not delete old logfile '%s'",
 				    oldlogs[i].fname);
@@ -2276,10 +2287,10 @@ mtime_old_timelog(const char *file)
 	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, dp->d_name, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
 			warn("Cannot stat '%s'", file);
 			continue;
 		}


More information about the svn-src-all mailing list