# IO#close\_on\_exec? and IO#close\_on\_exec=

**URL:** <https://www.ruby-forum.com/t/io-close-on-exec-and-io-close-on-exec/120728>\
**Category:** Development\
**Created:** [November 20, 2007, 6:04am UTC](https://www.ruby-forum.com/t/io-close-on-exec-and-io-close-on-exec/120728 "2007-11-20T06:04:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tanaka\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c67d28/32.png) [@Tanaka\_A](https://www.ruby-forum.com/u/Tanaka_A)\
**Post date:** [November 20, 2007, 6:04am UTC](https://www.ruby-forum.com/t/io-close-on-exec-and-io-close-on-exec/120728/1 "2007-11-20T06:04:24Z")

</div>

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);

---

<div class="post-metadata">

**Author:** ![Tanaka\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c67d28/32.png) [@Tanaka\_A](https://www.ruby-forum.com/u/Tanaka_A)\
**Post date:** [November 20, 2007, 9:09am UTC](https://www.ruby-forum.com/t/io-close-on-exec-and-io-close-on-exec/120728/2 "2007-11-20T09:09:10Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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
