connect,tryopen: set close-on-exec flag for new fds on Ruby 2.0+
[kgio.git] / ext / kgio / tryopen.c
blob85bbbc26f2680bb222c0b653dc94f04557bd62fb
1 #include <ruby.h>
2 #ifdef HAVE_RUBY_IO_H
3 # include <ruby/io.h>
4 #else
5 # include <rubyio.h>
6 #endif
8 #ifdef HAVE_RUBY_ST_H
9 # include <ruby/st.h>
10 #else
11 # include <st.h>
12 #endif
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include "set_file_path.h"
19 static ID id_for_fd, id_to_path, id_path;
20 static st_table *errno2sym;
22 struct open_args {
23 const char *pathname;
24 int flags;
25 mode_t mode;
28 #ifndef HAVE_RB_CLOEXEC_OPEN
29 # define rb_cloexec_open(p,f,m) open((p),(f),(m))
30 #endif
32 static VALUE nogvl_open(void *ptr)
34 struct open_args *o = ptr;
36 return (VALUE)rb_cloexec_open(o->pathname, o->flags, o->mode);
39 #ifndef HAVE_RB_THREAD_BLOCKING_REGION
40 # define RUBY_UBF_IO ((void *)(-1))
41 # include "rubysig.h"
42 typedef void rb_unblock_function_t(void *);
43 typedef VALUE rb_blocking_function_t(void *);
44 static VALUE rb_thread_blocking_region(
45 rb_blocking_function_t *fn, void *data1,
46 rb_unblock_function_t *ubf, void *data2)
48 VALUE rv;
50 TRAP_BEG; /* for FIFO */
51 rv = fn(data1);
52 TRAP_END;
54 return rv;
56 #endif /* ! HAVE_RB_THREAD_BLOCKING_REGION */
59 * call-seq:
61 * Kgio::File.tryopen(filename, [, mode [, perm]]) -> Kgio::File or Symbol
63 * Returns a Kgio::File object on a successful open. +filename+ is a
64 * path to any file on the filesystem. If specified, +mode+ is a bitmask
65 * of flags (see IO.sysopen) and +perm+ should be an octal number.
67 * This does not raise errors for most failures, but installs returns a
68 * Ruby symbol for the constant in the Errno::* namespace.
70 * Common error symbols are:
72 * - :ENOENT
73 * - :EACCES
75 * See your open(2) manpage for more information on open(2) errors.
77 static VALUE s_tryopen(int argc, VALUE *argv, VALUE klass)
79 int fd;
80 VALUE pathname, flags, mode;
81 struct open_args o;
82 int retried = 0;
83 VALUE rv;
85 rb_scan_args(argc, argv, "12", &pathname, &flags, &mode);
86 if (rb_respond_to(pathname, id_to_path))
87 pathname = rb_funcall(pathname, id_to_path, 0);
88 o.pathname = StringValueCStr(pathname);
90 switch (TYPE(flags)) {
91 case T_NIL: o.flags = O_RDONLY; break;
92 case T_FIXNUM: o.flags = FIX2INT(flags); break;
93 case T_BIGNUM: o.flags = NUM2INT(flags); break;
94 default: rb_raise(rb_eArgError, "flags must be an Integer");
96 switch (TYPE(mode)) {
97 case T_NIL: o.mode = 0666; break;
98 case T_FIXNUM: o.mode = FIX2INT(mode); break;
99 case T_BIGNUM: o.mode = NUM2INT(mode); break;
100 default: rb_raise(rb_eArgError, "mode must be an Integer");
103 retry:
104 fd = (int)rb_thread_blocking_region(nogvl_open, &o, RUBY_UBF_IO, 0);
105 if (fd == -1) {
106 if (errno == EMFILE || errno == ENFILE || errno == ENOMEM) {
107 rb_gc();
108 if (retried)
109 rb_sys_fail(o.pathname);
110 retried = 1;
111 goto retry;
113 if (fd == -1) {
114 int saved_errno = errno;
116 if (!st_lookup(errno2sym, (st_data_t)errno, &rv)) {
117 errno = saved_errno;
118 rb_sys_fail(o.pathname);
120 return rv;
123 rv = rb_funcall(klass, id_for_fd, 1, INT2FIX(fd));
124 set_file_path(rv, pathname);
125 return rv;
128 void init_kgio_tryopen(void)
130 VALUE mKgio = rb_define_module("Kgio");
131 VALUE mPipeMethods = rb_const_get(mKgio, rb_intern("PipeMethods"));
132 VALUE cFile;
133 VALUE tmp;
134 VALUE *ptr;
135 long len;
137 id_path = rb_intern("path");
138 id_for_fd = rb_intern("for_fd");
139 id_to_path = rb_intern("to_path");
142 * Document-class: Kgio::File
144 * This subclass of the core File class adds the "tryopen" singleton
145 * method for opening files. A single "tryopen" and check for the
146 * return value may be used to avoid unnecessary stat(2) syscalls
147 * or File.open exceptions when checking for the existence of a file
148 * and opening it.
150 cFile = rb_define_class_under(mKgio, "File", rb_cFile);
151 rb_define_singleton_method(cFile, "tryopen", s_tryopen, -1);
152 rb_include_module(cFile, mPipeMethods);
154 if (!rb_funcall(cFile, rb_intern("method_defined?"), 1,
155 ID2SYM(id_to_path)))
156 rb_define_alias(cFile, "to_path", "path");
158 errno2sym = st_init_numtable();
159 tmp = rb_funcall(rb_mErrno, rb_intern("constants"), 0);
160 ptr = RARRAY_PTR(tmp);
161 len = RARRAY_LEN(tmp);
162 for (; --len >= 0; ptr++) {
163 VALUE error;
164 ID const_id;
166 switch (TYPE(*ptr)) {
167 case T_SYMBOL: const_id = SYM2ID(*ptr); break;
168 case T_STRING: const_id = rb_intern(RSTRING_PTR(*ptr)); break;
169 default: rb_bug("constant not a symbol or string");
172 error = rb_const_get(rb_mErrno, const_id);
173 if ((TYPE(error) != T_CLASS) ||
174 !rb_const_defined(error, rb_intern("Errno")))
175 continue;
177 error = rb_const_get(error, rb_intern("Errno"));
178 switch (TYPE(error)) {
179 case T_FIXNUM:
180 case T_BIGNUM:
181 st_insert(errno2sym, (st_data_t)NUM2INT(error),
182 ID2SYM(const_id));