[LCP]LD_PRELOAD

openroad id97 at inf.its-sby.edu
Tue Jul 13 00:24:02 UTC 2004


i have problem with my LD_PRELOAD.
my case is:
i want to override open,readdir and close function of libc.
my list file of directory /home/id/kirim is :

-rw-r--r--    1 id       id            714 Jul 12 23:02 mylib.c
-rwxr-xr-x    1 id       id          22965 Jul 12 23:03 mylib.so

1. this is the source code(mylib.c)

/************source:mylib.c***********
#define _GNU_SOURCE
#include <sys/syscall.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dlfcn.h>
#include <dirent.h>

static void init (void) __attribute__ ((constructor));

int (*real_open)(__const char *name, int flags, mode_t mode);
struct dirent *(*real_readdir)(DIR *dir);
int (*real_close)(int fd);

static void init (void)
{
        real_open = dlsym(RTLD_NEXT,"open");
        real_readdir  = dlsym(RTLD_NEXT,"readdir");
        real_close  = dlsym(RTLD_NEXT,"close");
}
int open(const char *pathname, int flags, mode_t mode)
{
        printf("open called \n");
        return(real_open(pathname,flags,mode));
}
struct dirent *readdir(DIR *dir)
{
        printf("readdir called");
        return(real_readdir(dir));
}

/***************end of source mylib.c*****************************/

2. i compile it with :
gcc -g -fpic -shared -ldl -o mylib.so mylib.c

3. then i use command
   export LD_PRELOAD=/home/id/kirim/mylib.so
4. my open() function is work fine,i got :

$less mylib.c
..............
...blah..blah
open called

5. This is my problem.when i use ls -l, i expect string like 'readdir
called' cause ls use readdir function(readdir() called in 'ls' source
code).but it doesnt appear.this is ls output

id at bisma:~/kirim$ ls -l
total 28
-rw-r--r--    1 id       id            714 Jul 12 23:02 mylib.c
-rwxr-xr-x    1 id       id          22965 Jul 12 23:03 mylib.so
id at bisma:~/kirim$


I can't catch it.Anyone please give me an advice/solution bout my program.

Best Regards,
id
---





More information about the linuxCprogramming mailing list