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