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