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