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