Ejemplo n.º 1
0
static int
sscr_pty(int *amaster, int *aslave, char *name, struct termios *termp, void *winp)
{
	int master, slave;

	/* open master terminal */
	if ((master = ptym_open(name)) < 0)  {
		errno = ENOENT;	/* out of ptys */
		return (-1);
	}

	/* open slave terminal */
	if ((slave = ptys_open(master, name)) >= 0) {
		*amaster = master;
		*aslave = slave;
	} else {
		errno = ENOENT;	/* out of ptys */
		return (-1);
	}

	if (termp)
		(void) tcsetattr(slave, TCSAFLUSH, termp);
#ifdef TIOCSWINSZ
	if (winp != NULL)
		(void) ioctl(slave, TIOCSWINSZ, (struct winsize *)winp);
#endif
	return (0);
}
Ejemplo n.º 2
0
int opal_openpty(int *amaster, int *aslave, char *name,
                 struct termios *termp, struct winsize *winp)
{
    char line[20];
    *amaster = ptym_open(line);
    if (*amaster < 0) {
        return -1;
    }
    *aslave = ptys_open(*amaster, line);
    if (*aslave < 0) {
        close(*amaster);
        return -1;
    }
    if (name) {
        strcpy(name, line);
    }
#ifndef TCSAFLUSH
#define TCSAFLUSH TCSETAF
#endif
    if (termp) {
        (void) tcsetattr(*aslave, TCSAFLUSH, termp);
    }
#ifdef TIOCSWINSZ
    if (winp) {
        (void) ioctl(*aslave, TIOCSWINSZ, (char *) winp);
    }
#endif
    return 0;
}
Ejemplo n.º 3
0
/*
 * Class:     org_eclipse_cdt_utils_pty_PTY
 * Method:    forkpty
 * Signature: ()I
 */
