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