1 /*
2 * SysDB - src/utils/os.c
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 #if HAVE_CONFIG_H
29 # include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "utils/os.h"
33 #include "utils/error.h"
35 #include <errno.h>
37 #include <sys/types.h>
38 #include <sys/select.h>
39 #include <sys/socket.h>
40 #include <sys/stat.h>
41 #include <sys/time.h>
42 #include <sys/types.h>
44 #include <dirent.h>
46 #include <stdlib.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <unistd.h>
51 #include <libgen.h>
52 #include <netdb.h>
53 #include <pwd.h>
54 #include <time.h>
56 /*
57 * public API
58 */
60 char *
61 sdb_get_homedir(void)
62 {
63 char *username = sdb_get_current_user();
65 struct passwd pw_entry;
66 struct passwd *result = NULL;
68 char buf[4096];
70 int status;
72 if (username) {
73 memset(&pw_entry, 0, sizeof(pw_entry));
74 status = getpwnam_r(username, &pw_entry, buf, sizeof(buf), &result);
75 }
76 else
77 status = -1;
79 if (status || (! result)) {
80 char errbuf[1024];
81 sdb_log(SDB_LOG_WARNING, "os: Failed to determine home directory "
82 "for user %s: %s", username,
83 sdb_strerror(errno, errbuf, sizeof(errbuf)));
84 free(username);
85 return NULL;
86 }
87 free(username);
88 return strdup(result->pw_dir);
89 } /* sdb_get_homedir */
91 char *
92 sdb_realpath(const char *path)
93 {
94 if (! path)
95 return NULL;
97 if ((strlen(path) >= 2) && (path[0] == '~') && (path[1] == '/')) {
98 char *homedir = sdb_get_homedir();
99 char tmp[(homedir ? strlen(homedir) : 0) + strlen(path)];
100 char *ret;
102 if (! homedir)
103 return NULL;
105 snprintf(tmp, sizeof(tmp), "%s/%s", homedir, path + 2);
106 ret = realpath(tmp, NULL);
107 free(homedir);
108 return ret;
109 }
111 return realpath(path, NULL);
112 } /* sdb_realpath */
114 int
115 sdb_mkdir_all(const char *pathname, mode_t mode)
116 {
117 struct stat st;
118 char *pathname_copy;
119 char *base_dir;
121 int status = 0;
123 if ((! pathname) || (! *pathname)) {
124 errno = EINVAL;
125 return -1;
126 }
128 memset(&st, 0, sizeof(st));
129 if (! stat(pathname, &st)) {
130 if (! S_ISDIR(st.st_mode)) {
131 errno = ENOTDIR;
132 return -1;
133 }
134 return 0;
135 }
137 if (errno != ENOENT)
138 /* pathname exists but we cannot access it */
139 return -1;
141 pathname_copy = strdup(pathname);
142 if (! pathname_copy)
143 return -1;
144 base_dir = dirname(pathname_copy);
146 status = sdb_mkdir_all(base_dir, mode);
147 if (! status)
148 status = mkdir(pathname, mode);
150 free(pathname_copy);
151 return status;
152 } /* sdb_mkdir_all */
154 int
155 sdb_remove_all(const char *pathname)
156 {
157 struct stat st;
159 if ((! pathname) || (! *pathname)) {
160 errno = EINVAL;
161 return -1;
162 }
164 memset(&st, 0, sizeof(st));
165 if (stat(pathname, &st))
166 return -1;
168 if (S_ISDIR(st.st_mode)) {
169 DIR *d = opendir(pathname);
171 if (! d)
172 return -1;
174 while (42) {
175 struct dirent *de;
176 char filename[strlen(pathname) + sizeof(de->d_name) + 2];
178 errno = 0;
179 de = readdir(d);
180 if (! de) {
181 if (errno == 0)
182 break;
184 closedir(d);
185 return -1;
186 }
188 if ((de->d_name[0] == '.') && ((de->d_name[1] == '\0')
189 || ((de->d_name[1] == '.')&& (de->d_name[2] == '\0'))))
190 continue;
192 snprintf(filename, sizeof(filename),
193 "%s/%s", pathname, de->d_name);
194 if (sdb_remove_all(filename)) {
195 closedir(d);
196 return -1;
197 }
198 }
199 closedir(d);
200 }
201 return remove(pathname);
202 } /* sdb_remove_all */
204 char *
205 sdb_get_current_user(void)
206 {
207 struct passwd pw_entry;
208 struct passwd *result = NULL;
210 uid_t uid;
212 char buf[4096];
213 int status;
215 uid = geteuid();
216 memset(&pw_entry, 0, sizeof(pw_entry));
217 status = getpwuid_r(uid, &pw_entry, buf, sizeof(buf), &result);
219 if (status || (! result)) {
220 char errbuf[1024];
221 sdb_log(SDB_LOG_ERR, "Failed to determine current username: %s",
222 sdb_strerror(errno, errbuf, sizeof(errbuf)));
223 return NULL;
224 }
225 return strdup(result->pw_name);
226 } /* sdb_get_current_user */
228 int
229 sdb_select(int fd, int type)
230 {
231 fd_set fds;
232 fd_set *readfds = NULL;
233 fd_set *writefds = NULL;
234 fd_set *exceptfds = NULL;
236 if (fd < 0) {
237 errno = EBADF;
238 return -1;
239 }
241 FD_ZERO(&fds);
243 switch (type) {
244 case SDB_SELECTIN:
245 readfds = &fds;
246 break;
247 case SDB_SELECTOUT:
248 writefds = &fds;
249 break;
250 case SDB_SELECTERR:
251 exceptfds = &fds;
252 break;
253 default:
254 errno = EINVAL;
255 return -1;
256 }
258 FD_SET(fd, &fds);
260 while (42) {
261 int n;
262 errno = 0;
263 n = select(fd + 1, readfds, writefds, exceptfds, NULL);
265 if ((n < 0) && (errno != EINTR))
266 return n;
267 if (n > 0)
268 break;
269 }
270 return 0;
271 } /* sdb_select */
273 ssize_t
274 sdb_write(int fd, size_t msg_len, const void *msg)
275 {
276 const char *buf;
277 size_t len;
279 if ((fd < 0) || (msg_len && (! msg)))
280 return -1;
281 if (! msg_len)
282 return 0;
284 buf = msg;
285 len = msg_len;
286 while (len > 0) {
287 ssize_t status;
289 if (sdb_select(fd, SDB_SELECTOUT))
290 return -1;
292 errno = 0;
293 status = write(fd, buf, len);
294 if (status < 0) {
295 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
296 continue;
297 if (errno == EINTR)
298 continue;
300 return status;
301 }
303 len -= (size_t)status;
304 buf += status;
305 }
307 return (ssize_t)msg_len;
308 } /* sdb_write */
310 int
311 sdb_resolve(int network, const char *address, struct addrinfo **res)
312 {
313 struct addrinfo ai_hints;
314 const char *host;
315 char *port;
316 int status;
318 if (! res) {
319 errno = EINVAL;
320 return EAI_SYSTEM;
321 }
323 if (address) {
324 host = address;
325 port = strrchr(host, ':');
326 if (port) {
327 *port = '\0';
328 ++port;
329 }
330 if (! *host)
331 host = NULL;
332 }
333 else {
334 host = NULL;
335 port = NULL;
336 }
338 memset(&ai_hints, 0, sizeof(ai_hints));
339 ai_hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
340 if (network & SDB_NET_V4)
341 ai_hints.ai_family = AF_INET;
342 else if (network & SDB_NET_V6)
343 ai_hints.ai_family = AF_INET6;
344 else
345 ai_hints.ai_family = AF_UNSPEC;
347 if ((network & SDB_NET_IP) == SDB_NET_IP) {
348 ai_hints.ai_socktype = 0;
349 ai_hints.ai_protocol = 0;
350 }
351 else if (network & SDB_NET_TCP) {
352 ai_hints.ai_socktype = SOCK_STREAM;
353 ai_hints.ai_protocol = IPPROTO_TCP;
354 }
355 else if (network & SDB_NET_UDP) {
356 ai_hints.ai_socktype = SOCK_DGRAM;
357 ai_hints.ai_protocol = IPPROTO_UDP;
358 }
360 status = getaddrinfo(host, port, &ai_hints, res);
361 if (port) {
362 --port;
363 *port = ':';
364 }
365 return status;
366 } /* sdb_resolve */
368 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */