Code

8ffca4bcd70a5b6ea117ba748cd3b75cb2cf6c47
[sysdb.git] / src / include / utils / os.h
1 /*
2  * SysDB - src/include/utils/os.h
3  * Copyright (C) 2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #ifndef SDB_UTILS_OS_H
29 #define SDB_UTILS_OS_H 1
31 #include <sys/types.h>
32 #include <netdb.h>
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
38 /*
39  * sysdb_mkdir_all:
40  * Recursively create the directory 'pathname' (similar to 'mkdir -p' on the
41  * command line) using file permissions as specified by 'mode'.
42  *
43  * Returns:
44  *  - 0 on success
45  *  - a negative value else
46  */
47 int
48 sdb_mkdir_all(const char *pathname, mode_t mode);
50 /*
51  * sdb_remove_all:
52  * Recursively deletes the specified path from the filesystem.
53  *
54  * Returns:
55  *  - 0 on success
56  *  - a negative value else
57  */
58 int
59 sdb_remove_all(const char *pathname);
61 /*
62  * sdb_get_current_user:
63  * Returns the name of the current user. The string is allocated dynamically
64  * and has to be freed by the caller.
65  *
66  * Returns:
67  *  - the username on success
68  *  - NULL else
69  */
70 char *
71 sdb_get_current_user(void);
73 enum {
74         SDB_SELECTIN = 0,
75         SDB_SELECTOUT,
76         SDB_SELECTERR,
77 };
79 /*
80  * sdb_select:
81  * Wait for a file-descriptor to become ready for I/O operations of the
82  * specified type. This is a simple wrapper around the select() system call.
83  * The type argument may be any of the SDB_SELECT* constants.
84  *
85  * Returns:
86  *  - the number of file descriptors ready for I/O
87  *  - a negative value on error
88  */
89 int
90 sdb_select(int fd, int type);
92 /*
93  * sdb_write:
94  * Write a message to a file-descriptor. This is a simple wrapper around the
95  * write() system call ensuring that all data is written on success.
96  *
97  * Returns:
98  *  - the number of bytes written
99  *  - a negative value on error
100  */
101 ssize_t
102 sdb_write(int fd, size_t msg_len, const void *msg);
104 enum {
105         SDB_NET_TCP = 1 << 0,
106         SDB_NET_UDP = 1 << 1,
107         SDB_NET_IP  = SDB_NET_TCP | SDB_NET_UDP,
109         SDB_NET_V4  = 1 << 2,
110         SDB_NET_V6  = 1 << 3,
111 };
113 /*
114  * sdb_resolve:
115  * Resolve the specified address on the specified network which may be a
116  * bitwise OR of the SDB_NET constants. The network addresses are returned in
117  * the list pointed to by 'res'. The list is allocated dynamically and has to
118  * be freed using freeaddrinfo().
119  *
120  * Returns:
121  *  - zero on success
122  *  - an error code else; use gai_strerror() to translate the error into a
123  *    human readable string
124  */
125 int
126 sdb_resolve(int network, const char *address, struct addrinfo **res);
128 #ifdef __cplusplus
129 } /* extern "C" */
130 #endif
132 #endif /* ! SDB_UTILS_OS_H */
134 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */