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