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