/* kiddy.c by Michael Thorpe 97-11-16 */ #include #include #include #include #include #include #include #include #include "tbwm.h" #include "kiddy.h" extern struct termios termios_norm; int ptym_open(char *pts_name); int ptys_open(char *pts_name); void TBWM_Resizetty(int fd,int rows,int cols) { struct winsize winsize; winsize.ws_row=rows; winsize.ws_col=cols; winsize.ws_xpixel=0; winsize.ws_ypixel=0; if(ioctl(fd,TIOCSWINSZ,&winsize)) { fprintf(debug,"Error setting window size on tty %d\n",fd); fflush(debug); exit(-2); } } int makekiddy(pid_t *pid,int *fd,char **process,int rows,int cols) { int fdm,fds,i; pid_t my_pid; char s[20]; if(-1==(fdm=ptym_open(s))) return(-1); fprintf(debug,"Opened master tty %s\n",s); fflush(0); if(0>(my_pid=fork())) { fprintf(debug,"Error forking\n"); close(fdm); return(-1); } else if(0==my_pid) { close(fdm); if(0>setsid()) { fprintf(debug,"Error from setsid\n"); exit(-2); } if(-1==(fds=ptys_open(s))) { fprintf(debug,"Error opening slave tty %s\n",s); fflush(debug); exit(-2); } tcsetattr(fds,TCSANOW,&termios_norm); setenv("TERM","vt100",1); TBWM_Resizetty(fds,rows,cols); for(i=0;i<3;i++) if(dup2(fds,i) != i) { fprintf(debug,"dup2 error to fd %d\n",i); fflush(debug); exit(-2); } if(fds>2) close(fds); signal(SIGCHLD,SIG_DFL); execvp(*process,process); fprintf(debug,"Error execing\n"); fflush(debug); exit(-2); } else { fprintf(debug,"Created kid w/pid %d\n",my_pid); *pid=my_pid; *fd=fdm; return(0); } } int ptym_open(char *pts_name) { int fdm; char *ptr1,*ptr2; strcpy(pts_name,"/dev/ptyXY"); for(ptr1="pqrstuvwxyzPQRST";*ptr1 != 0;ptr1++) { pts_name[8]=*ptr1; for(ptr2="0123456789abcdef";*ptr2 != 0;ptr2++) { pts_name[9]=*ptr2; if(0>(fdm=open(pts_name,O_RDWR))) { if(errno==ENOENT) return(-1); else continue; } pts_name[5]='t'; return(fdm); } } return(-1); } int ptys_open(char *pts_name) { int fds; if(0>(fds=open(pts_name,O_RDWR))) return(-1); return(fds); }