| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include "cgroup.h" |
| 5 |
#include "core-forward.h" |
| 6 |
#include "exit-status.h" |
| 7 |
#include "kill.h" |
| 8 |
#include "pidref.h" |
| 9 |
#include "unit.h" |
| 10 |
|
| 11 |
/* FDNAME used to push the JSON mapping memfd that pairs upstream-propagated fdstore indices with |
| 12 |
* (unit-id, original fdname) tuples. The receiving manager looks for this fdname in LISTEN_FDNAMES |
| 13 |
* to find the mapping document. */ |
| 14 |
#define SERVICE_FDSTORE_MAPPING_FDNAME "systemd-fdstore-mapping" |
| 15 |
|
| 16 |
/* Prefix for the upstream FDNAME used when forwarding individual fd-store entries to a parent |
| 17 |
* supervisor: the entries are exposed as "sub-fdstore-<index>" so the supervisor's own fd-store |
| 18 |
* namespace doesn't collide with names a downstream service manager assigns. The trailing index |
| 19 |
* is matched up with an entry in the SERVICE_FDSTORE_MAPPING_FDNAME memfd to recover the original |
| 20 |
* (unit, fdname) pair. */ |
| 21 |
#define SERVICE_FDSTORE_SUB_FDNAME_PREFIX "sub-fdstore-" |
| 22 |
|
| 23 |
typedef enum ServiceRestart { |
| 24 |
SERVICE_RESTART_NO, |
| 25 |
SERVICE_RESTART_ON_SUCCESS, |
| 26 |
SERVICE_RESTART_ON_FAILURE, |
| 27 |
SERVICE_RESTART_ON_ABNORMAL, |
| 28 |
SERVICE_RESTART_ON_WATCHDOG, |
| 29 |
SERVICE_RESTART_ON_ABORT, |
| 30 |
SERVICE_RESTART_ALWAYS, |
| 31 |
_SERVICE_RESTART_MAX, |
| 32 |
_SERVICE_RESTART_INVALID = -EINVAL, |
| 33 |
} ServiceRestart; |
| 34 |
|
| 35 |
typedef enum ServiceType { |
| 36 |
SERVICE_SIMPLE, /* we fork and go on right-away (i.e. modern socket activated daemons) */ |
| 37 |
SERVICE_FORKING, /* forks by itself (i.e. traditional daemons) */ |
| 38 |
SERVICE_ONESHOT, /* we fork and wait until the program finishes (i.e. programs like fsck which run and need to finish before we continue) */ |
| 39 |
SERVICE_DBUS, /* we fork and wait until a specific D-Bus name appears on the bus */ |
| 40 |
SERVICE_NOTIFY, /* we fork and wait until a daemon sends us a ready message with sd_notify() */ |
| 41 |
SERVICE_NOTIFY_RELOAD, /* just like SERVICE_NOTIFY, but also implements a reload protocol via SIGHUP */ |
| 42 |
SERVICE_IDLE, /* much like simple, but delay exec() until all jobs are dispatched. */ |
| 43 |
SERVICE_EXEC, /* we fork and wait until we execute exec() (this means our own setup is waited for) */ |
| 44 |
_SERVICE_TYPE_MAX, |
| 45 |
_SERVICE_TYPE_INVALID = -EINVAL, |
| 46 |
} ServiceType; |
| 47 |
|
| 48 |
typedef enum ServiceExitType { |
| 49 |
SERVICE_EXIT_MAIN, /* we consider the main PID when deciding if the service exited */ |
| 50 |
SERVICE_EXIT_CGROUP, /* we wait for the last process in the cgroup to exit */ |
| 51 |
_SERVICE_EXIT_TYPE_MAX, |
| 52 |
_SERVICE_EXIT_TYPE_INVALID = -EINVAL, |
| 53 |
} ServiceExitType; |
| 54 |
|
| 55 |
typedef enum ServiceExecCommand { |
| 56 |
SERVICE_EXEC_CONDITION, |
| 57 |
SERVICE_EXEC_START_PRE, |
| 58 |
SERVICE_EXEC_START, |
| 59 |
SERVICE_EXEC_START_POST, |
| 60 |
SERVICE_EXEC_RELOAD, |
| 61 |
SERVICE_EXEC_RELOAD_POST, |
| 62 |
SERVICE_EXEC_STOP, |
| 63 |
SERVICE_EXEC_STOP_POST, |
| 64 |
_SERVICE_EXEC_COMMAND_MAX, |
| 65 |
_SERVICE_EXEC_COMMAND_INVALID = -EINVAL, |
| 66 |
} ServiceExecCommand; |
| 67 |
|
| 68 |
typedef enum NotifyState { |
| 69 |
NOTIFY_READY, |
| 70 |
NOTIFY_RELOADING, |
| 71 |
NOTIFY_RELOAD_READY, |
| 72 |
NOTIFY_STOPPING, |
| 73 |
_NOTIFY_STATE_MAX, |
| 74 |
_NOTIFY_STATE_INVALID = -EINVAL, |
| 75 |
} NotifyState; |
| 76 |
|
| 77 |
/* The values of this enum are referenced in man/systemd.exec.xml and src/shared/bus-unit-util.c. |
| 78 |
* Update those sources for each change to this enum. */ |
| 79 |
typedef enum ServiceResult { |
| 80 |
SERVICE_SUCCESS, |
| 81 |
SERVICE_FAILURE_RESOURCES, /* a bit of a misnomer, just our catch-all error for errnos we didn't expect */ |
| 82 |
SERVICE_FAILURE_PROTOCOL, |
| 83 |
SERVICE_FAILURE_TIMEOUT, |
| 84 |
SERVICE_FAILURE_EXIT_CODE, |
| 85 |
SERVICE_FAILURE_SIGNAL, |
| 86 |
SERVICE_FAILURE_CORE_DUMP, |
| 87 |
SERVICE_FAILURE_WATCHDOG, |
| 88 |
SERVICE_FAILURE_START_LIMIT_HIT, |
| 89 |
SERVICE_FAILURE_OOM_KILL, /* OOM Kill by the Kernel or systemd-oomd */ |
| 90 |
SERVICE_SKIP_CONDITION, |
| 91 |
_SERVICE_RESULT_MAX, |
| 92 |
_SERVICE_RESULT_INVALID = -EINVAL, |
| 93 |
} ServiceResult; |
| 94 |
|
| 95 |
typedef enum ServiceTimeoutFailureMode { |
| 96 |
SERVICE_TIMEOUT_TERMINATE, |
| 97 |
SERVICE_TIMEOUT_ABORT, |
| 98 |
SERVICE_TIMEOUT_KILL, |
| 99 |
_SERVICE_TIMEOUT_FAILURE_MODE_MAX, |
| 100 |
_SERVICE_TIMEOUT_FAILURE_MODE_INVALID = -EINVAL, |
| 101 |
} ServiceTimeoutFailureMode; |
| 102 |
|
| 103 |
typedef enum ServiceRestartMode { |
| 104 |
SERVICE_RESTART_MODE_NORMAL, |
| 105 |
SERVICE_RESTART_MODE_DIRECT, |
| 106 |
SERVICE_RESTART_MODE_DEBUG, |
| 107 |
_SERVICE_RESTART_MODE_MAX, |
| 108 |
_SERVICE_RESTART_MODE_INVALID = -EINVAL, |
| 109 |
} ServiceRestartMode; |
| 110 |
|
| 111 |
typedef enum ServiceRefreshOnReload { |
| 112 |
SERVICE_RELOAD_EXTENSIONS = 1 << 0, |
| 113 |
SERVICE_RELOAD_CREDENTIALS = 1 << 1, |
| 114 |
_SERVICE_REFRESH_ON_RELOAD_ALL = (1 << 2) - 1, |
| 115 |
_SERVICE_REFRESH_ON_RELOAD_INVALID = -EINVAL, |
| 116 |
} ServiceRefreshOnReload; |
| 117 |
|
| 118 |
#define SERVICE_REFRESH_ON_RELOAD_DEFAULT SERVICE_RELOAD_EXTENSIONS |
| 119 |
|
| 120 |
typedef struct ServiceFDStore { |
| 121 |
Service *service; |
| 122 |
|
| 123 |
int fd; |
| 124 |
char *fdname; |
| 125 |
sd_event_source *event_source; |
| 126 |
bool do_poll; |
| 127 |
/* If non-zero, this fd was forwarded to the NOTIFY_SOCKET supervisor via FDSTORE=1, with the |
| 128 |
* stringified value of this index as its FDNAME. The originating unit-id and original fdname |
| 129 |
* are recorded in a JSON mapping memfd that is also pushed upstream. */ |
| 130 |
uint64_t index; |
| 131 |
|
| 132 |
LIST_FIELDS(struct ServiceFDStore, fd_store); |
| 133 |
} ServiceFDStore; |
| 134 |
|
| 135 |
typedef struct ServiceExtraFD { |
| 136 |
int fd; |
| 137 |
char *fdname; |
| 138 |
} ServiceExtraFD; |
| 139 |
|
| 140 |
typedef struct Service { |
| 141 |
Unit meta; |
| 142 |
|
| 143 |
ServiceType type; |
| 144 |
ServiceExitType exit_type; |
| 145 |
ServiceRestart restart; |
| 146 |
ServiceRestartMode restart_mode; |
| 147 |
ExitStatusSet restart_prevent_status; |
| 148 |
ExitStatusSet restart_force_status; |
| 149 |
ExitStatusSet success_status; |
| 150 |
|
| 151 |
/* If set we'll read the main daemon PID from this file */ |
| 152 |
char *pid_file; |
| 153 |
|
| 154 |
unsigned n_restarts; |
| 155 |
unsigned restart_steps; |
| 156 |
usec_t restart_usec; |
| 157 |
usec_t restart_max_delay_usec; |
| 158 |
usec_t timeout_start_usec; |
| 159 |
usec_t timeout_stop_usec; |
| 160 |
usec_t timeout_abort_usec; |
| 161 |
bool timeout_abort_set; |
| 162 |
usec_t runtime_max_usec; |
| 163 |
usec_t runtime_rand_extra_usec; |
| 164 |
ServiceTimeoutFailureMode timeout_start_failure_mode; |
| 165 |
ServiceTimeoutFailureMode timeout_stop_failure_mode; |
| 166 |
|
| 167 |
dual_timestamp watchdog_timestamp; |
| 168 |
usec_t watchdog_usec; /* the requested watchdog timeout in the unit file */ |
| 169 |
usec_t watchdog_original_usec; /* the watchdog timeout that was in effect when the unit was started, i.e. the timeout the forked off processes currently see */ |
| 170 |
usec_t watchdog_override_usec; /* the watchdog timeout requested by the service itself through sd_notify() */ |
| 171 |
bool watchdog_override_enable; |
| 172 |
sd_event_source *watchdog_event_source; |
| 173 |
|
| 174 |
ExecContext exec_context; |
| 175 |
KillContext kill_context; |
| 176 |
CGroupContext cgroup_context; |
| 177 |
|
| 178 |
ServiceState state, deserialized_state; |
| 179 |
|
| 180 |
/* The exit status of the real main process */ |
| 181 |
ExecStatus main_exec_status; |
| 182 |
|
| 183 |
ExecCommand *exec_command[_SERVICE_EXEC_COMMAND_MAX]; |
| 184 |
|
| 185 |
/* The currently executed main process, which may be NULL if the main process got started via |
| 186 |
* forking mode and not by us */ |
| 187 |
ExecCommand *main_command; |
| 188 |
|
| 189 |
/* The currently executed control process */ |
| 190 |
ExecCommand *control_command; |
| 191 |
|
| 192 |
/* The ID of the control command currently being executed */ |
| 193 |
ServiceExecCommand control_command_id; |
| 194 |
|
| 195 |
/* Runtime data of the execution context */ |
| 196 |
ExecRuntime *exec_runtime; |
| 197 |
|
| 198 |
CGroupRuntime *cgroup_runtime; |
| 199 |
|
| 200 |
PidRef main_pid, control_pid; |
| 201 |
|
| 202 |
/* if we are a socket activated service instance, store information of the connection/peer/socket */ |
| 203 |
int socket_fd; |
| 204 |
SocketPeer *socket_peer; |
| 205 |
UnitRef accept_socket; |
| 206 |
bool socket_fd_selinux_context_net; |
| 207 |
|
| 208 |
bool permissions_start_only; |
| 209 |
bool root_directory_start_only; |
| 210 |
bool remain_after_exit; |
| 211 |
bool guess_main_pid; |
| 212 |
|
| 213 |
/* If we shut down, remember why */ |
| 214 |
ServiceResult result; |
| 215 |
ServiceResult reload_result; |
| 216 |
ServiceResult live_mount_result; |
| 217 |
ServiceResult clean_result; |
| 218 |
|
| 219 |
bool main_pid_known:1; |
| 220 |
bool main_pid_alien:1; |
| 221 |
bool bus_name_good:1; |
| 222 |
bool forbid_restart:1; |
| 223 |
bool start_timeout_defined:1; |
| 224 |
bool exec_fd_hot:1; |
| 225 |
|
| 226 |
char *bus_name; |
| 227 |
|
| 228 |
char *status_text; |
| 229 |
char *status_bus_error; |
| 230 |
char *status_varlink_error; |
| 231 |
int status_errno; |
| 232 |
|
| 233 |
sd_event_source *timer_event_source; |
| 234 |
PathSpec *pid_file_pathspec; |
| 235 |
|
| 236 |
NotifyAccess notify_access; |
| 237 |
NotifyAccess notify_access_override; |
| 238 |
NotifyState notify_state; |
| 239 |
|
| 240 |
sd_bus_slot *bus_name_pid_lookup_slot; |
| 241 |
|
| 242 |
sd_event_source *exec_fd_event_source; |
| 243 |
|
| 244 |
LIST_HEAD(ServiceFDStore, fd_store); |
| 245 |
size_t n_fd_store; |
| 246 |
unsigned n_fd_store_max; |
| 247 |
ExecPreserveMode fd_store_preserve_mode; |
| 248 |
|
| 249 |
int stdin_fd; |
| 250 |
int stdout_fd; |
| 251 |
int stderr_fd; |
| 252 |
|
| 253 |
/* File descriptor received from RootDirectoryFileDescriptor= */ |
| 254 |
int root_directory_fd; |
| 255 |
|
| 256 |
/* If service spawned from transient unit, extra file descriptors can be passed via dbus API */ |
| 257 |
ServiceExtraFD *extra_fds; |
| 258 |
size_t n_extra_fds; |
| 259 |
|
| 260 |
LIST_HEAD(OpenFile, open_files); |
| 261 |
|
| 262 |
int reload_signal; |
| 263 |
usec_t reload_begin_usec; |
| 264 |
|
| 265 |
bool refresh_on_reload_set; |
| 266 |
ServiceRefreshOnReload refresh_on_reload_flags; |
| 267 |
ServiceRefreshOnReload refreshed_mask; |
| 268 |
|
| 269 |
OOMPolicy oom_policy; |
| 270 |
|
| 271 |
char *usb_function_descriptors; |
| 272 |
char *usb_function_strings; |
| 273 |
|
| 274 |
/* The D-Bus request, we will reply once the operation is finished, so that callers can block */ |
| 275 |
sd_bus_message *mount_request; |
| 276 |
} Service; |
| 277 |
|
| 278 |
static inline usec_t service_timeout_abort_usec(Service *s) { |
| 279 |
assert(s); |
| 280 |
return s->timeout_abort_set ? s->timeout_abort_usec : s->timeout_stop_usec; |
| 281 |
} |
| 282 |
|
| 283 |
static inline NotifyAccess service_get_notify_access(Service *s) { |
| 284 |
assert(s); |
| 285 |
return s->notify_access_override < 0 ? s->notify_access : s->notify_access_override; |
| 286 |
} |
| 287 |
|
| 288 |
static inline usec_t service_get_watchdog_usec(Service *s) { |
| 289 |
assert(s); |
| 290 |
return s->watchdog_override_enable ? s->watchdog_override_usec : s->watchdog_original_usec; |
| 291 |
} |
| 292 |
|
| 293 |
extern const UnitVTable service_vtable; |
| 294 |
|
| 295 |
int service_set_socket_fd(Service *s, int fd, struct Socket *socket, struct SocketPeer *peer, bool selinux_context_net); |
| 296 |
void service_release_socket_fd(Service *s); |
| 297 |
|
| 298 |
int service_add_fd_store(Service *s, int fd_in, const char *name, bool do_poll, bool propagate_upstream); |
| 299 |
|
| 300 |
int service_propagate_fd_store_mapping_upstream(Manager *m); |
| 301 |
|
| 302 |
ServiceExtraFD* service_extra_fd_free(ServiceExtraFD *fd); |
| 303 |
|
| 304 |
usec_t service_restart_usec_next(const Service *s) _pure_; |
| 305 |
|
| 306 |
int service_determine_exec_selinux_label(Service *s, char **ret); |
| 307 |
|
| 308 |
DECLARE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart); |
| 309 |
|
| 310 |
DECLARE_STRING_TABLE_LOOKUP(service_restart_mode, ServiceRestartMode); |
| 311 |
|
| 312 |
DECLARE_STRING_TABLE_LOOKUP(service_type, ServiceType); |
| 313 |
|
| 314 |
DECLARE_STRING_TABLE_LOOKUP(service_exit_type, ServiceExitType); |
| 315 |
|
| 316 |
DECLARE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand); |
| 317 |
|
| 318 |
DECLARE_STRING_TABLE_LOOKUP(service_exec_ex_command, ServiceExecCommand); |
| 319 |
|
| 320 |
DECLARE_STRING_TABLE_LOOKUP(notify_state, NotifyState); |
| 321 |
|
| 322 |
DECLARE_STRING_TABLE_LOOKUP(service_result, ServiceResult); |
| 323 |
|
| 324 |
DECLARE_STRING_TABLE_LOOKUP(service_timeout_failure_mode, ServiceTimeoutFailureMode); |
| 325 |
|
| 326 |
ServiceRefreshOnReload service_refresh_on_reload_flag_from_string(const char *s) _pure_; |
| 327 |
int service_refresh_on_reload_from_string_many(const char *s, ServiceRefreshOnReload *ret); |
| 328 |
int service_refresh_on_reload_to_strv(ServiceRefreshOnReload flags, char ***ret); |
| 329 |
|
| 330 |
DEFINE_CAST(SERVICE, Service); |
| 331 |
|
| 332 |
/* Only exported for unit tests */ |
| 333 |
int service_deserialize_exec_command(Unit *u, const char *key, const char *value); |