]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/service.h
core: move timeout_clean_usec from Service to ExecContext
[thirdparty/systemd.git] / src / core / service.h
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
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"
9cf3ab0e 9#include "path.h"
1e2e8133 10#include "ratelimit.h"
57b7a260
FS
11#include "socket.h"
12#include "unit.h"
5cb5a6ff 13
034c6ed7 14typedef enum ServiceRestart {
525ee6f4 15 SERVICE_RESTART_NO,
034c6ed7 16 SERVICE_RESTART_ON_SUCCESS,
50caaedb 17 SERVICE_RESTART_ON_FAILURE,
6cfe2fde 18 SERVICE_RESTART_ON_ABNORMAL,
dc99a976 19 SERVICE_RESTART_ON_WATCHDOG,
50caaedb 20 SERVICE_RESTART_ON_ABORT,
94f04347
LP
21 SERVICE_RESTART_ALWAYS,
22 _SERVICE_RESTART_MAX,
23 _SERVICE_RESTART_INVALID = -1
034c6ed7
LP
24} ServiceRestart;
25
26typedef enum ServiceType {
05e343b7 27 SERVICE_SIMPLE, /* we fork and go on right-away (i.e. modern socket activated daemons) */
1f48cf56 28 SERVICE_FORKING, /* forks by itself (i.e. traditional daemons) */
34e9ba66 29 SERVICE_ONESHOT, /* we fork and wait until the program finishes (i.e. programs like fsck which run and need to finish before we continue) */
05e343b7 30 SERVICE_DBUS, /* we fork and wait until a specific D-Bus name appears on the bus */
8c47c732 31 SERVICE_NOTIFY, /* we fork and wait until a daemon sends us a ready message with sd_notify() */
f2b68789 32 SERVICE_IDLE, /* much like simple, but delay exec() until all jobs are dispatched. */
5686391b 33 SERVICE_EXEC, /* we fork and wait until we execute exec() (this means our own setup is waited for) */
94f04347
LP
34 _SERVICE_TYPE_MAX,
35 _SERVICE_TYPE_INVALID = -1
034c6ed7 36} ServiceType;
5cb5a6ff
LP
37
38typedef enum ServiceExecCommand {
31cd5f63 39 SERVICE_EXEC_CONDITION,
5cb5a6ff
LP
40 SERVICE_EXEC_START_PRE,
41 SERVICE_EXEC_START,
42 SERVICE_EXEC_START_POST,
5cb5a6ff 43 SERVICE_EXEC_RELOAD,
5cb5a6ff
LP
44 SERVICE_EXEC_STOP,
45 SERVICE_EXEC_STOP_POST,
e537352b
LP
46 _SERVICE_EXEC_COMMAND_MAX,
47 _SERVICE_EXEC_COMMAND_INVALID = -1
5cb5a6ff
LP
48} ServiceExecCommand;
49
308d72dc
LP
50typedef enum NotifyState {
51 NOTIFY_UNKNOWN,
52 NOTIFY_READY,
53 NOTIFY_RELOADING,
54 NOTIFY_STOPPING,
55 _NOTIFY_STATE_MAX,
56 _NOTIFY_STATE_INVALID = -1
57} NotifyState;
58
a4e26faf
JW
59/* The values of this enum are referenced in man/systemd.exec.xml and src/shared/bus-unit-util.c.
60 * Update those sources for each change to this enum. */
f42806df
LP
61typedef enum ServiceResult {
62 SERVICE_SUCCESS,
0b2de9d9 63 SERVICE_FAILURE_RESOURCES, /* a bit of a misnomer, just our catch-all error for errnos we didn't expect */
c35755fb 64 SERVICE_FAILURE_PROTOCOL,
f42806df
LP
65 SERVICE_FAILURE_TIMEOUT,
66 SERVICE_FAILURE_EXIT_CODE,
67 SERVICE_FAILURE_SIGNAL,
68 SERVICE_FAILURE_CORE_DUMP,
bb242b7b 69 SERVICE_FAILURE_WATCHDOG,
07299350 70 SERVICE_FAILURE_START_LIMIT_HIT,
afcfaa69 71 SERVICE_FAILURE_OOM_KILL,
31cd5f63 72 SERVICE_SKIP_CONDITION,
f42806df
LP
73 _SERVICE_RESULT_MAX,
74 _SERVICE_RESULT_INVALID = -1
75} ServiceResult;
76
a354329f
LP
77struct ServiceFDStore {
78 Service *service;
79
80 int fd;
8dd4c05b 81 char *fdname;
a354329f
LP
82 sd_event_source *event_source;
83
84 LIST_FIELDS(ServiceFDStore, fd_store);
85};
86
5cb5a6ff 87struct Service {
ac155bb8 88 Unit meta;
5cb5a6ff 89
034c6ed7
LP
90 ServiceType type;
91 ServiceRestart restart;
37520c1b
LP
92 ExitStatusSet restart_prevent_status;
93 ExitStatusSet restart_force_status;
96342de6 94 ExitStatusSet success_status;
034c6ed7
LP
95
96 /* If set we'll read the main daemon PID from this file */
97 char *pid_file;
98
99 usec_t restart_usec;
d568a335
MS
100 usec_t timeout_start_usec;
101 usec_t timeout_stop_usec;
dc653bf4 102 usec_t timeout_abort_usec;
54c1a6ab 103 bool timeout_abort_set;
36c16a7c 104 usec_t runtime_max_usec;
5cb5a6ff 105
a6927d7f 106 dual_timestamp watchdog_timestamp;
aa8c4bbf
LP
107 usec_t watchdog_usec; /* the requested watchdog timeout in the unit file */
108 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 */
109 usec_t watchdog_override_usec; /* the watchdog timeout requested by the service itself through sd_notify() */
2787d83c 110 bool watchdog_override_enable;
718db961 111 sd_event_source *watchdog_event_source;
a6927d7f 112
e537352b 113 ExecCommand* exec_command[_SERVICE_EXEC_COMMAND_MAX];
4819ff03 114
5cb5a6ff 115 ExecContext exec_context;
4819ff03 116 KillContext kill_context;
4ad49000 117 CGroupContext cgroup_context;
5cb5a6ff 118
a16e1123 119 ServiceState state, deserialized_state;
034c6ed7 120
867b3b7d 121 /* The exit status of the real main process */
034c6ed7
LP
122 ExecStatus main_exec_status;
123
867b3b7d 124 /* The currently executed control process */
034c6ed7 125 ExecCommand *control_command;
867b3b7d
LP
126
127 /* The currently executed main process, which may be NULL if
128 * the main process got started via forking mode and not by
129 * us */
130 ExecCommand *main_command;
131
132 /* The ID of the control command currently being executed */
a16e1123 133 ServiceExecCommand control_command_id;
867b3b7d 134
613b411c
LP
135 /* Runtime data of the execution context */
136 ExecRuntime *exec_runtime;
29206d46 137 DynamicCreds dynamic_creds;
613b411c 138
034c6ed7 139 pid_t main_pid, control_pid;
07459bb6 140 int socket_fd;
9d565427 141 SocketPeer *peer;
16115b0a 142 bool socket_fd_selinux_context_net;
8fe914ec
LP
143
144 bool permissions_start_only;
145 bool root_directory_start_only;
02ee865a 146 bool remain_after_exit;
3185a36b 147 bool guess_main_pid;
8fe914ec 148
05e343b7 149 /* If we shut down, remember why */
f42806df
LP
150 ServiceResult result;
151 ServiceResult reload_result;
4c2f5842 152 ServiceResult clean_result;
e2f3b44c 153
5de6b302 154 bool main_pid_known:1;
6dfa5494 155 bool main_pid_alien:1;
05e343b7 156 bool bus_name_good:1;
47342320 157 bool forbid_restart:1;
deb4e708
MK
158 /* Keep restart intention between UNIT_FAILED and UNIT_ACTIVATING */
159 bool will_auto_restart:1;
d568a335 160 bool start_timeout_defined:1;
0a6991e0 161 bool exec_fd_hot:1;
2c4104f0 162
05e343b7 163 char *bus_name;
d8ccf5fd 164 char *bus_name_owner; /* unique name of the current owner */
05e343b7 165
8c47c732 166 char *status_text;
4774e357 167 int status_errno;
8c47c732 168
57020a3a 169 UnitRef accept_socket;
4f2d528d 170
718db961 171 sd_event_source *timer_event_source;
3a111838 172 PathSpec *pid_file_pathspec;
8fe914ec
LP
173
174 NotifyAccess notify_access;
308d72dc 175 NotifyState notify_state;
a354329f 176
5686391b
LP
177 sd_event_source *exec_fd_event_source;
178
a354329f 179 ServiceFDStore *fd_store;
da6053d0 180 size_t n_fd_store;
a354329f 181 unsigned n_fd_store_max;
7eb2a8a1 182 unsigned n_keep_fd_store;
6b7e5923
PS
183
184 char *usb_function_descriptors;
185 char *usb_function_strings;
a34ceba6
LP
186
187 int stdin_fd;
188 int stdout_fd;
189 int stderr_fd;
7a0019d3
LP
190
191 unsigned n_restarts;
192 bool flush_n_restarts;
afcfaa69
LP
193
194 OOMPolicy oom_policy;
5cb5a6ff
LP
195};
196
dc653bf4 197static inline usec_t service_timeout_abort_usec(Service *s) {
9c79f0e0 198 assert(s);
dc653bf4
JK
199 return s->timeout_abort_set ? s->timeout_abort_usec : s->timeout_stop_usec;
200}
201
47be870b 202extern const UnitVTable service_vtable;
5cb5a6ff 203
16115b0a 204int service_set_socket_fd(Service *s, int fd, struct Socket *socket, bool selinux_context_net);
3e7a1f50 205void service_close_socket_fd(Service *s);
4f2d528d 206
44a6b1b6
ZJS
207const char* service_restart_to_string(ServiceRestart i) _const_;
208ServiceRestart service_restart_from_string(const char *s) _pure_;
94f04347 209
44a6b1b6
ZJS
210const char* service_type_to_string(ServiceType i) _const_;
211ServiceType service_type_from_string(const char *s) _pure_;
94f04347 212
44a6b1b6
ZJS
213const char* service_exec_command_to_string(ServiceExecCommand i) _const_;
214ServiceExecCommand service_exec_command_from_string(const char *s) _pure_;
94f04347 215
b3d59367
AZ
216const char* service_exec_ex_command_to_string(ServiceExecCommand i) _const_;
217ServiceExecCommand service_exec_ex_command_from_string(const char *s) _pure_;
218
308d72dc
LP
219const char* notify_state_to_string(NotifyState i) _const_;
220NotifyState notify_state_from_string(const char *s) _pure_;
221
44a6b1b6
ZJS
222const char* service_result_to_string(ServiceResult i) _const_;
223ServiceResult service_result_from_string(const char *s) _pure_;
57b7a260
FS
224
225DEFINE_CAST(SERVICE, Service);
3eac1bca
LP
226
227#define STATUS_TEXT_MAX (16U*1024U)