2 * Copyright (C) 2006-2008 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 blocking mode.
66 * \param fd The file descriptor.
70 int mark_fd_blocking(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 to non-blocking mode.
84 * \param fd The file descriptor.
88 int mark_fd_nonblocking(int fd)
90 int flags = fcntl(fd, F_GETFL);
92 return -ERRNO_TO_PARA_ERROR(errno);
93 flags = fcntl(fd, F_SETFL, ((long)flags) | O_NONBLOCK);
95 return -ERRNO_TO_PARA_ERROR(errno);
100 * Set a file descriptor in a fd_set.
102 * \param fd The file descriptor to be set.
103 * \param fds The file descriptor set.
104 * \param max_fileno Highest-numbered file descriptor.
106 * This wrapper for FD_SET() passes its first two arguments to \p FD_SET. Upon
107 * return, \a max_fileno contains the maximum of the old_value and \a fd.
111 void para_fd_set(int fd, fd_set *fds, int *max_fileno)
114 if (fd < 0 || fd >= FD_SETSIZE) {
115 PARA_EMERG_LOG("fatal: tried to add invalid fd %d\n", fd);
120 int flags = fcntl(fd, F_GETFL);
121 if (!(flags & O_NONBLOCK)) {
122 PARA_EMERG_LOG("fd %d is a blocking file descriptor\n", fd);
128 *max_fileno = PARA_MAX(*max_fileno, fd);
132 * Paraslash's wrapper for fgets(3).
134 * \param line Pointer to the buffer to store the line.
135 * \param size The size of the buffer given by \a line.
136 * \param f The stream to read from.
138 * \return Unlike the standard fgets() function, an integer value
139 * is returned. On success, this function returns 1. On errors, -E_FGETS
140 * is returned. A zero return value indicates an end of file condition.
142 __must_check int para_fgets(char *line, int size, FILE *f)
145 if (fgets(line, size, f))
151 if (errno != EINTR) {
152 PARA_ERROR_LOG("%s\n", strerror(errno));
160 * Paraslash's wrapper for mmap.
162 * \param length Number of bytes to mmap.
163 * \param prot Either PROT_NONE or the bitwise OR of one or more of
164 * PROT_EXEC PROT_READ PROT_WRITE.
165 * \param flags Exactly one of MAP_SHARED and MAP_PRIVATE.
166 * \param fd The file to mmap from.
167 * \param offset Mmap start.
169 * \return This function either returns a valid pointer to the mapped area
170 * or calls exit() on errors.
172 void *para_mmap(size_t length, int prot, int flags, int fd, off_t offset)
174 void *ret = mmap(NULL, length, prot, flags, fd, offset);
175 if (ret != MAP_FAILED)
177 PARA_EMERG_LOG("mmap failed: %s\n", strerror(errno));
178 PARA_EMERG_LOG("length: %zu, flags: %d, fd: %d, offset: %zu\n",
179 length, flags, fd, (size_t)offset);
184 * Wrapper for the open(2) system call.
186 * \param path The filename.
187 * \param flags The usual open(2) flags.
188 * \param mode Specifies the permissions to use.
190 * The mode parameter must be specified when O_CREAT is in the flags, and is
193 * \return The file descriptor on success, negative on errors.
197 int para_open(const char *path, int flags, mode_t mode)
199 int ret = open(path, flags, mode);
203 return -ERRNO_TO_PARA_ERROR(errno);
207 * Wrapper for chdir(2).
209 * \param path The specified directory.
213 int para_chdir(const char *path)
215 int ret = chdir(path);
219 return -ERRNO_TO_PARA_ERROR(errno);
223 * Save the cwd and open a given directory.
225 * \param dirname Path to the directory to open.
226 * \param dir Result pointer.
227 * \param cwd File descriptor of the current working directory.
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.
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
240 * On errors, the function undos everything, so the caller needs neither close
241 * any files, nor change back to the original working directory.
246 int para_opendir(const char *dirname, DIR **dir, int *cwd)
251 ret = para_open(".", O_RDONLY, 0);
256 ret = para_chdir(dirname);
262 ret = -ERRNO_TO_PARA_ERROR(errno);
263 /* Ignore return value of fchdir() and close(). We're busted anyway. */
273 * A wrapper for fchdir().
275 * \param fd An open file descriptor.
279 int para_fchdir(int fd)
282 return -ERRNO_TO_PARA_ERROR(errno);
287 * A wrapper for mkdir(2).
289 * \param path Name of the directory to create.
290 * \param mode The permissions to use.
294 int para_mkdir(const char *path, mode_t mode)
296 if (!mkdir(path, mode))
298 return -ERRNO_TO_PARA_ERROR(errno);
302 * Open a file and map it into memory.
304 * \param path Name of the regular file to map.
305 * \param open_mode Either \p O_RDONLY or \p O_RDWR.
306 * \param map On success, the mapping is returned here.
307 * \param size size of the mapping.
308 * \param fd_ptr The file descriptor of the mapping.
310 * If \a fd_ptr is \p NULL, the file descriptor resulting from the underlying
311 * open call is closed after mmap(). Otherwise the file is kept open and the
312 * file descriptor is returned in \a fd_ptr.
316 * \sa para_open(), mmap(2).
318 int mmap_full_file(const char *path, int open_mode, void **map,
319 size_t *size, int *fd_ptr)
321 int fd, ret, mmap_prot, mmap_flags;
322 struct stat file_status;
324 if (open_mode == O_RDONLY) {
325 mmap_prot = PROT_READ;
326 mmap_flags = MAP_PRIVATE;
328 mmap_prot = PROT_READ | PROT_WRITE;
329 mmap_flags = MAP_SHARED;
331 ret = para_open(path, open_mode, 0);
335 if (fstat(fd, &file_status) < 0) {
336 ret = -ERRNO_TO_PARA_ERROR(errno);
339 *size = file_status.st_size;
341 PARA_DEBUG_LOG("%s: size %zu\n", path, *size);
344 *map = mmap(NULL, *size, mmap_prot, mmap_flags, fd, 0);
345 if (*map == MAP_FAILED) {
352 if (ret < 0 || !fd_ptr)
360 * A wrapper for munmap(2).
362 * \param start The start address of the memory mapping.
363 * \param length The size of the mapping.
365 * \return Positive on success, \p -E_MUNMAP on errors.
367 * \sa munmap(2), mmap_full_file().
369 int para_munmap(void *start, size_t length)
371 if (munmap(start, length) >= 0)
373 PARA_ERROR_LOG("munmap (%p/%zu) failed: %s\n", start, length,
379 * check a file descriptor for writability
381 * \param fd the file descriptor
383 * \return positive if fd is ready for writing, zero if it isn't, negative if
389 struct timeval tv = {0, 0};
397 ret = select(fd + 1, NULL, &wfds, NULL, &tv);
398 if (ret < 0 && errno == EINTR)