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