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