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