Function Interposition in Linux


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Function Interposition in Linux
# 1  
Old 06-25-2012
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
# 2  
Old 06-25-2012
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.
# 3  
Old 06-25-2012
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
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

3. Shell Programming and Scripting

Will files, creaetd in one function of the same script will be recognized in another function?

Dear All. I have a script, which process files one by one. In the script I have two functions. one sftp files to different server the other from existing file create file with different name. My question is: Will sftp function recognize files names , which are created in another... (1 Reply)
Discussion started by: digioleg54
1 Replies

4. Homework & Coursework Questions

C++ with Linux - writing a "tee"-like function

Greetings, everyone. 1. The problem statement, all variables and given/known data: I'm running into a problem with my program concerning the actual output it does. When I open the file that gets the output, it contains a large number of hex(?) variables and not what the user wants. The... (0 Replies)
Discussion started by: assignmentoper
0 Replies

5. Programming

[C/Linux]Help in replacing obsolete function

Hi guys, I need help on some function replacement cause I get obsolete function warning(and I must remove it): -gethostbyaddr(arg1,arg2,arg3) -gethostbyname(arg1) -getservbyname(arg1,arg2) can be replaced with getaddrinfo(arg1,arg2,arg3,arg4) but I'm not able to undestand how(libraries... (0 Replies)
Discussion started by: fracche
0 Replies

6. UNIX for Dummies Questions & Answers

cut function in linux

hi; i have file with a lote of data i would like to cut only numbers that start with prefix 20408 my file contain thousands of rows like this ,204080700152648,20111215,,,20 31630536259,204080662332510,20 31622520779,204080660098298,20 31651343790,204080130071280,20... (2 Replies)
Discussion started by: kpinto
2 Replies

7. UNIX for Advanced & Expert Users

What is the function to get address of the virtual memory block in linux??

I want address of current virtual memory block? i am using fedora10:wall::wall: (1 Reply)
Discussion started by: powyama
1 Replies

8. Shell Programming and Scripting

system() function nd backtrick term in linux

I want to know about the difference between system() function nd backtrick term in linux???? (1 Reply)
Discussion started by: Mac91
1 Replies

9. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

10. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies
Login or Register to Ask a Question