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 * sdb_realpath:
52 * Returns the canonicalized absolute pathname for the specified path. The
53 * function expands all symbolic links and resolves references to '.', '..',
54 * and extra slash characters (/).
55 *
56 * '~/' at the start of the string will be replaced by the current user's home
57 * directory.
58 *
59 * Returns:
60 * - the canonicalized absolute pathname on success
61 * - NULL else
62 */
63 char *
64 sdb_realpath(const char *path);
66 /*
67 * sysdb_mkdir_all:
68 * Recursively create the directory 'pathname' (similar to 'mkdir -p' on the
69 * command line) using file permissions as specified by 'mode'.
70 *
71 * Returns:
72 * - 0 on success
73 * - a negative value else
74 */
75 int
76 sdb_mkdir_all(const char *pathname, mode_t mode);
78 /*
79 * sdb_remove_all:
80 * Recursively deletes the specified path from the filesystem.
81 *
82 * Returns:
83 * - 0 on success
84 * - a negative value else
85 */
86 int
87 sdb_remove_all(const char *pathname);
89 /*
90 * sdb_get_current_user:
91 * Returns the name of the current user. The string is allocated dynamically
92 * and has to be freed by the caller.
93 *
94 * Returns:
95 * - the username on success
96 * - NULL else
97 */
98 char *
99 sdb_get_current_user(void);
101 enum {
102 SDB_SELECTIN = 0,
103 SDB_SELECTOUT,
104 SDB_SELECTERR,
105 };
107 /*
108 * sdb_select:
109 * Wait for a file-descriptor to become ready for I/O operations of the
110 * specified type. This is a simple wrapper around the select() system call.
111 * The type argument may be any of the SDB_SELECT* constants.
112 *
113 * Returns:
114 * - the number of file descriptors ready for I/O
115 * - a negative value on error
116 */
117 int
118 sdb_select(int fd, int type);
120 /*
121 * sdb_write:
122 * Write a message to a file-descriptor. This is a simple wrapper around the
123 * write() system call ensuring that all data is written on success.
124 *
125 * Returns:
126 * - the number of bytes written
127 * - a negative value on error
128 */
129 ssize_t
130 sdb_write(int fd, size_t msg_len, const void *msg);
132 enum {
133 SDB_NET_TCP = 1 << 0,
134 SDB_NET_UDP = 1 << 1,
135 SDB_NET_IP = SDB_NET_TCP | SDB_NET_UDP,
137 SDB_NET_V4 = 1 << 2,
138 SDB_NET_V6 = 1 << 3,
139 };
141 /*
142 * sdb_resolve:
143 * Resolve the specified address on the specified network which may be a
144 * bitwise OR of the SDB_NET constants. The network addresses are returned in
145 * the list pointed to by 'res'. The list is allocated dynamically and has to
146 * be freed using freeaddrinfo().
147 *
148 * Returns:
149 * - zero on success
150 * - an error code else; use gai_strerror() to translate the error into a
151 * human readable string
152 */
153 int
154 sdb_resolve(int network, const char *address, struct addrinfo **res);
156 #ifdef __cplusplus
157 } /* extern "C" */
158 #endif
160 #endif /* ! SDB_UTILS_OS_H */
162 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */