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