+ Socket s = fd.s;
+
+ try {
+ if(how == SHUT_RD || how == SHUT_RDWR) Platform.socketHalfClose(s,false);
+ if(how == SHUT_WR || how == SHUT_RDWR) Platform.socketHalfClose(s,true);
+ } catch(IOException e) {
+ return -EIO;
+ }
+
+ return 0;
+ }
+
+ private int sys_sendto(int fdn, int addr, int count, int flags, int destAddr, int socklen) throws ErrnoException,ReadFaultException {
+ SocketFD fd = getSocketFD(fdn);
+ if(flags != 0) throw new ErrnoException(EINVAL);
+
+ int word1 = memRead(destAddr);
+ if( ((word1 >>> 16)&0xff) != AF_INET) return -EAFNOSUPPORT;
+ int port = word1 & 0xffff;
+ InetAddress inetAddr;
+ byte[] ip = new byte[4];
+ copyin(destAddr+4,ip,4);
+ try {
+ inetAddr = Platform.inetAddressFromBytes(ip);
+ } catch(UnknownHostException e) {
+ return -EADDRNOTAVAIL;
+ }
+
+ count = Math.min(count,MAX_CHUNK);
+ byte[] buf = byteBuf(count);
+ copyin(addr,buf,count);
+ try {
+ return fd.sendto(buf,0,count,inetAddr,port);
+ } catch(ErrnoException e) {
+ if(e.errno == EPIPE) exit(128+13,true);
+ throw e;
+ }
+ }
+
+ private int sys_recvfrom(int fdn, int addr, int count, int flags, int sourceAddr, int socklenAddr) throws ErrnoException, FaultException {
+ SocketFD fd = getSocketFD(fdn);
+ if(flags != 0) throw new ErrnoException(EINVAL);
+
+ InetAddress[] inetAddr = sourceAddr == 0 ? null : new InetAddress[1];
+ int[] port = sourceAddr == 0 ? null : new int[1];
+
+ count = Math.min(count,MAX_CHUNK);
+ byte[] buf = byteBuf(count);
+ int n = fd.recvfrom(buf,0,count,inetAddr,port);
+ copyout(buf,addr,n);
+
+ if(sourceAddr != 0) {
+ memWrite(sourceAddr,(AF_INET << 16) | port[0]);
+ byte[] ip = inetAddr[0].getAddress();
+ copyout(ip,sourceAddr+4,4);
+ }
+
+ return n;
+ }
+
+ private int sys_select(int n, int readFDs, int writeFDs, int exceptFDs, int timevalAddr) throws ReadFaultException, ErrnoException {
+ return -ENOSYS;
+ }
+
+ private static String hostName() {
+ try {
+ return InetAddress.getLocalHost().getHostName();
+ } catch(UnknownHostException e) {
+ return "darkstar";
+ }
+ }
+
+ private int sys_sysctl(int nameaddr, int namelen, int oldp, int oldlenaddr, int newp, int newlen) throws FaultException {
+ if(newp != 0) return -EPERM;
+ if(namelen == 0) return -ENOENT;
+ if(oldp == 0) return 0;
+
+ Object o = null;
+ switch(memRead(nameaddr)) {
+ case CTL_KERN:
+ if(namelen != 2) break;
+ switch(memRead(nameaddr+4)) {
+ case KERN_OSTYPE: o = "NestedVM"; break;
+ case KERN_HOSTNAME: o = hostName(); break;
+ case KERN_OSRELEASE: o = VERSION; break;
+ case KERN_VERSION: o = "NestedVM Kernel Version " + VERSION; break;
+ }
+ break;
+ case CTL_HW:
+ if(namelen != 2) break;
+ switch(memRead(nameaddr+4)) {
+ case HW_MACHINE: o = "NestedVM Virtual Machine"; break;
+ }
+ break;
+ }
+ if(o == null) return -ENOENT;
+ int len = memRead(oldlenaddr);
+ if(o instanceof String) {
+ byte[] b = getNullTerminatedBytes((String)o);
+ if(len < b.length) return -ENOMEM;
+ len = b.length;
+ copyout(b,oldp,len);
+ memWrite(oldlenaddr,len);
+ } else if(o instanceof Integer) {
+ if(len < 4) return -ENOMEM;
+ memWrite(oldp,((Integer)o).intValue());
+ } else {
+ throw new Error("should never happen");
+ }
+ return 0;
+ }
+
+ public static final class GlobalState {
+ Hashtable execCache = new Hashtable();
+
+ final UnixRuntime[] tasks;
+ int nextPID = 1;
+
+ /** Table of all current file locks held by this process. */
+ Seekable.Lock[] locks = new Seekable.Lock[16];
+
+ private MP[] mps = new MP[0];
+ private FS root;
+
+ public GlobalState() { this(255); }
+ public GlobalState(int maxProcs) { this(maxProcs,true); }
+ public GlobalState(int maxProcs, boolean defaultMounts) {
+ tasks = new UnixRuntime[maxProcs+1];
+ if(defaultMounts) {
+ File root = null;
+ if(Platform.getProperty("nestedvm.root") != null) {
+ root = new File(Platform.getProperty("nestedvm.root"));
+ if(!root.isDirectory()) throw new IllegalArgumentException("nestedvm.root is not a directory");
+ } else {
+ String cwd = Platform.getProperty("user.dir");
+ root = Platform.getRoot(new File(cwd != null ? cwd : "."));
+ }
+
+ addMount("/",new HostFS(root));
+
+ if(Platform.getProperty("nestedvm.root") == null) {
+ File[] roots = Platform.listRoots();
+ for(int i=0;i<roots.length;i++) {
+ String name = roots[i].getPath();
+ if(name.endsWith(File.separator))
+ name = name.substring(0,name.length()-1);
+ if(name.length() == 0 || name.indexOf('/') != -1) continue;
+ addMount("/" + name.toLowerCase(),new HostFS(roots[i]));
+ }
+ }
+
+ addMount("/dev",new DevFS());
+ addMount("/resource",new ResourceFS());
+ addMount("/cygdrive",new CygdriveFS());
+ }
+ }
+
+ public String mapHostPath(String s) { return mapHostPath(new File(s)); }
+ public String mapHostPath(File f) {
+ MP[] list;
+ FS root;
+ synchronized(this) { mps = this.mps; root = this.root; }
+ if(!f.isAbsolute()) f = new File(f.getAbsolutePath());
+ for(int i=mps.length;i>=0;i--) {
+ FS fs = i == mps.length ? root : mps[i].fs;
+ String path = i == mps.length ? "" : mps[i].path;
+ if(!(fs instanceof HostFS)) continue;
+ File fsroot = ((HostFS)fs).getRoot();
+ if(!fsroot.isAbsolute()) fsroot = new File(fsroot.getAbsolutePath());
+ if(f.getPath().startsWith(fsroot.getPath())) {
+ char sep = File.separatorChar;
+ String child = f.getPath().substring(fsroot.getPath().length());
+ if(sep != '/') {
+ char[] child_ = child.toCharArray();
+ for(int j=0;j<child_.length;j++) {
+ if(child_[j] == '/') child_[j] = sep;
+ else if(child_[j] == sep) child_[j] = '/';
+ }
+ child = new String(child_);
+ }
+ String mapped = "/" + (path.length()==0?"":path+"/") + child;
+ return mapped;
+ }
+ }
+ return null;
+ }
+
+ static class MP implements Sort.Comparable {
+ public MP(String path, FS fs) { this.path = path; this.fs = fs; }
+ public String path;
+ public FS fs;
+ public int compareTo(Object o) {
+ if(!(o instanceof MP)) return 1;
+ return -path.compareTo(((MP)o).path);
+ }
+ }
+
+ public synchronized FS getMount(String path) {
+ if(!path.startsWith("/")) throw new IllegalArgumentException("Mount point doesn't start with a /");
+ if(path.equals("/")) return root;
+ path = path.substring(1);
+ for(int i=0;i<mps.length;i++)
+ if(mps[i].path.equals(path)) return mps[i].fs;
+ return null;
+ }
+
+ public synchronized void addMount(String path, FS fs) {
+ if(getMount(path) != null) throw new IllegalArgumentException("mount point already exists");
+ if(!path.startsWith("/")) throw new IllegalArgumentException("Mount point doesn't start with a /");
+
+ if(fs.owner != null) fs.owner.removeMount(fs);
+ fs.owner = this;
+
+ if(path.equals("/")) { root = fs; fs.devno = 1; return; }
+ path = path.substring(1);
+ int oldLength = mps.length;
+ MP[] newMPS = new MP[oldLength + 1];
+ if(oldLength != 0) System.arraycopy(mps,0,newMPS,0,oldLength);
+ newMPS[oldLength] = new MP(path,fs);
+ Sort.sort(newMPS);
+ mps = newMPS;
+ int highdevno = 0;
+ for(int i=0;i<mps.length;i++) highdevno = max(highdevno,mps[i].fs.devno);
+ fs.devno = highdevno + 2;
+ }
+
+ public synchronized void removeMount(FS fs) {
+ for(int i=0;i<mps.length;i++) if(mps[i].fs == fs) { removeMount(i); return; }
+ throw new IllegalArgumentException("mount point doesn't exist");
+ }
+
+ public synchronized void removeMount(String path) {
+ if(!path.startsWith("/")) throw new IllegalArgumentException("Mount point doesn't start with a /");
+ if(path.equals("/")) {
+ removeMount(-1);
+ } else {
+ path = path.substring(1);
+ int p;
+ for(p=0;p<mps.length;p++) if(mps[p].path.equals(path)) break;
+ if(p == mps.length) throw new IllegalArgumentException("mount point doesn't exist");
+ removeMount(p);
+ }
+ }
+
+ private void removeMount(int index) {
+ if(index == -1) { root.owner = null; root = null; return; }
+ MP[] newMPS = new MP[mps.length - 1];
+ System.arraycopy(mps,0,newMPS,0,index);
+ System.arraycopy(mps,0,newMPS,index,mps.length-index-1);
+ mps = newMPS;
+ }
+
+ private Object fsop(int op, UnixRuntime r, String normalizedPath, int arg1, int arg2) throws ErrnoException {
+ int pl = normalizedPath.length();
+ if(pl != 0) {
+ MP[] list;
+ synchronized(this) { list = mps; }
+ for(int i=0;i<list.length;i++) {
+ MP mp = list[i];
+ int mpl = mp.path.length();
+ if(normalizedPath.startsWith(mp.path) && (pl == mpl || normalizedPath.charAt(mpl) == '/'))
+ return mp.fs.dispatch(op,r,pl == mpl ? "" : normalizedPath.substring(mpl+1),arg1,arg2);
+ }
+ }
+ return root.dispatch(op,r,normalizedPath,arg1,arg2);
+ }
+
+ public final FD open(UnixRuntime r, String path, int flags, int mode) throws ErrnoException { return (FD) fsop(FS.OPEN,r,path,flags,mode); }
+ public final FStat stat(UnixRuntime r, String path) throws ErrnoException { return (FStat) fsop(FS.STAT,r,path,0,0); }
+ public final FStat lstat(UnixRuntime r, String path) throws ErrnoException { return (FStat) fsop(FS.LSTAT,r,path,0,0); }
+ public final void mkdir(UnixRuntime r, String path, int mode) throws ErrnoException { fsop(FS.MKDIR,r,path,mode,0); }
+ public final void unlink(UnixRuntime r, String path) throws ErrnoException { fsop(FS.UNLINK,r,path,0,0); }
+
+ private static class CacheEnt {
+ public final long time;
+ public final long size;
+ public final Object o;
+ public CacheEnt(long time, long size, Object o) { this.time = time; this.size = size; this.o = o; }
+ }
+ }
+