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