chiark / gitweb /
build-sys: move .pc files next to the matching sources
[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 #include "exit-status.h"
38 #include "def.h"
39 #include "util.h"
40
41 #ifdef HAVE_SYSV_COMPAT
42
43 #define DEFAULT_SYSV_TIMEOUT_USEC (5*USEC_PER_MINUTE)
44
45 typedef enum RunlevelType {
46         RUNLEVEL_UP,
47         RUNLEVEL_DOWN,
48         RUNLEVEL_SYSINIT
49 } RunlevelType;
50
51 static const struct {
52         const char *path;
53         const char *target;
54         const RunlevelType type;
55 } rcnd_table[] = {
56         /* Standard SysV runlevels for start-up */
57         { "rc1.d",  SPECIAL_RESCUE_TARGET,    RUNLEVEL_UP },
58         { "rc2.d",  SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP },
59         { "rc3.d",  SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP },
60         { "rc4.d",  SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP },
61         { "rc5.d",  SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP },
62
63 #ifdef TARGET_SUSE
64         /* SUSE style boot.d */
65         { "boot.d", SPECIAL_SYSINIT_TARGET,   RUNLEVEL_SYSINIT },
66 #endif
67
68 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
69         /* Debian style rcS.d */
70         { "rcS.d",  SPECIAL_SYSINIT_TARGET,   RUNLEVEL_SYSINIT },
71 #endif
72
73         /* Standard SysV runlevels for shutdown */
74         { "rc0.d",  SPECIAL_POWEROFF_TARGET,  RUNLEVEL_DOWN },
75         { "rc6.d",  SPECIAL_REBOOT_TARGET,    RUNLEVEL_DOWN }
76
77         /* Note that the order here matters, as we read the
78            directories in this order, and we want to make sure that
79            sysv_start_priority is known when we first load the
80            unit. And that value we only know from S links. Hence
81            UP/SYSINIT must be read before DOWN */
82 };
83
84 #define RUNLEVELS_UP "12345"
85 /* #define RUNLEVELS_DOWN "06" */
86 #define RUNLEVELS_BOOT "bBsS"
87 #endif
88
89 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
90         [SERVICE_DEAD] = UNIT_INACTIVE,
91         [SERVICE_START_PRE] = UNIT_ACTIVATING,
92         [SERVICE_START] = UNIT_ACTIVATING,
93         [SERVICE_START_POST] = UNIT_ACTIVATING,
94         [SERVICE_RUNNING] = UNIT_ACTIVE,
95         [SERVICE_EXITED] = UNIT_ACTIVE,
96         [SERVICE_RELOAD] = UNIT_RELOADING,
97         [SERVICE_STOP] = UNIT_DEACTIVATING,
98         [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
99         [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
100         [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
101         [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
102         [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
103         [SERVICE_FAILED] = UNIT_FAILED,
104         [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
105 };
106
107 static void service_init(Unit *u) {
108         Service *s = SERVICE(u);
109
110         assert(u);
111         assert(u->meta.load_state == UNIT_STUB);
112
113         s->timeout_usec = DEFAULT_TIMEOUT_USEC;
114         s->restart_usec = DEFAULT_RESTART_USEC;
115         s->timer_watch.type = WATCH_INVALID;
116 #ifdef HAVE_SYSV_COMPAT
117         s->sysv_start_priority = -1;
118         s->sysv_start_priority_from_rcnd = -1;
119 #endif
120         s->socket_fd = -1;
121         s->guess_main_pid = true;
122
123         exec_context_init(&s->exec_context);
124
125         RATELIMIT_INIT(s->ratelimit, 10*USEC_PER_SEC, 5);
126
127         s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
128 }
129
130 static void service_unwatch_control_pid(Service *s) {
131         assert(s);
132
133         if (s->control_pid <= 0)
134                 return;
135
136         unit_unwatch_pid(UNIT(s), s->control_pid);
137         s->control_pid = 0;
138 }
139
140 static void service_unwatch_main_pid(Service *s) {
141         assert(s);
142
143         if (s->main_pid <= 0)
144                 return;
145
146         unit_unwatch_pid(UNIT(s), s->main_pid);
147         s->main_pid = 0;
148 }
149
150 static int service_set_main_pid(Service *s, pid_t pid) {
151         pid_t ppid;
152
153         assert(s);
154
155         if (pid <= 1)
156                 return -EINVAL;
157
158         if (pid == getpid())
159                 return -EINVAL;
160
161         s->main_pid = pid;
162         s->main_pid_known = true;
163
164         if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) {
165                 log_warning("%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
166                             s->meta.id, (unsigned long) pid);
167
168                 s->main_pid_alien = true;
169         } else
170                 s->main_pid_alien = false;
171
172         exec_status_start(&s->main_exec_status, pid);
173
174         return 0;
175 }
176
177 static void service_close_socket_fd(Service *s) {
178         assert(s);
179
180         if (s->socket_fd < 0)
181                 return;
182
183         close_nointr_nofail(s->socket_fd);
184         s->socket_fd = -1;
185 }
186
187 static void service_connection_unref(Service *s) {
188         assert(s);
189
190         if (!UNIT_DEREF(s->accept_socket))
191                 return;
192
193         socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket)));
194         unit_ref_unset(&s->accept_socket);
195 }
196
197 static void service_done(Unit *u) {
198         Service *s = SERVICE(u);
199
200         assert(s);
201
202         free(s->pid_file);
203         s->pid_file = NULL;
204
205 #ifdef HAVE_SYSV_COMPAT
206         free(s->sysv_path);
207         s->sysv_path = NULL;
208
209         free(s->sysv_runlevels);
210         s->sysv_runlevels = NULL;
211 #endif
212
213         free(s->status_text);
214         s->status_text = NULL;
215
216         exec_context_done(&s->exec_context);
217         exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
218         s->control_command = NULL;
219         s->main_command = NULL;
220
221         /* This will leak a process, but at least no memory or any of
222          * our resources */
223         service_unwatch_main_pid(s);
224         service_unwatch_control_pid(s);
225
226         if (s->bus_name)  {
227                 unit_unwatch_bus_name(UNIT(u), s->bus_name);
228                 free(s->bus_name);
229                 s->bus_name = NULL;
230         }
231
232         service_close_socket_fd(s);
233         service_connection_unref(s);
234
235         unit_ref_unset(&s->accept_socket);
236
237         unit_unwatch_timer(u, &s->timer_watch);
238 }
239
240 #ifdef HAVE_SYSV_COMPAT
241 static char *sysv_translate_name(const char *name) {
242         char *r;
243
244         if (!(r = new(char, strlen(name) + sizeof(".service"))))
245                 return NULL;
246
247 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
248         if (endswith(name, ".sh"))
249                 /* Drop Debian-style .sh suffix */
250                 strcpy(stpcpy(r, name) - 3, ".service");
251 #endif
252 #ifdef TARGET_SUSE
253         if (startswith(name, "boot."))
254                 /* Drop SuSE-style boot. prefix */
255                 strcpy(stpcpy(r, name + 5), ".service");
256 #endif
257 #ifdef TARGET_FRUGALWARE
258         if (startswith(name, "rc."))
259                 /* Drop Frugalware-style rc. prefix */
260                 strcpy(stpcpy(r, name + 3), ".service");
261 #endif
262         else
263                 /* Normal init scripts */
264                 strcpy(stpcpy(r, name), ".service");
265
266         return r;
267 }
268
269 static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
270
271         /* We silently ignore the $ prefix here. According to the LSB
272          * spec it simply indicates whether something is a
273          * standardized name or a distribution-specific one. Since we
274          * just follow what already exists and do not introduce new
275          * uses or names we don't care who introduced a new name. */
276
277         static const char * const table[] = {
278                 /* LSB defined facilities */
279                 "local_fs",             SPECIAL_LOCAL_FS_TARGET,
280 #if defined(TARGET_MANDRIVA) || defined(TARGET_MAGEIA)
281 #else
282                 /* Due to unfortunate name selection in Mandriva,
283                  * $network is provided by network-up which is ordered
284                  * after network which actually starts interfaces.
285                  * To break the loop, just ignore it */
286                 "network",              SPECIAL_NETWORK_TARGET,
287 #endif
288                 "named",                SPECIAL_NSS_LOOKUP_TARGET,
289                 "portmap",              SPECIAL_RPCBIND_TARGET,
290                 "remote_fs",            SPECIAL_REMOTE_FS_TARGET,
291                 "syslog",               SPECIAL_SYSLOG_TARGET,
292                 "time",                 SPECIAL_TIME_SYNC_TARGET,
293
294                 /* common extensions */
295                 "mail-transfer-agent",  SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
296                 "x-display-manager",    SPECIAL_DISPLAY_MANAGER_SERVICE,
297                 "null",                 NULL,
298
299 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
300                 "mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
301 #endif
302
303 #ifdef TARGET_FEDORA
304                 "MTA",                  SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
305                 "smtpdaemon",           SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
306                 "httpd",                SPECIAL_HTTP_DAEMON_TARGET,
307 #endif
308
309 #ifdef TARGET_SUSE
310                 "smtp",                 SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
311 #endif
312         };
313
314         unsigned i;
315         char *r;
316         const char *n;
317
318         assert(name);
319         assert(_r);
320
321         n = *name == '$' ? name + 1 : name;
322
323         for (i = 0; i < ELEMENTSOF(table); i += 2) {
324
325                 if (!streq(table[i], n))
326                         continue;
327
328                 if (!table[i+1])
329                         return 0;
330
331                 if (!(r = strdup(table[i+1])))
332                         return -ENOMEM;
333
334                 goto finish;
335         }
336
337         /* If we don't know this name, fallback heuristics to figure
338          * out whether something is a target or a service alias. */
339
340         if (*name == '$') {
341                 if (!unit_prefix_is_valid(n))
342                         return -EINVAL;
343
344                 /* Facilities starting with $ are most likely targets */
345                 r = unit_name_build(n, NULL, ".target");
346         } else if (filename && streq(name, filename))
347                 /* Names equaling the file name of the services are redundant */
348                 return 0;
349         else
350                 /* Everything else we assume to be normal service names */
351                 r = sysv_translate_name(n);
352
353         if (!r)
354                 return -ENOMEM;
355
356 finish:
357         *_r = r;
358
359         return 1;
360 }
361
362 static int sysv_fix_order(Service *s) {
363         Meta *other;
364         int r;
365
366         assert(s);
367
368         if (s->sysv_start_priority < 0)
369                 return 0;
370
371         /* For each pair of services where at least one lacks a LSB
372          * header, we use the start priority value to order things. */
373
374         LIST_FOREACH(units_by_type, other, s->meta.manager->units_by_type[UNIT_SERVICE]) {
375                 Service *t;
376                 UnitDependency d;
377                 bool special_s, special_t;
378
379                 t = (Service*) other;
380
381                 if (s == t)
382                         continue;
383
384                 if (t->meta.load_state != UNIT_LOADED)
385                         continue;
386
387                 if (t->sysv_start_priority < 0)
388                         continue;
389
390                 /* If both units have modern headers we don't care
391                  * about the priorities */
392                 if ((s->meta.fragment_path || s->sysv_has_lsb) &&
393                     (t->meta.fragment_path || t->sysv_has_lsb))
394                         continue;
395
396                 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
397                 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
398
399                 if (special_t && !special_s)
400                         d = UNIT_AFTER;
401                 else if (special_s && !special_t)
402                         d = UNIT_BEFORE;
403                 else if (t->sysv_start_priority < s->sysv_start_priority)
404                         d = UNIT_AFTER;
405                 else if (t->sysv_start_priority > s->sysv_start_priority)
406                         d = UNIT_BEFORE;
407                 else
408                         continue;
409
410                 /* FIXME: Maybe we should compare the name here lexicographically? */
411
412                 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
413                         return r;
414         }
415
416         return 0;
417 }
418
419 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
420         ExecCommand *c;
421
422         if (!(c = new0(ExecCommand, 1)))
423                 return NULL;
424
425         if (!(c->path = strdup(path))) {
426                 free(c);
427                 return NULL;
428         }
429
430         if (!(c->argv = strv_new(path, arg1, NULL))) {
431                 free(c->path);
432                 free(c);
433                 return NULL;
434         }
435
436         return c;
437 }
438
439 static int sysv_exec_commands(Service *s) {
440         ExecCommand *c;
441
442         assert(s);
443         assert(s->sysv_path);
444
445         if (!(c = exec_command_new(s->sysv_path, "start")))
446                 return -ENOMEM;
447         exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
448
449         if (!(c = exec_command_new(s->sysv_path, "stop")))
450                 return -ENOMEM;
451         exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
452
453         if (!(c = exec_command_new(s->sysv_path, "reload")))
454                 return -ENOMEM;
455         exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
456
457         return 0;
458 }
459
460 static int service_load_sysv_path(Service *s, const char *path) {
461         FILE *f;
462         Unit *u;
463         unsigned line = 0;
464         int r;
465         enum {
466                 NORMAL,
467                 DESCRIPTION,
468                 LSB,
469                 LSB_DESCRIPTION
470         } state = NORMAL;
471         char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description;
472         struct stat st;
473
474         assert(s);
475         assert(path);
476
477         u = UNIT(s);
478
479         if (!(f = fopen(path, "re"))) {
480                 r = errno == ENOENT ? 0 : -errno;
481                 goto finish;
482         }
483
484         zero(st);
485         if (fstat(fileno(f), &st) < 0) {
486                 r = -errno;
487                 goto finish;
488         }
489
490         free(s->sysv_path);
491         if (!(s->sysv_path = strdup(path))) {
492                 r = -ENOMEM;
493                 goto finish;
494         }
495
496         s->sysv_mtime = timespec_load(&st.st_mtim);
497
498         if (null_or_empty(&st)) {
499                 u->meta.load_state = UNIT_MASKED;
500                 r = 0;
501                 goto finish;
502         }
503
504         while (!feof(f)) {
505                 char l[LINE_MAX], *t;
506
507                 if (!fgets(l, sizeof(l), f)) {
508                         if (feof(f))
509                                 break;
510
511                         r = -errno;
512                         log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
513                         goto finish;
514                 }
515
516                 line++;
517
518                 t = strstrip(l);
519                 if (*t != '#')
520                         continue;
521
522                 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
523                         state = LSB;
524                         s->sysv_has_lsb = true;
525                         continue;
526                 }
527
528                 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
529                         state = NORMAL;
530                         continue;
531                 }
532
533                 t++;
534                 t += strspn(t, WHITESPACE);
535
536                 if (state == NORMAL) {
537
538                         /* Try to parse Red Hat style chkconfig headers */
539
540                         if (startswith_no_case(t, "chkconfig:")) {
541                                 int start_priority;
542                                 char runlevels[16], *k;
543
544                                 state = NORMAL;
545
546                                 if (sscanf(t+10, "%15s %i %*i",
547                                            runlevels,
548                                            &start_priority) != 2) {
549
550                                         log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
551                                         continue;
552                                 }
553
554                                 /* A start priority gathered from the
555                                  * symlink farms is preferred over the
556                                  * data from the LSB header. */
557                                 if (start_priority < 0 || start_priority > 99)
558                                         log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
559                                 else
560                                         s->sysv_start_priority = start_priority;
561
562                                 char_array_0(runlevels);
563                                 k = delete_chars(runlevels, WHITESPACE "-");
564
565                                 if (k[0]) {
566                                         char *d;
567
568                                         if (!(d = strdup(k))) {
569                                                 r = -ENOMEM;
570                                                 goto finish;
571                                         }
572
573                                         free(s->sysv_runlevels);
574                                         s->sysv_runlevels = d;
575                                 }
576
577                         } else if (startswith_no_case(t, "description:")) {
578
579                                 size_t k = strlen(t);
580                                 char *d;
581                                 const char *j;
582
583                                 if (t[k-1] == '\\') {
584                                         state = DESCRIPTION;
585                                         t[k-1] = 0;
586                                 }
587
588                                 if ((j = strstrip(t+12)) && *j) {
589                                         if (!(d = strdup(j))) {
590                                                 r = -ENOMEM;
591                                                 goto finish;
592                                         }
593                                 } else
594                                         d = NULL;
595
596                                 free(chkconfig_description);
597                                 chkconfig_description = d;
598
599                         } else if (startswith_no_case(t, "pidfile:")) {
600
601                                 char *fn;
602
603                                 state = NORMAL;
604
605                                 fn = strstrip(t+8);
606                                 if (!path_is_absolute(fn)) {
607                                         log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
608                                         continue;
609                                 }
610
611                                 if (!(fn = strdup(fn))) {
612                                         r = -ENOMEM;
613                                         goto finish;
614                                 }
615
616                                 free(s->pid_file);
617                                 s->pid_file = fn;
618                         }
619
620                 } else if (state == DESCRIPTION) {
621
622                         /* Try to parse Red Hat style description
623                          * continuation */
624
625                         size_t k = strlen(t);
626                         char *j;
627
628                         if (t[k-1] == '\\')
629                                 t[k-1] = 0;
630                         else
631                                 state = NORMAL;
632
633                         if ((j = strstrip(t)) && *j) {
634                                 char *d = NULL;
635
636                                 if (chkconfig_description)
637                                         d = join(chkconfig_description, " ", j, NULL);
638                                 else
639                                         d = strdup(j);
640
641                                 if (!d) {
642                                         r = -ENOMEM;
643                                         goto finish;
644                                 }
645
646                                 free(chkconfig_description);
647                                 chkconfig_description = d;
648                         }
649
650                 } else if (state == LSB || state == LSB_DESCRIPTION) {
651
652                         if (startswith_no_case(t, "Provides:")) {
653                                 char *i, *w;
654                                 size_t z;
655
656                                 state = LSB;
657
658                                 FOREACH_WORD_QUOTED(w, z, t+9, i) {
659                                         char *n, *m;
660
661                                         if (!(n = strndup(w, z))) {
662                                                 r = -ENOMEM;
663                                                 goto finish;
664                                         }
665
666                                         r = sysv_translate_facility(n, file_name_from_path(path), &m);
667                                         free(n);
668
669                                         if (r < 0)
670                                                 goto finish;
671
672                                         if (r == 0)
673                                                 continue;
674
675                                         if (unit_name_to_type(m) == UNIT_SERVICE)
676                                                 r = unit_add_name(u, m);
677                                         else
678                                                 /* NB: SysV targets
679                                                  * which are provided
680                                                  * by a service are
681                                                  * pulled in by the
682                                                  * services, as an
683                                                  * indication that the
684                                                  * generic service is
685                                                  * now available. This
686                                                  * is strictly
687                                                  * one-way. The
688                                                  * targets do NOT pull
689                                                  * in the SysV
690                                                  * services! */
691                                                 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_WANTS, m, NULL, true);
692
693                                         if (r < 0)
694                                                 log_error("[%s:%u] Failed to add LSB Provides name %s, ignoring: %s", path, line, m, strerror(-r));
695
696                                         free(m);
697                                 }
698
699                         } else if (startswith_no_case(t, "Required-Start:") ||
700                                    startswith_no_case(t, "Should-Start:") ||
701                                    startswith_no_case(t, "X-Start-Before:") ||
702                                    startswith_no_case(t, "X-Start-After:")) {
703                                 char *i, *w;
704                                 size_t z;
705
706                                 state = LSB;
707
708                                 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
709                                         char *n, *m;
710
711                                         if (!(n = strndup(w, z))) {
712                                                 r = -ENOMEM;
713                                                 goto finish;
714                                         }
715
716                                         r = sysv_translate_facility(n, file_name_from_path(path), &m);
717
718                                         if (r < 0) {
719                                                 log_error("[%s:%u] Failed to translate LSB dependency %s, ignoring: %s", path, line, n, strerror(-r));
720                                                 free(n);
721                                                 continue;
722                                         }
723
724                                         free(n);
725
726                                         if (r == 0)
727                                                 continue;
728
729                                         r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
730
731                                         if (r < 0)
732                                                 log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", path, line, m, strerror(-r));
733
734                                         free(m);
735                                 }
736                         } else if (startswith_no_case(t, "Default-Start:")) {
737                                 char *k, *d;
738
739                                 state = LSB;
740
741                                 k = delete_chars(t+14, WHITESPACE "-");
742
743                                 if (k[0] != 0) {
744                                         if (!(d = strdup(k))) {
745                                                 r = -ENOMEM;
746                                                 goto finish;
747                                         }
748
749                                         free(s->sysv_runlevels);
750                                         s->sysv_runlevels = d;
751                                 }
752
753                         } else if (startswith_no_case(t, "Description:")) {
754                                 char *d, *j;
755
756                                 state = LSB_DESCRIPTION;
757
758                                 if ((j = strstrip(t+12)) && *j) {
759                                         if (!(d = strdup(j))) {
760                                                 r = -ENOMEM;
761                                                 goto finish;
762                                         }
763                                 } else
764                                         d = NULL;
765
766                                 free(long_description);
767                                 long_description = d;
768
769                         } else if (startswith_no_case(t, "Short-Description:")) {
770                                 char *d, *j;
771
772                                 state = LSB;
773
774                                 if ((j = strstrip(t+18)) && *j) {
775                                         if (!(d = strdup(j))) {
776                                                 r = -ENOMEM;
777                                                 goto finish;
778                                         }
779                                 } else
780                                         d = NULL;
781
782                                 free(short_description);
783                                 short_description = d;
784
785                         } else if (state == LSB_DESCRIPTION) {
786
787                                 if (startswith(l, "#\t") || startswith(l, "#  ")) {
788                                         char *j;
789
790                                         if ((j = strstrip(t)) && *j) {
791                                                 char *d = NULL;
792
793                                                 if (long_description)
794                                                         d = join(long_description, " ", t, NULL);
795                                                 else
796                                                         d = strdup(j);
797
798                                                 if (!d) {
799                                                         r = -ENOMEM;
800                                                         goto finish;
801                                                 }
802
803                                                 free(long_description);
804                                                 long_description = d;
805                                         }
806
807                                 } else
808                                         state = LSB;
809                         }
810                 }
811         }
812
813         if ((r = sysv_exec_commands(s)) < 0)
814                 goto finish;
815         if (s->sysv_runlevels &&
816             chars_intersect(RUNLEVELS_BOOT, s->sysv_runlevels) &&
817             chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
818                 /* Service has both boot and "up" runlevels
819                    configured.  Kill the "up" ones. */
820                 delete_chars(s->sysv_runlevels, RUNLEVELS_UP);
821         }
822
823         if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
824                 /* If there a runlevels configured for this service
825                  * but none of the standard ones, then we assume this
826                  * is some special kind of service (which might be
827                  * needed for early boot) and don't create any links
828                  * to it. */
829
830                 s->meta.default_dependencies = false;
831
832                 /* Don't timeout special services during boot (like fsck) */
833                 s->timeout_usec = 0;
834         } else
835                 s->timeout_usec = DEFAULT_SYSV_TIMEOUT_USEC;
836
837         /* Special setting for all SysV services */
838         s->type = SERVICE_FORKING;
839         s->remain_after_exit = !s->pid_file;
840         s->guess_main_pid = false;
841         s->restart = SERVICE_RESTART_NO;
842
843         if (s->meta.manager->sysv_console)
844                 s->exec_context.std_output = EXEC_OUTPUT_JOURNAL_AND_CONSOLE;
845
846         s->exec_context.kill_mode = KILL_PROCESS;
847
848         /* We use the long description only if
849          * no short description is set. */
850
851         if (short_description)
852                 description = short_description;
853         else if (chkconfig_description)
854                 description = chkconfig_description;
855         else if (long_description)
856                 description = long_description;
857         else
858                 description = NULL;
859
860         if (description) {
861                 char *d;
862
863                 if (!(d = strappend(s->sysv_has_lsb ? "LSB: " : "SYSV: ", description))) {
864                         r = -ENOMEM;
865                         goto finish;
866                 }
867
868                 u->meta.description = d;
869         }
870
871         /* The priority that has been set in /etc/rcN.d/ hierarchies
872          * takes precedence over what is stored as default in the LSB
873          * header */
874         if (s->sysv_start_priority_from_rcnd >= 0)
875                 s->sysv_start_priority = s->sysv_start_priority_from_rcnd;
876
877         u->meta.load_state = UNIT_LOADED;
878         r = 0;
879
880 finish:
881
882         if (f)
883                 fclose(f);
884
885         free(short_description);
886         free(long_description);
887         free(chkconfig_description);
888
889         return r;
890 }
891
892 static int service_load_sysv_name(Service *s, const char *name) {
893         char **p;
894
895         assert(s);
896         assert(name);
897
898         /* For SysV services we strip the boot.*, rc.* and *.sh
899          * prefixes/suffixes. */
900 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
901         if (endswith(name, ".sh.service"))
902                 return -ENOENT;
903 #endif
904
905 #ifdef TARGET_SUSE
906         if (startswith(name, "boot."))
907                 return -ENOENT;
908 #endif
909
910 #ifdef TARGET_FRUGALWARE
911         if (startswith(name, "rc."))
912                 return -ENOENT;
913 #endif
914
915         STRV_FOREACH(p, s->meta.manager->lookup_paths.sysvinit_path) {
916                 char *path;
917                 int r;
918
919                 path = join(*p, "/", name, NULL);
920                 if (!path)
921                         return -ENOMEM;
922
923                 assert(endswith(path, ".service"));
924                 path[strlen(path)-8] = 0;
925
926                 r = service_load_sysv_path(s, path);
927
928 #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_ANGSTROM)
929                 if (r >= 0 && s->meta.load_state == UNIT_STUB) {
930                         /* Try Debian style *.sh source'able init scripts */
931                         strcat(path, ".sh");
932                         r = service_load_sysv_path(s, path);
933                 }
934 #endif
935                 free(path);
936
937 #ifdef TARGET_SUSE
938                 if (r >= 0 && s->meta.load_state == UNIT_STUB) {
939                         /* Try SUSE style boot.* init scripts */
940
941                         path = join(*p, "/boot.", name, NULL);
942                         if (!path)
943                                 return -ENOMEM;
944
945                         /* Drop .service suffix */
946                         path[strlen(path)-8] = 0;
947                         r = service_load_sysv_path(s, path);
948                         free(path);
949                 }
950 #endif
951
952 #ifdef TARGET_FRUGALWARE
953                 if (r >= 0 && s->meta.load_state == UNIT_STUB) {
954                         /* Try Frugalware style rc.* init scripts */
955
956                         path = join(*p, "/rc.", name, NULL);
957                         if (!path)
958                                 return -ENOMEM;
959
960                         /* Drop .service suffix */
961                         path[strlen(path)-8] = 0;
962                         r = service_load_sysv_path(s, path);
963                         free(path);
964                 }
965 #endif
966
967                 if (r < 0)
968                         return r;
969
970                 if ((s->meta.load_state != UNIT_STUB))
971                         break;
972         }
973
974         return 0;
975 }
976
977 static int service_load_sysv(Service *s) {
978         const char *t;
979         Iterator i;
980         int r;
981
982         assert(s);
983
984         /* Load service data from SysV init scripts, preferably with
985          * LSB headers ... */
986
987         if (strv_isempty(s->meta.manager->lookup_paths.sysvinit_path))
988                 return 0;
989
990         if ((t = s->meta.id))
991                 if ((r = service_load_sysv_name(s, t)) < 0)
992                         return r;
993
994         if (s->meta.load_state == UNIT_STUB)
995                 SET_FOREACH(t, s->meta.names, i) {
996                         if (t == s->meta.id)
997                                 continue;
998
999                         if ((r = service_load_sysv_name(s, t)) < 0)
1000                                 return r;
1001
1002                         if (s->meta.load_state != UNIT_STUB)
1003                                 break;
1004                 }
1005
1006         return 0;
1007 }
1008 #endif
1009
1010 static int fsck_fix_order(Service *s) {
1011         Meta *other;
1012         int r;
1013
1014         assert(s);
1015
1016         if (s->fsck_passno <= 0)
1017                 return 0;
1018
1019         /* For each pair of services where both have an fsck priority
1020          * we order things based on it. */
1021
1022         LIST_FOREACH(units_by_type, other, s->meta.manager->units_by_type[UNIT_SERVICE]) {
1023                 Service *t;
1024                 UnitDependency d;
1025
1026                 t = (Service*) other;
1027
1028                 if (s == t)
1029                         continue;
1030
1031                 if (t->meta.load_state != UNIT_LOADED)
1032                         continue;
1033
1034                 if (t->fsck_passno <= 0)
1035                         continue;
1036
1037                 if (t->fsck_passno < s->fsck_passno)
1038                         d = UNIT_AFTER;
1039                 else if (t->fsck_passno > s->fsck_passno)
1040                         d = UNIT_BEFORE;
1041                 else
1042                         continue;
1043
1044                 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
1045                         return r;
1046         }
1047
1048         return 0;
1049 }
1050
1051 static int service_verify(Service *s) {
1052         assert(s);
1053
1054         if (s->meta.load_state != UNIT_LOADED)
1055                 return 0;
1056
1057         if (!s->exec_command[SERVICE_EXEC_START]) {
1058                 log_error("%s lacks ExecStart setting. Refusing.", s->meta.id);
1059                 return -EINVAL;
1060         }
1061
1062         if (s->type != SERVICE_ONESHOT &&
1063             s->exec_command[SERVICE_EXEC_START]->command_next) {
1064                 log_error("%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", s->meta.id);
1065                 return -EINVAL;
1066         }
1067
1068         if (s->type == SERVICE_ONESHOT &&
1069             s->exec_command[SERVICE_EXEC_RELOAD]) {
1070                 log_error("%s has an ExecReload setting, which is not allowed for Type=oneshot services. Refusing.", s->meta.id);
1071                 return -EINVAL;
1072         }
1073
1074         if (s->type == SERVICE_DBUS && !s->bus_name) {
1075                 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", s->meta.id);
1076                 return -EINVAL;
1077         }
1078
1079         if (s->exec_context.pam_name && s->exec_context.kill_mode != KILL_CONTROL_GROUP) {
1080                 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s->meta.id);
1081                 return -EINVAL;
1082         }
1083
1084         return 0;
1085 }
1086
1087 static int service_add_default_dependencies(Service *s) {
1088         int r;
1089
1090         assert(s);
1091
1092         /* Add a number of automatic dependencies useful for the
1093          * majority of services. */
1094
1095         /* First, pull in base system */
1096         if (s->meta.manager->running_as == MANAGER_SYSTEM) {
1097
1098                 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
1099                         return r;
1100
1101         } else if (s->meta.manager->running_as == MANAGER_USER) {
1102
1103                 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0)
1104                         return r;
1105         }
1106
1107         /* Second, activate normal shutdown */
1108         return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
1109 }
1110
1111 static void service_fix_output(Service *s) {
1112         assert(s);
1113
1114         /* If nothing has been explicitly configured, patch default
1115          * output in. If input is socket/tty we avoid this however,
1116          * since in that case we want output to default to the same
1117          * place as we read input from. */
1118
1119         if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
1120             s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1121             s->exec_context.std_input == EXEC_INPUT_NULL)
1122                 s->exec_context.std_error = s->meta.manager->default_std_error;
1123
1124         if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1125             s->exec_context.std_input == EXEC_INPUT_NULL)
1126                 s->exec_context.std_output = s->meta.manager->default_std_output;
1127 }
1128
1129 static int service_load(Unit *u) {
1130         int r;
1131         Service *s = SERVICE(u);
1132
1133         assert(s);
1134
1135         /* Load a .service file */
1136         if ((r = unit_load_fragment(u)) < 0)
1137                 return r;
1138
1139 #ifdef HAVE_SYSV_COMPAT
1140         /* Load a classic init script as a fallback, if we couldn't find anything */
1141         if (u->meta.load_state == UNIT_STUB)
1142                 if ((r = service_load_sysv(s)) < 0)
1143                         return r;
1144 #endif
1145
1146         /* Still nothing found? Then let's give up */
1147         if (u->meta.load_state == UNIT_STUB)
1148                 return -ENOENT;
1149
1150         /* We were able to load something, then let's add in the
1151          * dropin directories. */
1152         if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
1153                 return r;
1154
1155         /* This is a new unit? Then let's add in some extras */
1156         if (u->meta.load_state == UNIT_LOADED) {
1157                 service_fix_output(s);
1158
1159                 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
1160                         return r;
1161
1162                 if ((r = unit_add_default_cgroups(u)) < 0)
1163                         return r;
1164
1165 #ifdef HAVE_SYSV_COMPAT
1166                 if ((r = sysv_fix_order(s)) < 0)
1167                         return r;
1168 #endif
1169
1170                 if ((r = fsck_fix_order(s)) < 0)
1171                         return r;
1172
1173                 if (s->bus_name)
1174                         if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
1175                                 return r;
1176
1177                 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1178                         s->notify_access = NOTIFY_MAIN;
1179
1180                 if (s->type == SERVICE_DBUS || s->bus_name)
1181                         if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_SOCKET, NULL, true)) < 0)
1182                                 return r;
1183
1184                 if (s->meta.default_dependencies)
1185                         if ((r = service_add_default_dependencies(s)) < 0)
1186                                 return r;
1187         }
1188
1189         return service_verify(s);
1190 }
1191
1192 static void service_dump(Unit *u, FILE *f, const char *prefix) {
1193
1194         ServiceExecCommand c;
1195         Service *s = SERVICE(u);
1196         const char *prefix2;
1197         char *p2;
1198
1199         assert(s);
1200
1201         p2 = strappend(prefix, "\t");
1202         prefix2 = p2 ? p2 : prefix;
1203
1204         fprintf(f,
1205                 "%sService State: %s\n"
1206                 "%sPermissionsStartOnly: %s\n"
1207                 "%sRootDirectoryStartOnly: %s\n"
1208                 "%sRemainAfterExit: %s\n"
1209                 "%sGuessMainPID: %s\n"
1210                 "%sType: %s\n"
1211                 "%sRestart: %s\n"
1212                 "%sNotifyAccess: %s\n",
1213                 prefix, service_state_to_string(s->state),
1214                 prefix, yes_no(s->permissions_start_only),
1215                 prefix, yes_no(s->root_directory_start_only),
1216                 prefix, yes_no(s->remain_after_exit),
1217                 prefix, yes_no(s->guess_main_pid),
1218                 prefix, service_type_to_string(s->type),
1219                 prefix, service_restart_to_string(s->restart),
1220                 prefix, notify_access_to_string(s->notify_access));
1221
1222         if (s->control_pid > 0)
1223                 fprintf(f,
1224                         "%sControl PID: %lu\n",
1225                         prefix, (unsigned long) s->control_pid);
1226
1227         if (s->main_pid > 0)
1228                 fprintf(f,
1229                         "%sMain PID: %lu\n"
1230                         "%sMain PID Known: %s\n"
1231                         "%sMain PID Alien: %s\n",
1232                         prefix, (unsigned long) s->main_pid,
1233                         prefix, yes_no(s->main_pid_known),
1234                         prefix, yes_no(s->main_pid_alien));
1235
1236         if (s->pid_file)
1237                 fprintf(f,
1238                         "%sPIDFile: %s\n",
1239                         prefix, s->pid_file);
1240
1241         if (s->bus_name)
1242                 fprintf(f,
1243                         "%sBusName: %s\n"
1244                         "%sBus Name Good: %s\n",
1245                         prefix, s->bus_name,
1246                         prefix, yes_no(s->bus_name_good));
1247
1248         exec_context_dump(&s->exec_context, f, prefix);
1249
1250         for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
1251
1252                 if (!s->exec_command[c])
1253                         continue;
1254
1255                 fprintf(f, "%s-> %s:\n",
1256                         prefix, service_exec_command_to_string(c));
1257
1258                 exec_command_dump_list(s->exec_command[c], f, prefix2);
1259         }
1260
1261 #ifdef HAVE_SYSV_COMPAT
1262         if (s->sysv_path)
1263                 fprintf(f,
1264                         "%sSysV Init Script Path: %s\n"
1265                         "%sSysV Init Script has LSB Header: %s\n"
1266                         "%sSysVEnabled: %s\n",
1267                         prefix, s->sysv_path,
1268                         prefix, yes_no(s->sysv_has_lsb),
1269                         prefix, yes_no(s->sysv_enabled));
1270
1271         if (s->sysv_start_priority >= 0)
1272                 fprintf(f,
1273                         "%sSysVStartPriority: %i\n",
1274                         prefix, s->sysv_start_priority);
1275
1276         if (s->sysv_runlevels)
1277                 fprintf(f, "%sSysVRunLevels: %s\n",
1278                         prefix, s->sysv_runlevels);
1279 #endif
1280
1281         if (s->fsck_passno > 0)
1282                 fprintf(f,
1283                         "%sFsckPassNo: %i\n",
1284                         prefix, s->fsck_passno);
1285
1286         if (s->status_text)
1287                 fprintf(f, "%sStatus Text: %s\n",
1288                         prefix, s->status_text);
1289
1290         free(p2);
1291 }
1292
1293 static int service_load_pid_file(Service *s, bool may_warn) {
1294         char *k;
1295         int r;
1296         pid_t pid;
1297
1298         assert(s);
1299
1300         if (!s->pid_file)
1301                 return -ENOENT;
1302
1303         if ((r = read_one_line_file(s->pid_file, &k)) < 0) {
1304                 if (may_warn)
1305                         log_info("PID file %s not readable (yet?) after %s.",
1306                                  s->pid_file, service_state_to_string(s->state));
1307                 return r;
1308         }
1309
1310         r = parse_pid(k, &pid);
1311         free(k);
1312
1313         if (r < 0)
1314                 return r;
1315
1316         if (kill(pid, 0) < 0 && errno != EPERM) {
1317                 if (may_warn)
1318                         log_info("PID %lu read from file %s does not exist.",
1319                                  (unsigned long) pid, s->pid_file);
1320                 return -ESRCH;
1321         }
1322
1323         if (s->main_pid_known) {
1324                 if (pid == s->main_pid)
1325                         return 0;
1326
1327                 log_debug("Main PID changing: %lu -> %lu",
1328                           (unsigned long) s->main_pid, (unsigned long) pid);
1329                 service_unwatch_main_pid(s);
1330                 s->main_pid_known = false;
1331         } else
1332                 log_debug("Main PID loaded: %lu", (unsigned long) pid);
1333
1334         if ((r = service_set_main_pid(s, pid)) < 0)
1335                 return r;
1336
1337         if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1338                 /* FIXME: we need to do something here */
1339                 return r;
1340
1341         return 0;
1342 }
1343
1344 static int service_search_main_pid(Service *s) {
1345         pid_t pid;
1346         int r;
1347
1348         assert(s);
1349
1350         /* If we know it anyway, don't ever fallback to unreliable
1351          * heuristics */
1352         if (s->main_pid_known)
1353                 return 0;
1354
1355         if (!s->guess_main_pid)
1356                 return 0;
1357
1358         assert(s->main_pid <= 0);
1359
1360         if ((pid = cgroup_bonding_search_main_pid_list(s->meta.cgroup_bondings)) <= 0)
1361                 return -ENOENT;
1362
1363         log_debug("Main PID guessed: %lu", (unsigned long) pid);
1364         if ((r = service_set_main_pid(s, pid)) < 0)
1365                 return r;
1366
1367         if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1368                 /* FIXME: we need to do something here */
1369                 return r;
1370
1371         return 0;
1372 }
1373
1374 static void service_notify_sockets_dead(Service *s) {
1375         Iterator i;
1376         Unit *u;
1377
1378         assert(s);
1379
1380         /* Notifies all our sockets when we die */
1381
1382         if (s->socket_fd >= 0)
1383                 return;
1384
1385         SET_FOREACH(u, s->meta.dependencies[UNIT_TRIGGERED_BY], i)
1386                 if (u->meta.type == UNIT_SOCKET)
1387                         socket_notify_service_dead(SOCKET(u));
1388
1389         return;
1390 }
1391
1392 static void service_unwatch_pid_file(Service *s) {
1393         if (!s->pid_file_pathspec)
1394                 return;
1395
1396         log_debug("Stopping watch for %s's PID file %s", s->meta.id, s->pid_file_pathspec->path);
1397         path_spec_unwatch(s->pid_file_pathspec, UNIT(s));
1398         path_spec_done(s->pid_file_pathspec);
1399         free(s->pid_file_pathspec);
1400         s->pid_file_pathspec = NULL;
1401 }
1402
1403 static void service_set_state(Service *s, ServiceState state) {
1404         ServiceState old_state;
1405         assert(s);
1406
1407         old_state = s->state;
1408         s->state = state;
1409
1410         service_unwatch_pid_file(s);
1411
1412         if (state != SERVICE_START_PRE &&
1413             state != SERVICE_START &&
1414             state != SERVICE_START_POST &&
1415             state != SERVICE_RELOAD &&
1416             state != SERVICE_STOP &&
1417             state != SERVICE_STOP_SIGTERM &&
1418             state != SERVICE_STOP_SIGKILL &&
1419             state != SERVICE_STOP_POST &&
1420             state != SERVICE_FINAL_SIGTERM &&
1421             state != SERVICE_FINAL_SIGKILL &&
1422             state != SERVICE_AUTO_RESTART)
1423                 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1424
1425         if (state != SERVICE_START &&
1426             state != SERVICE_START_POST &&
1427             state != SERVICE_RUNNING &&
1428             state != SERVICE_RELOAD &&
1429             state != SERVICE_STOP &&
1430             state != SERVICE_STOP_SIGTERM &&
1431             state != SERVICE_STOP_SIGKILL) {
1432                 service_unwatch_main_pid(s);
1433                 s->main_command = NULL;
1434         }
1435
1436         if (state != SERVICE_START_PRE &&
1437             state != SERVICE_START &&
1438             state != SERVICE_START_POST &&
1439             state != SERVICE_RELOAD &&
1440             state != SERVICE_STOP &&
1441             state != SERVICE_STOP_SIGTERM &&
1442             state != SERVICE_STOP_SIGKILL &&
1443             state != SERVICE_STOP_POST &&
1444             state != SERVICE_FINAL_SIGTERM &&
1445             state != SERVICE_FINAL_SIGKILL) {
1446                 service_unwatch_control_pid(s);
1447                 s->control_command = NULL;
1448                 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1449         }
1450
1451         if (state == SERVICE_DEAD ||
1452             state == SERVICE_STOP ||
1453             state == SERVICE_STOP_SIGTERM ||
1454             state == SERVICE_STOP_SIGKILL ||
1455             state == SERVICE_STOP_POST ||
1456             state == SERVICE_FINAL_SIGTERM ||
1457             state == SERVICE_FINAL_SIGKILL ||
1458             state == SERVICE_FAILED ||
1459             state == SERVICE_AUTO_RESTART)
1460                 service_notify_sockets_dead(s);
1461
1462         if (state != SERVICE_START_PRE &&
1463             state != SERVICE_START &&
1464             state != SERVICE_START_POST &&
1465             state != SERVICE_RUNNING &&
1466             state != SERVICE_RELOAD &&
1467             state != SERVICE_STOP &&
1468             state != SERVICE_STOP_SIGTERM &&
1469             state != SERVICE_STOP_SIGKILL &&
1470             state != SERVICE_STOP_POST &&
1471             state != SERVICE_FINAL_SIGTERM &&
1472             state != SERVICE_FINAL_SIGKILL &&
1473             !(state == SERVICE_DEAD && s->meta.job)) {
1474                 service_close_socket_fd(s);
1475                 service_connection_unref(s);
1476         }
1477
1478         /* For the inactive states unit_notify() will trim the cgroup,
1479          * but for exit we have to do that ourselves... */
1480         if (state == SERVICE_EXITED && s->meta.manager->n_reloading <= 0)
1481                 cgroup_bonding_trim_list(s->meta.cgroup_bondings, true);
1482
1483         if (old_state != state)
1484                 log_debug("%s changed %s -> %s", s->meta.id, service_state_to_string(old_state), service_state_to_string(state));
1485
1486         unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], !s->reload_failure);
1487         s->reload_failure = false;
1488 }
1489
1490 static int service_coldplug(Unit *u) {
1491         Service *s = SERVICE(u);
1492         int r;
1493
1494         assert(s);
1495         assert(s->state == SERVICE_DEAD);
1496
1497         if (s->deserialized_state != s->state) {
1498
1499                 if (s->deserialized_state == SERVICE_START_PRE ||
1500                     s->deserialized_state == SERVICE_START ||
1501                     s->deserialized_state == SERVICE_START_POST ||
1502                     s->deserialized_state == SERVICE_RELOAD ||
1503                     s->deserialized_state == SERVICE_STOP ||
1504                     s->deserialized_state == SERVICE_STOP_SIGTERM ||
1505                     s->deserialized_state == SERVICE_STOP_SIGKILL ||
1506                     s->deserialized_state == SERVICE_STOP_POST ||
1507                     s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1508                     s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1509                     s->deserialized_state == SERVICE_AUTO_RESTART) {
1510
1511                         if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1512                                 usec_t k;
1513
1514                                 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1515
1516                                 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1517                                         return r;
1518                         }
1519                 }
1520
1521                 if ((s->deserialized_state == SERVICE_START &&
1522                      (s->type == SERVICE_FORKING ||
1523                       s->type == SERVICE_DBUS ||
1524                       s->type == SERVICE_ONESHOT ||
1525                       s->type == SERVICE_NOTIFY)) ||
1526                     s->deserialized_state == SERVICE_START_POST ||
1527                     s->deserialized_state == SERVICE_RUNNING ||
1528                     s->deserialized_state == SERVICE_RELOAD ||
1529                     s->deserialized_state == SERVICE_STOP ||
1530                     s->deserialized_state == SERVICE_STOP_SIGTERM ||
1531                     s->deserialized_state == SERVICE_STOP_SIGKILL)
1532                         if (s->main_pid > 0)
1533                                 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1534                                         return r;
1535
1536                 if (s->deserialized_state == SERVICE_START_PRE ||
1537                     s->deserialized_state == SERVICE_START ||
1538                     s->deserialized_state == SERVICE_START_POST ||
1539                     s->deserialized_state == SERVICE_RELOAD ||
1540                     s->deserialized_state == SERVICE_STOP ||
1541                     s->deserialized_state == SERVICE_STOP_SIGTERM ||
1542                     s->deserialized_state == SERVICE_STOP_SIGKILL ||
1543                     s->deserialized_state == SERVICE_STOP_POST ||
1544                     s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1545                     s->deserialized_state == SERVICE_FINAL_SIGKILL)
1546                         if (s->control_pid > 0)
1547                                 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1548                                         return r;
1549
1550                 service_set_state(s, s->deserialized_state);
1551         }
1552
1553         return 0;
1554 }
1555
1556 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1557         Iterator i;
1558         int r;
1559         int *rfds = NULL;
1560         unsigned rn_fds = 0;
1561         Unit *u;
1562
1563         assert(s);
1564         assert(fds);
1565         assert(n_fds);
1566
1567         if (s->socket_fd >= 0)
1568                 return 0;
1569
1570         SET_FOREACH(u, s->meta.dependencies[UNIT_TRIGGERED_BY], i) {
1571                 int *cfds;
1572                 unsigned cn_fds;
1573                 Socket *sock;
1574
1575                 if (u->meta.type != UNIT_SOCKET)
1576                         continue;
1577
1578                 sock = SOCKET(u);
1579
1580                 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1581                         goto fail;
1582
1583                 if (!cfds)
1584                         continue;
1585
1586                 if (!rfds) {
1587                         rfds = cfds;
1588                         rn_fds = cn_fds;
1589                 } else {
1590                         int *t;
1591
1592                         if (!(t = new(int, rn_fds+cn_fds))) {
1593                                 free(cfds);
1594                                 r = -ENOMEM;
1595                                 goto fail;
1596                         }
1597
1598                         memcpy(t, rfds, rn_fds * sizeof(int));
1599                         memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
1600                         free(rfds);
1601                         free(cfds);
1602
1603                         rfds = t;
1604                         rn_fds = rn_fds+cn_fds;
1605                 }
1606         }
1607
1608         *fds = rfds;
1609         *n_fds = rn_fds;
1610
1611         return 0;
1612
1613 fail:
1614         free(rfds);
1615
1616         return r;
1617 }
1618
1619 static int service_spawn(
1620                 Service *s,
1621                 ExecCommand *c,
1622                 bool timeout,
1623                 bool pass_fds,
1624                 bool apply_permissions,
1625                 bool apply_chroot,
1626                 bool apply_tty_stdin,
1627                 bool set_notify_socket,
1628                 pid_t *_pid) {
1629
1630         pid_t pid;
1631         int r;
1632         int *fds = NULL, *fdsbuf = NULL;
1633         unsigned n_fds = 0, n_env = 0;
1634         char **argv = NULL, **final_env = NULL, **our_env = NULL;
1635
1636         assert(s);
1637         assert(c);
1638         assert(_pid);
1639
1640         if (pass_fds ||
1641             s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1642             s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1643             s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1644
1645                 if (s->socket_fd >= 0) {
1646                         fds = &s->socket_fd;
1647                         n_fds = 1;
1648                 } else {
1649                         if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0)
1650                                 goto fail;
1651
1652                         fds = fdsbuf;
1653                 }
1654         }
1655
1656         if (timeout && s->timeout_usec) {
1657                 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1658                         goto fail;
1659         } else
1660                 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1661
1662         if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1663                 r = -ENOMEM;
1664                 goto fail;
1665         }
1666
1667         if (!(our_env = new0(char*, 4))) {
1668                 r = -ENOMEM;
1669                 goto fail;
1670         }
1671
1672         if (set_notify_socket)
1673                 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", s->meta.manager->notify_socket) < 0) {
1674                         r = -ENOMEM;
1675                         goto fail;
1676                 }
1677
1678         if (s->main_pid > 0)
1679                 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1680                         r = -ENOMEM;
1681                         goto fail;
1682                 }
1683
1684         if (!(final_env = strv_env_merge(2,
1685                                          s->meta.manager->environment,
1686                                          our_env,
1687                                          NULL))) {
1688                 r = -ENOMEM;
1689                 goto fail;
1690         }
1691
1692         r = exec_spawn(c,
1693                        argv,
1694                        &s->exec_context,
1695                        fds, n_fds,
1696                        final_env,
1697                        apply_permissions,
1698                        apply_chroot,
1699                        apply_tty_stdin,
1700                        s->meta.manager->confirm_spawn,
1701                        s->meta.cgroup_bondings,
1702                        s->meta.cgroup_attributes,
1703                        &pid);
1704
1705         if (r < 0)
1706                 goto fail;
1707
1708
1709         if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1710                 /* FIXME: we need to do something here */
1711                 goto fail;
1712
1713         free(fdsbuf);
1714         strv_free(argv);
1715         strv_free(our_env);
1716         strv_free(final_env);
1717
1718         *_pid = pid;
1719
1720         return 0;
1721
1722 fail:
1723         free(fdsbuf);
1724         strv_free(argv);
1725         strv_free(our_env);
1726         strv_free(final_env);
1727
1728         if (timeout)
1729                 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1730
1731         return r;
1732 }
1733
1734 static int main_pid_good(Service *s) {
1735         assert(s);
1736
1737         /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1738          * don't know */
1739
1740         /* If we know the pid file, then lets just check if it is
1741          * still valid */
1742         if (s->main_pid_known) {
1743
1744                 /* If it's an alien child let's check if it is still
1745                  * alive ... */
1746                 if (s->main_pid_alien)
1747                         return kill(s->main_pid, 0) >= 0 || errno != ESRCH;
1748
1749                 /* .. otherwise assume we'll get a SIGCHLD for it,
1750                  * which we really should wait for to collect exit
1751                  * status and code */
1752                 return s->main_pid > 0;
1753         }
1754
1755         /* We don't know the pid */
1756         return -EAGAIN;
1757 }
1758
1759 static int control_pid_good(Service *s) {
1760         assert(s);
1761
1762         return s->control_pid > 0;
1763 }
1764
1765 static int cgroup_good(Service *s) {
1766         int r;
1767
1768         assert(s);
1769
1770         if ((r = cgroup_bonding_is_empty_list(s->meta.cgroup_bondings)) < 0)
1771                 return r;
1772
1773         return !r;
1774 }
1775
1776 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1777         int r;
1778         assert(s);
1779
1780         if (!success)
1781                 s->failure = true;
1782
1783         if (allow_restart &&
1784             !s->forbid_restart &&
1785             (s->restart == SERVICE_RESTART_ALWAYS ||
1786              (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure) ||
1787              (s->restart == SERVICE_RESTART_ON_FAILURE && s->failure) ||
1788              (s->restart == SERVICE_RESTART_ON_ABORT && s->failure &&
1789               (s->main_exec_status.code == CLD_KILLED ||
1790                s->main_exec_status.code == CLD_DUMPED)))) {
1791
1792                 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1793                         goto fail;
1794
1795                 service_set_state(s, SERVICE_AUTO_RESTART);
1796         } else
1797                 service_set_state(s, s->failure ? SERVICE_FAILED : SERVICE_DEAD);
1798
1799         s->forbid_restart = false;
1800
1801         return;
1802
1803 fail:
1804         log_warning("%s failed to run install restart timer: %s", s->meta.id, strerror(-r));
1805         service_enter_dead(s, false, false);
1806 }
1807
1808 static void service_enter_signal(Service *s, ServiceState state, bool success);
1809
1810 static void service_enter_stop_post(Service *s, bool success) {
1811         int r;
1812         assert(s);
1813
1814         if (!success)
1815                 s->failure = true;
1816
1817         service_unwatch_control_pid(s);
1818
1819         if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1820                 s->control_command_id = SERVICE_EXEC_STOP_POST;
1821
1822                 if ((r = service_spawn(s,
1823                                        s->control_command,
1824                                        true,
1825                                        false,
1826                                        !s->permissions_start_only,
1827                                        !s->root_directory_start_only,
1828                                        true,
1829                                        false,
1830                                        &s->control_pid)) < 0)
1831                         goto fail;
1832
1833
1834                 service_set_state(s, SERVICE_STOP_POST);
1835         } else
1836                 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
1837
1838         return;
1839
1840 fail:
1841         log_warning("%s failed to run 'stop-post' task: %s", s->meta.id, strerror(-r));
1842         service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1843 }
1844
1845 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1846         int r;
1847         Set *pid_set = NULL;
1848         bool wait_for_exit = false;
1849
1850         assert(s);
1851
1852         if (!success)
1853                 s->failure = true;
1854
1855         if (s->exec_context.kill_mode != KILL_NONE) {
1856                 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? s->exec_context.kill_signal : SIGKILL;
1857
1858                 if (s->main_pid > 0) {
1859                         if (kill_and_sigcont(s->main_pid, sig) < 0 && errno != ESRCH)
1860                                 log_warning("Failed to kill main process %li: %m", (long) s->main_pid);
1861                         else
1862                                 wait_for_exit = !s->main_pid_alien;
1863                 }
1864
1865                 if (s->control_pid > 0) {
1866                         if (kill_and_sigcont(s->control_pid, sig) < 0 && errno != ESRCH)
1867                                 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
1868                         else
1869                                 wait_for_exit = true;
1870                 }
1871
1872                 if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) {
1873
1874                         if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) {
1875                                 r = -ENOMEM;
1876                                 goto fail;
1877                         }
1878
1879                         /* Exclude the main/control pids from being killed via the cgroup */
1880                         if (s->main_pid > 0)
1881                                 if ((r = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0)
1882                                         goto fail;
1883
1884                         if (s->control_pid > 0)
1885                                 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
1886                                         goto fail;
1887
1888                         if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig, true, pid_set)) < 0) {
1889                                 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
1890                                         log_warning("Failed to kill control group: %s", strerror(-r));
1891                         } else if (r > 0)
1892                                 wait_for_exit = true;
1893
1894                         set_free(pid_set);
1895                         pid_set = NULL;
1896                 }
1897         }
1898
1899         if (wait_for_exit) {
1900                 if (s->timeout_usec > 0)
1901                         if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1902                                 goto fail;
1903
1904                 service_set_state(s, state);
1905         } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1906                 service_enter_stop_post(s, true);
1907         else
1908                 service_enter_dead(s, true, true);
1909
1910         return;
1911
1912 fail:
1913         log_warning("%s failed to kill processes: %s", s->meta.id, strerror(-r));
1914
1915         if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1916                 service_enter_stop_post(s, false);
1917         else
1918                 service_enter_dead(s, false, true);
1919
1920         if (pid_set)
1921                 set_free(pid_set);
1922 }
1923
1924 static void service_enter_stop(Service *s, bool success) {
1925         int r;
1926
1927         assert(s);
1928
1929         if (!success)
1930                 s->failure = true;
1931
1932         service_unwatch_control_pid(s);
1933
1934         if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
1935                 s->control_command_id = SERVICE_EXEC_STOP;
1936
1937                 if ((r = service_spawn(s,
1938                                        s->control_command,
1939                                        true,
1940                                        false,
1941                                        !s->permissions_start_only,
1942                                        !s->root_directory_start_only,
1943                                        false,
1944                                        false,
1945                                        &s->control_pid)) < 0)
1946                         goto fail;
1947
1948                 service_set_state(s, SERVICE_STOP);
1949         } else
1950                 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1951
1952         return;
1953
1954 fail:
1955         log_warning("%s failed to run 'stop' task: %s", s->meta.id, strerror(-r));
1956         service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1957 }
1958
1959 static void service_enter_running(Service *s, bool success) {
1960         int main_pid_ok, cgroup_ok;
1961         assert(s);
1962
1963         if (!success)
1964                 s->failure = true;
1965
1966         main_pid_ok = main_pid_good(s);
1967         cgroup_ok = cgroup_good(s);
1968
1969         if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
1970             (s->bus_name_good || s->type != SERVICE_DBUS))
1971                 service_set_state(s, SERVICE_RUNNING);
1972         else if (s->remain_after_exit)
1973                 service_set_state(s, SERVICE_EXITED);
1974         else
1975                 service_enter_stop(s, true);
1976 }
1977
1978 static void service_enter_start_post(Service *s) {
1979         int r;
1980         assert(s);
1981
1982         service_unwatch_control_pid(s);
1983
1984         if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
1985                 s->control_command_id = SERVICE_EXEC_START_POST;
1986
1987                 if ((r = service_spawn(s,
1988                                        s->control_command,
1989                                        true,
1990                                        false,
1991                                        !s->permissions_start_only,
1992                                        !s->root_directory_start_only,
1993                                        false,
1994                                        false,
1995                                        &s->control_pid)) < 0)
1996                         goto fail;
1997
1998                 service_set_state(s, SERVICE_START_POST);
1999         } else
2000                 service_enter_running(s, true);
2001
2002         return;
2003
2004 fail:
2005         log_warning("%s failed to run 'start-post' task: %s", s->meta.id, strerror(-r));
2006         service_enter_stop(s, false);
2007 }
2008
2009 static void service_enter_start(Service *s) {
2010         pid_t pid;
2011         int r;
2012         ExecCommand *c;
2013
2014         assert(s);
2015
2016         assert(s->exec_command[SERVICE_EXEC_START]);
2017         assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
2018
2019         if (s->type == SERVICE_FORKING)
2020                 service_unwatch_control_pid(s);
2021         else
2022                 service_unwatch_main_pid(s);
2023
2024         if (s->type == SERVICE_FORKING) {
2025                 s->control_command_id = SERVICE_EXEC_START;
2026                 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
2027
2028                 s->main_command = NULL;
2029         } else {
2030                 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2031                 s->control_command = NULL;
2032
2033                 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
2034         }
2035
2036         if ((r = service_spawn(s,
2037                                c,
2038                                s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY,
2039                                true,
2040                                true,
2041                                true,
2042                                true,
2043                                s->notify_access != NOTIFY_NONE,
2044                                &pid)) < 0)
2045                 goto fail;
2046
2047         if (s->type == SERVICE_SIMPLE) {
2048                 /* For simple services we immediately start
2049                  * the START_POST binaries. */
2050
2051                 service_set_main_pid(s, pid);
2052                 service_enter_start_post(s);
2053
2054         } else  if (s->type == SERVICE_FORKING) {
2055
2056                 /* For forking services we wait until the start
2057                  * process exited. */
2058
2059                 s->control_pid = pid;
2060                 service_set_state(s, SERVICE_START);
2061
2062         } else if (s->type == SERVICE_ONESHOT ||
2063                    s->type == SERVICE_DBUS ||
2064                    s->type == SERVICE_NOTIFY) {
2065
2066                 /* For oneshot services we wait until the start
2067                  * process exited, too, but it is our main process. */
2068
2069                 /* For D-Bus services we know the main pid right away,
2070                  * but wait for the bus name to appear on the
2071                  * bus. Notify services are similar. */
2072
2073                 service_set_main_pid(s, pid);
2074                 service_set_state(s, SERVICE_START);
2075         } else
2076                 assert_not_reached("Unknown service type");
2077
2078         return;
2079
2080 fail:
2081         log_warning("%s failed to run 'start' task: %s", s->meta.id, strerror(-r));
2082         service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2083 }
2084
2085 static void service_enter_start_pre(Service *s) {
2086         int r;
2087
2088         assert(s);
2089
2090         service_unwatch_control_pid(s);
2091
2092         if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
2093                 s->control_command_id = SERVICE_EXEC_START_PRE;
2094
2095                 if ((r = service_spawn(s,
2096                                        s->control_command,
2097                                        true,
2098                                        false,
2099                                        !s->permissions_start_only,
2100                                        !s->root_directory_start_only,
2101                                        true,
2102                                        false,
2103                                        &s->control_pid)) < 0)
2104                         goto fail;
2105
2106                 service_set_state(s, SERVICE_START_PRE);
2107         } else
2108                 service_enter_start(s);
2109
2110         return;
2111
2112 fail:
2113         log_warning("%s failed to run 'start-pre' task: %s", s->meta.id, strerror(-r));
2114         service_enter_dead(s, false, true);
2115 }
2116
2117 static void service_enter_restart(Service *s) {
2118         int r;
2119         DBusError error;
2120
2121         assert(s);
2122         dbus_error_init(&error);
2123
2124         if (s->meta.job) {
2125                 log_info("Job pending for unit, delaying automatic restart.");
2126
2127                 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
2128                         goto fail;
2129         }
2130
2131         service_enter_dead(s, true, false);
2132
2133         if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, &error, NULL)) < 0)
2134                 goto fail;
2135
2136         log_debug("%s scheduled restart job.", s->meta.id);
2137         return;
2138
2139 fail:
2140         log_warning("%s failed to schedule restart job: %s", s->meta.id, bus_error(&error, -r));
2141         service_enter_dead(s, false, false);
2142
2143         dbus_error_free(&error);
2144 }
2145
2146 static void service_enter_reload(Service *s) {
2147         int r;
2148
2149         assert(s);
2150
2151         service_unwatch_control_pid(s);
2152
2153         if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
2154                 s->control_command_id = SERVICE_EXEC_RELOAD;
2155
2156                 if ((r = service_spawn(s,
2157                                        s->control_command,
2158                                        true,
2159                                        false,
2160                                        !s->permissions_start_only,
2161                                        !s->root_directory_start_only,
2162                                        false,
2163                                        false,
2164                                        &s->control_pid)) < 0)
2165                         goto fail;
2166
2167                 service_set_state(s, SERVICE_RELOAD);
2168         } else
2169                 service_enter_running(s, true);
2170
2171         return;
2172
2173 fail:
2174         log_warning("%s failed to run 'reload' task: %s", s->meta.id, strerror(-r));
2175         s->reload_failure = true;
2176         service_enter_running(s, true);
2177 }
2178
2179 static void service_run_next_control(Service *s, bool success) {
2180         int r;
2181
2182         assert(s);
2183         assert(s->control_command);
2184         assert(s->control_command->command_next);
2185
2186         if (!success)
2187                 s->failure = true;
2188
2189         assert(s->control_command_id != SERVICE_EXEC_START);
2190
2191         s->control_command = s->control_command->command_next;
2192         service_unwatch_control_pid(s);
2193
2194         if ((r = service_spawn(s,
2195                                s->control_command,
2196                                true,
2197                                false,
2198                                !s->permissions_start_only,
2199                                !s->root_directory_start_only,
2200                                s->control_command_id == SERVICE_EXEC_START_PRE ||
2201                                s->control_command_id == SERVICE_EXEC_STOP_POST,
2202                                false,
2203                                &s->control_pid)) < 0)
2204                 goto fail;
2205
2206         return;
2207
2208 fail:
2209         log_warning("%s failed to run next control task: %s", s->meta.id, strerror(-r));
2210
2211         if (s->state == SERVICE_START_PRE)
2212                 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2213         else if (s->state == SERVICE_STOP)
2214                 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2215         else if (s->state == SERVICE_STOP_POST)
2216                 service_enter_dead(s, false, true);
2217         else if (s->state == SERVICE_RELOAD) {
2218                 s->reload_failure = true;
2219                 service_enter_running(s, true);
2220         } else
2221                 service_enter_stop(s, false);
2222 }
2223
2224 static void service_run_next_main(Service *s, bool success) {
2225         pid_t pid;
2226         int r;
2227
2228         assert(s);
2229         assert(s->main_command);
2230         assert(s->main_command->command_next);
2231         assert(s->type == SERVICE_ONESHOT);
2232
2233         if (!success)
2234                 s->failure = true;
2235
2236         s->main_command = s->main_command->command_next;
2237         service_unwatch_main_pid(s);
2238
2239         if ((r = service_spawn(s,
2240                                s->main_command,
2241                                false,
2242                                true,
2243                                true,
2244                                true,
2245                                true,
2246                                s->notify_access != NOTIFY_NONE,
2247                                &pid)) < 0)
2248                 goto fail;
2249
2250         service_set_main_pid(s, pid);
2251
2252         return;
2253
2254 fail:
2255         log_warning("%s failed to run next main task: %s", s->meta.id, strerror(-r));
2256         service_enter_stop(s, false);
2257 }
2258
2259 static int service_start(Unit *u) {
2260         Service *s = SERVICE(u);
2261
2262         assert(s);
2263
2264         /* We cannot fulfill this request right now, try again later
2265          * please! */
2266         if (s->state == SERVICE_STOP ||
2267             s->state == SERVICE_STOP_SIGTERM ||
2268             s->state == SERVICE_STOP_SIGKILL ||
2269             s->state == SERVICE_STOP_POST ||
2270             s->state == SERVICE_FINAL_SIGTERM ||
2271             s->state == SERVICE_FINAL_SIGKILL)
2272                 return -EAGAIN;
2273
2274         /* Already on it! */
2275         if (s->state == SERVICE_START_PRE ||
2276             s->state == SERVICE_START ||
2277             s->state == SERVICE_START_POST)
2278                 return 0;
2279
2280         assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED || s->state == SERVICE_AUTO_RESTART);
2281
2282         /* Make sure we don't enter a busy loop of some kind. */
2283         if (!ratelimit_test(&s->ratelimit)) {
2284                 log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id);
2285                 return -ECANCELED;
2286         }
2287
2288         s->failure = false;
2289         s->main_pid_known = false;
2290         s->main_pid_alien = false;
2291         s->forbid_restart = false;
2292
2293         service_enter_start_pre(s);
2294         return 0;
2295 }
2296
2297 static int service_stop(Unit *u) {
2298         Service *s = SERVICE(u);
2299
2300         assert(s);
2301
2302         /* This is a user request, so don't do restarts on this
2303          * shutdown. */
2304         s->forbid_restart = true;
2305
2306         /* Already on it */
2307         if (s->state == SERVICE_STOP ||
2308             s->state == SERVICE_STOP_SIGTERM ||
2309             s->state == SERVICE_STOP_SIGKILL ||
2310             s->state == SERVICE_STOP_POST ||
2311             s->state == SERVICE_FINAL_SIGTERM ||
2312             s->state == SERVICE_FINAL_SIGKILL)
2313                 return 0;
2314
2315         /* Don't allow a restart */
2316         if (s->state == SERVICE_AUTO_RESTART) {
2317                 service_set_state(s, SERVICE_DEAD);
2318                 return 0;
2319         }
2320
2321         /* If there's already something running we go directly into
2322          * kill mode. */
2323         if (s->state == SERVICE_START_PRE ||
2324             s->state == SERVICE_START ||
2325             s->state == SERVICE_START_POST ||
2326             s->state == SERVICE_RELOAD) {
2327                 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
2328                 return 0;
2329         }
2330
2331         assert(s->state == SERVICE_RUNNING ||
2332                s->state == SERVICE_EXITED);
2333
2334         service_enter_stop(s, true);
2335         return 0;
2336 }
2337
2338 static int service_reload(Unit *u) {
2339         Service *s = SERVICE(u);
2340
2341         assert(s);
2342
2343         assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2344
2345         service_enter_reload(s);
2346         return 0;
2347 }
2348
2349 static bool service_can_reload(Unit *u) {
2350         Service *s = SERVICE(u);
2351
2352         assert(s);
2353
2354         return !!s->exec_command[SERVICE_EXEC_RELOAD];
2355 }
2356
2357 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2358         Service *s = SERVICE(u);
2359
2360         assert(u);
2361         assert(f);
2362         assert(fds);
2363
2364         unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2365         unit_serialize_item(u, f, "failure", yes_no(s->failure));
2366
2367         if (s->control_pid > 0)
2368                 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
2369
2370         if (s->main_pid_known && s->main_pid > 0)
2371                 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2372
2373         unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2374
2375         if (s->status_text)
2376                 unit_serialize_item(u, f, "status-text", s->status_text);
2377
2378         /* There's a minor uncleanliness here: if there are multiple
2379          * commands attached here, we will start from the first one
2380          * again */
2381         if (s->control_command_id >= 0)
2382                 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
2383
2384         if (s->socket_fd >= 0) {
2385                 int copy;
2386
2387                 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2388                         return copy;
2389
2390                 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2391         }
2392
2393         if (s->main_exec_status.pid > 0) {
2394                 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid);
2395                 dual_timestamp_serialize(f, "main-exec-status-start", &s->main_exec_status.start_timestamp);
2396                 dual_timestamp_serialize(f, "main-exec-status-exit", &s->main_exec_status.exit_timestamp);
2397
2398                 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
2399                         unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2400                         unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2401                 }
2402         }
2403
2404         return 0;
2405 }
2406
2407 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2408         Service *s = SERVICE(u);
2409
2410         assert(u);
2411         assert(key);
2412         assert(value);
2413         assert(fds);
2414
2415         if (streq(key, "state")) {
2416                 ServiceState state;
2417
2418                 if ((state = service_state_from_string(value)) < 0)
2419                         log_debug("Failed to parse state value %s", value);
2420                 else
2421                         s->deserialized_state = state;
2422         } else if (streq(key, "failure")) {
2423                 int b;
2424
2425                 if ((b = parse_boolean(value)) < 0)
2426                         log_debug("Failed to parse failure value %s", value);
2427                 else
2428                         s->failure = b || s->failure;
2429         } else if (streq(key, "control-pid")) {
2430                 pid_t pid;
2431
2432                 if (parse_pid(value, &pid) < 0)
2433                         log_debug("Failed to parse control-pid value %s", value);
2434                 else
2435                         s->control_pid = pid;
2436         } else if (streq(key, "main-pid")) {
2437                 pid_t pid;
2438
2439                 if (parse_pid(value, &pid) < 0)
2440                         log_debug("Failed to parse main-pid value %s", value);
2441                 else
2442                         service_set_main_pid(s, (pid_t) pid);
2443         } else if (streq(key, "main-pid-known")) {
2444                 int b;
2445
2446                 if ((b = parse_boolean(value)) < 0)
2447                         log_debug("Failed to parse main-pid-known value %s", value);
2448                 else
2449                         s->main_pid_known = b;
2450         } else if (streq(key, "status-text")) {
2451                 char *t;
2452
2453                 if ((t = strdup(value))) {
2454                         free(s->status_text);
2455                         s->status_text = t;
2456                 }
2457
2458         } else if (streq(key, "control-command")) {
2459                 ServiceExecCommand id;
2460
2461                 if ((id = service_exec_command_from_string(value)) < 0)
2462                         log_debug("Failed to parse exec-command value %s", value);
2463                 else {
2464                         s->control_command_id = id;
2465                         s->control_command = s->exec_command[id];
2466                 }
2467         } else if (streq(key, "socket-fd")) {
2468                 int fd;
2469
2470                 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2471                         log_debug("Failed to parse socket-fd value %s", value);
2472                 else {
2473
2474                         if (s->socket_fd >= 0)
2475                                 close_nointr_nofail(s->socket_fd);
2476                         s->socket_fd = fdset_remove(fds, fd);
2477                 }
2478         } else if (streq(key, "main-exec-status-pid")) {
2479                 pid_t pid;
2480
2481                 if (parse_pid(value, &pid) < 0)
2482                         log_debug("Failed to parse main-exec-status-pid value %s", value);
2483                 else
2484                         s->main_exec_status.pid = pid;
2485         } else if (streq(key, "main-exec-status-code")) {
2486                 int i;
2487
2488                 if (safe_atoi(value, &i) < 0)
2489                         log_debug("Failed to parse main-exec-status-code value %s", value);
2490                 else
2491                         s->main_exec_status.code = i;
2492         } else if (streq(key, "main-exec-status-status")) {
2493                 int i;
2494
2495                 if (safe_atoi(value, &i) < 0)
2496                         log_debug("Failed to parse main-exec-status-status value %s", value);
2497                 else
2498                         s->main_exec_status.status = i;
2499         } else if (streq(key, "main-exec-status-start"))
2500                 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2501         else if (streq(key, "main-exec-status-exit"))
2502                 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
2503         else
2504                 log_debug("Unknown serialization key '%s'", key);
2505
2506         return 0;
2507 }
2508
2509 static UnitActiveState service_active_state(Unit *u) {
2510         assert(u);
2511
2512         return state_translation_table[SERVICE(u)->state];
2513 }
2514
2515 static const char *service_sub_state_to_string(Unit *u) {
2516         assert(u);
2517
2518         return service_state_to_string(SERVICE(u)->state);
2519 }
2520
2521 static bool service_check_gc(Unit *u) {
2522         Service *s = SERVICE(u);
2523
2524         assert(s);
2525
2526         /* Never clean up services that still have a process around,
2527          * even if the service is formally dead. */
2528         if (cgroup_good(s) > 0 ||
2529             main_pid_good(s) > 0 ||
2530             control_pid_good(s) > 0)
2531                 return true;
2532
2533 #ifdef HAVE_SYSV_COMPAT
2534         if (s->sysv_path)
2535                 return true;
2536 #endif
2537
2538         return false;
2539 }
2540
2541 static bool service_check_snapshot(Unit *u) {
2542         Service *s = SERVICE(u);
2543
2544         assert(s);
2545
2546         return !s->got_socket_fd;
2547 }
2548
2549 static int service_retry_pid_file(Service *s) {
2550         int r;
2551
2552         assert(s->pid_file);
2553         assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2554
2555         r = service_load_pid_file(s, false);
2556         if (r < 0)
2557                 return r;
2558
2559         service_unwatch_pid_file(s);
2560
2561         service_enter_running(s, true);
2562         return 0;
2563 }
2564
2565 static int service_watch_pid_file(Service *s) {
2566         int r;
2567
2568         log_debug("Setting watch for %s's PID file %s", s->meta.id, s->pid_file_pathspec->path);
2569         r = path_spec_watch(s->pid_file_pathspec, UNIT(s));
2570         if (r < 0)
2571                 goto fail;
2572
2573         /* the pidfile might have appeared just before we set the watch */
2574         service_retry_pid_file(s);
2575
2576         return 0;
2577 fail:
2578         log_error("Failed to set a watch for %s's PID file %s: %s",
2579                   s->meta.id, s->pid_file_pathspec->path, strerror(-r));
2580         service_unwatch_pid_file(s);
2581         return r;
2582 }
2583
2584 static int service_demand_pid_file(Service *s) {
2585         PathSpec *ps;
2586
2587         assert(s->pid_file);
2588         assert(!s->pid_file_pathspec);
2589
2590         ps = new0(PathSpec, 1);
2591         if (!ps)
2592                 return -ENOMEM;
2593
2594         ps->path = strdup(s->pid_file);
2595         if (!ps->path) {
2596                 free(ps);
2597                 return -ENOMEM;
2598         }
2599
2600         path_kill_slashes(ps->path);
2601
2602         /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2603          * keep their PID file open all the time. */
2604         ps->type = PATH_MODIFIED;
2605         ps->inotify_fd = -1;
2606
2607         s->pid_file_pathspec = ps;
2608
2609         return service_watch_pid_file(s);
2610 }
2611
2612 static void service_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
2613         Service *s = SERVICE(u);
2614
2615         assert(s);
2616         assert(fd >= 0);
2617         assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2618         assert(s->pid_file_pathspec);
2619         assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2620
2621         log_debug("inotify event for %s", u->meta.id);
2622
2623         if (path_spec_fd_event(s->pid_file_pathspec, events) < 0)
2624                 goto fail;
2625
2626         if (service_retry_pid_file(s) == 0)
2627                 return;
2628
2629         if (service_watch_pid_file(s) < 0)
2630                 goto fail;
2631
2632         return;
2633 fail:
2634         service_unwatch_pid_file(s);
2635         service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2636 }
2637
2638 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2639         Service *s = SERVICE(u);
2640         bool success;
2641
2642         assert(s);
2643         assert(pid >= 0);
2644
2645         if (!s->meta.fragment_path)
2646                 success = is_clean_exit_lsb(code, status);
2647         else
2648                 success = is_clean_exit(code, status);
2649
2650         if (s->main_pid == pid) {
2651                 /* Forking services may occasionally move to a new PID.
2652                  * As long as they update the PID file before exiting the old
2653                  * PID, they're fine. */
2654                 if (service_load_pid_file(s, false) == 0)
2655                         return;
2656
2657                 s->main_pid = 0;
2658                 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2659
2660                 /* If this is not a forking service than the main
2661                  * process got started and hence we copy the exit
2662                  * status so that it is recorded both as main and as
2663                  * control process exit status */
2664                 if (s->main_command) {
2665                         s->main_command->exec_status = s->main_exec_status;
2666
2667                         if (s->main_command->ignore)
2668                                 success = true;
2669                 }
2670
2671                 log_full(success ? LOG_DEBUG : LOG_NOTICE,
2672                          "%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
2673                 s->failure = s->failure || !success;
2674
2675                 if (s->main_command &&
2676                     s->main_command->command_next &&
2677                     success) {
2678
2679                         /* There is another command to *
2680                          * execute, so let's do that. */
2681
2682                         log_debug("%s running next main command for state %s", u->meta.id, service_state_to_string(s->state));
2683                         service_run_next_main(s, success);
2684
2685                 } else {
2686
2687                         /* The service exited, so the service is officially
2688                          * gone. */
2689                         s->main_command = NULL;
2690
2691                         switch (s->state) {
2692
2693                         case SERVICE_START_POST:
2694                         case SERVICE_RELOAD:
2695                         case SERVICE_STOP:
2696                                 /* Need to wait until the operation is
2697                                  * done */
2698                                 break;
2699
2700                         case SERVICE_START:
2701                                 if (s->type == SERVICE_ONESHOT) {
2702                                         /* This was our main goal, so let's go on */
2703                                         if (success)
2704                                                 service_enter_start_post(s);
2705                                         else
2706                                                 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2707                                         break;
2708                                 } else {
2709                                         assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY);
2710
2711                                         /* Fall through */
2712                                 }
2713
2714                         case SERVICE_RUNNING:
2715                                 service_enter_running(s, success);
2716                                 break;
2717
2718                         case SERVICE_STOP_SIGTERM:
2719                         case SERVICE_STOP_SIGKILL:
2720
2721                                 if (!control_pid_good(s))
2722                                         service_enter_stop_post(s, success);
2723
2724                                 /* If there is still a control process, wait for that first */
2725                                 break;
2726
2727                         default:
2728                                 assert_not_reached("Uh, main process died at wrong time.");
2729                         }
2730                 }
2731
2732         } else if (s->control_pid == pid) {
2733
2734                 s->control_pid = 0;
2735
2736                 if (s->control_command) {
2737                         exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
2738
2739                         if (s->control_command->ignore)
2740                                 success = true;
2741                 }
2742
2743                 log_full(success ? LOG_DEBUG : LOG_NOTICE,
2744                          "%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
2745                 s->failure = s->failure || !success;
2746
2747                 if (s->control_command &&
2748                     s->control_command->command_next &&
2749                     success) {
2750
2751                         /* There is another command to *
2752                          * execute, so let's do that. */
2753
2754                         log_debug("%s running next control command for state %s", u->meta.id, service_state_to_string(s->state));
2755                         service_run_next_control(s, success);
2756
2757                 } else {
2758                         /* No further commands for this step, so let's
2759                          * figure out what to do next */
2760
2761                         s->control_command = NULL;
2762                         s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2763
2764                         log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state));
2765
2766                         switch (s->state) {
2767
2768                         case SERVICE_START_PRE:
2769                                 if (success)
2770                                         service_enter_start(s);
2771                                 else
2772                                         service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2773                                 break;
2774
2775                         case SERVICE_START:
2776                                 assert(s->type == SERVICE_FORKING);
2777
2778                                 if (!success) {
2779                                         service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2780                                         break;
2781                                 }
2782
2783                                 if (s->pid_file) {
2784                                         /* Let's try to load the pid file here if we can.
2785                                          * The PID file might actually be created by a START_POST
2786                                          * script. In that case don't worry if the loading fails. */
2787                                         bool has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
2788                                         int r = service_load_pid_file(s, !has_start_post);
2789                                         if (!has_start_post && r < 0) {
2790                                                 r = service_demand_pid_file(s);
2791                                                 if (r < 0 || !cgroup_good(s))
2792                                                         service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2793                                                 break;
2794                                         }
2795                                 } else
2796                                         service_search_main_pid(s);
2797
2798                                 service_enter_start_post(s);
2799                                 break;
2800
2801                         case SERVICE_START_POST:
2802                                 if (!success) {
2803                                         service_enter_stop(s, false);
2804                                         break;
2805                                 }
2806
2807                                 if (s->pid_file) {
2808                                         int r = service_load_pid_file(s, true);
2809                                         if (r < 0) {
2810                                                 r = service_demand_pid_file(s);
2811                                                 if (r < 0 || !cgroup_good(s))
2812                                                         service_enter_stop(s, false);
2813                                                 break;
2814                                         }
2815                                 } else
2816                                         service_search_main_pid(s);
2817
2818                                 service_enter_running(s, true);
2819                                 break;
2820
2821                         case SERVICE_RELOAD:
2822                                 if (success) {
2823                                         service_load_pid_file(s, true);
2824                                         service_search_main_pid(s);
2825                                 }
2826
2827                                 s->reload_failure = !success;
2828                                 service_enter_running(s, true);
2829                                 break;
2830
2831                         case SERVICE_STOP:
2832                                 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
2833                                 break;
2834
2835                         case SERVICE_STOP_SIGTERM:
2836                         case SERVICE_STOP_SIGKILL:
2837                                 if (main_pid_good(s) <= 0)
2838                                         service_enter_stop_post(s, success);
2839
2840                                 /* If there is still a service
2841                                  * process around, wait until
2842                                  * that one quit, too */
2843                                 break;
2844
2845                         case SERVICE_STOP_POST:
2846                         case SERVICE_FINAL_SIGTERM:
2847                         case SERVICE_FINAL_SIGKILL:
2848                                 service_enter_dead(s, success, true);
2849                                 break;
2850
2851                         default:
2852                                 assert_not_reached("Uh, control process died at wrong time.");
2853                         }
2854                 }
2855         }
2856
2857         /* Notify clients about changed exit status */
2858         unit_add_to_dbus_queue(u);
2859 }
2860
2861 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
2862         Service *s = SERVICE(u);
2863
2864         assert(s);
2865         assert(elapsed == 1);
2866
2867         assert(w == &s->timer_watch);
2868
2869         switch (s->state) {
2870
2871         case SERVICE_START_PRE:
2872         case SERVICE_START:
2873                 log_warning("%s operation timed out. Terminating.", u->meta.id);
2874                 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2875                 break;
2876
2877         case SERVICE_START_POST:
2878                 log_warning("%s operation timed out. Stopping.", u->meta.id);
2879                 service_enter_stop(s, false);
2880                 break;
2881
2882         case SERVICE_RELOAD:
2883                 log_warning("%s operation timed out. Stopping.", u->meta.id);
2884                 s->reload_failure = true;
2885                 service_enter_running(s, true);
2886                 break;
2887
2888         case SERVICE_STOP:
2889                 log_warning("%s stopping timed out. Terminating.", u->meta.id);
2890                 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2891                 break;
2892
2893         case SERVICE_STOP_SIGTERM:
2894                 if (s->exec_context.send_sigkill) {
2895                         log_warning("%s stopping timed out. Killing.", u->meta.id);
2896                         service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
2897                 } else {
2898                         log_warning("%s stopping timed out. Skipping SIGKILL.", u->meta.id);
2899                         service_enter_stop_post(s, false);
2900                 }
2901
2902                 break;
2903
2904         case SERVICE_STOP_SIGKILL:
2905                 /* Uh, we sent a SIGKILL and it is still not gone?
2906                  * Must be something we cannot kill, so let's just be
2907                  * weirded out and continue */
2908
2909                 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
2910                 service_enter_stop_post(s, false);
2911                 break;
2912
2913         case SERVICE_STOP_POST:
2914                 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
2915                 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2916                 break;
2917
2918         case SERVICE_FINAL_SIGTERM:
2919                 if (s->exec_context.send_sigkill) {
2920                         log_warning("%s stopping timed out (2). Killing.", u->meta.id);
2921                         service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
2922                 } else {
2923                         log_warning("%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.", u->meta.id);
2924                         service_enter_dead(s, false, true);
2925                 }
2926
2927                 break;
2928
2929         case SERVICE_FINAL_SIGKILL:
2930                 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u->meta.id);
2931                 service_enter_dead(s, false, true);
2932                 break;
2933
2934         case SERVICE_AUTO_RESTART:
2935                 log_info("%s holdoff time over, scheduling restart.", u->meta.id);
2936                 service_enter_restart(s);
2937                 break;
2938
2939         default:
2940                 assert_not_reached("Timeout at wrong time.");
2941         }
2942 }
2943
2944 static void service_cgroup_notify_event(Unit *u) {
2945         Service *s = SERVICE(u);
2946
2947         assert(u);
2948
2949         log_debug("%s: cgroup is empty", u->meta.id);
2950
2951         switch (s->state) {
2952
2953                 /* Waiting for SIGCHLD is usually more interesting,
2954                  * because it includes return codes/signals. Which is
2955                  * why we ignore the cgroup events for most cases,
2956                  * except when we don't know pid which to expect the
2957                  * SIGCHLD for. */
2958
2959         case SERVICE_START:
2960         case SERVICE_START_POST:
2961                 /* If we were hoping for the daemon to write its PID file,
2962                  * we can give up now. */
2963                 if (s->pid_file_pathspec) {
2964                         log_warning("%s never wrote its PID file. Failing.", s->meta.id);
2965                         service_unwatch_pid_file(s);
2966                         if (s->state == SERVICE_START)
2967                                 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2968                         else
2969                                 service_enter_stop(s, false);
2970                 }
2971                 break;
2972
2973         case SERVICE_RUNNING:
2974                 service_enter_running(s, true);
2975                 break;
2976
2977         case SERVICE_STOP_SIGTERM:
2978         case SERVICE_STOP_SIGKILL:
2979
2980                 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2981                         service_enter_stop_post(s, true);
2982
2983                 break;
2984
2985         case SERVICE_FINAL_SIGTERM:
2986         case SERVICE_FINAL_SIGKILL:
2987                 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2988                         service_enter_dead(s, true, true);
2989
2990                 break;
2991
2992         default:
2993                 ;
2994         }
2995 }
2996
2997 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
2998         Service *s = SERVICE(u);
2999         const char *e;
3000
3001         assert(u);
3002
3003         if (s->notify_access == NOTIFY_NONE) {
3004                 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
3005                             u->meta.id, (unsigned long) pid);
3006                 return;
3007         }
3008
3009         if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
3010                 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
3011                             u->meta.id, (unsigned long) pid, (unsigned long) s->main_pid);
3012                 return;
3013         }
3014
3015         log_debug("%s: Got message", u->meta.id);
3016
3017         /* Interpret MAINPID= */
3018         if ((e = strv_find_prefix(tags, "MAINPID=")) &&
3019             (s->state == SERVICE_START ||
3020              s->state == SERVICE_START_POST ||
3021              s->state == SERVICE_RUNNING ||
3022              s->state == SERVICE_RELOAD)) {
3023
3024                 if (parse_pid(e + 8, &pid) < 0)
3025                         log_warning("Failed to parse notification message %s", e);
3026                 else {
3027                         log_debug("%s: got %s", u->meta.id, e);
3028                         service_set_main_pid(s, pid);
3029                 }
3030         }
3031
3032         /* Interpret READY= */
3033         if (s->type == SERVICE_NOTIFY &&
3034             s->state == SERVICE_START &&
3035             strv_find(tags, "READY=1")) {
3036                 log_debug("%s: got READY=1", u->meta.id);
3037
3038                 service_enter_start_post(s);
3039         }
3040
3041         /* Interpret STATUS= */
3042         if ((e = strv_find_prefix(tags, "STATUS="))) {
3043                 char *t;
3044
3045                 if (e[7]) {
3046                         if (!(t = strdup(e+7))) {
3047                                 log_error("Failed to allocate string.");
3048                                 return;
3049                         }
3050
3051                         log_debug("%s: got %s", u->meta.id, e);
3052
3053                         free(s->status_text);
3054                         s->status_text = t;
3055                 } else {
3056                         free(s->status_text);
3057                         s->status_text = NULL;
3058                 }
3059
3060         }
3061
3062         /* Notify clients about changed status or main pid */
3063         unit_add_to_dbus_queue(u);
3064 }
3065
3066 #ifdef HAVE_SYSV_COMPAT
3067
3068 #ifdef TARGET_SUSE
3069 static void sysv_facility_in_insserv_conf(Manager *mgr) {
3070         FILE *f=NULL;
3071         int r;
3072
3073         if (!(f = fopen("/etc/insserv.conf", "re"))) {
3074                 r = errno == ENOENT ? 0 : -errno;
3075                 goto finish;
3076         }
3077
3078         while (!feof(f)) {
3079                 char l[LINE_MAX], *t;
3080                 char **parsed = NULL;
3081
3082                 if (!fgets(l, sizeof(l), f)) {
3083                         if (feof(f))
3084                                 break;
3085
3086                         r = -errno;
3087                         log_error("Failed to read configuration file '/etc/insserv.conf': %s", strerror(-r));
3088                         goto finish;
3089                 }
3090
3091                 t = strstrip(l);
3092                 if (*t != '$' && *t != '<')
3093                         continue;
3094
3095                 parsed = strv_split(t,WHITESPACE);
3096                 /* we ignore <interactive>, not used, equivalent to X-Interactive */
3097                 if (parsed && !startswith_no_case (parsed[0], "<interactive>")) {
3098                         char *facility;
3099                         Unit *u;
3100                         if (sysv_translate_facility(parsed[0], NULL, &facility) < 0)
3101                                 continue;
3102                         if ((u = manager_get_unit(mgr, facility)) && (u->meta.type == UNIT_TARGET)) {
3103                                 UnitDependency e;
3104                                 char *dep = NULL, *name, **j;
3105
3106                                 STRV_FOREACH (j, parsed+1) {
3107                                         if (*j[0]=='+') {
3108                                                 e = UNIT_WANTS;
3109                                                 name = *j+1;
3110                                         }
3111                                         else {
3112                                                 e = UNIT_REQUIRES;
3113                                                 name = *j;
3114                                         }
3115                                         if (sysv_translate_facility(name, NULL, &dep) < 0)
3116                                                 continue;
3117
3118                                         r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, e, dep, NULL, true);
3119                                         free(dep);
3120                                 }
3121                         }
3122                         free(facility);
3123                 }
3124                 strv_free(parsed);
3125         }
3126 finish:
3127         if (f)
3128                 fclose(f);
3129
3130 }
3131 #endif
3132
3133 static int service_enumerate(Manager *m) {
3134         char **p;
3135         unsigned i;
3136         DIR *d = NULL;
3137         char *path = NULL, *fpath = NULL, *name = NULL;
3138         Set *runlevel_services[ELEMENTSOF(rcnd_table)], *shutdown_services = NULL;
3139         Unit *service;
3140         Iterator j;
3141         int r;
3142
3143         assert(m);
3144
3145         if (m->running_as != MANAGER_SYSTEM)
3146                 return 0;
3147
3148         zero(runlevel_services);
3149
3150         STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
3151                 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
3152                         struct dirent *de;
3153
3154                         free(path);
3155                         path = join(*p, "/", rcnd_table[i].path, NULL);
3156                         if (!path) {
3157                                 r = -ENOMEM;
3158                                 goto finish;
3159                         }
3160
3161                         if (d)
3162                                 closedir(d);
3163
3164                         if (!(d = opendir(path))) {
3165                                 if (errno != ENOENT)
3166                                         log_warning("opendir() failed on %s: %s", path, strerror(errno));
3167
3168                                 continue;
3169                         }
3170
3171                         while ((de = readdir(d))) {
3172                                 int a, b;
3173
3174                                 if (ignore_file(de->d_name))
3175                                         continue;
3176
3177                                 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
3178                                         continue;
3179
3180                                 if (strlen(de->d_name) < 4)
3181                                         continue;
3182
3183                                 a = undecchar(de->d_name[1]);
3184                                 b = undecchar(de->d_name[2]);
3185
3186                                 if (a < 0 || b < 0)
3187                                         continue;
3188
3189                                 free(fpath);
3190                                 fpath = join(path, "/", de->d_name, NULL);
3191                                 if (!fpath) {
3192                                         r = -ENOMEM;
3193                                         goto finish;
3194                                 }
3195
3196                                 if (access(fpath, X_OK) < 0) {
3197
3198                                         if (errno != ENOENT)
3199                                                 log_warning("access() failed on %s: %s", fpath, strerror(errno));
3200
3201                                         continue;
3202                                 }
3203
3204                                 free(name);
3205                                 if (!(name = sysv_translate_name(de->d_name + 3))) {
3206                                         r = -ENOMEM;
3207                                         goto finish;
3208                                 }
3209
3210                                 if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
3211                                         log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
3212                                         continue;
3213                                 }
3214
3215                                 if (de->d_name[0] == 'S')  {
3216
3217                                         if (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT) {
3218                                                 SERVICE(service)->sysv_start_priority_from_rcnd =
3219                                                         MAX(a*10 + b, SERVICE(service)->sysv_start_priority_from_rcnd);
3220
3221                                                 SERVICE(service)->sysv_enabled = true;
3222                                         }
3223
3224                                         if ((r = set_ensure_allocated(&runlevel_services[i], trivial_hash_func, trivial_compare_func)) < 0)
3225                                                 goto finish;
3226
3227                                         if ((r = set_put(runlevel_services[i], service)) < 0)
3228                                                 goto finish;
3229
3230                                 } else if (de->d_name[0] == 'K' &&
3231                                            (rcnd_table[i].type == RUNLEVEL_DOWN ||
3232                                             rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
3233
3234                                         if ((r = set_ensure_allocated(&shutdown_services, trivial_hash_func, trivial_compare_func)) < 0)
3235                                                 goto finish;
3236
3237                                         if ((r = set_put(shutdown_services, service)) < 0)
3238                                                 goto finish;
3239                                 }
3240                         }
3241                 }
3242
3243         /* Now we loaded all stubs and are aware of the lowest
3244         start-up priority for all services, not let's actually load
3245         the services, this will also tell us which services are
3246         actually native now */
3247         manager_dispatch_load_queue(m);
3248
3249         /* If this is a native service, rely on native ways to pull in
3250          * a service, don't pull it in via sysv rcN.d links. */
3251         for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
3252                 SET_FOREACH(service, runlevel_services[i], j) {
3253                         service = unit_follow_merge(service);
3254
3255                         if (service->meta.fragment_path)
3256                                 continue;
3257
3258                         if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
3259                                 goto finish;
3260                 }
3261
3262         /* We honour K links only for halt/reboot. For the normal
3263          * runlevels we assume the stop jobs will be implicitly added
3264          * by the core logic. Also, we don't really distinguish here
3265          * between the runlevels 0 and 6 and just add them to the
3266          * special shutdown target. On SUSE the boot.d/ runlevel is
3267          * also used for shutdown, so we add links for that too to the
3268          * shutdown target.*/
3269         SET_FOREACH(service, shutdown_services, j) {
3270                 service = unit_follow_merge(service);
3271
3272                 if (service->meta.fragment_path)
3273                         continue;
3274
3275                 if ((r = unit_add_two_dependencies_by_name(service, UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
3276                         goto finish;
3277         }
3278
3279         r = 0;
3280
3281 #ifdef TARGET_SUSE
3282         sysv_facility_in_insserv_conf (m);
3283 #endif
3284
3285 finish:
3286         free(path);
3287         free(fpath);
3288         free(name);
3289
3290         for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
3291                 set_free(runlevel_services[i]);
3292         set_free(shutdown_services);
3293
3294         if (d)
3295                 closedir(d);
3296
3297         return r;
3298 }
3299 #endif
3300
3301 static void service_bus_name_owner_change(
3302                 Unit *u,
3303                 const char *name,
3304                 const char *old_owner,
3305                 const char *new_owner) {
3306
3307         Service *s = SERVICE(u);
3308
3309         assert(s);
3310         assert(name);
3311
3312         assert(streq(s->bus_name, name));
3313         assert(old_owner || new_owner);
3314
3315         if (old_owner && new_owner)
3316                 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner);
3317         else if (old_owner)
3318                 log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner);
3319         else
3320                 log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner);
3321
3322         s->bus_name_good = !!new_owner;
3323
3324         if (s->type == SERVICE_DBUS) {
3325
3326                 /* service_enter_running() will figure out what to
3327                  * do */
3328                 if (s->state == SERVICE_RUNNING)
3329                         service_enter_running(s, true);
3330                 else if (s->state == SERVICE_START && new_owner)
3331                         service_enter_start_post(s);
3332
3333         } else if (new_owner &&
3334                    s->main_pid <= 0 &&
3335                    (s->state == SERVICE_START ||
3336                     s->state == SERVICE_START_POST ||
3337                     s->state == SERVICE_RUNNING ||
3338                     s->state == SERVICE_RELOAD)) {
3339
3340                 /* Try to acquire PID from bus service */
3341                 log_debug("Trying to acquire PID from D-Bus name...");
3342
3343                 bus_query_pid(u->meta.manager, name);
3344         }
3345 }
3346
3347 static void service_bus_query_pid_done(
3348                 Unit *u,
3349                 const char *name,
3350                 pid_t pid) {
3351
3352         Service *s = SERVICE(u);
3353
3354         assert(s);
3355         assert(name);
3356
3357         log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid);
3358
3359         if (s->main_pid <= 0 &&
3360             (s->state == SERVICE_START ||
3361              s->state == SERVICE_START_POST ||
3362              s->state == SERVICE_RUNNING ||
3363              s->state == SERVICE_RELOAD))
3364                 service_set_main_pid(s, pid);
3365 }
3366
3367 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
3368
3369         assert(s);
3370         assert(fd >= 0);
3371
3372         /* This is called by the socket code when instantiating a new
3373          * service for a stream socket and the socket needs to be
3374          * configured. */
3375
3376         if (s->meta.load_state != UNIT_LOADED)
3377                 return -EINVAL;
3378
3379         if (s->socket_fd >= 0)
3380                 return -EBUSY;
3381
3382         if (s->state != SERVICE_DEAD)
3383                 return -EAGAIN;
3384
3385         s->socket_fd = fd;
3386         s->got_socket_fd = true;
3387
3388         unit_ref_set(&s->accept_socket, UNIT(sock));
3389
3390         return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
3391 }
3392
3393 static void service_reset_failed(Unit *u) {
3394         Service *s = SERVICE(u);
3395
3396         assert(s);
3397
3398         if (s->state == SERVICE_FAILED)
3399                 service_set_state(s, SERVICE_DEAD);
3400
3401         s->failure = false;
3402 }
3403
3404 static bool service_need_daemon_reload(Unit *u) {
3405         Service *s = SERVICE(u);
3406
3407         assert(s);
3408
3409 #ifdef HAVE_SYSV_COMPAT
3410         if (s->sysv_path) {
3411                 struct stat st;
3412
3413                 zero(st);
3414                 if (stat(s->sysv_path, &st) < 0)
3415                         /* What, cannot access this anymore? */
3416                         return true;
3417
3418                 if (s->sysv_mtime > 0 &&
3419                     timespec_load(&st.st_mtim) != s->sysv_mtime)
3420                         return true;
3421         }
3422 #endif
3423
3424         return false;
3425 }
3426
3427 static int service_kill(Unit *u, KillWho who, KillMode mode, int signo, DBusError *error) {
3428         Service *s = SERVICE(u);
3429         int r = 0;
3430         Set *pid_set = NULL;
3431
3432         assert(s);
3433
3434         if (s->main_pid <= 0 && who == KILL_MAIN) {
3435                 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No main process to kill");
3436                 return -ESRCH;
3437         }
3438
3439         if (s->control_pid <= 0 && who == KILL_CONTROL) {
3440                 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
3441                 return -ESRCH;
3442         }
3443
3444         if (who == KILL_CONTROL || who == KILL_ALL)
3445                 if (s->control_pid > 0)
3446                         if (kill(s->control_pid, signo) < 0)
3447                                 r = -errno;
3448
3449         if (who == KILL_MAIN || who == KILL_ALL)
3450                 if (s->main_pid > 0)
3451                         if (kill(s->main_pid, signo) < 0)
3452                                 r = -errno;
3453
3454         if (who == KILL_ALL && mode == KILL_CONTROL_GROUP) {
3455                 int q;
3456
3457                 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func)))
3458                         return -ENOMEM;
3459
3460                 /* Exclude the control/main pid from being killed via the cgroup */
3461                 if (s->control_pid > 0)
3462                         if ((q = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0) {
3463                                 r = q;
3464                                 goto finish;
3465                         }
3466
3467                 if (s->main_pid > 0)
3468                         if ((q = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0) {
3469                                 r = q;
3470                                 goto finish;
3471                         }
3472
3473                 if ((q = cgroup_bonding_kill_list(s->meta.cgroup_bondings, signo, false, pid_set)) < 0)
3474                         if (q != -EAGAIN && q != -ESRCH && q != -ENOENT)
3475                                 r = q;
3476         }
3477
3478 finish:
3479         if (pid_set)
3480                 set_free(pid_set);
3481
3482         return r;
3483 }
3484
3485 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3486         [SERVICE_DEAD] = "dead",
3487         [SERVICE_START_PRE] = "start-pre",
3488         [SERVICE_START] = "start",
3489         [SERVICE_START_POST] = "start-post",
3490         [SERVICE_RUNNING] = "running",
3491         [SERVICE_EXITED] = "exited",
3492         [SERVICE_RELOAD] = "reload",
3493         [SERVICE_STOP] = "stop",
3494         [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3495         [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3496         [SERVICE_STOP_POST] = "stop-post",
3497         [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3498         [SERVICE_FINAL_SIGKILL] = "final-sigkill",
3499         [SERVICE_FAILED] = "failed",
3500         [SERVICE_AUTO_RESTART] = "auto-restart",
3501 };
3502
3503 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3504
3505 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3506         [SERVICE_RESTART_NO] = "no",
3507         [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3508         [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3509         [SERVICE_RESTART_ON_ABORT] = "on-abort",
3510         [SERVICE_RESTART_ALWAYS] = "always"
3511 };
3512
3513 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3514
3515 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3516         [SERVICE_SIMPLE] = "simple",
3517         [SERVICE_FORKING] = "forking",
3518         [SERVICE_ONESHOT] = "oneshot",
3519         [SERVICE_DBUS] = "dbus",
3520         [SERVICE_NOTIFY] = "notify"
3521 };
3522
3523 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3524
3525 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3526         [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3527         [SERVICE_EXEC_START] = "ExecStart",
3528         [SERVICE_EXEC_START_POST] = "ExecStartPost",
3529         [SERVICE_EXEC_RELOAD] = "ExecReload",
3530         [SERVICE_EXEC_STOP] = "ExecStop",
3531         [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3532 };
3533
3534 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3535
3536 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3537         [NOTIFY_NONE] = "none",
3538         [NOTIFY_MAIN] = "main",
3539         [NOTIFY_ALL] = "all"
3540 };
3541
3542 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3543
3544 const UnitVTable service_vtable = {
3545         .suffix = ".service",
3546         .sections =
3547                 "Unit\0"
3548                 "Service\0"
3549                 "Install\0",
3550         .show_status = true,
3551
3552         .init = service_init,
3553         .done = service_done,
3554         .load = service_load,
3555
3556         .coldplug = service_coldplug,
3557
3558         .dump = service_dump,
3559
3560         .start = service_start,
3561         .stop = service_stop,
3562         .reload = service_reload,
3563
3564         .can_reload = service_can_reload,
3565
3566         .kill = service_kill,
3567
3568         .serialize = service_serialize,
3569         .deserialize_item = service_deserialize_item,
3570
3571         .active_state = service_active_state,
3572         .sub_state_to_string = service_sub_state_to_string,
3573
3574         .check_gc = service_check_gc,
3575         .check_snapshot = service_check_snapshot,
3576
3577         .sigchld_event = service_sigchld_event,
3578         .timer_event = service_timer_event,
3579         .fd_event = service_fd_event,
3580
3581         .reset_failed = service_reset_failed,
3582
3583         .need_daemon_reload = service_need_daemon_reload,
3584
3585         .cgroup_notify_empty = service_cgroup_notify_event,
3586         .notify_message = service_notify_message,
3587
3588         .bus_name_owner_change = service_bus_name_owner_change,
3589         .bus_query_pid_done = service_bus_query_pid_done,
3590
3591         .bus_interface = "org.freedesktop.systemd1.Service",
3592         .bus_message_handler = bus_service_message_handler,
3593         .bus_invalidating_properties =  bus_service_invalidating_properties,
3594
3595 #ifdef HAVE_SYSV_COMPAT
3596         .enumerate = service_enumerate
3597 #endif
3598 };