[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

Re: library fuction calls

Dwivedi Ajay kumar wrote (ajayd@xxxxxxxxxx):

> 	I am trying to write a wrapper for one of the library functions
> (more specifically connect() ). Now what I want is whenever some application 
> calls connect()  my_connect should be called. my_connect does some
> checks on the sockaddr and then calls connect().
> 	I have noticed that there are aliases for connect like __connect
> and __libc_connect in the glibc source. I have written the code for
> connect() function which I am compiling as a shared library with -shared
> option. Now my questions are:
> 1. How do I make the system use my connect() function instead of the
>    glibc's connect(). 

If your program that you are trying to work with is not available in source,
and you want to replace the connect calls with your own - you need a library
interposer. Have a look at 
for details. This takes advantage of dynamic linking, but does not always work
(on setuid progs for example).

Most "malloc debuggers" work this way, they replace the C lib malloc/free
calls with their own. freshmeat.net has a wealth of such programs.

> 2. I need to call the glibc's connect() in my connect(). How do i do
> this. (I am trying this by calling __connect and __libc_connect instead in
> my connect and compiling a program with this shared lib. Obviously its not
> working, it seems like I am getting into infinite recursion.)

You need a bit of C macro magic to get this one going. It's not too difficult
to do this, just think about which connect() gets called in each reference and
how #define'ing or #undefine'ing could save you. Or perhaps you can find an
easiery way.

If you can't get it to work, mail me offline and I'll dig up some old code
that I wrote which does similar things with the malloc family.

Lastly, tinkering with libc is generally not advisable. Do it only if you
have a specific reason and know exactly what your're doing (or if you're just
curious and want to have fun, ofcourse ;-).

- Raja