| 1 |
#ifndef RBIMPL_INTERN_SELECT_LARGESIZE_H /*-*-C++-*-vi:se ft=cpp:*/ |
| 2 |
#define RBIMPL_INTERN_SELECT_LARGESIZE_H |
| 3 |
/** |
| 4 |
* @file |
| 5 |
* @author Ruby developers <[email protected]> |
| 6 |
* @copyright This file is a part of the programming language Ruby. |
| 7 |
* Permission is hereby granted, to either redistribute and/or |
| 8 |
* modify this file, provided that the conditions mentioned in the |
| 9 |
* file COPYING are met. Consult the file for details. |
| 10 |
* @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are |
| 11 |
* implementation details. Don't take them as canon. They could |
| 12 |
* rapidly appear then vanish. The name (path) of this header file |
| 13 |
* is also an implementation detail. Do not expect it to persist |
| 14 |
* at the place it is now. Developers are free to move it anywhere |
| 15 |
* anytime at will. |
| 16 |
* @note To ruby-core: remember that this header can be possibly |
| 17 |
* recursively included from extension libraries written in C++. |
| 18 |
* Do not expect for instance `__VA_ARGS__` is always available. |
| 19 |
* We assume C99 for ruby itself but we don't assume languages of |
| 20 |
* extension libraries. They could be written in C++98. |
| 21 |
* @brief Public APIs to provide ::rb_fd_select(). |
| 22 |
* |
| 23 |
* Several Unix platforms support file descriptors bigger than FD_SETSIZE in |
| 24 |
* `select(2)` system call. |
| 25 |
* |
| 26 |
* - Linux 2.2.12 (?) |
| 27 |
* |
| 28 |
* - NetBSD 1.2 (src/sys/kern/sys_generic.c:1.25) |
| 29 |
* `select(2)` documents how to allocate fd_set dynamically. |
| 30 |
* http://netbsd.gw.com/cgi-bin/man-cgi?select++NetBSD-4.0 |
| 31 |
* |
| 32 |
* - FreeBSD 2.2 (src/sys/kern/sys_generic.c:1.19) |
| 33 |
* |
| 34 |
* - OpenBSD 2.0 (src/sys/kern/sys_generic.c:1.4) |
| 35 |
* `select(2)` documents how to allocate fd_set dynamically. |
| 36 |
* http://www.openbsd.org/cgi-bin/man.cgi?query=select&manpath=OpenBSD+4.4 |
| 37 |
* |
| 38 |
* - Solaris 8 has `select_large_fdset` |
| 39 |
* |
| 40 |
* - Mac OS X 10.7 (Lion) |
| 41 |
* `select(2)` returns `EINVAL` if `nfds` is greater than `FD_SET_SIZE` and |
| 42 |
* `_DARWIN_UNLIMITED_SELECT` (or `_DARWIN_C_SOURCE`) isn't defined. |
| 43 |
* http://developer.apple.com/library/mac/#releasenotes/Darwin/SymbolVariantsRelNotes/_index.html |
| 44 |
* |
| 45 |
* When `fd_set` is not big enough to hold big file descriptors, it should be |
| 46 |
* allocated dynamically. Note that this assumes `fd_set` is structured as |
| 47 |
* bitmap. |
| 48 |
* |
| 49 |
* `rb_fd_init` allocates the memory. |
| 50 |
* `rb_fd_term` frees the memory. |
| 51 |
* `rb_fd_set` may re-allocate bitmap. |
| 52 |
* |
| 53 |
* So `rb_fd_set` doesn't reject file descriptors bigger than `FD_SETSIZE`. |
| 54 |
*/ |
| 55 |
#include "ruby/internal/attr/nonnull.h" |
| 56 |
#include "ruby/internal/attr/pure.h" |
| 57 |
#include "ruby/internal/dllexport.h" |
| 58 |
|
| 59 |
/**@cond INTERNAL_MACRO */ |
| 60 |
#define rb_fd_ptr rb_fd_ptr |
| 61 |
#define rb_fd_max rb_fd_max |
| 62 |
/** @endcond */ |
| 63 |
|
| 64 |
struct timeval; |
| 65 |
|
| 66 |
/** |
| 67 |
* The data structure which wraps the fd_set bitmap used by select(2). This |
| 68 |
* allows Ruby to use FD sets larger than that allowed by historic limitations |
| 69 |
* on modern platforms. |
| 70 |
*/ |
| 71 |
typedef struct { |
| 72 |
int maxfd; /**< Maximum allowed number of FDs. */ |
| 73 |
fd_set *fdset; /**< File descriptors buffer */ |
| 74 |
} rb_fdset_t; |
| 75 |
|
| 76 |
RBIMPL_SYMBOL_EXPORT_BEGIN() |
| 77 |
RBIMPL_ATTR_NONNULL(()) |
| 78 |
/** |
| 79 |
* (Re-)initialises a fdset. One must be initialised before other `rb_fd_*` |
| 80 |
* operations. Analogous to calling `malloc(3)` to allocate an `fd_set`. |
| 81 |
* |
| 82 |
* @param[out] f An fdset to squash. |
| 83 |
* @post `f` holds no file descriptors. |
| 84 |
*/ |
| 85 |
void rb_fd_init(rb_fdset_t *f); |
| 86 |
|
| 87 |
RBIMPL_ATTR_NONNULL(()) |
| 88 |
/** |
| 89 |
* Destroys the ::rb_fdset_t, releasing any memory and resources it used. It |
| 90 |
* must be reinitialised using rb_fd_init() before future use. Analogous to |
| 91 |
* calling `free(3)` to release memory for an `fd_set`. |
| 92 |
* |
| 93 |
* @param[out] f An fdset to squash. |
| 94 |
* @post `f` holds no file descriptors. |
| 95 |
*/ |
| 96 |
void rb_fd_term(rb_fdset_t *f); |
| 97 |
|
| 98 |
RBIMPL_ATTR_NONNULL(()) |
| 99 |
/** |
| 100 |
* Wipes out the current set of FDs. |
| 101 |
* |
| 102 |
* @param[out] f The fdset to clear. |
| 103 |
* @post `f` has no FDs. |
| 104 |
*/ |
| 105 |
void rb_fd_zero(rb_fdset_t *f); |
| 106 |
|
| 107 |
RBIMPL_ATTR_NONNULL(()) |
| 108 |
/** |
| 109 |
* Sets an fd to a fdset. |
| 110 |
* |
| 111 |
* @param[in] fd A file descriptor. |
| 112 |
* @param[out] f Target fdset. |
| 113 |
* @post `f` holds `fd`. |
| 114 |
*/ |
| 115 |
void rb_fd_set(int fd, rb_fdset_t *f); |
| 116 |
|
| 117 |
RBIMPL_ATTR_NONNULL(()) |
| 118 |
/** |
| 119 |
* Releases a specific FD from the given fdset. |
| 120 |
* |
| 121 |
* @param[in] fd Target FD. |
| 122 |
* @param[out] f The fdset that holds `fd`. |
| 123 |
* @post `f` doesn't hold n. |
| 124 |
*/ |
| 125 |
void rb_fd_clr(int fd, rb_fdset_t *f); |
| 126 |
|
| 127 |
RBIMPL_ATTR_NONNULL(()) |
| 128 |
RBIMPL_ATTR_PURE() |
| 129 |
/** |
| 130 |
* Queries if the given FD is in the given set. |
| 131 |
* |
| 132 |
* @param[in] fd Target FD. |
| 133 |
* @param[in] f The fdset to scan. |
| 134 |
* @retval 1 Yes there is. |
| 135 |
* @retval 0 No there isn't. |
| 136 |
* @see http://www.freebsd.org/cgi/query-pr.cgi?pr=91421 |
| 137 |
*/ |
| 138 |
int rb_fd_isset(int fd, const rb_fdset_t *f); |
| 139 |
|
| 140 |
/** |
| 141 |
* Destructively overwrites an fdset with another. |
| 142 |
* |
| 143 |
* @param[out] dst Target fdset. |
| 144 |
* @param[in] src Source fdset. |
| 145 |
* @param[in] max Maximum number of file descriptors to copy. |
| 146 |
* @post `dst` is a copy of `src`. |
| 147 |
*/ |
| 148 |
void rb_fd_copy(rb_fdset_t *dst, const fd_set *src, int max); |
| 149 |
|
| 150 |
/** |
| 151 |
* Identical to rb_fd_copy(), except it copies unlimited number of file |
| 152 |
* descriptors. |
| 153 |
* |
| 154 |
* @param[out] dst Target fdset. |
| 155 |
* @param[in] src Source fdset. |
| 156 |
* @post `dst` is a copy of `src`. |
| 157 |
*/ |
| 158 |
void rb_fd_dup(rb_fdset_t *dst, const rb_fdset_t *src); |
| 159 |
|
| 160 |
/** |
| 161 |
* Waits for multiple file descriptors at once. |
| 162 |
* |
| 163 |
* @param[in] nfds Max FD in everything passed, plus one. |
| 164 |
* @param[in,out] rfds Set of FDs to wait for reads. |
| 165 |
* @param[in,out] wfds Set of FDs to wait for writes. |
| 166 |
* @param[in,out] efds Set of FDs to wait for OOBs. |
| 167 |
* @param[in,out] timeout Max blocking duration. |
| 168 |
* @retval -1 Failed, errno set. |
| 169 |
* @retval 0 Timeout exceeded. |
| 170 |
* @retval otherwise Total number of file descriptors returned. |
| 171 |
* @post `rfds` contains readable FDs. |
| 172 |
* @post `wfds` contains writable FDs. |
| 173 |
* @post `efds` contains exceptional FDs. |
| 174 |
* @post `timeout` is the time left. |
| 175 |
* @note All pointers are allowed to be null pointers. |
| 176 |
*/ |
| 177 |
int rb_fd_select(int nfds, rb_fdset_t *rfds, rb_fdset_t *wfds, rb_fdset_t *efds, struct timeval *timeout); |
| 178 |
RBIMPL_SYMBOL_EXPORT_END() |
| 179 |
|
| 180 |
RBIMPL_ATTR_NONNULL(()) |
| 181 |
RBIMPL_ATTR_PURE() |
| 182 |
/** |
| 183 |
* Raw pointer to `fd_set`. |
| 184 |
* |
| 185 |
* @param[in] f Target fdset. |
| 186 |
* @retval NULL `f` is already terminated by rb_fd_term(). |
| 187 |
* @retval otherwise Underlying fd_set. |
| 188 |
* |
| 189 |
* @internal |
| 190 |
* |
| 191 |
* Extension library must not touch raw pointers. It was a bad idea to let |
| 192 |
* them use it. |
| 193 |
*/ |
| 194 |
static inline fd_set * |
| 195 |
rb_fd_ptr(const rb_fdset_t *f) |
| 196 |
{ |
| 197 |
return f->fdset; |
| 198 |
} |
| 199 |
|
| 200 |
RBIMPL_ATTR_NONNULL(()) |
| 201 |
RBIMPL_ATTR_PURE() |
| 202 |
/** |
| 203 |
* It seems this function has no use. Maybe just remove? |
| 204 |
* |
| 205 |
* @param[in] f A set. |
| 206 |
* @return Number of file descriptors stored. |
| 207 |
*/ |
| 208 |
static inline int |
| 209 |
rb_fd_max(const rb_fdset_t *f) |
| 210 |
{ |
| 211 |
return f->maxfd; |
| 212 |
} |
| 213 |
|
| 214 |
#endif /* RBIMPL_INTERN_SELECT_LARGESIZE_H */ |