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