Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-25-2012
Banned
 
Join Date: Mar 2012
Posts: 98
Thanks: 49
Thanked 1 Time in 1 Post
Function Interposition in Linux

Hello,

Please I try to apply the mechanism Interposition Function in Linux on the function write (write a socket) as follows:

Code:
# include <sys/types.h>
# include <sys/socket.h>
# include <netinet/in.h>
# include <arpa/inet.h>
# include <netdb.h>
# include <stdio.h>
# include <string.h>
int write (int n, char buffer [32], int sb)
{

int nb = write (so, "Ceci est un exemple de socket!!!", 32);
}

I also have the server code and client code, when the client connects to the server, the server sends a message to the client but when running I got the following error:

Erreur de segmentation
Have you an idea please ?

Thank you so much
Sponsored Links
    #2  
Old 06-25-2012
...@...
 
Join Date: Feb 2004
Location: NM
Posts: 9,657
Thanks: 164
Thanked 645 Times in 622 Posts
write is a system call, and you have write as a local function name. At best this becomes a never-ending recursive call.

Call your function mywrite() or something else, do not name your functions the same thing as syscalls, -read write open and so on.

Interposition is acheived by writing a shared library module (call it mylibrary.so as an example), compling it, then creating a special environment variable, LD_PRELOAD=mylibrary.so, and then running your code. But you still cannot do what your code shows.
Sponsored Links
    #3  
Old 06-25-2012
Banned
 
Join Date: Mar 2012
Posts: 98
Thanks: 49
Thanked 1 Time in 1 Post
Quote:
Interposition is acheived by writing a shared library module (call it mylibrary.so as an example), compling it, then creating a special environment variable, LD_PRELOAD=mylibrary.so, and then running your code.
--> i have done all what you say , i have modified the code like that:
Quote:
size_t write(int fd, const void *buf, size_t count)
{
static size_t (*write_func)(int, const void *, size_t) = NULL;

/* get reference to original (libc provided) write */
if (!write_func)
{
write_func = (size_t(*)(int, const void *, size_t)) dlsym(RTLD_NEXT, "write");
}
return write_func(fd, buffer, sizeof (buffer));
}
I have a question please :how canget the buffer "const void *buf" because I want to change it before sending ?

Thank you so much for help
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
[C/Linux]Help in replacing obsolete function fracche Programming 0 01-29-2012 09:11 AM
cut function in linux kpinto UNIX for Dummies Questions & Answers 2 12-28-2011 12:04 PM
What is the function to get address of the virtual memory block in linux?? powyama UNIX for Advanced & Expert Users 1 09-28-2011 01:04 AM
system() function nd backtrick term in linux Mac91 Shell Programming and Scripting 1 05-05-2011 12:16 PM
Passing global variable to a function which is called by another function sars Shell Programming and Scripting 4 06-30-2008 11:39 AM



All times are GMT -4. The time now is 11:19 PM.