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