4632N/A/*
4632N/A * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
4632N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4632N/A *
4632N/A * This code is free software; you can redistribute it and/or modify it
4632N/A * under the terms of the GNU General Public License version 2 only, as
4632N/A * published by the Free Software Foundation. Oracle designates this
4632N/A * particular file as subject to the "Classpath" exception as provided
4632N/A * by Oracle in the LICENSE file that accompanied this code.
4632N/A *
4632N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4632N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4632N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4632N/A * version 2 for more details (a copy is included in the LICENSE file that
4632N/A * accompanied this code).
4632N/A *
4632N/A * You should have received a copy of the GNU General Public License version
4632N/A * 2 along with this work; if not, write to the Free Software Foundation,
4632N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4632N/A *
4632N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4632N/A * or visit www.oracle.com if you need additional information or have any
4632N/A * questions.
4632N/A */
4632N/A
4632N/A#include <stdio.h>
4632N/A#include <stdlib.h>
4632N/A#include <signal.h>
4632N/A#include <pthread.h>
4632N/A#include <sys/types.h>
4632N/A#include <sys/socket.h>
4681N/A#include <sys/select.h>
4632N/A#include <sys/time.h>
4632N/A#include <sys/resource.h>
4632N/A#include <sys/uio.h>
4632N/A#include <unistd.h>
4632N/A#include <errno.h>
4632N/A
4632N/A#include <sys/poll.h>
4632N/A
4632N/A/*
4632N/A * Stack allocated by thread when doing blocking operation
4632N/A */
4632N/Atypedef struct threadEntry {
4632N/A pthread_t thr; /* this thread */
4632N/A struct threadEntry *next; /* next thread */
4632N/A int intr; /* interrupted */
4632N/A} threadEntry_t;
4632N/A
4632N/A/*
4632N/A * Heap allocated during initialized - one entry per fd
4632N/A */
4632N/Atypedef struct {
4632N/A pthread_mutex_t lock; /* fd lock */
4632N/A threadEntry_t *threads; /* threads blocked on fd */
4632N/A} fdEntry_t;
4632N/A
4632N/A/*
4632N/A * Signal to unblock thread
4632N/A */
4632N/Astatic int sigWakeup = SIGIO;
4632N/A
4632N/A/*
4632N/A * The fd table and the number of file descriptors
4632N/A */
4632N/Astatic fdEntry_t *fdTable;
4632N/Astatic int fdCount;
4632N/A
4632N/A/*
4632N/A * This limit applies if getlimit() returns unlimited.
4632N/A * Unfortunately, this means if someone wants a higher limt
4632N/A * then they have to set an explicit limit, higher than this,
4632N/A * which is probably counter-intuitive.
4632N/A */
4632N/A#define MAX_FD_COUNT 4096
4632N/A
4632N/A/*
4632N/A * Null signal handler
4632N/A */
4632N/Astatic void sig_wakeup(int sig) {
4632N/A}
4632N/A
4632N/A/*
4632N/A * Initialization routine (executed when library is loaded)
4632N/A * Allocate fd tables and sets up signal handler.
4632N/A */
4632N/Astatic void __attribute((constructor)) init() {
4632N/A struct rlimit nbr_files;
4632N/A sigset_t sigset;
4632N/A struct sigaction sa;
4654N/A int i;
4632N/A
4632N/A /*
4632N/A * Allocate table based on the maximum number of
4632N/A * file descriptors.
4632N/A */
4632N/A getrlimit(RLIMIT_NOFILE, &nbr_files);
4632N/A if (nbr_files.rlim_max == RLIM_INFINITY) {
4632N/A fdCount = MAX_FD_COUNT;
4632N/A } else {
4632N/A fdCount = nbr_files.rlim_max;
4632N/A }
4632N/A fdTable = (fdEntry_t *)calloc(fdCount, sizeof(fdEntry_t));
4632N/A if (fdTable == NULL) {
4632N/A fprintf(stderr, "library initialization failed - "
4632N/A "unable to allocate file descriptor table - out of memory");
4632N/A abort();
4632N/A }
4654N/A for (i=0; i<fdCount; i++) {
4654N/A pthread_mutex_init(&fdTable[i].lock, NULL);
4654N/A }
4632N/A
4632N/A /*
4632N/A * Setup the signal handler
4632N/A */
4632N/A sa.sa_handler = sig_wakeup;
4632N/A sa.sa_flags = 0;
4632N/A sigemptyset(&sa.sa_mask);
4632N/A sigaction(sigWakeup, &sa, NULL);
4632N/A
4632N/A sigemptyset(&sigset);
4632N/A sigaddset(&sigset, sigWakeup);
4632N/A sigprocmask(SIG_UNBLOCK, &sigset, NULL);
4632N/A}
4632N/A
4632N/A/*
4632N/A * Return the fd table for this fd or NULL is fd out
4632N/A * of range.
4632N/A */
4632N/Astatic inline fdEntry_t *getFdEntry(int fd)
4632N/A{
4632N/A if (fd < 0 || fd >= fdCount) {
4632N/A return NULL;
4632N/A }
4632N/A return &fdTable[fd];
4632N/A}
4632N/A
4632N/A/*
4632N/A * Start a blocking operation :-
4632N/A * Insert thread onto thread list for the fd.
4632N/A */
4632N/Astatic inline void startOp(fdEntry_t *fdEntry, threadEntry_t *self)
4632N/A{
4632N/A self->thr = pthread_self();
4632N/A self->intr = 0;
4632N/A
4632N/A pthread_mutex_lock(&(fdEntry->lock));
4632N/A {
4632N/A self->next = fdEntry->threads;
4632N/A fdEntry->threads = self;
4632N/A }
4632N/A pthread_mutex_unlock(&(fdEntry->lock));
4632N/A}
4632N/A
4632N/A/*
4632N/A * End a blocking operation :-
4632N/A * Remove thread from thread list for the fd
4632N/A * If fd has been interrupted then set errno to EBADF
4632N/A */
4632N/Astatic inline void endOp
4632N/A (fdEntry_t *fdEntry, threadEntry_t *self)
4632N/A{
4632N/A int orig_errno = errno;
4632N/A pthread_mutex_lock(&(fdEntry->lock));
4632N/A {
4632N/A threadEntry_t *curr, *prev=NULL;
4632N/A curr = fdEntry->threads;
4632N/A while (curr != NULL) {
4632N/A if (curr == self) {
4632N/A if (curr->intr) {
4632N/A orig_errno = EBADF;
4632N/A }
4632N/A if (prev == NULL) {
4632N/A fdEntry->threads = curr->next;
4632N/A } else {
4632N/A prev->next = curr->next;
4632N/A }
4632N/A break;
4632N/A }
4632N/A prev = curr;
4632N/A curr = curr->next;
4632N/A }
4632N/A }
4632N/A pthread_mutex_unlock(&(fdEntry->lock));
4632N/A errno = orig_errno;
4632N/A}
4632N/A
4632N/A/*
4632N/A * Close or dup2 a file descriptor ensuring that all threads blocked on
4632N/A * the file descriptor are notified via a wakeup signal.
4632N/A *
4632N/A * fd1 < 0 => close(fd2)
4632N/A * fd1 >= 0 => dup2(fd1, fd2)
4632N/A *
4632N/A * Returns -1 with errno set if operation fails.
4632N/A */
4632N/Astatic int closefd(int fd1, int fd2) {
4632N/A int rv, orig_errno;
4632N/A fdEntry_t *fdEntry = getFdEntry(fd2);
4632N/A if (fdEntry == NULL) {
4632N/A errno = EBADF;
4632N/A return -1;
4632N/A }
4632N/A
4632N/A /*
4632N/A * Lock the fd to hold-off additional I/O on this fd.
4632N/A */
4632N/A pthread_mutex_lock(&(fdEntry->lock));
4632N/A
4632N/A {
4632N/A /*
4632N/A * Send a wakeup signal to all threads blocked on this
4632N/A * file descriptor.
4632N/A */
4632N/A threadEntry_t *curr = fdEntry->threads;
4632N/A while (curr != NULL) {
4632N/A curr->intr = 1;
4632N/A pthread_kill( curr->thr, sigWakeup );
4632N/A curr = curr->next;
4632N/A }
4632N/A
4632N/A /*
4632N/A * And close/dup the file descriptor
4632N/A * (restart if interrupted by signal)
4632N/A */
4632N/A do {
4632N/A if (fd1 < 0) {
4632N/A rv = close(fd2);
4632N/A } else {
4632N/A rv = dup2(fd1, fd2);
4632N/A }
4632N/A } while (rv == -1 && errno == EINTR);
4632N/A
4632N/A }
4632N/A
4632N/A /*
4632N/A * Unlock without destroying errno
4632N/A */
4632N/A orig_errno = errno;
4632N/A pthread_mutex_unlock(&(fdEntry->lock));
4632N/A errno = orig_errno;
4632N/A
4632N/A return rv;
4632N/A}
4632N/A
4632N/A/*
4632N/A * Wrapper for dup2 - same semantics as dup2 system call except
4632N/A * that any threads blocked in an I/O system call on fd2 will be
4632N/A * preempted and return -1/EBADF;
4632N/A */
4632N/Aint NET_Dup2(int fd, int fd2) {
4632N/A if (fd < 0) {
4632N/A errno = EBADF;
4632N/A return -1;
4632N/A }
4632N/A return closefd(fd, fd2);
4632N/A}
4632N/A
4632N/A/*
4632N/A * Wrapper for close - same semantics as close system call
4632N/A * except that any threads blocked in an I/O on fd will be
4632N/A * preempted and the I/O system call will return -1/EBADF.
4632N/A */
4632N/Aint NET_SocketClose(int fd) {
4632N/A return closefd(-1, fd);
4632N/A}
4632N/A
4632N/A/************** Basic I/O operations here ***************/
4632N/A
4632N/A/*
4632N/A * Macro to perform a blocking IO operation. Restarts
4632N/A * automatically if interrupted by signal (other than
4632N/A * our wakeup signal)
4632N/A */
4632N/A#define BLOCKING_IO_RETURN_INT(FD, FUNC) { \
4632N/A int ret; \
4632N/A threadEntry_t self; \
4632N/A fdEntry_t *fdEntry = getFdEntry(FD); \
4632N/A if (fdEntry == NULL) { \
4632N/A errno = EBADF; \
4632N/A return -1; \
4632N/A } \
4632N/A do { \
4632N/A startOp(fdEntry, &self); \
4632N/A ret = FUNC; \
4632N/A endOp(fdEntry, &self); \
4632N/A } while (ret == -1 && errno == EINTR); \
4632N/A return ret; \
4632N/A}
4632N/A
4632N/Aint NET_Read(int s, void* buf, size_t len) {
4632N/A BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, 0) );
4632N/A}
4632N/A
4632N/Aint NET_ReadV(int s, const struct iovec * vector, int count) {
4632N/A BLOCKING_IO_RETURN_INT( s, readv(s, vector, count) );
4632N/A}
4632N/A
4632N/Aint NET_RecvFrom(int s, void *buf, int len, unsigned int flags,
4632N/A struct sockaddr *from, int *fromlen) {
4654N/A /* casting int *fromlen -> socklen_t* Both are ints */
4654N/A BLOCKING_IO_RETURN_INT( s, recvfrom(s, buf, len, flags, from, (socklen_t *)fromlen) );
4632N/A}
4632N/A
4632N/Aint NET_Send(int s, void *msg, int len, unsigned int flags) {
4632N/A BLOCKING_IO_RETURN_INT( s, send(s, msg, len, flags) );
4632N/A}
4632N/A
4632N/Aint NET_WriteV(int s, const struct iovec * vector, int count) {
4632N/A BLOCKING_IO_RETURN_INT( s, writev(s, vector, count) );
4632N/A}
4632N/A
4632N/Aint NET_SendTo(int s, const void *msg, int len, unsigned int
4632N/A flags, const struct sockaddr *to, int tolen) {
4632N/A BLOCKING_IO_RETURN_INT( s, sendto(s, msg, len, flags, to, tolen) );
4632N/A}
4632N/A
4632N/Aint NET_Accept(int s, struct sockaddr *addr, int *addrlen) {
4632N/A socklen_t len = *addrlen;
4632N/A int error = accept(s, addr, &len);
4632N/A if (error != -1)
4632N/A *addrlen = (int)len;
4632N/A BLOCKING_IO_RETURN_INT( s, error );
4632N/A}
4632N/A
4632N/Aint NET_Connect(int s, struct sockaddr *addr, int addrlen) {
4632N/A BLOCKING_IO_RETURN_INT( s, connect(s, addr, addrlen) );
4632N/A}
4632N/A
4632N/A#ifndef USE_SELECT
4632N/Aint NET_Poll(struct pollfd *ufds, unsigned int nfds, int timeout) {
4632N/A BLOCKING_IO_RETURN_INT( ufds[0].fd, poll(ufds, nfds, timeout) );
4632N/A}
4632N/A#else
4632N/Aint NET_Select(int s, fd_set *readfds, fd_set *writefds,
4632N/A fd_set *exceptfds, struct timeval *timeout) {
4632N/A BLOCKING_IO_RETURN_INT( s-1,
4632N/A select(s, readfds, writefds, exceptfds, timeout) );
4632N/A}
4632N/A#endif
4632N/A
4632N/A/*
4681N/A * Wrapper for select(s, timeout). We are using select() on Mac OS due to Bug 7131399.
4632N/A * Auto restarts with adjusted timeout if interrupted by
4632N/A * signal other than our wakeup signal.
4632N/A */
4632N/Aint NET_Timeout(int s, long timeout) {
4632N/A long prevtime = 0, newtime;
4681N/A struct timeval t, *tp = &t;
4632N/A fdEntry_t *fdEntry = getFdEntry(s);
4632N/A
4632N/A /*
4632N/A * Check that fd hasn't been closed.
4632N/A */
4632N/A if (fdEntry == NULL) {
4632N/A errno = EBADF;
4632N/A return -1;
4632N/A }
4632N/A
4632N/A /*
4632N/A * Pick up current time as may need to adjust timeout
4632N/A */
4632N/A if (timeout > 0) {
4681N/A /* Timed */
4681N/A struct timeval now;
4681N/A gettimeofday(&now, NULL);
4681N/A prevtime = now.tv_sec * 1000 + now.tv_usec / 1000;
4681N/A t.tv_sec = timeout / 1000;
4681N/A t.tv_usec = (timeout % 1000) * 1000;
4681N/A } else if (timeout < 0) {
4681N/A /* Blocking */
4681N/A tp = 0;
4681N/A } else {
4681N/A /* Poll */
4681N/A t.tv_sec = 0;
4681N/A t.tv_usec = 0;
4632N/A }
4632N/A
4632N/A for(;;) {
4681N/A fd_set rfds;
4632N/A int rv;
4632N/A threadEntry_t self;
4632N/A
4632N/A /*
4681N/A * call select on the fd. If interrupted by our wakeup signal
4632N/A * errno will be set to EBADF.
4632N/A */
4681N/A FD_ZERO(&rfds);
4681N/A FD_SET(s, &rfds);
4632N/A
4632N/A startOp(fdEntry, &self);
4681N/A rv = select(s+1, &rfds, 0, 0, tp);
4632N/A endOp(fdEntry, &self);
4632N/A
4632N/A /*
4632N/A * If interrupted then adjust timeout. If timeout
4632N/A * has expired return 0 (indicating timeout expired).
4632N/A */
4632N/A if (rv < 0 && errno == EINTR) {
4632N/A if (timeout > 0) {
4681N/A struct timeval now;
4681N/A gettimeofday(&now, NULL);
4681N/A newtime = now.tv_sec * 1000 + now.tv_usec / 1000;
4632N/A timeout -= newtime - prevtime;
4632N/A if (timeout <= 0) {
4632N/A return 0;
4632N/A }
4632N/A prevtime = newtime;
4681N/A t.tv_sec = timeout / 1000;
4681N/A t.tv_usec = (timeout % 1000) * 1000;
4632N/A }
4632N/A } else {
4632N/A return rv;
4632N/A }
4632N/A
4632N/A }
4632N/A}