1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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
This commit is contained in:
nobu 2010-11-04 16:21:38 +00:00
parent ee388f6ee2
commit d0553ffbb5
4 changed files with 55 additions and 7 deletions

View file

@ -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> 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. * io.c (rb_io_readlines, rb_io_each_line): limit must not be zero.

View file

@ -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 static int
proc_exec_v(char **argv, const char *prog) proc_exec_v(char **argv, const char *prog)
{ {
@ -1058,7 +1073,7 @@ proc_exec_v(char **argv, const char *prog)
#endif /* __EMX__ */ #endif /* __EMX__ */
before_exec(); before_exec();
execv(prog, argv); execv(prog, argv);
preserving_errno(after_exec()); preserving_errno(try_with_sh(prog, argv); after_exec());
return -1; return -1;
} }
@ -1068,7 +1083,7 @@ rb_proc_exec_n(int argc, VALUE *argv, const char *prog)
char **args; char **args;
int i; int i;
args = ALLOCA_N(char*, argc+1); args = ALLOCA_ARGV(argc+1);
for (i=0; i<argc; i++) { for (i=0; i<argc; i++) {
args[i] = RSTRING_PTR(argv[i]); args[i] = RSTRING_PTR(argv[i]);
} }
@ -1126,7 +1141,7 @@ rb_proc_exec(const char *str)
return -1; 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); ss = ALLOCA_N(char, s-str+1);
memcpy(ss, str, s-str); memcpy(ss, str, s-str);
ss[s-str] = '\0'; ss[s-str] = '\0';
@ -1167,8 +1182,11 @@ proc_spawn_v(char **argv, char *prog)
before_exec(); before_exec();
status = spawnv(P_WAIT, prog, argv); status = spawnv(P_WAIT, prog, argv);
rb_last_status_set(status == -1 ? 127 : status, 0); preserving_errno({
after_exec(); rb_last_status_set(status == -1 ? 127 : status, 0);
try_with_sh(prog, argv);
after_exec();
});
return status; return status;
} }
#endif #endif
@ -1179,7 +1197,7 @@ proc_spawn_n(int argc, VALUE *argv, VALUE prog)
char **args; char **args;
int i; int i;
args = ALLOCA_N(char*, argc + 1); args = ALLOCA_ARGV(argc + 1);
for (i = 0; i < argc; i++) { for (i = 0; i < argc; i++) {
args[i] = RSTRING_PTR(argv[i]); args[i] = RSTRING_PTR(argv[i]);
} }
@ -1210,7 +1228,7 @@ proc_spawn(char *str)
return status; 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); s = ALLOCA_N(char, s - str + 1);
strcpy(s, str); strcpy(s, str);
if (*a++ = strtok(s, " \t")) { if (*a++ = strtok(s, " \t")) {

View file

@ -1220,4 +1220,18 @@ class TestProcess < Test::Unit::TestCase
assert(status.success?, "[ruby-dev:38105]") assert(status.success?, "[ruby-dev:38105]")
} }
end 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 end

View file

@ -91,4 +91,15 @@ class TestSystem < Test::Unit::TestCase
def test_empty_evstr def test_empty_evstr
assert_equal("", eval('"#{}"', nil, __FILE__, __LINE__), "[ruby-dev:25113]") assert_equal("", eval('"#{}"', nil, __FILE__, __LINE__), "[ruby-dev:25113]")
end 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 end