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