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