---------- Forwarded message ---------- From: Brandon Niemczyk <bniemczyk@xxxxxxxxx> Date: Wed, 6 Oct 2004 02:28:09 -0400 Subject: Re: How to read from kernel ? To: Danett song <danett18@xxxxxxxxxxxx> Why do you want to read a file from a kernel module? I would think it would be better to implement something in proc, sysctl, or something. On Tue, 5 Oct 2004 20:02:07 -0300 (ART), Danett song <danett18@xxxxxxxxxxxx> wrote: > 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/ > > -- Brandon Niemczyk http://bniemczyk.doesntexist.com -- Brandon Niemczyk http://bniemczyk.doesntexist.com -- Kernelnewbies: Help each other learn about the Linux kernel. Archive: http://mail.nl.linux.org/kernelnewbies/ FAQ: http://kernelnewbies.org/faq/