@@ -466,7 +466,11 @@ lws_spawn_piped(const struct lws_spawn_piped_info *i)
466466 if (slave >= 0 ) {
467467 struct termios t ;
468468 tcgetattr (slave , & t );
469- cfmakeraw (& t );
469+ t .c_iflag &= (tcflag_t )~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON );
470+ t .c_oflag &= (tcflag_t )~OPOST ;
471+ t .c_lflag &= (tcflag_t )~(ECHO | ECHONL | ICANON | ISIG | IEXTEN );
472+ t .c_cflag &= (tcflag_t )~(CSIZE | PARENB );
473+ t .c_cflag |= CS8 ;
470474 tcsetattr (slave , TCSANOW , & t );
471475 lsp -> pipe_fds [n ][0 ] = master ;
472476 lsp -> pipe_fds [n ][1 ] = slave ;
@@ -745,20 +749,29 @@ lws_spawn_piped(const struct lws_spawn_piped_info *i)
745749 * Bind the child's stdin / out / err to its side of our pipes
746750 */
747751
748- for (m = 0 ; m < 3 ; m ++ ) {
749- if (dup2 (lsp -> pipe_fds [m ][m != 0 ], m ) < 0 ) {
750- lwsl_err ("%s: stdin dup2 failed\n" , __func__ );
751- goto bail3 ;
752+ {
753+ int cfd [3 ];
754+
755+ for (m = 0 ; m < 3 ; m ++ )
756+ cfd [m ] = lsp -> pipe_fds [m ][m != 0 ];
757+
758+ for (m = 0 ; m < 3 ; m ++ ) {
759+ if (cfd [m ] < 0 )
760+ continue ;
761+ if (dup2 (cfd [m ], m ) < 0 ) {
762+ lwsl_err ("%s: dup2 failed for fd index %d (oldfd %d, newfd %d): errno %d (%s)\n" ,
763+ __func__ , m , cfd [m ], m , errno , strerror (errno ));
764+ goto bail3 ;
765+ }
752766 }
753- /*
754- * CLOEXEC on the lws-side of the pipe fds should have already
755- * dealt with closing those for the child perspective.
756- *
757- * Now it has done the dup, the child should close its original
758- * copies of its side of the pipes.
759- */
760767
761- close (lsp -> pipe_fds [m ][m != 0 ]);
768+ for (m = 0 ; m < 3 ; m ++ ) {
769+ if (cfd [m ] >= 0 && cfd [m ] != 0 && cfd [m ] != 1 && cfd [m ] != 2 ) {
770+ close (cfd [m ]);
771+ if (i -> pty_mode && m == LWS_STDOUT )
772+ cfd [LWS_STDERR ] = -1 ; /* Prevent double close */
773+ }
774+ }
762775 }
763776
764777#if defined(__sun ) || !defined(LWS_HAVE_VFORK ) || !defined(LWS_HAVE_EXECVPE )
0 commit comments