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