IO#close_on_exec? and IO#close_on_exec=

IO#close_on_exec? e$B$He(B IO#close_on_exec= e$B$rF3F~$7$F!"$3$l$i$Ge(B
e$B$Oe(B 2e$B$D$Ne(B fd e$B$r07$&$N$O$I$&$G$7$g$&$+!#e(B

fd e$B$,e(B 2e$B$D$"$k$H!“C1=c$Je(B fcntl e$B$G$ON>J}$re(B close on exec
e$B$K$Ge(B
e$B$-$J$/$F:$$k!”$H$$$&$3$H$r@Ne(B [ruby-list:26156] e$B$GJs9p$7$F!“e(B
e$B$=$N7k2L$H$7$Fe(Bfcntl e$B$ON>J}$Ne(B fd
e$B$K<B9T$5$l$k$H$$$&$3$H$K$J$je(B
e$B$^$7$?$,!”$3$l$O$J$s$H$bMpK=$G$9!#$?$H$($P!"e(B
[ruby-list:26208] e$B$Ge(B F_DUPFD e$B$O$^$:$$$H=q$-$^$7$?$,!"e(BF_DUPFD
e$B$re(B 2e$B2s<B9T$7$F$R$H$D$N7k2L$rL5;k$9$k$H$$$&$N$O$J$s$H$b4qL/$Je(B
e$B5sF0$G$9!#e(B

e$B$H$$$&$o$1$G!“e(Bclose on exec e$B$KFC2=$7$F%a%=%C%I$rMQ0U$9$k$N$,e(B
e$B$$$$$H;W$$$^$9!#B>$Ne(B fcntl e$B$K$D$$$F$O!”$b$7I,MW$K$J$C$?$iHse(B
e$B>oMQ$N%$%s%9%?%s%9JQ?t$G$I$&$K$+$7$F!"=<J,$KMQK!$,GD0.$G$-$?e(B
e$B$i%a%=%C%I$K$9$k$3$H$r9M$($k$H$$$&$3$H$G!#e(B

Index: io.c

— io.c (revision 13977)
+++ io.c (working copy)
@@ -2295,6 +2295,99 @@ rb_io_isatty(VALUE io)
return Qtrue;
}

+/*

    • call-seq:
    • ios.close_on_exec?   => true or false
      
    • Returns true if ios will be closed on exec.
    • f = open("/dev/null")
      
    • f.close_on_exec?                 #=> false
      
    • f.close_on_exec = true
      
    • f.close_on_exec?                 #=> true
      
    • f.close_on_exec = false
      
    • f.close_on_exec?                 #=> false
      
  • */

+static VALUE
+rb_io_close_on_exec_p(VALUE io)
+{
+#if defined(HAVE_FCNTL) && defined(F_GETFD) && defined(F_SETFD) &&
defined(FD_CLOEXEC)

  • rb_io_t *fptr;
  • VALUE write_io;
  • int fd, ret;
  • write_io = GetWriteIO(io);
  • if (io != write_io) {
  •    GetOpenFile(write_io, fptr);
    
  •    if (fptr && 0 <= (fd = fptr->fd)) {
    
  •        if ((ret = fcntl(fd, F_GETFD)) == -1) 
    

rb_sys_fail(fptr->path);

  •        if (!(ret & FD_CLOEXEC)) return Qfalse;
    
  •    }
    
  • }
  • GetOpenFile(io, fptr);
  • if (fptr && 0 <= (fd = fptr->fd)) {
  •    if ((ret = fcntl(fd, F_GETFD)) == -1) rb_sys_fail(fptr->path);
    
  •    if (!(ret & FD_CLOEXEC)) return Qfalse;
    
  • }
  • return Qtrue;
    +#else
  • rb_notimplement();
  • return Qnil; /* not reached */
    +#endif
    +}

+/*

    • call-seq:
    • ios.close_on_exec = bool    => true or false
      
    • Sets a close-on-exec flag.
    • f = open("/dev/null")
      
    • f.close_on_exec = true
      
    • system("cat", "/proc/self/fd/#{f.fileno}")
      
    • #=> cat: /proc/self/fd/3: No such file or directory
      
  • */

+static VALUE
+rb_io_set_close_on_exec(VALUE io, VALUE arg)
+{
+#if defined(HAVE_FCNTL) && defined(F_GETFD) && defined(F_SETFD) &&
defined(FD_CLOEXEC)

  • int flag = RTEST(arg) ? FD_CLOEXEC : 0;
  • rb_io_t *fptr;
  • VALUE write_io;
  • int fd, ret;
  • write_io = GetWriteIO(io);
  • if (io != write_io) {
  •    GetOpenFile(write_io, fptr);
    
  •    if (fptr && 0 <= (fd = fptr->fd)) {
    
  •        if ((ret = fcntl(fptr->fd, F_GETFD)) == -1) 
    

rb_sys_fail(fptr->path);

  •        if ((ret & FD_CLOEXEC) != flag) {
    
  •            ret = (ret & ~FD_CLOEXEC) | flag;
    
  •            ret = fcntl(fd, F_SETFD, ret);
    
  •            if (ret == -1) rb_sys_fail(fptr->path);
    
  •        }
    
  •    }
    
  • }
  • GetOpenFile(io, fptr);
  • if (fptr && 0 <= (fd = fptr->fd)) {
  •    if ((ret = fcntl(fd, F_GETFD)) == -1) rb_sys_fail(fptr->path);
    
  •    if ((ret & FD_CLOEXEC) != flag) {
    
  •        ret = (ret & ~FD_CLOEXEC) | flag;
    
  •        ret = fcntl(fd, F_SETFD, ret);
    
  •        if (ret == -1) rb_sys_fail(fptr->path);
    
  •    }
    
  • }
    +#else
  • rb_notimplement();
    +#endif
  • return Qnil;
    +}

#define FMODE_PREP (1<<16)
#define IS_PREP_STDIO(f) ((f)->mode & FMODE_PREP)
#define PREP_STDIO_NAME(f) ((f)->path)
@@ -6021,6 +6114,9 @@ Init_IO(void)
rb_define_method(rb_cIO, “eof”, rb_io_eof, 0);
rb_define_method(rb_cIO, “eof?”, rb_io_eof, 0);

  • rb_define_method(rb_cIO, “close_on_exec?”, rb_io_close_on_exec_p,
    0);
  • rb_define_method(rb_cIO, “close_on_exec=”, rb_io_set_close_on_exec,
    1);
  • rb_define_method(rb_cIO, “close”, rb_io_close_m, 0);
    rb_define_method(rb_cIO, “closed?”, rb_io_closed, 0);
    rb_define_method(rb_cIO, “close_read”, rb_io_close_read, 0);

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:32323] IO#close_on_exec? and
IO#close_on_exec=”
on Tue, 20 Nov 2007 14:03:33 +0900, Tanaka A. [email protected]
writes:
|
|IO#close_on_exec? e$B$He(B IO#close_on_exec= e$B$rF3F~$7$F!"$3$l$i$Ge(B
|e$B$Oe(B 2e$B$D$Ne(B fd e$B$r07$&$N$O$I$&$G$7$g$&$+!#e(B

e$B$3$l$OF3F~$7$F$bNI$$$N$G$O$J$$$G$7$g$&$+!#e(B