8ab52c75bc023c2bc5b68ca61d2cc0f13ce0f916
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 struct dirent *res = NULL;
178 char filename[strlen(pathname) + sizeof(de.d_name) + 2];
180 memset(&de, 0, sizeof(de));
181 if (readdir_r(d, &de, &res)) {
182 closedir(d);
183 return -1;
184 }
186 if (! res)
187 break;
189 if ((de.d_name[0] == '.') && ((de.d_name[1] == '\0')
190 || ((de.d_name[1] == '.')&& (de.d_name[2] == '\0'))))
191 continue;
193 snprintf(filename, sizeof(filename),
194 "%s/%s", pathname, de.d_name);
195 if (sdb_remove_all(filename)) {
196 closedir(d);
197 return -1;
198 }
199 }
200 closedir(d);
201 }
202 return remove(pathname);
203 } /* sdb_remove_all */
205 char *
206 sdb_get_current_user(void)
207 {
208 struct passwd pw_entry;
209 struct passwd *result = NULL;
211 uid_t uid;
213 char buf[4096];
214 int status;
216 uid = geteuid();
217 memset(&pw_entry, 0, sizeof(pw_entry));
218 status = getpwuid_r(uid, &pw_entry, buf, sizeof(buf), &result);
220 if (status || (! result)) {
221 char errbuf[1024];
222 sdb_log(SDB_LOG_ERR, "Failed to determine current username: %s",
223 sdb_strerror(errno, errbuf, sizeof(errbuf)));
224 return NULL;
225 }
226 return strdup(result->pw_name);
227 } /* sdb_get_current_user */
229 int
230 sdb_select(int fd, int type)
231 {
232 fd_set fds;
233 fd_set *readfds = NULL;
234 fd_set *writefds = NULL;
235 fd_set *exceptfds = NULL;
237 if (fd < 0) {
238 errno = EBADF;
239 return -1;
240 }
242 FD_ZERO(&fds);
244 switch (type) {
245 case SDB_SELECTIN:
246 readfds = &fds;
247 break;
248 case SDB_SELECTOUT:
249 writefds = &fds;
250 break;
251 case SDB_SELECTERR:
252 exceptfds = &fds;
253 break;
254 default:
255 errno = EINVAL;
256 return -1;
257 }
259 FD_SET(fd, &fds);
261 while (42) {
262 int n;
263 errno = 0;
264 n = select(fd + 1, readfds, writefds, exceptfds, NULL);
266 if ((n < 0) && (errno != EINTR))
267 return n;
268 if (n > 0)
269 break;
270 }
271 return 0;
272 } /* sdb_select */
274 ssize_t
275 sdb_write(int fd, size_t msg_len, const void *msg)
276 {
277 const char *buf;
278 size_t len;
280 if ((fd < 0) || (msg_len && (! msg)))
281 return -1;
282 if (! msg_len)
283 return 0;
285 buf = msg;
286 len = msg_len;
287 while (len > 0) {
288 ssize_t status;
290 if (sdb_select(fd, SDB_SELECTOUT))
291 return -1;
293 errno = 0;
294 status = write(fd, buf, len);
295 if (status < 0) {
296 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
297 continue;
298 if (errno == EINTR)
299 continue;
301 return status;
302 }
304 len -= (size_t)status;
305 buf += status;
306 }
308 return (ssize_t)msg_len;
309 } /* sdb_write */
311 int
312 sdb_resolve(int network, const char *address, struct addrinfo **res)
313 {
314 struct addrinfo ai_hints;
315 const char *host;
316 char *port;
317 int status;
319 if (! res) {
320 errno = EINVAL;
321 return EAI_SYSTEM;
322 }
324 if (address) {
325 host = address;
326 port = strrchr(host, ':');
327 if (port) {
328 *port = '\0';
329 ++port;
330 }
331 if (! *host)
332 host = NULL;
333 }
334 else {
335 host = NULL;
336 port = NULL;
337 }
339 memset(&ai_hints, 0, sizeof(ai_hints));
340 ai_hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
341 if (network & SDB_NET_V4)
342 ai_hints.ai_family = AF_INET;
343 else if (network & SDB_NET_V6)
344 ai_hints.ai_family = AF_INET6;
345 else
346 ai_hints.ai_family = AF_UNSPEC;
348 if ((network & SDB_NET_IP) == SDB_NET_IP) {
349 ai_hints.ai_socktype = 0;
350 ai_hints.ai_protocol = 0;
351 }
352 else if (network & SDB_NET_TCP) {
353 ai_hints.ai_socktype = SOCK_STREAM;
354 ai_hints.ai_protocol = IPPROTO_TCP;
355 }
356 else if (network & SDB_NET_UDP) {
357 ai_hints.ai_socktype = SOCK_DGRAM;
358 ai_hints.ai_protocol = IPPROTO_UDP;
359 }
361 status = getaddrinfo(host, port, &ai_hints, res);
362 if (port) {
363 --port;
364 *port = ':';
365 }
366 return status;
367 } /* sdb_resolve */
369 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */