Code

Moved sdb_proto_send/sdb_proto_select to sdb_write/sdb_select.
[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/stat.h>
40 #include <dirent.h>
42 #include <stdlib.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <unistd.h>
47 #include <libgen.h>
48 #include <pwd.h>
50 /*
51  * public API
52  */
54 int
55 sdb_mkdir_all(const char *pathname, mode_t mode)
56 {
57         struct stat st;
58         char *pathname_copy;
59         char *base_dir;
61         int status = 0;
63         if ((! pathname) || (! *pathname)) {
64                 errno = EINVAL;
65                 return -1;
66         }
68         memset(&st, 0, sizeof(st));
69         if (! stat(pathname, &st)) {
70                 if (! S_ISDIR(st.st_mode)) {
71                         errno = ENOTDIR;
72                         return -1;
73                 }
74                 return 0;
75         }
77         if (errno != ENOENT)
78                 /* pathname exists but we cannot access it */
79                 return -1;
81         pathname_copy = strdup(pathname);
82         if (! pathname_copy)
83                 return -1;
84         base_dir = dirname(pathname_copy);
86         status = sdb_mkdir_all(base_dir, mode);
87         if (! status)
88                 status = mkdir(pathname, mode);
90         free(pathname_copy);
91         return status;
92 } /* sdb_mkdir_all */
94 int
95 sdb_remove_all(const char *pathname)
96 {
97         struct stat st;
99         if ((! pathname) || (! *pathname)) {
100                 errno = EINVAL;
101                 return -1;
102         }
104         memset(&st, 0, sizeof(st));
105         if (stat(pathname, &st))
106                 return -1;
108         if (S_ISDIR(st.st_mode)) {
109                 DIR *d = opendir(pathname);
111                 if (! d)
112                         return -1;
114                 while (42) {
115                         struct dirent de;
116                         struct dirent *res = NULL;
118                         char filename[strlen(pathname) + sizeof(de.d_name) + 2];
120                         memset(&de, 0, sizeof(de));
121                         if (readdir_r(d, &de, &res)) {
122                                 closedir(d);
123                                 return -1;
124                         }
126                         if (! res)
127                                 break;
129                         if ((de.d_name[0] == '.') && ((de.d_name[1] == '\0')
130                                                 || ((de.d_name[1] == '.')&& (de.d_name[2] == '\0'))))
131                                 continue;
133                         snprintf(filename, sizeof(filename),
134                                         "%s/%s", pathname, de.d_name);
135                         if (sdb_remove_all(filename)) {
136                                 closedir(d);
137                                 return -1;
138                         }
139                 }
140                 closedir(d);
141         }
142         return remove(pathname);
143 } /* sdb_remove_all */
145 char *
146 sdb_get_current_user(void)
148         struct passwd pw_entry;
149         struct passwd *result = NULL;
151         uid_t uid;
153         char buf[1024];
154         int status;
156         uid = geteuid();
157         memset(&pw_entry, 0, sizeof(pw_entry));
158         status = getpwuid_r(uid, &pw_entry, buf, sizeof(buf), &result);
160         if (status || (! result)) {
161                 char errbuf[1024];
162                 sdb_log(SDB_LOG_ERR, "Failed to determine current username: %s",
163                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
164                 return NULL;
165         }
166         return strdup(result->pw_name);
167 } /* sdb_get_current_user */
169 int
170 sdb_select(int fd, int type)
172         fd_set fds;
173         fd_set *readfds = NULL;
174         fd_set *writefds = NULL;
175         fd_set *exceptfds = NULL;
177         if (fd < 0) {
178                 errno = EBADF;
179                 return -1;
180         }
182         FD_ZERO(&fds);
184         switch (type) {
185                 case SDB_SELECTIN:
186                         readfds = &fds;
187                         break;
188                 case SDB_SELECTOUT:
189                         writefds = &fds;
190                         break;
191                 case SDB_SELECTERR:
192                         exceptfds = &fds;
193                         break;
194                 default:
195                         errno = EINVAL;
196                         return -1;
197         }
199         FD_SET(fd, &fds);
201         while (42) {
202                 int n;
203                 errno = 0;
204                 n = select(fd + 1, readfds, writefds, exceptfds, NULL);
206                 if ((n < 0) && (errno != EINTR))
207                         return n;
208                 if (n > 0)
209                         break;
210         }
211         return 0;
212 } /* sdb_select */
214 ssize_t
215 sdb_write(int fd, size_t msg_len, const void *msg)
217         const char *buf;
218         size_t len;
220         if ((fd < 0) || (msg_len && (! msg)))
221                 return -1;
222         if (! msg_len)
223                 return 0;
225         buf = msg;
226         len = msg_len;
227         while (len > 0) {
228                 ssize_t status;
230                 if (sdb_select(fd, SDB_SELECTOUT))
231                         return -1;
233                 errno = 0;
234                 status = write(fd, buf, len);
235                 if (status < 0) {
236                         if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
237                                 continue;
238                         if (errno == EINTR)
239                                 continue;
241                         return status;
242                 }
244                 len -= (size_t)status;
245                 buf += status;
246         }
248         return (ssize_t)msg_len;
249 } /* sdb_write */
251 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */