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