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