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