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