/// <summary> /// Starts a new process based on the given path and arguments /// </summary> /// <param name="path">The path</param> /// <param name="argv">The arguments</param> /// <param name="flags">Spawn flags</param> /// <returns>Errorcode or PID</returns> public static int StartProcess(string path, string[] argv, Task.SpawnFlags flags) { if (argv == null) { Panic.DoPanic("argv == null"); } Node node = VFS.GetByAbsolutePath(path); if (node == null) { return(-(int)ErrorCode.ENOENT); } // Open and create buffer VFS.Open(node, (int)FileMode.O_RDONLY); byte[] buffer = new byte[node.Size]; if (buffer == null) { Heap.Free(node); VFS.Close(node); return(-(int)ErrorCode.ENOMEM); } // Fill buffer contents VFS.Read(node, 0, node.Size, buffer); VFS.Close(node); // Pass execution to ELF loader int status = ELFLoader.Execute(buffer, node.Size, argv, flags); Heap.Free(buffer); Heap.Free(node); return(status); }
/// <summary> /// Cleans up the file descriptors /// </summary> public void Cleanup() { for (int i = 0; i < Used; i++) { Node node = GetNode(i); if (node == null) { continue; } VFS.Close(node); Heap.Free(node); } }
/// <summary> /// Closes a node /// </summary> /// <param name="descriptor">The file descriptor</param> /// <returns>The errorcode</returns> public int Close(int descriptor) { Node node = GetNode(descriptor); if (node == null) { return(-(int)ErrorCode.EBADF); } VFS.Close(node); Nodes[descriptor] = null; Used--; return(0); }
/// <summary> /// Replaces an old file descriptor with a new one /// </summary> /// <param name="oldfd">The old file descriptor</param> /// <param name="newfd">The new file descriptor</param> /// <returns>The new file descriptor, or an errorcode</returns> public int Dup2(int oldfd, int newfd) { if (oldfd < 0 || newfd < 0 || oldfd >= Capacity || newfd >= Capacity) { return(-(int)ErrorCode.EBADF); } // If there is an old file descriptor node, close it Node old = GetNode(oldfd); if (old != null) { VFS.Close(old); } // Clone the new one, replacing the old one Nodes[oldfd] = Nodes[newfd].Clone(); return(newfd); }