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