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