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