]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-journal: resolve paths passed to sd_journal_open_files() and friends 38681/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 21 Aug 2025 20:26:44 +0000 (05:26 +0900)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 12 Nov 2025 14:18:40 +0000 (15:18 +0100)
Fixes #38667.

src/libsystemd/sd-journal/journal-file.c
src/libsystemd/sd-journal/sd-journal.c

index 54658a38d540e93482253c3003dfdb3aa3f2a8b7..fa87c7d52e4262d0037fc39686d04a8b41225cf3 100644 (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;
index 195fdb72f217b8250299770341c4befd19cb1800..d29d1ad1f71c64cf25edb4c9228602c7cd0d7783 100644 (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) {