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