2 * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /** \file fd.c helper functions for file descriptor handling */
24 #include <sys/select.h>
28 * check whether a file exists
30 * \param fn the file name
32 * \return Non-zero iff file exists.
34 int file_exists(const char *fn)
38 return !stat(fn, &statbuf);
42 * paraslash's wrapper for select(2)
44 * It calls select(2) (with no exceptfds) and starts over if select() was
45 * interrupted by a signal.
47 * \param n the highest-numbered descriptor in any of the two sets, plus 1
48 * \param readfds fds that should be checked for readability
49 * \param writefds fds that should be checked for writablility
50 * \param timeout_tv upper bound on the amount of time elapsed before select()
53 * \return The return value of the underlying select() call.
55 * All arguments are passed verbatim to select(2).
56 * \sa select(2) select_tut(2)
58 int para_select(int n, fd_set *readfds, fd_set *writefds,
59 struct timeval *timeout_tv)
63 ret = select(n, readfds, writefds, NULL, timeout_tv);
65 } while (ret < 0 && err == EINTR);
67 PARA_CRIT_LOG("select error: %s, max_fileno: %d\n",
73 * set a file descriptor to non-blocking mode
75 * \param fd The file descriptor
77 * \returns 1 on success, -E_F_GETFL, -E_F_SETFL, on errors.
79 int mark_fd_nonblock(int fd)
81 int flags = fcntl(fd, F_GETFL);
84 if (fcntl(fd, F_SETFL, ((long)flags) | O_NONBLOCK) < 0)
90 * set a file descriptor in a fd_set
92 * \param fd the file descriptor to be set
93 * \param fds the file descriptor set
94 * \param max_fileno highest-numbered file descriptor
96 * This wrapper for FD_SET() passes its first two arguments to \p FD_SET. Upon
97 * return, \a max_fileno contains the maximum of the old_value and \a fd.
101 void para_fd_set(int fd, fd_set *fds, int *max_fileno)
104 if (fd < 0 || fd >= FD_SETSIZE) {
105 PARA_EMERG_LOG("fatal: tried to add invalid fd %d\n", fd);
110 int flags = fcntl(fd, F_GETFL);
111 if (!(flags & O_NONBLOCK)) {
112 PARA_EMERG_LOG("fd %d is a blocking file descriptor\n", fd);
118 *max_fileno = PARA_MAX(*max_fileno, fd);
122 * paraslash's wrapper for fread(3)
124 * \param dest destination pointer
125 * \param nbytes size of one element
126 * \param nmemb number of elements to write
127 * \param stream the input stream
129 * \return negative on errors, zero on end of file, and the number of bytes
130 * (not elements) read on success.
134 __must_check int para_fread(void *dest, size_t nbytes, size_t nmemb, FILE *stream)
136 size_t res = fread(dest, nbytes, nmemb, stream);
138 return nbytes * nmemb;
144 * paraslash's wrapper for fgets(3)
145 * \param line pointer to the buffer to store the line
146 * \param size the size of the buffer given by \a line
147 * \param f the stream to read from
149 * \return Unlike the standard fgets() function, an integer value
150 * is returned. On success, this function returns 1. On errors, -E_FGETS
151 * is returned. A zero return value indicates an end of file condition.
153 __must_check int para_fgets(char *line, int size, FILE *f)
156 if (fgets(line, size, f))
162 if (errno != EINTR) {
163 PARA_ERROR_LOG("%s\n", strerror(errno));