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