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