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