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