1 /*
   2  * Copyright (c) 2001, 2019, 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 <assert.h>
  27 #include <limits.h>
  28 #include <stdio.h>
  29 #include <stdlib.h>
  30 #include <signal.h>
  31 #include <pthread.h>
  32 #include <sys/types.h>
  33 #include <sys/socket.h>
  34 #include <sys/time.h>
  35 #include <sys/resource.h>
  36 #include <sys/uio.h>
  37 #include <unistd.h>
  38 #include <errno.h>
  39 #include <poll.h>
  40 #include "jvm.h"
  41 #include "net_util.h"
  42 
  43 /*
  44  * Stack allocated by thread when doing blocking operation
  45  */
  46 typedef struct threadEntry {
  47     pthread_t thr;                      /* this thread */
  48     struct threadEntry *next;           /* next thread */
  49     int intr;                           /* interrupted */
  50 } threadEntry_t;
  51 
  52 /*
  53  * Heap allocated during initialized - one entry per fd
  54  */
  55 typedef struct {
  56     pthread_mutex_t lock;               /* fd lock */
  57     threadEntry_t *threads;             /* threads blocked on fd */
  58 } fdEntry_t;
  59 
  60 /*
  61  * Signal to unblock thread
  62  */
  63 static int sigWakeup;
  64 
  65 /*
  66  * fdTable holds one entry per file descriptor, up to a certain
  67  * maximum.
  68  * Theoretically, the number of possible file descriptors can get
  69  * large, though usually it does not. Entries for small value file
  70  * descriptors are kept in a simple table, which covers most scenarios.
  71  * Entries for large value file descriptors are kept in an overflow
  72  * table, which is organized as a sparse two dimensional array whose
  73  * slabs are allocated on demand. This covers all corner cases while
  74  * keeping memory consumption reasonable.
  75  */
  76 
  77 /* Base table for low value file descriptors */
  78 static fdEntry_t* fdTable = NULL;
  79 /* Maximum size of base table (in number of entries). */
  80 static const int fdTableMaxSize = 0x1000; /* 4K */
  81 /* Actual size of base table (in number of entries) */
  82 static int fdTableLen = 0;
  83 /* Max. theoretical number of file descriptors on system. */
  84 static int fdLimit = 0;
  85 
  86 /* Overflow table, should base table not be large enough. Organized as
  87  *   an array of n slabs, each holding 64k entries.
  88  */
  89 static fdEntry_t** fdOverflowTable = NULL;
  90 /* Number of slabs in the overflow table */
  91 static int fdOverflowTableLen = 0;
  92 /* Number of entries in one slab */
  93 static const int fdOverflowTableSlabSize = 0x10000; /* 64k */
  94 pthread_mutex_t fdOverflowTableLock = PTHREAD_MUTEX_INITIALIZER;
  95 
  96 /*
  97  * Null signal handler
  98  */
  99 static void sig_wakeup(int sig) {
 100 }
 101 
 102 /*
 103  * Initialization routine (executed when library is loaded)
 104  * Allocate fd tables and sets up signal handler.
 105  */
 106 static void __attribute((constructor)) init() {
 107     struct rlimit nbr_files;
 108     sigset_t sigset;
 109     struct sigaction sa;
 110     int i = 0;
 111 
 112     /* Determine the maximum number of possible file descriptors. */
 113     if (-1 == getrlimit(RLIMIT_NOFILE, &nbr_files)) {
 114         fprintf(stderr, "library initialization failed - "
 115                 "unable to get max # of allocated fds\n");
 116         abort();
 117     }
 118     if (nbr_files.rlim_max != RLIM_INFINITY) {
 119         fdLimit = nbr_files.rlim_max;
 120     } else {
 121         /* We just do not know. */
 122         fdLimit = INT_MAX;
 123     }
 124 
 125     /* Allocate table for low value file descriptors. */
 126     fdTableLen = fdLimit < fdTableMaxSize ? fdLimit : fdTableMaxSize;
 127     fdTable = (fdEntry_t*) calloc(fdTableLen, sizeof(fdEntry_t));
 128     if (fdTable == NULL) {
 129         fprintf(stderr, "library initialization failed - "
 130                 "unable to allocate file descriptor table - out of memory");
 131         abort();
 132     } else {
 133         for (i = 0; i < fdTableLen; i ++) {
 134             pthread_mutex_init(&fdTable[i].lock, NULL);
 135         }
 136     }
 137 
 138     /* Allocate overflow table, if needed */
 139     if (fdLimit > fdTableMaxSize) {
 140         fdOverflowTableLen = ((fdLimit - fdTableMaxSize) / fdOverflowTableSlabSize) + 1;
 141         fdOverflowTable = (fdEntry_t**) calloc(fdOverflowTableLen, sizeof(fdEntry_t*));
 142         if (fdOverflowTable == NULL) {
 143             fprintf(stderr, "library initialization failed - "
 144                     "unable to allocate file descriptor overflow table - out of memory");
 145             abort();
 146         }
 147     }
 148 
 149     /*
 150      * Setup the signal handler
 151      */
 152     sigWakeup = SIGRTMAX - 2;
 153     sa.sa_handler = sig_wakeup;
 154     sa.sa_flags   = 0;
 155     sigemptyset(&sa.sa_mask);
 156     sigaction(sigWakeup, &sa, NULL);
 157 
 158     sigemptyset(&sigset);
 159     sigaddset(&sigset, sigWakeup);
 160     sigprocmask(SIG_UNBLOCK, &sigset, NULL);
 161 }
 162 
 163 /*
 164  * Return the fd table for this fd.
 165  */
 166 static inline fdEntry_t *getFdEntry(int fd)
 167 {
 168     fdEntry_t* result = NULL;
 169 
 170     if (fd < 0) {
 171         return NULL;
 172     }
 173 
 174     /* This should not happen. If it does, our assumption about
 175      * max. fd value was wrong. */
 176     assert(fd < fdLimit);
 177 
 178     if (fd < fdTableMaxSize) {
 179         /* fd is in base table. */
 180         assert(fd < fdTableLen);
 181         result = &fdTable[fd];
 182     } else {
 183         /* fd is in overflow table. */
 184         const int indexInOverflowTable = fd - fdTableMaxSize;
 185         const int rootindex = indexInOverflowTable / fdOverflowTableSlabSize;
 186         const int slabindex = indexInOverflowTable % fdOverflowTableSlabSize;
 187         fdEntry_t* slab = NULL;
 188         assert(rootindex < fdOverflowTableLen);
 189         assert(slabindex < fdOverflowTableSlabSize);
 190         pthread_mutex_lock(&fdOverflowTableLock);
 191         /* Allocate new slab in overflow table if needed */
 192         if (fdOverflowTable[rootindex] == NULL) {
 193             fdEntry_t* const newSlab =
 194                 (fdEntry_t*)calloc(fdOverflowTableSlabSize, sizeof(fdEntry_t));
 195             if (newSlab == NULL) {
 196                 fprintf(stderr, "Unable to allocate file descriptor overflow"
 197                         " table slab - out of memory");
 198                 pthread_mutex_unlock(&fdOverflowTableLock);
 199                 abort();
 200             } else {
 201                 int i;
 202                 for (i = 0; i < fdOverflowTableSlabSize; i ++) {
 203                     pthread_mutex_init(&newSlab[i].lock, NULL);
 204                 }
 205                 fdOverflowTable[rootindex] = newSlab;
 206             }
 207         }
 208         pthread_mutex_unlock(&fdOverflowTableLock);
 209         slab = fdOverflowTable[rootindex];
 210         result = &slab[slabindex];
 211     }
 212 
 213     return result;
 214 
 215 }
 216 
 217 /*
 218  * Start a blocking operation :-
 219  *    Insert thread onto thread list for the fd.
 220  */
 221 static inline void startOp(fdEntry_t *fdEntry, threadEntry_t *self)
 222 {
 223     self->thr = pthread_self();
 224     self->intr = 0;
 225 
 226     pthread_mutex_lock(&(fdEntry->lock));
 227     {
 228         self->next = fdEntry->threads;
 229         fdEntry->threads = self;
 230     }
 231     pthread_mutex_unlock(&(fdEntry->lock));
 232 }
 233 
 234 /*
 235  * End a blocking operation :-
 236  *     Remove thread from thread list for the fd
 237  *     If fd has been interrupted then set errno to EBADF
 238  */
 239 static inline void endOp
 240     (fdEntry_t *fdEntry, threadEntry_t *self)
 241 {
 242     int orig_errno = errno;
 243     pthread_mutex_lock(&(fdEntry->lock));
 244     {
 245         threadEntry_t *curr, *prev=NULL;
 246         curr = fdEntry->threads;
 247         while (curr != NULL) {
 248             if (curr == self) {
 249                 if (curr->intr) {
 250                     orig_errno = EBADF;
 251                 }
 252                 if (prev == NULL) {
 253                     fdEntry->threads = curr->next;
 254                 } else {
 255                     prev->next = curr->next;
 256                 }
 257                 break;
 258             }
 259             prev = curr;
 260             curr = curr->next;
 261         }
 262     }
 263     pthread_mutex_unlock(&(fdEntry->lock));
 264     errno = orig_errno;
 265 }
 266 
 267 /*
 268  * Close or dup2 a file descriptor ensuring that all threads blocked on
 269  * the file descriptor are notified via a wakeup signal.
 270  *
 271  *      fd1 < 0    => close(fd2)
 272  *      fd1 >= 0   => dup2(fd1, fd2)
 273  *
 274  * Returns -1 with errno set if operation fails.
 275  */
 276 static int closefd(int fd1, int fd2) {
 277     int rv, orig_errno;
 278     fdEntry_t *fdEntry = getFdEntry(fd2);
 279     if (fdEntry == NULL) {
 280         errno = EBADF;
 281         return -1;
 282     }
 283 
 284     /*
 285      * Lock the fd to hold-off additional I/O on this fd.
 286      */
 287     pthread_mutex_lock(&(fdEntry->lock));
 288 
 289     {
 290         /*
 291          * And close/dup the file descriptor
 292          * (restart if interrupted by signal)
 293          */
 294         if (fd1 < 0) {
 295             rv = close(fd2);
 296         } else {
 297             do {
 298                 rv = dup2(fd1, fd2);
 299             } while (rv == -1 && errno == EINTR);
 300         }
 301 
 302         /*
 303          * Send a wakeup signal to all threads blocked on this
 304          * file descriptor.
 305          */
 306         threadEntry_t *curr = fdEntry->threads;
 307         while (curr != NULL) {
 308             curr->intr = 1;
 309             pthread_kill( curr->thr, sigWakeup );
 310             curr = curr->next;
 311         }
 312     }
 313 
 314     /*
 315      * Unlock without destroying errno
 316      */
 317     orig_errno = errno;
 318     pthread_mutex_unlock(&(fdEntry->lock));
 319     errno = orig_errno;
 320 
 321     return rv;
 322 }
 323 
 324 /*
 325  * Wrapper for dup2 - same semantics as dup2 system call except
 326  * that any threads blocked in an I/O system call on fd2 will be
 327  * preempted and return -1/EBADF;
 328  */
 329 int NET_Dup2(int fd, int fd2) {
 330     if (fd < 0) {
 331         errno = EBADF;
 332         return -1;
 333     }
 334     return closefd(fd, fd2);
 335 }
 336 
 337 /*
 338  * Wrapper for close - same semantics as close system call
 339  * except that any threads blocked in an I/O on fd will be
 340  * preempted and the I/O system call will return -1/EBADF.
 341  */
 342 int NET_SocketClose(int fd) {
 343     return closefd(-1, fd);
 344 }
 345 
 346 /************** Basic I/O operations here ***************/
 347 
 348 /*
 349  * Macro to perform a blocking IO operation. Restarts
 350  * automatically if interrupted by signal (other than
 351  * our wakeup signal)
 352  */
 353 #define BLOCKING_IO_RETURN_INT(FD, FUNC) {      \
 354     int ret;                                    \
 355     threadEntry_t self;                         \
 356     fdEntry_t *fdEntry = getFdEntry(FD);        \
 357     if (fdEntry == NULL) {                      \
 358         errno = EBADF;                          \
 359         return -1;                              \
 360     }                                           \
 361     do {                                        \
 362         startOp(fdEntry, &self);                \
 363         ret = FUNC;                             \
 364         endOp(fdEntry, &self);                  \
 365     } while (ret == -1 && errno == EINTR);      \
 366     return ret;                                 \
 367 }
 368 
 369 int NET_Read(int s, void* buf, size_t len) {
 370     BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, 0) );
 371 }
 372 
 373 int NET_NonBlockingRead(int s, void* buf, size_t len) {
 374     BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, MSG_DONTWAIT) );
 375 }
 376 
 377 int NET_ReadV(int s, const struct iovec * vector, int count) {
 378     BLOCKING_IO_RETURN_INT( s, readv(s, vector, count) );
 379 }
 380 
 381 int NET_RecvFrom(int s, void *buf, int len, unsigned int flags,
 382        struct sockaddr *from, socklen_t *fromlen) {
 383     BLOCKING_IO_RETURN_INT( s, recvfrom(s, buf, len, flags, from, fromlen) );
 384 }
 385 
 386 int NET_Send(int s, void *msg, int len, unsigned int flags) {
 387     BLOCKING_IO_RETURN_INT( s, send(s, msg, len, flags) );
 388 }
 389 
 390 int NET_SendTo(int s, const void *msg, int len,  unsigned  int
 391        flags, const struct sockaddr *to, int tolen) {
 392     BLOCKING_IO_RETURN_INT( s, sendto(s, msg, len, flags, to, tolen) );
 393 }
 394 
 395 int NET_Accept(int s, struct sockaddr *addr, socklen_t *addrlen) {
 396     BLOCKING_IO_RETURN_INT( s, accept(s, addr, addrlen) );
 397 }
 398 
 399 int NET_Connect(int s, struct sockaddr *addr, int addrlen) {
 400     BLOCKING_IO_RETURN_INT( s, connect(s, addr, addrlen) );
 401 }
 402 
 403 int NET_Poll(struct pollfd *ufds, unsigned int nfds, int timeout) {
 404     BLOCKING_IO_RETURN_INT( ufds[0].fd, poll(ufds, nfds, timeout) );
 405 }
 406 
 407 /*
 408  * Wrapper for poll(s, timeout).
 409  * Auto restarts with adjusted timeout if interrupted by
 410  * signal other than our wakeup signal.
 411  */
 412 int NET_Timeout(JNIEnv *env, int s, long timeout, jlong nanoTimeStamp) {
 413     jlong prevNanoTime = nanoTimeStamp;
 414     jlong nanoTimeout = (jlong)timeout * NET_NSEC_PER_MSEC;
 415     fdEntry_t *fdEntry = getFdEntry(s);
 416 
 417     /*
 418      * Check that fd hasn't been closed.
 419      */
 420     if (fdEntry == NULL) {
 421         errno = EBADF;
 422         return -1;
 423     }
 424 
 425     for(;;) {
 426         struct pollfd pfd;
 427         int rv;
 428         threadEntry_t self;
 429 
 430         /*
 431          * Poll the fd. If interrupted by our wakeup signal
 432          * errno will be set to EBADF.
 433          */
 434         pfd.fd = s;
 435         pfd.events = POLLIN | POLLERR;
 436 
 437         startOp(fdEntry, &self);
 438         rv = poll(&pfd, 1, nanoTimeout / NET_NSEC_PER_MSEC);
 439         endOp(fdEntry, &self);
 440         /*
 441          * If interrupted then adjust timeout. If timeout
 442          * has expired return 0 (indicating timeout expired).
 443          */
 444         if (rv < 0 && errno == EINTR) {
 445             jlong newNanoTime = JVM_NanoTime(env, 0);
 446             nanoTimeout -= newNanoTime - prevNanoTime;
 447             if (nanoTimeout < NET_NSEC_PER_MSEC) {
 448                 return 0;
 449             }
 450             prevNanoTime = newNanoTime;
 451         } else {
 452             return rv;
 453         }
 454     }
 455 }