/* * Function for a thread that runs an arbitrary userlevel program by * name. * * Note: this cannot pass arguments to the program. You may wish to * change it so it can, because that will make testing much easier * in the future. * * It copies the program name because runprogram destroys the copy * it gets by passing it to vfs_open(). */ static void cmd_progthread(void *ptr, unsigned long nargs) { char **args = ptr; char progname[128]; int result; KASSERT(nargs >= 1); #if OPT_A2 #else if (nargs > 2) { kprintf("Warning: argument passing from menu not supported\n"); } #endif /* Hope we fit. */ KASSERT(strlen(args[0]) < sizeof(progname)); strcpy(progname, args[0]); #if OPT_A2 //kprintf("calling runprogram"); result = runprogram(progname, args, nargs); #else result = runprogram(progname); #endif // OPT_A2 if (result) { kprintf("Running program %s failed: %s\n", args[0], strerror(result)); return; } /* NOTREACHED: runprogram only returns on error. */ }
/* * Function for a thread that runs an arbitrary userlevel program by * name. * * Note: this cannot pass arguments to the program. You may wish to * change it so it can, because that will make testing much easier * in the future. * * It copies the program name because runprogram destroys the copy * it gets by passing it to vfs_open(). */ static void cmd_progthread(void *ptr, unsigned long nargs) { char **args = ptr; char progname[128]; int result; assert(nargs >= 1); /*if (nargs > 2) { kprintf("Warning: argument passing from menu not supported\n"); } */ /* Hope we fit. */ assert(strlen(args[0]) < sizeof(progname)); strcpy(progname, args[0]); //Check if we have arguments or not. if (nargs == 1) result = runprogram(progname, NULL,0); else result = runprogram(progname,args,nargs); if (result) { kprintf("Running program %s failed: %s\n", args[0], strerror(result)); return; } /* NOTREACHED: runprogram only returns on error. */ }
/* * Function for a thread that runs an arbitrary userlevel program by * name. * * Note: this cannot pass arguments to the program. You may wish to * change it so it can, because that will make testing much easier * in the future. * * It copies the program name because runprogram destroys the copy * it gets by passing it to vfs_open(). */ static void cmd_progthread(void *ptr, unsigned long nargs) { char **args = ptr; char progname[128]; char progname2[128]; int result; KASSERT(nargs >= 1); if (nargs > 2) { kprintf("Warning: argument passing from menu not supported\n"); } /* Hope we fit. */ KASSERT(strlen(args[0]) < sizeof(progname)); strcpy(progname, args[0]); strcpy(progname2,args[0]); /* demke: make extra copy for runprogram */ free_args(nargs, args); result = runprogram(progname2); if (result) { kprintf("Running program %s failed: %s\n", progname, strerror(result)); return; } /* NOTREACHED: runprogram only returns on error. */ }
/* * Common code for cmd_prog and cmd_shell. * * Note that this does not wait for the subprogram to finish, but * returns immediately to the menu. This is usually not what you want, * so you should have it call your system-calls-assignment waitpid * code after forking. * * Also note that because the subprogram's thread uses the "args" * array and strings, until you do this a race condition exists * between that code and the menu input code. */ static int common_prog(int nargs, char **args) { int err; int exit_code; struct process *proc; err = runprogram(nargs, args, &proc); if (err) { kprintf("runprogram failed: %s\n", strerror(err)); return err; } exit_code = process_waiton(proc); if (WIFEXITED(exit_code)) kprintf("%s exited with code %d\n", args[0], WEXITSTATUS(exit_code)); else if (WIFSIGNALED(exit_code)) kprintf("%s received a fatal signal: %d\n", args[0], WTERMSIG(exit_code)); else if (WIFSTOPPED(exit_code)) kprintf("%s stopped on signal %d\n", args[0], WSTOPSIG(exit_code)); process_destroy(proc->ps_pid); return 0; }
/** * @brief get ssh command result * @note 调用 runprogram函数执行shell命令 * @param [in]password,执行命令主机的密码 * @param [in]cmd,shell命令 * @param [out]result,shell命令执行结果 * @param [in]len,result长度 * @returns 执行成功返回0,其他返回失败 * @exception */ int getSSHCmdResult(const char*const password,const char*const cmd,char*result,int len) { //save password into args const char split = NULL; if(password == NULL) { printf("please input the password for node\n"); return -1; } args.pwtype=PWT_PASS; args.pwsrc.password=strdup(password); //split cmd into tokens by space if(cmd == NULL) { printf("please input the shell command\n"); return -1; } char *tokens[24]={NULL}; int i = 0; if(split == NULL) { tokens[i] = strtok(strdup(cmd)," "); while(tokens[++i]=strtok(NULL," ")); }else { tokens[i] = strtok(strdup(cmd),split); while(tokens[++i]=strtok(NULL,split)); } //call runprogram to execute cmd return runprogram( i, tokens,result,len); }
int sysexecv(char *program, char **args){ int s = splhigh(); int actual; char **mem_args; char *name; if(program==NULL){ splx(s); return EFAULT; } name = (char *)kmalloc(sizeof(char)*128); assert(name!=NULL); copyinstr((userptr_t)program,name,(strlen(program))+1,&actual); int i,nargs; i=0; nargs=0; while(args[i]!=NULL){ nargs++; i++; } DEBUG(1,"Nargs: %d\n",nargs); mem_args = (char **)kmalloc(sizeof(char *)*nargs); assert(mem_args!=NULL); for(i=0;i<nargs;i++){ mem_args[i] = (char *)kmalloc(sizeof(char)*128); assert(mem_args[i]!=NULL); } for(i=0;i<nargs;i++){ //strcpy(mem_args[i],args[i]); copyinstr((userptr_t)args[i],mem_args[i],strlen(args[i])+1,&actual); DEBUG(1,"prg args:%s\n",mem_args[i]); } as_destroy(curthread->t_vmspace); curthread->t_vmspace=NULL; DEBUG(1,"Program: %s\n",program); splx(s); return runprogram(name,mem_args,nargs); }
int sys_execv(userptr_t program, userptr_t args){ /* copy progname into kernel space */ char progname[256]; strcpy(progname, (char *)program); /* Destroy Current address space */ as_destroy(curthread->t_vmspace); curthread->t_vmspace = NULL; /* Do something with args for the time being*/ (void)args; /* Load the new program */ return runprogram(progname, (char **)args); }
/* * Function for a thread that runs an arbitrary userlevel program by * name. * * Note: this cannot pass arguments to the program. You may wish to * change it so it can, because that will make testing much easier * in the future. * * It copies the program name because runprogram destroys the copy * it gets by passing it to vfs_open(). */ static void cmd_progthread(void *ptr, unsigned long nargs) { char **args = ptr; char progname[128]; struct array *newft; pid_t newpid; int result; assert(nargs >= 1); if (nargs > 2) { kprintf("Warning: argument passing from menu not supported\n"); } /* Hope we fit. */ assert(strlen(args[0]) < sizeof(progname)); strcpy(progname, args[0]); /* cheesy hack to get exit working */ newpid = newprocess(1); if (newpid < 0) panic("cmd_progthread: allocating new process %s", strerror(newpid)); curthread->t_pid = newpid; // no pagetable corruption here /* set up filetable */ newft = copyfiletable(1); if (newft==NULL) panic("cmd_progthread: initializing filetable for child\n"); setfiletable(newpid, newft); result = runprogram(progname); if (result) { kprintf("Running program %s failed: %s\n", args[0], strerror(result)); return; } /* NOTREACHED: runprogram only returns on error. */ }
int main( int argc, char *argv[] ) { int opt_offset=parse_options( argc, argv ); if( opt_offset<0 ) { // There was some error show_help(); return -(opt_offset+1); // -1 becomes 0, -2 becomes 1 etc. } if( argc-opt_offset<1 ) { show_help(); return 0; } return runprogram( argc-opt_offset, argv+opt_offset ); }
/* * Function for a thread that runs an arbitrary userlevel program by * name. * * Note: this cannot pass arguments to the program. You may wish to * change it so it can, because that will make testing much easier * in the future. * * It copies the program name because runprogram destroys the copy * it gets by passing it to vfs_open(). */ static void cmd_progthread(void *ptr, unsigned long nargs) { char **args = ptr; char progname[128]; char progname2[128]; int result; /* BEGIN A3 SETUP */ /* Record pid of progthread, so only this thread will do a V() * on the semaphore when it exits. */ progthread_pid = curthread->t_pid; /* END A3 SETUP */ KASSERT(nargs >= 1); if (nargs > 2) { kprintf("Warning: argument passing from menu not supported\n"); } /* Hope we fit. */ KASSERT(strlen(args[0]) < sizeof(progname)); strcpy(progname, args[0]); strcpy(progname2,args[0]); /* demke: make extra copy for runprogram */ free_args(nargs, args); result = runprogram(progname2); if (result) { kprintf("Running program %s failed: %s\n", progname, strerror(result)); return; } /* NOTREACHED: runprogram only returns on error. */ }
/* * Function for a thread that runs an arbitrary userlevel program by * name. * * Note: this cannot pass arguments to the program. You may wish to * change it so it can, because that will make testing much easier * in the future. * * It copies the program name because runprogram destroys the copy * it gets by passing it to vfs_open(). */ static void cmd_progthread(void *ptr, unsigned long nargs) { char **args = ptr; char progname[128]; int result; KASSERT(nargs >= 1); /* Hope we fit. */ KASSERT(strlen(args[0]) < sizeof(progname)); strcpy(progname, args[0]); result = runprogram(progname,args,nargs); if (result) { kprintf("Running program %s failed: %s\n", args[0], strerror(result)); return; } /* NOTREACHED: runprogram only returns on error. */ }
/* * Function for a thread that runs an arbitrary userlevel program by * name. * * Note: this cannot pass arguments to the program. You may wish to * change it so it can, because that will make testing much easier * in the future. * * It copies the program name because runprogram destroys the copy * it gets by passing it to vfs_open(). */ static void cmd_progthread(void *ptr, unsigned long nargs) { char **args = ptr,*ar[25]; char *progname; int result; KASSERT(nargs >= 1); /*if (nargs > 2) { kprintf("Warning: argument passing from menu not supported\n"); }*/ if(nargs==1) args[1]=NULL; else args[nargs]=NULL; /* Hope we fit. */ //KASSERT(strlen(args[0]) < sizeof(progname)); progname=kstrdup(args[0]); //strcpy(progname, args[0]); for(int i=0;i<(int)nargs;i++) ar[i]=kstrdup(args[i]); result = runprogram(progname,nargs,ar); if (result) { kprintf("Running program %s failed: %s\n", args[0], strerror(result)); sys___exit(1); return; } sys___exit(0); /* NOTREACHED: runprogram only returns on error. */ }