sd-journal: fix relative path handling (#38681)

Fixes #38667.
This commit is contained in:
Yu Watanabe
2025-11-13 05:12:00 +09:00
committed by GitHub
2 changed files with 24 additions and 20 deletions

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;

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) {
@@ -1740,7 +1749,7 @@ static int add_file_by_name(
if (!path)
return -ENOMEM;
return add_any_file(j, -1, path);
return add_any_file(j, /* fd = */ -EBADF, path);
}
static int remove_file_by_name(
@@ -2427,7 +2436,7 @@ _public_ int sd_journal_open_files(sd_journal **ret, const char **paths, int fla
return -ENOMEM;
STRV_FOREACH(path, paths) {
r = add_any_file(j, -1, *path);
r = add_any_file(j, /* fd = */ -EBADF, *path);
if (r < 0)
return r;
}
@@ -2514,7 +2523,7 @@ _public_ int sd_journal_open_files_fd(sd_journal **ret, int fds[], unsigned n_fd
if (r < 0)
goto fail;
r = add_any_file(j, fds[i], NULL);
r = add_any_file(j, fds[i], /* path = */ NULL);
if (r < 0)
goto fail;
}