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