Permalink
Browse files

* backport r32579, r32581, r32587 by akr and r32588 by kazu.

r32579:

* io.c (rb_update_max_fd): new function.

* internal.h (rb_update_max_fd): declare rb_update_max_fd.

* thread_pthread.c (rb_thread_create_timer_thread): update max fd when
  timer thread pipe is created.

r32581:

* io.c (UPDATE_MAXFD): removed.

r32587:

* include/ruby/intern.h (rb_update_max_fd): declaration moved from
  internal.h.

* file.c: ditto.

* io.c: call rb_update_max_fd for each new fds.

* process.c: ditto.

* random.c: ditto.

* ruby.c: ditto.

* ext/io/console/console.c: ditto.

* ext/openssl/ossl_bio.c: ditto.

* ext/pty/pty.c: ditto.

* ext/socket/init.c: ditto.

* ext/socket/socket.c: ditto.

* ext/socket/ancdata.c: ditto.

* ext/socket/unixsocket.c: ditto.

r32588:

* io.c (rb_update_max_fd): remove parentheses. they are not in
  macro.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_3@32591 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information...
1 parent ea6044e commit fdcac09849a7644618a202215a2615f1f029b48b @akr akr committed Jul 20, 2011
Showing with 115 additions and 11 deletions.
  1. +51 −0 ChangeLog
  2. +2 −0 ext/io/console/console.c
  3. +1 −0 ext/openssl/ossl_bio.c
  4. +14 −0 ext/pty/pty.c
  5. +2 −0 ext/socket/ancdata.c
  6. +5 −0 ext/socket/init.c
  7. +2 −0 ext/socket/socket.c
  8. +1 −0 ext/socket/unixsocket.c
  9. +2 −0 file.c
  10. +1 −0 include/ruby/intern.h
  11. +19 −11 io.c
  12. +11 −0 process.c
  13. +1 −0 random.c
  14. +1 −0 ruby.c
  15. +2 −0 thread_pthread.c
