コード例 #1
0
int main(){
int ret;
ret=fork();

char cmd[100];

signal(SIGTTOU, SIG_IGN);

if(ret==0){
	//igonre SIGTTOU signal. it is generated when a background process calls tcsetpgrp()
	signal(SIGTTOU, SIG_IGN);
	printf("fg group is %d\n", tcgetpgrp(0));

	//create a new proces sgroup.
	setpgid(getpid(),getpid());

	//set it as fg group in the terminal
	tcsetpgrp(0,getpgid());

	printf("fg group is %d\n", tcgetpgrp(0));	
	fflush(stdout);	
	execlp("wc","wc",NULL);
}

wait(NULL);
tcsetpgrp(0,getpid());
printf("fg group is %d\n", tcgetpgrp(0));	

return 0;
}
コード例 #2
0
void init_tty (void){
	
	int term = STDIN_FILENO;
	shooSHPGID = getpgrp(); // Pega o meu group ID
		
	if( isatty(term) ){
		/*Enquanto nao tiver em foreground*/
		while( tcgetpgrp(term) != shooSHPGID ){ //Enquanto eu nao estiver em foreground
			kill(-tcgetpgrp(term), SIGTTIN ); //Tenta depor a shell
			shooSHPGID = getpgrp();
		}
		
		/*Ignorando sinais*/ 
		signal (SIGINT, SIG_IGN);
		signal (SIGQUIT, SIG_IGN);
		
		sa_chld.sa_sigaction = &hanchld;
		sa_chld.sa_flags = SA_SIGINFO;
		sigaddset (&(sa_chld.sa_mask), SIGINT);
		sigaddset (&(sa_chld.sa_mask), SIGQUIT);
		sigaddset (&(sa_chld.sa_mask), SIGTTIN);
		sigaddset (&(sa_chld.sa_mask), SIGTTOU);
		sigaddset (&(sa_chld.sa_mask), SIGCHLD);
		sigaddset (&(sa_chld.sa_mask), SIGCONT);
		sigaddset (&(sa_chld.sa_mask), SIGTSTP);

		sa_ttin.sa_sigaction = &hanttin;
		sa_ttin.sa_flags = SA_SIGINFO;

		sa_ttou.sa_handler = &hanttou;
		sa_ttou.sa_flags = 0;


		/*Tratamento de sinais*/
		sigaction (SIGCHLD, &sa_chld, NULL);
		sigaction (SIGTTIN, &sa_ttin, NULL);
		sigaction (SIGTTOU, &sa_ttou, NULL);
		signal (SIGTSTP, SIG_IGN);
		
		
		shooSHPGID = getpid(); // Pega o meu pid
		if( setpgid(shooSHPGID, shooSHPGID) < 0){ // Seta o meu group ID como o meu PID
			fprintf(stderr, "Erro: nao foi possivel setar o group ID\n");
			exit(1);
		}
		
		tcsetpgrp( term, shooSHPGID ); //Seta o meu group ID como o dono do terminal
		tcgetattr( term, &shooSHTermios ); //Pega meus atributos
	}
}
コード例 #3
0
ファイル: newsession.c プロジェクト: zxbuaa/apue2
int main(int argc, char *argv[])
{
	pid_t pid;
	int fd;

	if ((pid = fork()) == -1) {
		perror("fork");
		return -1;
	} else if (pid > 0) {
		int status;
		if (waitpid(pid, &status, 0) == -1) {
			perror("waitpid");
			return -1;
		}
		if (WIFEXITED(status)) {
			if (WEXITSTATUS(status) != 0) {
				fprintf("child exit %d\n", WEXITSTATUS(status));
				return -1;
			}
		} else if (WIFSIGNALED(status)) {
			fprintf("child termiated by signal %d\n", WTERMSIG(status));
			return -1;
		}
		printf("parent: pid=%d, ppid=%d, pgrp=%d, sid=%d, tpgrp=%d\n",
			getpid(), getppid(), getpgrp(), getsid(0), tcgetpgrp(STDIN_FILENO));
		return 0;
	}

	printf("child: pid=%d, ppid=%d, pgrp=%d, sid=%d, tpgrp=%d\n",
		getpid(), getppid(), getpgrp(), getsid(0), tcgetpgrp(STDIN_FILENO));
	if ((fd = open("/dev/tty", O_RDONLY)) == -1) {
		perror("open controlling terminal failed before creating session");
	} else {
		printf("open controlling terminal succeed before creating session\n");
		close(fd);
	}
	setsid();
	printf("child: pid=%d, ppid=%d, pgrp=%d, sid=%d, tpgrp=%d\n",
		getpid(), getppid(), getpgrp(), getsid(0), tcgetpgrp(STDIN_FILENO));
	if ((fd = open("/dev/tty", O_RDONLY)) == -1) {
		perror("open controlling terminal failed after creating session");
	} else {
		printf("open controlling terminal succeed after creating session\n");
		close(fd);
	}

	return 0;
}
コード例 #4
0
ファイル: shell.c プロジェクト: Tshimangadzo/hw1
void init_shell()
{   
  /* Check if we are running interactively */
  shell_terminal = STDIN_FILENO;

  /** Note that we cannot take control of the terminal if the shell
      is not interactive */
  shell_is_interactive = isatty(shell_terminal);

  if(shell_is_interactive){

    /* force into foreground */
    while(tcgetpgrp (shell_terminal) != (shell_pgid = getpgrp()))
      kill( - shell_pgid, SIGTTIN);

    shell_pgid = getpid();
    /* Put shell in its own process group */
    if(setpgid(shell_pgid, shell_pgid) < 0){
      perror("Couldn't put the shell in its own process group");
      exit(1);
    }

    /* Take control of the terminal */
    tcsetpgrp(shell_terminal, shell_pgid);
    tcgetattr(shell_terminal, &shell_tmodes);
  }
  /** YOUR CODE HERE */

}
コード例 #5
0
ファイル: dsh-tmp.c プロジェクト: jwang93/shell
void init_shell() {

  	/* See if we are running interactively.  */
	shell_terminal = STDIN_FILENO;
	/* isatty test whether a file descriptor referes to a terminal */
	shell_is_interactive = isatty(shell_terminal);

	if(shell_is_interactive) {
    		/* Loop until we are in the foreground.  */
    		while(tcgetpgrp(shell_terminal) != (shell_pgid = getpgrp()))
      			kill(- shell_pgid, SIGTTIN);

    		/* Ignore interactive and job-control signals.  */
                /* If tcsetpgrp() is called by a member of a background process 
                 * group in its session, and the calling process is not blocking or
                 * ignoring  SIGTTOU,  a SIGTTOU signal is sent to all members of 
                 * this background process group.
                 */

		signal(SIGTTOU, SIG_IGN);

		/* Put ourselves in our own process group.  */
		shell_pgid = getpid();
		if(setpgid(shell_pgid, shell_pgid) < 0) {
			perror("Couldn't put the shell in its own process group");
			exit(1);
		}

		/* Grab control of the terminal.  */
		tcsetpgrp(shell_terminal, shell_pgid);

		/* Save default terminal attributes for shell.  */
		tcgetattr(shell_terminal, &shell_tmodes);
	}
}
コード例 #6
0
ファイル: cash.c プロジェクト: tangfu/Cash
void init_shell(void){
  cash_terminal = STDIN_FILENO;
  cash_interactive = isatty(cash_terminal);
  
  if(cash_interactive){
    while(tcgetpgrp (cash_terminal) != (cash_pgid = getpgrp ()))
      kill (- cash_pgid, SIGTTIN);

      signal (SIGINT, SIG_IGN);
      signal (SIGQUIT, SIG_IGN);
      signal (SIGTSTP, SIG_IGN);
      signal (SIGTTIN, SIG_IGN);
      signal (SIGTTOU, SIG_IGN);
      
      cash_pid = getpid();
      cash_pgid = getpid();
      if(setpgid(cash_pgid, cash_pgid) < 0){
	if(logging)
	  write_to_log(SHELL_PANIC, "could not spawn interactive shell");
	exit_clean(1, "could not spawn interactive shell\n");
      }
      else
	tcsetpgrp(cash_terminal, cash_pgid);
      tcgetattr(cash_terminal, &cash_tmodes);
  }
}
コード例 #7
0
char *
osdep_get_name(int fd, unused char *tty)
{
	FILE	*f;
	char	*path, *buf;
	size_t	 len;
	int	 ch;
	pid_t	 pgrp;

	if ((pgrp = tcgetpgrp(fd)) == -1)
		return (NULL);

	xasprintf(&path, "/proc/%lld/cmdline", (long long) pgrp);
	if ((f = fopen(path, "r")) == NULL) {
		xfree(path);
		return (NULL);
	}
	xfree(path);

	len = 0;
	buf = NULL;
	while ((ch = fgetc(f)) != EOF) {
		if (ch == '\0')
			break;
		buf = xrealloc(buf, 1, len + 2);
		buf[len++] = ch;
	}
	if (buf != NULL)
		buf[len] = '\0';

	fclose(f);
	return (buf);
}
コード例 #8
0
/* If dsh is running interactively as the foreground job
 * then set the process group and signal handlers
 * */
