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