LCOV - code coverage report
Current view: top level - core - service.c (source / functions) Hit Total Coverage
Test: systemd test coverage Lines: 348 1551 22.4 %
Date: 2015-07-29 18:47:03 Functions: 42 88 47.7 %

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

Generated by: LCOV version 1.11