void init_dsh(){
	// in batch mode, STDIN_FILENO is not associated to terminal input.
	dsh_terminal_fd = STDIN_FILENO;
	dsh_is_interactive = isatty(dsh_terminal_fd);

	if(dsh_is_interactive) {
		/* Loop until we are in the foreground.  */
		DEBUG("dsh run as foreground");
		while(tcgetpgrp(dsh_terminal_fd) != (dsh_pgid = getpgrp())){
            // SIGTTIN: (SIGnal due to TeleType INput)
			kill(-dsh_pgid, SIGTTIN); /* Request for terminal input */
		}

		/* Ignore interactive and job-control signals.
		* If tcsetpgrp() is called by a member of a background process
		* group in its session, and the calling process is not blocking or
		* ignoring SIGTTOU,  a SIGTTOU signal is sent to all members of
		* this background process group.
		*/
        // register signal handler. ignore all the
        // SIGTTOU (SIGnal due to TeleType OUtput) is the corresponding signal when a backgrounded (nohuped, etc) job tries to output to the terminal.
		signal(SIGTTOU, SIG_IGN);
		dsh_pgid = getpid();
		if(setpgid(dsh_pgid, dsh_pgid) < 0) {
			perror("Couldn't put the dsh in its own process group");
			exit(EXIT_FAILURE);
		}
		seize_tty(dsh_pgid);
		printf("\n--- Hi, you are using dsh shell, have fun! ---\n\n");
	}
	else{
		DEBUG("dsh run as background");
	}
}
コード例 #9
0
ファイル: tty.c プロジェクト: ProjectZeroSlackr/CmdLine-Tools
void tty_init(void)
{
#ifndef __DJGPP__
	int fd;
	struct termios ti;

	if (tty_fd >= 0) return;

	if ((fd = open("/dev/tty", O_RDONLY | O_NONBLOCK)) < 0) return;

#ifndef __CYGWIN32__
	if (tcgetpgrp(fd) != getpid()) {
		close(fd); return;
	}
#endif

	tcgetattr(fd, &ti);
	saved_ti = ti;
	ti.c_lflag &= ~(ICANON | ECHO);
	ti.c_cc[VMIN] = 1;
	ti.c_cc[VTIME] = 0;
	tcsetattr(fd, TCSANOW, &ti);

	tty_fd = fd;

	atexit(tty_done);
#endif
}
コード例 #10
0
ファイル: helper.c プロジェクト: jeffhou/CS310-Shell
void init_dsh() 
{

	dsh_terminal_fd = STDIN_FILENO;

	/* isatty(): test whether a file descriptor refers to a terminal 
	 * isatty() returns 1 if fd is an open file descriptor referring to a
 	 * terminal; otherwise 0 is returned, and errno is set to indicate the error.
 	 * */
	dsh_is_interactive = isatty(dsh_terminal_fd);

  	/* See if we are running interactively.  */
	if(dsh_is_interactive) {
		/* Loop until we are in the foreground.  */
		while(tcgetpgrp(dsh_terminal_fd) != (dsh_pgid = getpgrp()))
			kill(- dsh_pgid, SIGTTIN); /* Request for terminal input */

		/* Ignore interactive and job-control signals.
		* If tcsetpgrp() is called by a member of a background process 
		* group in its session, and the calling process is not blocking or
		* ignoring SIGTTOU,  a SIGTTOU signal is sent to all members of 
		* this background process group.
		*/
		signal(SIGTTOU, SIG_IGN);

		/* Put the dsh in our own process group.  */
		dsh_pgid = getpid();
		if(setpgid(dsh_pgid, dsh_pgid) < 0) {
			perror("Couldn't put the dsh in its own process group");
			exit(EXIT_FAILURE);
		}
		seize_tty(dsh_pgid);
	} 
}
コード例 #11
0
/**
 * initializes variables and enables job control
 * NOTE: function substantially stolen by the very useful glibc manual:
 * http://www.gnu.org/software/libc/manual/html_node/Implementing-a-Shell.html
 */
