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