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