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