chiark / gitweb /
sd-daemon: extend return value logic of sd_notify()
[elogind.git] / src / sd-daemon.h
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 #ifndef foosddaemonhfoo
4 #define foosddaemonhfoo
5
6 /***
7   Copyright 2010 Lennart Poettering
8
9   Permission is hereby granted, free of charge, to any person
10   obtaining a copy of this software and associated documentation files
11   (the "Software"), to deal in the Software without restriction,
12   including without limitation the rights to use, copy, modify, merge,
13   publish, distribute, sublicense, and/or sell copies of the Software,
14   and to permit persons to whom the Software is furnished to do so,
15   subject to the following conditions:
16
17   The above copyright notice and this permission notice shall be
18   included in all copies or substantial portions of the Software.
19
20   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27   SOFTWARE.
28 ***/
29
30 #include <sys/types.h>
31 #include <inttypes.h>
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 /* Reference implementation of a few systemd related interfaces for
38  * writing daemons. These interfaces are trivial to implement. To
39  * simplify porting we provide this reference
40  * implementation. Applications are welcome to reimplement the
41  * algorithms described here, if they do not want to include these two
42  * source files. */
43
44 /*
45   Log levels for usage on stderr:
46
47           fprintf(stderr, SD_NOTICE "Hello World!");
48
49   This is similar to printk() usage in the kernel.
50 */
51
52 #define SD_EMERG   "<0>"  /* system is unusable */
53 #define SD_ALERT   "<1>"  /* action must be taken immediately */
54 #define SD_CRIT    "<2>"  /* critical conditions */
55 #define SD_ERR     "<3>"  /* error conditions */
56 #define SD_WARNING "<4>"  /* warning conditions */
57 #define SD_NOTICE  "<5>"  /* normal but significant condition */
58 #define SD_INFO    "<6>"  /* informational */
59 #define SD_DEBUG   "<7>"  /* debug-level messages */
60
61 /* The first passed file descriptor is fd 3 */
62 #define SD_LISTEN_FDS_START 3
63
64 /* Returns how many file descriptors have been passed, or a negative
65  * errno code on failure. Optionally, removes the $LISTEN_FDS and
66  * $LISTEN_PID file descriptors from the environment (recommended, but
67  * problematic in threaded environments). If r is the return value of
68  * this function you'll find the file descriptors passed as fds
69  * SD_LISTEN_FDS_START to SD_LISTEN_FDS_START+r-1. Returns a negative
70  * errno style error code on failure. This function call ensures that
71  * the FD_CLOEXEC flag is set for the passed file descriptors, to make
72  * sure they are not passed on to child processes. If FD_CLOEXEC shall
73  * not be set, the caller needs to unset it after this call for all file
74  * descriptors that are used.*/
75 int sd_listen_fds(int unset_environment);
76
77 /* Helper call for identifying a passed file descriptor. Returns 1 if
78  * the file descriptor is a FIFO in the file system stored under the
79  * specified path, 0 otherwise. If path is NULL a path name check will
80  * not be done and the call only verifies if the file descriptor
81  * refers to a FIFO. Returns a negative errno style error code on
82  * failure. */
83 int sd_is_fifo(int fd, const char *path);
84
85 /* Helper call for identifying a passed file descriptor. Returns 1 if
86  * the file descriptor is a socket of the specified family (AF_INET,
87  * ...) and type (SOCK_DGRAM, SOCK_STREAM, ...), 0 otherwise. If
88  * family is 0 a socket family check will not be done. If type is 0 a
89  * socket type check will not be done and the call only verifies if
90  * the file descriptor refers to a socket. If listening is > 0 it is
91  * verified that the socket is in listening mode. (i.e. listen() has
92  * been called) If listening is == 0 it is verified that the socket is
93  * not in listening mode. If listening is < 0 no listening mode check
94  * is done. Returns a negative errno style error code on failure. */
95 int sd_is_socket(int fd, int family, int type, int listening);
96
97 /* Helper call for identifying a passed file descriptor. Returns 1 if
98  * the file descriptor is an Internet socket, of the specified family
99  * (either AF_INET or AF_INET6) and the specified type (SOCK_DGRAM,
100  * SOCK_STREAM, ...), 0 otherwise. If version is 0 a protocol version
101  * check is not done. If type is 0 a socket type check will not be
102  * done. If port is 0 a socket port check will not be done. The
103  * listening flag is used the same way as in sd_is_socket(). Returns a
104  * negative errno style error code on failure. */
105 int sd_is_socket_inet(int fd, int family, int type, int listening, uint16_t port);
106
107 /* Helper call for identifying a passed file descriptor. Returns 1 if
108  * the file descriptor is an AF_UNIX socket of the specified type
109  * (SOCK_DGRAM, SOCK_STREAM, ...) and path, 0 otherwise. If type is 0
110  * a socket type check will not be done. If path is NULL a socket path
111  * check will not be done. For normal AF_UNIX sockets set length to
112  * 0. For abstract namespace sockets set length to the length of the
113  * socket name (including the initial 0 byte), and pass the full
114  * socket path in path (including the initial 0 byte). The listening
115  * flag is used the same way as in sd_is_socket(). Returns a negative
116  * errno style error code on failure. */
117 int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length);
118
119 /* Informs systemd about changed daemon state. This takes a numeber of
120  * newline seperated environment-style variable assignments in a
121  * string. The following strings are known:
122  *
123  *    READY=1      Tells systemd that daemon startup is finished (only
124  *                 relevant for services of Type=notify). The passed
125  *                 argument is a boolean "1" or "0". Since there is
126  *                 little value in signalling non-readiness the only
127  *                 value daemons should send is "READY=1".
128  *
129  *    STATUS=...   Passes a single-line status string back to systemd
130  *                 that describes the daemon state. This is free-from
131  *                 and can be used for various purposes: general state
132  *                 feedback, fsck-like programs could pass completion
133  *                 percentages and failing programs could pass a human
134  *                 readable error message. Example: "STATUS=Completed
135  *                 66% of file system check..."
136  *
137  *    ERRNO=...    If a daemon fails, the errno-style error code,
138  *                 formatted as string. Example: "ERRNO=2" for ENOENT.
139  *
140  *    BUSERROR=... If a daemon fails, the D-Bus error-style error
141  *                 code. Example: "BUSERROR=org.freedesktop.DBus.Error.TimedOut"
142  *
143  *    MAINPID=...  The main pid of a daemon, in case systemd did not
144  *                 fork off the process itself. Example: "MAINPID=4711"
145  *
146  * Daemons can choose to send additional variables.
147  *
148  * Returns a negative errno-style error code on failure. Returns > 0
149  * if systemd could be notified, 0 if it couldn't possibly because
150  * systemd is not running.
151  *
152  * See sd_notifyf() for more complete examples.
153  */
154 int sd_notify(int unset_environment, const char *state);
155
156 /* Similar to sd_send_state() but takes a format string.
157  *
158  * Example 1: A daemon could send the following after initialization:
159  *
160  * sd_notifyf(0, "READY=1\n"
161  *               "STATUS=Processing requests...\n"
162  *               "MAINPID=%lu",
163  *               (unsigned long) getpid());
164  *
165  * Example 2: A daemon could send the following shortly before
166  * exiting, on failure:
167  *
168  * sd_notifyf(0, "STATUS=Failed to start up: %s\n"
169  *               "ERRNO=%i",
170  *               strerror(errno),
171  *               errno);
172  */
173 int sd_notifyf(int unset_environment, const char *format, ...);
174
175 #ifdef __cplusplus
176 }
177 #endif
178
179 #endif