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