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