void init()
{
        RSH_PID = getpid();                                                             // retrieve the pid of the shell
        RSH_TERMINAL = STDIN_FILENO;                                       // terminal = STDIN
        RSH_IS_INTERACTIVE = isatty(RSH_TERMINAL);            // the shell is interactive if STDIN is the terminal

        if (RSH_IS_INTERACTIVE) {                                                 // is the shell interactive?
                while (tcgetpgrp(RSH_TERMINAL) != (RSH_PGID = getpgrp()))
                        kill(RSH_PID, SIGTTIN);                                                    // make sure we are in the foreground

                /**
                 * ignore all the job control stop signals and install custom signal handlers
                 */
                signal(SIGQUIT, SIG_IGN);
                signal(SIGTTOU, SIG_IGN);
                signal(SIGTTIN, SIG_IGN);
                signal(SIGTSTP, SIG_IGN);
                signal(SIGINT, SIG_IGN);
                signal(SIGCHLD, &signalHandler_child);

                setpgid(RSH_PID, RSH_PID);                                         // we make the shell process as new process group leader
                RSH_PGID = getpgrp();
                if (RSH_PID != RSH_PGID) {
                        printf("Error, the shell is not process group leader");
                        exit(EXIT_FAILURE);
                }
                if (tcsetpgrp(RSH_TERMINAL, RSH_PGID) == -1)      // if Rsh cannot grab control of the terminal
                        tcgetattr(RSH_TERMINAL, &RSH_TMODES);             // we save default terminal attributes for shell.

                currentDirectory = (char*) calloc(1024, sizeof(char));
        } else {
                printf("Could not make R-shell interactive. Exiting..\n");
                exit(EXIT_FAILURE);
        }
}
コード例 #12
0
ファイル: osdep-linux.c プロジェクト: abergmann/tmate-slave
char *
osdep_get_cwd(int fd)
{
	static char	 target[MAXPATHLEN + 1];
	char		*path;
	pid_t		 pgrp, sid;
	ssize_t		 n;

	if ((pgrp = tcgetpgrp(fd)) == -1)
		return (NULL);

	xasprintf(&path, "/proc/%lld/cwd", (long long) pgrp);
	n = readlink(path, target, MAXPATHLEN);
	free(path);

	if (n == -1 && ioctl(fd, TIOCGSID, &sid) != -1) {
		xasprintf(&path, "/proc/%lld/cwd", (long long) sid);
		n = readlink(path, target, MAXPATHLEN);
		free(path);
	}

	if (n > 0) {
		target[n] = '\0';
		return (target);
	}
	return (NULL);
}
コード例 #13
0
ファイル: port.c プロジェクト: mundis/slap
/* test if an open file refers to the process' controlling terminal.
 */
