You are not logged in.

#1 2010-05-08 22:43:32

Themaister
Member
From: Trondheim, Norway
Registered: 2008-07-21
Posts: 652
Website

[SOLVED] LD_PRELOAD and libc problems [C].

I'm currently doing some work on a plugin that does OSS emulation in C, and to do that, I need to intercept system calls. This gives me some headache, as I'm not always able to intercept these calls.

Let's say we have a library that just intercepts the open() call. It should print the path that is opened and then call the "real" libc open().

open_test.c

#include <stdio.h>
#include <unistd.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <stdarg.h>
#include <assert.h>

#if defined(RTLD_NEXT)
#define REAL_LIBC RTLD_NEXT
#else
#define REAL_LIBC ((void*) -1L)
#endif

static int (*real_open)(const char* path, int flags, ...);

static int lib_start = 0;

int open(const char* path, int flags, ...)
{
   printf("Open was called with path: \"%s\"\n", path);
   mode_t mode = 0;

   va_list arg;
   if ( flags & O_CREAT )
   {
      va_start(arg, flags);
      mode = va_arg(arg, mode_t);
      va_end(arg);
   }

   if ( !lib_start )
   {
      assert(real_open = dlsym(REAL_LIBC, "open"));
      lib_start++;
   }

   return real_open(path, flags, mode);
}

Then I compile it as such:

gcc -o open_test.so open_test.c -shared -fPIC -ldl

Then I'm able to run this with e.g.

LD_LIBRARY_PATH=. LD_PRELOAD=open_test.so <some app>

This works on many programs, and I'm able to see which files are opened. But some programs, e.g. MPlayer, apparently call to open(), but the calls go directly to libc without touching my library. Is there any reason this should happen?

Last edited by Themaister (2010-05-17 16:37:41)

Offline

#2 2010-05-17 16:38:06

Themaister
Member
From: Trondheim, Norway
Registered: 2008-07-21
Posts: 652
Website

Re: [SOLVED] LD_PRELOAD and libc problems [C].

Found the problem. Some programs use the open64() call.

Offline

Board footer

Powered by FluxBB