]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/service.h
service: rename service_close_socket_fd() → service_release_socket_fd()
[thirdparty/systemd.git] / src / core / service.h
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
c2f1db8f 2#pragma once
5cb5a6ff
LP
3
4typedef struct Service Service;
a354329f 5typedef struct ServiceFDStore ServiceFDStore;
5cb5a6ff 6
71d35b6b
TA
7#include "exit-status.h"
8#include "kill.h"
cd48e23f 9#include "open-file.h"
9cf3ab0e 10#include "path.h"
1e2e8133 11#include "ratelimit.h"
57b7a260
FS
12#include "socket.h"
13#include "unit.h"
5cb5a6ff 14
034c6ed7 15typedef enum ServiceRestart {
525ee6f4 16 SERVICE_RESTART_NO,
034c6ed7 17 SERVICE_RESTART_ON_SUCCESS,
50caaedb 18 SERVICE_RESTART_ON_FAILURE,
6cfe2fde 19 SERVICE_RESTART_ON_ABNORMAL,
dc99a976 20 SERVICE_RESTART_ON_WATCHDOG,
50caaedb 21 SERVICE_RESTART_ON_ABORT,
94f04347
LP
22 SERVICE_RESTART_ALWAYS,
23 _SERVICE_RESTART_MAX,
2d93c20e 24 _SERVICE_RESTART_INVALID = -EINVAL,
034c6ed7
LP
25} ServiceRestart;
26
27typedef enum ServiceType {
3bd28bf7
LP
28 SERVICE_SIMPLE, /* we fork and go on right-away (i.e. modern socket activated daemons) */
29 SERVICE_FORKING, /* forks by itself (i.e. traditional daemons) */
30 SERVICE_ONESHOT, /* we fork and wait until the program finishes (i.e. programs like fsck which run and need to finish before we continue) */
31 SERVICE_DBUS, /* we fork and wait until a specific D-Bus name appears on the bus */
32 SERVICE_NOTIFY, /* we fork and wait until a daemon sends us a ready message with sd_notify() */
33 SERVICE_NOTIFY_RELOAD, /* just like SERVICE_NOTIFY, but also implements a reload protocol via SIGHUP */
34 SERVICE_IDLE, /* much like simple, but delay exec() until all jobs are dispatched. */
35 SERVICE_EXEC, /* we fork and wait until we execute exec() (this means our own setup is waited for) */
94f04347 36 _SERVICE_TYPE_MAX,
2d93c20e 37 _SERVICE_TYPE_INVALID = -EINVAL,
034c6ed7 38} ServiceType;
5cb5a6ff 39
596e4470
HC
40typedef enum ServiceExitType {
41 SERVICE_EXIT_MAIN, /* we consider the main PID when deciding if the service exited */
42 SERVICE_EXIT_CGROUP, /* we wait for the last process in the cgroup to exit */
43 _SERVICE_EXIT_TYPE_MAX,
44 _SERVICE_EXIT_TYPE_INVALID = -EINVAL,
45} ServiceExitType;
46
5cb5a6ff 47typedef enum ServiceExecCommand {
31cd5f63 48 SERVICE_EXEC_CONDITION,
5cb5a6ff
LP
49 SERVICE_EXEC_START_PRE,
50 SERVICE_EXEC_START,
51 SERVICE_EXEC_START_POST,
5cb5a6ff 52 SERVICE_EXEC_RELOAD,
5cb5a6ff
LP
53 SERVICE_EXEC_STOP,
54 SERVICE_EXEC_STOP_POST,
e537352b 55 _SERVICE_EXEC_COMMAND_MAX,
2d93c20e 56 _SERVICE_EXEC_COMMAND_INVALID = -EINVAL,
5cb5a6ff
LP
57} ServiceExecCommand;
58
308d72dc
LP
59typedef enum NotifyState {
60 NOTIFY_UNKNOWN,
61 NOTIFY_READY,
62 NOTIFY_RELOADING,
63 NOTIFY_STOPPING,
64 _NOTIFY_STATE_MAX,
2d93c20e 65 _NOTIFY_STATE_INVALID = -EINVAL,
308d72dc
LP
66} NotifyState;
67
a4e26faf
JW
68/* The values of this enum are referenced in man/systemd.exec.xml and src/shared/bus-unit-util.c.
69 * Update those sources for each change to this enum. */
f42806df
LP
70typedef enum ServiceResult {
71 SERVICE_SUCCESS,
0b2de9d9 72 SERVICE_FAILURE_RESOURCES, /* a bit of a misnomer, just our catch-all error for errnos we didn't expect */
c35755fb 73 SERVICE_FAILURE_PROTOCOL,
f42806df
LP
74 SERVICE_FAILURE_TIMEOUT,
75 SERVICE_FAILURE_EXIT_CODE,
76 SERVICE_FAILURE_SIGNAL,
77 SERVICE_FAILURE_CORE_DUMP,
bb242b7b 78 SERVICE_FAILURE_WATCHDOG,
07299350 79 SERVICE_FAILURE_START_LIMIT_HIT,
38c41427 80 SERVICE_FAILURE_OOM_KILL, /* OOM Kill by the Kernel or systemd-oomd */
31cd5f63 81 SERVICE_SKIP_CONDITION,
f42806df 82 _SERVICE_RESULT_MAX,
2d93c20e 83 _SERVICE_RESULT_INVALID = -EINVAL,
f42806df
LP
84} ServiceResult;
85
bf760801
JK
86typedef enum ServiceTimeoutFailureMode {
87 SERVICE_TIMEOUT_TERMINATE,
88 SERVICE_TIMEOUT_ABORT,
89 SERVICE_TIMEOUT_KILL,
90 _SERVICE_TIMEOUT_FAILURE_MODE_MAX,
2d93c20e 91 _SERVICE_TIMEOUT_FAILURE_MODE_INVALID = -EINVAL,
bf760801
JK
92} ServiceTimeoutFailureMode;
93
a354329f
LP
94struct ServiceFDStore {
95 Service *service;
96
97 int fd;
8dd4c05b 98 char *fdname;
a354329f 99 sd_event_source *event_source;
30520492 100 bool do_poll;
a354329f
LP
101
102 LIST_FIELDS(ServiceFDStore, fd_store);
103};
104
5cb5a6ff 105struct Service {
ac155bb8 106 Unit meta;
5cb5a6ff 107
034c6ed7 108 ServiceType type;
596e4470 109 ServiceExitType exit_type;
034c6ed7 110 ServiceRestart restart;
37520c1b
LP
111 ExitStatusSet restart_prevent_status;
112 ExitStatusSet restart_force_status;
96342de6 113 ExitStatusSet success_status;
034c6ed7
LP
114
115 /* If set we'll read the main daemon PID from this file */
116 char *pid_file;
117
118 usec_t restart_usec;
be1adc27
MY
119 unsigned restart_steps;
120 usec_t restart_usec_max;
d568a335
MS
121 usec_t timeout_start_usec;
122 usec_t timeout_stop_usec;
dc653bf4 123 usec_t timeout_abort_usec;
54c1a6ab 124 bool timeout_abort_set;
36c16a7c 125 usec_t runtime_max_usec;
5918a933 126 usec_t runtime_rand_extra_usec;
bf760801
JK
127 ServiceTimeoutFailureMode timeout_start_failure_mode;
128 ServiceTimeoutFailureMode timeout_stop_failure_mode;
5cb5a6ff 129
a6927d7f 130 dual_timestamp watchdog_timestamp;
aa8c4bbf
LP
131 usec_t watchdog_usec; /* the requested watchdog timeout in the unit file */
132 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 */
133 usec_t watchdog_override_usec; /* the watchdog timeout requested by the service itself through sd_notify() */
2787d83c 134 bool watchdog_override_enable;
718db961 135 sd_event_source *watchdog_event_source;
a6927d7f 136
e537352b 137 ExecCommand* exec_command[_SERVICE_EXEC_COMMAND_MAX];
4819ff03 138
5cb5a6ff 139 ExecContext exec_context;
4819ff03 140 KillContext kill_context;
4ad49000 141 CGroupContext cgroup_context;
5cb5a6ff 142
a16e1123 143 ServiceState state, deserialized_state;
034c6ed7 144
867b3b7d 145 /* The exit status of the real main process */
034c6ed7
LP
146 ExecStatus main_exec_status;
147
867b3b7d 148 /* The currently executed control process */
034c6ed7 149 ExecCommand *control_command;
867b3b7d
LP
150
151 /* The currently executed main process, which may be NULL if
152 * the main process got started via forking mode and not by
153 * us */
154 ExecCommand *main_command;
155
156 /* The ID of the control command currently being executed */
a16e1123 157 ServiceExecCommand control_command_id;
867b3b7d 158
613b411c
LP
159 /* Runtime data of the execution context */
160 ExecRuntime *exec_runtime;
161
034c6ed7 162 pid_t main_pid, control_pid;
3fabebf4
LP
163
164 /* if we are a socket activated service instance, store information of the connection/peer/socket */
07459bb6 165 int socket_fd;
3fabebf4
LP
166 SocketPeer *socket_peer;
167 UnitRef accept_socket;
16115b0a 168 bool socket_fd_selinux_context_net;
8fe914ec
LP
169
170 bool permissions_start_only;
171 bool root_directory_start_only;
02ee865a 172 bool remain_after_exit;
3185a36b 173 bool guess_main_pid;
8fe914ec 174
05e343b7 175 /* If we shut down, remember why */
f42806df
LP
176 ServiceResult result;
177 ServiceResult reload_result;
4c2f5842 178 ServiceResult clean_result;
e2f3b44c 179
5de6b302 180 bool main_pid_known:1;
6dfa5494 181 bool main_pid_alien:1;
05e343b7 182 bool bus_name_good:1;
47342320 183 bool forbid_restart:1;
deb4e708
MK
184 /* Keep restart intention between UNIT_FAILED and UNIT_ACTIVATING */
185 bool will_auto_restart:1;
d568a335 186 bool start_timeout_defined:1;
0a6991e0 187 bool exec_fd_hot:1;
2c4104f0 188
05e343b7 189 char *bus_name;
d8ccf5fd 190 char *bus_name_owner; /* unique name of the current owner */
05e343b7 191
8c47c732 192 char *status_text;
4774e357 193 int status_errno;
8c47c732 194
718db961 195 sd_event_source *timer_event_source;
3a111838 196 PathSpec *pid_file_pathspec;
8fe914ec
LP
197
198 NotifyAccess notify_access;
19dff691 199 NotifyAccess notify_access_override;
308d72dc 200 NotifyState notify_state;
a354329f 201
e39eb045
LP
202 sd_bus_slot *bus_name_pid_lookup_slot;
203
5686391b
LP
204 sd_event_source *exec_fd_event_source;
205
a354329f 206 ServiceFDStore *fd_store;
da6053d0 207 size_t n_fd_store;
a354329f 208 unsigned n_fd_store_max;
b9c1883a 209 ExecPreserveMode fd_store_preserve_mode;
6b7e5923
PS
210
211 char *usb_function_descriptors;
212 char *usb_function_strings;
a34ceba6
LP
213
214 int stdin_fd;
215 int stdout_fd;
216 int stderr_fd;
7a0019d3
LP
217
218 unsigned n_restarts;
219 bool flush_n_restarts;
afcfaa69
LP
220
221 OOMPolicy oom_policy;
cd48e23f
RP
222
223 LIST_HEAD(OpenFile, open_files);
3bd28bf7
LP
224
225 int reload_signal;
226 usec_t reload_begin_usec;
5cb5a6ff
LP
227};
228
dc653bf4 229static inline usec_t service_timeout_abort_usec(Service *s) {
9c79f0e0 230 assert(s);
dc653bf4
JK
231 return s->timeout_abort_set ? s->timeout_abort_usec : s->timeout_stop_usec;
232}
233
19dff691
MY
234static inline NotifyAccess service_get_notify_access(Service *s) {
235 assert(s);
236 return s->notify_access_override < 0 ? s->notify_access : s->notify_access_override;
237}
238
4793c310
CD
239static inline usec_t service_get_watchdog_usec(Service *s) {
240 assert(s);
241 return s->watchdog_override_enable ? s->watchdog_override_usec : s->watchdog_original_usec;
242}
243
47be870b 244extern const UnitVTable service_vtable;
5cb5a6ff 245
3fabebf4 246int service_set_socket_fd(Service *s, int fd, struct Socket *socket, struct SocketPeer *peer, bool selinux_context_net);
81a1d6d6 247void service_release_socket_fd(Service *s);
4f2d528d 248
5171356e 249usec_t service_restart_usec_next(Service *s);
be1adc27 250
44a6b1b6
ZJS
251const char* service_restart_to_string(ServiceRestart i) _const_;
252ServiceRestart service_restart_from_string(const char *s) _pure_;
94f04347 253
44a6b1b6
ZJS
254const char* service_type_to_string(ServiceType i) _const_;
255ServiceType service_type_from_string(const char *s) _pure_;
94f04347 256
596e4470
HC
257const char* service_exit_type_to_string(ServiceExitType i) _const_;
258ServiceExitType service_exit_type_from_string(const char *s) _pure_;
259
44a6b1b6
ZJS
260const char* service_exec_command_to_string(ServiceExecCommand i) _const_;
261ServiceExecCommand service_exec_command_from_string(const char *s) _pure_;
94f04347 262
b3d59367
AZ
263const char* service_exec_ex_command_to_string(ServiceExecCommand i) _const_;
264ServiceExecCommand service_exec_ex_command_from_string(const char *s) _pure_;
265
308d72dc
LP
266const char* notify_state_to_string(NotifyState i) _const_;
267NotifyState notify_state_from_string(const char *s) _pure_;
268
44a6b1b6
ZJS
269const char* service_result_to_string(ServiceResult i) _const_;
270ServiceResult service_result_from_string(const char *s) _pure_;
57b7a260 271
bf760801
JK
272const char* service_timeout_failure_mode_to_string(ServiceTimeoutFailureMode i) _const_;
273ServiceTimeoutFailureMode service_timeout_failure_mode_from_string(const char *s) _pure_;
274
57b7a260 275DEFINE_CAST(SERVICE, Service);
3eac1bca
LP
276
277#define STATUS_TEXT_MAX (16U*1024U)
35243b77
ZJS
278
279/* Only exported for unit tests */
280int service_deserialize_exec_command(Unit *u, const char *key, const char *value);