]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/service.c
conf-parse: don't accept invalid bus names as BusName= arguments in service units
[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>
5cb5a6ff 26
574634bc 27#include "async.h"
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"
9a57c629 39#include "exit-status.h"
f6a6225e 40#include "def.h"
9eb977db 41#include "path-util.h"
f6a6225e 42#include "util.h"
7f110ff9 43#include "utf8.h"
4d1a6904 44#include "env-util.h"
a5c32cff 45#include "fileio.h"
718db961
LP
46#include "bus-error.h"
47#include "bus-util.h"
e44da745 48#include "bus-kernel.h"
034c6ed7 49
acbb0225 50static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
87f0e418
LP
51 [SERVICE_DEAD] = UNIT_INACTIVE,
52 [SERVICE_START_PRE] = UNIT_ACTIVATING,
53 [SERVICE_START] = UNIT_ACTIVATING,
54 [SERVICE_START_POST] = UNIT_ACTIVATING,
55 [SERVICE_RUNNING] = UNIT_ACTIVE,
80876c20 56 [SERVICE_EXITED] = UNIT_ACTIVE,
032ff4af 57 [SERVICE_RELOAD] = UNIT_RELOADING,
87f0e418 58 [SERVICE_STOP] = UNIT_DEACTIVATING,
db2cb23b 59 [SERVICE_STOP_SIGABRT] = UNIT_DEACTIVATING,
87f0e418
LP
60 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
61 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
62 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
63 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
64 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
fdf20a31 65 [SERVICE_FAILED] = UNIT_FAILED,
6124958c 66 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
034c6ed7 67};
5cb5a6ff 68
e056b01d
LP
69/* For Type=idle we never want to delay any other jobs, hence we
70 * consider idle jobs active as soon as we start working on them */
71static const UnitActiveState state_translation_table_idle[_SERVICE_STATE_MAX] = {
72 [SERVICE_DEAD] = UNIT_INACTIVE,
73 [SERVICE_START_PRE] = UNIT_ACTIVE,
74 [SERVICE_START] = UNIT_ACTIVE,
75 [SERVICE_START_POST] = UNIT_ACTIVE,
76 [SERVICE_RUNNING] = UNIT_ACTIVE,
77 [SERVICE_EXITED] = UNIT_ACTIVE,
78 [SERVICE_RELOAD] = UNIT_RELOADING,
79 [SERVICE_STOP] = UNIT_DEACTIVATING,
db2cb23b 80 [SERVICE_STOP_SIGABRT] = UNIT_DEACTIVATING,
e056b01d
LP
81 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
82 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
83 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
84 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
85 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
86 [SERVICE_FAILED] = UNIT_FAILED,
87 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
88};
89
718db961
LP
90static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata);
91static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
92static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata);
93
842129f5 94static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
308d72dc 95static void service_enter_reload_by_notify(Service *s);
842129f5 96
a16e1123
LP
97static void service_init(Unit *u) {
98 Service *s = SERVICE(u);
99
100 assert(u);
ac155bb8 101 assert(u->load_state == UNIT_STUB);
a16e1123 102
1f19a534
OS
103 s->timeout_start_usec = u->manager->default_timeout_start_usec;
104 s->timeout_stop_usec = u->manager->default_timeout_stop_usec;
105 s->restart_usec = u->manager->default_restart_usec;
0b86feac 106 s->type = _SERVICE_TYPE_INVALID;
a16e1123 107 s->socket_fd = -1;
e44da745 108 s->bus_endpoint_fd = -1;
3185a36b 109 s->guess_main_pid = true;
a16e1123 110
085afe36 111 RATELIMIT_INIT(s->start_limit, u->manager->default_start_limit_interval, u->manager->default_start_limit_burst);
a16e1123
LP
112
113 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
114}
115
5e94833f
LP
116static void service_unwatch_control_pid(Service *s) {
117 assert(s);
118
119 if (s->control_pid <= 0)
120 return;
121
122 unit_unwatch_pid(UNIT(s), s->control_pid);
123 s->control_pid = 0;
124}
125
126static void service_unwatch_main_pid(Service *s) {
127 assert(s);
128
129 if (s->main_pid <= 0)
130 return;
131
132 unit_unwatch_pid(UNIT(s), s->main_pid);
133 s->main_pid = 0;
134}
135
3e52541e
MS
136static void service_unwatch_pid_file(Service *s) {
137 if (!s->pid_file_pathspec)
138 return;
139
79008bdd 140 log_unit_debug(UNIT(s)->id, "Stopping watch for %s's PID file %s", UNIT(s)->id, s->pid_file_pathspec->path);
718db961 141 path_spec_unwatch(s->pid_file_pathspec);
3e52541e
MS
142 path_spec_done(s->pid_file_pathspec);
143 free(s->pid_file_pathspec);
144 s->pid_file_pathspec = NULL;
145}
146
5925dd3c 147static int service_set_main_pid(Service *s, pid_t pid) {
e55224ca
LP
148 pid_t ppid;
149
5925dd3c
LP
150 assert(s);
151
152 if (pid <= 1)
153 return -EINVAL;
154
155 if (pid == getpid())
156 return -EINVAL;
157
7400b9d2
LP
158 if (s->main_pid == pid && s->main_pid_known)
159 return 0;
160
161 if (s->main_pid != pid) {
162 service_unwatch_main_pid(s);
163 exec_status_start(&s->main_exec_status, pid);
164 }
41efeaec 165
6dfa5494
LP
166 s->main_pid = pid;
167 s->main_pid_known = true;
168
169 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) {
79008bdd 170 log_unit_warning(UNIT(s)->id, "%s: Supervising process "PID_FMT" which is not our child. We'll most likely not notice when it exits.", UNIT(s)->id, pid);
6dfa5494
LP
171 s->main_pid_alien = true;
172 } else
173 s->main_pid_alien = false;
5925dd3c
LP
174
175 return 0;
176}
177
4f2d528d
LP
178static void service_close_socket_fd(Service *s) {
179 assert(s);
180
574634bc 181 s->socket_fd = asynchronous_close(s->socket_fd);
4f2d528d
LP
182}
183
6cf6bbc2
LP
184static void service_connection_unref(Service *s) {
185 assert(s);
186
9444b1f2 187 if (!UNIT_ISSET(s->accept_socket))
6cf6bbc2
LP
188 return;
189
57020a3a
LP
190 socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket)));
191 unit_ref_unset(&s->accept_socket);
6cf6bbc2
LP
192}
193
a6927d7f
MO
194static void service_stop_watchdog(Service *s) {
195 assert(s);
196
718db961 197 s->watchdog_event_source = sd_event_source_unref(s->watchdog_event_source);
842129f5 198 s->watchdog_timestamp = DUAL_TIMESTAMP_NULL;
a6927d7f
MO
199}
200
842129f5 201static void service_start_watchdog(Service *s) {
bb242b7b
MO
202 int r;
203
204 assert(s);
205
842129f5 206 if (s->watchdog_usec <= 0)
bb242b7b
MO
207 return;
208
718db961
LP
209 if (s->watchdog_event_source) {
210 r = sd_event_source_set_time(s->watchdog_event_source, s->watchdog_timestamp.monotonic + s->watchdog_usec);
211 if (r < 0) {
31938a85 212 log_unit_warning_errno(UNIT(s)->id, r, "%s failed to reset watchdog timer: %m", UNIT(s)->id);
718db961
LP
213 return;
214 }
215
842129f5 216 r = sd_event_source_set_enabled(s->watchdog_event_source, SD_EVENT_ONESHOT);
c4ef3317 217 } else {
6a0f1f6d
LP
218 r = sd_event_add_time(
219 UNIT(s)->manager->event,
220 &s->watchdog_event_source,
221 CLOCK_MONOTONIC,
222 s->watchdog_timestamp.monotonic + s->watchdog_usec, 0,
223 service_dispatch_watchdog, s);
c4ef3317 224 if (r < 0) {
31938a85 225 log_unit_warning_errno(UNIT(s)->id, r, "%s failed to add watchdog timer: %m", UNIT(s)->id);
c4ef3317
LP
226 return;
227 }
228
229 /* Let's process everything else which might be a sign
230 * of living before we consider a service died. */
231 r = sd_event_source_set_priority(s->watchdog_event_source, SD_EVENT_PRIORITY_IDLE);
232 }
718db961 233
bb242b7b 234 if (r < 0)
31938a85 235 log_unit_warning_errno(UNIT(s)->id, r, "%s failed to install watchdog timer: %m", UNIT(s)->id);
bb242b7b
MO
236}
237
a6927d7f
MO
238static void service_reset_watchdog(Service *s) {
239 assert(s);
240
241 dual_timestamp_get(&s->watchdog_timestamp);
842129f5 242 service_start_watchdog(s);
a6927d7f
MO
243}
244
a354329f
LP
245static void service_fd_store_unlink(ServiceFDStore *fs) {
246
247 if (!fs)
248 return;
249
250 if (fs->service) {
251 assert(fs->service->n_fd_store > 0);
252 LIST_REMOVE(fd_store, fs->service->fd_store, fs);
253 fs->service->n_fd_store--;
254 }
255
256 if (fs->event_source) {
257 sd_event_source_set_enabled(fs->event_source, SD_EVENT_OFF);
258 sd_event_source_unref(fs->event_source);
259 }
260
261 safe_close(fs->fd);
262 free(fs);
263}
264
265static void service_release_resources(Unit *u) {
266 Service *s = SERVICE(u);
267
268 assert(s);
269
270 if (!s->fd_store)
271 return;
272
273 log_debug("Releasing all resources for %s", u->id);
274
275 while (s->fd_store)
276 service_fd_store_unlink(s->fd_store);
277
278 assert(s->n_fd_store == 0);
279}
280
87f0e418
LP
281static void service_done(Unit *u) {
282 Service *s = SERVICE(u);
44d8db9e
LP
283
284 assert(s);
285
286 free(s->pid_file);
287 s->pid_file = NULL;
288
8c47c732
LP
289 free(s->status_text);
290 s->status_text = NULL;
291
efe6e7d3
MO
292 free(s->reboot_arg);
293 s->reboot_arg = NULL;
294
613b411c 295 s->exec_runtime = exec_runtime_unref(s->exec_runtime);
e537352b 296 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
44d8db9e 297 s->control_command = NULL;
867b3b7d 298 s->main_command = NULL;
44d8db9e 299
37520c1b
LP
300 exit_status_set_free(&s->restart_prevent_status);
301 exit_status_set_free(&s->restart_force_status);
302 exit_status_set_free(&s->success_status);
96342de6 303
44d8db9e
LP
304 /* This will leak a process, but at least no memory or any of
305 * our resources */
5e94833f
LP
306 service_unwatch_main_pid(s);
307 service_unwatch_control_pid(s);
3e52541e 308 service_unwatch_pid_file(s);
44d8db9e 309
05e343b7 310 if (s->bus_name) {
ac155bb8 311 unit_unwatch_bus_name(u, s->bus_name);
05e343b7
LP
312 free(s->bus_name);
313 s->bus_name = NULL;
314 }
315
e44da745 316 s->bus_endpoint_fd = safe_close(s->bus_endpoint_fd);
4f2d528d 317 service_close_socket_fd(s);
6cf6bbc2 318 service_connection_unref(s);
4f2d528d 319
57020a3a 320 unit_ref_unset(&s->accept_socket);
f976f3f6 321
bb242b7b
MO
322 service_stop_watchdog(s);
323
718db961 324 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
a354329f
LP
325
326 service_release_resources(u);
718db961
LP
327}
328
2339fc93
LP
329static int on_fd_store_io(sd_event_source *e, int fd, uint32_t revents, void *userdata) {
330 ServiceFDStore *fs = userdata;
331
332 assert(e);
333 assert(fs);
334
335 /* If we get either EPOLLHUP or EPOLLERR, it's time to remove this entry from the fd store */
336 service_fd_store_unlink(fs);
337 return 0;
338}
339
340static int service_add_fd_store(Service *s, int fd) {
341 ServiceFDStore *fs;
342 int r;
343
344 assert(s);
345 assert(fd >= 0);
346
347 if (s->n_fd_store >= s->n_fd_store_max)
348 return 0;
349
350 LIST_FOREACH(fd_store, fs, s->fd_store) {
351 r = same_fd(fs->fd, fd);
352 if (r < 0)
353 return r;
354 if (r > 0) {
355 /* Already included */
356 safe_close(fd);
357 return 1;
358 }
359 }
360
361 fs = new0(ServiceFDStore, 1);
362 if (!fs)
363 return -ENOMEM;
364
365 fs->fd = fd;
366 fs->service = s;
367
368 r = sd_event_add_io(UNIT(s)->manager->event, &fs->event_source, fd, 0, on_fd_store_io, fs);
369 if (r < 0) {
370 free(fs);
371 return r;
372 }
373
374 LIST_PREPEND(fd_store, s->fd_store, fs);
375 s->n_fd_store++;
376
377 return 1;
378}
379
380static int service_add_fd_store_set(Service *s, FDSet *fds) {
381 int r;
382
383 assert(s);
384
385 if (fdset_size(fds) <= 0)
386 return 0;
387
388 while (s->n_fd_store < s->n_fd_store_max) {
389 _cleanup_close_ int fd = -1;
390
391 fd = fdset_steal_first(fds);
392 if (fd < 0)
393 break;
394
395 r = service_add_fd_store(s, fd);
396 if (r < 0)
397 return log_unit_error_errno(UNIT(s)->id, r, "%s: Couldn't add fd to fd store: %m", UNIT(s)->id);
398
399 if (r > 0) {
400 log_unit_debug(UNIT(s)->id, "%s: added fd to fd store.", UNIT(s)->id);
401 fd = -1;
402 }
403 }
404
405 if (fdset_size(fds) > 0)
406 log_unit_warning(UNIT(s)->id, "%s: tried to store more fds than FDStoreMax=%u allows, closing remaining.", UNIT(s)->id, s->n_fd_store_max);
407
408 return 0;
409}
410
718db961
LP
411static int service_arm_timer(Service *s, usec_t usec) {
412 int r;
413
414 assert(s);
415
718db961
LP
416 if (s->timer_event_source) {
417 r = sd_event_source_set_time(s->timer_event_source, now(CLOCK_MONOTONIC) + usec);
418 if (r < 0)
419 return r;
420
421 return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
422 }
423
6a0f1f6d
LP
424 return sd_event_add_time(
425 UNIT(s)->manager->event,
426 &s->timer_event_source,
427 CLOCK_MONOTONIC,
428 now(CLOCK_MONOTONIC) + usec, 0,
429 service_dispatch_timer, s);
44d8db9e
LP
430}
431
243b1432
LP
432static int service_verify(Service *s) {
433 assert(s);
434
1124fe6f 435 if (UNIT(s)->load_state != UNIT_LOADED)
243b1432
LP
436 return 0;
437
96fb8242 438 if (!s->exec_command[SERVICE_EXEC_START] && !s->exec_command[SERVICE_EXEC_STOP]) {
79008bdd 439 log_unit_error(UNIT(s)->id, "%s lacks both ExecStart= and ExecStop= setting. Refusing.", UNIT(s)->id);
243b1432
LP
440 return -EINVAL;
441 }
442
96fb8242 443 if (s->type != SERVICE_ONESHOT && !s->exec_command[SERVICE_EXEC_START]) {
79008bdd 444 log_unit_error(UNIT(s)->id, "%s has no ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
96fb8242
LP
445 return -EINVAL;
446 }
447
448 if (!s->remain_after_exit && !s->exec_command[SERVICE_EXEC_START]) {
79008bdd 449 log_unit_error(UNIT(s)->id, "%s has no ExecStart= setting, which is only allowed for RemainAfterExit=yes services. Refusing.", UNIT(s)->id);
96fb8242
LP
450 return -EINVAL;
451 }
452
453 if (s->type != SERVICE_ONESHOT && s->exec_command[SERVICE_EXEC_START]->command_next) {
79008bdd 454 log_unit_error(UNIT(s)->id, "%s has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
6cf6bbc2
LP
455 return -EINVAL;
456 }
457
b0693d30 458 if (s->type == SERVICE_ONESHOT && s->restart != SERVICE_RESTART_NO) {
79008bdd 459 log_unit_error(UNIT(s)->id, "%s has Restart= setting other than no, which isn't allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
37520c1b
LP
460 return -EINVAL;
461 }
462
55ebf98c 463 if (s->type == SERVICE_ONESHOT && !exit_status_set_is_empty(&s->restart_force_status)) {
79008bdd 464 log_unit_error(UNIT(s)->id, "%s has RestartForceStatus= set, which isn't allowed for Type=oneshot services. Refusing.", UNIT(s)->id);
b0693d30
MW
465 return -EINVAL;
466 }
467
05e343b7 468 if (s->type == SERVICE_DBUS && !s->bus_name) {
79008bdd 469 log_unit_error(UNIT(s)->id, "%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->id);
4d0e5dbd
LP
470 return -EINVAL;
471 }
472
7e2668c6 473 if (s->bus_name && s->type != SERVICE_DBUS)
79008bdd 474 log_unit_warning(UNIT(s)->id, "%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id);
7e2668c6 475
ebc2259d 476 if (s->exec_context.pam_name && !(s->kill_context.kill_mode == KILL_CONTROL_GROUP || s->kill_context.kill_mode == KILL_MIXED)) {
79008bdd 477 log_unit_error(UNIT(s)->id, "%s has PAM enabled. Kill mode must be set to 'control-group' or 'mixed'. Refusing.", UNIT(s)->id);
05e343b7
LP
478 return -EINVAL;
479 }
480
243b1432
LP
481 return 0;
482}
483
a40eb732
LP
484static int service_add_default_dependencies(Service *s) {
485 int r;
486
487 assert(s);
488
489 /* Add a number of automatic dependencies useful for the
490 * majority of services. */
491
492 /* First, pull in base system */
8bb2d17d 493 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true);
fccd44ec
KS
494 if (r < 0)
495 return r;
a40eb732
LP
496
497 /* Second, activate normal shutdown */
8bb2d17d 498 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
117dcc57 499 return r;
a40eb732
LP
500}
501
4dfc092a
LP
502static void service_fix_output(Service *s) {
503 assert(s);
504
505 /* If nothing has been explicitly configured, patch default
506 * output in. If input is socket/tty we avoid this however,
507 * since in that case we want output to default to the same
508 * place as we read input from. */
509
510 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
511 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
512 s->exec_context.std_input == EXEC_INPUT_NULL)
1124fe6f 513 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
4dfc092a
LP
514
515 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
516 s->exec_context.std_input == EXEC_INPUT_NULL)
1124fe6f 517 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
4dfc092a
LP
518}
519
e537352b 520static int service_load(Unit *u) {
e537352b 521 Service *s = SERVICE(u);
8bb2d17d 522 int r;
e537352b
LP
523
524 assert(s);
1e2e8133 525
5cb5a6ff 526 /* Load a .service file */
c2756a68
LP
527 r = unit_load_fragment(u);
528 if (r < 0)
5cb5a6ff
LP
529 return r;
530
23a177ef 531 /* Still nothing found? Then let's give up */
ac155bb8 532 if (u->load_state == UNIT_STUB)
23a177ef 533 return -ENOENT;
034c6ed7 534
23a177ef 535 /* This is a new unit? Then let's add in some extras */
ac155bb8 536 if (u->load_state == UNIT_LOADED) {
c2756a68
LP
537
538 /* We were able to load something, then let's add in
539 * the dropin directories. */
540 r = unit_load_dropin(u);
541 if (r < 0)
542 return r;
543
96fb8242
LP
544 if (s->type == _SERVICE_TYPE_INVALID) {
545 /* Figure out a type automatically */
546 if (s->bus_name)
547 s->type = SERVICE_DBUS;
548 else if (s->exec_command[SERVICE_EXEC_START])
549 s->type = SERVICE_SIMPLE;
550 else
551 s->type = SERVICE_ONESHOT;
552 }
0b86feac 553
d568a335
MS
554 /* Oneshot services have disabled start timeout by default */
555 if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined)
556 s->timeout_start_usec = 0;
98709151 557
4e2b0f9b
LP
558 service_fix_output(s);
559
598459ce
LP
560 r = unit_patch_contexts(u);
561 if (r < 0)
562 return r;
563
117dcc57
ZJS
564 r = unit_add_exec_dependencies(u, &s->exec_context);
565 if (r < 0)
23a177ef
LP
566 return r;
567
598459ce 568 r = unit_add_default_slice(u, &s->cgroup_context);
a016b922
LP
569 if (r < 0)
570 return r;
571
c952c6ec
LP
572 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
573 s->notify_access = NOTIFY_MAIN;
a40eb732 574
02c4ef9c
LP
575 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
576 s->notify_access = NOTIFY_MAIN;
577
598459ce
LP
578 if (s->bus_name) {
579 r = unit_watch_bus_name(u, s->bus_name);
117dcc57 580 if (r < 0)
a40eb732 581 return r;
117dcc57 582 }
e06c73cc 583
598459ce
LP
584 if (u->default_dependencies) {
585 r = service_add_default_dependencies(s);
586 if (r < 0)
817e224b 587
598459ce
LP
588 return r;
589 }
8e274523
LP
590 }
591
243b1432 592 return service_verify(s);
034c6ed7
LP
593}
594
87f0e418 595static void service_dump(Unit *u, FILE *f, const char *prefix) {
5cb5a6ff 596 ServiceExecCommand c;
87f0e418 597 Service *s = SERVICE(u);
47be870b 598 const char *prefix2;
5cb5a6ff
LP
599
600 assert(s);
601
4c940960
LP
602 prefix = strempty(prefix);
603 prefix2 = strappenda(prefix, "\t");
44d8db9e 604
5cb5a6ff 605 fprintf(f,
81a2b7ce 606 "%sService State: %s\n"
f42806df
LP
607 "%sResult: %s\n"
608 "%sReload Result: %s\n"
81a2b7ce 609 "%sPermissionsStartOnly: %s\n"
8e274523 610 "%sRootDirectoryStartOnly: %s\n"
02ee865a 611 "%sRemainAfterExit: %s\n"
3185a36b 612 "%sGuessMainPID: %s\n"
c952c6ec 613 "%sType: %s\n"
2cf3143a 614 "%sRestart: %s\n"
308d72dc
LP
615 "%sNotifyAccess: %s\n"
616 "%sNotifyState: %s\n",
81a2b7ce 617 prefix, service_state_to_string(s->state),
f42806df
LP
618 prefix, service_result_to_string(s->result),
619 prefix, service_result_to_string(s->reload_result),
81a2b7ce 620 prefix, yes_no(s->permissions_start_only),
8e274523 621 prefix, yes_no(s->root_directory_start_only),
02ee865a 622 prefix, yes_no(s->remain_after_exit),
3185a36b 623 prefix, yes_no(s->guess_main_pid),
c952c6ec 624 prefix, service_type_to_string(s->type),
2cf3143a 625 prefix, service_restart_to_string(s->restart),
308d72dc
LP
626 prefix, notify_access_to_string(s->notify_access),
627 prefix, notify_state_to_string(s->notify_state));
5cb5a6ff 628
70123e68
LP
629 if (s->control_pid > 0)
630 fprintf(f,
ccd06097
ZJS
631 "%sControl PID: "PID_FMT"\n",
632 prefix, s->control_pid);
70123e68
LP
633
634 if (s->main_pid > 0)
635 fprintf(f,
ccd06097 636 "%sMain PID: "PID_FMT"\n"
6dfa5494
LP
637 "%sMain PID Known: %s\n"
638 "%sMain PID Alien: %s\n",
ccd06097 639 prefix, s->main_pid,
6dfa5494
LP
640 prefix, yes_no(s->main_pid_known),
641 prefix, yes_no(s->main_pid_alien));
70123e68 642
034c6ed7
LP
643 if (s->pid_file)
644 fprintf(f,
645 "%sPIDFile: %s\n",
646 prefix, s->pid_file);
647
05e343b7
LP
648 if (s->bus_name)
649 fprintf(f,
650 "%sBusName: %s\n"
651 "%sBus Name Good: %s\n",
652 prefix, s->bus_name,
653 prefix, yes_no(s->bus_name_good));
654
4819ff03 655 kill_context_dump(&s->kill_context, f, prefix);
5cb5a6ff
LP
656 exec_context_dump(&s->exec_context, f, prefix);
657
e537352b 658 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
5cb5a6ff 659
44d8db9e
LP
660 if (!s->exec_command[c])
661 continue;
662
40d50879 663 fprintf(f, "%s-> %s:\n",
94f04347 664 prefix, service_exec_command_to_string(c));
44d8db9e
LP
665
666 exec_command_dump_list(s->exec_command[c], f, prefix2);
5cb5a6ff 667 }
44d8db9e 668
8c47c732
LP
669 if (s->status_text)
670 fprintf(f, "%sStatus Text: %s\n",
671 prefix, s->status_text);
a354329f
LP
672
673 if (s->n_fd_store_max > 0) {
674 fprintf(f,
675 "%sFile Descriptor Store Max: %u\n"
676 "%sFile Descriptor Store Current: %u\n",
677 prefix, s->n_fd_store_max,
678 prefix, s->n_fd_store);
679 }
5cb5a6ff
LP
680}
681
c5419d42 682static int service_load_pid_file(Service *s, bool may_warn) {
7fd1b19b 683 _cleanup_free_ char *k = NULL;
034c6ed7 684 int r;
5925dd3c 685 pid_t pid;
034c6ed7
LP
686
687 assert(s);
688
034c6ed7 689 if (!s->pid_file)
13230d5d 690 return -ENOENT;
034c6ed7 691
117dcc57
ZJS
692 r = read_one_line_file(s->pid_file, &k);
693 if (r < 0) {
c5419d42 694 if (may_warn)
79008bdd 695 log_unit_info(UNIT(s)->id, "PID file %s not readable (yet?) after %s.", s->pid_file, service_state_to_string(s->state));
034c6ed7 696 return r;
5375410b 697 }
034c6ed7 698
5925dd3c 699 r = parse_pid(k, &pid);
bc41f93e
ZJS
700 if (r < 0) {
701 if (may_warn)
31938a85 702 log_unit_info_errno(UNIT(s)->id, r, "Failed to read PID from file %s: %m", s->pid_file);
5925dd3c 703 return r;
bc41f93e 704 }
406eaf93 705
9f5650ae 706 if (!pid_is_alive(pid)) {
c5419d42 707 if (may_warn)
79008bdd 708 log_unit_info(UNIT(s)->id, "PID "PID_FMT" read from file %s does not exist or is a zombie.", pid, s->pid_file);
e10c9985
YS
709 return -ESRCH;
710 }
711
db01f8b3
MS
712 if (s->main_pid_known) {
713 if (pid == s->main_pid)
714 return 0;
715
79008bdd 716 log_unit_debug(UNIT(s)->id, "Main PID changing: "PID_FMT" -> "PID_FMT, s->main_pid, pid);
8bb2d17d 717
db01f8b3
MS
718 service_unwatch_main_pid(s);
719 s->main_pid_known = false;
3a111838 720 } else
79008bdd 721 log_unit_debug(UNIT(s)->id, "Main PID loaded: "PID_FMT, pid);
db01f8b3 722
117dcc57
ZJS
723 r = service_set_main_pid(s, pid);
724 if (r < 0)
16f6025e
LP
725 return r;
726
117dcc57 727 r = unit_watch_pid(UNIT(s), pid);
bc41f93e 728 if (r < 0) {
5925dd3c 729 /* FIXME: we need to do something here */
79008bdd 730 log_unit_warning(UNIT(s)->id, "Failed to watch PID "PID_FMT" from service %s", pid, UNIT(s)->id);
5925dd3c 731 return r;
bc41f93e 732 }
034c6ed7
LP
733
734 return 0;
735}
736
4fbf50b3
LP
737static int service_search_main_pid(Service *s) {
738 pid_t pid;
739 int r;
740
741 assert(s);
742
3185a36b
LP
743 /* If we know it anyway, don't ever fallback to unreliable
744 * heuristics */
4fbf50b3
LP
745 if (s->main_pid_known)
746 return 0;
747
3185a36b
LP
748 if (!s->guess_main_pid)
749 return 0;
750
4fbf50b3
LP
751 assert(s->main_pid <= 0);
752
4ad49000 753 pid = unit_search_main_pid(UNIT(s));
117dcc57 754 if (pid <= 0)
4fbf50b3
LP
755 return -ENOENT;
756
79008bdd 757 log_unit_debug(UNIT(s)->id, "Main PID guessed: "PID_FMT, pid);
117dcc57
ZJS
758 r = service_set_main_pid(s, pid);
759 if (r < 0)
4fbf50b3
LP
760 return r;
761
117dcc57 762 r = unit_watch_pid(UNIT(s), pid);
8bb2d17d 763 if (r < 0) {
4fbf50b3 764 /* FIXME: we need to do something here */
79008bdd 765 log_unit_warning(UNIT(s)->id, "Failed to watch PID "PID_FMT" from service %s", pid, UNIT(s)->id);
8bb2d17d
LP
766 return r;
767 }
768
769 return 0;
4fbf50b3
LP
770}
771
034c6ed7
LP
772static void service_set_state(Service *s, ServiceState state) {
773 ServiceState old_state;
e056b01d 774 const UnitActiveState *table;
842129f5 775
5cb5a6ff
LP
776 assert(s);
777
e056b01d
LP
778 table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
779
034c6ed7 780 old_state = s->state;
5cb5a6ff 781 s->state = state;
034c6ed7 782
3a111838
MS
783 service_unwatch_pid_file(s);
784
842129f5
LP
785 if (!IN_SET(state,
786 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
787 SERVICE_RELOAD,
bf108e55 788 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
db2cb23b 789 SERVICE_STOP_SIGABRT, SERVICE_STOP_POST,
842129f5
LP
790 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL,
791 SERVICE_AUTO_RESTART))
718db961 792 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
034c6ed7 793
842129f5
LP
794 if (!IN_SET(state,
795 SERVICE_START, SERVICE_START_POST,
796 SERVICE_RUNNING, SERVICE_RELOAD,
bf108e55 797 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
db2cb23b 798 SERVICE_STOP_SIGABRT, SERVICE_STOP_POST,
bf108e55 799 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
5e94833f 800 service_unwatch_main_pid(s);
867b3b7d
LP
801 s->main_command = NULL;
802 }
034c6ed7 803
842129f5
LP
804 if (!IN_SET(state,
805 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
806 SERVICE_RELOAD,
bf108e55 807 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
db2cb23b 808 SERVICE_STOP_SIGABRT, SERVICE_STOP_POST,
842129f5 809 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
5e94833f 810 service_unwatch_control_pid(s);
034c6ed7 811 s->control_command = NULL;
a16e1123 812 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
e537352b 813 }
034c6ed7 814
a911bb9a
LP
815 if (IN_SET(state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART))
816 unit_unwatch_all_pids(UNIT(s));
817
842129f5
LP
818 if (!IN_SET(state,
819 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
820 SERVICE_RUNNING, SERVICE_RELOAD,
821 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST,
db2cb23b 822 SERVICE_STOP_SIGABRT, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL) &&
1124fe6f 823 !(state == SERVICE_DEAD && UNIT(s)->job)) {
4f2d528d 824 service_close_socket_fd(s);
6cf6bbc2
LP
825 service_connection_unref(s);
826 }
4f2d528d 827
7596e9e1 828 if (!IN_SET(state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD))
a6927d7f
MO
829 service_stop_watchdog(s);
830
f6023656
LP
831 /* For the inactive states unit_notify() will trim the cgroup,
832 * but for exit we have to do that ourselves... */
1124fe6f 833 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
b1491eba 834 unit_destroy_cgroup_if_empty(UNIT(s));
f6023656 835
9cd86184
OB
836 /* For remain_after_exit services, let's see if we can "release" the
837 * hold on the console, since unit_notify() only does that in case of
838 * change of state */
f49650ce
LP
839 if (state == SERVICE_EXITED &&
840 s->remain_after_exit &&
9cd86184 841 UNIT(s)->manager->n_on_console > 0) {
f49650ce
LP
842
843 ExecContext *ec;
844
845 ec = unit_get_exec_context(UNIT(s));
9cd86184
OB
846 if (ec && exec_context_may_touch_console(ec)) {
847 Manager *m = UNIT(s)->manager;
848
849 m->n_on_console --;
850 if (m->n_on_console == 0)
851 /* unset no_console_output flag, since the console is free */
852 m->no_console_output = false;
853 }
854 }
855
e537352b 856 if (old_state != state)
79008bdd 857 log_unit_debug(UNIT(s)->id, "%s changed %s -> %s", UNIT(s)->id, service_state_to_string(old_state), service_state_to_string(state));
acbb0225 858
e056b01d 859 unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
f42806df 860 s->reload_result = SERVICE_SUCCESS;
034c6ed7
LP
861}
862
a16e1123
LP
863static int service_coldplug(Unit *u) {
864 Service *s = SERVICE(u);
865 int r;
866
867 assert(s);
868 assert(s->state == SERVICE_DEAD);
869
870 if (s->deserialized_state != s->state) {
871
bf108e55
LP
872 if (IN_SET(s->deserialized_state,
873 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
874 SERVICE_RELOAD,
875 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
db2cb23b 876 SERVICE_STOP_SIGABRT, SERVICE_STOP_POST,
bf108e55 877 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
92c1622e
LP
878
879 usec_t k;
6c12b52e 880
bf108e55 881 k = IN_SET(s->deserialized_state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RELOAD) ? s->timeout_start_usec : s->timeout_stop_usec;
e558336f 882
92c1622e
LP
883 /* For the start/stop timeouts 0 means off */
884 if (k > 0) {
885 r = service_arm_timer(s, k);
36697dc0 886 if (r < 0)
e558336f
LP
887 return r;
888 }
889 }
a16e1123 890
92c1622e
LP
891 if (s->deserialized_state == SERVICE_AUTO_RESTART) {
892
893 /* The restart timeouts 0 means immediately */
894 r = service_arm_timer(s, s->restart_usec);
895 if (r < 0)
896 return r;
897 }
898
9f5650ae 899 if (pid_is_unwaited(s->main_pid) &&
bf108e55
LP
900 ((s->deserialized_state == SERVICE_START && IN_SET(s->type, SERVICE_FORKING, SERVICE_DBUS, SERVICE_ONESHOT, SERVICE_NOTIFY)) ||
901 IN_SET(s->deserialized_state,
902 SERVICE_START, SERVICE_START_POST,
903 SERVICE_RUNNING, SERVICE_RELOAD,
904 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
db2cb23b 905 SERVICE_STOP_SIGABRT, SERVICE_STOP_POST,
bf108e55
LP
906 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL))) {
907 r = unit_watch_pid(UNIT(s), s->main_pid);
908 if (r < 0)
909 return r;
910 }
a16e1123 911
9f5650ae 912 if (pid_is_unwaited(s->control_pid) &&
bf108e55
LP
913 IN_SET(s->deserialized_state,
914 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
915 SERVICE_RELOAD,
916 SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL,
db2cb23b 917 SERVICE_STOP_SIGABRT, SERVICE_STOP_POST,
bf108e55
LP
918 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
919 r = unit_watch_pid(UNIT(s), s->control_pid);
920 if (r < 0)
921 return r;
922 }
a16e1123 923
a911bb9a
LP
924 if (!IN_SET(s->deserialized_state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART))
925 unit_watch_all_pids(UNIT(s));
926
842129f5
LP
927 if (IN_SET(s->deserialized_state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD))
928 service_start_watchdog(s);
bb242b7b 929
a16e1123
LP
930 service_set_state(s, s->deserialized_state);
931 }
92c1622e 932
a16e1123
LP
933 return 0;
934}
935
44d8db9e 936static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
a354329f
LP
937 _cleanup_free_ int *rfds = NULL;
938 unsigned rn_fds = 0;
44d8db9e
LP
939 Iterator i;
940 int r;
57020a3a 941 Unit *u;
44d8db9e
LP
942
943 assert(s);
944 assert(fds);
945 assert(n_fds);
946
6cf6bbc2
LP
947 if (s->socket_fd >= 0)
948 return 0;
949
1124fe6f 950 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
44d8db9e
LP
951 int *cfds;
952 unsigned cn_fds;
57020a3a
LP
953 Socket *sock;
954
ac155bb8 955 if (u->type != UNIT_SOCKET)
57020a3a
LP
956 continue;
957
958 sock = SOCKET(u);
44d8db9e 959
117dcc57
ZJS
960 r = socket_collect_fds(sock, &cfds, &cn_fds);
961 if (r < 0)
a354329f 962 return r;
44d8db9e 963
a354329f
LP
964 if (cn_fds <= 0) {
965 free(cfds);
44d8db9e 966 continue;
a354329f 967 }
44d8db9e
LP
968
969 if (!rfds) {
970 rfds = cfds;
971 rn_fds = cn_fds;
972 } else {
973 int *t;
974
a354329f 975 t = realloc(rfds, (rn_fds + cn_fds) * sizeof(int));
117dcc57 976 if (!t) {
44d8db9e 977 free(cfds);
a354329f 978 return -ENOMEM;
44d8db9e
LP
979 }
980
a354329f
LP
981 memcpy(t + rn_fds, cfds, cn_fds * sizeof(int));
982 rfds = t;
983 rn_fds += cn_fds;
984
44d8db9e
LP
985 free(cfds);
986
44d8db9e
LP
987 }
988 }
989
a354329f
LP
990 if (s->n_fd_store > 0) {
991 ServiceFDStore *fs;
992 int *t;
993
994 t = realloc(rfds, (rn_fds + s->n_fd_store) * sizeof(int));
995 if (!t)
996 return -ENOMEM;
997
998 rfds = t;
999 LIST_FOREACH(fd_store, fs, s->fd_store)
1000 rfds[rn_fds++] = fs->fd;
1001 }
1002
44d8db9e
LP
1003 *fds = rfds;
1004 *n_fds = rn_fds;
3e33402a 1005
a354329f 1006 rfds = NULL;
44d8db9e 1007 return 0;
44d8db9e
LP
1008}
1009
81a2b7ce
LP
1010static int service_spawn(
1011 Service *s,
1012 ExecCommand *c,
21b2ce39 1013 usec_t timeout,
81a2b7ce
LP
1014 bool pass_fds,
1015 bool apply_permissions,
1016 bool apply_chroot,
1e3ad081 1017 bool apply_tty_stdin,
ecedd90f 1018 bool is_control,
81a2b7ce
LP
1019 pid_t *_pid) {
1020
034c6ed7
LP
1021 pid_t pid;
1022 int r;
117dcc57 1023 int *fds = NULL;
7fd1b19b 1024 _cleanup_free_ int *fdsbuf = NULL;
2105e76a 1025 unsigned n_fds = 0, n_env = 0;
e44da745 1026 _cleanup_free_ char *bus_endpoint_path = NULL;
7fd1b19b 1027 _cleanup_strv_free_ char
117dcc57 1028 **argv = NULL, **final_env = NULL, **our_env = NULL;
4ad49000 1029 const char *path;
9fa95f85
DM
1030 ExecParameters exec_params = {
1031 .apply_permissions = apply_permissions,
1032 .apply_chroot = apply_chroot,
1033 .apply_tty_stdin = apply_tty_stdin,
e44da745 1034 .bus_endpoint_fd = -1,
16115b0a 1035 .selinux_context_net = s->socket_fd_selinux_context_net
9fa95f85 1036 };
034c6ed7
LP
1037
1038 assert(s);
1039 assert(c);
1040 assert(_pid);
1041
4ad49000
LP
1042 unit_realize_cgroup(UNIT(s));
1043
613b411c
LP
1044 r = unit_setup_exec_runtime(UNIT(s));
1045 if (r < 0)
1046 goto fail;
1047
6cf6bbc2
LP
1048 if (pass_fds ||
1049 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1050 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1051 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1052
4f2d528d
LP
1053 if (s->socket_fd >= 0) {
1054 fds = &s->socket_fd;
1055 n_fds = 1;
6cf6bbc2 1056 } else {
117dcc57
ZJS
1057 r = service_collect_fds(s, &fdsbuf, &n_fds);
1058 if (r < 0)
6cf6bbc2
LP
1059 goto fail;
1060
1061 fds = fdsbuf;
1062 }
4f2d528d 1063 }
44d8db9e 1064
21b2ce39
LP
1065 if (timeout > 0) {
1066 r = service_arm_timer(s, timeout);
92c1622e
LP
1067 if (r < 0)
1068 goto fail;
1069 } else
1070 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
034c6ed7 1071
19f6d710
LP
1072 r = unit_full_printf_strv(UNIT(s), c->argv, &argv);
1073 if (r < 0)
9e2f7c11 1074 goto fail;
9e2f7c11 1075
09812eb7 1076 our_env = new0(char*, 4);
97ae63e2 1077 if (!our_env) {
2105e76a
LP
1078 r = -ENOMEM;
1079 goto fail;
1080 }
c952c6ec 1081
a158dbf1 1082 if (is_control ? s->notify_access == NOTIFY_ALL : s->notify_access != NOTIFY_NONE)
1124fe6f 1083 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) {
c952c6ec
LP
1084 r = -ENOMEM;
1085 goto fail;
1086 }
1087
2105e76a 1088 if (s->main_pid > 0)
ccd06097 1089 if (asprintf(our_env + n_env++, "MAINPID="PID_FMT, s->main_pid) < 0) {
c952c6ec
LP
1090 r = -ENOMEM;
1091 goto fail;
1092 }
2105e76a 1093
4ad49000 1094 if (UNIT(s)->manager->running_as != SYSTEMD_SYSTEM)
ccd06097 1095 if (asprintf(our_env + n_env++, "MANAGERPID="PID_FMT, getpid()) < 0) {
97ae63e2
LP
1096 r = -ENOMEM;
1097 goto fail;
1098 }
1099
1100 final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL);
1101 if (!final_env) {
2105e76a
LP
1102 r = -ENOMEM;
1103 goto fail;
1104 }
c952c6ec 1105
4ad49000
LP
1106 if (is_control && UNIT(s)->cgroup_path) {
1107 path = strappenda(UNIT(s)->cgroup_path, "/control");
1108 cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
1109 } else
1110 path = UNIT(s)->cgroup_path;
1111
e44da745
DM
1112#ifdef ENABLE_KDBUS
1113 if (s->exec_context.bus_endpoint) {
1114 r = bus_kernel_create_endpoint(UNIT(s)->manager->running_as == SYSTEMD_SYSTEM ? "system" : "user",
1115 UNIT(s)->id, &bus_endpoint_path);
1116 if (r < 0)
1117 goto fail;
1118
1119 /* Pass the fd to the exec_params so that the child process can upload the policy.
1120 * Keep a reference to the fd in the service, so the endpoint is kept alive as long
1121 * as the service is running. */
1122 exec_params.bus_endpoint_fd = s->bus_endpoint_fd = r;
1123 }
1124#endif
1125
9fa95f85
DM
1126 exec_params.argv = argv;
1127 exec_params.fds = fds;
1128 exec_params.n_fds = n_fds;
1129 exec_params.environment = final_env;
1130 exec_params.confirm_spawn = UNIT(s)->manager->confirm_spawn;
1131 exec_params.cgroup_supported = UNIT(s)->manager->cgroup_supported;
1132 exec_params.cgroup_path = path;
a931ad47 1133 exec_params.cgroup_delegate = s->cgroup_context.delegate;
9fa95f85
DM
1134 exec_params.runtime_prefix = manager_get_runtime_prefix(UNIT(s)->manager);
1135 exec_params.unit_id = UNIT(s)->id;
1136 exec_params.watchdog_usec = s->watchdog_usec;
e44da745 1137 exec_params.bus_endpoint_path = bus_endpoint_path;
9fa95f85
DM
1138 if (s->type == SERVICE_IDLE)
1139 exec_params.idle_pipe = UNIT(s)->manager->idle_pipe;
1140
9e2f7c11 1141 r = exec_spawn(c,
9e2f7c11 1142 &s->exec_context,
9fa95f85 1143 &exec_params,
613b411c 1144 s->exec_runtime,
9e2f7c11 1145 &pid);
9e2f7c11 1146 if (r < 0)
034c6ed7
LP
1147 goto fail;
1148
117dcc57
ZJS
1149 r = unit_watch_pid(UNIT(s), pid);
1150 if (r < 0)
034c6ed7
LP
1151 /* FIXME: we need to do something here */
1152 goto fail;
1153
1154 *_pid = pid;
1155
5cb5a6ff 1156 return 0;
034c6ed7
LP
1157
1158fail:
1159 if (timeout)
718db961 1160 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
034c6ed7
LP
1161
1162 return r;
1163}
1164
80876c20
LP
1165static int main_pid_good(Service *s) {
1166 assert(s);
1167
1168 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1169 * don't know */
1170
1171 /* If we know the pid file, then lets just check if it is
1172 * still valid */
6dfa5494
LP
1173 if (s->main_pid_known) {
1174
1175 /* If it's an alien child let's check if it is still
1176 * alive ... */
62220cf7 1177 if (s->main_pid_alien && s->main_pid > 0)
9f5650ae 1178 return pid_is_alive(s->main_pid);
6dfa5494
LP
1179
1180 /* .. otherwise assume we'll get a SIGCHLD for it,
1181 * which we really should wait for to collect exit
1182 * status and code */
80876c20 1183 return s->main_pid > 0;
6dfa5494 1184 }
80876c20
LP
1185
1186 /* We don't know the pid */
1187 return -EAGAIN;
1188}
1189
44a6b1b6 1190_pure_ static int control_pid_good(Service *s) {
80876c20
LP
1191 assert(s);
1192
1193 return s->control_pid > 0;
1194}
1195
1196static int cgroup_good(Service *s) {
1197 int r;
1198
1199 assert(s);
1200
4ad49000
LP
1201 if (!UNIT(s)->cgroup_path)
1202 return 0;
1203
1204 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, UNIT(s)->cgroup_path, true);
117dcc57 1205 if (r < 0)
80876c20
LP
1206 return r;
1207
1208 return !r;
1209}
1210
f42806df 1211static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
034c6ed7
LP
1212 int r;
1213 assert(s);
1214
f42806df
LP
1215 if (f != SERVICE_SUCCESS)
1216 s->result = f;
034c6ed7 1217
0c7f15b3
MS
1218 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1219
2928b0a8 1220 if (s->result != SERVICE_SUCCESS) {
79008bdd 1221 log_unit_warning(UNIT(s)->id, "%s failed.", UNIT(s)->id);
2928b0a8
LP
1222 failure_action(UNIT(s)->manager, s->failure_action, s->reboot_arg);
1223 }
93ae25e6 1224
034c6ed7 1225 if (allow_restart &&
47342320 1226 !s->forbid_restart &&
034c6ed7 1227 (s->restart == SERVICE_RESTART_ALWAYS ||
f42806df
LP
1228 (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) ||
1229 (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) ||
6cfe2fde 1230 (s->restart == SERVICE_RESTART_ON_ABNORMAL && !IN_SET(s->result, SERVICE_SUCCESS, SERVICE_FAILURE_EXIT_CODE)) ||
dc99a976 1231 (s->restart == SERVICE_RESTART_ON_WATCHDOG && s->result == SERVICE_FAILURE_WATCHDOG) ||
37520c1b 1232 (s->restart == SERVICE_RESTART_ON_ABORT && IN_SET(s->result, SERVICE_FAILURE_SIGNAL, SERVICE_FAILURE_CORE_DUMP)) ||
3e2d435b 1233 (s->main_exec_status.code == CLD_EXITED && set_contains(s->restart_force_status.status, INT_TO_PTR(s->main_exec_status.status))) ||
37520c1b 1234 (IN_SET(s->main_exec_status.code, CLD_KILLED, CLD_DUMPED) && set_contains(s->restart_force_status.signal, INT_TO_PTR(s->main_exec_status.status)))) &&
3e2d435b 1235 (s->main_exec_status.code != CLD_EXITED || !set_contains(s->restart_prevent_status.status, INT_TO_PTR(s->main_exec_status.status))) &&
37520c1b 1236 (!IN_SET(s->main_exec_status.code, CLD_KILLED, CLD_DUMPED) || !set_contains(s->restart_prevent_status.signal, INT_TO_PTR(s->main_exec_status.status)))) {
034c6ed7 1237
718db961 1238 r = service_arm_timer(s, s->restart_usec);
f42806df 1239 if (r < 0)
034c6ed7
LP
1240 goto fail;
1241
1242 service_set_state(s, SERVICE_AUTO_RESTART);
0c7f15b3 1243 }
034c6ed7 1244
47342320
LP
1245 s->forbid_restart = false;
1246
e66cf1a3 1247 /* We want fresh tmpdirs in case service is started again immediately */
613b411c
LP
1248 exec_runtime_destroy(s->exec_runtime);
1249 s->exec_runtime = exec_runtime_unref(s->exec_runtime);
c17ec25e 1250
e66cf1a3
LP
1251 /* Also, remove the runtime directory in */
1252 exec_context_destroy_runtime_directory(&s->exec_context, manager_get_runtime_prefix(UNIT(s)->manager));
1253
9285c9ff
LN
1254 /* Try to delete the pid file. At this point it will be
1255 * out-of-date, and some software might be confused by it, so
1256 * let's remove it. */
1257 if (s->pid_file)
1258 unlink_noerrno(s->pid_file);
1259
034c6ed7
LP
1260 return;
1261
1262fail:
31938a85 1263 log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run install restart timer: %m", UNIT(s)->id);
f42806df 1264 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
034c6ed7
LP
1265}
1266
f42806df 1267static void service_enter_stop_post(Service *s, ServiceResult f) {
034c6ed7
LP
1268 int r;
1269 assert(s);
1270
f42806df
LP
1271 if (f != SERVICE_SUCCESS)
1272 s->result = f;
034c6ed7 1273
5e94833f 1274 service_unwatch_control_pid(s);
a911bb9a 1275 unit_watch_all_pids(UNIT(s));
5e94833f 1276
117dcc57
ZJS
1277 s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST];
1278 if (s->control_command) {
867b3b7d
LP
1279 s->control_command_id = SERVICE_EXEC_STOP_POST;
1280
ecedd90f
LP
1281 r = service_spawn(s,
1282 s->control_command,
21b2ce39 1283 s->timeout_stop_usec,
ecedd90f
LP
1284 false,
1285 !s->permissions_start_only,
1286 !s->root_directory_start_only,
1287 true,
ecedd90f
LP
1288 true,
1289 &s->control_pid);
1290 if (r < 0)
034c6ed7
LP
1291 goto fail;
1292
80876c20
LP
1293 service_set_state(s, SERVICE_STOP_POST);
1294 } else
ac84d1fb 1295 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS);
034c6ed7
LP
1296
1297 return;
1298
1299fail:
31938a85 1300 log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run 'stop-post' task: %m", UNIT(s)->id);
f42806df 1301 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
034c6ed7
LP
1302}
1303
f42806df 1304static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
034c6ed7 1305 int r;
034c6ed7
LP
1306
1307 assert(s);
1308
f42806df
LP
1309 if (f != SERVICE_SUCCESS)
1310 s->result = f;
034c6ed7 1311
a911bb9a
LP
1312 unit_watch_all_pids(UNIT(s));
1313
cd2086fe
LP
1314 r = unit_kill_context(
1315 UNIT(s),
1316 &s->kill_context,
db2cb23b
UTL
1317 (state != SERVICE_STOP_SIGTERM && state != SERVICE_FINAL_SIGTERM && state != SERVICE_STOP_SIGABRT) ?
1318 KILL_KILL : (state == SERVICE_STOP_SIGABRT ? KILL_ABORT : KILL_TERMINATE),
cd2086fe
LP
1319 s->main_pid,
1320 s->control_pid,
1321 s->main_pid_alien);
ac84d1fb 1322
cd2086fe
LP
1323 if (r < 0)
1324 goto fail;
034c6ed7 1325
cd2086fe 1326 if (r > 0) {
d568a335 1327 if (s->timeout_stop_usec > 0) {
718db961 1328 r = service_arm_timer(s, s->timeout_stop_usec);
d568a335 1329 if (r < 0)
e558336f 1330 goto fail;
d568a335 1331 }
d6ea93e3 1332
80876c20 1333 service_set_state(s, state);
db2cb23b 1334 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGABRT)
ac84d1fb
LP
1335 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_SUCCESS);
1336 else if (state == SERVICE_STOP_SIGKILL)
f42806df 1337 service_enter_stop_post(s, SERVICE_SUCCESS);
ac84d1fb
LP
1338 else if (state == SERVICE_FINAL_SIGTERM)
1339 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_SUCCESS);
80876c20 1340 else
f42806df 1341 service_enter_dead(s, SERVICE_SUCCESS, true);
034c6ed7
LP
1342
1343 return;
1344
1345fail:
31938a85 1346 log_unit_warning_errno(UNIT(s)->id, r, "%s failed to kill processes: %m", UNIT(s)->id);
034c6ed7 1347
db2cb23b
UTL
1348 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL ||
1349 state == SERVICE_STOP_SIGABRT)
f42806df 1350 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
034c6ed7 1351 else
f42806df 1352 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
034c6ed7
LP
1353}
1354
308d72dc
LP
1355static void service_enter_stop_by_notify(Service *s) {
1356 assert(s);
1357
1358 unit_watch_all_pids(UNIT(s));
1359
1360 if (s->timeout_stop_usec > 0)
1361 service_arm_timer(s, s->timeout_stop_usec);
1362
6041a7ee
MS
1363 /* The service told us it's stopping, so it's as if we SIGTERM'd it. */
1364 service_set_state(s, SERVICE_STOP_SIGTERM);
308d72dc
LP
1365}
1366
f42806df 1367static void service_enter_stop(Service *s, ServiceResult f) {
034c6ed7 1368 int r;
5925dd3c 1369
034c6ed7
LP
1370 assert(s);
1371
f42806df
LP
1372 if (f != SERVICE_SUCCESS)
1373 s->result = f;
034c6ed7 1374
5e94833f 1375 service_unwatch_control_pid(s);
a911bb9a 1376 unit_watch_all_pids(UNIT(s));
5e94833f 1377
117dcc57
ZJS
1378 s->control_command = s->exec_command[SERVICE_EXEC_STOP];
1379 if (s->control_command) {
867b3b7d
LP
1380 s->control_command_id = SERVICE_EXEC_STOP;
1381
ecedd90f
LP
1382 r = service_spawn(s,
1383 s->control_command,
21b2ce39 1384 s->timeout_stop_usec,
ecedd90f
LP
1385 false,
1386 !s->permissions_start_only,
1387 !s->root_directory_start_only,
1388 false,
ecedd90f
LP
1389 true,
1390 &s->control_pid);
1391 if (r < 0)
034c6ed7
LP
1392 goto fail;
1393
80876c20
LP
1394 service_set_state(s, SERVICE_STOP);
1395 } else
f42806df 1396 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
034c6ed7
LP
1397
1398 return;
1399
1400fail:
31938a85 1401 log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run 'stop' task: %m", UNIT(s)->id);
f42806df 1402 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
034c6ed7
LP
1403}
1404
f42806df 1405static void service_enter_running(Service *s, ServiceResult f) {
4eab639f 1406 int main_pid_ok, cgroup_ok;
80876c20
LP
1407 assert(s);
1408
f42806df
LP
1409 if (f != SERVICE_SUCCESS)
1410 s->result = f;
80876c20 1411
4eab639f
LP
1412 main_pid_ok = main_pid_good(s);
1413 cgroup_ok = cgroup_good(s);
1414
1415 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
308d72dc
LP
1416 (s->bus_name_good || s->type != SERVICE_DBUS)) {
1417
1418 /* If there are any queued up sd_notify()
1419 * notifications, process them now */
1420 if (s->notify_state == NOTIFY_RELOADING)
1421 service_enter_reload_by_notify(s);
1422 else if (s->notify_state == NOTIFY_STOPPING)
1423 service_enter_stop_by_notify(s);
1424 else
1425 service_set_state(s, SERVICE_RUNNING);
1426
1427 } else if (s->remain_after_exit)
80876c20
LP
1428 service_set_state(s, SERVICE_EXITED);
1429 else
f42806df 1430 service_enter_stop(s, SERVICE_SUCCESS);
80876c20
LP
1431}
1432
034c6ed7
LP
1433static void service_enter_start_post(Service *s) {
1434 int r;
1435 assert(s);
1436
5e94833f 1437 service_unwatch_control_pid(s);
842129f5 1438 service_reset_watchdog(s);
bb242b7b 1439
117dcc57
ZJS
1440 s->control_command = s->exec_command[SERVICE_EXEC_START_POST];
1441 if (s->control_command) {
867b3b7d
LP
1442 s->control_command_id = SERVICE_EXEC_START_POST;
1443
ecedd90f
LP
1444 r = service_spawn(s,
1445 s->control_command,
21b2ce39 1446 s->timeout_start_usec,
ecedd90f
LP
1447 false,
1448 !s->permissions_start_only,
1449 !s->root_directory_start_only,
1450 false,
ecedd90f
LP
1451 true,
1452 &s->control_pid);
1453 if (r < 0)
034c6ed7
LP
1454 goto fail;
1455
80876c20
LP
1456 service_set_state(s, SERVICE_START_POST);
1457 } else
f42806df 1458 service_enter_running(s, SERVICE_SUCCESS);
034c6ed7
LP
1459
1460 return;
1461
1462fail:
31938a85 1463 log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run 'start-post' task: %m", UNIT(s)->id);
f42806df 1464 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
034c6ed7
LP
1465}
1466
4ad49000
LP
1467static void service_kill_control_processes(Service *s) {
1468 char *p;
1469
1470 if (!UNIT(s)->cgroup_path)
1471 return;
1472
1473 p = strappenda(UNIT(s)->cgroup_path, "/control");
4ad49000
LP
1474 cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, p, SIGKILL, true, true, true, NULL);
1475}
1476
034c6ed7 1477static void service_enter_start(Service *s) {
4ad49000 1478 ExecCommand *c;
034c6ed7
LP
1479 pid_t pid;
1480 int r;
1481
1482 assert(s);
1483
41efeaec
LP
1484 service_unwatch_control_pid(s);
1485 service_unwatch_main_pid(s);
80876c20 1486
8f53a7b8
LP
1487 /* We want to ensure that nobody leaks processes from
1488 * START_PRE here, so let's go on a killing spree, People
1489 * should not spawn long running processes from START_PRE. */
4ad49000 1490 service_kill_control_processes(s);
8f53a7b8 1491
867b3b7d
LP
1492 if (s->type == SERVICE_FORKING) {
1493 s->control_command_id = SERVICE_EXEC_START;
1494 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
1495
1496 s->main_command = NULL;
1497 } else {
1498 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1499 s->control_command = NULL;
1500
1501 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
1502 }
34e9ba66 1503
96fb8242
LP
1504 if (!c) {
1505 assert(s->type == SERVICE_ONESHOT);
1506 service_enter_start_post(s);
1507 return;
1508 }
1509
ecedd90f
LP
1510 r = service_spawn(s,
1511 c,
21b2ce39 1512 IN_SET(s->type, SERVICE_FORKING, SERVICE_DBUS, SERVICE_NOTIFY, SERVICE_ONESHOT) ? s->timeout_start_usec : 0,
ecedd90f
LP
1513 true,
1514 true,
1515 true,
1516 true,
ecedd90f
LP
1517 false,
1518 &pid);
1519 if (r < 0)
034c6ed7
LP
1520 goto fail;
1521
f2b68789 1522 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) {
034c6ed7
LP
1523 /* For simple services we immediately start
1524 * the START_POST binaries. */
1525
5925dd3c 1526 service_set_main_pid(s, pid);
034c6ed7
LP
1527 service_enter_start_post(s);
1528
1529 } else if (s->type == SERVICE_FORKING) {
1530
1531 /* For forking services we wait until the start
1532 * process exited. */
1533
e55224ca 1534 s->control_pid = pid;
80876c20
LP
1535 service_set_state(s, SERVICE_START);
1536
34e9ba66 1537 } else if (s->type == SERVICE_ONESHOT ||
8c47c732
LP
1538 s->type == SERVICE_DBUS ||
1539 s->type == SERVICE_NOTIFY) {
7d55e835 1540
34e9ba66 1541 /* For oneshot services we wait until the start
7d55e835
LP
1542 * process exited, too, but it is our main process. */
1543
05e343b7 1544 /* For D-Bus services we know the main pid right away,
8c47c732
LP
1545 * but wait for the bus name to appear on the
1546 * bus. Notify services are similar. */
05e343b7 1547
5925dd3c 1548 service_set_main_pid(s, pid);
80876c20 1549 service_set_state(s, SERVICE_START);
034c6ed7
LP
1550 } else
1551 assert_not_reached("Unknown service type");
1552
1553 return;
1554
1555fail:
31938a85 1556 log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run 'start' task: %m", UNIT(s)->id);
f42806df 1557 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
034c6ed7
LP
1558}
1559
1560static void service_enter_start_pre(Service *s) {
1561 int r;
1562
1563 assert(s);
1564
5e94833f
LP
1565 service_unwatch_control_pid(s);
1566
117dcc57
ZJS
1567 s->control_command = s->exec_command[SERVICE_EXEC_START_PRE];
1568 if (s->control_command) {
8f53a7b8
LP
1569 /* Before we start anything, let's clear up what might
1570 * be left from previous runs. */
4ad49000 1571 service_kill_control_processes(s);
8f53a7b8 1572
867b3b7d
LP
1573 s->control_command_id = SERVICE_EXEC_START_PRE;
1574
ecedd90f
LP
1575 r = service_spawn(s,
1576 s->control_command,
21b2ce39 1577 s->timeout_start_usec,
ecedd90f
LP
1578 false,
1579 !s->permissions_start_only,
1580 !s->root_directory_start_only,
1581 true,
ecedd90f
LP
1582 true,
1583 &s->control_pid);
1584 if (r < 0)
034c6ed7
LP
1585 goto fail;
1586
80876c20
LP
1587 service_set_state(s, SERVICE_START_PRE);
1588 } else
034c6ed7
LP
1589 service_enter_start(s);
1590
1591 return;
1592
1593fail:
31938a85 1594 log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run 'start-pre' task: %m", UNIT(s)->id);
f42806df 1595 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
034c6ed7
LP
1596}
1597
1598static void service_enter_restart(Service *s) {
718db961 1599 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
034c6ed7 1600 int r;
398ef8ba 1601
034c6ed7
LP
1602 assert(s);
1603
a8bb2e65
LP
1604 if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) {
1605 /* Don't restart things if we are going down anyway */
79008bdd 1606 log_unit_info(UNIT(s)->id, "Stop job pending for unit, delaying automatic restart.");
2edfa366 1607
718db961 1608 r = service_arm_timer(s, s->restart_usec);
a8bb2e65 1609 if (r < 0)
2edfa366 1610 goto fail;
feae8adb
DW
1611
1612 return;
2edfa366
LP
1613 }
1614
48bb5876
DW
1615 /* Any units that are bound to this service must also be
1616 * restarted. We use JOB_RESTART (instead of the more obvious
1617 * JOB_START) here so that those dependency jobs will be added
1618 * as well. */
1619 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL);
1620 if (r < 0)
034c6ed7
LP
1621 goto fail;
1622
a8bb2e65
LP
1623 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
1624 * it will be canceled as part of the service_stop() call that
1625 * is executed as part of JOB_RESTART. */
1626
79008bdd 1627 log_unit_debug(UNIT(s)->id, "%s scheduled restart job.", UNIT(s)->id);
034c6ed7
LP
1628 return;
1629
1630fail:
79008bdd 1631 log_unit_warning(UNIT(s)->id, "%s failed to schedule restart job: %s", UNIT(s)->id, bus_error_message(&error, -r));
f42806df 1632 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
034c6ed7
LP
1633}
1634
308d72dc
LP
1635static void service_enter_reload_by_notify(Service *s) {
1636 assert(s);
1637
1638 if (s->timeout_start_usec > 0)
1639 service_arm_timer(s, s->timeout_start_usec);
1640
1641 service_set_state(s, SERVICE_RELOAD);
1642}
1643
034c6ed7
LP
1644static void service_enter_reload(Service *s) {
1645 int r;
1646
1647 assert(s);
1648
5e94833f
LP
1649 service_unwatch_control_pid(s);
1650
117dcc57
ZJS
1651 s->control_command = s->exec_command[SERVICE_EXEC_RELOAD];
1652 if (s->control_command) {
867b3b7d
LP
1653 s->control_command_id = SERVICE_EXEC_RELOAD;
1654
ecedd90f
LP
1655 r = service_spawn(s,
1656 s->control_command,
21b2ce39 1657 s->timeout_start_usec,
ecedd90f
LP
1658 false,
1659 !s->permissions_start_only,
1660 !s->root_directory_start_only,
1661 false,
ecedd90f
LP
1662 true,
1663 &s->control_pid);
1664 if (r < 0)
034c6ed7
LP
1665 goto fail;
1666
80876c20
LP
1667 service_set_state(s, SERVICE_RELOAD);
1668 } else
f42806df 1669 service_enter_running(s, SERVICE_SUCCESS);
034c6ed7
LP
1670
1671 return;
1672
1673fail:
31938a85 1674 log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run 'reload' task: %m", UNIT(s)->id);
f42806df
LP
1675 s->reload_result = SERVICE_FAILURE_RESOURCES;
1676 service_enter_running(s, SERVICE_SUCCESS);
034c6ed7
LP
1677}
1678
f42806df 1679static void service_run_next_control(Service *s) {
034c6ed7
LP
1680 int r;
1681
1682 assert(s);
1683 assert(s->control_command);
1684 assert(s->control_command->command_next);
1685
34e9ba66 1686 assert(s->control_command_id != SERVICE_EXEC_START);
034c6ed7 1687
34e9ba66 1688 s->control_command = s->control_command->command_next;
5e94833f
LP
1689 service_unwatch_control_pid(s);
1690
ecedd90f
LP
1691 r = service_spawn(s,
1692 s->control_command,
21b2ce39 1693 IN_SET(s->state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD) ? s->timeout_start_usec : s->timeout_stop_usec,
ecedd90f
LP
1694 false,
1695 !s->permissions_start_only,
1696 !s->root_directory_start_only,
1697 s->control_command_id == SERVICE_EXEC_START_PRE ||
1698 s->control_command_id == SERVICE_EXEC_STOP_POST,
ecedd90f
LP
1699 true,
1700 &s->control_pid);
1701 if (r < 0)
034c6ed7
LP
1702 goto fail;
1703
1704 return;
1705
1706fail:
31938a85 1707 log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run next control task: %m", UNIT(s)->id);
034c6ed7 1708
80876c20 1709 if (s->state == SERVICE_START_PRE)
f42806df 1710 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
80876c20 1711 else if (s->state == SERVICE_STOP)
f42806df 1712 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
034c6ed7 1713 else if (s->state == SERVICE_STOP_POST)
f42806df 1714 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
e2f3b44c 1715 else if (s->state == SERVICE_RELOAD) {
f42806df
LP
1716 s->reload_result = SERVICE_FAILURE_RESOURCES;
1717 service_enter_running(s, SERVICE_SUCCESS);
e2f3b44c 1718 } else
f42806df 1719 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
5cb5a6ff
LP
1720}
1721
f42806df 1722static void service_run_next_main(Service *s) {
34e9ba66
LP
1723 pid_t pid;
1724 int r;
1725
1726 assert(s);
867b3b7d
LP
1727 assert(s->main_command);
1728 assert(s->main_command->command_next);
1729 assert(s->type == SERVICE_ONESHOT);
34e9ba66 1730
867b3b7d 1731 s->main_command = s->main_command->command_next;
34e9ba66
LP
1732 service_unwatch_main_pid(s);
1733
ecedd90f
LP
1734 r = service_spawn(s,
1735 s->main_command,
21b2ce39 1736 s->timeout_start_usec,
ecedd90f
LP
1737 true,
1738 true,
1739 true,
1740 true,
ecedd90f
LP
1741 false,
1742 &pid);
1743 if (r < 0)
34e9ba66
LP
1744 goto fail;
1745
1746 service_set_main_pid(s, pid);
1747
1748 return;
1749
1750fail:
31938a85 1751 log_unit_warning_errno(UNIT(s)->id, r, "%s failed to run next main task: %m", UNIT(s)->id);
f42806df 1752 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
34e9ba66
LP
1753}
1754
93ae25e6
MO
1755static int service_start_limit_test(Service *s) {
1756 assert(s);
1757
1758 if (ratelimit_test(&s->start_limit))
1759 return 0;
1760
79008bdd 1761 log_unit_warning(UNIT(s)->id, "start request repeated too quickly for %s", UNIT(s)->id);
2928b0a8
LP
1762
1763 return failure_action(UNIT(s)->manager, s->start_limit_action, s->reboot_arg);
93ae25e6
MO
1764}
1765
87f0e418
LP
1766static int service_start(Unit *u) {
1767 Service *s = SERVICE(u);
4b939747 1768 int r;
5cb5a6ff
LP
1769
1770 assert(s);
1771
034c6ed7
LP
1772 /* We cannot fulfill this request right now, try again later
1773 * please! */
1774 if (s->state == SERVICE_STOP ||
db2cb23b 1775 s->state == SERVICE_STOP_SIGABRT ||
034c6ed7
LP
1776 s->state == SERVICE_STOP_SIGTERM ||
1777 s->state == SERVICE_STOP_SIGKILL ||
1778 s->state == SERVICE_STOP_POST ||
1779 s->state == SERVICE_FINAL_SIGTERM ||
1780 s->state == SERVICE_FINAL_SIGKILL)
5cb5a6ff
LP
1781 return -EAGAIN;
1782
034c6ed7
LP
1783 /* Already on it! */
1784 if (s->state == SERVICE_START_PRE ||
1785 s->state == SERVICE_START ||
1786 s->state == SERVICE_START_POST)
1787 return 0;
1788
2e9d6c12 1789 /* A service that will be restarted must be stopped first to
7f2cddae 1790 * trigger BindsTo and/or OnFailure dependencies. If a user
2e9d6c12 1791 * does not want to wait for the holdoff time to elapse, the
d4943dc7
LP
1792 * service should be manually restarted, not started. We
1793 * simply return EAGAIN here, so that any start jobs stay
1794 * queued, and assume that the auto restart timer will
1795 * eventually trigger the restart. */
1796 if (s->state == SERVICE_AUTO_RESTART)
a8bb2e65 1797 return -EAGAIN;
2e9d6c12
DW
1798
1799 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED);
5cb5a6ff 1800
1e2e8133 1801 /* Make sure we don't enter a busy loop of some kind. */
4b939747 1802 r = service_start_limit_test(s);
c2f34808 1803 if (r < 0) {
8d1b002a 1804 service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false);
4b939747 1805 return r;
c2f34808 1806 }
1e2e8133 1807
f42806df
LP
1808 s->result = SERVICE_SUCCESS;
1809 s->reload_result = SERVICE_SUCCESS;
034c6ed7 1810 s->main_pid_known = false;
6dfa5494 1811 s->main_pid_alien = false;
47342320 1812 s->forbid_restart = false;
034c6ed7 1813
8cfdb077
LP
1814 free(s->status_text);
1815 s->status_text = NULL;
1816 s->status_errno = 0;
1817
308d72dc
LP
1818 s->notify_state = NOTIFY_UNKNOWN;
1819
034c6ed7
LP
1820 service_enter_start_pre(s);
1821 return 0;
5cb5a6ff
LP
1822}
1823
87f0e418
LP
1824static int service_stop(Unit *u) {
1825 Service *s = SERVICE(u);
5cb5a6ff
LP
1826
1827 assert(s);
1828
f0c7b229 1829 /* Don't create restart jobs from here. */
47342320 1830 s->forbid_restart = true;
034c6ed7 1831
e537352b
LP
1832 /* Already on it */
1833 if (s->state == SERVICE_STOP ||
db2cb23b 1834 s->state == SERVICE_STOP_SIGABRT ||
e537352b
LP
1835 s->state == SERVICE_STOP_SIGTERM ||
1836 s->state == SERVICE_STOP_SIGKILL ||
1837 s->state == SERVICE_STOP_POST ||
1838 s->state == SERVICE_FINAL_SIGTERM ||
1839 s->state == SERVICE_FINAL_SIGKILL)
1840 return 0;
1841
f0c7b229 1842 /* A restart will be scheduled or is in progress. */
034c6ed7 1843 if (s->state == SERVICE_AUTO_RESTART) {
0c7f15b3 1844 service_set_state(s, SERVICE_DEAD);
034c6ed7
LP
1845 return 0;
1846 }
1847
3f6c78dc
LP
1848 /* If there's already something running we go directly into
1849 * kill mode. */
1850 if (s->state == SERVICE_START_PRE ||
1851 s->state == SERVICE_START ||
1852 s->state == SERVICE_START_POST ||
1853 s->state == SERVICE_RELOAD) {
f42806df 1854 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
3f6c78dc
LP
1855 return 0;
1856 }
5cb5a6ff 1857
3f6c78dc
LP
1858 assert(s->state == SERVICE_RUNNING ||
1859 s->state == SERVICE_EXITED);
3a762661 1860
f42806df 1861 service_enter_stop(s, SERVICE_SUCCESS);
5cb5a6ff
LP
1862 return 0;
1863}
1864
87f0e418
LP
1865static int service_reload(Unit *u) {
1866 Service *s = SERVICE(u);
034c6ed7
LP
1867
1868 assert(s);
1869
80876c20 1870 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
034c6ed7
LP
1871
1872 service_enter_reload(s);
5cb5a6ff
LP
1873 return 0;
1874}
1875
44a6b1b6 1876_pure_ static bool service_can_reload(Unit *u) {
87f0e418 1877 Service *s = SERVICE(u);
034c6ed7
LP
1878
1879 assert(s);
1880
1881 return !!s->exec_command[SERVICE_EXEC_RELOAD];
1882}
1883
a16e1123
LP
1884static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
1885 Service *s = SERVICE(u);
2339fc93 1886 ServiceFDStore *fs;
a16e1123
LP
1887
1888 assert(u);
1889 assert(f);
1890 assert(fds);
1891
1892 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
f42806df
LP
1893 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
1894 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
a16e1123
LP
1895
1896 if (s->control_pid > 0)
ccd06097
ZJS
1897 unit_serialize_item_format(u, f, "control-pid", PID_FMT,
1898 s->control_pid);
a16e1123 1899
5925dd3c 1900 if (s->main_pid_known && s->main_pid > 0)
ccd06097 1901 unit_serialize_item_format(u, f, "main-pid", PID_FMT, s->main_pid);
a16e1123
LP
1902
1903 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
1904
3a2776bc
LP
1905 if (s->status_text)
1906 unit_serialize_item(u, f, "status-text", s->status_text);
1907
cfc4eb4c
LP
1908 /* FIXME: There's a minor uncleanliness here: if there are
1909 * multiple commands attached here, we will start from the
1910 * first one again */
a16e1123 1911 if (s->control_command_id >= 0)
117dcc57
ZJS
1912 unit_serialize_item(u, f, "control-command",
1913 service_exec_command_to_string(s->control_command_id));
a16e1123
LP
1914
1915 if (s->socket_fd >= 0) {
1916 int copy;
1917
2339fc93
LP
1918 copy = fdset_put_dup(fds, s->socket_fd);
1919 if (copy < 0)
a16e1123
LP
1920 return copy;
1921
1922 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
1923 }
1924
e44da745
DM
1925 if (s->bus_endpoint_fd >= 0) {
1926 int copy;
1927
2339fc93
LP
1928 copy = fdset_put_dup(fds, s->bus_endpoint_fd);
1929 if (copy < 0)
e44da745
DM
1930 return copy;
1931
1932 unit_serialize_item_format(u, f, "endpoint-fd", "%i", copy);
1933 }
1934
2339fc93
LP
1935 LIST_FOREACH(fd_store, fs, s->fd_store) {
1936 int copy;
1937
1938 copy = fdset_put_dup(fds, fs->fd);
1939 if (copy < 0)
1940 return copy;
1941
1942 unit_serialize_item_format(u, f, "fd-store-fd", "%i", copy);
1943 }
1944
ecdbca40 1945 if (s->main_exec_status.pid > 0) {
ccd06097
ZJS
1946 unit_serialize_item_format(u, f, "main-exec-status-pid", PID_FMT,
1947 s->main_exec_status.pid);
117dcc57
ZJS
1948 dual_timestamp_serialize(f, "main-exec-status-start",
1949 &s->main_exec_status.start_timestamp);
1950 dual_timestamp_serialize(f, "main-exec-status-exit",
1951 &s->main_exec_status.exit_timestamp);
ecdbca40 1952
799fd0fd 1953 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
117dcc57
ZJS
1954 unit_serialize_item_format(u, f, "main-exec-status-code", "%i",
1955 s->main_exec_status.code);
1956 unit_serialize_item_format(u, f, "main-exec-status-status", "%i",
1957 s->main_exec_status.status);
ecdbca40
LP
1958 }
1959 }
a6927d7f 1960 if (dual_timestamp_is_set(&s->watchdog_timestamp))
842129f5 1961 dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp);
ecdbca40 1962
6aca9a58 1963 if (s->forbid_restart)
8d1a2802 1964 unit_serialize_item(u, f, "forbid-restart", yes_no(s->forbid_restart));
6aca9a58 1965
a16e1123
LP
1966 return 0;
1967}
1968
1969static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1970 Service *s = SERVICE(u);
2339fc93 1971 int r;
a16e1123
LP
1972
1973 assert(u);
1974 assert(key);
1975 assert(value);
1976 assert(fds);
1977
1978 if (streq(key, "state")) {
1979 ServiceState state;
1980
117dcc57
ZJS
1981 state = service_state_from_string(value);
1982 if (state < 0)
79008bdd 1983 log_unit_debug(u->id, "Failed to parse state value %s", value);
a16e1123
LP
1984 else
1985 s->deserialized_state = state;
f42806df
LP
1986 } else if (streq(key, "result")) {
1987 ServiceResult f;
1988
1989 f = service_result_from_string(value);
1990 if (f < 0)
79008bdd 1991 log_unit_debug(u->id, "Failed to parse result value %s", value);
f42806df
LP
1992 else if (f != SERVICE_SUCCESS)
1993 s->result = f;
1994
1995 } else if (streq(key, "reload-result")) {
1996 ServiceResult f;
1997
1998 f = service_result_from_string(value);
1999 if (f < 0)
79008bdd 2000 log_unit_debug(u->id, "Failed to parse reload result value %s", value);
f42806df
LP
2001 else if (f != SERVICE_SUCCESS)
2002 s->reload_result = f;
a16e1123 2003
a16e1123 2004 } else if (streq(key, "control-pid")) {
5925dd3c 2005 pid_t pid;
a16e1123 2006
e364ad06 2007 if (parse_pid(value, &pid) < 0)
79008bdd 2008 log_unit_debug(u->id, "Failed to parse control-pid value %s", value);
a16e1123 2009 else
e55224ca 2010 s->control_pid = pid;
a16e1123 2011 } else if (streq(key, "main-pid")) {
5925dd3c 2012 pid_t pid;
a16e1123 2013
e364ad06 2014 if (parse_pid(value, &pid) < 0)
79008bdd 2015 log_unit_debug(u->id, "Failed to parse main-pid value %s", value);
7400b9d2
LP
2016 else {
2017 service_set_main_pid(s, pid);
2018 unit_watch_pid(UNIT(s), pid);
2019 }
a16e1123
LP
2020 } else if (streq(key, "main-pid-known")) {
2021 int b;
2022
117dcc57
ZJS
2023 b = parse_boolean(value);
2024 if (b < 0)
79008bdd 2025 log_unit_debug(u->id, "Failed to parse main-pid-known value %s", value);
a16e1123
LP
2026 else
2027 s->main_pid_known = b;
3a2776bc
LP
2028 } else if (streq(key, "status-text")) {
2029 char *t;
2030
117dcc57
ZJS
2031 t = strdup(value);
2032 if (!t)
2033 log_oom();
2034 else {
3a2776bc
LP
2035 free(s->status_text);
2036 s->status_text = t;
2037 }
2038
a16e1123
LP
2039 } else if (streq(key, "control-command")) {
2040 ServiceExecCommand id;
2041
117dcc57
ZJS
2042 id = service_exec_command_from_string(value);
2043 if (id < 0)
79008bdd 2044 log_unit_debug(u->id, "Failed to parse exec-command value %s", value);
a16e1123
LP
2045 else {
2046 s->control_command_id = id;
2047 s->control_command = s->exec_command[id];
2048 }
2049 } else if (streq(key, "socket-fd")) {
2050 int fd;
2051
2052 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
79008bdd 2053 log_unit_debug(u->id, "Failed to parse socket-fd value %s", value);
a16e1123 2054 else {
574634bc 2055 asynchronous_close(s->socket_fd);
a16e1123
LP
2056 s->socket_fd = fdset_remove(fds, fd);
2057 }
e44da745
DM
2058 } else if (streq(key, "endpoint-fd")) {
2059 int fd;
2060
2061 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
79008bdd 2062 log_unit_debug(u->id, "Failed to parse endpoint-fd value %s", value);
e44da745
DM
2063 else {
2064 safe_close(s->bus_endpoint_fd);
2065 s->bus_endpoint_fd = fdset_remove(fds, fd);
2066 }
2339fc93
LP
2067 } else if (streq(key, "fd-store-fd")) {
2068 int fd;
2069
2070 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2071 log_unit_debug(u->id, "Failed to parse fd-store-fd value %s", value);
2072 else {
2073 r = service_add_fd_store(s, fd);
2074 if (r < 0)
2075 log_unit_error_errno(u->id, r, "Failed to add fd to store: %m");
2076 else if (r > 0)
2077 fdset_remove(fds, fd);
2078 }
2079
ecdbca40
LP
2080 } else if (streq(key, "main-exec-status-pid")) {
2081 pid_t pid;
2082
e364ad06 2083 if (parse_pid(value, &pid) < 0)
79008bdd 2084 log_unit_debug(u->id, "Failed to parse main-exec-status-pid value %s", value);
ecdbca40
LP
2085 else
2086 s->main_exec_status.pid = pid;
2087 } else if (streq(key, "main-exec-status-code")) {
2088 int i;
2089
e364ad06 2090 if (safe_atoi(value, &i) < 0)
79008bdd 2091 log_unit_debug(u->id, "Failed to parse main-exec-status-code value %s", value);
ecdbca40
LP
2092 else
2093 s->main_exec_status.code = i;
2094 } else if (streq(key, "main-exec-status-status")) {
2095 int i;
2096
e364ad06 2097 if (safe_atoi(value, &i) < 0)
79008bdd 2098 log_unit_debug(u->id, "Failed to parse main-exec-status-status value %s", value);
ecdbca40
LP
2099 else
2100 s->main_exec_status.status = i;
799fd0fd
LP
2101 } else if (streq(key, "main-exec-status-start"))
2102 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2103 else if (streq(key, "main-exec-status-exit"))
2104 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
a6927d7f
MO
2105 else if (streq(key, "watchdog-timestamp"))
2106 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
613b411c 2107 else if (streq(key, "forbid-restart")) {
6aca9a58
SE
2108 int b;
2109
2110 b = parse_boolean(value);
2111 if (b < 0)
79008bdd 2112 log_unit_debug(u->id, "Failed to parse forbid-restart value %s", value);
6aca9a58
SE
2113 else
2114 s->forbid_restart = b;
c17ec25e 2115 } else
79008bdd 2116 log_unit_debug(u->id, "Unknown serialization key '%s'", key);
a16e1123
LP
2117
2118 return 0;
2119}
2120
44a6b1b6 2121_pure_ static UnitActiveState service_active_state(Unit *u) {
e056b01d
LP
2122 const UnitActiveState *table;
2123
87f0e418 2124 assert(u);
5cb5a6ff 2125
e056b01d
LP
2126 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
2127
2128 return table[SERVICE(u)->state];
034c6ed7
LP
2129}
2130
10a94420
LP
2131static const char *service_sub_state_to_string(Unit *u) {
2132 assert(u);
2133
2134 return service_state_to_string(SERVICE(u)->state);
2135}
2136
701cc384
LP
2137static bool service_check_gc(Unit *u) {
2138 Service *s = SERVICE(u);
2139
2140 assert(s);
2141
6d55002a
LP
2142 /* Never clean up services that still have a process around,
2143 * even if the service is formally dead. */
2144 if (cgroup_good(s) > 0 ||
2145 main_pid_good(s) > 0 ||
2146 control_pid_good(s) > 0)
2147 return true;
2148
6d55002a
LP
2149 return false;
2150}
2151
44a6b1b6 2152_pure_ static bool service_check_snapshot(Unit *u) {
701cc384
LP
2153 Service *s = SERVICE(u);
2154
2155 assert(s);
2156
8bb2d17d 2157 return s->socket_fd < 0;
701cc384
LP
2158}
2159
3a111838
MS
2160static int service_retry_pid_file(Service *s) {
2161 int r;
2162
2163 assert(s->pid_file);
2164 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2165
2166 r = service_load_pid_file(s, false);
2167 if (r < 0)
2168 return r;
2169
2170 service_unwatch_pid_file(s);
2171
f42806df 2172 service_enter_running(s, SERVICE_SUCCESS);
3a111838
MS
2173 return 0;
2174}
2175
2176static int service_watch_pid_file(Service *s) {
2177 int r;
2178
79008bdd 2179 log_unit_debug(UNIT(s)->id, "Setting watch for %s's PID file %s", UNIT(s)->id, s->pid_file_pathspec->path);
8bb2d17d 2180
718db961 2181 r = path_spec_watch(s->pid_file_pathspec, service_dispatch_io);
3a111838
MS
2182 if (r < 0)
2183 goto fail;
2184
2185 /* the pidfile might have appeared just before we set the watch */
79008bdd 2186 log_unit_debug(UNIT(s)->id, "Trying to read %s's PID file %s in case it changed", UNIT(s)->id, s->pid_file_pathspec->path);
3a111838
MS
2187 service_retry_pid_file(s);
2188
2189 return 0;
2190fail:
31938a85 2191 log_unit_error_errno(UNIT(s)->id, r, "Failed to set a watch for %s's PID file %s: %m", UNIT(s)->id, s->pid_file_pathspec->path);
3a111838
MS
2192 service_unwatch_pid_file(s);
2193 return r;
2194}
2195
2196static int service_demand_pid_file(Service *s) {
2197 PathSpec *ps;
2198
2199 assert(s->pid_file);
2200 assert(!s->pid_file_pathspec);
2201
2202 ps = new0(PathSpec, 1);
2203 if (!ps)
2204 return -ENOMEM;
2205
718db961 2206 ps->unit = UNIT(s);
3a111838
MS
2207 ps->path = strdup(s->pid_file);
2208 if (!ps->path) {
2209 free(ps);
2210 return -ENOMEM;
2211 }
2212
2213 path_kill_slashes(ps->path);
2214
2215 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2216 * keep their PID file open all the time. */
2217 ps->type = PATH_MODIFIED;
2218 ps->inotify_fd = -1;
2219
2220 s->pid_file_pathspec = ps;
2221
2222 return service_watch_pid_file(s);
2223}
2224
718db961 2225static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata) {
e14c2802
LP
2226 PathSpec *p = userdata;
2227 Service *s;
2228
2229 assert(p);
2230
2231 s = SERVICE(p->unit);
3a111838
MS
2232
2233 assert(s);
2234 assert(fd >= 0);
2235 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2236 assert(s->pid_file_pathspec);
57020a3a 2237 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
3a111838 2238
79008bdd 2239 log_unit_debug(UNIT(s)->id, "inotify event for %s", UNIT(s)->id);
3a111838 2240
e14c2802 2241 if (path_spec_fd_event(p, events) < 0)
3a111838
MS
2242 goto fail;
2243
2244 if (service_retry_pid_file(s) == 0)
718db961 2245 return 0;
3a111838
MS
2246
2247 if (service_watch_pid_file(s) < 0)
2248 goto fail;
2249
718db961
LP
2250 return 0;
2251
3a111838
MS
2252fail:
2253 service_unwatch_pid_file(s);
f42806df 2254 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
718db961 2255 return 0;
3a111838
MS
2256}
2257
a911bb9a
LP
2258static void service_notify_cgroup_empty_event(Unit *u) {
2259 Service *s = SERVICE(u);
2260
2261 assert(u);
2262
79008bdd 2263 log_unit_debug(u->id, "%s: cgroup is empty", u->id);
a911bb9a
LP
2264
2265 switch (s->state) {
2266
2267 /* Waiting for SIGCHLD is usually more interesting,
2268 * because it includes return codes/signals. Which is
2269 * why we ignore the cgroup events for most cases,
2270 * except when we don't know pid which to expect the
2271 * SIGCHLD for. */
2272
2273 case SERVICE_START:
2274 case SERVICE_START_POST:
2275 /* If we were hoping for the daemon to write its PID file,
2276 * we can give up now. */
2277 if (s->pid_file_pathspec) {
79008bdd 2278 log_unit_warning(u->id, "%s never wrote its PID file. Failing.", UNIT(s)->id);
8bb2d17d 2279
a911bb9a
LP
2280 service_unwatch_pid_file(s);
2281 if (s->state == SERVICE_START)
2282 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2283 else
2284 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2285 }
2286 break;
2287
2288 case SERVICE_RUNNING:
2289 /* service_enter_running() will figure out what to do */
2290 service_enter_running(s, SERVICE_SUCCESS);
2291 break;
2292
db2cb23b 2293 case SERVICE_STOP_SIGABRT:
a911bb9a
LP
2294 case SERVICE_STOP_SIGTERM:
2295 case SERVICE_STOP_SIGKILL:
2296
2297 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2298 service_enter_stop_post(s, SERVICE_SUCCESS);
2299
2300 break;
2301
2302 case SERVICE_STOP_POST:
2303 case SERVICE_FINAL_SIGTERM:
2304 case SERVICE_FINAL_SIGKILL:
2305 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2306 service_enter_dead(s, SERVICE_SUCCESS, true);
2307
2308 break;
2309
2310 default:
2311 ;
2312 }
2313}
2314
87f0e418
LP
2315static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2316 Service *s = SERVICE(u);
f42806df 2317 ServiceResult f;
5cb5a6ff
LP
2318
2319 assert(s);
034c6ed7
LP
2320 assert(pid >= 0);
2321
96342de6
LN
2322 if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) :
2323 is_clean_exit_lsb(code, status, &s->success_status))
f42806df
LP
2324 f = SERVICE_SUCCESS;
2325 else if (code == CLD_EXITED)
2326 f = SERVICE_FAILURE_EXIT_CODE;
2327 else if (code == CLD_KILLED)
2328 f = SERVICE_FAILURE_SIGNAL;
2329 else if (code == CLD_DUMPED)
2330 f = SERVICE_FAILURE_CORE_DUMP;
d06dacd0 2331 else
cfc4eb4c 2332 assert_not_reached("Unknown code");
034c6ed7
LP
2333
2334 if (s->main_pid == pid) {
db01f8b3
MS
2335 /* Forking services may occasionally move to a new PID.
2336 * As long as they update the PID file before exiting the old
2337 * PID, they're fine. */
5375410b 2338 if (service_load_pid_file(s, false) == 0)
db01f8b3 2339 return;
034c6ed7 2340
034c6ed7 2341 s->main_pid = 0;
6ea832a2 2342 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
034c6ed7 2343
867b3b7d 2344 if (s->main_command) {
fbeefb45
LP
2345 /* If this is not a forking service than the
2346 * main process got started and hence we copy
2347 * the exit status so that it is recorded both
2348 * as main and as control process exit
2349 * status */
2350
867b3b7d 2351 s->main_command->exec_status = s->main_exec_status;
b708e7ce 2352
867b3b7d 2353 if (s->main_command->ignore)
f42806df 2354 f = SERVICE_SUCCESS;
fbeefb45
LP
2355 } else if (s->exec_command[SERVICE_EXEC_START]) {
2356
2357 /* If this is a forked process, then we should
2358 * ignore the return value if this was
2359 * configured for the starter process */
2360
2361 if (s->exec_command[SERVICE_EXEC_START]->ignore)
2362 f = SERVICE_SUCCESS;
034c6ed7
LP
2363 }
2364
e2cc6eca
LP
2365 log_unit_struct(u->id,
2366 f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2367 LOG_MESSAGE("%s: main process exited, code=%s, status=%i/%s",
2368 u->id, sigchld_code_to_string(code), status,
2369 strna(code == CLD_EXITED
2370 ? exit_status_to_string(status, EXIT_STATUS_FULL)
2371 : signal_to_string(status))),
2372 "EXIT_CODE=%s", sigchld_code_to_string(code),
2373 "EXIT_STATUS=%i", status,
2374 NULL);
f42806df
LP
2375
2376 if (f != SERVICE_SUCCESS)
2377 s->result = f;
034c6ed7 2378
867b3b7d
LP
2379 if (s->main_command &&
2380 s->main_command->command_next &&
f42806df 2381 f == SERVICE_SUCCESS) {
034c6ed7 2382
34e9ba66
LP
2383 /* There is another command to *
2384 * execute, so let's do that. */
034c6ed7 2385
79008bdd 2386 log_unit_debug(u->id, "%s running next main command for state %s", u->id, service_state_to_string(s->state));
f42806df 2387 service_run_next_main(s);
034c6ed7 2388
34e9ba66
LP
2389 } else {
2390
2391 /* The service exited, so the service is officially
2392 * gone. */
867b3b7d 2393 s->main_command = NULL;
34e9ba66
LP
2394
2395 switch (s->state) {
2396
2397 case SERVICE_START_POST:
2398 case SERVICE_RELOAD:
2399 case SERVICE_STOP:
2400 /* Need to wait until the operation is
2401 * done */
c4653a4d 2402 break;
7d55e835 2403
34e9ba66
LP
2404 case SERVICE_START:
2405 if (s->type == SERVICE_ONESHOT) {
2406 /* This was our main goal, so let's go on */
f42806df 2407 if (f == SERVICE_SUCCESS)
34e9ba66
LP
2408 service_enter_start_post(s);
2409 else
f42806df 2410 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
34e9ba66 2411 break;
34e9ba66 2412 }
034c6ed7 2413
bfba3256
LP
2414 /* Fall through */
2415
34e9ba66 2416 case SERVICE_RUNNING:
f42806df 2417 service_enter_running(s, f);
34e9ba66 2418 break;
034c6ed7 2419
db2cb23b 2420 case SERVICE_STOP_SIGABRT:
34e9ba66
LP
2421 case SERVICE_STOP_SIGTERM:
2422 case SERVICE_STOP_SIGKILL:
5cb5a6ff 2423
34e9ba66 2424 if (!control_pid_good(s))
f42806df 2425 service_enter_stop_post(s, f);
5cb5a6ff 2426
34e9ba66
LP
2427 /* If there is still a control process, wait for that first */
2428 break;
2429
bf108e55
LP
2430 case SERVICE_STOP_POST:
2431 case SERVICE_FINAL_SIGTERM:
2432 case SERVICE_FINAL_SIGKILL:
2433
2434 if (!control_pid_good(s))
2435 service_enter_dead(s, f, true);
2436 break;
2437
34e9ba66
LP
2438 default:
2439 assert_not_reached("Uh, main process died at wrong time.");
2440 }
034c6ed7 2441 }
5cb5a6ff 2442
034c6ed7 2443 } else if (s->control_pid == pid) {
34e9ba66
LP
2444 s->control_pid = 0;
2445
b708e7ce 2446 if (s->control_command) {
8bb2d17d 2447 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
a16e1123 2448
b708e7ce 2449 if (s->control_command->ignore)
f42806df 2450 f = SERVICE_SUCCESS;
b708e7ce
LP
2451 }
2452
79008bdd
LP
2453 log_unit_full(u->id,
2454 f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
66870f90
ZJS
2455 "%s: control process exited, code=%s status=%i",
2456 u->id, sigchld_code_to_string(code), status);
f42806df
LP
2457
2458 if (f != SERVICE_SUCCESS)
2459 s->result = f;
034c6ed7 2460
88f3e0c9
LP
2461 /* Immediately get rid of the cgroup, so that the
2462 * kernel doesn't delay the cgroup empty messages for
2463 * the service cgroup any longer than necessary */
4ad49000 2464 service_kill_control_processes(s);
88f3e0c9 2465
34e9ba66
LP
2466 if (s->control_command &&
2467 s->control_command->command_next &&
f42806df 2468 f == SERVICE_SUCCESS) {
034c6ed7
LP
2469
2470 /* There is another command to *
2471 * execute, so let's do that. */
2472
79008bdd 2473 log_unit_debug(u->id, "%s running next control command for state %s", u->id, service_state_to_string(s->state));
f42806df 2474 service_run_next_control(s);
034c6ed7 2475
80876c20 2476 } else {
034c6ed7
LP
2477 /* No further commands for this step, so let's
2478 * figure out what to do next */
2479
a16e1123
LP
2480 s->control_command = NULL;
2481 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2482
79008bdd 2483 log_unit_debug(u->id, "%s got final SIGCHLD for state %s", u->id, service_state_to_string(s->state));
bd982a8b 2484
034c6ed7
LP
2485 switch (s->state) {
2486
2487 case SERVICE_START_PRE:
f42806df 2488 if (f == SERVICE_SUCCESS)
034c6ed7
LP
2489 service_enter_start(s);
2490 else
f42806df 2491 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
034c6ed7
LP
2492 break;
2493
2494 case SERVICE_START:
bfba3256
LP
2495 if (s->type != SERVICE_FORKING)
2496 /* Maybe spurious event due to a reload that changed the type? */
2497 break;
034c6ed7 2498
f42806df
LP
2499 if (f != SERVICE_SUCCESS) {
2500 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3a111838
MS
2501 break;
2502 }
034c6ed7 2503
3a111838 2504 if (s->pid_file) {
f42806df
LP
2505 bool has_start_post;
2506 int r;
2507
3a111838
MS
2508 /* Let's try to load the pid file here if we can.
2509 * The PID file might actually be created by a START_POST
2510 * script. In that case don't worry if the loading fails. */
f42806df
LP
2511
2512 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
2513 r = service_load_pid_file(s, !has_start_post);
3a111838
MS
2514 if (!has_start_post && r < 0) {
2515 r = service_demand_pid_file(s);
2516 if (r < 0 || !cgroup_good(s))
f42806df 2517 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3a111838
MS
2518 break;
2519 }
034c6ed7 2520 } else
3a111838 2521 service_search_main_pid(s);
034c6ed7 2522
3a111838 2523 service_enter_start_post(s);
034c6ed7
LP
2524 break;
2525
2526 case SERVICE_START_POST:
f42806df
LP
2527 if (f != SERVICE_SUCCESS) {
2528 service_enter_stop(s, f);
2096e009 2529 break;
034c6ed7
LP
2530 }
2531
2096e009 2532 if (s->pid_file) {
f42806df
LP
2533 int r;
2534
2535 r = service_load_pid_file(s, true);
2096e009
MS
2536 if (r < 0) {
2537 r = service_demand_pid_file(s);
2538 if (r < 0 || !cgroup_good(s))
f42806df 2539 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2096e009
MS
2540 break;
2541 }
2542 } else
2543 service_search_main_pid(s);
2544
f42806df 2545 service_enter_running(s, SERVICE_SUCCESS);
3185a36b 2546 break;
034c6ed7
LP
2547
2548 case SERVICE_RELOAD:
f42806df 2549 if (f == SERVICE_SUCCESS) {
5375410b 2550 service_load_pid_file(s, true);
3185a36b
LP
2551 service_search_main_pid(s);
2552 }
2553
f42806df
LP
2554 s->reload_result = f;
2555 service_enter_running(s, SERVICE_SUCCESS);
034c6ed7
LP
2556 break;
2557
2558 case SERVICE_STOP:
f42806df 2559 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
034c6ed7
LP
2560 break;
2561
db2cb23b 2562 case SERVICE_STOP_SIGABRT:
034c6ed7
LP
2563 case SERVICE_STOP_SIGTERM:
2564 case SERVICE_STOP_SIGKILL:
2565 if (main_pid_good(s) <= 0)
f42806df 2566 service_enter_stop_post(s, f);
034c6ed7
LP
2567
2568 /* If there is still a service
2569 * process around, wait until
2570 * that one quit, too */
2571 break;
2572
2573 case SERVICE_STOP_POST:
2574 case SERVICE_FINAL_SIGTERM:
2575 case SERVICE_FINAL_SIGKILL:
bf108e55
LP
2576 if (main_pid_good(s) <= 0)
2577 service_enter_dead(s, f, true);
034c6ed7
LP
2578 break;
2579
2580 default:
2581 assert_not_reached("Uh, control process died at wrong time.");
2582 }
2583 }
8c47c732 2584 }
c4e2ceae
LP
2585
2586 /* Notify clients about changed exit status */
2587 unit_add_to_dbus_queue(u);
a911bb9a
LP
2588
2589 /* We got one SIGCHLD for the service, let's watch all
2590 * processes that are now running of the service, and watch
2591 * that. Among the PIDs we then watch will be children
2592 * reassigned to us, which hopefully allows us to identify
2593 * when all children are gone */
2594 unit_tidy_watch_pids(u, s->main_pid, s->control_pid);
2595 unit_watch_all_pids(u);
2596
2597 /* If the PID set is empty now, then let's finish this off */
2598 if (set_isempty(u->pids))
2599 service_notify_cgroup_empty_event(u);
034c6ed7
LP
2600}
2601
718db961
LP
2602static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
2603 Service *s = SERVICE(userdata);
034c6ed7
LP
2604
2605 assert(s);
718db961 2606 assert(source == s->timer_event_source);
034c6ed7
LP
2607
2608 switch (s->state) {
2609
2610 case SERVICE_START_PRE:
2611 case SERVICE_START:
79008bdd 2612 log_unit_warning(UNIT(s)->id, "%s %s operation timed out. Terminating.", UNIT(s)->id, s->state == SERVICE_START ? "start" : "start-pre");
f42806df 2613 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
80876c20
LP
2614 break;
2615
034c6ed7 2616 case SERVICE_START_POST:
79008bdd 2617 log_unit_warning(UNIT(s)->id, "%s start-post operation timed out. Stopping.", UNIT(s)->id);
f42806df 2618 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
034c6ed7
LP
2619 break;
2620
e2f3b44c 2621 case SERVICE_RELOAD:
79008bdd 2622 log_unit_warning(UNIT(s)->id, "%s reload operation timed out. Stopping.", UNIT(s)->id);
f42806df
LP
2623 s->reload_result = SERVICE_FAILURE_TIMEOUT;
2624 service_enter_running(s, SERVICE_SUCCESS);
e2f3b44c
LP
2625 break;
2626
034c6ed7 2627 case SERVICE_STOP:
79008bdd 2628 log_unit_warning(UNIT(s)->id, "%s stopping timed out. Terminating.", UNIT(s)->id);
f42806df 2629 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
034c6ed7
LP
2630 break;
2631
db2cb23b 2632 case SERVICE_STOP_SIGABRT:
79008bdd 2633 log_unit_warning(UNIT(s)->id,
db2cb23b
UTL
2634 "%s stop-sigabrt timed out. Terminating.", UNIT(s)->id);
2635 service_enter_signal(s, SERVICE_STOP_SIGTERM, s->result);
2636 break;
2637
034c6ed7 2638 case SERVICE_STOP_SIGTERM:
4819ff03 2639 if (s->kill_context.send_sigkill) {
79008bdd 2640 log_unit_warning(UNIT(s)->id, "%s stop-sigterm timed out. Killing.", UNIT(s)->id);
f42806df 2641 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
ba035df2 2642 } else {
79008bdd 2643 log_unit_warning(UNIT(s)->id, "%s stop-sigterm timed out. Skipping SIGKILL.", UNIT(s)->id);
f42806df 2644 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
ba035df2
LP
2645 }
2646
034c6ed7
LP
2647 break;
2648
2649 case SERVICE_STOP_SIGKILL:
35b8ca3a 2650 /* Uh, we sent a SIGKILL and it is still not gone?
034c6ed7
LP
2651 * Must be something we cannot kill, so let's just be
2652 * weirded out and continue */
2653
79008bdd 2654 log_unit_warning(UNIT(s)->id, "%s still around after SIGKILL. Ignoring.", UNIT(s)->id);
f42806df 2655 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
034c6ed7
LP
2656 break;
2657
2658 case SERVICE_STOP_POST:
79008bdd 2659 log_unit_warning(UNIT(s)->id, "%s stop-post timed out. Terminating.", UNIT(s)->id);
f42806df 2660 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
034c6ed7
LP
2661 break;
2662
2663 case SERVICE_FINAL_SIGTERM:
4819ff03 2664 if (s->kill_context.send_sigkill) {
79008bdd 2665 log_unit_warning(UNIT(s)->id, "%s stop-final-sigterm timed out. Killing.", UNIT(s)->id);
f42806df 2666 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
ba035df2 2667 } else {
79008bdd 2668 log_unit_warning(UNIT(s)->id, "%s stop-final-sigterm timed out. Skipping SIGKILL. Entering failed mode.", UNIT(s)->id);
f42806df 2669 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
ba035df2
LP
2670 }
2671
034c6ed7
LP
2672 break;
2673
2674 case SERVICE_FINAL_SIGKILL:
79008bdd 2675 log_unit_warning(UNIT(s)->id, "%s still around after final SIGKILL. Entering failed mode.", UNIT(s)->id);
f42806df 2676 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
034c6ed7
LP
2677 break;
2678
2679 case SERVICE_AUTO_RESTART:
79008bdd 2680 log_unit_info(UNIT(s)->id,
ef417cfd
ZJS
2681 s->restart_usec > 0 ?
2682 "%s holdoff time over, scheduling restart." :
2683 "%s has no holdoff time, scheduling restart.",
2684 UNIT(s)->id);
034c6ed7
LP
2685 service_enter_restart(s);
2686 break;
2687
2688 default:
2689 assert_not_reached("Timeout at wrong time.");
2690 }
718db961
LP
2691
2692 return 0;
2693}
2694
2695static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata) {
2696 Service *s = SERVICE(userdata);
a7850c7d 2697 char t[FORMAT_TIMESPAN_MAX];
718db961
LP
2698
2699 assert(s);
2700 assert(source == s->watchdog_event_source);
2701
79008bdd 2702 log_unit_error(UNIT(s)->id, "%s watchdog timeout (limit %s)!", UNIT(s)->id,
a7850c7d 2703 format_timespan(t, sizeof(t), s->watchdog_usec, 1));
8bb2d17d 2704
db2cb23b 2705 service_enter_signal(s, SERVICE_STOP_SIGABRT, SERVICE_FAILURE_WATCHDOG);
842129f5 2706
718db961 2707 return 0;
5cb5a6ff
LP
2708}
2709
a354329f 2710static void service_notify_message(Unit *u, pid_t pid, char **tags, FDSet *fds) {
8c47c732 2711 Service *s = SERVICE(u);
308d72dc 2712 _cleanup_free_ char *cc = NULL;
30b5275a 2713 bool notify_dbus = false;
308d72dc 2714 const char *e;
8c47c732
LP
2715
2716 assert(u);
2717
308d72dc 2718 cc = strv_join(tags, ", ");
79008bdd 2719 log_unit_debug(u->id, "%s: Got notification message from PID "PID_FMT" (%s)",
308d72dc 2720 u->id, pid, isempty(cc) ? "n/a" : cc);
da13d4d2 2721
c952c6ec 2722 if (s->notify_access == NOTIFY_NONE) {
79008bdd 2723 log_unit_warning(u->id, "%s: Got notification message from PID "PID_FMT", but reception is disabled.", u->id, pid);
c952c6ec
LP
2724 return;
2725 }
2726
336c6e46 2727 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
336c6e46 2728 if (s->main_pid != 0)
79008bdd 2729 log_unit_warning(u->id, "%s: Got notification message from PID "PID_FMT", but reception only permitted for main PID "PID_FMT, u->id, pid, s->main_pid);
336c6e46 2730 else
79008bdd 2731 log_unit_debug(u->id, "%s: Got notification message from PID "PID_FMT", but reception only permitted for main PID which is currently not known", u->id, pid);
c952c6ec
LP
2732 return;
2733 }
2734
8c47c732 2735 /* Interpret MAINPID= */
28849dba 2736 e = strv_find_startswith(tags, "MAINPID=");
5e56b378 2737 if (e && IN_SET(s->state, SERVICE_START, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) {
28849dba 2738 if (parse_pid(e, &pid) < 0)
79008bdd 2739 log_unit_warning(u->id, "Failed to parse MAINPID= field in notification message: %s", e);
8c47c732 2740 else {
79008bdd 2741 log_unit_debug(u->id, "%s: got MAINPID=%s", u->id, e);
28849dba 2742
5925dd3c 2743 service_set_main_pid(s, pid);
7400b9d2 2744 unit_watch_pid(UNIT(s), pid);
30b5275a 2745 notify_dbus = true;
8c47c732
LP
2746 }
2747 }
2748
308d72dc
LP
2749 /* Interpret RELOADING= */
2750 if (strv_find(tags, "RELOADING=1")) {
2751
79008bdd 2752 log_unit_debug(u->id, "%s: got RELOADING=1", u->id);
308d72dc
LP
2753 s->notify_state = NOTIFY_RELOADING;
2754
2755 if (s->state == SERVICE_RUNNING)
2756 service_enter_reload_by_notify(s);
2757
2758 notify_dbus = true;
2759 }
2760
8c47c732 2761 /* Interpret READY= */
308d72dc
LP
2762 if (strv_find(tags, "READY=1")) {
2763
79008bdd 2764 log_unit_debug(u->id, "%s: got READY=1", u->id);
308d72dc
LP
2765 s->notify_state = NOTIFY_READY;
2766
2767 /* Type=notify services inform us about completed
2768 * initialization with READY=1 */
2769 if (s->type == SERVICE_NOTIFY && s->state == SERVICE_START)
2770 service_enter_start_post(s);
2771
2772 /* Sending READY=1 while we are reloading informs us
2773 * that the reloading is complete */
2774 if (s->state == SERVICE_RELOAD && s->control_pid == 0)
2775 service_enter_running(s, SERVICE_SUCCESS);
2776
2777 notify_dbus = true;
2778 }
2779
2780 /* Interpret STOPPING= */
2781 if (strv_find(tags, "STOPPING=1")) {
2782
79008bdd 2783 log_unit_debug(u->id, "%s: got STOPPING=1", u->id);
308d72dc
LP
2784 s->notify_state = NOTIFY_STOPPING;
2785
2786 if (s->state == SERVICE_RUNNING)
2787 service_enter_stop_by_notify(s);
2788
30b5275a 2789 notify_dbus = true;
8c47c732
LP
2790 }
2791
2792 /* Interpret STATUS= */
28849dba 2793 e = strv_find_startswith(tags, "STATUS=");
7f110ff9 2794 if (e) {
28849dba 2795 _cleanup_free_ char *t = NULL;
8c47c732 2796
28849dba
LP
2797 if (!isempty(e)) {
2798 if (!utf8_is_valid(e))
79008bdd 2799 log_unit_warning(u->id, "Status message in notification is not UTF-8 clean.");
28849dba 2800 else {
79008bdd 2801 log_unit_debug(u->id, "%s: got STATUS=%s", u->id, e);
7f110ff9 2802
28849dba
LP
2803 t = strdup(e);
2804 if (!t)
2805 log_oom();
3a2776bc 2806 }
28849dba 2807 }
8c47c732 2808
30b5275a 2809 if (!streq_ptr(s->status_text, t)) {
28849dba 2810
3a2776bc
LP
2811 free(s->status_text);
2812 s->status_text = t;
28849dba
LP
2813 t = NULL;
2814
30b5275a 2815 notify_dbus = true;
28849dba 2816 }
8c47c732 2817 }
842129f5 2818
4774e357 2819 /* Interpret ERRNO= */
28849dba 2820 e = strv_find_startswith(tags, "ERRNO=");
4774e357
MAA
2821 if (e) {
2822 int status_errno;
2823
28849dba 2824 if (safe_atoi(e, &status_errno) < 0 || status_errno < 0)
79008bdd 2825 log_unit_warning(u->id, "Failed to parse ERRNO= field in notification message: %s", e);
4774e357 2826 else {
79008bdd 2827 log_unit_debug(u->id, "%s: got ERRNO=%s", u->id, e);
4774e357
MAA
2828
2829 if (s->status_errno != status_errno) {
2830 s->status_errno = status_errno;
2831 notify_dbus = true;
2832 }
2833 }
2834 }
2835
6f285378 2836 /* Interpret WATCHDOG= */
a6927d7f 2837 if (strv_find(tags, "WATCHDOG=1")) {
79008bdd 2838 log_unit_debug(u->id, "%s: got WATCHDOG=1", u->id);
842129f5 2839 service_reset_watchdog(s);
a6927d7f 2840 }
c4e2ceae 2841
a354329f
LP
2842 /* Add the passed fds to the fd store */
2843 if (strv_find(tags, "FDSTORE=1")) {
2844 log_unit_debug(u->id, "%s: got FDSTORE=1", u->id);
2339fc93 2845 service_add_fd_store_set(s, fds);
a354329f
LP
2846 }
2847
c4e2ceae 2848 /* Notify clients about changed status or main pid */
30b5275a
LP
2849 if (notify_dbus)
2850 unit_add_to_dbus_queue(u);
8c47c732
LP
2851}
2852
68db7a3b
ZJS
2853static int service_get_timeout(Unit *u, uint64_t *timeout) {
2854 Service *s = SERVICE(u);
2855 int r;
2856
2857 if (!s->timer_event_source)
2858 return 0;
2859
2860 r = sd_event_source_get_time(s->timer_event_source, timeout);
2861 if (r < 0)
2862 return r;
2863
2864 return 1;
2865}
2866
05e343b7
LP
2867static void service_bus_name_owner_change(
2868 Unit *u,
2869 const char *name,
2870 const char *old_owner,
2871 const char *new_owner) {
2872
2873 Service *s = SERVICE(u);
718db961 2874 int r;
05e343b7
LP
2875
2876 assert(s);
2877 assert(name);
2878
2879 assert(streq(s->bus_name, name));
2880 assert(old_owner || new_owner);
2881
2882 if (old_owner && new_owner)
79008bdd 2883 log_unit_debug(u->id, "%s's D-Bus name %s changed owner from %s to %s", u->id, name, old_owner, new_owner);
05e343b7 2884 else if (old_owner)
79008bdd 2885 log_unit_debug(u->id, "%s's D-Bus name %s no longer registered by %s", u->id, name, old_owner);
05e343b7 2886 else
79008bdd 2887 log_unit_debug(u->id, "%s's D-Bus name %s now registered by %s", u->id, name, new_owner);
05e343b7
LP
2888
2889 s->bus_name_good = !!new_owner;
2890
2891 if (s->type == SERVICE_DBUS) {
2892
2893 /* service_enter_running() will figure out what to
2894 * do */
2895 if (s->state == SERVICE_RUNNING)
f42806df 2896 service_enter_running(s, SERVICE_SUCCESS);
05e343b7
LP
2897 else if (s->state == SERVICE_START && new_owner)
2898 service_enter_start_post(s);
2899
2900 } else if (new_owner &&
2901 s->main_pid <= 0 &&
2902 (s->state == SERVICE_START ||
2903 s->state == SERVICE_START_POST ||
2904 s->state == SERVICE_RUNNING ||
2905 s->state == SERVICE_RELOAD)) {
2906
5b12334d 2907 _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
718db961 2908 pid_t pid;
05e343b7 2909
718db961 2910 /* Try to acquire PID from bus service */
05e343b7 2911
056f95d0 2912 r = sd_bus_get_name_creds(u->manager->api_bus, name, SD_BUS_CREDS_PID, &creds);
5b12334d
LP
2913 if (r >= 0)
2914 r = sd_bus_creds_get_pid(creds, &pid);
718db961 2915 if (r >= 0) {
79008bdd 2916 log_unit_debug(u->id, "%s's D-Bus name %s is now owned by process %u", u->id, name, (unsigned) pid);
05e343b7 2917
718db961
LP
2918 service_set_main_pid(s, pid);
2919 unit_watch_pid(UNIT(s), pid);
2920 }
7400b9d2 2921 }
05e343b7
LP
2922}
2923
16115b0a 2924int service_set_socket_fd(Service *s, int fd, Socket *sock, bool selinux_context_net) {
79a98c60
LP
2925 _cleanup_free_ char *peer = NULL;
2926 int r;
57020a3a 2927
4f2d528d
LP
2928 assert(s);
2929 assert(fd >= 0);
2930
2931 /* This is called by the socket code when instantiating a new
2932 * service for a stream socket and the socket needs to be
2933 * configured. */
2934
1124fe6f 2935 if (UNIT(s)->load_state != UNIT_LOADED)
4f2d528d
LP
2936 return -EINVAL;
2937
2938 if (s->socket_fd >= 0)
2939 return -EBUSY;
2940
2941 if (s->state != SERVICE_DEAD)
2942 return -EAGAIN;
2943
79a98c60
LP
2944 if (getpeername_pretty(fd, &peer) >= 0) {
2945
2946 if (UNIT(s)->description) {
2947 _cleanup_free_ char *a;
2948
2949 a = strjoin(UNIT(s)->description, " (", peer, ")", NULL);
2950 if (!a)
2951 return -ENOMEM;
2952
2953 r = unit_set_description(UNIT(s), a);
2954 } else
2955 r = unit_set_description(UNIT(s), peer);
2956
2957 if (r < 0)
2958 return r;
2959 }
2960
4f2d528d 2961 s->socket_fd = fd;
16115b0a 2962 s->socket_fd_selinux_context_net = selinux_context_net;
6cf6bbc2 2963
57020a3a
LP
2964 unit_ref_set(&s->accept_socket, UNIT(sock));
2965
2966 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
4f2d528d
LP
2967}
2968
fdf20a31 2969static void service_reset_failed(Unit *u) {
5632e374
LP
2970 Service *s = SERVICE(u);
2971
2972 assert(s);
2973
fdf20a31 2974 if (s->state == SERVICE_FAILED)
5632e374
LP
2975 service_set_state(s, SERVICE_DEAD);
2976
f42806df
LP
2977 s->result = SERVICE_SUCCESS;
2978 s->reload_result = SERVICE_SUCCESS;
451b34cc
LP
2979
2980 RATELIMIT_RESET(s->start_limit);
5632e374
LP
2981}
2982
718db961 2983static int service_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
8a0867d6 2984 Service *s = SERVICE(u);
41efeaec 2985
814cc562 2986 return unit_kill_common(u, who, signo, s->main_pid, s->control_pid, error);
8a0867d6
LP
2987}
2988
94f04347
LP
2989static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2990 [SERVICE_DEAD] = "dead",
2991 [SERVICE_START_PRE] = "start-pre",
2992 [SERVICE_START] = "start",
2993 [SERVICE_START_POST] = "start-post",
2994 [SERVICE_RUNNING] = "running",
80876c20 2995 [SERVICE_EXITED] = "exited",
94f04347
LP
2996 [SERVICE_RELOAD] = "reload",
2997 [SERVICE_STOP] = "stop",
1378a3b5 2998 [SERVICE_STOP_SIGABRT] = "stop-sigabrt",
94f04347
LP
2999 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3000 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3001 [SERVICE_STOP_POST] = "stop-post",
3002 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3003 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
fdf20a31 3004 [SERVICE_FAILED] = "failed",
94f04347
LP
3005 [SERVICE_AUTO_RESTART] = "auto-restart",
3006};
3007
3008DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3009
3010static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
525ee6f4
LP
3011 [SERVICE_RESTART_NO] = "no",
3012 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
50caaedb 3013 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
6cfe2fde 3014 [SERVICE_RESTART_ON_ABNORMAL] = "on-abnormal",
dc99a976 3015 [SERVICE_RESTART_ON_WATCHDOG] = "on-watchdog",
50caaedb 3016 [SERVICE_RESTART_ON_ABORT] = "on-abort",
6cfe2fde 3017 [SERVICE_RESTART_ALWAYS] = "always",
94f04347
LP
3018};
3019
3020DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3021
3022static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
94f04347 3023 [SERVICE_SIMPLE] = "simple",
0d624a78 3024 [SERVICE_FORKING] = "forking",
34e9ba66 3025 [SERVICE_ONESHOT] = "oneshot",
8c47c732 3026 [SERVICE_DBUS] = "dbus",
f2b68789
LP
3027 [SERVICE_NOTIFY] = "notify",
3028 [SERVICE_IDLE] = "idle"
94f04347
LP
3029};
3030
3031DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3032
e537352b 3033static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
94f04347
LP
3034 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3035 [SERVICE_EXEC_START] = "ExecStart",
3036 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3037 [SERVICE_EXEC_RELOAD] = "ExecReload",
3038 [SERVICE_EXEC_STOP] = "ExecStop",
3039 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3040};
3041
3042DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3043
c952c6ec
LP
3044static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3045 [NOTIFY_NONE] = "none",
3046 [NOTIFY_MAIN] = "main",
3047 [NOTIFY_ALL] = "all"
3048};
3049
3050DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3051
308d72dc
LP
3052static const char* const notify_state_table[_NOTIFY_STATE_MAX] = {
3053 [NOTIFY_UNKNOWN] = "unknown",
3054 [NOTIFY_READY] = "ready",
3055 [NOTIFY_RELOADING] = "reloading",
3056 [NOTIFY_STOPPING] = "stopping",
3057};
3058
3059DEFINE_STRING_TABLE_LOOKUP(notify_state, NotifyState);
3060
f42806df
LP
3061static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3062 [SERVICE_SUCCESS] = "success",
3063 [SERVICE_FAILURE_RESOURCES] = "resources",
3064 [SERVICE_FAILURE_TIMEOUT] = "timeout",
3065 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3066 [SERVICE_FAILURE_SIGNAL] = "signal",
bb242b7b 3067 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
8d1b002a
LP
3068 [SERVICE_FAILURE_WATCHDOG] = "watchdog",
3069 [SERVICE_FAILURE_START_LIMIT] = "start-limit"
f42806df
LP
3070};
3071
3072DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3073
87f0e418 3074const UnitVTable service_vtable = {
7d17cfbc 3075 .object_size = sizeof(Service),
718db961
LP
3076 .exec_context_offset = offsetof(Service, exec_context),
3077 .cgroup_context_offset = offsetof(Service, cgroup_context),
3078 .kill_context_offset = offsetof(Service, kill_context),
613b411c 3079 .exec_runtime_offset = offsetof(Service, exec_runtime),
3ef63c31 3080
f975e971
LP
3081 .sections =
3082 "Unit\0"
3083 "Service\0"
3084 "Install\0",
4ad49000 3085 .private_section = "Service",
71645aca 3086
034c6ed7
LP
3087 .init = service_init,
3088 .done = service_done,
a16e1123 3089 .load = service_load,
a354329f 3090 .release_resources = service_release_resources,
a16e1123
LP
3091
3092 .coldplug = service_coldplug,
034c6ed7 3093
5cb5a6ff
LP
3094 .dump = service_dump,
3095
3096 .start = service_start,
3097 .stop = service_stop,
3098 .reload = service_reload,
3099
034c6ed7
LP
3100 .can_reload = service_can_reload,
3101
8a0867d6
LP
3102 .kill = service_kill,
3103
a16e1123
LP
3104 .serialize = service_serialize,
3105 .deserialize_item = service_deserialize_item,
3106
5cb5a6ff 3107 .active_state = service_active_state,
10a94420 3108 .sub_state_to_string = service_sub_state_to_string,
5cb5a6ff 3109
701cc384
LP
3110 .check_gc = service_check_gc,
3111 .check_snapshot = service_check_snapshot,
3112
034c6ed7 3113 .sigchld_event = service_sigchld_event,
2c4104f0 3114
fdf20a31 3115 .reset_failed = service_reset_failed,
5632e374 3116
4ad49000 3117 .notify_cgroup_empty = service_notify_cgroup_empty_event,
8c47c732 3118 .notify_message = service_notify_message,
8e274523 3119
05e343b7 3120 .bus_name_owner_change = service_bus_name_owner_change,
05e343b7 3121
c4e2ceae 3122 .bus_interface = "org.freedesktop.systemd1.Service",
718db961 3123 .bus_vtable = bus_service_vtable,
74c964d3
LP
3124 .bus_set_property = bus_service_set_property,
3125 .bus_commit_properties = bus_service_commit_properties,
4139c1b2 3126
68db7a3b 3127 .get_timeout = service_get_timeout,
718db961
LP
3128 .can_transient = true,
3129
c6918296
MS
3130 .status_message_formats = {
3131 .starting_stopping = {
3132 [0] = "Starting %s...",
3133 [1] = "Stopping %s...",
3134 },
3135 .finished_start_job = {
3136 [JOB_DONE] = "Started %s.",
3137 [JOB_FAILED] = "Failed to start %s.",
3138 [JOB_DEPENDENCY] = "Dependency failed for %s.",
3139 [JOB_TIMEOUT] = "Timed out starting %s.",
3140 },
3141 .finished_stop_job = {
3142 [JOB_DONE] = "Stopped %s.",
3143 [JOB_FAILED] = "Stopped (with error) %s.",
3144 [JOB_TIMEOUT] = "Timed out stopping %s.",
3145 },
3146 },
5cb5a6ff 3147};