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