Can $LOAD_PATH be relative?

Question based on an assertion made in another thread…

I was under the impression that $LOAD_PATH is relative to the current
location of the ruby executable. On Windows this certainly seems to be
the case. I can move ruby.exe and see that $LOAD_PATH changes.

However, on Linux this doesn’t seem to be the case, no matter where I put
the ruby binary, $LOAD_PATH points to /usr/local/lib/ruby/…

Is there anyway to make $LOAD_PATH relative on Linux (and I suspect also
on Windows if you use MingW to compile) just as it is on the Windows
mswin32 build?

…I’m hoping this is just a configure command-line switch since I need to
put the Ruby binary and some libs on a CDROM and have it execute from
there.

Phil

ptkwt@aracnet.com (Phil Tomson) writes:

Question based on an assertion made in another thread…

I was under the impression that $LOAD_PATH is relative to the current
location of the ruby executable. On Windows this certainly seems to be
the case. I can move ruby.exe and see that $LOAD_PATH changes.

However, on Linux this doesn’t seem to be the case, no matter where I put
the ruby binary, $LOAD_PATH points to /usr/local/lib/ruby/…

Is there anyway to make $LOAD_PATH relative on Linux (and I suspect also
on Windows if you use MingW to compile) just as it is on the Windows
mswin32 build?

…I’m hoping this is just a configure command-line switch since I need to
put the Ruby binary and some libs on a CDROM and have it execute from
there.

You can add to $LOAD_PATH when the script starts, and those paths can be
relative to the script itself. Does that help? For example,

HERE = File.dirname(__FILE__) # __FILE__ does not alwyas == $0
$LOAD_PATH << HERE          # Add script's directory
$LOAD_PATH << File.join(HERE, '..') # Add directory above script

Since you are in control of the directory structure on the CD, you should be
able to find the Ruby libs relative to the script.

You could also use the -I command line switch or the environment variable
RUBYLIB to add to the list of directories to search.

Jim

···


Jim Menard, jimm@io.com, http://www.io.com/~jimm/
“Is ‘anal-retentive’ hyphenated?” – Alison Bechdel

ptkwt@aracnet.com (Phil Tomson) wrote in message news:c3fhqv028i3@enews2.newsguy.com

Question based on an assertion made in another thread…

I was under the impression that $LOAD_PATH is relative to the current
location of the ruby executable. On Windows this certainly seems to be
the case. I can move ruby.exe and see that $LOAD_PATH changes.

However, on Linux this doesn’t seem to be the case, no matter where I put
the ruby binary, $LOAD_PATH points to /usr/local/lib/ruby/…

Is there anyway to make $LOAD_PATH relative on Linux (and I suspect also
on Windows if you use MingW to compile) just as it is on the Windows
mswin32 build?

…I’m hoping this is just a configure command-line switch since I need to
put the Ruby binary and some libs on a CDROM and have it execute from
there.

Phil

In a shell window, set the environment variable RUBYLIB with the list
of paths you want to use.

True, I could do those things, but I’m wondering how they managed to make
$LOAD_PATH relative on Windows?

Phil

···

In article wsqk71gd188.fsf@io.com, Jim Menard jimm@io.com wrote:

ptkwt@aracnet.com (Phil Tomson) writes:

Question based on an assertion made in another thread…

I was under the impression that $LOAD_PATH is relative to the current
location of the ruby executable. On Windows this certainly seems to be
the case. I can move ruby.exe and see that $LOAD_PATH changes.

However, on Linux this doesn’t seem to be the case, no matter where I put
the ruby binary, $LOAD_PATH points to /usr/local/lib/ruby/…

Is there anyway to make $LOAD_PATH relative on Linux (and I suspect also
on Windows if you use MingW to compile) just as it is on the Windows
mswin32 build?

…I’m hoping this is just a configure command-line switch since I need to
put the Ruby binary and some libs on a CDROM and have it execute from
there.

You can add to $LOAD_PATH when the script starts, and those paths can be
relative to the script itself. Does that help? For example,

HERE = File.dirname(FILE) # FILE does not alwyas == $0
$LOAD_PATH << HERE # Add script’s directory
$LOAD_PATH << File.join(HERE, ‘…’) # Add directory above script

Since you are in control of the directory structure on the CD, you should be
able to find the Ruby libs relative to the script.

You could also use the -I command line switch or the environment variable
RUBYLIB to add to the list of directories to search.

In a shell window, set the environment variable RUBYLIB with the list
of paths you want to use.

Try it with -T1

Guy Decoux

Phil Tomson wrote:

True, I could do those things, but I’m wondering how they managed to make
$LOAD_PATH relative on Windows?

