jdk/src/java.base/linux/native/libnet/linux_close.c
author erikj
Mon, 26 Jan 2015 10:24:25 +0100
changeset 28658 344426303820
parent 25859 jdk/src/java.base/unix/native/libnet/linux_close.c@3317bb8137f4
child 37609 332ee9a0ec11
permissions -rw-r--r--
8055190: Cleanup include and exclude of core-libs native libraries after source code restructure Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 15522
diff changeset
     2
 * Copyright (c) 2001, 2013, 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>
23043
15face72cb3b 8035897: Better memory allocation for file descriptors greater than 1024 on macosx
chegar
parents: 23033
diff changeset
    37
#include <sys/poll.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Stack allocated by thread when doing blocking operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
typedef struct threadEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    pthread_t thr;                      /* this thread */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    struct threadEntry *next;           /* next thread */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    int intr;                           /* interrupted */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
} threadEntry_t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Heap allocated during initialized - one entry per fd
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
typedef struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    pthread_mutex_t lock;               /* fd lock */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    threadEntry_t *threads;             /* threads blocked on fd */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
} fdEntry_t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * Signal to unblock thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
static int sigWakeup = (__SIGRTMAX - 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * The fd table and the number of file descriptors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
static fdEntry_t *fdTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
static int fdCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * Null signal handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
static void sig_wakeup(int sig) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * Initialization routine (executed when library is loaded)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * Allocate fd tables and sets up signal handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
static void __attribute((constructor)) init() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    struct rlimit nbr_files;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    sigset_t sigset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    struct sigaction sa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Allocate table based on the maximum number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * file descriptors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    getrlimit(RLIMIT_NOFILE, &nbr_files);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    fdCount = nbr_files.rlim_max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    fdTable = (fdEntry_t *)calloc(fdCount, sizeof(fdEntry_t));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    if (fdTable == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        fprintf(stderr, "library initialization failed - "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                "unable to allocate file descriptor table - out of memory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        abort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Setup the signal handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    sa.sa_handler = sig_wakeup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    sa.sa_flags   = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    sigemptyset(&sa.sa_mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    sigaction(sigWakeup, &sa, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    sigemptyset(&sigset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    sigaddset(&sigset, sigWakeup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    sigprocmask(SIG_UNBLOCK, &sigset, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * Return the fd table for this fd or NULL is fd out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * of range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
static inline fdEntry_t *getFdEntry(int fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
{
8548
130426abe301 6849693: index of fdTable should be less than num. of fdTable in jdk7
chegar
parents: 5506
diff changeset
   114
    if (fd < 0 || fd >= fdCount) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    return &fdTable[fd];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * Start a blocking operation :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *    Insert thread onto thread list for the fd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
static inline void startOp(fdEntry_t *fdEntry, threadEntry_t *self)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    self->thr = pthread_self();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    self->intr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    pthread_mutex_lock(&(fdEntry->lock));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        self->next = fdEntry->threads;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        fdEntry->threads = self;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    pthread_mutex_unlock(&(fdEntry->lock));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * End a blocking operation :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 *     Remove thread from thread list for the fd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 *     If fd has been interrupted then set errno to EBADF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
static inline void endOp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    (fdEntry_t *fdEntry, threadEntry_t *self)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    int orig_errno = errno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    pthread_mutex_lock(&(fdEntry->lock));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        threadEntry_t *curr, *prev=NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        curr = fdEntry->threads;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        while (curr != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            if (curr == self) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                if (curr->intr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    orig_errno = EBADF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                if (prev == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    fdEntry->threads = curr->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    prev->next = curr->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            prev = curr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            curr = curr->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    pthread_mutex_unlock(&(fdEntry->lock));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    errno = orig_errno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * Close or dup2 a file descriptor ensuring that all threads blocked on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * the file descriptor are notified via a wakeup signal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 *      fd1 < 0    => close(fd2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 *      fd1 >= 0   => dup2(fd1, fd2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 * Returns -1 with errno set if operation fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
static int closefd(int fd1, int fd2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    int rv, orig_errno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    fdEntry_t *fdEntry = getFdEntry(fd2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    if (fdEntry == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        errno = EBADF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Lock the fd to hold-off additional I/O on this fd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    pthread_mutex_lock(&(fdEntry->lock));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
         * And close/dup the file descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
         * (restart if interrupted by signal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            if (fd1 < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                rv = close(fd2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                rv = dup2(fd1, fd2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        } while (rv == -1 && errno == EINTR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
15522
e6d9ef53b499 8006395: Race in async socket close on Linux
chegar
parents: 9035
diff changeset
   205
        /*
e6d9ef53b499 8006395: Race in async socket close on Linux
chegar
parents: 9035
diff changeset
   206
         * Send a wakeup signal to all threads blocked on this
e6d9ef53b499 8006395: Race in async socket close on Linux
chegar
parents: 9035
diff changeset
   207
         * file descriptor.
e6d9ef53b499 8006395: Race in async socket close on Linux
chegar
parents: 9035
diff changeset
   208
         */
e6d9ef53b499 8006395: Race in async socket close on Linux
chegar
parents: 9035
diff changeset
   209
        threadEntry_t *curr = fdEntry->threads;
e6d9ef53b499 8006395: Race in async socket close on Linux
chegar
parents: 9035
diff changeset
   210
        while (curr != NULL) {
e6d9ef53b499 8006395: Race in async socket close on Linux
chegar
parents: 9035
diff changeset
   211
            curr->intr = 1;
e6d9ef53b499 8006395: Race in async socket close on Linux
chegar
parents: 9035
diff changeset
   212
            pthread_kill( curr->thr, sigWakeup );
e6d9ef53b499 8006395: Race in async socket close on Linux
chegar
parents: 9035
diff changeset
   213
            curr = curr->next;
e6d9ef53b499 8006395: Race in async socket close on Linux
chegar
parents: 9035
diff changeset
   214
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Unlock without destroying errno
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    orig_errno = errno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    pthread_mutex_unlock(&(fdEntry->lock));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    errno = orig_errno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
 * Wrapper for dup2 - same semantics as dup2 system call except
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
 * that any threads blocked in an I/O system call on fd2 will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
 * preempted and return -1/EBADF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
int NET_Dup2(int fd, int fd2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    if (fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        errno = EBADF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    return closefd(fd, fd2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
 * Wrapper for close - same semantics as close system call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 * except that any threads blocked in an I/O on fd will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 * preempted and the I/O system call will return -1/EBADF.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
int NET_SocketClose(int fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    return closefd(-1, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
/************** Basic I/O operations here ***************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
 * Macro to perform a blocking IO operation. Restarts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
 * automatically if interrupted by signal (other than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
 * our wakeup signal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
#define BLOCKING_IO_RETURN_INT(FD, FUNC) {      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    int ret;                                    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    threadEntry_t self;                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    fdEntry_t *fdEntry = getFdEntry(FD);        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    if (fdEntry == NULL) {                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        errno = EBADF;                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        return -1;                              \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    do {                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        startOp(fdEntry, &self);                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        ret = FUNC;                             \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        endOp(fdEntry, &self);                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    } while (ret == -1 && errno == EINTR);      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    return ret;                                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
int NET_Read(int s, void* buf, size_t len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, 0) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
int NET_ReadV(int s, const struct iovec * vector, int count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    BLOCKING_IO_RETURN_INT( s, readv(s, vector, count) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
int NET_RecvFrom(int s, void *buf, int len, unsigned int flags,
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   281
       struct sockaddr *from, socklen_t *fromlen) {
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   282
    BLOCKING_IO_RETURN_INT( s, recvfrom(s, buf, len, flags, from, fromlen) );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
int NET_Send(int s, void *msg, int len, unsigned int flags) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    BLOCKING_IO_RETURN_INT( s, send(s, msg, len, flags) );
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_WriteV(int s, const struct iovec * vector, int count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    BLOCKING_IO_RETURN_INT( s, writev(s, vector, count) );
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_SendTo(int s, const void *msg, int len,  unsigned  int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
       flags, const struct sockaddr *to, int tolen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    BLOCKING_IO_RETURN_INT( s, sendto(s, msg, len, flags, to, tolen) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   298
int NET_Accept(int s, struct sockaddr *addr, socklen_t *addrlen) {
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
   299
    BLOCKING_IO_RETURN_INT( s, accept(s, addr, addrlen) );
2
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_Connect(int s, struct sockaddr *addr, int addrlen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    BLOCKING_IO_RETURN_INT( s, connect(s, addr, addrlen) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
int NET_Poll(struct pollfd *ufds, unsigned int nfds, int timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    BLOCKING_IO_RETURN_INT( ufds[0].fd, poll(ufds, nfds, timeout) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
 * Wrapper for poll(s, timeout).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
 * Auto restarts with adjusted timeout if interrupted by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
 * signal other than our wakeup signal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
 */
23048
cf8cda42cd52 8036584: Review comments from 8035897
chegar
parents: 23043
diff changeset
   315
int NET_Timeout(int s, long timeout) {
910
1f53246fb014 6729881: Compiler warning in networking native code
chegar
parents: 2
diff changeset
   316
    long prevtime = 0, newtime;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    struct timeval t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    fdEntry_t *fdEntry = getFdEntry(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * Check that fd hasn't been closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    if (fdEntry == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        errno = EBADF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * Pick up current time as may need to adjust timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    if (timeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        gettimeofday(&t, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        prevtime = t.tv_sec * 1000  +  t.tv_usec / 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    for(;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        struct pollfd pfd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        int rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        threadEntry_t self;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
         * Poll the fd. If interrupted by our wakeup signal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
         * errno will be set to EBADF.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        pfd.fd = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        pfd.events = POLLIN | POLLERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        startOp(fdEntry, &self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        rv = poll(&pfd, 1, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        endOp(fdEntry, &self);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
         * If interrupted then adjust timeout. If timeout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
         * has expired return 0 (indicating timeout expired).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if (rv < 0 && errno == EINTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            if (timeout > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                gettimeofday(&t, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                newtime = t.tv_sec * 1000  +  t.tv_usec / 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                timeout -= newtime - prevtime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                if (timeout <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                prevtime = newtime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
}