1 /* 2 * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 #include <stdio.h> 27 #include <stdlib.h> 28 #include <signal.h> 29 #include <pthread.h> 30 #include <sys/types.h> 31 #include <sys/socket.h> 32 #include <sys/time.h> 33 #include <sys/resource.h> 34 #include <sys/uio.h> 35 #include <unistd.h> 36 #include <errno.h> 37 #include <sys/poll.h> 38 39 #include "jni.h" 40 41 /* 42 * Stack allocated by thread when doing blocking operation 43 */ 44 typedef struct threadEntry { 45 pthread_t thr; /* this thread */ 46 struct threadEntry *next; /* next thread */ 47 int intr; /* interrupted */ 48 } threadEntry_t; 49 50 /* 51 * Heap allocated during initialized - one entry per fd 52 */ 53 typedef struct { 54 pthread_mutex_t lock; /* fd lock */ 55 threadEntry_t *threads; /* threads blocked on fd */ 56 } fdEntry_t; 57 58 /* 59 * Signal to unblock thread 60 */ 61 static int sigWakeup = (__SIGRTMAX - 2); 62 63 /* 64 * The fd table and the number of file descriptors 65 */ 66 static fdEntry_t *fdTable; 67 static int fdCount; 68 69 /* 70 * Null signal handler 71 */ 72 static void sig_wakeup(int sig) { 73 } 74 75 /* 76 * Initialization routine (executed when library is loaded) 77 * Allocate fd tables and sets up signal handler. 78 */ 79 static void __attribute((constructor)) init() { 80 struct rlimit nbr_files; 81 sigset_t sigset; 82 struct sigaction sa; 83 84 /* 85 * Allocate table based on the maximum number of 86 * file descriptors. 87 */ 88 getrlimit(RLIMIT_NOFILE, &nbr_files); 89 fdCount = nbr_files.rlim_max; 90 fdTable = (fdEntry_t *)calloc(fdCount, sizeof(fdEntry_t)); 91 if (fdTable == NULL) { 92 fprintf(stderr, "library initialization failed - " 93 "unable to allocate file descriptor table - out of memory"); 94 abort(); 95 } 96 97 /* 98 * Setup the signal handler 99 */ 100 sa.sa_handler = sig_wakeup; 101 sa.sa_flags = 0; 102 sigemptyset(&sa.sa_mask); 103 sigaction(sigWakeup, &sa, NULL); 104 105 sigemptyset(&sigset); 106 sigaddset(&sigset, sigWakeup); 107 sigprocmask(SIG_UNBLOCK, &sigset, NULL); 108 } 109 110 /* 111 * Return the fd table for this fd or NULL is fd out 112 * of range. 113 */ 114 static inline fdEntry_t *getFdEntry(int fd) 115 { 116 if (fd < 0 || fd >= fdCount) { 117 return NULL; 118 } 119 return &fdTable[fd]; 120 } 121 122 /* 123 * Start a blocking operation :- 124 * Insert thread onto thread list for the fd. 125 */ 126 static inline void startOp(fdEntry_t *fdEntry, threadEntry_t *self) 127 { 128 self->thr = pthread_self(); 129 self->intr = 0; 130 131 pthread_mutex_lock(&(fdEntry->lock)); 132 { 133 self->next = fdEntry->threads; 134 fdEntry->threads = self; 135 } 136 pthread_mutex_unlock(&(fdEntry->lock)); 137 } 138 139 /* 140 * End a blocking operation :- 141 * Remove thread from thread list for the fd 142 * If fd has been interrupted then set errno to EBADF 143 */ 144 static inline void endOp 145 (fdEntry_t *fdEntry, threadEntry_t *self) 146 { 147 int orig_errno = errno; 148 pthread_mutex_lock(&(fdEntry->lock)); 149 { 150 threadEntry_t *curr, *prev=NULL; 151 curr = fdEntry->threads; 152 while (curr != NULL) { 153 if (curr == self) { 154 if (curr->intr) { 155 orig_errno = EBADF; 156 } 157 if (prev == NULL) { 158 fdEntry->threads = curr->next; 159 } else { 160 prev->next = curr->next; 161 } 162 break; 163 } 164 prev = curr; 165 curr = curr->next; 166 } 167 } 168 pthread_mutex_unlock(&(fdEntry->lock)); 169 errno = orig_errno; 170 } 171 172 /* 173 * Close or dup2 a file descriptor ensuring that all threads blocked on 174 * the file descriptor are notified via a wakeup signal. 175 * 176 * fd1 < 0 => close(fd2) 177 * fd1 >= 0 => dup2(fd1, fd2) 178 * 179 * Returns -1 with errno set if operation fails. 180 */ 181 static int closefd(int fd1, int fd2) { 182 int rv, orig_errno; 183 fdEntry_t *fdEntry = getFdEntry(fd2); 184 if (fdEntry == NULL) { 185 errno = EBADF; 186 return -1; 187 } 188 189 /* 190 * Lock the fd to hold-off additional I/O on this fd. 191 */ 192 pthread_mutex_lock(&(fdEntry->lock)); 193 194 { 195 /* 196 * And close/dup the file descriptor 197 * (restart if interrupted by signal) 198 */ 199 do { 200 if (fd1 < 0) { 201 rv = close(fd2); 202 } else { 203 rv = dup2(fd1, fd2); 204 } 205 } while (rv == -1 && errno == EINTR); 206 207 /* 208 * Send a wakeup signal to all threads blocked on this 209 * file descriptor. 210 */ 211 threadEntry_t *curr = fdEntry->threads; 212 while (curr != NULL) { 213 curr->intr = 1; 214 pthread_kill( curr->thr, sigWakeup ); 215 curr = curr->next; 216 } 217 } 218 219 /* 220 * Unlock without destroying errno 221 */ 222 orig_errno = errno; 223 pthread_mutex_unlock(&(fdEntry->lock)); 224 errno = orig_errno; 225 226 return rv; 227 } 228 229 /* 230 * Wrapper for dup2 - same semantics as dup2 system call except 231 * that any threads blocked in an I/O system call on fd2 will be 232 * preempted and return -1/EBADF; 233 */ 234 int NET_Dup2(int fd, int fd2) { 235 if (fd < 0) { 236 errno = EBADF; 237 return -1; 238 } 239 return closefd(fd, fd2); 240 } 241 242 /* 243 * Wrapper for close - same semantics as close system call 244 * except that any threads blocked in an I/O on fd will be 245 * preempted and the I/O system call will return -1/EBADF. 246 */ 247 int NET_SocketClose(int fd) { 248 return closefd(-1, fd); 249 } 250 251 /************** Basic I/O operations here ***************/ 252 253 /* 254 * Macro to perform a blocking IO operation. Restarts 255 * automatically if interrupted by signal (other than 256 * our wakeup signal) 257 */ 258 #define BLOCKING_IO_RETURN_INT(FD, FUNC) { \ 259 int ret; \ 260 threadEntry_t self; \ 261 fdEntry_t *fdEntry = getFdEntry(FD); \ 262 if (fdEntry == NULL) { \ 263 errno = EBADF; \ 264 return -1; \ 265 } \ 266 do { \ 267 startOp(fdEntry, &self); \ 268 ret = FUNC; \ 269 endOp(fdEntry, &self); \ 270 } while (ret == -1 && errno == EINTR); \ 271 return ret; \ 272 } 273 274 int NET_Read(int s, void* buf, size_t len) { 275 BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, 0) ); 276 } 277 278 int NET_ReadV(int s, const struct iovec * vector, int count) { 279 BLOCKING_IO_RETURN_INT( s, readv(s, vector, count) ); 280 } 281 282 int NET_RecvFrom(int s, void *buf, int len, unsigned int flags, 283 struct sockaddr *from, socklen_t *fromlen) { 284 BLOCKING_IO_RETURN_INT( s, recvfrom(s, buf, len, flags, from, fromlen) ); 285 } 286 287 int NET_Send(int s, void *msg, int len, unsigned int flags) { 288 BLOCKING_IO_RETURN_INT( s, send(s, msg, len, flags) ); 289 } 290 291 int NET_WriteV(int s, const struct iovec * vector, int count) { 292 BLOCKING_IO_RETURN_INT( s, writev(s, vector, count) ); 293 } 294 295 int NET_SendTo(int s, const void *msg, int len, unsigned int 296 flags, const struct sockaddr *to, int tolen) { 297 BLOCKING_IO_RETURN_INT( s, sendto(s, msg, len, flags, to, tolen) ); 298 } 299 300 int NET_Accept(int s, struct sockaddr *addr, socklen_t *addrlen) { 301 BLOCKING_IO_RETURN_INT( s, accept(s, addr, addrlen) ); 302 } 303 304 int NET_Connect(int s, struct sockaddr *addr, int addrlen) { 305 BLOCKING_IO_RETURN_INT( s, connect(s, addr, addrlen) ); 306 } 307 308 #ifndef USE_SELECT 309 int NET_Poll(struct pollfd *ufds, unsigned int nfds, int timeout) { 310 BLOCKING_IO_RETURN_INT( ufds[0].fd, poll(ufds, nfds, timeout) ); 311 } 312 #else 313 int NET_Select(int s, fd_set *readfds, fd_set *writefds, 314 fd_set *exceptfds, struct timeval *timeout) { 315 BLOCKING_IO_RETURN_INT( s-1, 316 select(s, readfds, writefds, exceptfds, timeout) ); 317 } 318 #endif 319 320 /* 321 * Wrapper for poll(s, timeout). 322 * Auto restarts with adjusted timeout if interrupted by 323 * signal other than our wakeup signal. 324 */ 325 int NET_Timeout(JNIEnv *unused, int s, long timeout) { 326 long prevtime = 0, newtime; 327 struct timeval t; 328 fdEntry_t *fdEntry = getFdEntry(s); 329 330 /* 331 * Check that fd hasn't been closed. 332 */ 333 if (fdEntry == NULL) { 334 errno = EBADF; 335 return -1; 336 } 337 338 /* 339 * Pick up current time as may need to adjust timeout 340 */ 341 if (timeout > 0) { 342 gettimeofday(&t, NULL); 343 prevtime = t.tv_sec * 1000 + t.tv_usec / 1000; 344 } 345 346 for(;;) { 347 struct pollfd pfd; 348 int rv; 349 threadEntry_t self; 350 351 /* 352 * Poll the fd. If interrupted by our wakeup signal 353 * errno will be set to EBADF. 354 */ 355 pfd.fd = s; 356 pfd.events = POLLIN | POLLERR; 357 358 startOp(fdEntry, &self); 359 rv = poll(&pfd, 1, timeout); 360 endOp(fdEntry, &self); 361 362 /* 363 * If interrupted then adjust timeout. If timeout 364 * has expired return 0 (indicating timeout expired). 365 */ 366 if (rv < 0 && errno == EINTR) { 367 if (timeout > 0) { 368 gettimeofday(&t, NULL); 369 newtime = t.tv_sec * 1000 + t.tv_usec / 1000; 370 timeout -= newtime - prevtime; 371 if (timeout <= 0) { 372 return 0; 373 } 374 prevtime = newtime; 375 } 376 } else { 377 return rv; 378 } 379 380 } 381 }