afs.cmd: Beautify help texts.
[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  * \returns 1 on success, -E_F_GETFL, -E_F_SETFL, on errors.
68  */
69 int mark_fd_nonblock(int fd)
70 {
71         int flags = fcntl(fd, F_GETFL);
72         if (flags < 0)
73                 return -E_F_GETFL;
74         if (fcntl(fd, F_SETFL, ((long)flags) | O_NONBLOCK) < 0)
75                 return -E_F_SETFL;
76         return 1;
77 }
78
79 /**
80  * set a file descriptor in a fd_set
81  *
82  * \param fd the file descriptor to be set
83  * \param fds the file descriptor set
84  * \param max_fileno highest-numbered file descriptor
85  *
86  * This wrapper for FD_SET() passes its first two arguments to \p FD_SET. Upon
87  * return, \a max_fileno contains the maximum of the old_value and \a fd.
88  *
89  * \sa para_select
90 */
91 void para_fd_set(int fd, fd_set *fds, int *max_fileno)
92 {
93
94         if (fd < 0 || fd >= FD_SETSIZE) {
95                 PARA_EMERG_LOG("fatal: tried to add invalid fd %d\n", fd);
96                 exit(EXIT_FAILURE);
97         }
98 #if 0
99         {
100                 int flags = fcntl(fd, F_GETFL);
101                 if (!(flags & O_NONBLOCK)) {
102                         PARA_EMERG_LOG("fd %d is a blocking file descriptor\n", fd);
103                         exit(EXIT_FAILURE);
104                 }
105         }
106 #endif
107         FD_SET(fd, fds);
108         *max_fileno = PARA_MAX(*max_fileno, fd);
109 }
110
111 /**
112 * paraslash's wrapper for fgets(3)
113 * \param line pointer to the buffer to store the line
114 * \param size the size of the buffer given by \a line
115 * \param f the stream to read from
116 *
117 * \return Unlike the standard fgets() function, an integer value
118 * is returned. On success, this function returns 1. On errors, -E_FGETS
119 * is returned. A zero return value indicates an end of file condition.
120 */
121 __must_check int para_fgets(char *line, int size, FILE *f)
122 {
123 again:
124         if (fgets(line, size, f))
125                 return 1;
126         if (feof(f))
127                 return 0;
128         if (!ferror(f))
129                 return -E_FGETS;
130         if (errno != EINTR) {
131                 PARA_ERROR_LOG("%s\n", strerror(errno));
132                 return -E_FGETS;
133         }
134         clearerr(f);
135         goto again;
136 }
137
138 /**
139  * Paraslash's wrapper for mmap.
140  *
141  * \param length number of bytes to mmap
142  * \param prot either PROT_NONE or the bitwise OR of one or more of
143  * PROT_EXEC PROT_READ PROT_WRITE
144  * \param flags exactly one of MAP_SHARED and MAP_PRIVATE
145  * \param fd the file to mmap from
146  * \param offset mmap start
147  *
148  * \return This function either returns a valid pointer to the mapped area
149  * or calls exit() on errors.
150  */
151 void *para_mmap(size_t length, int prot, int flags, int fd, off_t offset)
152 {
153         void *ret = mmap(NULL, length, prot, flags, fd, offset);
154         if (ret != MAP_FAILED)
155                 return ret;
156         PARA_EMERG_LOG("mmap failed: %s\n", strerror(errno));
157         PARA_EMERG_LOG("length: %zu, flags: %d, fd: %d, offset: %zu\n",
158                 length, flags, fd, (size_t)offset);
159         exit(EXIT_FAILURE);
160 }
161
162 /**
163  * Wrapper for the open(2) system call.
164  *
165  * \param path The filename.
166  * \param flags The usual open(2) flags.
167  * \param mode Specifies the permissions to use.
168  *
169  * The mode parameter must be specified when O_CREAT is in the flags, and is ignored
170  * otherwise.
171  *
172  * \return Positive on success, negative on errors.
173  *
174  * \sa open(2).
175  */
176 int para_open(const char *path, int flags, mode_t mode)
177 {
178         int ret = open(path, flags, mode);
179
180         if (ret >= 0)
181                 return ret;
182         switch (errno) {
183         case EEXIST:
184                 ret = -E_EXIST;
185                 break;
186         case EISDIR:
187                 ret = -E_ISDIR;
188                 break;
189         case ENOENT:
190                 ret = -E_NOENT;
191                 break;
192         case EPERM:
193                 ret = -E_OPEN_PERM;
194                 break;
195         };
196         PARA_ERROR_LOG("failed to open %s: %s\n", path, strerror(errno));
197         return -E_OPEN;
198 }
199
200 /**
201  * Wrapper for chdir(2).
202  *
203  * \param path the specified directory.
204  *
205  * \return Positive on success, negative on errors.
206  */
207 int para_chdir(const char *path)
208 {
209         int ret = chdir(path);
210
211         if (ret >= 0)
212                 return 1;
213         switch (errno) {
214         case ENOENT:
215                 return -E_NOENT;
216         case EACCES:
217                 return -E_CHDIR_PERM;
218         };
219         return -E_CHDIR;
220 }
221
222 /**
223  * Save the cwd and open a given directory.
224  *
225  * \param dirname Path to the directory to open.
226  * \param dir Result pointer.
227  * \param cwd File descriptor of the current working directory.
228  *
229  * \return Positive on success, negative on errors.
230  *
231  * Opening the current directory (".") and calling fchdir() to return is
232  * usually faster and more reliable than saving cwd in some buffer and calling
233  * chdir() afterwards.
234  *
235  * If \a cwd is not \p NULL "." is opened and the resulting file descriptor is
236  * stored in \a cwd. If the function returns success, and \a cwd is not \p
237  * NULL, the caller must close this file descriptor (probably after calling
238  * fchdir(*cwd)).
239  *
240  * On errors, the function undos everything, so the caller needs neither close
241  * any files, nor change back to the original working directory.
242  *
243  * \sa getcwd(3).
244  *
245  */
246 int para_opendir(const char *dirname, DIR **dir, int *cwd)
247 {
248         int ret;
249
250         if (cwd) {
251                 ret = para_open(".", O_RDONLY, 0);
252                 if (ret < 0)
253                         return ret;
254                 *cwd = ret;
255         }
256         ret = para_chdir(dirname);
257         if (ret < 0)
258                 goto close_cwd;
259         ret = -E_OPENDIR;
260         *dir = opendir(".");
261         if (!*dir)
262                 goto change_to_orig_dir;
263         return 1;
264 /* Ignore return value of fchdir() and close(). We're busted anyway. */
265 change_to_orig_dir:
266         if (cwd)
267                 fchdir(*cwd);
268 close_cwd:
269         if (cwd)
270                 close(*cwd);
271         return ret;
272 }
273
274 /**
275  * A wrapper for fchdir().
276  *
277  * \param fd An open file descriptor
278  *
279  * \return Positive on success, negative on errors.
280  */
281 int para_fchdir(int fd)
282 {
283         if (fchdir(fd) < 0)
284                 return -E_FCHDIR;
285         return 1;
286 }
287
288 /**
289  * A wrapper for mkdir(2).
290  *
291  * \param path Name of the directory to create.
292  * \param mode The permissions to use.
293  *
294  * \return positive on success, negative on errors.
295  */
296 int para_mkdir(const char *path, mode_t mode)
297 {
298         if (!mkdir(path, mode))
299                 return 1;
300         if (errno == EEXIST)
301                 return -E_EXIST;
302         if (errno == ENOSPC)
303                 return -E_NOSPC;
304         if (errno == ENOTDIR)
305                 return -E_NOTDIR;
306         if (errno == EPERM)
307                 return -E_MKDIR_PERM;
308         return -E_MKDIR;
309 }