Because Ruby was coded to work that way. Why it wasn’t made to do it
under *nix, I have no idea. I think it’s a critical requirement,
otherwise having private copies of Ruby installs is impossible. It will
always go to /usr/lib/ruby, etc. no matter where Ruby is run from.
After my message about this subject was ignored (Installation/Config
question), I decided to just write it myself and patch Ruby.

Here’s what I did.
After ./configure - the bottom of config.h must be changed to be rooted
off of /lib (the rest stays the same)
#define LOAD_RELATIVE 1 must also be added.

(here was what mine looked like)
#define RUBY_LIB “/lib/ruby/1.8”
#define RUBY_SITE_LIB “/lib/ruby/site_ruby”
#define RUBY_SITE_LIB2 “/lib/ruby/site_ruby/1.8”
#define RUBY_PLATFORM “i686-linux”
#define RUBY_ARCHLIB “/lib/ruby/1.8/i686-linux”
#define RUBY_SITE_ARCHLIB “/lib/ruby/site_ruby/1.8/i686-linux”
#define LOAD_RELATIVE 1

in ruby.c in the ruby_init_loadpath() method I added a #else section
after the #elif defined(_EMX) which was withih the #if defined
LOAD_RELATIVE section.
It looked something like this:
#elif defined(EMX)
_execname(libpath, FILENAME_MAX);
#else
/* pmb 03/01/2004 */
buf = malloc(size);
while ((rv = readlink(“/proc/self/exe”, buf, size)) == size) {
size *= 2;
buf = realloc(buf, size);
}
if (rv < 0 || rv >= FILENAME_MAX) {
free(buf);
fprintf(stderr, “Unable to get path to self through
/proc/self/exe”);
exit(1);
}
strncpy(libpath, buf, rv);
libpath[rv] = ‘\0’;
free(buf);
#endif

···

Hi –

···

On Sat, 20 Mar 2004, Patrick Bennett wrote:

Phil Tomson wrote:

True, I could do those things, but I’m wondering how they managed to make
$LOAD_PATH relative on Windows?

Because Ruby was coded to work that way. Why it wasn’t made to do it
under *nix, I have no idea. I think it’s a critical requirement,
otherwise having private copies of Ruby installs is impossible. It will
always go to /usr/lib/ruby, etc. no matter where Ruby is run from.
After my message about this subject was ignored (Installation/Config
question), I decided to just write it myself and patch Ruby.

I’ve always been able to install private Rubies. I just set --prefix
when ./configure’ing. Does that not do what you need?

David


David A. Black
dblack@wobblini.net

Patrick Bennett patrick.bennett@inin.com writes:

Phil Tomson wrote:

True, I could do those things, but I’m wondering how they managed to make
$LOAD_PATH relative on Windows?

Because Ruby was coded to work that way. Why it wasn’t made to do it
under *nix, I have no idea.

Under Unix you specify the path at compile time. The default is
/usr/local/{bin,lib} but you can change that with arguments to configure.

Jim

···


Jim Menard, jimm@io.com, http://www.io.com/~jimm/
“Yeah, well, don’t count your weasels before they pop, dink.” – The Tick

David A. Black wrote:

I’ve always been able to install private Rubies. I just set --prefix
when ./configure’ing. Does that not do what you need?

No, I needed to define a Ruby tree that could be installed (through
source control checkout) anywhere by a user. ‘Fixed’ paths are IMO,
unacceptable. It works quite well in Windows. I was really surprised
to find that it was all hard-coded for *nix installs.

Jim Menard wrote:

Under Unix you specify the path at compile time. The default is
/usr/local/{bin,lib} but you can change that with arguments to configure.

Yes, I know. This wasn’t acceptable for my needs. Thanks though…

Is there an option to configure to make it relative like is is on Windows
or did they do it with ‘#if defined’ pre-processor statements in the C
Code.

Phil

···

In article wsq1xnofr5p.fsf@io.com, Jim Menard jimm@io.com wrote:

Patrick Bennett patrick.bennett@inin.com writes:

Phil Tomson wrote:

True, I could do those things, but I’m wondering how they managed to make
$LOAD_PATH relative on Windows?

Because Ruby was coded to work that way. Why it wasn’t made to do it
under *nix, I have no idea.

Under Unix you specify the path at compile time. The default is
/usr/local/{bin,lib} but you can change that with arguments to configure.

No, I needed to define a Ruby tree that could be installed (through
source control checkout) *anywhere* by a user. 'Fixed' paths are IMO,
unacceptable. It works quite well in Windows. I was really surprised
to find that it was all hard-coded for *nix installs.

