]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/service.h
Merge pull request #8981 from keszybz/ratelimit-and-dbus
[thirdparty/systemd.git] / src / core / service.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5 This file is part of systemd.
6
7 Copyright 2010 Lennart Poettering
8 ***/
9
10 typedef struct Service Service;
11 typedef struct ServiceFDStore ServiceFDStore;
12
13 #include "exit-status.h"
14 #include "kill.h"
15 #include "path.h"
16 #include "ratelimit.h"
17 #include "socket.h"
18 #include "unit.h"
19
20 typedef enum ServiceRestart {
21 SERVICE_RESTART_NO,
22 SERVICE_RESTART_ON_SUCCESS,
23 SERVICE_RESTART_ON_FAILURE,
24 SERVICE_RESTART_ON_ABNORMAL,
25 SERVICE_RESTART_ON_WATCHDOG,
26 SERVICE_RESTART_ON_ABORT,
27 SERVICE_RESTART_ALWAYS,
28 _SERVICE_RESTART_MAX,
29 _SERVICE_RESTART_INVALID = -1
30 } ServiceRestart;
31
32 typedef enum ServiceType {
33 SERVICE_SIMPLE, /* we fork and go on right-away (i.e. modern socket activated daemons) */
34 SERVICE_FORKING, /* forks by itself (i.e. traditional daemons) */
35 SERVICE_ONESHOT, /* we fork and wait until the program finishes (i.e. programs like fsck which run and need to finish before we continue) */
36 SERVICE_DBUS, /* we fork and wait until a specific D-Bus name appears on the bus */
37 SERVICE_NOTIFY, /* we fork and wait until a daemon sends us a ready message with sd_notify() */
38 SERVICE_IDLE, /* much like simple, but delay exec() until all jobs are dispatched. */
39 _SERVICE_TYPE_MAX,
40 _SERVICE_TYPE_INVALID = -1
41 } ServiceType;
42
43 typedef enum ServiceExecCommand {
44 SERVICE_EXEC_START_PRE,
45 SERVICE_EXEC_START,
46 SERVICE_EXEC_START_POST,
47 SERVICE_EXEC_RELOAD,
48 SERVICE_EXEC_STOP,
49 SERVICE_EXEC_STOP_POST,
50 _SERVICE_EXEC_COMMAND_MAX,
51 _SERVICE_EXEC_COMMAND_INVALID = -1
52 } ServiceExecCommand;
53
54 typedef enum NotifyState {
55 NOTIFY_UNKNOWN,
56 NOTIFY_READY,
57 NOTIFY_RELOADING,
58 NOTIFY_STOPPING,
59 _NOTIFY_STATE_MAX,
60 _NOTIFY_STATE_INVALID = -1
61 } NotifyState;
62
63 /* The values of this enum are referenced in man/systemd.exec.xml and src/shared/bus-unit-util.c.
64 * Update those sources for each change to this enum. */
65 typedef enum ServiceResult {
66 SERVICE_SUCCESS,
67 SERVICE_FAILURE_RESOURCES, /* a bit of a misnomer, just our catch-all error for errnos we didn't expect */
68 SERVICE_FAILURE_PROTOCOL,
69 SERVICE_FAILURE_TIMEOUT,
70 SERVICE_FAILURE_EXIT_CODE,
71 SERVICE_FAILURE_SIGNAL,
72 SERVICE_FAILURE_CORE_DUMP,
73 SERVICE_FAILURE_WATCHDOG,
74 SERVICE_FAILURE_START_LIMIT_HIT,
75 _SERVICE_RESULT_MAX,
76 _SERVICE_RESULT_INVALID = -1
77 } ServiceResult;
78
79 struct ServiceFDStore {
80 Service *service;
81
82 int fd;
83 char *fdname;
84 sd_event_source *event_source;
85
86 LIST_FIELDS(ServiceFDStore, fd_store);
87 };
88
89 struct Service {
90 Unit meta;
91
92 ServiceType type;
93 ServiceRestart restart;
94 ExitStatusSet restart_prevent_status;
95 ExitStatusSet restart_force_status;
96 ExitStatusSet success_status;
97
98 /* If set we'll read the main daemon PID from this file */
99 char *pid_file;
100
101 usec_t restart_usec;
102 usec_t timeout_start_usec;
103 usec_t timeout_stop_usec;
104 usec_t runtime_max_usec;
105
106 dual_timestamp watchdog_timestamp;
107 usec_t watchdog_usec;
108 usec_t watchdog_override_usec;
109 bool watchdog_override_enable;
110 sd_event_source *watchdog_event_source;
111
112 ExecCommand* exec_command[_SERVICE_EXEC_COMMAND_MAX];
113
114 ExecContext exec_context;
115 KillContext kill_context;
116 CGroupContext cgroup_context;
117
118 ServiceState state, deserialized_state;
119
120 /* The exit status of the real main process */
121 ExecStatus main_exec_status;
122
123 /* The currently executed control process */
124 ExecCommand *control_command;
125
126 /* The currently executed main process, which may be NULL if
127 * the main process got started via forking mode and not by
128 * us */
129 ExecCommand *main_command;
130
131 /* The ID of the control command currently being executed */
132 ServiceExecCommand control_command_id;
133
134 /* Runtime data of the execution context */
135 ExecRuntime *exec_runtime;
136 DynamicCreds dynamic_creds;
137
138 pid_t main_pid, control_pid;
139 int socket_fd;
140 SocketPeer *peer;
141 bool socket_fd_selinux_context_net;
142
143 bool permissions_start_only;
144 bool root_directory_start_only;
145 bool remain_after_exit;
146 bool guess_main_pid;
147
148 /* If we shut down, remember why */
149 ServiceResult result;
150 ServiceResult reload_result;
151
152 bool main_pid_known:1;
153 bool main_pid_alien:1;
154 bool bus_name_good:1;
155 bool forbid_restart:1;
156 /* Keep restart intention between UNIT_FAILED and UNIT_ACTIVATING */
157 bool will_auto_restart:1;
158 bool start_timeout_defined:1;
159
160 char *bus_name;
161 char *bus_name_owner; /* unique name of the current owner */
162
163 char *status_text;
164 int status_errno;
165
166 UnitRef accept_socket;
167
168 sd_event_source *timer_event_source;
169 PathSpec *pid_file_pathspec;
170
171 NotifyAccess notify_access;
172 NotifyState notify_state;
173
174 ServiceFDStore *fd_store;
175 size_t n_fd_store;
176 unsigned n_fd_store_max;
177 unsigned n_keep_fd_store;
178
179 char *usb_function_descriptors;
180 char *usb_function_strings;
181
182 int stdin_fd;
183 int stdout_fd;
184 int stderr_fd;
185
186 unsigned n_restarts;
187 bool flush_n_restarts;
188 };
189
190 extern const UnitVTable service_vtable;
191
192 int service_set_socket_fd(Service *s, int fd, struct Socket *socket, bool selinux_context_net);
193 void service_close_socket_fd(Service *s);
194
195 const char* service_restart_to_string(ServiceRestart i) _const_;
196 ServiceRestart service_restart_from_string(const char *s) _pure_;
197
198 const char* service_type_to_string(ServiceType i) _const_;
199 ServiceType service_type_from_string(const char *s) _pure_;
200
201 const char* service_exec_command_to_string(ServiceExecCommand i) _const_;
202 ServiceExecCommand service_exec_command_from_string(const char *s) _pure_;
203
204 const char* notify_state_to_string(NotifyState i) _const_;
205 NotifyState notify_state_from_string(const char *s) _pure_;
206
207 const char* service_result_to_string(ServiceResult i) _const_;
208 ServiceResult service_result_from_string(const char *s) _pure_;
209
210 DEFINE_CAST(SERVICE, Service);