[Midnightbsd-cvs] src [9776] trunk/usr.sbin/newsyslog/newsyslog.c: NFS compat fixes
laffer1 at midnightbsd.org
laffer1 at midnightbsd.org
Thu Feb 22 00:13:55 EST 2018
Revision: 9776
http://svnweb.midnightbsd.org/src/?rev=9776
Author: laffer1
Date: 2018-02-22 00:13:54 -0500 (Thu, 22 Feb 2018)
Log Message:
-----------
NFS compat fixes
Modified Paths:
--------------
trunk/usr.sbin/newsyslog/newsyslog.c
Modified: trunk/usr.sbin/newsyslog/newsyslog.c
===================================================================
--- trunk/usr.sbin/newsyslog/newsyslog.c 2018-02-22 05:11:11 UTC (rev 9775)
+++ trunk/usr.sbin/newsyslog/newsyslog.c 2018-02-22 05:13:54 UTC (rev 9776)
@@ -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);
@@ -1511,7 +1522,7 @@
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;
@@ -1543,9 +1554,9 @@
/* 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;
/*
@@ -1593,7 +1604,7 @@
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);
@@ -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, dp->d_name, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
warn("Cannot stat '%s'", file);
continue;
}
More information about the Midnightbsd-cvs
mailing list