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