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