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

ruby-changes:44545

From: nobu <ko1@a...>
Date: Sun, 6 Nov 2016 09:58:55 +0900 (JST)
Subject: [ruby-changes:44545] nobu:r56618 (trunk): process.c: PATH env in spawn

nobu	2016-11-06 09:58:49 +0900 (Sun, 06 Nov 2016)

  New Revision: 56618

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=56618

  Log:
    process.c: PATH env in spawn
    
    * process.c (rb_exec_fillarg): honor the given path environment
      variable.  [ruby-core:53103] [Bug #8004]

  Modified files:
    trunk/ChangeLog
    trunk/internal.h
    trunk/process.c
    trunk/test/ruby/test_process.rb
Index: internal.h
===================================================================
--- internal.h	(revision 56617)
+++ internal.h	(revision 56618)
@@ -1338,6 +1338,7 @@ struct rb_execarg { https://github.com/ruby/ruby/blob/trunk/internal.h#L1338
     VALUE fd_open;
     VALUE fd_dup2_child;
     VALUE env_modification; /* Qfalse or [[k1,v1], ...] */
+    VALUE path_env;
     VALUE chdir_dir;
 };
 
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 56617)
+++ ChangeLog	(revision 56618)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Nov  6 09:58:47 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* process.c (rb_exec_fillarg): honor the given path environment
+	  variable.  [ruby-core:53103] [Bug #8004]
+
 Sun Nov  6 01:52:31 2016  Akira Matsuda  <ronnie@d...>
 
 	* lib/erb.rb: Alias regist_scanner to register_scanner
Index: process.c
===================================================================
--- process.c	(revision 56617)
+++ process.c	(revision 56618)
@@ -1350,6 +1350,7 @@ mark_exec_arg(void *ptr) https://github.com/ruby/ruby/blob/trunk/process.c#L1350
     rb_gc_mark(eargp->fd_open);
     rb_gc_mark(eargp->fd_dup2_child);
     rb_gc_mark(eargp->env_modification);
+    rb_gc_mark(eargp->path_env);
     rb_gc_mark(eargp->chdir_dir);
 }
 
@@ -1929,12 +1930,19 @@ rb_execarg_extract_options(VALUE execarg https://github.com/ruby/ruby/blob/trunk/process.c#L1930
     return args[1];
 }
 
+#ifdef ENV_IGNORECASE
+#define ENVMATCH(s1, s2) (STRCASECMP((s1), (s2)) == 0)
+#else
+#define ENVMATCH(n1, n2) (strcmp((n1), (n2)) == 0)
+#endif
+
 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;
+    VALUE env = ((VALUE *)arg)[0];
+    VALUE *path = &((VALUE *)arg)[1];
     char *k;
 
     k = StringValueCStr(key);
@@ -1947,20 +1955,25 @@ check_exec_env_i(st_data_t st_key, st_da https://github.com/ruby/ruby/blob/trunk/process.c#L1955
     key = EXPORT_STR(key);
     if (!NIL_P(val)) val = EXPORT_STR(val);
 
+    if (ENVMATCH(k, PATH_ENV)) {
+	*path = val;
+    }
     rb_ary_push(env, hide_obj(rb_assoc_new(key, val)));
 
     return ST_CONTINUE;
 }
 
 static VALUE
-rb_check_exec_env(VALUE hash)
+rb_check_exec_env(VALUE hash, VALUE *path)
 {
-    VALUE env;
+    VALUE env[2];
 
-    env = hide_obj(rb_ary_new());
+    env[0] = hide_obj(rb_ary_new());
+    env[1] = Qfalse;
     st_foreach(rb_hash_tbl_raw(hash), check_exec_env_i, (st_data_t)env);
+    *path = env[1];
 
-    return env;
+    return env[0];
 }
 
 static VALUE
@@ -2066,7 +2079,7 @@ rb_exec_fillarg(VALUE prog, int argc, VA https://github.com/ruby/ruby/blob/trunk/process.c#L2079
         rb_check_exec_options(opthash, execarg_obj);
     }
     if (!NIL_P(env)) {
-        env = rb_check_exec_env(env);
+        env = rb_check_exec_env(env, &eargp->path_env);
         eargp->env_modification = env;
     }
 
@@ -2190,7 +2203,10 @@ rb_exec_fillarg(VALUE prog, int argc, VA https://github.com/ruby/ruby/blob/trunk/process.c#L2203
 
     if (!eargp->use_shell) {
 	const char *abspath;
-        abspath = dln_find_exe_r(RSTRING_PTR(eargp->invoke.cmd.command_name), 0, fbuf, sizeof(fbuf));
+	const char *path_env = 0;
+	if (RTEST(eargp->path_env)) path_env = RSTRING_PTR(eargp->path_env);
+	abspath = dln_find_exe_r(RSTRING_PTR(eargp->invoke.cmd.command_name),
+				 path_env, fbuf, sizeof(fbuf));
 	if (abspath)
 	    eargp->invoke.cmd.command_abspath = rb_str_new_cstr(abspath);
 	else
@@ -2271,7 +2287,7 @@ void https://github.com/ruby/ruby/blob/trunk/process.c#L2287
 rb_execarg_setenv(VALUE execarg_obj, VALUE env)
 {
     struct rb_execarg *eargp = rb_execarg_get(execarg_obj);
-    env = !NIL_P(env) ? rb_check_exec_env(env) : Qfalse;
+    env = !NIL_P(env) ? rb_check_exec_env(env, &eargp->path_env) : Qfalse;
     eargp->env_modification = env;
 }
 
Index: test/ruby/test_process.rb
===================================================================
--- test/ruby/test_process.rb	(revision 56617)
+++ test/ruby/test_process.rb	(revision 56618)
@@ -316,6 +316,16 @@ class TestProcess < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_process.rb#L316
     end
   end
 
+  def test_execopt_env_path
+    bug8004 = '[ruby-core:53103] [Bug #8004]'
+    Dir.mktmpdir do |d|
+      open("#{d}/tmp_script.cmd", "w") {|f| f.puts ": ;"; f.chmod(0755)}
+      assert_not_nil(pid = Process.spawn({"PATH" => d}, "tmp_script.cmd"), bug8004)
+      wpid, st = Process.waitpid2(pid)
+      assert_equal([pid, true], [wpid, st.success?], bug8004)
+    end
+  end
+
   def _test_execopts_env_popen(cmd)
     message = cmd.inspect
     IO.popen({"FOO"=>"BAR"}, cmd) {|io|

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

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