http://www.perlmonks.org?node_id=673172

spartanwins has asked for the wisdom of the Perl Monks concerning the following question:

Its regarding 'interposing' open system call and perl interaction.

I have a working code snippet which will interpose 'open' system call on Linux/Unix. But I am not able to trap 'open' calls made from within perl programs My interpose methos uses LD_PRELOAD and hence assumes that the applications are using dynamic linking...

I have studied ex::override to some degree, but could not figure out how I could interpose 'open' from pre-existing perl code
  • Comment on Interpose system call 'open' using LD_PRELOAD does not work for 'open' called from within perl programs. Why?

Replies are listed 'Best First'.
Re: Interpose system call 'open' using LD_PRELOAD does not work for 'open' called from within perl programs. Why?
by Corion (Patriarch) on Mar 10, 2008 at 07:13 UTC

    At some place, the perl executable has to enter in contact with the operating system and invoke its open (or fopen?) system call, so that is a place to wedge your code in.

    I don't know how your interception mechanism works, but if it tries to hand the program a fake libc instead of intercepting the system calls, you will run afoul of static linking. Whether your version of the perl executable uses static linking is another matter - Perl can be built to use static linking for some parts and dynamic linking for others. It may also well be that Perl uses some other call to open a file instead of open, but I don't know the C library well enough to suggest which other ones to intercept.

    If you stay within Perl, you can always override GLOBAL::open, from C or from Perl. If you want to install this universally, put it in a Perl module and preload that module via the PERL5OPTS environment variable.

Re: Interpose system call 'open' using LD_PRELOAD does not work for 'open' called from within perl programs. Why?
by ysth (Canon) on Mar 10, 2008 at 08:03 UTC
    I'm guessing the the actual open call you need to intercept is in libperl.so.5.8.8. Maybe loading that and then your intercept library in LD_PRELOAD would help? Not something I know a lot about.
Re: Interpose system call 'open' using LD_PRELOAD does not work for 'open' called from within perl programs. Why?
by pc88mxer (Vicar) on Mar 10, 2008 at 19:57 UTC
    Update: Figured it out. You need to intercept open64, not open.

    Previously I wrote:

    This is interesting. I can intercept other functions like getpid and close, but there's something different about open.

    I'd ask the perl5-porters mailing list - I'm sure someone there would know what's going on.

Re: Interpose system call 'open' using LD_PRELOAD does not work for 'open' called from within perl programs. Why?
by kyle (Abbot) on Mar 10, 2008 at 17:46 UTC

    What's the XY Problem here? What are you really trying to accomplish? What do you have, and what are you trying to get? Perhaps there is a better way than trying to replace open.