2 * Copyright (C) 2006-2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file fd.c Helper functions for file descriptor handling. */
13 #include <sys/select.h>
19 * Check whether a file exists.
21 * \param fn The file name.
23 * \return Non-zero iff file exists.
25 int file_exists(const char *fn)
29 return !stat(fn, &statbuf);
33 * Paraslash's wrapper for select(2).
35 * It calls select(2) (with no exceptfds) and starts over if select() was
36 * interrupted by a signal.
38 * \param n The highest-numbered descriptor in any of the two sets, plus 1.
39 * \param readfds fds that should be checked for readability.
40 * \param writefds fds that should be checked for writablility.
41 * \param timeout_tv upper bound on the amount of time elapsed before select()
44 * \return The return value of the underlying select() call on success, the
45 * negative system error code on errors.
47 * All arguments are passed verbatim to select(2).
48 * \sa select(2) select_tut(2).
50 int para_select(int n, fd_set *readfds, fd_set *writefds,
51 struct timeval *timeout_tv)
55 ret = select(n, readfds, writefds, NULL, timeout_tv);
57 } while (ret < 0 && err == EINTR);
59 return -ERRNO_TO_PARA_ERROR(errno);
64 * Set a file descriptor to non-blocking mode.
66 * \param fd The file descriptor.
70 int mark_fd_nonblock(int fd)
72 int flags = fcntl(fd, F_GETFL);
74 return -ERRNO_TO_PARA_ERROR(errno);
75 flags = fcntl(fd, F_SETFL, ((long)flags) | O_NONBLOCK);
77 return -ERRNO_TO_PARA_ERROR(errno);
82 * Set a file descriptor in a fd_set.
84 * \param fd The file descriptor to be set.
85 * \param fds The file descriptor set.
86 * \param max_fileno Highest-numbered file descriptor.
88 * This wrapper for FD_SET() passes its first two arguments to \p FD_SET. Upon
89 * return, \a max_fileno contains the maximum of the old_value and \a fd.
93 void para_fd_set(int fd, fd_set *fds, int *max_fileno)
96 if (fd < 0 || fd >= FD_SETSIZE) {
97 PARA_EMERG_LOG("fatal: tried to add invalid fd %d\n", fd);
102 int flags = fcntl(fd, F_GETFL);
103 if (!(flags & O_NONBLOCK)) {
104 PARA_EMERG_LOG("fd %d is a blocking file descriptor\n", fd);
110 *max_fileno = PARA_MAX(*max_fileno, fd);
114 * Paraslash's wrapper for fgets(3).
116 * \param line Pointer to the buffer to store the line.
117 * \param size The size of the buffer given by \a line.
118 * \param f The stream to read from.
120 * \return Unlike the standard fgets() function, an integer value
121 * is returned. On success, this function returns 1. On errors, -E_FGETS
122 * is returned. A zero return value indicates an end of file condition.
124 __must_check int para_fgets(char *line, int size, FILE *f)
127 if (fgets(line, size, f))
133 if (errno != EINTR) {
134 PARA_ERROR_LOG("%s\n", strerror(errno));
142 * Paraslash's wrapper for mmap.
144 * \param length Number of bytes to mmap.
145 * \param prot Either PROT_NONE or the bitwise OR of one or more of
146 * PROT_EXEC PROT_READ PROT_WRITE.
147 * \param flags Exactly one of MAP_SHARED and MAP_PRIVATE.
148 * \param fd The file to mmap from.
149 * \param offset Mmap start.
151 * \return This function either returns a valid pointer to the mapped area
152 * or calls exit() on errors.
154 void *para_mmap(size_t length, int prot, int flags, int fd, off_t offset)
156 void *ret = mmap(NULL, length, prot, flags, fd, offset);
157 if (ret != MAP_FAILED)
159 PARA_EMERG_LOG("mmap failed: %s\n", strerror(errno));
160 PARA_EMERG_LOG("length: %zu, flags: %d, fd: %d, offset: %zu\n",
161 length, flags, fd, (size_t)offset);
166 * Wrapper for the open(2) system call.
168 * \param path The filename.
169 * \param flags The usual open(2) flags.
170 * \param mode Specifies the permissions to use.
172 * The mode parameter must be specified when O_CREAT is in the flags, and is
175 * \return The file descriptor on success, negative on errors.
179 int para_open(const char *path, int flags, mode_t mode)
181 int ret = open(path, flags, mode);
185 return -ERRNO_TO_PARA_ERROR(errno);
189 * Wrapper for chdir(2).
191 * \param path The specified directory.
195 int para_chdir(const char *path)
197 int ret = chdir(path);
201 return -ERRNO_TO_PARA_ERROR(errno);
205 * Save the cwd and open a given directory.
207 * \param dirname Path to the directory to open.
208 * \param dir Result pointer.
209 * \param cwd File descriptor of the current working directory.
213 * Opening the current directory (".") and calling fchdir() to return is
214 * usually faster and more reliable than saving cwd in some buffer and calling
215 * chdir() afterwards.
217 * If \a cwd is not \p NULL "." is opened and the resulting file descriptor is
218 * stored in \a cwd. If the function returns success, and \a cwd is not \p
219 * NULL, the caller must close this file descriptor (probably after calling
222 * On errors, the function undos everything, so the caller needs neither close
223 * any files, nor change back to the original working directory.
228 int para_opendir(const char *dirname, DIR **dir, int *cwd)
233 ret = para_open(".", O_RDONLY, 0);
238 ret = para_chdir(dirname);
244 ret = -ERRNO_TO_PARA_ERROR(errno);
245 /* Ignore return value of fchdir() and close(). We're busted anyway. */
255 * A wrapper for fchdir().
257 * \param fd An open file descriptor.
261 int para_fchdir(int fd)
264 return -ERRNO_TO_PARA_ERROR(errno);
269 * A wrapper for mkdir(2).
271 * \param path Name of the directory to create.
272 * \param mode The permissions to use.
276 int para_mkdir(const char *path, mode_t mode)
278 if (!mkdir(path, mode))
280 return -ERRNO_TO_PARA_ERROR(errno);
284 * Open a file and map it into memory.
286 * \param path Name of the regular file to map.
287 * \param open_mode Either \p O_RDONLY or \p O_RDWR.
288 * \param map On success, the mapping is returned here.
289 * \param size size of the mapping.
290 * \param fd_ptr The file descriptor of the mapping.
292 * If \a fd_ptr is \p NULL, the file descriptor resulting from the underlying
293 * open call is closed after mmap(). Otherwise the file is kept open and the
294 * file descriptor is returned in \a fd_ptr.
296 * \return Positive on success, negative on errors. Possible errors include: \p
297 * E_FSTAT, any errors returned by para_open(), \p E_EMPTY, \p E_MMAP.
299 * \sa para_open(), mmap(2).
301 int mmap_full_file(const char *path, int open_mode, void **map,
302 size_t *size, int *fd_ptr)
304 int fd, ret, mmap_prot, mmap_flags;
305 struct stat file_status;
307 if (open_mode == O_RDONLY) {
308 mmap_prot = PROT_READ;
309 mmap_flags = MAP_PRIVATE;
311 mmap_prot = PROT_READ | PROT_WRITE;
312 mmap_flags = MAP_SHARED;
314 ret = para_open(path, open_mode, 0);
318 if (fstat(fd, &file_status) < 0) {
319 ret = -ERRNO_TO_PARA_ERROR(errno);
322 *size = file_status.st_size;
324 PARA_DEBUG_LOG("%s: size %zu\n", path, *size);
327 *map = mmap(NULL, *size, mmap_prot, mmap_flags, fd, 0);
328 if (*map == MAP_FAILED) {
335 if (ret < 0 || !fd_ptr)
343 * A wrapper for munmap(2).
345 * \param start The start address of the memory mapping.
346 * \param length The size of the mapping.
348 * \return Positive on success, \p -E_MUNMAP on errors.
350 * \sa munmap(2), mmap_full_file().
352 int para_munmap(void *start, size_t length)
354 if (munmap(start, length) >= 0)
356 PARA_ERROR_LOG("munmap (%p/%zu) failed: %s\n", start, length,
362 * check a file descriptor for writability
364 * \param fd the file descriptor
366 * \return positive if fd is ready for writing, zero if it isn't, negative if
372 struct timeval tv = {0, 0};
380 ret = select(fd + 1, NULL, &wfds, NULL, &tv);
381 if (ret < 0 && errno == EINTR)