View
51 ChangeLog
@@ -1,3 +1,54 @@
+Thu Jul 21 07:07:57 2011 Tanaka Akira <akr@fsij.org>
+
+ * backport r32579, r32581, r32587 by akr and r32588 by kazu.
+
+ r32579:
+
+ * io.c (rb_update_max_fd): new function.
+
+ * internal.h (rb_update_max_fd): declare rb_update_max_fd.
+
+ * thread_pthread.c (rb_thread_create_timer_thread): update max fd when
+ timer thread pipe is created.
+
+ r32581:
+
+ * io.c (UPDATE_MAXFD): removed.
+
+ r32587:
+
+ * include/ruby/intern.h (rb_update_max_fd): declaration moved from
+ internal.h.
+
+ * file.c: ditto.
+
+ * io.c: call rb_update_max_fd for each new fds.
+
+ * process.c: ditto.
+
+ * random.c: ditto.
+
+ * ruby.c: ditto.
+
+ * ext/io/console/console.c: ditto.
+
+ * ext/openssl/ossl_bio.c: ditto.
+
+ * ext/pty/pty.c: ditto.
+
+ * ext/socket/init.c: ditto.
+
+ * ext/socket/socket.c: ditto.
+
+ * ext/socket/ancdata.c: ditto.
+
+ * ext/socket/unixsocket.c: ditto.
+
+ r32588:
+
+ * io.c (rb_update_max_fd): remove parentheses. they are not in
+ macro.
+
Sun Jul 17 08:07:31 2011 Martin Bosslet <Martin.Bosslet@googlemail.com>
* backport r32563 from trunk
View
2 ext/io/console/console.c
@@ -562,6 +562,7 @@ console_dev(VALUE klass)
#ifdef CONSOLE_DEVICE_FOR_WRITING
fd = open(CONSOLE_DEVICE_FOR_WRITING, O_WRONLY);
if (fd < 0) return Qnil;
+ rb_update_max_fd(fd);
args[1] = INT2FIX(O_WRONLY);
args[0] = INT2NUM(fd);
out = rb_class_new_instance(2, args, klass);
@@ -573,6 +574,7 @@ console_dev(VALUE klass)
#endif
return Qnil;
}
+ rb_update_max_fd(fd);
args[1] = INT2FIX(O_RDWR);
args[0] = INT2NUM(fd);
con = rb_class_new_instance(2, args, klass);
View
1 ext/openssl/ossl_bio.c
@@ -28,6 +28,7 @@ ossl_obj2bio(VALUE obj)
if ((fd = dup(FPTR_TO_FD(fptr))) < 0){
rb_sys_fail(0);
}
+ rb_update_max_fd(fd);
if (!(fp = fdopen(fd, "r"))){
close(fd);
rb_sys_fail(0);
View
14 ext/pty/pty.c
@@ -177,6 +177,7 @@ chfunc(void *data, char *errbuf, size_t errbuf_len)
{
int i = open("/dev/tty", O_RDONLY);
if (i < 0) ERROR_EXIT("/dev/tty");
+ rb_update_max_fd(i);
if (ioctl(i, TIOCNOTTY, (char *)0))
ERROR_EXIT("ioctl(TIOCNOTTY)");
close(i);
@@ -198,6 +199,7 @@ chfunc(void *data, char *errbuf, size_t errbuf_len)
if (slave < 0) {
ERROR_EXIT("open: pty slave");
}
+ rb_update_max_fd(slave);
close(master);
#endif
dup2(slave,0);
@@ -289,13 +291,15 @@ get_device_once(int *master, int *slave, char SlaveName[DEVICELEN], int nomesg,
sigemptyset(&dfl.sa_mask);
if ((masterfd = posix_openpt(O_RDWR|O_NOCTTY)) == -1) goto error;
+ rb_update_max_fd(masterfd);
if (sigaction(SIGCHLD, &dfl, &old) == -1) goto error;
if (grantpt(masterfd) == -1) goto grantpt_error;
if (sigaction(SIGCHLD, &old, NULL) == -1) goto error;
if (unlockpt(masterfd) == -1) goto error;
if ((slavedevice = ptsname(masterfd)) == NULL) goto error;
if (no_mesg(slavedevice, nomesg) == -1) goto error;
if ((slavefd = open(slavedevice, O_RDWR|O_NOCTTY, 0)) == -1) goto error;
+ rb_update_max_fd(slavefd);
#if defined I_PUSH && !defined linux
if (ioctl(slavefd, I_PUSH, "ptem") == -1) goto error;
@@ -327,6 +331,8 @@ get_device_once(int *master, int *slave, char SlaveName[DEVICELEN], int nomesg,
if (!fail) return -1;
rb_raise(rb_eRuntimeError, "openpty() failed");
}
+ rb_update_max_fd(*master);
+ rb_update_max_fd(*slave);
if (no_mesg(SlaveName, nomesg) == -1) {
if (!fail) return -1;
rb_raise(rb_eRuntimeError, "can't chmod slave pty");
@@ -342,8 +348,11 @@ get_device_once(int *master, int *slave, char SlaveName[DEVICELEN], int nomesg,
if (!fail) return -1;
rb_raise(rb_eRuntimeError, "_getpty() failed");
}
+ rb_update_max_fd(*master);
*slave = open(name, O_RDWR);
+ /* error check? */
+ rb_update_max_fd(*slave);
strlcpy(SlaveName, name, DEVICELEN);
return 0;
@@ -357,13 +366,15 @@ get_device_once(int *master, int *slave, char SlaveName[DEVICELEN], int nomesg,
extern int grantpt(int);
if((masterfd = open("/dev/ptmx", O_RDWR, 0)) == -1) goto error;
+ rb_update_max_fd(masterfd);
s = signal(SIGCHLD, SIG_DFL);
if(grantpt(masterfd) == -1) goto error;
signal(SIGCHLD, s);
if(unlockpt(masterfd) == -1) goto error;
if((slavedevice = ptsname(masterfd)) == NULL) goto error;
if (no_mesg(slavedevice, nomesg) == -1) goto error;
if((slavefd = open(slavedevice, O_RDWR, 0)) == -1) goto error;
+ rb_update_max_fd(slavefd);
#if defined I_PUSH && !defined linux
if(ioctl(slavefd, I_PUSH, "ptem") == -1) goto error;
if(ioctl(slavefd, I_PUSH, "ldterm") == -1) goto error;
@@ -387,9 +398,11 @@ get_device_once(int *master, int *slave, char SlaveName[DEVICELEN], int nomesg,
for (p = deviceNo; *p != NULL; p++) {
snprintf(MasterName, sizeof MasterName, MasterDevice, *p);
if ((masterfd = open(MasterName,O_RDWR,0)) >= 0) {
+ rb_update_max_fd(masterfd);
*master = masterfd;
snprintf(SlaveName, DEVICELEN, SlaveDevice, *p);
if ((slavefd = open(SlaveName,O_RDWR,0)) >= 0) {
+ rb_update_max_fd(slavefd);
*slave = slavefd;
if (chown(SlaveName, getuid(), getgid()) != 0) goto error;
if (chmod(SlaveName, nomesg ? 0600 : 0622) != 0) goto error;
@@ -577,6 +590,7 @@ pty_getpty(int argc, VALUE *argv, VALUE self)
wfptr->fd = dup(info.fd);
if (wfptr->fd == -1)
rb_sys_fail("dup()");
+ rb_update_max_fd(wfptr->fd);
wfptr->pathv = rfptr->pathv;
res = rb_ary_new2(3);
View
2 ext/socket/ancdata.c
@@ -1384,6 +1384,7 @@ discard_cmsg(struct cmsghdr *cmh, char *msg_end)
int *end = (int *)((char *)cmh + cmh->cmsg_len);
while ((char *)fdp + sizeof(int) <= (char *)end &&
(char *)fdp + sizeof(int) <= msg_end) {
+ rb_update_max_fd(*fdp);
close(*fdp);
fdp++;
}
@@ -1426,6 +1427,7 @@ make_io_for_unix_rights(VALUE ctl, struct cmsghdr *cmh, char *msg_end)
VALUE io;
if (fstat(fd, &stbuf) == -1)
rb_raise(rb_eSocket, "invalid fd in SCM_RIGHTS");
+ rb_update_max_fd(fd);
if (S_ISSOCK(stbuf.st_mode))
io = rsock_init_sock(rb_obj_alloc(rb_cSocket), fd);
else
View
5 ext/socket/init.c
@@ -48,6 +48,7 @@ rsock_init_sock(VALUE sock, int fd)
if (fstat(fd, &sbuf) < 0)
rb_sys_fail(0);
+ rb_update_max_fd(fd);
if (!S_ISSOCK(sbuf.st_mode))
rb_raise(rb_eArgError, "not a socket file descriptor");
#else
@@ -250,6 +251,8 @@ rsock_socket(int domain, int type, int proto)
fd = socket(domain, type, proto);
}
}
+ if (0 <= fd)
+ rb_update_max_fd(fd);
return fd;
}
@@ -463,6 +466,7 @@ rsock_s_accept_nonblock(VALUE klass, rb_io_t *fptr, struct sockaddr *sockaddr, s
}
rb_sys_fail("accept(2)");
}
+ rb_update_max_fd(fd2);
make_fd_nonblock(fd2);
return rsock_init_sock(rb_obj_alloc(klass), fd2);
}
@@ -509,6 +513,7 @@ rsock_s_accept(VALUE klass, int fd, struct sockaddr *sockaddr, socklen_t *len)
}
rb_sys_fail(0);
}
+ rb_update_max_fd(fd2);
if (!klass) return INT2NUM(fd2);
return rsock_init_sock(rb_obj_alloc(klass), fd2);
}
View
2 ext/socket/socket.c
@@ -119,6 +119,8 @@ rsock_sock_s_socketpair(int argc, VALUE *argv, VALUE klass)
if (ret < 0) {
rb_sys_fail("socketpair(2)");
}
+ rb_update_max_fd(sp[0]);
+ rb_update_max_fd(sp[1]);
s1 = rsock_init_sock(rb_obj_alloc(klass), sp[0]);
s2 = rsock_init_sock(rb_obj_alloc(klass), sp[1]);
View
1 ext/socket/unixsocket.c
@@ -383,6 +383,7 @@ unix_recv_io(int argc, VALUE *argv, VALUE sock)
#if FD_PASSING_BY_MSG_CONTROL
memcpy(&fd, CMSG_DATA(&cmsg.hdr), sizeof(int));
#endif
+ rb_update_max_fd(fd);
if (klass == Qnil)
return INT2FIX(fd);
View
2 file.c
@@ -3914,6 +3914,7 @@ rb_file_s_truncate(VALUE klass, VALUE path, VALUE len)
if ((tmpfd = open(StringValueCStr(path), 0)) < 0) {
rb_sys_fail(RSTRING_PTR(path));
}
+ rb_update_max_fd(tmpfd);
if (chsize(tmpfd, pos) < 0) {
close(tmpfd);
rb_sys_fail(RSTRING_PTR(path));
@@ -5061,6 +5062,7 @@ file_load_ok(const char *path)
int ret = 1;
int fd = open(path, O_RDONLY);
if (fd == -1) return 0;
+ rb_update_max_fd(fd);
#if !defined DOSISH
{
struct stat st;
View
1 include/ruby/intern.h
@@ -484,6 +484,7 @@ void rb_close_before_exec(int lowfd, int maxhint, VALUE noclose_fds);
int rb_pipe(int *pipes);
int rb_reserved_fd_p(int fd);
#define RB_RESERVED_FD_P(fd) rb_reserved_fd_p(fd)
+void rb_update_max_fd(int fd);
/* marshal.c */
VALUE rb_marshal_dump(VALUE, VALUE);
VALUE rb_marshal_load(VALUE);
View
30 io.c
@@ -151,10 +151,11 @@ struct argf {
};
static int max_file_descriptor = NOFILE;
-#define UPDATE_MAXFD(fd) \
- do { \
- if (max_file_descriptor < (fd)) max_file_descriptor = (fd); \
- } while (0)
+void
+rb_update_max_fd(int fd)
+{
+ if (max_file_descriptor < fd) max_file_descriptor = fd;
+}
#define argf_of(obj) (*(struct argf *)DATA_PTR(obj))
#define ARGF argf_of(argf)
@@ -526,7 +527,7 @@ ruby_dup(int orig)
rb_sys_fail(0);
}
}
- UPDATE_MAXFD(fd);
+ rb_update_max_fd(fd);
return fd;
}
@@ -4591,7 +4592,11 @@ sysopen_func(void *ptr)
static inline int
rb_sysopen_internal(struct sysopen_struct *data)
{
- return (int)rb_thread_blocking_region(sysopen_func, data, RUBY_UBF_IO, 0);
+ int fd;
+ fd = (int)rb_thread_blocking_region(sysopen_func, data, RUBY_UBF_IO, 0);
+ if (0 <= fd)
+ rb_update_max_fd(fd);
+ return fd;
}
static int
@@ -4617,7 +4622,7 @@ rb_sysopen(VALUE fname, int oflags, mode_t perm)
rb_sys_fail(RSTRING_PTR(fname));
}
}
- UPDATE_MAXFD(fd);
+ rb_update_max_fd(fd);
return fd;
}
@@ -4910,8 +4915,8 @@ rb_pipe(int *pipes)
}
}
if (ret == 0) {
- UPDATE_MAXFD(pipes[0]);
- UPDATE_MAXFD(pipes[1]);
+ rb_update_max_fd(pipes[0]);
+ rb_update_max_fd(pipes[1]);
}
return ret;
}
@@ -5793,13 +5798,15 @@ io_reopen(VALUE io, VALUE nfile)
/* need to keep FILE objects of stdin, stdout and stderr */
if (dup2(fd2, fd) < 0)
rb_sys_fail_path(orig->pathv);
+ rb_update_max_fd(fd);
}
else {
fclose(fptr->stdio_file);
fptr->stdio_file = 0;
fptr->fd = -1;
if (dup2(fd2, fd) < 0)
rb_sys_fail_path(orig->pathv);
+ rb_update_max_fd(fd);
fptr->fd = fd;
}
rb_thread_fd_close(fd);
@@ -6383,6 +6390,7 @@ prep_io(int fd, int fmode, VALUE klass, const char *path)
fp->mode = fmode;
io_check_tty(fp);
if (path) fp->pathv = rb_obj_freeze(rb_str_new_cstr(path));
+ rb_update_max_fd(fd);
return io;
}
@@ -6535,7 +6543,7 @@ rb_io_initialize(int argc, VALUE *argv, VALUE io)
#else
if (fstat(fd, &st) == -1) rb_sys_fail(0);
#endif
- UPDATE_MAXFD(fd);
+ rb_update_max_fd(fd);
#if defined(HAVE_FCNTL) && defined(F_GETFL)
ofmode = rb_io_oflags_fmode(oflags);
if (NIL_P(vmode)) {
@@ -7690,7 +7698,7 @@ io_cntl(int fd, int cmd, long narg, int io_p)
retval = (int)rb_thread_io_blocking_region(nogvl_io_cntl, &arg, fd);
#if defined(F_DUPFD)
if (!io_p && retval != -1 && cmd == F_DUPFD) {
- UPDATE_MAXFD(retval);
+ rb_update_max_fd(retval);
}
#endif
View
11 process.c
@@ -1961,6 +1961,7 @@ save_redirect_fd(int fd, VALUE save, char *errmsg, size_t errmsg_buflen)
ERRMSG("dup");
return -1;
}
+ rb_update_max_fd(save_fd);
newary = rb_ary_entry(save, EXEC_OPTION_DUP2);
if (NIL_P(newary)) {
newary = hide_obj(rb_ary_new());
@@ -2077,6 +2078,7 @@ run_exec_dup2(VALUE ary, VALUE save, char *errmsg, size_t errmsg_buflen)
ERRMSG("dup2");
goto fail;
}
+ rb_update_max_fd(pairs[j].newfd);
pairs[j].oldfd = -1;
j = pairs[j].older_index;
if (j != -1)
@@ -2115,13 +2117,15 @@ run_exec_dup2(VALUE ary, VALUE save, char *errmsg, size_t errmsg_buflen)
ERRMSG("dup");
goto fail;
}
+ rb_update_max_fd(extra_fd);
}
else {
ret = redirect_dup2(pairs[i].oldfd, extra_fd);
if (ret == -1) {
ERRMSG("dup2");
goto fail;
}
+ rb_update_max_fd(extra_fd);
}
pairs[i].oldfd = extra_fd;
j = pairs[i].older_index;
@@ -2132,6 +2136,7 @@ run_exec_dup2(VALUE ary, VALUE save, char *errmsg, size_t errmsg_buflen)
ERRMSG("dup2");
goto fail;
}
+ rb_update_max_fd(ret);
pairs[j].oldfd = -1;
j = pairs[j].older_index;
}
@@ -2189,6 +2194,7 @@ run_exec_open(VALUE ary, VALUE save, char *errmsg, size_t errmsg_buflen)
ERRMSG("open");
return -1;
}
+ rb_update_max_fd(fd2);
while (i < RARRAY_LEN(ary) &&
(elt = RARRAY_PTR(ary)[i], RARRAY_PTR(elt)[1] == param)) {
fd = FIX2INT(RARRAY_PTR(elt)[0]);
@@ -2203,6 +2209,7 @@ run_exec_open(VALUE ary, VALUE save, char *errmsg, size_t errmsg_buflen)
ERRMSG("dup2");
return -1;
}
+ rb_update_max_fd(fd);
}
i++;
}
@@ -2235,6 +2242,7 @@ run_exec_dup2_child(VALUE ary, VALUE save, char *errmsg, size_t errmsg_buflen)
ERRMSG("dup2");
return -1;
}
+ rb_update_max_fd(newfd);
}
return 0;
}
@@ -2501,6 +2509,7 @@ move_fds_to_avoid_crash(int *fdp, int n, VALUE fds)
ret = fcntl(fdp[i], F_DUPFD, min);
if (ret == -1)
return -1;
+ rb_update_max_fd(ret);
close(fdp[i]);
fdp[i] = ret;
}
@@ -3583,6 +3592,7 @@ ruby_setsid(void)
if (ret == -1) return -1;
if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
+ rb_update_max_fd(fd);
ioctl(fd, TIOCNOTTY, NULL);
close(fd);
}
@@ -4873,6 +4883,7 @@ rb_daemon(int nochdir, int noclose)
err = chdir("/");
if (!noclose && (n = open("/dev/null", O_RDWR, 0)) != -1) {
+ rb_update_max_fd(n);
(void)dup2(n, 0);
(void)dup2(n, 1);
(void)dup2(n, 2);
View
1 random.c
@@ -512,6 +512,7 @@ fill_random_seed(unsigned int seed[DEFAULT_SEED_CNT])
|O_NOCTTY
#endif
)) >= 0) {
+ rb_update_max_fd(fd);
if (fstat(fd, &statbuf) == 0 && S_ISCHR(statbuf.st_mode)) {
if (read(fd, seed, DEFAULT_SEED_LEN) < DEFAULT_SEED_LEN) {
/* abandon */;
View
1 ruby.c
@@ -1527,6 +1527,7 @@ load_file_internal(VALUE arg)
if ((fd = open(fname, mode)) < 0) {
rb_load_fail(fname);
}
+ rb_update_max_fd(fd);
f = rb_io_fdopen(fd, mode, fname);
}
View
2 thread_pthread.c
@@ -1193,6 +1193,8 @@ rb_thread_create_timer_thread(void)
if (err != 0) {
rb_bug_errno("thread_timer: Failed to create communication pipe for timer thread", errno);
}
+ rb_update_max_fd(timer_thread_pipe[0]);
+ rb_update_max_fd(timer_thread_pipe[1]);
#if defined(HAVE_FCNTL) && defined(F_GETFL) && defined(F_SETFL)
{
int oflags;

0 comments on commit fdcac09

Please sign in to comment.