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