sd-journal: resolve paths passed to sd_journal_open_files() and friends

Fixes #38667.
This commit is contained in:
Yu Watanabe
2025-11-12 15:18:40 +01:00
committed by Zbigniew Jędrzejewski-Szmek
parent 09811f99f4
commit 733c5c98ad
2 changed files with 21 additions and 17 deletions
+12 -17
View File
@@ -4131,28 +4131,12 @@ int journal_file_open(
.last_direction = _DIRECTION_INVALID,
};
if (fname) {
f->path = strdup(fname);
if (!f->path) {
r = -ENOMEM;
goto fail;
}
} else {
assert(fd >= 0);
/* If we don't know the path, fill in something explanatory and vaguely useful */
if (asprintf(&f->path, "/proc/self/%i", fd) < 0) {
r = -ENOMEM;
goto fail;
}
}
if (f->fd < 0) {
/* We pass O_NONBLOCK here, so that in case somebody pointed us to some character device node or FIFO
* or so, we likely fail quickly than block for long. For regular files O_NONBLOCK has no effect, hence
* it doesn't hurt in that case. */
f->fd = openat_report_new(AT_FDCWD, f->path, f->open_flags|O_CLOEXEC|O_NONBLOCK, f->mode, &newly_created);
f->fd = openat_report_new(AT_FDCWD, fname, f->open_flags|O_CLOEXEC|O_NONBLOCK, f->mode, &newly_created);
if (f->fd < 0) {
r = f->fd;
goto fail;
@@ -4165,12 +4149,23 @@ int journal_file_open(
if (r < 0)
goto fail;
r = fd_get_path(f->fd, &f->path);
if (r < 0)
goto fail;
if (!newly_created) {
r = journal_file_fstat(f);
if (r < 0)
goto fail;
}
} else {
/* If we don't know the path, fill in something explanatory and vaguely useful */
f->path = strdup(fname ?: FORMAT_PROC_FD_PATH(fd));
if (!f->path) {
r = -ENOMEM;
goto fail;
}
r = journal_file_fstat(f);
if (r < 0)
goto fail;
+9
View File
@@ -1580,6 +1580,7 @@ static int add_any_file(
const char *path) {
_cleanup_close_ int our_fd = -EBADF;
_cleanup_free_ char *resolved_path = NULL;
JournalFile *f;
struct stat st;
int r;
@@ -1606,6 +1607,14 @@ static int add_any_file(
r = log_debug_errno(errno, "Failed to turn off O_NONBLOCK for %s: %m", path);
goto error;
}
r = fd_get_path(fd, &resolved_path);
if (r < 0) {
r = log_debug_errno(r, "Failed to resolve path '%s': %m", path);
goto error;
}
path = resolved_path;
}
if (fstat(fd, &st) < 0) {