Debian Codegraph @

src/launch/service.c

raw
1 /*
2 * D-Bus Service
3 *
4 * A D-Bus service represents the connection between a DBus name and a systemd
5 * service.
6 *
7 * Calling add or remove on a service adds or removes an activatable name in
8 * the broker. Calling activate on a service starts the corresponding systemd
9 * service.
10 *
11 * D-Bus services can specify a systemd service to start, or alternatively the
12 * executable to be started. In the latter case, a transient unit is generated
13 * based on the information in the D-Bus service.
14 */
15
16 #include <c-rbtree.h>
17 #include <c-stdaux.h>
18 #include <stdlib.h>
19 #include <systemd/sd-bus.h>
20 #include "broker/controller.h"
21 #include "catalog/catalog-ids.h"
22 #include "launch/service.h"
23 #include "util/error.h"
24 #include "util/log.h"
25 #include "util/systemd.h"
26
27 static void log_append_bus_error(Log *log, const sd_bus_error *error) {
28 log_appendf(log, "DBUS_BROKER_LAUNCH_BUS_ERROR_NAME=%s\n", error->name);
29 log_appendf(log, "DBUS_BROKER_LAUNCH_BUS_ERROR_MESSAGE=%s\n", error->message);
30 }
31
32 static void log_append_service_path(Log *log, const char *path) {
33 if (path)
34 log_appendf(log, "DBUS_BROKER_LAUNCH_SERVICE_PATH=%s\n", path);
35 }
36
37 static void log_append_service_name(Log *log, const char *name) {
38 if (name)
39 log_appendf(log, "DBUS_BROKER_LAUNCH_SERVICE_NAME=%s\n", name);
40 }
41
42 static void log_append_service_unit(Log *log, const char *unit) {
43 if (unit)
44 log_appendf(log, "DBUS_BROKER_LAUNCH_SERVICE_UNIT=%s\n", unit);
45 }
46
47 static void log_append_service_user(Log *log, const char *user) {
48 if (user)
49 log_appendf(log, "DBUS_BROKER_LAUNCH_SERVICE_USER=%s\n", user);
50 }
51
52 static void log_append_service(Log *log, Service *service) {
53 log_append_service_name(log, service->name);
54
55 if (service->active_data) {
56 log_append_service_path(log, service->active_data->path);
57 log_append_service_unit(log, service->active_data->unit);
58 log_append_service_user(log, service->active_data->user);
59 log_appendf(
60 log,
61 "DBUS_BROKER_LAUNCH_SERVICE_UID=%"PRIu32"\n",
62 service->active_data->uid
63 );
64 for (size_t i = 0; i < service->active_data->argc; ++i)
65 log_appendf(
66 log,
67 "DBUS_BROKER_LAUNCH_ARG%zu=%s\n",
68 i,
69 service->active_data->argv[i]
70 );
71 } else {
72 log_append_service_path(log, service->data->path);
73 log_append_service_unit(log, service->data->unit);
74 log_append_service_user(log, service->data->user);
75 log_appendf(
76 log,
77 "DBUS_BROKER_LAUNCH_SERVICE_UID=%"PRIu32"\n",
78 service->data->uid
79 );
80 for (size_t i = 0; i < service->data->argc; ++i)
81 log_appendf(
82 log,
83 "DBUS_BROKER_LAUNCH_ARG%zu=%s\n",
84 i,
85 service->data->argv[i]
86 );
87 }
88
89 log_appendf(
90 log,
91 "DBUS_BROKER_LAUNCH_SERVICE_INSTANCE=%"PRIu64"\n",
92 service->instance
93 );
94 log_appendf(
95 log,
96 "DBUS_BROKER_LAUNCH_SERVICE_ID=%s\n",
97 service->id
98 );
99 }
100
101 int service_new(Service **servicep,
102 Launcher *launcher,
103 const char *name,
104 CRBNode **slot_by_name,
105 CRBNode *parent_by_name,
106 const char *path,
107 const char *unit,
108 size_t argc,
109 char **argv,
110 const char *user,
111 uid_t uid) {
112 _c_cleanup_(service_freep) Service *service = NULL;
113 CRBNode **slot, *parent;
114 int r;
115
116 service = calloc(1, sizeof(*service) + C_DECIMAL_MAX(uint64_t) + 1);
117 if (!service)
118 return error_origin(-ENOMEM);
119
120 service->launcher = launcher;
121 service->rb = (CRBNode)C_RBNODE_INIT(service->rb);
122 service->rb_by_name = (CRBNode)C_RBNODE_INIT(service->rb_by_name);
123 sprintf(service->id, "%" PRIu64, ++launcher->service_ids);
124
125 service->name = strdup(name);
126 if (!service->name)
127 return error_origin(-ENOMEM);
128
129 r = service_data_new(&service->data, path, unit, user, uid, argc, argv);
130 if (r)
131 return error_fold(r);
132
133 slot = c_rbtree_find_slot(&launcher->services, service_compare, service->id, &parent);
134 c_assert(slot);
135 c_rbtree_add(&launcher->services, parent, slot, &service->rb);
136 c_rbtree_add(&launcher->services_by_name, parent_by_name, slot_by_name, &service->rb_by_name);
137
138 *servicep = service;
139 service = NULL;
140 return 0;
141 }
142
143 Service *service_free(Service *service) {
144 if (!service)
145 return NULL;
146
147 c_rbnode_unlink(&service->rb_by_name);
148 c_rbnode_unlink(&service->rb);
149
150 service_data_free(service->data);
151 free(service->name);
152
153 free(service->job);
154 free(service->active_unit);
155 service_data_free(service->active_data);
156
157 service->slot_start_unit = sd_bus_slot_unref(service->slot_start_unit);
158 service->slot_watch_unit = sd_bus_slot_unref(service->slot_watch_unit);
159 service->slot_watch_jobs = sd_bus_slot_unref(service->slot_watch_jobs);
160 free(service);
161
162 return NULL;
163 }
164
165 int service_update(Service *service, const char *path, const char *unit, size_t argc, char **argv, const char *user, uid_t uid) {
166 ServiceData *data;
167 int r;
168
169 r = service_data_new(&data, path, unit, user, uid, argc, argv);
170 if (r)
171 return error_fold(r);
172
173 service_data_free(service->data);
174 service->data = data;
175 return 0;
176 }
177
178 int service_compare(CRBTree *t, void *k, CRBNode *n) {
179 Service *service = c_container_of(n, Service, rb);
180
181 return strcmp(k, service->id);
182 }
183
184 int service_compare_by_name(CRBTree *t, void *k, CRBNode *n) {
185 Service *service = c_container_of(n, Service, rb_by_name);
186
187 return strcmp(k, service->name);
188 }
189
190 static void service_discard_activation(Service *service) {
191 service->job = c_free(service->job);
192 service->active_unit = c_free(service->active_unit);
193 service->active_data = service_data_free(service->active_data);
194 service->is_transient = false;
195 service->slot_start_unit = sd_bus_slot_unref(service->slot_start_unit);
196 service->slot_watch_unit = sd_bus_slot_unref(service->slot_watch_unit);
197 service->slot_watch_jobs = sd_bus_slot_unref(service->slot_watch_jobs);
198 }
199
200 static int service_reset_activation(Service *service, unsigned int name_error) {
201 _c_cleanup_(c_freep) char *object_path = NULL;
202 int r;
203
204 service_discard_activation(service);
205
206 r = asprintf(&object_path, "/org/bus1/DBus/Name/%s", service->id);
207 if (r < 0)
208 return error_origin(-errno);
209
210 r = sd_bus_call_method(service->launcher->bus_controller,
211 NULL,
212 object_path,
213 "org.bus1.DBus.Name",
214 "Reset",
215 NULL,
216 NULL,
217 "ts",
218 service->last_serial,
219 controller_name_error_to_string(name_error));
220 if (r < 0)
221 return error_origin(r);
222
223 return 0;
224 }
225
226 static int service_watch_jobs_handler(sd_bus_message *message, void *userdata, sd_bus_error *errorp) {
227 Service *service = userdata;
228 const char *path = NULL, *unit = NULL, *result = NULL;
229 uint32_t id;
230 int r;
231
232 /*
233 * Whenever we have an activation job queued, we want to know when it
234 * is done. We get a `JobRemoved` signal from systemd, which includes
235 * the reason why the job is done.
236 * We get those signals for all jobs, since we cannot know a job-id to
237 * match for before we create job. This is quite unfortunate, but
238 * little we can do about it now.
239 *
240 * We require this signal merely to know when systemd finished handling
241 * a job. For properly configured services, this should already tell us
242 * whether the startup was successful or failed. But for basic services
243 * that use no notify or dbus systemd-startup handling, we have to
244 * continue tracking their `ActiveState` to see whether they failed.
245 */
246
247 r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result);
248 if (r < 0)
249 return error_origin(r);
250
251 if (!service->job || strcmp(path, service->job))
252 return 0;
253
254 if (!strcmp(result, "done") || !strcmp(result, "skipped")) {
255 /*
256 * Our job completed successfully. Make sure to stop watching
257 * it so the `ActiveState` handling will take effect.
258 */
259 service->job = c_free(service->job);
260 service->slot_watch_jobs = sd_bus_slot_unref(service->slot_watch_jobs);
261 } else {
262 /*
263 * Our job failed. Forward this information to the broker so it
264 * can fail pending activations.
265 */
266 r = service_reset_activation(
267 service,
268 !strcmp(result, "skipped")
269 ? CONTROLLER_NAME_ERROR_STARTUP_SKIPPED
270 : CONTROLLER_NAME_ERROR_STARTUP_FAILURE
271 );
272 if (r)
273 return error_trace(r);
274 }
275
276 return 0;
277 }
278
279 static int service_watch_jobs(Service *service) {
280 int r;
281
282 assert(!service->slot_watch_jobs);
283 assert(!service->job);
284
285 r = sd_bus_match_signal_async(
286 service->launcher->bus_regular,
287 &service->slot_watch_jobs,
288 "org.freedesktop.systemd1",
289 "/org/freedesktop/systemd1",
290 "org.freedesktop.systemd1.Manager",
291 "JobRemoved",
292 service_watch_jobs_handler,
293 NULL,
294 service
295 );
296 if (r < 0)
297 return error_origin(r);
298
299 return 0;
300 }
301
302 static int service_start_unit_handler(sd_bus_message *message, void *userdata, sd_bus_error *errorp) {
303 Service *service = userdata;
304 Launcher *launcher = service->launcher;
305 unsigned int name_error;
306 const sd_bus_error *error;
307 const char *job;
308 int r;
309
310 service->slot_start_unit = sd_bus_slot_unref(service->slot_start_unit);
311
312 error = sd_bus_message_get_error(message);
313 if (!error) {
314 /*
315 * We successfully queued the job. Now remember the job path,
316 * so we can properly track when it finishes (via the
317 * JobRemoved signal).
318 */
319
320 assert(!service->job);
321
322 r = sd_bus_message_read(message, "o", &job);
323 if (r < 0)
324 return error_origin(r);
325
326 service->job = strdup(job);
327 if (!service->job)
328 return error_origin(-ENOMEM);
329
330 return 1;
331 }
332
333 /*
334 * We always forward activation failure to the broker, which then
335 * forwards it as error reply to all pending messages on that
336 * activation. We augment this with a detailed error message in all
337 * cases where we consider the error non-recoverable. In case of
338 * recoverable situations, we want to stay silent and simply forward
339 * the information to the sender of the activation message.
340 */
341 if (strcmp(error->name, "org.freedesktop.systemd1.TransactionIsDestructive") != 0) {
342 /*
343 * We currently use a whitelist of situations where we consider
344 * the activation failure recoverable. These currently include:
345 *
346 * * `TransactionIsDestructive` from systemd tells us that the
347 * start request was valid, but was denied because a
348 * non-recoverable conflicting stop request is currently
349 * pending. Most common scenario is the service launcher
350 * shutting down, but any systemd-job can theoretically
351 * select this mode.
352 * Since this indicates that our request was valid and
353 * properly configured, we treat this as recoverable error.
354 * * `NoSuchUnit` from systemd tells us that the unit file
355 * was not found. This may indicate that the service was
356 * disabled, which is a supported configuration. In this
357 * case we only log once.
358 * * `UnitMasked` from systemd tells us that the administrator
359 * masked the unit we want to activate. This is again a
360 * valid way to disable a service locally. Similar to
361 * `NoSuchUnit` we warn once and then stay silent.
362 *
363 * In any other situation we log an error message, since these
364 * are non-recoverable and indicate system configuration
365 * errors.
366 */
367 if (strcmp(error->name, "org.freedesktop.systemd1.NoSuchUnit") == 0) {
368 if (!service->n_missing_unit++) {
369 log_append_here(&launcher->log, LOG_WARNING, 0, DBUS_BROKER_CATALOG_ACTIVATE_NO_UNIT);
370 log_append_bus_error(&launcher->log, error);
371 log_append_service(&launcher->log, service);
372
373 r = log_commitf(&launcher->log,
374 "Activation request for '%s' failed: The systemd unit '%s' could not be found.\n",
375 service->name,
376 service->active_unit);
377 if (r)
378 return error_fold(r);
379 }
380
381 name_error = CONTROLLER_NAME_ERROR_UNKNOWN_UNIT;
382 } else if (strcmp(error->name, "org.freedesktop.systemd1.UnitMasked") == 0) {
383 if (!service->n_masked_unit++) {
384 log_append_here(&launcher->log, LOG_NOTICE, 0, DBUS_BROKER_CATALOG_ACTIVATE_MASKED_UNIT);
385 log_append_bus_error(&launcher->log, error);
386 log_append_service(&launcher->log, service);
387
388 r = log_commitf(&launcher->log,
389 "Activation request for '%s' failed: The systemd unit '%s' is masked.\n",
390 service->name,
391 service->active_unit);
392 if (r)
393 return error_fold(r);
394 }
395
396 name_error = CONTROLLER_NAME_ERROR_MASKED_UNIT;
397 } else {
398 log_append_here(&launcher->log, LOG_ERR, 0, NULL);
399 log_append_bus_error(&launcher->log, error);
400 log_append_service(&launcher->log, service);
401
402 r = log_commitf(&launcher->log,
403 "Activation request for '%s' failed.\n",
404 service->name);
405 if (r)
406 return error_fold(r);
407
408 name_error = CONTROLLER_NAME_ERROR_INVALID_UNIT;
409 }
410 } else {
411 name_error = CONTROLLER_NAME_ERROR_DESTRUCTIVE_TRANSACTION;
412 }
413
414 r = service_reset_activation(service, name_error);
415 if (r)
416 return error_trace(r);
417
418 return 1;
419 }
420
421 static int service_start_unit(Service *service) {
422 Launcher *launcher = service->launcher;
423 _c_cleanup_(sd_bus_message_unrefp) sd_bus_message *method_call = NULL;
424 int r;
425
426 r = sd_bus_message_new_method_call(launcher->bus_regular, &method_call,
427 "org.freedesktop.systemd1",
428 "/org/freedesktop/systemd1",
429 "org.freedesktop.systemd1.Manager",
430 "StartUnit");
431 if (r < 0)
432 return error_origin(r);
433
434 r = sd_bus_message_append(method_call, "ss", service->active_unit, "replace");
435 if (r < 0)
436 return error_origin(r);
437
438 r = sd_bus_call_async(launcher->bus_regular, &service->slot_start_unit, method_call, service_start_unit_handler, service, -1);
439 if (r < 0)
440 return error_origin(r);
441
442 return 0;
443 }
444
445 static int service_start_transient_unit(Service *service) {
446 Launcher *launcher = service->launcher;
447 _c_cleanup_(sd_bus_message_unrefp) sd_bus_message *method_call = NULL;
448 int r;
449
450 r = sd_bus_message_new_method_call(launcher->bus_regular, &method_call,
451 "org.freedesktop.systemd1",
452 "/org/freedesktop/systemd1",
453 "org.freedesktop.systemd1.Manager",
454 "StartTransientUnit");
455 if (r < 0)
456 return error_origin(r);
457
458 r = sd_bus_message_append(method_call, "ss", service->active_unit, "replace");
459 if (r < 0)
460 return error_origin(r);
461
462 r = sd_bus_message_open_container(method_call, 'a', "(sv)");
463 if (r < 0)
464 return error_origin(r);
465
466 {
467 r = sd_bus_message_open_container(method_call, 'r', "sv");
468 if (r < 0)
469 return error_origin(r);
470
471 {
472 r = sd_bus_message_append(method_call, "s", "ExecStart");
473 if (r < 0)
474 return error_origin(r);
475
476 r = sd_bus_message_open_container(method_call, 'v', "a(sasb)");
477 if (r < 0)
478 return error_origin(r);
479
480 {
481 r = sd_bus_message_open_container(method_call, 'a', "(sasb)");
482 if (r < 0)
483 return error_origin(r);
484
485 {
486 r = sd_bus_message_open_container(method_call, 'r', "sasb");
487 if (r < 0)
488 return error_origin(r);
489
490 {
491 r = sd_bus_message_append(method_call, "s", service->active_data->argv[0]);
492 if (r < 0)
493 return error_origin(r);
494
495 r = sd_bus_message_open_container(method_call, 'a', "s");
496 if (r < 0)
497 return error_origin(r);
498
499 {
500 for (size_t i = 0; i < service->active_data->argc; ++i) {
501 r = sd_bus_message_append(method_call, "s", service->active_data->argv[i]);
502 if (r < 0)
503 return error_origin(r);
504 }
505 }
506
507 r = sd_bus_message_close_container(method_call);
508 if (r < 0)
509 return error_origin(r);
510
511 r = sd_bus_message_append(method_call, "b", false);
512 if (r < 0)
513 return error_origin(r);
514 }
515
516 r = sd_bus_message_close_container(method_call);
517 if (r < 0)
518 return error_origin(r);
519 }
520
521 r = sd_bus_message_close_container(method_call);
522 if (r < 0)
523 return error_origin(r);
524 }
525
526 r = sd_bus_message_close_container(method_call);
527 if (r < 0)
528 return error_origin(r);
529 }
530
531 r = sd_bus_message_close_container(method_call);
532 if (r < 0)
533 return error_origin(r);
534
535 r = sd_bus_message_open_container(method_call, 'r', "sv");
536 if (r < 0)
537 return error_origin(r);
538
539 {
540 r = sd_bus_message_append(method_call, "s", "KillMode");
541 if (r < 0)
542 return error_origin(r);
543
544 r = sd_bus_message_open_container(method_call, 'v', "s");
545 if (r < 0)
546 return error_origin(r);
547
548 {
549 r = sd_bus_message_append(method_call, "s", "process");
550 if (r < 0)
551 return error_origin(r);
552 }
553
554 r = sd_bus_message_close_container(method_call);
555 if (r < 0)
556 return error_origin(r);
557 }
558
559 r = sd_bus_message_close_container(method_call);
560 if (r < 0)
561 return error_origin(r);
562
563 r = sd_bus_message_open_container(method_call, 'r', "sv");
564 if (r < 0)
565 return error_origin(r);
566
567 {
568 r = sd_bus_message_append(method_call, "s", "CollectMode");
569 if (r < 0)
570 return error_origin(r);
571
572 r = sd_bus_message_open_container(method_call, 'v', "s");
573 if (r < 0)
574 return error_origin(r);
575
576 {
577 r = sd_bus_message_append(method_call, "s", "inactive-or-failed");
578 if (r < 0)
579 return error_origin(r);
580 }
581
582 r = sd_bus_message_close_container(method_call);
583 if (r < 0)
584 return error_origin(r);
585 }
586
587 r = sd_bus_message_close_container(method_call);
588 if (r < 0)
589 return error_origin(r);
590
591 if (service->active_data->user) {
592 /*
593 * Ideally we would unconditionally pass the UID
594 * we are accounting on to systemd to run the service
595 * under. However, in the case of the user instance,
596 * systemd fails to start a transient unit if a user
597 * is provided due to lack of permission. In practice
598 * this works out ok, as in this case we would have
599 * provided our own UID, which is systemd's UID, so
600 * it would ammount to a no-op. It would have been
601 * better if systemd could detect this case and not
602 * fail, but in practice this is perfectly fine.
603 */
604 r = sd_bus_message_open_container(method_call, 'r', "sv");
605 if (r < 0)
606 return error_origin(r);
607
608 {
609 r = sd_bus_message_append(method_call, "s", "User");
610 if (r < 0)
611 return error_origin(r);
612
613 r = sd_bus_message_open_container(method_call, 'v', "s");
614 if (r < 0)
615 return error_origin(r);
616
617 {
618 _c_cleanup_(c_freep) char *uid = NULL;
619
620 /*
621 * Pass the UID we parsed, rather than the
622 * original username. This should resolve
623 * to the same, but out of an abundance of
624 * caution, we try to avoid any
625 * inconsistencies.
626 */
627 r = asprintf(&uid, "%"PRIu32, service->active_data->uid);
628 if (r < 0)
629 return error_origin(-errno);
630
631 r = sd_bus_message_append(method_call, "s", uid);
632 if (r < 0)
633 return error_origin(r);
634 }
635
636 r = sd_bus_message_close_container(method_call);
637 if (r < 0)
638 return error_origin(r);
639 }
640
641 r = sd_bus_message_close_container(method_call);
642 if (r < 0)
643 return error_origin(r);
644 }
645 }
646
647 r = sd_bus_message_close_container(method_call);
648 if (r < 0)
649 return error_origin(r);
650
651 r = sd_bus_message_append(method_call, "a(sa(sv))", 0);
652 if (r < 0)
653 return error_origin(r);
654
655 r = sd_bus_call_async(launcher->bus_regular, &service->slot_start_unit, method_call, service_start_unit_handler, service, -1);
656 if (r < 0)
657 return error_origin(r);
658
659 return 0;
660 }
661
662 static int service_watch_unit_handler(sd_bus_message *message, void *userdata, sd_bus_error *errorp) {
663 Service *service = userdata;
664 const char *interface = NULL, *property = NULL, *value = NULL;
665 int r, condition_result = 1;
666
667 /*
668 * If we still watch the job-signals it means systemd has not yet fully
669 * finished our job. In this case, any errors will be caught by the
670 * `JobRemoved` handler and we do not have to track the unit, yet.
671 * Moreover, we must not track the unit, since it might still return
672 * failures before our job was actually handled by systemd.
673 *
674 * Hence, simply ignore any PropertiesChanged signals until we got
675 * confirmation by systemd that our job finished. Bus ordering will
676 * guarantee that we catch unit-failures both before and after the job
677 * completion.
678 */
679 if (service->slot_watch_jobs)
680 return 0;
681
682 /*
683 * The properties of the bus unit changed. We are only interested in
684 * the "ActiveState" and "ConditionResult" properties. We check whether
685 * it is included in the payload. If not, we ignore the signal.
686 *
687 * Note that we rely on systemd including it with value in the signal.
688 * We will not query it, if it was merely invalidated. This is a
689 * systemd API guarantee, and we rely on it.
690 */
691
692 /* Parse: "s" */
693 {
694 r = sd_bus_message_read(message, "s", &interface);
695 if (r < 0)
696 return error_origin(r);
697 }
698
699 /* We are not interested in properties other than the Unit-Interface */
700 if (strcmp(interface, "org.freedesktop.systemd1.Unit") != 0)
701 return 0;
702
703 /* Parse: "a{sv}" */
704 {
705 r = sd_bus_message_enter_container(message, 'a', "{sv}");
706 if (r < 0)
707 return error_origin(r);
708
709 while (!sd_bus_message_at_end(message, false)) {
710 r = sd_bus_message_enter_container(message, 'e', "sv");
711 if (r < 0)
712 return error_origin(r);
713
714 r = sd_bus_message_read(message, "s", &property);
715 if (r < 0)
716 return error_origin(r);
717
718 if (!strcmp(property, "ActiveState")) {
719 r = sd_bus_message_enter_container(message, 'v', "s");
720 if (r < 0)
721 return error_origin(r);
722
723 r = sd_bus_message_read(message, "s", &value);
724 if (r < 0)
725 return error_origin(r);
726
727 r = sd_bus_message_exit_container(message);
728 if (r < 0)
729 return error_origin(r);
730 } else if (!strcmp(property, "ConditionResult")) {
731 r = sd_bus_message_enter_container(message, 'v', "b");
732 if (r < 0)
733 return error_origin(r);
734
735 r = sd_bus_message_read(message, "b", &condition_result);
736 if (r < 0)
737 return error_origin(r);
738
739 r = sd_bus_message_exit_container(message);
740 if (r < 0)
741 return error_origin(r);
742 } else {
743 r = sd_bus_message_skip(message, "v");
744 if (r < 0)
745 return error_origin(r);
746 }
747
748 r = sd_bus_message_exit_container(message);
749 if (r < 0)
750 return error_origin(r);
751 }
752
753 r = sd_bus_message_exit_container(message);
754 if (r < 0)
755 return error_origin(r);
756 }
757
758 /*
759 * The possible values of "ActiveState" are:
760 *
761 * active, reloading, inactive, failed, activating, deactivating
762 *
763 * We are never interested in positive results, because the broker
764 * already gets those by tracking the name to be acquired. Therefore,
765 * we only ever track negative results. This means we only ever react
766 * to "failed".
767 * We could also react to units entering "inactive", but we cannot know
768 * upfront whether the unit is just a oneshot unit and thus is expected
769 * to enter "inactive" when it finished. Hence, we simply require
770 * anything to explicitly fail if they want to reset the activation.
771 *
772 * Additionally, we also check for "ConditionResult". If false, systemd
773 * did not start the unit due to unsatisfied conditions, however, it
774 * still considers the job a success. We need to consider it a failure,
775 * though, as the job will never end up claiming its name.
776 */
777 if ((value && !strcmp(value, "failed")) || !condition_result) {
778 r = service_reset_activation(service, CONTROLLER_NAME_ERROR_UNIT_FAILURE);
779 if (r)
780 return error_trace(r);
781 }
782
783 return 0;
784 }
785
786 static int service_watch_unit_load_handler(sd_bus_message *message, void *userdata, sd_bus_error *errorp) {
787 Service *service = userdata;
788 const char *object_path;
789 int r;
790
791 service->slot_watch_unit = sd_bus_slot_unref(service->slot_watch_unit);
792
793 if (sd_bus_message_get_error(message)) {
794 service_reset_activation(service, CONTROLLER_NAME_ERROR_UNIT_FAILURE);
795 return 1;
796 }
797
798 r = sd_bus_message_read(message, "o", &object_path);
799 if (r < 0)
800 return error_origin(r);
801
802 r = sd_bus_match_signal_async(
803 service->launcher->bus_regular,
804 &service->slot_watch_unit,
805 "org.freedesktop.systemd1",
806 object_path,
807 "org.freedesktop.DBus.Properties",
808 "PropertiesChanged",
809 service_watch_unit_handler,
810 NULL,
811 service
812 );
813 if (r < 0)
814 return error_origin(r);
815
816 /*
817 * With the correct `PropertiesChanged` match installed, we can start
818 * the service. For classic activation, we use transient units. For
819 * systemd activation, we just spawn the specified unit.
820 */
821
822 if (service->is_transient)
823 r = service_start_transient_unit(service);
824 else
825 r = service_start_unit(service);
826 if (r)
827 return error_fold(r);
828
829 return 1;
830 }
831
832 static int service_watch_unit(Service *service, const char *unit) {
833 int r;
834
835 assert(!service->slot_watch_unit);
836
837 /*
838 * We first fetch the object-path for the unit in question, then we
839 * install a watch-handler for its properties. We re-use the
840 * `slot_watch_unit` slot for both operations.
841 * By fetching the object-path first, we resolve possible aliases and
842 * avoid hard-coding the object-path translation of systemd.
843 *
844 * XXX: Changes to the unit that cause changes to the object-path while
845 * we use it will likely cause us to watch the wrong signals. We
846 * accept this for now as it can be argued to be a
847 * misconfiguration. But a proper fix would be nice in the future.
848 */
849
850 r = sd_bus_call_method_async(
851 service->launcher->bus_regular,
852 &service->slot_watch_unit,
853 "org.freedesktop.systemd1",
854 "/org/freedesktop/systemd1",
855 "org.freedesktop.systemd1.Manager",
856 "LoadUnit",
857 service_watch_unit_load_handler,
858 service,
859 "s",
860 unit
861 );
862 if (r < 0)
863 return error_origin(r);
864
865 return 0;
866 }
867
868 /**
869 * service_activate() - trigger a service activation
870 * @service: service to activate
871 * @serial: activation serial number
872 *
873 * This activates the specified service. Any previous activation is discarded
874 * silently. The new activation replaces a possible old one.
875 *
876 * An activation starts the systemd unit that the dbus-service-file configured.
877 * In case no unit was specified, a transient unit is created, which spawns the
878 * executable specified in the dbus-service-file.
879 *
880 * The launcher never tracks successfull activations. It is up to the broker to
881 * consider an activation successful, once the corresponding bus-name is
882 * claimed. Furthermore, the broker is free to consider an activation failed at
883 * any point in time, without notifying the launcher. There is no need to
884 * cancel the activation in the launcher.
885 *
886 * The role of the launcher is merely to start the right units on request, and
887 * track whenever those fail. If they fail, the activation is discarded and the
888 * failure is forwarded to the broker. However, it is not the job of the
889 * launcher to tell whether an activation succeeded.
890 *
891 * Long story short, this function triggers an activation and tracks the status
892 * of the activation until it fails. If it fails, the information is forwarded
893 * to the broker and the activation is discarded. If it does not fail, it
894 * continues tracking the activation until the entire service object is removed
895 * (see service_remove()).
896 *
897 * In all cases this does not mean that there is a matching activation object
898 * in the broker. The broker activation can have a completely different
899 * lifetime than this activation in the launcher.
900 *
901 * Returns: 0 on success, negative error code on failure.
902 */
903 int service_activate(Service *service, uint64_t serial) {
904 _c_cleanup_(c_freep) char *escaped_name = NULL;
905 Launcher *launcher = service->launcher;
906 const char *unique_name;
907 int r;
908
909 service_discard_activation(service);
910 service->last_serial = serial;
911
912 r = service_data_duplicate(service->data, &service->active_data);
913 if (r)
914 return error_fold(r);
915
916 if (!strcmp(service->name, "org.freedesktop.systemd1")) {
917 /*
918 * systemd activation requests are silently ignored.
919 * In the future this special-case can be dropped
920 * once systemd ships a service file without an
921 * Exec directive.
922 */
923 return 0;
924 }
925
926 c_assert(service->running);
927 c_assert(!service->active_unit);
928
929 if (service->active_data->unit) {
930 service->active_unit = strdup(service->active_data->unit);
931 if (!service->active_unit)
932 return error_origin(-ENOMEM);
933 } else if (service->active_data->argc > 0) {
934 r = sd_bus_get_unique_name(launcher->bus_regular, &unique_name);
935 if (r < 0)
936 return error_origin(r);
937
938 r = systemd_escape_unit(&escaped_name, service->name);
939 if (r)
940 return error_fold(r);
941
942 r = asprintf(
943 &service->active_unit,
944 "dbus-%s-%s@%"PRIu64".service",
945 unique_name,
946 escaped_name,
947 service->instance++
948 );
949 if (r < 0)
950 return error_origin(-errno);
951
952 service->is_transient = true;
953 } else {
954 /*
955 * If no unit-file, nor any command-line is specified, we
956 * expect the service to self-activate. This is an extension
957 * over the reference-implementation, which refuses to load
958 * such service files.
959 * However, this is very handy for services like PID1, or other
960 * auto-start services, which we know will appear on the bus at
961 * some point, but don't need to be triggered. They can now
962 * provide service-files and be available on the bus right from
963 * the beginning, without requiring activation from us.
964 * Technically, you could achieve the same with `/bin/true` as
965 * command, but being explicit is always preferred.
966 */
967 }
968
969 if (service->active_unit) {
970 r = service_watch_jobs(service);
971 if (r)
972 return error_fold(r);
973
974 r = service_watch_unit(service, service->active_unit);
975 if (r)
976 return error_fold(r);
977
978 /*
979 * Actual start of the service unit is delayed until all the
980 * correct D-Bus matches are configured.
981 */
982 }
983
984 return 0;
985 }
986
987 int service_add(Service *service) {
988 Launcher *launcher = service->launcher;
989 _c_cleanup_(c_freep) char *object_path = NULL;
990 int r;
991
992 if (service->running)
993 return 0;
994
995 r = asprintf(&object_path, "/org/bus1/DBus/Name/%s", service->id);
996 if (r < 0)
997 return error_origin(-ENOMEM);
998
999 r = sd_bus_call_method(launcher->bus_controller,
1000 NULL,
1001 "/org/bus1/DBus/Broker",
1002 "org.bus1.DBus.Broker",
1003 "AddName",
1004 NULL,
1005 NULL,
1006 "osu",
1007 object_path,
1008 service->name,
1009 service->data->uid);
1010 if (r < 0)
1011 return error_origin(r);
1012
1013 service->running = true;
1014 return 0;
1015 }
1016
1017 int service_remove(Service *service) {
1018 Launcher *launcher = service->launcher;
1019 _c_cleanup_(c_freep) char *object_path = NULL;
1020 int r;
1021
1022 if (!service->running)
1023 return 0;
1024
1025 service_discard_activation(service);
1026
1027 r = asprintf(&object_path, "/org/bus1/DBus/Name/%s", service->id);
1028 if (r < 0)
1029 return error_origin(-ENOMEM);
1030
1031 r = sd_bus_call_method(launcher->bus_controller,
1032 NULL,
1033 object_path,
1034 "org.bus1.DBus.Name",
1035 "Release",
1036 NULL,
1037 NULL,
1038 "");
1039 if (r < 0)
1040 return error_origin(r);
1041
1042 service->running = false;
1043 return 0;
1044 }
1045
1046 int service_data_new(
1047 ServiceData **datap,
1048 const char *path,
1049 const char *unit,
1050 const char *user,
1051 uid_t uid,
1052 size_t argc,
1053 char **argv
1054 ) {
1055 _c_cleanup_(service_data_freep) ServiceData *data = NULL;
1056 size_t i;
1057
1058 data = calloc(1, sizeof(*data) + argc * sizeof(*argv));
1059 if (!data)
1060 return error_origin(-ENOMEM);
1061
1062 if (path) {
1063 data->path = strdup(path);
1064 if (!data->path)
1065 return error_origin(-ENOMEM);
1066 }
1067
1068 if (unit) {
1069 data->unit = strdup(unit);
1070 if (!data->unit)
1071 return error_origin(-ENOMEM);
1072 }
1073
1074 if (user) {
1075 data->user = strdup(user);
1076 if (!data->user)
1077 return error_origin(-ENOMEM);
1078 }
1079
1080 data->uid = uid;
1081 data->argc = argc;
1082
1083 for (i = 0; i < argc; ++i) {
1084 data->argv[i] = strdup(argv[i]);
1085 if (!data->argv[i])
1086 return error_origin(-ENOMEM);
1087 }
1088
1089 *datap = data;
1090 data = NULL;
1091 return 0;
1092 }
1093
1094 ServiceData *service_data_free(ServiceData *data) {
1095 size_t i;
1096
1097 if (!data)
1098 return NULL;
1099
1100 for (i = 0; i < data->argc; ++i)
1101 c_free(data->argv[i]);
1102
1103 c_free(data->user);
1104 c_free(data->unit);
1105 c_free(data->path);
1106 c_free(data);
1107
1108 return NULL;
1109 }
1110
1111 int service_data_duplicate(ServiceData *data, ServiceData **dupp) {
1112 return service_data_new(
1113 dupp,
1114 data->path,
1115 data->unit,
1116 data->user,
1117 data->uid,
1118 data->argc,
1119 data->argv
1120 );
1121 }