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