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