[Date Prev][Date Next] [Chronological] [Thread] [Top]

Re: lutil_detach()



At 05:07 AM 1/23/2003, Hallvard B Furuseth wrote:
>Is there any reason why lutil_detach() closes stdin/out/err before
>doing dup2() to them?

Not that I can recall.

>dup2() closes the destination descriptor
>anyway, at least in the Unixes I know.  Closing them has the slight
>problem that if they were already closed so 'sd' (the /dev/null
>descriptor) is 0, 1, or 2, 'sd' is closed.
>
>If there are no protests, I'll apply this patch.

none here.


>It skips closing the descriptors, tries to open /dev/null and then /
>in read-only mode if opening /dev/null failed, and skips the dup2()s
>as well if open() failed.
>
>--- libraries/liblutil/detach.c 19 Apr 2000 11:35:43 -0000      1.12
>+++ libraries/liblutil/detach.c 23 Jan 2003 13:00:59 -0000
>@@ -73,13 +73,10 @@
>                }
> 
>-               if ( (sd = open( "/dev/null", O_RDWR )) == -1 ) {
>+               if ( (sd = open( "/dev/null", O_RDWR   )) == -1 &&
>+                    (sd = open( "/dev/null", O_RDONLY )) == -1 &&
>+                    /* Panic -- open *something* */
>+                    (sd = open( "/",         O_RDONLY )) == -1    ) {
>                        perror("/dev/null");
>-               }
>-
>-               /* close stdin, stdout, stderr */
>-               close( STDIN_FILENO );
>-               close( STDOUT_FILENO );
>-               close( STDERR_FILENO );
>-
>+               } else {
>                /* redirect stdin, stdout, stderr to /dev/null */
>                dup2( sd, STDIN_FILENO );
>@@ -87,5 +84,13 @@
>                dup2( sd, STDERR_FILENO );
> 
>+                       switch( sd ) {
>+                       default:
>                                close( sd );
>+                       case STDIN_FILENO:
>+                       case STDOUT_FILENO:
>+                       case STDERR_FILENO:
>+                               break;
>+                       }
>+               }
> 
>                if ( do_close ) {