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