]> git.tuebingen.mpg.de Git - paraslash.git/blob - fd.c
6bd21b5bda7411d7f4444e1c985a5c661ccadec2
[paraslash.git] / fd.c
1 /*
2  * Copyright (C) 2006-2007 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file fd.c helper functions for file descriptor handling */
8
9 #include <sys/types.h>
10 #include <dirent.h>
11 #include <sys/mman.h>
12 #include <fcntl.h>
13 #include <sys/select.h>
14
15 #include "para.h"
16 #include "error.h"
17 /**
18  * check whether a file exists
19  *
20  * \param fn the file name
21  *
22  * \return Non-zero iff file exists.
23  */
24 int file_exists(const char *fn)
25 {
26         struct stat statbuf;
27
28         return !stat(fn, &statbuf);
29 }
30
31 /**
32  * paraslash's wrapper for select(2)
33  *
34  * It calls select(2) (with no exceptfds) and starts over if select() was
35  * interrupted by a signal.
36  *
37  * \param n the highest-numbered descriptor in any of the two sets, plus 1
38  * \param readfds fds that should be checked for readability
39  * \param writefds fds that should be checked for writablility
40  * \param timeout_tv upper bound on the amount of time elapsed before select()
41  * returns
42  *
43  * \return The return value of the underlying select() call.
44  *
45  * All arguments are passed verbatim to select(2).
46  * \sa select(2) select_tut(2)
47  */
48 int para_select(int n, fd_set *readfds, fd_set *writefds,
49                 struct timeval *timeout_tv)
50 {
51         int ret, err;
52         do {
53                 ret = select(n, readfds, writefds, NULL, timeout_tv);
54                 err = errno;
55         } while (ret < 0 && err == EINTR);
56         if (ret < 0)
57                 PARA_CRIT_LOG("select error: %s, max_fileno: %d\n",
58                         strerror(err), n);
59         return ret;
60 }
61
62 /**
63  * Set a file descriptor to non-blocking mode.
64  *
65  * \param fd The file descriptor.
66  *
67  * \return Standard.
68  */
69 int mark_fd_nonblock(int fd)
70 {
71         int flags = fcntl(fd, F_GETFL);
72         if (flags < 0)
73                 return -ERRNO_TO_PARA_ERROR(errno);
74         flags = fcntl(fd, F_SETFL, ((long)flags) | O_NONBLOCK);
75         if (flags < 0)
76                 return -ERRNO_TO_PARA_ERROR(errno);
77         return 1;
78 }
79
80 /**
81  * set a file descriptor in a fd_set
82  *
83  * \param fd the file descriptor to be set
84  * \param fds the file descriptor set
85  * \param max_fileno highest-numbered file descriptor
86  *
87  * This wrapper for FD_SET() passes its first two arguments to \p FD_SET. Upon
88  * return, \a max_fileno contains the maximum of the old_value and \a fd.
89  *
90  * \sa para_select
91 */
92 void para_fd_set(int fd, fd_set *fds, int *max_fileno)
93 {
94
95         if (fd < 0 || fd >= FD_SETSIZE) {
96                 PARA_EMERG_LOG("fatal: tried to add invalid fd %d\n", fd);
97                 exit(EXIT_FAILURE);
98         }
99 #if 0
100         {
101                 int flags = fcntl(fd, F_GETFL);
102                 if (!(flags & O_NONBLOCK)) {
103                         PARA_EMERG_LOG("fd %d is a blocking file descriptor\n", fd);
104                         exit(EXIT_FAILURE);
105                 }
106         }
107 #endif
108         FD_SET(fd, fds);
109         *max_fileno = PARA_MAX(*max_fileno, fd);
110 }
111
112 /**
113 * paraslash's wrapper for fgets(3)
114 * \param line pointer to the buffer to store the line
115 * \param size the size of the buffer given by \a line
116 * \param f the stream to read from
117 *
118 * \return Unlike the standard fgets() function, an integer value
119 * is returned. On success, this function returns 1. On errors, -E_FGETS
120 * is returned. A zero return value indicates an end of file condition.
121 */
122 __must_check int para_fgets(char *line, int size, FILE *f)
123 {
124 again:
125         if (fgets(line, size, f))
126                 return 1;
127         if (feof(f))
128                 return 0;
129         if (!ferror(f))
130                 return -E_FGETS;
131         if (errno != EINTR) {
132                 PARA_ERROR_LOG("%s\n", strerror(errno));
133                 return -E_FGETS;
134         }
135         clearerr(f);
136         goto again;
137 }
138
139 /**
140  * Paraslash's wrapper for mmap.
141  *
142  * \param length number of bytes to mmap
143  * \param prot either PROT_NONE or the bitwise OR of one or more of
144  * PROT_EXEC PROT_READ PROT_WRITE
145  * \param flags exactly one of MAP_SHARED and MAP_PRIVATE
146  * \param fd the file to mmap from
147  * \param offset mmap start
148  *
149  * \return This function either returns a valid pointer to the mapped area
150  * or calls exit() on errors.
151  */
152 void *para_mmap(size_t length, int prot, int flags, int fd, off_t offset)
153 {
154         void *ret = mmap(NULL, length, prot, flags, fd, offset);
155         if (ret != MAP_FAILED)
156                 return ret;
157         PARA_EMERG_LOG("mmap failed: %s\n", strerror(errno));
158         PARA_EMERG_LOG("length: %zu, flags: %d, fd: %d, offset: %zu\n",
159                 length, flags, fd, (size_t)offset);
160         exit(EXIT_FAILURE);
161 }
162
163 /**
164  * Wrapper for the open(2) system call.
165  *
166  * \param path The filename.
167  * \param flags The usual open(2) flags.
168  * \param mode Specifies the permissions to use.
169  *
170  * The mode parameter must be specified when O_CREAT is in the flags, and is ignored
171  * otherwise.
172  *
173  * \return Positive on success, negative on errors.
174  *
175  * \sa open(2).
176  */
177 int para_open(const char *path, int flags, mode_t mode)
178 {
179         int ret = open(path, flags, mode);
180
181         if (ret >= 0)
182                 return ret;
183         switch (errno) {
184         case EEXIST:
185                 ret = -E_EXIST;
186                 break;
187         case EISDIR:
188                 ret = -E_ISDIR;
189                 break;
190         case ENOENT:
191                 ret = -E_NOENT;
192                 break;
193         case EPERM:
194                 ret = -E_OPEN_PERM;
195                 break;
196         };
197         PARA_ERROR_LOG("failed to open %s: %s\n", path, strerror(errno));
198         return -E_OPEN;
199 }
200
201 /**
202  * Wrapper for chdir(2).
203  *
204  * \param path the specified directory.
205  *
206  * \return Positive on success, negative on errors.
207  */
208 int para_chdir(const char *path)
209 {
210         int ret = chdir(path);
211
212         if (ret >= 0)
213                 return 1;
214         switch (errno) {
215         case ENOENT:
216                 return -E_NOENT;
217         case EACCES:
218                 return -E_CHDIR_PERM;
219         };
220         return -E_CHDIR;
221 }
222
223 /**
224  * Save the cwd and open a given directory.
225  *
226  * \param dirname Path to the directory to open.
227  * \param dir Result pointer.
228  * \param cwd File descriptor of the current working directory.
229  *
230  * \return Positive on success, negative on errors.
231  *
232  * Opening the current directory (".") and calling fchdir() to return is
233  * usually faster and more reliable than saving cwd in some buffer and calling
234  * chdir() afterwards.
235  *
236  * If \a cwd is not \p NULL "." is opened and the resulting file descriptor is
237  * stored in \a cwd. If the function returns success, and \a cwd is not \p
238  * NULL, the caller must close this file descriptor (probably after calling
239  * fchdir(*cwd)).
240  *
241  * On errors, the function undos everything, so the caller needs neither close
242  * any files, nor change back to the original working directory.
243  *
244  * \sa getcwd(3).
245  *
246  */
247 int para_opendir(const char *dirname, DIR **dir, int *cwd)
248 {
249         int ret;
250
251         if (cwd) {
252                 ret = para_open(".", O_RDONLY, 0);
253                 if (ret < 0)
254                         return ret;
255                 *cwd = ret;
256         }
257         ret = para_chdir(dirname);
258         if (ret < 0)
259                 goto close_cwd;
260         ret = -E_OPENDIR;
261         *dir = opendir(".");
262         if (!*dir)
263                 goto change_to_orig_dir;
264         return 1;
265 /* Ignore return value of fchdir() and close(). We're busted anyway. */
266 change_to_orig_dir:
267         if (cwd)
268                 fchdir(*cwd);
269 close_cwd:
270         if (cwd)
271                 close(*cwd);
272         return ret;
273 }
274
275 /**
276  * A wrapper for fchdir().
277  *
278  * \param fd An open file descriptor
279  *
280  * \return Positive on success, negative on errors.
281  */
282 int para_fchdir(int fd)
283 {
284         if (fchdir(fd) < 0)
285                 return -E_FCHDIR;
286         return 1;
287 }
288
289 /**
290  * A wrapper for mkdir(2).
291  *
292  * \param path Name of the directory to create.
293  * \param mode The permissions to use.
294  *
295  * \return positive on success, negative on errors.
296  */
297 int para_mkdir(const char *path, mode_t mode)
298 {
299         if (!mkdir(path, mode))
300                 return 1;
301         if (errno == EEXIST)
302                 return -E_EXIST;
303         if (errno == ENOSPC)
304                 return -E_NOSPC;
305         if (errno == ENOTDIR)
306                 return -E_NOTDIR;
307         if (errno == EPERM)
308                 return -E_MKDIR_PERM;
309         return -E_MKDIR;
310 }