int sys_pipe(unsigned int *filedes) { int fd[2]; int error; debug("PIPE: called.\n"); if ((error = do_pipe(fd))) return error; debug2("PIPE: Returned %d %d.\n", fd[0], fd[1]); return verified_memcpy_tofs(filedes, fd, 2 * sizeof(int)); }
/* * sys_pipe() is the normal C calling standard for creating * a pipe. It's not the way Unix traditionally does this, though. */ asmlinkage int sys_pipe(unsigned long r4, unsigned long r5, unsigned long r6, unsigned long r7, struct pt_regs regs) { int fd[2]; int error; error = do_pipe(fd); if (!error) { regs.regs[1] = fd[1]; return fd[0]; } return error; }
int main(int ac, char *argv[]) { t_pipe *pipe; pipe = malloc(sizeof(*pipe)); pipe->cmds = malloc(5 * sizeof(char **)); pipe->cmds[0] = malloc(10 * sizeof(char*)); pipe->cmds[0][0] = malloc(128 * sizeof(char)); strcpy(pipe->cmds[0][0], "/usr/bin/env"); pipe->cmds[0][1] = malloc(128 * sizeof(char)); strcpy(pipe->cmds[0][1], "env"); pipe->cmds[0][2] = malloc(128 * sizeof(char)); pipe->cmds[0][2] = NULL; pipe->cmds[1] = malloc(10 * sizeof(char*)); pipe->cmds[1][0] = malloc(128 * sizeof(char)); strcpy(pipe->cmds[1][0], "/bin/grep"); pipe->cmds[1][1] = malloc(128 * sizeof(char)); strcpy(pipe->cmds[1][1], "grep"); pipe->cmds[1][2] = malloc(128 * sizeof(char)); strcpy(pipe->cmds[1][2], "-e"); pipe->cmds[1][3] = malloc(128 * sizeof(char)); strcpy(pipe->cmds[1][3], "HOME"); pipe->cmds[1][4] = malloc(128 * sizeof(char)); pipe->cmds[1][4] = NULL; pipe->cmds[2] = malloc(10 * sizeof(char*)); pipe->cmds[2][0] = malloc(128 * sizeof(char)); strcpy(pipe->cmds[2][0], "/bin/less"); pipe->cmds[2][1] = malloc(128 * sizeof(char)); strcpy(pipe->cmds[2][1], "less"); pipe->cmds[2][2] = malloc(128 * sizeof(char)); pipe->cmds[2][2] = NULL; pipe->cmds[3] = malloc(10 * sizeof(char*)); pipe->cmds[3][0] = malloc(128 * sizeof(char)); strcpy(pipe->cmds[3][0], "/bin/cat"); pipe->cmds[3][1] = malloc(128 * sizeof(char)); strcpy(pipe->cmds[3][1], "cat"); pipe->cmds[3][2] = malloc(128 * sizeof(char)); pipe->cmds[3][2] = NULL; pipe->total = 2; if (do_pipe(pipe) != -1) printf("\t\t\t\t\t\t=> Infinite Pipe successed\n"); else printf("\t\t\t\t\t\t=> Infinite Pipe failed\n"); }
/* * sys_pipe() is the normal C calling standard for creating * a pipe. It's not the way Unix traditionally does this, though. */ asmlinkage int sys_pipe(unsigned long * fildes) { int fd[2]; int error; lock_kernel(); error = do_pipe(fd); unlock_kernel(); if (!error) { if (copy_to_user(fildes, fd, 2*sizeof(int))) error = -EFAULT; } return error; }
/* * sys_pipe() is the normal C calling standard for creating * a pipe. It's not the way unix traditionally does this, though. */ asmlinkage int sys_pipe(unsigned long * fildes) { int fd[2]; int error; error = verify_area(VERIFY_WRITE,fildes,8); if (error) return error; error = do_pipe(fd); if (error) return error; put_user(fd[0],0+fildes); put_user(fd[1],1+fildes); return 0; }
asmlinkage int sys_pipe(abi64_no_regargs, struct pt_regs regs) { int fd[2]; int error, res; error = do_pipe(fd); if (error) { res = error; goto out; } regs.regs[3] = fd[1]; res = fd[0]; out: return res; }
/* * On IA-64, we return the two file descriptors in ret0 and ret1 (r8 * and r9) as this is faster than doing a copy_to_user(). */ asmlinkage long sys_pipe (void) { struct pt_regs *regs = ia64_task_regs(current); int fd[2]; int retval; retval = do_pipe(fd); if (retval) goto out; retval = fd[0]; regs->r9 = fd[1]; out: return retval; }
/* * On IA-64, we return the two file descriptors in ret0 and ret1 (r8 * and r9) as this is faster than doing a copy_to_user(). */ asmlinkage long sys_pipe (long arg0, long arg1, long arg2, long arg3, long arg4, long arg5, long arg6, long arg7, long stack) { struct pt_regs *regs = (struct pt_regs *) &stack; int fd[2]; int retval; retval = do_pipe(fd); if (retval) goto out; retval = fd[0]; regs->r9 = fd[1]; out: return retval; }
/* * sys_pipe() is the normal C calling standard for creating * a pipe. It's not the way unix traditionally does this, though. */ asmlinkage int sys_pipe(int *fildes) { int fd[2]; int error; PPCDBG(PPCDBG_SYS64X, "sys_pipe - entered - pid=%ld current=%lx comm=%s \n", current->pid, current, current->comm); error = do_pipe(fd); if (!error) { if (copy_to_user(fildes, fd, 2*sizeof(int))) error = -EFAULT; } PPCDBG(PPCDBG_SYS64X, "sys_pipe - exited - pid=%ld current=%lx comm=%s \n", current->pid, current, current->comm); return error; }
asmlinkage int sys_pipe(struct pt_regs regs) { int fd[2]; int error, res; lock_kernel(); error = do_pipe(fd); if (error) { res = error; goto out; } set_gpreg(®s, 3, fd[1]); res = fd[0]; out: unlock_kernel(); return res; }
static int sys_pipe(int arg[2]) { int kern_args[2]; int ret; ret = do_pipe(kern_args); if (ret == 0) { ret = copy_to_user(arg, kern_args, sizeof(kern_args)); } if (ret != 0) { curthr->kt_errno = -ret; return -1; } return 0; }
int main(int argc, char **argv) { extern char *__progname; int ret, c; ret = 0; while ((c = getopt(argc, argv, "fFlpPrstT")) != -1) { switch (c) { case 'f': ret |= check_inheritance(); break; case 'F': ret |= do_fdpass(); break; case 'l': ret |= do_flock(); break; case 'p': ret |= do_pipe(); break; case 'P': ret |= do_process(); break; case 'r': ret |= do_random(); break; case 's': ret |= do_signal(); break; case 't': ret |= do_tun(); break; case 'T': ret |= do_pty(); break; default: fprintf(stderr, "Usage: %s -[fPprTt]\n", __progname); exit(1); } } return (ret); }
//MAIN int main() { signal(SIGINT, sighandler); char cmd[MAX_COMMAND_LENGTH + 1]; char histar[10][MAX_COMMAND_LENGTH + 1]; char hischar[MAX_COMMAND_LENGTH + 1]; int a = 0; int piped =0; int redirin = 0; int redirout = 0; int hiscount = 0; while(1) { char* params[MAX_NUMBER_OF_PARAMS + 1]; printf("azarifog> "); // Read command from standard input if(fgets(cmd, sizeof(cmd), stdin) == NULL) break; // Remove trailing newline character, if any if(cmd[strlen(cmd)-1] == '\n') { cmd[strlen(cmd)-1] = '\0'; } //Copy the input for history to save later.. strncpy(hischar, cmd, MAX_COMMAND_LENGTH + 1); // Split cmd into array of parameters int n = parseCmd(cmd, params); // If exit is entered if(strcmp(params[0], "exit") == 0) break; //History if(strcmp(params[0], "history") == 0){ int count_num = 1; if(hiscount < 10) count_num = hiscount; else count_num = 10; for(int h = 0 ; h < count_num; h++){ printf("%s\n" , histar[h]); } continue; } else { int hc = hiscount % 10; strcpy(histar[hc], hischar); //printf("Copying to index at %d: %s\n" ,hc, hischar); hiscount++; } //Check variables to find out if commands is pipe or redirect for(a = 0; a < n; a++){ if(!strcmp(params[a], "|")){ piped++; //pipe command entered //printf("piped: %i", piped); } if(!strcmp(params[a], "<")){ redirin++; //redireted in command //printf("redirin: %i", redirin); } if(!strcmp(params[a], ">")){ redirout++; //redirected out command //printf("redirout: %i", redirout); } } //If there is no need for pipe or redirection if(piped == 0 && redirin == 0 && redirout == 0){ //printf("PARAM: %s\n", params[0]); if(executeCmd(params) == 0) break; } else{ //If there is a need for pipe if(piped != 0 ){ //Pipe function do_pipe(params, piped); } //If there is redirection else if(redirin == 1 || redirout == 1){ int ic1 = 2; int ic2 = 4; for(int i=0;params[i]!='\0';i++) { if(strcmp(params[i],"<")==0) { params[i]=NULL; ic1 = i+1; } else if(strcmp(params[i],">")==0) { params[i]=NULL; ic2 = i+1; } } // Fork process pid_t pid = fork(); // Error if (pid == -1) { char* error = strerror(errno); printf("fork: %s\n", error); return 1; } // Child process else if (pid == 0) { int in, out; int p = 0; char *args2[MAX_COMMAND_LENGTH + 1] = {}; for(p =0; p < ic1; p++){ args2[p] = params [p]; //printf("%s" , args2[p]); } args2[p] = NULL; // Open input and output files if(redirin == 1){ in = open(params[ic1], O_RDONLY); //Error handling if the file does not exist. if(in == -1){ printf("The file does not exist: %s\n", params[ic1]); piped = 0; redirin = 0; redirout = 0; close(in); break; } // Replace standard input with input file dup2(in, 0); close(in); } if(redirout == 1){ out = open(params[ic2], O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR); // replace standard output with output file dup2(out, 1); close(out); } // execute arguments execvp(args2[0], args2); // Error occurred char* error = strerror(errno); printf("Error! azarifog: %s: %s\n", params[0], error); } // Parent process else { // Wait for child process to finish int childStatus; waitpid(pid, &childStatus, 0); } } // redirect } //reset variables piped = 0; redirin = 0; redirout = 0; //For the signal handler testing //sleep(1); } return 0; }
void eval(char *cmdline) { char *token[MAXCMDS][MAXARGS]; /* token[i] is a command typed in the command line */ int nbcmd; /* number of commands typed in the command line */ int bg; /* should the job run in bg or fg? */ pid_t pid; /* process id */ sigset_t mask; /* signal mask */ char **argv; /* Parse command line */ bg = parseline(cmdline, token, &nbcmd); if (nbcmd > 1) /* a command pipeline has been typed in */ do_pipe(token, nbcmd, bg); else { /* no pipeline, only one command */ argv = token[0]; if (!builtin_cmd(argv)) { /* * This is a little tricky. Block SIGCHLD, SIGINT, and SIGTSTP * signals until we can add the job to the job list. This * eliminates some nasty races between adding a job to the job * list and the arrival of SIGCHLD, SIGINT, and SIGTSTP signals. */ if (sigemptyset(&mask) < 0) unix_error("sigemptyset error"); if (sigaddset(&mask, SIGCHLD)) unix_error("sigaddset error"); if (sigaddset(&mask, SIGINT)) unix_error("sigaddset error"); if (sigaddset(&mask, SIGTSTP)) unix_error("sigaddset error"); if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) unix_error("sigprocmask error"); /* Create a child process */ if ((pid = fork()) < 0) unix_error("fork error"); /* * Child process */ if (pid == 0) { /* Child unblocks signals */ sigprocmask(SIG_UNBLOCK, &mask, NULL); /* Each new job must get a new process group ID so that the kernel doesn't send ctrl-c and ctrl-z signals to all of the shell's jobs */ if (setpgid(0, 0) < 0) unix_error("setpgid error"); /* Now load and run the program in the new job */ if (execvp(argv[0], argv) < 0) { printf("%s: Command not found\n", argv[0]); exit(EXIT_FAILURE); } } /* * Parent process */ /* Parent adds the job, and then unblocks signals so that the signals handlers can run again */ jobs_addjob(pid, (bg == 1 ? BG : FG), cmdline); sigprocmask(SIG_UNBLOCK, &mask, NULL); if (!bg) waitfg(pid); else printf("[%d] (%d) %s\n", jobs_pid2jid(pid), (int) pid, cmdline); } } return; }
int sys_pipe2(struct tcb *tcp) { return do_pipe(tcp, 1); }