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