From: Yu Watanabe Date: Wed, 5 Jul 2023 00:53:44 +0000 (+0900) Subject: journal-util: extract journal_open_machine() from journalctl X-Git-Tag: v254-rc1~31^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=2ec1fb31e9d1f9b024178b02d4b86c4d35c6ca7d;p=thirdparty%2Fsystemd.git journal-util: extract journal_open_machine() from journalctl --- diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index 8b36e2bbd21..c70b98a3b1e 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -2348,7 +2348,6 @@ static int run(int argc, char *argv[]) { _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL; _cleanup_(umount_and_freep) char *mounted_dir = NULL; _cleanup_(sd_journal_closep) sd_journal *j = NULL; - _cleanup_close_ int machine_fd = -EBADF; int n_shown, r, poll_fd = -EBADF; setlocale(LC_ALL, ""); @@ -2467,35 +2466,9 @@ static int run(int argc, char *argv[]) { r = sd_journal_open_files_fd(&j, (int[]) { STDIN_FILENO }, 1, 0); else if (arg_file) r = sd_journal_open_files(&j, (const char**) arg_file, 0); - else if (arg_machine) { - _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; - _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; - _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; - int fd; - - if (geteuid() != 0) - /* The file descriptor returned by OpenMachineRootDirectory() will be owned by users/groups of - * the container, thus we need root privileges to override them. */ - return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Using the --machine= switch requires root privileges."); - - r = sd_bus_open_system(&bus); - if (r < 0) - return log_error_errno(r, "Failed to open system bus: %m"); - - r = bus_call_method(bus, bus_machine_mgr, "OpenMachineRootDirectory", &error, &reply, "s", arg_machine); - if (r < 0) - return log_error_errno(r, "Failed to open root directory: %s", bus_error_message(&error, r)); - - r = sd_bus_message_read(reply, "h", &fd); - if (r < 0) - return bus_log_parse_error(r); - - machine_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3); - if (machine_fd < 0) - return log_error_errno(errno, "Failed to duplicate file descriptor: %m"); - - r = sd_journal_open_directory_fd(&j, machine_fd, SD_JOURNAL_OS_ROOT); - } else + else if (arg_machine) + r = journal_open_machine(&j, arg_machine); + else r = sd_journal_open_namespace( &j, arg_namespace, diff --git a/src/shared/journal-util.c b/src/shared/journal-util.c index ea396fcc9ba..d73d7c47d04 100644 --- a/src/shared/journal-util.c +++ b/src/shared/journal-util.c @@ -1,6 +1,10 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "acl-util.h" +#include "bus-error.h" +#include "bus-locator.h" +#include "bus-util.h" +#include "fd-util.h" #include "fs-util.h" #include "hashmap.h" #include "journal-internal.h" @@ -140,3 +144,45 @@ int journal_access_check_and_warn(sd_journal *j, bool quiet, bool want_other_use return r; } + +int journal_open_machine(sd_journal **ret, const char *machine) { + _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; + _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; + _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; + _cleanup_(sd_journal_closep) sd_journal *j = NULL; + _cleanup_close_ int machine_fd = -EBADF; + int fd, r; + + assert(ret); + assert(machine); + + if (geteuid() != 0) + /* The file descriptor returned by OpenMachineRootDirectory() will be owned by users/groups of + * the container, thus we need root privileges to override them. */ + return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Using the --machine= switch requires root privileges."); + + r = sd_bus_open_system(&bus); + if (r < 0) + return log_error_errno(r, "Failed to open system bus: %m"); + + r = bus_call_method(bus, bus_machine_mgr, "OpenMachineRootDirectory", &error, &reply, "s", machine); + if (r < 0) + return log_error_errno(r, "Failed to open root directory of machine '%s': %s", + machine, bus_error_message(&error, r)); + + r = sd_bus_message_read(reply, "h", &fd); + if (r < 0) + return bus_log_parse_error(r); + + machine_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3); + if (machine_fd < 0) + return log_error_errno(errno, "Failed to duplicate file descriptor: %m"); + + r = sd_journal_open_directory_fd(&j, machine_fd, SD_JOURNAL_OS_ROOT | SD_JOURNAL_TAKE_DIRECTORY_FD); + if (r < 0) + return log_error_errno(r, "Failed to open journal in machine '%s': %m", machine); + + TAKE_FD(machine_fd); + *ret = TAKE_PTR(j); + return 0; +} diff --git a/src/shared/journal-util.h b/src/shared/journal-util.h index 86fcba058db..afad249c901 100644 --- a/src/shared/journal-util.h +++ b/src/shared/journal-util.h @@ -8,3 +8,4 @@ int journal_access_blocked(sd_journal *j); int journal_access_check_and_warn(sd_journal *j, bool quiet, bool want_other_users); +int journal_open_machine(sd_journal **ret, const char *machine);