Skip to content

Commit

Permalink
* dln.c (dln_find_exe_r, dln_find_file_r): reentrant versions.
Browse files Browse the repository at this point in the history
* file.c (rb_find_file_ext, rb_find_file), process.c (proc_exec_v),
  (rb_proc_exec, proc_spawn_v, proc_spawn), ruby.c (process_options):
  use reentrant versions.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16319 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
nobu committed May 7, 2008
1 parent 86afcfe commit 4264ca9
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 19 deletions.
8 changes: 8 additions & 0 deletions ChangeLog
@@ -1,3 +1,11 @@
Thu May 8 06:43:52 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>

* dln.c (dln_find_exe_r, dln_find_file_r): reentrant versions.

* file.c (rb_find_file_ext, rb_find_file), process.c (proc_exec_v),
(rb_proc_exec, proc_spawn_v, proc_spawn), ruby.c (process_options):
use reentrant versions.

Thu May 8 06:27:33 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>

* thread.c (rb_thread_key_p): thread local storage stores ID.
Expand Down
29 changes: 21 additions & 8 deletions dln.c
Expand Up @@ -1568,10 +1568,10 @@ dln_load(const char *file)
return 0; /* dummy return */
}

static char *dln_find_1(const char *fname, const char *path, int exe_flag);
static char *dln_find_1(const char *fname, const char *path, char *buf, int size, int exe_flag);

char *
dln_find_exe(const char *fname, const char *path)
dln_find_exe_r(const char *fname, const char *path, char *buf, int size)
{
if (!path) {
path = getenv(PATH_ENV);
Expand All @@ -1584,25 +1584,38 @@ dln_find_exe(const char *fname, const char *path)
path = "/usr/local/bin:/usr/ucb:/usr/bin:/bin:.";
#endif
}
return dln_find_1(fname, path, 1);
return dln_find_1(fname, path, buf, size, 1);
}

char *
dln_find_file(const char *fname, const char *path)
dln_find_file_r(const char *fname, const char *path, char *buf, int size)
{
#ifndef __MACOS__
if (!path) path = ".";
return dln_find_1(fname, path, 0);
return dln_find_1(fname, path, buf, size, 0);
#else
if (!path) path = ".";
return _macruby_path_conv_posix_to_macos(dln_find_1(fname, path, 0));
return _macruby_path_conv_posix_to_macos(dln_find_1(fname, path, buf, size, 0));
#endif
}

static char fbuf[MAXPATHLEN];

char *
dln_find_exe(const char *fname, const char *path)
{
return dln_find_exe_r(fname, path, fbuf, sizeof(fbuf));
}

char *
dln_find_file(const char *fname, const char *path)
{
return dln_find_file_r(fname, path, fbuf, sizeof(fbuf));
}

