views:

73

answers:

3

I have a server and a client written in C. I try to load a shared library in the server and then pass library function pointers to the client. This way I can change the library without have to compile the client.

Because of every process has its own separate memory space, I wonder if it is possible to load a shared library on a shared memory, pass the function pointers and map the shared memory on the client and then make the client execute the code of the library loaded by the server.

+3  A: 

By definition shared library is shared, so two processes will use same physical memory for library's code segment. So instead of making up some dodgy schemes you can just pass the names of the library and function from server to client and client will get function address using dlopen() + dlsym().

Please note that in this case there will be two copies of data segments (if library has some global or static variables), e.g. if server sets some static variable inside the library function, it's value won't change for the client.

qrdl
Yes, you're right. So, I dlopen the library on server, pass the function names to client.From client, I use dlopen on the same name library (which is already open), and dlsym to get function pointers to execute its code.But according to man dlopen, this should gave me the same handle on both client and server, and I got: 0x970b0d0 for the server and 0x89d2a38 for the client. Does this means the library has been loaded two times?
quimm2003
You got virtual addresses that belong to process address spaces of server and client, but they should map into same physical address.
qrdl
A: 

You could, perhaps, if the OS doesn't call making shared memory also executable a security problem (which it probably should).

But you don't need to. And, you should not be passing pointers to other processes. Instead, give the functions names or code points in your protocol. The shared library mechanism will mean that there is only one copy of the library in memory, unless the OS decides not to share for some reason. If the OS decides not to share, the possible reasons for that will usually cause your pointer passing trick to fail also.

Andrew McGregor
A: 

Thank you very much for your quick answers. You've helped me a lot.

quimm2003