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