chiark / gitweb /
sd-daemon: add API to verify socket types
[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 <inttypes.h>
31
32 /* Reference implementation of a few systemd related interfaces for
33  * writing daemons. These interfaces are trivial to implement, however
34  * to simplify porting we provide this reference
35  * implementation. Applications are free to reimplement the algorithms
36  * described here. */
37
38 /*
39   Log levels for usage on stderr:
40
41           fprintf(stderr, SD_NOTICE "Hello World!");
42
43   This is similar to printk() usage in the kernel.
44 */
45
46 #define SD_EMERG   "<0>"  /* system is unusable */
47 #define SD_ALERT   "<1>"  /* action must be taken immediately */
48 #define SD_CRIT    "<2>"  /* critical conditions */
49 #define SD_ERR     "<3>"  /* error conditions */
50 #define SD_WARNING "<4>"  /* warning conditions */
51 #define SD_NOTICE  "<5>"  /* normal but significant condition */
52 #define SD_INFO    "<6>"  /* informational */
53 #define SD_DEBUG   "<7>"  /* debug-level messages */
54
55 /* The first passed file descriptor is fd 3 */
56 #define SD_LISTEN_FDS_START 3
57
58 /* Returns how many file descriptors have been passed, or a negative
59  * errno code on failure. Optionally removes the $LISTEN_FDS and
60  * $LISTEN_PID file descriptors from the environment
61  * (recommended). You'll find the file descriptors passed as fds
62  * SD_LISTEN_FDS_START to SD_LISTEN_FDS_START+r-1 if r is the return
63  * value of this functioin. Returns a negative errno style error code
64  * on failure. */
65 int sd_listen_fds(int unset_environment);
66
67 /* Helper call for identifying a passed file descriptor. Returns 1 if
68  * the file descriptor is a FIFO in the file system stored under the
69  * specified path, 0 otherwise. If path is NULL a path name check will
70  * not be done and the call only verifies if the file descriptor
71  * refers to a FIFO. Returns a negative errno style error code on
72  * failure. */
73 int sd_is_fifo(int fd, const char *path);
74
75 /* Helper call for identifying a passed file descriptor. Returns 1 if
76  * the file descriptor is a socket of the specified type (SOCK_DGRAM,
77  * SOCK_STREAM, ...), 0 otherwise. If type is 0 a socket type check
78  * will not be done and the call only verifies if the file descriptor
79  * refers to a socket. Returns a negative errno style error code on
80  * failure. */
81 int sd_is_socket(int fd, int type, int listening);
82
83 /* Helper call for identifying a passed file descriptor. Returns 1 if
84  * the file descriptor is an Internet socket (either AF_INET or
85  * AF_INET6) of the specified type (SOCK_DGRAM, SOCK_STREAM, ...), 0
86  * otherwise. If type is 0 a socket type check will not be done. If
87  * port is 0 a socket port check will not be done. Returns a negative
88  * errno style error code on failure. */
89 int sd_is_socket_inet(int fd, int type, int listening, uint16_t port);
90
91 /* Helper call for identifying a passed file descriptor. Returns 1 if
92  * the file descriptor is an AF_UNIX socket of the specified type
93  * (SOCK_DGRAM, SOCK_STREAM, ...) and path, 0 otherwise. If type is 0
94  * a socket type check will not be done. If path is NULL a socket path
95  * check will not be done. For normal AF_UNIX sockets set length to
96  * 0. For abstract namespace sockets set length to the length of the
97  * socket name (excluding the initial 0 byte). Returns a negative
98  * errno style error code on failure. */
99 int sd_is_socket_unix(int fd, int type, int listening, const char *path, size_t length);
100
101 #endif