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 */
12 #include <sys/select.h>
16 * check whether a file exists
18 * \param fn the file name
20 * \return Non-zero iff file exists.
22 int file_exists(const char *fn)
26 return !stat(fn, &statbuf);
30 * paraslash's wrapper for select(2)
32 * It calls select(2) (with no exceptfds) and starts over if select() was
33 * interrupted by a signal.
35 * \param n the highest-numbered descriptor in any of the two sets, plus 1
36 * \param readfds fds that should be checked for readability
37 * \param writefds fds that should be checked for writablility
38 * \param timeout_tv upper bound on the amount of time elapsed before select()
41 * \return The return value of the underlying select() call.
43 * All arguments are passed verbatim to select(2).
44 * \sa select(2) select_tut(2)
46 int para_select(int n, fd_set *readfds, fd_set *writefds,
47 struct timeval *timeout_tv)
51 ret = select(n, readfds, writefds, NULL, timeout_tv);
53 } while (ret < 0 && err == EINTR);
55 PARA_CRIT_LOG("select error: %s, max_fileno: %d\n",
63 * set a file descriptor to non-blocking mode
65 * \param fd The file descriptor
67 * \returns 1 on success, -E_F_GETFL, -E_F_SETFL, on errors.
69 int mark_fd_nonblock(int fd)
71 int flags = fcntl(fd, F_GETFL);
74 if (fcntl(fd, F_SETFL, ((long)flags) | O_NONBLOCK) < 0)
80 * set a file descriptor in a fd_set
82 * \param fd the file descriptor to be set
83 * \param fds the file descriptor set
84 * \param max_fileno highest-numbered file descriptor
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.
91 void para_fd_set(int fd, fd_set *fds, int *max_fileno)
94 if (fd < 0 || fd >= FD_SETSIZE) {
95 PARA_EMERG_LOG("fatal: tried to add invalid fd %d\n", fd);
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);
108 *max_fileno = PARA_MAX(*max_fileno, fd);
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
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.
121 __must_check int para_fgets(char *line, int size, FILE *f)
124 if (fgets(line, size, f))
130 if (errno != EINTR) {
131 PARA_ERROR_LOG("%s\n", strerror(errno));
139 * *paraslash's wrapper for mmap
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
148 * \return This function either returns a valid pointer to the mapped area
149 * or calls exit() on errors.
151 void *para_mmap(size_t length, int prot, int flags, int fd, off_t offset)
153 void *ret = mmap(NULL, length, prot, flags, fd, offset);
154 if (ret != MAP_FAILED)
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);