chiark / gitweb /
80cd6ad9c65eddb43a2d4b15986d5588b52f4594
[elogind.git] / src / service.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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 = strappend("LSB: ", 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 = strappend("LSB: ", 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 = strappend("LSB: ", 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->remain_after_exit = true;
710         s->restart = SERVICE_ONCE;
711         s->exec_context.std_output =
712                 (s->meta.manager->sysv_console || s->exec_context.std_input == EXEC_INPUT_TTY)
713                 ? EXEC_OUTPUT_TTY : EXEC_OUTPUT_NULL;
714         s->exec_context.kill_mode = KILL_PROCESS_GROUP;
715
716         u->meta.load_state = UNIT_LOADED;
717         r = 0;
718
719 finish:
720
721         if (f)
722                 fclose(f);
723
724         return r;
725 }
726
727 static int service_load_sysv_name(Service *s, const char *name) {
728         char **p;
729
730         assert(s);
731         assert(name);
732
733         /* For SysV services we strip the boot. or .sh
734          * prefixes/suffixes. */
735         if (startswith(name, "boot.") ||
736             endswith(name, ".sh.service"))
737                 return -ENOENT;
738
739         STRV_FOREACH(p, s->meta.manager->lookup_paths.sysvinit_path) {
740                 char *path;
741                 int r;
742
743                 if (asprintf(&path, "%s/%s", *p, name) < 0)
744                         return -ENOMEM;
745
746                 assert(endswith(path, ".service"));
747                 path[strlen(path)-8] = 0;
748
749                 r = service_load_sysv_path(s, path);
750
751                 if (r >= 0 && s->meta.load_state == UNIT_STUB) {
752                         /* Try Debian style xxx.sh source'able init scripts */
753                         strcat(path, ".sh");
754                         r = service_load_sysv_path(s, path);
755                 }
756
757                 free(path);
758
759                 if (r >= 0 && s->meta.load_state == UNIT_STUB) {
760                         /* Try SUSE style boot.xxx init scripts */
761
762                         if (asprintf(&path, "%s/boot.%s", *p, name) < 0)
763                                 return -ENOMEM;
764
765                         path[strlen(path)-8] = 0;
766                         r = service_load_sysv_path(s, path);
767                         free(path);
768                 }
769
770                 if (r < 0)
771                         return r;
772
773                 if ((s->meta.load_state != UNIT_STUB))
774                         break;
775         }
776
777         return 0;
778 }
779
780 static int service_load_sysv(Service *s) {
781         const char *t;
782         Iterator i;
783         int r;
784
785         assert(s);
786
787         /* Load service data from SysV init scripts, preferably with
788          * LSB headers ... */
789
790         if (strv_isempty(s->meta.manager->lookup_paths.sysvinit_path))
791                 return 0;
792
793         if ((t = s->meta.id))
794                 if ((r = service_load_sysv_name(s, t)) < 0)
795                         return r;
796
797         if (s->meta.load_state == UNIT_STUB)
798                 SET_FOREACH(t, s->meta.names, i) {
799                         if (t == s->meta.id)
800                                 continue;
801
802                         if ((r = service_load_sysv_name(s, t)) < 0)
803                                 return r;
804
805                         if (s->meta.load_state != UNIT_STUB)
806                                 break;
807                 }
808
809         return 0;
810 }
811
812 static int service_add_bus_name(Service *s) {
813         char *n;
814         int r;
815
816         assert(s);
817         assert(s->bus_name);
818
819         if (asprintf(&n, "dbus-%s.service", s->bus_name) < 0)
820                 return 0;
821
822         r = unit_merge_by_name(UNIT(s), n);
823         free(n);
824
825         return r;
826 }
827
828 static int service_verify(Service *s) {
829         assert(s);
830
831         if (s->meta.load_state != UNIT_LOADED)
832                 return 0;
833
834         if (!s->exec_command[SERVICE_EXEC_START]) {
835                 log_error("%s lacks ExecStart setting. Refusing.", s->meta.id);
836                 return -EINVAL;
837         }
838
839         if (s->type != SERVICE_ONESHOT &&
840             s->exec_command[SERVICE_EXEC_START]->command_next) {
841                 log_error("%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", s->meta.id);
842                 return -EINVAL;
843         }
844
845         if (s->type == SERVICE_DBUS && !s->bus_name) {
846                 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", s->meta.id);
847                 return -EINVAL;
848         }
849
850         if (s->exec_context.pam_name && s->exec_context.kill_mode != KILL_CONTROL_GROUP) {
851                 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s->meta.id);
852                 return -EINVAL;
853         }
854
855         return 0;
856 }
857
858 static int service_add_default_dependencies(Service *s) {
859         int r;
860
861         assert(s);
862
863         /* Add a number of automatic dependencies useful for the
864          * majority of services. */
865
866         /* First, pull in base system */
867         if (s->meta.manager->running_as == MANAGER_SYSTEM) {
868
869                 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
870                         return r;
871
872         } else if (s->meta.manager->running_as == MANAGER_SESSION) {
873
874                 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0)
875                         return r;
876         }
877
878         /* Second, activate normal shutdown */
879         return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTED_BY, SPECIAL_SHUTDOWN_TARGET, NULL, true);
880 }
881
882 static int service_load(Unit *u) {
883         int r;
884         Service *s = SERVICE(u);
885
886         assert(s);
887
888         /* Load a .service file */
889         if ((r = unit_load_fragment(u)) < 0)
890                 return r;
891
892         /* Load a classic init script as a fallback, if we couldn't find anything */
893         if (u->meta.load_state == UNIT_STUB)
894                 if ((r = service_load_sysv(s)) < 0)
895                         return r;
896
897         /* Still nothing found? Then let's give up */
898         if (u->meta.load_state == UNIT_STUB)
899                 return -ENOENT;
900
901         /* We were able to load something, then let's add in the
902          * dropin directories. */
903         if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
904                 return r;
905
906         /* This is a new unit? Then let's add in some extras */
907         if (u->meta.load_state == UNIT_LOADED) {
908                 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
909                         return r;
910
911                 if ((r = unit_add_default_cgroup(u)) < 0)
912                         return r;
913
914                 if ((r = sysv_fix_order(s)) < 0)
915                         return r;
916
917                 if (s->bus_name) {
918                         if ((r = service_add_bus_name(s)) < 0)
919                                 return r;
920
921                         if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
922                                 return r;
923                 }
924
925                 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
926                         s->notify_access = NOTIFY_MAIN;
927
928                 if (s->type == SERVICE_DBUS || s->bus_name)
929                         if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_TARGET, NULL, true)) < 0)
930                                 return r;
931
932                 if (s->meta.default_dependencies)
933                         if ((r = service_add_default_dependencies(s)) < 0)
934                                 return r;
935         }
936
937         return service_verify(s);
938 }
939
940 static void service_dump(Unit *u, FILE *f, const char *prefix) {
941
942         ServiceExecCommand c;
943         Service *s = SERVICE(u);
944         const char *prefix2;
945         char *p2;
946
947         assert(s);
948
949         p2 = strappend(prefix, "\t");
950         prefix2 = p2 ? p2 : prefix;
951
952         fprintf(f,
953                 "%sService State: %s\n"
954                 "%sPermissionsStartOnly: %s\n"
955                 "%sRootDirectoryStartOnly: %s\n"
956                 "%sRemainAfterExit: %s\n"
957                 "%sType: %s\n"
958                 "%sRestart: %s\n"
959                 "%sNotifyAccess: %s\n",
960                 prefix, service_state_to_string(s->state),
961                 prefix, yes_no(s->permissions_start_only),
962                 prefix, yes_no(s->root_directory_start_only),
963                 prefix, yes_no(s->remain_after_exit),
964                 prefix, service_type_to_string(s->type),
965                 prefix, service_restart_to_string(s->restart),
966                 prefix, notify_access_to_string(s->notify_access));
967
968         if (s->control_pid > 0)
969                 fprintf(f,
970                         "%sControl PID: %lu\n",
971                         prefix, (unsigned long) s->control_pid);
972
973         if (s->main_pid > 0)
974                 fprintf(f,
975                         "%sMain PID: %lu\n",
976                         prefix, (unsigned long) s->main_pid);
977
978         if (s->pid_file)
979                 fprintf(f,
980                         "%sPIDFile: %s\n",
981                         prefix, s->pid_file);
982
983         if (s->bus_name)
984                 fprintf(f,
985                         "%sBusName: %s\n"
986                         "%sBus Name Good: %s\n",
987                         prefix, s->bus_name,
988                         prefix, yes_no(s->bus_name_good));
989
990         exec_context_dump(&s->exec_context, f, prefix);
991
992         for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
993
994                 if (!s->exec_command[c])
995                         continue;
996
997                 fprintf(f, "%s-> %s:\n",
998                         prefix, service_exec_command_to_string(c));
999
1000                 exec_command_dump_list(s->exec_command[c], f, prefix2);
1001         }
1002
1003         if (s->sysv_path)
1004                 fprintf(f,
1005                         "%sSysV Init Script Path: %s\n"
1006                         "%sSysV Init Script has LSB Header: %s\n",
1007                         prefix, s->sysv_path,
1008                         prefix, yes_no(s->sysv_has_lsb));
1009
1010         if (s->sysv_start_priority >= 0)
1011                 fprintf(f,
1012                         "%sSysVStartPriority: %i\n"
1013                         "%sSysVEnabled: %s\n",
1014                         prefix, s->sysv_start_priority,
1015                         prefix, yes_no(s->sysv_enabled));
1016
1017         if (s->sysv_runlevels)
1018                 fprintf(f, "%sSysVRunLevels: %s\n",
1019                         prefix, s->sysv_runlevels);
1020
1021         if (s->status_text)
1022                 fprintf(f, "%sStatus Text: %s\n",
1023                         prefix, s->status_text);
1024
1025         free(p2);
1026 }
1027
1028 static int service_load_pid_file(Service *s) {
1029         char *k;
1030         int r;
1031         pid_t pid;
1032
1033         assert(s);
1034
1035         if (s->main_pid_known)
1036                 return 0;
1037
1038         assert(s->main_pid <= 0);
1039
1040         if (!s->pid_file)
1041                 return -ENOENT;
1042
1043         if ((r = read_one_line_file(s->pid_file, &k)) < 0)
1044                 return r;
1045
1046         r = parse_pid(k, &pid);
1047         free(k);
1048
1049         if (r < 0)
1050                 return r;
1051
1052         if (kill(pid, 0) < 0 && errno != EPERM) {
1053                 log_warning("PID %lu read from file %s does not exist. Your service or init script might be broken.",
1054                             (unsigned long) pid, s->pid_file);
1055                 return -ESRCH;
1056         }
1057
1058         if ((r = service_set_main_pid(s, pid)) < 0)
1059                 return r;
1060
1061         if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1062                 /* FIXME: we need to do something here */
1063                 return r;
1064
1065         return 0;
1066 }
1067
1068 static int service_get_sockets(Service *s, Set **_set) {
1069         Set *set;
1070         Iterator i;
1071         char *t;
1072         int r;
1073
1074         assert(s);
1075         assert(_set);
1076
1077         if (s->socket_fd >= 0)
1078                 return 0;
1079
1080         /* Collects all Socket objects that belong to this
1081          * service. Note that a service might have multiple sockets
1082          * via multiple names. */
1083
1084         if (!(set = set_new(NULL, NULL)))
1085                 return -ENOMEM;
1086
1087         SET_FOREACH(t, s->meta.names, i) {
1088                 char *k;
1089                 Unit *p;
1090
1091                 /* Look for all socket objects that go by any of our
1092                  * units and collect their fds */
1093
1094                 if (!(k = unit_name_change_suffix(t, ".socket"))) {
1095                         r = -ENOMEM;
1096                         goto fail;
1097                 }
1098
1099                 p = manager_get_unit(s->meta.manager, k);
1100                 free(k);
1101
1102                 if (!p)
1103                         continue;
1104
1105                 if ((r = set_put(set, p)) < 0)
1106                         goto fail;
1107         }
1108
1109         *_set = set;
1110         return 0;
1111
1112 fail:
1113         set_free(set);
1114         return r;
1115 }
1116
1117 static int service_notify_sockets_dead(Service *s) {
1118         Iterator i;
1119         Set *set;
1120         Socket *sock;
1121         int r;
1122
1123         assert(s);
1124
1125         if (s->socket_fd >= 0)
1126                 return 0;
1127
1128         /* Notifies all our sockets when we die */
1129         if ((r = service_get_sockets(s, &set)) < 0)
1130                 return r;
1131
1132         SET_FOREACH(sock, set, i)
1133                 socket_notify_service_dead(sock);
1134
1135         set_free(set);
1136
1137         return 0;
1138 }
1139
1140 static void service_set_state(Service *s, ServiceState state) {
1141         ServiceState old_state;
1142         assert(s);
1143
1144         old_state = s->state;
1145         s->state = state;
1146
1147         if (state != SERVICE_START_PRE &&
1148             state != SERVICE_START &&
1149             state != SERVICE_START_POST &&
1150             state != SERVICE_RELOAD &&
1151             state != SERVICE_STOP &&
1152             state != SERVICE_STOP_SIGTERM &&
1153             state != SERVICE_STOP_SIGKILL &&
1154             state != SERVICE_STOP_POST &&
1155             state != SERVICE_FINAL_SIGTERM &&
1156             state != SERVICE_FINAL_SIGKILL &&
1157             state != SERVICE_AUTO_RESTART)
1158                 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1159
1160         if (state != SERVICE_START &&
1161             state != SERVICE_START_POST &&
1162             state != SERVICE_RUNNING &&
1163             state != SERVICE_RELOAD &&
1164             state != SERVICE_STOP &&
1165             state != SERVICE_STOP_SIGTERM &&
1166             state != SERVICE_STOP_SIGKILL)
1167                 service_unwatch_main_pid(s);
1168
1169         if (state != SERVICE_START_PRE &&
1170             state != SERVICE_START &&
1171             state != SERVICE_START_POST &&
1172             state != SERVICE_RELOAD &&
1173             state != SERVICE_STOP &&
1174             state != SERVICE_STOP_SIGTERM &&
1175             state != SERVICE_STOP_SIGKILL &&
1176             state != SERVICE_STOP_POST &&
1177             state != SERVICE_FINAL_SIGTERM &&
1178             state != SERVICE_FINAL_SIGKILL) {
1179                 service_unwatch_control_pid(s);
1180                 s->control_command = NULL;
1181                 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1182         }
1183
1184         if (state == SERVICE_DEAD ||
1185             state == SERVICE_STOP ||
1186             state == SERVICE_STOP_SIGTERM ||
1187             state == SERVICE_STOP_SIGKILL ||
1188             state == SERVICE_STOP_POST ||
1189             state == SERVICE_FINAL_SIGTERM ||
1190             state == SERVICE_FINAL_SIGKILL ||
1191             state == SERVICE_MAINTENANCE ||
1192             state == SERVICE_AUTO_RESTART)
1193                 service_notify_sockets_dead(s);
1194
1195         if (state != SERVICE_START_PRE &&
1196             state != SERVICE_START &&
1197             state != SERVICE_START_POST &&
1198             state != SERVICE_RUNNING &&
1199             state != SERVICE_RELOAD &&
1200             state != SERVICE_STOP &&
1201             state != SERVICE_STOP_SIGTERM &&
1202             state != SERVICE_STOP_SIGKILL &&
1203             state != SERVICE_STOP_POST &&
1204             state != SERVICE_FINAL_SIGTERM &&
1205             state != SERVICE_FINAL_SIGKILL &&
1206             !(state == SERVICE_DEAD && s->meta.job)) {
1207                 service_close_socket_fd(s);
1208                 service_connection_unref(s);
1209         }
1210
1211         /* For the inactive states unit_notify() will trim the cgroup,
1212          * but for exit we have to do that ourselves... */
1213         if (state == SERVICE_EXITED)
1214                 cgroup_bonding_trim_list(s->meta.cgroup_bondings, true);
1215
1216         if (old_state != state)
1217                 log_debug("%s changed %s -> %s", s->meta.id, service_state_to_string(old_state), service_state_to_string(state));
1218
1219         unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
1220 }
1221
1222 static int service_coldplug(Unit *u) {
1223         Service *s = SERVICE(u);
1224         int r;
1225
1226         assert(s);
1227         assert(s->state == SERVICE_DEAD);
1228
1229         if (s->deserialized_state != s->state) {
1230
1231                 if (s->deserialized_state == SERVICE_START_PRE ||
1232                     s->deserialized_state == SERVICE_START ||
1233                     s->deserialized_state == SERVICE_START_POST ||
1234                     s->deserialized_state == SERVICE_RELOAD ||
1235                     s->deserialized_state == SERVICE_STOP ||
1236                     s->deserialized_state == SERVICE_STOP_SIGTERM ||
1237                     s->deserialized_state == SERVICE_STOP_SIGKILL ||
1238                     s->deserialized_state == SERVICE_STOP_POST ||
1239                     s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1240                     s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1241                     s->deserialized_state == SERVICE_AUTO_RESTART) {
1242
1243                         if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1244                                 usec_t k;
1245
1246                                 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1247
1248                                 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1249                                         return r;
1250                         }
1251                 }
1252
1253                 if ((s->deserialized_state == SERVICE_START &&
1254                      (s->type == SERVICE_FORKING ||
1255                       s->type == SERVICE_DBUS ||
1256                       s->type == SERVICE_ONESHOT ||
1257                       s->type == SERVICE_NOTIFY)) ||
1258                     s->deserialized_state == SERVICE_START_POST ||
1259                     s->deserialized_state == SERVICE_RUNNING ||
1260                     s->deserialized_state == SERVICE_RELOAD ||
1261                     s->deserialized_state == SERVICE_STOP ||
1262                     s->deserialized_state == SERVICE_STOP_SIGTERM ||
1263                     s->deserialized_state == SERVICE_STOP_SIGKILL)
1264                         if (s->main_pid > 0)
1265                                 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1266                                         return r;
1267
1268                 if (s->deserialized_state == SERVICE_START_PRE ||
1269                     s->deserialized_state == SERVICE_START ||
1270                     s->deserialized_state == SERVICE_START_POST ||
1271                     s->deserialized_state == SERVICE_RELOAD ||
1272                     s->deserialized_state == SERVICE_STOP ||
1273                     s->deserialized_state == SERVICE_STOP_SIGTERM ||
1274                     s->deserialized_state == SERVICE_STOP_SIGKILL ||
1275                     s->deserialized_state == SERVICE_STOP_POST ||
1276                     s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1277                     s->deserialized_state == SERVICE_FINAL_SIGKILL)
1278                         if (s->control_pid > 0)
1279                                 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1280                                         return r;
1281
1282                 service_set_state(s, s->deserialized_state);
1283         }
1284
1285         return 0;
1286 }
1287
1288 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1289         Iterator i;
1290         int r;
1291         int *rfds = NULL;
1292         unsigned rn_fds = 0;
1293         Set *set;
1294         Socket *sock;
1295
1296         assert(s);
1297         assert(fds);
1298         assert(n_fds);
1299
1300         if (s->socket_fd >= 0)
1301                 return 0;
1302
1303         if ((r = service_get_sockets(s, &set)) < 0)
1304                 return r;
1305
1306         SET_FOREACH(sock, set, i) {
1307                 int *cfds;
1308                 unsigned cn_fds;
1309
1310                 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1311                         goto fail;
1312
1313                 if (!cfds)
1314                         continue;
1315
1316                 if (!rfds) {
1317                         rfds = cfds;
1318                         rn_fds = cn_fds;
1319                 } else {
1320                         int *t;
1321
1322                         if (!(t = new(int, rn_fds+cn_fds))) {
1323                                 free(cfds);
1324                                 r = -ENOMEM;
1325                                 goto fail;
1326                         }
1327
1328                         memcpy(t, rfds, rn_fds);
1329                         memcpy(t+rn_fds, cfds, cn_fds);
1330                         free(rfds);
1331                         free(cfds);
1332
1333                         rfds = t;
1334                         rn_fds = rn_fds+cn_fds;
1335                 }
1336         }
1337
1338         *fds = rfds;
1339         *n_fds = rn_fds;
1340
1341         set_free(set);
1342
1343         return 0;
1344
1345 fail:
1346         set_free(set);
1347         free(rfds);
1348
1349         return r;
1350 }
1351
1352 static int service_spawn(
1353                 Service *s,
1354                 ExecCommand *c,
1355                 bool timeout,
1356                 bool pass_fds,
1357                 bool apply_permissions,
1358                 bool apply_chroot,
1359                 bool apply_tty_stdin,
1360                 bool set_notify_socket,
1361                 pid_t *_pid) {
1362
1363         pid_t pid;
1364         int r;
1365         int *fds = NULL, *fdsbuf = NULL;
1366         unsigned n_fds = 0, n_env = 0;
1367         char **argv = NULL, **final_env = NULL, **our_env = NULL;
1368
1369         assert(s);
1370         assert(c);
1371         assert(_pid);
1372
1373         if (pass_fds ||
1374             s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1375             s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1376             s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1377
1378                 if (s->socket_fd >= 0) {
1379                         fds = &s->socket_fd;
1380                         n_fds = 1;
1381                 } else {
1382                         if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0)
1383                                 goto fail;
1384
1385                         fds = fdsbuf;
1386                 }
1387         }
1388
1389         if (timeout && s->timeout_usec) {
1390                 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1391                         goto fail;
1392         } else
1393                 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1394
1395         if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1396                 r = -ENOMEM;
1397                 goto fail;
1398         }
1399
1400         if (!(our_env = new0(char*, 3))) {
1401                 r = -ENOMEM;
1402                 goto fail;
1403         }
1404
1405         if (set_notify_socket)
1406                 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=@%s", s->meta.manager->notify_socket) < 0) {
1407                         r = -ENOMEM;
1408                         goto fail;
1409                 }
1410
1411         if (s->main_pid > 0)
1412                 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1413                         r = -ENOMEM;
1414                         goto fail;
1415                 }
1416
1417         if (!(final_env = strv_env_merge(2,
1418                                          s->meta.manager->environment,
1419                                          our_env,
1420                                          NULL))) {
1421                 r = -ENOMEM;
1422                 goto fail;
1423         }
1424
1425         r = exec_spawn(c,
1426                        argv,
1427                        &s->exec_context,
1428                        fds, n_fds,
1429                        final_env,
1430                        apply_permissions,
1431                        apply_chroot,
1432                        apply_tty_stdin,
1433                        s->meta.manager->confirm_spawn,
1434                        s->meta.cgroup_bondings,
1435                        &pid);
1436
1437         if (r < 0)
1438                 goto fail;
1439
1440
1441         if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1442                 /* FIXME: we need to do something here */
1443                 goto fail;
1444
1445         free(fdsbuf);
1446         strv_free(argv);
1447         strv_free(our_env);
1448         strv_free(final_env);
1449
1450         *_pid = pid;
1451
1452         return 0;
1453
1454 fail:
1455         free(fdsbuf);
1456         strv_free(argv);
1457         strv_free(our_env);
1458         strv_free(final_env);
1459
1460         if (timeout)
1461                 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1462
1463         return r;
1464 }
1465
1466 static int main_pid_good(Service *s) {
1467         assert(s);
1468
1469         /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1470          * don't know */
1471
1472         /* If we know the pid file, then lets just check if it is
1473          * still valid */
1474         if (s->main_pid_known)
1475                 return s->main_pid > 0;
1476
1477         /* We don't know the pid */
1478         return -EAGAIN;
1479 }
1480
1481 static int control_pid_good(Service *s) {
1482         assert(s);
1483
1484         return s->control_pid > 0;
1485 }
1486
1487 static int cgroup_good(Service *s) {
1488         int r;
1489
1490         assert(s);
1491
1492         if ((r = cgroup_bonding_is_empty_list(s->meta.cgroup_bondings)) < 0)
1493                 return r;
1494
1495         return !r;
1496 }
1497
1498 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1499         int r;
1500         assert(s);
1501
1502         if (!success)
1503                 s->failure = true;
1504
1505         if (allow_restart &&
1506             !s->forbid_restart &&
1507             (s->restart == SERVICE_RESTART_ALWAYS ||
1508              (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1509
1510                 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1511                         goto fail;
1512
1513                 service_set_state(s, SERVICE_AUTO_RESTART);
1514         } else
1515                 service_set_state(s, s->failure ? SERVICE_MAINTENANCE : SERVICE_DEAD);
1516
1517         s->forbid_restart = false;
1518
1519         return;
1520
1521 fail:
1522         log_warning("%s failed to run install restart timer: %s", s->meta.id, strerror(-r));
1523         service_enter_dead(s, false, false);
1524 }
1525
1526 static void service_enter_signal(Service *s, ServiceState state, bool success);
1527
1528 static void service_enter_stop_post(Service *s, bool success) {
1529         int r;
1530         assert(s);
1531
1532         if (!success)
1533                 s->failure = true;
1534
1535         service_unwatch_control_pid(s);
1536
1537         s->control_command_id = SERVICE_EXEC_STOP_POST;
1538         if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1539                 if ((r = service_spawn(s,
1540                                        s->control_command,
1541                                        true,
1542                                        false,
1543                                        !s->permissions_start_only,
1544                                        !s->root_directory_start_only,
1545                                        true,
1546                                        false,
1547                                        &s->control_pid)) < 0)
1548                         goto fail;
1549
1550
1551                 service_set_state(s, SERVICE_STOP_POST);
1552         } else
1553                 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
1554
1555         return;
1556
1557 fail:
1558         log_warning("%s failed to run 'stop-post' task: %s", s->meta.id, strerror(-r));
1559         service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1560 }
1561
1562 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1563         int r;
1564         bool sent = false;
1565
1566         assert(s);
1567
1568         if (!success)
1569                 s->failure = true;
1570
1571         if (s->exec_context.kill_mode != KILL_NONE) {
1572                 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? s->exec_context.kill_signal : SIGKILL;
1573
1574                 if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) {
1575
1576                         if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig)) < 0) {
1577                                 if (r != -EAGAIN && r != -ESRCH)
1578                                         goto fail;
1579                         } else
1580                                 sent = true;
1581                 }
1582
1583                 if (!sent) {
1584                         r = 0;
1585
1586                         if (s->main_pid > 0) {
1587                                 if (kill(s->exec_context.kill_mode == KILL_PROCESS ? s->main_pid : -s->main_pid, sig) < 0 && errno != ESRCH)
1588                                         r = -errno;
1589                                 else
1590                                         sent = true;
1591                         }
1592
1593                         if (s->control_pid > 0) {
1594                                 if (kill(s->exec_context.kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH)
1595                                         r = -errno;
1596                                 else
1597                                         sent = true;
1598                         }
1599
1600                         if (r < 0)
1601                                 goto fail;
1602                 }
1603         }
1604
1605         if (sent && (s->main_pid > 0 || s->control_pid > 0)) {
1606                 if (s->timeout_usec > 0)
1607                         if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1608                                 goto fail;
1609
1610                 service_set_state(s, state);
1611         } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1612                 service_enter_stop_post(s, true);
1613         else
1614                 service_enter_dead(s, true, true);
1615
1616         return;
1617
1618 fail:
1619         log_warning("%s failed to kill processes: %s", s->meta.id, strerror(-r));
1620
1621         if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1622                 service_enter_stop_post(s, false);
1623         else
1624                 service_enter_dead(s, false, true);
1625 }
1626
1627 static void service_enter_stop(Service *s, bool success) {
1628         int r;
1629
1630         assert(s);
1631
1632         if (!success)
1633                 s->failure = true;
1634
1635         service_unwatch_control_pid(s);
1636
1637         s->control_command_id = SERVICE_EXEC_STOP;
1638         if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
1639                 if ((r = service_spawn(s,
1640                                        s->control_command,
1641                                        true,
1642                                        false,
1643                                        !s->permissions_start_only,
1644                                        !s->root_directory_start_only,
1645                                        false,
1646                                        false,
1647                                        &s->control_pid)) < 0)
1648                         goto fail;
1649
1650                 service_set_state(s, SERVICE_STOP);
1651         } else
1652                 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1653
1654         return;
1655
1656 fail:
1657         log_warning("%s failed to run 'stop' task: %s", s->meta.id, strerror(-r));
1658         service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1659 }
1660
1661 static void service_enter_running(Service *s, bool success) {
1662         int main_pid_ok, cgroup_ok;
1663         assert(s);
1664
1665         if (!success)
1666                 s->failure = true;
1667
1668         main_pid_ok = main_pid_good(s);
1669         cgroup_ok = cgroup_good(s);
1670
1671         if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
1672             (s->bus_name_good || s->type != SERVICE_DBUS))
1673                 service_set_state(s, SERVICE_RUNNING);
1674         else if (s->remain_after_exit)
1675                 service_set_state(s, SERVICE_EXITED);
1676         else
1677                 service_enter_stop(s, true);
1678 }
1679
1680 static void service_enter_start_post(Service *s) {
1681         int r;
1682         assert(s);
1683
1684         service_unwatch_control_pid(s);
1685
1686         s->control_command_id = SERVICE_EXEC_START_POST;
1687         if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
1688                 if ((r = service_spawn(s,
1689                                        s->control_command,
1690                                        true,
1691                                        false,
1692                                        !s->permissions_start_only,
1693                                        !s->root_directory_start_only,
1694                                        false,
1695                                        false,
1696                                        &s->control_pid)) < 0)
1697                         goto fail;
1698
1699                 service_set_state(s, SERVICE_START_POST);
1700         } else
1701                 service_enter_running(s, true);
1702
1703         return;
1704
1705 fail:
1706         log_warning("%s failed to run 'start-post' task: %s", s->meta.id, strerror(-r));
1707         service_enter_stop(s, false);
1708 }
1709
1710 static void service_enter_start(Service *s) {
1711         pid_t pid;
1712         int r;
1713
1714         assert(s);
1715
1716         assert(s->exec_command[SERVICE_EXEC_START]);
1717         assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
1718
1719         if (s->type == SERVICE_FORKING)
1720                 service_unwatch_control_pid(s);
1721         else
1722                 service_unwatch_main_pid(s);
1723
1724         s->control_command_id = SERVICE_EXEC_START;
1725         s->control_command = s->exec_command[SERVICE_EXEC_START];
1726
1727         if ((r = service_spawn(s,
1728                                s->control_command,
1729                                s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY,
1730                                true,
1731                                true,
1732                                true,
1733                                true,
1734                                s->notify_access != NOTIFY_NONE,
1735                                &pid)) < 0)
1736                 goto fail;
1737
1738         if (s->type == SERVICE_SIMPLE) {
1739                 /* For simple services we immediately start
1740                  * the START_POST binaries. */
1741
1742                 service_set_main_pid(s, pid);
1743                 service_enter_start_post(s);
1744
1745         } else  if (s->type == SERVICE_FORKING) {
1746
1747                 /* For forking services we wait until the start
1748                  * process exited. */
1749
1750                 s->control_pid = pid;
1751                 service_set_state(s, SERVICE_START);
1752
1753         } else if (s->type == SERVICE_ONESHOT ||
1754                    s->type == SERVICE_DBUS ||
1755                    s->type == SERVICE_NOTIFY) {
1756
1757                 /* For oneshot services we wait until the start
1758                  * process exited, too, but it is our main process. */
1759
1760                 /* For D-Bus services we know the main pid right away,
1761                  * but wait for the bus name to appear on the
1762                  * bus. Notify services are similar. */
1763
1764                 service_set_main_pid(s, pid);
1765                 service_set_state(s, SERVICE_START);
1766         } else
1767                 assert_not_reached("Unknown service type");
1768
1769         return;
1770
1771 fail:
1772         log_warning("%s failed to run 'start' task: %s", s->meta.id, strerror(-r));
1773         service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1774 }
1775
1776 static void service_enter_start_pre(Service *s) {
1777         int r;
1778
1779         assert(s);
1780
1781         service_unwatch_control_pid(s);
1782
1783         s->control_command_id = SERVICE_EXEC_START_PRE;
1784         if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
1785                 if ((r = service_spawn(s,
1786                                        s->control_command,
1787                                        true,
1788                                        false,
1789                                        !s->permissions_start_only,
1790                                        !s->root_directory_start_only,
1791                                        true,
1792                                        false,
1793                                        &s->control_pid)) < 0)
1794                         goto fail;
1795
1796                 service_set_state(s, SERVICE_START_PRE);
1797         } else
1798                 service_enter_start(s);
1799
1800         return;
1801
1802 fail:
1803         log_warning("%s failed to run 'start-pre' task: %s", s->meta.id, strerror(-r));
1804         service_enter_dead(s, false, true);
1805 }
1806
1807 static void service_enter_restart(Service *s) {
1808         int r;
1809         DBusError error;
1810
1811         assert(s);
1812         dbus_error_init(&error);
1813
1814         service_enter_dead(s, true, false);
1815
1816         if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL, NULL)) < 0)
1817                 goto fail;
1818
1819         log_debug("%s scheduled restart job.", s->meta.id);
1820         return;
1821
1822 fail:
1823         log_warning("%s failed to schedule restart job: %s", s->meta.id, bus_error(&error, -r));
1824         service_enter_dead(s, false, false);
1825
1826         dbus_error_free(&error);
1827 }
1828
1829 static void service_enter_reload(Service *s) {
1830         int r;
1831
1832         assert(s);
1833
1834         service_unwatch_control_pid(s);
1835
1836         s->control_command_id = SERVICE_EXEC_RELOAD;
1837         if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
1838                 if ((r = service_spawn(s,
1839                                        s->control_command,
1840                                        true,
1841                                        false,
1842                                        !s->permissions_start_only,
1843                                        !s->root_directory_start_only,
1844                                        false,
1845                                        false,
1846                                        &s->control_pid)) < 0)
1847                         goto fail;
1848
1849                 service_set_state(s, SERVICE_RELOAD);
1850         } else
1851                 service_enter_running(s, true);
1852
1853         return;
1854
1855 fail:
1856         log_warning("%s failed to run 'reload' task: %s", s->meta.id, strerror(-r));
1857         service_enter_stop(s, false);
1858 }
1859
1860 static void service_run_next_control(Service *s, bool success) {
1861         int r;
1862
1863         assert(s);
1864         assert(s->control_command);
1865         assert(s->control_command->command_next);
1866
1867         if (!success)
1868                 s->failure = true;
1869
1870         assert(s->control_command_id != SERVICE_EXEC_START);
1871
1872         s->control_command = s->control_command->command_next;
1873         service_unwatch_control_pid(s);
1874
1875         if ((r = service_spawn(s,
1876                                s->control_command,
1877                                true,
1878                                false,
1879                                !s->permissions_start_only,
1880                                !s->root_directory_start_only,
1881                                s->control_command_id == SERVICE_EXEC_START_PRE ||
1882                                s->control_command_id == SERVICE_EXEC_STOP_POST,
1883                                false,
1884                                &s->control_pid)) < 0)
1885                 goto fail;
1886
1887         return;
1888
1889 fail:
1890         log_warning("%s failed to run next control task: %s", s->meta.id, strerror(-r));
1891
1892         if (s->state == SERVICE_START_PRE)
1893                 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1894         else if (s->state == SERVICE_STOP)
1895                 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1896         else if (s->state == SERVICE_STOP_POST)
1897                 service_enter_dead(s, false, true);
1898         else
1899                 service_enter_stop(s, false);
1900 }
1901
1902 static void service_run_next_main(Service *s, bool success) {
1903         pid_t pid;
1904         int r;
1905
1906         assert(s);
1907         assert(s->control_command);
1908         assert(s->control_command->command_next);
1909
1910         if (!success)
1911                 s->failure = true;
1912
1913         assert(s->control_command_id == SERVICE_EXEC_START);
1914         assert(s->type == SERVICE_ONESHOT);
1915
1916         s->control_command = s->control_command->command_next;
1917         service_unwatch_main_pid(s);
1918
1919         if ((r = service_spawn(s,
1920                                s->control_command,
1921                                false,
1922                                true,
1923                                true,
1924                                true,
1925                                true,
1926                                s->notify_access != NOTIFY_NONE,
1927                                &pid)) < 0)
1928                 goto fail;
1929
1930         service_set_main_pid(s, pid);
1931
1932         return;
1933
1934 fail:
1935         log_warning("%s failed to run next main task: %s", s->meta.id, strerror(-r));
1936         service_enter_stop(s, false);
1937 }
1938
1939 static int service_start(Unit *u) {
1940         Service *s = SERVICE(u);
1941
1942         assert(s);
1943
1944         /* We cannot fulfill this request right now, try again later
1945          * please! */
1946         if (s->state == SERVICE_STOP ||
1947             s->state == SERVICE_STOP_SIGTERM ||
1948             s->state == SERVICE_STOP_SIGKILL ||
1949             s->state == SERVICE_STOP_POST ||
1950             s->state == SERVICE_FINAL_SIGTERM ||
1951             s->state == SERVICE_FINAL_SIGKILL)
1952                 return -EAGAIN;
1953
1954         /* Already on it! */
1955         if (s->state == SERVICE_START_PRE ||
1956             s->state == SERVICE_START ||
1957             s->state == SERVICE_START_POST)
1958                 return 0;
1959
1960         assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTENANCE || s->state == SERVICE_AUTO_RESTART);
1961
1962         /* Make sure we don't enter a busy loop of some kind. */
1963         if (!ratelimit_test(&s->ratelimit)) {
1964                 log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id);
1965                 return -ECANCELED;
1966         }
1967
1968         if ((s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1969              s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1970              s->exec_context.std_error == EXEC_OUTPUT_SOCKET) &&
1971             s->socket_fd < 0) {
1972                 log_warning("%s can only be started with a per-connection socket.", u->meta.id);
1973                 return -EINVAL;
1974         }
1975
1976         s->failure = false;
1977         s->main_pid_known = false;
1978         s->forbid_restart = false;
1979
1980         service_enter_start_pre(s);
1981         return 0;
1982 }
1983
1984 static int service_stop(Unit *u) {
1985         Service *s = SERVICE(u);
1986
1987         assert(s);
1988
1989         /* This is a user request, so don't do restarts on this
1990          * shutdown. */
1991         s->forbid_restart = true;
1992
1993         /* Already on it */
1994         if (s->state == SERVICE_STOP ||
1995             s->state == SERVICE_STOP_SIGTERM ||
1996             s->state == SERVICE_STOP_SIGKILL ||
1997             s->state == SERVICE_STOP_POST ||
1998             s->state == SERVICE_FINAL_SIGTERM ||
1999             s->state == SERVICE_FINAL_SIGKILL)
2000                 return 0;
2001
2002         /* Don't allow a restart */
2003         if (s->state == SERVICE_AUTO_RESTART) {
2004                 service_set_state(s, SERVICE_DEAD);
2005                 return 0;
2006         }
2007
2008         /* If there's already something running we go directly into
2009          * kill mode. */
2010         if (s->state == SERVICE_START_PRE ||
2011             s->state == SERVICE_START ||
2012             s->state == SERVICE_START_POST ||
2013             s->state == SERVICE_RELOAD) {
2014                 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
2015                 return 0;
2016         }
2017
2018         assert(s->state == SERVICE_RUNNING ||
2019                s->state == SERVICE_EXITED);
2020
2021         service_enter_stop(s, true);
2022         return 0;
2023 }
2024
2025 static int service_reload(Unit *u) {
2026         Service *s = SERVICE(u);
2027
2028         assert(s);
2029
2030         assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2031
2032         service_enter_reload(s);
2033         return 0;
2034 }
2035
2036 static bool service_can_reload(Unit *u) {
2037         Service *s = SERVICE(u);
2038
2039         assert(s);
2040
2041         return !!s->exec_command[SERVICE_EXEC_RELOAD];
2042 }
2043
2044 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2045         Service *s = SERVICE(u);
2046
2047         assert(u);
2048         assert(f);
2049         assert(fds);
2050
2051         unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2052         unit_serialize_item(u, f, "failure", yes_no(s->failure));
2053
2054         if (s->control_pid > 0)
2055                 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
2056
2057         if (s->main_pid_known && s->main_pid > 0)
2058                 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2059
2060         unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2061
2062         if (s->status_text)
2063                 unit_serialize_item(u, f, "status-text", s->status_text);
2064
2065         /* There's a minor uncleanliness here: if there are multiple
2066          * commands attached here, we will start from the first one
2067          * again */
2068         if (s->control_command_id >= 0)
2069                 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
2070
2071         if (s->socket_fd >= 0) {
2072                 int copy;
2073
2074                 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2075                         return copy;
2076
2077                 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2078         }
2079
2080         if (s->main_exec_status.pid > 0) {
2081                 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid);
2082
2083                 if (s->main_exec_status.start_timestamp.realtime > 0) {
2084                         unit_serialize_item_format(u, f, "main-exec-status-start-realtime",
2085                                                    "%llu", (unsigned long long) s->main_exec_status.start_timestamp.realtime);
2086
2087                         unit_serialize_item_format(u, f, "main-exec-status-start-monotonic",
2088                                                    "%llu", (unsigned long long) s->main_exec_status.start_timestamp.monotonic);
2089                 }
2090
2091                 if (s->main_exec_status.exit_timestamp.realtime > 0) {
2092                         unit_serialize_item_format(u, f, "main-exec-status-exit-realtime",
2093                                                    "%llu", (unsigned long long) s->main_exec_status.exit_timestamp.realtime);
2094                         unit_serialize_item_format(u, f, "main-exec-status-exit-monotonic",
2095                                                    "%llu", (unsigned long long) s->main_exec_status.exit_timestamp.monotonic);
2096
2097                         unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2098                         unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2099                 }
2100         }
2101
2102         return 0;
2103 }
2104
2105 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2106         Service *s = SERVICE(u);
2107
2108         assert(u);
2109         assert(key);
2110         assert(value);
2111         assert(fds);
2112
2113         if (streq(key, "state")) {
2114                 ServiceState state;
2115
2116                 if ((state = service_state_from_string(value)) < 0)
2117                         log_debug("Failed to parse state value %s", value);
2118                 else
2119                         s->deserialized_state = state;
2120         } else if (streq(key, "failure")) {
2121                 int b;
2122
2123                 if ((b = parse_boolean(value)) < 0)
2124                         log_debug("Failed to parse failure value %s", value);
2125                 else
2126                         s->failure = b || s->failure;
2127         } else if (streq(key, "control-pid")) {
2128                 pid_t pid;
2129
2130                 if (parse_pid(value, &pid) < 0)
2131                         log_debug("Failed to parse control-pid value %s", value);
2132                 else
2133                         s->control_pid = pid;
2134         } else if (streq(key, "main-pid")) {
2135                 pid_t pid;
2136
2137                 if (parse_pid(value, &pid) < 0)
2138                         log_debug("Failed to parse main-pid value %s", value);
2139                 else
2140                         service_set_main_pid(s, (pid_t) pid);
2141         } else if (streq(key, "main-pid-known")) {
2142                 int b;
2143
2144                 if ((b = parse_boolean(value)) < 0)
2145                         log_debug("Failed to parse main-pid-known value %s", value);
2146                 else
2147                         s->main_pid_known = b;
2148         } else if (streq(key, "status-text")) {
2149                 char *t;
2150
2151                 if ((t = strdup(value))) {
2152                         free(s->status_text);
2153                         s->status_text = t;
2154                 }
2155
2156         } else if (streq(key, "control-command")) {
2157                 ServiceExecCommand id;
2158
2159                 if ((id = service_exec_command_from_string(value)) < 0)
2160                         log_debug("Failed to parse exec-command value %s", value);
2161                 else {
2162                         s->control_command_id = id;
2163                         s->control_command = s->exec_command[id];
2164                 }
2165         } else if (streq(key, "socket-fd")) {
2166                 int fd;
2167
2168                 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2169                         log_debug("Failed to parse socket-fd value %s", value);
2170                 else {
2171
2172                         if (s->socket_fd >= 0)
2173                                 close_nointr_nofail(s->socket_fd);
2174                         s->socket_fd = fdset_remove(fds, fd);
2175                 }
2176         } else if (streq(key, "main-exec-status-pid")) {
2177                 pid_t pid;
2178
2179                 if (parse_pid(value, &pid) < 0)
2180                         log_debug("Failed to parse main-exec-status-pid value %s", value);
2181                 else
2182                         s->main_exec_status.pid = pid;
2183         } else if (streq(key, "main-exec-status-code")) {
2184                 int i;
2185
2186                 if (safe_atoi(value, &i) < 0)
2187                         log_debug("Failed to parse main-exec-status-code value %s", value);
2188                 else
2189                         s->main_exec_status.code = i;
2190         } else if (streq(key, "main-exec-status-status")) {
2191                 int i;
2192
2193                 if (safe_atoi(value, &i) < 0)
2194                         log_debug("Failed to parse main-exec-status-status value %s", value);
2195                 else
2196                         s->main_exec_status.status = i;
2197         } else if (streq(key, "main-exec-status-start-realtime")) {
2198                 uint64_t k;
2199
2200                 if (safe_atou64(value, &k) < 0)
2201                         log_debug("Failed to parse main-exec-status-start-realtime value %s", value);
2202                 else
2203                         s->main_exec_status.start_timestamp.realtime = (usec_t) k;
2204         } else if (streq(key, "main-exec-status-start-monotonic")) {
2205                 uint64_t k;
2206
2207                 if (safe_atou64(value, &k) < 0)
2208                         log_debug("Failed to parse main-exec-status-start-monotonic value %s", value);
2209                 else
2210                         s->main_exec_status.start_timestamp.monotonic = (usec_t) k;
2211         } else if (streq(key, "main-exec-status-exit-realtime")) {
2212                 uint64_t k;
2213
2214                 if (safe_atou64(value, &k) < 0)
2215                         log_debug("Failed to parse main-exec-status-exit-realtime value %s", value);
2216                 else
2217                         s->main_exec_status.exit_timestamp.realtime = (usec_t) k;
2218         } else if (streq(key, "main-exec-status-exit-monotonic")) {
2219                 uint64_t k;
2220
2221                 if (safe_atou64(value, &k) < 0)
2222                         log_debug("Failed to parse main-exec-status-exit-monotonic value %s", value);
2223                 else
2224                         s->main_exec_status.exit_timestamp.monotonic = (usec_t) k;
2225         } else
2226                 log_debug("Unknown serialization key '%s'", key);
2227
2228         return 0;
2229 }
2230
2231 static UnitActiveState service_active_state(Unit *u) {
2232         assert(u);
2233
2234         return state_translation_table[SERVICE(u)->state];
2235 }
2236
2237 static const char *service_sub_state_to_string(Unit *u) {
2238         assert(u);
2239
2240         return service_state_to_string(SERVICE(u)->state);
2241 }
2242
2243 static bool service_check_gc(Unit *u) {
2244         Service *s = SERVICE(u);
2245
2246         assert(s);
2247
2248         return !!s->sysv_path;
2249 }
2250
2251 static bool service_check_snapshot(Unit *u) {
2252         Service *s = SERVICE(u);
2253
2254         assert(s);
2255
2256         return !s->got_socket_fd;
2257 }
2258
2259 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2260         Service *s = SERVICE(u);
2261         bool success;
2262
2263         assert(s);
2264         assert(pid >= 0);
2265
2266         success = is_clean_exit(code, status);
2267
2268         if (s->main_pid == pid) {
2269
2270                 s->main_pid = 0;
2271                 exec_status_exit(&s->main_exec_status, pid, code, status);
2272
2273                 if (s->type != SERVICE_FORKING && s->control_command) {
2274                         s->control_command->exec_status = s->main_exec_status;
2275
2276                         if (s->control_command->ignore)
2277                                 success = true;
2278                 }
2279
2280                 log_full(success ? LOG_DEBUG : LOG_NOTICE,
2281                          "%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
2282                 s->failure = s->failure || !success;
2283
2284                 if (s->control_command &&
2285                     s->control_command->command_next &&
2286                     success) {
2287
2288                         /* There is another command to *
2289                          * execute, so let's do that. */
2290
2291                         log_debug("%s running next main command for state %s", u->meta.id, service_state_to_string(s->state));
2292                         service_run_next_main(s, success);
2293
2294                 } else {
2295
2296                         /* The service exited, so the service is officially
2297                          * gone. */
2298
2299                         s->control_command = NULL;
2300                         s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2301
2302                         switch (s->state) {
2303
2304                         case SERVICE_START_POST:
2305                         case SERVICE_RELOAD:
2306                         case SERVICE_STOP:
2307                                 /* Need to wait until the operation is
2308                                  * done */
2309                                 break;
2310
2311                         case SERVICE_START:
2312                                 if (s->type == SERVICE_ONESHOT) {
2313                                         /* This was our main goal, so let's go on */
2314                                         if (success)
2315                                                 service_enter_start_post(s);
2316                                         else
2317                                                 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2318                                         break;
2319                                 } else {
2320                                         assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY);
2321
2322                                         /* Fall through */
2323                                 }
2324
2325                         case SERVICE_RUNNING:
2326                                 service_enter_running(s, success);
2327                                 break;
2328
2329                         case SERVICE_STOP_SIGTERM:
2330                         case SERVICE_STOP_SIGKILL:
2331
2332                                 if (!control_pid_good(s))
2333                                         service_enter_stop_post(s, success);
2334
2335                                 /* If there is still a control process, wait for that first */
2336                                 break;
2337
2338                         default:
2339                                 assert_not_reached("Uh, main process died at wrong time.");
2340                         }
2341                 }
2342
2343         } else if (s->control_pid == pid) {
2344
2345                 s->control_pid = 0;
2346
2347                 if (s->control_command) {
2348                         exec_status_exit(&s->control_command->exec_status, pid, code, status);
2349
2350                         if (s->control_command->ignore)
2351                                 success = true;
2352                 }
2353
2354                log_full(success ? LOG_DEBUG : LOG_NOTICE,
2355                          "%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
2356                 s->failure = s->failure || !success;
2357
2358                 if (s->control_command &&
2359                     s->control_command->command_next &&
2360                     success) {
2361
2362                         /* There is another command to *
2363                          * execute, so let's do that. */
2364
2365                         log_debug("%s running next control command for state %s", u->meta.id, service_state_to_string(s->state));
2366                         service_run_next_control(s, success);
2367
2368                 } else {
2369                         /* No further commands for this step, so let's
2370                          * figure out what to do next */
2371
2372                         s->control_command = NULL;
2373                         s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2374
2375                         log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state));
2376
2377                         switch (s->state) {
2378
2379                         case SERVICE_START_PRE:
2380                                 if (success)
2381                                         service_enter_start(s);
2382                                 else
2383                                         service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2384                                 break;
2385
2386                         case SERVICE_START:
2387                                 assert(s->type == SERVICE_FORKING);
2388
2389                                 /* Let's try to load the pid
2390                                  * file here if we can. We
2391                                  * ignore the return value,
2392                                  * since the PID file might
2393                                  * actually be created by a
2394                                  * START_POST script */
2395
2396                                 if (success) {
2397                                         if (s->pid_file)
2398                                                 service_load_pid_file(s);
2399
2400                                         service_enter_start_post(s);
2401                                 } else
2402                                         service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2403
2404                                 break;
2405
2406                         case SERVICE_START_POST:
2407                                 if (success && s->pid_file && !s->main_pid_known) {
2408                                         int r;
2409
2410                                         /* Hmm, let's see if we can
2411                                          * load the pid now after the
2412                                          * start-post scripts got
2413                                          * executed. */
2414
2415                                         if ((r = service_load_pid_file(s)) < 0)
2416                                                 log_warning("%s: failed to load PID file %s: %s", s->meta.id, s->pid_file, strerror(-r));
2417                                 }
2418
2419                                 /* Fall through */
2420
2421                         case SERVICE_RELOAD:
2422                                 if (success)
2423                                         service_enter_running(s, true);
2424                                 else
2425                                         service_enter_stop(s, false);
2426
2427                                 break;
2428
2429                         case SERVICE_STOP:
2430                                 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
2431                                 break;
2432
2433                         case SERVICE_STOP_SIGTERM:
2434                         case SERVICE_STOP_SIGKILL:
2435                                 if (main_pid_good(s) <= 0)
2436                                         service_enter_stop_post(s, success);
2437
2438                                 /* If there is still a service
2439                                  * process around, wait until
2440                                  * that one quit, too */
2441                                 break;
2442
2443                         case SERVICE_STOP_POST:
2444                         case SERVICE_FINAL_SIGTERM:
2445                         case SERVICE_FINAL_SIGKILL:
2446                                 service_enter_dead(s, success, true);
2447                                 break;
2448
2449                         default:
2450                                 assert_not_reached("Uh, control process died at wrong time.");
2451                         }
2452                 }
2453         }
2454 }
2455
2456 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
2457         Service *s = SERVICE(u);
2458
2459         assert(s);
2460         assert(elapsed == 1);
2461
2462         assert(w == &s->timer_watch);
2463
2464         switch (s->state) {
2465
2466         case SERVICE_START_PRE:
2467         case SERVICE_START:
2468                 log_warning("%s operation timed out. Terminating.", u->meta.id);
2469                 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2470                 break;
2471
2472         case SERVICE_START_POST:
2473         case SERVICE_RELOAD:
2474                 log_warning("%s operation timed out. Stopping.", u->meta.id);
2475                 service_enter_stop(s, false);
2476                 break;
2477
2478         case SERVICE_STOP:
2479                 log_warning("%s stopping timed out. Terminating.", u->meta.id);
2480                 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2481                 break;
2482
2483         case SERVICE_STOP_SIGTERM:
2484                 log_warning("%s stopping timed out. Killing.", u->meta.id);
2485                 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
2486                 break;
2487
2488         case SERVICE_STOP_SIGKILL:
2489                 /* Uh, wie sent a SIGKILL and it is still not gone?
2490                  * Must be something we cannot kill, so let's just be
2491                  * weirded out and continue */
2492
2493                 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
2494                 service_enter_stop_post(s, false);
2495                 break;
2496
2497         case SERVICE_STOP_POST:
2498                 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
2499                 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2500                 break;
2501
2502         case SERVICE_FINAL_SIGTERM:
2503                 log_warning("%s stopping timed out (2). Killing.", u->meta.id);
2504                 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
2505                 break;
2506
2507         case SERVICE_FINAL_SIGKILL:
2508                 log_warning("%s still around after SIGKILL (2). Entering maintenance mode.", u->meta.id);
2509                 service_enter_dead(s, false, true);
2510                 break;
2511
2512         case SERVICE_AUTO_RESTART:
2513                 log_info("%s holdoff time over, scheduling restart.", u->meta.id);
2514                 service_enter_restart(s);
2515                 break;
2516
2517         default:
2518                 assert_not_reached("Timeout at wrong time.");
2519         }
2520 }
2521
2522 static void service_cgroup_notify_event(Unit *u) {
2523         Service *s = SERVICE(u);
2524
2525         assert(u);
2526
2527         log_debug("%s: cgroup is empty", u->meta.id);
2528
2529         switch (s->state) {
2530
2531                 /* Waiting for SIGCHLD is usually more interesting,
2532                  * because it includes return codes/signals. Which is
2533                  * why we ignore the cgroup events for most cases,
2534                  * except when we don't know pid which to expect the
2535                  * SIGCHLD for. */
2536
2537         case SERVICE_RUNNING:
2538                 service_enter_running(s, true);
2539                 break;
2540
2541         default:
2542                 ;
2543         }
2544 }
2545
2546 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
2547         Service *s = SERVICE(u);
2548         const char *e;
2549
2550         assert(u);
2551
2552         if (s->notify_access == NOTIFY_NONE) {
2553                 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
2554                             u->meta.id, (unsigned long) pid);
2555                 return;
2556         }
2557
2558         if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
2559                 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
2560                             u->meta.id, (unsigned long) pid, (unsigned long) s->main_pid);
2561                 return;
2562         }
2563
2564         log_debug("%s: Got message", u->meta.id);
2565
2566         /* Interpret MAINPID= */
2567         if ((e = strv_find_prefix(tags, "MAINPID=")) &&
2568             (s->state == SERVICE_START ||
2569              s->state == SERVICE_START_POST ||
2570              s->state == SERVICE_RUNNING ||
2571              s->state == SERVICE_RELOAD)) {
2572
2573                 if (parse_pid(e + 8, &pid) < 0)
2574                         log_warning("Failed to parse notification message %s", e);
2575                 else {
2576                         log_debug("%s: got %s", u->meta.id, e);
2577                         service_set_main_pid(s, pid);
2578                 }
2579         }
2580
2581         /* Interpret READY= */
2582         if (s->type == SERVICE_NOTIFY &&
2583             s->state == SERVICE_START &&
2584             strv_find(tags, "READY=1")) {
2585                 log_debug("%s: got READY=1", u->meta.id);
2586
2587                 service_enter_start_post(s);
2588         }
2589
2590         /* Interpret STATUS= */
2591         if ((e = strv_find_prefix(tags, "STATUS="))) {
2592                 char *t;
2593
2594                 if (e[7]) {
2595                         if (!(t = strdup(e+7))) {
2596                                 log_error("Failed to allocate string.");
2597                                 return;
2598                         }
2599
2600                         log_debug("%s: got %s", u->meta.id, e);
2601
2602                         free(s->status_text);
2603                         s->status_text = t;
2604                 } else {
2605                         free(s->status_text);
2606                         s->status_text = NULL;
2607                 }
2608
2609         }
2610 }
2611
2612 static int service_enumerate(Manager *m) {
2613         char **p;
2614         unsigned i;
2615         DIR *d = NULL;
2616         char *path = NULL, *fpath = NULL, *name = NULL;
2617         int r;
2618
2619         assert(m);
2620
2621         STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
2622                 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
2623                         struct dirent *de;
2624
2625                         free(path);
2626                         path = NULL;
2627                         if (asprintf(&path, "%s/%s", *p, rcnd_table[i].path) < 0) {
2628                                 r = -ENOMEM;
2629                                 goto finish;
2630                         }
2631
2632                         if (d)
2633                                 closedir(d);
2634
2635                         if (!(d = opendir(path))) {
2636                                 if (errno != ENOENT)
2637                                         log_warning("opendir() failed on %s: %s", path, strerror(errno));
2638
2639                                 continue;
2640                         }
2641
2642                         while ((de = readdir(d))) {
2643                                 Unit *service;
2644                                 int a, b;
2645
2646                                 if (ignore_file(de->d_name))
2647                                         continue;
2648
2649                                 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
2650                                         continue;
2651
2652                                 if (strlen(de->d_name) < 4)
2653                                         continue;
2654
2655                                 a = undecchar(de->d_name[1]);
2656                                 b = undecchar(de->d_name[2]);
2657
2658                                 if (a < 0 || b < 0)
2659                                         continue;
2660
2661                                 free(fpath);
2662                                 fpath = NULL;
2663                                 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i].path, de->d_name) < 0) {
2664                                         r = -ENOMEM;
2665                                         goto finish;
2666                                 }
2667
2668                                 if (access(fpath, X_OK) < 0) {
2669
2670                                         if (errno != ENOENT)
2671                                                 log_warning("access() failed on %s: %s", fpath, strerror(errno));
2672
2673                                         continue;
2674                                 }
2675
2676                                 free(name);
2677                                 if (!(name = sysv_translate_name(de->d_name + 3))) {
2678                                         r = -ENOMEM;
2679                                         goto finish;
2680                                 }
2681
2682                                 if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
2683                                         log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
2684                                         continue;
2685                                 }
2686
2687                                 if (de->d_name[0] == 'S' &&
2688                                     (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
2689                                         SERVICE(service)->sysv_start_priority =
2690                                                 MAX(a*10 + b, SERVICE(service)->sysv_start_priority);
2691                                         SERVICE(service)->sysv_enabled = true;
2692                                 }
2693
2694                                 manager_dispatch_load_queue(m);
2695                                 service = unit_follow_merge(service);
2696
2697                                 /* If this is a native service, rely
2698                                  * on native ways to pull in a
2699                                  * service, don't pull it in via sysv
2700                                  * rcN.d links. */
2701                                 if (service->meta.fragment_path)
2702                                         continue;
2703
2704                                 if (de->d_name[0] == 'S') {
2705
2706                                         if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
2707                                                 goto finish;
2708
2709                                 } else if (de->d_name[0] == 'K' &&
2710                                            (rcnd_table[i].type == RUNLEVEL_DOWN ||
2711                                             rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
2712
2713                                         /* We honour K links only for
2714                                          * halt/reboot. For the normal
2715                                          * runlevels we assume the
2716                                          * stop jobs will be
2717                                          * implicitly added by the
2718                                          * core logic. Also, we don't
2719                                          * really distuingish here
2720                                          * between the runlevels 0 and
2721                                          * 6 and just add them to the
2722                                          * special shutdown target. On
2723                                          * SUSE the boot.d/ runlevel
2724                                          * is also used for shutdown,
2725                                          * so we add links for that
2726                                          * too to the shutdown
2727                                          * target.*/
2728
2729                                         if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
2730                                                 goto finish;
2731                                 }
2732                         }
2733                 }
2734
2735         r = 0;
2736
2737 finish:
2738         free(path);
2739         free(fpath);
2740         free(name);
2741
2742         if (d)
2743                 closedir(d);
2744
2745         return r;
2746 }
2747
2748 static void service_bus_name_owner_change(
2749                 Unit *u,
2750                 const char *name,
2751                 const char *old_owner,
2752                 const char *new_owner) {
2753
2754         Service *s = SERVICE(u);
2755
2756         assert(s);
2757         assert(name);
2758
2759         assert(streq(s->bus_name, name));
2760         assert(old_owner || new_owner);
2761
2762         if (old_owner && new_owner)
2763                 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner);
2764         else if (old_owner)
2765                 log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner);
2766         else
2767                 log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner);
2768
2769         s->bus_name_good = !!new_owner;
2770
2771         if (s->type == SERVICE_DBUS) {
2772
2773                 /* service_enter_running() will figure out what to
2774                  * do */
2775                 if (s->state == SERVICE_RUNNING)
2776                         service_enter_running(s, true);
2777                 else if (s->state == SERVICE_START && new_owner)
2778                         service_enter_start_post(s);
2779
2780         } else if (new_owner &&
2781                    s->main_pid <= 0 &&
2782                    (s->state == SERVICE_START ||
2783                     s->state == SERVICE_START_POST ||
2784                     s->state == SERVICE_RUNNING ||
2785                     s->state == SERVICE_RELOAD)) {
2786
2787                 /* Try to acquire PID from bus service */
2788                 log_debug("Trying to acquire PID from D-Bus name...");
2789
2790                 bus_query_pid(u->meta.manager, name);
2791         }
2792 }
2793
2794 static void service_bus_query_pid_done(
2795                 Unit *u,
2796                 const char *name,
2797                 pid_t pid) {
2798
2799         Service *s = SERVICE(u);
2800
2801         assert(s);
2802         assert(name);
2803
2804         log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid);
2805
2806         if (s->main_pid <= 0 &&
2807             (s->state == SERVICE_START ||
2808              s->state == SERVICE_START_POST ||
2809              s->state == SERVICE_RUNNING ||
2810              s->state == SERVICE_RELOAD))
2811                 service_set_main_pid(s, pid);
2812 }
2813
2814 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
2815         assert(s);
2816         assert(fd >= 0);
2817
2818         /* This is called by the socket code when instantiating a new
2819          * service for a stream socket and the socket needs to be
2820          * configured. */
2821
2822         if (s->meta.load_state != UNIT_LOADED)
2823                 return -EINVAL;
2824
2825         if (s->socket_fd >= 0)
2826                 return -EBUSY;
2827
2828         if (s->state != SERVICE_DEAD)
2829                 return -EAGAIN;
2830
2831         s->socket_fd = fd;
2832         s->got_socket_fd = true;
2833         s->socket = sock;
2834
2835         return 0;
2836 }
2837
2838 static void service_reset_maintenance(Unit *u) {
2839         Service *s = SERVICE(u);
2840
2841         assert(s);
2842
2843         if (s->state == SERVICE_MAINTENANCE)
2844                 service_set_state(s, SERVICE_DEAD);
2845
2846         s->failure = false;
2847 }
2848
2849 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2850         [SERVICE_DEAD] = "dead",
2851         [SERVICE_START_PRE] = "start-pre",
2852         [SERVICE_START] = "start",
2853         [SERVICE_START_POST] = "start-post",
2854         [SERVICE_RUNNING] = "running",
2855         [SERVICE_EXITED] = "exited",
2856         [SERVICE_RELOAD] = "reload",
2857         [SERVICE_STOP] = "stop",
2858         [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2859         [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2860         [SERVICE_STOP_POST] = "stop-post",
2861         [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2862         [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2863         [SERVICE_MAINTENANCE] = "maintenance",
2864         [SERVICE_AUTO_RESTART] = "auto-restart",
2865 };
2866
2867 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2868
2869 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2870         [SERVICE_ONCE] = "once",
2871         [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
2872         [SERVICE_RESTART_ALWAYS] = "restart-always",
2873 };
2874
2875 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2876
2877 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2878         [SERVICE_SIMPLE] = "simple",
2879         [SERVICE_FORKING] = "forking",
2880         [SERVICE_ONESHOT] = "oneshot",
2881         [SERVICE_DBUS] = "dbus",
2882         [SERVICE_NOTIFY] = "notify"
2883 };
2884
2885 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2886
2887 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
2888         [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2889         [SERVICE_EXEC_START] = "ExecStart",
2890         [SERVICE_EXEC_START_POST] = "ExecStartPost",
2891         [SERVICE_EXEC_RELOAD] = "ExecReload",
2892         [SERVICE_EXEC_STOP] = "ExecStop",
2893         [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2894 };
2895
2896 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2897
2898 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
2899         [NOTIFY_NONE] = "none",
2900         [NOTIFY_MAIN] = "main",
2901         [NOTIFY_ALL] = "all"
2902 };
2903
2904 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
2905
2906 const UnitVTable service_vtable = {
2907         .suffix = ".service",
2908         .show_status = true,
2909
2910         .init = service_init,
2911         .done = service_done,
2912         .load = service_load,
2913
2914         .coldplug = service_coldplug,
2915
2916         .dump = service_dump,
2917
2918         .start = service_start,
2919         .stop = service_stop,
2920         .reload = service_reload,
2921
2922         .can_reload = service_can_reload,
2923
2924         .serialize = service_serialize,
2925         .deserialize_item = service_deserialize_item,
2926
2927         .active_state = service_active_state,
2928         .sub_state_to_string = service_sub_state_to_string,
2929
2930         .check_gc = service_check_gc,
2931         .check_snapshot = service_check_snapshot,
2932
2933         .sigchld_event = service_sigchld_event,
2934         .timer_event = service_timer_event,
2935
2936         .reset_maintenance = service_reset_maintenance,
2937
2938         .cgroup_notify_empty = service_cgroup_notify_event,
2939         .notify_message = service_notify_message,
2940
2941         .bus_name_owner_change = service_bus_name_owner_change,
2942         .bus_query_pid_done = service_bus_query_pid_done,
2943
2944         .bus_message_handler = bus_service_message_handler,
2945
2946         .enumerate = service_enumerate
2947 };