Debian Codegraph @

src/analyze/analyze-unit-shell.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdlib.h>
4 #include <unistd.h>
5
6 #include "sd-bus.h"
7
8 #include "alloc-util.h"
9 #include "analyze.h"
10 #include "analyze-unit-shell.h"
11 #include "bus-error.h"
12 #include "bus-util.h"
13 #include "fd-util.h"
14 #include "log.h"
15 #include "namespace-util.h"
16 #include "pidref.h"
17 #include "process-util.h"
18 #include "runtime-scope.h"
19 #include "strv.h"
20 #include "unit-def.h"
21 #include "unit-name.h"
22
23 int verb_unit_shell(int argc, char *argv[], uintptr_t _data, void *userdata) {
24 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
25 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
26 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
27 _cleanup_free_ char *unit = NULL;
28 int r;
29
30 if (arg_transport != BUS_TRANSPORT_LOCAL)
31 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot spawn a unit shell for a remote service.");
32
33 r = unit_name_mangle_with_suffix(argv[1], "as unit", UNIT_NAME_MANGLE_WARN, ".service", &unit);
34 if (r < 0)
35 return log_error_errno(r, "Failed to mangle name '%s': %m", argv[1]);
36
37 r = acquire_bus(&bus, /* use_full_bus= */ NULL);
38 if (r < 0)
39 return bus_log_connect_error(r, arg_transport, arg_runtime_scope);
40
41 _cleanup_free_ char *object = unit_dbus_path_from_name(unit);
42 if (!object)
43 return log_oom();
44
45 r = sd_bus_get_property(
46 bus,
47 "org.freedesktop.systemd1",
48 object,
49 "org.freedesktop.systemd1.Service",
50 "MainPID",
51 &error,
52 &reply,
53 "u");
54 if (r < 0)
55 return log_error_errno(r, "Failed to get the main PID of %s: %s", unit, bus_error_message(&error, r));
56
57 pid_t pid;
58 r = sd_bus_message_read(reply, "u", &pid);
59 if (r < 0)
60 return log_error_errno(r, "Failed to read the main PID of %s from reply: %m", unit);
61 if (pid == 0)
62 return log_error_errno(SYNTHETIC_ERRNO(ESRCH), "Unit %s has no MainPID (hint: inactive?)", unit);
63
64 _cleanup_close_ int mntns_fd = -EBADF, root_fd = -EBADF, pidns_fd = -EBADF, netns_fd = -EBADF, userns_fd = -EBADF;
65 r = namespace_open(
66 pid,
67 &pidns_fd,
68 &mntns_fd,
69 &netns_fd,
70 &userns_fd,
71 &root_fd);
72 if (r < 0)
73 return log_error_errno(r, "Failed to retrieve FDs of namespaces of %s: %m", unit);
74
75 _cleanup_strv_free_ char **args = NULL;
76 if (argc > 2) {
77 args = strv_copy(strv_skip(argv, 2));
78 if (!args)
79 return log_oom();
80 }
81
82 _cleanup_(pidref_done) PidRef child = PIDREF_NULL;
83 r = namespace_fork(
84 "(unit-shell-ns)",
85 "(unit-shell)",
86 FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGKILL,
87 pidns_fd,
88 mntns_fd,
89 netns_fd,
90 userns_fd,
91 root_fd,
92 &child);
93 if (r < 0)
94 return log_error_errno(r, "Failed to fork and enter the namespace of %s: %m", unit);
95
96 if (r == 0) {
97 if (args) {
98 (void) execvp(args[0], args);
99 log_error_errno(errno, "Failed to execute '%s': %m", *args);
100 } else {
101 (void) execl(DEFAULT_USER_SHELL, "-" DEFAULT_USER_SHELL_NAME, NULL);
102 log_debug_errno(errno, "Failed to execute '" DEFAULT_USER_SHELL "', ignoring: %m");
103
104 if (!streq(DEFAULT_USER_SHELL, "/bin/bash")) {
105 (void) execl("/bin/bash", "-bash", NULL);
106 log_debug_errno(errno, "Failed to execute '/bin/bash', ignoring: %m");
107 }
108
109 if (!streq(DEFAULT_USER_SHELL, "/bin/sh")) {
110 (void) execl("/bin/sh", "-sh", NULL);
111 log_debug_errno(errno, "Failed to execute '/bin/sh', ignoring: %m");
112 }
113
114 log_error_errno(errno, "Failed to execute '" DEFAULT_USER_SHELL "', '/bin/bash', and '/bin/sh': %m");
115 }
116 _exit(EXIT_FAILURE);
117 }
118
119 return pidref_wait_for_terminate_and_check(
120 "(unit-shell)",
121 &child,
122 WAIT_LOG_ABNORMAL|WAIT_LOG_NON_ZERO_EXIT_STATUS);
123 }