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