1 package org.ibex.nestedvm;
3 import org.ibex.nestedvm.util.*;
7 // FIXME: Fix readdir in support_aux.c
8 // FIXME: Make plain old "mips-unknown-elf-gcc -o foo foo.c" work (modify spec file or whatever)
10 // FEATURE: Remove System.{out,err}.printlns and throw Errors where applicable
12 // FIXME: BusyBox's ASH doesn't like \r\n at the end of lines
13 // is ash just broken or are many apps like this? if so workaround in nestedvm
15 public abstract class UnixRuntime extends Runtime implements Cloneable {
16 /** The pid of this "process" */
18 private UnixRuntime parent;
19 public final int getPid() { return pid; }
21 private static final GlobalState defaultGD = new GlobalState();
22 private GlobalState gd = defaultGD;
24 /** proceses' current working directory - absolute path WITHOUT leading slash
25 "" = root, "bin" = /bin "usr/bin" = /usr/bin */
28 /** The runtime that should be run next when in state == EXECED */
29 private UnixRuntime execedRuntime;
31 private Object children; // used only for synchronizatin
32 private Vector activeChildren;
33 private Vector exitedChildren;
35 protected UnixRuntime(int pageSize, int totalPages) {
36 super(pageSize,totalPages);
38 // FEATURE: Do the proper mangling for non-unix hosts
39 String userdir = getSystemProperty("user.dir");
40 cwd = userdir != null && userdir.startsWith("/") && File.separatorChar == '/' ? userdir.substring(1) : "";
43 // NOTE: getDisplayName() is a Java2 function
44 private static String posixTZ() {
45 StringBuffer sb = new StringBuffer();
46 TimeZone zone = TimeZone.getDefault();
47 int off = zone.getRawOffset() / 1000;
48 sb.append(zone.getDisplayName(false,TimeZone.SHORT));
49 if(off > 0) sb.append("-");
51 sb.append(off/3600); off = off%3600;
52 if(off > 0) sb.append(":").append(off/60); off=off%60;
53 if(off > 0) sb.append(":").append(off);
54 if(zone.useDaylightTime())
55 sb.append(zone.getDisplayName(true,TimeZone.SHORT));
59 private static boolean envHas(String key,String[] environ) {
60 for(int i=0;i<environ.length;i++)
61 if(environ[i]!=null && environ[i].startsWith(key + "=")) return true;
65 String[] createEnv(String[] extra) {
66 String[] defaults = new String[5];
68 if(extra == null) extra = new String[0];
69 if(!envHas("USER",extra) && getSystemProperty("user.name") != null)
70 defaults[n++] = "USER=" + getSystemProperty("user.name");
71 if(!envHas("HOME",extra) && getSystemProperty("user.name") != null)
72 defaults[n++] = "HOME=" + getSystemProperty("user.home");
73 if(!envHas("SHELL",extra)) defaults[n++] = "SHELL=/bin/sh";
74 if(!envHas("TERM",extra)) defaults[n++] = "TERM=vt100";
75 if(!envHas("TZ",extra)) defaults[n++] = "TZ=" + posixTZ();
76 String[] env = new String[extra.length+n];
77 for(int i=0;i<n;i++) env[i] = defaults[i];
78 for(int i=0;i<extra.length;i++) env[n++] = extra[i];
82 private static class ProcessTableFullExn extends RuntimeException { }
85 UnixRuntime[] tasks = gd.tasks;
88 if(tasks[pid] == null || tasks[pid].pid != pid) throw new Error("should never happen");
91 int nextPID = gd.nextPID;
92 for(int i=nextPID;i<tasks.length;i++) if(tasks[i] == null) { newpid = i; break; }
93 if(newpid == -1) for(int i=1;i<nextPID;i++) if(tasks[i] == null) { newpid = i; break; }
94 if(newpid == -1) throw new ProcessTableFullExn();
96 gd.nextPID = newpid + 1;
102 int _syscall(int syscall, int a, int b, int c, int d) throws ErrnoException, FaultException {
104 case SYS_kill: return sys_kill(a,b);
105 case SYS_fork: return sys_fork();
106 case SYS_pipe: return sys_pipe(a);
107 case SYS_dup2: return sys_dup2(a,b);
108 case SYS_waitpid: return sys_waitpid(a,b,c);
109 case SYS_stat: return sys_stat(a,b);
110 case SYS_lstat: return sys_lstat(a,b);
111 case SYS_mkdir: return sys_mkdir(a,b);
112 case SYS_getcwd: return sys_getcwd(a,b);
113 case SYS_chdir: return sys_chdir(a);
114 case SYS_exec: return sys_exec(a,b,c);
116 default: return super._syscall(syscall,a,b,c,d);
120 FD _open(String path, int flags, int mode) throws ErrnoException {
121 return gd.open(this,normalizePath(path),flags,mode);
124 /** The kill syscall.
125 SIGSTOP, SIGTSTO, SIGTTIN, and SIGTTOUT pause the process.
126 SIGCONT, SIGCHLD, SIGIO, and SIGWINCH are ignored.
127 Anything else terminates the process. */
128 private int sys_kill(int pid, int signal) {
129 // This will only be called by raise() in newlib to invoke the default handler
130 // We don't have to worry about actually delivering the signal
131 if(pid != pid) return -ESRCH;
132 if(signal < 0 || signal >= 32) return -EINVAL;
147 return syscall(SYS_exit,128+signal,0,0,0);
152 private int sys_waitpid(int pid, int statusAddr, int options) throws FaultException, ErrnoException {
153 System.err.println("PID: " + this.pid + " is waiting on " + pid);
154 final int WNOHANG = 1;
155 if((options & ~(WNOHANG)) != 0) return -EINVAL;
156 if(pid == 0 || pid < -1) {
157 System.err.println("WARNING: waitpid called with a pid of " + pid);
160 boolean blocking = (options&WNOHANG)==0;
162 if(pid !=-1 && (pid <= 0 || pid >= gd.tasks.length)) return -ECHILD;
163 if(children == null) return blocking ? -ECHILD : 0;
165 UnixRuntime done = null;
167 synchronized(children) {
170 if(exitedChildren.size() > 0) done = (UnixRuntime)exitedChildren.remove(exitedChildren.size() - 1);
172 UnixRuntime t = gd.tasks[pid];
173 if(t.parent != this) return -ECHILD;
174 if(t.state == EXITED) {
175 if(!exitedChildren.remove(t)) throw new Error("should never happen");
179 // process group stuff, EINVAL returned above
180 throw new Error("should never happen");
183 if(!blocking) return 0;
184 try { children.wait(); } catch(InterruptedException e) {}
185 System.err.println("waitpid woke up: " + exitedChildren.size());
187 gd.tasks[done.pid] = null;
192 if(statusAddr!=0) memWrite(statusAddr,done.exitStatus()<<8);
198 if(children != null) synchronized(children) {
199 for(Enumeration e = exitedChildren.elements(); e.hasMoreElements(); ) {
200 UnixRuntime child = (UnixRuntime) e.nextElement();
201 gd.tasks[child.pid] = null;
203 exitedChildren.clear();
204 for(Enumeration e = activeChildren.elements(); e.hasMoreElements(); ) {
205 UnixRuntime child = (UnixRuntime) e.nextElement();
208 activeChildren.clear();
211 UnixRuntime _parent = parent;
212 if(_parent == null) {
213 gd.tasks[pid] = null;
215 synchronized(_parent.children) {
217 gd.tasks[pid] = null;
219 parent.activeChildren.remove(this);
220 parent.exitedChildren.add(this);
221 parent.children.notify();
227 protected Object clone() throws CloneNotSupportedException {
228 UnixRuntime r = (UnixRuntime) super.clone();
232 r.activeChildren = r.exitedChildren = null;
236 private int sys_fork() {
237 CPUState state = new CPUState();
239 int sp = state.r[SP];
243 r = (UnixRuntime) clone();
244 } catch(Exception e) {
253 } catch(ProcessTableFullExn e) {
257 System.err.println("fork " + pid + " -> " + r.pid + " tasks[" + r.pid + "] = " + gd.tasks[r.pid]);
258 if(children == null) {
259 children = new Object();
260 activeChildren = new Vector();
261 exitedChildren = new Vector();
263 activeChildren.add(r);
265 state.r[V0] = 0; // return 0 to child
266 state.pc += 4; // skip over syscall instruction
267 r.setCPUState(state);
270 new ForkedProcess(r);
275 public static final class ForkedProcess extends Thread {
276 private final UnixRuntime initial;
277 public ForkedProcess(UnixRuntime initial) { this.initial = initial; start(); }
278 public void run() { UnixRuntime.executeAndExec(initial); }
281 public static int runAndExec(UnixRuntime r, String argv0, String[] rest) { return runAndExec(r,concatArgv(argv0,rest)); }
282 public static int runAndExec(UnixRuntime r, String[] argv) { r.start(argv); return executeAndExec(r); }
284 public static int executeAndExec(UnixRuntime r) {
287 if(r.execute()) break;
288 System.err.println("WARNING: Pause requested while executing runAndExec()");
290 if(r.state != EXECED) return r.exitStatus();
295 private String[] readStringArray(int addr) throws ReadFaultException {
297 for(int p=addr;memRead(p) != 0;p+=4) count++;
298 String[] a = new String[count];
299 for(int i=0,p=addr;i<count;i++,p+=4) a[i] = cstring(memRead(p));
303 private int sys_exec(int cpath, int cargv, int cenvp) throws ErrnoException, FaultException {
304 return exec(normalizePath(cstring(cpath)),readStringArray(cargv),readStringArray(cenvp));
307 private int exec(String normalizedPath, String[] argv, String[] envp) throws ErrnoException {
308 if(argv.length == 0) argv = new String[]{""};
310 Object o = gd.exec(this,normalizedPath);
311 if(o == null) return -ENOENT;
313 if(o instanceof Class) {
316 return exec((UnixRuntime) c.newInstance(),argv,envp);
317 } catch(Exception e) {
322 String[] command = (String[]) o;
323 String[] newArgv = new String[argv.length + command[1] != null ? 2 : 1];
324 int p = command[0].lastIndexOf('/');
325 newArgv[0] = p == -1 ? command[0] : command[0].substring(p+1);
327 if(command[1] != null) newArgv[p++] = command[1];
328 newArgv[p++] = "/" + normalizedPath;
329 for(int i=1;i<argv.length;i++) newArgv[p++] = argv[i];
330 return exec(command[0],newArgv,envp);
334 private int exec(UnixRuntime r, String[] argv, String[] envp) {
336 System.err.println("Execing " + r);
337 for(int i=0;i<OPEN_MAX;i++) if(closeOnExec[i]) closeFD(i);
339 r.closeOnExec = closeOnExec;
340 // make sure this doesn't get messed with these since we didn't copy them
357 // FEATURE: Use custom PipeFD - be sure to support PIPE_BUF of data
358 private int sys_pipe(int addr) throws FaultException {
359 PipedOutputStream writerStream = new PipedOutputStream();
360 PipedInputStream readerStream;
362 readerStream = new PipedInputStream(writerStream);
363 } catch(IOException e) {
366 FD reader = new InputStreamFD(readerStream);
367 FD writer = new OutputStreamFD(writerStream);
368 int fd1 = addFD(reader);
369 if(fd1 < 0) return -ENFILE;
370 int fd2 = addFD(writer);
371 if(fd2 < 0) { closeFD(fd1); return -ENFILE; }
374 memWrite(addr+4,fd2);
375 } catch(FaultException e) {
383 private int sys_dup2(int oldd, int newd) {
384 if(oldd == newd) return 0;
385 if(oldd < 0 || oldd >= OPEN_MAX) return -EBADFD;
386 if(newd < 0 || newd >= OPEN_MAX) return -EBADFD;
387 if(fds[oldd] == null) return -EBADFD;
388 if(fds[newd] != null) fds[newd].close();
389 fds[newd] = fds[oldd].dup();
393 private int sys_stat(int cstring, int addr) throws FaultException, ErrnoException {
394 FStat s = gd.stat(this,normalizePath(cstring(cstring)));
395 if(s == null) return -ENOENT;
399 private int sys_lstat(int cstring, int addr) throws FaultException, ErrnoException {
400 FStat s = gd.lstat(this,normalizePath(cstring(cstring)));
401 if(s == null) return -ENOENT;
405 private int sys_mkdir(int cstring, int mode) throws FaultException, ErrnoException {
406 gd.mkdir(this,normalizePath(cstring(cstring)),mode);
411 private int sys_getcwd(int addr, int size) throws FaultException, ErrnoException {
412 byte[] b = getBytes(cwd);
413 if(size == 0) return -EINVAL;
414 if(size < b.length+2) return -ERANGE;
416 copyout(b,addr+1,b.length);
417 memset(addr+b.length+1,0,1);
421 private int sys_chdir(int addr) throws ErrnoException, FaultException {
422 String path = normalizePath(cstring(addr));
423 System.err.println("Chdir: " + cstring(addr) + " -> " + path + " pwd: " + cwd);
424 if(gd.stat(this,path).type() != FStat.S_IFDIR) return -ENOTDIR;
426 System.err.println("Now: [" + cwd + "]");
430 // FEATURE: Run through the fork/wait stuff one more time
431 public static class GlobalState {
432 protected static final int OPEN = 1;
433 protected static final int STAT = 2;
434 protected static final int LSTAT = 3;
435 protected static final int MKDIR = 4;
437 final UnixRuntime[] tasks;
440 private final MP[][] mps = new MP[128][];
443 public GlobalState() { this(255); }
444 public GlobalState(int maxProcs) { this(maxProcs,true); }
445 public GlobalState(int maxProcs, boolean defaultMounts) {
446 tasks = new UnixRuntime[maxProcs+1];
449 addMount("/dev",new DevFS());
453 private static class MP {
454 public MP(String path, FS fs) { this.path = path; this.fs = fs; }
457 public int compareTo(Object o) {
458 if(!(o instanceof MP)) return 1;
459 return -path.compareTo(((MP)o).path);
463 public synchronized FS getMount(String path) {
464 if(!path.startsWith("/")) throw new IllegalArgumentException("Mount point doesn't start with a /");
465 if(path.equals("/")) return root;
466 path = path.substring(1);
467 int f = path.charAt(0) & 0x7f;
468 for(int i=0;mps[f] != null && i < mps[f].length;i++)
469 if(mps[f][i].path.equals(path)) return mps[f][i].fs;
473 public synchronized void addMount(String path, FS fs) {
474 if(getMount(path) != null) throw new IllegalArgumentException("mount point already exists");
475 if(!path.startsWith("/")) throw new IllegalArgumentException("Mount point doesn't start with a /");
476 if(path.equals("/")) { root = fs; return; }
477 path = path.substring(1);
478 int f = path.charAt(0) & 0x7f;
479 int oldLength = mps[f] == null ? 0 : mps[f].length;
480 MP[] newList = new MP[oldLength + 1];
481 if(oldLength != 0) System.arraycopy(mps[f],0,newList,0,oldLength);
482 newList[oldLength] = new MP(path,fs);
483 Arrays.sort(newList);
487 public synchronized void removeMount(String path) {
488 if(getMount(path) == null) throw new IllegalArgumentException("mount point doesn't exist");
489 if(!path.startsWith("/")) throw new IllegalArgumentException("Mount point doesn't start with a /");
490 if(path.equals("/")) { root = null; return; }
491 path = path.substring(1);
492 int f = path.charAt(0) & 0x7f;
493 MP[] oldList = mps[f];
494 MP[] newList = new MP[oldList.length - 1];
496 for(p=0;p<oldList.length;p++) if(oldList[p].path.equals(path)) break;
497 if(p == oldList.length) throw new Error("should never happen");
498 System.arraycopy(oldList,0,newList,0,p);
499 System.arraycopy(oldList,0,newList,p,oldList.length-p-1);
503 private Object fsop(int op, UnixRuntime r, String path, int arg1, int arg2) throws ErrnoException {
504 int pl = path.length();
506 MP[] list = mps[path.charAt(0) & 0x7f];
508 for(int i=0;i<list.length;i++) {
510 int mpl = mp.path.length();
511 if(path.startsWith(mp.path) && (pl == mpl || (pl < mpl && path.charAt(mpl) == '/')))
512 return dispatch(mp.fs,op,r,pl == mpl ? "" : path.substring(mpl+1),arg1,arg2);
516 return dispatch(root,op,r,path,arg1,arg2);
519 private static Object dispatch(FS fs, int op, UnixRuntime r, String path, int arg1, int arg2) throws ErrnoException {
521 case OPEN: return fs.open(r,path,arg1,arg2);
522 case STAT: return fs.stat(r,path);
523 case LSTAT: return fs.lstat(r,path);
524 case MKDIR: fs.mkdir(r,path,arg1); return null;
525 default: throw new Error("should never happen");
529 public final FD open(UnixRuntime r, String path, int flags, int mode) throws ErrnoException { return (FD) fsop(OPEN,r,path,flags,mode); }
530 public final FStat stat(UnixRuntime r, String path) throws ErrnoException { return (FStat) fsop(STAT,r,path,0,0); }
531 public final FStat lstat(UnixRuntime r, String path) throws ErrnoException { return (FStat) fsop(LSTAT,r,path,0,0); }
532 public final void mkdir(UnixRuntime r, String path, int mode) throws ErrnoException { fsop(MKDIR,r,path,mode,0); }
534 private Hashtable execCache = new Hashtable();
535 private static class CacheEnt {
536 public final long time;
537 public final long size;
538 public final Object o;
539 public CacheEnt(long time, long size, Object o) { this.time = time; this.size = size; this.o = o; }
542 public synchronized Object exec(UnixRuntime r, String path) throws ErrnoException {
543 FStat fstat = stat(r,path);
544 if(fstat == null) return null;
545 long mtime = fstat.mtime();
546 long size = fstat.size();
547 CacheEnt ent = (CacheEnt) execCache.get(path);
549 System.err.println("Found cached entry for " + path);
550 if(ent.time == mtime && ent.size == size) return ent.o;
551 System.err.println("Cache was out of date");
552 execCache.remove(path);
554 FD fd = open(r,path,RD_ONLY,0);
555 if(fd == null) return null;
556 Seekable s = fd.seekable();
558 String[] command = null;
560 if(s == null) throw new ErrnoException(EACCES);
561 byte[] buf = new byte[4096];
564 int n = s.read(buf,0,buf.length);
565 if(n == -1) throw new Error("zero length file");
568 case '\177': // possible ELF
569 if(n < 4 && s.tryReadFully(buf,n,4-n) != 4-n) throw new ErrnoException(ENOEXEC);
570 if(buf[1] != 'E' || buf[2] != 'L' || buf[3] != 'F') throw new ErrnoException(ENOEXEC);
574 int n2 = s.read(buf,1,buf.length-1);
575 if(n2 == -1) throw new ErrnoException(ENOEXEC);
578 if(buf[1] != '!') throw new ErrnoException(ENOEXEC);
582 for(int i=p;i<p+n;i++) if(buf[i] == '\n') { p = i; break OUTER; }
584 if(p == buf.length) break OUTER;
585 n = s.read(buf,p,buf.length-p);
587 command = new String[2];
589 for(arg=2;arg<p;arg++) if(buf[arg] == ' ') break;
592 while(arg < p && buf[arg] == ' ') arg++;
593 command[0] = new String(buf,2,cmdEnd);
594 command[1] = arg < p ? new String(buf,arg,p-arg) : null;
596 command[0] = new String(buf,2,p-2);
598 System.err.println("command[0]: " + command[0] + " command[1]: " + command[1]);
601 throw new ErrnoException(ENOEXEC);
603 } catch(IOException e) {
605 throw new ErrnoException(EIO);
608 if(command == null) {
612 Class c = RuntimeCompiler.compile(s);
613 System.err.println("Compile succeeded: " + c);
614 ent = new CacheEnt(mtime,size,c);
615 } catch(Compiler.Exn e) {
616 throw new ErrnoException(ENOEXEC);
617 } catch(IOException e) {
618 throw new ErrnoException(EIO);
621 ent = new CacheEnt(mtime,size,command);
626 execCache.put(path,ent);
631 public abstract static class FS {
632 // FIXME: inode stuff
633 protected static FD directoryFD(String[] files, int hashCode) {
634 ByteArrayOutputStream bos = new ByteArrayOutputStream();
635 DataOutputStream dos = new DataOutputStream(bos);
637 for(int i=0;i<files.length;i++) {
638 byte[] b = getBytes(files[i]);
639 int inode = (files[i].hashCode() ^ hashCode) & 0xfffff;
641 dos.writeInt(b.length);
642 dos.write(b,0,b.length);
644 } catch(IOException e) {
647 final byte[] data = bos.toByteArray();
648 return new SeekableFD(new Seekable.ByteArray(data,false),RD_ONLY) {
649 protected FStat _fstat() { return new FStat() {
650 public int length() { return data.length; }
651 public int type() { return S_IFDIR; }
656 public FStat lstat(UnixRuntime r, String path) throws ErrnoException { return stat(r,path); }
658 // If this returns null it'll be truned into an ENOENT
659 public abstract FD open(UnixRuntime r, String path, int flags, int mode) throws ErrnoException;
660 // If this returns null it'll be turned into an ENOENT
661 public abstract FStat stat(UnixRuntime r, String path) throws ErrnoException;
662 public abstract void mkdir(UnixRuntime r, String path, int mode) throws ErrnoException;
665 // FEATURE: chroot support in here
666 private String normalizePath(String path) {
667 boolean absolute = path.startsWith("/");
668 int cwdl = cwd.length();
669 // NOTE: This isn't just a fast path, it handles cases the code below doesn't
670 if(!path.startsWith(".") && path.indexOf("./") == -1 && path.indexOf("//") == -1 && !path.endsWith("."))
671 return absolute ? path.substring(1) : cwdl == 0 ? path : path.length() == 0 ? cwd : cwd + "/" + path;
673 char[] in = new char[path.length()+1];
674 char[] out = new char[in.length + (absolute ? -1 : cwd.length())];
678 do { inp++; } while(in[inp] == '/');
679 } else if(cwdl != 0) {
680 cwd.getChars(0,cwdl,out,0);
684 path.getChars(0,path.length(),in,0);
685 while(in[inp] != 0) {
687 if(in[inp] != '/') { out[outp++] = in[inp++]; continue; }
688 while(in[inp] == '/') inp++;
690 if(in[inp] == '\0') continue;
691 if(in[inp] != '.') { out[outp++] = '/'; out[outp++] = in[inp++]; continue; }
692 if(in[inp+1] == '\0' || in[inp+1] == '/') { inp++; continue; }
693 if(in[inp+1] == '.' && (in[inp+2] == '\0' || in[inp+2] == '/')) { // ..
696 while(outp > 0 && out[outp] != '/') outp--;
697 System.err.println("After ..: " + new String(out,0,outp));
704 if(outp > 0 && out[outp-1] == '/') outp--;
705 //System.err.println("normalize: " + path + " -> " + new String(out,0,outp) + " (cwd: " + cwd + ")");
706 return new String(out,0,outp);
709 FStat hostFStat(final File f) {
712 FileInputStream fis = new FileInputStream(f);
714 case '\177': e = fis.read() == 'E' && fis.read() == 'L' && fis.read() == 'F'; break;
715 case '#': e = fis.read() == '!';
718 } catch(IOException e2) { }
719 return new HostFStat(f,e);
722 // FIXME: inode stuff
723 FD hostFSDirFD(File f) { return FS.directoryFD(f.list(),f.hashCode()); }
725 public static class HostFS extends FS {
727 public File getRoot() { return root; }
729 private static File hostRootDir() {
730 String cwd = getSystemProperty("user.dir");
731 File f = new File(cwd != null ? cwd : ".");
732 f = new File(f.getAbsolutePath());
733 while(f.getParent() != null) f = new File(f.getParent());
737 private File hostFile(String path) {
738 char sep = File.separatorChar;
740 char buf[] = path.toCharArray();
741 for(int i=0;i<buf.length;i++) {
743 if(c == '/') buf[i] = sep;
744 else if(c == sep) buf[i] = '/';
746 path = new String(buf);
748 return new File(root,path);
751 public HostFS() { this(hostRootDir()); }
752 public HostFS(String root) { this(new File(root)); }
753 public HostFS(File root) { this.root = root; }
756 public FD open(UnixRuntime r, String path, int flags, int mode) throws ErrnoException {
757 final File f = hostFile(path);
758 return r.hostFSOpen(f,flags,mode);
761 public FStat stat(UnixRuntime r, String path) throws ErrnoException {
762 File f = hostFile(path);
763 if(r.sm != null && !r.sm.allowStat(f)) throw new ErrnoException(EACCES);
764 if(!f.exists()) return null;
765 return r.hostFStat(f);
768 public void mkdir(UnixRuntime r, String path, int mode) throws ErrnoException {
769 File f = hostFile(path);
770 if(r.sm != null && !r.sm.allowWrite(f)) throw new ErrnoException(EACCES);
771 if(f.exists() && f.isDirectory()) throw new ErrnoException(EEXIST);
772 if(f.exists()) throw new ErrnoException(ENOTDIR);
773 File parent = f.getParentFile();
774 if(parent!=null && (!parent.exists() || !parent.isDirectory())) throw new ErrnoException(ENOTDIR);
775 if(!f.mkdir()) throw new ErrnoException(EIO);
779 public static class DevFS extends FS {
780 private static class DevFStat extends FStat {
781 public int dev() { return 1; }
782 public int mode() { return 0666; }
783 public int type() { return S_IFCHR; }
784 public int nlink() { return 1; }
786 private static FD devZeroFD = new FD() {
787 public boolean readable() { return true; }
788 public boolean writable() { return true; }
789 public int read(byte[] a, int off, int length) { Arrays.fill(a,off,off+length,(byte)0); return length; }
790 public int write(byte[] a, int off, int length) { return length; }
791 public int seek(int n, int whence) { return 0; }
792 public FStat _fstat() { return new DevFStat(); }
794 private static FD devNullFD = new FD() {
795 public boolean readable() { return true; }
796 public boolean writable() { return true; }
797 public int read(byte[] a, int off, int length) { return 0; }
798 public int write(byte[] a, int off, int length) { return length; }
799 public int seek(int n, int whence) { return 0; }
800 public FStat _fstat() { return new DevFStat(); }
803 public FD open(UnixRuntime r, String path, int mode, int flags) throws ErrnoException {
804 if(path.equals("null")) return devNullFD;
805 if(path.equals("zero")) return devZeroFD;
806 if(path.startsWith("fd/")) {
809 n = Integer.parseInt(path.substring(4));
810 } catch(NumberFormatException e) {
813 if(n < 0 || n >= OPEN_MAX) return null;
814 if(r.fds[n] == null) return null;
815 return r.fds[n].dup();
817 if(path.equals("fd")) {
819 for(int i=0;i<OPEN_MAX;i++) if(r.fds[i] != null) count++;
820 String[] files = new String[count];
822 for(int i=0;i<OPEN_MAX;i++) if(r.fds[i] != null) files[count++] = Integer.toString(i);
823 return directoryFD(files,hashCode());
825 if(path.equals("")) {
826 String[] files = { "null", "zero", "fd" };
827 return directoryFD(files,hashCode());
832 public FStat stat(UnixRuntime r,String path) throws ErrnoException {
833 if(path.equals("null")) return devNullFD.fstat();
834 if(path.equals("zero")) return devZeroFD.fstat();
835 if(path.startsWith("fd/")) {
838 n = Integer.parseInt(path.substring(4));
839 } catch(NumberFormatException e) {
842 if(n < 0 || n >= OPEN_MAX) return null;
843 if(r.fds[n] == null) return null;
844 return r.fds[n].fstat();
846 if(path.equals("fd")) return new FStat() { public int type() { return S_IFDIR; } public int mode() { return 0444; }};
847 if(path.equals("")) return new FStat() { public int type() { return S_IFDIR; } public int mode() { return 0444; }};
851 public void mkdir(UnixRuntime r, String path, int mode) throws ErrnoException { throw new ErrnoException(EACCES); }