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