]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/systemd/sd-daemon.h
grypt-util: drop two emacs modelines
[thirdparty/systemd.git] / src / systemd / sd-daemon.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #ifndef foosddaemonhfoo
3 #define foosddaemonhfoo
4
5 /***
6 Copyright 2013 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <inttypes.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25
26 #include "_sd-common.h"
27
28 _SD_BEGIN_DECLARATIONS;
29
30 /*
31 The following functionality is provided:
32
33 - Support for logging with log levels on stderr
34 - File descriptor passing for socket-based activation
35 - Daemon startup and status notification
36 - Detection of systemd boots
37
38 See sd-daemon(3) for more information.
39 */
40
41 /*
42 Log levels for usage on stderr:
43
44 fprintf(stderr, SD_NOTICE "Hello World!\n");
45
46 This is similar to printk() usage in the kernel.
47 */
48 #define SD_EMERG "<0>" /* system is unusable */
49 #define SD_ALERT "<1>" /* action must be taken immediately */
50 #define SD_CRIT "<2>" /* critical conditions */
51 #define SD_ERR "<3>" /* error conditions */
52 #define SD_WARNING "<4>" /* warning conditions */
53 #define SD_NOTICE "<5>" /* normal but significant condition */
54 #define SD_INFO "<6>" /* informational */
55 #define SD_DEBUG "<7>" /* debug-level messages */
56
57 /* The first passed file descriptor is fd 3 */
58 #define SD_LISTEN_FDS_START 3
59
60 /*
61 Returns how many file descriptors have been passed, or a negative
62 errno code on failure. Optionally, removes the $LISTEN_FDS and
63 $LISTEN_PID file descriptors from the environment (recommended, but
64 problematic in threaded environments). If r is the return value of
65 this function you'll find the file descriptors passed as fds
66 SD_LISTEN_FDS_START to SD_LISTEN_FDS_START+r-1. Returns a negative
67 errno style error code on failure. This function call ensures that
68 the FD_CLOEXEC flag is set for the passed file descriptors, to make
69 sure they are not passed on to child processes. If FD_CLOEXEC shall
70 not be set, the caller needs to unset it after this call for all file
71 descriptors that are used.
72
73 See sd_listen_fds(3) for more information.
74 */
75 int sd_listen_fds(int unset_environment);
76
77 int sd_listen_fds_with_names(int unset_environment, char ***names);
78
79 /*
80 Helper call for identifying a passed file descriptor. Returns 1 if
81 the file descriptor is a FIFO in the file system stored under the
82 specified path, 0 otherwise. If path is NULL a path name check will
83 not be done and the call only verifies if the file descriptor
84 refers to a FIFO. Returns a negative errno style error code on
85 failure.
86
87 See sd_is_fifo(3) for more information.
88 */
89 int sd_is_fifo(int fd, const char *path);
90
91 /*
92 Helper call for identifying a passed file descriptor. Returns 1 if
93 the file descriptor is a special character device on the file
94 system stored under the specified path, 0 otherwise.
95 If path is NULL a path name check will not be done and the call
96 only verifies if the file descriptor refers to a special character.
97 Returns a negative errno style error code on failure.
98
99 See sd_is_special(3) for more information.
100 */
101 int sd_is_special(int fd, const char *path);
102
103 /*
104 Helper call for identifying a passed file descriptor. Returns 1 if
105 the file descriptor is a socket of the specified family (AF_INET,
106 ...) and type (SOCK_DGRAM, SOCK_STREAM, ...), 0 otherwise. If
107 family is 0 a socket family check will not be done. If type is 0 a
108 socket type check will not be done and the call only verifies if
109 the file descriptor refers to a socket. If listening is > 0 it is
110 verified that the socket is in listening mode. (i.e. listen() has
111 been called) If listening is == 0 it is verified that the socket is
112 not in listening mode. If listening is < 0 no listening mode check
113 is done. Returns a negative errno style error code on failure.
114
115 See sd_is_socket(3) for more information.
116 */
117 int sd_is_socket(int fd, int family, int type, int listening);
118
119 /*
120 Helper call for identifying a passed file descriptor. Returns 1 if
121 the file descriptor is an Internet socket, of the specified family
122 (either AF_INET or AF_INET6) and the specified type (SOCK_DGRAM,
123 SOCK_STREAM, ...), 0 otherwise. If version is 0 a protocol version
124 check is not done. If type is 0 a socket type check will not be
125 done. If port is 0 a socket port check will not be done. The
126 listening flag is used the same way as in sd_is_socket(). Returns a
127 negative errno style error code on failure.
128
129 See sd_is_socket_inet(3) for more information.
130 */
131 int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port);
132
133 /*
134 Helper call for identifying a passed file descriptor. Returns 1 if the
135 file descriptor is an Internet socket of the specified type
136 (SOCK_DGRAM, SOCK_STREAM, ...), and if the address of the socket is
137 the same as the address specified by addr. The listening flag is used
138 the same way as in sd_is_socket(). Returns a negative errno style
139 error code on failure.
140
141 See sd_is_socket_sockaddr(3) for more information.
142 */
143 int sd_is_socket_sockaddr(int fd, int type, const struct sockaddr* addr, unsigned addr_len, int listening);
144
145 /*
146 Helper call for identifying a passed file descriptor. Returns 1 if
147 the file descriptor is an AF_UNIX socket of the specified type
148 (SOCK_DGRAM, SOCK_STREAM, ...) and path, 0 otherwise. If type is 0
149 a socket type check will not be done. If path is NULL a socket path
150 check will not be done. For normal AF_UNIX sockets set length to
151 0. For abstract namespace sockets set length to the length of the
152 socket name (including the initial 0 byte), and pass the full
153 socket path in path (including the initial 0 byte). The listening
154 flag is used the same way as in sd_is_socket(). Returns a negative
155 errno style error code on failure.
156
157 See sd_is_socket_unix(3) for more information.
158 */
159 int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length);
160
161 /*
162 Helper call for identifying a passed file descriptor. Returns 1 if
163 the file descriptor is a POSIX Message Queue of the specified name,
164 0 otherwise. If path is NULL a message queue name check is not
165 done. Returns a negative errno style error code on failure.
166
167 See sd_is_mq(3) for more information.
168 */
169 int sd_is_mq(int fd, const char *path);
170
171 /*
172 Informs systemd about changed daemon state. This takes a number of
173 newline separated environment-style variable assignments in a
174 string. The following variables are known:
175
176 MAINPID=... The main PID of a daemon, in case systemd did not
177 fork off the process itself. Example: "MAINPID=4711"
178
179 READY=1 Tells systemd that daemon startup or daemon reload
180 is finished (only relevant for services of Type=notify).
181 The passed argument is a boolean "1" or "0". Since there
182 is little value in signaling non-readiness the only
183 value daemons should send is "READY=1".
184
185 RELOADING=1 Tell systemd that the daemon began reloading its
186 configuration. When the configuration has been
187 reloaded completely, READY=1 should be sent to inform
188 systemd about this.
189
190 STOPPING=1 Tells systemd that the daemon is about to go down.
191
192 STATUS=... Passes a single-line status string back to systemd
193 that describes the daemon state. This is free-form
194 and can be used for various purposes: general state
195 feedback, fsck-like programs could pass completion
196 percentages and failing programs could pass a human
197 readable error message. Example: "STATUS=Completed
198 66% of file system check..."
199
200 ERRNO=... If a daemon fails, the errno-style error code,
201 formatted as string. Example: "ERRNO=2" for ENOENT.
202
203 BUSERROR=... If a daemon fails, the D-Bus error-style error
204 code. Example: "BUSERROR=org.freedesktop.DBus.Error.TimedOut"
205
206 WATCHDOG=1 Tells systemd to update the watchdog timestamp.
207 Services using this feature should do this in
208 regular intervals. A watchdog framework can use the
209 timestamps to detect failed services. Also see
210 sd_watchdog_enabled() below.
211
212 WATCHDOG_USEC=...
213 Reset watchdog_usec value during runtime.
214 To reset watchdog_usec value, start the service again.
215 Example: "WATCHDOG_USEC=20000000"
216
217 FDSTORE=1 Store the file descriptors passed along with the
218 message in the per-service file descriptor store,
219 and pass them to the main process again on next
220 invocation. This variable is only supported with
221 sd_pid_notify_with_fds().
222
223 FDSTOREREMOVE=1
224 Remove one or more file descriptors from the file
225 descriptor store, identified by the name specified
226 in FDNAME=, see below.
227
228 FDNAME= A name to assign to new file descriptors stored in the
229 file descriptor store, or the name of the file descriptors
230 to remove in case of FDSTOREREMOVE=1.
231
232 Daemons can choose to send additional variables. However, it is
233 recommended to prefix variable names not listed above with X_.
234
235 Returns a negative errno-style error code on failure. Returns > 0
236 if systemd could be notified, 0 if it couldn't possibly because
237 systemd is not running.
238
239 Example: When a daemon finished starting up, it could issue this
240 call to notify systemd about it:
241
242 sd_notify(0, "READY=1");
243
244 See sd_notifyf() for more complete examples.
245
246 See sd_notify(3) for more information.
247 */
248 int sd_notify(int unset_environment, const char *state);
249
250 /*
251 Similar to sd_notify() but takes a format string.
252
253 Example 1: A daemon could send the following after initialization:
254
255 sd_notifyf(0, "READY=1\n"
256 "STATUS=Processing requests...\n"
257 "MAINPID=%lu",
258 (unsigned long) getpid());
259
260 Example 2: A daemon could send the following shortly before
261 exiting, on failure:
262
263 sd_notifyf(0, "STATUS=Failed to start up: %s\n"
264 "ERRNO=%i",
265 strerror(errno),
266 errno);
267
268 See sd_notifyf(3) for more information.
269 */
270 int sd_notifyf(int unset_environment, const char *format, ...) _sd_printf_(2,3);
271
272 /*
273 Similar to sd_notify(), but send the message on behalf of another
274 process, if the appropriate permissions are available.
275 */
276 int sd_pid_notify(pid_t pid, int unset_environment, const char *state);
277
278 /*
279 Similar to sd_notifyf(), but send the message on behalf of another
280 process, if the appropriate permissions are available.
281 */
282 int sd_pid_notifyf(pid_t pid, int unset_environment, const char *format, ...) _sd_printf_(3,4);
283
284 /*
285 Similar to sd_pid_notify(), but also passes the specified fd array
286 to the service manager for storage. This is particularly useful for
287 FDSTORE=1 messages.
288 */
289 int sd_pid_notify_with_fds(pid_t pid, int unset_environment, const char *state, const int *fds, unsigned n_fds);
290
291 /*
292 Returns > 0 if the system was booted with systemd. Returns < 0 on
293 error. Returns 0 if the system was not booted with systemd. Note
294 that all of the functions above handle non-systemd boots just
295 fine. You should NOT protect them with a call to this function. Also
296 note that this function checks whether the system, not the user
297 session is controlled by systemd. However the functions above work
298 for both user and system services.
299
300 See sd_booted(3) for more information.
301 */
302 int sd_booted(void);
303
304 /*
305 Returns > 0 if the service manager expects watchdog keep-alive
306 events to be sent regularly via sd_notify(0, "WATCHDOG=1"). Returns
307 0 if it does not expect this. If the usec argument is non-NULL
308 returns the watchdog timeout in µs after which the service manager
309 will act on a process that has not sent a watchdog keep alive
310 message. This function is useful to implement services that
311 recognize automatically if they are being run under supervision of
312 systemd with WatchdogSec= set. It is recommended for clients to
313 generate keep-alive pings via sd_notify(0, "WATCHDOG=1") every half
314 of the returned time.
315
316 See sd_watchdog_enabled(3) for more information.
317 */
318 int sd_watchdog_enabled(int unset_environment, uint64_t *usec);
319
320 _SD_END_DECLARATIONS;
321
322 #endif