BOOL isctty(INT fd)
        {
        if (tcgetpgrp(fd) != -1)
                return (YES);
        else
                return (NO);
        }
コード例 #14
0
ファイル: mvi.c プロジェクト: waywardmonkeys/bellcore-mgr
static void
check_fg( char *ptyname) {
    int pg, fgpg, fgpg2, fd;

    fd = open( "/dev/tty", 0);
    if( fd < 0)  complain( "cant open /dev/tty");
    fgpg = tcgetpgrp( fd);
    if( fgpg < 0)  complain( "cant tcgetpg");
    pg = getpgrp();
    if(pg != fgpg)  complain( "pg not fg");
    close( fd);
    fd = open( ptyname, 0);
    fgpg2 = tcgetpgrp( fd);
    if( fgpg2 != fgpg)  complain( "ctty not pty");
    close( fd);
}
コード例 #15
0
/* Make sure the shell is running interactively as the foreground job
   before proceeding. */
void init_shell () {
	/* See if we are running interactively.  */
	shell_terminal = STDIN_FILENO;
	shell_is_interactive = isatty (shell_terminal);

	if (shell_is_interactive) {
		/* Loop until we are in the foreground.  */
		while (tcgetpgrp (shell_terminal) != (shell_pgid = getpgrp ()))
			kill (- shell_pgid, SIGTTIN);

		/* Ignore interactive and job-control signals.  */
		signal (SIGINT, SIG_IGN);
		signal (SIGQUIT, SIG_IGN);
		signal (SIGTSTP, SIG_IGN);
		signal (SIGTTIN, SIG_IGN);
		signal (SIGTTOU, SIG_IGN);
		signal (SIGCHLD, SIG_IGN);

		/* Put ourselves in our own process group.  */
		shell_pgid = getpid ();
		if (setpgid (shell_pgid, shell_pgid) < 0) {
			perror ("Couldn't put the shell in its own process group");
			exit (1);
		}

		/* Grab control of the terminal.  */
		tcsetpgrp (shell_terminal, shell_pgid);

		/* Save default terminal attributes for shell.  */
		tcgetattr (shell_terminal, &shell_tmodes);
	}
}
コード例 #16
0
void init()
{
	//history[11] = {0};

        MSH_PID = getpid();
        MSH_TERMINAL = STDIN_FILENO;
        MSH_IS_INTERACTIVE = isatty(MSH_TERMINAL);

        if (MSH_IS_INTERACTIVE) {
                while (tcgetpgrp(MSH_TERMINAL) != (MSH_PGID = getpgrp()))
                        kill(MSH_PID, SIGTTIN);

                signal(SIGQUIT, SIG_IGN);
                signal(SIGTTOU, SIG_IGN);
                signal(SIGTTIN, SIG_IGN);
                signal(SIGTSTP, SIG_IGN);
                signal(SIGINT, SIG_IGN);
                signal(SIGCHLD, &signalHandler_child);

                setpgid(MSH_PID, MSH_PID);
                MSH_PGID = getpgrp();
                if (MSH_PID != MSH_PGID) {
                        printf("Error, the shell is not process group leader");
                        exit(EXIT_FAILURE);
                }
                if (tcsetpgrp(MSH_TERMINAL, MSH_PGID) == -1)
                        tcgetattr(MSH_TERMINAL, &MSH_TMODES);

                currentDirectory = (char*) calloc(1024, sizeof(char));
        } else {
                printf("Could not make MyShell interactive. Exiting...\n");
                exit(EXIT_FAILURE);
        }
}
コード例 #17
0
ファイル: osdep-netbsd.c プロジェクト: SeamusConnor/tmux
char *
osdep_get_name(int fd, __unused char *tty)
{
	int		 mib[6];
	struct stat	 sb;
	size_t		 len, i;
	struct kinfo_proc2 *buf, *newbuf, *bestp;
	char		*name;

	if (stat(tty, &sb) == -1)
		return (NULL);
	if ((mib[3] = tcgetpgrp(fd)) == -1)
		return (NULL);

	buf = NULL;
	len = sizeof bestp;

	mib[0] = CTL_KERN;
	mib[1] = KERN_PROC2;
	mib[2] = KERN_PROC_PGRP;
	mib[4] = sizeof *buf;

retry:
	mib[5] = 0;

	if (sysctl(mib, __arraycount(mib), NULL, &len, NULL, 0) == -1)
		return (NULL);

	if ((newbuf = realloc(buf, len)) == NULL)
		goto error;
	buf = newbuf;

	mib[5] = len / (sizeof *buf);
	if (sysctl(mib, __arraycount(mib), buf, &len, NULL, 0) == -1) {
		if (errno == ENOMEM)
			goto retry;
		goto error;
	}

	bestp = NULL;
	for (i = 0; i < len / (sizeof *buf); i++) {
		if (buf[i].p_tdev != sb.st_rdev)
			continue;
		if (bestp == NULL)
			bestp = &buf[i];
		else
			bestp = cmp_procs(&buf[i], bestp);
	}

	name = NULL;
	if (bestp != NULL)
		name = strdup(bestp->p_comm);

	free(buf);
	return (name);

error:
	free(buf);
	return (NULL);
}
コード例 #18
0
ファイル: shell.c プロジェクト: posei/hw1
void init_shell() {
    /* Check if we are running interactively */
    shell_terminal = STDIN_FILENO;
    s = malloc(INPUT_STRING_SIZE+1); /* user input string */

    /** Note that we cannot take control of the terminal if the shell
    is not interactive */
    shell_is_interactive = isatty(shell_terminal);

    if (shell_is_interactive) {
        /* force into foreground */
        while (tcgetpgrp (shell_terminal) != (shell_pgid = getpgrp()))
            kill (- shell_pgid, SIGTTIN);

            /* Ignore interactive and job-control signals.  */
            signal (SIGINT, SIG_IGN);
            signal (SIGQUIT, SIG_IGN);
            signal (SIGTSTP, SIG_IGN);
            signal (SIGTTIN, SIG_IGN);
            signal (SIGTTOU, SIG_IGN);
            signal (SIGCHLD, SIG_IGN);

        shell_pgid = getpid();
        /* Put shell in its own process group */
        if(setpgid(shell_pgid, shell_pgid) < 0) {
          perror("Couldn't put the shell in its own process group... Eish.");
          exit(1);
        }

        /* Take control of the terminal */
        tcsetpgrp(shell_terminal, shell_pgid);
        tcgetattr(shell_terminal, &shell_tmodes);
  }
}
コード例 #19
0
ファイル: jobctl00.c プロジェクト: OSLL/pmover
static int is_fg(void)
{
	pid_t pgid = getpgrp();
	pid_t tcpgid = tcgetpgrp(1);

	return (pgid != -1) && (pgid == tcpgid);
}
コード例 #20
0
ファイル: error.c プロジェクト: ele7enxxh/dtrace-pf
void
onint(void)
{
	sigset_t sigs;

	/*
	 * The !in_dotrap here is safe.  The only way we can arrive here
	 * with in_dotrap set is that a trap handler set SIGINT to SIG_DFL
	 * and killed itself.
	 */

	if (suppressint && !in_dotrap) {
		intpending++;
		return;
	}
	intpending = 0;
	sigemptyset(&sigs);
	sigprocmask(SIG_SETMASK, &sigs, NULL);

	/*
	 * This doesn't seem to be needed, since main() emits a newline.
	 */
#if 0
	if (tcgetpgrp(0) == getpid())
		write(STDERR_FILENO, "\n", 1);
#endif
	if (rootshell && iflag)
		exraise(EXINT);
	else {
		signal(SIGINT, SIG_DFL);
		kill(getpid(), SIGINT);
	}
}
コード例 #21
0
void
init_shell( void ) {

  // Check if we are running interactively
  shell_terminal = STDIN_FILENO;
  shell_is_interactive = isatty( shell_terminal );

  if( shell_is_interactive ){

    // force into foreground
    while( tcgetpgrp( shell_terminal ) != ( shell_pgid = getpgrp() ) )
      kill( - shell_pgid, SIGTTIN);

    // put shell in its own process group
    shell_pgid = getpid();
    if( setpgid( shell_pgid, shell_pgid ) < 0 ){
      perror( "Couldn't put the shell in its own process group" );
      exit( EXIT_FAILURE );
    }

    // take control of the terminal
    tcsetpgrp(shell_terminal, shell_pgid);
    tcgetattr(shell_terminal, &shell_tmodes);
  }

  // ignore signals
  SET_SIGNALS( SIG_IGN );
  
}
コード例 #22
0
static int
start_inferior (char *argv[], char *statusptr)
{
#ifdef SIGTTOU
  signal (SIGTTOU, SIG_DFL);
  signal (SIGTTIN, SIG_DFL);
#endif

  signal_pid = create_inferior (argv[0], argv);

  fprintf (stderr, "Process %s created; pid = %ld\n", argv[0],
	   signal_pid);
  fflush (stderr);

#ifdef SIGTTOU
  signal (SIGTTOU, SIG_IGN);
  signal (SIGTTIN, SIG_IGN);
  terminal_fd = fileno (stderr);
  old_foreground_pgrp = tcgetpgrp (terminal_fd);
  tcsetpgrp (terminal_fd, signal_pid);
  atexit (restore_old_foreground_pgrp);
#endif

  /* Wait till we are at 1st instruction in program, return signal number.  */
  return mywait (statusptr, 0);
}
コード例 #23
0
ファイル: bshell.c プロジェクト: mbionchi/bshell
/*www.gnu.org/software/libc/manual/html_node/Initializing-the-Shell.html*/
void init_shell() {
    /*keep track of shell attributes*/
    pid_t shell_pgid;
    int shell_is_interactive;

    /*make sure shell is foreground process*/
    shell_terminal = STDIN_FILENO;
    while (tcgetpgrp(shell_terminal) != (shell_pgid = getpgrp())) {
        kill (- shell_pgid, SIGTTIN);
    }
    /*ignore interactive and job-control signals so the
    shell doesn't kill its own process*/
    signal(SIGINT, SIG_IGN);
    signal(SIGQUIT, SIG_IGN);
    signal(SIGTSTP, SIG_IGN);
    signal(SIGTTIN, SIG_IGN);
    signal(SIGTTOU, SIG_IGN);
    signal(SIGCHLD, SIG_IGN);

    /*put ourselves in our own process group*/
    shell_pgid = getpid();
    if (setpgid(shell_pgid, shell_pgid) < 0) {
        perror("Couldn't put the shell in its own process group.");
        exit(1);
    }

    /*grab control of the terminal*/
    tcsetpgrp(shell_terminal, shell_pgid);
    /*save default terminal attributes for shell*/
    tcgetattr(shell_terminal, &shell_tmodes);
}
コード例 #24
0
ファイル: figure9-1.c プロジェクト: CodeJuan/apue.2e
static void
pr_ids(char *name)
{
  printf("%s: pid = %d, ppid = %d, pgrp = %d, tpgrp = %d\n",
         name, getpid(), getppid(), getpgrp(), tcgetpgrp(STDIN_FILENO));
  fflush(stdout);
}
コード例 #25
0
/* ____ SOSTIUIRE QUESTO PROGRAMMA __ */
int main(int argc, char* argv[])
{
    pid_t pid;
    int fd;
    get_info("CALLING PROCESS", 0);

    fd = open("/dev/tty", O_RDWR);
    printf("PGID leader: %ld\n", (long)tcgetpgrp(fd));
    printf("PGID foreground: %ld\n", (long)tcgetsid(0));

    switch (pid = fork()) {

    case -1:
        fprintf(stderr, "Err.(%s) fork() failed\n", strerror(errno));
        exit(EXIT_FAILURE);

    case 0:
        /* Il processo figlio crea una nuova sessione, pertanto PGID e
        SID sono settati al PID del figlio medesimo */
        setsid();
        get_info("CHILD PROCESS", pid);

        exit(EXIT_SUCCESS);

    default:
        waitpid(pid, NULL, 0);
    }

    return (EXIT_SUCCESS);
}
コード例 #26
0
void bootstrap(){

processid = getpid();
shellbash = isatty(STDIN_FILENO);  

if (shellbash) {			
	while (tcgetpgrp(STDIN_FILENO) != (parentPID = getpgrp()))
		kill(processid, SIGTTIN);
		act_child.sa_handler = signalHandler_child;
		act_int.sa_handler = signalHandler_int;
		sigaction(SIGCHLD, &act_child, 0);
		sigaction(SIGINT, &act_int, 0);
		setpgid(processid, processid); 
		parentPID = getpgrp();

if (processid != parentPID) {
exit(EXIT_FAILURE);
}
tcsetpgrp(STDIN_FILENO, parentPID);
tcgetattr(STDIN_FILENO, &bashmodes);
currentDirectory = (char*) calloc(1024, sizeof(char));
} else {
exit(EXIT_FAILURE);
}
}
コード例 #27
0
ファイル: osdep-freebsd.c プロジェクト: Darkoe/tmux
static char *
osdep_get_cwd_fallback(int fd)
{
	static char		 wd[PATH_MAX];
	struct kinfo_file	*info = NULL;
	pid_t			 pgrp;
	int			 nrecords, i;

	if ((pgrp = tcgetpgrp(fd)) == -1)
		return (NULL);

	if ((info = kinfo_getfile(pgrp, &nrecords)) == NULL)
		return (NULL);

	for (i = 0; i < nrecords; i++) {
		if (info[i].kf_fd == KF_FD_TYPE_CWD) {
			strlcpy(wd, info[i].kf_path, sizeof wd);
			free(info);
			return (wd);
		}
	}

	free(info);
	return (NULL);
}
コード例 #28
0
ファイル: tsh.c プロジェクト: YurieCo/csapp-2
/* 
 * sigchld_handler - The kernel sends a SIGCHLD to the shell whenever
 *     a child job terminates (becomes a zombie), or stops because it
 *     received a SIGSTOP, SIGTSTP, SIGTTIN or SIGTTOU signal. The 
 *     handler reaps all available zombie children, but doesn't wait 
 *     for any other currently running children to terminate.  
 */
