When we look for a new trail file there might be a race between find trail

file name and opening it. This race was not properly handled, because we were
copying new name before checking for openat(2) error and when we were trying
again we were starting with the next trail file. This could result in skipping
distribution of such a trail file.

Fix this problem by checking for ENOENT first (only for .not_terminated files)
and then updating (or not) tr_filename before restarting the search.

PR:		200139
Reported by:	peter
Approved by:	re (kib)
This commit is contained in:
Pawel Jakub Dawidek 2018-10-04 05:48:09 +00:00
parent 8046db8b49
commit ac67acf0ef
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=339176

View File

@ -361,17 +361,38 @@ trail_next(struct trail *trail)
pjdlog_debug(1, "No new trail files.");
return;
}
PJDLOG_VERIFY(strlcpy(trail->tr_filename, curfile,
sizeof(trail->tr_filename)) < sizeof(trail->tr_filename));
dfd = dirfd(trail->tr_dirfp);
PJDLOG_ASSERT(dfd >= 0);
trail->tr_filefd = openat(dfd, trail->tr_filename, O_RDONLY);
trail->tr_filefd = openat(dfd, curfile, O_RDONLY);
if (trail->tr_filefd == -1) {
pjdlog_errno(LOG_ERR,
"Unable to open file \"%s/%s\", skipping",
trail->tr_dirname, trail->tr_filename);
if (errno == ENOENT && trail_is_not_terminated(curfile)) {
/*
* The .not_terminated file was most likely renamed.
* Keep trail->tr_filename as a starting point and
* search again.
*/
pjdlog_debug(1,
"Unable to open \"%s/%s\", most likely renamed in the meantime, retrying.",
trail->tr_dirname, curfile);
} else {
/*
* We were unable to open the file, but not because of
* the above. This shouldn't happen, but it did.
* We don't know why it happen, so the best we can do
* is to just skip this file - this is why we copy the
* name, so we can start and the next entry.
*/
PJDLOG_VERIFY(strlcpy(trail->tr_filename, curfile,
sizeof(trail->tr_filename)) <
sizeof(trail->tr_filename));
pjdlog_errno(LOG_ERR,
"Unable to open file \"%s/%s\", skipping",
trail->tr_dirname, curfile);
}
goto again;
}
PJDLOG_VERIFY(strlcpy(trail->tr_filename, curfile,
sizeof(trail->tr_filename)) < sizeof(trail->tr_filename));
pjdlog_debug(1, "Found next trail file: \"%s/%s\".", trail->tr_dirname,
trail->tr_filename);
}