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