]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journal-util: extract journal_open_machine() from journalctl
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 5 Jul 2023 00:53:44 +0000 (09:53 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 5 Jul 2023 15:06:25 +0000 (00:06 +0900)
src/journal/journalctl.c
src/shared/journal-util.c
src/shared/journal-util.h

index 8b36e2bbd21407054acc5c4bf8252d957fbafe25..c70b98a3b1e287759feba66b021130bb90db9754 100644 (file)
@@ -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,
index ea396fcc9ba21f706ec501f52b99b99e288c59c7..d73d7c47d04c46d0e7558dbc3e508527fa44ee60 100644 (file)
@@ -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;
+}
index 86fcba058dbc285f53d12905a10cb9a6c7ab57c7..afad249c90153157971d00ebde944fe6dae53e40 100644 (file)
@@ -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);