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