From: Yu Watanabe Date: Thu, 21 Aug 2025 20:26:44 +0000 (+0900) Subject: sd-journal: resolve paths passed to sd_journal_open_files() and friends X-Git-Tag: v259-rc1~82^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F38681%2Fhead;p=thirdparty%2Fsystemd.git sd-journal: resolve paths passed to sd_journal_open_files() and friends Fixes #38667. --- diff --git a/src/libsystemd/sd-journal/journal-file.c b/src/libsystemd/sd-journal/journal-file.c index 54658a38d54..fa87c7d52e4 100644 --- a/src/libsystemd/sd-journal/journal-file.c +++ b/src/libsystemd/sd-journal/journal-file.c @@ -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; diff --git a/src/libsystemd/sd-journal/sd-journal.c b/src/libsystemd/sd-journal/sd-journal.c index 195fdb72f21..d29d1ad1f71 100644 --- a/src/libsystemd/sd-journal/sd-journal.c +++ b/src/libsystemd/sd-journal/sd-journal.c @@ -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) {