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