]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/service.c
kmod: autoload unix.ko if it isn't around
[thirdparty/systemd.git] / src / service.c
CommitLineData
5cb5a6ff
LP
1/*-*- Mode: C; c-basic-offset: 8 -*-*/
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"
2c4104f0
LP
35
36#define COMMENTS "#;\n"
37#define NEWLINES "\n\r"
38#define LINE_MAX 4096
034c6ed7 39
09cd1ab1
LP
40typedef enum RunlevelType {
41 RUNLEVEL_UP,
42 RUNLEVEL_DOWN,
fc5df99e 43 RUNLEVEL_SYSINIT
09cd1ab1
LP
44} RunlevelType;
45
46static const struct {
47 const char *path;
48 const char *target;
49 const RunlevelType type;
50} rcnd_table[] = {
fbe9f3a9
LP
51 /* Standard SysV runlevels */
52 { "rc0.d", SPECIAL_RUNLEVEL0_TARGET, RUNLEVEL_DOWN },
53 { "rc1.d", SPECIAL_RUNLEVEL1_TARGET, RUNLEVEL_UP },
54 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP },
55 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP },
56 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP },
57 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP },
58 { "rc6.d", SPECIAL_RUNLEVEL6_TARGET, RUNLEVEL_DOWN },
59
cfe243e3 60 /* SUSE style boot.d */
fc5df99e 61 { "boot.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
fbe9f3a9
LP
62
63 /* Debian style rcS.d */
fc5df99e 64 { "rcS.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
23a177ef
LP
65};
66
09cd1ab1 67#define RUNLEVELS_UP "12345"
fbe9f3a9
LP
68/* #define RUNLEVELS_DOWN "06" */
69/* #define RUNLEVELS_BOOT "bBsS" */
09cd1ab1 70
acbb0225 71static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
87f0e418
LP
72 [SERVICE_DEAD] = UNIT_INACTIVE,
73 [SERVICE_START_PRE] = UNIT_ACTIVATING,
74 [SERVICE_START] = UNIT_ACTIVATING,
75 [SERVICE_START_POST] = UNIT_ACTIVATING,
76 [SERVICE_RUNNING] = UNIT_ACTIVE,
80876c20 77 [SERVICE_EXITED] = UNIT_ACTIVE,
87f0e418
LP
78 [SERVICE_RELOAD] = UNIT_ACTIVE_RELOADING,
79 [SERVICE_STOP] = UNIT_DEACTIVATING,
80 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
81 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
82 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
83 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
84 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
85 [SERVICE_MAINTAINANCE] = UNIT_INACTIVE,
86 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING,
034c6ed7 87};
5cb5a6ff 88
a16e1123
LP
89static void service_init(Unit *u) {
90 Service *s = SERVICE(u);
91
92 assert(u);
93 assert(u->meta.load_state == UNIT_STUB);
94
95 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
96 s->restart_usec = DEFAULT_RESTART_USEC;
97 s->timer_watch.type = WATCH_INVALID;
98 s->sysv_start_priority = -1;
99 s->socket_fd = -1;
100
101 exec_context_init(&s->exec_context);
102
103 RATELIMIT_INIT(s->ratelimit, 10*USEC_PER_SEC, 5);
104
105 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
106}
107
5e94833f
LP
108static void service_unwatch_control_pid(Service *s) {
109 assert(s);
110
111 if (s->control_pid <= 0)
112 return;
113
114 unit_unwatch_pid(UNIT(s), s->control_pid);
115 s->control_pid = 0;
116}
117
118static void service_unwatch_main_pid(Service *s) {
119 assert(s);
120
121 if (s->main_pid <= 0)
122 return;
123
124 unit_unwatch_pid(UNIT(s), s->main_pid);
125 s->main_pid = 0;
126}
127
4f2d528d
LP
128static void service_close_socket_fd(Service *s) {
129 assert(s);
130
131 if (s->socket_fd < 0)
132 return;
133
134 close_nointr_nofail(s->socket_fd);
135 s->socket_fd = -1;
136}
137
87f0e418
LP
138static void service_done(Unit *u) {
139 Service *s = SERVICE(u);
44d8db9e
LP
140
141 assert(s);
142
143 free(s->pid_file);
144 s->pid_file = NULL;
145
2c4104f0
LP
146 free(s->sysv_path);
147 s->sysv_path = NULL;
148
8309400a
LP
149 free(s->sysv_runlevels);
150 s->sysv_runlevels = NULL;
151
44d8db9e 152 exec_context_done(&s->exec_context);
e537352b 153 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
44d8db9e
LP
154 s->control_command = NULL;
155
156 /* This will leak a process, but at least no memory or any of
157 * our resources */
5e94833f
LP
158 service_unwatch_main_pid(s);
159 service_unwatch_control_pid(s);
44d8db9e 160
05e343b7
LP
161 if (s->bus_name) {
162 unit_unwatch_bus_name(UNIT(u), s->bus_name);
163 free(s->bus_name);
164 s->bus_name = NULL;
165 }
166
4f2d528d
LP
167 service_close_socket_fd(s);
168
acbb0225 169 unit_unwatch_timer(u, &s->timer_watch);
44d8db9e
LP
170}
171
b7ccee3c
LP
172static char *sysv_translate_name(const char *name) {
173 char *r;
174
175 if (!(r = new(char, strlen(name) + sizeof(".service"))))
176 return NULL;
177
178 if (startswith(name, "boot."))
179 /* Drop SuSE-style boot. prefix */
180 strcpy(stpcpy(r, name + 5), ".service");
181 else if (endswith(name, ".sh"))
182 /* Drop Debian-style .sh suffix */
183 strcpy(stpcpy(r, name) - 3, ".service");
184 else
185 /* Normal init scripts */
186 strcpy(stpcpy(r, name), ".service");
187
188 return r;
189}
190
191static int sysv_translate_facility(const char *name, char **_r) {
2c4104f0
LP
192
193 static const char * const table[] = {
6464aa08 194 /* LSB defined facilities */
2c4104f0
LP
195 "$local_fs", SPECIAL_LOCAL_FS_TARGET,
196 "$network", SPECIAL_NETWORK_TARGET,
197 "$named", SPECIAL_NSS_LOOKUP_TARGET,
198 "$portmap", SPECIAL_RPCBIND_TARGET,
199 "$remote_fs", SPECIAL_REMOTE_FS_TARGET,
200 "$syslog", SPECIAL_SYSLOG_TARGET,
6464aa08
LP
201 "$time", SPECIAL_RTC_SET_TARGET,
202
203 /* Debian extensions */
204 "$mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
205 "$mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
1e287fe3 206 "$x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE
2c4104f0
LP
207 };
208
209 unsigned i;
210 char *r;
211
212 for (i = 0; i < ELEMENTSOF(table); i += 2)
213 if (streq(table[i], name)) {
214 if (!(r = strdup(table[i+1])))
215 return -ENOMEM;
216
217 goto finish;
218 }
219
220 if (*name == '$')
221 return 0;
222
b7ccee3c 223 if (!(r = sysv_translate_name(name)))
2c4104f0
LP
224 return -ENOMEM;
225
226finish:
227
228 if (_r)
229 *_r = r;
230
231 return 1;
232}
233
56d748b4 234static int sysv_fix_order(Service *s) {
2c4104f0
LP
235 Meta *other;
236 int r;
237
238 assert(s);
239
240 if (s->sysv_start_priority < 0)
241 return 0;
242
23a177ef
LP
243 /* For each pair of services where at least one lacks a LSB
244 * header, we use the start priority value to order things. */
2c4104f0
LP
245
246 LIST_FOREACH(units_per_type, other, UNIT(s)->meta.manager->units_per_type[UNIT_SERVICE]) {
247 Service *t;
248 UnitDependency d;
249
250 t = (Service*) other;
251
252 if (s == t)
253 continue;
254
255 if (t->sysv_start_priority < 0)
256 continue;
257
51a1a79d
LP
258 /* If both units have modern headers we don't care
259 * about the priorities */
260 if ((!s->sysv_path || s->sysv_has_lsb) &&
261 (!t->sysv_path || t->sysv_has_lsb))
23a177ef
LP
262 continue;
263
2c4104f0
LP
264 if (t->sysv_start_priority < s->sysv_start_priority)
265 d = UNIT_AFTER;
266 else if (t->sysv_start_priority > s->sysv_start_priority)
267 d = UNIT_BEFORE;
268 else
269 continue;
270
271 /* FIXME: Maybe we should compare the name here lexicographically? */
272
701cc384 273 if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
2c4104f0
LP
274 return r;
275 }
276
277 return 0;
278}
279
280static ExecCommand *exec_command_new(const char *path, const char *arg1) {
281 ExecCommand *c;
282
283 if (!(c = new0(ExecCommand, 1)))
284 return NULL;
285
286 if (!(c->path = strdup(path))) {
287 free(c);
288 return NULL;
289 }
290
291 if (!(c->argv = strv_new(path, arg1, NULL))) {
292 free(c->path);
293 free(c);
294 return NULL;
295 }
296
297 return c;
298}
299
300static int sysv_exec_commands(Service *s) {
301 ExecCommand *c;
302
303 assert(s);
304 assert(s->sysv_path);
305
306 if (!(c = exec_command_new(s->sysv_path, "start")))
307 return -ENOMEM;
308 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
309
310 if (!(c = exec_command_new(s->sysv_path, "stop")))
311 return -ENOMEM;
312 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
313
314 if (!(c = exec_command_new(s->sysv_path, "reload")))
315 return -ENOMEM;
316 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
317
318 return 0;
319}
320
e537352b 321static int service_load_sysv_path(Service *s, const char *path) {
2c4104f0
LP
322 FILE *f;
323 Unit *u;
324 unsigned line = 0;
325 int r;
326 enum {
327 NORMAL,
328 DESCRIPTION,
329 LSB,
330 LSB_DESCRIPTION
331 } state = NORMAL;
23a177ef
LP
332
333 assert(s);
334 assert(path);
2c4104f0
LP
335
336 u = UNIT(s);
337
338 if (!(f = fopen(path, "re"))) {
339 r = errno == ENOENT ? 0 : -errno;
340 goto finish;
341 }
342
2c4104f0
LP
343 free(s->sysv_path);
344 if (!(s->sysv_path = strdup(path))) {
345 r = -ENOMEM;
346 goto finish;
347 }
348
349 while (!feof(f)) {
350 char l[LINE_MAX], *t;
351
352 if (!fgets(l, sizeof(l), f)) {
353 if (feof(f))
354 break;
355
356 r = -errno;
357 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
358 goto finish;
359 }
360
361 line++;
362
363 t = strstrip(l);
364 if (*t != '#')
365 continue;
366
367 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
368 state = LSB;
23a177ef 369 s->sysv_has_lsb = true;
2c4104f0
LP
370 continue;
371 }
372
373 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
374 state = NORMAL;
375 continue;
376 }
377
378 t++;
379 t += strspn(t, WHITESPACE);
380
381 if (state == NORMAL) {
382
383 /* Try to parse Red Hat style chkconfig headers */
384
c2b35af6 385 if (startswith_no_case(t, "chkconfig:")) {
2c4104f0 386 int start_priority;
8309400a 387 char runlevels[16], *k;
2c4104f0
LP
388
389 state = NORMAL;
390
8309400a
LP
391 if (sscanf(t+10, "%15s %i %*i",
392 runlevels,
393 &start_priority) != 2) {
2c4104f0
LP
394
395 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
396 continue;
397 }
398
fbe9f3a9
LP
399 /* A start priority gathered from the
400 * symlink farms is preferred over the
401 * data from the LSB header. */
8309400a 402 if (start_priority < 0 || start_priority > 99)
2c4104f0 403 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
db06e3b6 404 else if (s->sysv_start_priority < 0)
8309400a
LP
405 s->sysv_start_priority = start_priority;
406
407 char_array_0(runlevels);
408 k = delete_chars(runlevels, WHITESPACE "-");
409
410 if (k[0]) {
411 char *d;
412
413 if (!(d = strdup(k))) {
414 r = -ENOMEM;
415 goto finish;
416 }
417
418 free(s->sysv_runlevels);
419 s->sysv_runlevels = d;
2c4104f0
LP
420 }
421
c2b35af6 422 } else if (startswith_no_case(t, "description:")) {
2c4104f0
LP
423
424 size_t k = strlen(t);
425 char *d;
426
427 if (t[k-1] == '\\') {
428 state = DESCRIPTION;
429 t[k-1] = 0;
430 }
431
432 if (!(d = strdup(strstrip(t+12)))) {
433 r = -ENOMEM;
434 goto finish;
435 }
436
437 free(u->meta.description);
438 u->meta.description = d;
439
c2b35af6 440 } else if (startswith_no_case(t, "pidfile:")) {
2c4104f0
LP
441
442 char *fn;
443
444 state = NORMAL;
445
446 fn = strstrip(t+8);
447 if (!path_is_absolute(fn)) {
448 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
449 continue;
450 }
451
452 if (!(fn = strdup(fn))) {
453 r = -ENOMEM;
454 goto finish;
455 }
456
457 free(s->pid_file);
458 s->pid_file = fn;
459 }
460
461 } else if (state == DESCRIPTION) {
462
463 /* Try to parse Red Hat style description
464 * continuation */
465
466 size_t k = strlen(t);
467 char *d;
468
469 if (t[k-1] == '\\')
470 t[k-1] = 0;
471 else
472 state = NORMAL;
473
474 assert(u->meta.description);
475 if (asprintf(&d, "%s %s", u->meta.description, strstrip(t)) < 0) {
476 r = -ENOMEM;
477 goto finish;
478 }
479
480 free(u->meta.description);
481 u->meta.description = d;
482
483 } else if (state == LSB || state == LSB_DESCRIPTION) {
484
c2b35af6 485 if (startswith_no_case(t, "Provides:")) {
2c4104f0
LP
486 char *i, *w;
487 size_t z;
488
489 state = LSB;
490
491 FOREACH_WORD(w, z, t+9, i) {
492 char *n, *m;
493
494 if (!(n = strndup(w, z))) {
495 r = -ENOMEM;
496 goto finish;
497 }
498
b7ccee3c 499 r = sysv_translate_facility(n, &m);
2c4104f0
LP
500 free(n);
501
502 if (r < 0)
503 goto finish;
504
505 if (r == 0)
506 continue;
507
bd77d0fc
LP
508 if (unit_name_to_type(m) == UNIT_SERVICE)
509 r = unit_add_name(u, m);
510 else {
701cc384
LP
511 if ((r = unit_add_dependency_by_name_inverse(u, UNIT_REQUIRES, m, NULL, true)) >= 0)
512 r = unit_add_dependency_by_name(u, UNIT_BEFORE, m, NULL, true);
bd77d0fc
LP
513 }
514
2c4104f0
LP
515 free(m);
516
517 if (r < 0)
518 goto finish;
519 }
520
c2b35af6
LP
521 } else if (startswith_no_case(t, "Required-Start:") ||
522 startswith_no_case(t, "Should-Start:") ||
523 startswith_no_case(t, "X-Start-Before:") ||
524 startswith_no_case(t, "X-Start-After:")) {
2c4104f0
LP
525 char *i, *w;
526 size_t z;
527
528 state = LSB;
529
530 FOREACH_WORD(w, z, strchr(t, ':')+1, i) {
531 char *n, *m;
532
533 if (!(n = strndup(w, z))) {
534 r = -ENOMEM;
535 goto finish;
536 }
537
b7ccee3c 538 r = sysv_translate_facility(n, &m);
2c4104f0
LP
539 free(n);
540
541 if (r < 0)
542 goto finish;
543
544 if (r == 0)
545 continue;
546
c2b35af6 547 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
2c4104f0
LP
548 free(m);
549
550 if (r < 0)
551 goto finish;
552 }
c2b35af6 553 } else if (startswith_no_case(t, "Default-Start:")) {
8309400a
LP
554 char *k, *d;
555
556 state = LSB;
557
558 k = delete_chars(t+14, WHITESPACE "-");
559
560 if (k[0] != 0) {
561 if (!(d = strdup(k))) {
562 r = -ENOMEM;
563 goto finish;
564 }
565
566 free(s->sysv_runlevels);
567 s->sysv_runlevels = d;
568 }
2c4104f0 569
c2b35af6 570 } else if (startswith_no_case(t, "Description:")) {
2c4104f0
LP
571 char *d;
572
573 state = LSB_DESCRIPTION;
574
575 if (!(d = strdup(strstrip(t+12)))) {
576 r = -ENOMEM;
577 goto finish;
578 }
579
580 free(u->meta.description);
581 u->meta.description = d;
582
c2b35af6 583 } else if (startswith_no_case(t, "Short-Description:") &&
8309400a 584 !u->meta.description) {
2c4104f0
LP
585 char *d;
586
587 /* We use the short description only
588 * if no long description is set. */
589
590 state = LSB;
591
592 if (!(d = strdup(strstrip(t+18)))) {
593 r = -ENOMEM;
594 goto finish;
595 }
596
2c4104f0
LP
597 u->meta.description = d;
598
723c83fd
LP
599 } else if (startswith_no_case(t, "X-Interactive:")) {
600 int b;
601
602 if ((b = parse_boolean(strstrip(t+14))) < 0) {
603 log_warning("[%s:%u] Couldn't parse interactive flag. Ignoring.", path, line);
604 continue;
605 }
606
607 if (b)
608 s->exec_context.std_input = EXEC_INPUT_TTY;
609 else
610 s->exec_context.std_input = EXEC_INPUT_NULL;
611
2c4104f0
LP
612 } else if (state == LSB_DESCRIPTION) {
613
614 if (startswith(l, "#\t") || startswith(l, "# ")) {
615 char *d;
616
617 assert(u->meta.description);
618 if (asprintf(&d, "%s %s", u->meta.description, t) < 0) {
619 r = -ENOMEM;
620 goto finish;
621 }
622
623 free(u->meta.description);
624 u->meta.description = d;
625 } else
626 state = LSB;
627 }
628 }
629 }
630
2c4104f0
LP
631 if ((r = sysv_exec_commands(s)) < 0)
632 goto finish;
633
09cd1ab1 634 if (!s->sysv_runlevels || chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
0bc824be
LP
635 /* If there a runlevels configured for this service
636 * but none of the standard ones, then we assume this
637 * is some special kind of service (which might be
638 * needed for early boot) and don't create any links
639 * to it. */
640
701cc384
LP
641 if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0 ||
642 (r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
0bc824be 643 goto finish;
09cd1ab1
LP
644
645 } else
646 /* Don't timeout special services during boot (like fsck) */
647 s->timeout_usec = 0;
0fd030be 648
80876c20 649 /* Special setting for all SysV services */
1f48cf56 650 s->type = SERVICE_FORKING;
80876c20 651 s->valid_no_process = true;
b29a8e58 652 s->kill_mode = KILL_PROCESS_GROUP;
1f48cf56 653 s->restart = SERVICE_ONCE;
80876c20 654
e537352b 655 u->meta.load_state = UNIT_LOADED;
23a177ef 656 r = 0;
2c4104f0
LP
657
658finish:
659
660 if (f)
661 fclose(f);
662
663 return r;
664}
665
e537352b 666static int service_load_sysv_name(Service *s, const char *name) {
2c4104f0
LP
667 char **p;
668
669 assert(s);
670 assert(name);
671
d017c6ca
LP
672 /* For SysV services we strip the boot. or .sh
673 * prefixes/suffixes. */
674 if (startswith(name, "boot.") ||
675 endswith(name, ".sh.service"))
676 return -ENOENT;
677
84e3543e 678 STRV_FOREACH(p, UNIT(s)->meta.manager->lookup_paths.sysvinit_path) {
2c4104f0
LP
679 char *path;
680 int r;
681
682 if (asprintf(&path, "%s/%s", *p, name) < 0)
683 return -ENOMEM;
684
685 assert(endswith(path, ".service"));
686 path[strlen(path)-8] = 0;
687
e537352b 688 r = service_load_sysv_path(s, path);
fbe9f3a9
LP
689
690 if (r >= 0 && UNIT(s)->meta.load_state == UNIT_STUB) {
d017c6ca 691 /* Try Debian style xxx.sh source'able init scripts */
fbe9f3a9
LP
692 strcat(path, ".sh");
693 r = service_load_sysv_path(s, path);
694 }
695
2c4104f0
LP
696 free(path);
697
fbe9f3a9 698 if (r >= 0 && UNIT(s)->meta.load_state == UNIT_STUB) {
cfe243e3 699 /* Try SUSE style boot.xxx init scripts */
fbe9f3a9
LP
700
701 if (asprintf(&path, "%s/boot.%s", *p, name) < 0)
702 return -ENOMEM;
703
704 path[strlen(path)-8] = 0;
705 r = service_load_sysv_path(s, path);
706 free(path);
707 }
708
23a177ef 709 if (r < 0)
2c4104f0 710 return r;
23a177ef 711
e537352b 712 if ((UNIT(s)->meta.load_state != UNIT_STUB))
23a177ef 713 break;
2c4104f0
LP
714 }
715
716 return 0;
717}
718
e537352b 719static int service_load_sysv(Service *s) {
2c4104f0
LP
720 const char *t;
721 Iterator i;
722 int r;
723
5cb5a6ff
LP
724 assert(s);
725
726 /* Load service data from SysV init scripts, preferably with
727 * LSB headers ... */
728
84e3543e 729 if (strv_isempty(UNIT(s)->meta.manager->lookup_paths.sysvinit_path))
2c4104f0
LP
730 return 0;
731
9e2f7c11 732 if ((t = UNIT(s)->meta.id))
e537352b 733 if ((r = service_load_sysv_name(s, t)) < 0)
2c4104f0
LP
734 return r;
735
e537352b 736 if (UNIT(s)->meta.load_state == UNIT_STUB)
23a177ef 737 SET_FOREACH(t, UNIT(s)->meta.names, i) {
9e2f7c11 738 if (t == UNIT(s)->meta.id)
e537352b
LP
739 continue;
740
741 if ((r == service_load_sysv_name(s, t)) < 0)
23a177ef
LP
742 return r;
743
e537352b 744 if (UNIT(s)->meta.load_state != UNIT_STUB)
23a177ef
LP
745 break;
746 }
2c4104f0
LP
747
748 return 0;
5cb5a6ff
LP
749}
750
05e343b7
LP
751static int service_add_bus_name(Service *s) {
752 char *n;
753 int r;
754
755 assert(s);
756 assert(s->bus_name);
757
758 if (asprintf(&n, "dbus-%s.service", s->bus_name) < 0)
759 return 0;
760
761 r = unit_merge_by_name(UNIT(s), n);
762 free(n);
763
764 return r;
765}
766
243b1432
LP
767static int service_verify(Service *s) {
768 assert(s);
769
770 if (UNIT(s)->meta.load_state != UNIT_LOADED)
771 return 0;
772
773 if (!s->exec_command[SERVICE_EXEC_START]) {
9e2f7c11 774 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s)->meta.id);
243b1432
LP
775 return -EINVAL;
776 }
777
05e343b7
LP
778 if (s->type == SERVICE_DBUS && !s->bus_name) {
779 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->meta.id);
780 return -EINVAL;
781 }
782
243b1432
LP
783 return 0;
784}
785
e537352b
LP
786static int service_load(Unit *u) {
787 int r;
788 Service *s = SERVICE(u);
789
790 assert(s);
1e2e8133 791
5cb5a6ff 792 /* Load a .service file */
e537352b 793 if ((r = unit_load_fragment(u)) < 0)
5cb5a6ff
LP
794 return r;
795
bd77d0fc 796 /* Load a classic init script as a fallback, if we couldn't find anything */
e537352b
LP
797 if (u->meta.load_state == UNIT_STUB)
798 if ((r = service_load_sysv(s)) < 0)
23a177ef 799 return r;
d46de8a1 800
23a177ef 801 /* Still nothing found? Then let's give up */
e537352b 802 if (u->meta.load_state == UNIT_STUB)
23a177ef 803 return -ENOENT;
034c6ed7 804
23a177ef
LP
805 /* We were able to load something, then let's add in the
806 * dropin directories. */
807 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
8e274523 808 return r;
23a177ef
LP
809
810 /* This is a new unit? Then let's add in some extras */
e537352b 811 if (u->meta.load_state == UNIT_LOADED) {
23a177ef
LP
812 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
813 return r;
814
815 if ((r = unit_add_default_cgroup(u)) < 0)
816 return r;
817
56d748b4 818 if ((r = sysv_fix_order(s)) < 0)
23a177ef 819 return r;
05e343b7
LP
820
821 if (s->bus_name) {
822 if ((r = service_add_bus_name(s)) < 0)
823 return r;
824
825 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
826 return r;
827 }
8e274523
LP
828 }
829
243b1432 830 return service_verify(s);
034c6ed7
LP
831}
832
87f0e418 833static void service_dump(Unit *u, FILE *f, const char *prefix) {
5cb5a6ff 834
5cb5a6ff 835 ServiceExecCommand c;
87f0e418 836 Service *s = SERVICE(u);
47be870b
LP
837 const char *prefix2;
838 char *p2;
5cb5a6ff
LP
839
840 assert(s);
841
47be870b
LP
842 p2 = strappend(prefix, "\t");
843 prefix2 = p2 ? p2 : prefix;
44d8db9e 844
5cb5a6ff 845 fprintf(f,
81a2b7ce
LP
846 "%sService State: %s\n"
847 "%sPermissionsStartOnly: %s\n"
8e274523 848 "%sRootDirectoryStartOnly: %s\n"
03d6ab85 849 "%sValidNoProcess: %s\n"
50159e6a 850 "%sKillMode: %s\n"
03d6ab85 851 "%sType: %s\n",
81a2b7ce
LP
852 prefix, service_state_to_string(s->state),
853 prefix, yes_no(s->permissions_start_only),
8e274523 854 prefix, yes_no(s->root_directory_start_only),
03d6ab85 855 prefix, yes_no(s->valid_no_process),
50159e6a 856 prefix, kill_mode_to_string(s->kill_mode),
03d6ab85 857 prefix, service_type_to_string(s->type));
5cb5a6ff 858
70123e68
LP
859 if (s->control_pid > 0)
860 fprintf(f,
861 "%sControl PID: %llu\n",
862 prefix, (unsigned long long) s->control_pid);
863
864 if (s->main_pid > 0)
865 fprintf(f,
866 "%sMain PID: %llu\n",
867 prefix, (unsigned long long) s->main_pid);
868
034c6ed7
LP
869 if (s->pid_file)
870 fprintf(f,
871 "%sPIDFile: %s\n",
872 prefix, s->pid_file);
873
05e343b7
LP
874 if (s->bus_name)
875 fprintf(f,
876 "%sBusName: %s\n"
877 "%sBus Name Good: %s\n",
878 prefix, s->bus_name,
879 prefix, yes_no(s->bus_name_good));
880
5cb5a6ff
LP
881 exec_context_dump(&s->exec_context, f, prefix);
882
e537352b 883 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
5cb5a6ff 884
44d8db9e
LP
885 if (!s->exec_command[c])
886 continue;
887
40d50879 888 fprintf(f, "%s-> %s:\n",
94f04347 889 prefix, service_exec_command_to_string(c));
44d8db9e
LP
890
891 exec_command_dump_list(s->exec_command[c], f, prefix2);
5cb5a6ff 892 }
44d8db9e 893
2c4104f0
LP
894 if (s->sysv_path)
895 fprintf(f,
23a177ef
LP
896 "%sSysV Init Script Path: %s\n"
897 "%sSysV Init Script has LSB Header: %s\n",
898 prefix, s->sysv_path,
899 prefix, yes_no(s->sysv_has_lsb));
2c4104f0
LP
900
901 if (s->sysv_start_priority >= 0)
902 fprintf(f,
23a177ef 903 "%sSysVStartPriority: %i\n",
2c4104f0
LP
904 prefix, s->sysv_start_priority);
905
8309400a
LP
906 if (s->sysv_runlevels)
907 fprintf(f, "%sSysVRunLevels: %s\n",
908 prefix, s->sysv_runlevels);
23a177ef 909
47be870b 910 free(p2);
5cb5a6ff
LP
911}
912
034c6ed7
LP
913static int service_load_pid_file(Service *s) {
914 char *k;
915 unsigned long p;
916 int r;
917
918 assert(s);
919
920 if (s->main_pid_known)
921 return 0;
922
5e94833f
LP
923 assert(s->main_pid <= 0);
924
034c6ed7
LP
925 if (!s->pid_file)
926 return -ENOENT;
927
928 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
929 return r;
930
931 if ((r = safe_atolu(k, &p)) < 0) {
932 free(k);
933 return r;
934 }
935
936 if ((unsigned long) (pid_t) p != p)
937 return -ERANGE;
938
406eaf93
LP
939 if (p <= 1)
940 return -ERANGE;
941
b8c597d5
LP
942 if (kill((pid_t) p, 0) < 0 && errno != EPERM) {
943 log_warning("PID %llu read from file %s does not exist. Your service or init script might be broken.",
944 (unsigned long long) p, s->pid_file);
945 return -ESRCH;
946 }
947
16f6025e
LP
948 if ((r = unit_watch_pid(UNIT(s), (pid_t) p)) < 0)
949 /* FIXME: we need to do something here */
950 return r;
951
b8c597d5 952 s->main_pid = (pid_t) p;
034c6ed7
LP
953 s->main_pid_known = true;
954
955 return 0;
956}
957
3e33402a
LP
958static int service_get_sockets(Service *s, Set **_set) {
959 Set *set;
ceee3d82
LP
960 Iterator i;
961 char *t;
3e33402a 962 int r;
ceee3d82
LP
963
964 assert(s);
3e33402a
LP
965 assert(_set);
966
967 /* Collects all Socket objects that belong to this
968 * service. Note that a service might have multiple sockets
969 * via multiple names. */
970
971 if (!(set = set_new(NULL, NULL)))
972 return -ENOMEM;
ceee3d82
LP
973
974 SET_FOREACH(t, UNIT(s)->meta.names, i) {
975 char *k;
976 Unit *p;
977
978 /* Look for all socket objects that go by any of our
979 * units and collect their fds */
980
3e33402a
LP
981 if (!(k = unit_name_change_suffix(t, ".socket"))) {
982 r = -ENOMEM;
983 goto fail;
984 }
ceee3d82
LP
985
986 p = manager_get_unit(UNIT(s)->meta.manager, k);
987 free(k);
988
8d567588
LP
989 if (!p)
990 continue;
ceee3d82 991
3e33402a
LP
992 if ((r = set_put(set, p)) < 0)
993 goto fail;
ceee3d82
LP
994 }
995
3e33402a
LP
996 *_set = set;
997 return 0;
998
999fail:
1000 set_free(set);
1001 return r;
1002}
1003
e537352b 1004static int service_notify_sockets_dead(Service *s) {
3e33402a
LP
1005 Iterator i;
1006 Set *set;
47be870b 1007 Socket *sock;
3e33402a
LP
1008 int r;
1009
1010 assert(s);
1011
1012 /* Notifies all our sockets when we die */
3e33402a
LP
1013 if ((r = service_get_sockets(s, &set)) < 0)
1014 return r;
1015
47be870b
LP
1016 SET_FOREACH(sock, set, i)
1017 socket_notify_service_dead(sock);
3e33402a
LP
1018
1019 set_free(set);
1020
ceee3d82
LP
1021 return 0;
1022}
1023
034c6ed7
LP
1024static void service_set_state(Service *s, ServiceState state) {
1025 ServiceState old_state;
5cb5a6ff
LP
1026 assert(s);
1027
034c6ed7 1028 old_state = s->state;
5cb5a6ff 1029 s->state = state;
034c6ed7
LP
1030
1031 if (state != SERVICE_START_PRE &&
1032 state != SERVICE_START &&
1033 state != SERVICE_START_POST &&
1034 state != SERVICE_RELOAD &&
1035 state != SERVICE_STOP &&
1036 state != SERVICE_STOP_SIGTERM &&
1037 state != SERVICE_STOP_SIGKILL &&
1038 state != SERVICE_STOP_POST &&
1039 state != SERVICE_FINAL_SIGTERM &&
1040 state != SERVICE_FINAL_SIGKILL &&
1041 state != SERVICE_AUTO_RESTART)
acbb0225 1042 unit_unwatch_timer(UNIT(s), &s->timer_watch);
034c6ed7 1043
7d55e835
LP
1044 if (state != SERVICE_START &&
1045 state != SERVICE_START_POST &&
034c6ed7
LP
1046 state != SERVICE_RUNNING &&
1047 state != SERVICE_RELOAD &&
1048 state != SERVICE_STOP &&
1049 state != SERVICE_STOP_SIGTERM &&
1050 state != SERVICE_STOP_SIGKILL)
5e94833f 1051 service_unwatch_main_pid(s);
034c6ed7
LP
1052
1053 if (state != SERVICE_START_PRE &&
1054 state != SERVICE_START &&
1055 state != SERVICE_START_POST &&
1056 state != SERVICE_RELOAD &&
1057 state != SERVICE_STOP &&
1058 state != SERVICE_STOP_SIGTERM &&
1059 state != SERVICE_STOP_SIGKILL &&
1060 state != SERVICE_STOP_POST &&
1061 state != SERVICE_FINAL_SIGTERM &&
e537352b 1062 state != SERVICE_FINAL_SIGKILL) {
5e94833f 1063 service_unwatch_control_pid(s);
034c6ed7 1064 s->control_command = NULL;
a16e1123 1065 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
e537352b 1066 }
034c6ed7 1067
ceee3d82
LP
1068 if (state == SERVICE_DEAD ||
1069 state == SERVICE_STOP ||
1070 state == SERVICE_STOP_SIGTERM ||
1071 state == SERVICE_STOP_SIGKILL ||
1072 state == SERVICE_STOP_POST ||
1073 state == SERVICE_FINAL_SIGTERM ||
1074 state == SERVICE_FINAL_SIGKILL ||
1075 state == SERVICE_MAINTAINANCE ||
1076 state == SERVICE_AUTO_RESTART)
e537352b 1077 service_notify_sockets_dead(s);
ceee3d82 1078
4f2d528d
LP
1079 if (state != SERVICE_START_PRE &&
1080 state != SERVICE_START &&
1081 !(state == SERVICE_DEAD && UNIT(s)->meta.job))
1082 service_close_socket_fd(s);
1083
e537352b 1084 if (old_state != state)
40d50879 1085 log_debug("%s changed %s -> %s", UNIT(s)->meta.id, service_state_to_string(old_state), service_state_to_string(state));
acbb0225
LP
1086
1087 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
034c6ed7
LP
1088}
1089
a16e1123
LP
1090static int service_coldplug(Unit *u) {
1091 Service *s = SERVICE(u);
1092 int r;
1093
1094 assert(s);
1095 assert(s->state == SERVICE_DEAD);
1096
1097 if (s->deserialized_state != s->state) {
1098
1099 if (s->deserialized_state == SERVICE_START_PRE ||
1100 s->deserialized_state == SERVICE_START ||
1101 s->deserialized_state == SERVICE_START_POST ||
1102 s->deserialized_state == SERVICE_RELOAD ||
1103 s->deserialized_state == SERVICE_STOP ||
1104 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1105 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1106 s->deserialized_state == SERVICE_STOP_POST ||
1107 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1108 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
e558336f
LP
1109 s->deserialized_state == SERVICE_AUTO_RESTART) {
1110
1111 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1112 usec_t k;
1113
1114 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1115
1116 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1117 return r;
1118 }
1119 }
a16e1123
LP
1120
1121 if ((s->deserialized_state == SERVICE_START &&
1122 (s->type == SERVICE_FORKING ||
1123 s->type == SERVICE_DBUS)) ||
1124 s->deserialized_state == SERVICE_START_POST ||
1125 s->deserialized_state == SERVICE_RUNNING ||
1126 s->deserialized_state == SERVICE_RELOAD ||
1127 s->deserialized_state == SERVICE_STOP ||
1128 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1129 s->deserialized_state == SERVICE_STOP_SIGKILL)
1130 if (s->main_pid > 0)
1131 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1132 return r;
1133
1134 if (s->deserialized_state == SERVICE_START_PRE ||
1135 s->deserialized_state == SERVICE_START ||
1136 s->deserialized_state == SERVICE_START_POST ||
1137 s->deserialized_state == SERVICE_RELOAD ||
1138 s->deserialized_state == SERVICE_STOP ||
1139 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1140 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1141 s->deserialized_state == SERVICE_STOP_POST ||
1142 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1143 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1144 if (s->control_pid > 0)
1145 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1146 return r;
1147
1148 service_set_state(s, s->deserialized_state);
1149 }
1150
1151 return 0;
1152}
1153
44d8db9e
LP
1154static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1155 Iterator i;
1156 int r;
1157 int *rfds = NULL;
1158 unsigned rn_fds = 0;
3e33402a 1159 Set *set;
47be870b 1160 Socket *sock;
44d8db9e
LP
1161
1162 assert(s);
1163 assert(fds);
1164 assert(n_fds);
1165
3e33402a
LP
1166 if ((r = service_get_sockets(s, &set)) < 0)
1167 return r;
1168
47be870b 1169 SET_FOREACH(sock, set, i) {
44d8db9e
LP
1170 int *cfds;
1171 unsigned cn_fds;
1172
47be870b 1173 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
44d8db9e
LP
1174 goto fail;
1175
1176 if (!cfds)
1177 continue;
1178
1179 if (!rfds) {
1180 rfds = cfds;
1181 rn_fds = cn_fds;
1182 } else {
1183 int *t;
1184
1185 if (!(t = new(int, rn_fds+cn_fds))) {
1186 free(cfds);
1187 r = -ENOMEM;
1188 goto fail;
1189 }
1190
1191 memcpy(t, rfds, rn_fds);
1192 memcpy(t+rn_fds, cfds, cn_fds);
1193 free(rfds);
1194 free(cfds);
1195
1196 rfds = t;
1197 rn_fds = rn_fds+cn_fds;
1198 }
1199 }
1200
1201 *fds = rfds;
1202 *n_fds = rn_fds;
3e33402a
LP
1203
1204 set_free(set);
1205
44d8db9e
LP
1206 return 0;
1207
1208fail:
3e33402a 1209 set_free(set);
44d8db9e 1210 free(rfds);
3e33402a 1211
44d8db9e
LP
1212 return r;
1213}
1214
81a2b7ce
LP
1215static int service_spawn(
1216 Service *s,
1217 ExecCommand *c,
1218 bool timeout,
1219 bool pass_fds,
1220 bool apply_permissions,
1221 bool apply_chroot,
1222 pid_t *_pid) {
1223
034c6ed7
LP
1224 pid_t pid;
1225 int r;
44d8db9e
LP
1226 int *fds = NULL;
1227 unsigned n_fds = 0;
9e2f7c11 1228 char **argv;
034c6ed7
LP
1229
1230 assert(s);
1231 assert(c);
1232 assert(_pid);
1233
4f2d528d
LP
1234 if (pass_fds) {
1235 if (s->socket_fd >= 0) {
1236 fds = &s->socket_fd;
1237 n_fds = 1;
1238 } else if ((r = service_collect_fds(s, &fds, &n_fds)) < 0)
44d8db9e 1239 goto fail;
4f2d528d 1240 }
44d8db9e 1241
e558336f 1242 if (timeout && s->timeout_usec) {
acbb0225 1243 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
034c6ed7
LP
1244 goto fail;
1245 } else
acbb0225 1246 unit_unwatch_timer(UNIT(s), &s->timer_watch);
034c6ed7 1247
9e2f7c11
LP
1248 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1249 r = -ENOMEM;
1250 goto fail;
1251 }
1252
1253 r = exec_spawn(c,
1254 argv,
1255 &s->exec_context,
1256 fds, n_fds,
1137a57c 1257 s->meta.manager->environment,
9e2f7c11
LP
1258 apply_permissions,
1259 apply_chroot,
1260 UNIT(s)->meta.manager->confirm_spawn,
1261 UNIT(s)->meta.cgroup_bondings,
1262 &pid);
1263
1264 strv_free(argv);
1265 if (r < 0)
034c6ed7
LP
1266 goto fail;
1267
4f2d528d
LP
1268 if (fds) {
1269 if (s->socket_fd >= 0)
1270 service_close_socket_fd(s);
1271 else
1272 free(fds);
1273 }
1274
87f0e418 1275 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
034c6ed7
LP
1276 /* FIXME: we need to do something here */
1277 goto fail;
1278
1279 *_pid = pid;
1280
5cb5a6ff 1281 return 0;
034c6ed7
LP
1282
1283fail:
44d8db9e
LP
1284 free(fds);
1285
034c6ed7 1286 if (timeout)
acbb0225 1287 unit_unwatch_timer(UNIT(s), &s->timer_watch);
034c6ed7
LP
1288
1289 return r;
1290}
1291
80876c20
LP
1292static int main_pid_good(Service *s) {
1293 assert(s);
1294
1295 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1296 * don't know */
1297
1298 /* If we know the pid file, then lets just check if it is
1299 * still valid */
1300 if (s->main_pid_known)
1301 return s->main_pid > 0;
1302
1303 /* We don't know the pid */
1304 return -EAGAIN;
1305}
1306
1307static int control_pid_good(Service *s) {
1308 assert(s);
1309
1310 return s->control_pid > 0;
1311}
1312
1313static int cgroup_good(Service *s) {
1314 int r;
1315
1316 assert(s);
1317
1318 if (s->valid_no_process)
1319 return -EAGAIN;
1320
1321 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->meta.cgroup_bondings)) < 0)
1322 return r;
1323
1324 return !r;
1325}
1326
034c6ed7
LP
1327static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1328 int r;
1329 assert(s);
1330
1331 if (!success)
1332 s->failure = true;
1333
1334 if (allow_restart &&
3a762661 1335 s->allow_restart &&
034c6ed7
LP
1336 (s->restart == SERVICE_RESTART_ALWAYS ||
1337 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1338
acbb0225 1339 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
034c6ed7
LP
1340 goto fail;
1341
1342 service_set_state(s, SERVICE_AUTO_RESTART);
1343 } else
1344 service_set_state(s, s->failure ? SERVICE_MAINTAINANCE : SERVICE_DEAD);
1345
1346 return;
1347
1348fail:
9e2f7c11 1349 log_warning("%s failed to run install restart timer: %s", UNIT(s)->meta.id, strerror(-r));
034c6ed7
LP
1350 service_enter_dead(s, false, false);
1351}
1352
1353static void service_enter_signal(Service *s, ServiceState state, bool success);
1354
1355static void service_enter_stop_post(Service *s, bool success) {
1356 int r;
1357 assert(s);
1358
1359 if (!success)
1360 s->failure = true;
1361
5e94833f
LP
1362 service_unwatch_control_pid(s);
1363
a16e1123 1364 s->control_command_id = SERVICE_EXEC_STOP_POST;
80876c20 1365 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
81a2b7ce
LP
1366 if ((r = service_spawn(s,
1367 s->control_command,
1368 true,
1369 false,
1370 !s->permissions_start_only,
1371 !s->root_directory_start_only,
1372 &s->control_pid)) < 0)
034c6ed7
LP
1373 goto fail;
1374
d6ea93e3 1375
80876c20
LP
1376 service_set_state(s, SERVICE_STOP_POST);
1377 } else
1378 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
034c6ed7
LP
1379
1380 return;
1381
1382fail:
7020e8be 1383 log_warning("%s failed to run 'stop-post' task: %s", UNIT(s)->meta.id, strerror(-r));
034c6ed7
LP
1384 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1385}
1386
1387static void service_enter_signal(Service *s, ServiceState state, bool success) {
1388 int r;
1389 bool sent = false;
1390
1391 assert(s);
1392
1393 if (!success)
1394 s->failure = true;
1395
80876c20
LP
1396 if (s->kill_mode != KILL_NONE) {
1397 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
034c6ed7 1398
50159e6a 1399 if (s->kill_mode == KILL_CONTROL_GROUP) {
034c6ed7 1400
50159e6a
LP
1401 if ((r = cgroup_bonding_kill_list(UNIT(s)->meta.cgroup_bondings, sig)) < 0) {
1402 if (r != -EAGAIN && r != -ESRCH)
1403 goto fail;
1404 } else
034c6ed7
LP
1405 sent = true;
1406 }
1407
50159e6a
LP
1408 if (!sent) {
1409 r = 0;
80876c20 1410
50159e6a
LP
1411 if (s->main_pid > 0) {
1412 if (kill(s->kill_mode == KILL_PROCESS ? s->main_pid : -s->main_pid, sig) < 0 && errno != ESRCH)
1413 r = -errno;
1414 else
1415 sent = true;
1416 }
1417
1418 if (s->control_pid > 0) {
1419 if (kill(s->kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH)
1420 r = -errno;
1421 else
1422 sent = true;
1423 }
1424
1425 if (r < 0)
1426 goto fail;
1427 }
d6ea93e3 1428 }
034c6ed7 1429
e93bc5a6 1430 if (sent && (s->main_pid > 0 || s->control_pid > 0)) {
e558336f
LP
1431 if (s->timeout_usec > 0)
1432 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1433 goto fail;
d6ea93e3 1434
80876c20
LP
1435 service_set_state(s, state);
1436 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1437 service_enter_stop_post(s, true);
1438 else
034c6ed7
LP
1439 service_enter_dead(s, true, true);
1440
1441 return;
1442
1443fail:
9e2f7c11 1444 log_warning("%s failed to kill processes: %s", UNIT(s)->meta.id, strerror(-r));
034c6ed7 1445
80876c20 1446 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
034c6ed7
LP
1447 service_enter_stop_post(s, false);
1448 else
1449 service_enter_dead(s, false, true);
1450}
1451
1452static void service_enter_stop(Service *s, bool success) {
1453 int r;
1454 assert(s);
1455
1456 if (!success)
1457 s->failure = true;
1458
5e94833f
LP
1459 service_unwatch_control_pid(s);
1460
a16e1123 1461 s->control_command_id = SERVICE_EXEC_STOP;
80876c20 1462 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
81a2b7ce
LP
1463 if ((r = service_spawn(s,
1464 s->control_command,
1465 true,
1466 false,
1467 !s->permissions_start_only,
1468 !s->root_directory_start_only,
1469 &s->control_pid)) < 0)
034c6ed7
LP
1470 goto fail;
1471
80876c20
LP
1472 service_set_state(s, SERVICE_STOP);
1473 } else
034c6ed7
LP
1474 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1475
1476 return;
1477
1478fail:
7020e8be 1479 log_warning("%s failed to run 'stop' task: %s", UNIT(s)->meta.id, strerror(-r));
034c6ed7
LP
1480 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1481}
1482
80876c20
LP
1483static void service_enter_running(Service *s, bool success) {
1484 assert(s);
1485
1486 if (!success)
1487 s->failure = true;
1488
05e343b7
LP
1489 if (main_pid_good(s) != 0 &&
1490 cgroup_good(s) != 0 &&
1491 (s->bus_name_good || s->type != SERVICE_DBUS))
80876c20
LP
1492 service_set_state(s, SERVICE_RUNNING);
1493 else if (s->valid_no_process)
1494 service_set_state(s, SERVICE_EXITED);
1495 else
1496 service_enter_stop(s, true);
1497}
1498
034c6ed7
LP
1499static void service_enter_start_post(Service *s) {
1500 int r;
1501 assert(s);
1502
5e94833f
LP
1503 service_unwatch_control_pid(s);
1504
a16e1123 1505 s->control_command_id = SERVICE_EXEC_START_POST;
80876c20 1506 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
81a2b7ce
LP
1507 if ((r = service_spawn(s,
1508 s->control_command,
1509 true,
1510 false,
1511 !s->permissions_start_only,
1512 !s->root_directory_start_only,
1513 &s->control_pid)) < 0)
034c6ed7
LP
1514 goto fail;
1515
d6ea93e3 1516
80876c20
LP
1517 service_set_state(s, SERVICE_START_POST);
1518 } else
1519 service_enter_running(s, true);
034c6ed7
LP
1520
1521 return;
1522
1523fail:
7020e8be 1524 log_warning("%s failed to run 'start-post' task: %s", UNIT(s)->meta.id, strerror(-r));
034c6ed7
LP
1525 service_enter_stop(s, false);
1526}
1527
1528static void service_enter_start(Service *s) {
1529 pid_t pid;
1530 int r;
1531
1532 assert(s);
1533
1534 assert(s->exec_command[SERVICE_EXEC_START]);
1535 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
1536
80876c20
LP
1537 if (s->type == SERVICE_FORKING)
1538 service_unwatch_control_pid(s);
1539 else
1540 service_unwatch_main_pid(s);
1541
81a2b7ce
LP
1542 if ((r = service_spawn(s,
1543 s->exec_command[SERVICE_EXEC_START],
05e343b7 1544 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS,
81a2b7ce
LP
1545 true,
1546 true,
1547 true,
1548 &pid)) < 0)
034c6ed7
LP
1549 goto fail;
1550
1551 if (s->type == SERVICE_SIMPLE) {
1552 /* For simple services we immediately start
1553 * the START_POST binaries. */
1554
1555 s->main_pid = pid;
1556 s->main_pid_known = true;
80876c20 1557
034c6ed7
LP
1558 service_enter_start_post(s);
1559
1560 } else if (s->type == SERVICE_FORKING) {
1561
1562 /* For forking services we wait until the start
1563 * process exited. */
1564
1565 s->control_pid = pid;
80876c20 1566
a16e1123 1567 s->control_command_id = SERVICE_EXEC_START;
034c6ed7 1568 s->control_command = s->exec_command[SERVICE_EXEC_START];
80876c20
LP
1569 service_set_state(s, SERVICE_START);
1570
05e343b7
LP
1571 } else if (s->type == SERVICE_FINISH ||
1572 s->type == SERVICE_DBUS) {
7d55e835
LP
1573
1574 /* For finishing services we wait until the start
1575 * process exited, too, but it is our main process. */
1576
05e343b7
LP
1577 /* For D-Bus services we know the main pid right away,
1578 * but wait for the bus name to appear on the bus. */
1579
7d55e835 1580 s->main_pid = pid;
5e94833f 1581 s->main_pid_known = true;
80876c20 1582
80876c20 1583 service_set_state(s, SERVICE_START);
034c6ed7
LP
1584 } else
1585 assert_not_reached("Unknown service type");
1586
1587 return;
1588
1589fail:
7020e8be 1590 log_warning("%s failed to run 'start' task: %s", UNIT(s)->meta.id, strerror(-r));
80876c20 1591 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
034c6ed7
LP
1592}
1593
1594static void service_enter_start_pre(Service *s) {
1595 int r;
1596
1597 assert(s);
1598
5e94833f
LP
1599 service_unwatch_control_pid(s);
1600
a16e1123 1601 s->control_command_id = SERVICE_EXEC_START_PRE;
80876c20 1602 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
81a2b7ce
LP
1603 if ((r = service_spawn(s,
1604 s->control_command,
1605 true,
1606 false,
1607 !s->permissions_start_only,
1608 !s->root_directory_start_only,
1609 &s->control_pid)) < 0)
034c6ed7
LP
1610 goto fail;
1611
80876c20
LP
1612 service_set_state(s, SERVICE_START_PRE);
1613 } else
034c6ed7
LP
1614 service_enter_start(s);
1615
1616 return;
1617
1618fail:
7020e8be 1619 log_warning("%s failed to run 'start-pre' task: %s", UNIT(s)->meta.id, strerror(-r));
034c6ed7
LP
1620 service_enter_dead(s, false, true);
1621}
1622
1623static void service_enter_restart(Service *s) {
1624 int r;
1625 assert(s);
1626
9ea9a0c8
LP
1627 service_enter_dead(s, true, false);
1628
87f0e418 1629 if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
034c6ed7
LP
1630 goto fail;
1631
9e2f7c11 1632 log_debug("%s scheduled restart job.", UNIT(s)->meta.id);
034c6ed7
LP
1633 return;
1634
1635fail:
1636
9e2f7c11 1637 log_warning("%s failed to schedule restart job: %s", UNIT(s)->meta.id, strerror(-r));
034c6ed7
LP
1638 service_enter_dead(s, false, false);
1639}
1640
1641static void service_enter_reload(Service *s) {
1642 int r;
1643
1644 assert(s);
1645
5e94833f
LP
1646 service_unwatch_control_pid(s);
1647
a16e1123 1648 s->control_command_id = SERVICE_EXEC_RELOAD;
80876c20 1649 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
81a2b7ce
LP
1650 if ((r = service_spawn(s,
1651 s->control_command,
1652 true,
1653 false,
1654 !s->permissions_start_only,
1655 !s->root_directory_start_only,
1656 &s->control_pid)) < 0)
034c6ed7
LP
1657 goto fail;
1658
80876c20
LP
1659 service_set_state(s, SERVICE_RELOAD);
1660 } else
1661 service_enter_running(s, true);
034c6ed7
LP
1662
1663 return;
1664
1665fail:
7020e8be 1666 log_warning("%s failed to run 'reload' task: %s", UNIT(s)->meta.id, strerror(-r));
034c6ed7
LP
1667 service_enter_stop(s, false);
1668}
1669
1670static void service_run_next(Service *s, bool success) {
1671 int r;
1672
1673 assert(s);
1674 assert(s->control_command);
1675 assert(s->control_command->command_next);
1676
1677 if (!success)
1678 s->failure = true;
1679
1680 s->control_command = s->control_command->command_next;
1681
5e94833f
LP
1682 service_unwatch_control_pid(s);
1683
81a2b7ce
LP
1684 if ((r = service_spawn(s,
1685 s->control_command,
1686 true,
1687 false,
1688 !s->permissions_start_only,
1689 !s->root_directory_start_only,
1690 &s->control_pid)) < 0)
034c6ed7
LP
1691 goto fail;
1692
1693 return;
1694
1695fail:
7020e8be 1696 log_warning("%s failed to run spawn next task: %s", UNIT(s)->meta.id, strerror(-r));
034c6ed7 1697
80876c20
LP
1698 if (s->state == SERVICE_START_PRE)
1699 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1700 else if (s->state == SERVICE_STOP)
1701 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
034c6ed7
LP
1702 else if (s->state == SERVICE_STOP_POST)
1703 service_enter_dead(s, false, true);
1704 else
1705 service_enter_stop(s, false);
5cb5a6ff
LP
1706}
1707
87f0e418
LP
1708static int service_start(Unit *u) {
1709 Service *s = SERVICE(u);
5cb5a6ff
LP
1710
1711 assert(s);
1712
034c6ed7
LP
1713 /* We cannot fulfill this request right now, try again later
1714 * please! */
1715 if (s->state == SERVICE_STOP ||
1716 s->state == SERVICE_STOP_SIGTERM ||
1717 s->state == SERVICE_STOP_SIGKILL ||
1718 s->state == SERVICE_STOP_POST ||
1719 s->state == SERVICE_FINAL_SIGTERM ||
1720 s->state == SERVICE_FINAL_SIGKILL)
5cb5a6ff
LP
1721 return -EAGAIN;
1722
034c6ed7
LP
1723 /* Already on it! */
1724 if (s->state == SERVICE_START_PRE ||
1725 s->state == SERVICE_START ||
1726 s->state == SERVICE_START_POST)
1727 return 0;
1728
1729 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
5cb5a6ff 1730
1e2e8133
LP
1731 /* Make sure we don't enter a busy loop of some kind. */
1732 if (!ratelimit_test(&s->ratelimit)) {
9e2f7c11 1733 log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id);
d5159713 1734 return -ECANCELED;
1e2e8133
LP
1735 }
1736
034c6ed7
LP
1737 s->failure = false;
1738 s->main_pid_known = false;
3a762661 1739 s->allow_restart = true;
034c6ed7
LP
1740
1741 service_enter_start_pre(s);
1742 return 0;
5cb5a6ff
LP
1743}
1744
87f0e418
LP
1745static int service_stop(Unit *u) {
1746 Service *s = SERVICE(u);
5cb5a6ff
LP
1747
1748 assert(s);
1749
e537352b 1750 /* Cannot do this now */
034c6ed7
LP
1751 if (s->state == SERVICE_START_PRE ||
1752 s->state == SERVICE_START ||
1753 s->state == SERVICE_START_POST ||
1754 s->state == SERVICE_RELOAD)
1755 return -EAGAIN;
1756
e537352b
LP
1757 /* Already on it */
1758 if (s->state == SERVICE_STOP ||
1759 s->state == SERVICE_STOP_SIGTERM ||
1760 s->state == SERVICE_STOP_SIGKILL ||
1761 s->state == SERVICE_STOP_POST ||
1762 s->state == SERVICE_FINAL_SIGTERM ||
1763 s->state == SERVICE_FINAL_SIGKILL)
1764 return 0;
1765
034c6ed7
LP
1766 if (s->state == SERVICE_AUTO_RESTART) {
1767 service_set_state(s, SERVICE_DEAD);
1768 return 0;
1769 }
1770
80876c20 1771 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
5cb5a6ff 1772
3a762661
LP
1773 /* This is a user request, so don't do restarts on this
1774 * shutdown. */
1775 s->allow_restart = false;
1776
034c6ed7 1777 service_enter_stop(s, true);
5cb5a6ff
LP
1778 return 0;
1779}
1780
87f0e418
LP
1781static int service_reload(Unit *u) {
1782 Service *s = SERVICE(u);
034c6ed7
LP
1783
1784 assert(s);
1785
80876c20 1786 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
034c6ed7
LP
1787
1788 service_enter_reload(s);
5cb5a6ff
LP
1789 return 0;
1790}
1791
87f0e418
LP
1792static bool service_can_reload(Unit *u) {
1793 Service *s = SERVICE(u);
034c6ed7
LP
1794
1795 assert(s);
1796
1797 return !!s->exec_command[SERVICE_EXEC_RELOAD];
1798}
1799
a16e1123
LP
1800static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
1801 Service *s = SERVICE(u);
1802
1803 assert(u);
1804 assert(f);
1805 assert(fds);
1806
1807 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
1808 unit_serialize_item(u, f, "failure", yes_no(s->failure));
1809
1810 if (s->control_pid > 0)
1811 unit_serialize_item_format(u, f, "control-pid", "%u", (unsigned) (s->control_pid));
1812
1813 if (s->main_pid > 0)
1814 unit_serialize_item_format(u, f, "main-pid", "%u", (unsigned) (s->main_pid));
1815
1816 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
1817
1818 /* There's a minor uncleanliness here: if there are multiple
1819 * commands attached here, we will start from the first one
1820 * again */
1821 if (s->control_command_id >= 0)
825636e5 1822 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
a16e1123
LP
1823
1824 if (s->socket_fd >= 0) {
1825 int copy;
1826
1827 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
1828 return copy;
1829
1830 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
1831 }
1832
1833 return 0;
1834}
1835
1836static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1837 Service *s = SERVICE(u);
1838 int r;
1839
1840 assert(u);
1841 assert(key);
1842 assert(value);
1843 assert(fds);
1844
1845 if (streq(key, "state")) {
1846 ServiceState state;
1847
1848 if ((state = service_state_from_string(value)) < 0)
1849 log_debug("Failed to parse state value %s", value);
1850 else
1851 s->deserialized_state = state;
1852 } else if (streq(key, "failure")) {
1853 int b;
1854
1855 if ((b = parse_boolean(value)) < 0)
1856 log_debug("Failed to parse failure value %s", value);
1857 else
1858 s->failure = b || s->failure;
1859 } else if (streq(key, "control-pid")) {
1860 unsigned pid;
1861
1862 if ((r = safe_atou(value, &pid)) < 0 || pid <= 0)
1863 log_debug("Failed to parse control-pid value %s", value);
1864 else
1865 s->control_pid = (pid_t) pid;
1866 } else if (streq(key, "main-pid")) {
1867 unsigned pid;
1868
1869 if ((r = safe_atou(value, &pid)) < 0 || pid <= 0)
1870 log_debug("Failed to parse main-pid value %s", value);
1871 else
1872 s->main_pid = (pid_t) pid;
1873 } else if (streq(key, "main-pid-known")) {
1874 int b;
1875
1876 if ((b = parse_boolean(value)) < 0)
1877 log_debug("Failed to parse main-pid-known value %s", value);
1878 else
1879 s->main_pid_known = b;
1880 } else if (streq(key, "control-command")) {
1881 ServiceExecCommand id;
1882
1883 if ((id = service_exec_command_from_string(value)) < 0)
1884 log_debug("Failed to parse exec-command value %s", value);
1885 else {
1886 s->control_command_id = id;
1887 s->control_command = s->exec_command[id];
1888 }
1889 } else if (streq(key, "socket-fd")) {
1890 int fd;
1891
1892 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
1893 log_debug("Failed to parse socket-fd value %s", value);
1894 else {
1895
1896 if (s->socket_fd >= 0)
1897 close_nointr_nofail(s->socket_fd);
1898 s->socket_fd = fdset_remove(fds, fd);
1899 }
1900 } else
1901 log_debug("Unknown serialization key '%s'", key);
1902
1903 return 0;
1904}
1905
87f0e418
LP
1906static UnitActiveState service_active_state(Unit *u) {
1907 assert(u);
5cb5a6ff 1908
acbb0225 1909 return state_translation_table[SERVICE(u)->state];
034c6ed7
LP
1910}
1911
10a94420
LP
1912static const char *service_sub_state_to_string(Unit *u) {
1913 assert(u);
1914
1915 return service_state_to_string(SERVICE(u)->state);
1916}
1917
701cc384
LP
1918static bool service_check_gc(Unit *u) {
1919 Service *s = SERVICE(u);
1920
1921 assert(s);
1922
1923 return !!s->sysv_path;
1924}
1925
1926static bool service_check_snapshot(Unit *u) {
1927 Service *s = SERVICE(u);
1928
1929 assert(s);
1930
1931 return !s->got_socket_fd;
1932}
1933
87f0e418
LP
1934static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1935 Service *s = SERVICE(u);
034c6ed7 1936 bool success;
5cb5a6ff
LP
1937
1938 assert(s);
034c6ed7
LP
1939 assert(pid >= 0);
1940
cb8a8f78 1941 success = is_clean_exit(code, status);
034c6ed7
LP
1942 s->failure = s->failure || !success;
1943
1944 if (s->main_pid == pid) {
1945
1946 exec_status_fill(&s->main_exec_status, pid, code, status);
1947 s->main_pid = 0;
1948
7d55e835 1949 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_FINISH) {
034c6ed7
LP
1950 assert(s->exec_command[SERVICE_EXEC_START]);
1951 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
1952 }
1953
9e2f7c11 1954 log_debug("%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
034c6ed7
LP
1955
1956 /* The service exited, so the service is officially
1957 * gone. */
1958
1959 switch (s->state) {
1960
1961 case SERVICE_START_POST:
1962 case SERVICE_RELOAD:
1963 case SERVICE_STOP:
1964 /* Need to wait until the operation is
1965 * done */
1966 break;
1967
7d55e835 1968 case SERVICE_START:
c4653a4d
LP
1969 if (s->type == SERVICE_FINISH) {
1970 /* This was our main goal, so let's go on */
1971 if (success)
1972 service_enter_start_post(s);
1973 else
1974 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1975 break;
1976 } else {
1977 assert(s->type == SERVICE_DBUS);
7d55e835 1978
c4653a4d
LP
1979 /* Fall through */
1980 }
7d55e835 1981
034c6ed7 1982 case SERVICE_RUNNING:
80876c20 1983 service_enter_running(s, success);
034c6ed7
LP
1984 break;
1985
1986 case SERVICE_STOP_SIGTERM:
1987 case SERVICE_STOP_SIGKILL:
1988
1989 if (!control_pid_good(s))
1990 service_enter_stop_post(s, success);
5cb5a6ff 1991
034c6ed7
LP
1992 /* If there is still a control process, wait for that first */
1993 break;
5cb5a6ff 1994
034c6ed7
LP
1995 default:
1996 assert_not_reached("Uh, main process died at wrong time.");
1997 }
5cb5a6ff 1998
034c6ed7 1999 } else if (s->control_pid == pid) {
034c6ed7 2000
a16e1123
LP
2001 if (s->control_command)
2002 exec_status_fill(&s->control_command->exec_status, pid, code, status);
2003
034c6ed7
LP
2004 s->control_pid = 0;
2005
9e2f7c11 2006 log_debug("%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
034c6ed7
LP
2007
2008 /* If we are shutting things down anyway we
2009 * don't care about failing commands. */
2010
a16e1123 2011 if (s->control_command && s->control_command->command_next && success) {
034c6ed7
LP
2012
2013 /* There is another command to *
2014 * execute, so let's do that. */
2015
9e2f7c11 2016 log_debug("%s running next command for state %s", u->meta.id, service_state_to_string(s->state));
034c6ed7
LP
2017 service_run_next(s, success);
2018
80876c20 2019 } else {
034c6ed7
LP
2020 /* No further commands for this step, so let's
2021 * figure out what to do next */
2022
a16e1123
LP
2023 s->control_command = NULL;
2024 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2025
9e2f7c11 2026 log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state));
bd982a8b 2027
034c6ed7
LP
2028 switch (s->state) {
2029
2030 case SERVICE_START_PRE:
2031 if (success)
2032 service_enter_start(s);
2033 else
80876c20 2034 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
034c6ed7
LP
2035 break;
2036
2037 case SERVICE_START:
2038 assert(s->type == SERVICE_FORKING);
2039
2040 /* Let's try to load the pid
2041 * file here if we can. We
2042 * ignore the return value,
2043 * since the PID file might
2044 * actually be created by a
2045 * START_POST script */
2046
2047 if (success) {
2048 if (s->pid_file)
2049 service_load_pid_file(s);
2050
2051 service_enter_start_post(s);
2052 } else
80876c20 2053 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
034c6ed7
LP
2054
2055 break;
2056
2057 case SERVICE_START_POST:
2058 if (success && s->pid_file && !s->main_pid_known) {
2059 int r;
2060
2061 /* Hmm, let's see if we can
2062 * load the pid now after the
2063 * start-post scripts got
2064 * executed. */
2065
2066 if ((r = service_load_pid_file(s)) < 0)
9e2f7c11 2067 log_warning("%s: failed to load PID file %s: %s", UNIT(s)->meta.id, s->pid_file, strerror(-r));
034c6ed7
LP
2068 }
2069
2070 /* Fall through */
2071
2072 case SERVICE_RELOAD:
80876c20
LP
2073 if (success)
2074 service_enter_running(s, true);
2075 else
034c6ed7
LP
2076 service_enter_stop(s, false);
2077
2078 break;
2079
2080 case SERVICE_STOP:
80876c20 2081 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
034c6ed7
LP
2082 break;
2083
2084 case SERVICE_STOP_SIGTERM:
2085 case SERVICE_STOP_SIGKILL:
2086 if (main_pid_good(s) <= 0)
2087 service_enter_stop_post(s, success);
2088
2089 /* If there is still a service
2090 * process around, wait until
2091 * that one quit, too */
2092 break;
2093
2094 case SERVICE_STOP_POST:
2095 case SERVICE_FINAL_SIGTERM:
2096 case SERVICE_FINAL_SIGKILL:
2097 service_enter_dead(s, success, true);
2098 break;
2099
2100 default:
2101 assert_not_reached("Uh, control process died at wrong time.");
2102 }
2103 }
2104 } else
2105 assert_not_reached("Got SIGCHLD for unkown PID");
2106}
2107
acbb0225 2108static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
87f0e418 2109 Service *s = SERVICE(u);
034c6ed7
LP
2110
2111 assert(s);
2112 assert(elapsed == 1);
2113
acbb0225 2114 assert(w == &s->timer_watch);
034c6ed7
LP
2115
2116 switch (s->state) {
2117
2118 case SERVICE_START_PRE:
2119 case SERVICE_START:
9e2f7c11 2120 log_warning("%s operation timed out. Terminating.", u->meta.id);
80876c20
LP
2121 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2122 break;
2123
034c6ed7
LP
2124 case SERVICE_START_POST:
2125 case SERVICE_RELOAD:
9e2f7c11 2126 log_warning("%s operation timed out. Stopping.", u->meta.id);
034c6ed7
LP
2127 service_enter_stop(s, false);
2128 break;
2129
2130 case SERVICE_STOP:
9e2f7c11 2131 log_warning("%s stopping timed out. Terminating.", u->meta.id);
034c6ed7
LP
2132 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2133 break;
2134
2135 case SERVICE_STOP_SIGTERM:
9e2f7c11 2136 log_warning("%s stopping timed out. Killing.", u->meta.id);
034c6ed7
LP
2137 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
2138 break;
2139
2140 case SERVICE_STOP_SIGKILL:
2141 /* Uh, wie sent a SIGKILL and it is still not gone?
2142 * Must be something we cannot kill, so let's just be
2143 * weirded out and continue */
2144
9e2f7c11 2145 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
034c6ed7
LP
2146 service_enter_stop_post(s, false);
2147 break;
2148
2149 case SERVICE_STOP_POST:
9e2f7c11 2150 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
034c6ed7
LP
2151 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2152 break;
2153
2154 case SERVICE_FINAL_SIGTERM:
9e2f7c11 2155 log_warning("%s stopping timed out (2). Killing.", u->meta.id);
034c6ed7
LP
2156 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
2157 break;
2158
2159 case SERVICE_FINAL_SIGKILL:
9e2f7c11 2160 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", u->meta.id);
034c6ed7
LP
2161 service_enter_dead(s, false, true);
2162 break;
2163
2164 case SERVICE_AUTO_RESTART:
9e2f7c11 2165 log_debug("%s holdoff time over, scheduling restart.", u->meta.id);
034c6ed7
LP
2166 service_enter_restart(s);
2167 break;
2168
2169 default:
2170 assert_not_reached("Timeout at wrong time.");
2171 }
5cb5a6ff
LP
2172}
2173
8e274523
LP
2174static void service_cgroup_notify_event(Unit *u) {
2175 Service *s = SERVICE(u);
2176
2177 assert(u);
2178
9e2f7c11 2179 log_debug("%s: cgroup is empty", u->meta.id);
8e274523
LP
2180
2181 switch (s->state) {
2182
2183 /* Waiting for SIGCHLD is usually more interesting,
2184 * because it includes return codes/signals. Which is
2185 * why we ignore the cgroup events for most cases,
2186 * except when we don't know pid which to expect the
2187 * SIGCHLD for. */
2188
2189 case SERVICE_RUNNING:
80876c20 2190 service_enter_running(s, true);
8e274523
LP
2191 break;
2192
2193 default:
2194 ;
2195 }
2196}
2197
2c4104f0 2198static int service_enumerate(Manager *m) {
2c4104f0
LP
2199 char **p;
2200 unsigned i;
2201 DIR *d = NULL;
2202 char *path = NULL, *fpath = NULL, *name = NULL;
2203 int r;
2204
2205 assert(m);
2206
84e3543e 2207 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
09cd1ab1 2208 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
2c4104f0
LP
2209 struct dirent *de;
2210
2211 free(path);
2212 path = NULL;
09cd1ab1 2213 if (asprintf(&path, "%s/%s", *p, rcnd_table[i].path) < 0) {
2c4104f0
LP
2214 r = -ENOMEM;
2215 goto finish;
2216 }
2217
2218 if (d)
2219 closedir(d);
2220
2221 if (!(d = opendir(path))) {
2222 if (errno != ENOENT)
2223 log_warning("opendir() failed on %s: %s", path, strerror(errno));
2224
2225 continue;
2226 }
2227
2228 while ((de = readdir(d))) {
6542952f 2229 Unit *service;
db06e3b6 2230 int a, b;
2c4104f0
LP
2231
2232 if (ignore_file(de->d_name))
2233 continue;
2234
2235 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
2236 continue;
2237
2238 if (strlen(de->d_name) < 4)
2239 continue;
2240
db06e3b6
LP
2241 a = undecchar(de->d_name[1]);
2242 b = undecchar(de->d_name[2]);
2243
2244 if (a < 0 || b < 0)
2245 continue;
2246
2c4104f0
LP
2247 free(fpath);
2248 fpath = NULL;
09cd1ab1 2249 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i].path, de->d_name) < 0) {
2c4104f0
LP
2250 r = -ENOMEM;
2251 goto finish;
2252 }
2253
2254 if (access(fpath, X_OK) < 0) {
2255
2256 if (errno != ENOENT)
2257 log_warning("access() failed on %s: %s", fpath, strerror(errno));
2258
2259 continue;
2260 }
2261
2262 free(name);
b7ccee3c 2263 if (!(name = sysv_translate_name(de->d_name + 3))) {
2c4104f0
LP
2264 r = -ENOMEM;
2265 goto finish;
2266 }
2267
fbe9f3a9
LP
2268 if ((r = manager_load_unit_prepare(m, name, NULL, &service)) < 0) {
2269 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
2270 continue;
2271 }
2c4104f0 2272
09cd1ab1 2273 if (de->d_name[0] == 'S' &&
fc5df99e 2274 (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT))
fbe9f3a9
LP
2275 SERVICE(service)->sysv_start_priority =
2276 MAX(a*10 + b, SERVICE(service)->sysv_start_priority);
db06e3b6
LP
2277
2278 manager_dispatch_load_queue(m);
2279 service = unit_follow_merge(service);
2280
2c4104f0 2281 if (de->d_name[0] == 'S') {
6542952f
LP
2282 Unit *runlevel_target;
2283
09cd1ab1 2284 if ((r = manager_load_unit(m, rcnd_table[i].target, NULL, &runlevel_target)) < 0)
6542952f
LP
2285 goto finish;
2286
2287 if ((r = unit_add_dependency(runlevel_target, UNIT_WANTS, service, true)) < 0)
2c4104f0
LP
2288 goto finish;
2289
a0fcc5f6 2290 if ((r = unit_add_dependency(service, UNIT_BEFORE, runlevel_target, true)) < 0)
2c4104f0 2291 goto finish;
23a177ef 2292
fc5df99e
LP
2293 } else if (de->d_name[0] == 'K' &&
2294 (rcnd_table[i].type == RUNLEVEL_DOWN ||
2295 rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
6542952f
LP
2296 Unit *shutdown_target;
2297
23a177ef
LP
2298 /* We honour K links only for
2299 * halt/reboot. For the normal
2300 * runlevels we assume the
2301 * stop jobs will be
2302 * implicitly added by the
6542952f
LP
2303 * core logic. Also, we don't
2304 * really distuingish here
2305 * between the runlevels 0 and
2306 * 6 and just add them to the
fc5df99e
LP
2307 * special shutdown target. On
2308 * SUSE the boot.d/ runlevel
2309 * is also used for shutdown,
2310 * so we add links for that
2311 * too to the shutdown
2312 * target.*/
6542952f
LP
2313
2314 if ((r = manager_load_unit(m, SPECIAL_SHUTDOWN_TARGET, NULL, &shutdown_target)) < 0)
2315 goto finish;
23a177ef 2316
a0fcc5f6 2317 if ((r = unit_add_dependency(service, UNIT_CONFLICTS, shutdown_target, true)) < 0)
2c4104f0
LP
2318 goto finish;
2319
a0fcc5f6 2320 if ((r = unit_add_dependency(service, UNIT_BEFORE, shutdown_target, true)) < 0)
2c4104f0
LP
2321 goto finish;
2322 }
2323 }
2324 }
2325
2326 r = 0;
2327
2328finish:
2329 free(path);
2330 free(fpath);
2331 free(name);
fbe9f3a9
LP
2332
2333 if (d)
2334 closedir(d);
2c4104f0
LP
2335
2336 return r;
2337}
2338
05e343b7
LP
2339static void service_bus_name_owner_change(
2340 Unit *u,
2341 const char *name,
2342 const char *old_owner,
2343 const char *new_owner) {
2344
2345 Service *s = SERVICE(u);
2346
2347 assert(s);
2348 assert(name);
2349
2350 assert(streq(s->bus_name, name));
2351 assert(old_owner || new_owner);
2352
2353 if (old_owner && new_owner)
2354 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner);
2355 else if (old_owner)
2356 log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner);
2357 else
2358 log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner);
2359
2360 s->bus_name_good = !!new_owner;
2361
2362 if (s->type == SERVICE_DBUS) {
2363
2364 /* service_enter_running() will figure out what to
2365 * do */
2366 if (s->state == SERVICE_RUNNING)
2367 service_enter_running(s, true);
2368 else if (s->state == SERVICE_START && new_owner)
2369 service_enter_start_post(s);
2370
2371 } else if (new_owner &&
2372 s->main_pid <= 0 &&
2373 (s->state == SERVICE_START ||
2374 s->state == SERVICE_START_POST ||
2375 s->state == SERVICE_RUNNING ||
2376 s->state == SERVICE_RELOAD)) {
2377
2378 /* Try to acquire PID from bus service */
2379 log_debug("Trying to acquire PID from D-Bus name...");
2380
2381 bus_query_pid(u->meta.manager, name);
2382 }
2383}
2384
2385static void service_bus_query_pid_done(
2386 Unit *u,
2387 const char *name,
2388 pid_t pid) {
2389
2390 Service *s = SERVICE(u);
2391
2392 assert(s);
2393 assert(name);
2394
2395 log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid);
2396
2397 if (s->main_pid <= 0 &&
2398 (s->state == SERVICE_START ||
2399 s->state == SERVICE_START_POST ||
2400 s->state == SERVICE_RUNNING ||
2401 s->state == SERVICE_RELOAD))
2402 s->main_pid = pid;
2403}
2404
4f2d528d
LP
2405int service_set_socket_fd(Service *s, int fd) {
2406 assert(s);
2407 assert(fd >= 0);
2408
2409 /* This is called by the socket code when instantiating a new
2410 * service for a stream socket and the socket needs to be
2411 * configured. */
2412
2413 if (UNIT(s)->meta.load_state != UNIT_LOADED)
2414 return -EINVAL;
2415
2416 if (s->socket_fd >= 0)
2417 return -EBUSY;
2418
2419 if (s->state != SERVICE_DEAD)
2420 return -EAGAIN;
2421
2422 s->socket_fd = fd;
701cc384 2423 s->got_socket_fd = true;
4f2d528d
LP
2424 return 0;
2425}
2426
94f04347
LP
2427static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2428 [SERVICE_DEAD] = "dead",
2429 [SERVICE_START_PRE] = "start-pre",
2430 [SERVICE_START] = "start",
2431 [SERVICE_START_POST] = "start-post",
2432 [SERVICE_RUNNING] = "running",
80876c20 2433 [SERVICE_EXITED] = "exited",
94f04347
LP
2434 [SERVICE_RELOAD] = "reload",
2435 [SERVICE_STOP] = "stop",
2436 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2437 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2438 [SERVICE_STOP_POST] = "stop-post",
2439 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2440 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2441 [SERVICE_MAINTAINANCE] = "maintainance",
2442 [SERVICE_AUTO_RESTART] = "auto-restart",
2443};
2444
2445DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2446
2447static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2448 [SERVICE_ONCE] = "once",
2449 [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
40531a55 2450 [SERVICE_RESTART_ALWAYS] = "restart-always",
94f04347
LP
2451};
2452
2453DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2454
2455static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2456 [SERVICE_FORKING] = "forking",
2457 [SERVICE_SIMPLE] = "simple",
05e343b7
LP
2458 [SERVICE_FINISH] = "finish",
2459 [SERVICE_DBUS] = "dbus"
94f04347
LP
2460};
2461
2462DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2463
e537352b 2464static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
94f04347
LP
2465 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2466 [SERVICE_EXEC_START] = "ExecStart",
2467 [SERVICE_EXEC_START_POST] = "ExecStartPost",
2468 [SERVICE_EXEC_RELOAD] = "ExecReload",
2469 [SERVICE_EXEC_STOP] = "ExecStop",
2470 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2471};
2472
2473DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2474
87f0e418 2475const UnitVTable service_vtable = {
5cb5a6ff
LP
2476 .suffix = ".service",
2477
034c6ed7
LP
2478 .init = service_init,
2479 .done = service_done,
a16e1123
LP
2480 .load = service_load,
2481
2482 .coldplug = service_coldplug,
034c6ed7 2483
5cb5a6ff
LP
2484 .dump = service_dump,
2485
2486 .start = service_start,
2487 .stop = service_stop,
2488 .reload = service_reload,
2489
034c6ed7
LP
2490 .can_reload = service_can_reload,
2491
a16e1123
LP
2492 .serialize = service_serialize,
2493 .deserialize_item = service_deserialize_item,
2494
5cb5a6ff 2495 .active_state = service_active_state,
10a94420 2496 .sub_state_to_string = service_sub_state_to_string,
5cb5a6ff 2497
701cc384
LP
2498 .check_gc = service_check_gc,
2499 .check_snapshot = service_check_snapshot,
2500
034c6ed7
LP
2501 .sigchld_event = service_sigchld_event,
2502 .timer_event = service_timer_event,
2c4104f0 2503
8e274523
LP
2504 .cgroup_notify_empty = service_cgroup_notify_event,
2505
05e343b7
LP
2506 .bus_name_owner_change = service_bus_name_owner_change,
2507 .bus_query_pid_done = service_bus_query_pid_done,
2508
4139c1b2
LP
2509 .bus_message_handler = bus_service_message_handler,
2510
2c4104f0 2511 .enumerate = service_enumerate
5cb5a6ff 2512};