summaryrefslogtreecommitdiffstats
path: root/src/shared/journal-util.c
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2023-07-05 02:53:44 +0200
committerYu Watanabe <watanabe.yu+github@gmail.com>2023-07-05 17:06:25 +0200
commit2ec1fb31e9d1f9b024178b02d4b86c4d35c6ca7d (patch)
tree4a2a86b4398a3d44d3a64bd8be03c5580bd63860 /src/shared/journal-util.c
parentsd-journal: introduce SD_JOURNAL_TAKE_DIRECTORY_FD flag for sd_journal_open_d... (diff)
downloadsystemd-2ec1fb31e9d1f9b024178b02d4b86c4d35c6ca7d.tar.xz
systemd-2ec1fb31e9d1f9b024178b02d4b86c4d35c6ca7d.zip
journal-util: extract journal_open_machine() from journalctl
Diffstat (limited to 'src/shared/journal-util.c')
-rw-r--r--src/shared/journal-util.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/shared/journal-util.c b/src/shared/journal-util.c
index ea396fcc9b..d73d7c47d0 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;
+}