diff options
author | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-04-24 14:46:39 +0000 |
---|---|---|
committer | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-04-24 14:46:39 +0000 |
commit | 278b63a3e459993c44aa0515239970fde517642a (patch) | |
tree | e529cfce435bff40c710dec4f838b3e861a3fd0d /process.c | |
parent | 9b694aaa8588a1f43fd0ae8c1142c7a854a3f6c6 (diff) |
* 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]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16183 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'process.c')
-rw-r--r-- | process.c | 1106 |
1 files changed, 1037 insertions, 69 deletions
@@ -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 @@ static VALUE S_Tms; #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 @@ proc_spawn(char *str) #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 @@ rb_check_argv(int argc, VALUE *argv) 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 @@ rb_check_argv(int argc, VALUE *argv) * 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 @@ VALUE 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 @@ rb_exec(const struct rb_exec_arg *e) 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 @@ proc_syswait(VALUE pid) #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 @@ proc_syswait(VALUE pid) * 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 @@ rb_fork(int *status, int (*chfunc)(void*), void *charg) #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_f_fork(VALUE obj) 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_spawn(int argc, VALUE *argv) { 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 @@ rb_spawn(int argc, VALUE *argv) /* * 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 @@ rb_spawn(int argc, VALUE *argv) * <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 @@ rb_f_system(int argc, VALUE *argv) /* * 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 @@ proc_setpriority(VALUE obj, VALUE which, VALUE who, VALUE prio) #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 @@ rlimit_resource_type(VALUE rtype) #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 @@ proc_daemon(int argc, VALUE *argv) 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: |