@@ -213,6 +213,8 @@ static int child_notifier = -1;
213213
214214enum child_errcode {
215215 CHILD_ERR_CHDIR ,
216+ CHILD_ERR_DUP2 ,
217+ CHILD_ERR_CLOSE ,
216218 CHILD_ERR_ENOENT ,
217219 CHILD_ERR_SILENT ,
218220 CHILD_ERR_ERRNO
@@ -235,6 +237,24 @@ static void child_die(enum child_errcode err)
235237 _exit (1 );
236238}
237239
240+ static void child_dup2 (int fd , int to )
241+ {
242+ if (dup2 (fd , to ) < 0 )
243+ child_die (CHILD_ERR_DUP2 );
244+ }
245+
246+ static void child_close (int fd )
247+ {
248+ if (close (fd ))
249+ child_die (CHILD_ERR_CLOSE );
250+ }
251+
252+ static void child_close_pair (int fd [2 ])
253+ {
254+ child_close (fd [0 ]);
255+ child_close (fd [1 ]);
256+ }
257+
238258/*
239259 * parent will make it look like the child spewed a fatal error and died
240260 * this is needed to prevent changes to t0061.
@@ -277,6 +297,12 @@ static void child_err_spew(struct child_process *cmd, struct child_err *cerr)
277297 error_errno ("exec '%s': cd to '%s' failed" ,
278298 cmd -> argv [0 ], cmd -> dir );
279299 break ;
300+ case CHILD_ERR_DUP2 :
301+ error_errno ("dup2() in child failed" );
302+ break ;
303+ case CHILD_ERR_CLOSE :
304+ error_errno ("close() in child failed" );
305+ break ;
280306 case CHILD_ERR_ENOENT :
281307 error_errno ("cannot run %s" , cmd -> argv [0 ]);
282308 break ;
@@ -527,35 +553,35 @@ int start_command(struct child_process *cmd)
527553 child_notifier = notify_pipe [1 ];
528554
529555 if (cmd -> no_stdin )
530- dup2 (null_fd , 0 );
556+ child_dup2 (null_fd , 0 );
531557 else if (need_in ) {
532- dup2 (fdin [0 ], 0 );
533- close_pair (fdin );
558+ child_dup2 (fdin [0 ], 0 );
559+ child_close_pair (fdin );
534560 } else if (cmd -> in ) {
535- dup2 (cmd -> in , 0 );
536- close (cmd -> in );
561+ child_dup2 (cmd -> in , 0 );
562+ child_close (cmd -> in );
537563 }
538564
539565 if (cmd -> no_stderr )
540- dup2 (null_fd , 2 );
566+ child_dup2 (null_fd , 2 );
541567 else if (need_err ) {
542- dup2 (fderr [1 ], 2 );
543- close_pair (fderr );
568+ child_dup2 (fderr [1 ], 2 );
569+ child_close_pair (fderr );
544570 } else if (cmd -> err > 1 ) {
545- dup2 (cmd -> err , 2 );
546- close (cmd -> err );
571+ child_dup2 (cmd -> err , 2 );
572+ child_close (cmd -> err );
547573 }
548574
549575 if (cmd -> no_stdout )
550- dup2 (null_fd , 1 );
576+ child_dup2 (null_fd , 1 );
551577 else if (cmd -> stdout_to_stderr )
552- dup2 (2 , 1 );
578+ child_dup2 (2 , 1 );
553579 else if (need_out ) {
554- dup2 (fdout [1 ], 1 );
555- close_pair (fdout );
580+ child_dup2 (fdout [1 ], 1 );
581+ child_close_pair (fdout );
556582 } else if (cmd -> out > 1 ) {
557- dup2 (cmd -> out , 1 );
558- close (cmd -> out );
583+ child_dup2 (cmd -> out , 1 );
584+ child_close (cmd -> out );
559585 }
560586
561587 if (cmd -> dir && chdir (cmd -> dir ))
0 commit comments