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