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