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