Code

os utils: Fixed a typo in the UDP resolver implementation.
[sysdb.git] / src / utils / os.c
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/socket.h>
39 #include <sys/stat.h>
41 #include <dirent.h>
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <unistd.h>
48 #include <libgen.h>
49 #include <netdb.h>
50 #include <pwd.h>
52 /*
53  * public API
54  */
56 int
57 sdb_mkdir_all(const char *pathname, mode_t mode)
58 {
59         struct stat st;
60         char *pathname_copy;
61         char *base_dir;
63         int status = 0;
65         if ((! pathname) || (! *pathname)) {
66                 errno = EINVAL;
67                 return -1;
68         }
70         memset(&st, 0, sizeof(st));
71         if (! stat(pathname, &st)) {
72                 if (! S_ISDIR(st.st_mode)) {
73                         errno = ENOTDIR;
74                         return -1;
75                 }
76                 return 0;
77         }
79         if (errno != ENOENT)
80                 /* pathname exists but we cannot access it */
81                 return -1;
83         pathname_copy = strdup(pathname);
84         if (! pathname_copy)
85                 return -1;
86         base_dir = dirname(pathname_copy);
88         status = sdb_mkdir_all(base_dir, mode);
89         if (! status)
90                 status = mkdir(pathname, mode);
92         free(pathname_copy);
93         return status;
94 } /* sdb_mkdir_all */
96 int
97 sdb_remove_all(const char *pathname)
98 {
99         struct stat st;
101         if ((! pathname) || (! *pathname)) {
102                 errno = EINVAL;
103                 return -1;
104         }
106         memset(&st, 0, sizeof(st));
107         if (stat(pathname, &st))
108                 return -1;
110         if (S_ISDIR(st.st_mode)) {
111                 DIR *d = opendir(pathname);
113                 if (! d)
114                         return -1;
116                 while (42) {
117                         struct dirent de;
118                         struct dirent *res = NULL;
120                         char filename[strlen(pathname) + sizeof(de.d_name) + 2];
122                         memset(&de, 0, sizeof(de));
123                         if (readdir_r(d, &de, &res)) {
124                                 closedir(d);
125                                 return -1;
126                         }
128                         if (! res)
129                                 break;
131                         if ((de.d_name[0] == '.') && ((de.d_name[1] == '\0')
132                                                 || ((de.d_name[1] == '.')&& (de.d_name[2] == '\0'))))
133                                 continue;
135                         snprintf(filename, sizeof(filename),
136                                         "%s/%s", pathname, de.d_name);
137                         if (sdb_remove_all(filename)) {
138                                 closedir(d);
139                                 return -1;
140                         }
141                 }
142                 closedir(d);
143         }
144         return remove(pathname);
145 } /* sdb_remove_all */
147 char *
148 sdb_get_current_user(void)
150         struct passwd pw_entry;
151         struct passwd *result = NULL;
153         uid_t uid;
155         char buf[1024];
156         int status;
158         uid = geteuid();
159         memset(&pw_entry, 0, sizeof(pw_entry));
160         status = getpwuid_r(uid, &pw_entry, buf, sizeof(buf), &result);
162         if (status || (! result)) {
163                 char errbuf[1024];
164                 sdb_log(SDB_LOG_ERR, "Failed to determine current username: %s",
165                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
166                 return NULL;
167         }
168         return strdup(result->pw_name);
169 } /* sdb_get_current_user */
171 int
172 sdb_select(int fd, int type)
174         fd_set fds;
175         fd_set *readfds = NULL;
176         fd_set *writefds = NULL;
177         fd_set *exceptfds = NULL;
179         if (fd < 0) {
180                 errno = EBADF;
181                 return -1;
182         }
184         FD_ZERO(&fds);
186         switch (type) {
187                 case SDB_SELECTIN:
188                         readfds = &fds;
189                         break;
190                 case SDB_SELECTOUT:
191                         writefds = &fds;
192                         break;
193                 case SDB_SELECTERR:
194                         exceptfds = &fds;
195                         break;
196                 default:
197                         errno = EINVAL;
198                         return -1;
199         }
201         FD_SET(fd, &fds);
203         while (42) {
204                 int n;
205                 errno = 0;
206                 n = select(fd + 1, readfds, writefds, exceptfds, NULL);
208                 if ((n < 0) && (errno != EINTR))
209                         return n;
210                 if (n > 0)
211                         break;
212         }
213         return 0;
214 } /* sdb_select */
216 ssize_t
217 sdb_write(int fd, size_t msg_len, const void *msg)
219         const char *buf;
220         size_t len;
222         if ((fd < 0) || (msg_len && (! msg)))
223                 return -1;
224         if (! msg_len)
225                 return 0;
227         buf = msg;
228         len = msg_len;
229         while (len > 0) {
230                 ssize_t status;
232                 if (sdb_select(fd, SDB_SELECTOUT))
233                         return -1;
235                 errno = 0;
236                 status = write(fd, buf, len);
237                 if (status < 0) {
238                         if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
239                                 continue;
240                         if (errno == EINTR)
241                                 continue;
243                         return status;
244                 }
246                 len -= (size_t)status;
247                 buf += status;
248         }
250         return (ssize_t)msg_len;
251 } /* sdb_write */
253 int
254 sdb_resolve(int network, const char *address, struct addrinfo **res)
256         struct addrinfo ai_hints;
257         const char *host;
258         char *port;
259         int status;
261         if (! res) {
262                 errno = EINVAL;
263                 return EAI_SYSTEM;
264         }
266         if (address) {
267                 host = address;
268                 port = strchr(host, ':');
269                 if (port) {
270                         *port = '\0';
271                         ++port;
272                 }
273                 if (! *host)
274                         host = NULL;
275         }
276         else {
277                 host = NULL;
278                 port = NULL;
279         }
281         memset(&ai_hints, 0, sizeof(ai_hints));
282         ai_hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
283         if (network & SDB_NET_V4)
284                 ai_hints.ai_family = AF_INET;
285         else if (network & SDB_NET_V6)
286                 ai_hints.ai_family = AF_INET6;
287         else
288                 ai_hints.ai_family = AF_UNSPEC;
290         if ((network & SDB_NET_IP) == SDB_NET_IP) {
291                 ai_hints.ai_socktype = 0;
292                 ai_hints.ai_protocol = 0;
293         }
294         else if (network & SDB_NET_TCP) {
295                 ai_hints.ai_socktype = SOCK_STREAM;
296                 ai_hints.ai_protocol = IPPROTO_TCP;
297         }
298         else if (network & SDB_NET_UDP) {
299                 ai_hints.ai_socktype = SOCK_DGRAM;
300                 ai_hints.ai_protocol = IPPROTO_UDP;
301         }
303         status = getaddrinfo(host, port, &ai_hints, res);
304         if (port) {
305                 --port;
306                 *port = ':';
307         }
308         return status;
309 } /* sdb_resolve */
311 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */