chiark / gitweb /
audit,utmp: implement audit logic and rip utmp stuff out of the main daemon and into...
[elogind.git] / src / unit.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <assert.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <sys/epoll.h>
26 #include <sys/timerfd.h>
27 #include <sys/poll.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <sys/stat.h>
31
32 #include "set.h"
33 #include "unit.h"
34 #include "macro.h"
35 #include "strv.h"
36 #include "load-fragment.h"
37 #include "load-dropin.h"
38 #include "log.h"
39 #include "unit-name.h"
40 #include "specifier.h"
41 #include "dbus-unit.h"
42 #include "special.h"
43 #include "cgroup-util.h"
44 #include "missing.h"
45
46 const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX] = {
47         [UNIT_SERVICE] = &service_vtable,
48         [UNIT_TIMER] = &timer_vtable,
49         [UNIT_SOCKET] = &socket_vtable,
50         [UNIT_TARGET] = &target_vtable,
51         [UNIT_DEVICE] = &device_vtable,
52         [UNIT_MOUNT] = &mount_vtable,
53         [UNIT_AUTOMOUNT] = &automount_vtable,
54         [UNIT_SNAPSHOT] = &snapshot_vtable,
55         [UNIT_SWAP] = &swap_vtable,
56         [UNIT_PATH] = &path_vtable
57 };
58
59 Unit *unit_new(Manager *m) {
60         Unit *u;
61
62         assert(m);
63
64         if (!(u = new0(Unit, 1)))
65                 return NULL;
66
67         if (!(u->meta.names = set_new(string_hash_func, string_compare_func))) {
68                 free(u);
69                 return NULL;
70         }
71
72         u->meta.manager = m;
73         u->meta.type = _UNIT_TYPE_INVALID;
74         u->meta.deserialized_job = _JOB_TYPE_INVALID;
75         u->meta.default_dependencies = true;
76
77         return u;
78 }
79
80 bool unit_has_name(Unit *u, const char *name) {
81         assert(u);
82         assert(name);
83
84         return !!set_get(u->meta.names, (char*) name);
85 }
86
87 int unit_add_name(Unit *u, const char *text) {
88         UnitType t;
89         char *s, *i = NULL;
90         int r;
91
92         assert(u);
93         assert(text);
94
95         if (unit_name_is_template(text)) {
96                 if (!u->meta.instance)
97                         return -EINVAL;
98
99                 s = unit_name_replace_instance(text, u->meta.instance);
100         } else
101                 s = strdup(text);
102
103         if (!s)
104                 return -ENOMEM;
105
106         if (!unit_name_is_valid(s)) {
107                 r = -EINVAL;
108                 goto fail;
109         }
110
111         assert_se((t = unit_name_to_type(s)) >= 0);
112
113         if (u->meta.type != _UNIT_TYPE_INVALID && t != u->meta.type) {
114                 r = -EINVAL;
115                 goto fail;
116         }
117
118         if ((r = unit_name_to_instance(s, &i)) < 0)
119                 goto fail;
120
121         if (i && unit_vtable[t]->no_instances)
122                 goto fail;
123
124         /* Ensure that this unit is either instanced or not instanced,
125          * but not both. */
126         if (u->meta.type != _UNIT_TYPE_INVALID && !u->meta.instance != !i) {
127                 r = -EINVAL;
128                 goto fail;
129         }
130
131         if (unit_vtable[t]->no_alias &&
132             !set_isempty(u->meta.names) &&
133             !set_get(u->meta.names, s)) {
134                 r = -EEXIST;
135                 goto fail;
136         }
137
138         if (hashmap_size(u->meta.manager->units) >= MANAGER_MAX_NAMES) {
139                 r = -E2BIG;
140                 goto fail;
141         }
142
143         if ((r = set_put(u->meta.names, s)) < 0) {
144                 if (r == -EEXIST)
145                         r = 0;
146                 goto fail;
147         }
148
149         if ((r = hashmap_put(u->meta.manager->units, s, u)) < 0) {
150                 set_remove(u->meta.names, s);
151                 goto fail;
152         }
153
154         if (u->meta.type == _UNIT_TYPE_INVALID) {
155
156                 u->meta.type = t;
157                 u->meta.id = s;
158                 u->meta.instance = i;
159
160                 LIST_PREPEND(Meta, units_per_type, u->meta.manager->units_per_type[t], &u->meta);
161
162                 if (UNIT_VTABLE(u)->init)
163                         UNIT_VTABLE(u)->init(u);
164         } else
165                 free(i);
166
167         unit_add_to_dbus_queue(u);
168         return 0;
169
170 fail:
171         free(s);
172         free(i);
173
174         return r;
175 }
176
177 int unit_choose_id(Unit *u, const char *name) {
178         char *s, *t = NULL, *i;
179         int r;
180
181         assert(u);
182         assert(name);
183
184         if (unit_name_is_template(name)) {
185
186                 if (!u->meta.instance)
187                         return -EINVAL;
188
189                 if (!(t = unit_name_replace_instance(name, u->meta.instance)))
190                         return -ENOMEM;
191
192                 name = t;
193         }
194
195         /* Selects one of the names of this unit as the id */
196         s = set_get(u->meta.names, (char*) name);
197         free(t);
198
199         if (!s)
200                 return -ENOENT;
201
202         if ((r = unit_name_to_instance(s, &i)) < 0)
203                 return r;
204
205         u->meta.id = s;
206
207         free(u->meta.instance);
208         u->meta.instance = i;
209
210         unit_add_to_dbus_queue(u);
211
212         return 0;
213 }
214
215 int unit_set_description(Unit *u, const char *description) {
216         char *s;
217
218         assert(u);
219
220         if (!(s = strdup(description)))
221                 return -ENOMEM;
222
223         free(u->meta.description);
224         u->meta.description = s;
225
226         unit_add_to_dbus_queue(u);
227         return 0;
228 }
229
230 bool unit_check_gc(Unit *u) {
231         assert(u);
232
233         if (u->meta.load_state == UNIT_STUB)
234                 return true;
235
236         if (UNIT_VTABLE(u)->no_gc)
237                 return true;
238
239         if (u->meta.job)
240                 return true;
241
242         if (unit_active_state(u) != UNIT_INACTIVE)
243                 return true;
244
245         if (UNIT_VTABLE(u)->check_gc)
246                 if (UNIT_VTABLE(u)->check_gc(u))
247                         return true;
248
249         return false;
250 }
251
252 void unit_add_to_load_queue(Unit *u) {
253         assert(u);
254         assert(u->meta.type != _UNIT_TYPE_INVALID);
255
256         if (u->meta.load_state != UNIT_STUB || u->meta.in_load_queue)
257                 return;
258
259         LIST_PREPEND(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
260         u->meta.in_load_queue = true;
261 }
262
263 void unit_add_to_cleanup_queue(Unit *u) {
264         assert(u);
265
266         if (u->meta.in_cleanup_queue)
267                 return;
268
269         LIST_PREPEND(Meta, cleanup_queue, u->meta.manager->cleanup_queue, &u->meta);
270         u->meta.in_cleanup_queue = true;
271 }
272
273 void unit_add_to_gc_queue(Unit *u) {
274         assert(u);
275
276         if (u->meta.in_gc_queue || u->meta.in_cleanup_queue)
277                 return;
278
279         if (unit_check_gc(u))
280                 return;
281
282         LIST_PREPEND(Meta, gc_queue, u->meta.manager->gc_queue, &u->meta);
283         u->meta.in_gc_queue = true;
284
285         u->meta.manager->n_in_gc_queue ++;
286
287         if (u->meta.manager->gc_queue_timestamp <= 0)
288                 u->meta.manager->gc_queue_timestamp = now(CLOCK_MONOTONIC);
289 }
290
291 void unit_add_to_dbus_queue(Unit *u) {
292         assert(u);
293         assert(u->meta.type != _UNIT_TYPE_INVALID);
294
295         if (u->meta.load_state == UNIT_STUB || u->meta.in_dbus_queue)
296                 return;
297
298         /* Shortcut things if nobody cares */
299         if (!bus_has_subscriber(u->meta.manager)) {
300                 u->meta.sent_dbus_new_signal = true;
301                 return;
302         }
303
304         LIST_PREPEND(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
305         u->meta.in_dbus_queue = true;
306 }
307
308 static void bidi_set_free(Unit *u, Set *s) {
309         Iterator i;
310         Unit *other;
311
312         assert(u);
313
314         /* Frees the set and makes sure we are dropped from the
315          * inverse pointers */
316
317         SET_FOREACH(other, s, i) {
318                 UnitDependency d;
319
320                 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
321                         set_remove(other->meta.dependencies[d], u);
322
323                 unit_add_to_gc_queue(other);
324         }
325
326         set_free(s);
327 }
328
329 void unit_free(Unit *u) {
330         UnitDependency d;
331         Iterator i;
332         char *t;
333
334         assert(u);
335
336         bus_unit_send_removed_signal(u);
337
338         if (u->meta.load_state != UNIT_STUB)
339                 if (UNIT_VTABLE(u)->done)
340                         UNIT_VTABLE(u)->done(u);
341
342         SET_FOREACH(t, u->meta.names, i)
343                 hashmap_remove_value(u->meta.manager->units, t, u);
344
345         if (u->meta.job)
346                 job_free(u->meta.job);
347
348         for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
349                 bidi_set_free(u, u->meta.dependencies[d]);
350
351         if (u->meta.type != _UNIT_TYPE_INVALID)
352                 LIST_REMOVE(Meta, units_per_type, u->meta.manager->units_per_type[u->meta.type], &u->meta);
353
354         if (u->meta.in_load_queue)
355                 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
356
357         if (u->meta.in_dbus_queue)
358                 LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
359
360         if (u->meta.in_cleanup_queue)
361                 LIST_REMOVE(Meta, cleanup_queue, u->meta.manager->cleanup_queue, &u->meta);
362
363         if (u->meta.in_gc_queue) {
364                 LIST_REMOVE(Meta, gc_queue, u->meta.manager->gc_queue, &u->meta);
365                 u->meta.manager->n_in_gc_queue--;
366         }
367
368         cgroup_bonding_free_list(u->meta.cgroup_bondings);
369
370         free(u->meta.description);
371         free(u->meta.fragment_path);
372
373         set_free_free(u->meta.names);
374
375         free(u->meta.instance);
376
377         free(u);
378 }
379
380 UnitActiveState unit_active_state(Unit *u) {
381         assert(u);
382
383         if (u->meta.load_state == UNIT_MERGED)
384                 return unit_active_state(unit_follow_merge(u));
385
386         /* After a reload it might happen that a unit is not correctly
387          * loaded but still has a process around. That's why we won't
388          * shortcut failed loading to UNIT_INACTIVE_MAINTENANCE. */
389
390         return UNIT_VTABLE(u)->active_state(u);
391 }
392
393 const char* unit_sub_state_to_string(Unit *u) {
394         assert(u);
395
396         return UNIT_VTABLE(u)->sub_state_to_string(u);
397 }
398
399 static void complete_move(Set **s, Set **other) {
400         assert(s);
401         assert(other);
402
403         if (!*other)
404                 return;
405
406         if (*s)
407                 set_move(*s, *other);
408         else {
409                 *s = *other;
410                 *other = NULL;
411         }
412 }
413
414 static void merge_names(Unit *u, Unit *other) {
415         char *t;
416         Iterator i;
417
418         assert(u);
419         assert(other);
420
421         complete_move(&u->meta.names, &other->meta.names);
422
423         set_free_free(other->meta.names);
424         other->meta.names = NULL;
425         other->meta.id = NULL;
426
427         SET_FOREACH(t, u->meta.names, i)
428                 assert_se(hashmap_replace(u->meta.manager->units, t, u) == 0);
429 }
430
431 static void merge_dependencies(Unit *u, Unit *other, UnitDependency d) {
432         Iterator i;
433         Unit *back;
434         int r;
435
436         assert(u);
437         assert(other);
438         assert(d < _UNIT_DEPENDENCY_MAX);
439
440         SET_FOREACH(back, other->meta.dependencies[d], i) {
441                 UnitDependency k;
442
443                 for (k = 0; k < _UNIT_DEPENDENCY_MAX; k++)
444                         if ((r = set_remove_and_put(back->meta.dependencies[k], other, u)) < 0) {
445
446                                 if (r == -EEXIST)
447                                         set_remove(back->meta.dependencies[k], other);
448                                 else
449                                         assert(r == -ENOENT);
450                         }
451         }
452
453         complete_move(&u->meta.dependencies[d], &other->meta.dependencies[d]);
454
455         set_free(other->meta.dependencies[d]);
456         other->meta.dependencies[d] = NULL;
457 }
458
459 int unit_merge(Unit *u, Unit *other) {
460         UnitDependency d;
461
462         assert(u);
463         assert(other);
464         assert(u->meta.manager == other->meta.manager);
465         assert(u->meta.type != _UNIT_TYPE_INVALID);
466
467         other = unit_follow_merge(other);
468
469         if (other == u)
470                 return 0;
471
472         if (u->meta.type != other->meta.type)
473                 return -EINVAL;
474
475         if (!u->meta.instance != !other->meta.instance)
476                 return -EINVAL;
477
478         if (other->meta.load_state != UNIT_STUB &&
479             other->meta.load_state != UNIT_FAILED)
480                 return -EEXIST;
481
482         if (other->meta.job)
483                 return -EEXIST;
484
485         if (!UNIT_IS_INACTIVE_OR_MAINTENANCE(unit_active_state(other)))
486                 return -EEXIST;
487
488         /* Merge names */
489         merge_names(u, other);
490
491         /* Merge dependencies */
492         for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
493                 merge_dependencies(u, other, d);
494
495         other->meta.load_state = UNIT_MERGED;
496         other->meta.merged_into = u;
497
498         /* If there is still some data attached to the other node, we
499          * don't need it anymore, and can free it. */
500         if (other->meta.load_state != UNIT_STUB)
501                 if (UNIT_VTABLE(other)->done)
502                         UNIT_VTABLE(other)->done(other);
503
504         unit_add_to_dbus_queue(u);
505         unit_add_to_cleanup_queue(other);
506
507         return 0;
508 }
509
510 int unit_merge_by_name(Unit *u, const char *name) {
511         Unit *other;
512         int r;
513         char *s = NULL;
514
515         assert(u);
516         assert(name);
517
518         if (unit_name_is_template(name)) {
519                 if (!u->meta.instance)
520                         return -EINVAL;
521
522                 if (!(s = unit_name_replace_instance(name, u->meta.instance)))
523                         return -ENOMEM;
524
525                 name = s;
526         }
527
528         if (!(other = manager_get_unit(u->meta.manager, name)))
529                 r = unit_add_name(u, name);
530         else
531                 r = unit_merge(u, other);
532
533         free(s);
534         return r;
535 }
536
537 Unit* unit_follow_merge(Unit *u) {
538         assert(u);
539
540         while (u->meta.load_state == UNIT_MERGED)
541                 assert_se(u = u->meta.merged_into);
542
543         return u;
544 }
545
546 int unit_add_exec_dependencies(Unit *u, ExecContext *c) {
547         int r;
548
549         assert(u);
550         assert(c);
551
552         if (c->std_output != EXEC_OUTPUT_KMSG &&
553             c->std_output != EXEC_OUTPUT_SYSLOG &&
554             c->std_error != EXEC_OUTPUT_KMSG &&
555             c->std_error != EXEC_OUTPUT_SYSLOG)
556                 return 0;
557
558         /* If syslog or kernel logging is requested, make sure our own
559          * logging daemon is run first. */
560
561         if ((r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_LOGGER_SOCKET, NULL, true)) < 0)
562                 return r;
563
564         if (u->meta.manager->running_as == MANAGER_SYSTEM)
565                 if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_LOGGER_SOCKET, NULL, true)) < 0)
566                         return r;
567
568         return 0;
569 }
570
571 const char *unit_description(Unit *u) {
572         assert(u);
573
574         if (u->meta.description)
575                 return u->meta.description;
576
577         return u->meta.id;
578 }
579
580 void unit_dump(Unit *u, FILE *f, const char *prefix) {
581         char *t;
582         UnitDependency d;
583         Iterator i;
584         char *p2;
585         const char *prefix2;
586         CGroupBonding *b;
587         char
588                 timestamp1[FORMAT_TIMESTAMP_MAX],
589                 timestamp2[FORMAT_TIMESTAMP_MAX],
590                 timestamp3[FORMAT_TIMESTAMP_MAX],
591                 timestamp4[FORMAT_TIMESTAMP_MAX],
592                 timespan[FORMAT_TIMESPAN_MAX];
593         Unit *following;
594
595         assert(u);
596         assert(u->meta.type >= 0);
597
598         if (!prefix)
599                 prefix = "";
600         p2 = strappend(prefix, "\t");
601         prefix2 = p2 ? p2 : prefix;
602
603         fprintf(f,
604                 "%s-> Unit %s:\n"
605                 "%s\tDescription: %s\n"
606                 "%s\tInstance: %s\n"
607                 "%s\tUnit Load State: %s\n"
608                 "%s\tUnit Active State: %s\n"
609                 "%s\tInactive Exit Timestamp: %s\n"
610                 "%s\tActive Enter Timestamp: %s\n"
611                 "%s\tActive Exit Timestamp: %s\n"
612                 "%s\tInactive Enter Timestamp: %s\n"
613                 "%s\tGC Check Good: %s\n"
614                 "%s\tNeed Daemon Reload: %s\n",
615                 prefix, u->meta.id,
616                 prefix, unit_description(u),
617                 prefix, strna(u->meta.instance),
618                 prefix, unit_load_state_to_string(u->meta.load_state),
619                 prefix, unit_active_state_to_string(unit_active_state(u)),
620                 prefix, strna(format_timestamp(timestamp1, sizeof(timestamp1), u->meta.inactive_exit_timestamp.realtime)),
621                 prefix, strna(format_timestamp(timestamp2, sizeof(timestamp2), u->meta.active_enter_timestamp.realtime)),
622                 prefix, strna(format_timestamp(timestamp3, sizeof(timestamp3), u->meta.active_exit_timestamp.realtime)),
623                 prefix, strna(format_timestamp(timestamp4, sizeof(timestamp4), u->meta.inactive_enter_timestamp.realtime)),
624                 prefix, yes_no(unit_check_gc(u)),
625                 prefix, yes_no(unit_need_daemon_reload(u)));
626
627         SET_FOREACH(t, u->meta.names, i)
628                 fprintf(f, "%s\tName: %s\n", prefix, t);
629
630         if ((following = unit_following(u)))
631                 fprintf(f, "%s\tFollowing: %s\n", prefix, following->meta.id);
632
633         if (u->meta.fragment_path)
634                 fprintf(f, "%s\tFragment Path: %s\n", prefix, u->meta.fragment_path);
635
636         if (u->meta.job_timeout > 0)
637                 fprintf(f, "%s\tJob Timeout: %s\n", prefix, format_timespan(timespan, sizeof(timespan), u->meta.job_timeout));
638
639         for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) {
640                 Unit *other;
641
642                 SET_FOREACH(other, u->meta.dependencies[d], i)
643                         fprintf(f, "%s\t%s: %s\n", prefix, unit_dependency_to_string(d), other->meta.id);
644         }
645
646         if (u->meta.load_state == UNIT_LOADED) {
647                 fprintf(f,
648                         "%s\tRecursive Stop: %s\n"
649                         "%s\tStopWhenUnneeded: %s\n"
650                         "%s\tRefuseManualStart: %s\n"
651                         "%s\tRefuseManualStop: %s\n"
652                         "%s\tDefaultDependencies: %s\n"
653                         "%s\tIgnoreDependencyFailure: %s\n",
654                         prefix, yes_no(u->meta.recursive_stop),
655                         prefix, yes_no(u->meta.stop_when_unneeded),
656                         prefix, yes_no(u->meta.refuse_manual_start),
657                         prefix, yes_no(u->meta.refuse_manual_stop),
658                         prefix, yes_no(u->meta.default_dependencies),
659                         prefix, yes_no(u->meta.ignore_dependency_failure));
660
661                 LIST_FOREACH(by_unit, b, u->meta.cgroup_bondings)
662                         fprintf(f, "%s\tControlGroup: %s:%s\n",
663                                 prefix, b->controller, b->path);
664
665                 if (UNIT_VTABLE(u)->dump)
666                         UNIT_VTABLE(u)->dump(u, f, prefix2);
667
668         } else if (u->meta.load_state == UNIT_MERGED)
669                 fprintf(f,
670                         "%s\tMerged into: %s\n",
671                         prefix, u->meta.merged_into->meta.id);
672
673         if (u->meta.job)
674                 job_dump(u->meta.job, f, prefix2);
675
676         free(p2);
677 }
678
679 /* Common implementation for multiple backends */
680 int unit_load_fragment_and_dropin(Unit *u) {
681         int r;
682
683         assert(u);
684
685         /* Load a .service file */
686         if ((r = unit_load_fragment(u)) < 0)
687                 return r;
688
689         if (u->meta.load_state == UNIT_STUB)
690                 return -ENOENT;
691
692         /* Load drop-in directory data */
693         if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
694                 return r;
695
696         return 0;
697 }
698
699 /* Common implementation for multiple backends */
700 int unit_load_fragment_and_dropin_optional(Unit *u) {
701         int r;
702
703         assert(u);
704
705         /* Same as unit_load_fragment_and_dropin(), but whether
706          * something can be loaded or not doesn't matter. */
707
708         /* Load a .service file */
709         if ((r = unit_load_fragment(u)) < 0)
710                 return r;
711
712         if (u->meta.load_state == UNIT_STUB)
713                 u->meta.load_state = UNIT_LOADED;
714
715         /* Load drop-in directory data */
716         if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
717                 return r;
718
719         return 0;
720 }
721
722 /* Common implementation for multiple backends */
723 int unit_load_nop(Unit *u) {
724         assert(u);
725
726         if (u->meta.load_state == UNIT_STUB)
727                 u->meta.load_state = UNIT_LOADED;
728
729         return 0;
730 }
731
732 int unit_load(Unit *u) {
733         int r;
734
735         assert(u);
736
737         if (u->meta.in_load_queue) {
738                 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
739                 u->meta.in_load_queue = false;
740         }
741
742         if (u->meta.type == _UNIT_TYPE_INVALID)
743                 return -EINVAL;
744
745         if (u->meta.load_state != UNIT_STUB)
746                 return 0;
747
748         if (UNIT_VTABLE(u)->load)
749                 if ((r = UNIT_VTABLE(u)->load(u)) < 0)
750                         goto fail;
751
752         if (u->meta.load_state == UNIT_STUB) {
753                 r = -ENOENT;
754                 goto fail;
755         }
756
757         assert((u->meta.load_state != UNIT_MERGED) == !u->meta.merged_into);
758
759         unit_add_to_dbus_queue(unit_follow_merge(u));
760         unit_add_to_gc_queue(u);
761
762         return 0;
763
764 fail:
765         u->meta.load_state = UNIT_FAILED;
766         unit_add_to_dbus_queue(u);
767
768         log_notice("Failed to load configuration for %s: %s", u->meta.id, strerror(-r));
769
770         return r;
771 }
772
773 /* Errors:
774  *         -EBADR:     This unit type does not support starting.
775  *         -EALREADY:  Unit is already started.
776  *         -EAGAIN:    An operation is already in progress. Retry later.
777  *         -ECANCELED: Too many requests for now.
778  */
779 int unit_start(Unit *u) {
780         UnitActiveState state;
781
782         assert(u);
783
784         if (u->meta.load_state != UNIT_LOADED)
785                 return -EINVAL;
786
787         /* If this is already (being) started, then this will
788          * succeed. Note that this will even succeed if this unit is
789          * not startable by the user. This is relied on to detect when
790          * we need to wait for units and when waiting is finished. */
791         state = unit_active_state(u);
792         if (UNIT_IS_ACTIVE_OR_RELOADING(state))
793                 return -EALREADY;
794
795         /* If it is stopped, but we cannot start it, then fail */
796         if (!UNIT_VTABLE(u)->start)
797                 return -EBADR;
798
799         /* We don't suppress calls to ->start() here when we are
800          * already starting, to allow this request to be used as a
801          * "hurry up" call, for example when the unit is in some "auto
802          * restart" state where it waits for a holdoff timer to elapse
803          * before it will start again. */
804
805         unit_add_to_dbus_queue(u);
806
807         unit_status_printf(u, "Starting %s...\n", unit_description(u));
808
809         return UNIT_VTABLE(u)->start(u);
810 }
811
812 bool unit_can_start(Unit *u) {
813         assert(u);
814
815         return !!UNIT_VTABLE(u)->start;
816 }
817
818 /* Errors:
819  *         -EBADR:    This unit type does not support stopping.
820  *         -EALREADY: Unit is already stopped.
821  *         -EAGAIN:   An operation is already in progress. Retry later.
822  */
823 int unit_stop(Unit *u) {
824         UnitActiveState state;
825
826         assert(u);
827
828         state = unit_active_state(u);
829         if (UNIT_IS_INACTIVE_OR_MAINTENANCE(state))
830                 return -EALREADY;
831
832         if (!UNIT_VTABLE(u)->stop)
833                 return -EBADR;
834
835         unit_add_to_dbus_queue(u);
836
837         unit_status_printf(u, "Stopping %s...\n", unit_description(u));
838
839         return UNIT_VTABLE(u)->stop(u);
840 }
841
842 /* Errors:
843  *         -EBADR:    This unit type does not support reloading.
844  *         -ENOEXEC:  Unit is not started.
845  *         -EAGAIN:   An operation is already in progress. Retry later.
846  */
847 int unit_reload(Unit *u) {
848         UnitActiveState state;
849
850         assert(u);
851
852         if (u->meta.load_state != UNIT_LOADED)
853                 return -EINVAL;
854
855         if (!unit_can_reload(u))
856                 return -EBADR;
857
858         state = unit_active_state(u);
859         if (unit_active_state(u) == UNIT_RELOADING)
860                 return -EALREADY;
861
862         if (unit_active_state(u) != UNIT_ACTIVE)
863                 return -ENOEXEC;
864
865         unit_add_to_dbus_queue(u);
866         return UNIT_VTABLE(u)->reload(u);
867 }
868
869 bool unit_can_reload(Unit *u) {
870         assert(u);
871
872         if (!UNIT_VTABLE(u)->reload)
873                 return false;
874
875         if (!UNIT_VTABLE(u)->can_reload)
876                 return true;
877
878         return UNIT_VTABLE(u)->can_reload(u);
879 }
880
881 static void unit_check_uneeded(Unit *u) {
882         Iterator i;
883         Unit *other;
884
885         assert(u);
886
887         /* If this service shall be shut down when unneeded then do
888          * so. */
889
890         if (!u->meta.stop_when_unneeded)
891                 return;
892
893         if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)))
894                 return;
895
896         SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
897                 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
898                         return;
899
900         SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY_OVERRIDABLE], i)
901                 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
902                         return;
903
904         SET_FOREACH(other, u->meta.dependencies[UNIT_WANTED_BY], i)
905                 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
906                         return;
907
908         log_info("Service %s is not needed anymore. Stopping.", u->meta.id);
909
910         /* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */
911         manager_add_job(u->meta.manager, JOB_STOP, u, JOB_FAIL, true, NULL, NULL);
912 }
913
914 static void retroactively_start_dependencies(Unit *u) {
915         Iterator i;
916         Unit *other;
917
918         assert(u);
919         assert(UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)));
920
921         SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
922                 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
923                         manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL, NULL);
924
925         SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i)
926                 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
927                         manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL, NULL);
928
929         SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i)
930                 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
931                         manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL, NULL);
932
933         SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i)
934                 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
935                         manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL, NULL);
936
937         SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i)
938                 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
939                         manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL, NULL);
940
941         SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTED_BY], i)
942                 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
943                         manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL, NULL);
944 }
945
946 static void retroactively_stop_dependencies(Unit *u) {
947         Iterator i;
948         Unit *other;
949
950         assert(u);
951         assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u)));
952
953         if (u->meta.recursive_stop) {
954                 /* Pull down units need us recursively if enabled */
955                 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
956                         if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
957                                 manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL, NULL);
958         }
959
960         /* Garbage collect services that might not be needed anymore, if enabled */
961         SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
962                 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
963                         unit_check_uneeded(other);
964         SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i)
965                 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
966                         unit_check_uneeded(other);
967         SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i)
968                 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
969                         unit_check_uneeded(other);
970         SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i)
971                 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
972                         unit_check_uneeded(other);
973         SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE_OVERRIDABLE], i)
974                 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
975                         unit_check_uneeded(other);
976 }
977
978 void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns) {
979         dual_timestamp ts;
980         bool unexpected;
981
982         assert(u);
983         assert(os < _UNIT_ACTIVE_STATE_MAX);
984         assert(ns < _UNIT_ACTIVE_STATE_MAX);
985
986         /* Note that this is called for all low-level state changes,
987          * even if they might map to the same high-level
988          * UnitActiveState! That means that ns == os is OK an expected
989          * behaviour here. For example: if a mount point is remounted
990          * this function will be called too and the utmp code below
991          * relies on that! */
992
993         dual_timestamp_get(&ts);
994
995         if (UNIT_IS_INACTIVE_OR_MAINTENANCE(os) && !UNIT_IS_INACTIVE_OR_MAINTENANCE(ns))
996                 u->meta.inactive_exit_timestamp = ts;
997         else if (!UNIT_IS_INACTIVE_OR_MAINTENANCE(os) && UNIT_IS_INACTIVE_OR_MAINTENANCE(ns))
998                 u->meta.inactive_enter_timestamp = ts;
999
1000         if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns))
1001                 u->meta.active_enter_timestamp = ts;
1002         else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns))
1003                 u->meta.active_exit_timestamp = ts;
1004
1005         if (UNIT_IS_INACTIVE_OR_MAINTENANCE(ns))
1006                 cgroup_bonding_trim_list(u->meta.cgroup_bondings, true);
1007
1008         timer_unit_notify(u, ns);
1009         path_unit_notify(u, ns);
1010
1011         if (u->meta.job) {
1012                 unexpected = false;
1013
1014                 if (u->meta.job->state == JOB_WAITING)
1015
1016                         /* So we reached a different state for this
1017                          * job. Let's see if we can run it now if it
1018                          * failed previously due to EAGAIN. */
1019                         job_add_to_run_queue(u->meta.job);
1020
1021                 /* Let's check whether this state change constitutes a
1022                  * finished job, or maybe cotradicts a running job and
1023                  * hence needs to invalidate jobs. */
1024
1025                 switch (u->meta.job->type) {
1026
1027                 case JOB_START:
1028                 case JOB_VERIFY_ACTIVE:
1029
1030                         if (UNIT_IS_ACTIVE_OR_RELOADING(ns))
1031                                 job_finish_and_invalidate(u->meta.job, true);
1032                         else if (u->meta.job->state == JOB_RUNNING && ns != UNIT_ACTIVATING) {
1033                                 unexpected = true;
1034                                 job_finish_and_invalidate(u->meta.job, false);
1035                         }
1036
1037                         break;
1038
1039                 case JOB_RELOAD:
1040                 case JOB_RELOAD_OR_START:
1041
1042                         if (u->meta.job->state == JOB_RUNNING) {
1043                                 if (ns == UNIT_ACTIVE)
1044                                         job_finish_and_invalidate(u->meta.job, true);
1045                                 else if (ns != UNIT_ACTIVATING && ns != UNIT_RELOADING) {
1046                                         unexpected = true;
1047                                         job_finish_and_invalidate(u->meta.job, false);
1048                                 }
1049                         }
1050
1051                         break;
1052
1053                 case JOB_STOP:
1054                 case JOB_RESTART:
1055                 case JOB_TRY_RESTART:
1056
1057                         if (ns == UNIT_INACTIVE || ns == UNIT_MAINTENANCE)
1058                                 job_finish_and_invalidate(u->meta.job, true);
1059                         else if (u->meta.job->state == JOB_RUNNING && ns != UNIT_DEACTIVATING) {
1060                                 unexpected = true;
1061                                 job_finish_and_invalidate(u->meta.job, false);
1062                         }
1063
1064                         break;
1065
1066                 default:
1067                         assert_not_reached("Job type unknown");
1068                 }
1069
1070         } else
1071                 unexpected = true;
1072
1073         /* If this state change happened without being requested by a
1074          * job, then let's retroactively start or stop
1075          * dependencies. We skip that step when deserializing, since
1076          * we don't want to create any additional jobs just because
1077          * something is already activated. */
1078
1079         if (unexpected && u->meta.manager->n_deserializing <= 0) {
1080                 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(os) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns))
1081                         retroactively_start_dependencies(u);
1082                 else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns))
1083                         retroactively_stop_dependencies(u);
1084         }
1085
1086         if (ns != os && ns == UNIT_MAINTENANCE) {
1087                 Iterator i;
1088                 Unit *other;
1089
1090                 SET_FOREACH(other, u->meta.dependencies[UNIT_ON_FAILURE], i)
1091                         manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL, NULL);
1092
1093                 log_notice("Unit %s entered maintenance state.", u->meta.id);
1094         }
1095
1096         /* Some names are special */
1097         if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
1098                 if (unit_has_name(u, SPECIAL_DBUS_SERVICE))
1099                         /* The bus just might have become available,
1100                          * hence try to connect to it, if we aren't
1101                          * yet connected. */
1102                         bus_init(u->meta.manager);
1103
1104                 if (unit_has_name(u, SPECIAL_SYSLOG_SERVICE))
1105                         /* The syslog daemon just might have become
1106                          * available, hence try to connect to it, if
1107                          * we aren't yet connected. */
1108                         log_open();
1109
1110                 if (u->meta.type == UNIT_SERVICE &&
1111                     !UNIT_IS_ACTIVE_OR_RELOADING(os))
1112                         /* Write audit record if we have just finished starting up */
1113                         manager_send_unit_audit(u->meta.manager, u, AUDIT_SERVICE_START, 1);
1114
1115         } else {
1116
1117                 if (unit_has_name(u, SPECIAL_SYSLOG_SERVICE))
1118                         /* The syslog daemon might just have
1119                          * terminated, hence try to disconnect from
1120                          * it. */
1121                         log_close_syslog();
1122
1123                 /* We don't care about D-Bus here, since we'll get an
1124                  * asynchronous notification for it anyway. */
1125
1126                 if (u->meta.type == UNIT_SERVICE &&
1127                     UNIT_IS_INACTIVE_OR_MAINTENANCE(ns) &&
1128                     !UNIT_IS_INACTIVE_OR_MAINTENANCE(os))
1129
1130                         /* Write audit record if we have just finished shutting down */
1131                         manager_send_unit_audit(u->meta.manager, u, AUDIT_SERVICE_STOP, ns == UNIT_INACTIVE);
1132         }
1133
1134         /* Maybe we finished startup and are now ready for being
1135          * stopped because unneeded? */
1136         unit_check_uneeded(u);
1137
1138         unit_add_to_dbus_queue(u);
1139         unit_add_to_gc_queue(u);
1140 }
1141
1142 int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w) {
1143         struct epoll_event ev;
1144
1145         assert(u);
1146         assert(fd >= 0);
1147         assert(w);
1148         assert(w->type == WATCH_INVALID || (w->type == WATCH_FD && w->fd == fd && w->data.unit == u));
1149
1150         zero(ev);
1151         ev.data.ptr = w;
1152         ev.events = events;
1153
1154         if (epoll_ctl(u->meta.manager->epoll_fd,
1155                       w->type == WATCH_INVALID ? EPOLL_CTL_ADD : EPOLL_CTL_MOD,
1156                       fd,
1157                       &ev) < 0)
1158                 return -errno;
1159
1160         w->fd = fd;
1161         w->type = WATCH_FD;
1162         w->data.unit = u;
1163
1164         return 0;
1165 }
1166
1167 void unit_unwatch_fd(Unit *u, Watch *w) {
1168         assert(u);
1169         assert(w);
1170
1171         if (w->type == WATCH_INVALID)
1172                 return;
1173
1174         assert(w->type == WATCH_FD);
1175         assert(w->data.unit == u);
1176         assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
1177
1178         w->fd = -1;
1179         w->type = WATCH_INVALID;
1180         w->data.unit = NULL;
1181 }
1182
1183 int unit_watch_pid(Unit *u, pid_t pid) {
1184         assert(u);
1185         assert(pid >= 1);
1186
1187         /* Watch a specific PID. We only support one unit watching
1188          * each PID for now. */
1189
1190         return hashmap_put(u->meta.manager->watch_pids, LONG_TO_PTR(pid), u);
1191 }
1192
1193 void unit_unwatch_pid(Unit *u, pid_t pid) {
1194         assert(u);
1195         assert(pid >= 1);
1196
1197         hashmap_remove_value(u->meta.manager->watch_pids, LONG_TO_PTR(pid), u);
1198 }
1199
1200 int unit_watch_timer(Unit *u, usec_t delay, Watch *w) {
1201         struct itimerspec its;
1202         int flags, fd;
1203         bool ours;
1204
1205         assert(u);
1206         assert(w);
1207         assert(w->type == WATCH_INVALID || (w->type == WATCH_UNIT_TIMER && w->data.unit == u));
1208
1209         /* This will try to reuse the old timer if there is one */
1210
1211         if (w->type == WATCH_UNIT_TIMER) {
1212                 assert(w->data.unit == u);
1213                 assert(w->fd >= 0);
1214
1215                 ours = false;
1216                 fd = w->fd;
1217         } else if (w->type == WATCH_INVALID) {
1218
1219                 ours = true;
1220                 if ((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
1221                         return -errno;
1222         } else
1223                 assert_not_reached("Invalid watch type");
1224
1225         zero(its);
1226
1227         if (delay <= 0) {
1228                 /* Set absolute time in the past, but not 0, since we
1229                  * don't want to disarm the timer */
1230                 its.it_value.tv_sec = 0;
1231                 its.it_value.tv_nsec = 1;
1232
1233                 flags = TFD_TIMER_ABSTIME;
1234         } else {
1235                 timespec_store(&its.it_value, delay);
1236                 flags = 0;
1237         }
1238
1239         /* This will also flush the elapse counter */
1240         if (timerfd_settime(fd, flags, &its, NULL) < 0)
1241                 goto fail;
1242
1243         if (w->type == WATCH_INVALID) {
1244                 struct epoll_event ev;
1245
1246                 zero(ev);
1247                 ev.data.ptr = w;
1248                 ev.events = EPOLLIN;
1249
1250                 if (epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0)
1251                         goto fail;
1252         }
1253
1254         w->type = WATCH_UNIT_TIMER;
1255         w->fd = fd;
1256         w->data.unit = u;
1257
1258         return 0;
1259
1260 fail:
1261         if (ours)
1262                 close_nointr_nofail(fd);
1263
1264         return -errno;
1265 }
1266
1267 void unit_unwatch_timer(Unit *u, Watch *w) {
1268         assert(u);
1269         assert(w);
1270
1271         if (w->type == WATCH_INVALID)
1272                 return;
1273
1274         assert(w->type == WATCH_UNIT_TIMER);
1275         assert(w->data.unit == u);
1276         assert(w->fd >= 0);
1277
1278         assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
1279         close_nointr_nofail(w->fd);
1280
1281         w->fd = -1;
1282         w->type = WATCH_INVALID;
1283         w->data.unit = NULL;
1284 }
1285
1286 bool unit_job_is_applicable(Unit *u, JobType j) {
1287         assert(u);
1288         assert(j >= 0 && j < _JOB_TYPE_MAX);
1289
1290         switch (j) {
1291
1292         case JOB_VERIFY_ACTIVE:
1293         case JOB_START:
1294                 return true;
1295
1296         case JOB_STOP:
1297         case JOB_RESTART:
1298         case JOB_TRY_RESTART:
1299                 return unit_can_start(u);
1300
1301         case JOB_RELOAD:
1302                 return unit_can_reload(u);
1303
1304         case JOB_RELOAD_OR_START:
1305                 return unit_can_reload(u) && unit_can_start(u);
1306
1307         default:
1308                 assert_not_reached("Invalid job type");
1309         }
1310 }
1311
1312 int unit_add_dependency(Unit *u, UnitDependency d, Unit *other, bool add_reference) {
1313
1314         static const UnitDependency inverse_table[_UNIT_DEPENDENCY_MAX] = {
1315                 [UNIT_REQUIRES] = UNIT_REQUIRED_BY,
1316                 [UNIT_REQUIRES_OVERRIDABLE] = UNIT_REQUIRED_BY_OVERRIDABLE,
1317                 [UNIT_WANTS] = UNIT_WANTED_BY,
1318                 [UNIT_REQUISITE] = UNIT_REQUIRED_BY,
1319                 [UNIT_REQUISITE_OVERRIDABLE] = UNIT_REQUIRED_BY_OVERRIDABLE,
1320                 [UNIT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID,
1321                 [UNIT_REQUIRED_BY_OVERRIDABLE] = _UNIT_DEPENDENCY_INVALID,
1322                 [UNIT_WANTED_BY] = _UNIT_DEPENDENCY_INVALID,
1323                 [UNIT_CONFLICTS] = UNIT_CONFLICTED_BY,
1324                 [UNIT_CONFLICTED_BY] = UNIT_CONFLICTS,
1325                 [UNIT_BEFORE] = UNIT_AFTER,
1326                 [UNIT_AFTER] = UNIT_BEFORE,
1327                 [UNIT_ON_FAILURE] = _UNIT_DEPENDENCY_INVALID,
1328                 [UNIT_REFERENCES] = UNIT_REFERENCED_BY,
1329                 [UNIT_REFERENCED_BY] = UNIT_REFERENCES
1330         };
1331         int r, q = 0, v = 0, w = 0;
1332
1333         assert(u);
1334         assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX);
1335         assert(other);
1336
1337         /* We won't allow dependencies on ourselves. We will not
1338          * consider them an error however. */
1339         if (u == other)
1340                 return 0;
1341
1342         if (UNIT_VTABLE(u)->no_requires &&
1343             (d == UNIT_REQUIRES ||
1344              d == UNIT_REQUIRES_OVERRIDABLE ||
1345              d == UNIT_REQUISITE ||
1346              d == UNIT_REQUISITE_OVERRIDABLE)) {
1347                     return -EINVAL;
1348         }
1349
1350         if ((r = set_ensure_allocated(&u->meta.dependencies[d], trivial_hash_func, trivial_compare_func)) < 0)
1351                 return r;
1352
1353         if (inverse_table[d] != _UNIT_DEPENDENCY_INVALID)
1354                 if ((r = set_ensure_allocated(&other->meta.dependencies[inverse_table[d]], trivial_hash_func, trivial_compare_func)) < 0)
1355                         return r;
1356
1357         if (add_reference)
1358                 if ((r = set_ensure_allocated(&u->meta.dependencies[UNIT_REFERENCES], trivial_hash_func, trivial_compare_func)) < 0 ||
1359                     (r = set_ensure_allocated(&other->meta.dependencies[UNIT_REFERENCED_BY], trivial_hash_func, trivial_compare_func)) < 0)
1360                         return r;
1361
1362         if ((q = set_put(u->meta.dependencies[d], other)) < 0)
1363                 return q;
1364
1365         if (inverse_table[d] != _UNIT_DEPENDENCY_INVALID)
1366                 if ((v = set_put(other->meta.dependencies[inverse_table[d]], u)) < 0) {
1367                         r = v;
1368                         goto fail;
1369                 }
1370
1371         if (add_reference) {
1372                 if ((w = set_put(u->meta.dependencies[UNIT_REFERENCES], other)) < 0) {
1373                         r = w;
1374                         goto fail;
1375                 }
1376
1377                 if ((r = set_put(other->meta.dependencies[UNIT_REFERENCED_BY], u)) < 0)
1378                         goto fail;
1379         }
1380
1381         unit_add_to_dbus_queue(u);
1382         return 0;
1383
1384 fail:
1385         if (q > 0)
1386                 set_remove(u->meta.dependencies[d], other);
1387
1388         if (v > 0)
1389                 set_remove(other->meta.dependencies[inverse_table[d]], u);
1390
1391         if (w > 0)
1392                 set_remove(u->meta.dependencies[UNIT_REFERENCES], other);
1393
1394         return r;
1395 }
1396
1397 int unit_add_two_dependencies(Unit *u, UnitDependency d, UnitDependency e, Unit *other, bool add_reference) {
1398         int r;
1399
1400         assert(u);
1401
1402         if ((r = unit_add_dependency(u, d, other, add_reference)) < 0)
1403                 return r;
1404
1405         if ((r = unit_add_dependency(u, e, other, add_reference)) < 0)
1406                 return r;
1407
1408         return 0;
1409 }
1410
1411 static const char *resolve_template(Unit *u, const char *name, const char*path, char **p) {
1412         char *s;
1413
1414         assert(u);
1415         assert(name || path);
1416
1417         if (!name)
1418                 name = file_name_from_path(path);
1419
1420         if (!unit_name_is_template(name)) {
1421                 *p = NULL;
1422                 return name;
1423         }
1424
1425         if (u->meta.instance)
1426                 s = unit_name_replace_instance(name, u->meta.instance);
1427         else {
1428                 char *i;
1429
1430                 if (!(i = unit_name_to_prefix(u->meta.id)))
1431                         return NULL;
1432
1433                 s = unit_name_replace_instance(name, i);
1434                 free(i);
1435         }
1436
1437         if (!s)
1438                 return NULL;
1439
1440         *p = s;
1441         return s;
1442 }
1443
1444 int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, const char *path, bool add_reference) {
1445         Unit *other;
1446         int r;
1447         char *s;
1448
1449         assert(u);
1450         assert(name || path);
1451
1452         if (!(name = resolve_template(u, name, path, &s)))
1453                 return -ENOMEM;
1454
1455         if ((r = manager_load_unit(u->meta.manager, name, path, NULL, &other)) < 0)
1456                 goto finish;
1457
1458         r = unit_add_dependency(u, d, other, add_reference);
1459
1460 finish:
1461         free(s);
1462         return r;
1463 }
1464
1465 int unit_add_two_dependencies_by_name(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference) {
1466         Unit *other;
1467         int r;
1468         char *s;
1469
1470         assert(u);
1471         assert(name || path);
1472
1473         if (!(name = resolve_template(u, name, path, &s)))
1474                 return -ENOMEM;
1475
1476         if ((r = manager_load_unit(u->meta.manager, name, path, NULL, &other)) < 0)
1477                 goto finish;
1478
1479         r = unit_add_two_dependencies(u, d, e, other, add_reference);
1480
1481 finish:
1482         free(s);
1483         return r;
1484 }
1485
1486 int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *name, const char *path, bool add_reference) {
1487         Unit *other;
1488         int r;
1489         char *s;
1490
1491         assert(u);
1492         assert(name || path);
1493
1494         if (!(name = resolve_template(u, name, path, &s)))
1495                 return -ENOMEM;
1496
1497         if ((r = manager_load_unit(u->meta.manager, name, path, NULL, &other)) < 0)
1498                 goto finish;
1499
1500         r = unit_add_dependency(other, d, u, add_reference);
1501
1502 finish:
1503         free(s);
1504         return r;
1505 }
1506
1507 int unit_add_two_dependencies_by_name_inverse(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference) {
1508         Unit *other;
1509         int r;
1510         char *s;
1511
1512         assert(u);
1513         assert(name || path);
1514
1515         if (!(name = resolve_template(u, name, path, &s)))
1516                 return -ENOMEM;
1517
1518         if ((r = manager_load_unit(u->meta.manager, name, path, NULL, &other)) < 0)
1519                 goto finish;
1520
1521         if ((r = unit_add_two_dependencies(other, d, e, u, add_reference)) < 0)
1522                 goto finish;
1523
1524 finish:
1525         free(s);
1526         return r;
1527 }
1528
1529 int set_unit_path(const char *p) {
1530         char *cwd, *c;
1531         int r;
1532
1533         /* This is mostly for debug purposes */
1534
1535         if (path_is_absolute(p)) {
1536                 if (!(c = strdup(p)))
1537                         return -ENOMEM;
1538         } else {
1539                 if (!(cwd = get_current_dir_name()))
1540                         return -errno;
1541
1542                 r = asprintf(&c, "%s/%s", cwd, p);
1543                 free(cwd);
1544
1545                 if (r < 0)
1546                         return -ENOMEM;
1547         }
1548
1549         if (setenv("SYSTEMD_UNIT_PATH", c, 0) < 0) {
1550                 r = -errno;
1551                 free(c);
1552                 return r;
1553         }
1554
1555         return 0;
1556 }
1557
1558 char *unit_dbus_path(Unit *u) {
1559         char *p, *e;
1560
1561         assert(u);
1562
1563         if (!u->meta.id)
1564                 return NULL;
1565
1566         if (!(e = bus_path_escape(u->meta.id)))
1567                 return NULL;
1568
1569         p = strappend("/org/freedesktop/systemd1/unit/", e);
1570         free(e);
1571
1572         return p;
1573 }
1574
1575 int unit_add_cgroup(Unit *u, CGroupBonding *b) {
1576         CGroupBonding *l;
1577         int r;
1578
1579         assert(u);
1580         assert(b);
1581
1582         assert(b->path);
1583
1584         if (!b->controller)
1585                 if (!(b->controller = strdup(SYSTEMD_CGROUP_CONTROLLER)))
1586                         return -ENOMEM;
1587
1588         /* Ensure this hasn't been added yet */
1589         assert(!b->unit);
1590
1591         l = hashmap_get(u->meta.manager->cgroup_bondings, b->path);
1592         LIST_PREPEND(CGroupBonding, by_path, l, b);
1593
1594         if ((r = hashmap_replace(u->meta.manager->cgroup_bondings, b->path, l)) < 0) {
1595                 LIST_REMOVE(CGroupBonding, by_path, l, b);
1596                 return r;
1597         }
1598
1599         LIST_PREPEND(CGroupBonding, by_unit, u->meta.cgroup_bondings, b);
1600         b->unit = u;
1601
1602         return 0;
1603 }
1604
1605 static char *default_cgroup_path(Unit *u) {
1606         char *p;
1607         int r;
1608
1609         assert(u);
1610
1611         if (u->meta.instance) {
1612                 char *t;
1613
1614                 if (!(t = unit_name_template(u->meta.id)))
1615                         return NULL;
1616
1617                 r = asprintf(&p, "%s/%s/%s", u->meta.manager->cgroup_hierarchy, t, u->meta.instance);
1618                 free(t);
1619         } else
1620                 r = asprintf(&p, "%s/%s", u->meta.manager->cgroup_hierarchy, u->meta.id);
1621
1622         return r < 0 ? NULL : p;
1623 }
1624
1625 int unit_add_cgroup_from_text(Unit *u, const char *name) {
1626         char *controller = NULL, *path = NULL;
1627         CGroupBonding *b = NULL;
1628         int r;
1629
1630         assert(u);
1631         assert(name);
1632
1633         if ((r = cg_split_spec(name, &controller, &path)) < 0)
1634                 return r;
1635
1636         if (!path)
1637                 path = default_cgroup_path(u);
1638
1639         if (!controller)
1640                 controller = strdup(SYSTEMD_CGROUP_CONTROLLER);
1641
1642         if (!path || !controller) {
1643                 free(path);
1644                 free(controller);
1645
1646                 return -ENOMEM;
1647         }
1648
1649         if (cgroup_bonding_find_list(u->meta.cgroup_bondings, controller)) {
1650                 r = -EEXIST;
1651                 goto fail;
1652         }
1653
1654         if (!(b = new0(CGroupBonding, 1))) {
1655                 r = -ENOMEM;
1656                 goto fail;
1657         }
1658
1659         b->controller = controller;
1660         b->path = path;
1661         b->only_us = false;
1662         b->clean_up = false;
1663
1664         if ((r = unit_add_cgroup(u, b)) < 0)
1665                 goto fail;
1666
1667         return 0;
1668
1669 fail:
1670         free(path);
1671         free(controller);
1672         free(b);
1673
1674         return r;
1675 }
1676
1677 int unit_add_default_cgroup(Unit *u) {
1678         CGroupBonding *b;
1679         int r = -ENOMEM;
1680
1681         assert(u);
1682
1683         /* Adds in the default cgroup data, if it wasn't specified yet */
1684
1685         if (unit_get_default_cgroup(u))
1686                 return 0;
1687
1688         if (!(b = new0(CGroupBonding, 1)))
1689                 return -ENOMEM;
1690
1691         if (!(b->path = default_cgroup_path(u)))
1692                 goto fail;
1693
1694         b->clean_up = true;
1695         b->only_us = true;
1696
1697         if ((r = unit_add_cgroup(u, b)) < 0)
1698                 goto fail;
1699
1700         return 0;
1701
1702 fail:
1703         free(b->path);
1704         free(b->controller);
1705         free(b);
1706
1707         return r;
1708 }
1709
1710 CGroupBonding* unit_get_default_cgroup(Unit *u) {
1711         assert(u);
1712
1713         return cgroup_bonding_find_list(u->meta.cgroup_bondings, SYSTEMD_CGROUP_CONTROLLER);
1714 }
1715
1716 int unit_load_related_unit(Unit *u, const char *type, Unit **_found) {
1717         char *t;
1718         int r;
1719
1720         assert(u);
1721         assert(type);
1722         assert(_found);
1723
1724         if (!(t = unit_name_change_suffix(u->meta.id, type)))
1725                 return -ENOMEM;
1726
1727         assert(!unit_has_name(u, t));
1728
1729         r = manager_load_unit(u->meta.manager, t, NULL, NULL, _found);
1730         free(t);
1731
1732         assert(r < 0 || *_found != u);
1733
1734         return r;
1735 }
1736
1737 int unit_get_related_unit(Unit *u, const char *type, Unit **_found) {
1738         Unit *found;
1739         char *t;
1740
1741         assert(u);
1742         assert(type);
1743         assert(_found);
1744
1745         if (!(t = unit_name_change_suffix(u->meta.id, type)))
1746                 return -ENOMEM;
1747
1748         assert(!unit_has_name(u, t));
1749
1750         found = manager_get_unit(u->meta.manager, t);
1751         free(t);
1752
1753         if (!found)
1754                 return -ENOENT;
1755
1756         *_found = found;
1757         return 0;
1758 }
1759
1760 static char *specifier_prefix_and_instance(char specifier, void *data, void *userdata) {
1761         Unit *u = userdata;
1762         assert(u);
1763
1764         return unit_name_to_prefix_and_instance(u->meta.id);
1765 }
1766
1767 static char *specifier_prefix(char specifier, void *data, void *userdata) {
1768         Unit *u = userdata;
1769         assert(u);
1770
1771         return unit_name_to_prefix(u->meta.id);
1772 }
1773
1774 static char *specifier_prefix_unescaped(char specifier, void *data, void *userdata) {
1775         Unit *u = userdata;
1776         char *p, *r;
1777
1778         assert(u);
1779
1780         if (!(p = unit_name_to_prefix(u->meta.id)))
1781                 return NULL;
1782
1783         r = unit_name_unescape(p);
1784         free(p);
1785
1786         return r;
1787 }
1788
1789 static char *specifier_instance_unescaped(char specifier, void *data, void *userdata) {
1790         Unit *u = userdata;
1791         assert(u);
1792
1793         if (u->meta.instance)
1794                 return unit_name_unescape(u->meta.instance);
1795
1796         return strdup("");
1797 }
1798
1799 char *unit_name_printf(Unit *u, const char* format) {
1800
1801         /*
1802          * This will use the passed string as format string and
1803          * replace the following specifiers:
1804          *
1805          * %n: the full id of the unit                 (foo@bar.waldo)
1806          * %N: the id of the unit without the suffix   (foo@bar)
1807          * %p: the prefix                              (foo)
1808          * %i: the instance                            (bar)
1809          */
1810
1811         const Specifier table[] = {
1812                 { 'n', specifier_string,              u->meta.id },
1813                 { 'N', specifier_prefix_and_instance, NULL },
1814                 { 'p', specifier_prefix,              NULL },
1815                 { 'i', specifier_string,              u->meta.instance },
1816                 { 0, NULL, NULL }
1817         };
1818
1819         assert(u);
1820         assert(format);
1821
1822         return specifier_printf(format, table, u);
1823 }
1824
1825 char *unit_full_printf(Unit *u, const char *format) {
1826
1827         /* This is similar to unit_name_printf() but also supports
1828          * unescaping */
1829
1830         const Specifier table[] = {
1831                 { 'n', specifier_string,              u->meta.id },
1832                 { 'N', specifier_prefix_and_instance, NULL },
1833                 { 'p', specifier_prefix,              NULL },
1834                 { 'P', specifier_prefix_unescaped,    NULL },
1835                 { 'i', specifier_string,              u->meta.instance },
1836                 { 'I', specifier_instance_unescaped,  NULL },
1837                 { 0, NULL, NULL }
1838         };
1839
1840         assert(u);
1841         assert(format);
1842
1843         return specifier_printf(format, table, u);
1844 }
1845
1846 char **unit_full_printf_strv(Unit *u, char **l) {
1847         size_t n;
1848         char **r, **i, **j;
1849
1850         /* Applies unit_full_printf to every entry in l */
1851
1852         assert(u);
1853
1854         n = strv_length(l);
1855         if (!(r = new(char*, n+1)))
1856                 return NULL;
1857
1858         for (i = l, j = r; *i; i++, j++)
1859                 if (!(*j = unit_full_printf(u, *i)))
1860                         goto fail;
1861
1862         *j = NULL;
1863         return r;
1864
1865 fail:
1866         j--;
1867         while (j >= r)
1868                 free(*j);
1869
1870         free(r);
1871
1872         return NULL;
1873 }
1874
1875 int unit_watch_bus_name(Unit *u, const char *name) {
1876         assert(u);
1877         assert(name);
1878
1879         /* Watch a specific name on the bus. We only support one unit
1880          * watching each name for now. */
1881
1882         return hashmap_put(u->meta.manager->watch_bus, name, u);
1883 }
1884
1885 void unit_unwatch_bus_name(Unit *u, const char *name) {
1886         assert(u);
1887         assert(name);
1888
1889         hashmap_remove_value(u->meta.manager->watch_bus, name, u);
1890 }
1891
1892 bool unit_can_serialize(Unit *u) {
1893         assert(u);
1894
1895         return UNIT_VTABLE(u)->serialize && UNIT_VTABLE(u)->deserialize_item;
1896 }
1897
1898 int unit_serialize(Unit *u, FILE *f, FDSet *fds) {
1899         int r;
1900
1901         assert(u);
1902         assert(f);
1903         assert(fds);
1904
1905         if (!unit_can_serialize(u))
1906                 return 0;
1907
1908         if ((r = UNIT_VTABLE(u)->serialize(u, f, fds)) < 0)
1909                 return r;
1910
1911         if (u->meta.job)
1912                 unit_serialize_item(u, f, "job", job_type_to_string(u->meta.job->type));
1913
1914         /* End marker */
1915         fputc('\n', f);
1916         return 0;
1917 }
1918
1919 void unit_serialize_item_format(Unit *u, FILE *f, const char *key, const char *format, ...) {
1920         va_list ap;
1921
1922         assert(u);
1923         assert(f);
1924         assert(key);
1925         assert(format);
1926
1927         fputs(key, f);
1928         fputc('=', f);
1929
1930         va_start(ap, format);
1931         vfprintf(f, format, ap);
1932         va_end(ap);
1933
1934         fputc('\n', f);
1935 }
1936
1937 void unit_serialize_item(Unit *u, FILE *f, const char *key, const char *value) {
1938         assert(u);
1939         assert(f);
1940         assert(key);
1941         assert(value);
1942
1943         fprintf(f, "%s=%s\n", key, value);
1944 }
1945
1946 int unit_deserialize(Unit *u, FILE *f, FDSet *fds) {
1947         int r;
1948
1949         assert(u);
1950         assert(f);
1951         assert(fds);
1952
1953         if (!unit_can_serialize(u))
1954                 return 0;
1955
1956         for (;;) {
1957                 char line[1024], *l, *v;
1958                 size_t k;
1959
1960                 if (!fgets(line, sizeof(line), f)) {
1961                         if (feof(f))
1962                                 return 0;
1963                         return -errno;
1964                 }
1965
1966                 l = strstrip(line);
1967
1968                 /* End marker */
1969                 if (l[0] == 0)
1970                         return 0;
1971
1972                 k = strcspn(l, "=");
1973
1974                 if (l[k] == '=') {
1975                         l[k] = 0;
1976                         v = l+k+1;
1977                 } else
1978                         v = l+k;
1979
1980                 if (streq(l, "job")) {
1981                         JobType type;
1982
1983                         if ((type = job_type_from_string(v)) < 0)
1984                                 log_debug("Failed to parse job type value %s", v);
1985                         else
1986                                 u->meta.deserialized_job = type;
1987
1988                         continue;
1989                 }
1990
1991                 if ((r = UNIT_VTABLE(u)->deserialize_item(u, l, v, fds)) < 0)
1992                         return r;
1993         }
1994 }
1995
1996 int unit_add_node_link(Unit *u, const char *what, bool wants) {
1997         Unit *device;
1998         char *e;
1999         int r;
2000
2001         assert(u);
2002
2003         if (!what)
2004                 return 0;
2005
2006         /* Adds in links to the device node that this unit is based on */
2007
2008         if (!is_device_path(what))
2009                 return 0;
2010
2011         if (!(e = unit_name_build_escape(what+1, NULL, ".device")))
2012                 return -ENOMEM;
2013
2014         r = manager_load_unit(u->meta.manager, e, NULL, NULL, &device);
2015         free(e);
2016
2017         if (r < 0)
2018                 return r;
2019
2020         if ((r = unit_add_two_dependencies(u, UNIT_AFTER, UNIT_REQUIRES, device, true)) < 0)
2021                 return r;
2022
2023         if (wants)
2024                 if ((r = unit_add_dependency(device, UNIT_WANTS, u, false)) < 0)
2025                         return r;
2026
2027         return 0;
2028 }
2029
2030 int unit_coldplug(Unit *u) {
2031         int r;
2032
2033         assert(u);
2034
2035         if (UNIT_VTABLE(u)->coldplug)
2036                 if ((r = UNIT_VTABLE(u)->coldplug(u)) < 0)
2037                         return r;
2038
2039         if (u->meta.deserialized_job >= 0) {
2040                 if ((r = manager_add_job(u->meta.manager, u->meta.deserialized_job, u, JOB_FAIL, false, NULL, NULL)) < 0)
2041                         return r;
2042
2043                 u->meta.deserialized_job = _JOB_TYPE_INVALID;
2044         }
2045
2046         return 0;
2047 }
2048
2049 void unit_status_printf(Unit *u, const char *format, ...) {
2050         va_list ap;
2051
2052         assert(u);
2053         assert(format);
2054
2055         if (!UNIT_VTABLE(u)->show_status)
2056                 return;
2057
2058         if (u->meta.manager->running_as != MANAGER_SYSTEM)
2059                 return;
2060
2061         if (!u->meta.manager->show_status)
2062                 return;
2063
2064         if (!manager_is_booting_or_shutting_down(u->meta.manager))
2065                 return;
2066
2067         va_start(ap, format);
2068         status_vprintf(format, ap);
2069         va_end(ap);
2070 }
2071
2072 bool unit_need_daemon_reload(Unit *u) {
2073         struct stat st;
2074
2075         assert(u);
2076
2077         if (!u->meta.fragment_path)
2078                 return false;
2079
2080         zero(st);
2081         if (stat(u->meta.fragment_path, &st) < 0)
2082                 /* What, cannot access this anymore? */
2083                 return true;
2084
2085         return
2086                 u->meta.fragment_mtime &&
2087                 timespec_load(&st.st_mtim) != u->meta.fragment_mtime;
2088 }
2089
2090 void unit_reset_maintenance(Unit *u) {
2091         assert(u);
2092
2093         if (UNIT_VTABLE(u)->reset_maintenance)
2094                 UNIT_VTABLE(u)->reset_maintenance(u);
2095 }
2096
2097 Unit *unit_following(Unit *u) {
2098         assert(u);
2099
2100         if (UNIT_VTABLE(u)->following)
2101                 return UNIT_VTABLE(u)->following(u);
2102
2103         return NULL;
2104 }
2105
2106 static const char* const unit_type_table[_UNIT_TYPE_MAX] = {
2107         [UNIT_SERVICE] = "service",
2108         [UNIT_TIMER] = "timer",
2109         [UNIT_SOCKET] = "socket",
2110         [UNIT_TARGET] = "target",
2111         [UNIT_DEVICE] = "device",
2112         [UNIT_MOUNT] = "mount",
2113         [UNIT_AUTOMOUNT] = "automount",
2114         [UNIT_SNAPSHOT] = "snapshot",
2115         [UNIT_SWAP] = "swap"
2116 };
2117
2118 DEFINE_STRING_TABLE_LOOKUP(unit_type, UnitType);
2119
2120 static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = {
2121         [UNIT_STUB] = "stub",
2122         [UNIT_LOADED] = "loaded",
2123         [UNIT_FAILED] = "failed",
2124         [UNIT_MERGED] = "merged"
2125 };
2126
2127 DEFINE_STRING_TABLE_LOOKUP(unit_load_state, UnitLoadState);
2128
2129 static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = {
2130         [UNIT_ACTIVE] = "active",
2131         [UNIT_RELOADING] = "reloading",
2132         [UNIT_INACTIVE] = "inactive",
2133         [UNIT_MAINTENANCE] = "maintenance",
2134         [UNIT_ACTIVATING] = "activating",
2135         [UNIT_DEACTIVATING] = "deactivating"
2136 };
2137
2138 DEFINE_STRING_TABLE_LOOKUP(unit_active_state, UnitActiveState);
2139
2140 static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = {
2141         [UNIT_REQUIRES] = "Requires",
2142         [UNIT_REQUIRES_OVERRIDABLE] = "RequiresOverridable",
2143         [UNIT_WANTS] = "Wants",
2144         [UNIT_REQUISITE] = "Requisite",
2145         [UNIT_REQUISITE_OVERRIDABLE] = "RequisiteOverridable",
2146         [UNIT_REQUIRED_BY] = "RequiredBy",
2147         [UNIT_REQUIRED_BY_OVERRIDABLE] = "RequiredByOverridable",
2148         [UNIT_WANTED_BY] = "WantedBy",
2149         [UNIT_CONFLICTS] = "Conflicts",
2150         [UNIT_CONFLICTED_BY] = "ConflictedBy",
2151         [UNIT_BEFORE] = "Before",
2152         [UNIT_AFTER] = "After",
2153         [UNIT_REFERENCES] = "References",
2154         [UNIT_REFERENCED_BY] = "ReferencedBy",
2155         [UNIT_ON_FAILURE] = "OnFailure"
2156 };
2157
2158 DEFINE_STRING_TABLE_LOOKUP(unit_dependency, UnitDependency);
2159
2160 static const char* const kill_mode_table[_KILL_MODE_MAX] = {
2161         [KILL_CONTROL_GROUP] = "control-group",
2162         [KILL_PROCESS_GROUP] = "process-group",
2163         [KILL_PROCESS] = "process",
2164         [KILL_NONE] = "none"
2165 };
2166
2167 DEFINE_STRING_TABLE_LOOKUP(kill_mode, KillMode);