JNIEXPORT jstring JNICALL
Java_org_eclipse_cdt_utils_pty_PTY_openMaster (JNIEnv *env, jobject jobj, jboolean console) {
	jfieldID fid; /* Store the field ID */
	jstring jstr = NULL;
	int master = -1;
	char line[1024];	/* FIXME: Should be enough */
	jclass cls;

	line[0] = '\0';

	master = ptym_open(line);
	if (master >= 0) {
		if (console) {
			// turn off echo
			set_noecho(master);
		}

		/* Get a reference to the obj's class */
		cls = (*env)->GetObjectClass(env, jobj);

		/* Set the master fd.  */
		fid = (*env)->GetFieldID(env, cls, "master", "I");
		if (fid == NULL) {
			return NULL;
		}
		(*env)->SetIntField(env, jobj, fid, (jint)master);

		/* Create a new String for the slave.  */
		jstr = (*env)->NewStringUTF(env, line);
	}
	return jstr;
}
Ejemplo n.º 4
0
pid_t
pty_fork(int *ptrfdm, char *slave_name,
		 const struct termios *slave_termios,
		 const struct winsize *slave_winsize)
{
	int		fdm, fds;
	pid_t	pid;
	char	pts_name[20];

	if ( (fdm = ptym_open(pts_name)) < 0)
		err_sys("can't open master pty: %s", pts_name);

	if (slave_name != NULL)
		strcpy(slave_name, pts_name);	/* return name of slave */

	if ( (pid = fork()) < 0)
		return(-1);

	else if (pid == 0) {		/* child */
		if (setsid() < 0)
			err_sys("setsid error");

					/* SVR4 acquires controlling terminal on open() */
		if ( (fds = ptys_open(fdm, pts_name)) < 0)
			err_sys("can't open slave pty");
		close(fdm);		/* all done with master in child */

#if	defined(TIOCSCTTY) && !defined(CIBAUD)
				/* 44BSD way to acquire controlling terminal */
				/* !CIBAUD to avoid doing this under SunOS */
		if (ioctl(fds, TIOCSCTTY, (char *) 0) < 0)
			err_sys("TIOCSCTTY error");
#endif
				/* set slave's termios and window size */
		if (slave_termios != NULL) {
			if (tcsetattr(fds, TCSANOW, slave_termios) < 0)
				err_sys("tcsetattr error on slave pty");
		}
		if (slave_winsize != NULL) {
			if (ioctl(fds, TIOCSWINSZ, slave_winsize) < 0)
				err_sys("TIOCSWINSZ error on slave pty");
		}
				/* slave becomes stdin/stdout/stderr of child */
		if (dup2(fds, STDIN_FILENO) != STDIN_FILENO)
			err_sys("dup2 error to stdin");
		if (dup2(fds, STDOUT_FILENO) != STDOUT_FILENO)
			err_sys("dup2 error to stdout");
		if (dup2(fds, STDERR_FILENO) != STDERR_FILENO)
			err_sys("dup2 error to stderr");
		if (fds > STDERR_FILENO)
			close(fds);
		return(0);		/* child returns 0 just like fork() */

	} else {					/* parent */
		*ptrfdm = fdm;	/* return fd of master */
		return(pid);	/* parent returns pid of child */
	}
}
Ejemplo n.º 5
0
int aloca_master()
{
   if ( (glob_fdm = ptym_open(glob_name)) < 0)
   {
	 msg("can't open master pty");
	 return ERRO;
   }
   return OK;
}
Ejemplo n.º 6
0
// ****************************************************************************
//  Function:  pty_fork
//
//  Purpose:
//    Like fork(), but creates a PTY and returns its file descriptor/name.
//
//  Arguments:
//    fdm       : new file descriptor
//    slave_name: new pty path
//
//  Programmer:  Jeremy Meredith
//  Creation:    April 27, 2001
//
//  Modifications:
//    Jeremy Meredith, Tue Oct 22 15:00:37 PDT 2002
//    Added an optional signal child handler.  Since setting this must happen
//    before fork() and after grantpt(), it must be set in this function.
//
// ****************************************************************************
pid_t
pty_fork(int &fdm, void (*sigchld_handler)(int), char *slave_name)
{
    int         fds;
    pid_t       pid;
    char        pts_name[20];

    if ( (fdm = ptym_open(pts_name)) < 0)
        fprintf(stderr,"can't open master pty: %s", pts_name);

    if (slave_name != NULL)
        strcpy(slave_name, pts_name);   // return name of slave

    if (sigchld_handler != NULL)
        signal(SIGCHLD, sigchld_handler);

    if ( (pid = fork()) < 0)
        return(-1);

    else if (pid == 0) {                // child
        if (setsid() < 0)
            fprintf(stderr,"setsid error");

        // SVR4 acquires controlling terminal on open()
        if ( (fds = ptys_open(fdm, pts_name)) < 0)
            fprintf(stderr,"can't open slave pty: %d\n",fds);
        close(fdm);             // all done with master in child

#if     defined(TIOCSCTTY) && !defined(CIBAUD)
        // 44BSD way to acquire controlling terminal
        // !CIBAUD to avoid doing this under SunOS
        if (ioctl(fds, TIOCSCTTY, (char *) 0) < 0)
            fprintf(stderr,"TIOCSCTTY error");
#endif
        // slave becomes stdin/stdout/stderr of child
        if (dup2(fds, STDIN_FILENO) != STDIN_FILENO)
            fprintf(stderr,"dup2 error to stdin");
        if (dup2(fds, STDOUT_FILENO) != STDOUT_FILENO)
            fprintf(stderr,"dup2 error to stdout");
        if (dup2(fds, STDERR_FILENO) != STDERR_FILENO)
            fprintf(stderr,"dup2 error to stderr");
        if (fds > STDERR_FILENO)
            close(fds);
        return(0);              // child returns 0 just like fork()

    } else {            // parent
        return(pid);    // return pid of child
    }
}
Ejemplo n.º 7
0
int
GUCEF_pty_open(int *fdm, int *fds)
{
    char	pts_name[20];

    if ((*fdm = ptym_open(pts_name)) < 0)
	return 1;

    if ((*fds = ptys_open(*fdm, pts_name)) < 0) {
	close(*fdm);
	return 1;
    }

    return 0;
}
Ejemplo n.º 8
0
int open_pty_pair(int *master_fd,int *slave_fd)
{
	char pts_name[20];

	*master_fd = ptym_open(pts_name,20);
	if (*master_fd < 0) {
		perror("error opening master pty");
		return -1;
	}
	*slave_fd = ptys_open(*master_fd,pts_name);
	if (*slave_fd < 0) {
		perror("error opening slave tty");
		return -1;
	}
	return 0;
}
Ejemplo n.º 9
0
int main(int argc, char* argv[])
{
  char master1[1024];
  char slave1[1024];

  struct sigaction sigact;
  timer_t timer;
  struct itimerspec itval;

  fd1=ptym_open(master1,slave1,1024);
  printf("(%s)\n",slave1);

  if ((slave = open(slave1, 0)) < 0)
    perror("open slave");

  conf_ser(fd1);

  sigact.sa_handler = handler;
  sigact.sa_flags = 0;
  sigemptyset(&sigact.sa_mask);

  if (sigaction(SIGALRM, &sigact, NULL) < 0)
	  perror("sigaction");

  itval.it_interval.tv_sec  = 0;
  itval.it_interval.tv_nsec = 7 * 1000 * 1000;
  itval.it_value.tv_sec  = 0;
  itval.it_value.tv_nsec = 7 * 1000 * 1000;

  if (timer_create(CLOCK_REALTIME, NULL, &timer) < 0)
    perror("timer_create");
  if (timer_settime(timer, 0, &itval, NULL) < 0)
    perror("timer_settime");

  while(1)
  {
    // usleep(7 * 1000);
    // copydata(fd1);
    pause();
  }

  close(fd1);
  close(slave);

  return EXIT_SUCCESS;
}
int main(int argc, char *argv[])
{
	char pts_name[PTSNAME_BUFLEN];
	int pts_master;
	int fd1, fd2; /* For "who"'s output */
	int rc = 0;

	if (argc < 3) {
		fprintf (stderr, "Usage: %s file1 file2\n",
				argv[0]);
		exit(1);
	}

	fd1 = open_output_file(argv[1]);
	fd2 = open_output_file(argv[2]);

	/* STEP 1: Create the pty device */
	if ((pts_master = ptym_open(pts_name)) < 0) {
		fprintf(stderr, "ptym_open failed, exiting..\n");
		rc = 1;
		goto out;
	}

	/* STEP 2: Add an entry */
	if (spawn_utempter(pts_master, "add") != 0) {
		rc = 1; goto out;
	}

	/* STEP 3: "who" for our caller to verify if the entry was added */
	(void)spawn_who(fd1);

	/* STEP 4: Delete the entry */
	if (spawn_utempter(pts_master, "del") != 0) {
		rc = 1 ;  goto out;
	}

	/* STEP 5: "who" to verify the deletion */
	(void)spawn_who(fd2);

out:
	close(pts_master);
	close (fd1);
	close (fd2);
	exit(rc);
}
Ejemplo n.º 11
0
int main(int argc, char **argv, char **envp) {
	const char *path = "./bufferring_test";
	int channels[3] = { -1, -1, -1};
	int status;	
	FILE *app_stdin;
	FILE *app_stdout;
	FILE *app_stderr;
	char pts_name[32];
	int fdm;	
	char buffer[32];

	fdm = ptym_open(pts_name);
	status =  exec_pty(path, argv, envp, ".", channels, pts_name, fdm);
	if (status >= 0) {
		//app_stdin = fdopen(channels[0], "w");	
		app_stdout = fdopen(channels[1], "r");	
		app_stderr = fdopen(channels[2], "r");	
		if (app_stdout == NULL || app_stderr == NULL /*|| app_stdin == NULL*/) {
			fprintf(stderr, "PROBLEMS\n");
		} else {
			printf("PID %d\n", status);
			if (isatty(fdm)) {
				printf("Is atty\n");
			}
			write(fdm, "foo\n", 4);
			write(fdm, "bar\n", 4);
			//fputs("foo\n", app_stdin);
			//fputs("bar\n", app_stdin);
			//fflush(app_stdin);
			while(fgets(buffer, sizeof buffer, app_stdout) != NULL) {
				fprintf(stdout, "STDOUT: %s\n", buffer);
			}
			while(fgets(buffer, sizeof buffer, app_stderr) != NULL) {
				fprintf(stdout, "STDERR: %s\n", buffer);
			}
		}
	}
	fputs("bye\n", stdout);
	close(channels[0]);
	close(channels[1]);
	close(channels[2]);
	return 0;	
}
Ejemplo n.º 12
0
int main( int argc, char * argv[ ], char * envp[] )
{
  	struct termios term;
	struct sigaction new;
	int newfd=20,fds,fdm,nfds;
	char slave[20];
	char temp_store[4096];
	int i,count;

	fd_set	readfds;
	chdir("/");
	fdm = ptym_open(slave);
	if(fdm < 0)
	{
		strcat(temp_store, "ERROR : Could not open pseudoterminal\n");
		write(20, temp_store, strlen(temp_store));
		return(1);
	}
	dup2(fdm, 13);
	close(fdm);
	fdm=13;
	fcntl(fdm,F_SETFD,FD_CLOEXEC);
	fds = ptys_open(slave);
	if(tcgetattr(fds, &term) >= 0) //Setting in ECHO mode
	{
		term.c_lflag |= ECHO;
  		tcsetattr(fds,TCSANOW, &term);
	}
		
	dup2(fds, 14);
	close(fds);
	fds=14;

	dup2(fds,0);
	dup2(fds,1);
	dup2(fds,2);
	close(fds);
	pid=spawnl("/usr/bin/logins.exe", "-logins.exe", ( char * )0);
	if (pid < 0)
		exit(1);
	nfds = ((newfd > fdm)?newfd:fdm) + 1;
	sigemptyset(&new.sa_mask);
	signal(SIGCLD, sighan);
	signal(SIGHUP, sighuphandler);
	fcntl(20,F_SETFD, 1);

	while(1)
	{
		int r;
		FD_ZERO(&readfds);
		FD_SET(newfd,&readfds);
		FD_SET(fdm,&readfds);

		if ((r=select(nfds,&readfds,NULL,NULL,NULL))<0)
			break;
		else
		{
			char ch[4096];
			int ret;
			if(!r)
				continue;
			if(FD_ISSET(newfd,&readfds))
			{
				ret = read(newfd,ch,4096);
				if (ret<=0)
					break;

				count = 0;
				for (i=0;i <= ret;i++)
				{
					if (ch[i] != '\r')
					{
						temp_store[count] = ch[i];
						count++;   /* To keep track of no of chars*/
						continue;
					}
					else
					{
						 temp_store[count] = ch[i];/*copy the carriage return */
						 count++;
						 if ((i < ret)&& (ch[i+1] == '\n'))
							 i++; /*skip the new line character */
					}
				} /* End of for loop */
				write(fdm,temp_store,count-1);
			}
			if(FD_ISSET(fdm,&readfds))
			{
				ret = read(fdm,ch,4096);
				if(ret <= 0)
					break;
				write(newfd,ch,ret);
			}
		}
	}
	close(newfd);
	close(fdm);
	kill(-tcgetpgrp(0),SIGHUP);
	sleep(1);
	kill(-pid,SIGHUP);
	return(1);
}
Ejemplo n.º 13
0
pid_t pty_fork(int *ptm_fd, char *pts_name, size_t pts_buff_len,
	       const struct termios *slave_termios, const struct winsize *slave_winsize) {

	if ((*ptm_fd = ptym_open(pts_name, pts_buff_len)) < 0)
		return -1;

	pid_t pid;
	int pipefds[2];
	int child_status;
	int err_code;

	if (pipe(pipefds) < 0) {
		err_code = errno;
		goto errout;
	}

	if ((pid = fork()) < 0) {
		err_code = errno;
		goto pipeout1;
	}

	if (pid == 0) {

		int pts_fd = 0;
		int status = 0;

		close(pipefds[0]);
		close(*ptm_fd);

		if (setsid() < 0)
			goto childout;

		if ((pts_fd = ptys_open(pts_name)) < 0)
			goto childout;

		if (slave_termios != NULL)
			if (tcsetattr(pts_fd, 0, slave_termios) < 0)
				goto childout;

		if (slave_winsize != NULL)
			if (ioctl(pts_fd, TIOCSWINSZ, slave_winsize) < 0)
				goto childout;

		if (dup2(pts_fd, STDIN_FILENO) < 0)
			goto childout;
		if (dup2(pts_fd, STDOUT_FILENO) < 0)
			goto childout;
		if (dup2(pts_fd, STDERR_FILENO) < 0)
			goto childout;

		write(pipefds[1], &status, sizeof(status));
		close(pipefds[1]);
		close(pts_fd);

		return 0;

	childout:
		status = errno;
		write(pipefds[1], &status, sizeof(status));
		exit(EXIT_FAILURE);
	}

	close(pipefds[1]);

	ssize_t n;
	if ((n = read(pipefds[0], &child_status, sizeof(child_status))) != sizeof(child_status)) {
		if (n >= 0)
			err_code = EIO;
		goto pipeout0;
	} else if (child_status != 0) {
		err_code = child_status;
		goto pipeout0;
	}

	close(pipefds[0]);

	return pid;

pipeout1:
	close(pipefds[1]);
pipeout0:
	close(pipefds[0]);
errout:
	close(*ptm_fd);
	errno = err_code;
	return -1;
}
Ejemplo n.º 14
0
pid_t pty_fork(int *ptrfdm, char *slave_name, int slave_namesz, const struct termios *slave_termios, const struct winsize *slave_winsize){
	int fdm, fds;
	pid_t pid;
	char pts_name[20];

	if((fdm = ptym_open(pts_name, sizeof(pts_name))) < 0)
		err_sys("can't open master pty: %s, error %d", pts_name, fdm);

	if(slave_name != NULL){
		/*
		 * Return name of slave. Null terminate to handle case
		 * where strlen(pts_name) > slave_namesz;
		 */
		strncpy(slave_name, pts_name, slave_namesz);
		slave_name[slave_namesz - 1] = '\0';
	}

	if((pid = fork()) < 0){
		return -1;
	}else if(pid == 0){/*child*/
		if(setsid() < 0)
			err_sys("setsid error");
		/*
		 * System V acquires controlling terminal on open().
		 */
		if((fds = ptys_open(pts_name)) < 0)
			err_sys("can't open slave pty");
		close(fdm);/*all done with master in child*/

#if defined(TIOCSCTTY)
		/*
		 * TIOCSCTTY is the BSD way to acquire a controlling terminal.
		 */
		if(ioctl(fds, TIOCSCTTY, (char *)0) < 0)
			err_sys("TIOCSCTTY error");
#endif
		
		/*
		 * Set slave's termios and window size.
		 */
		if(slave_termios != NULL){
			if(tcsetattr(fds, TCSANOW, slave_termios) < 0)
				err_sys("tcsetattr error on slave pty");
		}
		if(slave_winsize != NULL){
			if(ioctl(fds, TIOCSWINSZ, slave_winsize) < 0)
				err_sys("TIOCSWINSZ error on slave pty");
		}

		/*
		 * Slave becomes stdin/stdout/stderr of child.
		 */
		if(dup2(fds, STDIN_FILENO) != STDIN_FILENO)
			err_sys("dup2 error to stdin");
		if(dup2(fds, STDOUT_FILENO) != STDOUT_FILENO)
			err_sys("dup2 error to stdin");
		if(dup2(fds, STDERR_FILENO) != STDERR_FILENO)
			err_sys("dup2 error to stdin");
		if(fds != STDIN_FILENO && fds != STDOUT_FILENO && fds != STDERR_FILENO)
			close(fds);
		return 0;/*child return 0 just like fork()*/
	}else{
		*ptrfdm = fdm;/*return fd of master*/
		return pid;/*parent returns pid of child*/
	}
}
Ejemplo n.º 15
0
/****** uti/pty/fork_pty() *****************************************************
*  NAME
*     fork_pty() -- Opens a pty, forks and redirects the std handles
*
*  SYNOPSIS
*     pid_t fork_pty(int *ptrfdm, int *fd_pipe_err, dstring *err_msg)
*
*  FUNCTION
*     Opens a pty, forks and redirects stdin, stdout and stderr of the child
*     to the pty.
*
*  INPUTS
*     int *ptrfdm      - Receives the file descriptor of the master side of
*                        the pty.
*     int *fd_pipe_err - A int[2] array that receives the file descriptors
*                        of a pipe to separately redirect stderr.
*                        To achieve the same behaviour like rlogin/rsh, this
*                        is normally disabled, compile with
*                        -DUSE_PTY_AND_PIPE_ERR to enable this feature.
*     dstring *err_msg - Receives an error string in case of error.
*
*  RESULT
*     pid_t - -1 in case of error,
*              0 in the child process,
*              or the pid of the child process in the parent process.
*
*  NOTES
*     MT-NOTE: fork_pty() is not MT safe
*
*  SEE ALSO
*     pty/fork_no_pty
*******************************************************************************/
pid_t fork_pty(int *ptrfdm, int *fd_pipe_err, dstring *err_msg)
{
    pid_t pid;
    int   fdm, fds;
    char  pts_name[20];
    int   old_euid;

    /*
     * We run this either as root with euid="sge admin user" or as an unprivileged
     * user.  If we are root with euid="sge admin user", we must change our
     * euid back to root for this function.
     */
    old_euid = geteuid();
    if (getuid() == SGE_SUPERUSER_UID) {
        seteuid(SGE_SUPERUSER_UID);
    }
    if ((fdm = ptym_open(pts_name)) < 0) {
        sge_dstring_sprintf(err_msg, "can't open master pty \"%s\": %d, %s",
                            pts_name, errno, strerror(errno));
        return -1;
    }
#if defined(USE_PTY_AND_PIPE_ERR)
    if (pipe(fd_pipe_err) == -1) {
        sge_dstring_sprintf(err_msg, "can't create pipe for stderr: %d, %s",
                            errno, strerror(errno));
        return -1;
    }
#endif
    if ((pid = fork()) < 0) {
        return -1;
    } else if (pid == 0) {     /* child */
        if ((g_newpgrp = setsid()) < 0) {
            sge_dstring_sprintf(err_msg, "setsid() error: %d, %s",
                                errno, strerror(errno));
            return -1;
        }

        /* Open pty slave */
        if ((fds = ptys_open(fdm, pts_name)) < 0) {
            seteuid(old_euid);
            sge_dstring_sprintf(err_msg, "can't open slave pty: %d", fds);
            return -1;
        }
        seteuid(old_euid);
        close(fdm);
        fdm = -1;   /* all done with master in child */

#if   defined(TIOCSCTTY) && !defined(CIBAUD)
        /* 44BSD way to acquire controlling terminal */
        /* !CIBAUD to avoid doing this under SunOS */
        if (ioctl(fds, TIOCSCTTY, (char *) 0) < 0) {
            sge_dstring_sprintf(err_msg, "TIOCSCTTY error: %d, %s",
                                errno, strerror(errno));
            return -1;
        }
#endif
        /* slave becomes stdin/stdout/stderr of child */
        if ((dup2(fds, STDIN_FILENO)) != STDIN_FILENO) {
            sge_dstring_sprintf(err_msg, "dup2 to stdin error: %d, %s",
                                errno, strerror(errno));
            return -1;
        }
        if ((dup2(fds, STDOUT_FILENO)) != STDOUT_FILENO) {
            sge_dstring_sprintf(err_msg, "dup2 to stdout error: %d, %s",
                                errno, strerror(errno));
            return -1;
        }
#if defined(USE_PTY_AND_PIPE_ERR)
        close(fd_pipe_err[0]);
        fd_pipe_err[0] = -1;
        if ((dup2(fd_pipe_err[1], STDERR_FILENO)) != STDERR_FILENO) {
            sge_dstring_sprintf(err_msg, "dup2 to stderr error: %d, %s",
                                errno, strerror(errno));
            return -1;
        }
        close(fd_pipe_err[1]);
        fd_pipe_err[1] = -1;
#else
        if ((dup2(fds, STDERR_FILENO)) != STDERR_FILENO) {
            sge_dstring_sprintf(err_msg, "dup2 to stderr error: %d, %s",
                                errno, strerror(errno));
            return -1;
        }
#endif

        if (fds > STDERR_FILENO) {
            close(fds);
            fds = -1;
        }
        return 0;      /* child returns 0 just like fork() */
    } else {          /* parent */
        *ptrfdm = fdm; /* return fd of master */
        close(fd_pipe_err[1]);
        fd_pipe_err[1] = -1;
        seteuid(old_euid);
        return pid;    /* parent returns pid of child */
    }
}
Ejemplo n.º 16
0
pid_t
pty_fork2(int *ptrfdm, char *slave_name)
{
	int		fdm, fds;
	pid_t	pid;
	char	pts_name[20];

	if ( (fdm = ptym_open(pts_name)) < 0)
	{
	    msg("can't open master pty");
	    return ERRO;
	}

	strcpy(slave_name, pts_name);	/* return name of slave */
        /*/printf("\npty_fork2: %d %s", fdm, pts_name); */

	if ( (pid = fork()) < 0)
	{
	    msg("Can not fork");
	    return ERRO;
	}

	else if (pid == 0) {		/* child */
		/*/ sleep(30); */
		if (setsid() < 0)
		{
		   msg("setsid error");
		   exit(1);
		}

		/* SVR4 acquires controlling terminal on open() */
		if ( (fds = ptys_open(fdm, pts_name)) < 0)
		{
		    msg("can't open slave pty");
		    exit(1);
		}
		close(fdm);		/* all done with master in child */

#ifdef	LINUX
				/* 44BSD way to acquire controlling terminal */
    		if (ioctl(fds, TIOCSCTTY, (char *) 0) < 0)
    		{
		    msg("TIOCSCTTY error");
	  	    return ERRO;
   		}
#endif
		if (tty_copy(fds) == ERRO)
		   exit(1);
				/* slave becomes stdin/stdout/stderr of child */
		if (dup2(fds, STDOUT_FILENO) != STDOUT_FILENO ||
		    dup2(fds, STDERR_FILENO) != STDERR_FILENO ||
		    dup2(fds, STDIN_FILENO) != STDIN_FILENO )
		{
		    msg("dup2 error to slave");
		    exit(1);
		}
		if (fds > STDERR_FILENO)
		   close(fds);
		return 0;		/* child returns 0 just like fork() */

	} else {					/* parent */
		*ptrfdm = fdm;	/* return fd of master */
		return(pid);	/* parent returns pid of child */
	}
}
Ejemplo n.º 17
0
pid_t
pty_fork(int *ptrfdm, int pipe_out[], int pipe_err[])
{
	int		fdm, fds, k;
	pid_t	pid;
	char	pts_name[20];

/*/        fdm = glob_fdm;
//        strcpy(pts_name, glob_name); */
        
	if ( (fdm = ptym_open(pts_name)) < 0)
	{
	    msg("can't open master pty");
	    return ERRO;
	}

	strcpy(slave_name, pts_name);	/* return name of slave */

        /* /printf("\npty_fork: %d %s", fdm, pts_name); */
	pipe(pipe_out);
	pipe(pipe_err);

	if ( (pid = fork()) < 0)
	{   
	    msg("Can not fork");
	    return(ERRO);
 	}

	else if (pid == 0) {		/* child */
		/* /sleep(30); */
		if (setsid() < 0)
		{
		   msg("setsid error");
		   exit(1);
		}

		/* SVR4 acquires controlling terminal on open() */
		if ( (fds = ptys_open(fdm, pts_name)) < 0)
		{
		    printf("%d %s" , fdm, pts_name);
		    msg("can't open slave pty");
		    exit(1);
		}
		close(fdm);		/* all done with master in child */
		/* /printf("\nfdm: %d fds: %d\n", fdm, fds);fflush(stdin); */

#ifdef	LINUX
				/* 44BSD way to acquire controlling terminal */
    		if (ioctl(fds, TIOCSCTTY, (char *) 0) < 0)
    		{
		    msg("TIOCSCTTY error");
	  	    return ERRO;
   		}
#endif
		if (tty_copy(fds) == ERRO) /* set slave terminal mode */
		{
		    msg("tty_copy error");
		    perror(" ");
		    exit(1);
		}

		if (dup2(pipe_out[1], STDOUT_FILENO) != STDOUT_FILENO)
		{
		    msg("dup2 error to stdout");
		    exit(1);
		}
		if (dup2(pipe_err[1], STDERR_FILENO) != STDERR_FILENO)
		{
		    msg("dup2 error to stderr");
		    exit(1);
		} 
		if (dup2(fds, STDIN_FILENO) != STDIN_FILENO)
		{
		    msg("dup2 error to stdin");
		    exit(1);
		}
		if (pipe_out[1] > STDERR_FILENO)
			close(pipe_out[1]);
		 if (pipe_err[1] > STDERR_FILENO)
			close(pipe_err[1]);
		if (fds > STDERR_FILENO)
		{
			 close(fds);
		}
		close(pipe_out[0]);
		close(pipe_err[0]);
		return 0;		/* child returns 0 just like fork() */

	} else {					/* parent */
		*ptrfdm = fdm;	/* return fd of master */
		close(pipe_out[1]);
		close(pipe_err[1]);
		return(pid);	/* parent returns pid of child */
	}
}