void 
sigchld_handler(int sig) 
{
    pid_t pid;
    int status;
    while((pid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0){
        if(pid == fgpid(job_list)){
            if(tcgetpgrp(STDIN_FILENO) != shell_pid) {
                tcsetpgrp(STDIN_FILENO, shell_pid);
            }
        }
        if(WIFEXITED(status)){
            deletejob(job_list, pid);
        } else if(WIFSIGNALED(status)){
            printf("Job [%d] (%d) terminated by signal %d\n",
                   pid2jid(pid), pid, WTERMSIG(status));
            deletejob(job_list, pid);
        } else if (WIFSTOPPED(status)) {
            printf("Job [%d] (%d) stopped by signal %d\n", 
                   pid2jid(pid), pid, WSTOPSIG(status));
            getjobpid(job_list, pid)->state = ST;
        }
    }
    return;
}
コード例 #29
0
ファイル: progress.c プロジェクト: asilkybush/rsync-bpc
void show_progress(OFF_T ofs, OFF_T size)
{
	struct timeval now;
#if defined HAVE_GETPGRP && defined HAVE_TCGETPGRP
	static pid_t pgrp = -1;
	pid_t tc_pgrp;
#endif

	if (am_server)
		return;

#if defined HAVE_GETPGRP && defined HAVE_TCGETPGRP
	if (pgrp == -1)
		pgrp = getpgrp(GETPGRP_ARG);
#endif

	gettimeofday(&now, NULL);

	if (INFO_GTE(PROGRESS, 2)) {
		ofs = stats.total_transferred_size - size + ofs;
		size = stats.total_size;
	}

	if (!ph_start.time.tv_sec) {
		int i;

		/* Try to guess the real starting time when the sender started
		 * to send us data by using the time we last received some data
		 * in the last file (if it was recent enough). */
		if (msdiff(&ph_list[newest_hpos].time, &now) <= 1500) {
			ph_start.time = ph_list[newest_hpos].time;
			ph_start.ofs = 0;
		} else {
			ph_start.time.tv_sec = now.tv_sec;
			ph_start.time.tv_usec = now.tv_usec;
			ph_start.ofs = ofs;
		}

		for (i = 0; i < PROGRESS_HISTORY_SECS; i++)
			ph_list[i] = ph_start;
	}
	else {
		if (msdiff(&ph_list[newest_hpos].time, &now) < 1000)
			return;

		newest_hpos = oldest_hpos;
		oldest_hpos = (oldest_hpos + 1) % PROGRESS_HISTORY_SECS;
		ph_list[newest_hpos].time.tv_sec = now.tv_sec;
		ph_list[newest_hpos].time.tv_usec = now.tv_usec;
		ph_list[newest_hpos].ofs = ofs;
	}

#if defined HAVE_GETPGRP && defined HAVE_TCGETPGRP
	tc_pgrp = tcgetpgrp(STDOUT_FILENO);
	if (tc_pgrp != pgrp && tc_pgrp != -1)
		return;
#endif

	rprint_progress(ofs, size, &now, False);
}
コード例 #30
0
ファイル: server.c プロジェクト: gxliu/bdm-osbdm
static int
start_inferior (char *argv[], char *statusptr)
{
#ifdef SIGTTOU
  signal (SIGTTOU, SIG_DFL);
  signal (SIGTTIN, SIG_DFL);
#endif

  signal_pid = create_inferior (argv[0], argv);

  /* FIXME: we don't actually know at this point that the create
     actually succeeded.  We won't know that until we wait.  */
  printf_filtered ("Process %s created; pid = %ld\n", argv[0],
                   signal_pid);

#ifdef SIGTTOU
  signal (SIGTTOU, SIG_IGN);
  signal (SIGTTIN, SIG_IGN);
  terminal_fd = fileno (stderr);
  old_foreground_pgrp = tcgetpgrp (terminal_fd);
  tcsetpgrp (terminal_fd, signal_pid);
  atexit (restore_old_foreground_pgrp);
#endif

  /* Wait till we are at 1st instruction in program, return signal
     number (assuming success).  */
  return mywait (statusptr, 0);
}