コード例 #1
0
ファイル: pré-exec.c プロジェクト: Colliotv/42.sh
BOOL	exec_pipes(t_pipes *p, t_info *info, FLAG son, FD pi[3])
{
  STATUS	status_quo;
  pid_t		pid;
  FD		w[2];

  if (!(pid = -(!((unsigned long)(p->next != NULL)))))
    {
      if (pipe(w) == -1)
	return (FALSE);
      if ((pid = xfork()) < 0)
	return (FALSE);
    }
  if (!pid)
    {
      init_pipe(w, pi, W_IN);
      exec_pipes(p->next, info, SON, pi);
      return (FALSE);
    }
  if (p->next)
    init_pipe(w, pi, W_OUT);
  if (exec_cmd(p->cmd, info, info->son = (son | (2 * (pid > 0))), pi) == FALSE)
    return (FALSE);
  my_wait(pid, &status_quo);
  if (pid < 0)
    return (TRUE);
  return (get_status(info, status_quo));
}
コード例 #2
0
ファイル: exec.c プロジェクト: JACOBFelix/strace
static void			exec_strace(pid_t pid,
					    int sys,
					    int was_sys,
					    long ret)
{
  struct user_regs_struct	reg;

  while (ptrace(PTRACE_SINGLESTEP, pid, NULL, NULL) != -1)
    {
      my_wait(pid);
      if (ptrace(PTRACE_GETREGS, pid, NULL, ((void *)(&reg))) != -1)
	{
	  if ((ret = ptrace(PTRACE_PEEKTEXT, pid, ((void *)(reg.rip)), NULL))
	      == -1)
	    exit(dprintf(2, "ptrace(PREACE_PEEKTEXT Failed\n"));
	  else
	    {
	      if (was_sys == 1 && ((was_sys -= 1) == 0))
		print_ret(&g_arg[sys], reg.rax, pid);
	      else
		{
		  ret &= 0x000000000000ffff;
		  if ((ret == 0x050FU || ret == 0x80CDU || ret == 0x340FU)
		      && ((++was_sys) == 1))
		    moulitrixdecon(&sys, reg, pid);
		}
	    }
	}
    }
}
コード例 #3
0
int main(int argc, char * argv[]){


  char * line; 
  pid_t c_pid;
  int i;
  char * cmd_argv[MAX_ARGS]; //NOTE: use this store your argv array, 
                             //      don't go beyond MAX_ARGS
  char prompt[1024];
  char * tok;   //NOTE: This is useful for tokenizatoin

  //Need to ignore these signals for terminal control to work
  signal(SIGTTIN, SIG_IGN);
  signal(SIGTTOU, SIG_IGN);

  
  //for readline setup, don't edit!
  rl_bind_key('\t', rl_abort);



  while(1){ //fork/exec/wait loop

    //setup the prompt
    snprintf(prompt, 1024, "fg-shell (%d) #> ",last_pid);
    line = readline(prompt);   //readline allocates a new line every time, need to free
    
    //read EOF, break
    if (line == NULL){
      free(line);
      printf("\n");
      break;
    }

    //read empty line, continue
    if (strcmp(line,"")==0){
      free(line);
      continue;
    }


    ////////////////////////////////////////////////////////////////
    //*DO* EDIT BELOW
    //
    
    //FILL IN COMPLETED SOLUTION FROM LAB 5
    //  - parsing line using strtok into the cmd_argv array

    //TODO: See if user entered "fg" command 
    //      If so, try and start the background process and call my_wait()
    //      If no last background process, report error to STDERR
    //      Don't forget to give the foreground process control of the terminal

    //perform a foreground
    if ( strcmp(cmd_argv[0], "fg") == 0){


      //TODO: FILL IN HERE


      free(line); //free line
      continue; //continue the shell after waiting
    }


    //////////////////////////////////////////////////////////////
    //DO NOT EDIT BELOW
    //

    if ( (c_pid = fork()) == 0 ){
      /* CHILD */

      //set as it's own process group
      setpgrp();
      
      execvp(cmd_argv[0],cmd_argv);
      perror("fg-shell"); //error
      _exit(2); //hard exit, don't want a fork bomb!

    }else if (c_pid > 0){
      /* PARENT */

      //give child its own process group
      if(setpgid(c_pid, c_pid) < 0){
	perror("setpgrp");
      }

      //give child the terminal control
      if( tcsetpgrp(0, c_pid) < 0){
	perror("tcsetpgrp");
      }

      //wait for a state change
      my_wait();
      
    }else{
      /* ERROR */
      perror("fork failed");
      _exit(2);
      
    }
    
    free(line); //free the current line
  } 
  return 0;
}