chiark / gitweb /
service: rename Type=finish to Type=oneshot and allow multiple ExecStart= lines for...
[elogind.git] / src / service.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 <errno.h>
23 #include <signal.h>
24 #include <dirent.h>
25 #include <unistd.h>
26
27 #include "unit.h"
28 #include "service.h"
29 #include "load-fragment.h"
30 #include "load-dropin.h"
31 #include "log.h"
32 #include "strv.h"
33 #include "unit-name.h"
34 #include "dbus-service.h"
35 #include "special.h"
36 #include "bus-errors.h"
37
38 #define COMMENTS "#;\n"
39 #define NEWLINES "\n\r"
40
41 typedef enum RunlevelType {
42         RUNLEVEL_UP,
43         RUNLEVEL_DOWN,
44         RUNLEVEL_SYSINIT
45 } RunlevelType;
46
47 static const struct {
48         const char *path;
49         const char *target;
50         const RunlevelType type;
51 } rcnd_table[] = {
52         /* Standard SysV runlevels for start-up */
53         { "rc1.d",  SPECIAL_RESCUE_TARGET,    RUNLEVEL_UP },
54         { "rc2.d",  SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP },
55         { "rc3.d",  SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP },
56         { "rc4.d",  SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP },
57         { "rc5.d",  SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP },
58
59 #ifdef TARGET_SUSE
60         /* SUSE style boot.d */
61         { "boot.d", SPECIAL_SYSINIT_TARGET,   RUNLEVEL_SYSINIT },
62 #endif
63
64 #ifdef TARGET_DEBIAN
65         /* Debian style rcS.d */
66         { "rcS.d",  SPECIAL_SYSINIT_TARGET,   RUNLEVEL_SYSINIT },
67 #endif
68
69         /* Standard SysV runlevels for shutdown */
70         { "rc0.d",  SPECIAL_POWEROFF_TARGET,  RUNLEVEL_DOWN },
71         { "rc6.d",  SPECIAL_REBOOT_TARGET,    RUNLEVEL_DOWN }
72
73         /* Note that the order here matters, as we read the
74            directories in this order, and we want to make sure that
75            sysv_start_priority is known when we first load the
76            unit. And that value we only know from S links. Hence
77            UP/SYSINIT must be read before DOWN */
78 };
79
80 #define RUNLEVELS_UP "12345"
81 /* #define RUNLEVELS_DOWN "06" */
82 /* #define RUNLEVELS_BOOT "bBsS" */
83
84 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
85         [SERVICE_DEAD] = UNIT_INACTIVE,
86         [SERVICE_START_PRE] = UNIT_ACTIVATING,
87         [SERVICE_START] = UNIT_ACTIVATING,
88         [SERVICE_START_POST] = UNIT_ACTIVATING,
89         [SERVICE_RUNNING] = UNIT_ACTIVE,
90         [SERVICE_EXITED] = UNIT_ACTIVE,
91         [SERVICE_RELOAD] = UNIT_RELOADING,
92         [SERVICE_STOP] = UNIT_DEACTIVATING,
93         [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
94         [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
95         [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
96         [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
97         [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
98         [SERVICE_MAINTENANCE] = UNIT_MAINTENANCE,
99         [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
100 };
101
102 static void service_init(Unit *u) {
103         Service *s = SERVICE(u);
104
105         assert(u);
106         assert(u->meta.load_state == UNIT_STUB);
107
108         s->timeout_usec = DEFAULT_TIMEOUT_USEC;
109         s->restart_usec = DEFAULT_RESTART_USEC;
110         s->timer_watch.type = WATCH_INVALID;
111         s->sysv_start_priority = -1;
112         s->socket_fd = -1;
113
114         exec_context_init(&s->exec_context);
115
116         RATELIMIT_INIT(s->ratelimit, 10*USEC_PER_SEC, 5);
117
118         s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
119 }
120
121 static void service_unwatch_control_pid(Service *s) {
122         assert(s);
123
124         if (s->control_pid <= 0)
125                 return;
126
127         unit_unwatch_pid(UNIT(s), s->control_pid);
128         s->control_pid = 0;
129 }
130
131 static void service_unwatch_main_pid(Service *s) {
132         assert(s);
133
134         if (s->main_pid <= 0)
135                 return;
136
137         unit_unwatch_pid(UNIT(s), s->main_pid);
138         s->main_pid = 0;
139 }
140
141 static int service_set_main_pid(Service *s, pid_t pid) {
142         pid_t ppid;
143
144         assert(s);
145
146         if (pid <= 1)
147                 return -EINVAL;
148
149         if (pid == getpid())
150                 return -EINVAL;
151
152         if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid())
153                 log_warning("%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
154                             s->meta.id, (unsigned long) pid);
155
156         s->main_pid = pid;
157         s->main_pid_known = true;
158
159         exec_status_start(&s->main_exec_status, pid);
160
161         return 0;
162 }
163
164 static void service_close_socket_fd(Service *s) {
165         assert(s);
166
167         if (s->socket_fd < 0)
168                 return;
169
170         close_nointr_nofail(s->socket_fd);
171         s->socket_fd = -1;
172 }
173
174 static void service_connection_unref(Service *s) {
175         assert(s);
176
177         if (!s->socket)
178                 return;
179
180         socket_connection_unref(s->socket);
181         s->socket = NULL;
182 }
183
184 static void service_done(Unit *u) {
185         Service *s = SERVICE(u);
186
187         assert(s);
188
189         free(s->pid_file);
190         s->pid_file = NULL;
191
192         free(s->sysv_path);
193         s->sysv_path = NULL;
194
195         free(s->sysv_runlevels);
196         s->sysv_runlevels = NULL;
197
198         free(s->status_text);
199         s->status_text = NULL;
200
201         exec_context_done(&s->exec_context);
202         exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
203         s->control_command = NULL;
204
205         /* This will leak a process, but at least no memory or any of
206          * our resources */
207         service_unwatch_main_pid(s);
208         service_unwatch_control_pid(s);
209
210         if (s->bus_name)  {
211                 unit_unwatch_bus_name(UNIT(u), s->bus_name);
212                 free(s->bus_name);
213                 s->bus_name = NULL;
214         }
215
216         service_close_socket_fd(s);
217         service_connection_unref(s);
218
219         unit_unwatch_timer(u, &s->timer_watch);
220 }
221
222 static char *sysv_translate_name(const char *name) {
223         char *r;
224
225         if (!(r = new(char, strlen(name) + sizeof(".service"))))
226                 return NULL;
227
228         if (startswith(name, "boot."))
229                 /* Drop SuSE-style boot. prefix */
230                 strcpy(stpcpy(r, name + 5), ".service");
231         else if (endswith(name, ".sh"))
232                 /* Drop Debian-style .sh suffix */
233                 strcpy(stpcpy(r, name) - 3, ".service");
234         else
235                 /* Normal init scripts */
236                 strcpy(stpcpy(r, name), ".service");
237
238         return r;
239 }
240
241 static int sysv_translate_facility(const char *name, char **_r) {
242
243         static const char * const table[] = {
244                 /* LSB defined facilities */
245                 "$local_fs",  SPECIAL_LOCAL_FS_TARGET,
246                 "$network",   SPECIAL_NETWORK_TARGET,
247                 "$named",     SPECIAL_NSS_LOOKUP_TARGET,
248                 "$portmap",   SPECIAL_RPCBIND_TARGET,
249                 "$remote_fs", SPECIAL_REMOTE_FS_TARGET,
250                 "$syslog",    SPECIAL_SYSLOG_TARGET,
251                 "$time",      SPECIAL_RTC_SET_TARGET,
252
253                 /* Debian extensions */
254                 "$mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
255                 "$mail-transfer-agent",  SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
256                 "$x-display-manager",    SPECIAL_DISPLAY_MANAGER_SERVICE
257         };
258
259         unsigned i;
260         char *r;
261
262         for (i = 0; i < ELEMENTSOF(table); i += 2)
263                 if (streq(table[i], name)) {
264                         if (!(r = strdup(table[i+1])))
265                                 return -ENOMEM;
266
267                         goto finish;
268                 }
269
270         if (*name == '$')
271                 return 0;
272
273         if (!(r = sysv_translate_name(name)))
274                 return -ENOMEM;
275
276 finish:
277
278         if (_r)
279                 *_r = r;
280
281         return 1;
282 }
283
284 static int sysv_fix_order(Service *s) {
285         Meta *other;
286         int r;
287
288         assert(s);
289
290         if (s->sysv_start_priority < 0)
291                 return 0;
292
293         /* For each pair of services where at least one lacks a LSB
294          * header, we use the start priority value to order things. */
295
296         LIST_FOREACH(units_per_type, other, s->meta.manager->units_per_type[UNIT_SERVICE]) {
297                 Service *t;
298                 UnitDependency d;
299                 bool special_s, special_t;
300
301                 t = (Service*) other;
302
303                 if (s == t)
304                         continue;
305
306                 if (t->sysv_start_priority < 0)
307                         continue;
308
309                 /* If both units have modern headers we don't care
310                  * about the priorities */
311                 if ((!s->sysv_path || s->sysv_has_lsb) &&
312                     (!t->sysv_path || t->sysv_has_lsb))
313                         continue;
314
315                 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
316                 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
317
318                 if (special_t && !special_s)
319                         d = UNIT_AFTER;
320                 else if (special_s && !special_t)
321                         d = UNIT_BEFORE;
322                 else if (t->sysv_start_priority < s->sysv_start_priority)
323                         d = UNIT_AFTER;
324                 else if (t->sysv_start_priority > s->sysv_start_priority)
325                         d = UNIT_BEFORE;
326                 else
327                         continue;
328
329                 /* FIXME: Maybe we should compare the name here lexicographically? */
330
331                 if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
332                         return r;
333         }
334
335         return 0;
336 }
337
338 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
339         ExecCommand *c;
340
341         if (!(c = new0(ExecCommand, 1)))
342                 return NULL;
343
344         if (!(c->path = strdup(path))) {
345                 free(c);
346                 return NULL;
347         }
348
349         if (!(c->argv = strv_new(path, arg1, NULL))) {
350                 free(c->path);
351                 free(c);
352                 return NULL;
353         }
354
355         return c;
356 }
357
358 static int sysv_exec_commands(Service *s) {
359         ExecCommand *c;
360
361         assert(s);
362         assert(s->sysv_path);
363
364         if (!(c = exec_command_new(s->sysv_path, "start")))
365                 return -ENOMEM;
366         exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
367
368         if (!(c = exec_command_new(s->sysv_path, "stop")))
369                 return -ENOMEM;
370         exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
371
372         if (!(c = exec_command_new(s->sysv_path, "reload")))
373                 return -ENOMEM;
374         exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
375
376         return 0;
377 }
378
379 static int service_load_sysv_path(Service *s, const char *path) {
380         FILE *f;
381         Unit *u;
382         unsigned line = 0;
383         int r;
384         enum {
385                 NORMAL,
386                 DESCRIPTION,
387                 LSB,
388                 LSB_DESCRIPTION
389         } state = NORMAL;
390
391         assert(s);
392         assert(path);
393
394         u = UNIT(s);
395
396         if (!(f = fopen(path, "re"))) {
397                 r = errno == ENOENT ? 0 : -errno;
398                 goto finish;
399         }
400
401         free(s->sysv_path);
402         if (!(s->sysv_path = strdup(path))) {
403                 r = -ENOMEM;
404                 goto finish;
405         }
406
407         while (!feof(f)) {
408                 char l[LINE_MAX], *t;
409
410                 if (!fgets(l, sizeof(l), f)) {
411                         if (feof(f))
412                                 break;
413
414                         r = -errno;
415                         log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
416                         goto finish;
417                 }
418
419                 line++;
420
421                 t = strstrip(l);
422                 if (*t != '#')
423                         continue;
424
425                 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
426                         state = LSB;
427                         s->sysv_has_lsb = true;
428                         continue;
429                 }
430
431                 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
432                         state = NORMAL;
433                         continue;
434                 }
435
436                 t++;
437                 t += strspn(t, WHITESPACE);
438
439                 if (state == NORMAL) {
440
441                         /* Try to parse Red Hat style chkconfig headers */
442
443                         if (startswith_no_case(t, "chkconfig:")) {
444                                 int start_priority;
445                                 char runlevels[16], *k;
446
447                                 state = NORMAL;
448
449                                 if (sscanf(t+10, "%15s %i %*i",
450                                            runlevels,
451                                            &start_priority) != 2) {
452
453                                         log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
454                                         continue;
455                                 }
456
457                                 /* A start priority gathered from the
458                                  * symlink farms is preferred over the
459                                  * data from the LSB header. */
460                                 if (start_priority < 0 || start_priority > 99)
461                                         log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
462                                 else if (s->sysv_start_priority < 0)
463                                         s->sysv_start_priority = start_priority;
464
465                                 char_array_0(runlevels);
466                                 k = delete_chars(runlevels, WHITESPACE "-");
467
468                                 if (k[0]) {
469                                         char *d;
470
471                                         if (!(d = strdup(k))) {
472                                                 r = -ENOMEM;
473                                                 goto finish;
474                                         }
475
476                                         free(s->sysv_runlevels);
477                                         s->sysv_runlevels = d;
478                                 }
479
480                         } else if (startswith_no_case(t, "description:") &&
481                                    !u->meta.description) {
482
483                                 size_t k = strlen(t);
484                                 char *d;
485
486                                 if (t[k-1] == '\\') {
487                                         state = DESCRIPTION;
488                                         t[k-1] = 0;
489                                 }
490
491                                 if (!(d = strdup(strstrip(t+12)))) {
492                                         r = -ENOMEM;
493                                         goto finish;
494                                 }
495
496                                 free(u->meta.description);
497                                 u->meta.description = d;
498
499                         } else if (startswith_no_case(t, "pidfile:")) {
500
501                                 char *fn;
502
503                                 state = NORMAL;
504
505                                 fn = strstrip(t+8);
506                                 if (!path_is_absolute(fn)) {
507                                         log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
508                                         continue;
509                                 }
510
511                                 if (!(fn = strdup(fn))) {
512                                         r = -ENOMEM;
513                                         goto finish;
514                                 }
515
516                                 free(s->pid_file);
517                                 s->pid_file = fn;
518                         }
519
520                 } else if (state == DESCRIPTION) {
521
522                         /* Try to parse Red Hat style description
523                          * continuation */
524
525                         size_t k = strlen(t);
526                         char *d;
527
528                         if (t[k-1] == '\\')
529                                 t[k-1] = 0;
530                         else
531                                 state = NORMAL;
532
533                         assert(u->meta.description);
534                         if (asprintf(&d, "%s %s", u->meta.description, strstrip(t)) < 0) {
535                                 r = -ENOMEM;
536                                 goto finish;
537                         }
538
539                         free(u->meta.description);
540                         u->meta.description = d;
541
542                 } else if (state == LSB || state == LSB_DESCRIPTION) {
543
544                         if (startswith_no_case(t, "Provides:")) {
545                                 char *i, *w;
546                                 size_t z;
547
548                                 state = LSB;
549
550                                 FOREACH_WORD_QUOTED(w, z, t+9, i) {
551                                         char *n, *m;
552
553                                         if (!(n = strndup(w, z))) {
554                                                 r = -ENOMEM;
555                                                 goto finish;
556                                         }
557
558                                         r = sysv_translate_facility(n, &m);
559                                         free(n);
560
561                                         if (r < 0)
562                                                 goto finish;
563
564                                         if (r == 0)
565                                                 continue;
566
567                                         if (unit_name_to_type(m) == UNIT_SERVICE)
568                                                 r = unit_add_name(u, m);
569                                         else if (s->sysv_enabled)
570                                                 r = unit_add_two_dependencies_by_name_inverse(u, UNIT_AFTER, UNIT_WANTS, m, NULL, true);
571                                         else
572                                                 r = unit_add_dependency_by_name_inverse(u, UNIT_AFTER, m, NULL, true);
573
574                                         free(m);
575
576                                         if (r < 0)
577                                                 goto finish;
578                                 }
579
580                         } else if (startswith_no_case(t, "Required-Start:") ||
581                                    startswith_no_case(t, "Should-Start:") ||
582                                    startswith_no_case(t, "X-Start-Before:") ||
583                                    startswith_no_case(t, "X-Start-After:")) {
584                                 char *i, *w;
585                                 size_t z;
586
587                                 state = LSB;
588
589                                 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
590                                         char *n, *m;
591
592                                         if (!(n = strndup(w, z))) {
593                                                 r = -ENOMEM;
594                                                 goto finish;
595                                         }
596
597                                         r = sysv_translate_facility(n, &m);
598                                         free(n);
599
600                                         if (r < 0)
601                                                 goto finish;
602
603                                         if (r == 0)
604                                                 continue;
605
606                                         r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
607                                         free(m);
608
609                                         if (r < 0)
610                                                 goto finish;
611                                 }
612                         } else if (startswith_no_case(t, "Default-Start:")) {
613                                 char *k, *d;
614
615                                 state = LSB;
616
617                                 k = delete_chars(t+14, WHITESPACE "-");
618
619                                 if (k[0] != 0) {
620                                         if (!(d = strdup(k))) {
621                                                 r = -ENOMEM;
622                                                 goto finish;
623                                         }
624
625                                         free(s->sysv_runlevels);
626                                         s->sysv_runlevels = d;
627                                 }
628
629                         } else if (startswith_no_case(t, "Description:") &&
630                                    !u->meta.description) {
631                                 char *d;
632
633                                 /* We use the long description only if
634                                  * no short description is set. */
635
636                                 state = LSB_DESCRIPTION;
637
638                                 if (!(d = strdup(strstrip(t+12)))) {
639                                         r = -ENOMEM;
640                                         goto finish;
641                                 }
642
643                                 free(u->meta.description);
644                                 u->meta.description = d;
645
646                         } else if (startswith_no_case(t, "Short-Description:")) {
647                                 char *d;
648
649                                 state = LSB;
650
651                                 if (!(d = strdup(strstrip(t+18)))) {
652                                         r = -ENOMEM;
653                                         goto finish;
654                                 }
655
656                                 free(u->meta.description);
657                                 u->meta.description = d;
658
659                         } else if (startswith_no_case(t, "X-Interactive:")) {
660                                 int b;
661
662                                 if ((b = parse_boolean(strstrip(t+14))) < 0) {
663                                         log_warning("[%s:%u] Couldn't parse interactive flag. Ignoring.", path, line);
664                                         continue;
665                                 }
666
667                                 if (b)
668                                         s->exec_context.std_input = EXEC_INPUT_TTY;
669                                 else
670                                         s->exec_context.std_input = EXEC_INPUT_NULL;
671
672                         } else if (state == LSB_DESCRIPTION) {
673
674                                 if (startswith(l, "#\t") || startswith(l, "#  ")) {
675                                         char *d;
676
677                                         assert(u->meta.description);
678                                         if (asprintf(&d, "%s %s", u->meta.description, t) < 0) {
679                                                 r = -ENOMEM;
680                                                 goto finish;
681                                         }
682
683                                         free(u->meta.description);
684                                         u->meta.description = d;
685                                 } else
686                                         state = LSB;
687                         }
688                 }
689         }
690
691         if ((r = sysv_exec_commands(s)) < 0)
692                 goto finish;
693
694         if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
695                 /* If there a runlevels configured for this service
696                  * but none of the standard ones, then we assume this
697                  * is some special kind of service (which might be
698                  * needed for early boot) and don't create any links
699                  * to it. */
700
701                 s->meta.default_dependencies = false;
702
703                 /* Don't timeout special services during boot (like fsck) */
704                 s->timeout_usec = 0;
705         }
706
707         /* Special setting for all SysV services */
708         s->type = SERVICE_FORKING;
709         s->valid_no_process = true;
710         s->restart = SERVICE_ONCE;
711         s->exec_context.std_output = s->meta.manager->sysv_console ? EXEC_OUTPUT_TTY : EXEC_OUTPUT_NULL;
712         s->exec_context.kill_mode = KILL_PROCESS_GROUP;
713
714         u->meta.load_state = UNIT_LOADED;
715         r = 0;
716
717 finish:
718
719         if (f)
720                 fclose(f);
721
722         return r;
723 }
724
725 static int service_load_sysv_name(Service *s, const char *name) {
726         char **p;
727
728         assert(s);
729         assert(name);
730
731         /* For SysV services we strip the boot. or .sh
732          * prefixes/suffixes. */
733         if (startswith(name, "boot.") ||
734             endswith(name, ".sh.service"))
735                 return -ENOENT;
736
737         STRV_FOREACH(p, s->meta.manager->lookup_paths.sysvinit_path) {
738                 char *path;
739                 int r;
740
741                 if (asprintf(&path, "%s/%s", *p, name) < 0)
742                         return -ENOMEM;
743
744                 assert(endswith(path, ".service"));
745                 path[strlen(path)-8] = 0;
746
747                 r = service_load_sysv_path(s, path);
748
749                 if (r >= 0 && s->meta.load_state == UNIT_STUB) {
750                         /* Try Debian style xxx.sh source'able init scripts */
751                         strcat(path, ".sh");
752                         r = service_load_sysv_path(s, path);
753                 }
754
755                 free(path);
756
757                 if (r >= 0 && s->meta.load_state == UNIT_STUB) {
758                         /* Try SUSE style boot.xxx init scripts */
759
760                         if (asprintf(&path, "%s/boot.%s", *p, name) < 0)
761                                 return -ENOMEM;
762
763                         path[strlen(path)-8] = 0;
764                         r = service_load_sysv_path(s, path);
765                         free(path);
766                 }
767
768                 if (r < 0)
769                         return r;
770
771                 if ((s->meta.load_state != UNIT_STUB))
772                         break;
773         }
774
775         return 0;
776 }
777
778 static int service_load_sysv(Service *s) {
779         const char *t;
780         Iterator i;
781         int r;
782
783         assert(s);
784
785         /* Load service data from SysV init scripts, preferably with
786          * LSB headers ... */
787
788         if (strv_isempty(s->meta.manager->lookup_paths.sysvinit_path))
789                 return 0;
790
791         if ((t = s->meta.id))
792                 if ((r = service_load_sysv_name(s, t)) < 0)
793                         return r;
794
795         if (s->meta.load_state == UNIT_STUB)
796                 SET_FOREACH(t, s->meta.names, i) {
797                         if (t == s->meta.id)
798                                 continue;
799
800                         if ((r = service_load_sysv_name(s, t)) < 0)
801                                 return r;
802
803                         if (s->meta.load_state != UNIT_STUB)
804                                 break;
805                 }
806
807         return 0;
808 }
809
810 static int service_add_bus_name(Service *s) {
811         char *n;
812         int r;
813
814         assert(s);
815         assert(s->bus_name);
816
817         if (asprintf(&n, "dbus-%s.service", s->bus_name) < 0)
818                 return 0;
819
820         r = unit_merge_by_name(UNIT(s), n);
821         free(n);
822
823         return r;
824 }
825
826 static int service_verify(Service *s) {
827         assert(s);
828
829         if (s->meta.load_state != UNIT_LOADED)
830                 return 0;
831
832         if (!s->exec_command[SERVICE_EXEC_START]) {
833                 log_error("%s lacks ExecStart setting. Refusing.", s->meta.id);
834                 return -EINVAL;
835         }
836
837         if (s->type != SERVICE_ONESHOT &&
838             s->exec_command[SERVICE_EXEC_START]->command_next) {
839                 log_error("%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", s->meta.id);
840                 return -EINVAL;
841         }
842
843         if (s->type == SERVICE_DBUS && !s->bus_name) {
844                 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", s->meta.id);
845                 return -EINVAL;
846         }
847
848         if (s->exec_context.pam_name && s->exec_context.kill_mode != KILL_CONTROL_GROUP) {
849                 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s->meta.id);
850                 return -EINVAL;
851         }
852
853         return 0;
854 }
855
856 static int service_add_default_dependencies(Service *s) {
857         int r;
858
859         assert(s);
860
861         /* Add a number of automatic dependencies useful for the
862          * majority of services. */
863
864         /* First, pull in base system */
865         if (s->meta.manager->running_as == MANAGER_SYSTEM) {
866
867                 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
868                         return r;
869
870         } else if (s->meta.manager->running_as == MANAGER_SESSION) {
871
872                 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0)
873                         return r;
874         }
875
876         /* Second, activate normal shutdown */
877         return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTED_BY, SPECIAL_SHUTDOWN_TARGET, NULL, true);
878 }
879
880 static int service_load(Unit *u) {
881         int r;
882         Service *s = SERVICE(u);
883
884         assert(s);
885
886         /* Load a .service file */
887         if ((r = unit_load_fragment(u)) < 0)
888                 return r;
889
890         /* Load a classic init script as a fallback, if we couldn't find anything */
891         if (u->meta.load_state == UNIT_STUB)
892                 if ((r = service_load_sysv(s)) < 0)
893                         return r;
894
895         /* Still nothing found? Then let's give up */
896         if (u->meta.load_state == UNIT_STUB)
897                 return -ENOENT;
898
899         /* We were able to load something, then let's add in the
900          * dropin directories. */
901         if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
902                 return r;
903
904         /* This is a new unit? Then let's add in some extras */
905         if (u->meta.load_state == UNIT_LOADED) {
906                 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
907                         return r;
908
909                 if ((r = unit_add_default_cgroup(u)) < 0)
910                         return r;
911
912                 if ((r = sysv_fix_order(s)) < 0)
913                         return r;
914
915                 if (s->bus_name) {
916                         if ((r = service_add_bus_name(s)) < 0)
917                                 return r;
918
919                         if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
920                                 return r;
921                 }
922
923                 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
924                         s->notify_access = NOTIFY_MAIN;
925
926                 if (s->type == SERVICE_DBUS || s->bus_name)
927                         if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_TARGET, NULL, true)) < 0)
928                                 return r;
929
930                 if (s->meta.default_dependencies)
931                         if ((r = service_add_default_dependencies(s)) < 0)
932                                 return r;
933         }
934
935         return service_verify(s);
936 }
937
938 static void service_dump(Unit *u, FILE *f, const char *prefix) {
939
940         ServiceExecCommand c;
941         Service *s = SERVICE(u);
942         const char *prefix2;
943         char *p2;
944
945         assert(s);
946
947         p2 = strappend(prefix, "\t");
948         prefix2 = p2 ? p2 : prefix;
949
950         fprintf(f,
951                 "%sService State: %s\n"
952                 "%sPermissionsStartOnly: %s\n"
953                 "%sRootDirectoryStartOnly: %s\n"
954                 "%sValidNoProcess: %s\n"
955                 "%sType: %s\n"
956                 "%sRestart: %s\n"
957                 "%sNotifyAccess: %s\n",
958                 prefix, service_state_to_string(s->state),
959                 prefix, yes_no(s->permissions_start_only),
960                 prefix, yes_no(s->root_directory_start_only),
961                 prefix, yes_no(s->valid_no_process),
962                 prefix, service_type_to_string(s->type),
963                 prefix, service_restart_to_string(s->restart),
964                 prefix, notify_access_to_string(s->notify_access));
965
966         if (s->control_pid > 0)
967                 fprintf(f,
968                         "%sControl PID: %lu\n",
969                         prefix, (unsigned long) s->control_pid);
970
971         if (s->main_pid > 0)
972                 fprintf(f,
973                         "%sMain PID: %lu\n",
974                         prefix, (unsigned long) s->main_pid);
975
976         if (s->pid_file)
977                 fprintf(f,
978                         "%sPIDFile: %s\n",
979                         prefix, s->pid_file);
980
981         if (s->bus_name)
982                 fprintf(f,
983                         "%sBusName: %s\n"
984                         "%sBus Name Good: %s\n",
985                         prefix, s->bus_name,
986                         prefix, yes_no(s->bus_name_good));
987
988         exec_context_dump(&s->exec_context, f, prefix);
989
990         for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
991
992                 if (!s->exec_command[c])
993                         continue;
994
995                 fprintf(f, "%s-> %s:\n",
996                         prefix, service_exec_command_to_string(c));
997
998                 exec_command_dump_list(s->exec_command[c], f, prefix2);
999         }
1000
1001         if (s->sysv_path)
1002                 fprintf(f,
1003                         "%sSysV Init Script Path: %s\n"
1004                         "%sSysV Init Script has LSB Header: %s\n",
1005                         prefix, s->sysv_path,
1006                         prefix, yes_no(s->sysv_has_lsb));
1007
1008         if (s->sysv_start_priority >= 0)
1009                 fprintf(f,
1010                         "%sSysVStartPriority: %i\n"
1011                         "%sSysVEnabled: %s\n",
1012                         prefix, s->sysv_start_priority,
1013                         prefix, yes_no(s->sysv_enabled));
1014
1015         if (s->sysv_runlevels)
1016                 fprintf(f, "%sSysVRunLevels: %s\n",
1017                         prefix, s->sysv_runlevels);
1018
1019         if (s->status_text)
1020                 fprintf(f, "%sStatus Text: %s\n",
1021                         prefix, s->status_text);
1022
1023         free(p2);
1024 }
1025
1026 static int service_load_pid_file(Service *s) {
1027         char *k;
1028         int r;
1029         pid_t pid;
1030
1031         assert(s);
1032
1033         if (s->main_pid_known)
1034                 return 0;
1035
1036         assert(s->main_pid <= 0);
1037
1038         if (!s->pid_file)
1039                 return -ENOENT;
1040
1041         if ((r = read_one_line_file(s->pid_file, &k)) < 0)
1042                 return r;
1043
1044         r = parse_pid(k, &pid);
1045         free(k);
1046
1047         if (r < 0)
1048                 return r;
1049
1050         if (kill(pid, 0) < 0 && errno != EPERM) {
1051                 log_warning("PID %lu read from file %s does not exist. Your service or init script might be broken.",
1052                             (unsigned long) pid, s->pid_file);
1053                 return -ESRCH;
1054         }
1055
1056         if ((r = service_set_main_pid(s, pid)) < 0)
1057                 return r;
1058
1059         if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1060                 /* FIXME: we need to do something here */
1061                 return r;
1062
1063         return 0;
1064 }
1065
1066 static int service_get_sockets(Service *s, Set **_set) {
1067         Set *set;
1068         Iterator i;
1069         char *t;
1070         int r;
1071
1072         assert(s);
1073         assert(_set);
1074
1075         if (s->socket_fd >= 0)
1076                 return 0;
1077
1078         /* Collects all Socket objects that belong to this
1079          * service. Note that a service might have multiple sockets
1080          * via multiple names. */
1081
1082         if (!(set = set_new(NULL, NULL)))
1083                 return -ENOMEM;
1084
1085         SET_FOREACH(t, s->meta.names, i) {
1086                 char *k;
1087                 Unit *p;
1088
1089                 /* Look for all socket objects that go by any of our
1090                  * units and collect their fds */
1091
1092                 if (!(k = unit_name_change_suffix(t, ".socket"))) {
1093                         r = -ENOMEM;
1094                         goto fail;
1095                 }
1096
1097                 p = manager_get_unit(s->meta.manager, k);
1098                 free(k);
1099
1100                 if (!p)
1101                         continue;
1102
1103                 if ((r = set_put(set, p)) < 0)
1104                         goto fail;
1105         }
1106
1107         *_set = set;
1108         return 0;
1109
1110 fail:
1111         set_free(set);
1112         return r;
1113 }
1114
1115 static int service_notify_sockets_dead(Service *s) {
1116         Iterator i;
1117         Set *set;
1118         Socket *sock;
1119         int r;
1120
1121         assert(s);
1122
1123         if (s->socket_fd >= 0)
1124                 return 0;
1125
1126         /* Notifies all our sockets when we die */
1127         if ((r = service_get_sockets(s, &set)) < 0)
1128                 return r;
1129
1130         SET_FOREACH(sock, set, i)
1131                 socket_notify_service_dead(sock);
1132
1133         set_free(set);
1134
1135         return 0;
1136 }
1137
1138 static void service_set_state(Service *s, ServiceState state) {
1139         ServiceState old_state;
1140         assert(s);
1141
1142         old_state = s->state;
1143         s->state = state;
1144
1145         if (state != SERVICE_START_PRE &&
1146             state != SERVICE_START &&
1147             state != SERVICE_START_POST &&
1148             state != SERVICE_RELOAD &&
1149             state != SERVICE_STOP &&
1150             state != SERVICE_STOP_SIGTERM &&
1151             state != SERVICE_STOP_SIGKILL &&
1152             state != SERVICE_STOP_POST &&
1153             state != SERVICE_FINAL_SIGTERM &&
1154             state != SERVICE_FINAL_SIGKILL &&
1155             state != SERVICE_AUTO_RESTART)
1156                 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1157
1158         if (state != SERVICE_START &&
1159             state != SERVICE_START_POST &&
1160             state != SERVICE_RUNNING &&
1161             state != SERVICE_RELOAD &&
1162             state != SERVICE_STOP &&
1163             state != SERVICE_STOP_SIGTERM &&
1164             state != SERVICE_STOP_SIGKILL)
1165                 service_unwatch_main_pid(s);
1166
1167         if (state != SERVICE_START_PRE &&
1168             state != SERVICE_START &&
1169             state != SERVICE_START_POST &&
1170             state != SERVICE_RELOAD &&
1171             state != SERVICE_STOP &&
1172             state != SERVICE_STOP_SIGTERM &&
1173             state != SERVICE_STOP_SIGKILL &&
1174             state != SERVICE_STOP_POST &&
1175             state != SERVICE_FINAL_SIGTERM &&
1176             state != SERVICE_FINAL_SIGKILL) {
1177                 service_unwatch_control_pid(s);
1178                 s->control_command = NULL;
1179                 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1180         }
1181
1182         if (state == SERVICE_DEAD ||
1183             state == SERVICE_STOP ||
1184             state == SERVICE_STOP_SIGTERM ||
1185             state == SERVICE_STOP_SIGKILL ||
1186             state == SERVICE_STOP_POST ||
1187             state == SERVICE_FINAL_SIGTERM ||
1188             state == SERVICE_FINAL_SIGKILL ||
1189             state == SERVICE_MAINTENANCE ||
1190             state == SERVICE_AUTO_RESTART)
1191                 service_notify_sockets_dead(s);
1192
1193         if (state != SERVICE_START_PRE &&
1194             state != SERVICE_START &&
1195             state != SERVICE_START_POST &&
1196             state != SERVICE_RUNNING &&
1197             state != SERVICE_RELOAD &&
1198             state != SERVICE_STOP &&
1199             state != SERVICE_STOP_SIGTERM &&
1200             state != SERVICE_STOP_SIGKILL &&
1201             state != SERVICE_STOP_POST &&
1202             state != SERVICE_FINAL_SIGTERM &&
1203             state != SERVICE_FINAL_SIGKILL &&
1204             !(state == SERVICE_DEAD && s->meta.job)) {
1205                 service_close_socket_fd(s);
1206                 service_connection_unref(s);
1207         }
1208
1209         /* For the inactive states unit_notify() will trim the cgroup,
1210          * but for exit we have to do that ourselves... */
1211         if (state == SERVICE_EXITED)
1212                 cgroup_bonding_trim_list(s->meta.cgroup_bondings, true);
1213
1214         if (old_state != state)
1215                 log_debug("%s changed %s -> %s", s->meta.id, service_state_to_string(old_state), service_state_to_string(state));
1216
1217         unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
1218 }
1219
1220 static int service_coldplug(Unit *u) {
1221         Service *s = SERVICE(u);
1222         int r;
1223
1224         assert(s);
1225         assert(s->state == SERVICE_DEAD);
1226
1227         if (s->deserialized_state != s->state) {
1228
1229                 if (s->deserialized_state == SERVICE_START_PRE ||
1230                     s->deserialized_state == SERVICE_START ||
1231                     s->deserialized_state == SERVICE_START_POST ||
1232                     s->deserialized_state == SERVICE_RELOAD ||
1233                     s->deserialized_state == SERVICE_STOP ||
1234                     s->deserialized_state == SERVICE_STOP_SIGTERM ||
1235                     s->deserialized_state == SERVICE_STOP_SIGKILL ||
1236                     s->deserialized_state == SERVICE_STOP_POST ||
1237                     s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1238                     s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1239                     s->deserialized_state == SERVICE_AUTO_RESTART) {
1240
1241                         if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1242                                 usec_t k;
1243
1244                                 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1245
1246                                 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1247                                         return r;
1248                         }
1249                 }
1250
1251                 if ((s->deserialized_state == SERVICE_START &&
1252                      (s->type == SERVICE_FORKING ||
1253                       s->type == SERVICE_DBUS ||
1254                       s->type == SERVICE_ONESHOT ||
1255                       s->type == SERVICE_NOTIFY)) ||
1256                     s->deserialized_state == SERVICE_START_POST ||
1257                     s->deserialized_state == SERVICE_RUNNING ||
1258                     s->deserialized_state == SERVICE_RELOAD ||
1259                     s->deserialized_state == SERVICE_STOP ||
1260                     s->deserialized_state == SERVICE_STOP_SIGTERM ||
1261                     s->deserialized_state == SERVICE_STOP_SIGKILL)
1262                         if (s->main_pid > 0)
1263                                 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1264                                         return r;
1265
1266                 if (s->deserialized_state == SERVICE_START_PRE ||
1267                     s->deserialized_state == SERVICE_START ||
1268                     s->deserialized_state == SERVICE_START_POST ||
1269                     s->deserialized_state == SERVICE_RELOAD ||
1270                     s->deserialized_state == SERVICE_STOP ||
1271                     s->deserialized_state == SERVICE_STOP_SIGTERM ||
1272                     s->deserialized_state == SERVICE_STOP_SIGKILL ||
1273                     s->deserialized_state == SERVICE_STOP_POST ||
1274                     s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1275                     s->deserialized_state == SERVICE_FINAL_SIGKILL)
1276                         if (s->control_pid > 0)
1277                                 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1278                                         return r;
1279
1280                 service_set_state(s, s->deserialized_state);
1281         }
1282
1283         return 0;
1284 }
1285
1286 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1287         Iterator i;
1288         int r;
1289         int *rfds = NULL;
1290         unsigned rn_fds = 0;
1291         Set *set;
1292         Socket *sock;
1293
1294         assert(s);
1295         assert(fds);
1296         assert(n_fds);
1297
1298         if (s->socket_fd >= 0)
1299                 return 0;
1300
1301         if ((r = service_get_sockets(s, &set)) < 0)
1302                 return r;
1303
1304         SET_FOREACH(sock, set, i) {
1305                 int *cfds;
1306                 unsigned cn_fds;
1307
1308                 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1309                         goto fail;
1310
1311                 if (!cfds)
1312                         continue;
1313
1314                 if (!rfds) {
1315                         rfds = cfds;
1316                         rn_fds = cn_fds;
1317                 } else {
1318                         int *t;
1319
1320                         if (!(t = new(int, rn_fds+cn_fds))) {
1321                                 free(cfds);
1322                                 r = -ENOMEM;
1323                                 goto fail;
1324                         }
1325
1326                         memcpy(t, rfds, rn_fds);
1327                         memcpy(t+rn_fds, cfds, cn_fds);
1328                         free(rfds);
1329                         free(cfds);
1330
1331                         rfds = t;
1332                         rn_fds = rn_fds+cn_fds;
1333                 }
1334         }
1335
1336         *fds = rfds;
1337         *n_fds = rn_fds;
1338
1339         set_free(set);
1340
1341         return 0;
1342
1343 fail:
1344         set_free(set);
1345         free(rfds);
1346
1347         return r;
1348 }
1349
1350 static int service_spawn(
1351                 Service *s,
1352                 ExecCommand *c,
1353                 bool timeout,
1354                 bool pass_fds,
1355                 bool apply_permissions,
1356                 bool apply_chroot,
1357                 bool apply_tty_stdin,
1358                 bool set_notify_socket,
1359                 pid_t *_pid) {
1360
1361         pid_t pid;
1362         int r;
1363         int *fds = NULL, *fdsbuf = NULL;
1364         unsigned n_fds = 0, n_env = 0;
1365         char **argv = NULL, **final_env = NULL, **our_env = NULL;
1366
1367         assert(s);
1368         assert(c);
1369         assert(_pid);
1370
1371         if (pass_fds ||
1372             s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1373             s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1374             s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1375
1376                 if (s->socket_fd >= 0) {
1377                         fds = &s->socket_fd;
1378                         n_fds = 1;
1379                 } else {
1380                         if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0)
1381                                 goto fail;
1382
1383                         fds = fdsbuf;
1384                 }
1385         }
1386
1387         if (timeout && s->timeout_usec) {
1388                 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1389                         goto fail;
1390         } else
1391                 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1392
1393         if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1394                 r = -ENOMEM;
1395                 goto fail;
1396         }
1397
1398         if (!(our_env = new0(char*, 3))) {
1399                 r = -ENOMEM;
1400                 goto fail;
1401         }
1402
1403         if (set_notify_socket)
1404                 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=@%s", s->meta.manager->notify_socket) < 0) {
1405                         r = -ENOMEM;
1406                         goto fail;
1407                 }
1408
1409         if (s->main_pid > 0)
1410                 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1411                         r = -ENOMEM;
1412                         goto fail;
1413                 }
1414
1415         if (!(final_env = strv_env_merge(2,
1416                                          s->meta.manager->environment,
1417                                          our_env,
1418                                          NULL))) {
1419                 r = -ENOMEM;
1420                 goto fail;
1421         }
1422
1423         r = exec_spawn(c,
1424                        argv,
1425                        &s->exec_context,
1426                        fds, n_fds,
1427                        final_env,
1428                        apply_permissions,
1429                        apply_chroot,
1430                        apply_tty_stdin,
1431                        s->meta.manager->confirm_spawn,
1432                        s->meta.cgroup_bondings,
1433                        &pid);
1434
1435         if (r < 0)
1436                 goto fail;
1437
1438
1439         if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1440                 /* FIXME: we need to do something here */
1441                 goto fail;
1442
1443         free(fdsbuf);
1444         strv_free(argv);
1445         strv_free(our_env);
1446         strv_free(final_env);
1447
1448         *_pid = pid;
1449
1450         return 0;
1451
1452 fail:
1453         free(fdsbuf);
1454         strv_free(argv);
1455         strv_free(our_env);
1456         strv_free(final_env);
1457
1458         if (timeout)
1459                 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1460
1461         return r;
1462 }
1463
1464 static int main_pid_good(Service *s) {
1465         assert(s);
1466
1467         /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1468          * don't know */
1469
1470         /* If we know the pid file, then lets just check if it is
1471          * still valid */
1472         if (s->main_pid_known)
1473                 return s->main_pid > 0;
1474
1475         /* We don't know the pid */
1476         return -EAGAIN;
1477 }
1478
1479 static int control_pid_good(Service *s) {
1480         assert(s);
1481
1482         return s->control_pid > 0;
1483 }
1484
1485 static int cgroup_good(Service *s) {
1486         int r;
1487
1488         assert(s);
1489
1490         if ((r = cgroup_bonding_is_empty_list(s->meta.cgroup_bondings)) < 0)
1491                 return r;
1492
1493         return !r;
1494 }
1495
1496 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1497         int r;
1498         assert(s);
1499
1500         if (!success)
1501                 s->failure = true;
1502
1503         if (allow_restart &&
1504             !s->forbid_restart &&
1505             (s->restart == SERVICE_RESTART_ALWAYS ||
1506              (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1507
1508                 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1509                         goto fail;
1510
1511                 service_set_state(s, SERVICE_AUTO_RESTART);
1512         } else
1513                 service_set_state(s, s->failure ? SERVICE_MAINTENANCE : SERVICE_DEAD);
1514
1515         s->forbid_restart = false;
1516
1517         return;
1518
1519 fail:
1520         log_warning("%s failed to run install restart timer: %s", s->meta.id, strerror(-r));
1521         service_enter_dead(s, false, false);
1522 }
1523
1524 static void service_enter_signal(Service *s, ServiceState state, bool success);
1525
1526 static void service_enter_stop_post(Service *s, bool success) {
1527         int r;
1528         assert(s);
1529
1530         if (!success)
1531                 s->failure = true;
1532
1533         service_unwatch_control_pid(s);
1534
1535         s->control_command_id = SERVICE_EXEC_STOP_POST;
1536         if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1537                 if ((r = service_spawn(s,
1538                                        s->control_command,
1539                                        true,
1540                                        false,
1541                                        !s->permissions_start_only,
1542                                        !s->root_directory_start_only,
1543                                        true,
1544                                        false,
1545                                        &s->control_pid)) < 0)
1546                         goto fail;
1547
1548
1549                 service_set_state(s, SERVICE_STOP_POST);
1550         } else
1551                 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
1552
1553         return;
1554
1555 fail:
1556         log_warning("%s failed to run 'stop-post' task: %s", s->meta.id, strerror(-r));
1557         service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1558 }
1559
1560 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1561         int r;
1562         bool sent = false;
1563
1564         assert(s);
1565
1566         if (!success)
1567                 s->failure = true;
1568
1569         if (s->exec_context.kill_mode != KILL_NONE) {
1570                 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? s->exec_context.kill_signal : SIGKILL;
1571
1572                 if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) {
1573
1574                         if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig)) < 0) {
1575                                 if (r != -EAGAIN && r != -ESRCH)
1576                                         goto fail;
1577                         } else
1578                                 sent = true;
1579                 }
1580
1581                 if (!sent) {
1582                         r = 0;
1583
1584                         if (s->main_pid > 0) {
1585                                 if (kill(s->exec_context.kill_mode == KILL_PROCESS ? s->main_pid : -s->main_pid, sig) < 0 && errno != ESRCH)
1586                                         r = -errno;
1587                                 else
1588                                         sent = true;
1589                         }
1590
1591                         if (s->control_pid > 0) {
1592                                 if (kill(s->exec_context.kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH)
1593                                         r = -errno;
1594                                 else
1595                                         sent = true;
1596                         }
1597
1598                         if (r < 0)
1599                                 goto fail;
1600                 }
1601         }
1602
1603         if (sent && (s->main_pid > 0 || s->control_pid > 0)) {
1604                 if (s->timeout_usec > 0)
1605                         if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1606                                 goto fail;
1607
1608                 service_set_state(s, state);
1609         } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1610                 service_enter_stop_post(s, true);
1611         else
1612                 service_enter_dead(s, true, true);
1613
1614         return;
1615
1616 fail:
1617         log_warning("%s failed to kill processes: %s", s->meta.id, strerror(-r));
1618
1619         if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1620                 service_enter_stop_post(s, false);
1621         else
1622                 service_enter_dead(s, false, true);
1623 }
1624
1625 static void service_enter_stop(Service *s, bool success) {
1626         int r;
1627
1628         assert(s);
1629
1630         if (!success)
1631                 s->failure = true;
1632
1633         service_unwatch_control_pid(s);
1634
1635         s->control_command_id = SERVICE_EXEC_STOP;
1636         if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
1637                 if ((r = service_spawn(s,
1638                                        s->control_command,
1639                                        true,
1640                                        false,
1641                                        !s->permissions_start_only,
1642                                        !s->root_directory_start_only,
1643                                        false,
1644                                        false,
1645                                        &s->control_pid)) < 0)
1646                         goto fail;
1647
1648                 service_set_state(s, SERVICE_STOP);
1649         } else
1650                 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1651
1652         return;
1653
1654 fail:
1655         log_warning("%s failed to run 'stop' task: %s", s->meta.id, strerror(-r));
1656         service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1657 }
1658
1659 static void service_enter_running(Service *s, bool success) {
1660         int main_pid_ok, cgroup_ok;
1661         assert(s);
1662
1663         if (!success)
1664                 s->failure = true;
1665
1666         main_pid_ok = main_pid_good(s);
1667         cgroup_ok = cgroup_good(s);
1668
1669         if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
1670             (s->bus_name_good || s->type != SERVICE_DBUS))
1671                 service_set_state(s, SERVICE_RUNNING);
1672         else if (s->valid_no_process)
1673                 service_set_state(s, SERVICE_EXITED);
1674         else
1675                 service_enter_stop(s, true);
1676 }
1677
1678 static void service_enter_start_post(Service *s) {
1679         int r;
1680         assert(s);
1681
1682         service_unwatch_control_pid(s);
1683
1684         s->control_command_id = SERVICE_EXEC_START_POST;
1685         if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
1686                 if ((r = service_spawn(s,
1687                                        s->control_command,
1688                                        true,
1689                                        false,
1690                                        !s->permissions_start_only,
1691                                        !s->root_directory_start_only,
1692                                        false,
1693                                        false,
1694                                        &s->control_pid)) < 0)
1695                         goto fail;
1696
1697                 service_set_state(s, SERVICE_START_POST);
1698         } else
1699                 service_enter_running(s, true);
1700
1701         return;
1702
1703 fail:
1704         log_warning("%s failed to run 'start-post' task: %s", s->meta.id, strerror(-r));
1705         service_enter_stop(s, false);
1706 }
1707
1708 static void service_enter_start(Service *s) {
1709         pid_t pid;
1710         int r;
1711
1712         assert(s);
1713
1714         assert(s->exec_command[SERVICE_EXEC_START]);
1715         assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
1716
1717         if (s->type == SERVICE_FORKING)
1718                 service_unwatch_control_pid(s);
1719         else
1720                 service_unwatch_main_pid(s);
1721
1722         s->control_command_id = SERVICE_EXEC_START;
1723         s->control_command = s->exec_command[SERVICE_EXEC_START];
1724
1725         if ((r = service_spawn(s,
1726                                s->control_command,
1727                                s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY,
1728                                true,
1729                                true,
1730                                true,
1731                                true,
1732                                s->notify_access != NOTIFY_NONE,
1733                                &pid)) < 0)
1734                 goto fail;
1735
1736         if (s->type == SERVICE_SIMPLE) {
1737                 /* For simple services we immediately start
1738                  * the START_POST binaries. */
1739
1740                 service_set_main_pid(s, pid);
1741                 service_enter_start_post(s);
1742
1743         } else  if (s->type == SERVICE_FORKING) {
1744
1745                 /* For forking services we wait until the start
1746                  * process exited. */
1747
1748                 s->control_pid = pid;
1749                 service_set_state(s, SERVICE_START);
1750
1751         } else if (s->type == SERVICE_ONESHOT ||
1752                    s->type == SERVICE_DBUS ||
1753                    s->type == SERVICE_NOTIFY) {
1754
1755                 /* For oneshot services we wait until the start
1756                  * process exited, too, but it is our main process. */
1757
1758                 /* For D-Bus services we know the main pid right away,
1759                  * but wait for the bus name to appear on the
1760                  * bus. Notify services are similar. */
1761
1762                 service_set_main_pid(s, pid);
1763                 service_set_state(s, SERVICE_START);
1764         } else
1765                 assert_not_reached("Unknown service type");
1766
1767         return;
1768
1769 fail:
1770         log_warning("%s failed to run 'start' task: %s", s->meta.id, strerror(-r));
1771         service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1772 }
1773
1774 static void service_enter_start_pre(Service *s) {
1775         int r;
1776
1777         assert(s);
1778
1779         service_unwatch_control_pid(s);
1780
1781         s->control_command_id = SERVICE_EXEC_START_PRE;
1782         if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
1783                 if ((r = service_spawn(s,
1784                                        s->control_command,
1785                                        true,
1786                                        false,
1787                                        !s->permissions_start_only,
1788                                        !s->root_directory_start_only,
1789                                        true,
1790                                        false,
1791                                        &s->control_pid)) < 0)
1792                         goto fail;
1793
1794                 service_set_state(s, SERVICE_START_PRE);
1795         } else
1796                 service_enter_start(s);
1797
1798         return;
1799
1800 fail:
1801         log_warning("%s failed to run 'start-pre' task: %s", s->meta.id, strerror(-r));
1802         service_enter_dead(s, false, true);
1803 }
1804
1805 static void service_enter_restart(Service *s) {
1806         int r;
1807         DBusError error;
1808
1809         assert(s);
1810         dbus_error_init(&error);
1811
1812         service_enter_dead(s, true, false);
1813
1814         if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL, NULL)) < 0)
1815                 goto fail;
1816
1817         log_debug("%s scheduled restart job.", s->meta.id);
1818         return;
1819
1820 fail:
1821         log_warning("%s failed to schedule restart job: %s", s->meta.id, bus_error(&error, -r));
1822         service_enter_dead(s, false, false);
1823
1824         dbus_error_free(&error);
1825 }
1826
1827 static void service_enter_reload(Service *s) {
1828         int r;
1829
1830         assert(s);
1831
1832         service_unwatch_control_pid(s);
1833
1834         s->control_command_id = SERVICE_EXEC_RELOAD;
1835         if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
1836                 if ((r = service_spawn(s,
1837                                        s->control_command,
1838                                        true,
1839                                        false,
1840                                        !s->permissions_start_only,
1841                                        !s->root_directory_start_only,
1842                                        false,
1843                                        false,
1844                                        &s->control_pid)) < 0)
1845                         goto fail;
1846
1847                 service_set_state(s, SERVICE_RELOAD);
1848         } else
1849                 service_enter_running(s, true);
1850
1851         return;
1852
1853 fail:
1854         log_warning("%s failed to run 'reload' task: %s", s->meta.id, strerror(-r));
1855         service_enter_stop(s, false);
1856 }
1857
1858 static void service_run_next_control(Service *s, bool success) {
1859         int r;
1860
1861         assert(s);
1862         assert(s->control_command);
1863         assert(s->control_command->command_next);
1864
1865         if (!success)
1866                 s->failure = true;
1867
1868         assert(s->control_command_id != SERVICE_EXEC_START);
1869
1870         s->control_command = s->control_command->command_next;
1871         service_unwatch_control_pid(s);
1872
1873         if ((r = service_spawn(s,
1874                                s->control_command,
1875                                true,
1876                                false,
1877                                !s->permissions_start_only,
1878                                !s->root_directory_start_only,
1879                                s->control_command_id == SERVICE_EXEC_START_PRE ||
1880                                s->control_command_id == SERVICE_EXEC_STOP_POST,
1881                                false,
1882                                &s->control_pid)) < 0)
1883                 goto fail;
1884
1885         return;
1886
1887 fail:
1888         log_warning("%s failed to run next control task: %s", s->meta.id, strerror(-r));
1889
1890         if (s->state == SERVICE_START_PRE)
1891                 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1892         else if (s->state == SERVICE_STOP)
1893                 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1894         else if (s->state == SERVICE_STOP_POST)
1895                 service_enter_dead(s, false, true);
1896         else
1897                 service_enter_stop(s, false);
1898 }
1899
1900 static void service_run_next_main(Service *s, bool success) {
1901         pid_t pid;
1902         int r;
1903
1904         assert(s);
1905         assert(s->control_command);
1906         assert(s->control_command->command_next);
1907
1908         if (!success)
1909                 s->failure = true;
1910
1911         assert(s->control_command_id == SERVICE_EXEC_START);
1912         assert(s->type == SERVICE_ONESHOT);
1913
1914         s->control_command = s->control_command->command_next;
1915         service_unwatch_main_pid(s);
1916
1917         if ((r = service_spawn(s,
1918                                s->control_command,
1919                                false,
1920                                true,
1921                                true,
1922                                true,
1923                                true,
1924                                s->notify_access != NOTIFY_NONE,
1925                                &pid)) < 0)
1926                 goto fail;
1927
1928         service_set_main_pid(s, pid);
1929
1930         return;
1931
1932 fail:
1933         log_warning("%s failed to run next main task: %s", s->meta.id, strerror(-r));
1934         service_enter_stop(s, false);
1935 }
1936
1937 static int service_start(Unit *u) {
1938         Service *s = SERVICE(u);
1939
1940         assert(s);
1941
1942         /* We cannot fulfill this request right now, try again later
1943          * please! */
1944         if (s->state == SERVICE_STOP ||
1945             s->state == SERVICE_STOP_SIGTERM ||
1946             s->state == SERVICE_STOP_SIGKILL ||
1947             s->state == SERVICE_STOP_POST ||
1948             s->state == SERVICE_FINAL_SIGTERM ||
1949             s->state == SERVICE_FINAL_SIGKILL)
1950                 return -EAGAIN;
1951
1952         /* Already on it! */
1953         if (s->state == SERVICE_START_PRE ||
1954             s->state == SERVICE_START ||
1955             s->state == SERVICE_START_POST)
1956                 return 0;
1957
1958         assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTENANCE || s->state == SERVICE_AUTO_RESTART);
1959
1960         /* Make sure we don't enter a busy loop of some kind. */
1961         if (!ratelimit_test(&s->ratelimit)) {
1962                 log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id);
1963                 return -ECANCELED;
1964         }
1965
1966         if ((s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1967              s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1968              s->exec_context.std_error == EXEC_OUTPUT_SOCKET) &&
1969             s->socket_fd < 0) {
1970                 log_warning("%s can only be started with a per-connection socket.", u->meta.id);
1971                 return -EINVAL;
1972         }
1973
1974         s->failure = false;
1975         s->main_pid_known = false;
1976         s->forbid_restart = false;
1977
1978         service_enter_start_pre(s);
1979         return 0;
1980 }
1981
1982 static int service_stop(Unit *u) {
1983         Service *s = SERVICE(u);
1984
1985         assert(s);
1986
1987         /* This is a user request, so don't do restarts on this
1988          * shutdown. */
1989         s->forbid_restart = true;
1990
1991         /* Already on it */
1992         if (s->state == SERVICE_STOP ||
1993             s->state == SERVICE_STOP_SIGTERM ||
1994             s->state == SERVICE_STOP_SIGKILL ||
1995             s->state == SERVICE_STOP_POST ||
1996             s->state == SERVICE_FINAL_SIGTERM ||
1997             s->state == SERVICE_FINAL_SIGKILL)
1998                 return 0;
1999
2000         /* Don't allow a restart */
2001         if (s->state == SERVICE_AUTO_RESTART) {
2002                 service_set_state(s, SERVICE_DEAD);
2003                 return 0;
2004         }
2005
2006         /* If there's already something running we go directly into
2007          * kill mode. */
2008         if (s->state == SERVICE_START_PRE ||
2009             s->state == SERVICE_START ||
2010             s->state == SERVICE_START_POST ||
2011             s->state == SERVICE_RELOAD) {
2012                 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
2013                 return 0;
2014         }
2015
2016         assert(s->state == SERVICE_RUNNING ||
2017                s->state == SERVICE_EXITED);
2018
2019         service_enter_stop(s, true);
2020         return 0;
2021 }
2022
2023 static int service_reload(Unit *u) {
2024         Service *s = SERVICE(u);
2025
2026         assert(s);
2027
2028         assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2029
2030         service_enter_reload(s);
2031         return 0;
2032 }
2033
2034 static bool service_can_reload(Unit *u) {
2035         Service *s = SERVICE(u);
2036
2037         assert(s);
2038
2039         return !!s->exec_command[SERVICE_EXEC_RELOAD];
2040 }
2041
2042 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2043         Service *s = SERVICE(u);
2044
2045         assert(u);
2046         assert(f);
2047         assert(fds);
2048
2049         unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2050         unit_serialize_item(u, f, "failure", yes_no(s->failure));
2051
2052         if (s->control_pid > 0)
2053                 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
2054
2055         if (s->main_pid_known && s->main_pid > 0)
2056                 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2057
2058         unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2059
2060         if (s->status_text)
2061                 unit_serialize_item(u, f, "status-text", s->status_text);
2062
2063         /* There's a minor uncleanliness here: if there are multiple
2064          * commands attached here, we will start from the first one
2065          * again */
2066         if (s->control_command_id >= 0)
2067                 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
2068
2069         if (s->socket_fd >= 0) {
2070                 int copy;
2071
2072                 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2073                         return copy;
2074
2075                 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2076         }
2077
2078         if (s->main_exec_status.pid > 0) {
2079                 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid);
2080
2081                 if (s->main_exec_status.start_timestamp.realtime > 0) {
2082                         unit_serialize_item_format(u, f, "main-exec-status-start-realtime",
2083                                                    "%llu", (unsigned long long) s->main_exec_status.start_timestamp.realtime);
2084
2085                         unit_serialize_item_format(u, f, "main-exec-status-start-monotonic",
2086                                                    "%llu", (unsigned long long) s->main_exec_status.start_timestamp.monotonic);
2087                 }
2088
2089                 if (s->main_exec_status.exit_timestamp.realtime > 0) {
2090                         unit_serialize_item_format(u, f, "main-exec-status-exit-realtime",
2091                                                    "%llu", (unsigned long long) s->main_exec_status.exit_timestamp.realtime);
2092                         unit_serialize_item_format(u, f, "main-exec-status-exit-monotonic",
2093                                                    "%llu", (unsigned long long) s->main_exec_status.exit_timestamp.monotonic);
2094
2095                         unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2096                         unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2097                 }
2098         }
2099
2100         return 0;
2101 }
2102
2103 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2104         Service *s = SERVICE(u);
2105
2106         assert(u);
2107         assert(key);
2108         assert(value);
2109         assert(fds);
2110
2111         if (streq(key, "state")) {
2112                 ServiceState state;
2113
2114                 if ((state = service_state_from_string(value)) < 0)
2115                         log_debug("Failed to parse state value %s", value);
2116                 else
2117                         s->deserialized_state = state;
2118         } else if (streq(key, "failure")) {
2119                 int b;
2120
2121                 if ((b = parse_boolean(value)) < 0)
2122                         log_debug("Failed to parse failure value %s", value);
2123                 else
2124                         s->failure = b || s->failure;
2125         } else if (streq(key, "control-pid")) {
2126                 pid_t pid;
2127
2128                 if (parse_pid(value, &pid) < 0)
2129                         log_debug("Failed to parse control-pid value %s", value);
2130                 else
2131                         s->control_pid = pid;
2132         } else if (streq(key, "main-pid")) {
2133                 pid_t pid;
2134
2135                 if (parse_pid(value, &pid) < 0)
2136                         log_debug("Failed to parse main-pid value %s", value);
2137                 else
2138                         service_set_main_pid(s, (pid_t) pid);
2139         } else if (streq(key, "main-pid-known")) {
2140                 int b;
2141
2142                 if ((b = parse_boolean(value)) < 0)
2143                         log_debug("Failed to parse main-pid-known value %s", value);
2144                 else
2145                         s->main_pid_known = b;
2146         } else if (streq(key, "status-text")) {
2147                 char *t;
2148
2149                 if ((t = strdup(value))) {
2150                         free(s->status_text);
2151                         s->status_text = t;
2152                 }
2153
2154         } else if (streq(key, "control-command")) {
2155                 ServiceExecCommand id;
2156
2157                 if ((id = service_exec_command_from_string(value)) < 0)
2158                         log_debug("Failed to parse exec-command value %s", value);
2159                 else {
2160                         s->control_command_id = id;
2161                         s->control_command = s->exec_command[id];
2162                 }
2163         } else if (streq(key, "socket-fd")) {
2164                 int fd;
2165
2166                 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2167                         log_debug("Failed to parse socket-fd value %s", value);
2168                 else {
2169
2170                         if (s->socket_fd >= 0)
2171                                 close_nointr_nofail(s->socket_fd);
2172                         s->socket_fd = fdset_remove(fds, fd);
2173                 }
2174         } else if (streq(key, "main-exec-status-pid")) {
2175                 pid_t pid;
2176
2177                 if (parse_pid(value, &pid) < 0)
2178                         log_debug("Failed to parse main-exec-status-pid value %s", value);
2179                 else
2180                         s->main_exec_status.pid = pid;
2181         } else if (streq(key, "main-exec-status-code")) {
2182                 int i;
2183
2184                 if (safe_atoi(value, &i) < 0)
2185                         log_debug("Failed to parse main-exec-status-code value %s", value);
2186                 else
2187                         s->main_exec_status.code = i;
2188         } else if (streq(key, "main-exec-status-status")) {
2189                 int i;
2190
2191                 if (safe_atoi(value, &i) < 0)
2192                         log_debug("Failed to parse main-exec-status-status value %s", value);
2193                 else
2194                         s->main_exec_status.status = i;
2195         } else if (streq(key, "main-exec-status-start-realtime")) {
2196                 uint64_t k;
2197
2198                 if (safe_atou64(value, &k) < 0)
2199                         log_debug("Failed to parse main-exec-status-start-realtime value %s", value);
2200                 else
2201                         s->main_exec_status.start_timestamp.realtime = (usec_t) k;
2202         } else if (streq(key, "main-exec-status-start-monotonic")) {
2203                 uint64_t k;
2204
2205                 if (safe_atou64(value, &k) < 0)
2206                         log_debug("Failed to parse main-exec-status-start-monotonic value %s", value);
2207                 else
2208                         s->main_exec_status.start_timestamp.monotonic = (usec_t) k;
2209         } else if (streq(key, "main-exec-status-exit-realtime")) {
2210                 uint64_t k;
2211
2212                 if (safe_atou64(value, &k) < 0)
2213                         log_debug("Failed to parse main-exec-status-exit-realtime value %s", value);
2214                 else
2215                         s->main_exec_status.exit_timestamp.realtime = (usec_t) k;
2216         } else if (streq(key, "main-exec-status-exit-monotonic")) {
2217                 uint64_t k;
2218
2219                 if (safe_atou64(value, &k) < 0)
2220                         log_debug("Failed to parse main-exec-status-exit-monotonic value %s", value);
2221                 else
2222                         s->main_exec_status.exit_timestamp.monotonic = (usec_t) k;
2223         } else
2224                 log_debug("Unknown serialization key '%s'", key);
2225
2226         return 0;
2227 }
2228
2229 static UnitActiveState service_active_state(Unit *u) {
2230         assert(u);
2231
2232         return state_translation_table[SERVICE(u)->state];
2233 }
2234
2235 static const char *service_sub_state_to_string(Unit *u) {
2236         assert(u);
2237
2238         return service_state_to_string(SERVICE(u)->state);
2239 }
2240
2241 static bool service_check_gc(Unit *u) {
2242         Service *s = SERVICE(u);
2243
2244         assert(s);
2245
2246         return !!s->sysv_path;
2247 }
2248
2249 static bool service_check_snapshot(Unit *u) {
2250         Service *s = SERVICE(u);
2251
2252         assert(s);
2253
2254         return !s->got_socket_fd;
2255 }
2256
2257 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2258         Service *s = SERVICE(u);
2259         bool success;
2260
2261         assert(s);
2262         assert(pid >= 0);
2263
2264         success = is_clean_exit(code, status);
2265
2266         if (s->main_pid == pid) {
2267
2268                 s->main_pid = 0;
2269                 exec_status_exit(&s->main_exec_status, pid, code, status);
2270
2271                 if (s->type != SERVICE_FORKING && s->control_command) {
2272                         s->control_command->exec_status = s->main_exec_status;
2273
2274                         if (s->control_command->ignore)
2275                                 success = true;
2276                 }
2277
2278                 log_full(success ? LOG_DEBUG : LOG_NOTICE,
2279                          "%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
2280                 s->failure = s->failure || !success;
2281
2282                 if (s->control_command &&
2283                     s->control_command->command_next &&
2284                     success) {
2285
2286                         /* There is another command to *
2287                          * execute, so let's do that. */
2288
2289                         log_debug("%s running next main command for state %s", u->meta.id, service_state_to_string(s->state));
2290                         service_run_next_main(s, success);
2291
2292                 } else {
2293
2294                         /* The service exited, so the service is officially
2295                          * gone. */
2296
2297                         s->control_command = NULL;
2298                         s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2299
2300                         switch (s->state) {
2301
2302                         case SERVICE_START_POST:
2303                         case SERVICE_RELOAD:
2304                         case SERVICE_STOP:
2305                                 /* Need to wait until the operation is
2306                                  * done */
2307                                 break;
2308
2309                         case SERVICE_START:
2310                                 if (s->type == SERVICE_ONESHOT) {
2311                                         /* This was our main goal, so let's go on */
2312                                         if (success)
2313                                                 service_enter_start_post(s);
2314                                         else
2315                                                 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2316                                         break;
2317                                 } else {
2318                                         assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY);
2319
2320                                         /* Fall through */
2321                                 }
2322
2323                         case SERVICE_RUNNING:
2324                                 service_enter_running(s, success);
2325                                 break;
2326
2327                         case SERVICE_STOP_SIGTERM:
2328                         case SERVICE_STOP_SIGKILL:
2329
2330                                 if (!control_pid_good(s))
2331                                         service_enter_stop_post(s, success);
2332
2333                                 /* If there is still a control process, wait for that first */
2334                                 break;
2335
2336                         default:
2337                                 assert_not_reached("Uh, main process died at wrong time.");
2338                         }
2339                 }
2340
2341         } else if (s->control_pid == pid) {
2342
2343                 s->control_pid = 0;
2344
2345                 if (s->control_command) {
2346                         exec_status_exit(&s->control_command->exec_status, pid, code, status);
2347
2348                         if (s->control_command->ignore)
2349                                 success = true;
2350                 }
2351
2352                log_full(success ? LOG_DEBUG : LOG_NOTICE,
2353                          "%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
2354                 s->failure = s->failure || !success;
2355
2356                 if (s->control_command &&
2357                     s->control_command->command_next &&
2358                     success) {
2359
2360                         /* There is another command to *
2361                          * execute, so let's do that. */
2362
2363                         log_debug("%s running next control command for state %s", u->meta.id, service_state_to_string(s->state));
2364                         service_run_next_control(s, success);
2365
2366                 } else {
2367                         /* No further commands for this step, so let's
2368                          * figure out what to do next */
2369
2370                         s->control_command = NULL;
2371                         s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2372
2373                         log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state));
2374
2375                         switch (s->state) {
2376
2377                         case SERVICE_START_PRE:
2378                                 if (success)
2379                                         service_enter_start(s);
2380                                 else
2381                                         service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2382                                 break;
2383
2384                         case SERVICE_START:
2385                                 assert(s->type == SERVICE_FORKING);
2386
2387                                 /* Let's try to load the pid
2388                                  * file here if we can. We
2389                                  * ignore the return value,
2390                                  * since the PID file might
2391                                  * actually be created by a
2392                                  * START_POST script */
2393
2394                                 if (success) {
2395                                         if (s->pid_file)
2396                                                 service_load_pid_file(s);
2397
2398                                         service_enter_start_post(s);
2399                                 } else
2400                                         service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2401
2402                                 break;
2403
2404                         case SERVICE_START_POST:
2405                                 if (success && s->pid_file && !s->main_pid_known) {
2406                                         int r;
2407
2408                                         /* Hmm, let's see if we can
2409                                          * load the pid now after the
2410                                          * start-post scripts got
2411                                          * executed. */
2412
2413                                         if ((r = service_load_pid_file(s)) < 0)
2414                                                 log_warning("%s: failed to load PID file %s: %s", s->meta.id, s->pid_file, strerror(-r));
2415                                 }
2416
2417                                 /* Fall through */
2418
2419                         case SERVICE_RELOAD:
2420                                 if (success)
2421                                         service_enter_running(s, true);
2422                                 else
2423                                         service_enter_stop(s, false);
2424
2425                                 break;
2426
2427                         case SERVICE_STOP:
2428                                 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
2429                                 break;
2430
2431                         case SERVICE_STOP_SIGTERM:
2432                         case SERVICE_STOP_SIGKILL:
2433                                 if (main_pid_good(s) <= 0)
2434                                         service_enter_stop_post(s, success);
2435
2436                                 /* If there is still a service
2437                                  * process around, wait until
2438                                  * that one quit, too */
2439                                 break;
2440
2441                         case SERVICE_STOP_POST:
2442                         case SERVICE_FINAL_SIGTERM:
2443                         case SERVICE_FINAL_SIGKILL:
2444                                 service_enter_dead(s, success, true);
2445                                 break;
2446
2447                         default:
2448                                 assert_not_reached("Uh, control process died at wrong time.");
2449                         }
2450                 }
2451         }
2452 }
2453
2454 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
2455         Service *s = SERVICE(u);
2456
2457         assert(s);
2458         assert(elapsed == 1);
2459
2460         assert(w == &s->timer_watch);
2461
2462         switch (s->state) {
2463
2464         case SERVICE_START_PRE:
2465         case SERVICE_START:
2466                 log_warning("%s operation timed out. Terminating.", u->meta.id);
2467                 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2468                 break;
2469
2470         case SERVICE_START_POST:
2471         case SERVICE_RELOAD:
2472                 log_warning("%s operation timed out. Stopping.", u->meta.id);
2473                 service_enter_stop(s, false);
2474                 break;
2475
2476         case SERVICE_STOP:
2477                 log_warning("%s stopping timed out. Terminating.", u->meta.id);
2478                 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2479                 break;
2480
2481         case SERVICE_STOP_SIGTERM:
2482                 log_warning("%s stopping timed out. Killing.", u->meta.id);
2483                 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
2484                 break;
2485
2486         case SERVICE_STOP_SIGKILL:
2487                 /* Uh, wie sent a SIGKILL and it is still not gone?
2488                  * Must be something we cannot kill, so let's just be
2489                  * weirded out and continue */
2490
2491                 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
2492                 service_enter_stop_post(s, false);
2493                 break;
2494
2495         case SERVICE_STOP_POST:
2496                 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
2497                 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2498                 break;
2499
2500         case SERVICE_FINAL_SIGTERM:
2501                 log_warning("%s stopping timed out (2). Killing.", u->meta.id);
2502                 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
2503                 break;
2504
2505         case SERVICE_FINAL_SIGKILL:
2506                 log_warning("%s still around after SIGKILL (2). Entering maintenance mode.", u->meta.id);
2507                 service_enter_dead(s, false, true);
2508                 break;
2509
2510         case SERVICE_AUTO_RESTART:
2511                 log_info("%s holdoff time over, scheduling restart.", u->meta.id);
2512                 service_enter_restart(s);
2513                 break;
2514
2515         default:
2516                 assert_not_reached("Timeout at wrong time.");
2517         }
2518 }
2519
2520 static void service_cgroup_notify_event(Unit *u) {
2521         Service *s = SERVICE(u);
2522
2523         assert(u);
2524
2525         log_debug("%s: cgroup is empty", u->meta.id);
2526
2527         switch (s->state) {
2528
2529                 /* Waiting for SIGCHLD is usually more interesting,
2530                  * because it includes return codes/signals. Which is
2531                  * why we ignore the cgroup events for most cases,
2532                  * except when we don't know pid which to expect the
2533                  * SIGCHLD for. */
2534
2535         case SERVICE_RUNNING:
2536                 service_enter_running(s, true);
2537                 break;
2538
2539         default:
2540                 ;
2541         }
2542 }
2543
2544 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
2545         Service *s = SERVICE(u);
2546         const char *e;
2547
2548         assert(u);
2549
2550         if (s->notify_access == NOTIFY_NONE) {
2551                 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
2552                             u->meta.id, (unsigned long) pid);
2553                 return;
2554         }
2555
2556         if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
2557                 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
2558                             u->meta.id, (unsigned long) pid, (unsigned long) s->main_pid);
2559                 return;
2560         }
2561
2562         log_debug("%s: Got message", u->meta.id);
2563
2564         /* Interpret MAINPID= */
2565         if ((e = strv_find_prefix(tags, "MAINPID=")) &&
2566             (s->state == SERVICE_START ||
2567              s->state == SERVICE_START_POST ||
2568              s->state == SERVICE_RUNNING ||
2569              s->state == SERVICE_RELOAD)) {
2570
2571                 if (parse_pid(e + 8, &pid) < 0)
2572                         log_warning("Failed to parse notification message %s", e);
2573                 else {
2574                         log_debug("%s: got %s", u->meta.id, e);
2575                         service_set_main_pid(s, pid);
2576                 }
2577         }
2578
2579         /* Interpret READY= */
2580         if (s->type == SERVICE_NOTIFY &&
2581             s->state == SERVICE_START &&
2582             strv_find(tags, "READY=1")) {
2583                 log_debug("%s: got READY=1", u->meta.id);
2584
2585                 service_enter_start_post(s);
2586         }
2587
2588         /* Interpret STATUS= */
2589         if ((e = strv_find_prefix(tags, "STATUS="))) {
2590                 char *t;
2591
2592                 if (e[7]) {
2593                         if (!(t = strdup(e+7))) {
2594                                 log_error("Failed to allocate string.");
2595                                 return;
2596                         }
2597
2598                         log_debug("%s: got %s", u->meta.id, e);
2599
2600                         free(s->status_text);
2601                         s->status_text = t;
2602                 } else {
2603                         free(s->status_text);
2604                         s->status_text = NULL;
2605                 }
2606
2607         }
2608 }
2609
2610 static int service_enumerate(Manager *m) {
2611         char **p;
2612         unsigned i;
2613         DIR *d = NULL;
2614         char *path = NULL, *fpath = NULL, *name = NULL;
2615         int r;
2616
2617         assert(m);
2618
2619         STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
2620                 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
2621                         struct dirent *de;
2622
2623                         free(path);
2624                         path = NULL;
2625                         if (asprintf(&path, "%s/%s", *p, rcnd_table[i].path) < 0) {
2626                                 r = -ENOMEM;
2627                                 goto finish;
2628                         }
2629
2630                         if (d)
2631                                 closedir(d);
2632
2633                         if (!(d = opendir(path))) {
2634                                 if (errno != ENOENT)
2635                                         log_warning("opendir() failed on %s: %s", path, strerror(errno));
2636
2637                                 continue;
2638                         }
2639
2640                         while ((de = readdir(d))) {
2641                                 Unit *service;
2642                                 int a, b;
2643
2644                                 if (ignore_file(de->d_name))
2645                                         continue;
2646
2647                                 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
2648                                         continue;
2649
2650                                 if (strlen(de->d_name) < 4)
2651                                         continue;
2652
2653                                 a = undecchar(de->d_name[1]);
2654                                 b = undecchar(de->d_name[2]);
2655
2656                                 if (a < 0 || b < 0)
2657                                         continue;
2658
2659                                 free(fpath);
2660                                 fpath = NULL;
2661                                 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i].path, de->d_name) < 0) {
2662                                         r = -ENOMEM;
2663                                         goto finish;
2664                                 }
2665
2666                                 if (access(fpath, X_OK) < 0) {
2667
2668                                         if (errno != ENOENT)
2669                                                 log_warning("access() failed on %s: %s", fpath, strerror(errno));
2670
2671                                         continue;
2672                                 }
2673
2674                                 free(name);
2675                                 if (!(name = sysv_translate_name(de->d_name + 3))) {
2676                                         r = -ENOMEM;
2677                                         goto finish;
2678                                 }
2679
2680                                 if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
2681                                         log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
2682                                         continue;
2683                                 }
2684
2685                                 if (de->d_name[0] == 'S' &&
2686                                     (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
2687                                         SERVICE(service)->sysv_start_priority =
2688                                                 MAX(a*10 + b, SERVICE(service)->sysv_start_priority);
2689                                         SERVICE(service)->sysv_enabled = true;
2690                                 }
2691
2692                                 manager_dispatch_load_queue(m);
2693                                 service = unit_follow_merge(service);
2694
2695                                 /* If this is a native service, rely
2696                                  * on native ways to pull in a
2697                                  * service, don't pull it in via sysv
2698                                  * rcN.d links. */
2699                                 if (service->meta.fragment_path)
2700                                         continue;
2701
2702                                 if (de->d_name[0] == 'S') {
2703
2704                                         if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
2705                                                 goto finish;
2706
2707                                 } else if (de->d_name[0] == 'K' &&
2708                                            (rcnd_table[i].type == RUNLEVEL_DOWN ||
2709                                             rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
2710
2711                                         /* We honour K links only for
2712                                          * halt/reboot. For the normal
2713                                          * runlevels we assume the
2714                                          * stop jobs will be
2715                                          * implicitly added by the
2716                                          * core logic. Also, we don't
2717                                          * really distuingish here
2718                                          * between the runlevels 0 and
2719                                          * 6 and just add them to the
2720                                          * special shutdown target. On
2721                                          * SUSE the boot.d/ runlevel
2722                                          * is also used for shutdown,
2723                                          * so we add links for that
2724                                          * too to the shutdown
2725                                          * target.*/
2726
2727                                         if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
2728                                                 goto finish;
2729                                 }
2730                         }
2731                 }
2732
2733         r = 0;
2734
2735 finish:
2736         free(path);
2737         free(fpath);
2738         free(name);
2739
2740         if (d)
2741                 closedir(d);
2742
2743         return r;
2744 }
2745
2746 static void service_bus_name_owner_change(
2747                 Unit *u,
2748                 const char *name,
2749                 const char *old_owner,
2750                 const char *new_owner) {
2751
2752         Service *s = SERVICE(u);
2753
2754         assert(s);
2755         assert(name);
2756
2757         assert(streq(s->bus_name, name));
2758         assert(old_owner || new_owner);
2759
2760         if (old_owner && new_owner)
2761                 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner);
2762         else if (old_owner)
2763                 log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner);
2764         else
2765                 log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner);
2766
2767         s->bus_name_good = !!new_owner;
2768
2769         if (s->type == SERVICE_DBUS) {
2770
2771                 /* service_enter_running() will figure out what to
2772                  * do */
2773                 if (s->state == SERVICE_RUNNING)
2774                         service_enter_running(s, true);
2775                 else if (s->state == SERVICE_START && new_owner)
2776                         service_enter_start_post(s);
2777
2778         } else if (new_owner &&
2779                    s->main_pid <= 0 &&
2780                    (s->state == SERVICE_START ||
2781                     s->state == SERVICE_START_POST ||
2782                     s->state == SERVICE_RUNNING ||
2783                     s->state == SERVICE_RELOAD)) {
2784
2785                 /* Try to acquire PID from bus service */
2786                 log_debug("Trying to acquire PID from D-Bus name...");
2787
2788                 bus_query_pid(u->meta.manager, name);
2789         }
2790 }
2791
2792 static void service_bus_query_pid_done(
2793                 Unit *u,
2794                 const char *name,
2795                 pid_t pid) {
2796
2797         Service *s = SERVICE(u);
2798
2799         assert(s);
2800         assert(name);
2801
2802         log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid);
2803
2804         if (s->main_pid <= 0 &&
2805             (s->state == SERVICE_START ||
2806              s->state == SERVICE_START_POST ||
2807              s->state == SERVICE_RUNNING ||
2808              s->state == SERVICE_RELOAD))
2809                 service_set_main_pid(s, pid);
2810 }
2811
2812 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
2813         assert(s);
2814         assert(fd >= 0);
2815
2816         /* This is called by the socket code when instantiating a new
2817          * service for a stream socket and the socket needs to be
2818          * configured. */
2819
2820         if (s->meta.load_state != UNIT_LOADED)
2821                 return -EINVAL;
2822
2823         if (s->socket_fd >= 0)
2824                 return -EBUSY;
2825
2826         if (s->state != SERVICE_DEAD)
2827                 return -EAGAIN;
2828
2829         s->socket_fd = fd;
2830         s->got_socket_fd = true;
2831         s->socket = sock;
2832
2833         return 0;
2834 }
2835
2836 static void service_reset_maintenance(Unit *u) {
2837         Service *s = SERVICE(u);
2838
2839         assert(s);
2840
2841         if (s->state == SERVICE_MAINTENANCE)
2842                 service_set_state(s, SERVICE_DEAD);
2843
2844         s->failure = false;
2845 }
2846
2847 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2848         [SERVICE_DEAD] = "dead",
2849         [SERVICE_START_PRE] = "start-pre",
2850         [SERVICE_START] = "start",
2851         [SERVICE_START_POST] = "start-post",
2852         [SERVICE_RUNNING] = "running",
2853         [SERVICE_EXITED] = "exited",
2854         [SERVICE_RELOAD] = "reload",
2855         [SERVICE_STOP] = "stop",
2856         [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2857         [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2858         [SERVICE_STOP_POST] = "stop-post",
2859         [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2860         [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2861         [SERVICE_MAINTENANCE] = "maintenance",
2862         [SERVICE_AUTO_RESTART] = "auto-restart",
2863 };
2864
2865 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2866
2867 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2868         [SERVICE_ONCE] = "once",
2869         [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
2870         [SERVICE_RESTART_ALWAYS] = "restart-always",
2871 };
2872
2873 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2874
2875 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2876         [SERVICE_SIMPLE] = "simple",
2877         [SERVICE_FORKING] = "forking",
2878         [SERVICE_ONESHOT] = "oneshot",
2879         [SERVICE_DBUS] = "dbus",
2880         [SERVICE_NOTIFY] = "notify"
2881 };
2882
2883 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2884
2885 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
2886         [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2887         [SERVICE_EXEC_START] = "ExecStart",
2888         [SERVICE_EXEC_START_POST] = "ExecStartPost",
2889         [SERVICE_EXEC_RELOAD] = "ExecReload",
2890         [SERVICE_EXEC_STOP] = "ExecStop",
2891         [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2892 };
2893
2894 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2895
2896 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
2897         [NOTIFY_NONE] = "none",
2898         [NOTIFY_MAIN] = "main",
2899         [NOTIFY_ALL] = "all"
2900 };
2901
2902 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
2903
2904 const UnitVTable service_vtable = {
2905         .suffix = ".service",
2906         .show_status = true,
2907
2908         .init = service_init,
2909         .done = service_done,
2910         .load = service_load,
2911
2912         .coldplug = service_coldplug,
2913
2914         .dump = service_dump,
2915
2916         .start = service_start,
2917         .stop = service_stop,
2918         .reload = service_reload,
2919
2920         .can_reload = service_can_reload,
2921
2922         .serialize = service_serialize,
2923         .deserialize_item = service_deserialize_item,
2924
2925         .active_state = service_active_state,
2926         .sub_state_to_string = service_sub_state_to_string,
2927
2928         .check_gc = service_check_gc,
2929         .check_snapshot = service_check_snapshot,
2930
2931         .sigchld_event = service_sigchld_event,
2932         .timer_event = service_timer_event,
2933
2934         .reset_maintenance = service_reset_maintenance,
2935
2936         .cgroup_notify_empty = service_cgroup_notify_event,
2937         .notify_message = service_notify_message,
2938
2939         .bus_name_owner_change = service_bus_name_owner_change,
2940         .bus_query_pid_done = service_bus_query_pid_done,
2941
2942         .bus_message_handler = bus_service_message_handler,
2943
2944         .enumerate = service_enumerate
2945 };