⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 syscalls.c

📁 linux下建立JAVA虚拟机的源码KAFFE
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * syscalls.c * * Copyright (c) 1996, 1997 *      Transvirtual Technologies, Inc.  All rights reserved. * * See the file "license.terms" for information on usage and redistribution * of this file. */#include "config.h"#include "debug.h"#include "config-std.h"#include "config-mem.h"#include "config-io.h"#include "config-signal.h"#include "config-net.h"#include "gtypes.h"#include "jsyscall.h"#include "jsignal.h"#include "nets.h"#include "lock-impl.h"#if defined(HAVE_SYS_WAIT_H)#include <sys/wait.h>#endif/* * various building blocks for timeout system call functions */#define SET_DEADLINE(deadline, timeout) 		\	if (timeout != NOTIMEOUT) {			\		deadline = timeout + currentTime();	\	}#define BREAK_IF_LATE(deadline, timeout)		\	if (timeout != NOTIMEOUT) {			\		if (currentTime() >= deadline) {	\			errno = ETIMEDOUT;		\			break;				\		}					\	}#define IGNORE_EINTR(r)					\	if (r == -1 && errno == EINTR) {		\		continue;				\	}#define SET_RETURN(r)					\	if (r == -1) {					\		r = errno;				\	} #define SET_RETURN_OUT(r, out, ret)			\	if (r == -1) {					\		r = errno;				\	} else {					\		*out = ret;				\		r = 0;					\	}static int selectHelper(int n, fd_set *readfds, fd_set *writefds, fd_set *errfds, int timeout){  struct timeval tv;  int ret;  jthread_current()->interrupting = 0;  if (timeout == NOTIMEOUT)    {      ret = select(n, readfds, writefds, errfds, NULL);    }  else    {            tv.tv_sec = timeout / 1000;      tv.tv_usec = (timeout % 1000) * 1000;      ret = select(n, readfds, writefds, errfds, &tv);    }  if (ret == 0)    errno = ETIMEDOUT;  else if (ret == -1)    {      errno = EINTR;      jthread_current()->interrupting = 1;    }    return ret;}static intwaitForTimeout(int fd, int timeout){  fd_set rset;    FD_ZERO(&rset);  FD_SET(fd,&rset);  return selectHelper(fd+1, &rset, NULL, NULL, timeout);}static intwaitForRW(int fd, int timeout){  fd_set rwset;    FD_ZERO(&rwset);  FD_SET(fd,&rwset);    return selectHelper(fd+1, &rwset, &rwset, NULL, timeout);}static intwaitForWritable(int fd, int timeout){  fd_set wset;    FD_ZERO(&wset);  FD_SET(fd,&wset);    return selectHelper(fd+1, NULL, &wset, NULL, timeout);}staticjlongcurrentTime(void){    struct timeval tm;    gettimeofday(&tm, NULL);    return (((jlong)tm.tv_sec * 1000L) + ((jlong)tm.tv_usec / 1000L));}/* * Threaded socket create. */static intjthreadedSocket(int af, int type, int proto, int *out){        int r;        r = socket(af, type, proto);        if (r == -1) {                r = errno;        } else {                *out = r;                r = 0;        }        return (r);}static intjthreadedOpen(const char* path, int flags, int mode, int *out){        int r;        /* Cygnus WinNT requires this */        r = open(path, flags|O_BINARY, mode);        if (r == -1) {                r = errno;        } else {                *out = r;                r = 0;        }        return (r);}static intjthreadedClose(int fd){	int rc = 0;	if (close(fd) == -1) {		rc = errno;	}	return (rc);}static intjthreadedSocketShutdown(int fd){       int rc = 0;         if (shutdown(fd, 2) == -1)	 rc = errno;       return rc;}static intjthreadedListen(int fd, int l){	int rc = 0;	if (listen(fd, l) == -1) {		rc = errno;	}	return (rc);}static intjthreadedKill(int pid, int sig){	int rc = 0;	if (kill(pid, sig) == -1) {		rc = errno;	}	return (rc);}static intjthreadedBind(int fd, struct sockaddr *addr, socklen_t namelen){	int rc = 0;	if (bind(fd, addr, namelen) == -1) {		rc = errno;	}	return (rc);}static intjthreadedLSeek(int fd, off_t offset, int whence, off_t *out){	int rc = 0;	*out = lseek(fd, offset, whence);	if (*out == -1) {		rc = errno;	}	return (rc);}static intjthreadedFStat(int fd, struct stat *sb){	int rc = 0;	if (fstat(fd, sb) == -1) {		rc = errno;	}	return (rc);}static intjthreadedStat(const char* path, struct stat *sb){	int rc = 0;	if (stat(path, sb) == -1) {		rc = errno;	}	return (rc);}static intjthreadedFTruncate(int fd, off_t new_size){	int rc = 0;		if (ftruncate(fd, new_size) == -1) {		rc = errno;	}	return (rc);}static intjthreadedFSync(int fd){	int rc = 0;		if (fsync(fd) == -1) {		rc = errno;	}	return (rc);}static intjthreadedMkdir(const char *path, int mode){	int rc = 0;	if (mkdir(path, (mode_t)mode) == -1) {		rc = errno;	}	return (rc);}static intjthreadedRmdir(const char *path){	int rc = 0;	if (rmdir(path) == -1) {		rc = errno;	}	return (rc);}static intjthreadedRename(const char *path1, const char *path2){	int rc = 0;	if (rename(path1, path2) == -1) {		rc = errno;	}	return (rc);}static intjthreadedRemove(const char *entry){	int rc = 0;	if (remove(entry) == -1) {		rc = errno;	}	return (rc);}static int     jthreadedSendto(int a, const void* b, size_t c, int d, const struct sockaddr* e,		int f, ssize_t *out){	int rc = 0;	*out = e ? sendto(a, b, c, d, e, (socklen_t)f) : send(a, b, c, d);	if (*out == -1) {		rc = errno;	}	return (rc);}static intjthreadedSetSockOpt(int a, int b, int c, const void* d, int e){	int rc = 0;	if (setsockopt(a, b, c, d, (socklen_t)e) == -1) {		rc = errno;	}	return (rc);}static intjthreadedGetSockOpt(int a, int b, int c, void* d, socklen_t* e){	int rc = 0;	if (getsockopt(a, b, c, d, e) == -1) {		rc = errno;	}	return (rc);}static intjthreadedGetSockName(int a, struct sockaddr* b, socklen_t* c){	int rc = 0;	if (getsockname(a, b, c) == -1) {		rc = errno;	}	return (rc);}static intjthreadedGetPeerName(int a, struct sockaddr* b, socklen_t* c){	int rc = 0;	if (getpeername(a, b, c) == -1) {		rc = errno;	}	return (rc);}static intjthreadedGetHostByName(const char *host, struct hostent** out){	int rc = 0;	/* NB: this will block the whole process while we're looking up	 * a name.  However, gethostbyname is extremely async-signal-unsafe,	 * and hence we have no other choice.  Now I understand why old 	 * Netscapes blocked when looking up a name.  	 *	 * In a UNIXy system, a possible solution is to spawn a process	 * for lookups, which I think is what NS eventually did.	 */	*out = gethostbyname(host);	if (*out == 0) {		rc = h_errno;		if (rc == 0) {			*out = (void*)-1;			rc = errno;		}	}	return (rc);}static intjthreadedGetHostByAddr(const char *host, size_t l, int t, struct hostent** out){	int rc = 0;	/* NB: same comment as for jthreadedGetHostByName applies here */	*out = gethostbyaddr(host, l, t);	if (*out == 0) {		rc = h_errno;		if (rc == 0) {			*out = (void*)-1;			rc = errno;		}	}	return (rc);}static intjthreadedSelect(int a, fd_set* b, fd_set* c, fd_set* d, 		struct timeval* e, int* out){	int rc = 0;	if ((*out = select(a, b, c, d, e)) == -1) {		rc = errno;	}	return (rc);}/* * Threaded socket connect. */static intjthreadedConnect(int fd, struct sockaddr* addr, socklen_t len, int timeout){	int r;	jlong deadline = 0;	int inProgress = 0;	SET_DEADLINE(deadline, timeout)	for (;;) {		r = connect(fd, addr, len);		if (r == 0 || !(errno == EINPROGRESS 				|| errno == EINTR || errno == EISCONN)) {			break;	/* success or real error */		}		if (r == -1 && errno == EISCONN) {			/* On Solaris 2.5, after getting EINPROGRESS			   from a non-blocking connect request, we			   won't ever get success.  When we're waken			   up, we'll either get EISCONN, which should			   be taken as success, or a real failure.			   However, we can't map EISCONN to success			   inconditionally, because attempting to			   connect the same socket again should raise			   an exception.			   Mapping EISCONN to success might lead to			   false positives if connect fails and			   another thread succeeds to connect this			   socket before this one is waken up.  Let's			   just hope it doesn't happen for now.  */			if (inProgress) {				r = 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -