fix some compiler warnings on Mac OS
[paraslash.git] / fd.c
1 /*
2 * Copyright (C) 2006-2007 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 #include <sys/mman.h>
23 #include <fcntl.h>
24 #include <sys/select.h>
25
26 #include "error.h"
27 /**
28 * check whether a file exists
29 *
30 * \param fn the file name
31 *
32 * \return Non-zero iff file exists.
33 */
34 int file_exists(const char *fn)
35 {
36 struct stat statbuf;
37
38 return !stat(fn, &statbuf);
39 }
40
41 /**
42 * paraslash's wrapper for select(2)
43 *
44 * It calls select(2) (with no exceptfds) and starts over if select() was
45 * interrupted by a signal.
46 *
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()
51 * returns
52 *
53 * \return The return value of the underlying select() call.
54 *
55 * All arguments are passed verbatim to select(2).
56 * \sa select(2) select_tut(2)
57 */
58 int para_select(int n, fd_set *readfds, fd_set *writefds,
59 struct timeval *timeout_tv)
60 {
61 int ret, err;
62 do {
63 ret = select(n, readfds, writefds, NULL, timeout_tv);
64 err = errno;
65 } while (ret < 0 && err == EINTR);
66 if (ret < 0)
67 PARA_CRIT_LOG("select error: %s, max_fileno: %d\n",
68 strerror(err), n);
69 return ret;
70 }
71
72 /**
73 * set a file descriptor to non-blocking mode
74 *
75 * \param fd The file descriptor
76 *
77 * \returns 1 on success, -E_F_GETFL, -E_F_SETFL, on errors.
78 */
79 int mark_fd_nonblock(int fd)
80 {
81 int flags = fcntl(fd, F_GETFL);
82 if (flags < 0)
83 return -E_F_GETFL;
84 if (fcntl(fd, F_SETFL, ((long)flags) | O_NONBLOCK) < 0)
85 return -E_F_SETFL;
86 return 1;
87 }
88
89 /**
90 * set a file descriptor in a fd_set
91 *
92 * \param fd the file descriptor to be set
93 * \param fds the file descriptor set
94 * \param max_fileno highest-numbered file descriptor
95 *
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.
98 *
99 * \sa para_select
100 */
101 void para_fd_set(int fd, fd_set *fds, int *max_fileno)
102 {
103
104 if (fd < 0 || fd >= FD_SETSIZE) {
105 PARA_EMERG_LOG("fatal: tried to add invalid fd %d\n", fd);
106 exit(EXIT_FAILURE);
107 }
108 #if 0
109 {
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);
113 exit(EXIT_FAILURE);
114 }
115 }
116 #endif
117 FD_SET(fd, fds);
118 *max_fileno = PARA_MAX(*max_fileno, fd);
119 }
120
121 /**
122 * paraslash's wrapper for fgets(3)
123 * \param line pointer to the buffer to store the line
124 * \param size the size of the buffer given by \a line
125 * \param f the stream to read from
126 *
127 * \return Unlike the standard fgets() function, an integer value
128 * is returned. On success, this function returns 1. On errors, -E_FGETS
129 * is returned. A zero return value indicates an end of file condition.
130 */
131 __must_check int para_fgets(char *line, int size, FILE *f)
132 {
133 again:
134 if (fgets(line, size, f))
135 return 1;
136 if (feof(f))
137 return 0;
138 if (!ferror(f))
139 return -E_FGETS;
140 if (errno != EINTR) {
141 PARA_ERROR_LOG("%s\n", strerror(errno));
142 return -E_FGETS;
143 }
144 clearerr(f);
145 goto again;
146 }
147
148 /**
149 * *paraslash's wrapper for mmap
150 *
151 * \param length number of bytes to mmap
152 * \param prot either PROT_NONE or the bitwise OR of one or more of
153 * PROT_EXEC PROT_READ PROT_WRITE
154 * \param flags exactly one of MAP_SHARED and MAP_PRIVATE
155 * \param fd the file to mmap from
156 * \param offset mmap start
157 *
158 * \return This function either returns a valid pointer to the mapped area
159 * or calls exit() on errors.
160 */
161 void *para_mmap(size_t length, int prot, int flags, int fd, off_t offset)
162 {
163 void *ret = mmap(NULL, length, prot, flags, fd, offset);
164 if (ret != MAP_FAILED)
165 return ret;
166 PARA_EMERG_LOG("mmap failed: %s\n", strerror(errno));
167 PARA_EMERG_LOG("length: %zu, flags: %d, fd: %d, offset: %zu\n",
168 length, flags, fd, (size_t)offset);
169 exit(EXIT_FAILURE);
170 }
171