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