static char *
dln_find_1(const char *fname, const char *path, int exe_flag /* non 0 if looking for executable. */)
dln_find_1(const char *fname, const char *path, char *fbuf, int size,
int exe_flag /* non 0 if looking for executable. */)
{
register const char *dp;
register const char *ep;
Expand Down Expand Up @@ -1642,7 +1655,7 @@ dln_find_1(const char *fname, const char *path, int exe_flag /* non 0 if looking
/* find the length of that component */
l = ep - dp;
bp = fbuf;
fspace = sizeof fbuf - 2;
fspace = size - 2;
if (l > 0) {
/*
** If the length of the component is zero length,
Expand Down
2 changes: 2 additions & 0 deletions dln.h
Expand Up @@ -30,6 +30,8 @@

char *dln_find_exe(const char*,const char*);
char *dln_find_file(const char*,const char*);
char *dln_find_exe_r(const char*,const char*,char*,int);
char *dln_find_file_r(const char*,const char*,char*,int);

#ifdef USE_DLN_A_OUT
extern char *dln_argv0;
Expand Down
6 changes: 4 additions & 2 deletions file.c
Expand Up @@ -4334,11 +4334,12 @@ rb_find_file_ext(VALUE *filep, const char *const *ext)
OBJ_FREEZE(fname);
for (i = 0; i < RARRAY_LEN(load_path); i++) {
VALUE str = RARRAY_PTR(load_path)[i];
char fbuf[MAXPATHLEN];

FilePathValue(str);
if (RSTRING_LEN(str) == 0) continue;
path = RSTRING_PTR(str);
found = dln_find_file(StringValueCStr(fname), path);
found = dln_find_file_r(StringValueCStr(fname), path, fbuf, sizeof(fbuf));
if (found && file_load_ok(found)) {
*filep = rb_str_new2(found);
return j+1;
Expand All @@ -4354,6 +4355,7 @@ rb_find_file(VALUE path)
VALUE tmp, load_path;
char *f = StringValueCStr(path);
char *lpath;
char fbuf[MAXPATHLEN];

if (f[0] == '~') {
path = rb_file_expand_path(path, Qnil);
Expand Down Expand Up @@ -4411,7 +4413,7 @@ rb_find_file(VALUE path)
if (!lpath) {
return 0; /* no path, no load */
}
if (!(f = dln_find_file(f, lpath))) {
if (!(f = dln_find_file_r(f, lpath, fbuf, sizeof(fbuf)))) {
return 0;
}
if (rb_safe_level() >= 1 && !fpath_check(f)) {
Expand Down
25 changes: 18 additions & 7 deletions process.c
Expand Up @@ -51,6 +51,12 @@ struct timeval rb_time_interval(VALUE);
#ifdef HAVE_SYS_RESOURCE_H
# include <sys/resource.h>
#endif
#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
#ifndef MAXPATHLEN
# define MAXPATHLEN 1024
#endif
#include "ruby/st.h"

#ifdef __EMX__
Expand Down Expand Up @@ -963,7 +969,7 @@ void rb_thread_reset_timer_thread(void);
#define after_exec() \
(rb_thread_start_timer_thread(), rb_disable_interrupt())

extern char *dln_find_exe(const char *fname, const char *path);
#include "dln.h"

static void
security(const char *str)
Expand All @@ -978,9 +984,11 @@ security(const char *str)
static int
proc_exec_v(char **argv, const char *prog)
{
char fbuf[MAXPATHLEN];

if (!prog)
prog = argv[0];
prog = dln_find_exe(prog, 0);
prog = dln_find_exe_r(prog, 0, fbuf, sizeof(fbuf));
if (!prog) {
errno = ENOENT;
return -1;
Expand Down Expand Up @@ -1015,7 +1023,7 @@ proc_exec_v(char **argv, const char *prog)
*p = '\\';
new_argv[0] = COMMAND;
argv = new_argv;
prog = dln_find_exe(argv[0], 0);
prog = dln_find_exe_r(argv[0], 0, fbuf, sizeof(fbuf));
if (!prog) {
errno = ENOENT;
return -1;
Expand Down Expand Up @@ -1081,7 +1089,8 @@ rb_proc_exec(const char *str)
if (status != -1)
exit(status);
#elif defined(__human68k__) || defined(__CYGWIN32__) || defined(__EMX__)
char *shell = dln_find_exe("sh", 0);
char fbuf[MAXPATHLEN];
char *shell = dln_find_exe_r("sh", 0, fbuf, sizeof(fbuf));
int status = -1;
before_exec();
if (shell)
Expand Down Expand Up @@ -1128,13 +1137,14 @@ rb_proc_exec(const char *str)
static rb_pid_t
proc_spawn_v(char **argv, char *prog)
{
char fbuf[MAXPATHLEN];
char *extension;
rb_pid_t status;

if (!prog)
prog = argv[0];
security(prog);
prog = dln_find_exe(prog, 0);
prog = dln_find_exe_r(prog, 0, fbuf, sizeof(fbuf));
if (!prog)
return -1;

Expand All @@ -1155,7 +1165,7 @@ proc_spawn_v(char **argv, char *prog)
*p = '\\';
new_argv[0] = COMMAND;
argv = new_argv;
prog = dln_find_exe(argv[0], 0);
prog = dln_find_exe_r(argv[0], 0, fbuf, sizeof(fbuf));
if (!prog) {
errno = ENOENT;
return -1;
Expand Down Expand Up @@ -1192,13 +1202,14 @@ proc_spawn_n(int argc, VALUE *argv, VALUE prog)
static rb_pid_t
proc_spawn(char *str)
{
char fbuf[MAXPATHLEN];
char *s, *t;
char **argv, **a;
rb_pid_t status;

for (s = str; *s; s++) {
if (*s != ' ' && !ISALPHA(*s) && strchr("*?{}[]<>()~&|\\$;'`\"\n",*s)) {
char *shell = dln_find_exe("sh", 0);
char *shell = dln_find_exe_r("sh", 0, fbuf, sizeof(fbuf));
before_exec();
status = shell?spawnl(P_WAIT,shell,"sh","-c",str,(char*)NULL):system(str);
rb_last_status_set(status == -1 ? 127 : status, 0);
Expand Down
5 changes: 3 additions & 2 deletions ruby.c
Expand Up @@ -953,6 +953,7 @@ process_options(VALUE arg)
VALUE parser;
rb_encoding *enc, *lenc;
const char *s;
char fbuf[MAXPATHLEN];
int i = proc_options(argc, argv, opt);
int safe;

Expand Down Expand Up @@ -1033,10 +1034,10 @@ process_options(VALUE arg)

opt->script = 0;
if (path) {
opt->script = dln_find_file(argv[0], path);
opt->script = dln_find_file_r(argv[0], path, fbuf, sizeof(fbuf));
}
if (!opt->script) {
opt->script = dln_find_file(argv[0], getenv(PATH_ENV));
opt->script = dln_find_file_r(argv[0], getenv(PATH_ENV), fbuf, sizeof(fbuf));
}
if (!opt->script)
opt->script = argv[0];
Expand Down

0 comments on commit 4264ca9

Please sign in to comment.