]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/service.c
Merge pull request #3557 from whot/hwdb-updates
[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);
07299350 1960 int r;
5cb5a6ff
LP
1961
1962 assert(s);
1963
034c6ed7
LP
1964 /* We cannot fulfill this request right now, try again later
1965 * please! */
a00973af
LP
1966 if (IN_SET(s->state,
1967 SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST,
1968 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL))
5cb5a6ff
LP
1969 return -EAGAIN;
1970
034c6ed7 1971 /* Already on it! */
a00973af 1972 if (IN_SET(s->state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST))
034c6ed7
LP
1973 return 0;
1974
2e9d6c12 1975 /* A service that will be restarted must be stopped first to
7f2cddae 1976 * trigger BindsTo and/or OnFailure dependencies. If a user
2e9d6c12 1977 * does not want to wait for the holdoff time to elapse, the
d4943dc7
LP
1978 * service should be manually restarted, not started. We
1979 * simply return EAGAIN here, so that any start jobs stay
1980 * queued, and assume that the auto restart timer will
1981 * eventually trigger the restart. */
1982 if (s->state == SERVICE_AUTO_RESTART)
a8bb2e65 1983 return -EAGAIN;
2e9d6c12 1984
a00973af 1985 assert(IN_SET(s->state, SERVICE_DEAD, SERVICE_FAILED));
5cb5a6ff 1986
07299350
LP
1987 /* Make sure we don't enter a busy loop of some kind. */
1988 r = unit_start_limit_test(u);
1989 if (r < 0) {
1990 service_enter_dead(s, SERVICE_FAILURE_START_LIMIT_HIT, false);
1991 return r;
1992 }
1993
f42806df
LP
1994 s->result = SERVICE_SUCCESS;
1995 s->reload_result = SERVICE_SUCCESS;
034c6ed7 1996 s->main_pid_known = false;
6dfa5494 1997 s->main_pid_alien = false;
47342320 1998 s->forbid_restart = false;
5ad096b3 1999 s->reset_cpu_usage = true;
034c6ed7 2000
a1e58e8e 2001 s->status_text = mfree(s->status_text);
8cfdb077
LP
2002 s->status_errno = 0;
2003
308d72dc
LP
2004 s->notify_state = NOTIFY_UNKNOWN;
2005
034c6ed7 2006 service_enter_start_pre(s);
82a2b6bb 2007 return 1;
5cb5a6ff
LP
2008}
2009
87f0e418
LP
2010static int service_stop(Unit *u) {
2011 Service *s = SERVICE(u);
5cb5a6ff
LP
2012
2013 assert(s);
2014
a509f0e6 2015 /* Don't create restart jobs from manual stops. */
47342320 2016 s->forbid_restart = true;
034c6ed7 2017
e537352b 2018 /* Already on it */
a00973af
LP
2019 if (IN_SET(s->state,
2020 SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST,
2021 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL))
e537352b
LP
2022 return 0;
2023
f0c7b229 2024 /* A restart will be scheduled or is in progress. */
034c6ed7 2025 if (s->state == SERVICE_AUTO_RESTART) {
0c7f15b3 2026 service_set_state(s, SERVICE_DEAD);
034c6ed7
LP
2027 return 0;
2028 }
2029
3f6c78dc
LP
2030 /* If there's already something running we go directly into
2031 * kill mode. */
a00973af 2032 if (IN_SET(s->state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RELOAD)) {
f42806df 2033 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
3f6c78dc
LP
2034 return 0;
2035 }
5cb5a6ff 2036
a00973af 2037 assert(IN_SET(s->state, SERVICE_RUNNING, SERVICE_EXITED));
3a762661 2038
f42806df 2039 service_enter_stop(s, SERVICE_SUCCESS);
82a2b6bb 2040 return 1;
5cb5a6ff
LP
2041}
2042
87f0e418
LP
2043static int service_reload(Unit *u) {
2044 Service *s = SERVICE(u);
034c6ed7
LP
2045
2046 assert(s);
2047
80876c20 2048 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
034c6ed7
LP
2049
2050 service_enter_reload(s);
2d018ae2 2051 return 1;
5cb5a6ff
LP
2052}
2053
44a6b1b6 2054_pure_ static bool service_can_reload(Unit *u) {
87f0e418 2055 Service *s = SERVICE(u);
034c6ed7
LP
2056
2057 assert(s);
2058
2059 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2060}
2061
a16e1123
LP
2062static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2063 Service *s = SERVICE(u);
2339fc93 2064 ServiceFDStore *fs;
a34ceba6 2065 int r;
a16e1123
LP
2066
2067 assert(u);
2068 assert(f);
2069 assert(fds);
2070
2071 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
f42806df
LP
2072 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
2073 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
a16e1123
LP
2074
2075 if (s->control_pid > 0)
f06db334 2076 unit_serialize_item_format(u, f, "control-pid", PID_FMT, s->control_pid);
a16e1123 2077
5925dd3c 2078 if (s->main_pid_known && s->main_pid > 0)
ccd06097 2079 unit_serialize_item_format(u, f, "main-pid", PID_FMT, s->main_pid);
a16e1123
LP
2080
2081 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
de1d4f9b 2082 unit_serialize_item(u, f, "bus-name-good", yes_no(s->bus_name_good));
d8ccf5fd 2083 unit_serialize_item(u, f, "bus-name-owner", s->bus_name_owner);
a16e1123 2084
a34ceba6
LP
2085 r = unit_serialize_item_escaped(u, f, "status-text", s->status_text);
2086 if (r < 0)
2087 return r;
3a2776bc 2088
cfc4eb4c
LP
2089 /* FIXME: There's a minor uncleanliness here: if there are
2090 * multiple commands attached here, we will start from the
2091 * first one again */
a16e1123 2092 if (s->control_command_id >= 0)
f06db334 2093 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
a16e1123 2094
a34ceba6
LP
2095 r = unit_serialize_item_fd(u, f, fds, "stdin-fd", s->stdin_fd);
2096 if (r < 0)
2097 return r;
2098 r = unit_serialize_item_fd(u, f, fds, "stdout-fd", s->stdout_fd);
2099 if (r < 0)
2100 return r;
2101 r = unit_serialize_item_fd(u, f, fds, "stderr-fd", s->stderr_fd);
2102 if (r < 0)
2103 return r;
e44da745 2104
a34ceba6 2105 r = unit_serialize_item_fd(u, f, fds, "socket-fd", s->socket_fd);
a34ceba6
LP
2106 if (r < 0)
2107 return r;
e44da745 2108
2339fc93 2109 LIST_FOREACH(fd_store, fs, s->fd_store) {
8dd4c05b 2110 _cleanup_free_ char *c = NULL;
2339fc93
LP
2111 int copy;
2112
2113 copy = fdset_put_dup(fds, fs->fd);
2114 if (copy < 0)
2115 return copy;
2116
8dd4c05b
LP
2117 c = cescape(fs->fdname);
2118
2119 unit_serialize_item_format(u, f, "fd-store-fd", "%i %s", copy, strempty(c));
2339fc93
LP
2120 }
2121
ecdbca40 2122 if (s->main_exec_status.pid > 0) {
f06db334
LP
2123 unit_serialize_item_format(u, f, "main-exec-status-pid", PID_FMT, s->main_exec_status.pid);
2124 dual_timestamp_serialize(f, "main-exec-status-start", &s->main_exec_status.start_timestamp);
2125 dual_timestamp_serialize(f, "main-exec-status-exit", &s->main_exec_status.exit_timestamp);
ecdbca40 2126
799fd0fd 2127 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
f06db334
LP
2128 unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2129 unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
ecdbca40
LP
2130 }
2131 }
f06db334 2132
36b693a6 2133 dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp);
ecdbca40 2134
a34ceba6 2135 unit_serialize_item(u, f, "forbid-restart", yes_no(s->forbid_restart));
6aca9a58 2136
a16e1123
LP
2137 return 0;
2138}
2139
2140static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2141 Service *s = SERVICE(u);
2339fc93 2142 int r;
a16e1123
LP
2143
2144 assert(u);
2145 assert(key);
2146 assert(value);
2147 assert(fds);
2148
2149 if (streq(key, "state")) {
2150 ServiceState state;
2151
117dcc57
ZJS
2152 state = service_state_from_string(value);
2153 if (state < 0)
f2341e0a 2154 log_unit_debug(u, "Failed to parse state value: %s", value);
a16e1123
LP
2155 else
2156 s->deserialized_state = state;
f42806df
LP
2157 } else if (streq(key, "result")) {
2158 ServiceResult f;
2159
2160 f = service_result_from_string(value);
2161 if (f < 0)
f2341e0a 2162 log_unit_debug(u, "Failed to parse result value: %s", value);
f42806df
LP
2163 else if (f != SERVICE_SUCCESS)
2164 s->result = f;
2165
2166 } else if (streq(key, "reload-result")) {
2167 ServiceResult f;
2168
2169 f = service_result_from_string(value);
2170 if (f < 0)
f2341e0a 2171 log_unit_debug(u, "Failed to parse reload result value: %s", value);
f42806df
LP
2172 else if (f != SERVICE_SUCCESS)
2173 s->reload_result = f;
a16e1123 2174
a16e1123 2175 } else if (streq(key, "control-pid")) {
5925dd3c 2176 pid_t pid;
a16e1123 2177
e364ad06 2178 if (parse_pid(value, &pid) < 0)
f2341e0a 2179 log_unit_debug(u, "Failed to parse control-pid value: %s", value);
a16e1123 2180 else
e55224ca 2181 s->control_pid = pid;
a16e1123 2182 } else if (streq(key, "main-pid")) {
5925dd3c 2183 pid_t pid;
a16e1123 2184
e364ad06 2185 if (parse_pid(value, &pid) < 0)
f2341e0a 2186 log_unit_debug(u, "Failed to parse main-pid value: %s", value);
7400b9d2
LP
2187 else {
2188 service_set_main_pid(s, pid);
2189 unit_watch_pid(UNIT(s), pid);
2190 }
a16e1123
LP
2191 } else if (streq(key, "main-pid-known")) {
2192 int b;
2193
117dcc57
ZJS
2194 b = parse_boolean(value);
2195 if (b < 0)
f2341e0a 2196 log_unit_debug(u, "Failed to parse main-pid-known value: %s", value);
a16e1123
LP
2197 else
2198 s->main_pid_known = b;
de1d4f9b
WF
2199 } else if (streq(key, "bus-name-good")) {
2200 int b;
2201
2202 b = parse_boolean(value);
2203 if (b < 0)
2204 log_unit_debug(u, "Failed to parse bus-name-good value: %s", value);
2205 else
2206 s->bus_name_good = b;
d8ccf5fd
DM
2207 } else if (streq(key, "bus-name-owner")) {
2208 r = free_and_strdup(&s->bus_name_owner, value);
2209 if (r < 0)
2210 log_unit_error_errno(u, r, "Unable to deserialize current bus owner %s: %m", value);
3a2776bc
LP
2211 } else if (streq(key, "status-text")) {
2212 char *t;
2213
f2341e0a
LP
2214 r = cunescape(value, 0, &t);
2215 if (r < 0)
2216 log_unit_debug_errno(u, r, "Failed to unescape status text: %s", value);
117dcc57 2217 else {
3a2776bc
LP
2218 free(s->status_text);
2219 s->status_text = t;
2220 }
2221
a16e1123
LP
2222 } else if (streq(key, "control-command")) {
2223 ServiceExecCommand id;
2224
117dcc57
ZJS
2225 id = service_exec_command_from_string(value);
2226 if (id < 0)
f2341e0a 2227 log_unit_debug(u, "Failed to parse exec-command value: %s", value);
a16e1123
LP
2228 else {
2229 s->control_command_id = id;
2230 s->control_command = s->exec_command[id];
2231 }
2232 } else if (streq(key, "socket-fd")) {
2233 int fd;
2234
2235 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
f2341e0a 2236 log_unit_debug(u, "Failed to parse socket-fd value: %s", value);
a16e1123 2237 else {
574634bc 2238 asynchronous_close(s->socket_fd);
a16e1123
LP
2239 s->socket_fd = fdset_remove(fds, fd);
2240 }
2339fc93 2241 } else if (streq(key, "fd-store-fd")) {
8dd4c05b
LP
2242 const char *fdv;
2243 size_t pf;
2339fc93
LP
2244 int fd;
2245
8dd4c05b
LP
2246 pf = strcspn(value, WHITESPACE);
2247 fdv = strndupa(value, pf);
2248
2249 if (safe_atoi(fdv, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
f2341e0a 2250 log_unit_debug(u, "Failed to parse fd-store-fd value: %s", value);
2339fc93 2251 else {
8dd4c05b
LP
2252 _cleanup_free_ char *t = NULL;
2253 const char *fdn;
2254
2255 fdn = value + pf;
2256 fdn += strspn(fdn, WHITESPACE);
2257 (void) cunescape(fdn, 0, &t);
2258
2259 r = service_add_fd_store(s, fd, t);
2339fc93 2260 if (r < 0)
f2341e0a 2261 log_unit_error_errno(u, r, "Failed to add fd to store: %m");
2339fc93
LP
2262 else if (r > 0)
2263 fdset_remove(fds, fd);
2264 }
2265
ecdbca40
LP
2266 } else if (streq(key, "main-exec-status-pid")) {
2267 pid_t pid;
2268
e364ad06 2269 if (parse_pid(value, &pid) < 0)
f2341e0a 2270 log_unit_debug(u, "Failed to parse main-exec-status-pid value: %s", value);
ecdbca40
LP
2271 else
2272 s->main_exec_status.pid = pid;
2273 } else if (streq(key, "main-exec-status-code")) {
2274 int i;
2275
e364ad06 2276 if (safe_atoi(value, &i) < 0)
f2341e0a 2277 log_unit_debug(u, "Failed to parse main-exec-status-code value: %s", value);
ecdbca40
LP
2278 else
2279 s->main_exec_status.code = i;
2280 } else if (streq(key, "main-exec-status-status")) {
2281 int i;
2282
e364ad06 2283 if (safe_atoi(value, &i) < 0)
f2341e0a 2284 log_unit_debug(u, "Failed to parse main-exec-status-status value: %s", value);
ecdbca40
LP
2285 else
2286 s->main_exec_status.status = i;
799fd0fd
LP
2287 } else if (streq(key, "main-exec-status-start"))
2288 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2289 else if (streq(key, "main-exec-status-exit"))
2290 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
a6927d7f
MO
2291 else if (streq(key, "watchdog-timestamp"))
2292 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
613b411c 2293 else if (streq(key, "forbid-restart")) {
6aca9a58
SE
2294 int b;
2295
2296 b = parse_boolean(value);
2297 if (b < 0)
f2341e0a 2298 log_unit_debug(u, "Failed to parse forbid-restart value: %s", value);
6aca9a58
SE
2299 else
2300 s->forbid_restart = b;
a34ceba6
LP
2301 } else if (streq(key, "stdin-fd")) {
2302 int fd;
2303
2304 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2305 log_unit_debug(u, "Failed to parse stdin-fd value: %s", value);
2306 else {
2307 asynchronous_close(s->stdin_fd);
2308 s->stdin_fd = fdset_remove(fds, fd);
1e22b5cd 2309 s->exec_context.stdio_as_fds = true;
a34ceba6
LP
2310 }
2311 } else if (streq(key, "stdout-fd")) {
2312 int fd;
2313
2314 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2315 log_unit_debug(u, "Failed to parse stdout-fd value: %s", value);
2316 else {
2317 asynchronous_close(s->stdout_fd);
2318 s->stdout_fd = fdset_remove(fds, fd);
1e22b5cd 2319 s->exec_context.stdio_as_fds = true;
a34ceba6
LP
2320 }
2321 } else if (streq(key, "stderr-fd")) {
2322 int fd;
2323
2324 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2325 log_unit_debug(u, "Failed to parse stderr-fd value: %s", value);
2326 else {
2327 asynchronous_close(s->stderr_fd);
2328 s->stderr_fd = fdset_remove(fds, fd);
1e22b5cd 2329 s->exec_context.stdio_as_fds = true;
a34ceba6 2330 }
c17ec25e 2331 } else
f2341e0a 2332 log_unit_debug(u, "Unknown serialization key: %s", key);
a16e1123
LP
2333
2334 return 0;
2335}
2336
44a6b1b6 2337_pure_ static UnitActiveState service_active_state(Unit *u) {
e056b01d
LP
2338 const UnitActiveState *table;
2339
87f0e418 2340 assert(u);
5cb5a6ff 2341
e056b01d
LP
2342 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
2343
2344 return table[SERVICE(u)->state];
034c6ed7
LP
2345}
2346
10a94420
LP
2347static const char *service_sub_state_to_string(Unit *u) {
2348 assert(u);
2349
2350 return service_state_to_string(SERVICE(u)->state);
2351}
2352
701cc384
LP
2353static bool service_check_gc(Unit *u) {
2354 Service *s = SERVICE(u);
2355
2356 assert(s);
2357
6d55002a
LP
2358 /* Never clean up services that still have a process around,
2359 * even if the service is formally dead. */
2360 if (cgroup_good(s) > 0 ||
2361 main_pid_good(s) > 0 ||
2362 control_pid_good(s) > 0)
2363 return true;
2364
6d55002a
LP
2365 return false;
2366}
2367
3a111838
MS
2368static int service_retry_pid_file(Service *s) {
2369 int r;
2370
2371 assert(s->pid_file);
2372 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2373
2374 r = service_load_pid_file(s, false);
2375 if (r < 0)
2376 return r;
2377
2378 service_unwatch_pid_file(s);
2379
f42806df 2380 service_enter_running(s, SERVICE_SUCCESS);
3a111838
MS
2381 return 0;
2382}
2383
2384static int service_watch_pid_file(Service *s) {
2385 int r;
2386
f2341e0a 2387 log_unit_debug(UNIT(s), "Setting watch for PID file %s", s->pid_file_pathspec->path);
8bb2d17d 2388
718db961 2389 r = path_spec_watch(s->pid_file_pathspec, service_dispatch_io);
3a111838
MS
2390 if (r < 0)
2391 goto fail;
2392
2393 /* the pidfile might have appeared just before we set the watch */
f2341e0a 2394 log_unit_debug(UNIT(s), "Trying to read PID file %s in case it changed", s->pid_file_pathspec->path);
3a111838
MS
2395 service_retry_pid_file(s);
2396
2397 return 0;
2398fail:
f2341e0a 2399 log_unit_error_errno(UNIT(s), r, "Failed to set a watch for PID file %s: %m", s->pid_file_pathspec->path);
3a111838
MS
2400 service_unwatch_pid_file(s);
2401 return r;
2402}
2403
2404static int service_demand_pid_file(Service *s) {
2405 PathSpec *ps;
2406
2407 assert(s->pid_file);
2408 assert(!s->pid_file_pathspec);
2409
2410 ps = new0(PathSpec, 1);
2411 if (!ps)
2412 return -ENOMEM;
2413
718db961 2414 ps->unit = UNIT(s);
3a111838
MS
2415 ps->path = strdup(s->pid_file);
2416 if (!ps->path) {
2417 free(ps);
2418 return -ENOMEM;
2419 }
2420
2421 path_kill_slashes(ps->path);
2422
2423 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2424 * keep their PID file open all the time. */
2425 ps->type = PATH_MODIFIED;
2426 ps->inotify_fd = -1;
2427
2428 s->pid_file_pathspec = ps;
2429
2430 return service_watch_pid_file(s);
2431}
2432
718db961 2433static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata) {
e14c2802
LP
2434 PathSpec *p = userdata;
2435 Service *s;
2436
2437 assert(p);
2438
2439 s = SERVICE(p->unit);
3a111838
MS
2440
2441 assert(s);
2442 assert(fd >= 0);
2443 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2444 assert(s->pid_file_pathspec);
57020a3a 2445 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
3a111838 2446
f2341e0a 2447 log_unit_debug(UNIT(s), "inotify event");
3a111838 2448
e14c2802 2449 if (path_spec_fd_event(p, events) < 0)
3a111838
MS
2450 goto fail;
2451
2452 if (service_retry_pid_file(s) == 0)
718db961 2453 return 0;
3a111838
MS
2454
2455 if (service_watch_pid_file(s) < 0)
2456 goto fail;
2457
718db961
LP
2458 return 0;
2459
3a111838
MS
2460fail:
2461 service_unwatch_pid_file(s);
f42806df 2462 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
718db961 2463 return 0;
3a111838
MS
2464}
2465
a911bb9a
LP
2466static void service_notify_cgroup_empty_event(Unit *u) {
2467 Service *s = SERVICE(u);
2468
2469 assert(u);
2470
f2341e0a 2471 log_unit_debug(u, "cgroup is empty");
a911bb9a
LP
2472
2473 switch (s->state) {
2474
2475 /* Waiting for SIGCHLD is usually more interesting,
2476 * because it includes return codes/signals. Which is
2477 * why we ignore the cgroup events for most cases,
2478 * except when we don't know pid which to expect the
2479 * SIGCHLD for. */
2480
2481 case SERVICE_START:
2482 case SERVICE_START_POST:
2483 /* If we were hoping for the daemon to write its PID file,
2484 * we can give up now. */
2485 if (s->pid_file_pathspec) {
f2341e0a 2486 log_unit_warning(u, "Daemon never wrote its PID file. Failing.");
8bb2d17d 2487
a911bb9a
LP
2488 service_unwatch_pid_file(s);
2489 if (s->state == SERVICE_START)
2490 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2491 else
2492 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2493 }
2494 break;
2495
2496 case SERVICE_RUNNING:
2497 /* service_enter_running() will figure out what to do */
2498 service_enter_running(s, SERVICE_SUCCESS);
2499 break;
2500
db2cb23b 2501 case SERVICE_STOP_SIGABRT:
a911bb9a
LP
2502 case SERVICE_STOP_SIGTERM:
2503 case SERVICE_STOP_SIGKILL:
2504
2505 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2506 service_enter_stop_post(s, SERVICE_SUCCESS);
2507
2508 break;
2509
2510 case SERVICE_STOP_POST:
2511 case SERVICE_FINAL_SIGTERM:
2512 case SERVICE_FINAL_SIGKILL:
2513 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2514 service_enter_dead(s, SERVICE_SUCCESS, true);
2515
2516 break;
2517
2518 default:
2519 ;
2520 }
2521}
2522
87f0e418
LP
2523static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2524 Service *s = SERVICE(u);
f42806df 2525 ServiceResult f;
5cb5a6ff
LP
2526
2527 assert(s);
034c6ed7
LP
2528 assert(pid >= 0);
2529
96342de6
LN
2530 if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) :
2531 is_clean_exit_lsb(code, status, &s->success_status))
f42806df
LP
2532 f = SERVICE_SUCCESS;
2533 else if (code == CLD_EXITED)
2534 f = SERVICE_FAILURE_EXIT_CODE;
2535 else if (code == CLD_KILLED)
2536 f = SERVICE_FAILURE_SIGNAL;
2537 else if (code == CLD_DUMPED)
2538 f = SERVICE_FAILURE_CORE_DUMP;
d06dacd0 2539 else
cfc4eb4c 2540 assert_not_reached("Unknown code");
034c6ed7
LP
2541
2542 if (s->main_pid == pid) {
db01f8b3
MS
2543 /* Forking services may occasionally move to a new PID.
2544 * As long as they update the PID file before exiting the old
2545 * PID, they're fine. */
5375410b 2546 if (service_load_pid_file(s, false) == 0)
db01f8b3 2547 return;
034c6ed7 2548
034c6ed7 2549 s->main_pid = 0;
6ea832a2 2550 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
034c6ed7 2551
867b3b7d 2552 if (s->main_command) {
fbeefb45
LP
2553 /* If this is not a forking service than the
2554 * main process got started and hence we copy
2555 * the exit status so that it is recorded both
2556 * as main and as control process exit
2557 * status */
2558
867b3b7d 2559 s->main_command->exec_status = s->main_exec_status;
b708e7ce 2560
867b3b7d 2561 if (s->main_command->ignore)
f42806df 2562 f = SERVICE_SUCCESS;
fbeefb45
LP
2563 } else if (s->exec_command[SERVICE_EXEC_START]) {
2564
2565 /* If this is a forked process, then we should
2566 * ignore the return value if this was
2567 * configured for the starter process */
2568
2569 if (s->exec_command[SERVICE_EXEC_START]->ignore)
2570 f = SERVICE_SUCCESS;
034c6ed7
LP
2571 }
2572
f2341e0a
LP
2573 log_struct(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2574 LOG_UNIT_ID(u),
2575 LOG_UNIT_MESSAGE(u, "Main process exited, code=%s, status=%i/%s",
2576 sigchld_code_to_string(code), status,
e2cc6eca
LP
2577 strna(code == CLD_EXITED
2578 ? exit_status_to_string(status, EXIT_STATUS_FULL)
2579 : signal_to_string(status))),
f2341e0a
LP
2580 "EXIT_CODE=%s", sigchld_code_to_string(code),
2581 "EXIT_STATUS=%i", status,
2582 NULL);
f42806df
LP
2583
2584 if (f != SERVICE_SUCCESS)
2585 s->result = f;
034c6ed7 2586
867b3b7d
LP
2587 if (s->main_command &&
2588 s->main_command->command_next &&
f42806df 2589 f == SERVICE_SUCCESS) {
034c6ed7 2590
34e9ba66
LP
2591 /* There is another command to *
2592 * execute, so let's do that. */
034c6ed7 2593
f2341e0a 2594 log_unit_debug(u, "Running next main command for state %s.", service_state_to_string(s->state));
f42806df 2595 service_run_next_main(s);
034c6ed7 2596
34e9ba66
LP
2597 } else {
2598
2599 /* The service exited, so the service is officially
2600 * gone. */
867b3b7d 2601 s->main_command = NULL;
34e9ba66
LP
2602
2603 switch (s->state) {
2604
2605 case SERVICE_START_POST:
2606 case SERVICE_RELOAD:
2607 case SERVICE_STOP:
2608 /* Need to wait until the operation is
2609 * done */
c4653a4d 2610 break;
7d55e835 2611
34e9ba66
LP
2612 case SERVICE_START:
2613 if (s->type == SERVICE_ONESHOT) {
2614 /* This was our main goal, so let's go on */
f42806df 2615 if (f == SERVICE_SUCCESS)
34e9ba66
LP
2616 service_enter_start_post(s);
2617 else
f42806df 2618 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
34e9ba66 2619 break;
34e9ba66 2620 }
034c6ed7 2621
bfba3256
LP
2622 /* Fall through */
2623
34e9ba66 2624 case SERVICE_RUNNING:
f42806df 2625 service_enter_running(s, f);
34e9ba66 2626 break;
034c6ed7 2627
db2cb23b 2628 case SERVICE_STOP_SIGABRT:
34e9ba66
LP
2629 case SERVICE_STOP_SIGTERM:
2630 case SERVICE_STOP_SIGKILL:
5cb5a6ff 2631
34e9ba66 2632 if (!control_pid_good(s))
f42806df 2633 service_enter_stop_post(s, f);
5cb5a6ff 2634
34e9ba66
LP
2635 /* If there is still a control process, wait for that first */
2636 break;
2637
bf108e55
LP
2638 case SERVICE_STOP_POST:
2639 case SERVICE_FINAL_SIGTERM:
2640 case SERVICE_FINAL_SIGKILL:
2641
2642 if (!control_pid_good(s))
2643 service_enter_dead(s, f, true);
2644 break;
2645
34e9ba66
LP
2646 default:
2647 assert_not_reached("Uh, main process died at wrong time.");
2648 }
034c6ed7 2649 }
5cb5a6ff 2650
034c6ed7 2651 } else if (s->control_pid == pid) {
34e9ba66
LP
2652 s->control_pid = 0;
2653
b708e7ce 2654 if (s->control_command) {
8bb2d17d 2655 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
a16e1123 2656
b708e7ce 2657 if (s->control_command->ignore)
f42806df 2658 f = SERVICE_SUCCESS;
b708e7ce
LP
2659 }
2660
f2341e0a
LP
2661 log_unit_full(u, f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, 0,
2662 "Control process exited, code=%s status=%i",
2663 sigchld_code_to_string(code), status);
f42806df
LP
2664
2665 if (f != SERVICE_SUCCESS)
2666 s->result = f;
034c6ed7 2667
88f3e0c9
LP
2668 /* Immediately get rid of the cgroup, so that the
2669 * kernel doesn't delay the cgroup empty messages for
2670 * the service cgroup any longer than necessary */
4ad49000 2671 service_kill_control_processes(s);
88f3e0c9 2672
34e9ba66
LP
2673 if (s->control_command &&
2674 s->control_command->command_next &&
f42806df 2675 f == SERVICE_SUCCESS) {
034c6ed7
LP
2676
2677 /* There is another command to *
2678 * execute, so let's do that. */
2679
f2341e0a 2680 log_unit_debug(u, "Running next control command for state %s.", service_state_to_string(s->state));
f42806df 2681 service_run_next_control(s);
034c6ed7 2682
80876c20 2683 } else {
034c6ed7
LP
2684 /* No further commands for this step, so let's
2685 * figure out what to do next */
2686
a16e1123
LP
2687 s->control_command = NULL;
2688 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2689
f2341e0a 2690 log_unit_debug(u, "Got final SIGCHLD for state %s.", service_state_to_string(s->state));
bd982a8b 2691
034c6ed7
LP
2692 switch (s->state) {
2693
2694 case SERVICE_START_PRE:
f42806df 2695 if (f == SERVICE_SUCCESS)
034c6ed7
LP
2696 service_enter_start(s);
2697 else
f42806df 2698 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
034c6ed7
LP
2699 break;
2700
2701 case SERVICE_START:
bfba3256
LP
2702 if (s->type != SERVICE_FORKING)
2703 /* Maybe spurious event due to a reload that changed the type? */
2704 break;
034c6ed7 2705
f42806df
LP
2706 if (f != SERVICE_SUCCESS) {
2707 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
3a111838
MS
2708 break;
2709 }
034c6ed7 2710
3a111838 2711 if (s->pid_file) {
f42806df
LP
2712 bool has_start_post;
2713 int r;
2714
3a111838
MS
2715 /* Let's try to load the pid file here if we can.
2716 * The PID file might actually be created by a START_POST
2717 * script. In that case don't worry if the loading fails. */
f42806df
LP
2718
2719 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
2720 r = service_load_pid_file(s, !has_start_post);
3a111838
MS
2721 if (!has_start_post && r < 0) {
2722 r = service_demand_pid_file(s);
2723 if (r < 0 || !cgroup_good(s))
f42806df 2724 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
3a111838
MS
2725 break;
2726 }
034c6ed7 2727 } else
783e05d6 2728 service_search_main_pid(s);
034c6ed7 2729
3a111838 2730 service_enter_start_post(s);
034c6ed7
LP
2731 break;
2732
2733 case SERVICE_START_POST:
f42806df 2734 if (f != SERVICE_SUCCESS) {
ce359e98 2735 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
2096e009 2736 break;
034c6ed7
LP
2737 }
2738
2096e009 2739 if (s->pid_file) {
f42806df
LP
2740 int r;
2741
2742 r = service_load_pid_file(s, true);
2096e009
MS
2743 if (r < 0) {
2744 r = service_demand_pid_file(s);
2745 if (r < 0 || !cgroup_good(s))
f42806df 2746 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2096e009
MS
2747 break;
2748 }
2749 } else
783e05d6 2750 service_search_main_pid(s);
2096e009 2751
f42806df 2752 service_enter_running(s, SERVICE_SUCCESS);
3185a36b 2753 break;
034c6ed7
LP
2754
2755 case SERVICE_RELOAD:
7236ce6e
ZJS
2756 if (f == SERVICE_SUCCESS)
2757 if (service_load_pid_file(s, true) < 0)
2758 service_search_main_pid(s);
3185a36b 2759
f42806df
LP
2760 s->reload_result = f;
2761 service_enter_running(s, SERVICE_SUCCESS);
034c6ed7
LP
2762 break;
2763
2764 case SERVICE_STOP:
f42806df 2765 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
034c6ed7
LP
2766 break;
2767
db2cb23b 2768 case SERVICE_STOP_SIGABRT:
034c6ed7
LP
2769 case SERVICE_STOP_SIGTERM:
2770 case SERVICE_STOP_SIGKILL:
2771 if (main_pid_good(s) <= 0)
f42806df 2772 service_enter_stop_post(s, f);
034c6ed7
LP
2773
2774 /* If there is still a service
2775 * process around, wait until
2776 * that one quit, too */
2777 break;
2778
2779 case SERVICE_STOP_POST:
2780 case SERVICE_FINAL_SIGTERM:
2781 case SERVICE_FINAL_SIGKILL:
bf108e55
LP
2782 if (main_pid_good(s) <= 0)
2783 service_enter_dead(s, f, true);
034c6ed7
LP
2784 break;
2785
2786 default:
2787 assert_not_reached("Uh, control process died at wrong time.");
2788 }
2789 }
8c47c732 2790 }
c4e2ceae
LP
2791
2792 /* Notify clients about changed exit status */
2793 unit_add_to_dbus_queue(u);
a911bb9a
LP
2794
2795 /* We got one SIGCHLD for the service, let's watch all
2796 * processes that are now running of the service, and watch
2797 * that. Among the PIDs we then watch will be children
2798 * reassigned to us, which hopefully allows us to identify
2799 * when all children are gone */
2800 unit_tidy_watch_pids(u, s->main_pid, s->control_pid);
2801 unit_watch_all_pids(u);
2802
bbc85a16
EV
2803 /* If the PID set is empty now, then let's finish this off
2804 (On unified we use proper notifications) */
2805 if (cg_unified() <= 0 && set_isempty(u->pids))
a911bb9a 2806 service_notify_cgroup_empty_event(u);
034c6ed7
LP
2807}
2808
718db961
LP
2809static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
2810 Service *s = SERVICE(userdata);
034c6ed7
LP
2811
2812 assert(s);
718db961 2813 assert(source == s->timer_event_source);
034c6ed7
LP
2814
2815 switch (s->state) {
2816
2817 case SERVICE_START_PRE:
2818 case SERVICE_START:
f2341e0a 2819 log_unit_warning(UNIT(s), "%s operation timed out. Terminating.", s->state == SERVICE_START ? "Start" : "Start-pre");
f42806df 2820 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
80876c20
LP
2821 break;
2822
034c6ed7 2823 case SERVICE_START_POST:
f2341e0a 2824 log_unit_warning(UNIT(s), "Start-post operation timed out. Stopping.");
ce359e98 2825 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
034c6ed7
LP
2826 break;
2827
36c16a7c
LP
2828 case SERVICE_RUNNING:
2829 log_unit_warning(UNIT(s), "Service reached runtime time limit. Stopping.");
2830 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
2831 break;
2832
e2f3b44c 2833 case SERVICE_RELOAD:
089b64d5 2834 log_unit_warning(UNIT(s), "Reload operation timed out. Killing reload process.");
478b6192 2835 service_kill_control_processes(s);
f42806df
LP
2836 s->reload_result = SERVICE_FAILURE_TIMEOUT;
2837 service_enter_running(s, SERVICE_SUCCESS);
e2f3b44c
LP
2838 break;
2839
034c6ed7 2840 case SERVICE_STOP:
f2341e0a 2841 log_unit_warning(UNIT(s), "Stopping timed out. Terminating.");
f42806df 2842 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
034c6ed7
LP
2843 break;
2844
db2cb23b 2845 case SERVICE_STOP_SIGABRT:
f2341e0a 2846 log_unit_warning(UNIT(s), "State 'stop-sigabrt' timed out. Terminating.");
2ab2ab7b 2847 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
db2cb23b
UTL
2848 break;
2849
034c6ed7 2850 case SERVICE_STOP_SIGTERM:
4819ff03 2851 if (s->kill_context.send_sigkill) {
f2341e0a 2852 log_unit_warning(UNIT(s), "State 'stop-sigterm' timed out. Killing.");
f42806df 2853 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
ba035df2 2854 } else {
f2341e0a 2855 log_unit_warning(UNIT(s), "State 'stop-sigterm' timed out. Skipping SIGKILL.");
f42806df 2856 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
ba035df2
LP
2857 }
2858
034c6ed7
LP
2859 break;
2860
2861 case SERVICE_STOP_SIGKILL:
35b8ca3a 2862 /* Uh, we sent a SIGKILL and it is still not gone?
034c6ed7
LP
2863 * Must be something we cannot kill, so let's just be
2864 * weirded out and continue */
2865
f2341e0a 2866 log_unit_warning(UNIT(s), "Processes still around after SIGKILL. Ignoring.");
f42806df 2867 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
034c6ed7
LP
2868 break;
2869
2870 case SERVICE_STOP_POST:
f2341e0a 2871 log_unit_warning(UNIT(s), "State 'stop-post' timed out. Terminating.");
f42806df 2872 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
034c6ed7
LP
2873 break;
2874
2875 case SERVICE_FINAL_SIGTERM:
4819ff03 2876 if (s->kill_context.send_sigkill) {
f2341e0a 2877 log_unit_warning(UNIT(s), "State 'stop-final-sigterm' timed out. Killing.");
f42806df 2878 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
ba035df2 2879 } else {
f2341e0a 2880 log_unit_warning(UNIT(s), "State 'stop-final-sigterm' timed out. Skipping SIGKILL. Entering failed mode.");
f42806df 2881 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
ba035df2
LP
2882 }
2883
034c6ed7
LP
2884 break;
2885
2886 case SERVICE_FINAL_SIGKILL:
f2341e0a 2887 log_unit_warning(UNIT(s), "Processes still around after final SIGKILL. Entering failed mode.");
f42806df 2888 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
034c6ed7
LP
2889 break;
2890
2891 case SERVICE_AUTO_RESTART:
f2341e0a 2892 log_unit_info(UNIT(s),
ef417cfd 2893 s->restart_usec > 0 ?
f2341e0a
LP
2894 "Service hold-off time over, scheduling restart." :
2895 "Service has no hold-off time, scheduling restart.");
034c6ed7
LP
2896 service_enter_restart(s);
2897 break;
2898
2899 default:
2900 assert_not_reached("Timeout at wrong time.");
2901 }
718db961
LP
2902
2903 return 0;
2904}
2905
2906static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata) {
2907 Service *s = SERVICE(userdata);
a7850c7d 2908 char t[FORMAT_TIMESPAN_MAX];
718db961
LP
2909
2910 assert(s);
2911 assert(source == s->watchdog_event_source);
2912
f2341e0a 2913 log_unit_error(UNIT(s), "Watchdog timeout (limit %s)!",
a7850c7d 2914 format_timespan(t, sizeof(t), s->watchdog_usec, 1));
8bb2d17d 2915
db2cb23b 2916 service_enter_signal(s, SERVICE_STOP_SIGABRT, SERVICE_FAILURE_WATCHDOG);
842129f5 2917
718db961 2918 return 0;
5cb5a6ff
LP
2919}
2920
a354329f 2921static void service_notify_message(Unit *u, pid_t pid, char **tags, FDSet *fds) {
8c47c732 2922 Service *s = SERVICE(u);
308d72dc 2923 _cleanup_free_ char *cc = NULL;
30b5275a 2924 bool notify_dbus = false;
308d72dc 2925 const char *e;
8c47c732
LP
2926
2927 assert(u);
2928
308d72dc 2929 cc = strv_join(tags, ", ");
da13d4d2 2930
c952c6ec 2931 if (s->notify_access == NOTIFY_NONE) {
f2341e0a 2932 log_unit_warning(u, "Got notification message from PID "PID_FMT", but reception is disabled.", pid);
c952c6ec 2933 return;
34959677 2934 } else if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
336c6e46 2935 if (s->main_pid != 0)
f2341e0a 2936 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 2937 else
f2341e0a 2938 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 2939 return;
34959677
TG
2940 } else
2941 log_unit_debug(u, "Got notification message from PID "PID_FMT" (%s)", pid, isempty(cc) ? "n/a" : cc);
c952c6ec 2942
8c47c732 2943 /* Interpret MAINPID= */
28849dba 2944 e = strv_find_startswith(tags, "MAINPID=");
5e56b378 2945 if (e && IN_SET(s->state, SERVICE_START, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) {
28849dba 2946 if (parse_pid(e, &pid) < 0)
f2341e0a 2947 log_unit_warning(u, "Failed to parse MAINPID= field in notification message: %s", e);
8c47c732 2948 else {
5925dd3c 2949 service_set_main_pid(s, pid);
7400b9d2 2950 unit_watch_pid(UNIT(s), pid);
30b5275a 2951 notify_dbus = true;
8c47c732
LP
2952 }
2953 }
2954
308d72dc
LP
2955 /* Interpret RELOADING= */
2956 if (strv_find(tags, "RELOADING=1")) {
2957
308d72dc
LP
2958 s->notify_state = NOTIFY_RELOADING;
2959
2960 if (s->state == SERVICE_RUNNING)
2961 service_enter_reload_by_notify(s);
2962
2963 notify_dbus = true;
2964 }
2965
8c47c732 2966 /* Interpret READY= */
308d72dc
LP
2967 if (strv_find(tags, "READY=1")) {
2968
308d72dc
LP
2969 s->notify_state = NOTIFY_READY;
2970
2971 /* Type=notify services inform us about completed
2972 * initialization with READY=1 */
2973 if (s->type == SERVICE_NOTIFY && s->state == SERVICE_START)
2974 service_enter_start_post(s);
2975
2976 /* Sending READY=1 while we are reloading informs us
2977 * that the reloading is complete */
2978 if (s->state == SERVICE_RELOAD && s->control_pid == 0)
2979 service_enter_running(s, SERVICE_SUCCESS);
2980
2981 notify_dbus = true;
2982 }
2983
2984 /* Interpret STOPPING= */
2985 if (strv_find(tags, "STOPPING=1")) {
2986
308d72dc
LP
2987 s->notify_state = NOTIFY_STOPPING;
2988
2989 if (s->state == SERVICE_RUNNING)
2990 service_enter_stop_by_notify(s);
2991
30b5275a 2992 notify_dbus = true;
8c47c732
LP
2993 }
2994
2995 /* Interpret STATUS= */
28849dba 2996 e = strv_find_startswith(tags, "STATUS=");
7f110ff9 2997 if (e) {
28849dba 2998 _cleanup_free_ char *t = NULL;
8c47c732 2999
28849dba
LP
3000 if (!isempty(e)) {
3001 if (!utf8_is_valid(e))
f2341e0a 3002 log_unit_warning(u, "Status message in notification message is not UTF-8 clean.");
28849dba 3003 else {
28849dba
LP
3004 t = strdup(e);
3005 if (!t)
3006 log_oom();
3a2776bc 3007 }
28849dba 3008 }
8c47c732 3009
30b5275a 3010 if (!streq_ptr(s->status_text, t)) {
28849dba 3011
3a2776bc
LP
3012 free(s->status_text);
3013 s->status_text = t;
28849dba
LP
3014 t = NULL;
3015
30b5275a 3016 notify_dbus = true;
28849dba 3017 }
8c47c732 3018 }
842129f5 3019
4774e357 3020 /* Interpret ERRNO= */
28849dba 3021 e = strv_find_startswith(tags, "ERRNO=");
4774e357
MAA
3022 if (e) {
3023 int status_errno;
3024
28849dba 3025 if (safe_atoi(e, &status_errno) < 0 || status_errno < 0)
f2341e0a 3026 log_unit_warning(u, "Failed to parse ERRNO= field in notification message: %s", e);
4774e357 3027 else {
4774e357
MAA
3028 if (s->status_errno != status_errno) {
3029 s->status_errno = status_errno;
3030 notify_dbus = true;
3031 }
3032 }
3033 }
3034
6f285378 3035 /* Interpret WATCHDOG= */
1f6b4113 3036 if (strv_find(tags, "WATCHDOG=1"))
842129f5 3037 service_reset_watchdog(s);
c4e2ceae 3038
8dd4c05b
LP
3039 if (strv_find(tags, "FDSTORE=1")) {
3040 const char *name;
3041
3042 name = strv_find_startswith(tags, "FDNAME=");
3043 if (name && !fdname_is_valid(name)) {
3044 log_unit_warning(u, "Passed FDNAME= name is invalid, ignoring.");
3045 name = NULL;
3046 }
3047
3048 service_add_fd_store_set(s, fds, name);
3049 }
a354329f 3050
c4e2ceae 3051 /* Notify clients about changed status or main pid */
30b5275a
LP
3052 if (notify_dbus)
3053 unit_add_to_dbus_queue(u);
8c47c732
LP
3054}
3055
7a7821c8 3056static int service_get_timeout(Unit *u, usec_t *timeout) {
68db7a3b 3057 Service *s = SERVICE(u);
7a7821c8 3058 uint64_t t;
68db7a3b
ZJS
3059 int r;
3060
3061 if (!s->timer_event_source)
3062 return 0;
3063
7a7821c8 3064 r = sd_event_source_get_time(s->timer_event_source, &t);
68db7a3b
ZJS
3065 if (r < 0)
3066 return r;
7a7821c8
LP
3067 if (t == USEC_INFINITY)
3068 return 0;
68db7a3b 3069
7a7821c8 3070 *timeout = t;
68db7a3b
ZJS
3071 return 1;
3072}
3073
05e343b7
LP
3074static void service_bus_name_owner_change(
3075 Unit *u,
3076 const char *name,
3077 const char *old_owner,
3078 const char *new_owner) {
3079
3080 Service *s = SERVICE(u);
718db961 3081 int r;
05e343b7
LP
3082
3083 assert(s);
3084 assert(name);
3085
3086 assert(streq(s->bus_name, name));
3087 assert(old_owner || new_owner);
3088
3089 if (old_owner && new_owner)
f2341e0a 3090 log_unit_debug(u, "D-Bus name %s changed owner from %s to %s", name, old_owner, new_owner);
05e343b7 3091 else if (old_owner)
f2341e0a 3092 log_unit_debug(u, "D-Bus name %s no longer registered by %s", name, old_owner);
05e343b7 3093 else
f2341e0a 3094 log_unit_debug(u, "D-Bus name %s now registered by %s", name, new_owner);
05e343b7
LP
3095
3096 s->bus_name_good = !!new_owner;
3097
d8ccf5fd
DM
3098 /* Track the current owner, so we can reconstruct changes after a daemon reload */
3099 r = free_and_strdup(&s->bus_name_owner, new_owner);
3100 if (r < 0) {
3101 log_unit_error_errno(u, r, "Unable to set new bus name owner %s: %m", new_owner);
3102 return;
3103 }
3104
05e343b7
LP
3105 if (s->type == SERVICE_DBUS) {
3106
3107 /* service_enter_running() will figure out what to
3108 * do */
3109 if (s->state == SERVICE_RUNNING)
f42806df 3110 service_enter_running(s, SERVICE_SUCCESS);
05e343b7
LP
3111 else if (s->state == SERVICE_START && new_owner)
3112 service_enter_start_post(s);
3113
3114 } else if (new_owner &&
3115 s->main_pid <= 0 &&
3116 (s->state == SERVICE_START ||
3117 s->state == SERVICE_START_POST ||
3118 s->state == SERVICE_RUNNING ||
3119 s->state == SERVICE_RELOAD)) {
3120
4afd3348 3121 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
718db961 3122 pid_t pid;
05e343b7 3123
718db961 3124 /* Try to acquire PID from bus service */
05e343b7 3125
056f95d0 3126 r = sd_bus_get_name_creds(u->manager->api_bus, name, SD_BUS_CREDS_PID, &creds);
5b12334d
LP
3127 if (r >= 0)
3128 r = sd_bus_creds_get_pid(creds, &pid);
718db961 3129 if (r >= 0) {
f2341e0a 3130 log_unit_debug(u, "D-Bus name %s is now owned by process %u", name, (unsigned) pid);
05e343b7 3131
718db961
LP
3132 service_set_main_pid(s, pid);
3133 unit_watch_pid(UNIT(s), pid);
3134 }
7400b9d2 3135 }
05e343b7
LP
3136}
3137
16115b0a 3138int service_set_socket_fd(Service *s, int fd, Socket *sock, bool selinux_context_net) {
79a98c60
LP
3139 _cleanup_free_ char *peer = NULL;
3140 int r;
57020a3a 3141
4f2d528d
LP
3142 assert(s);
3143 assert(fd >= 0);
3144
7f2fbbff
LP
3145 /* This is called by the socket code when instantiating a new service for a stream socket and the socket needs
3146 * to be configured. We take ownership of the passed fd on success. */
4f2d528d 3147
1124fe6f 3148 if (UNIT(s)->load_state != UNIT_LOADED)
4f2d528d
LP
3149 return -EINVAL;
3150
3151 if (s->socket_fd >= 0)
3152 return -EBUSY;
3153
3154 if (s->state != SERVICE_DEAD)
3155 return -EAGAIN;
3156
366b7db4 3157 if (getpeername_pretty(fd, true, &peer) >= 0) {
79a98c60
LP
3158
3159 if (UNIT(s)->description) {
3160 _cleanup_free_ char *a;
3161
3162 a = strjoin(UNIT(s)->description, " (", peer, ")", NULL);
3163 if (!a)
3164 return -ENOMEM;
3165
3166 r = unit_set_description(UNIT(s), a);
3167 } else
3168 r = unit_set_description(UNIT(s), peer);
3169
3170 if (r < 0)
3171 return r;
3172 }
3173
7f2fbbff
LP
3174 r = unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
3175 if (r < 0)
3176 return r;
3177
4f2d528d 3178 s->socket_fd = fd;
16115b0a 3179 s->socket_fd_selinux_context_net = selinux_context_net;
6cf6bbc2 3180
57020a3a 3181 unit_ref_set(&s->accept_socket, UNIT(sock));
7f2fbbff 3182 return 0;
4f2d528d
LP
3183}
3184
fdf20a31 3185static void service_reset_failed(Unit *u) {
5632e374
LP
3186 Service *s = SERVICE(u);
3187
3188 assert(s);
3189
fdf20a31 3190 if (s->state == SERVICE_FAILED)
5632e374
LP
3191 service_set_state(s, SERVICE_DEAD);
3192
f42806df
LP
3193 s->result = SERVICE_SUCCESS;
3194 s->reload_result = SERVICE_SUCCESS;
5632e374
LP
3195}
3196
718db961 3197static int service_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
8a0867d6 3198 Service *s = SERVICE(u);
41efeaec 3199
814cc562 3200 return unit_kill_common(u, who, signo, s->main_pid, s->control_pid, error);
8a0867d6
LP
3201}
3202
291d565a
LP
3203static int service_main_pid(Unit *u) {
3204 Service *s = SERVICE(u);
3205
3206 assert(s);
3207
3208 return s->main_pid;
3209}
3210
3211static int service_control_pid(Unit *u) {
3212 Service *s = SERVICE(u);
3213
3214 assert(s);
3215
3216 return s->control_pid;
3217}
3218
94f04347 3219static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
525ee6f4
LP
3220 [SERVICE_RESTART_NO] = "no",
3221 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
50caaedb 3222 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
6cfe2fde 3223 [SERVICE_RESTART_ON_ABNORMAL] = "on-abnormal",
dc99a976 3224 [SERVICE_RESTART_ON_WATCHDOG] = "on-watchdog",
50caaedb 3225 [SERVICE_RESTART_ON_ABORT] = "on-abort",
6cfe2fde 3226 [SERVICE_RESTART_ALWAYS] = "always",
94f04347
LP
3227};
3228
3229DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3230
3231static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
94f04347 3232 [SERVICE_SIMPLE] = "simple",
0d624a78 3233 [SERVICE_FORKING] = "forking",
34e9ba66 3234 [SERVICE_ONESHOT] = "oneshot",
8c47c732 3235 [SERVICE_DBUS] = "dbus",
f2b68789
LP
3236 [SERVICE_NOTIFY] = "notify",
3237 [SERVICE_IDLE] = "idle"
94f04347
LP
3238};
3239
3240DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3241
e537352b 3242static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
94f04347
LP
3243 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3244 [SERVICE_EXEC_START] = "ExecStart",
3245 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3246 [SERVICE_EXEC_RELOAD] = "ExecReload",
3247 [SERVICE_EXEC_STOP] = "ExecStop",
3248 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3249};
3250
3251DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3252
c952c6ec
LP
3253static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3254 [NOTIFY_NONE] = "none",
3255 [NOTIFY_MAIN] = "main",
3256 [NOTIFY_ALL] = "all"
3257};
3258
3259DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3260
308d72dc
LP
3261static const char* const notify_state_table[_NOTIFY_STATE_MAX] = {
3262 [NOTIFY_UNKNOWN] = "unknown",
3263 [NOTIFY_READY] = "ready",
3264 [NOTIFY_RELOADING] = "reloading",
3265 [NOTIFY_STOPPING] = "stopping",
3266};
3267
3268DEFINE_STRING_TABLE_LOOKUP(notify_state, NotifyState);
3269
f42806df
LP
3270static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3271 [SERVICE_SUCCESS] = "success",
3272 [SERVICE_FAILURE_RESOURCES] = "resources",
3273 [SERVICE_FAILURE_TIMEOUT] = "timeout",
3274 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3275 [SERVICE_FAILURE_SIGNAL] = "signal",
bb242b7b 3276 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
8d1b002a 3277 [SERVICE_FAILURE_WATCHDOG] = "watchdog",
07299350 3278 [SERVICE_FAILURE_START_LIMIT_HIT] = "start-limit-hit",
f42806df
LP
3279};
3280
3281DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3282
87f0e418 3283const UnitVTable service_vtable = {
7d17cfbc 3284 .object_size = sizeof(Service),
718db961
LP
3285 .exec_context_offset = offsetof(Service, exec_context),
3286 .cgroup_context_offset = offsetof(Service, cgroup_context),
3287 .kill_context_offset = offsetof(Service, kill_context),
613b411c 3288 .exec_runtime_offset = offsetof(Service, exec_runtime),
3ef63c31 3289
f975e971
LP
3290 .sections =
3291 "Unit\0"
3292 "Service\0"
3293 "Install\0",
4ad49000 3294 .private_section = "Service",
71645aca 3295
034c6ed7
LP
3296 .init = service_init,
3297 .done = service_done,
a16e1123 3298 .load = service_load,
a354329f 3299 .release_resources = service_release_resources,
a16e1123
LP
3300
3301 .coldplug = service_coldplug,
034c6ed7 3302
5cb5a6ff
LP
3303 .dump = service_dump,
3304
3305 .start = service_start,
3306 .stop = service_stop,
3307 .reload = service_reload,
3308
034c6ed7
LP
3309 .can_reload = service_can_reload,
3310
8a0867d6
LP
3311 .kill = service_kill,
3312
a16e1123
LP
3313 .serialize = service_serialize,
3314 .deserialize_item = service_deserialize_item,
3315
5cb5a6ff 3316 .active_state = service_active_state,
10a94420 3317 .sub_state_to_string = service_sub_state_to_string,
5cb5a6ff 3318
701cc384 3319 .check_gc = service_check_gc,
701cc384 3320
034c6ed7 3321 .sigchld_event = service_sigchld_event,
2c4104f0 3322
fdf20a31 3323 .reset_failed = service_reset_failed,
5632e374 3324
4ad49000 3325 .notify_cgroup_empty = service_notify_cgroup_empty_event,
8c47c732 3326 .notify_message = service_notify_message,
8e274523 3327
291d565a
LP
3328 .main_pid = service_main_pid,
3329 .control_pid = service_control_pid,
3330
05e343b7 3331 .bus_name_owner_change = service_bus_name_owner_change,
05e343b7 3332
718db961 3333 .bus_vtable = bus_service_vtable,
74c964d3
LP
3334 .bus_set_property = bus_service_set_property,
3335 .bus_commit_properties = bus_service_commit_properties,
4139c1b2 3336
68db7a3b 3337 .get_timeout = service_get_timeout,
718db961
LP
3338 .can_transient = true,
3339
c6918296
MS
3340 .status_message_formats = {
3341 .starting_stopping = {
3342 [0] = "Starting %s...",
3343 [1] = "Stopping %s...",
3344 },
3345 .finished_start_job = {
3346 [JOB_DONE] = "Started %s.",
3347 [JOB_FAILED] = "Failed to start %s.",
c6918296
MS
3348 },
3349 .finished_stop_job = {
3350 [JOB_DONE] = "Stopped %s.",
3351 [JOB_FAILED] = "Stopped (with error) %s.",
c6918296
MS
3352 },
3353 },
5cb5a6ff 3354};