Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/transport/socket/socket_md.c
48472 views
/*1* Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include <stdlib.h>26#include <sys/types.h>27#include <sys/socket.h>28#include <netinet/in.h>29#include <arpa/inet.h>30#include <unistd.h>31#include <fcntl.h>32#include <errno.h>33#include <string.h>34#include <sys/time.h>35#ifdef __solaris__36#include <thread.h>37#else38#include <pthread.h>39#include <sys/poll.h>40#endif4142#include "socket_md.h"43#include "sysSocket.h"4445int46dbgsysListen(int fd, int backlog) {47return listen(fd, backlog);48}4950int51dbgsysConnect(int fd, struct sockaddr *name, socklen_t namelen) {52int rv = connect(fd, name, namelen);53if (rv < 0 && (errno == EINPROGRESS || errno == EINTR)) {54return DBG_EINPROGRESS;55} else {56return rv;57}58}5960int61dbgsysFinishConnect(int fd, int timeout) {62int rv = dbgsysPoll(fd, 0, 1, timeout);63if (rv == 0) {64return DBG_ETIMEOUT;65}66if (rv > 0) {67return 0;68}69return rv;70}7172int73dbgsysAccept(int fd, struct sockaddr *name, socklen_t *namelen) {74int rv;75for (;;) {76rv = accept(fd, name, namelen);77if (rv >= 0) {78return rv;79}80if (errno != ECONNABORTED && errno != EINTR) {81return rv;82}83}84}8586int87dbgsysRecvFrom(int fd, char *buf, size_t nBytes,88int flags, struct sockaddr *from, socklen_t *fromlen) {89int rv;90do {91rv = recvfrom(fd, buf, nBytes, flags, from, fromlen);92} while (rv == -1 && errno == EINTR);9394return rv;95}9697int98dbgsysSendTo(int fd, char *buf, size_t len,99int flags, struct sockaddr *to, socklen_t tolen) {100int rv;101do {102rv = sendto(fd, buf, len, flags, to, tolen);103} while (rv == -1 && errno == EINTR);104105return rv;106}107108int109dbgsysRecv(int fd, char *buf, size_t nBytes, int flags) {110int rv;111do {112rv = recv(fd, buf, nBytes, flags);113} while (rv == -1 && errno == EINTR);114115return rv;116}117118int119dbgsysSend(int fd, char *buf, size_t nBytes, int flags) {120int rv;121do {122rv = send(fd, buf, nBytes, flags);123} while (rv == -1 && errno == EINTR);124125return rv;126}127128struct hostent *129dbgsysGetHostByName(char *hostname) {130return gethostbyname(hostname);131}132133unsigned short134dbgsysHostToNetworkShort(unsigned short hostshort) {135return htons(hostshort);136}137138int139dbgsysSocket(int domain, int type, int protocol) {140return socket(domain, type, protocol);141}142143int dbgsysSocketClose(int fd) {144int rv;145do {146rv = close(fd);147} while (rv == -1 && errno == EINTR);148149return rv;150}151152int153dbgsysBind(int fd, struct sockaddr *name, socklen_t namelen) {154return bind(fd, name, namelen);155}156157uint32_t158dbgsysInetAddr(const char* cp) {159return (uint32_t)inet_addr(cp);160}161162uint32_t163dbgsysHostToNetworkLong(uint32_t hostlong) {164return htonl(hostlong);165}166167unsigned short168dbgsysNetworkToHostShort(unsigned short netshort) {169return ntohs(netshort);170}171172int173dbgsysGetSocketName(int fd, struct sockaddr *name, socklen_t *namelen) {174return getsockname(fd, name, namelen);175}176177uint32_t178dbgsysNetworkToHostLong(uint32_t netlong) {179return ntohl(netlong);180}181182183int184dbgsysSetSocketOption(int fd, jint cmd, jboolean on, jvalue value)185{186if (cmd == TCP_NODELAY) {187struct protoent *proto = getprotobyname("TCP");188int tcp_level = (proto == 0 ? IPPROTO_TCP: proto->p_proto);189uint32_t onl = (uint32_t)on;190191if (setsockopt(fd, tcp_level, TCP_NODELAY,192(char *)&onl, sizeof(uint32_t)) < 0) {193return SYS_ERR;194}195} else if (cmd == SO_LINGER) {196struct linger arg;197arg.l_onoff = on;198199if(on) {200arg.l_linger = (unsigned short)value.i;201if(setsockopt(fd, SOL_SOCKET, SO_LINGER,202(char*)&arg, sizeof(arg)) < 0) {203return SYS_ERR;204}205} else {206if (setsockopt(fd, SOL_SOCKET, SO_LINGER,207(char*)&arg, sizeof(arg)) < 0) {208return SYS_ERR;209}210}211} else if (cmd == SO_SNDBUF) {212jint buflen = value.i;213if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF,214(char *)&buflen, sizeof(buflen)) < 0) {215return SYS_ERR;216}217} else if (cmd == SO_REUSEADDR) {218int oni = (int)on;219if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,220(char *)&oni, sizeof(oni)) < 0) {221return SYS_ERR;222223}224} else {225return SYS_ERR;226}227return SYS_OK;228}229230int231dbgsysConfigureBlocking(int fd, jboolean blocking) {232int flags = fcntl(fd, F_GETFL);233234if ((blocking == JNI_FALSE) && !(flags & O_NONBLOCK)) {235return fcntl(fd, F_SETFL, flags | O_NONBLOCK);236}237if ((blocking == JNI_TRUE) && (flags & O_NONBLOCK)) {238return fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);239}240return 0;241}242243int244dbgsysPoll(int fd, jboolean rd, jboolean wr, long timeout) {245struct pollfd fds[1];246int rv;247248fds[0].fd = fd;249fds[0].events = 0;250if (rd) {251fds[0].events |= POLLIN;252}253if (wr) {254fds[0].events |= POLLOUT;255}256fds[0].revents = 0;257258rv = poll(&fds[0], 1, timeout);259if (rv >= 0) {260rv = 0;261if (fds[0].revents & POLLIN) {262rv |= DBG_POLLIN;263}264if (fds[0].revents & POLLOUT) {265rv |= DBG_POLLOUT;266}267}268return rv;269}270271int272dbgsysGetLastIOError(char *buf, jint size) {273char *msg = strerror(errno);274strncpy(buf, msg, size-1);275buf[size-1] = '\0';276return 0;277}278279#ifdef __solaris__280int281dbgsysTlsAlloc() {282thread_key_t tk;283if (thr_keycreate(&tk, NULL)) {284perror("thr_keycreate");285exit(-1);286}287return (int)tk;288}289290void291dbgsysTlsFree(int index) {292/* no-op */293}294295void296dbgsysTlsPut(int index, void *value) {297thr_setspecific((thread_key_t)index, value) ;298}299300void *301dbgsysTlsGet(int index) {302void* r = NULL;303thr_getspecific((thread_key_t)index, &r);304return r;305}306307#else308int309dbgsysTlsAlloc() {310pthread_key_t key;311if (pthread_key_create(&key, NULL)) {312perror("pthread_key_create");313exit(-1);314}315return (int)key;316}317318void319dbgsysTlsFree(int index) {320pthread_key_delete((pthread_key_t)index);321}322323void324dbgsysTlsPut(int index, void *value) {325pthread_setspecific((pthread_key_t)index, value) ;326}327328void *329dbgsysTlsGet(int index) {330return pthread_getspecific((pthread_key_t)index);331}332333#endif334335long336dbgsysCurrentTimeMillis() {337struct timeval t;338gettimeofday(&t, 0);339return ((jlong)t.tv_sec) * 1000 + (jlong)(t.tv_usec/1000);340}341342343