]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/service.c
bootchart items
[thirdparty/systemd.git] / src / core / service.c
CommitLineData
d6c9574f 1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
5cb5a6ff 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
5430f7f2
LP
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
a7334b09
LP
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
5430f7f2 16 Lesser General Public License for more details.
a7334b09 17
5430f7f2 18 You should have received a copy of the GNU Lesser General Public License
a7334b09
LP
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>
4b939747 26#include <sys/reboot.h>
5cb5a6ff 27
4b939747 28#include "manager.h"
87f0e418 29#include "unit.h"
5cb5a6ff
LP
30#include "service.h"
31#include "load-fragment.h"
32#include "load-dropin.h"
034c6ed7 33#include "log.h"
2c4104f0 34#include "strv.h"
9e2f7c11 35#include "unit-name.h"
41f9172f 36#include "unit-printf.h"
4139c1b2 37#include "dbus-service.h"
514f4ef5 38#include "special.h"
398ef8ba 39#include "bus-errors.h"
9a57c629 40#include "exit-status.h"
f6a6225e 41#include "def.h"
9eb977db 42#include "path-util.h"
f6a6225e 43#include "util.h"
7f110ff9 44#include "utf8.h"
4d1a6904 45#include "env-util.h"
034c6ed7 46
07459bb6 47#ifdef HAVE_SYSV_COMPAT
f34277d9 48
f6a6225e 49#define DEFAULT_SYSV_TIMEOUT_USEC (5*USEC_PER_MINUTE)
f34277d9 50
09cd1ab1
LP
51typedef enum RunlevelType {
52 RUNLEVEL_UP,
3cdebc21 53 RUNLEVEL_DOWN
09cd1ab1
LP
54} RunlevelType;
55
56static const struct {
57 const char *path;
58 const char *target;
59 const RunlevelType type;
60} rcnd_table[] = {
9d25f5ed 61 /* Standard SysV runlevels for start-up */
514f4ef5 62 { "rc1.d", SPECIAL_RESCUE_TARGET, RUNLEVEL_UP },
fbe9f3a9
LP
63 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP },
64 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP },
65 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP },
66 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP },
fbe9f3a9 67
9d25f5ed
LP
68 /* Standard SysV runlevels for shutdown */
69 { "rc0.d", SPECIAL_POWEROFF_TARGET, RUNLEVEL_DOWN },
70 { "rc6.d", SPECIAL_REBOOT_TARGET, RUNLEVEL_DOWN }
71
72 /* Note that the order here matters, as we read the
73 directories in this order, and we want to make sure that
74 sysv_start_priority is known when we first load the
75 unit. And that value we only know from S links. Hence
3cdebc21 76 UP must be read before DOWN */
23a177ef
LP
77};
78
09cd1ab1 79#define RUNLEVELS_UP "12345"
07459bb6 80#endif
09cd1ab1 81
acbb0225 82static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
87f0e418
LP
83 [SERVICE_DEAD] = UNIT_INACTIVE,
84 [SERVICE_START_PRE] = UNIT_ACTIVATING,
85 [SERVICE_START] = UNIT_ACTIVATING,
86 [SERVICE_START_POST] = UNIT_ACTIVATING,
87 [SERVICE_RUNNING] = UNIT_ACTIVE,
80876c20 88 [SERVICE_EXITED] = UNIT_ACTIVE,
032ff4af 89 [SERVICE_RELOAD] = UNIT_RELOADING,
87f0e418
LP
90 [SERVICE_STOP] = UNIT_DEACTIVATING,
91 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
92 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
93 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
94 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
95 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
fdf20a31 96 [SERVICE_FAILED] = UNIT_FAILED,
6124958c 97 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
034c6ed7 98};
5cb5a6ff 99
e056b01d
LP
100/* For Type=idle we never want to delay any other jobs, hence we
101 * consider idle jobs active as soon as we start working on them */
102static const UnitActiveState state_translation_table_idle[_SERVICE_STATE_MAX] = {
103 [SERVICE_DEAD] = UNIT_INACTIVE,
104 [SERVICE_START_PRE] = UNIT_ACTIVE,
105 [SERVICE_START] = UNIT_ACTIVE,
106 [SERVICE_START_POST] = UNIT_ACTIVE,
107 [SERVICE_RUNNING] = UNIT_ACTIVE,
108 [SERVICE_EXITED] = UNIT_ACTIVE,
109 [SERVICE_RELOAD] = UNIT_RELOADING,
110 [SERVICE_STOP] = UNIT_DEACTIVATING,
111 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
112 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
113 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
114 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
115 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
116 [SERVICE_FAILED] = UNIT_FAILED,
117 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
118};
119
a16e1123
LP
120static void service_init(Unit *u) {
121 Service *s = SERVICE(u);
122
123 assert(u);
ac155bb8 124 assert(u->load_state == UNIT_STUB);
a16e1123 125
d568a335
MS
126 s->timeout_start_usec = DEFAULT_TIMEOUT_USEC;
127 s->timeout_stop_usec = DEFAULT_TIMEOUT_USEC;
a16e1123 128 s->restart_usec = DEFAULT_RESTART_USEC;
0b86feac 129 s->type = _SERVICE_TYPE_INVALID;
bb242b7b 130
68b29a9f
LP
131 watch_init(&s->watchdog_watch);
132 watch_init(&s->timer_watch);
bb242b7b 133
07459bb6 134#ifdef HAVE_SYSV_COMPAT
a16e1123 135 s->sysv_start_priority = -1;
ea87ca5a 136 s->sysv_start_priority_from_rcnd = -1;
07459bb6 137#endif
a16e1123 138 s->socket_fd = -1;
3185a36b 139 s->guess_main_pid = true;
a16e1123
LP
140
141 exec_context_init(&s->exec_context);
4819ff03 142 kill_context_init(&s->kill_context);
a16e1123 143
4b939747 144 RATELIMIT_INIT(s->start_limit, 10*USEC_PER_SEC, 5);
a16e1123
LP
145
146 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
147}
148
5e94833f
LP
149static void service_unwatch_control_pid(Service *s) {
150 assert(s);
151
152 if (s->control_pid <= 0)
153 return;
154
155 unit_unwatch_pid(UNIT(s), s->control_pid);
156 s->control_pid = 0;
157}
158
159static void service_unwatch_main_pid(Service *s) {
160 assert(s);
161
162 if (s->main_pid <= 0)
163 return;
164
165 unit_unwatch_pid(UNIT(s), s->main_pid);
166 s->main_pid = 0;
167}
168
3e52541e
MS
169static void service_unwatch_pid_file(Service *s) {
170 if (!s->pid_file_pathspec)
171 return;
172
66870f90
ZJS
173 log_debug_unit(UNIT(s)->id, "Stopping watch for %s's PID file %s",
174 UNIT(s)->id, s->pid_file_pathspec->path);
3e52541e
MS
175 path_spec_unwatch(s->pid_file_pathspec, UNIT(s));
176 path_spec_done(s->pid_file_pathspec);
177 free(s->pid_file_pathspec);
178 s->pid_file_pathspec = NULL;
179}
180
5925dd3c 181static int service_set_main_pid(Service *s, pid_t pid) {
e55224ca
LP
182 pid_t ppid;
183
5925dd3c
LP
184 assert(s);
185
186 if (pid <= 1)
187 return -EINVAL;
188
189 if (pid == getpid())
190 return -EINVAL;
191
6dfa5494
LP
192 s->main_pid = pid;
193 s->main_pid_known = true;
194
195 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) {
66870f90
ZJS
196 log_warning_unit(UNIT(s)->id,
197 "%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
198 UNIT(s)->id, (unsigned long) pid);
e55224ca 199
6dfa5494
LP
200 s->main_pid_alien = true;
201 } else
202 s->main_pid_alien = false;
5925dd3c 203
b58b4116
LP
204 exec_status_start(&s->main_exec_status, pid);
205
5925dd3c
LP
206 return 0;
207}
208
4f2d528d
LP
209static void service_close_socket_fd(Service *s) {
210 assert(s);
211
212 if (s->socket_fd < 0)
213 return;
214
215 close_nointr_nofail(s->socket_fd);
216 s->socket_fd = -1;
217}
218
6cf6bbc2
LP
219static void service_connection_unref(Service *s) {
220 assert(s);
221
57020a3a 222 if (!UNIT_DEREF(s->accept_socket))
6cf6bbc2
LP
223 return;
224
57020a3a
LP
225 socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket)));
226 unit_ref_unset(&s->accept_socket);
6cf6bbc2
LP
227}
228
a6927d7f
MO
229static void service_stop_watchdog(Service *s) {
230 assert(s);
231
bb242b7b 232 unit_unwatch_timer(UNIT(s), &s->watchdog_watch);
a6927d7f
MO
233 s->watchdog_timestamp.realtime = 0;
234 s->watchdog_timestamp.monotonic = 0;
235}
236
bb242b7b
MO
237static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart);
238
239static void service_handle_watchdog(Service *s) {
240 usec_t offset;
241 int r;
242
243 assert(s);
244
245 if (s->watchdog_usec == 0)
246 return;
247
248 offset = now(CLOCK_MONOTONIC) - s->watchdog_timestamp.monotonic;
249 if (offset >= s->watchdog_usec) {
66870f90 250 log_error_unit(UNIT(s)->id, "%s watchdog timeout!", UNIT(s)->id);
bb242b7b
MO
251 service_enter_dead(s, SERVICE_FAILURE_WATCHDOG, true);
252 return;
253 }
254
36697dc0 255 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->watchdog_usec - offset, &s->watchdog_watch);
bb242b7b 256 if (r < 0)
66870f90
ZJS
257 log_warning_unit(UNIT(s)->id,
258 "%s failed to install watchdog timer: %s",
259 UNIT(s)->id, strerror(-r));
bb242b7b
MO
260}
261
a6927d7f
MO
262static void service_reset_watchdog(Service *s) {
263 assert(s);
264
265 dual_timestamp_get(&s->watchdog_timestamp);
bb242b7b 266 service_handle_watchdog(s);
a6927d7f
MO
267}
268
87f0e418
LP
269static void service_done(Unit *u) {
270 Service *s = SERVICE(u);
44d8db9e
LP
271
272 assert(s);
273
274 free(s->pid_file);
275 s->pid_file = NULL;
276
07459bb6 277#ifdef HAVE_SYSV_COMPAT
8309400a
LP
278 free(s->sysv_runlevels);
279 s->sysv_runlevels = NULL;
07459bb6 280#endif
8309400a 281
8c47c732
LP
282 free(s->status_text);
283 s->status_text = NULL;
284
44d8db9e 285 exec_context_done(&s->exec_context);
e537352b 286 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
44d8db9e 287 s->control_command = NULL;
867b3b7d 288 s->main_command = NULL;
44d8db9e 289
96342de6
LN
290 set_free(s->restart_ignore_status.code);
291 s->restart_ignore_status.code = NULL;
292 set_free(s->restart_ignore_status.signal);
293 s->restart_ignore_status.signal = NULL;
294
295 set_free(s->success_status.code);
296 s->success_status.code = NULL;
297 set_free(s->success_status.signal);
298 s->success_status.signal = NULL;
299
44d8db9e
LP
300 /* This will leak a process, but at least no memory or any of
301 * our resources */
5e94833f
LP
302 service_unwatch_main_pid(s);
303 service_unwatch_control_pid(s);
3e52541e 304 service_unwatch_pid_file(s);
44d8db9e 305
05e343b7 306 if (s->bus_name) {
ac155bb8 307 unit_unwatch_bus_name(u, s->bus_name);
05e343b7
LP
308 free(s->bus_name);
309 s->bus_name = NULL;
310 }
311
4f2d528d 312 service_close_socket_fd(s);
6cf6bbc2 313 service_connection_unref(s);
4f2d528d 314
57020a3a 315 unit_ref_unset(&s->accept_socket);
f976f3f6 316
bb242b7b
MO
317 service_stop_watchdog(s);
318
acbb0225 319 unit_unwatch_timer(u, &s->timer_watch);
44d8db9e
LP
320}
321
07459bb6 322#ifdef HAVE_SYSV_COMPAT
b7ccee3c
LP
323static char *sysv_translate_name(const char *name) {
324 char *r;
325
5d4caf56
LP
326 r = new(char, strlen(name) + sizeof(".service"));
327 if (!r)
b7ccee3c
LP
328 return NULL;
329
5b819198 330 if (endswith(name, ".sh"))
85a3fa0e 331 /* Drop .sh suffix */
5b819198 332 strcpy(stpcpy(r, name) - 3, ".service");
5b819198 333 if (startswith(name, "rc."))
85a3fa0e 334 /* Drop rc. prefix */
65530632 335 strcpy(stpcpy(r, name + 3), ".service");
b7ccee3c 336 else
85a3fa0e 337 /* Normal init script name */
b7ccee3c
LP
338 strcpy(stpcpy(r, name), ".service");
339
340 return r;
341}
342
ee95669f 343static int sysv_translate_facility(const char *name, const char *filename, char **_r) {
2c4104f0 344
a7d3cc26
LP
345 /* We silently ignore the $ prefix here. According to the LSB
346 * spec it simply indicates whether something is a
347 * standardized name or a distribution-specific one. Since we
348 * just follow what already exists and do not introduce new
349 * uses or names we don't care who introduced a new name. */
350
2c4104f0 351 static const char * const table[] = {
6464aa08 352 /* LSB defined facilities */
5d4caf56 353 "local_fs", NULL,
a7d3cc26
LP
354 "network", SPECIAL_NETWORK_TARGET,
355 "named", SPECIAL_NSS_LOOKUP_TARGET,
356 "portmap", SPECIAL_RPCBIND_TARGET,
357 "remote_fs", SPECIAL_REMOTE_FS_TARGET,
5d4caf56 358 "syslog", NULL,
4466194c 359 "time", SPECIAL_TIME_SYNC_TARGET,
2c4104f0
LP
360 };
361
362 unsigned i;
363 char *r;
ee95669f 364 const char *n;
2c4104f0 365
ee95669f
LP
366 assert(name);
367 assert(_r);
a7d3cc26 368
ee95669f 369 n = *name == '$' ? name + 1 : name;
a7d3cc26 370
ee95669f 371 for (i = 0; i < ELEMENTSOF(table); i += 2) {
a7d3cc26 372
ee95669f
LP
373 if (!streq(table[i], n))
374 continue;
2c4104f0 375
ee95669f
LP
376 if (!table[i+1])
377 return 0;
378
5d4caf56
LP
379 r = strdup(table[i+1]);
380 if (!r)
381 return log_oom();
ee95669f
LP
382
383 goto finish;
384 }
2c4104f0 385
a7d3cc26 386 /* If we don't know this name, fallback heuristics to figure
0003d1ab 387 * out whether something is a target or a service alias. */
a7d3cc26 388
e83c7c0b
LP
389 if (*name == '$') {
390 if (!unit_prefix_is_valid(n))
391 return -EINVAL;
392
ee95669f
LP
393 /* Facilities starting with $ are most likely targets */
394 r = unit_name_build(n, NULL, ".target");
e83c7c0b 395 } else if (filename && streq(name, filename))
35b8ca3a 396 /* Names equaling the file name of the services are redundant */
ee95669f 397 return 0;
32159d3a 398 else
ee95669f
LP
399 /* Everything else we assume to be normal service names */
400 r = sysv_translate_name(n);
2c4104f0 401
32159d3a 402 if (!r)
2c4104f0
LP
403 return -ENOMEM;
404
405finish:
b4353094 406 *_r = r;
2c4104f0
LP
407
408 return 1;
409}
410
56d748b4 411static int sysv_fix_order(Service *s) {
ac155bb8 412 Unit *other;
2c4104f0
LP
413 int r;
414
415 assert(s);
416
417 if (s->sysv_start_priority < 0)
418 return 0;
419
23a177ef
LP
420 /* For each pair of services where at least one lacks a LSB
421 * header, we use the start priority value to order things. */
2c4104f0 422
1124fe6f 423 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
2c4104f0
LP
424 Service *t;
425 UnitDependency d;
eeaafddc 426 bool special_s, special_t;
2c4104f0 427
595ed347 428 t = SERVICE(other);
2c4104f0
LP
429
430 if (s == t)
431 continue;
9f151f29 432
1124fe6f 433 if (UNIT(t)->load_state != UNIT_LOADED)
9f151f29 434 continue;
2c4104f0
LP
435
436 if (t->sysv_start_priority < 0)
437 continue;
438
51a1a79d
LP
439 /* If both units have modern headers we don't care
440 * about the priorities */
1124fe6f
MS
441 if ((UNIT(s)->fragment_path || s->sysv_has_lsb) &&
442 (UNIT(t)->fragment_path || t->sysv_has_lsb))
23a177ef
LP
443 continue;
444
eeaafddc
LP
445 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
446 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
447
448 if (special_t && !special_s)
449 d = UNIT_AFTER;
450 else if (special_s && !special_t)
451 d = UNIT_BEFORE;
452 else if (t->sysv_start_priority < s->sysv_start_priority)
2c4104f0
LP
453 d = UNIT_AFTER;
454 else if (t->sysv_start_priority > s->sysv_start_priority)
455 d = UNIT_BEFORE;
456 else
457 continue;
458
459 /* FIXME: Maybe we should compare the name here lexicographically? */
460
da19d5c1 461 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
2c4104f0
LP
462 return r;
463 }
464
465 return 0;
466}
467
468static ExecCommand *exec_command_new(const char *path, const char *arg1) {
469 ExecCommand *c;
470
471 if (!(c = new0(ExecCommand, 1)))
472 return NULL;
473
474 if (!(c->path = strdup(path))) {
475 free(c);
476 return NULL;
477 }
478
479 if (!(c->argv = strv_new(path, arg1, NULL))) {
480 free(c->path);
481 free(c);
482 return NULL;
483 }
484
485 return c;
486}
487
37e2941d 488static int sysv_exec_commands(Service *s, const bool supports_reload) {
2c4104f0
LP
489 ExecCommand *c;
490
491 assert(s);
1b64d026
LP
492 assert(s->is_sysv);
493 assert(UNIT(s)->source_path);
2c4104f0 494
1b64d026
LP
495 c = exec_command_new(UNIT(s)->source_path, "start");
496 if (!c)
2c4104f0
LP
497 return -ENOMEM;
498 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
499
1b64d026
LP
500 c = exec_command_new(UNIT(s)->source_path, "stop");
501 if (!c)
2c4104f0
LP
502 return -ENOMEM;
503 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
504
37e2941d
MS
505 if (supports_reload) {
506 c = exec_command_new(UNIT(s)->source_path, "reload");
507 if (!c)
508 return -ENOMEM;
509 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
510 }
2c4104f0
LP
511
512 return 0;
513}
514
37e2941d
MS
515static bool usage_contains_reload(const char *line) {
516 return (strcasestr(line, "{reload|") ||
517 strcasestr(line, "{reload}") ||
518 strcasestr(line, "{reload\"") ||
519 strcasestr(line, "|reload|") ||
520 strcasestr(line, "|reload}") ||
521 strcasestr(line, "|reload\""));
522}
523
e537352b 524static int service_load_sysv_path(Service *s, const char *path) {
2c4104f0
LP
525 FILE *f;
526 Unit *u;
527 unsigned line = 0;
528 int r;
529 enum {
530 NORMAL,
531 DESCRIPTION,
532 LSB,
37e2941d
MS
533 LSB_DESCRIPTION,
534 USAGE_CONTINUATION
2c4104f0 535 } state = NORMAL;
0b5d26f9 536 char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description;
5f4b19f4 537 struct stat st;
37e2941d 538 bool supports_reload = false;
23a177ef
LP
539
540 assert(s);
541 assert(path);
2c4104f0
LP
542
543 u = UNIT(s);
544
1b64d026
LP
545 f = fopen(path, "re");
546 if (!f) {
2c4104f0
LP
547 r = errno == ENOENT ? 0 : -errno;
548 goto finish;
549 }
550
5f4b19f4
LP
551 if (fstat(fileno(f), &st) < 0) {
552 r = -errno;
553 goto finish;
554 }
555
1b64d026
LP
556 free(u->source_path);
557 u->source_path = strdup(path);
558 if (!u->source_path) {
2c4104f0
LP
559 r = -ENOMEM;
560 goto finish;
561 }
1b64d026 562 u->source_mtime = timespec_load(&st.st_mtim);
5f4b19f4
LP
563
564 if (null_or_empty(&st)) {
ac155bb8 565 u->load_state = UNIT_MASKED;
5f4b19f4
LP
566 r = 0;
567 goto finish;
568 }
569
1b64d026
LP
570 s->is_sysv = true;
571
2c4104f0
LP
572 while (!feof(f)) {
573 char l[LINE_MAX], *t;
574
575 if (!fgets(l, sizeof(l), f)) {
576 if (feof(f))
577 break;
578
579 r = -errno;
66870f90
ZJS
580 log_error_unit(u->id,
581 "Failed to read configuration file '%s': %s",
582 path, strerror(-r));
2c4104f0
LP
583 goto finish;
584 }
585
586 line++;
587
588 t = strstrip(l);
37e2941d
MS
589 if (*t != '#') {
590 /* Try to figure out whether this init script supports
591 * the reload operation. This heuristic looks for
592 * "Usage" lines which include the reload option. */
593 if ( state == USAGE_CONTINUATION ||
594 (state == NORMAL && strcasestr(t, "usage"))) {
595 if (usage_contains_reload(t)) {
596 supports_reload = true;
597 state = NORMAL;
598 } else if (t[strlen(t)-1] == '\\')
599 state = USAGE_CONTINUATION;
600 else
601 state = NORMAL;
602 }
603
2c4104f0 604 continue;
37e2941d 605 }
2c4104f0
LP
606
607 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
608 state = LSB;
23a177ef 609 s->sysv_has_lsb = true;
2c4104f0
LP
610 continue;
611 }
612
613 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
614 state = NORMAL;
615 continue;
616 }
617
618 t++;
619 t += strspn(t, WHITESPACE);
620
621 if (state == NORMAL) {
622
623 /* Try to parse Red Hat style chkconfig headers */
624
c2b35af6 625 if (startswith_no_case(t, "chkconfig:")) {
2c4104f0 626 int start_priority;
8309400a 627 char runlevels[16], *k;
2c4104f0
LP
628
629 state = NORMAL;
630
8309400a
LP
631 if (sscanf(t+10, "%15s %i %*i",
632 runlevels,
633 &start_priority) != 2) {
2c4104f0 634
66870f90
ZJS
635 log_warning_unit(u->id,
636 "[%s:%u] Failed to parse chkconfig line. Ignoring.",
637 path, line);
2c4104f0
LP
638 continue;
639 }
640
fbe9f3a9
LP
641 /* A start priority gathered from the
642 * symlink farms is preferred over the
643 * data from the LSB header. */
8309400a 644 if (start_priority < 0 || start_priority > 99)
66870f90
ZJS
645 log_warning_unit(u->id,
646 "[%s:%u] Start priority out of range. Ignoring.",
647 path, line);
ea87ca5a 648 else
8309400a
LP
649 s->sysv_start_priority = start_priority;
650
651 char_array_0(runlevels);
652 k = delete_chars(runlevels, WHITESPACE "-");
653
654 if (k[0]) {
655 char *d;
656
657 if (!(d = strdup(k))) {
658 r = -ENOMEM;
659 goto finish;
660 }
661
662 free(s->sysv_runlevels);
663 s->sysv_runlevels = d;
2c4104f0
LP
664 }
665
0b5d26f9 666 } else if (startswith_no_case(t, "description:")) {
2c4104f0
LP
667
668 size_t k = strlen(t);
669 char *d;
0b5d26f9 670 const char *j;
2c4104f0
LP
671
672 if (t[k-1] == '\\') {
673 state = DESCRIPTION;
674 t[k-1] = 0;
675 }
676
0b5d26f9
LP
677 if ((j = strstrip(t+12)) && *j) {
678 if (!(d = strdup(j))) {
679 r = -ENOMEM;
680 goto finish;
681 }
682 } else
683 d = NULL;
2c4104f0 684
0b5d26f9
LP
685 free(chkconfig_description);
686 chkconfig_description = d;
2c4104f0 687
c2b35af6 688 } else if (startswith_no_case(t, "pidfile:")) {
2c4104f0
LP
689
690 char *fn;
691
692 state = NORMAL;
693
694 fn = strstrip(t+8);
695 if (!path_is_absolute(fn)) {
66870f90
ZJS
696 log_warning_unit(u->id,
697 "[%s:%u] PID file not absolute. Ignoring.",
698 path, line);
2c4104f0
LP
699 continue;
700 }
701
702 if (!(fn = strdup(fn))) {
703 r = -ENOMEM;
704 goto finish;
705 }
706
707 free(s->pid_file);
708 s->pid_file = fn;
709 }
710
711 } else if (state == DESCRIPTION) {
712
713 /* Try to parse Red Hat style description
714 * continuation */
715
716 size_t k = strlen(t);
0b5d26f9 717 char *j;
2c4104f0
LP
718
719 if (t[k-1] == '\\')
720 t[k-1] = 0;
721 else
722 state = NORMAL;
723
0b5d26f9
LP
724 if ((j = strstrip(t)) && *j) {
725 char *d = NULL;
726
727 if (chkconfig_description)
b7def684 728 d = strjoin(chkconfig_description, " ", j, NULL);
0b5d26f9
LP
729 else
730 d = strdup(j);
2c4104f0 731
0b5d26f9
LP
732 if (!d) {
733 r = -ENOMEM;
734 goto finish;
735 }
736
737 free(chkconfig_description);
738 chkconfig_description = d;
739 }
2c4104f0
LP
740
741 } else if (state == LSB || state == LSB_DESCRIPTION) {
742
c2b35af6 743 if (startswith_no_case(t, "Provides:")) {
2c4104f0
LP
744 char *i, *w;
745 size_t z;
746
747 state = LSB;
748
f60f22df 749 FOREACH_WORD_QUOTED(w, z, t+9, i) {
2c4104f0
LP
750 char *n, *m;
751
752 if (!(n = strndup(w, z))) {
753 r = -ENOMEM;
754 goto finish;
755 }
756
9eb977db 757 r = sysv_translate_facility(n, path_get_file_name(path), &m);
2c4104f0
LP
758 free(n);
759
760 if (r < 0)
761 goto finish;
762
763 if (r == 0)
764 continue;
765
bd77d0fc
LP
766 if (unit_name_to_type(m) == UNIT_SERVICE)
767 r = unit_add_name(u, m);
9700edb4
LP
768 else
769 /* NB: SysV targets
770 * which are provided
771 * by a service are
772 * pulled in by the
773 * services, as an
774 * indication that the
775 * generic service is
776 * now available. This
777 * is strictly
778 * one-way. The
779 * targets do NOT pull
780 * in the SysV
781 * services! */
782 r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_WANTS, m, NULL, true);
bd77d0fc 783
2c4104f0 784 if (r < 0)
66870f90
ZJS
785 log_error_unit(u->id,
786 "[%s:%u] Failed to add LSB Provides name %s, ignoring: %s",
787 path, line, m, strerror(-r));
42a097a2
LP
788
789 free(m);
2c4104f0
LP
790 }
791
c2b35af6
LP
792 } else if (startswith_no_case(t, "Required-Start:") ||
793 startswith_no_case(t, "Should-Start:") ||
794 startswith_no_case(t, "X-Start-Before:") ||
795 startswith_no_case(t, "X-Start-After:")) {
2c4104f0
LP
796 char *i, *w;
797 size_t z;
798
799 state = LSB;
800
f60f22df 801 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
2c4104f0
LP
802 char *n, *m;
803
804 if (!(n = strndup(w, z))) {
805 r = -ENOMEM;
806 goto finish;
807 }
808
9eb977db 809 r = sysv_translate_facility(n, path_get_file_name(path), &m);
e83c7c0b 810 if (r < 0) {
66870f90
ZJS
811 log_error_unit(u->id,
812 "[%s:%u] Failed to translate LSB dependency %s, ignoring: %s",
813 path, line, n, strerror(-r));
e83c7c0b
LP
814 free(n);
815 continue;
816 }
817
818 free(n);
2c4104f0
LP
819
820 if (r == 0)
821 continue;
822
c2b35af6 823 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
2c4104f0
LP
824
825 if (r < 0)
66870f90
ZJS
826 log_error_unit(u->id, "[%s:%u] Failed to add dependency on %s, ignoring: %s",
827 path, line, m, strerror(-r));
42a097a2
LP
828
829 free(m);
2c4104f0 830 }
c2b35af6 831 } else if (startswith_no_case(t, "Default-Start:")) {
8309400a
LP
832 char *k, *d;
833
834 state = LSB;
835
836 k = delete_chars(t+14, WHITESPACE "-");
837
838 if (k[0] != 0) {
839 if (!(d = strdup(k))) {
840 r = -ENOMEM;
841 goto finish;
842 }
843
844 free(s->sysv_runlevels);
845 s->sysv_runlevels = d;
846 }
2c4104f0 847
0b5d26f9
LP
848 } else if (startswith_no_case(t, "Description:")) {
849 char *d, *j;
ed4c1cc6 850
2c4104f0
LP
851 state = LSB_DESCRIPTION;
852
0b5d26f9
LP
853 if ((j = strstrip(t+12)) && *j) {
854 if (!(d = strdup(j))) {
855 r = -ENOMEM;
856 goto finish;
857 }
858 } else
859 d = NULL;
2c4104f0 860
0b5d26f9
LP
861 free(long_description);
862 long_description = d;
2c4104f0 863
ed4c1cc6 864 } else if (startswith_no_case(t, "Short-Description:")) {
0b5d26f9 865 char *d, *j;
2c4104f0 866
2c4104f0
LP
867 state = LSB;
868
0b5d26f9
LP
869 if ((j = strstrip(t+18)) && *j) {
870 if (!(d = strdup(j))) {
871 r = -ENOMEM;
872 goto finish;
873 }
874 } else
875 d = NULL;
2c4104f0 876
0b5d26f9
LP
877 free(short_description);
878 short_description = d;
2c4104f0
LP
879
880 } else if (state == LSB_DESCRIPTION) {
881
882 if (startswith(l, "#\t") || startswith(l, "# ")) {
0b5d26f9 883 char *j;
2c4104f0 884
0b5d26f9
LP
885 if ((j = strstrip(t)) && *j) {
886 char *d = NULL;
887
888 if (long_description)
b7def684 889 d = strjoin(long_description, " ", t, NULL);
0b5d26f9
LP
890 else
891 d = strdup(j);
892
893 if (!d) {
894 r = -ENOMEM;
895 goto finish;
896 }
897
898 free(long_description);
899 long_description = d;
2c4104f0
LP
900 }
901
2c4104f0
LP
902 } else
903 state = LSB;
904 }
905 }
906 }
907
37e2941d 908 if ((r = sysv_exec_commands(s, supports_reload)) < 0)
2c4104f0
LP
909 goto finish;
910
a40eb732 911 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
0bc824be
LP
912 /* If there a runlevels configured for this service
913 * but none of the standard ones, then we assume this
914 * is some special kind of service (which might be
915 * needed for early boot) and don't create any links
916 * to it. */
917
1124fe6f 918 UNIT(s)->default_dependencies = false;
09cd1ab1 919
09cd1ab1 920 /* Don't timeout special services during boot (like fsck) */
d568a335
MS
921 s->timeout_start_usec = 0;
922 s->timeout_stop_usec = 0;
923 } else {
924 s->timeout_start_usec = DEFAULT_SYSV_TIMEOUT_USEC;
925 s->timeout_stop_usec = DEFAULT_SYSV_TIMEOUT_USEC;
926 }
927
80876c20 928 /* Special setting for all SysV services */
1f48cf56 929 s->type = SERVICE_FORKING;
f8788303 930 s->remain_after_exit = !s->pid_file;
1835f23c 931 s->guess_main_pid = false;
525ee6f4 932 s->restart = SERVICE_RESTART_NO;
353e12c2 933 s->exec_context.ignore_sigpipe = false;
4819ff03 934 s->kill_context.kill_mode = KILL_PROCESS;
80876c20 935
0b5d26f9
LP
936 /* We use the long description only if
937 * no short description is set. */
938
939 if (short_description)
940 description = short_description;
941 else if (chkconfig_description)
942 description = chkconfig_description;
943 else if (long_description)
944 description = long_description;
945 else
946 description = NULL;
947
948 if (description) {
949 char *d;
950
e527618d 951 if (!(d = strappend(s->sysv_has_lsb ? "LSB: " : "SYSV: ", description))) {
0b5d26f9
LP
952 r = -ENOMEM;
953 goto finish;
954 }
955
ac155bb8 956 u->description = d;
0b5d26f9
LP
957 }
958
ea87ca5a
LP
959 /* The priority that has been set in /etc/rcN.d/ hierarchies
960 * takes precedence over what is stored as default in the LSB
961 * header */
962 if (s->sysv_start_priority_from_rcnd >= 0)
963 s->sysv_start_priority = s->sysv_start_priority_from_rcnd;
964
ac155bb8 965 u->load_state = UNIT_LOADED;
23a177ef 966 r = 0;
2c4104f0
LP
967
968finish:
969
970 if (f)
971 fclose(f);
972
0b5d26f9
LP
973 free(short_description);
974 free(long_description);
975 free(chkconfig_description);
976
2c4104f0
LP
977 return r;
978}
979
e537352b 980static int service_load_sysv_name(Service *s, const char *name) {
2c4104f0
LP
981 char **p;
982
983 assert(s);
984 assert(name);
985
3cdebc21 986 /* For SysV services we strip the rc.* and *.sh
d017c6ca 987 * prefixes/suffixes. */
a83ad683
ZJS
988 if (startswith(name, "rc.") ||
989 endswith(name, ".sh.service"))
d017c6ca
LP
990 return -ENOENT;
991
1124fe6f 992 STRV_FOREACH(p, UNIT(s)->manager->lookup_paths.sysvinit_path) {
2c4104f0
LP
993 char *path;
994 int r;
995
b7def684 996 path = strjoin(*p, "/", name, NULL);
44d91056 997 if (!path)
2c4104f0
LP
998 return -ENOMEM;
999
1000 assert(endswith(path, ".service"));
1001 path[strlen(path)-8] = 0;
1002
e537352b 1003 r = service_load_sysv_path(s, path);
fbe9f3a9 1004
1124fe6f 1005 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
20771ae3 1006 /* Try *.sh source'able init scripts */
fbe9f3a9
LP
1007 strcat(path, ".sh");
1008 r = service_load_sysv_path(s, path);
1009 }
2c4104f0
LP
1010 free(path);
1011
1124fe6f 1012 if (r >= 0 && UNIT(s)->load_state == UNIT_STUB) {
20771ae3 1013 /* Try rc.* init scripts */
65530632 1014
b7def684 1015 path = strjoin(*p, "/rc.", name, NULL);
44d91056 1016 if (!path)
65530632
MV
1017 return -ENOMEM;
1018
e1992852 1019 /* Drop .service suffix */
65530632
MV
1020 path[strlen(path)-8] = 0;
1021 r = service_load_sysv_path(s, path);
1022 free(path);
1023 }
1024
23a177ef 1025 if (r < 0)
2c4104f0 1026 return r;
23a177ef 1027
88516c0c 1028 if (UNIT(s)->load_state != UNIT_STUB)
23a177ef 1029 break;
2c4104f0
LP
1030 }
1031
1032 return 0;
1033}
1034
e537352b 1035static int service_load_sysv(Service *s) {
2c4104f0
LP
1036 const char *t;
1037 Iterator i;
1038 int r;
1039
5cb5a6ff
LP
1040 assert(s);
1041
1042 /* Load service data from SysV init scripts, preferably with
1043 * LSB headers ... */
1044
1124fe6f 1045 if (strv_isempty(UNIT(s)->manager->lookup_paths.sysvinit_path))
2c4104f0
LP
1046 return 0;
1047
1124fe6f 1048 if ((t = UNIT(s)->id))
e537352b 1049 if ((r = service_load_sysv_name(s, t)) < 0)
2c4104f0
LP
1050 return r;
1051
1124fe6f
MS
1052 if (UNIT(s)->load_state == UNIT_STUB)
1053 SET_FOREACH(t, UNIT(s)->names, i) {
1054 if (t == UNIT(s)->id)
e537352b
LP
1055 continue;
1056
e364ad06 1057 if ((r = service_load_sysv_name(s, t)) < 0)
23a177ef
LP
1058 return r;
1059
1124fe6f 1060 if (UNIT(s)->load_state != UNIT_STUB)
23a177ef
LP
1061 break;
1062 }
2c4104f0
LP
1063
1064 return 0;
5cb5a6ff 1065}
07459bb6 1066#endif
5cb5a6ff 1067
9fff8a1f 1068static int fsck_fix_order(Service *s) {
ac155bb8 1069 Unit *other;
9fff8a1f
LP
1070 int r;
1071
1072 assert(s);
1073
1074 if (s->fsck_passno <= 0)
1075 return 0;
1076
1077 /* For each pair of services where both have an fsck priority
1078 * we order things based on it. */
1079
1124fe6f 1080 LIST_FOREACH(units_by_type, other, UNIT(s)->manager->units_by_type[UNIT_SERVICE]) {
9fff8a1f
LP
1081 Service *t;
1082 UnitDependency d;
1083
595ed347 1084 t = SERVICE(other);
9fff8a1f
LP
1085
1086 if (s == t)
1087 continue;
1088
1124fe6f 1089 if (UNIT(t)->load_state != UNIT_LOADED)
9fff8a1f
LP
1090 continue;
1091
1092 if (t->fsck_passno <= 0)
1093 continue;
1094
1095 if (t->fsck_passno < s->fsck_passno)
1096 d = UNIT_AFTER;
1097 else if (t->fsck_passno > s->fsck_passno)
1098 d = UNIT_BEFORE;
1099 else
1100 continue;
1101
da19d5c1 1102 if ((r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
9fff8a1f
LP
1103 return r;
1104 }
1105
1106 return 0;
1107}
1108
243b1432
LP
1109static int service_verify(Service *s) {
1110 assert(s);
1111
1124fe6f 1112 if (UNIT(s)->load_state != UNIT_LOADED)
243b1432
LP
1113 return 0;
1114
1115 if (!s->exec_command[SERVICE_EXEC_START]) {
66870f90
ZJS
1116 log_error_unit(UNIT(s)->id,
1117 "%s lacks ExecStart setting. Refusing.", UNIT(s)->id);
243b1432
LP
1118 return -EINVAL;
1119 }
1120
34e9ba66
LP
1121 if (s->type != SERVICE_ONESHOT &&
1122 s->exec_command[SERVICE_EXEC_START]->command_next) {
66870f90
ZJS
1123 log_error_unit(UNIT(s)->id,
1124 "%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
6cf6bbc2
LP
1125 return -EINVAL;
1126 }
1127
05e343b7 1128 if (s->type == SERVICE_DBUS && !s->bus_name) {
66870f90
ZJS
1129 log_error_unit(UNIT(s)->id,
1130 "%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->id);
4d0e5dbd
LP
1131 return -EINVAL;
1132 }
1133
7e2668c6 1134 if (s->bus_name && s->type != SERVICE_DBUS)
66870f90
ZJS
1135 log_warning_unit(UNIT(s)->id,
1136 "%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
7e2668c6 1137
4819ff03 1138 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
66870f90
ZJS
1139 log_error_unit(UNIT(s)->id,
1140 "%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s)->id);
05e343b7
LP
1141 return -EINVAL;
1142 }
1143
243b1432
LP
1144 return 0;
1145}
1146
a40eb732
LP
1147static int service_add_default_dependencies(Service *s) {
1148 int r;
1149
1150 assert(s);
1151
1152 /* Add a number of automatic dependencies useful for the
1153 * majority of services. */
1154
1155 /* First, pull in base system */
67445f4e 1156 if (UNIT(s)->manager->running_as == SYSTEMD_SYSTEM) {
a40eb732
LP
1157
1158 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
1159 return r;
1160
67445f4e 1161 } else if (UNIT(s)->manager->running_as == SYSTEMD_USER) {
a40eb732
LP
1162
1163 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0)
1164 return r;
1165 }
1166
1167 /* Second, activate normal shutdown */
ead8e478 1168 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
a40eb732
LP
1169}
1170
4dfc092a
LP
1171static void service_fix_output(Service *s) {
1172 assert(s);
1173
1174 /* If nothing has been explicitly configured, patch default
1175 * output in. If input is socket/tty we avoid this however,
1176 * since in that case we want output to default to the same
1177 * place as we read input from. */
1178
1179 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
1180 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1181 s->exec_context.std_input == EXEC_INPUT_NULL)
1124fe6f 1182 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
4dfc092a
LP
1183
1184 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
1185 s->exec_context.std_input == EXEC_INPUT_NULL)
1124fe6f 1186 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
4dfc092a
LP
1187}
1188
e537352b
LP
1189static int service_load(Unit *u) {
1190 int r;
1191 Service *s = SERVICE(u);
1192
1193 assert(s);
1e2e8133 1194
5cb5a6ff 1195 /* Load a .service file */
e537352b 1196 if ((r = unit_load_fragment(u)) < 0)
5cb5a6ff
LP
1197 return r;
1198
07459bb6 1199#ifdef HAVE_SYSV_COMPAT
bd77d0fc 1200 /* Load a classic init script as a fallback, if we couldn't find anything */
ac155bb8 1201 if (u->load_state == UNIT_STUB)
e537352b 1202 if ((r = service_load_sysv(s)) < 0)
23a177ef 1203 return r;
07459bb6 1204#endif
d46de8a1 1205
23a177ef 1206 /* Still nothing found? Then let's give up */
ac155bb8 1207 if (u->load_state == UNIT_STUB)
23a177ef 1208 return -ENOENT;
034c6ed7 1209
23a177ef
LP
1210 /* We were able to load something, then let's add in the
1211 * dropin directories. */
1212 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
8e274523 1213 return r;
23a177ef
LP
1214
1215 /* This is a new unit? Then let's add in some extras */
ac155bb8 1216 if (u->load_state == UNIT_LOADED) {
0b86feac
LP
1217 if (s->type == _SERVICE_TYPE_INVALID)
1218 s->type = s->bus_name ? SERVICE_DBUS : SERVICE_SIMPLE;
1219
d568a335
MS
1220 /* Oneshot services have disabled start timeout by default */
1221 if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined)
1222 s->timeout_start_usec = 0;
98709151 1223
4e2b0f9b
LP
1224 service_fix_output(s);
1225
23a177ef
LP
1226 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
1227 return r;
1228
d686d8a9 1229 if ((r = unit_add_default_cgroups(u)) < 0)
23a177ef
LP
1230 return r;
1231
07459bb6 1232#ifdef HAVE_SYSV_COMPAT
56d748b4 1233 if ((r = sysv_fix_order(s)) < 0)
23a177ef 1234 return r;
07459bb6 1235#endif
05e343b7 1236
9fff8a1f
LP
1237 if ((r = fsck_fix_order(s)) < 0)
1238 return r;
1239
ee0dd802 1240 if (s->bus_name)
05e343b7 1241 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
a40eb732 1242 return r;
c952c6ec
LP
1243
1244 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
1245 s->notify_access = NOTIFY_MAIN;
a40eb732 1246
02c4ef9c
LP
1247 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
1248 s->notify_access = NOTIFY_MAIN;
1249
a40eb732 1250 if (s->type == SERVICE_DBUS || s->bus_name)
177b3ffe 1251 if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_SOCKET, NULL, true)) < 0)
a40eb732
LP
1252 return r;
1253
1124fe6f 1254 if (UNIT(s)->default_dependencies)
a40eb732
LP
1255 if ((r = service_add_default_dependencies(s)) < 0)
1256 return r;
e06c73cc 1257
cba6e062 1258 r = unit_exec_context_defaults(u, &s->exec_context);
e06c73cc
LP
1259 if (r < 0)
1260 return r;
8e274523
LP
1261 }
1262
243b1432 1263 return service_verify(s);
034c6ed7
LP
1264}
1265
87f0e418 1266static void service_dump(Unit *u, FILE *f, const char *prefix) {
5cb5a6ff 1267
5cb5a6ff 1268 ServiceExecCommand c;
87f0e418 1269 Service *s = SERVICE(u);
47be870b
LP
1270 const char *prefix2;
1271 char *p2;
5cb5a6ff
LP
1272
1273 assert(s);
1274
47be870b
LP
1275 p2 = strappend(prefix, "\t");
1276 prefix2 = p2 ? p2 : prefix;
44d8db9e 1277
5cb5a6ff 1278 fprintf(f,
81a2b7ce 1279 "%sService State: %s\n"
f42806df
LP
1280 "%sResult: %s\n"
1281 "%sReload Result: %s\n"
81a2b7ce 1282 "%sPermissionsStartOnly: %s\n"
8e274523 1283 "%sRootDirectoryStartOnly: %s\n"
02ee865a 1284 "%sRemainAfterExit: %s\n"
3185a36b 1285 "%sGuessMainPID: %s\n"
c952c6ec 1286 "%sType: %s\n"
2cf3143a 1287 "%sRestart: %s\n"
c952c6ec 1288 "%sNotifyAccess: %s\n",
81a2b7ce 1289 prefix, service_state_to_string(s->state),
f42806df
LP
1290 prefix, service_result_to_string(s->result),
1291 prefix, service_result_to_string(s->reload_result),
81a2b7ce 1292 prefix, yes_no(s->permissions_start_only),
8e274523 1293 prefix, yes_no(s->root_directory_start_only),
02ee865a 1294 prefix, yes_no(s->remain_after_exit),
3185a36b 1295 prefix, yes_no(s->guess_main_pid),
c952c6ec 1296 prefix, service_type_to_string(s->type),
2cf3143a 1297 prefix, service_restart_to_string(s->restart),
c952c6ec 1298 prefix, notify_access_to_string(s->notify_access));
5cb5a6ff 1299
70123e68
LP
1300 if (s->control_pid > 0)
1301 fprintf(f,
bb00e604
LP
1302 "%sControl PID: %lu\n",
1303 prefix, (unsigned long) s->control_pid);
70123e68
LP
1304
1305 if (s->main_pid > 0)
1306 fprintf(f,
6dfa5494
LP
1307 "%sMain PID: %lu\n"
1308 "%sMain PID Known: %s\n"
1309 "%sMain PID Alien: %s\n",
1310 prefix, (unsigned long) s->main_pid,
1311 prefix, yes_no(s->main_pid_known),
1312 prefix, yes_no(s->main_pid_alien));
70123e68 1313
034c6ed7
LP
1314 if (s->pid_file)
1315 fprintf(f,
1316 "%sPIDFile: %s\n",
1317 prefix, s->pid_file);
1318
05e343b7
LP
1319 if (s->bus_name)
1320 fprintf(f,
1321 "%sBusName: %s\n"
1322 "%sBus Name Good: %s\n",
1323 prefix, s->bus_name,
1324 prefix, yes_no(s->bus_name_good));
1325
4819ff03 1326 kill_context_dump(&s->kill_context, f, prefix);
5cb5a6ff
LP
1327 exec_context_dump(&s->exec_context, f, prefix);
1328
e537352b 1329 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
5cb5a6ff 1330
44d8db9e
LP
1331 if (!s->exec_command[c])
1332 continue;
1333
40d50879 1334 fprintf(f, "%s-> %s:\n",
94f04347 1335 prefix, service_exec_command_to_string(c));
44d8db9e
LP
1336
1337 exec_command_dump_list(s->exec_command[c], f, prefix2);
5cb5a6ff 1338 }
44d8db9e 1339
07459bb6 1340#ifdef HAVE_SYSV_COMPAT
1b64d026 1341 if (s->is_sysv)
2c4104f0 1342 fprintf(f,
9fff8a1f
LP
1343 "%sSysV Init Script has LSB Header: %s\n"
1344 "%sSysVEnabled: %s\n",
9fff8a1f
LP
1345 prefix, yes_no(s->sysv_has_lsb),
1346 prefix, yes_no(s->sysv_enabled));
2c4104f0
LP
1347
1348 if (s->sysv_start_priority >= 0)
1349 fprintf(f,
9fff8a1f
LP
1350 "%sSysVStartPriority: %i\n",
1351 prefix, s->sysv_start_priority);
2c4104f0 1352
8309400a
LP
1353 if (s->sysv_runlevels)
1354 fprintf(f, "%sSysVRunLevels: %s\n",
1355 prefix, s->sysv_runlevels);
07459bb6 1356#endif
23a177ef 1357
9fff8a1f
LP
1358 if (s->fsck_passno > 0)
1359 fprintf(f,
1360 "%sFsckPassNo: %i\n",
1361 prefix, s->fsck_passno);
1362
8c47c732
LP
1363 if (s->status_text)
1364 fprintf(f, "%sStatus Text: %s\n",
1365 prefix, s->status_text);
1366
47be870b 1367 free(p2);
5cb5a6ff
LP
1368}
1369
c5419d42 1370static int service_load_pid_file(Service *s, bool may_warn) {
034c6ed7 1371 char *k;
034c6ed7 1372 int r;
5925dd3c 1373 pid_t pid;
034c6ed7
LP
1374
1375 assert(s);
1376
034c6ed7 1377 if (!s->pid_file)
13230d5d 1378 return -ENOENT;
034c6ed7 1379
5375410b 1380 if ((r = read_one_line_file(s->pid_file, &k)) < 0) {
c5419d42 1381 if (may_warn)
66870f90
ZJS
1382 log_info_unit(UNIT(s)->id,
1383 "PID file %s not readable (yet?) after %s.",
1384 s->pid_file, service_state_to_string(s->state));
034c6ed7 1385 return r;
5375410b 1386 }
034c6ed7 1387
5925dd3c
LP
1388 r = parse_pid(k, &pid);
1389 free(k);
034c6ed7 1390
5925dd3c
LP
1391 if (r < 0)
1392 return r;
406eaf93 1393
5925dd3c 1394 if (kill(pid, 0) < 0 && errno != EPERM) {
c5419d42 1395 if (may_warn)
66870f90
ZJS
1396 log_info_unit(UNIT(s)->id,
1397 "PID %lu read from file %s does not exist.",
1398 (unsigned long) pid, s->pid_file);
b8c597d5
LP
1399 return -ESRCH;
1400 }
1401
db01f8b3
MS
1402 if (s->main_pid_known) {
1403 if (pid == s->main_pid)
1404 return 0;
1405
66870f90
ZJS
1406 log_debug_unit(UNIT(s)->id,
1407 "Main PID changing: %lu -> %lu",
1408 (unsigned long) s->main_pid, (unsigned long) pid);
db01f8b3
MS
1409 service_unwatch_main_pid(s);
1410 s->main_pid_known = false;
3a111838 1411 } else
66870f90
ZJS
1412 log_debug_unit(UNIT(s)->id,
1413 "Main PID loaded: %lu", (unsigned long) pid);
db01f8b3 1414
5925dd3c 1415 if ((r = service_set_main_pid(s, pid)) < 0)
16f6025e
LP
1416 return r;
1417
5925dd3c
LP
1418 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1419 /* FIXME: we need to do something here */
1420 return r;
034c6ed7
LP
1421
1422 return 0;
1423}
1424
4fbf50b3
LP
1425static int service_search_main_pid(Service *s) {
1426 pid_t pid;
1427 int r;
1428
1429 assert(s);
1430
3185a36b
LP
1431 /* If we know it anyway, don't ever fallback to unreliable
1432 * heuristics */
4fbf50b3
LP
1433 if (s->main_pid_known)
1434 return 0;
1435
3185a36b
LP
1436 if (!s->guess_main_pid)
1437 return 0;
1438
4fbf50b3
LP
1439 assert(s->main_pid <= 0);
1440
1124fe6f 1441 if ((pid = cgroup_bonding_search_main_pid_list(UNIT(s)->cgroup_bondings)) <= 0)
4fbf50b3
LP
1442 return -ENOENT;
1443
66870f90
ZJS
1444 log_debug_unit(UNIT(s)->id,
1445 "Main PID guessed: %lu", (unsigned long) pid);
4fbf50b3
LP
1446 if ((r = service_set_main_pid(s, pid)) < 0)
1447 return r;
1448
1449 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1450 /* FIXME: we need to do something here */
1451 return r;
1452
1453 return 0;
1454}
1455
6bda96a0 1456static void service_notify_sockets_dead(Service *s, bool failed_permanent) {
ceee3d82 1457 Iterator i;
57020a3a 1458 Unit *u;
3e33402a
LP
1459
1460 assert(s);
1461
f976f3f6
LP
1462 /* Notifies all our sockets when we die */
1463
6cf6bbc2 1464 if (s->socket_fd >= 0)
57020a3a 1465 return;
3e33402a 1466
1124fe6f 1467 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i)
ac155bb8 1468 if (u->type == UNIT_SOCKET)
6bda96a0 1469 socket_notify_service_dead(SOCKET(u), failed_permanent);
3e33402a 1470
57020a3a 1471 return;
ceee3d82
LP
1472}
1473
034c6ed7
LP
1474static void service_set_state(Service *s, ServiceState state) {
1475 ServiceState old_state;
e056b01d 1476 const UnitActiveState *table;
5cb5a6ff
LP
1477 assert(s);
1478
e056b01d
LP
1479 table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
1480
034c6ed7 1481 old_state = s->state;
5cb5a6ff 1482 s->state = state;
034c6ed7 1483
3a111838
MS
1484 service_unwatch_pid_file(s);
1485
034c6ed7
LP
1486 if (state != SERVICE_START_PRE &&
1487 state != SERVICE_START &&
1488 state != SERVICE_START_POST &&
1489 state != SERVICE_RELOAD &&
1490 state != SERVICE_STOP &&
1491 state != SERVICE_STOP_SIGTERM &&
1492 state != SERVICE_STOP_SIGKILL &&
1493 state != SERVICE_STOP_POST &&
1494 state != SERVICE_FINAL_SIGTERM &&
1495 state != SERVICE_FINAL_SIGKILL &&
1496 state != SERVICE_AUTO_RESTART)
acbb0225 1497 unit_unwatch_timer(UNIT(s), &s->timer_watch);
034c6ed7 1498
7d55e835
LP
1499 if (state != SERVICE_START &&
1500 state != SERVICE_START_POST &&
034c6ed7
LP
1501 state != SERVICE_RUNNING &&
1502 state != SERVICE_RELOAD &&
1503 state != SERVICE_STOP &&
1504 state != SERVICE_STOP_SIGTERM &&
867b3b7d 1505 state != SERVICE_STOP_SIGKILL) {
5e94833f 1506 service_unwatch_main_pid(s);
867b3b7d
LP
1507 s->main_command = NULL;
1508 }
034c6ed7
LP
1509
1510 if (state != SERVICE_START_PRE &&
1511 state != SERVICE_START &&
1512 state != SERVICE_START_POST &&
1513 state != SERVICE_RELOAD &&
1514 state != SERVICE_STOP &&
1515 state != SERVICE_STOP_SIGTERM &&
1516 state != SERVICE_STOP_SIGKILL &&
1517 state != SERVICE_STOP_POST &&
1518 state != SERVICE_FINAL_SIGTERM &&
e537352b 1519 state != SERVICE_FINAL_SIGKILL) {
5e94833f 1520 service_unwatch_control_pid(s);
034c6ed7 1521 s->control_command = NULL;
a16e1123 1522 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
e537352b 1523 }
034c6ed7 1524
464876c9
LP
1525 if (state == SERVICE_FAILED)
1526 service_notify_sockets_dead(s, s->result == SERVICE_FAILURE_START_LIMIT);
1527
ceee3d82
LP
1528 if (state == SERVICE_DEAD ||
1529 state == SERVICE_STOP ||
1530 state == SERVICE_STOP_SIGTERM ||
1531 state == SERVICE_STOP_SIGKILL ||
1532 state == SERVICE_STOP_POST ||
1533 state == SERVICE_FINAL_SIGTERM ||
1534 state == SERVICE_FINAL_SIGKILL ||
ceee3d82 1535 state == SERVICE_AUTO_RESTART)
c2f34808 1536 service_notify_sockets_dead(s, false);
ceee3d82 1537
4f2d528d
LP
1538 if (state != SERVICE_START_PRE &&
1539 state != SERVICE_START &&
6cf6bbc2
LP
1540 state != SERVICE_START_POST &&
1541 state != SERVICE_RUNNING &&
1542 state != SERVICE_RELOAD &&
1543 state != SERVICE_STOP &&
1544 state != SERVICE_STOP_SIGTERM &&
1545 state != SERVICE_STOP_SIGKILL &&
1546 state != SERVICE_STOP_POST &&
1547 state != SERVICE_FINAL_SIGTERM &&
1548 state != SERVICE_FINAL_SIGKILL &&
1124fe6f 1549 !(state == SERVICE_DEAD && UNIT(s)->job)) {
4f2d528d 1550 service_close_socket_fd(s);
6cf6bbc2
LP
1551 service_connection_unref(s);
1552 }
4f2d528d 1553
6d594baa 1554 if (state == SERVICE_STOP || state == SERVICE_STOP_SIGTERM)
a6927d7f
MO
1555 service_stop_watchdog(s);
1556
f6023656
LP
1557 /* For the inactive states unit_notify() will trim the cgroup,
1558 * but for exit we have to do that ourselves... */
1124fe6f
MS
1559 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
1560 cgroup_bonding_trim_list(UNIT(s)->cgroup_bondings, true);
f6023656 1561
e537352b 1562 if (old_state != state)
66870f90
ZJS
1563 log_debug_unit(UNIT(s)->id,
1564 "%s changed %s -> %s", UNIT(s)->id,
1565 service_state_to_string(old_state),
1566 service_state_to_string(state));
acbb0225 1567
e056b01d 1568 unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
f42806df 1569 s->reload_result = SERVICE_SUCCESS;
034c6ed7
LP
1570}
1571
a16e1123
LP
1572static int service_coldplug(Unit *u) {
1573 Service *s = SERVICE(u);
1574 int r;
1575
1576 assert(s);
1577 assert(s->state == SERVICE_DEAD);
1578
1579 if (s->deserialized_state != s->state) {
1580
1581 if (s->deserialized_state == SERVICE_START_PRE ||
1582 s->deserialized_state == SERVICE_START ||
1583 s->deserialized_state == SERVICE_START_POST ||
1584 s->deserialized_state == SERVICE_RELOAD ||
1585 s->deserialized_state == SERVICE_STOP ||
1586 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1587 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1588 s->deserialized_state == SERVICE_STOP_POST ||
1589 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1590 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
e558336f 1591 s->deserialized_state == SERVICE_AUTO_RESTART) {
d568a335 1592 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_start_usec > 0) {
e558336f
LP
1593 usec_t k;
1594
d568a335 1595 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_start_usec;
e558336f 1596
36697dc0
LP
1597 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, k, &s->timer_watch);
1598 if (r < 0)
e558336f
LP
1599 return r;
1600 }
1601 }
a16e1123
LP
1602
1603 if ((s->deserialized_state == SERVICE_START &&
1604 (s->type == SERVICE_FORKING ||
8c47c732 1605 s->type == SERVICE_DBUS ||
34e9ba66 1606 s->type == SERVICE_ONESHOT ||
8c47c732 1607 s->type == SERVICE_NOTIFY)) ||
a16e1123
LP
1608 s->deserialized_state == SERVICE_START_POST ||
1609 s->deserialized_state == SERVICE_RUNNING ||
1610 s->deserialized_state == SERVICE_RELOAD ||
1611 s->deserialized_state == SERVICE_STOP ||
1612 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1613 s->deserialized_state == SERVICE_STOP_SIGKILL)
1614 if (s->main_pid > 0)
1615 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1616 return r;
1617
1618 if (s->deserialized_state == SERVICE_START_PRE ||
1619 s->deserialized_state == SERVICE_START ||
1620 s->deserialized_state == SERVICE_START_POST ||
1621 s->deserialized_state == SERVICE_RELOAD ||
1622 s->deserialized_state == SERVICE_STOP ||
1623 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1624 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1625 s->deserialized_state == SERVICE_STOP_POST ||
1626 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1627 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1628 if (s->control_pid > 0)
1629 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1630 return r;
1631
bb242b7b
MO
1632 if (s->deserialized_state == SERVICE_START_POST ||
1633 s->deserialized_state == SERVICE_RUNNING)
1634 service_handle_watchdog(s);
1635
a16e1123
LP
1636 service_set_state(s, s->deserialized_state);
1637 }
a16e1123
LP
1638 return 0;
1639}
1640
44d8db9e
LP
1641static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1642 Iterator i;
1643 int r;
1644 int *rfds = NULL;
1645 unsigned rn_fds = 0;
57020a3a 1646 Unit *u;
44d8db9e
LP
1647
1648 assert(s);
1649 assert(fds);
1650 assert(n_fds);
1651
6cf6bbc2
LP
1652 if (s->socket_fd >= 0)
1653 return 0;
1654
1124fe6f 1655 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
44d8db9e
LP
1656 int *cfds;
1657 unsigned cn_fds;
57020a3a
LP
1658 Socket *sock;
1659
ac155bb8 1660 if (u->type != UNIT_SOCKET)
57020a3a
LP
1661 continue;
1662
1663 sock = SOCKET(u);
44d8db9e 1664
47be870b 1665 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
44d8db9e
LP
1666 goto fail;
1667
1668 if (!cfds)
1669 continue;
1670
1671 if (!rfds) {
1672 rfds = cfds;
1673 rn_fds = cn_fds;
1674 } else {
1675 int *t;
1676
1677 if (!(t = new(int, rn_fds+cn_fds))) {
1678 free(cfds);
1679 r = -ENOMEM;
1680 goto fail;
1681 }
1682
9c1b183c
LP
1683 memcpy(t, rfds, rn_fds * sizeof(int));
1684 memcpy(t+rn_fds, cfds, cn_fds * sizeof(int));
44d8db9e
LP
1685 free(rfds);
1686 free(cfds);
1687
1688 rfds = t;
1689 rn_fds = rn_fds+cn_fds;
1690 }
1691 }
1692
1693 *fds = rfds;
1694 *n_fds = rn_fds;
3e33402a 1695
44d8db9e
LP
1696 return 0;
1697
1698fail:
1699 free(rfds);
3e33402a 1700
44d8db9e
LP
1701 return r;
1702}
1703
81a2b7ce
LP
1704static int service_spawn(
1705 Service *s,
1706 ExecCommand *c,
1707 bool timeout,
1708 bool pass_fds,
1709 bool apply_permissions,
1710 bool apply_chroot,
1e3ad081 1711 bool apply_tty_stdin,
c952c6ec 1712 bool set_notify_socket,
ecedd90f 1713 bool is_control,
81a2b7ce
LP
1714 pid_t *_pid) {
1715
034c6ed7
LP
1716 pid_t pid;
1717 int r;
6cf6bbc2 1718 int *fds = NULL, *fdsbuf = NULL;
2105e76a
LP
1719 unsigned n_fds = 0, n_env = 0;
1720 char **argv = NULL, **final_env = NULL, **our_env = NULL;
034c6ed7
LP
1721
1722 assert(s);
1723 assert(c);
1724 assert(_pid);
1725
6cf6bbc2
LP
1726 if (pass_fds ||
1727 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1728 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1729 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1730
4f2d528d
LP
1731 if (s->socket_fd >= 0) {
1732 fds = &s->socket_fd;
1733 n_fds = 1;
6cf6bbc2
LP
1734 } else {
1735 if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0)
1736 goto fail;
1737
1738 fds = fdsbuf;
1739 }
4f2d528d 1740 }
44d8db9e 1741
d568a335 1742 if (timeout && s->timeout_start_usec) {
36697dc0 1743 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_start_usec, &s->timer_watch);
d568a335 1744 if (r < 0)
034c6ed7
LP
1745 goto fail;
1746 } else
acbb0225 1747 unit_unwatch_timer(UNIT(s), &s->timer_watch);
034c6ed7 1748
9e2f7c11
LP
1749 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1750 r = -ENOMEM;
1751 goto fail;
1752 }
1753
97ae63e2
LP
1754 our_env = new0(char*, 5);
1755 if (!our_env) {
2105e76a
LP
1756 r = -ENOMEM;
1757 goto fail;
1758 }
c952c6ec 1759
2105e76a 1760 if (set_notify_socket)
1124fe6f 1761 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
c952c6ec
LP
1762 r = -ENOMEM;
1763 goto fail;
1764 }
1765
2105e76a
LP
1766 if (s->main_pid > 0)
1767 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
c952c6ec
LP
1768 r = -ENOMEM;
1769 goto fail;
1770 }
2105e76a 1771
6e0bcc98
MO
1772 if (s->watchdog_usec > 0)
1773 if (asprintf(our_env + n_env++, "WATCHDOG_USEC=%llu", (unsigned long long) s->watchdog_usec) < 0) {
1774 r = -ENOMEM;
1775 goto fail;
1776 }
1777
97ae63e2
LP
1778 if (s->meta.manager->running_as != SYSTEMD_SYSTEM)
1779 if (asprintf(our_env + n_env++, "MANAGERPID=%lu", (unsigned long) getpid()) < 0) {
1780 r = -ENOMEM;
1781 goto fail;
1782 }
1783
1784 final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL);
1785 if (!final_env) {
2105e76a
LP
1786 r = -ENOMEM;
1787 goto fail;
1788 }
c952c6ec 1789
9e2f7c11
LP
1790 r = exec_spawn(c,
1791 argv,
1792 &s->exec_context,
1793 fds, n_fds,
2105e76a 1794 final_env,
9e2f7c11
LP
1795 apply_permissions,
1796 apply_chroot,
1e3ad081 1797 apply_tty_stdin,
1124fe6f
MS
1798 UNIT(s)->manager->confirm_spawn,
1799 UNIT(s)->cgroup_bondings,
1800 UNIT(s)->cgroup_attributes,
ecedd90f 1801 is_control ? "control" : NULL,
62bca2c6 1802 UNIT(s)->id,
f2b68789 1803 s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
9e2f7c11
LP
1804 &pid);
1805
9e2f7c11 1806 if (r < 0)
034c6ed7
LP
1807 goto fail;
1808
87f0e418 1809 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
034c6ed7
LP
1810 /* FIXME: we need to do something here */
1811 goto fail;
1812
2105e76a
LP
1813 free(fdsbuf);
1814 strv_free(argv);
1815 strv_free(our_env);
1816 strv_free(final_env);
1817
034c6ed7
LP
1818 *_pid = pid;
1819
5cb5a6ff 1820 return 0;
034c6ed7
LP
1821
1822fail:
2105e76a 1823 free(fdsbuf);
c952c6ec 1824 strv_free(argv);
2105e76a
LP
1825 strv_free(our_env);
1826 strv_free(final_env);
c952c6ec 1827
034c6ed7 1828 if (timeout)
acbb0225 1829 unit_unwatch_timer(UNIT(s), &s->timer_watch);
034c6ed7
LP
1830
1831 return r;
1832}
1833
80876c20
LP
1834static int main_pid_good(Service *s) {
1835 assert(s);
1836
1837 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1838 * don't know */
1839
1840 /* If we know the pid file, then lets just check if it is
1841 * still valid */
6dfa5494
LP
1842 if (s->main_pid_known) {
1843
1844 /* If it's an alien child let's check if it is still
1845 * alive ... */
1846 if (s->main_pid_alien)
1847 return kill(s->main_pid, 0) >= 0 || errno != ESRCH;
1848
1849 /* .. otherwise assume we'll get a SIGCHLD for it,
1850 * which we really should wait for to collect exit
1851 * status and code */
80876c20 1852 return s->main_pid > 0;
6dfa5494 1853 }
80876c20
LP
1854
1855 /* We don't know the pid */
1856 return -EAGAIN;
1857}
1858
1859static int control_pid_good(Service *s) {
1860 assert(s);
1861
1862 return s->control_pid > 0;
1863}
1864
1865static int cgroup_good(Service *s) {
1866 int r;
1867
1868 assert(s);
1869
1124fe6f 1870 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->cgroup_bondings)) < 0)
80876c20
LP
1871 return r;
1872
1873 return !r;
1874}
1875
f42806df 1876static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
034c6ed7
LP
1877 int r;
1878 assert(s);
1879
f42806df
LP
1880 if (f != SERVICE_SUCCESS)
1881 s->result = f;
034c6ed7 1882
0c7f15b3
MS
1883 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1884
034c6ed7 1885 if (allow_restart &&
47342320 1886 !s->forbid_restart &&
034c6ed7 1887 (s->restart == SERVICE_RESTART_ALWAYS ||
f42806df
LP
1888 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1889 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
1890 (s->restart == SERVICE_RESTART_ON_ABORT && (s->result == SERVICE_FAILURE_SIGNAL ||
96342de6
LN
1891 s->result == SERVICE_FAILURE_CORE_DUMP))) &&
1892 (s->result != SERVICE_FAILURE_EXIT_CODE ||
1893 !set_contains(s->restart_ignore_status.code, INT_TO_PTR(s->main_exec_status.status))) &&
1894 (s->result != SERVICE_FAILURE_SIGNAL ||
1895 !set_contains(s->restart_ignore_status.signal, INT_TO_PTR(s->main_exec_status.status)))
1896 ) {
034c6ed7 1897
36697dc0 1898 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->restart_usec, &s->timer_watch);
f42806df 1899 if (r < 0)
034c6ed7
LP
1900 goto fail;
1901
1902 service_set_state(s, SERVICE_AUTO_RESTART);
0c7f15b3 1903 }
034c6ed7 1904
47342320
LP
1905 s->forbid_restart = false;
1906
034c6ed7
LP
1907 return;
1908
1909fail:
66870f90
ZJS
1910 log_warning_unit(UNIT(s)->id,
1911 "%s failed to run install restart timer: %s",
1912 UNIT(s)->id, strerror(-r));
f42806df 1913 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
034c6ed7
LP
1914}
1915
f42806df 1916static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
034c6ed7 1917
f42806df 1918static void service_enter_stop_post(Service *s, ServiceResult f) {
034c6ed7
LP
1919 int r;
1920 assert(s);
1921
f42806df
LP
1922 if (f != SERVICE_SUCCESS)
1923 s->result = f;
034c6ed7 1924
5e94833f
LP
1925 service_unwatch_control_pid(s);
1926
80876c20 1927 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
867b3b7d
LP
1928 s->control_command_id = SERVICE_EXEC_STOP_POST;
1929
ecedd90f
LP
1930 r = service_spawn(s,
1931 s->control_command,
1932 true,
1933 false,
1934 !s->permissions_start_only,
1935 !s->root_directory_start_only,
1936 true,
1937 false,
1938 true,
1939 &s->control_pid);
1940 if (r < 0)
034c6ed7
LP
1941 goto fail;
1942
d6ea93e3 1943
80876c20
LP
1944 service_set_state(s, SERVICE_STOP_POST);
1945 } else
f42806df 1946 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS);
034c6ed7
LP
1947
1948 return;
1949
1950fail:
66870f90
ZJS
1951 log_warning_unit(UNIT(s)->id,
1952 "%s failed to run 'stop-post' task: %s",
1953 UNIT(s)->id, strerror(-r));
f42806df 1954 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
034c6ed7
LP
1955}
1956
f42806df 1957static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
034c6ed7 1958 int r;
034c6ed7
LP
1959
1960 assert(s);
1961
f42806df
LP
1962 if (f != SERVICE_SUCCESS)
1963 s->result = f;
034c6ed7 1964
cd2086fe
LP
1965 r = unit_kill_context(
1966 UNIT(s),
1967 &s->kill_context,
1968 state != SERVICE_STOP_SIGTERM && state != SERVICE_FINAL_SIGTERM,
1969 s->main_pid,
1970 s->control_pid,
1971 s->main_pid_alien);
1972 if (r < 0)
1973 goto fail;
034c6ed7 1974
cd2086fe 1975 if (r > 0) {
d568a335 1976 if (s->timeout_stop_usec > 0) {
36697dc0 1977 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->timeout_stop_usec, &s->timer_watch);
d568a335 1978 if (r < 0)
e558336f 1979 goto fail;
d568a335 1980 }
d6ea93e3 1981
80876c20
LP
1982 service_set_state(s, state);
1983 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
f42806df 1984 service_enter_stop_post(s, SERVICE_SUCCESS);
80876c20 1985 else
f42806df 1986 service_enter_dead(s, SERVICE_SUCCESS, true);
034c6ed7
LP
1987
1988 return;
1989
1990fail:
66870f90
ZJS
1991 log_warning_unit(UNIT(s)->id,
1992 "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r));
034c6ed7 1993
80876c20 1994 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
f42806df 1995 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
034c6ed7 1996 else
f42806df 1997 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
034c6ed7
LP
1998}
1999
f42806df 2000static void service_enter_stop(Service *s, ServiceResult f) {
034c6ed7 2001 int r;
5925dd3c 2002
034c6ed7
LP
2003 assert(s);
2004
f42806df
LP
2005 if (f != SERVICE_SUCCESS)
2006 s->result = f;
034c6ed7 2007
5e94833f
LP
2008 service_unwatch_control_pid(s);
2009
80876c20 2010 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
867b3b7d
LP
2011 s->control_command_id = SERVICE_EXEC_STOP;
2012
ecedd90f
LP
2013 r = service_spawn(s,
2014 s->control_command,
2015 true,
2016 false,
2017 !s->permissions_start_only,
2018 !s->root_directory_start_only,
2019 false,
2020 false,
2021 true,
2022 &s->control_pid);
2023 if (r < 0)
034c6ed7
LP
2024 goto fail;
2025
80876c20
LP
2026 service_set_state(s, SERVICE_STOP);
2027 } else
f42806df 2028 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
034c6ed7
LP
2029
2030 return;
2031
2032fail:
66870f90
ZJS
2033 log_warning_unit(UNIT(s)->id,
2034 "%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r));
f42806df 2035 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
034c6ed7
LP
2036}
2037
f42806df 2038static void service_enter_running(Service *s, ServiceResult f) {
4eab639f 2039 int main_pid_ok, cgroup_ok;
80876c20
LP
2040 assert(s);
2041
f42806df
LP
2042 if (f != SERVICE_SUCCESS)
2043 s->result = f;
80876c20 2044
4eab639f
LP
2045 main_pid_ok = main_pid_good(s);
2046 cgroup_ok = cgroup_good(s);
2047
2048 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
05e343b7 2049 (s->bus_name_good || s->type != SERVICE_DBUS))
80876c20 2050 service_set_state(s, SERVICE_RUNNING);
02ee865a 2051 else if (s->remain_after_exit)
80876c20
LP
2052 service_set_state(s, SERVICE_EXITED);
2053 else
f42806df 2054 service_enter_stop(s, SERVICE_SUCCESS);
80876c20
LP
2055}
2056
034c6ed7
LP
2057static void service_enter_start_post(Service *s) {
2058 int r;
2059 assert(s);
2060
5e94833f
LP
2061 service_unwatch_control_pid(s);
2062
bb242b7b
MO
2063 if (s->watchdog_usec > 0)
2064 service_reset_watchdog(s);
2065
80876c20 2066 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
867b3b7d
LP
2067 s->control_command_id = SERVICE_EXEC_START_POST;
2068
ecedd90f
LP
2069 r = service_spawn(s,
2070 s->control_command,
2071 true,
2072 false,
2073 !s->permissions_start_only,
2074 !s->root_directory_start_only,
2075 false,
2076 false,
2077 true,
2078 &s->control_pid);
2079 if (r < 0)
034c6ed7
LP
2080 goto fail;
2081
80876c20
LP
2082 service_set_state(s, SERVICE_START_POST);
2083 } else
f42806df 2084 service_enter_running(s, SERVICE_SUCCESS);
034c6ed7
LP
2085
2086 return;
2087
2088fail:
66870f90
ZJS
2089 log_warning_unit(UNIT(s)->id,
2090 "%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r));
f42806df 2091 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
034c6ed7
LP
2092}
2093
2094static void service_enter_start(Service *s) {
2095 pid_t pid;
2096 int r;
867b3b7d 2097 ExecCommand *c;
034c6ed7
LP
2098
2099 assert(s);
2100
2101 assert(s->exec_command[SERVICE_EXEC_START]);
34e9ba66 2102 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
034c6ed7 2103
80876c20
LP
2104 if (s->type == SERVICE_FORKING)
2105 service_unwatch_control_pid(s);
2106 else
2107 service_unwatch_main_pid(s);
2108
8f53a7b8
LP
2109 /* We want to ensure that nobody leaks processes from
2110 * START_PRE here, so let's go on a killing spree, People
2111 * should not spawn long running processes from START_PRE. */
88f3e0c9 2112 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
8f53a7b8 2113
867b3b7d
LP
2114 if (s->type == SERVICE_FORKING) {
2115 s->control_command_id = SERVICE_EXEC_START;
2116 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
2117
2118 s->main_command = NULL;
2119 } else {
2120 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2121 s->control_command = NULL;
2122
2123 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
2124 }
34e9ba66 2125
ecedd90f
LP
2126 r = service_spawn(s,
2127 c,
98709151 2128 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY || s->type == SERVICE_ONESHOT,
ecedd90f
LP
2129 true,
2130 true,
2131 true,
2132 true,
2133 s->notify_access != NOTIFY_NONE,
2134 false,
2135 &pid);
2136 if (r < 0)
034c6ed7
LP
2137 goto fail;
2138
f2b68789 2139 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
034c6ed7
LP
2140 /* For simple services we immediately start
2141 * the START_POST binaries. */
2142
5925dd3c 2143 service_set_main_pid(s, pid);
034c6ed7
LP
2144 service_enter_start_post(s);
2145
2146 } else if (s->type == SERVICE_FORKING) {
2147
2148 /* For forking services we wait until the start
2149 * process exited. */
2150
e55224ca 2151 s->control_pid = pid;
80876c20
LP
2152 service_set_state(s, SERVICE_START);
2153
34e9ba66 2154 } else if (s->type == SERVICE_ONESHOT ||
8c47c732
LP
2155 s->type == SERVICE_DBUS ||
2156 s->type == SERVICE_NOTIFY) {
7d55e835 2157
34e9ba66 2158 /* For oneshot services we wait until the start
7d55e835
LP
2159 * process exited, too, but it is our main process. */
2160
05e343b7 2161 /* For D-Bus services we know the main pid right away,
8c47c732
LP
2162 * but wait for the bus name to appear on the
2163 * bus. Notify services are similar. */
05e343b7 2164
5925dd3c 2165 service_set_main_pid(s, pid);
80876c20 2166 service_set_state(s, SERVICE_START);
034c6ed7
LP
2167 } else
2168 assert_not_reached("Unknown service type");
2169
2170 return;
2171
2172fail:
66870f90
ZJS
2173 log_warning_unit(UNIT(s)->id,
2174 "%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r));
f42806df 2175 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
034c6ed7
LP
2176}
2177
2178static void service_enter_start_pre(Service *s) {
2179 int r;
2180
2181 assert(s);
2182
5e94833f
LP
2183 service_unwatch_control_pid(s);
2184
80876c20 2185 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
8f53a7b8
LP
2186
2187 /* Before we start anything, let's clear up what might
2188 * be left from previous runs. */
88f3e0c9 2189 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
8f53a7b8 2190
867b3b7d
LP
2191 s->control_command_id = SERVICE_EXEC_START_PRE;
2192
ecedd90f
LP
2193 r = service_spawn(s,
2194 s->control_command,
2195 true,
2196 false,
2197 !s->permissions_start_only,
2198 !s->root_directory_start_only,
2199 true,
2200 false,
2201 true,
2202 &s->control_pid);
2203 if (r < 0)
034c6ed7
LP
2204 goto fail;
2205
80876c20
LP
2206 service_set_state(s, SERVICE_START_PRE);
2207 } else
034c6ed7
LP
2208 service_enter_start(s);
2209
2210 return;
2211
2212fail:
66870f90
ZJS
2213 log_warning_unit(UNIT(s)->id,
2214 "%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r));
f42806df 2215 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
034c6ed7
LP
2216}
2217
2218static void service_enter_restart(Service *s) {
2219 int r;
398ef8ba
LP
2220 DBusError error;
2221
034c6ed7 2222 assert(s);
398ef8ba 2223 dbus_error_init(&error);
034c6ed7 2224
a8bb2e65
LP
2225 if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) {
2226 /* Don't restart things if we are going down anyway */
66870f90
ZJS
2227 log_info_unit(UNIT(s)->id,
2228 "Stop job pending for unit, delaying automatic restart.");
2edfa366 2229
36697dc0 2230 r = unit_watch_timer(UNIT(s), CLOCK_MONOTONIC, true, s->restart_usec, &s->timer_watch);
a8bb2e65 2231 if (r < 0)
2edfa366 2232 goto fail;
feae8adb
DW
2233
2234 return;
2edfa366
LP
2235 }
2236
48bb5876
DW
2237 /* Any units that are bound to this service must also be
2238 * restarted. We use JOB_RESTART (instead of the more obvious
2239 * JOB_START) here so that those dependency jobs will be added
2240 * as well. */
2241 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
2242 if (r < 0)
034c6ed7
LP
2243 goto fail;
2244
a8bb2e65
LP
2245 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
2246 * it will be canceled as part of the service_stop() call that
2247 * is executed as part of JOB_RESTART. */
2248
66870f90
ZJS
2249 log_debug_unit(UNIT(s)->id,
2250 "%s scheduled restart job.", UNIT(s)->id);
034c6ed7
LP
2251 return;
2252
2253fail:
66870f90
ZJS
2254 log_warning_unit(UNIT(s)->id,
2255 "%s failed to schedule restart job: %s",
2256 UNIT(s)->id, bus_error(&error, -r));
f42806df 2257 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
398ef8ba
LP
2258
2259 dbus_error_free(&error);
034c6ed7
LP
2260}
2261
2262static void service_enter_reload(Service *s) {
2263 int r;
2264
2265 assert(s);
2266
5e94833f
LP
2267 service_unwatch_control_pid(s);
2268
80876c20 2269 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
867b3b7d
LP
2270 s->control_command_id = SERVICE_EXEC_RELOAD;
2271
ecedd90f
LP
2272 r = service_spawn(s,
2273 s->control_command,
2274 true,
2275 false,
2276 !s->permissions_start_only,
2277 !s->root_directory_start_only,
2278 false,
2279 false,
2280 true,
2281 &s->control_pid);
2282 if (r < 0)
034c6ed7
LP
2283 goto fail;
2284
80876c20
LP
2285 service_set_state(s, SERVICE_RELOAD);
2286 } else
f42806df 2287 service_enter_running(s, SERVICE_SUCCESS);
034c6ed7
LP
2288
2289 return;
2290
2291fail:
66870f90
ZJS
2292 log_warning_unit(UNIT(s)->id,
2293 "%s failed to run 'reload' task: %s",
2294 UNIT(s)->id, strerror(-r));
f42806df
LP
2295 s->reload_result = SERVICE_FAILURE_RESOURCES;
2296 service_enter_running(s, SERVICE_SUCCESS);
034c6ed7
LP
2297}
2298
f42806df 2299static void service_run_next_control(Service *s) {
034c6ed7
LP
2300 int r;
2301
2302 assert(s);
2303 assert(s->control_command);
2304 assert(s->control_command->command_next);
2305
34e9ba66 2306 assert(s->control_command_id != SERVICE_EXEC_START);
034c6ed7 2307
34e9ba66 2308 s->control_command = s->control_command->command_next;
5e94833f
LP
2309 service_unwatch_control_pid(s);
2310
ecedd90f
LP
2311 r = service_spawn(s,
2312 s->control_command,
2313 true,
2314 false,
2315 !s->permissions_start_only,
2316 !s->root_directory_start_only,
2317 s->control_command_id == SERVICE_EXEC_START_PRE ||
2318 s->control_command_id == SERVICE_EXEC_STOP_POST,
2319 false,
2320 true,
2321 &s->control_pid);
2322 if (r < 0)
034c6ed7
LP
2323 goto fail;
2324
2325 return;
2326
2327fail:
66870f90
ZJS
2328 log_warning_unit(UNIT(s)->id,
2329 "%s failed to run next control task: %s",
2330 UNIT(s)->id, strerror(-r));
034c6ed7 2331
80876c20 2332 if (s->state == SERVICE_START_PRE)
f42806df 2333 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
80876c20 2334 else if (s->state == SERVICE_STOP)
f42806df 2335 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
034c6ed7 2336 else if (s->state == SERVICE_STOP_POST)
f42806df 2337 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
e2f3b44c 2338 else if (s->state == SERVICE_RELOAD) {
f42806df
LP
2339 s->reload_result = SERVICE_FAILURE_RESOURCES;
2340 service_enter_running(s, SERVICE_SUCCESS);
e2f3b44c 2341 } else
f42806df 2342 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
5cb5a6ff
LP
2343}
2344
f42806df 2345static void service_run_next_main(Service *s) {
34e9ba66
LP
2346 pid_t pid;
2347 int r;
2348
2349 assert(s);
867b3b7d
LP
2350 assert(s->main_command);
2351 assert(s->main_command->command_next);
2352 assert(s->type == SERVICE_ONESHOT);
34e9ba66 2353
867b3b7d 2354 s->main_command = s->main_command->command_next;
34e9ba66
LP
2355 service_unwatch_main_pid(s);
2356
ecedd90f
LP
2357 r = service_spawn(s,
2358 s->main_command,
98709151 2359 true,
ecedd90f
LP
2360 true,
2361 true,
2362 true,
2363 true,
2364 s->notify_access != NOTIFY_NONE,
2365 false,
2366 &pid);
2367 if (r < 0)
34e9ba66
LP
2368 goto fail;
2369
2370 service_set_main_pid(s, pid);
2371
2372 return;
2373
2374fail:
66870f90
ZJS
2375 log_warning_unit(UNIT(s)->id,
2376 "%s failed to run next main task: %s", UNIT(s)->id, strerror(-r));
f42806df 2377 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
34e9ba66
LP
2378}
2379
4b939747
MO
2380static int service_start_limit_test(Service *s) {
2381 assert(s);
2382
2383 if (ratelimit_test(&s->start_limit))
2384 return 0;
2385
2386 switch (s->start_limit_action) {
2387
2388 case SERVICE_START_LIMIT_NONE:
66870f90
ZJS
2389 log_warning_unit(UNIT(s)->id,
2390 "%s start request repeated too quickly, refusing to start.",
2391 UNIT(s)->id);
4b939747
MO
2392 break;
2393
2394 case SERVICE_START_LIMIT_REBOOT: {
2395 DBusError error;
2396 int r;
2397
2398 dbus_error_init(&error);
2399
66870f90
ZJS
2400 log_warning_unit(UNIT(s)->id,
2401 "%s start request repeated too quickly, rebooting.", UNIT(s)->id);
4b939747
MO
2402
2403 r = manager_add_job_by_name(UNIT(s)->manager, JOB_START, SPECIAL_REBOOT_TARGET, JOB_REPLACE, true, &error, NULL);
2404 if (r < 0) {
66870f90
ZJS
2405 log_error_unit(UNIT(s)->id,
2406 "Failed to reboot: %s.", bus_error(&error, r));
4b939747
MO
2407 dbus_error_free(&error);
2408 }
2409
2410 break;
2411 }
2412
2413 case SERVICE_START_LIMIT_REBOOT_FORCE:
66870f90
ZJS
2414 log_warning_unit(UNIT(s)->id,
2415 "%s start request repeated too quickly, forcibly rebooting.", UNIT(s)->id);
4b939747
MO
2416 UNIT(s)->manager->exit_code = MANAGER_REBOOT;
2417 break;
2418
2419 case SERVICE_START_LIMIT_REBOOT_IMMEDIATE:
66870f90
ZJS
2420 log_warning_unit(UNIT(s)->id,
2421 "%s start request repeated too quickly, rebooting immediately.", UNIT(s)->id);
0049f05a 2422 sync();
4b939747
MO
2423 reboot(RB_AUTOBOOT);
2424 break;
2425
2426 default:
66870f90
ZJS
2427 log_error_unit(UNIT(s)->id,
2428 "start limit action=%i", s->start_limit_action);
4b939747
MO
2429 assert_not_reached("Unknown StartLimitAction.");
2430 }
2431
2432 return -ECANCELED;
2433}
2434
87f0e418
LP
2435static int service_start(Unit *u) {
2436 Service *s = SERVICE(u);
4b939747 2437 int r;
5cb5a6ff
LP
2438
2439 assert(s);
2440
034c6ed7
LP
2441 /* We cannot fulfill this request right now, try again later
2442 * please! */
2443 if (s->state == SERVICE_STOP ||
2444 s->state == SERVICE_STOP_SIGTERM ||
2445 s->state == SERVICE_STOP_SIGKILL ||
2446 s->state == SERVICE_STOP_POST ||
2447 s->state == SERVICE_FINAL_SIGTERM ||
2448 s->state == SERVICE_FINAL_SIGKILL)
5cb5a6ff
LP
2449 return -EAGAIN;
2450
034c6ed7
LP
2451 /* Already on it! */
2452 if (s->state == SERVICE_START_PRE ||
2453 s->state == SERVICE_START ||
2454 s->state == SERVICE_START_POST)
2455 return 0;
2456
2e9d6c12 2457 /* A service that will be restarted must be stopped first to
7f2cddae 2458 * trigger BindsTo and/or OnFailure dependencies. If a user
2e9d6c12 2459 * does not want to wait for the holdoff time to elapse, the
d4943dc7
LP
2460 * service should be manually restarted, not started. We
2461 * simply return EAGAIN here, so that any start jobs stay
2462 * queued, and assume that the auto restart timer will
2463 * eventually trigger the restart. */
2464 if (s->state == SERVICE_AUTO_RESTART)
a8bb2e65 2465 return -EAGAIN;
2e9d6c12
DW
2466
2467 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
5cb5a6ff 2468
1e2e8133 2469 /* Make sure we don't enter a busy loop of some kind. */
4b939747 2470 r = service_start_limit_test(s);
c2f34808 2471 if (r < 0) {
8d1b002a 2472 service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false);
4b939747 2473 return r;
c2f34808 2474 }
1e2e8133 2475
f42806df
LP
2476 s->result = SERVICE_SUCCESS;
2477 s->reload_result = SERVICE_SUCCESS;
034c6ed7 2478 s->main_pid_known = false;
6dfa5494 2479 s->main_pid_alien = false;
47342320 2480 s->forbid_restart = false;
034c6ed7
LP
2481
2482 service_enter_start_pre(s);
2483 return 0;
5cb5a6ff
LP
2484}
2485
87f0e418
LP
2486static int service_stop(Unit *u) {
2487 Service *s = SERVICE(u);
5cb5a6ff
LP
2488
2489 assert(s);
2490
f0c7b229 2491 /* Don't create restart jobs from here. */
47342320 2492 s->forbid_restart = true;
034c6ed7 2493
e537352b
LP
2494 /* Already on it */
2495 if (s->state == SERVICE_STOP ||
2496 s->state == SERVICE_STOP_SIGTERM ||
2497 s->state == SERVICE_STOP_SIGKILL ||
2498 s->state == SERVICE_STOP_POST ||
2499 s->state == SERVICE_FINAL_SIGTERM ||
2500 s->state == SERVICE_FINAL_SIGKILL)
2501 return 0;
2502
f0c7b229 2503 /* A restart will be scheduled or is in progress. */
034c6ed7 2504 if (s->state == SERVICE_AUTO_RESTART) {
0c7f15b3 2505 service_set_state(s, SERVICE_DEAD);
034c6ed7
LP
2506 return 0;
2507 }
2508
3f6c78dc
LP
2509 /* If there's already something running we go directly into
2510 * kill mode. */
2511 if (s->state == SERVICE_START_PRE ||
2512 s->state == SERVICE_START ||
2513 s->state == SERVICE_START_POST ||
2514 s->state == SERVICE_RELOAD) {
f42806df 2515 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
3f6c78dc
LP
2516 return 0;
2517 }
5cb5a6ff 2518
3f6c78dc
LP
2519 assert(s->state == SERVICE_RUNNING ||
2520 s->state == SERVICE_EXITED);
3a762661 2521
f42806df 2522 service_enter_stop(s, SERVICE_SUCCESS);
5cb5a6ff
LP
2523 return 0;
2524}
2525
87f0e418
LP
2526static int service_reload(Unit *u) {
2527 Service *s = SERVICE(u);
034c6ed7
LP
2528
2529 assert(s);
2530
80876c20 2531 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
034c6ed7
LP
2532
2533 service_enter_reload(s);
5cb5a6ff
LP
2534 return 0;
2535}
2536
87f0e418
LP
2537static bool service_can_reload(Unit *u) {
2538 Service *s = SERVICE(u);
034c6ed7
LP
2539
2540 assert(s);
2541
2542 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2543}
2544
a16e1123
LP
2545static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2546 Service *s = SERVICE(u);
2547
2548 assert(u);
2549 assert(f);
2550 assert(fds);
2551
2552 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
f42806df
LP
2553 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
2554 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
a16e1123
LP
2555
2556 if (s->control_pid > 0)
5925dd3c 2557 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
a16e1123 2558
5925dd3c
LP
2559 if (s->main_pid_known && s->main_pid > 0)
2560 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
a16e1123
LP
2561
2562 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2563
3a2776bc
LP
2564 if (s->status_text)
2565 unit_serialize_item(u, f, "status-text", s->status_text);
2566
cfc4eb4c
LP
2567 /* FIXME: There's a minor uncleanliness here: if there are
2568 * multiple commands attached here, we will start from the
2569 * first one again */
a16e1123 2570 if (s->control_command_id >= 0)
825636e5 2571 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
a16e1123
LP
2572
2573 if (s->socket_fd >= 0) {
2574 int copy;
2575
2576 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2577 return copy;
2578
2579 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2580 }
2581
ecdbca40
LP
2582 if (s->main_exec_status.pid > 0) {
2583 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid);
799fd0fd
LP
2584 dual_timestamp_serialize(f, "main-exec-status-start", &s->main_exec_status.start_timestamp);
2585 dual_timestamp_serialize(f, "main-exec-status-exit", &s->main_exec_status.exit_timestamp);
ecdbca40 2586
799fd0fd 2587 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
ecdbca40
LP
2588 unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2589 unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2590 }
2591 }
a6927d7f
MO
2592 if (dual_timestamp_is_set(&s->watchdog_timestamp))
2593 dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp);
ecdbca40 2594
a16e1123
LP
2595 return 0;
2596}
2597
2598static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2599 Service *s = SERVICE(u);
a16e1123
LP
2600
2601 assert(u);
2602 assert(key);
2603 assert(value);
2604 assert(fds);
2605
2606 if (streq(key, "state")) {
2607 ServiceState state;
2608
2609 if ((state = service_state_from_string(value)) < 0)
66870f90 2610 log_debug_unit(u->id, "Failed to parse state value %s", value);
a16e1123
LP
2611 else
2612 s->deserialized_state = state;
f42806df
LP
2613 } else if (streq(key, "result")) {
2614 ServiceResult f;
2615
2616 f = service_result_from_string(value);
2617 if (f < 0)
66870f90 2618 log_debug_unit(u->id, "Failed to parse result value %s", value);
f42806df
LP
2619 else if (f != SERVICE_SUCCESS)
2620 s->result = f;
2621
2622 } else if (streq(key, "reload-result")) {
2623 ServiceResult f;
2624
2625 f = service_result_from_string(value);
2626 if (f < 0)
66870f90 2627 log_debug_unit(u->id, "Failed to parse reload result value %s", value);
f42806df
LP
2628 else if (f != SERVICE_SUCCESS)
2629 s->reload_result = f;
a16e1123 2630
a16e1123 2631 } else if (streq(key, "control-pid")) {
5925dd3c 2632 pid_t pid;
a16e1123 2633
e364ad06 2634 if (parse_pid(value, &pid) < 0)
66870f90 2635 log_debug_unit(u->id, "Failed to parse control-pid value %s", value);
a16e1123 2636 else
e55224ca 2637 s->control_pid = pid;
a16e1123 2638 } else if (streq(key, "main-pid")) {
5925dd3c 2639 pid_t pid;
a16e1123 2640
e364ad06 2641 if (parse_pid(value, &pid) < 0)
66870f90 2642 log_debug_unit(u->id, "Failed to parse main-pid value %s", value);
a16e1123 2643 else
5925dd3c 2644 service_set_main_pid(s, (pid_t) pid);
a16e1123
LP
2645 } else if (streq(key, "main-pid-known")) {
2646 int b;
2647
2648 if ((b = parse_boolean(value)) < 0)
66870f90 2649 log_debug_unit(u->id, "Failed to parse main-pid-known value %s", value);
a16e1123
LP
2650 else
2651 s->main_pid_known = b;
3a2776bc
LP
2652 } else if (streq(key, "status-text")) {
2653 char *t;
2654
2655 if ((t = strdup(value))) {
2656 free(s->status_text);
2657 s->status_text = t;
2658 }
2659
a16e1123
LP
2660 } else if (streq(key, "control-command")) {
2661 ServiceExecCommand id;
2662
2663 if ((id = service_exec_command_from_string(value)) < 0)
66870f90 2664 log_debug_unit(u->id, "Failed to parse exec-command value %s", value);
a16e1123
LP
2665 else {
2666 s->control_command_id = id;
2667 s->control_command = s->exec_command[id];
2668 }
2669 } else if (streq(key, "socket-fd")) {
2670 int fd;
2671
2672 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
66870f90 2673 log_debug_unit(u->id, "Failed to parse socket-fd value %s", value);
a16e1123
LP
2674 else {
2675
2676 if (s->socket_fd >= 0)
2677 close_nointr_nofail(s->socket_fd);
2678 s->socket_fd = fdset_remove(fds, fd);
2679 }
ecdbca40
LP
2680 } else if (streq(key, "main-exec-status-pid")) {
2681 pid_t pid;
2682
e364ad06 2683 if (parse_pid(value, &pid) < 0)
66870f90 2684 log_debug_unit(u->id, "Failed to parse main-exec-status-pid value %s", value);
ecdbca40
LP
2685 else
2686 s->main_exec_status.pid = pid;
2687 } else if (streq(key, "main-exec-status-code")) {
2688 int i;
2689
e364ad06 2690 if (safe_atoi(value, &i) < 0)
66870f90 2691 log_debug_unit(u->id, "Failed to parse main-exec-status-code value %s", value);
ecdbca40
LP
2692 else
2693 s->main_exec_status.code = i;
2694 } else if (streq(key, "main-exec-status-status")) {
2695 int i;
2696
e364ad06 2697 if (safe_atoi(value, &i) < 0)
66870f90 2698 log_debug_unit(u->id, "Failed to parse main-exec-status-status value %s", value);
ecdbca40
LP
2699 else
2700 s->main_exec_status.status = i;
799fd0fd
LP
2701 } else if (streq(key, "main-exec-status-start"))
2702 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2703 else if (streq(key, "main-exec-status-exit"))
2704 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
a6927d7f
MO
2705 else if (streq(key, "watchdog-timestamp"))
2706 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
799fd0fd 2707 else
66870f90 2708 log_debug_unit(u->id, "Unknown serialization key '%s'", key);
a16e1123
LP
2709
2710 return 0;
2711}
2712
87f0e418 2713static UnitActiveState service_active_state(Unit *u) {
e056b01d
LP
2714 const UnitActiveState *table;
2715
87f0e418 2716 assert(u);
5cb5a6ff 2717
e056b01d
LP
2718 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
2719
2720 return table[SERVICE(u)->state];
034c6ed7
LP
2721}
2722
10a94420
LP
2723static const char *service_sub_state_to_string(Unit *u) {
2724 assert(u);
2725
2726 return service_state_to_string(SERVICE(u)->state);
2727}
2728
701cc384
LP
2729static bool service_check_gc(Unit *u) {
2730 Service *s = SERVICE(u);
2731
2732 assert(s);
2733
6d55002a
LP
2734 /* Never clean up services that still have a process around,
2735 * even if the service is formally dead. */
2736 if (cgroup_good(s) > 0 ||
2737 main_pid_good(s) > 0 ||
2738 control_pid_good(s) > 0)
2739 return true;
2740
2741#ifdef HAVE_SYSV_COMPAT
1b64d026 2742 if (s->is_sysv)
6d55002a 2743 return true;
07459bb6 2744#endif
701cc384 2745
6d55002a
LP
2746 return false;
2747}
2748
701cc384
LP
2749static bool service_check_snapshot(Unit *u) {
2750 Service *s = SERVICE(u);
2751
2752 assert(s);
2753
2754 return !s->got_socket_fd;
2755}
2756
3a111838
MS
2757static int service_retry_pid_file(Service *s) {
2758 int r;
2759
2760 assert(s->pid_file);
2761 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2762
2763 r = service_load_pid_file(s, false);
2764 if (r < 0)
2765 return r;
2766
2767 service_unwatch_pid_file(s);
2768
f42806df 2769 service_enter_running(s, SERVICE_SUCCESS);
3a111838
MS
2770 return 0;
2771}
2772
2773static int service_watch_pid_file(Service *s) {
2774 int r;
2775
66870f90
ZJS
2776 log_debug_unit(UNIT(s)->id,
2777 "Setting watch for %s's PID file %s",
2778 UNIT(s)->id, s->pid_file_pathspec->path);
57020a3a 2779 r = path_spec_watch(s->pid_file_pathspec, UNIT(s));
3a111838
MS
2780 if (r < 0)
2781 goto fail;
2782
2783 /* the pidfile might have appeared just before we set the watch */
2784 service_retry_pid_file(s);
2785
2786 return 0;
2787fail:
66870f90
ZJS
2788 log_error_unit(UNIT(s)->id,
2789 "Failed to set a watch for %s's PID file %s: %s",
2790 UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r));
3a111838
MS
2791 service_unwatch_pid_file(s);
2792 return r;
2793}
2794
2795static int service_demand_pid_file(Service *s) {
2796 PathSpec *ps;
2797
2798 assert(s->pid_file);
2799 assert(!s->pid_file_pathspec);
2800
2801 ps = new0(PathSpec, 1);
2802 if (!ps)
2803 return -ENOMEM;
2804
2805 ps->path = strdup(s->pid_file);
2806 if (!ps->path) {
2807 free(ps);
2808 return -ENOMEM;
2809 }
2810
2811 path_kill_slashes(ps->path);
2812
2813 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2814 * keep their PID file open all the time. */
2815 ps->type = PATH_MODIFIED;
2816 ps->inotify_fd = -1;
2817
2818 s->pid_file_pathspec = ps;
2819
2820 return service_watch_pid_file(s);
2821}
2822
2823static void service_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
2824 Service *s = SERVICE(u);
2825
2826 assert(s);
2827 assert(fd >= 0);
2828 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2829 assert(s->pid_file_pathspec);
57020a3a 2830 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
3a111838 2831
66870f90 2832 log_debug_unit(u->id, "inotify event for %s", u->id);
3a111838 2833
57020a3a 2834 if (path_spec_fd_event(s->pid_file_pathspec, events) < 0)
3a111838
MS
2835 goto fail;
2836
2837 if (service_retry_pid_file(s) == 0)
2838 return;
2839
2840 if (service_watch_pid_file(s) < 0)
2841 goto fail;
2842
2843 return;
2844fail:
2845 service_unwatch_pid_file(s);
f42806df 2846 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
3a111838
MS
2847}
2848
87f0e418
LP
2849static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2850 Service *s = SERVICE(u);
f42806df 2851 ServiceResult f;
5cb5a6ff
LP
2852
2853 assert(s);
034c6ed7
LP
2854 assert(pid >= 0);
2855
96342de6
LN
2856 if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) :
2857 is_clean_exit_lsb(code, status, &s->success_status))
f42806df
LP
2858 f = SERVICE_SUCCESS;
2859 else if (code == CLD_EXITED)
2860 f = SERVICE_FAILURE_EXIT_CODE;
2861 else if (code == CLD_KILLED)
2862 f = SERVICE_FAILURE_SIGNAL;
2863 else if (code == CLD_DUMPED)
2864 f = SERVICE_FAILURE_CORE_DUMP;
d06dacd0 2865 else
cfc4eb4c 2866 assert_not_reached("Unknown code");
034c6ed7
LP
2867
2868 if (s->main_pid == pid) {
db01f8b3
MS
2869 /* Forking services may occasionally move to a new PID.
2870 * As long as they update the PID file before exiting the old
2871 * PID, they're fine. */
5375410b 2872 if (service_load_pid_file(s, false) == 0)
db01f8b3 2873 return;
034c6ed7 2874
034c6ed7 2875 s->main_pid = 0;
6ea832a2 2876 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
034c6ed7 2877
867b3b7d 2878 if (s->main_command) {
fbeefb45
LP
2879 /* If this is not a forking service than the
2880 * main process got started and hence we copy
2881 * the exit status so that it is recorded both
2882 * as main and as control process exit
2883 * status */
2884
867b3b7d 2885 s->main_command->exec_status = s->main_exec_status;
b708e7ce 2886
867b3b7d 2887 if (s->main_command->ignore)
f42806df 2888 f = SERVICE_SUCCESS;
fbeefb45
LP
2889 } else if (s->exec_command[SERVICE_EXEC_START]) {
2890
2891 /* If this is a forked process, then we should
2892 * ignore the return value if this was
2893 * configured for the starter process */
2894
2895 if (s->exec_command[SERVICE_EXEC_START]->ignore)
2896 f = SERVICE_SUCCESS;
034c6ed7
LP
2897 }
2898
bbc9006e
MT
2899 log_struct_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2900 u->id,
23635a85
ZJS
2901 "MESSAGE=%s: main process exited, code=%s, status=%i/%s",
2902 u->id, sigchld_code_to_string(code), status,
2903 strna(code == CLD_EXITED
2904 ? exit_status_to_string(status, EXIT_STATUS_FULL)
2905 : signal_to_string(status)),
23635a85
ZJS
2906 "EXIT_CODE=%s", sigchld_code_to_string(code),
2907 "EXIT_STATUS=%i", status,
2908 NULL);
f42806df
LP
2909
2910 if (f != SERVICE_SUCCESS)
2911 s->result = f;
034c6ed7 2912
867b3b7d
LP
2913 if (s->main_command &&
2914 s->main_command->command_next &&
f42806df 2915 f == SERVICE_SUCCESS) {
034c6ed7 2916
34e9ba66
LP
2917 /* There is another command to *
2918 * execute, so let's do that. */
034c6ed7 2919
66870f90
ZJS
2920 log_debug_unit(u->id,
2921 "%s running next main command for state %s",
2922 u->id, service_state_to_string(s->state));
f42806df 2923 service_run_next_main(s);
034c6ed7 2924
34e9ba66
LP
2925 } else {
2926
2927 /* The service exited, so the service is officially
2928 * gone. */
867b3b7d 2929 s->main_command = NULL;
34e9ba66
LP
2930
2931 switch (s->state) {
2932
2933 case SERVICE_START_POST:
2934 case SERVICE_RELOAD:
2935 case SERVICE_STOP:
2936 /* Need to wait until the operation is
2937 * done */
c4653a4d 2938 break;
7d55e835 2939
34e9ba66
LP
2940 case SERVICE_START:
2941 if (s->type == SERVICE_ONESHOT) {
2942 /* This was our main goal, so let's go on */
f42806df 2943 if (f == SERVICE_SUCCESS)
34e9ba66
LP
2944 service_enter_start_post(s);
2945 else
f42806df 2946 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
34e9ba66 2947 break;
34e9ba66 2948 }
034c6ed7 2949
bfba3256
LP
2950 /* Fall through */
2951
34e9ba66 2952 case SERVICE_RUNNING:
f42806df 2953 service_enter_running(s, f);
34e9ba66 2954 break;
034c6ed7 2955
34e9ba66
LP
2956 case SERVICE_STOP_SIGTERM:
2957 case SERVICE_STOP_SIGKILL:
5cb5a6ff 2958
34e9ba66 2959 if (!control_pid_good(s))
f42806df 2960 service_enter_stop_post(s, f);
5cb5a6ff 2961
34e9ba66
LP
2962 /* If there is still a control process, wait for that first */
2963 break;
2964
2965 default:
2966 assert_not_reached("Uh, main process died at wrong time.");
2967 }
034c6ed7 2968 }
5cb5a6ff 2969
034c6ed7 2970 } else if (s->control_pid == pid) {
034c6ed7 2971
34e9ba66
LP
2972 s->control_pid = 0;
2973
b708e7ce 2974 if (s->control_command) {
6ea832a2 2975 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
a16e1123 2976
b708e7ce 2977 if (s->control_command->ignore)
f42806df 2978 f = SERVICE_SUCCESS;
b708e7ce
LP
2979 }
2980
66870f90
ZJS
2981 log_full_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id,
2982 "%s: control process exited, code=%s status=%i",
2983 u->id, sigchld_code_to_string(code), status);
f42806df
LP
2984
2985 if (f != SERVICE_SUCCESS)
2986 s->result = f;
034c6ed7 2987
88f3e0c9
LP
2988 /* Immediately get rid of the cgroup, so that the
2989 * kernel doesn't delay the cgroup empty messages for
2990 * the service cgroup any longer than necessary */
2991 cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, SIGKILL, true, true, NULL, "control");
2992
34e9ba66
LP
2993 if (s->control_command &&
2994 s->control_command->command_next &&
f42806df 2995 f == SERVICE_SUCCESS) {
034c6ed7
LP
2996
2997 /* There is another command to *
2998 * execute, so let's do that. */
2999
66870f90
ZJS
3000 log_debug_unit(u->id,
3001 "%s running next control command for state %s",
3002 u->id, service_state_to_string(s->state));
f42806df 3003 service_run_next_control(s);
034c6ed7 3004
80876c20 3005 } else {
034c6ed7
LP
3006 /* No further commands for this step, so let's
3007 * figure out what to do next */
3008
a16e1123
LP
3009 s->control_command = NULL;
3010 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
3011
66870f90
ZJS
3012 log_debug_unit(u->id,
3013 "%s got final SIGCHLD for state %s",
3014 u->id, service_state_to_string(s->state));
bd982a8b 3015
034c6ed7
LP
3016 switch (s->state) {
3017
3018 case SERVICE_START_PRE:
f42806df 3019 if (f == SERVICE_SUCCESS)
034c6ed7
LP
3020 service_enter_start(s);
3021 else
f42806df 3022 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
034c6ed7
LP
3023 break;
3024
3025 case SERVICE_START:
bfba3256
LP
3026 if (s->type != SERVICE_FORKING)
3027 /* Maybe spurious event due to a reload that changed the type? */
3028 break;
034c6ed7 3029
f42806df
LP
3030 if (f != SERVICE_SUCCESS) {
3031 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3a111838
MS
3032 break;
3033 }
034c6ed7 3034
3a111838 3035 if (s->pid_file) {
f42806df
LP
3036 bool has_start_post;
3037 int r;
3038
3a111838
MS
3039 /* Let's try to load the pid file here if we can.
3040 * The PID file might actually be created by a START_POST
3041 * script. In that case don't worry if the loading fails. */
f42806df
LP
3042
3043 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
3044 r = service_load_pid_file(s, !has_start_post);
3a111838
MS
3045 if (!has_start_post && r < 0) {
3046 r = service_demand_pid_file(s);
3047 if (r < 0 || !cgroup_good(s))
f42806df 3048 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3a111838
MS
3049 break;
3050 }
034c6ed7 3051 } else
3a111838 3052 service_search_main_pid(s);
034c6ed7 3053
3a111838 3054 service_enter_start_post(s);
034c6ed7
LP
3055 break;
3056
3057 case SERVICE_START_POST:
f42806df
LP
3058 if (f != SERVICE_SUCCESS) {
3059 service_enter_stop(s, f);
2096e009 3060 break;
034c6ed7
LP
3061 }
3062
2096e009 3063 if (s->pid_file) {
f42806df
LP
3064 int r;
3065
3066 r = service_load_pid_file(s, true);
2096e009
MS
3067 if (r < 0) {
3068 r = service_demand_pid_file(s);
3069 if (r < 0 || !cgroup_good(s))
f42806df 3070 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2096e009
MS
3071 break;
3072 }
3073 } else
3074 service_search_main_pid(s);
3075
f42806df 3076 service_enter_running(s, SERVICE_SUCCESS);
3185a36b 3077 break;
034c6ed7
LP
3078
3079 case SERVICE_RELOAD:
f42806df 3080 if (f == SERVICE_SUCCESS) {
5375410b 3081 service_load_pid_file(s, true);
3185a36b
LP
3082 service_search_main_pid(s);
3083 }
3084
f42806df
LP
3085 s->reload_result = f;
3086 service_enter_running(s, SERVICE_SUCCESS);
034c6ed7
LP
3087 break;
3088
3089 case SERVICE_STOP:
f42806df 3090 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
034c6ed7
LP
3091 break;
3092
3093 case SERVICE_STOP_SIGTERM:
3094 case SERVICE_STOP_SIGKILL:
3095 if (main_pid_good(s) <= 0)
f42806df 3096 service_enter_stop_post(s, f);
034c6ed7
LP
3097
3098 /* If there is still a service
3099 * process around, wait until
3100 * that one quit, too */
3101 break;
3102
3103 case SERVICE_STOP_POST:
3104 case SERVICE_FINAL_SIGTERM:
3105 case SERVICE_FINAL_SIGKILL:
f42806df 3106 service_enter_dead(s, f, true);
034c6ed7
LP
3107 break;
3108
3109 default:
3110 assert_not_reached("Uh, control process died at wrong time.");
3111 }
3112 }
8c47c732 3113 }
c4e2ceae
LP
3114
3115 /* Notify clients about changed exit status */
3116 unit_add_to_dbus_queue(u);
034c6ed7
LP
3117}
3118
acbb0225 3119static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
87f0e418 3120 Service *s = SERVICE(u);
034c6ed7
LP
3121
3122 assert(s);
3123 assert(elapsed == 1);
3124
bb242b7b
MO
3125 if (w == &s->watchdog_watch) {
3126 service_handle_watchdog(s);
3127 return;
3128 }
3129
acbb0225 3130 assert(w == &s->timer_watch);
034c6ed7
LP
3131
3132 switch (s->state) {
3133
3134 case SERVICE_START_PRE:
3135 case SERVICE_START:
66870f90
ZJS
3136 log_warning_unit(u->id,
3137 "%s operation timed out. Terminating.", u->id);
f42806df 3138 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
80876c20
LP
3139 break;
3140
034c6ed7 3141 case SERVICE_START_POST:
66870f90
ZJS
3142 log_warning_unit(u->id,
3143 "%s operation timed out. Stopping.", u->id);
f42806df 3144 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
034c6ed7
LP
3145 break;
3146
e2f3b44c 3147 case SERVICE_RELOAD:
66870f90
ZJS
3148 log_warning_unit(u->id,
3149 "%s operation timed out. Stopping.", u->id);
f42806df
LP
3150 s->reload_result = SERVICE_FAILURE_TIMEOUT;
3151 service_enter_running(s, SERVICE_SUCCESS);
e2f3b44c
LP
3152 break;
3153
034c6ed7 3154 case SERVICE_STOP:
66870f90
ZJS
3155 log_warning_unit(u->id,
3156 "%s stopping timed out. Terminating.", u->id);
f42806df 3157 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
034c6ed7
LP
3158 break;
3159
3160 case SERVICE_STOP_SIGTERM:
4819ff03 3161 if (s->kill_context.send_sigkill) {
66870f90
ZJS
3162 log_warning_unit(u->id,
3163 "%s stopping timed out. Killing.", u->id);
f42806df 3164 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
ba035df2 3165 } else {
66870f90
ZJS
3166 log_warning_unit(u->id,
3167 "%s stopping timed out. Skipping SIGKILL.", u->id);
f42806df 3168 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
ba035df2
LP
3169 }
3170
034c6ed7
LP
3171 break;
3172
3173 case SERVICE_STOP_SIGKILL:
35b8ca3a 3174 /* Uh, we sent a SIGKILL and it is still not gone?
034c6ed7
LP
3175 * Must be something we cannot kill, so let's just be
3176 * weirded out and continue */
3177
66870f90
ZJS
3178 log_warning_unit(u->id,
3179 "%s still around after SIGKILL. Ignoring.", u->id);
f42806df 3180 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
034c6ed7
LP
3181 break;
3182
3183 case SERVICE_STOP_POST:
66870f90
ZJS
3184 log_warning_unit(u->id,
3185 "%s stopping timed out (2). Terminating.", u->id);
f42806df 3186 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
034c6ed7
LP
3187 break;
3188
3189 case SERVICE_FINAL_SIGTERM:
4819ff03 3190 if (s->kill_context.send_sigkill) {
66870f90
ZJS
3191 log_warning_unit(u->id,
3192 "%s stopping timed out (2). Killing.", u->id);
f42806df 3193 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
ba035df2 3194 } else {
66870f90
ZJS
3195 log_warning_unit(u->id,
3196 "%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.",
3197 u->id);
f42806df 3198 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
ba035df2
LP
3199 }
3200
034c6ed7
LP
3201 break;
3202
3203 case SERVICE_FINAL_SIGKILL:
66870f90
ZJS
3204 log_warning_unit(u->id,
3205 "%s still around after SIGKILL (2). Entering failed mode.", u->id);
f42806df 3206 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
034c6ed7
LP
3207 break;
3208
3209 case SERVICE_AUTO_RESTART:
66870f90
ZJS
3210 log_info_unit(u->id,
3211 "%s holdoff time over, scheduling restart.", u->id);
034c6ed7
LP
3212 service_enter_restart(s);
3213 break;
3214
3215 default:
3216 assert_not_reached("Timeout at wrong time.");
3217 }
5cb5a6ff
LP
3218}
3219
8e274523
LP
3220static void service_cgroup_notify_event(Unit *u) {
3221 Service *s = SERVICE(u);
3222
3223 assert(u);
3224
66870f90
ZJS
3225 log_debug_unit(u->id,
3226 "%s: cgroup is empty", u->id);
8e274523
LP
3227
3228 switch (s->state) {
3229
3230 /* Waiting for SIGCHLD is usually more interesting,
3231 * because it includes return codes/signals. Which is
3232 * why we ignore the cgroup events for most cases,
3233 * except when we don't know pid which to expect the
3234 * SIGCHLD for. */
3235
3a111838
MS
3236 case SERVICE_START:
3237 case SERVICE_START_POST:
3238 /* If we were hoping for the daemon to write its PID file,
3239 * we can give up now. */
3240 if (s->pid_file_pathspec) {
66870f90
ZJS
3241 log_warning_unit(u->id,
3242 "%s never wrote its PID file. Failing.", UNIT(s)->id);
3a111838
MS
3243 service_unwatch_pid_file(s);
3244 if (s->state == SERVICE_START)
f42806df 3245 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3a111838 3246 else
f42806df 3247 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
3a111838
MS
3248 }
3249 break;
3250
8e274523 3251 case SERVICE_RUNNING:
f42806df
LP
3252 /* service_enter_running() will figure out what to do */
3253 service_enter_running(s, SERVICE_SUCCESS);
8e274523
LP
3254 break;
3255
28708d8a
LP
3256 case SERVICE_STOP_SIGTERM:
3257 case SERVICE_STOP_SIGKILL:
6dfa5494 3258
28708d8a 3259 if (main_pid_good(s) <= 0 && !control_pid_good(s))
f42806df 3260 service_enter_stop_post(s, SERVICE_SUCCESS);
28708d8a
LP
3261
3262 break;
3263
7f97f0fe
LP
3264 case SERVICE_FINAL_SIGTERM:
3265 case SERVICE_FINAL_SIGKILL:
3266 if (main_pid_good(s) <= 0 && !control_pid_good(s))
e201a038 3267 service_enter_dead(s, SERVICE_SUCCESS, true);
7f97f0fe
LP
3268
3269 break;
3270
8e274523
LP
3271 default:
3272 ;
3273 }
3274}
3275
c952c6ec 3276static void service_notify_message(Unit *u, pid_t pid, char **tags) {
8c47c732
LP
3277 Service *s = SERVICE(u);
3278 const char *e;
3279
3280 assert(u);
3281
c952c6ec 3282 if (s->notify_access == NOTIFY_NONE) {
66870f90
ZJS
3283 log_warning_unit(u->id,
3284 "%s: Got notification message from PID %lu, but reception is disabled.",
3285 u->id, (unsigned long) pid);
c952c6ec
LP
3286 return;
3287 }
3288
3289 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
66870f90
ZJS
3290 log_warning_unit(u->id,
3291 "%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
3292 u->id, (unsigned long) pid, (unsigned long) s->main_pid);
c952c6ec
LP
3293 return;
3294 }
3295
66870f90
ZJS
3296 log_debug_unit(u->id,
3297 "%s: Got message", u->id);
8c47c732
LP
3298
3299 /* Interpret MAINPID= */
3300 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
3301 (s->state == SERVICE_START ||
3302 s->state == SERVICE_START_POST ||
3303 s->state == SERVICE_RUNNING ||
3304 s->state == SERVICE_RELOAD)) {
8c47c732 3305
5925dd3c 3306 if (parse_pid(e + 8, &pid) < 0)
66870f90
ZJS
3307 log_warning_unit(u->id,
3308 "Failed to parse notification message %s", e);
8c47c732 3309 else {
66870f90
ZJS
3310 log_debug_unit(u->id,
3311 "%s: got %s", u->id, e);
5925dd3c 3312 service_set_main_pid(s, pid);
8c47c732
LP
3313 }
3314 }
3315
3316 /* Interpret READY= */
3317 if (s->type == SERVICE_NOTIFY &&
3318 s->state == SERVICE_START &&
3319 strv_find(tags, "READY=1")) {
66870f90
ZJS
3320 log_debug_unit(u->id,
3321 "%s: got READY=1", u->id);
8c47c732
LP
3322
3323 service_enter_start_post(s);
3324 }
3325
3326 /* Interpret STATUS= */
7f110ff9
LP
3327 e = strv_find_prefix(tags, "STATUS=");
3328 if (e) {
8c47c732
LP
3329 char *t;
3330
3a2776bc 3331 if (e[7]) {
7f110ff9
LP
3332
3333 if (!utf8_is_valid(e+7)) {
66870f90
ZJS
3334 log_warning_unit(u->id,
3335 "Status message in notification is not UTF-8 clean.");
7f110ff9
LP
3336 return;
3337 }
3338
3339 t = strdup(e+7);
3340 if (!t) {
66870f90
ZJS
3341 log_error_unit(u->id,
3342 "Failed to allocate string.");
3a2776bc
LP
3343 return;
3344 }
3345
66870f90
ZJS
3346 log_debug_unit(u->id,
3347 "%s: got %s", u->id, e);
8c47c732 3348
3a2776bc
LP
3349 free(s->status_text);
3350 s->status_text = t;
3351 } else {
3352 free(s->status_text);
3353 s->status_text = NULL;
3354 }
8c47c732 3355
8c47c732 3356 }
a6927d7f 3357 if (strv_find(tags, "WATCHDOG=1")) {
66870f90
ZJS
3358 log_debug_unit(u->id,
3359 "%s: got WATCHDOG=1", u->id);
90527fbb
MO
3360 if (dual_timestamp_is_set(&s->watchdog_timestamp))
3361 service_reset_watchdog(s);
a6927d7f 3362 }
c4e2ceae
LP
3363
3364 /* Notify clients about changed status or main pid */
3365 unit_add_to_dbus_queue(u);
8c47c732
LP
3366}
3367
07459bb6 3368#ifdef HAVE_SYSV_COMPAT
de3910a3 3369
2c4104f0 3370static int service_enumerate(Manager *m) {
2c4104f0
LP
3371 char **p;
3372 unsigned i;
3373 DIR *d = NULL;
3374 char *path = NULL, *fpath = NULL, *name = NULL;
c68364b7
LP
3375 Set *runlevel_services[ELEMENTSOF(rcnd_table)], *shutdown_services = NULL;
3376 Unit *service;
3377 Iterator j;
2c4104f0
LP
3378 int r;
3379
3380 assert(m);
3381
67445f4e 3382 if (m->running_as != SYSTEMD_SYSTEM)
b1bc08e5
LP
3383 return 0;
3384
c68364b7
LP
3385 zero(runlevel_services);
3386
84e3543e 3387 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
09cd1ab1 3388 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
2c4104f0
LP
3389 struct dirent *de;
3390
3391 free(path);
b7def684 3392 path = strjoin(*p, "/", rcnd_table[i].path, NULL);
70132bd0 3393 if (!path) {
2c4104f0
LP
3394 r = -ENOMEM;
3395 goto finish;
3396 }
3397
3398 if (d)
3399 closedir(d);
3400
3401 if (!(d = opendir(path))) {
3402 if (errno != ENOENT)
3403 log_warning("opendir() failed on %s: %s", path, strerror(errno));
3404
3405 continue;
3406 }
3407
3408 while ((de = readdir(d))) {
db06e3b6 3409 int a, b;
2c4104f0
LP
3410
3411 if (ignore_file(de->d_name))
3412 continue;
3413
3414 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
3415 continue;
3416
3417 if (strlen(de->d_name) < 4)
3418 continue;
3419
db06e3b6
LP
3420 a = undecchar(de->d_name[1]);
3421 b = undecchar(de->d_name[2]);
3422
3423 if (a < 0 || b < 0)
3424 continue;
3425
2c4104f0 3426 free(fpath);
b7def684 3427 fpath = strjoin(path, "/", de->d_name, NULL);
8ea913b2 3428 if (!fpath) {
2c4104f0
LP
3429 r = -ENOMEM;
3430 goto finish;
3431 }
3432
3433 if (access(fpath, X_OK) < 0) {
3434
3435 if (errno != ENOENT)
3436 log_warning("access() failed on %s: %s", fpath, strerror(errno));
3437
3438 continue;
3439 }
3440
3441 free(name);
b7ccee3c 3442 if (!(name = sysv_translate_name(de->d_name + 3))) {
2c4104f0
LP
3443 r = -ENOMEM;
3444 goto finish;
3445 }
3446
66870f90
ZJS
3447 r = manager_load_unit_prepare(m, name, NULL, NULL, &service);
3448 if (r < 0) {
fbe9f3a9
LP
3449 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
3450 continue;
3451 }
2c4104f0 3452
c68364b7
LP
3453 if (de->d_name[0] == 'S') {
3454
3cdebc21 3455 if (rcnd_table[i].type == RUNLEVEL_UP) {
ea87ca5a
LP
3456 SERVICE(service)->sysv_start_priority_from_rcnd =
3457 MAX(a*10 + b, SERVICE(service)->sysv_start_priority_from_rcnd);
db06e3b6 3458
c68364b7 3459 SERVICE(service)->sysv_enabled = true;
f73d93a4 3460 }
db06e3b6 3461
c68364b7
LP
3462 if ((r = set_ensure_allocated(&runlevel_services[i], trivial_hash_func, trivial_compare_func)) < 0)
3463 goto finish;
2c4104f0 3464
c68364b7 3465 if ((r = set_put(runlevel_services[i], service)) < 0)
2c4104f0 3466 goto finish;
23a177ef 3467
fc5df99e 3468 } else if (de->d_name[0] == 'K' &&
3cdebc21 3469 (rcnd_table[i].type == RUNLEVEL_DOWN)) {
6542952f 3470
c68364b7
LP
3471 if ((r = set_ensure_allocated(&shutdown_services, trivial_hash_func, trivial_compare_func)) < 0)
3472 goto finish;
3473
3474 if ((r = set_put(shutdown_services, service)) < 0)
2c4104f0
LP
3475 goto finish;
3476 }
3477 }
3478 }
3479
c68364b7
LP
3480 /* Now we loaded all stubs and are aware of the lowest
3481 start-up priority for all services, not let's actually load
3482 the services, this will also tell us which services are
3483 actually native now */
3484 manager_dispatch_load_queue(m);
3485
3486 /* If this is a native service, rely on native ways to pull in
3487 * a service, don't pull it in via sysv rcN.d links. */
3488 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
3489 SET_FOREACH(service, runlevel_services[i], j) {
3490 service = unit_follow_merge(service);
3491
ac155bb8 3492 if (service->fragment_path)
c68364b7
LP
3493 continue;
3494
3495 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
3496 goto finish;
3497 }
3498
3499 /* We honour K links only for halt/reboot. For the normal
3500 * runlevels we assume the stop jobs will be implicitly added
35b8ca3a 3501 * by the core logic. Also, we don't really distinguish here
c68364b7 3502 * between the runlevels 0 and 6 and just add them to the
3cdebc21 3503 * special shutdown target. */
c68364b7
LP
3504 SET_FOREACH(service, shutdown_services, j) {
3505 service = unit_follow_merge(service);
3506
ac155bb8 3507 if (service->fragment_path)
c68364b7
LP
3508 continue;
3509
ead8e478 3510 if ((r = unit_add_two_dependencies_by_name(service, UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
c68364b7
LP
3511 goto finish;
3512 }
3513
2c4104f0
LP
3514 r = 0;
3515
3516finish:
3517 free(path);
3518 free(fpath);
3519 free(name);
fbe9f3a9 3520
c68364b7
LP
3521 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
3522 set_free(runlevel_services[i]);
3523 set_free(shutdown_services);
3524
fbe9f3a9
LP
3525 if (d)
3526 closedir(d);
2c4104f0
LP
3527
3528 return r;
3529}
07459bb6 3530#endif
2c4104f0 3531
05e343b7
LP
3532static void service_bus_name_owner_change(
3533 Unit *u,
3534 const char *name,
3535 const char *old_owner,
3536 const char *new_owner) {
3537
3538 Service *s = SERVICE(u);
3539
3540 assert(s);
3541 assert(name);
3542
3543 assert(streq(s->bus_name, name));
3544 assert(old_owner || new_owner);
3545
3546 if (old_owner && new_owner)
66870f90
ZJS
3547 log_debug_unit(u->id,
3548 "%s's D-Bus name %s changed owner from %s to %s",
3549 u->id, name, old_owner, new_owner);
05e343b7 3550 else if (old_owner)
66870f90
ZJS
3551 log_debug_unit(u->id,
3552 "%s's D-Bus name %s no longer registered by %s",
3553 u->id, name, old_owner);
05e343b7 3554 else
66870f90
ZJS
3555 log_debug_unit(u->id,
3556 "%s's D-Bus name %s now registered by %s",
3557 u->id, name, new_owner);
05e343b7
LP
3558
3559 s->bus_name_good = !!new_owner;
3560
3561 if (s->type == SERVICE_DBUS) {
3562
3563 /* service_enter_running() will figure out what to
3564 * do */
3565 if (s->state == SERVICE_RUNNING)
f42806df 3566 service_enter_running(s, SERVICE_SUCCESS);
05e343b7
LP
3567 else if (s->state == SERVICE_START && new_owner)
3568 service_enter_start_post(s);
3569
3570 } else if (new_owner &&
3571 s->main_pid <= 0 &&
3572 (s->state == SERVICE_START ||
3573 s->state == SERVICE_START_POST ||
3574 s->state == SERVICE_RUNNING ||
3575 s->state == SERVICE_RELOAD)) {
3576
3577 /* Try to acquire PID from bus service */
66870f90
ZJS
3578 log_debug_unit(u->id,
3579 "Trying to acquire PID from D-Bus name...");
05e343b7 3580
ac155bb8 3581 bus_query_pid(u->manager, name);
05e343b7
LP
3582 }
3583}
3584
3585static void service_bus_query_pid_done(
3586 Unit *u,
3587 const char *name,
3588 pid_t pid) {
3589
3590 Service *s = SERVICE(u);
3591
3592 assert(s);
3593 assert(name);
3594
66870f90
ZJS
3595 log_debug_unit(u->id,
3596 "%s's D-Bus name %s is now owned by process %u",
3597 u->id, name, (unsigned) pid);
05e343b7
LP
3598
3599 if (s->main_pid <= 0 &&
3600 (s->state == SERVICE_START ||
3601 s->state == SERVICE_START_POST ||
3602 s->state == SERVICE_RUNNING ||
3603 s->state == SERVICE_RELOAD))
5925dd3c 3604 service_set_main_pid(s, pid);
05e343b7
LP
3605}
3606
6cf6bbc2 3607int service_set_socket_fd(Service *s, int fd, Socket *sock) {
57020a3a 3608
4f2d528d
LP
3609 assert(s);
3610 assert(fd >= 0);
3611
3612 /* This is called by the socket code when instantiating a new
3613 * service for a stream socket and the socket needs to be
3614 * configured. */
3615
1124fe6f 3616 if (UNIT(s)->load_state != UNIT_LOADED)
4f2d528d
LP
3617 return -EINVAL;
3618
3619 if (s->socket_fd >= 0)
3620 return -EBUSY;
3621
3622 if (s->state != SERVICE_DEAD)
3623 return -EAGAIN;
3624
3625 s->socket_fd = fd;
701cc384 3626 s->got_socket_fd = true;
6cf6bbc2 3627
57020a3a
LP
3628 unit_ref_set(&s->accept_socket, UNIT(sock));
3629
3630 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
4f2d528d
LP
3631}
3632
fdf20a31 3633static void service_reset_failed(Unit *u) {
5632e374
LP
3634 Service *s = SERVICE(u);
3635
3636 assert(s);
3637
fdf20a31 3638 if (s->state == SERVICE_FAILED)
5632e374
LP
3639 service_set_state(s, SERVICE_DEAD);
3640
f42806df
LP
3641 s->result = SERVICE_SUCCESS;
3642 s->reload_result = SERVICE_SUCCESS;
451b34cc
LP
3643
3644 RATELIMIT_RESET(s->start_limit);
5632e374
LP
3645}
3646
c74f17d9 3647static int service_kill(Unit *u, KillWho who, int signo, DBusError *error) {
8a0867d6
LP
3648 Service *s = SERVICE(u);
3649 int r = 0;
3650 Set *pid_set = NULL;
3651
3652 assert(s);
3653
3654 if (s->main_pid <= 0 && who == KILL_MAIN) {
3655 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No main process to kill");
a17204af 3656 return -ESRCH;
8a0867d6
LP
3657 }
3658
3659 if (s->control_pid <= 0 && who == KILL_CONTROL) {
3660 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
a17204af 3661 return -ESRCH;
8a0867d6
LP
3662 }
3663
3611581e
LP
3664 if (who == KILL_CONTROL || who == KILL_ALL)
3665 if (s->control_pid > 0)
3666 if (kill(s->control_pid, signo) < 0)
3667 r = -errno;
8a0867d6 3668
3611581e
LP
3669 if (who == KILL_MAIN || who == KILL_ALL)
3670 if (s->main_pid > 0)
3671 if (kill(s->main_pid, signo) < 0)
3672 r = -errno;
8a0867d6 3673
c74f17d9 3674 if (who == KILL_ALL) {
8a0867d6
LP
3675 int q;
3676
c74f17d9
LP
3677 pid_set = set_new(trivial_hash_func, trivial_compare_func);
3678 if (!pid_set)
8a0867d6
LP
3679 return -ENOMEM;
3680
3681 /* Exclude the control/main pid from being killed via the cgroup */
c74f17d9
LP
3682 if (s->control_pid > 0) {
3683 q = set_put(pid_set, LONG_TO_PTR(s->control_pid));
3684 if (q < 0) {
8a0867d6
LP
3685 r = q;
3686 goto finish;
3687 }
c74f17d9 3688 }
8a0867d6 3689
c74f17d9
LP
3690 if (s->main_pid > 0) {
3691 q = set_put(pid_set, LONG_TO_PTR(s->main_pid));
3692 if (q < 0) {
8a0867d6
LP
3693 r = q;
3694 goto finish;
3695 }
c74f17d9
LP
3696 }
3697
88f3e0c9 3698 q = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, signo, false, false, pid_set, NULL);
c74f17d9
LP
3699 if (q < 0 && q != -EAGAIN && q != -ESRCH && q != -ENOENT)
3700 r = q;
8a0867d6
LP
3701 }
3702
3703finish:
3704 if (pid_set)
3705 set_free(pid_set);
3706
3707 return r;
3708}
3709
94f04347
LP
3710static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3711 [SERVICE_DEAD] = "dead",
3712 [SERVICE_START_PRE] = "start-pre",
3713 [SERVICE_START] = "start",
3714 [SERVICE_START_POST] = "start-post",
3715 [SERVICE_RUNNING] = "running",
80876c20 3716 [SERVICE_EXITED] = "exited",
94f04347
LP
3717 [SERVICE_RELOAD] = "reload",
3718 [SERVICE_STOP] = "stop",
3719 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3720 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3721 [SERVICE_STOP_POST] = "stop-post",
3722 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3723 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
fdf20a31 3724 [SERVICE_FAILED] = "failed",
94f04347
LP
3725 [SERVICE_AUTO_RESTART] = "auto-restart",
3726};
3727
3728DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3729
3730static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
525ee6f4
LP
3731 [SERVICE_RESTART_NO] = "no",
3732 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
50caaedb
LP
3733 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3734 [SERVICE_RESTART_ON_ABORT] = "on-abort",
3735 [SERVICE_RESTART_ALWAYS] = "always"
94f04347
LP
3736};
3737
3738DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3739
3740static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
94f04347 3741 [SERVICE_SIMPLE] = "simple",
0d624a78 3742 [SERVICE_FORKING] = "forking",
34e9ba66 3743 [SERVICE_ONESHOT] = "oneshot",
8c47c732 3744 [SERVICE_DBUS] = "dbus",
f2b68789
LP
3745 [SERVICE_NOTIFY] = "notify",
3746 [SERVICE_IDLE] = "idle"
94f04347
LP
3747};
3748
3749DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3750
e537352b 3751static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
94f04347
LP
3752 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3753 [SERVICE_EXEC_START] = "ExecStart",
3754 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3755 [SERVICE_EXEC_RELOAD] = "ExecReload",
3756 [SERVICE_EXEC_STOP] = "ExecStop",
3757 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3758};
3759
3760DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3761
c952c6ec
LP
3762static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3763 [NOTIFY_NONE] = "none",
3764 [NOTIFY_MAIN] = "main",
3765 [NOTIFY_ALL] = "all"
3766};
3767
3768DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3769
f42806df
LP
3770static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3771 [SERVICE_SUCCESS] = "success",
3772 [SERVICE_FAILURE_RESOURCES] = "resources",
3773 [SERVICE_FAILURE_TIMEOUT] = "timeout",
3774 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3775 [SERVICE_FAILURE_SIGNAL] = "signal",
bb242b7b 3776 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
8d1b002a
LP
3777 [SERVICE_FAILURE_WATCHDOG] = "watchdog",
3778 [SERVICE_FAILURE_START_LIMIT] = "start-limit"
f42806df
LP
3779};
3780
3781DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3782
4b939747
MO
3783static const char* const start_limit_action_table[_SERVICE_START_LIMIT_MAX] = {
3784 [SERVICE_START_LIMIT_NONE] = "none",
3785 [SERVICE_START_LIMIT_REBOOT] = "reboot",
3786 [SERVICE_START_LIMIT_REBOOT_FORCE] = "reboot-force",
3787 [SERVICE_START_LIMIT_REBOOT_IMMEDIATE] = "reboot-immediate"
3788};
3789DEFINE_STRING_TABLE_LOOKUP(start_limit_action, StartLimitAction);
3790
87f0e418 3791const UnitVTable service_vtable = {
7d17cfbc 3792 .object_size = sizeof(Service),
3ef63c31 3793
f975e971
LP
3794 .sections =
3795 "Unit\0"
3796 "Service\0"
3797 "Install\0",
5cb5a6ff 3798
71645aca
LP
3799 .exec_context_offset = offsetof(Service, exec_context),
3800 .exec_section = "Service",
3801
034c6ed7
LP
3802 .init = service_init,
3803 .done = service_done,
a16e1123
LP
3804 .load = service_load,
3805
3806 .coldplug = service_coldplug,
034c6ed7 3807
5cb5a6ff
LP
3808 .dump = service_dump,
3809
3810 .start = service_start,
3811 .stop = service_stop,
3812 .reload = service_reload,
3813
034c6ed7
LP
3814 .can_reload = service_can_reload,
3815
8a0867d6
LP
3816 .kill = service_kill,
3817
a16e1123
LP
3818 .serialize = service_serialize,
3819 .deserialize_item = service_deserialize_item,
3820
5cb5a6ff 3821 .active_state = service_active_state,
10a94420 3822 .sub_state_to_string = service_sub_state_to_string,
5cb5a6ff 3823
701cc384
LP
3824 .check_gc = service_check_gc,
3825 .check_snapshot = service_check_snapshot,
3826
034c6ed7
LP
3827 .sigchld_event = service_sigchld_event,
3828 .timer_event = service_timer_event,
3a111838 3829 .fd_event = service_fd_event,
2c4104f0 3830
fdf20a31 3831 .reset_failed = service_reset_failed,
5632e374 3832
8e274523 3833 .cgroup_notify_empty = service_cgroup_notify_event,
8c47c732 3834 .notify_message = service_notify_message,
8e274523 3835
05e343b7
LP
3836 .bus_name_owner_change = service_bus_name_owner_change,
3837 .bus_query_pid_done = service_bus_query_pid_done,
3838
c4e2ceae 3839 .bus_interface = "org.freedesktop.systemd1.Service",
4139c1b2 3840 .bus_message_handler = bus_service_message_handler,
c4e2ceae 3841 .bus_invalidating_properties = bus_service_invalidating_properties,
4139c1b2 3842
07459bb6 3843#ifdef HAVE_SYSV_COMPAT
c6918296 3844 .enumerate = service_enumerate,
07459bb6 3845#endif
c6918296
MS
3846 .status_message_formats = {
3847 .starting_stopping = {
3848 [0] = "Starting %s...",
3849 [1] = "Stopping %s...",
3850 },
3851 .finished_start_job = {
3852 [JOB_DONE] = "Started %s.",
3853 [JOB_FAILED] = "Failed to start %s.",
3854 [JOB_DEPENDENCY] = "Dependency failed for %s.",
3855 [JOB_TIMEOUT] = "Timed out starting %s.",
3856 },
3857 .finished_stop_job = {
3858 [JOB_DONE] = "Stopped %s.",
3859 [JOB_FAILED] = "Stopped (with error) %s.",
3860 [JOB_TIMEOUT] = "Timed out stopping %s.",
3861 },
3862 },
5cb5a6ff 3863};