4928850e38d7d01688702d8acb8e898da53e284d
[paraslash.git] / fd.c
1 /*
2 * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
3 *
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.
8 *
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.
13 *
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.
17 */
18
19 /** \file fd.c helper functions for file descriptor handling */
20
21 #include "para.h"
22
23 #include <sys/select.h>
24
25 #include "error.h"
26 /**
27 * check whether a file exists
28 *
29 * \param fn the file name
30 *
31 * \return Non-zero iff file exists.
32 */
33 int file_exists(const char *fn)
34 {
35 struct stat statbuf;
36
37 return !stat(fn, &statbuf);
38 }
39
40 /**
41 * paraslash's wrapper for select(2)
42 *
43 * It calls select(2) (with no exceptfds) and starts over if select() was
44 * interrupted by a signal.
45 *
46 * \param n the highest-numbered descriptor in any of the two sets, plus 1
47 * \param readfds fds that should be checked for readability
48 * \param writefds fds that should be checked for writablility
49 * \param timeout upper bound on the amount of time elapsed before select()
50 * returns
51 *
52 * \return The return value of the underlying select() call.
53 *
54 * All arguments are passed verbatim to select(2).
55 * \sa select(2) select_tut(2)
56 */
57 int para_select(int n, fd_set *readfds, fd_set *writefds,
58 struct timeval *timeout)
59 {
60 int ret, err;
61 do {
62 ret = select(n, readfds, writefds, NULL, timeout);
63 err = errno;
64 } while (ret < 0 && err == EINTR);
65 if (ret < 0)
66 PARA_CRIT_LOG("select error (%s)\n", strerror(err));
67 return ret;
68 }
69
70 /**
71 * set a file descriptor to non-blocking mode
72 *
73 * \param fd The file descriptor
74 *
75 * \returns 1 on success, -E_F_GETFL, -E_F_SETFL, on errors.
76 */
77 int mark_fd_nonblock(int fd)
78 {
79 int flags = fcntl(fd, F_GETFL);
80 if (flags < 0)
81 return -E_F_GETFL;
82 if (fcntl(fd, F_SETFL, ((long)flags) | O_NONBLOCK) < 0)
83 return -E_F_SETFL;
84 return 1;
85 }
86
87 /**
88 * set a file descriptor in a fd_set
89 *
90 * \param fd the file descriptor to be set
91 * \param fds the file descriptor set
92 * \param max_fileno highest-numbered file descriptor
93 *
94 * This wrapper for FD_SET() passes its first two arguments to \p FD_SET. Upon
95 * return, \a max_fileno contains the maximum of the old_value and \a fd.
96 */
97 void para_fd_set(int fd, fd_set *fds, int *max_fileno)
98 {
99 FD_SET(fd, fds);
100 *max_fileno = MAX(*max_fileno, fd);
101 }