Updated version of open_init_pty
Hi,
This patch removes the unused functions tty_cbreak() and
tty_raw(). I have also nailed down the cause of the error that caused truncated out put (I was able to reproduce the problem by running "open_init_pty ls" in a small directory -- the problem was faulty handling of the SIG_CHLD signal, where I was not draining the last bits stuck in the pipe.
Also fixed are partial writes (which could happen if the write
was interrupted), now we resume the write from where we left off until all the bits are written. This could have resulted in lost characters as well.
This is not perfect, since it does not work very well for
interactive scripts that do not take full control f the screen -- but it works for init scripts, it works with dselect, aptitude, and the lot, so this is good enough for this iteration.
I have also increased the time the program sits in select, as
opposed to running around the loop; but even setting the last argument of select to NULL (and waiting forever) shows
"open_init_pty top" taking up a large chunk of CPU. I have made
open_init_pty nice the parent process after executing the child process, but before moving any data around, to help with this situation.
Ideally, open_init_pty should track the child process setting
of the pseudo pty settings (like local echo settings, and so on) and transferring that to the controlling tty, so that local echo is only turned off when the child process wants it that way. More testing is needed for that.
Anyway, a patch from the last version I sent in is
attached. Please let me know if I should send in the current version of the file.
manoj
* modified files
run_init/open_init_pty.c +67 -75
@@ -46,74 +46,6 @@static enum {RESET ,RAW ,CBREAK } tty_state =RESET ;static int -tty_cbreak (int fd) -{ -struct termios buf; - -if (tty_state ==RESET ) - { -if (tcgetattr (fd, &saved_termios) <0 ) - { -return -1 ; - } - } - - buf = saved_termios; - buf.c_lflag &= ~(ECHO |ICANON );/* echo off, canonical mode off */ - buf.c_cc[VMIN ] =1 ;/* one byte at a time, no timer */ - buf.c_cc[VTIME ] =0 ; -if (tcsetattr (fd,TCSANOW , &buf) <0 ) - { -return -1 ; - }/* end of if(tcsetattr(fileno(stdin), TCSANOW, &buf) < 0) */ - tty_state =CBREAK ; - saved_fd = fd; -return 0 ; -} - -static int -tty_raw (int fd) -{ -struct termios buf; - -if (tty_state ==RESET ) - { -if (tcgetattr (fd, &saved_termios) <0 ) - { -return -1 ; - } - } - - buf = saved_termios; -/* -* echo off, canonical mode off, extended input processing off, -* signal chars off -*/ - buf.c_lflag &= ~(ECHO |ICANON |IEXTEN |ISIG ); -/* -* no SIGINT on break, CR-to-NL off, input parity check off, do not -* strip 8th bit on input,output flow control off -*/ - buf.c_iflag &= ~(BRKINT |ICRNL |INPCK |ISTRIP |IXON ); -/* Clear size bits, parity checking off */ - buf.c_cflag &= ~(CSIZE |PARENB ); -/* set 8 bits/char */ - buf.c_cflag |=CS8 ; -/* Output processing off */ - buf.c_oflag &= ~(OPOST ); - - buf.c_cc[VMIN ] =1 ;/* one byte at a time, no timer */ - buf.c_cc[VTIME ] =0 ; -if (tcsetattr (fd,TCSANOW , &buf) <0 ) - { -return -1 ; - }/* end of if(tcsetattr(fileno(stdin), TCSANOW, &buf) < 0) */ - tty_state =RAW ; - saved_fd = fd; -return 0 ; -} - -static int tty_semi_raw (int fd) {struct termios buf;
@@ -129,7 +61,7 @@ buf = saved_termios;/* * echo off, canonical mode off, extended input processing off, -* signal chars off +* signal chars off */ buf.c_lflag &= ~(ECHO |ICANON |IEXTEN |ISIG );/*
@@ -310,15 +242,25 @@ }/* end of if(atexit(tty_atexit) < 0) */ } + +/* ignore return from nice, but lower our priority */ +nice (19 ); +/* while no signal, we loop around */ -while (1 ) { +int done =0 ; +while (! done) {struct timeval interval;fd_set t_readfds;fd_set t_writefds;fd_set t_exceptfds; - +/* +* We still use a blocked signal, and check for SIGCHLD every +* loop, since waiting infinitely did not really help the load +* when running, say, top. +*/ interval.tv_sec =0 ; - interval.tv_usec =100 ; + interval.tv_usec =200000 ;/* so, check for signals every 200 milli +seconds */ t_readfds = readfds; t_writefds = writefds;
@@ -329,7 +271,8 @@if ( retval ==SIGCHLD ) {/* child terminated */ -break ; + done =1 ;/* in case they do not close off their +file descriptors */ }else {
@@ -365,6 +308,7 @@ {/* Nothing left to read? */ fflush (stdout);fflush (stderr); +/* fprintf(stderr, "DEBUG: %d: Nothing left to read?\n", __LINE__);*/ exit (EXIT_SUCCESS ); }/* end of else */ }/* end of if(retval < 0) */
@@ -376,12 +320,37 @@ {/* child closed connection */ fflush (stdout);fflush (stderr); +/*fprintf(stderr, "DEBUG: %d: child closed connection?\n", __LINE__);*/ exit (EXIT_SUCCESS ); } }/* end of if(retval == 0) */ else { -write (fileno (stdout), buf, (unsigned int )retval); +ssize_t nleft = retval; +ssize_t nwritten =0 ; +char * ptr = buf; +while (nleft >0 ) + { +if ((nwritten =write (fileno (stdout), ptr, + (unsigned int )nleft)) <=0 ) + { +if (errno ==EINTR ) + { + nwritten =0 ; + }/* end of if(errno == EINTR) */ +else + { +perror ("write" ); +fflush (stdout); +fflush (stderr); +exit (EXIT_SUCCESS ); + }/* end of else */ + }/* end of if((nwritten = write(sockfd, ptr, nleft)) <= 0) */ + nleft -= nwritten; + ptr += nwritten; + }/* end of while(nleft > 0) */ + +/* fprintf(stderr, "DEBUG: %d: wrote %d\n", __LINE__, retval);*/ fflush (stdout); }/* end of else */ }/* end of else */
@@ -413,7 +382,30 @@ }/* end of if(retval == 0) */ else { -write (pty_master, buf, (unsigned int )retval); +ssize_t nleft = retval; +ssize_t nwritten =0 ; +char * ptr = buf; +while (nleft >0 ) + { +if ((nwritten =write (pty_master, ptr, + (unsigned int )nleft)) <=0 ) + { +if (errno ==EINTR ) + { + nwritten =0 ; + }/* end of if(errno == EINTR) */ +else + { +perror ("write" ); +fflush (stdout); +fflush (stderr); +exit (EXIT_SUCCESS ); + }/* end of else */ + }/* end of if((nwritten = write(sockfd, ptr, nleft)) <= 0) */ + nleft -= nwritten; + ptr += nwritten; + }/* end of while(nleft > 0) */ +fflush (stdout); }/* end of else */ }/* end of else */
Signature · 4 lines
-- Operator, please trace this call and tell me where I am. Manoj Srivastava <[email protected]> <[email protected]> 1024D/BF24424C print 4966 F272 D093 B493 410B 924B 21BA DABB BF24 424C
Quoted 33 lines · Manoj Srivastava
Hi,
This patch removes the unused functions
tty_cbreak()and
tty_raw(). I have also nailed down the cause of the error that caused truncated out put (I was able to reproduce the problem by running "open_init_ptyls" in a small directory -- the problem was faulty handling of theSIG_CHLDsignal, where I was not draining the last bits stuck in the pipe.Also fixed are partial writes (which could happen if the write
was interrupted), now we resume the write from where we left off until all the bits are written. This could have resulted in lost characters as well.This is not perfect, since it does not work very well for
interactive scripts that do not take full control f the screen -- but it works for init scripts, it works with dselect, aptitude, and the lot, so this is good enough for this iteration.I have also increased the time the program sits in select, as
opposed to running around the loop; but even setting the last argument of select to NULL (and waiting forever) shows
"open_init_ptytop" taking up a large chunk of CPU. I have made
open_init_ptynice the parent process after executing the child process, but before moving any data around, to help with this situation.Ideally,
open_init_ptyshould track the child process setting
of the pseudo pty settings (like local echo settings, and so on) and transferring that to the controlling tty, so that local echo is only turned off when the child process wants it that way. More testing is needed for that.
Quoted 3 lines
Anyway, a patch from the last version I sent in is
attached. Please let me know if I should send in the current version of the file.
Thanks, merged into sourceforge CVS as of policycoreutils 1.21.10.
Quoted 2 lines
______________________________________________________________________
Signature · 8 lines
Stephen Smalley <[email protected]> National Security Agency -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.