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 * sdb_get_homedir:
40 * Returns the home directory of the current user. The buffer to hold the
41 * return value is allocated dynamically and has to be freed by the caller.
42 *
43 * Returns:
44 * - the current user's home directory on success
45 * - NULL else
46 */
47 char *
48 sdb_get_homedir(void);
50 /*
51 * sysdb_mkdir_all:
52 * Recursively create the directory 'pathname' (similar to 'mkdir -p' on the
53 * command line) using file permissions as specified by 'mode'.
54 *
55 * Returns:
56 * - 0 on success
57 * - a negative value else
58 */
59 int
60 sdb_mkdir_all(const char *pathname, mode_t mode);
62 /*
63 * sdb_remove_all:
64 * Recursively deletes the specified path from the filesystem.
65 *
66 * Returns:
67 * - 0 on success
68 * - a negative value else
69 */
70 int
71 sdb_remove_all(const char *pathname);
73 /*
74 * sdb_get_current_user:
75 * Returns the name of the current user. The string is allocated dynamically
76 * and has to be freed by the caller.
77 *
78 * Returns:
79 * - the username on success
80 * - NULL else
81 */
82 char *
83 sdb_get_current_user(void);
85 enum {
86 SDB_SELECTIN = 0,
87 SDB_SELECTOUT,
88 SDB_SELECTERR,
89 };
91 /*
92 * sdb_select:
93 * Wait for a file-descriptor to become ready for I/O operations of the
94 * specified type. This is a simple wrapper around the select() system call.
95 * The type argument may be any of the SDB_SELECT* constants.
96 *
97 * Returns:
98 * - the number of file descriptors ready for I/O
99 * - a negative value on error
100 */
101 int
102 sdb_select(int fd, int type);
104 /*
105 * sdb_write:
106 * Write a message to a file-descriptor. This is a simple wrapper around the
107 * write() system call ensuring that all data is written on success.
108 *
109 * Returns:
110 * - the number of bytes written
111 * - a negative value on error
112 */
113 ssize_t
114 sdb_write(int fd, size_t msg_len, const void *msg);
116 enum {
117 SDB_NET_TCP = 1 << 0,
118 SDB_NET_UDP = 1 << 1,
119 SDB_NET_IP = SDB_NET_TCP | SDB_NET_UDP,
121 SDB_NET_V4 = 1 << 2,
122 SDB_NET_V6 = 1 << 3,
123 };
125 /*
126 * sdb_resolve:
127 * Resolve the specified address on the specified network which may be a
128 * bitwise OR of the SDB_NET constants. The network addresses are returned in
129 * the list pointed to by 'res'. The list is allocated dynamically and has to
130 * be freed using freeaddrinfo().
131 *
132 * Returns:
133 * - zero on success
134 * - an error code else; use gai_strerror() to translate the error into a
135 * human readable string
136 */
137 int
138 sdb_resolve(int network, const char *address, struct addrinfo **res);
140 #ifdef __cplusplus
141 } /* extern "C" */
142 #endif
144 #endif /* ! SDB_UTILS_OS_H */
146 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */