How to read from kernel ?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello,

I tryed figure out how to read a file from a LKM, i
done like this:

At init_module

int init_module(void){

        or_open=sys_call_table[__NR_open];
        or_read=sys_call_table[__NR_read];
        or_close=sys_call_table[__NR_close];

        return(0);
}

So the code in a function that need to read a file:

char tmp[512];
char buff[1024];
int ret, size;
mm_segment_t old_fs;

memset(tmp, 0, sizeof(tmp));

strcpy(tmp, "/tmp/myfile.txt");

//old_fs = get_fs();
//set_fs(get_ds());

ret = (*or_open) (tmp, O_RDONLY|O_LARGEFILE, 0640);

//set_fs(old_fs);

if ( ret < 0){

      printk("\nFailed to open");
      return (0);

}

memset(buff, 0, sizeof(buff));

size = (*or_read)(ret, buff, sizeof(buff));

printk("\nContents is %s", buff);

(*or_close)(ret);

Well, the problem is if i don't use get_fs() and
set_fs() i always get value ZERO from open! :(

If i use get_fs() and set_fs() then my kernel crash.
What i'm doing wrong ? Can someone give me a example ?

Someone can say that i shouldn't use the read, open,
etc from syscall_table... but i only found this
example in internet, if someone present-me a better
way i will use.

Regards,



	
	
		
_______________________________________________________ 
Yahoo! Acesso Grátis - Internet rápida e grátis. Instale o discador agora! http://br.acesso.yahoo.com/

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux