[前][次][番号順一覧][スレッド一覧]

ruby-changes:4689

From: ko1@a...
Date: Thu, 24 Apr 2008 23:49:13 +0900 (JST)
Subject: [ruby-changes:4689] akr - Ruby:r16183 (trunk): * include/ruby/intern.h (rb_env_clear): declared.

akr	2008-04-24 23:46:39 +0900 (Thu, 24 Apr 2008)

  New Revision: 16183

  Modified files:
    trunk/ChangeLog
    trunk/hash.c
    trunk/include/ruby/intern.h
    trunk/io.c
    trunk/process.c
    trunk/test/ruby/test_process.rb

  Log:
    * include/ruby/intern.h (rb_env_clear): declared.
      (rb_io_mode_modenum): declared.
      (rb_close_before_exec): declared.
      (struct rb_exec_arg): add options and redirect_fds field.
      (rb_check_argv): removed.
      (rb_exec_initarg): declared.
      (rb_exec_getargs): declared.
      (rb_exec_initarg2): declared.
      (rb_fork): add third argument: fds.
    
    * io.c (max_file_descriptor): new static variable to record maximum
      file descriptor ruby used.
      (UPDATE_MAXFD): new macro.
      (UPDATE_MAXFD_PIPE): new macro.
      (rb_io_mode_modenum): externed.
      (rb_sysopen): update max_file_descriptor.
      (rb_close_before_exec): new function.
      (popen_exec): redirection removed because it is done by extended
      spawn mechanism.
      (pipe_open): generate a hash for spawn options to specify
      redirections.
      (pipe_open_v): use rb_exec_getargs.
      (pipe_open_s): use rb_exec_getargs.
      (rb_io_initialize): update max_file_descriptor..
    
    * process.c (hide_obj): new function.
      (check_exec_redirect_fd): new function.
      (check_exec_redirect): new function.
      (check_exec_options_i): new function.
      (check_exec_fds): new function.
      (rb_check_exec_options): new function.
      (check_exec_env_i): new function.
      (rb_check_exec_env): new function.
      (rb_exec_getargs): new function.
      (rb_exec_initarg2): new function.
      (rb_exec_initarg): new function.
      (rb_f_exec): use rb_exec_initarg.
      (intcmp): new function.
      (run_exec_dup2): new function.
      (run_exec_close): new function.
      (run_exec_open): new function.
      (run_exec_pgroup): new function.
      (run_exec_rlimit): new function.
      (run_exec_options): new function.
      (rb_exec): call run_exec_options.
      (move_fds_to_avoid_crash): new function.
      (pipe_nocrash): new function.
      (rb_fork): use pipe_nocrash to avoid file descriptor conflicts.
      (rb_spawn): use rb_exec_initarg.
      (rlimit_resource_name2int): extracted from rlimit_resource_type.
      (rlimit_type_by_hname): new function.
      (rlimit_type_by_lname): new function.
      (rlimit_resource_type): use rlimit_type_by_hname.
      (proc_daemon): add fds argument for rb_fork.
    
    * hash.c (rb_env_clear): renamed from env_clear and externed.
    
    [ruby-dev:34086]
    


  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/hash.c?r1=16183&r2=16182&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/test/ruby/test_process.rb?r1=16183&r2=16182&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=16183&r2=16182&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/io.c?r1=16183&r2=16182&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/process.c?r1=16183&r2=16182&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/include/ruby/intern.h?r1=16183&r2=16182&diff_format=u

Index: include/ruby/intern.h
===================================================================
--- include/ruby/intern.h	(revision 16182)
+++ include/ruby/intern.h	(revision 16183)
@@ -353,6 +353,7 @@
 struct st_table *rb_hash_tbl(VALUE);
 int rb_path_check(const char*);
 int rb_env_path_tainted(void);
+VALUE rb_env_clear(void);
 /* io.c */
 #define rb_defout rb_stdout
 RUBY_EXTERN VALUE rb_fs;
@@ -377,6 +378,8 @@
 VALUE rb_gets(void);
 void rb_write_error(const char*);
 void rb_write_error2(const char*, long);
+int rb_io_mode_modenum(const char *mode);
+void rb_close_before_exec(int lowfd, int maxhint, VALUE noclose_fds);
 /* marshal.c */
 VALUE rb_marshal_dump(VALUE, VALUE);
 VALUE rb_marshal_load(VALUE);
@@ -444,12 +447,16 @@
     int argc;
     VALUE *argv;
     const char *prog;
+    VALUE options;
+    VALUE redirect_fds;
 };
 int rb_proc_exec_n(int, VALUE*, const char*);
 int rb_proc_exec(const char*);
-VALUE rb_check_argv(int, VALUE*);
+VALUE rb_exec_initarg(int argc, VALUE *argv, int accept_shell, struct rb_exec_arg *e);
+VALUE rb_exec_getargs(int *argc_p, VALUE **argv_p, int accept_shell, VALUE *env_ret, VALUE *opthash_ret);
+void rb_exec_initarg2(VALUE prog, int argc, VALUE *argv, VALUE env, VALUE opthash, struct rb_exec_arg *e);
 int rb_exec(const struct rb_exec_arg*);
-rb_pid_t rb_fork(int*, int (*)(void*), void*);
+rb_pid_t rb_fork(int*, int (*)(void*), void*, VALUE);
 VALUE rb_f_exec(int,VALUE*);
 rb_pid_t rb_waitpid(rb_pid_t pid, int *status, int flags);
 void rb_syswait(rb_pid_t pid);
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 16182)
+++ ChangeLog	(revision 16183)
@@ -1,3 +1,64 @@
+Thu Apr 24 23:25:17 2008  Tanaka Akira  <akr@f...>
+
+	* include/ruby/intern.h (rb_env_clear): declared.
+	  (rb_io_mode_modenum): declared.
+	  (rb_close_before_exec): declared.
+	  (struct rb_exec_arg): add options and redirect_fds field.
+	  (rb_check_argv): removed.
+	  (rb_exec_initarg): declared.
+	  (rb_exec_getargs): declared.
+	  (rb_exec_initarg2): declared.
+	  (rb_fork): add third argument: fds.
+
+	* io.c (max_file_descriptor): new static variable to record maximum
+	  file descriptor ruby used.
+	  (UPDATE_MAXFD): new macro.
+	  (UPDATE_MAXFD_PIPE): new macro.
+	  (rb_io_mode_modenum): externed.
+	  (rb_sysopen): update max_file_descriptor.
+	  (rb_close_before_exec): new function.
+	  (popen_exec): redirection removed because it is done by extended
+	  spawn mechanism.
+	  (pipe_open): generate a hash for spawn options to specify
+	  redirections.
+	  (pipe_open_v): use rb_exec_getargs.
+	  (pipe_open_s): use rb_exec_getargs.
+	  (rb_io_initialize): update max_file_descriptor..
+
+	* process.c (hide_obj): new function.
+	  (check_exec_redirect_fd): new function.
+	  (check_exec_redirect): new function.
+	  (check_exec_options_i): new function.
+	  (check_exec_fds): new function.
+	  (rb_check_exec_options): new function.
+	  (check_exec_env_i): new function.
+	  (rb_check_exec_env): new function.
+	  (rb_exec_getargs): new function.
+	  (rb_exec_initarg2): new function.
+	  (rb_exec_initarg): new function.
+	  (rb_f_exec): use rb_exec_initarg.
+	  (intcmp): new function.
+	  (run_exec_dup2): new function.
+	  (run_exec_close): new function.
+	  (run_exec_open): new function.
+	  (run_exec_pgroup): new function.
+	  (run_exec_rlimit): new function.
+	  (run_exec_options): new function.
+	  (rb_exec): call run_exec_options.
+	  (move_fds_to_avoid_crash): new function.
+	  (pipe_nocrash): new function.
+	  (rb_fork): use pipe_nocrash to avoid file descriptor conflicts.
+	  (rb_spawn): use rb_exec_initarg.
+	  (rlimit_resource_name2int): extracted from rlimit_resource_type.
+	  (rlimit_type_by_hname): new function.
+	  (rlimit_type_by_lname): new function.
+	  (rlimit_resource_type): use rlimit_type_by_hname.
+	  (proc_daemon): add fds argument for rb_fork.
+
+	* hash.c (rb_env_clear): renamed from env_clear and externed.
+
+	[ruby-dev:34086]
+
 Thu Apr 24 23:00:58 2008  Yusuke Endoh  <mame@t...>
 
 	* test/ruby/test_thread.rb: fix typos.
Index: io.c
===================================================================
--- io.c	(revision 16182)
+++ io.c	(revision 16183)
@@ -139,6 +139,18 @@
     rb_encoding *enc, *enc2;
 };
 
+static int max_file_descriptor = NOFILE;
+#define UPDATE_MAXFD(fd) \
+    do { \
+        if (max_file_descriptor < (fd)) max_file_descriptor = (fd); \
+    } while (0)
+#define UPDATE_MAXFD_PIPE(filedes) \
+    do { \
+        UPDATE_MAXFD((filedes)[0]); \
+        UPDATE_MAXFD((filedes)[1]); \
+    } while (0)
+
+
 #define argf_of(obj) (*(struct argf *)DATA_PTR(obj))
 #define ARGF argf_of(argf)
 
@@ -3228,7 +3240,7 @@
     return flags;
 }
 
-static int
+int
 rb_io_mode_modenum(const char *mode)
 {
     int flags = 0;
@@ -3387,6 +3399,7 @@
 	    rb_sys_fail(fname);
 	}
     }
+    UPDATE_MAXFD(fd);
     return fd;
 }
 
@@ -3629,26 +3642,41 @@
     }
 }
 
+void
+rb_close_before_exec(int lowfd, int maxhint, VALUE noclose_fds)
+{
+    int fd, ret;
+    int max = max_file_descriptor;
+    if (max < maxhint)
+        max = maxhint;
+    for (fd = lowfd; fd <= max; fd++) {
+        if (!NIL_P(noclose_fds) &&
+            RTEST(rb_hash_lookup(noclose_fds, INT2FIX(fd))))
+            continue;
+#ifdef FD_CLOEXEC
+	ret = fcntl(fd, F_GETFD);
+	if (ret != -1 && !(ret & FD_CLOEXEC)) {
+            fcntl(fd, F_SETFD, ret|FD_CLOEXEC);
+        }
+#else
+	close(fd);
+#endif
+    }
+}
+
 static int
 popen_exec(void *pp)
 {
     struct popen_arg *p = (struct popen_arg*)pp;
-    int fd;
 
     rb_thread_atfork();
-    popen_redirect(p);
-    for (fd = 3; fd < NOFILE; fd++) {
-#ifdef FD_CLOEXEC
-	if (fcntl(fd, F_GETFD) & FD_CLOEXEC) continue;
-#endif
-	close(fd);
-    }
     return rb_exec(&p->exec);
 }
 #endif
 
 static VALUE
-pipe_open(const char *cmd, int argc, VALUE *argv, const char *mode)
+pipe_open(VALUE prog, int argc, VALUE *argv,
+    VALUE env, VALUE opthash, const char *mode)
 {
     int modef = rb_io_mode_flags(mode);
     int pid = 0;
@@ -3659,6 +3687,7 @@
 #if defined(HAVE_FORK)
     int status;
     struct popen_arg arg;
+    VALUE env2 = 0;
 #elif defined(_WIN32)
     int openmode = rb_io_mode_modenum(mode);
     const char *exename = NULL;
@@ -3667,8 +3696,16 @@
     FILE *fp = 0;
     int fd = -1;
     int write_fd = -1;
+    const char *cmd = 0;
 
+    if (prog)
+        cmd = StringValueCStr(prog);
+
 #if defined(HAVE_FORK)
+    if (prog) {
+        env2 = rb_hash_new();
+        RBASIC(env2)->klass = 0;
+    }
     arg.modef = modef;
     arg.pair[0] = arg.pair[1] = -1;
     arg.write_pair[0] = arg.write_pair[1] = -1;
@@ -3676,6 +3713,7 @@
       case FMODE_READABLE|FMODE_WRITABLE:
         if (pipe(arg.write_pair) < 0)
             rb_sys_fail(cmd);
+        UPDATE_MAXFD_PIPE(arg.write_pair);
         if (pipe(arg.pair) < 0) {
             int e = errno;
             close(arg.write_pair[0]);
@@ -3683,29 +3721,47 @@
             errno = e;
             rb_sys_fail(cmd);
         }
+        UPDATE_MAXFD_PIPE(arg.pair);
+        if (env2) {
+            rb_hash_aset(env2, INT2FIX(0), INT2FIX(arg.write_pair[0]));
+            rb_hash_aset(env2, INT2FIX(1), INT2FIX(arg.pair[1]));
+        }
 	break;
       case FMODE_READABLE:
         if (pipe(arg.pair) < 0)
             rb_sys_fail(cmd);
+        UPDATE_MAXFD_PIPE(arg.pair);
+        if (env2)
+            rb_hash_aset(env2, INT2FIX(1), INT2FIX(arg.pair[1]));
 	break;
       case FMODE_WRITABLE:
         if (pipe(arg.pair) < 0)
             rb_sys_fail(cmd);
+        UPDATE_MAXFD_PIPE(arg.pair);
+        if (env2)
+            rb_hash_aset(env2, INT2FIX(0), INT2FIX(arg.pair[0]));
 	break;
       default:
         rb_sys_fail(cmd);
     }
-    if (cmd) {
-	arg.exec.argc = argc;
-	arg.exec.argv = argv;
-	arg.exec.prog = cmd;
-	pid = rb_fork(&status, popen_exec, &arg);
+    if (prog) {
+        VALUE close_others = ID2SYM(rb_intern("close_others"));
+        if (NIL_P(opthash) || !st_lookup(RHASH_TBL(opthash), close_others, 0))
+            rb_hash_aset(env2, close_others, Qtrue);
+        if (NIL_P(opthash))
+            opthash = env2;
+        else {
+            opthash = rb_assoc_new(env2, opthash);
+            RBASIC(opthash)->klass = 0;
+        }
+        rb_exec_initarg2(prog, argc, argv, env, opthash, &arg.exec);
+	pid = rb_fork(&status, popen_exec, &arg, arg.exec.redirect_fds);
     }
     else {
 	fflush(stdin);		/* is it really needed? */
 	rb_io_flush(rb_stdout);
 	rb_io_flush(rb_stderr);
-	pid = rb_fork(&status, 0, 0);
+	pid = rb_fork(&status, 0, 0, Qnil);
 	if (pid == 0) {		/* child */
 	    popen_redirect(&arg);
 	    rb_io_synchronized(RFILE(orig_stdout)->fptr);
@@ -3812,27 +3868,29 @@
 static VALUE
 pipe_open_v(int argc, VALUE *argv, const char *mode)
 {
-    VALUE prog = rb_check_argv(argc, argv);
-    const char *cmd;
-
-    if (!RB_GC_GUARD(prog)) prog = argv[0];
-    cmd = StringValueCStr(prog);
-    return pipe_open(cmd, argc, argv, mode);
+    VALUE prog, env=Qnil, opthash=Qnil;
+    prog = rb_exec_getargs(&argc, &argv, Qfalse, &env, &opthash);
+    return pipe_open(prog, argc, argv, env, opthash, mode);
 }
 
 static VALUE
 pipe_open_s(VALUE prog, const char *mode)
 {
-    const char *cmd = (rb_check_argv(1, &prog), RSTRING_PTR(prog));
+    const char *cmd = RSTRING_PTR(prog);
+    int argc = 1;
+    VALUE *argv = &prog;
+    VALUE env=Qnil, opthash=Qnil;
 
-    if (strcmp("-", cmd) == 0) {
+    if (RSTRING_LEN(prog) == 1 && cmd[0] == '-') {
 #if !defined(HAVE_FORK)
 	rb_raise(rb_eNotImpError,
 		 "fork() function is unimplemented on this machine");
 #endif
-	cmd = 0;
+        return pipe_open(0, 0, 0, Qnil, Qnil, mode);
     }
-    return pipe_open(cmd, 0, &prog, mode);
+
+    rb_exec_getargs(&argc, &argv, Qtrue, &env, &opthash);
+    return pipe_open(prog, 0, 0, Qnil, Qnil, mode);
 }
 
 /*
@@ -3845,7 +3903,9 @@
  *  <code>IO</code> object.  If _cmd_ is a +String+
  *  ``<code>-</code>'', then a new instance of Ruby is started as the
  *  subprocess.  If <i>cmd</i> is an +Array+ of +String+, then it will
- *  be used as the subprocess's +argv+ bypassing a shell.  The default
+ *  be used as the subprocess's +argv+ bypassing a shell.
+ *  The array can contains a hash at first for environments and
+ *  a hash at last for options similar to <code>spawn</code>.  The default
  *  mode for the new file object is ``r'', but <i>mode</i> may be set
  *  to any of the modes listed in the description for class IO.
  *
@@ -4873,6 +4933,7 @@
     orig = rb_io_check_io(fnum);
     if (NIL_P(orig)) {
 	fd = NUM2INT(fnum);
+        UPDATE_MAXFD(fd);
 	if (argc != 2) {
 #if defined(HAVE_FCNTL) && defined(F_GETFL)
 	    flags = fcntl(fd, F_GETFL);
@@ -6021,6 +6082,7 @@
     rb_scan_args(argc, argv, "02", &v1, &v2);
     if (pipe(pipes) == -1)
 	rb_sys_fail(0);
+    UPDATE_MAXFD_PIPE(pipes);
 
     args[0] = klass;
     args[1] = INT2NUM(pipes[0]);
Index: process.c
===================================================================
--- process.c	(revision 16182)
+++ process.c	(revision 16183)
@@ -13,6 +13,8 @@
 
 #include "ruby/ruby.h"
 #include "ruby/signal.h"
+#include "ruby/io.h"
+#include "ruby/util.h"
 #include "vm_core.h"
 
 #include <stdio.h>
@@ -116,6 +118,17 @@
 #endif
 #endif
 
+#if SIZEOF_RLIM_T == SIZEOF_INT
+# define RLIM2NUM(v) UINT2NUM(v)
+# define NUM2RLIM(v) NUM2UINT(v)
+#elif SIZEOF_RLIM_T == SIZEOF_LONG
+# define RLIM2NUM(v) ULONG2NUM(v)
+# define NUM2RLIM(v) NUM2ULONG(v)
+#elif SIZEOF_RLIM_T == SIZEOF_LONG_LONG
+# define RLIM2NUM(v) ULL2NUM(v)
+# define NUM2RLIM(v) NUM2ULL(v)
+#endif
+
 #define preserving_errno(stmts) \
 	do {int saved_errno = errno; stmts; errno = saved_errno;} while (0)
 
@@ -1202,7 +1215,353 @@
 #endif
 #endif
 
-VALUE
+static VALUE
+hide_obj(VALUE obj)
+{
+    RBASIC(obj)->klass = 0;
+    return obj;
+}
+
+enum {
+    EXEC_OPTION_PGROUP,
+    EXEC_OPTION_RLIMIT,
+    EXEC_OPTION_UNSETENV_OTHERS,
+    EXEC_OPTION_ENV,
+    EXEC_OPTION_CHDIR,
+    EXEC_OPTION_UMASK,
+    EXEC_OPTION_DUP2,
+    EXEC_OPTION_CLOSE,
+    EXEC_OPTION_OPEN,
+    EXEC_OPTION_CLOSE_OTHERS,
+};
+
+static VALUE
+check_exec_redirect_fd(VALUE v)
+{
+    VALUE tmp;
+    int fd;
+    if (FIXNUM_P(v)) {
+        fd = FIX2INT(v);
+    }
+    else if (!NIL_P(tmp = rb_check_convert_type(v, T_FILE, "IO", "to_io"))) {
+        rb_io_t *fptr;
+        GetOpenFile(tmp, fptr);
+        fd = fptr->fd;
+    }
+    else {
+        rb_raise(rb_eArgError, "wrong exec redirect");
+    }
+    if (fd < 0) {
+        rb_raise(rb_eArgError, "negative file descriptor");
+    }
+    return INT2FIX(fd);
+}
+
+static void
+check_exec_redirect(VALUE key, VALUE val, VALUE options)
+{
+    int index;
+    VALUE ary, param;
+    VALUE path, flags, perm;
+    ID id;
+
+    switch (TYPE(val)) {
+      case T_SYMBOL:
+        id = SYM2ID(val);
+        if (id == rb_intern("close")) {
+            index = EXEC_OPTION_CLOSE;
+            param = Qnil;
+        }
+        else {
+            rb_raise(rb_eArgError, "wrong exec redirect symbol: %s",
+                                   rb_id2name(id));
+        }
+        break;
+
+      case T_FILE:
+        val = check_exec_redirect_fd(val);
+        /* fall through */
+      case T_FIXNUM:
+        index = EXEC_OPTION_DUP2;
+        param = val;
+        break;
+
+      case T_ARRAY:
+        index = EXEC_OPTION_OPEN;
+        path = rb_ary_entry(val, 0);
+        FilePathValue(path);
+        flags = rb_ary_entry(val, 1);
+        if (NIL_P(flags))
+            flags = INT2NUM(O_RDONLY);
+        else if (TYPE(flags) == T_STRING)
+            flags = INT2NUM(rb_io_mode_modenum(StringValueCStr(flags)));
+        else
+            flags = rb_to_int(flags);
+        perm = rb_ary_entry(val, 2);
+        perm = NIL_P(perm) ? INT2FIX(0644) : rb_to_int(perm);
+        param = hide_obj(rb_ary_new3(3, hide_obj(rb_str_dup(path)),
+                                        flags, perm));
+        break;
+
+      case T_STRING:
+        index = EXEC_OPTION_OPEN;
+        path = val;
+        FilePathValue(path);
+        if ((FIXNUM_P(key) && (FIX2INT(key) == 1 || FIX2INT(key) == 2)) ||
+            key == rb_stdout || key == rb_stderr)
+            flags = INT2NUM(O_WRONLY|O_CREAT|O_TRUNC);
+        else
+            flags = INT2NUM(O_RDONLY);
+        perm = INT2FIX(0644);
+        param = hide_obj(rb_ary_new3(3, hide_obj(rb_str_dup(path)),
+                                        flags, perm));
+        break;
+
+      default:
+        rb_raise(rb_eArgError, "wrong exec redirect action");
+    }
+
+    ary = rb_ary_entry(options, index);
+    if (NIL_P(ary)) {
+        ary = hide_obj(rb_ary_new());
+        rb_ary_store(options, index, ary);
+    }
+    if (TYPE(key) != T_ARRAY) {
+        VALUE fd = check_exec_redirect_fd(key);
+        rb_ary_push(ary, hide_obj(rb_assoc_new(fd, param)));
+    }
+    else {
+        int i, n=0;
+        for (i = 0 ; i < RARRAY_LEN(key); i++) {
+            VALUE v = RARRAY_PTR(key)[i];
+            VALUE fd = check_exec_redirect_fd(v);
+            rb_ary_push(ary, hide_obj(rb_assoc_new(fd, param)));
+            n++;
+        }
+    }
+}
+
+#ifdef RLIM2NUM
+static int rlimit_type_by_lname(const char *name);
+#endif
+
+static int
+check_exec_options_i(st_data_t st_key, st_data_t st_val, st_data_t arg)
+{
+    VALUE key = (VALUE)st_key;
+    VALUE val = (VALUE)st_val;
+    VALUE options = (VALUE)arg;
+    ID id;
+#ifdef RLIM2NUM
+    int rtype;
+#endif
+
+    rb_secure(2);
+
+    switch (TYPE(key)) {
+      case T_SYMBOL:
+        id = SYM2ID(key);
+#ifdef HAVE_SETPGID
+        if (id == rb_intern("pgroup")) {
+            if (!NIL_P(rb_ary_entry(options, EXEC_OPTION_PGROUP))) {
+                rb_raise(rb_eArgError, "pgroup option specified twice");
+            }
+            if (!RTEST(val))
+                val = Qfalse;
+            else if (val == Qtrue)
+                val = INT2FIX(0);
+            else {
+                pid_t pgroup = NUM2PIDT(val);
+                if (pgroup < 0) {
+                    rb_raise(rb_eArgError, "negative process group ID : %ld", (long)pgroup);
+                }
+                val = PIDT2NUM(pgroup);
+            }
+            rb_ary_store(options, EXEC_OPTION_PGROUP, val);
+        }
+        else
+#endif
+#ifdef RLIM2NUM
+        if (strncmp("rlimit_", rb_id2name(id), 7) == 0 &&
+            (rtype = rlimit_type_by_lname(rb_id2name(id)+7)) != -1) {
+            VALUE ary = rb_ary_entry(options, EXEC_OPTION_RLIMIT);
+            VALUE tmp, softlim, hardlim;
+            if (NIL_P(ary)) {
+                ary = hide_obj(rb_ary_new());
+                rb_ary_store(options, EXEC_OPTION_RLIMIT, ary);
+            }
+            tmp = rb_check_array_type(val);
+            if (!NIL_P(tmp)) {
+                if (RARRAY_LEN(tmp) == 1)
+                    softlim = hardlim = rb_to_int(rb_ary_entry(tmp, 0));
+                else if (RARRAY_LEN(tmp) == 2) {
+                    softlim = rb_to_int(rb_ary_entry(tmp, 0));
+                    hardlim = rb_to_int(rb_ary_entry(tmp, 1));
+                }
+                else {
+                    rb_raise(rb_eArgError, "wrong exec rlimit option");
+                }
+            }
+            else {
+                softlim = hardlim = rb_to_int(val);
+            }
+            tmp = hide_obj(rb_ary_new3(3, INT2NUM(rtype), softlim, hardlim));
+            rb_ary_push(ary, tmp);
+        }
+        else
+#endif
+        if (id == rb_intern("unsetenv_others")) {
+            if (!NIL_P(rb_ary_entry(options, EXEC_OPTION_UNSETENV_OTHERS))) {
+                rb_raise(rb_eArgError, "unsetenv_others option specified twice");
+            }
+            val = RTEST(val) ? Qtrue : Qfalse;
+            rb_ary_store(options, EXEC_OPTION_UNSETENV_OTHERS, val);
+        }
+        else if (id == rb_intern("chdir")) {
+            if (!NIL_P(rb_ary_entry(options, EXEC_OPTION_CHDIR))) {
+                rb_raise(rb_eArgError, "chdir option specified twice");
+            }
+            FilePathValue(val);
+            rb_ary_store(options, EXEC_OPTION_CHDIR,
+                                  hide_obj(rb_str_dup(val)));
+        }
+        else if (id == rb_intern("umask")) {
+            mode_t cmask = NUM2LONG(val);
+            if (!NIL_P(rb_ary_entry(options, EXEC_OPTION_UMASK))) {
+                rb_raise(rb_eArgError, "umask option specified twice");
+            }
+            rb_ary_store(options, EXEC_OPTION_UMASK, LONG2NUM(cmask));
+        }
+        else if (id == rb_intern("close_others")) {
+            if (!NIL_P(rb_ary_entry(options, EXEC_OPTION_CLOSE_OTHERS))) {
+                rb_raise(rb_eArgError, "close_others option specified twice");
+            }
+            val = RTEST(val) ? Qtrue : Qfalse;
+            rb_ary_store(options, EXEC_OPTION_CLOSE_OTHERS, val);
+        }
+        else if (id == rb_intern("in")) {
+            key = INT2FIX(0);
+            goto redirect;
+        }
+        else if (id == rb_intern("out")) {
+            key = INT2FIX(1);
+            goto redirect;
+        }
+        else if (id == rb_intern("err")) {
+            key = INT2FIX(2);
+            goto redirect;
+        }
+        else {
+            rb_raise(rb_eArgError, "wrong exec option symbol: %s",
+                                   rb_id2name(id));
+        }
+        break;
+
+      case T_FIXNUM:
+      case T_FILE:
+      case T_ARRAY:
+redirect:
+        check_exec_redirect(key, val, options);
+        break;
+
+      default:
+        rb_raise(rb_eArgError, "wrong exec option");
+    }
+
+    return ST_CONTINUE;
+}
+
+static VALUE
+check_exec_fds(VALUE options)
+{
+    VALUE h = rb_hash_new();
+    VALUE ary;
+    int index, i;
+    int maxhint = -1;
+
+    for (index = EXEC_OPTION_DUP2; index <= EXEC_OPTION_OPEN; index++) {
+        ary = rb_ary_entry(options, index);
+        if (NIL_P(ary))
+            continue;
+        for (i = 0; i < RARRAY_LEN(ary); i++) {
+            VALUE elt = RARRAY_PTR(ary)[i];
+            int fd = FIX2INT(RARRAY_PTR(elt)[0]);
+            if (RTEST(rb_hash_lookup(h, INT2FIX(fd)))) {
+                rb_raise(rb_eArgError, "fd %d specified twice", fd);
+            }
+            rb_hash_aset(h, INT2FIX(fd), Qtrue);
+            if (maxhint < fd)
+                maxhint = fd;
+            if (index == EXEC_OPTION_DUP2) {
+                fd = FIX2INT(RARRAY_PTR(elt)[1]);
+                if (maxhint < fd)
+                    maxhint = fd;
+            }
+        }
+    }
+    if (RTEST(rb_ary_entry(options, EXEC_OPTION_CLOSE_OTHERS))) {
+        rb_ary_store(options, EXEC_OPTION_CLOSE_OTHERS, INT2FIX(maxhint));
+    }
+    return h;
+}
+
+static VALUE
+rb_check_exec_options(VALUE opthash, VALUE *fds)
+{
+    VALUE options, h;
+    int i;
+
+    options = hide_obj(rb_ary_new());
+
+    if (TYPE(opthash) == T_ARRAY) {
+        for (i = 0; i < RARRAY_LEN(opthash); i++) {
+            VALUE hash = RARRAY_PTR(opthash)[i];
+            st_foreach(RHASH_TBL(hash), check_exec_options_i, (st_data_t)options);
+        }
+    }
+    else {
+        st_foreach(RHASH_TBL(opthash), check_exec_options_i, (st_data_t)options);
+    }
+
+    h = check_exec_fds(options);
+    if (fds)
+        *fds = h;
+
+    return options;
+}
+
+static int
+check_exec_env_i(st_data_t st_key, st_data_t st_val, st_data_t arg)
+{
+    VALUE key = (VALUE)st_key;
+    VALUE val = (VALUE)st_val;
+    VALUE env = (VALUE)arg;
+    char *k;
+
+    k = StringValueCStr(key);
+    if (strchr(k, '='))
+        rb_raise(rb_eArgError, "environment name contains a equal : %s", k);
+
+    if (!NIL_P(val))
+        StringValueCStr(val);
+
+    rb_ary_push(env, hide_obj(rb_assoc_new(key, val)));
+
+    return ST_CONTINUE;
+}
+
+static VALUE
+rb_check_exec_env(hash)
+{
+    VALUE env;
+
+    env = hide_obj(rb_ary_new());
+    st_foreach(RHASH_TBL(hash), check_exec_env_i, (st_data_t)env);
+
+    return env;
+}
+
+static VALUE
 rb_check_argv(int argc, VALUE *argv)
 {
     VALUE tmp, prog;
@@ -1235,16 +1594,83 @@
     return prog;
 }
 
+VALUE
+rb_exec_getargs(int *argc_p, VALUE **argv_p, int accept_shell, VALUE *env_ret, VALUE *options_ret)
+{
+    VALUE hash, prog;
+
+    if (0 < *argc_p) {
+        hash = rb_check_convert_type((*argv_p)[*argc_p-1], T_HASH, "Hash", "to_hash");
+        if (!NIL_P(hash)) {
+            *options_ret = hash;
+            (*argc_p)--;
+        }
+    }
+
+    if (0 < *argc_p) {
+        hash = rb_check_convert_type((*argv_p)[0], T_HASH, "Hash", "to_hash");
+        if (!NIL_P(hash)) {
+            *env_ret = hash;
+            (*argc_p)--;
+            (*argv_p)++;
+        }
+    }
+    prog = rb_check_argv(*argc_p, *argv_p);
+    if (!prog) {
+        prog = (*argv_p)[0];
+        if (accept_shell && *argc_p == 1) {
+            *argc_p = 0;
+            *argv_p = 0;
+        }
+    }
+    return prog;
+}
+
+void
+rb_exec_initarg2(VALUE prog, int argc, VALUE *argv,
+    VALUE env, VALUE opthash, struct rb_exec_arg *e)
+{
+    VALUE options=Qnil, fds=Qnil;
+
+    MEMZERO(e, struct rb_exec_arg, 1);
+
+    if (!NIL_P(opthash)) {
+        options = rb_check_exec_options(opthash, &fds);
+    }
+    if (!NIL_P(env)) {
+        env = rb_check_exec_env(env);
+        if (NIL_P(options))
+            options = hide_obj(rb_ary_new());
+        rb_ary_store(options, EXEC_OPTION_ENV, env);
+    }
+
+    e->argc = argc;
+    e->argv = argv;
+    e->prog = prog ? RSTRING_PTR(prog) : 0;
+    e->options = options;
+    e->redirect_fds = fds;
+}
+
+
+VALUE
+rb_exec_initarg(int argc, VALUE *argv, int accept_shell, struct rb_exec_arg *e)
+{
+    VALUE prog, env=Qnil, opthash=Qnil;
+    prog = rb_exec_getargs(&argc, &argv, accept_shell, &env, &opthash);
+    rb_exec_initarg2(prog, argc, argv, env, opthash, e);
+    return prog;
+}
+
 /*
  *  call-seq:
- *     exec(command [, arg, ...])
+ *     exec([env,] command [, arg, ...] [,options])
  *
  *  Replaces the current process by running the given external _command_.
- *  If +exec+ is given a single argument, that argument is
+ *  If optional arguments, sequence of +arg+, are not given, that argument is
  *  taken as a line that is subject to shell expansion before being
- *  executed. If multiple arguments are given, the second and subsequent
- *  arguments are passed as parameters to _command_ with no shell
- *  expansion. If the first argument is a two-element array, the first
+ *  executed. If one or more +arg+ given, they
+ *  are passed as parameters to _command_ with no shell
+ *  expansion. If +command+ is a two-element array, the first
  *  element is the command to be executed, and the second argument is
  *  used as the <code>argv[0]</code> value, which may show up in process
  *  listings. In MSDOS environments, the command is executed in a
@@ -1252,6 +1678,10 @@
  *  used, so the running command may inherit some of the environment of
  *  the original program (including open file descriptors).
  *
+ *  The hash arguments, env and options, are same as
+ *  <code>system</code> and <code>spawn</code>.
+ *  See <code>spawn</code> for details.
+ *
  *  Raises SystemCallError if the _command_ couldn't execute (typically
  *  <code>Errno::ENOENT</code> when it was not found).
  *
@@ -1267,24 +1697,366 @@
 rb_f_exec(int argc, VALUE *argv)
 {
     struct rb_exec_arg e;
-    VALUE prog;
 
-    prog = rb_check_argv(argc, argv);
-    if (!prog && argc == 1) {
-	e.argc = 0;
-	e.argv = 0;
-	e.prog = RSTRING_PTR(argv[0]);
-    }
-    else {
-	e.argc = argc;
-	e.argv = argv;
-	e.prog = prog ? RSTRING_PTR(prog) : 0;
-    }
+    rb_exec_initarg(argc, argv, Qtrue, &e);
+
     rb_exec(&e);
     rb_sys_fail(e.prog);
     return Qnil;		/* dummy */
 }
 
+/*#define DEBUG_REDIRECT*/
+#if defined(DEBUG_REDIRECT)
+
+#include <stdarg.h>
+
+static void
+ttyprintf(const char *fmt, ...)
+{
+    va_list ap;
+    FILE *tty;
+    int save = errno;
+    tty = fopen("/dev/tty", "w");
+    if (!tty)
+        return;
+
+    va_start(ap, fmt);
+    vfprintf(tty, fmt, ap);
+    va_end(ap);
+    fclose(tty);
+    errno = save;
+}
+
+static int
+redirect_dup(int oldfd)
+{
+    int ret;
+    ret = dup(oldfd);
+    ttyprintf("dup(%d) => %d\n", oldfd, ret);
+    return ret;
+}
+
+static int
+redirect_dup2(int oldfd, int newfd)
+{
+    int ret;
+    ret = dup2(oldfd, newfd);
+    ttyprintf("dup2(%d, %d)\n", oldfd, newfd);
+    return ret;
+}
+
+static int
+redirect_close(int fd)
+{
+    int ret;
+    ret = close(fd);
+    ttyprintf("close(%d)\n", fd);
+    return ret;
+}
+
+static int
+redirect_open(const char *pathname, int flags, mode_t perm)
+{
+    int ret;
+    ret = open(pathname, flags, perm);
+    ttyprintf("open(\"%s\", 0x%x, 0%o) => %d\n", pathname, flags, perm, ret);
+    return ret;
+}
+
+#else
+#define redirect_dup(oldfd) dup(oldfd)
+#define redirect_dup2(oldfd, newfd) dup2(oldfd, newfd)
+#define redirect_close(fd) close(fd)
+#define redirect_open(pathname, flags, perm) open(pathname, flags, perm)
+#endif
+
+static int
+intcmp(const void *a, const void *b)
+{
+    return *(int*)a - *(int*)b;
+}
+
+static int
+run_exec_dup2(VALUE ary)
+{
+    int n, i;
+    int ret;
+    int extra_fd = -1;
+    struct fd_pair {
+        int oldfd;
+        int newfd;
+        int older_index;
+        int num_newer;
+    } *pairs = 0;
+
+    n = RARRAY_LEN(ary);
+    pairs = ALLOC_N(struct fd_pair, n);
+
+    /* initialize oldfd and newfd: O(n) */
+    for (i = 0; i < n; i++) {
+        VALUE elt = RARRAY_PTR(ary)[i];
+        pairs[i].oldfd = FIX2INT(RARRAY_PTR(elt)[1]);
+        pairs[i].newfd = FIX2INT(RARRAY_PTR(elt)[0]); /* unique */
+        pairs[i].older_index = -1;
+    }
+
+    /* sort the table by oldfd: O(n log n) */
+    qsort(pairs, n, sizeof(struct fd_pair), intcmp);
+
+    /* initialize older_index and num_newer: O(n log n) */
+    for (i = 0; i < n; i++) {
+        int newfd = pairs[i].newfd;
+        struct fd_pair key, *found;
+        key.oldfd = newfd;
+        found = bsearch(&key, pairs, n, sizeof(struct fd_pair), intcmp);
+        pairs[i].num_newer = 0;
+        if (found) {
+            while (pairs < found && (found-1)->oldfd == newfd)
+                found--;
+            while (found < pairs+n && found->oldfd == newfd) {
+                pairs[i].num_newer++;
+                found->older_index = i;
+                found++;
+            }
+        }
+    }
+
+    /* non-cyclic redirection: O(n) */
+    for (i = 0; i < n; i++) {
+        int j = i;
+        while (j != -1 && pairs[j].oldfd != -1 && pairs[j].num_newer == 0) {
+            ret = redirect_dup2(pairs[j].oldfd, pairs[j].newfd);
+            if (ret == -1)
+                goto fail;
+            pairs[j].oldfd = -1;
+            j = pairs[j].older_index;
+            if (j != -1)
+                pairs[j].num_newer--;
+        }
+    }
+
+    /* cyclic redirection: O(n) */
+    for (i = 0; i < n; i++) {
+        int j;
+        if (pairs[i].oldfd == -1)
+            continue;
+        if (pairs[i].oldfd == pairs[i].newfd) { /* self cycle */
+            int fd = pairs[i].oldfd;
+            ret = fcntl(fd, F_GETFD);
+            if (ret == -1)
+                goto fail;
+            if (ret & FD_CLOEXEC) {
+                ret &= ~FD_CLOEXEC;
+                ret = fcntl(fd, F_SETFD, ret);
+                if (ret == -1)
+                    goto fail;
+            }
+            pairs[i].oldfd = -1;
+            continue;
+        }
+        if (extra_fd == -1) {
+            extra_fd = redirect_dup(pairs[i].oldfd);
+            if (extra_fd == -1)
+                goto fail;
+        }
+        else {
+            ret = redirect_dup2(pairs[i].oldfd, extra_fd);
+            if (ret == -1)
+                goto fail;
+        }
+        pairs[i].oldfd = extra_fd;
+        j = pairs[i].older_index;
+        pairs[i].older_index = -1;
+        while (j != -1) {
+            ret = redirect_dup2(pairs[j].oldfd, pairs[j].newfd);
+            if (ret == -1)
+                goto fail;
+            pairs[j].oldfd = -1;
+            j = pairs[j].older_index;
+        }
+    }
+    if (extra_fd != -1) {
+        ret = redirect_close(extra_fd);
+        if (ret == -1)
+            goto fail;
+    }
+
+    return 0;
+
+fail:
+    if (pairs)
+        xfree(pairs);
+    return -1;
+}
+
+static int
+run_exec_close(VALUE ary)
+{
+    int i, ret;
+
+    for (i = 0; i < RARRAY_LEN(ary); i++) {
+        VALUE elt = RARRAY_PTR(ary)[i];
+        int fd = FIX2INT(RARRAY_PTR(elt)[0]);
+        ret = redirect_close(fd);
+        if (ret == -1)
+            return -1;
+    }
+    return 0;
+}
+
+static int
+run_exec_open(VALUE ary)
+{
+    int i, ret;
+
+    for (i = 0; i < RARRAY_LEN(ary);) {
+        VALUE elt = RARRAY_PTR(ary)[i];
+        int fd = FIX2INT(RARRAY_PTR(elt)[0]);
+        VALUE param = RARRAY_PTR(elt)[1];
+        char *path = RSTRING_PTR(RARRAY_PTR(param)[0]);
+        int flags = NUM2INT(RARRAY_PTR(param)[1]);
+        int perm = NUM2INT(RARRAY_PTR(param)[2]);
+        int need_close = 1;
+        int fd2 = redirect_open(path, flags, perm);
+        if (fd2 == -1) return -1;
+        while (i < RARRAY_LEN(ary) &&
+               (elt = RARRAY_PTR(ary)[i], RARRAY_PTR(elt)[1] == param)) {
+            fd = FIX2INT(RARRAY_PTR(elt)[0]);
+            if (fd == fd2) {
+                need_close = 0;
+            }
+            else {
+                ret = redirect_dup2(fd2, fd);
+                if (ret == -1) return -1;
+            }
+            i++;
+        }
+        if (need_close) {
+            ret = redirect_close(fd2);
+            if (ret == -1) return -1;
+        }
+    }
+    return 0;
+}
+
+#ifdef HAVE_SETPGID
+static int
+run_exec_pgroup(VALUE obj)
+{
+    /*
+     * If FD_CLOEXEC is available, rb_fork waits the child's execve.
+     * So setpgid is done in the child when rb_fork is returned in the parent.
+     * No race condition, even without setpgid from the parent.
+     * (Is there an environment which has setpgid but FD_CLOEXEC?)
+     */ 
+    pid_t pgroup = NUM2PIDT(obj);
+    if (pgroup == 0) {
+        pgroup = getpid();
+    }
+    return setpgid(getpid(), pgroup);
+}
+#endif
+
+#ifdef RLIM2NUM
+static int
+run_exec_rlimit(VALUE ary)
+{
+    int i;
+    for (i = 0; i < RARRAY_LEN(ary); i++) {
+        VALUE elt = RARRAY_PTR(ary)[i];
+        int rtype = NUM2INT(RARRAY_PTR(elt)[0]);
+        struct rlimit rlim;
+        rlim.rlim_cur = NUM2RLIM(RARRAY_PTR(elt)[1]);
+        rlim.rlim_max = NUM2RLIM(RARRAY_PTR(elt)[2]);
+        if (setrlimit(rtype, &rlim) == -1)
+            return -1;
+    }
+    return 0;
+}
+#endif
+
+static int
+run_exec_options(const struct rb_exec_arg *e)
+{
+    VALUE options = e->options;
+    VALUE obj;
+
+    if (!RTEST(options))
+        return 0;
+
+#ifdef HAVE_SETPGID
+    obj = rb_ary_entry(options, EXEC_OPTION_PGROUP);
+    if (RTEST(obj)) {
+        if (run_exec_pgroup(obj) == -1)
+            return -1;
+    }
+#endif
+
+#ifdef RLIM2NUM
+    obj = rb_ary_entry(options, EXEC_OPTION_RLIMIT);
+    if (!NIL_P(obj)) {
+        if (run_exec_rlimit(obj) == -1)
+            return -1;
+    }
+#endif
+
+    obj = rb_ary_entry(options, EXEC_OPTION_UNSETENV_OTHERS);
+    if (RTEST(obj)) {
+        rb_env_clear();
+    }
+
+    obj = rb_ary_entry(options, EXEC_OPTION_ENV);
+    if (!NIL_P(obj)) {
+        int i;
+        for (i = 0; i < RARRAY_LEN(obj); i++) {
+            VALUE pair = RARRAY_PTR(obj)[i];
+            VALUE key = RARRAY_PTR(pair)[0];
+            VALUE val = RARRAY_PTR(pair)[1];
+            if (NIL_P(val))
+                ruby_setenv(StringValueCStr(key), 0);
+            else
+                ruby_setenv(StringValueCStr(key), StringValueCStr(val));
+        }
+    }
+
+    obj = rb_ary_entry(options, EXEC_OPTION_CHDIR);
+    if (!NIL_P(obj)) {
+        if (chdir(RSTRING_PTR(obj)) == -1)
+            return -1;
+    }
+
+    obj = rb_ary_entry(options, EXEC_OPTION_UMASK);
+    if (!NIL_P(obj)) {
+        mode_t mask = NUM2LONG(obj);
+        umask(mask); /* never fail */
+    }
+
+    obj = rb_ary_entry(options, EXEC_OPTION_DUP2);
+    if (!NIL_P(obj)) {
+        if (run_exec_dup2(obj) == -1)
+            return -1;
+    }
+
+    obj = rb_ary_entry(options, EXEC_OPTION_CLOSE);
+    if (!NIL_P(obj)) {
+        if (run_exec_close(obj) == -1)
+            return -1;
+    }
+
+    obj = rb_ary_entry(options, EXEC_OPTION_CLOSE_OTHERS);
+    if (RTEST(obj)) {
+        rb_close_before_exec(3, FIX2INT(obj), e->redirect_fds);
+    }
+
+    obj = rb_ary_entry(options, EXEC_OPTION_OPEN);
+    if (!NIL_P(obj)) {
+        if (run_exec_open(obj) == -1)
+            return -1;
+    }
+
+    return 0;
+}
+
 int
 rb_exec(const struct rb_exec_arg *e)
 {
@@ -1292,6 +2064,10 @@
     VALUE *argv = e->argv;
     const char *prog = e->prog;
 
+    if (run_exec_options(e) < 0) {
+        return -1;
+    }
+
     if (argc == 0) {
 	rb_proc_exec(prog);
     }
@@ -1328,6 +2104,47 @@
 #endif
 #endif
 
+static int
+move_fds_to_avoid_crash(int *fdp, int n, VALUE fds)
+{
+    long min = 0;
+    int i;
+    for (i = 0; i < n; i++) {
+        int ret;
+        while (RTEST(rb_hash_lookup(fds, INT2FIX(fdp[i])))) {
+            if (min <= fdp[i])
+                min = fdp[i]+1;
+            while (RTEST(rb_hash_lookup(fds, INT2FIX(min))))
+                min++;
+            ret = fcntl(fdp[i], F_DUPFD, min);
+            if (ret == -1)
+                return -1;
+            close(fdp[i]);
+            fdp[i] = ret;
+        }
+    }
+    return 0;
+}
+
+static int
+pipe_nocrash(int filedes[2], VALUE fds)
+{
+    int ret;
+    ret = pipe(filedes);
+    if (ret == -1)
+        return -1;
+    if (RTEST(fds)) {
+        int save = errno;
+        if (move_fds_to_avoid_crash(filedes, 2, fds) == -1) {
+            close(filedes[0]);
+            close(filedes[1]);
+            return -1;
+        }
+        errno = save;
+    }
+    return ret;
+}
+
 /*
  * Forks child process, and returns the process ID in the parent
  * process.
@@ -1346,10 +2163,13 @@
  * returns -1 in the parent process.  On the other platforms, just
  * returns pid.
  *
+ * If fds is not Qnil, internal pipe for the errno propagation is
+ * arranged to avoid conflicts of the hash keys in +fds+.
+ *
  * +chfunc+ must not raise any exceptions.
  */
 rb_pid_t
-rb_fork(int *status, int (*chfunc)(void*), void *charg)
+rb_fork(int *status, int (*chfunc)(void*), void *charg, VALUE fds)
 {
     rb_pid_t pid;
     int err, state = 0;
@@ -1370,7 +2190,7 @@
 
 #ifdef FD_CLOEXEC
     if (chfunc) {
-	if (pipe(ep)) return -1;
+	if (pipe_nocrash(ep, fds)) return -1;
 	if (fcntl(ep[1], F_SETFD, FD_CLOEXEC)) {
 	    preserving_errno((close(ep[0]), close(ep[1])));
 	    return -1;
@@ -1473,7 +2293,7 @@
 
     rb_secure(2);
 
-    switch (pid = rb_fork(0, 0, 0)) {
+    switch (pid = rb_fork(0, 0, 0, Qnil)) {
       case 0:
 #ifdef linux
 	after_exec();
@@ -1715,22 +2535,13 @@
 {
     rb_pid_t status;
     VALUE prog;
+    struct rb_exec_arg earg;
 
-    prog = rb_check_argv(argc, argv);
+    prog = rb_exec_initarg(argc, argv, Qtrue, &earg);
 
-    if (!prog && argc == 1) {
-	--argc;
-	prog = *argv++;
-    }
 #if defined HAVE_FORK
-    {
-	struct rb_exec_arg earg;
-	earg.argc = argc;
-	earg.argv = argv;
-	earg.prog = prog ? RSTRING_PTR(prog) : 0;
-	status = rb_fork(&status, rb_exec_atfork, &earg);
-	if (prog && argc) argv[0] = prog;
-    }
+    status = rb_fork(&status, rb_exec_atfork, &earg, earg.redirect_fds);
+    if (prog && earg.argc) earg.argv[0] = prog;
 #elif defined HAVE_SPAWNV
     if (!argc) {
 	status = proc_spawn(RSTRING_PTR(prog));
@@ -1754,7 +2565,7 @@
 
 /*
  *  call-seq:
- *     system(cmd [, arg, ...])    => true or false
+ *     system([env,] cmd [, arg, ...] [,options])    => true, false or nil
  *
  *  Executes _cmd_ in a subshell, returning +true+ if the command
  *  gives zero exit status, +false+ for non zero exit status. Returns
@@ -1762,6 +2573,10 @@
  *  <code>$?</code>. The arguments are processed in the same way as
  *  for <code>Kernel::exec</code>.
  *
+ *  The hash arguments, env and options, are same as
+ *  <code>exec</code> and <code>spawn</code>.
+ *  See <code>spawn</code> for details.
+ *
  *     system("echo *")
  *     system("echo", "*")
  *
@@ -1804,10 +2619,137 @@
 
 /*
  *  call-seq:
- *     spawn(cmd [, arg, ...])     => pid
+ *     spawn([env,] cmd [, arg, ...] [,options])     => pid
  *
  *  Similar to <code>Kernel::system</code> except for not waiting for
  *  end of _cmd_, but returns its <i>pid</i>.
+ *
+ *  If a hash is given as +env+, the environment is
+ *  updated by +env+ before <code>exec(2)</code> in the child process.
+ *
+ *  If a hash is given as +options+,
+ *  it specifies
+ *  process group,
+ *  resource limit, 
+ *  current directory,
+ *  umask and
+ *  redirects for the child process.
+ *  Also, it can be specified to clear environment variables.
+ *
+ *  The <code>:unsetenv_others</code> key in +options+ specifies
+ *  to clear environment variables, other than specified by +env+.
+ *
+ *    pid = spawn(command, :unsetenv_others=>true) # no environment variable
+ *    pid = spawn({"FOO"=>"BAR"}, command, :unsetenv_others=>true) # FOO only
+ *
+ *  The <code>:pgroup</code> key in +options+ specifies a process group.
+ *  The corresponding value should be true, zero or positive integer.
+ *  true and zero means the process should be a process leader.
+ *  Other values specifies a process group to be belongs.
+ *
+ *    pid = spawn(command, :pgroup=>true) # process leader
+ *    pid = spawn(command, :pgroup=>10) # belongs to the process group 10
+ *
+ *  The <code>:rlimit_</code><em>foo</em> key specifies a resource limit.
+ *  <em>foo</em> should be one of resource types such as <code>core</code>
+ *  The corresponding value should be an integer or an array which have one or
+ *  two integers: same as cur_limit and max_limit arguments for
+ *  Process.setrlimit.
+ *
+ *    pid = spawn(command, :rlimit_core=>0) # never dump core.
+ *    cur, max = Process.getrlimit(:CORE)
+ *    pid = spawn(command, :rlimit_core=>[0,max]) # disable core temporary.
+ *    pid = spawn(command, :rlimit_core=>max) # enable core dump
+ *
+ *  The <code>:chdir</code> key in +options+ specifies the current directory.
+ *
+ *    pid = spawn(command, :chdir=>"/var/tmp")
+ *
+ *  The <code>:umask</code> key in +options+ specifies the umask.
+ *
+ *    pid = spawn(command, :umask=>077)
+ *
+ *  The :in, :out, :err, a fixnum, an IO and an array key specifies a redirect.
+ *  The redirection maps a file descriptor in the child process.
+ *
+ *  For example, stderr can be merged into stdout:
+ *
+ *    pid = spawn(command, :err=>:out)
+ *    pid = spawn(command, STDERR=>STDOUT)
+ *    pid = spawn(command, 2=>1)
+ *
+ *  The hash keys specifies a file descriptor
+ *  in the child process started by <code>spawn</code>.
+ *  :err, STDERR and 2 specifies the standard error stream.
+ *
+ *  The hash values specifies a file descriptor
+ *  in the parent process which invokes <code>spawn</code>.
+ *  :out, STDOUT and 1 specifies the standard output stream.
+ *
+ *  The standard output in the child process is not specified.
+ *  So it is inherited from the parent process.
+ *
+ *  The standard input stream can be specifed by :in, STDIN and 0.
+ *  
+ *  A filename can be specified as a hash value.
+ *
+ *    pid = spawn(command, STDIN=>"/dev/null") # read mode
+ *    pid = spawn(command, STDOUT=>"/dev/null") # write mode
+ *    pid = spawn(command, STDERR=>"log") # write mode
+ *    pid = spawn(command, 3=>"/dev/null") # read mode
+ *
+ *  For standard output and standard error,
+ *  it is opened in write mode.
+ *  Otherwise read mode is used.
+ *
+ *  For specifying flags and permission of file creation explicitly,
+ *  an array is used instead.
+ *
+ *    pid = spawn(command, STDIN=>["file"]) # read mode is assumed
+ *    pid = spawn(command, STDIN=>["file", "r"])
+ *    pid = spawn(command, STDOUT=>["log", "w"]) # 0644 assumed
+ *    pid = spawn(command, STDOUT=>["log", "w", 0600])
+ *    pid = spawn(command, STDOUT=>["log", File::WRONLY|File::EXCL|File::CREAT, 0600])
+ *
+ *  The array specifies a filename, flags and permission.
+ *  The flags can be a string or an integer.
+ *  If the flags is ommitted or nil, File::RDONLY is assumed.
+ *  The permission should be an integer.
+ *  If the permission is ommitted or nil, 0644 is assumed.
+ *
+ *  If an array of IOs and integers are specified as a hash key,
+ *  all the elemetns are redirected.
+ *
+ *    # standard output and standard error is redirected to log file.
+ *    pid = spawn(command, [STDOUT, STDERR]=>["log", "w"])
+ *
+ *  It is possible to specify a file descriptor to close using
+ *  <code>:close</code>.
+ *
+ *    # similar to IO.popen
+ *    r, w = IO.pipe
+ *    pid = spawn(command, STDOUT=>w, r=>:close, w=>:close)
+ *    w.close
+ *
+ *  Also, all non-standard unspecified descriptors can be closed by :close_others option.
+ *  The "standard" descriptors are 0, 1 and 2.
+ *  (Current implementation closes decriptors less than some constant, such as 256.)
+ *
+ *    # more similar to IO.popen
+ *    r, w = IO.pipe
+ *    pid = spawn(command, STDOUT=>w, :close_others=>true)
+ *    w.close
+ *
+ *  It is also possible to exchange file descriptors.
+ *
+ *    pid = spawn(command, STDOUT=>STDERR, STDERR=>STDOUT)
+ *
+ *  The hash keys specify file descriptors in the child process.
+ *  The hash values specifies file descriptors in the parent process.
+ *  So the above specifies exchanging STDOUT and STDERR.
+ *  Internally, +spawn+ uses an extra file descriptor to resolve such cyclic
+ *  file descriptor mapping.
+ *
  */
 
 static VALUE
@@ -2091,42 +3033,22 @@
 #endif
 }
 
-#if SIZEOF_RLIM_T == SIZEOF_INT
-# define RLIM2NUM(v) UINT2NUM(v)
-# define NUM2RLIM(v) NUM2UINT(v)
-#elif SIZEOF_RLIM_T == SIZEOF_LONG
-# define RLIM2NUM(v) ULONG2NUM(v)
-# define NUM2RLIM(v) NUM2ULONG(v)
-#elif SIZEOF_RLIM_T == SIZEOF_LONG_LONG
-# define RLIM2NUM(v) ULL2NUM(v)
-# define NUM2RLIM(v) NUM2ULL(v)
-#endif
-
 #if defined(RLIM2NUM)
 static int
-rlimit_resource_type(VALUE rtype)
+rlimit_resource_name2int(const char *name, int casetype)
 {
-    const char *name;
-    VALUE v;
-
-    switch (TYPE(rtype)) {
-      case T_SYMBOL:
-        name = rb_id2name(SYM2ID(rtype));
-        break;
-
-      default:
-        v = rb_check_string_type(rtype);
-        if (!NIL_P(v)) {
-            rtype = v;
-      case T_STRING:
-            name = StringValueCStr(rtype);
-            break;
+    size_t len = strlen(name);
+    if (16 < len) return -1;
+    if (casetype == 1) {
+        int i;
+        char *name2 = ALLOCA_N(char, len+1);
+        for (i = 0; i < len; i++) {
+            if (!ISLOWER(name[i]))
+                return -1;
+            name2[i] = TOUPPER(name[i]);
         }
-        /* fall through */
-
-      case T_FIXNUM:
-      case T_BIGNUM:
-        return NUM2INT(rtype);
+        name2[len] = '\0';
+        name = name2;
     }
 
     switch (*name) {
@@ -2187,6 +3109,52 @@
 #endif
         break;
     }
+    return -1;
+}
+
+static int
+rlimit_type_by_hname(const char *name)
+{
+    return rlimit_resource_name2int(name, 0);
+}
+
+static int
+rlimit_type_by_lname(const char *name)
+{
+    return rlimit_resource_name2int(name, 1);
+}
+
+static int
+rlimit_resource_type(VALUE rtype)
+{
+    const char *name;
+    VALUE v;
+    int r;
+
+    switch (TYPE(rtype)) {
+      case T_SYMBOL:
+        name = rb_id2name(SYM2ID(rtype));
+        break;
+
+      default:
+        v = rb_check_string_type(rtype);
+        if (!NIL_P(v)) {
+            rtype = v;
+      case T_STRING:
+            name = StringValueCStr(rtype);
+            break;
+        }
+        /* fall through */
+
+      case T_FIXNUM:
+      case T_BIGNUM:
+        return NUM2INT(rtype);
+    }
+
+    r = rlimit_type_by_hname(name);
+    if (r != -1)
+        return r;
+
     rb_raise(rb_eArgError, "invalid resource name: %s", name);
 }
 
@@ -3122,7 +4090,7 @@
     if (n < 0) rb_sys_fail("daemon");
     return INT2FIX(n);
 #elif defined(HAVE_FORK)
-    switch (rb_fork(0, 0, 0)) {
+    switch (rb_fork(0, 0, 0, Qnil)) {
       case -1:
 	return (-1);
       case 0:
Index: hash.c
===================================================================
--- hash.c	(revision 16182)
+++ hash.c	(revision 16183)
@@ -2215,8 +2215,8 @@
     return result;
 }
 
-static VALUE
-env_clear(void)
+VALUE
+rb_env_clear(void)
 {
     volatile VALUE keys;
     long i;
@@ -2640,7 +2640,7 @@
     rb_define_singleton_method(envtbl,"each_value", env_each_value, 0);
     rb_define_singleton_method(envtbl,"delete", env_delete_m, 1);
     rb_define_singleton_method(envtbl,"delete_if", env_delete_if, 0);
-    rb_define_singleton_method(envtbl,"clear", env_clear, 0);
+    rb_define_singleton_method(envtbl,"clear", rb_env_clear, 0);
     rb_define_singleton_method(envtbl,"reject", env_reject, 0);
     rb_define_singleton_method(envtbl,"reject!", env_reject_bang, 0);
     rb_define_singleton_method(envtbl,"select", env_select, 0);
Index: test/ruby/test_process.rb
===================================================================
--- test/ruby/test_process.rb	(revision 16182)
+++ test/ruby/test_process.rb	(revision 16183)
@@ -1,4 +1,7 @@
 require 'test/unit'
+require 'tmpdir'
+require 'require_relative'
+require_relative 'envutil'
 
 class TestProcess < Test::Unit::TestCase
   def test_rlimit_availability
@@ -74,4 +77,431 @@
     assert_raise(Errno::EPERM) { Process.setrlimit(:NOFILE, :INFINITY) }
     assert_raise(Errno::EPERM) { Process.setrlimit(:NOFILE, "INFINITY") }
   end
+
+  def with_tmpchdir
+    Dir.mktmpdir {|d|
+      Dir.chdir(d) {
+        yield d
+      }
+    }
+  end
+
+  def test_execopts_opts
+    assert_nothing_raised {
+      Process.wait Process.spawn("true", {})
+    }
+    assert_raise(ArgumentError) {
+      Process.wait Process.spawn("true", :foo => 100)
+    }
+    assert_raise(ArgumentError) {
+      Process.wait Process.spawn("true", Process => 100)
+    }
+  end
+
+  def test_execopts_pgroup
+    ruby = EnvUtil.rubybin
+    assert_nothing_raised { system("true", :pgroup=>false) }
+
+    io = IO.popen([ruby, "-e", "print Process.getpgrp"])
+    assert_equal(Process.getpgrp.to_s, io.read)
+    io.close
+
+    io = IO.popen([ruby, "-e", "print Process.getpgrp", :pgroup=>true])
+    assert_equal(io.pid.to_s, io.read)
+    io.close
+
+    assert_raise(ArgumentError) { system("true", :pgroup=>-1) }
+    assert_raise(Errno::EPERM) { Process.wait spawn("true", :pgroup=>1) }
+
+    io1 = IO.popen([ruby, "-e", "print Process.getpgrp", :pgroup=>true])
+    io2 = IO.popen([ruby, "-e", "print Process.getpgrp", :pgroup=>io1.pid])
+    assert_equal(io1.pid.to_s, io1.read)
+    assert_equal(io1.pid.to_s, io2.read)
+    Process.wait io1.pid
+    Process.wait io2.pid
+    io1.close
+    io2.close
+
+  end
+
+  def test_execopts_rlimit
+    return unless rlimit_exist?
+    assert_raise(ArgumentError) { system("true", :rlimit_foo=>0) }
+    assert_raise(ArgumentError) { system("true", :rlimit_NOFILE=>0) }
+    assert_raise(ArgumentError) { system("true", :rlimit_nofile=>[]) }
+    assert_raise(ArgumentError) { system("true", :rlimit_nofile=>[1,2,3]) }
+
+    max = Process.getrlimit(:CORE).last
+
+    n = max
+    IO.popen([EnvUtil.rubybin, "-e",
+             "p Process.getrlimit(:CORE)", :rlimit_core=>n]) {|io|
+      assert_equal("[#{n}, #{n}]\n", io.read)
+    }
+
+    n = 0
+    IO.popen([EnvUtil.rubybin, "-e",
+             "p Process.getrlimit(:CORE)", :rlimit_core=>n]) {|io|
+      assert_equal("[#{n}, #{n}]\n", io.read)
+    }
+
+    n = max
+    IO.popen([EnvUtil.rubybin, "-e",
+             "p Process.getrlimit(:CORE)", :rlimit_core=>[n]]) {|io|
+      assert_equal("[#{n}, #{n}]", io.read.chomp)
+    }
+
+    m, n = 0, max
+    IO.popen([EnvUtil.rubybin, "-e",
+             "p Process.getrlimit(:CORE)", :rlimit_core=>[m,n]]) {|io|
+      assert_equal("[#{m}, #{n}]", io.read.chomp)
+    }
+
+    m, n = 0, 0
+    IO.popen([EnvUtil.rubybin, "-e",
+             "p Process.getrlimit(:CORE)", :rlimit_core=>[m,n]]) {|io|
+      assert_equal("[#{m}, #{n}]", io.read.chomp)
+    }
+
+    n = max
+    IO.popen([EnvUtil.rubybin, "-e",
+      "p Process.getrlimit(:CORE), Process.getrlimit(:CPU)",
+      :rlimit_core=>n, :rlimit_cpu=>3600]) {|io|
+      assert_equal("[#{n}, #{n}]\n[3600, 3600]", io.read.chomp)
+    }
+  end
+
+  def test_execopts_env
+    assert_raise(ArgumentError) {
+      system({"F=O"=>"BAR"}, "env")
+    }
+
+    h = {}
+    ENV.each {|k,v| h[k] = nil unless k == "PATH" }
+    IO.popen([h, "env"]) {|io|
+      assert_equal(1, io.readlines.length)
+    }
+
+    IO.popen([{"FOO"=>"BAR"}, "env"]) {|io|
+      assert_match(/FOO=BAR/, io.read)
+    }
+
+    with_tmpchdir {|d|
+      system({"fofo"=>"haha"}, "env", STDOUT=>"out")
+      assert_match(/fofo=haha/, File.read("out").chomp)
+    }
+  end
+
+  def test_execopts_unsetenv_others
+    IO.popen(["/usr/bin/env", :unsetenv_others=>true]) {|io|
+      assert_equal("", io.read)
+    }
+    IO.popen([{"A"=>"B"}, "/usr/bin/env", :unsetenv_others=>true]) {|io|
+      assert_equal("A=B\n", io.read)
+    }
+  end
+
+  def test_execopts_chdir
+    with_tmpchdir {|d|
+      Process.wait Process.spawn("pwd > dir", :chdir => d)
+      assert_equal(d, File.read("#{d}/dir").chomp)
+      assert_raise(Errno::ENOENT) {
+        Process.wait Process.spawn("true", :chdir => "d/notexist")
+      }
+    }
+  end
+
+  def test_execopts_umask
+    with_tmpchdir {|d|
+      n = "#{d}/mask"
+      Process.wait Process.spawn("sh -c umask > #{n}", :umask => 0)
+      assert_equal("0000", File.read(n).chomp)
+      Process.wait Process.spawn("sh -c umask > #{n}", :umask => 0777)
+      assert_equal("0777", File.read(n).chomp)
+    }
+  end
+
+  def with_pipe
+    begin
+      r, w = IO.pipe
+      yield r, w
+    ensure
+      r.close unless r.closed?
+      w.close unless w.closed?
+    end
+  end
+
+  def with_pipes(n)
+    ary = []
+    begin
+      n.times {
+        ary << IO.pipe
+      }
+      yield ary
+    ensure
+      ary.each {|r, w|
+        r.close unless r.closed?
+        w.close unless w.closed?
+      }
+    end
+  end
+
+  def test_execopts_redirect
+    with_tmpchdir {|d|
+      Process.wait Process.spawn("echo a", STDOUT=>["out", File::WRONLY|File::CREAT|File::TRUNC, 0644])
+      assert_equal("a", File.read("out").chomp)
+      Process.wait Process.spawn("echo 0", STDOUT=>["out", File::WRONLY|File::CREAT|File::APPEND, 0644])
+      assert_equal("a\n0\n", File.read("out"))
+      Process.wait Process.spawn("sort", STDIN=>["out", File::RDONLY, 0644],
+                                         STDOUT=>["out2", File::WRONLY|File::CREAT|File::TRUNC, 0644])
+      assert_equal("0\na\n", File.read("out2"))
+      Process.wait Process.spawn("echo b", [STDOUT, STDERR]=>["out", File::WRONLY|File::CREAT|File::TRUNC, 0644])
+      assert_equal("b", File.read("out").chomp)
+      Process.wait Process.spawn("echo a", STDOUT=>:close, STDERR=>["out", File::WRONLY|File::CREAT|File::TRUNC, 0644])
+      #p File.read("out")
+      assert(!File.read("out").empty?) # error message such as "echo: write error: Bad file descriptor\n".
+      Process.wait Process.spawn("echo c", STDERR=>STDOUT, STDOUT=>["out", File::WRONLY|File::CREAT|File::TRUNC, 0644])
+      assert_equal("c", File.read("out").chomp)
+      File.open("out", "w") {|f|
+        Process.wait Process.spawn("echo d", f=>STDOUT, STDOUT=>f)
+        assert_equal("d", File.read("out").chomp)
+      }
+      Process.wait Process.spawn("echo e", STDOUT=>["out", File::WRONLY|File::CREAT|File::TRUNC, 0644],
+                                 3=>STDOUT, 4=>STDOUT, 5=>STDOUT, 6=>STDOUT, 7=>STDOUT)
+      assert_equal("e", File.read("out").chomp)
+      File.open("out", "w") {|f|
+        h = {STDOUT=>f, f=>STDOUT}
+        3.upto(30) {|i| h[i] = STDOUT if f.fileno != i }
+        Process.wait Process.spawn("echo f", h)
+        assert_equal("f", File.read("out").chomp)
+      }
+      assert_raise(ArgumentError) {
+        Process.wait Process.spawn("echo f", 1=>Process)
+      }
+      assert_raise(ArgumentError) {
+        Process.wait Process.spawn("echo f", [Process]=>1)
+      }
+      assert_raise(ArgumentError) {
+        Process.wait Process.spawn("echo f", [1, STDOUT]=>2)
+      }
+      assert_raise(ArgumentError) {
+        Process.wait Process.spawn("echo f", -1=>2)
+      }
+      Process.wait Process.spawn("echo hhh; echo ggg", STDOUT=>"out")
+      assert_equal("hhh\nggg\n", File.read("out"))
+      Process.wait Process.spawn("sort", STDIN=>"out", STDOUT=>"out2")
+      assert_equal("ggg\nhhh\n", File.read("out2"))
+
+      assert_raise(Errno::ENOENT) {
+        Process.wait Process.spawn("non-existing-command", (3..100).to_a=>["err", File::WRONLY|File::CREAT])
+      }
+      assert_equal("", File.read("err"))
+
+      system("echo bb; echo aa", STDOUT=>["out", "w"])
+      assert_equal("bb\naa\n", File.read("out"))
+      system("sort", STDIN=>["out"], STDOUT=>"out2")
+      assert_equal("aa\nbb\n", File.read("out2"))
+
+      with_pipe {|r1, w1|
+        with_pipe {|r2, w2|
+          pid = spawn("sort", STDIN=>r1, STDOUT=>w2, w1=>:close, r2=>:close)
+          r1.close
+          w2.close
+          w1.puts "c"
+          w1.puts "a"
+          w1.puts "b"
+          w1.close
+          assert_equal("a\nb\nc\n", r2.read)
+        }
+      }
+
+      with_pipes(5) {|pipes|
+        ios = pipes.flatten
+        h = {}
+        ios.length.times {|i| h[ios[i]] = ios[(i-1)%ios.length] }
+        h2 = h.invert
+        rios = pipes.map {|r, w| r }
+        wios = pipes.map {|r, w| w }
+        child_wfds = wios.map {|w| h2[w].fileno }
+        pid = spawn(EnvUtil.rubybin, "-e",
+                "[#{child_wfds.join(',')}].each {|fd| IO.new(fd).puts fd }", h)
+        pipes.each {|r, w|
+          assert_equal("#{h2[w].fileno}\n", r.gets)
+        }
+        Process.wait pid;
+      }
+
+      with_pipes(5) {|pipes|
+        ios = pipes.flatten
+        h = {}
+        ios.length.times {|i| h[ios[i]] = ios[(i+1)%ios.length] }
+        h2 = h.invert
+        rios = pipes.map {|r, w| r }
+        wios = pipes.map {|r, w| w }
+        child_wfds = wios.map {|w| h2[w].fileno }
+        pid = spawn(EnvUtil.rubybin, "-e",
+                "[#{child_wfds.join(',')}].each {|fd| IO.new(fd).puts fd }", h)
+        pipes.each {|r, w|
+          assert_equal("#{h2[w].fileno}\n", r.gets)
+        }
+        Process.wait pid;
+      }
+
+      closed_fd = nil
+      with_pipes(5) {|pipes|
+        io = pipes.last.last
+        closed_fd = io.fileno
+      }
+      assert_raise(Errno::EBADF) { Process.wait spawn("true", closed_fd=>closed_fd) }
+
+      with_pipe {|r, w|
+        w.close_on_exec = true
+        pid = spawn(EnvUtil.rubybin, "-e", "IO.new(#{w.fileno}).print 'a'", w=>w)
+        w.close
+        assert_equal("a", r.read)
+        Process.wait pid
+      }
+
+      system("echo funya", :out=>"out")
+      assert_equal("funya\n", File.read("out"))
+      system("echo henya 1>&2", :err=>"out")
+      assert_equal("henya\n", File.read("out"))
+      IO.popen(["cat", :in=>"out"]) {|io|
+        assert_equal("henya\n", io.read)
+      }
+    }
+  end
+
+  def test_execopts_exec
+    with_tmpchdir {|d|
+      pid = fork {
+        exec "echo aaa", STDOUT=>"foo"
+      }
+      Process.wait pid
+      assert_equal("aaa\n", File.read("foo"))
+    }
+  end
+
+  def test_execopts_popen
+    with_tmpchdir {|d|
+      IO.popen("echo foo") {|io| assert_equal("foo\n", io.read) }
+      assert_raise(Errno::ENOENT) { IO.popen(["echo bar"]) {} }
+      IO.popen(["echo", "baz"]) {|io| assert_equal("baz\n", io.read) }
+      #IO.popen(["echo", "qux", STDOUT=>STDOUT]) {|io| assert_equal("qux\n", io.read) }
+      IO.popen(["echo", "hoge", STDERR=>STDOUT]) {|io|
+        assert_equal("hoge\n", io.read)
+      }
+      #IO.popen(["echo", "fuga", STDOUT=>"out"]) {|io|
+      #  assert_equal("", io.read)
+      #}
+      #assert_equal("fuga\n", File.read("out"))
+      #IO.popen(["sh", "-c", "echo a >&3", 3=>STDOUT]) {|io|
+      #  assert_equal("a\n", io.read)
+      #}
+      IO.popen(["sh", "-c", "echo b >&9",
+               9=>["out2", File::WRONLY|File::CREAT|File::TRUNC]]) {|io|
+        assert_equal("", io.read)
+      }
+      assert_equal("b\n", File.read("out2"))
+      IO.popen("-") {|io|
+        if !io
+          puts "fooo"
+        else
+          assert_equal("fooo\n", io.read)
+        end
+      }
+    }
+  end
+
+  def test_fd_inheritance
+    with_pipe {|r, w|
+      system("echo ba >&#{w.fileno}")
+      w.close
+      assert_equal("ba\n", r.read)
+    }
+    with_pipe {|r, w|
+      Process.wait spawn("echo bi >&#{w.fileno}")
+      w.close
+      assert_equal("bi\n", r.read)
+    }
+    with_pipe {|r, w|
+      Process.wait fork { exec("echo bu >&#{w.fileno}") }
+      w.close
+      assert_equal("bu\n", r.read)
+    }
+    with_pipe {|r, w|
+      io = IO.popen("echo be 2>&1 >&#{w.fileno}")
+      w.close
+      errmsg = io.read
+      assert_equal("", r.read)
+      assert_not_equal("", errmsg)
+    }
+    with_pipe {|r, w|
+      io = IO.popen([EnvUtil.rubybin, "-e", "STDERR.reopen(STDOUT); IO.new(#{w.fileno}).puts('me')"])
+      w.close
+      errmsg = io.read
+      assert_equal("", r.read)
+      assert_not_equal("", errmsg)
+    }
+    with_pipe {|r, w|
+      errmsg = `echo bo 2>&1 >&#{w.fileno}`
+      w.close
+      assert_equal("", r.read)
+      assert_not_equal("", errmsg)
+    }
+  end
+
+  def test_execopts_close_others
+    with_tmpchdir {|d|
+      with_pipe {|r, w|
+        system("exec 2>err; echo ma >&#{w.fileno}", :close_others=>true)
+        w.close
+        assert_equal("", r.read)
+        assert_not_equal("", File.read("err"))
+        File.unlink("err")
+      }
+      with_pipe {|r, w|
+        Process.wait spawn("exec 2>err; echo mi >&#{w.fileno}", :close_others=>true)
+        w.close
+        assert_equal("", r.read)
+        assert_not_equal("", File.read("err"))
+        File.unlink("err")
+      }
+      with_pipe {|r, w|
+        Process.wait fork { exec("exec 2>err; echo mu >&#{w.fileno}", :close_others=>true) }
+        w.close
+        assert_equal("", r.read)
+        assert_not_equal("", File.read("err"))
+        File.unlink("err")
+      }
+      with_pipe {|r, w|
+        io = IO.popen([EnvUtil.rubybin, "-e", "STDERR.reopen(STDOUT); IO.new(#{w.fileno}).puts('me')", :close_others=>true])
+        w.close
+        errmsg = io.read
+        assert_equal("", r.read)
+        assert_not_equal("", errmsg)
+      }
+      with_pipe {|r, w|
+        io = IO.popen([EnvUtil.rubybin, "-e", "STDERR.reopen(STDOUT); IO.new(#{w.fileno}).puts('mo')", :close_others=>false])
+        w.close
+        errmsg = io.read
+        assert_equal("mo\n", r.read)
+        assert_equal("", errmsg)
+      }
+      with_pipe {|r, w|
+        io = IO.popen([EnvUtil.rubybin, "-e", "STDERR.reopen(STDOUT); IO.new(#{w.fileno}).puts('mo')", :close_others=>nil])
+        w.close
+        errmsg = io.read
+        assert_equal("mo\n", r.read)
+        assert_equal("", errmsg)
+      }
+
+    }
+  end
+
+  def test_system
+    str = "echo fofo"
+    assert_nil(system([str, str]))
+  end
+
 end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]