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