/* * Spawn a single command. */ void Spawn_Single_Command(struct Process procList[], int nproc, const char *path) { int pid; if (nproc > 1) { Print("Error: pipes not supported yet\n"); return; } if (procList[0].flags & (INFILE|OUTFILE)) { Print("Error: I/O redirection not supported yet\n"); return; } Print("program:%s\ncommand%s\npath%s\n", procList[0].program, procList[0].command, path ); pid = Spawn_With_Path(procList[0].program, procList[0].command, path); if (pid < 0) Print("Could not spawn process: %s\n", Get_Error_String(pid)); else { int exitCode = Wait(pid); if (exitCodes) Print("Exit code was %d\n", exitCode); } }
int ttestFormat() { int pid; char commandline[250]; if (false) { (void)snprintf(commandline, 249, "gfs2f.exe ide1 %u %u", disksize_mb, blocksize); pid = Spawn_With_Path("gfs2f.exe", commandline, "/c:/a", 0); return Wait(pid); } else { return 0; } }
/* * Spawn a single command. */ void Spawn_Single_Command(struct Process *proc, const char *path, int background) { int pid; pid = proc->pid = Spawn_With_Path(proc->program, proc->command, path, background); if (pid < 0) Print("Could not spawn process: %s\n", Get_Error_String(pid)); else { int exitCode = Wait(pid); if (exitCodes) Print("Exit code was %d\n", exitCode); } }
/* * Spawn a pipeline. */ void Spawn_Pipeline(struct Process procList[], int nproc, const char *path) { int i; int numSpawned, exitCode=0; bool err = false; struct Process *prev; /* * Spawn all processes in the pipeline, opening input/output files * and pipes as needed. */ for (i = 0, numSpawned = 0, prev = 0; !err && i < nproc; ++i, ++numSpawned) { struct Process *proc = &procList[i]; int readfd = 0, writefd = 1; proc->readfd = proc->writefd = proc->pipefd = proc->pid = -1; /* Open input file */ if (proc->flags & INFILE) { readfd = Open(proc->infile, O_READ); if (readfd < 0) { Print("Could not open file: %s\n", Get_Error_String(readfd)); err = true; } } if (!err) { /* Open output file or pipe */ if (proc->flags & OUTFILE) { writefd = Open(proc->outfile, O_WRITE|O_CREATE); if (writefd < 0) { Print("Could not open file: %s\n", Get_Error_String(writefd)); err = true; } } else if (proc->flags & PIPE) { int rc = Create_Pipe(&proc->pipefd, &writefd); if (rc != 0) { Print("Could not create pipe: %s\n", Get_Error_String(rc)); err = true; } } } /* Is output being piped from previous process? */ if (!err && prev != 0 && (prev->flags & PIPE)) { readfd = prev->pipefd; } proc->readfd = readfd; proc->writefd = writefd; if (!err) { proc->pid = Spawn_With_Path(proc->program, proc->command, proc->readfd, proc->writefd, path); if (proc->pid < 0) { Print("Could not spawn process: %s\n", Get_Error_String(proc->pid)); err = true; } } prev = proc; } /* * Close all pipes and I/O redirection file descriptors * (except the shell's stdin and stdout). * This won't affect the child processes because they have * their own Cloned versions of the File objects. */ for (i = 0, prev = 0; i < numSpawned; ++i) { struct Process *proc = &procList[i]; if (proc->readfd > 1) Close(proc->readfd); if (proc->writefd > 1) Close(proc->writefd); if (i == numSpawned - 1 && (proc->flags & PIPE)) { /* * Process meant to read from this pipe was never spawned. * The read fd for the pipe is stored in the "pipefd" field. */ Close(proc->pipefd); } } /* * Wait for all processes to exit. */ for (i = 0; i < numSpawned; ++i) { struct Process *proc = &procList[i]; if (proc->pid >= 0) exitCode = Wait(proc->pid); } if (numSpawned > 0 && exitCodes) Print("Exit code was %d\n", exitCode); }