ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am intercepting sys_write system call and within my_sys_write i will call the original syswrite.
Before calling the original sys_write, i have to get the contents of the file which will be replaced
by the current write.
So i open a new file descriptor inside my_sys_write and read the current buffer before calling the
original sys_write.
I have a problem in reading the current buffer using sys_read which returns zero .
I am giving the relevant code below
Code:
ssize_t my_sys_write(unsigned int fd, const char * buf, size_t count)
{
// Get the name of absolute name from file descritpor
// calling the sys_open to get a new handle for reading
int openForRead = OriginalSysOpen(absoluteFileName,O_RDONLY,0777);
// calling sys_read and storing the contents in the tmpBuf, bytesRead always return zero
int bytesRead = OriginalSysRead(openForRead,tmpBuf, count );
printk(" OPENFILEHANDLE %d BYTESREAD %d \n", openForRead,bytesRead);
OriginalSysClose(openForRead);
int returnValue = OriginalSysWrite(fd,buf,count);
return returnValue;
}
After few hours of frustation, i am posting here to get some help.
Thank u for the reply, Though i haven't yet gone through the document i have taken care of the user - kernel addr space using
the get_fs and set_fs. Code actually looks like below but still i am receiveing the number of bytes read as zero.
Code:
ssize_t my_sys_write(unsigned int fd, const char * buf, size_t count)
{
// Get the name of absolute name from file descritpor
mm_segment_t tmpfs;
tmpfs = get_fs();
set_fs (get_ds());
// calling the sys_open to get a new handle for reading
int openForRead = OriginalSysOpen(absoluteFileName,O_RDONLY,0777);
// calling sys_read and storing the contents in the tmpBuf, bytesRead always return zero
int bytesRead = OriginalSysRead(openForRead,tmpBuf, count );
printk(" OPENFILEHANDLE %d BYTESREAD %d \n", openForRead,bytesRead);
OriginalSysClose(openForRead);
int returnValue = OriginalSysWrite(fd,buf,count);
set_fs(tmpfs);
return returnValue;
}
I would like to add some more points that i noted while trying to fix the above mentioned error.
I wrote a simple c program which makes a call to write function. This write function was trapped by the kernel module (see above code) and successfully read the contents that are supposed to be replaced by the "OriginalSysWrite"
Instead if i open a file in vim editor and made some modifications and save the file. Here again the write was intercepted by "my_sys_write" but the "OriginalSysRead" inside "my_sys_read" returns zero.
If anyone can help me on this, please reply
don't get offended, but: these are things you need to figure out yourself. people can't and shouldn't hold your hand when you're kernel hacking. read the source for the functions giving you problem, do some simple tests, do w/e it takes to solve the problem. you can do it. i have faith.
Really heartening to see your mail, thanks for your suggestions and i will do the same.
Thanks once again for the support and confidence you are giving.
Its time to close the thread. At last i found out the where the problem lies. vi or any editors open the file in trunc mode before doing a write.Hence when i trapped
the sys_write system call, the file descriptor is pointer to a empty file. And i was trying to read from this empty file which was returning zero bytes.
Thanks for all your support.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.