From: brian Date: Sat, 24 Apr 2004 06:53:33 +0000 (-0700) Subject: more preliminary exec() stuff X-Git-Tag: merge~29 X-Git-Url: http://git.megacz.com/?p=nestedvm.git;a=commitdiff_plain;h=ef1c27c7eff6c432f3ad7a21671947b87539acb2 more preliminary exec() stuff darcs-hash:20040424065333-24bed-eb68df45e44b6cbbdc7c5ae50fc5702d580b162e.gz --- diff --git a/src/org/ibex/nestedvm/ClassFileCompiler.java b/src/org/ibex/nestedvm/ClassFileCompiler.java index 0c4dd67..def556a 100644 --- a/src/org/ibex/nestedvm/ClassFileCompiler.java +++ b/src/org/ibex/nestedvm/ClassFileCompiler.java @@ -390,6 +390,8 @@ public class ClassFileCompiler extends Compiler implements org.apache.bcel.Const main.setMaxStack(); cl.addMethod(main.getMethod()); + if(printStats) + System.out.println("Constant Pool Size: " + cp.getSize()); cl.getJavaClass().dump(os); } diff --git a/src/org/ibex/nestedvm/ClassLoader.java b/src/org/ibex/nestedvm/ClassLoader.java index 860e3a7..2ef8fa8 100644 --- a/src/org/ibex/nestedvm/ClassLoader.java +++ b/src/org/ibex/nestedvm/ClassLoader.java @@ -1,9 +1,9 @@ package org.ibex.nestedvm; -import java.io.*; +//import java.io.*; import java.util.*; -import org.ibex.nestedvm.util.*; +//import org.ibex.nestedvm.util.*; // FIXME: This is totally broken now diff --git a/src/org/ibex/nestedvm/Runtime.java b/src/org/ibex/nestedvm/Runtime.java index a04dac2..39a4f14 100644 --- a/src/org/ibex/nestedvm/Runtime.java +++ b/src/org/ibex/nestedvm/Runtime.java @@ -1070,6 +1070,10 @@ public abstract class Runtime implements UsermodeConstants,Registers,Cloneable { /** Seek in the filedescriptor. Whence is SEEK_SET, SEEK_CUR, or SEEK_END. Should return -1 on error or the new position. */ public int seek(int n, int whence) throws IOException { return -1; } + /** Return a Seekable object representing this file descriptor (can be read only) + This is required for exec() */ + Seekable seekable() { return null; } + /** Should return true if this is a tty */ // FEATURE: get rid of the isatty syscall and just do with newlib's dumb isatty.c public boolean isatty() { return false; } @@ -1100,6 +1104,8 @@ public abstract class Runtime implements UsermodeConstants,Registers,Cloneable { protected abstract FStat _fstat(); + Seekable seekable() { return data; } + public int seek(int n, int whence) throws IOException { switch(whence) { case SEEK_SET: break; diff --git a/src/org/ibex/nestedvm/UnixRuntime.java b/src/org/ibex/nestedvm/UnixRuntime.java index 24a7967..5b47219 100644 --- a/src/org/ibex/nestedvm/UnixRuntime.java +++ b/src/org/ibex/nestedvm/UnixRuntime.java @@ -289,53 +289,63 @@ public abstract class UnixRuntime extends Runtime implements Cloneable { } private int exec(String path, String[] argv, String[] envp) { - final UnixRuntime r; + if(argv.length == 0) argv = new String[]{""}; + Seekable s; + FD fd; - throw new Error("FIXME exec() not finished"); - //Class klass = fs.getClass(path); - //if(klass != null) return exec(klass,argv,envp); - - /*try { - Seekable s = fs.getSeekable(path); - boolean elf = false; - boolean script = false; - switch(s.read()) { - case '\177': elf = s.read() == 'E' && s.read() == 'L' && s.read() == 'F'; break; - case '#': script = s.read() == '!'; - } - s.close(); - if(script) { - // FIXME #! support - throw new Error("FIXME can't exec scripts yet"); - // append args, etc - // return exec(...) - } else if(elf) { - klass = fs.(path); - if(klass == null) return -ENOEXEC; - return exec(klass,argv,envp); - } else { - return -ENOEXEC; - } - }*/ - // FEATURE: Way too much overlap in the handling of ioexeptions everhwere - /*catch(ErrnoException e) { return -e.errno; } + try { + fd = fs.open(path,RD_ONLY,0); + System.err.println(fd + " " + path); + if(fd == null) return -ENOENT; + s = fd.seekable(); + if(s == null) return -ENOEXEC; + } + catch(ErrnoException e) { return -e.errno; } catch(FileNotFoundException e) { if(e.getMessage() != null && e.getMessage().indexOf("Permission denied") >= 0) return -EACCES; return -ENOENT; } catch(IOException e) { return -EIO; } - catch(FaultException e) { return -EFAULT; }*/ - } - - private int exec(Class c, String[] argv, String[] envp) { - UnixRuntime r; try { - r = (UnixRuntime) c.newInstance(); - } catch(Exception e) { - return -ENOMEM; + int p = 0; + byte[] buf = new byte[4096]; + OUTER: for(;;) { + int n = s.read(buf,p,buf.length-p); + if(n == -1) break; + for(;n > 0; n--) if(buf[p++] == '\n' || p == 4096) break OUTER; + } + if(buf[0] == '!' && buf[1] == '#') { + String cmd = new String(buf,2,p-2); + String argv1 = null; + if((p = cmd.indexOf(' ')) != -1) { + do { p++; } while(cmd.charAt(p)==' '); + argv1 = cmd.substring(p); + cmd = cmd.substring(0,p-1); + } + String[] newArgv = new String[argv.length + argv1 != null ? 2 : 1]; + p = 0; + newArgv[p++] = argv[0]; + if(argv1 != null) newArgv[p++] = argv1; + newArgv[p++] = path; + for(int i=1;i " + f + " " + f.exists()); if(f.isDirectory()) { if((flags&3)!=RD_ONLY) throw new ErrnoException(EACCES); return directoryFD(f.list(),path.hashCode());