package org.ibex.nestedvm;
import org.ibex.nestedvm.util.*;
+// FEATURE: This is ugly, this stuff needs to be in org.ibex.util or something
+import org.ibex.classgen.util.Sort;
import java.io.*;
import java.util.*;
import java.net.*;
? userdir.substring(1) : "";
}
- // NOTE: getDisplayName() is a Java2 function
private static String posixTZ() {
StringBuffer sb = new StringBuffer();
TimeZone zone = TimeZone.getDefault();
int off = zone.getRawOffset() / 1000;
- sb.append(zone.getDisplayName(false,TimeZone.SHORT));
+ sb.append(Platform.timeZoneGetDisplayName(zone,false,false));
if(off > 0) sb.append("-");
else off = -off;
sb.append(off/3600); off = off%3600;
if(off > 0) sb.append(":").append(off/60); off=off%60;
if(off > 0) sb.append(":").append(off);
if(zone.useDaylightTime())
- sb.append(zone.getDisplayName(true,TimeZone.SHORT));
+ sb.append(Platform.timeZoneGetDisplayName(zone,true,false));
return sb.toString();
}
synchronized(parent.children) {
int i = parent.activeChildren.indexOf(prev);
if(i == -1) throw new Error("should never happen");
- parent.activeChildren.set(i,this);
+ parent.activeChildren.setElementAt(this,i);
}
} else {
int newpid = -1;
synchronized(children) {
for(;;) {
if(pid == -1) {
- if(exitedChildren.size() > 0) done = (UnixRuntime)exitedChildren.remove(exitedChildren.size() - 1);
+ if(exitedChildren.size() > 0) {
+ done = (UnixRuntime)exitedChildren.elementAt(exitedChildren.size() - 1);
+ exitedChildren.removeElementAt(exitedChildren.size() - 1);
+ }
} else if(pid > 0) {
UnixRuntime t = gs.tasks[pid];
if(t.parent != this) return -ECHILD;
if(t.state == EXITED) {
- if(!exitedChildren.remove(t)) throw new Error("should never happen");
+ if(!exitedChildren.removeElement(t)) throw new Error("should never happen");
done = t;
}
} else {
UnixRuntime child = (UnixRuntime) e.nextElement();
gs.tasks[child.pid] = null;
}
- exitedChildren.clear();
+ exitedChildren.removeAllElements();
for(Enumeration e = activeChildren.elements(); e.hasMoreElements(); ) {
UnixRuntime child = (UnixRuntime) e.nextElement();
child.parent = null;
}
- activeChildren.clear();
+ activeChildren.removeAllElements();
}
UnixRuntime _parent = parent;
if(parent == null) {
gs.tasks[pid] = null;
} else {
- if(!parent.activeChildren.remove(this)) throw new Error("should never happen _exited: pid: " + pid);
- parent.exitedChildren.add(this);
+ if(!parent.activeChildren.removeElement(this)) throw new Error("should never happen _exited: pid: " + pid);
+ parent.exitedChildren.addElement(this);
parent.children.notify();
}
}
activeChildren = new Vector();
exitedChildren = new Vector();
}
- activeChildren.add(r);
+ activeChildren.addElement(r);
state.r[V0] = 0; // return 0 to child
state.pc += 4; // skip over syscall instruction
private int sys_chdir(int addr) throws ErrnoException, FaultException {
String path = normalizePath(cstring(addr));
- //System.err.println("Chdir: " + cstring(addr) + " -> " + path + " pwd: " + cwd);
- if(gs.stat(this,path).type() != FStat.S_IFDIR) return -ENOTDIR;
+ FStat st = gs.stat(this,path);
+ if(st == null) return -ENOENT;
+ if(st.type() != FStat.S_IFDIR) return -ENOTDIR;
cwd = path;
- //System.err.println("Now: [" + cwd + "]");
return 0;
}
return n;
}
+ // FEATURE: UDP is totally broken
+
static class SocketFD extends FD {
public static final int TYPE_STREAM = 0;
public static final int TYPE_DGRAM = 1;
public void setOptions() {
try {
if(o != null && type() == TYPE_STREAM && !listen()) {
- ((Socket)o).setKeepAlive((options & SO_KEEPALIVE) != 0);
+ Platform.socketSetKeepAlive((Socket)o,(options & SO_KEEPALIVE) != 0);
}
} catch(SocketException e) {
if(STDERR_DIAG) e.printStackTrace();
throw new ErrnoException(EIO);
}
} else {
+ if(off != 0) throw new IllegalArgumentException("off must be 0");
DatagramSocket ds = (DatagramSocket) o;
- dp.setData(a,off,length);
+ dp.setData(a);
+ dp.setLength(length);
try {
ds.receive(dp);
} catch(IOException e) {
throw new ErrnoException(EIO);
}
} else {
+ if(off != 0) throw new IllegalArgumentException("off must be 0");
DatagramSocket ds = (DatagramSocket) o;
- dp.setData(a,off,length);
+ dp.setData(a);
+ dp.setLength(length);
try {
ds.send(dp);
} catch(IOException e) {
InetAddress inetAddr;
try {
- inetAddr = InetAddress.getByAddress(ip);
+ inetAddr = Platform.inetAddressFromBytes(ip);
} catch(UnknownHostException e) {
return -EADDRNOTAVAIL;
}
break;
}
case SocketFD.TYPE_DGRAM: {
- DatagramSocket s = (DatagramSocket) fd.o;
- if(s == null) s = new DatagramSocket();
- s.connect(inetAddr,port);
+ if(fd.dp == null) fd.dp = new DatagramPacket(null,0);
+ fd.dp.setAddress(inetAddr);
+ fd.dp.setPort(port);
break;
}
default:
copyin(addr+4,ip,4);
try {
- inetAddr = InetAddress.getByAddress(ip);
+ inetAddr = Platform.inetAddressFromBytes(ip);
} catch(UnknownHostException e) {
return -EADDRNOTAVAIL;
}
Socket s = (Socket) fd.o;
try {
- if(how == SHUT_RD || how == SHUT_RDWR) s.shutdownInput();
- if(how == SHUT_WR || how == SHUT_RDWR) s.shutdownOutput();
+ 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;
}
-
-
- /*public int sys_opensocket(int cstring, int port) throws FaultException, ErrnoException {
- String hostname = cstring(cstring);
- try {
- FD fd = new SocketFD(new Socket(hostname,port));
- int n = addFD(fd);
- if(n == -1) fd.close();
- return n;
- } catch(IOException e) {
- return -EIO;
- }
- }
-
- private static class ListenSocketFD extends FD {
- ServerSocket s;
- public ListenSocketFD(ServerSocket s) { this.s = s; }
- public int flags() { return 0; }
- // FEATURE: What should these be?
- public FStat _fstat() { return new FStat(); }
- public void _close() { try { s.close(); } catch(IOException e) { } }
- }
-
- public int sys_listensocket(int port) {
- try {
- ListenSocketFD fd = new ListenSocketFD(new ServerSocket(port));
- int n = addFD(fd);
- if(n == -1) fd.close();
- return n;
- } catch(IOException e) {
- return -EIO;
- }
- }
-
- public int sys_accept(int fdn) {
- if(fdn < 0 || fdn >= OPEN_MAX) return -EBADFD;
- if(fds[fdn] == null) return -EBADFD;
- if(!(fds[fdn] instanceof ListenSocketFD)) return -EBADFD;
- try {
- ServerSocket s = ((ListenSocketFD)fds[fdn]).s;
- SocketFD fd = new SocketFD(s.accept());
- int n = addFD(fd);
- if(n == -1) fd.close();
- return n;
- } catch(IOException e) {
- return -EIO;
- }
- }*/
// FEATURE: Run through the fork/wait stuff one more time
public static class GlobalState {
}
}
- private static class MP implements Comparable {
+ private static class MP implements Sort.Comparable {
public MP(String path, FS fs) { this.path = path; this.fs = fs; }
public String path;
public FS fs;
MP[] newMPS = new MP[oldLength + 1];
if(oldLength != 0) System.arraycopy(mps,0,newMPS,0,oldLength);
newMPS[oldLength] = new MP(path,fs);
- Arrays.sort(newMPS);
+ Sort.sort(newMPS);
mps = newMPS;
int highdevno = 0;
for(int i=0;i<mps.length;i++) highdevno = max(highdevno,mps[i].fs.devno);
mps = newMPS;
}
+ // FEATURE: We shouldn't need to special case the root dir, it should work in the MP array
private Object fsop(int op, UnixRuntime r, String normalizedPath, int arg1, int arg2) throws ErrnoException {
int pl = normalizedPath.length();
if(pl != 0) {
for(int i=0;i<list.length;i++) {
MP mp = list[i];
int mpl = mp.path.length();
- if(normalizedPath.startsWith(mp.path) && (pl == mpl || (pl < mpl && normalizedPath.charAt(mpl) == '/')))
+ if(normalizedPath.startsWith(mp.path) && (pl == mpl || normalizedPath.charAt(mpl) == '/'))
return dispatch(mp.fs,op,r,pl == mpl ? "" : normalizedPath.substring(mpl+1),arg1,arg2);
}
}
return dispatch(root,op,r,normalizedPath,arg1,arg2);
}
+ // FEATURE: move this into FS so some filesystem can override it directly (devfs)
private static Object dispatch(FS fs, int op, UnixRuntime r, String path, int arg1, int arg2) throws ErrnoException {
switch(op) {
case OPEN: return fs.open(r,path,arg1,arg2);
}
public synchronized Object exec(UnixRuntime r, String path) throws ErrnoException {
- // FIXME: Hideous hack to make a standalone busybox possible
+ // HACK: Hideous hack to make a standalone busybox possible
if(path.equals("bin/busybox") && Boolean.valueOf(getSystemProperty("nestedvm.busyboxhack")).booleanValue())
return r.getClass();
FStat fstat = stat(r,path);
// its an elf binary
try {
s.seek(0);
- Class c = RuntimeCompiler.compile(s);
+ Class c = RuntimeCompiler.compile(s,"unixruntime");
//System.err.println("Compile succeeded: " + c);
ent = new CacheEnt(mtime,size,c);
} catch(Compiler.Exn e) {
public FD open(UnixRuntime r, String path, int flags, int mode) throws ErrnoException {
- // FIXME: horrendous, ugly hack needed by TeX... sorry Brian...
- path = path.trim();
final File f = hostFile(path);
return r.hostFSOpen(f,flags,mode,this);
}
if(r.sm != null && !r.sm.allowWrite(f)) throw new ErrnoException(EACCES);
if(f.exists() && f.isDirectory()) throw new ErrnoException(EEXIST);
if(f.exists()) throw new ErrnoException(ENOTDIR);
- File parent = f.getParentFile();
+ File parent = getParentFile(f);
if(parent!=null && (!parent.exists() || !parent.isDirectory())) throw new ErrnoException(ENOTDIR);
if(!f.mkdir()) throw new ErrnoException(EIO);
}
+ private static File getParentFile(File f) {
+ String p = f.getParent();
+ return p == null ? null : new File(f,p);
+ }
+
public class HostDirFD extends DirFD {
private final File f;
private final File[] children;
- public HostDirFD(File f) { this.f = f; children = f.listFiles(); }
+ public HostDirFD(File f) {
+ this.f = f;
+ String[] l = f.list();
+ children = new File[l.length];
+ for(int i=0;i<l.length;i++) children[i] = new File(f,l[i]);
+ }
public int size() { return children.length; }
public String name(int n) { return children[n].getName(); }
public int inode(int n) { return inodes.get(children[n].getAbsolutePath()); }
public int parentInode() {
- File parent = f.getParentFile();
+ File parent = getParentFile(f);
return parent == null ? -1 : inodes.get(parent.getAbsolutePath());
}
public int myInode() { return inodes.get(f.getAbsolutePath()); }
}
private FD devZeroFD = new FD() {
- public int read(byte[] a, int off, int length) { Arrays.fill(a,off,off+length,(byte)0); return length; }
+ public int read(byte[] a, int off, int length) {
+ /*Arrays.fill(a,off,off+length,(byte)0);*/
+ for(int i=off;i<off+length;i++) a[i] = 0;
+ return length;
+ }
public int write(byte[] a, int off, int length) { return length; }
public int seek(int n, int whence) { return 0; }
public FStat _fstat() { return new DevFStat(){ public int inode() { return ZERO_INODE; } }; }
if(path.startsWith("fd/")) {
int n;
try {
- n = Integer.parseInt(path.substring(4));
+ n = Integer.parseInt(path.substring(3));
} catch(NumberFormatException e) {
return null;
}