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