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