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