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