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