summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-11-04 16:21:38 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-11-04 16:21:38 +0000
commitd0553ffbb53c10ff5354d063c4c4d15348fe955e (patch)
treee1c807641da2aa3a30a7a2aba99bb9889df4face
parentee388f6ee2790908dffbb4b20d7d1d43b8ddb38a (diff)
* process.c (proc_exec_v, proc_spawn_v): try to execute with sh if
no shebang. [ruby-core:32745] [EXPERIMENTAL] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29695 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--process.c32
-rw-r--r--test/ruby/test_process.rb14
-rw-r--r--test/ruby/test_system.rb11
4 files changed, 55 insertions, 7 deletions
diff --git a/ChangeLog b/ChangeLog
index e3d993e885..14fcd2e6ed 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Fri Nov 5 01:21:31 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * process.c (proc_exec_v, proc_spawn_v): try to execute with sh if
+ no shebang. [ruby-core:32745] [EXPERIMENTAL]
+
Fri Nov 5 00:39:00 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
* io.c (rb_io_readlines, rb_io_each_line): limit must not be zero.
diff --git a/process.c b/process.c
index 55b83b10b8..dbcf9219c1 100644
--- a/process.c
+++ b/process.c
@@ -1014,6 +1014,21 @@ security(const char *str)
}
}
+#ifdef HAVE_FORK
+#define try_with_sh(prog, argv) ((saved_errno == ENOEXEC) ? exec_with_sh(prog, argv) : (void)0)
+static void
+exec_with_sh(const char *prog, char **argv)
+{
+ *argv = (char *)prog;
+ *--argv = (char *)"sh";
+ execv("/bin/sh", argv);
+}
+#define ALLOCA_ARGV(n) ALLOCA_N(char*, (n)+1)
+#else
+#define try_with_sh(prog, argv) (void)0
+#define ALLOCA_ARGV(n) ALLOCA_N(char*, n)
+#endif
+
static int
proc_exec_v(char **argv, const char *prog)
{
@@ -1058,7 +1073,7 @@ proc_exec_v(char **argv, const char *prog)
#endif /* __EMX__ */
before_exec();
execv(prog, argv);
- preserving_errno(after_exec());
+ preserving_errno(try_with_sh(prog, argv); after_exec());
return -1;
}
@@ -1068,7 +1083,7 @@ rb_proc_exec_n(int argc, VALUE *argv, const char *prog)
char **args;
int i;
- args = ALLOCA_N(char*, argc+1);
+ args = ALLOCA_ARGV(argc+1);
for (i=0; i<argc; i++) {
args[i] = RSTRING_PTR(argv[i]);
}
@@ -1126,7 +1141,7 @@ rb_proc_exec(const char *str)
return -1;
}
}
- a = argv = ALLOCA_N(char*, (s-str)/2+2);
+ a = argv = ALLOCA_ARGV((s-str)/2+2);
ss = ALLOCA_N(char, s-str+1);
memcpy(ss, str, s-str);
ss[s-str] = '\0';
@@ -1167,8 +1182,11 @@ proc_spawn_v(char **argv, char *prog)
before_exec();
status = spawnv(P_WAIT, prog, argv);
- rb_last_status_set(status == -1 ? 127 : status, 0);
- after_exec();
+ preserving_errno({
+ rb_last_status_set(status == -1 ? 127 : status, 0);
+ try_with_sh(prog, argv);
+ after_exec();
+ });
return status;
}
#endif
@@ -1179,7 +1197,7 @@ proc_spawn_n(int argc, VALUE *argv, VALUE prog)
char **args;
int i;
- args = ALLOCA_N(char*, argc + 1);
+ args = ALLOCA_ARGV(argc + 1);
for (i = 0; i < argc; i++) {
args[i] = RSTRING_PTR(argv[i]);
}
@@ -1210,7 +1228,7 @@ proc_spawn(char *str)
return status;
}
}
- a = argv = ALLOCA_N(char*, (s - str) / 2 + 2);
+ a = argv = ALLOCA_ARGV((s - str) / 2 + 2);
s = ALLOCA_N(char, s - str + 1);
strcpy(s, str);
if (*a++ = strtok(s, " \t")) {
diff --git a/test/ruby/test_process.rb b/test/ruby/test_process.rb
index df1086dad1..4e16400582 100644
--- a/test/ruby/test_process.rb
+++ b/test/ruby/test_process.rb
@@ -1220,4 +1220,18 @@ class TestProcess < Test::Unit::TestCase
assert(status.success?, "[ruby-dev:38105]")
}
end
+
+ def test_fallback_to_sh
+ feature = '[ruby-core:32745]'
+ with_tmpchdir do |d|
+ open("tmp_script.#{$$}", "w") {|f| f.puts ": ;"; f.chmod(0755)}
+ assert_not_nil(pid = Process.spawn("./tmp_script.#{$$}"), feature)
+ wpid, st = Process.waitpid2(pid)
+ assert_equal([pid, true], [wpid, st.success?], feature)
+
+ open("tmp_script.#{$$}", "w") {|f| f.puts "echo $#: $@"; f.chmod(0755)}
+ result = IO.popen(["./tmp_script.#{$$}", "a b", "c"]) {|f| f.read}
+ assert_equal("2: a b c\n", result, feature)
+ end
+ end
end
diff --git a/test/ruby/test_system.rb b/test/ruby/test_system.rb
index 358fa54ef8..2a5107f70c 100644
--- a/test/ruby/test_system.rb
+++ b/test/ruby/test_system.rb
@@ -91,4 +91,15 @@ class TestSystem < Test::Unit::TestCase
def test_empty_evstr
assert_equal("", eval('"#{}"', nil, __FILE__, __LINE__), "[ruby-dev:25113]")
end
+
+ def test_fallback_to_sh
+ Dir.mktmpdir("ruby_script_tmp") {|tmpdir|
+ tmpfilename = "#{tmpdir}/ruby_script_tmp.#{$$}"
+ open(tmpfilename, "w") {|f|
+ f.puts ": ;"
+ f.chmod(0755)
+ }
+ assert_equal(true, system(tmpfilename), '[ruby-core:32745]')
+ }
+ end
end