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