security problem

Guy Decoux

Quoteing ptkwt@aracnet.com, on Sun, Mar 21, 2004 at 03:39:30AM +0900:

Under Unix you specify the path at compile time. The default is
/usr/local/{bin,lib} but you can change that with arguments to configure.

Is there an option to configure to make it relative like is is on Windows

No, there is no such option, and it is not possible to reliably
implement such an option on unix:

http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC23

Sam

No, there is no such option, and it is not possible to reliably
implement such an option on unix:

Never use a command like strings, or you'll have a surprise

Never use an editor, or you'll learn how modify a file.

It's easy to do what he want, I see no good reasons to make it easier.

Guy Decoux

ts wrote:

“P” == Patrick Bennett patrick.bennett@inin.com writes:

No, I needed to define a Ruby tree that could be installed (through
source control checkout) anywhere by a user. ‘Fixed’ paths are IMO,
unacceptable. It works quite well in Windows. I was really surprised
to find that it was all hard-coded for *nix installs.

security problem

…and it wouldn’t be under Windows? Sorry, I don’t buy it. :frowning:

ts wrote:

security problem

...and it wouldn't be under Windows? Sorry, I don't buy it. :frowning:

Where I've said that it's not a security problem under Windows ?

Guy Decoux

Quoteing patrick.bennett@inin.com, on Sun, Mar 21, 2004 at 02:18:01AM +0900:

>> No, I needed to define a Ruby tree that could be installed (through
>> source control checkout) *anywhere* by a user. 'Fixed' paths are IMO,
>> unacceptable. It works quite well in Windows. I was really surprised
>> to find that it was all hard-coded for *nix installs.
>
>security problem

How (it's impossible) to find a process's executable file in Unix is a
FAQ:

  http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC23

Executables in unix don't have a knowledge of "where they are". They
don't, in fact, have to "be" anywhere. Some are even built into the boot
image, so don't in fact have to have ever "been" anywhere.

Do I recall you saying that you'd hacked ruby to get it's executable
location? I'd like to see that hack, I'm pretty confident I can think up
a number of cases where it wouldn't work.

Also, why would you think that something that is a security concern in
unix would be in windows? Windows isn't even a multi-user OS! It also
doesn't support hard or symbolic links, or set-user-id flags on
executables, or allow you to delete a running executable, you can in
unix.

Cheers,
Sam

[…]

Sam

Re read carefully my message (there is only 2 words) : I really don’t
understand your reply.

Guy Decoux

security problem

[…]

Executables in unix don’t have a knowledge of “where they are”. They
don’t, in fact, have to “be” anywhere. Some are even built into the boot
image, so don’t in fact have to have ever “been” anywhere.

Well, on some operating systems it’s certainly possible to find the
executable image corresponding to a process (eg: Linux) and on others
you can find the device number / inode no. of each text section within
the process.

Of course, none of this is really important right now, since I belive he
was referring to the fact that a program could be fooled into reading a
different configuration file to the one the administrator intended, if a
user were to either copy or create a link to the binary somewhere they
had write access to. Of course, it shouldn’t be a problem on a well
configured system, but that’s something of an ideal.

Do I recall you saying that you’d hacked ruby to get it’s executable
location? I’d like to see that hack, I’m pretty confident I can think up
a number of cases where it wouldn’t work.

Operating systems that aren’t Linux being the main case here.

Windows isn’t even a multi-user OS! It also
doesn’t support hard or symbolic links, or set-user-id flags on
executables, or allow you to delete a running executable, you can in
unix.

Windows 2000 and XP support both multi-user operation and soft/hard
links with NTFS.

···


Ceri Storey cez@necrofish.org.uk

1-2 weeks ago I asked the same question about a Linux binary Ruby
installation that can be installed anywhere in users’ home directory
(without the user building Ruby himself). To accomplish this, the
following is needed:

  1. build Ruby with --enable-load-relative (you must patch Ruby with ±
    100-line patch from Nobu; sorry I couldn’t search ruby-talk.org at the
    moment but it’s around the first week of March 2004) which will make
    $LOAD_PATH relative;

  2. either:

a) make a small wrapper for ruby executable, which makes Ruby find its
shared libraries in “…/lib” instead of only in /lib, /usr/lib, and
other paths specified in /etc/ld.so.conf; (I’m attaching the small
wrapper I wrote, which again was kindly revised by Nobu);

b) build a static version of Ruby (–disable-shared in configure
option), which will then depend only on basic shared libraries (like glibc).

After that, you can just bundle the resulting Ruby installation.

Hope that helps,

wrapper.c (2.05 KB)

···


dave