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