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