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