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