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