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