]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
4139c1b2 | 2 | |
ca78ad1d | 3 | #include <fcntl.h> |
0d536673 | 4 | |
b5efdb8a | 5 | #include "alloc-util.h" |
a34ceba6 | 6 | #include "async.h" |
40af3d02 | 7 | #include "bus-get-properties.h" |
07630cea | 8 | #include "dbus-cgroup.h" |
4139c1b2 | 9 | #include "dbus-execute.h" |
4819ff03 | 10 | #include "dbus-kill.h" |
afcfaa69 | 11 | #include "dbus-manager.h" |
3ffd4af2 | 12 | #include "dbus-service.h" |
d2f05617 | 13 | #include "dbus-util.h" |
5e8deb94 | 14 | #include "execute.h" |
d2f05617 | 15 | #include "exit-status.h" |
3ffd4af2 | 16 | #include "fd-util.h" |
0d39fa9c | 17 | #include "fileio.h" |
5e8deb94 LB |
18 | #include "locale-util.h" |
19 | #include "mount-util.h" | |
cd48e23f | 20 | #include "open-file.h" |
d2f05617 | 21 | #include "parse-util.h" |
07630cea | 22 | #include "path-util.h" |
5e8deb94 | 23 | #include "selinux-access.h" |
07630cea | 24 | #include "service.h" |
d2f05617 | 25 | #include "signal-util.h" |
07630cea LP |
26 | #include "string-util.h" |
27 | #include "strv.h" | |
28 | #include "unit.h" | |
718db961 LP |
29 | |
30 | static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_type, service_type, ServiceType); | |
596e4470 | 31 | static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_exit_type, service_exit_type, ServiceExitType); |
718db961 LP |
32 | static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_result, service_result, ServiceResult); |
33 | static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_restart, service_restart, ServiceRestart); | |
87a47f99 | 34 | static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_emergency_action, emergency_action, EmergencyAction); |
19dff691 | 35 | static BUS_DEFINE_PROPERTY_GET2(property_get_notify_access, "s", Service, service_get_notify_access, notify_access_to_string); |
57b33e0c | 36 | static BUS_DEFINE_PROPERTY_GET(property_get_restart_usec_current, "t", Service, service_restart_usec); |
3bf0cb65 | 37 | static BUS_DEFINE_PROPERTY_GET(property_get_timeout_abort_usec, "t", Service, service_timeout_abort_usec); |
4793c310 | 38 | static BUS_DEFINE_PROPERTY_GET(property_get_watchdog_usec, "t", Service, service_get_watchdog_usec); |
bf760801 | 39 | static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_timeout_failure_mode, service_timeout_failure_mode, ServiceTimeoutFailureMode); |
dc653bf4 | 40 | |
cd48e23f RP |
41 | static int property_get_open_files( |
42 | sd_bus *bus, | |
43 | const char *path, | |
44 | const char *interface, | |
45 | const char *property, | |
46 | sd_bus_message *reply, | |
47 | void *userdata, | |
48 | sd_bus_error *error) { | |
49 | ||
50 | OpenFile **open_files = ASSERT_PTR(userdata); | |
51 | int r; | |
52 | ||
53 | assert(bus); | |
54 | assert(reply); | |
55 | ||
56 | r = sd_bus_message_open_container(reply, 'a', "(sst)"); | |
57 | if (r < 0) | |
58 | return r; | |
59 | ||
60 | LIST_FOREACH(open_files, of, *open_files) { | |
61 | r = sd_bus_message_append(reply, "(sst)", of->path, of->fdname, of->flags); | |
62 | if (r < 0) | |
63 | return r; | |
64 | } | |
65 | ||
66 | return sd_bus_message_close_container(reply); | |
67 | } | |
68 | ||
dea700bf YW |
69 | static int property_get_exit_status_set( |
70 | sd_bus *bus, | |
71 | const char *path, | |
72 | const char *interface, | |
73 | const char *property, | |
74 | sd_bus_message *reply, | |
75 | void *userdata, | |
76 | sd_bus_error *error) { | |
77 | ||
99534007 | 78 | const ExitStatusSet *status_set = ASSERT_PTR(userdata); |
23d5dd16 | 79 | unsigned n; |
dea700bf YW |
80 | int r; |
81 | ||
82 | assert(bus); | |
83 | assert(reply); | |
dea700bf YW |
84 | |
85 | r = sd_bus_message_open_container(reply, 'r', "aiai"); | |
86 | if (r < 0) | |
87 | return r; | |
88 | ||
89 | r = sd_bus_message_open_container(reply, 'a', "i"); | |
90 | if (r < 0) | |
91 | return r; | |
92 | ||
90e74a66 | 93 | BITMAP_FOREACH(n, &status_set->status) { |
23d5dd16 | 94 | assert(n < 256); |
dea700bf | 95 | |
23d5dd16 | 96 | r = sd_bus_message_append_basic(reply, 'i', &n); |
dea700bf YW |
97 | if (r < 0) |
98 | return r; | |
99 | } | |
100 | ||
101 | r = sd_bus_message_close_container(reply); | |
102 | if (r < 0) | |
103 | return r; | |
104 | ||
105 | r = sd_bus_message_open_container(reply, 'a', "i"); | |
106 | if (r < 0) | |
107 | return r; | |
108 | ||
90e74a66 | 109 | BITMAP_FOREACH(n, &status_set->signal) { |
dea700bf YW |
110 | const char *str; |
111 | ||
23d5dd16 | 112 | str = signal_to_string(n); |
dea700bf YW |
113 | if (!str) |
114 | continue; | |
115 | ||
23d5dd16 | 116 | r = sd_bus_message_append_basic(reply, 'i', &n); |
dea700bf YW |
117 | if (r < 0) |
118 | return r; | |
119 | } | |
120 | ||
121 | r = sd_bus_message_close_container(reply); | |
122 | if (r < 0) | |
123 | return r; | |
124 | ||
125 | return sd_bus_message_close_container(reply); | |
126 | } | |
127 | ||
af477139 LB |
128 | static int bus_service_method_mount(sd_bus_message *message, void *userdata, sd_bus_error *error, bool is_image) { |
129 | _cleanup_(mount_options_free_allp) MountOptions *options = NULL; | |
5e8deb94 | 130 | const char *dest, *src, *propagate_directory; |
af477139 | 131 | int read_only, make_file_or_directory; |
99534007 | 132 | Unit *u = ASSERT_PTR(userdata); |
5e8deb94 LB |
133 | ExecContext *c; |
134 | pid_t unit_pid; | |
135 | int r; | |
136 | ||
137 | assert(message); | |
5e8deb94 LB |
138 | |
139 | if (!MANAGER_IS_SYSTEM(u->manager)) | |
1b09b81c | 140 | return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Adding bind mounts at runtime is only supported for system managers."); |
5e8deb94 LB |
141 | |
142 | r = mac_selinux_unit_access_check(u, message, "start", error); | |
143 | if (r < 0) | |
144 | return r; | |
145 | ||
146 | r = sd_bus_message_read(message, "ssbb", &src, &dest, &read_only, &make_file_or_directory); | |
147 | if (r < 0) | |
148 | return r; | |
149 | ||
150 | if (!path_is_absolute(src) || !path_is_normalized(src)) | |
1b09b81c | 151 | return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Source path must be absolute and normalized."); |
5e8deb94 | 152 | |
af477139 | 153 | if (!is_image && isempty(dest)) |
5e8deb94 LB |
154 | dest = src; |
155 | else if (!path_is_absolute(dest) || !path_is_normalized(dest)) | |
1b09b81c | 156 | return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Destination path must be absolute and normalized."); |
5e8deb94 | 157 | |
af477139 LB |
158 | if (is_image) { |
159 | r = bus_read_mount_options(message, error, &options, NULL, ""); | |
160 | if (r < 0) | |
161 | return r; | |
162 | } | |
163 | ||
5e8deb94 LB |
164 | r = bus_verify_manage_units_async_full( |
165 | u, | |
af477139 | 166 | is_image ? "mount-image" : "bind-mount", |
5e8deb94 | 167 | CAP_SYS_ADMIN, |
af477139 | 168 | N_("Authentication is required to mount on '$(unit)'."), |
5e8deb94 LB |
169 | true, |
170 | message, | |
171 | error); | |
172 | if (r < 0) | |
173 | return r; | |
174 | if (r == 0) | |
175 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
176 | ||
177 | if (u->type != UNIT_SERVICE) | |
1b09b81c | 178 | return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Unit is not of type .service"); |
5e8deb94 LB |
179 | |
180 | /* If it would be dropped at startup time, return an error. The context should always be available, but | |
181 | * there's an assert in exec_needs_mount_namespace, so double-check just in case. */ | |
182 | c = unit_get_exec_context(u); | |
183 | if (!c) | |
1b09b81c | 184 | return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Cannot access unit execution context"); |
5e8deb94 LB |
185 | if (path_startswith_strv(dest, c->inaccessible_paths)) |
186 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "%s is not accessible to this unit", dest); | |
187 | ||
188 | /* Ensure that the unit was started in a private mount namespace */ | |
189 | if (!exec_needs_mount_namespace(c, NULL, unit_get_exec_runtime(u))) | |
1b09b81c | 190 | return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Unit not running in private mount namespace, cannot activate bind mount"); |
5e8deb94 LB |
191 | |
192 | unit_pid = unit_main_pid(u); | |
193 | if (unit_pid == 0 || !UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u))) | |
1b09b81c | 194 | return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Unit is not running"); |
5e8deb94 LB |
195 | |
196 | propagate_directory = strjoina("/run/systemd/propagate/", u->id); | |
af477139 LB |
197 | if (is_image) |
198 | r = mount_image_in_namespace(unit_pid, | |
199 | propagate_directory, | |
200 | "/run/systemd/incoming/", | |
201 | src, dest, read_only, make_file_or_directory, options); | |
202 | else | |
203 | r = bind_mount_in_namespace(unit_pid, | |
204 | propagate_directory, | |
205 | "/run/systemd/incoming/", | |
206 | src, dest, read_only, make_file_or_directory); | |
5e8deb94 LB |
207 | if (r < 0) |
208 | return sd_bus_error_set_errnof(error, r, "Failed to mount %s on %s in unit's namespace: %m", src, dest); | |
209 | ||
210 | return sd_bus_reply_method_return(message, NULL); | |
211 | } | |
212 | ||
af477139 LB |
213 | int bus_service_method_bind_mount(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
214 | return bus_service_method_mount(message, userdata, error, false); | |
215 | } | |
216 | ||
217 | int bus_service_method_mount_image(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
218 | return bus_service_method_mount(message, userdata, error, true); | |
219 | } | |
220 | ||
718db961 LP |
221 | const sd_bus_vtable bus_service_vtable[] = { |
222 | SD_BUS_VTABLE_START(0), | |
556089dc | 223 | SD_BUS_PROPERTY("Type", "s", property_get_type, offsetof(Service, type), SD_BUS_VTABLE_PROPERTY_CONST), |
596e4470 | 224 | SD_BUS_PROPERTY("ExitType", "s", property_get_exit_type, offsetof(Service, exit_type), SD_BUS_VTABLE_PROPERTY_CONST), |
556089dc LP |
225 | SD_BUS_PROPERTY("Restart", "s", property_get_restart, offsetof(Service, restart), SD_BUS_VTABLE_PROPERTY_CONST), |
226 | SD_BUS_PROPERTY("PIDFile", "s", NULL, offsetof(Service, pid_file), SD_BUS_VTABLE_PROPERTY_CONST), | |
19dff691 | 227 | SD_BUS_PROPERTY("NotifyAccess", "s", property_get_notify_access, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
556089dc | 228 | SD_BUS_PROPERTY("RestartUSec", "t", bus_property_get_usec, offsetof(Service, restart_usec), SD_BUS_VTABLE_PROPERTY_CONST), |
be1adc27 MY |
229 | SD_BUS_PROPERTY("RestartSteps", "u", bus_property_get_unsigned, offsetof(Service, restart_steps), SD_BUS_VTABLE_PROPERTY_CONST), |
230 | SD_BUS_PROPERTY("RestartUSecMax", "t", bus_property_get_usec, offsetof(Service, restart_usec_max), SD_BUS_VTABLE_PROPERTY_CONST), | |
57b33e0c | 231 | SD_BUS_PROPERTY("RestartUSecCurrent", "t", property_get_restart_usec_current, 0, 0), |
556089dc LP |
232 | SD_BUS_PROPERTY("TimeoutStartUSec", "t", bus_property_get_usec, offsetof(Service, timeout_start_usec), SD_BUS_VTABLE_PROPERTY_CONST), |
233 | SD_BUS_PROPERTY("TimeoutStopUSec", "t", bus_property_get_usec, offsetof(Service, timeout_stop_usec), SD_BUS_VTABLE_PROPERTY_CONST), | |
dc653bf4 | 234 | SD_BUS_PROPERTY("TimeoutAbortUSec", "t", property_get_timeout_abort_usec, 0, 0), |
bf760801 JK |
235 | SD_BUS_PROPERTY("TimeoutStartFailureMode", "s", property_get_timeout_failure_mode, offsetof(Service, timeout_start_failure_mode), SD_BUS_VTABLE_PROPERTY_CONST), |
236 | SD_BUS_PROPERTY("TimeoutStopFailureMode", "s", property_get_timeout_failure_mode, offsetof(Service, timeout_stop_failure_mode), SD_BUS_VTABLE_PROPERTY_CONST), | |
36c16a7c | 237 | SD_BUS_PROPERTY("RuntimeMaxUSec", "t", bus_property_get_usec, offsetof(Service, runtime_max_usec), SD_BUS_VTABLE_PROPERTY_CONST), |
5918a933 | 238 | SD_BUS_PROPERTY("RuntimeRandomizedExtraUSec", "t", bus_property_get_usec, offsetof(Service, runtime_rand_extra_usec), SD_BUS_VTABLE_PROPERTY_CONST), |
4793c310 | 239 | SD_BUS_PROPERTY("WatchdogUSec", "t", property_get_watchdog_usec, 0, 0), |
718db961 | 240 | BUS_PROPERTY_DUAL_TIMESTAMP("WatchdogTimestamp", offsetof(Service, watchdog_timestamp), 0), |
899feb72 | 241 | SD_BUS_PROPERTY("PermissionsStartOnly", "b", bus_property_get_bool, offsetof(Service, permissions_start_only), SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN), /* 😷 deprecated */ |
556089dc LP |
242 | SD_BUS_PROPERTY("RootDirectoryStartOnly", "b", bus_property_get_bool, offsetof(Service, root_directory_start_only), SD_BUS_VTABLE_PROPERTY_CONST), |
243 | SD_BUS_PROPERTY("RemainAfterExit", "b", bus_property_get_bool, offsetof(Service, remain_after_exit), SD_BUS_VTABLE_PROPERTY_CONST), | |
244 | SD_BUS_PROPERTY("GuessMainPID", "b", bus_property_get_bool, offsetof(Service, guess_main_pid), SD_BUS_VTABLE_PROPERTY_CONST), | |
dea700bf YW |
245 | SD_BUS_PROPERTY("RestartPreventExitStatus", "(aiai)", property_get_exit_status_set, offsetof(Service, restart_prevent_status), SD_BUS_VTABLE_PROPERTY_CONST), |
246 | SD_BUS_PROPERTY("RestartForceExitStatus", "(aiai)", property_get_exit_status_set, offsetof(Service, restart_force_status), SD_BUS_VTABLE_PROPERTY_CONST), | |
247 | SD_BUS_PROPERTY("SuccessExitStatus", "(aiai)", property_get_exit_status_set, offsetof(Service, success_status), SD_BUS_VTABLE_PROPERTY_CONST), | |
718db961 LP |
248 | SD_BUS_PROPERTY("MainPID", "u", bus_property_get_pid, offsetof(Service, main_pid), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
249 | SD_BUS_PROPERTY("ControlPID", "u", bus_property_get_pid, offsetof(Service, control_pid), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
556089dc | 250 | SD_BUS_PROPERTY("BusName", "s", NULL, offsetof(Service, bus_name), SD_BUS_VTABLE_PROPERTY_CONST), |
d8e023aa LP |
251 | SD_BUS_PROPERTY("FileDescriptorStoreMax", "u", bus_property_get_unsigned, offsetof(Service, n_fd_store_max), SD_BUS_VTABLE_PROPERTY_CONST), |
252 | SD_BUS_PROPERTY("NFileDescriptorStore", "u", bus_property_get_unsigned, offsetof(Service, n_fd_store), 0), | |
718db961 | 253 | SD_BUS_PROPERTY("StatusText", "s", NULL, offsetof(Service, status_text), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
05156503 | 254 | SD_BUS_PROPERTY("StatusErrno", "i", bus_property_get_int, offsetof(Service, status_errno), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
718db961 | 255 | SD_BUS_PROPERTY("Result", "s", property_get_result, offsetof(Service, result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
ebf8d79a | 256 | SD_BUS_PROPERTY("ReloadResult", "s", property_get_result, offsetof(Service, reload_result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
345f3221 | 257 | SD_BUS_PROPERTY("CleanResult", "s", property_get_result, offsetof(Service, clean_result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
b4525804 LP |
258 | SD_BUS_PROPERTY("USBFunctionDescriptors", "s", NULL, offsetof(Service, usb_function_descriptors), SD_BUS_VTABLE_PROPERTY_CONST), |
259 | SD_BUS_PROPERTY("USBFunctionStrings", "s", NULL, offsetof(Service, usb_function_strings), SD_BUS_VTABLE_PROPERTY_CONST), | |
05156503 YW |
260 | SD_BUS_PROPERTY("UID", "u", bus_property_get_uid, offsetof(Unit, ref_uid), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
261 | SD_BUS_PROPERTY("GID", "u", bus_property_get_gid, offsetof(Unit, ref_gid), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
7a0019d3 | 262 | SD_BUS_PROPERTY("NRestarts", "u", bus_property_get_unsigned, offsetof(Service, n_restarts), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
afcfaa69 | 263 | SD_BUS_PROPERTY("OOMPolicy", "s", bus_property_get_oom_policy, offsetof(Service, oom_policy), SD_BUS_VTABLE_PROPERTY_CONST), |
cd48e23f | 264 | SD_BUS_PROPERTY("OpenFile", "a(sst)", property_get_open_files, offsetof(Service, open_files), SD_BUS_VTABLE_PROPERTY_CONST), |
3bd28bf7 | 265 | SD_BUS_PROPERTY("ReloadSignal", "i", bus_property_get_int, offsetof(Service, reload_signal), SD_BUS_VTABLE_PROPERTY_CONST), |
00d9ef85 | 266 | |
718db961 | 267 | BUS_EXEC_STATUS_VTABLE("ExecMain", offsetof(Service, main_exec_status), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
31cd5f63 | 268 | BUS_EXEC_COMMAND_LIST_VTABLE("ExecCondition", offsetof(Service, exec_command[SERVICE_EXEC_CONDITION]), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION), |
898fc00e | 269 | BUS_EXEC_EX_COMMAND_LIST_VTABLE("ExecConditionEx", offsetof(Service, exec_command[SERVICE_EXEC_CONDITION]), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION), |
556089dc | 270 | BUS_EXEC_COMMAND_LIST_VTABLE("ExecStartPre", offsetof(Service, exec_command[SERVICE_EXEC_START_PRE]), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION), |
b3d59367 | 271 | BUS_EXEC_EX_COMMAND_LIST_VTABLE("ExecStartPreEx", offsetof(Service, exec_command[SERVICE_EXEC_START_PRE]), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION), |
556089dc | 272 | BUS_EXEC_COMMAND_LIST_VTABLE("ExecStart", offsetof(Service, exec_command[SERVICE_EXEC_START]), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION), |
b3d59367 | 273 | BUS_EXEC_EX_COMMAND_LIST_VTABLE("ExecStartEx", offsetof(Service, exec_command[SERVICE_EXEC_START]), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION), |
556089dc | 274 | BUS_EXEC_COMMAND_LIST_VTABLE("ExecStartPost", offsetof(Service, exec_command[SERVICE_EXEC_START_POST]), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION), |
b3d59367 | 275 | BUS_EXEC_EX_COMMAND_LIST_VTABLE("ExecStartPostEx", offsetof(Service, exec_command[SERVICE_EXEC_START_POST]), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION), |
556089dc | 276 | BUS_EXEC_COMMAND_LIST_VTABLE("ExecReload", offsetof(Service, exec_command[SERVICE_EXEC_RELOAD]), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION), |
898fc00e | 277 | BUS_EXEC_EX_COMMAND_LIST_VTABLE("ExecReloadEx", offsetof(Service, exec_command[SERVICE_EXEC_RELOAD]), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION), |
556089dc | 278 | BUS_EXEC_COMMAND_LIST_VTABLE("ExecStop", offsetof(Service, exec_command[SERVICE_EXEC_STOP]), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION), |
898fc00e | 279 | BUS_EXEC_EX_COMMAND_LIST_VTABLE("ExecStopEx", offsetof(Service, exec_command[SERVICE_EXEC_STOP]), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION), |
556089dc | 280 | BUS_EXEC_COMMAND_LIST_VTABLE("ExecStopPost", offsetof(Service, exec_command[SERVICE_EXEC_STOP_POST]), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION), |
898fc00e | 281 | BUS_EXEC_EX_COMMAND_LIST_VTABLE("ExecStopPostEx", offsetof(Service, exec_command[SERVICE_EXEC_STOP_POST]), SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION), |
51d73fd9 | 282 | |
a008b6d7 | 283 | SD_BUS_METHOD_WITH_ARGS("BindMount", |
284 | SD_BUS_ARGS("s", source, "s", destination, "b", read_only, "b", mkdir), | |
285 | SD_BUS_NO_RESULT, | |
286 | bus_service_method_bind_mount, | |
287 | SD_BUS_VTABLE_UNPRIVILEGED), | |
288 | ||
289 | SD_BUS_METHOD_WITH_ARGS("MountImage", | |
290 | SD_BUS_ARGS("s", source, "s", destination, "b", read_only, "b", mkdir, "a(ss)", options), | |
291 | SD_BUS_NO_RESULT, | |
af477139 LB |
292 | bus_service_method_mount_image, |
293 | SD_BUS_VTABLE_UNPRIVILEGED), | |
294 | ||
51d73fd9 | 295 | /* The following four are obsolete, and thus marked hidden here. They moved into the Unit interface */ |
7bf081a1 ZJS |
296 | SD_BUS_PROPERTY("StartLimitInterval", "t", bus_property_get_usec, offsetof(Unit, start_ratelimit.interval), SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN), |
297 | SD_BUS_PROPERTY("StartLimitBurst", "u", bus_property_get_unsigned, offsetof(Unit, start_ratelimit.burst), SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN), | |
87a47f99 | 298 | SD_BUS_PROPERTY("StartLimitAction", "s", property_get_emergency_action, offsetof(Unit, start_limit_action), SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN), |
53c35a76 | 299 | SD_BUS_PROPERTY("FailureAction", "s", property_get_emergency_action, offsetof(Unit, failure_action), SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN), |
51d73fd9 | 300 | SD_BUS_PROPERTY("RebootArgument", "s", NULL, offsetof(Unit, reboot_arg), SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN), |
718db961 | 301 | SD_BUS_VTABLE_END |
d200735e MS |
302 | }; |
303 | ||
d2f05617 YW |
304 | static int bus_set_transient_exit_status( |
305 | Unit *u, | |
c2756a68 | 306 | const char *name, |
d2f05617 | 307 | ExitStatusSet *status_set, |
718db961 | 308 | sd_bus_message *message, |
2e59b241 | 309 | UnitWriteFlags flags, |
718db961 | 310 | sd_bus_error *error) { |
c2756a68 | 311 | |
a672f4fe | 312 | const int32_t *status, *signal; |
62b21e2e | 313 | size_t n_status, n_signal, i; |
c2756a68 LP |
314 | int r; |
315 | ||
d2f05617 YW |
316 | r = sd_bus_message_enter_container(message, 'r', "aiai"); |
317 | if (r < 0) | |
318 | return r; | |
c2756a68 | 319 | |
62b21e2e | 320 | r = sd_bus_message_read_array(message, 'i', (const void **) &status, &n_status); |
d2f05617 YW |
321 | if (r < 0) |
322 | return r; | |
2e59b241 | 323 | |
62b21e2e | 324 | r = sd_bus_message_read_array(message, 'i', (const void **) &signal, &n_signal); |
d2f05617 YW |
325 | if (r < 0) |
326 | return r; | |
6577c7ce | 327 | |
d2f05617 YW |
328 | r = sd_bus_message_exit_container(message); |
329 | if (r < 0) | |
330 | return r; | |
6577c7ce | 331 | |
62b21e2e ZJS |
332 | n_status /= sizeof(int32_t); |
333 | n_signal /= sizeof(int32_t); | |
a672f4fe | 334 | |
62b21e2e | 335 | if (n_status == 0 && n_signal == 0 && !UNIT_WRITE_FLAGS_NOOP(flags)) { |
d2f05617 YW |
336 | exit_status_set_free(status_set); |
337 | unit_write_settingf(u, flags, name, "%s=", name); | |
6577c7ce | 338 | return 1; |
d2f05617 | 339 | } |
6577c7ce | 340 | |
62b21e2e | 341 | for (i = 0; i < n_status; i++) { |
d2f05617 | 342 | if (status[i] < 0 || status[i] > 255) |
a672f4fe | 343 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid status code in %s: %"PRIi32, name, status[i]); |
c7040b5d | 344 | |
d2f05617 | 345 | if (!UNIT_WRITE_FLAGS_NOOP(flags)) { |
23d5dd16 | 346 | r = bitmap_set(&status_set->status, status[i]); |
d2f05617 YW |
347 | if (r < 0) |
348 | return r; | |
c7040b5d | 349 | |
a672f4fe | 350 | unit_write_settingf(u, flags, name, "%s=%"PRIi32, name, status[i]); |
c7040b5d | 351 | } |
d2f05617 | 352 | } |
c7040b5d | 353 | |
62b21e2e | 354 | for (i = 0; i < n_signal; i++) { |
d2f05617 | 355 | const char *str; |
36c16a7c | 356 | |
a672f4fe | 357 | str = signal_to_string((int) signal[i]); |
d2f05617 | 358 | if (!str) |
a672f4fe | 359 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal in %s: %"PRIi32, name, signal[i]); |
36c16a7c | 360 | |
2e59b241 | 361 | if (!UNIT_WRITE_FLAGS_NOOP(flags)) { |
23d5dd16 | 362 | r = bitmap_set(&status_set->signal, signal[i]); |
d2f05617 YW |
363 | if (r < 0) |
364 | return r; | |
365 | ||
366 | unit_write_settingf(u, flags, name, "%s=%s", name, str); | |
36c16a7c | 367 | } |
d2f05617 | 368 | } |
36c16a7c | 369 | |
d2f05617 YW |
370 | return 1; |
371 | } | |
c7040b5d | 372 | |
d2f05617 YW |
373 | static int bus_set_transient_std_fd( |
374 | Unit *u, | |
375 | const char *name, | |
376 | int *p, | |
377 | bool *b, | |
378 | sd_bus_message *message, | |
379 | UnitWriteFlags flags, | |
380 | sd_bus_error *error) { | |
4ea0d7f4 | 381 | |
d2f05617 | 382 | int fd, r; |
4ea0d7f4 | 383 | |
d2f05617 YW |
384 | assert(p); |
385 | assert(b); | |
4ea0d7f4 | 386 | |
d2f05617 YW |
387 | r = sd_bus_message_read(message, "h", &fd); |
388 | if (r < 0) | |
389 | return r; | |
4ea0d7f4 | 390 | |
d2f05617 YW |
391 | if (!UNIT_WRITE_FLAGS_NOOP(flags)) { |
392 | int copy; | |
4ea0d7f4 | 393 | |
d2f05617 YW |
394 | copy = fcntl(fd, F_DUPFD_CLOEXEC, 3); |
395 | if (copy < 0) | |
396 | return -errno; | |
a34ceba6 | 397 | |
d2f05617 YW |
398 | asynchronous_close(*p); |
399 | *p = copy; | |
400 | *b = true; | |
401 | } | |
a34ceba6 | 402 | |
d2f05617 YW |
403 | return 1; |
404 | } | |
405 | static BUS_DEFINE_SET_TRANSIENT_PARSE(notify_access, NotifyAccess, notify_access_from_string); | |
406 | static BUS_DEFINE_SET_TRANSIENT_PARSE(service_type, ServiceType, service_type_from_string); | |
596e4470 | 407 | static BUS_DEFINE_SET_TRANSIENT_PARSE(service_exit_type, ServiceExitType, service_exit_type_from_string); |
d2f05617 | 408 | static BUS_DEFINE_SET_TRANSIENT_PARSE(service_restart, ServiceRestart, service_restart_from_string); |
afcfaa69 | 409 | static BUS_DEFINE_SET_TRANSIENT_PARSE(oom_policy, OOMPolicy, oom_policy_from_string); |
5453a4b1 | 410 | static BUS_DEFINE_SET_TRANSIENT_STRING_WITH_CHECK(bus_name, sd_bus_service_name_is_valid); |
bf760801 | 411 | static BUS_DEFINE_SET_TRANSIENT_PARSE(timeout_failure_mode, ServiceTimeoutFailureMode, service_timeout_failure_mode_from_string); |
3bd28bf7 | 412 | static BUS_DEFINE_SET_TRANSIENT_TO_STRING(reload_signal, "i", int32_t, int, "%" PRIi32, signal_to_string_with_check); |
a34ceba6 | 413 | |
d2f05617 YW |
414 | static int bus_service_set_transient_property( |
415 | Service *s, | |
416 | const char *name, | |
417 | sd_bus_message *message, | |
418 | UnitWriteFlags flags, | |
419 | sd_bus_error *error) { | |
a34ceba6 | 420 | |
d2f05617 YW |
421 | Unit *u = UNIT(s); |
422 | ServiceExecCommand ci; | |
423 | int r; | |
9efb9df9 | 424 | |
d2f05617 YW |
425 | assert(s); |
426 | assert(name); | |
427 | assert(message); | |
9efb9df9 | 428 | |
d2f05617 | 429 | flags |= UNIT_PRIVATE; |
9efb9df9 | 430 | |
d2f05617 YW |
431 | if (streq(name, "PermissionsStartOnly")) |
432 | return bus_set_transient_bool(u, name, &s->permissions_start_only, message, flags, error); | |
9efb9df9 | 433 | |
d2f05617 YW |
434 | if (streq(name, "RootDirectoryStartOnly")) |
435 | return bus_set_transient_bool(u, name, &s->root_directory_start_only, message, flags, error); | |
9efb9df9 | 436 | |
d2f05617 YW |
437 | if (streq(name, "RemainAfterExit")) |
438 | return bus_set_transient_bool(u, name, &s->remain_after_exit, message, flags, error); | |
9efb9df9 | 439 | |
d2f05617 YW |
440 | if (streq(name, "GuessMainPID")) |
441 | return bus_set_transient_bool(u, name, &s->guess_main_pid, message, flags, error); | |
9efb9df9 | 442 | |
d2f05617 YW |
443 | if (streq(name, "Type")) |
444 | return bus_set_transient_service_type(u, name, &s->type, message, flags, error); | |
9efb9df9 | 445 | |
596e4470 HC |
446 | if (streq(name, "ExitType")) |
447 | return bus_set_transient_service_exit_type(u, name, &s->exit_type, message, flags, error); | |
448 | ||
afcfaa69 LP |
449 | if (streq(name, "OOMPolicy")) |
450 | return bus_set_transient_oom_policy(u, name, &s->oom_policy, message, flags, error); | |
451 | ||
d2f05617 YW |
452 | if (streq(name, "RestartUSec")) |
453 | return bus_set_transient_usec(u, name, &s->restart_usec, message, flags, error); | |
454 | ||
be1adc27 MY |
455 | if (streq(name, "RestartSteps")) |
456 | return bus_set_transient_unsigned(u, name, &s->restart_steps, message, flags, error); | |
457 | ||
458 | if (streq(name, "RestartUSecMax")) | |
459 | return bus_set_transient_usec(u, name, &s->restart_usec_max, message, flags, error); | |
460 | ||
d2f05617 YW |
461 | if (streq(name, "TimeoutStartUSec")) { |
462 | r = bus_set_transient_usec(u, name, &s->timeout_start_usec, message, flags, error); | |
463 | if (r >= 0 && !UNIT_WRITE_FLAGS_NOOP(flags)) | |
464 | s->start_timeout_defined = true; | |
465 | ||
466 | return r; | |
467 | } | |
468 | ||
469 | if (streq(name, "TimeoutStopUSec")) | |
470 | return bus_set_transient_usec(u, name, &s->timeout_stop_usec, message, flags, error); | |
471 | ||
e737017b ZJS |
472 | if (streq(name, "TimeoutAbortUSec")) { |
473 | r = bus_set_transient_usec(u, name, &s->timeout_abort_usec, message, flags, error); | |
474 | if (r >= 0 && !UNIT_WRITE_FLAGS_NOOP(flags)) | |
475 | s->timeout_abort_set = true; | |
476 | return r; | |
477 | } | |
478 | ||
bf760801 JK |
479 | if (streq(name, "TimeoutStartFailureMode")) |
480 | return bus_set_transient_timeout_failure_mode(u, name, &s->timeout_start_failure_mode, message, flags, error); | |
481 | ||
482 | if (streq(name, "TimeoutStopFailureMode")) | |
483 | return bus_set_transient_timeout_failure_mode(u, name, &s->timeout_stop_failure_mode, message, flags, error); | |
484 | ||
d2f05617 YW |
485 | if (streq(name, "RuntimeMaxUSec")) |
486 | return bus_set_transient_usec(u, name, &s->runtime_max_usec, message, flags, error); | |
487 | ||
5918a933 AB |
488 | if (streq(name, "RuntimeRandomizedExtraUSec")) |
489 | return bus_set_transient_usec(u, name, &s->runtime_rand_extra_usec, message, flags, error); | |
490 | ||
d2f05617 YW |
491 | if (streq(name, "WatchdogUSec")) |
492 | return bus_set_transient_usec(u, name, &s->watchdog_usec, message, flags, error); | |
493 | ||
494 | if (streq(name, "FileDescriptorStoreMax")) | |
495 | return bus_set_transient_unsigned(u, name, &s->n_fd_store_max, message, flags, error); | |
496 | ||
497 | if (streq(name, "NotifyAccess")) | |
498 | return bus_set_transient_notify_access(u, name, &s->notify_access, message, flags, error); | |
499 | ||
a9353a5c LP |
500 | if (streq(name, "PIDFile")) { |
501 | _cleanup_free_ char *n = NULL; | |
502 | const char *v, *e; | |
503 | ||
504 | r = sd_bus_message_read(message, "s", &v); | |
505 | if (r < 0) | |
506 | return r; | |
507 | ||
64242fd3 YW |
508 | if (!isempty(v)) { |
509 | n = path_make_absolute(v, u->manager->prefix[EXEC_DIRECTORY_RUNTIME]); | |
510 | if (!n) | |
511 | return -ENOMEM; | |
a9353a5c | 512 | |
4ff361cc | 513 | path_simplify(n); |
a9353a5c | 514 | |
64242fd3 YW |
515 | if (!path_is_normalized(n)) |
516 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "PIDFile= path '%s' is not valid", n); | |
a9353a5c | 517 | |
64242fd3 YW |
518 | e = path_startswith(n, "/var/run/"); |
519 | if (e) { | |
520 | char *z; | |
a9353a5c | 521 | |
657ee2d8 | 522 | z = path_join("/run", e); |
64242fd3 YW |
523 | if (!z) |
524 | return log_oom(); | |
a9353a5c | 525 | |
64242fd3 | 526 | if (!UNIT_WRITE_FLAGS_NOOP(flags)) |
e2341b6b DT |
527 | log_unit_notice(u, "Transient unit's PIDFile= property references path below legacy directory /var/run, updating %s %s %s; please update client accordingly.", |
528 | n, special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), z); | |
a9353a5c | 529 | |
64242fd3 YW |
530 | free_and_replace(n, z); |
531 | } | |
c79d66fc YW |
532 | } |
533 | ||
534 | if (!UNIT_WRITE_FLAGS_NOOP(flags)) { | |
a9353a5c | 535 | free_and_replace(s->pid_file, n); |
64242fd3 | 536 | unit_write_settingf(u, flags, name, "%s=%s", name, strempty(s->pid_file)); |
c79d66fc | 537 | } |
a9353a5c LP |
538 | |
539 | return 1; | |
540 | } | |
d2f05617 YW |
541 | |
542 | if (streq(name, "USBFunctionDescriptors")) | |
543 | return bus_set_transient_path(u, name, &s->usb_function_descriptors, message, flags, error); | |
544 | ||
545 | if (streq(name, "USBFunctionStrings")) | |
546 | return bus_set_transient_path(u, name, &s->usb_function_strings, message, flags, error); | |
547 | ||
548 | if (streq(name, "BusName")) | |
549 | return bus_set_transient_bus_name(u, name, &s->bus_name, message, flags, error); | |
550 | ||
551 | if (streq(name, "Restart")) | |
552 | return bus_set_transient_service_restart(u, name, &s->restart, message, flags, error); | |
553 | ||
554 | if (streq(name, "RestartPreventExitStatus")) | |
555 | return bus_set_transient_exit_status(u, name, &s->restart_prevent_status, message, flags, error); | |
556 | ||
557 | if (streq(name, "RestartForceExitStatus")) | |
558 | return bus_set_transient_exit_status(u, name, &s->restart_force_status, message, flags, error); | |
559 | ||
560 | if (streq(name, "SuccessExitStatus")) | |
561 | return bus_set_transient_exit_status(u, name, &s->success_status, message, flags, error); | |
562 | ||
be6bca47 | 563 | ci = service_exec_command_from_string(name); |
b3d59367 | 564 | ci = (ci >= 0) ? ci : service_exec_ex_command_from_string(name); |
be6bca47 | 565 | if (ci >= 0) |
d2f05617 YW |
566 | return bus_set_transient_exec_command(u, name, &s->exec_command[ci], message, flags, error); |
567 | ||
568 | if (streq(name, "StandardInputFileDescriptor")) | |
569 | return bus_set_transient_std_fd(u, name, &s->stdin_fd, &s->exec_context.stdio_as_fds, message, flags, error); | |
570 | ||
571 | if (streq(name, "StandardOutputFileDescriptor")) | |
572 | return bus_set_transient_std_fd(u, name, &s->stdout_fd, &s->exec_context.stdio_as_fds, message, flags, error); | |
9efb9df9 | 573 | |
d2f05617 YW |
574 | if (streq(name, "StandardErrorFileDescriptor")) |
575 | return bus_set_transient_std_fd(u, name, &s->stderr_fd, &s->exec_context.stdio_as_fds, message, flags, error); | |
c2756a68 | 576 | |
cd48e23f RP |
577 | if (streq(name, "OpenFile")) { |
578 | const char *path, *fdname; | |
579 | uint64_t offlags; | |
580 | ||
581 | r = sd_bus_message_enter_container(message, 'a', "(sst)"); | |
582 | if (r < 0) | |
583 | return r; | |
584 | ||
585 | while ((r = sd_bus_message_read(message, "(sst)", &path, &fdname, &offlags)) > 0) { | |
586 | _cleanup_(open_file_freep) OpenFile *of = NULL; | |
587 | _cleanup_free_ char *ofs = NULL; | |
588 | ||
589 | of = new(OpenFile, 1); | |
590 | if (!of) | |
591 | return -ENOMEM; | |
592 | ||
593 | *of = (OpenFile) { | |
594 | .path = strdup(path), | |
595 | .fdname = strdup(fdname), | |
596 | .flags = offlags, | |
597 | }; | |
598 | ||
599 | if (!of->path || !of->fdname) | |
600 | return -ENOMEM; | |
601 | ||
602 | r = open_file_validate(of); | |
603 | if (r < 0) | |
604 | return r; | |
605 | ||
606 | if (UNIT_WRITE_FLAGS_NOOP(flags)) | |
607 | continue; | |
608 | ||
609 | r = open_file_to_string(of, &ofs); | |
610 | if (r < 0) | |
611 | return sd_bus_error_set_errnof( | |
612 | error, r, "Failed to convert OpenFile= value to string: %m"); | |
613 | ||
614 | LIST_APPEND(open_files, s->open_files, TAKE_PTR(of)); | |
615 | unit_write_settingf(u, flags | UNIT_ESCAPE_SPECIFIERS, name, "OpenFile=%s", ofs); | |
616 | } | |
617 | if (r < 0) | |
618 | return r; | |
619 | ||
620 | r = sd_bus_message_exit_container(message); | |
621 | if (r < 0) | |
622 | return r; | |
623 | ||
624 | return 1; | |
625 | } | |
626 | ||
3bd28bf7 LP |
627 | if (streq(name, "ReloadSignal")) |
628 | return bus_set_transient_reload_signal(u, name, &s->reload_signal, message, flags, error); | |
629 | ||
c2756a68 LP |
630 | return 0; |
631 | } | |
632 | ||
74c964d3 LP |
633 | int bus_service_set_property( |
634 | Unit *u, | |
635 | const char *name, | |
718db961 | 636 | sd_bus_message *message, |
2e59b241 | 637 | UnitWriteFlags flags, |
718db961 | 638 | sd_bus_error *error) { |
74c964d3 LP |
639 | |
640 | Service *s = SERVICE(u); | |
641 | int r; | |
642 | ||
718db961 | 643 | assert(s); |
74c964d3 | 644 | assert(name); |
718db961 | 645 | assert(message); |
74c964d3 | 646 | |
2e59b241 | 647 | r = bus_cgroup_set_property(u, &s->cgroup_context, name, message, flags, error); |
74c964d3 LP |
648 | if (r != 0) |
649 | return r; | |
650 | ||
c2756a68 LP |
651 | if (u->transient && u->load_state == UNIT_STUB) { |
652 | /* This is a transient unit, let's load a little more */ | |
653 | ||
2e59b241 | 654 | r = bus_service_set_transient_property(s, name, message, flags, error); |
c2756a68 LP |
655 | if (r != 0) |
656 | return r; | |
a6c0353b | 657 | |
2e59b241 | 658 | r = bus_exec_context_set_transient_property(u, &s->exec_context, name, message, flags, error); |
c7040b5d LP |
659 | if (r != 0) |
660 | return r; | |
661 | ||
2e59b241 | 662 | r = bus_kill_context_set_transient_property(u, &s->kill_context, name, message, flags, error); |
a6c0353b LP |
663 | if (r != 0) |
664 | return r; | |
c2756a68 LP |
665 | } |
666 | ||
74c964d3 LP |
667 | return 0; |
668 | } | |
669 | ||
670 | int bus_service_commit_properties(Unit *u) { | |
671 | assert(u); | |
672 | ||
673 | unit_realize_cgroup(u); | |
bc432dc7 | 674 | |
74c964d3 LP |
675 | return 0; |
676 | } |