chiark / gitweb /
systemctl: at full stop after last message before shutting down
[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_CONFLICTED_BY, 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->forbid_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         s->forbid_restart = false;
1515
1516         return;
1517
1518 fail:
1519         log_warning("%s failed to run install restart timer: %s", s->meta.id, strerror(-r));
1520         service_enter_dead(s, false, false);
1521 }
1522
1523 static void service_enter_signal(Service *s, ServiceState state, bool success);
1524
1525 static void service_enter_stop_post(Service *s, bool success) {
1526         int r;
1527         assert(s);
1528
1529         if (!success)
1530                 s->failure = true;
1531
1532         service_unwatch_control_pid(s);
1533
1534         s->control_command_id = SERVICE_EXEC_STOP_POST;
1535         if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1536                 if ((r = service_spawn(s,
1537                                        s->control_command,
1538                                        true,
1539                                        false,
1540                                        !s->permissions_start_only,
1541                                        !s->root_directory_start_only,
1542                                        true,
1543                                        false,
1544                                        &s->control_pid)) < 0)
1545                         goto fail;
1546
1547
1548                 service_set_state(s, SERVICE_STOP_POST);
1549         } else
1550                 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
1551
1552         return;
1553
1554 fail:
1555         log_warning("%s failed to run 'stop-post' task: %s", s->meta.id, strerror(-r));
1556         service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1557 }
1558
1559 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1560         int r;
1561         bool sent = false;
1562
1563         assert(s);
1564
1565         if (!success)
1566                 s->failure = true;
1567
1568         if (s->exec_context.kill_mode != KILL_NONE) {
1569                 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? s->exec_context.kill_signal : SIGKILL;
1570
1571                 if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) {
1572
1573                         if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig)) < 0) {
1574                                 if (r != -EAGAIN && r != -ESRCH)
1575                                         goto fail;
1576                         } else
1577                                 sent = true;
1578                 }
1579
1580                 if (!sent) {
1581                         r = 0;
1582
1583                         if (s->main_pid > 0) {
1584                                 if (kill(s->exec_context.kill_mode == KILL_PROCESS ? s->main_pid : -s->main_pid, sig) < 0 && errno != ESRCH)
1585                                         r = -errno;
1586                                 else
1587                                         sent = true;
1588                         }
1589
1590                         if (s->control_pid > 0) {
1591                                 if (kill(s->exec_context.kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH)
1592                                         r = -errno;
1593                                 else
1594                                         sent = true;
1595                         }
1596
1597                         if (r < 0)
1598                                 goto fail;
1599                 }
1600         }
1601
1602         if (sent && (s->main_pid > 0 || s->control_pid > 0)) {
1603                 if (s->timeout_usec > 0)
1604                         if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1605                                 goto fail;
1606
1607                 service_set_state(s, state);
1608         } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1609                 service_enter_stop_post(s, true);
1610         else
1611                 service_enter_dead(s, true, true);
1612
1613         return;
1614
1615 fail:
1616         log_warning("%s failed to kill processes: %s", s->meta.id, strerror(-r));
1617
1618         if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1619                 service_enter_stop_post(s, false);
1620         else
1621                 service_enter_dead(s, false, true);
1622 }
1623
1624 static void service_enter_stop(Service *s, bool success) {
1625         int r;
1626
1627         assert(s);
1628
1629         if (!success)
1630                 s->failure = true;
1631
1632         service_unwatch_control_pid(s);
1633
1634         s->control_command_id = SERVICE_EXEC_STOP;
1635         if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
1636                 if ((r = service_spawn(s,
1637                                        s->control_command,
1638                                        true,
1639                                        false,
1640                                        !s->permissions_start_only,
1641                                        !s->root_directory_start_only,
1642                                        false,
1643                                        false,
1644                                        &s->control_pid)) < 0)
1645                         goto fail;
1646
1647                 service_set_state(s, SERVICE_STOP);
1648         } else
1649                 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1650
1651         return;
1652
1653 fail:
1654         log_warning("%s failed to run 'stop' task: %s", s->meta.id, strerror(-r));
1655         service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1656 }
1657
1658 static void service_enter_running(Service *s, bool success) {
1659         int main_pid_ok, cgroup_ok;
1660         assert(s);
1661
1662         if (!success)
1663                 s->failure = true;
1664
1665         main_pid_ok = main_pid_good(s);
1666         cgroup_ok = cgroup_good(s);
1667
1668         if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
1669             (s->bus_name_good || s->type != SERVICE_DBUS))
1670                 service_set_state(s, SERVICE_RUNNING);
1671         else if (s->valid_no_process)
1672                 service_set_state(s, SERVICE_EXITED);
1673         else
1674                 service_enter_stop(s, true);
1675 }
1676
1677 static void service_enter_start_post(Service *s) {
1678         int r;
1679         assert(s);
1680
1681         service_unwatch_control_pid(s);
1682
1683         s->control_command_id = SERVICE_EXEC_START_POST;
1684         if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
1685                 if ((r = service_spawn(s,
1686                                        s->control_command,
1687                                        true,
1688                                        false,
1689                                        !s->permissions_start_only,
1690                                        !s->root_directory_start_only,
1691                                        false,
1692                                        false,
1693                                        &s->control_pid)) < 0)
1694                         goto fail;
1695
1696                 service_set_state(s, SERVICE_START_POST);
1697         } else
1698                 service_enter_running(s, true);
1699
1700         return;
1701
1702 fail:
1703         log_warning("%s failed to run 'start-post' task: %s", s->meta.id, strerror(-r));
1704         service_enter_stop(s, false);
1705 }
1706
1707 static void service_enter_start(Service *s) {
1708         pid_t pid;
1709         int r;
1710
1711         assert(s);
1712
1713         assert(s->exec_command[SERVICE_EXEC_START]);
1714         assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
1715
1716         if (s->type == SERVICE_FORKING)
1717                 service_unwatch_control_pid(s);
1718         else
1719                 service_unwatch_main_pid(s);
1720
1721         if ((r = service_spawn(s,
1722                                s->exec_command[SERVICE_EXEC_START],
1723                                s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY,
1724                                true,
1725                                true,
1726                                true,
1727                                true,
1728                                s->notify_access != NOTIFY_NONE,
1729                                &pid)) < 0)
1730                 goto fail;
1731
1732         if (s->type == SERVICE_SIMPLE) {
1733                 /* For simple services we immediately start
1734                  * the START_POST binaries. */
1735
1736                 service_set_main_pid(s, pid);
1737                 service_enter_start_post(s);
1738
1739         } else  if (s->type == SERVICE_FORKING) {
1740
1741                 /* For forking services we wait until the start
1742                  * process exited. */
1743
1744                 s->control_command_id = SERVICE_EXEC_START;
1745                 s->control_command = s->exec_command[SERVICE_EXEC_START];
1746
1747                 s->control_pid = pid;
1748                 service_set_state(s, SERVICE_START);
1749
1750         } else if (s->type == SERVICE_FINISH ||
1751                    s->type == SERVICE_DBUS ||
1752                    s->type == SERVICE_NOTIFY) {
1753
1754                 /* For finishing services we wait until the start
1755                  * process exited, too, but it is our main process. */
1756
1757                 /* For D-Bus services we know the main pid right away,
1758                  * but wait for the bus name to appear on the
1759                  * bus. Notify services are similar. */
1760
1761                 service_set_main_pid(s, pid);
1762                 service_set_state(s, SERVICE_START);
1763         } else
1764                 assert_not_reached("Unknown service type");
1765
1766         return;
1767
1768 fail:
1769         log_warning("%s failed to run 'start' task: %s", s->meta.id, strerror(-r));
1770         service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1771 }
1772
1773 static void service_enter_start_pre(Service *s) {
1774         int r;
1775
1776         assert(s);
1777
1778         service_unwatch_control_pid(s);
1779
1780         s->control_command_id = SERVICE_EXEC_START_PRE;
1781         if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
1782                 if ((r = service_spawn(s,
1783                                        s->control_command,
1784                                        true,
1785                                        false,
1786                                        !s->permissions_start_only,
1787                                        !s->root_directory_start_only,
1788                                        true,
1789                                        false,
1790                                        &s->control_pid)) < 0)
1791                         goto fail;
1792
1793                 service_set_state(s, SERVICE_START_PRE);
1794         } else
1795                 service_enter_start(s);
1796
1797         return;
1798
1799 fail:
1800         log_warning("%s failed to run 'start-pre' task: %s", s->meta.id, strerror(-r));
1801         service_enter_dead(s, false, true);
1802 }
1803
1804 static void service_enter_restart(Service *s) {
1805         int r;
1806         DBusError error;
1807
1808         assert(s);
1809         dbus_error_init(&error);
1810
1811         service_enter_dead(s, true, false);
1812
1813         if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL, NULL)) < 0)
1814                 goto fail;
1815
1816         log_debug("%s scheduled restart job.", s->meta.id);
1817         return;
1818
1819 fail:
1820         log_warning("%s failed to schedule restart job: %s", s->meta.id, bus_error(&error, -r));
1821         service_enter_dead(s, false, false);
1822
1823         dbus_error_free(&error);
1824 }
1825
1826 static void service_enter_reload(Service *s) {
1827         int r;
1828
1829         assert(s);
1830
1831         service_unwatch_control_pid(s);
1832
1833         s->control_command_id = SERVICE_EXEC_RELOAD;
1834         if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
1835                 if ((r = service_spawn(s,
1836                                        s->control_command,
1837                                        true,
1838                                        false,
1839                                        !s->permissions_start_only,
1840                                        !s->root_directory_start_only,
1841                                        false,
1842                                        false,
1843                                        &s->control_pid)) < 0)
1844                         goto fail;
1845
1846                 service_set_state(s, SERVICE_RELOAD);
1847         } else
1848                 service_enter_running(s, true);
1849
1850         return;
1851
1852 fail:
1853         log_warning("%s failed to run 'reload' task: %s", s->meta.id, strerror(-r));
1854         service_enter_stop(s, false);
1855 }
1856
1857 static void service_run_next(Service *s, bool success) {
1858         int r;
1859
1860         assert(s);
1861         assert(s->control_command);
1862         assert(s->control_command->command_next);
1863
1864         if (!success)
1865                 s->failure = true;
1866
1867         s->control_command = s->control_command->command_next;
1868
1869         service_unwatch_control_pid(s);
1870
1871         if ((r = service_spawn(s,
1872                                s->control_command,
1873                                true,
1874                                false,
1875                                !s->permissions_start_only,
1876                                !s->root_directory_start_only,
1877                                s->control_command_id == SERVICE_EXEC_START_PRE ||
1878                                s->control_command_id == SERVICE_EXEC_STOP_POST,
1879                                false,
1880                                &s->control_pid)) < 0)
1881                 goto fail;
1882
1883         return;
1884
1885 fail:
1886         log_warning("%s failed to run next task: %s", s->meta.id, strerror(-r));
1887
1888         if (s->state == SERVICE_START_PRE)
1889                 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1890         else if (s->state == SERVICE_STOP)
1891                 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1892         else if (s->state == SERVICE_STOP_POST)
1893                 service_enter_dead(s, false, true);
1894         else
1895                 service_enter_stop(s, false);
1896 }
1897
1898 static int service_start(Unit *u) {
1899         Service *s = SERVICE(u);
1900
1901         assert(s);
1902
1903         /* We cannot fulfill this request right now, try again later
1904          * please! */
1905         if (s->state == SERVICE_STOP ||
1906             s->state == SERVICE_STOP_SIGTERM ||
1907             s->state == SERVICE_STOP_SIGKILL ||
1908             s->state == SERVICE_STOP_POST ||
1909             s->state == SERVICE_FINAL_SIGTERM ||
1910             s->state == SERVICE_FINAL_SIGKILL)
1911                 return -EAGAIN;
1912
1913         /* Already on it! */
1914         if (s->state == SERVICE_START_PRE ||
1915             s->state == SERVICE_START ||
1916             s->state == SERVICE_START_POST)
1917                 return 0;
1918
1919         assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTENANCE || s->state == SERVICE_AUTO_RESTART);
1920
1921         /* Make sure we don't enter a busy loop of some kind. */
1922         if (!ratelimit_test(&s->ratelimit)) {
1923                 log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id);
1924                 return -ECANCELED;
1925         }
1926
1927         if ((s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1928              s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1929              s->exec_context.std_error == EXEC_OUTPUT_SOCKET) &&
1930             s->socket_fd < 0) {
1931                 log_warning("%s can only be started with a per-connection socket.", u->meta.id);
1932                 return -EINVAL;
1933         }
1934
1935         s->failure = false;
1936         s->main_pid_known = false;
1937         s->forbid_restart = false;
1938
1939         service_enter_start_pre(s);
1940         return 0;
1941 }
1942
1943 static int service_stop(Unit *u) {
1944         Service *s = SERVICE(u);
1945
1946         assert(s);
1947
1948         /* This is a user request, so don't do restarts on this
1949          * shutdown. */
1950         s->forbid_restart = true;
1951
1952         /* Already on it */
1953         if (s->state == SERVICE_STOP ||
1954             s->state == SERVICE_STOP_SIGTERM ||
1955             s->state == SERVICE_STOP_SIGKILL ||
1956             s->state == SERVICE_STOP_POST ||
1957             s->state == SERVICE_FINAL_SIGTERM ||
1958             s->state == SERVICE_FINAL_SIGKILL)
1959                 return 0;
1960
1961         /* Don't allow a restart */
1962         if (s->state == SERVICE_AUTO_RESTART) {
1963                 service_set_state(s, SERVICE_DEAD);
1964                 return 0;
1965         }
1966
1967         /* If there's already something running we go directly into
1968          * kill mode. */
1969         if (s->state == SERVICE_START_PRE ||
1970             s->state == SERVICE_START ||
1971             s->state == SERVICE_START_POST ||
1972             s->state == SERVICE_RELOAD) {
1973                 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1974                 return 0;
1975         }
1976
1977         assert(s->state == SERVICE_RUNNING ||
1978                s->state == SERVICE_EXITED);
1979
1980         service_enter_stop(s, true);
1981         return 0;
1982 }
1983
1984 static int service_reload(Unit *u) {
1985         Service *s = SERVICE(u);
1986
1987         assert(s);
1988
1989         assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1990
1991         service_enter_reload(s);
1992         return 0;
1993 }
1994
1995 static bool service_can_reload(Unit *u) {
1996         Service *s = SERVICE(u);
1997
1998         assert(s);
1999
2000         return !!s->exec_command[SERVICE_EXEC_RELOAD];
2001 }
2002
2003 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2004         Service *s = SERVICE(u);
2005
2006         assert(u);
2007         assert(f);
2008         assert(fds);
2009
2010         unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2011         unit_serialize_item(u, f, "failure", yes_no(s->failure));
2012
2013         if (s->control_pid > 0)
2014                 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
2015
2016         if (s->main_pid_known && s->main_pid > 0)
2017                 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2018
2019         unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2020
2021         if (s->status_text)
2022                 unit_serialize_item(u, f, "status-text", s->status_text);
2023
2024         /* There's a minor uncleanliness here: if there are multiple
2025          * commands attached here, we will start from the first one
2026          * again */
2027         if (s->control_command_id >= 0)
2028                 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
2029
2030         if (s->socket_fd >= 0) {
2031                 int copy;
2032
2033                 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2034                         return copy;
2035
2036                 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2037         }
2038
2039         if (s->main_exec_status.pid > 0) {
2040                 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid);
2041
2042                 if (s->main_exec_status.start_timestamp.realtime > 0) {
2043                         unit_serialize_item_format(u, f, "main-exec-status-start-realtime",
2044                                                    "%llu", (unsigned long long) s->main_exec_status.start_timestamp.realtime);
2045
2046                         unit_serialize_item_format(u, f, "main-exec-status-start-monotonic",
2047                                                    "%llu", (unsigned long long) s->main_exec_status.start_timestamp.monotonic);
2048                 }
2049
2050                 if (s->main_exec_status.exit_timestamp.realtime > 0) {
2051                         unit_serialize_item_format(u, f, "main-exec-status-exit-realtime",
2052                                                    "%llu", (unsigned long long) s->main_exec_status.exit_timestamp.realtime);
2053                         unit_serialize_item_format(u, f, "main-exec-status-exit-monotonic",
2054                                                    "%llu", (unsigned long long) s->main_exec_status.exit_timestamp.monotonic);
2055
2056                         unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2057                         unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2058                 }
2059         }
2060
2061         return 0;
2062 }
2063
2064 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2065         Service *s = SERVICE(u);
2066
2067         assert(u);
2068         assert(key);
2069         assert(value);
2070         assert(fds);
2071
2072         if (streq(key, "state")) {
2073                 ServiceState state;
2074
2075                 if ((state = service_state_from_string(value)) < 0)
2076                         log_debug("Failed to parse state value %s", value);
2077                 else
2078                         s->deserialized_state = state;
2079         } else if (streq(key, "failure")) {
2080                 int b;
2081
2082                 if ((b = parse_boolean(value)) < 0)
2083                         log_debug("Failed to parse failure value %s", value);
2084                 else
2085                         s->failure = b || s->failure;
2086         } else if (streq(key, "control-pid")) {
2087                 pid_t pid;
2088
2089                 if (parse_pid(value, &pid) < 0)
2090                         log_debug("Failed to parse control-pid value %s", value);
2091                 else
2092                         s->control_pid = pid;
2093         } else if (streq(key, "main-pid")) {
2094                 pid_t pid;
2095
2096                 if (parse_pid(value, &pid) < 0)
2097                         log_debug("Failed to parse main-pid value %s", value);
2098                 else
2099                         service_set_main_pid(s, (pid_t) pid);
2100         } else if (streq(key, "main-pid-known")) {
2101                 int b;
2102
2103                 if ((b = parse_boolean(value)) < 0)
2104                         log_debug("Failed to parse main-pid-known value %s", value);
2105                 else
2106                         s->main_pid_known = b;
2107         } else if (streq(key, "status-text")) {
2108                 char *t;
2109
2110                 if ((t = strdup(value))) {
2111                         free(s->status_text);
2112                         s->status_text = t;
2113                 }
2114
2115         } else if (streq(key, "control-command")) {
2116                 ServiceExecCommand id;
2117
2118                 if ((id = service_exec_command_from_string(value)) < 0)
2119                         log_debug("Failed to parse exec-command value %s", value);
2120                 else {
2121                         s->control_command_id = id;
2122                         s->control_command = s->exec_command[id];
2123                 }
2124         } else if (streq(key, "socket-fd")) {
2125                 int fd;
2126
2127                 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2128                         log_debug("Failed to parse socket-fd value %s", value);
2129                 else {
2130
2131                         if (s->socket_fd >= 0)
2132                                 close_nointr_nofail(s->socket_fd);
2133                         s->socket_fd = fdset_remove(fds, fd);
2134                 }
2135         } else if (streq(key, "main-exec-status-pid")) {
2136                 pid_t pid;
2137
2138                 if (parse_pid(value, &pid) < 0)
2139                         log_debug("Failed to parse main-exec-status-pid value %s", value);
2140                 else
2141                         s->main_exec_status.pid = pid;
2142         } else if (streq(key, "main-exec-status-code")) {
2143                 int i;
2144
2145                 if (safe_atoi(value, &i) < 0)
2146                         log_debug("Failed to parse main-exec-status-code value %s", value);
2147                 else
2148                         s->main_exec_status.code = i;
2149         } else if (streq(key, "main-exec-status-status")) {
2150                 int i;
2151
2152                 if (safe_atoi(value, &i) < 0)
2153                         log_debug("Failed to parse main-exec-status-status value %s", value);
2154                 else
2155                         s->main_exec_status.status = i;
2156         } else if (streq(key, "main-exec-status-start-realtime")) {
2157                 uint64_t k;
2158
2159                 if (safe_atou64(value, &k) < 0)
2160                         log_debug("Failed to parse main-exec-status-start-realtime value %s", value);
2161                 else
2162                         s->main_exec_status.start_timestamp.realtime = (usec_t) k;
2163         } else if (streq(key, "main-exec-status-start-monotonic")) {
2164                 uint64_t k;
2165
2166                 if (safe_atou64(value, &k) < 0)
2167                         log_debug("Failed to parse main-exec-status-start-monotonic value %s", value);
2168                 else
2169                         s->main_exec_status.start_timestamp.monotonic = (usec_t) k;
2170         } else if (streq(key, "main-exec-status-exit-realtime")) {
2171                 uint64_t k;
2172
2173                 if (safe_atou64(value, &k) < 0)
2174                         log_debug("Failed to parse main-exec-status-exit-realtime value %s", value);
2175                 else
2176                         s->main_exec_status.exit_timestamp.realtime = (usec_t) k;
2177         } else if (streq(key, "main-exec-status-exit-monotonic")) {
2178                 uint64_t k;
2179
2180                 if (safe_atou64(value, &k) < 0)
2181                         log_debug("Failed to parse main-exec-status-exit-monotonic value %s", value);
2182                 else
2183                         s->main_exec_status.exit_timestamp.monotonic = (usec_t) k;
2184         } else
2185                 log_debug("Unknown serialization key '%s'", key);
2186
2187         return 0;
2188 }
2189
2190 static UnitActiveState service_active_state(Unit *u) {
2191         assert(u);
2192
2193         return state_translation_table[SERVICE(u)->state];
2194 }
2195
2196 static const char *service_sub_state_to_string(Unit *u) {
2197         assert(u);
2198
2199         return service_state_to_string(SERVICE(u)->state);
2200 }
2201
2202 static bool service_check_gc(Unit *u) {
2203         Service *s = SERVICE(u);
2204
2205         assert(s);
2206
2207         return !!s->sysv_path;
2208 }
2209
2210 static bool service_check_snapshot(Unit *u) {
2211         Service *s = SERVICE(u);
2212
2213         assert(s);
2214
2215         return !s->got_socket_fd;
2216 }
2217
2218 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2219         Service *s = SERVICE(u);
2220         bool success;
2221
2222         assert(s);
2223         assert(pid >= 0);
2224
2225         success = is_clean_exit(code, status);
2226
2227         if (s->main_pid == pid) {
2228
2229                 exec_status_exit(&s->main_exec_status, pid, code, status);
2230                 s->main_pid = 0;
2231
2232                 if (s->type != SERVICE_FORKING) {
2233                         assert(s->exec_command[SERVICE_EXEC_START]);
2234                         s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
2235
2236                         if (s->exec_command[SERVICE_EXEC_START]->ignore)
2237                                 success = true;
2238                 }
2239
2240                 log_full(success ? LOG_DEBUG : LOG_NOTICE,
2241                          "%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
2242                 s->failure = s->failure || !success;
2243
2244                 /* The service exited, so the service is officially
2245                  * gone. */
2246
2247                 switch (s->state) {
2248
2249                 case SERVICE_START_POST:
2250                 case SERVICE_RELOAD:
2251                 case SERVICE_STOP:
2252                         /* Need to wait until the operation is
2253                          * done */
2254                         break;
2255
2256                 case SERVICE_START:
2257                         if (s->type == SERVICE_FINISH) {
2258                                 /* This was our main goal, so let's go on */
2259                                 if (success)
2260                                         service_enter_start_post(s);
2261                                 else
2262                                         service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2263                                 break;
2264                         } else {
2265                                 assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY);
2266
2267                                 /* Fall through */
2268                         }
2269
2270                 case SERVICE_RUNNING:
2271                         service_enter_running(s, success);
2272                         break;
2273
2274                 case SERVICE_STOP_SIGTERM:
2275                 case SERVICE_STOP_SIGKILL:
2276
2277                         if (!control_pid_good(s))
2278                                 service_enter_stop_post(s, success);
2279
2280                         /* If there is still a control process, wait for that first */
2281                         break;
2282
2283                 default:
2284                         assert_not_reached("Uh, main process died at wrong time.");
2285                 }
2286
2287         } else if (s->control_pid == pid) {
2288
2289                 if (s->control_command) {
2290                         exec_status_exit(&s->control_command->exec_status, pid, code, status);
2291
2292                         if (s->control_command->ignore)
2293                                 success = true;
2294                 }
2295
2296                 s->control_pid = 0;
2297
2298                 log_full(success ? LOG_DEBUG : LOG_NOTICE,
2299                          "%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
2300                 s->failure = s->failure || !success;
2301
2302                 /* If we are shutting things down anyway we
2303                  * don't care about failing commands. */
2304
2305                 if (s->control_command && s->control_command->command_next && success) {
2306
2307                         /* There is another command to *
2308                          * execute, so let's do that. */
2309
2310                         log_debug("%s running next command for state %s", u->meta.id, service_state_to_string(s->state));
2311                         service_run_next(s, success);
2312
2313                 } else {
2314                         /* No further commands for this step, so let's
2315                          * figure out what to do next */
2316
2317                         s->control_command = NULL;
2318                         s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2319
2320                         log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state));
2321
2322                         switch (s->state) {
2323
2324                         case SERVICE_START_PRE:
2325                                 if (success)
2326                                         service_enter_start(s);
2327                                 else
2328                                         service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2329                                 break;
2330
2331                         case SERVICE_START:
2332                                 assert(s->type == SERVICE_FORKING);
2333
2334                                 /* Let's try to load the pid
2335                                  * file here if we can. We
2336                                  * ignore the return value,
2337                                  * since the PID file might
2338                                  * actually be created by a
2339                                  * START_POST script */
2340
2341                                 if (success) {
2342                                         if (s->pid_file)
2343                                                 service_load_pid_file(s);
2344
2345                                         service_enter_start_post(s);
2346                                 } else
2347                                         service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2348
2349                                 break;
2350
2351                         case SERVICE_START_POST:
2352                                 if (success && s->pid_file && !s->main_pid_known) {
2353                                         int r;
2354
2355                                         /* Hmm, let's see if we can
2356                                          * load the pid now after the
2357                                          * start-post scripts got
2358                                          * executed. */
2359
2360                                         if ((r = service_load_pid_file(s)) < 0)
2361                                                 log_warning("%s: failed to load PID file %s: %s", s->meta.id, s->pid_file, strerror(-r));
2362                                 }
2363
2364                                 /* Fall through */
2365
2366                         case SERVICE_RELOAD:
2367                                 if (success)
2368                                         service_enter_running(s, true);
2369                                 else
2370                                         service_enter_stop(s, false);
2371
2372                                 break;
2373
2374                         case SERVICE_STOP:
2375                                 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
2376                                 break;
2377
2378                         case SERVICE_STOP_SIGTERM:
2379                         case SERVICE_STOP_SIGKILL:
2380                                 if (main_pid_good(s) <= 0)
2381                                         service_enter_stop_post(s, success);
2382
2383                                 /* If there is still a service
2384                                  * process around, wait until
2385                                  * that one quit, too */
2386                                 break;
2387
2388                         case SERVICE_STOP_POST:
2389                         case SERVICE_FINAL_SIGTERM:
2390                         case SERVICE_FINAL_SIGKILL:
2391                                 service_enter_dead(s, success, true);
2392                                 break;
2393
2394                         default:
2395                                 assert_not_reached("Uh, control process died at wrong time.");
2396                         }
2397                 }
2398         }
2399 }
2400
2401 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
2402         Service *s = SERVICE(u);
2403
2404         assert(s);
2405         assert(elapsed == 1);
2406
2407         assert(w == &s->timer_watch);
2408
2409         switch (s->state) {
2410
2411         case SERVICE_START_PRE:
2412         case SERVICE_START:
2413                 log_warning("%s operation timed out. Terminating.", u->meta.id);
2414                 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2415                 break;
2416
2417         case SERVICE_START_POST:
2418         case SERVICE_RELOAD:
2419                 log_warning("%s operation timed out. Stopping.", u->meta.id);
2420                 service_enter_stop(s, false);
2421                 break;
2422
2423         case SERVICE_STOP:
2424                 log_warning("%s stopping timed out. Terminating.", u->meta.id);
2425                 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2426                 break;
2427
2428         case SERVICE_STOP_SIGTERM:
2429                 log_warning("%s stopping timed out. Killing.", u->meta.id);
2430                 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
2431                 break;
2432
2433         case SERVICE_STOP_SIGKILL:
2434                 /* Uh, wie sent a SIGKILL and it is still not gone?
2435                  * Must be something we cannot kill, so let's just be
2436                  * weirded out and continue */
2437
2438                 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
2439                 service_enter_stop_post(s, false);
2440                 break;
2441
2442         case SERVICE_STOP_POST:
2443                 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
2444                 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2445                 break;
2446
2447         case SERVICE_FINAL_SIGTERM:
2448                 log_warning("%s stopping timed out (2). Killing.", u->meta.id);
2449                 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
2450                 break;
2451
2452         case SERVICE_FINAL_SIGKILL:
2453                 log_warning("%s still around after SIGKILL (2). Entering maintenance mode.", u->meta.id);
2454                 service_enter_dead(s, false, true);
2455                 break;
2456
2457         case SERVICE_AUTO_RESTART:
2458                 log_info("%s holdoff time over, scheduling restart.", u->meta.id);
2459                 service_enter_restart(s);
2460                 break;
2461
2462         default:
2463                 assert_not_reached("Timeout at wrong time.");
2464         }
2465 }
2466
2467 static void service_cgroup_notify_event(Unit *u) {
2468         Service *s = SERVICE(u);
2469
2470         assert(u);
2471
2472         log_debug("%s: cgroup is empty", u->meta.id);
2473
2474         switch (s->state) {
2475
2476                 /* Waiting for SIGCHLD is usually more interesting,
2477                  * because it includes return codes/signals. Which is
2478                  * why we ignore the cgroup events for most cases,
2479                  * except when we don't know pid which to expect the
2480                  * SIGCHLD for. */
2481
2482         case SERVICE_RUNNING:
2483                 service_enter_running(s, true);
2484                 break;
2485
2486         default:
2487                 ;
2488         }
2489 }
2490
2491 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
2492         Service *s = SERVICE(u);
2493         const char *e;
2494
2495         assert(u);
2496
2497         if (s->notify_access == NOTIFY_NONE) {
2498                 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
2499                             u->meta.id, (unsigned long) pid);
2500                 return;
2501         }
2502
2503         if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
2504                 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
2505                             u->meta.id, (unsigned long) pid, (unsigned long) s->main_pid);
2506                 return;
2507         }
2508
2509         log_debug("%s: Got message", u->meta.id);
2510
2511         /* Interpret MAINPID= */
2512         if ((e = strv_find_prefix(tags, "MAINPID=")) &&
2513             (s->state == SERVICE_START ||
2514              s->state == SERVICE_START_POST ||
2515              s->state == SERVICE_RUNNING ||
2516              s->state == SERVICE_RELOAD)) {
2517
2518                 if (parse_pid(e + 8, &pid) < 0)
2519                         log_warning("Failed to parse notification message %s", e);
2520                 else {
2521                         log_debug("%s: got %s", u->meta.id, e);
2522                         service_set_main_pid(s, pid);
2523                 }
2524         }
2525
2526         /* Interpret READY= */
2527         if (s->type == SERVICE_NOTIFY &&
2528             s->state == SERVICE_START &&
2529             strv_find(tags, "READY=1")) {
2530                 log_debug("%s: got READY=1", u->meta.id);
2531
2532                 service_enter_start_post(s);
2533         }
2534
2535         /* Interpret STATUS= */
2536         if ((e = strv_find_prefix(tags, "STATUS="))) {
2537                 char *t;
2538
2539                 if (e[7]) {
2540                         if (!(t = strdup(e+7))) {
2541                                 log_error("Failed to allocate string.");
2542                                 return;
2543                         }
2544
2545                         log_debug("%s: got %s", u->meta.id, e);
2546
2547                         free(s->status_text);
2548                         s->status_text = t;
2549                 } else {
2550                         free(s->status_text);
2551                         s->status_text = NULL;
2552                 }
2553
2554         }
2555 }
2556
2557 static int service_enumerate(Manager *m) {
2558         char **p;
2559         unsigned i;
2560         DIR *d = NULL;
2561         char *path = NULL, *fpath = NULL, *name = NULL;
2562         int r;
2563
2564         assert(m);
2565
2566         STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
2567                 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
2568                         struct dirent *de;
2569
2570                         free(path);
2571                         path = NULL;
2572                         if (asprintf(&path, "%s/%s", *p, rcnd_table[i].path) < 0) {
2573                                 r = -ENOMEM;
2574                                 goto finish;
2575                         }
2576
2577                         if (d)
2578                                 closedir(d);
2579
2580                         if (!(d = opendir(path))) {
2581                                 if (errno != ENOENT)
2582                                         log_warning("opendir() failed on %s: %s", path, strerror(errno));
2583
2584                                 continue;
2585                         }
2586
2587                         while ((de = readdir(d))) {
2588                                 Unit *service;
2589                                 int a, b;
2590
2591                                 if (ignore_file(de->d_name))
2592                                         continue;
2593
2594                                 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
2595                                         continue;
2596
2597                                 if (strlen(de->d_name) < 4)
2598                                         continue;
2599
2600                                 a = undecchar(de->d_name[1]);
2601                                 b = undecchar(de->d_name[2]);
2602
2603                                 if (a < 0 || b < 0)
2604                                         continue;
2605
2606                                 free(fpath);
2607                                 fpath = NULL;
2608                                 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i].path, de->d_name) < 0) {
2609                                         r = -ENOMEM;
2610                                         goto finish;
2611                                 }
2612
2613                                 if (access(fpath, X_OK) < 0) {
2614
2615                                         if (errno != ENOENT)
2616                                                 log_warning("access() failed on %s: %s", fpath, strerror(errno));
2617
2618                                         continue;
2619                                 }
2620
2621                                 free(name);
2622                                 if (!(name = sysv_translate_name(de->d_name + 3))) {
2623                                         r = -ENOMEM;
2624                                         goto finish;
2625                                 }
2626
2627                                 if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
2628                                         log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
2629                                         continue;
2630                                 }
2631
2632                                 if (de->d_name[0] == 'S' &&
2633                                     (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
2634                                         SERVICE(service)->sysv_start_priority =
2635                                                 MAX(a*10 + b, SERVICE(service)->sysv_start_priority);
2636                                         SERVICE(service)->sysv_enabled = true;
2637                                 }
2638
2639                                 manager_dispatch_load_queue(m);
2640                                 service = unit_follow_merge(service);
2641
2642                                 /* If this is a native service, rely
2643                                  * on native ways to pull in a
2644                                  * service, don't pull it in via sysv
2645                                  * rcN.d links. */
2646                                 if (service->meta.fragment_path)
2647                                         continue;
2648
2649                                 if (de->d_name[0] == 'S') {
2650
2651                                         if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
2652                                                 goto finish;
2653
2654                                 } else if (de->d_name[0] == 'K' &&
2655                                            (rcnd_table[i].type == RUNLEVEL_DOWN ||
2656                                             rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
2657
2658                                         /* We honour K links only for
2659                                          * halt/reboot. For the normal
2660                                          * runlevels we assume the
2661                                          * stop jobs will be
2662                                          * implicitly added by the
2663                                          * core logic. Also, we don't
2664                                          * really distuingish here
2665                                          * between the runlevels 0 and
2666                                          * 6 and just add them to the
2667                                          * special shutdown target. On
2668                                          * SUSE the boot.d/ runlevel
2669                                          * is also used for shutdown,
2670                                          * so we add links for that
2671                                          * too to the shutdown
2672                                          * target.*/
2673
2674                                         if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
2675                                                 goto finish;
2676                                 }
2677                         }
2678                 }
2679
2680         r = 0;
2681
2682 finish:
2683         free(path);
2684         free(fpath);
2685         free(name);
2686
2687         if (d)
2688                 closedir(d);
2689
2690         return r;
2691 }
2692
2693 static void service_bus_name_owner_change(
2694                 Unit *u,
2695                 const char *name,
2696                 const char *old_owner,
2697                 const char *new_owner) {
2698
2699         Service *s = SERVICE(u);
2700
2701         assert(s);
2702         assert(name);
2703
2704         assert(streq(s->bus_name, name));
2705         assert(old_owner || new_owner);
2706
2707         if (old_owner && new_owner)
2708                 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner);
2709         else if (old_owner)
2710                 log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner);
2711         else
2712                 log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner);
2713
2714         s->bus_name_good = !!new_owner;
2715
2716         if (s->type == SERVICE_DBUS) {
2717
2718                 /* service_enter_running() will figure out what to
2719                  * do */
2720                 if (s->state == SERVICE_RUNNING)
2721                         service_enter_running(s, true);
2722                 else if (s->state == SERVICE_START && new_owner)
2723                         service_enter_start_post(s);
2724
2725         } else if (new_owner &&
2726                    s->main_pid <= 0 &&
2727                    (s->state == SERVICE_START ||
2728                     s->state == SERVICE_START_POST ||
2729                     s->state == SERVICE_RUNNING ||
2730                     s->state == SERVICE_RELOAD)) {
2731
2732                 /* Try to acquire PID from bus service */
2733                 log_debug("Trying to acquire PID from D-Bus name...");
2734
2735                 bus_query_pid(u->meta.manager, name);
2736         }
2737 }
2738
2739 static void service_bus_query_pid_done(
2740                 Unit *u,
2741                 const char *name,
2742                 pid_t pid) {
2743
2744         Service *s = SERVICE(u);
2745
2746         assert(s);
2747         assert(name);
2748
2749         log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid);
2750
2751         if (s->main_pid <= 0 &&
2752             (s->state == SERVICE_START ||
2753              s->state == SERVICE_START_POST ||
2754              s->state == SERVICE_RUNNING ||
2755              s->state == SERVICE_RELOAD))
2756                 service_set_main_pid(s, pid);
2757 }
2758
2759 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
2760         assert(s);
2761         assert(fd >= 0);
2762
2763         /* This is called by the socket code when instantiating a new
2764          * service for a stream socket and the socket needs to be
2765          * configured. */
2766
2767         if (s->meta.load_state != UNIT_LOADED)
2768                 return -EINVAL;
2769
2770         if (s->socket_fd >= 0)
2771                 return -EBUSY;
2772
2773         if (s->state != SERVICE_DEAD)
2774                 return -EAGAIN;
2775
2776         s->socket_fd = fd;
2777         s->got_socket_fd = true;
2778         s->socket = sock;
2779
2780         return 0;
2781 }
2782
2783 static void service_reset_maintenance(Unit *u) {
2784         Service *s = SERVICE(u);
2785
2786         assert(s);
2787
2788         if (s->state == SERVICE_MAINTENANCE)
2789                 service_set_state(s, SERVICE_DEAD);
2790
2791         s->failure = false;
2792 }
2793
2794 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2795         [SERVICE_DEAD] = "dead",
2796         [SERVICE_START_PRE] = "start-pre",
2797         [SERVICE_START] = "start",
2798         [SERVICE_START_POST] = "start-post",
2799         [SERVICE_RUNNING] = "running",
2800         [SERVICE_EXITED] = "exited",
2801         [SERVICE_RELOAD] = "reload",
2802         [SERVICE_STOP] = "stop",
2803         [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2804         [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2805         [SERVICE_STOP_POST] = "stop-post",
2806         [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2807         [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2808         [SERVICE_MAINTENANCE] = "maintenance",
2809         [SERVICE_AUTO_RESTART] = "auto-restart",
2810 };
2811
2812 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2813
2814 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2815         [SERVICE_ONCE] = "once",
2816         [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
2817         [SERVICE_RESTART_ALWAYS] = "restart-always",
2818 };
2819
2820 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2821
2822 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2823         [SERVICE_SIMPLE] = "simple",
2824         [SERVICE_FORKING] = "forking",
2825         [SERVICE_FINISH] = "finish",
2826         [SERVICE_DBUS] = "dbus",
2827         [SERVICE_NOTIFY] = "notify"
2828 };
2829
2830 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2831
2832 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
2833         [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2834         [SERVICE_EXEC_START] = "ExecStart",
2835         [SERVICE_EXEC_START_POST] = "ExecStartPost",
2836         [SERVICE_EXEC_RELOAD] = "ExecReload",
2837         [SERVICE_EXEC_STOP] = "ExecStop",
2838         [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2839 };
2840
2841 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2842
2843 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
2844         [NOTIFY_NONE] = "none",
2845         [NOTIFY_MAIN] = "main",
2846         [NOTIFY_ALL] = "all"
2847 };
2848
2849 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
2850
2851 const UnitVTable service_vtable = {
2852         .suffix = ".service",
2853         .show_status = true,
2854
2855         .init = service_init,
2856         .done = service_done,
2857         .load = service_load,
2858
2859         .coldplug = service_coldplug,
2860
2861         .dump = service_dump,
2862
2863         .start = service_start,
2864         .stop = service_stop,
2865         .reload = service_reload,
2866
2867         .can_reload = service_can_reload,
2868
2869         .serialize = service_serialize,
2870         .deserialize_item = service_deserialize_item,
2871
2872         .active_state = service_active_state,
2873         .sub_state_to_string = service_sub_state_to_string,
2874
2875         .check_gc = service_check_gc,
2876         .check_snapshot = service_check_snapshot,
2877
2878         .sigchld_event = service_sigchld_event,
2879         .timer_event = service_timer_event,
2880
2881         .reset_maintenance = service_reset_maintenance,
2882
2883         .cgroup_notify_empty = service_cgroup_notify_event,
2884         .notify_message = service_notify_message,
2885
2886         .bus_name_owner_change = service_bus_name_owner_change,
2887         .bus_query_pid_done = service_bus_query_pid_done,
2888
2889         .bus_message_handler = bus_service_message_handler,
2890
2891         .enumerate = service_enumerate
2892 };