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