hi all,
After reading all those mails on how to read file in kernel module, i think i should share my way of reading a file in a module.
I don't know whether it is right or wrong, but i have tried and it works.
Please give me views about this way of reading a file.
The code below is there to read a configuration file having count of number of systemcalls.
int ERR_RD_FILE;
static int errno;
int read_config_file(const char *fn)
{
long l;
int i,j,no_of_syscalls=0;
set_fs(KERNEL_DS);
fd = open(fn, O_RDONLY, 0);
set_fs(USER_DS);
if (fd == -1)
{
printk("\nUnable to load config file '%s' %d.", fn, errno);
ERR_RD_FILE=0;
return NULL;
}
set_fs(KERNEL_DS);
l = lseek(fd, 0L, 2);
set_fs(USER_DS);
if (l <= 0 || l > 131072)
{
printk("\nTOO big a file '%s' %d.", fn, errno);
sys_close(fd);
ERR_RD_FILE=0;
return NULL;
}
set_fs(KERNEL_DS);
lseek(fd, 0L, 0);
set_fs(USER_DS);
set_fs(KERNEL_DS);
no_of_syscalls=(int*)vmalloc(sizeof(int));
if( (read(fd,no_of_syscalls,sizeof(int) ))!=sizeof(int) )
{
set_fs(USER_DS);
printk("\nFailed to read '%s' %d.", fn, errno);
vfree(no_of_syscalls);
sys_close(fd);
set_fs(USER_DS);
ERR_RD_FILE=0;
return NULL;
}
sys_close(fd);
ERR_RD_FILE=1;
return no_of_syscalls;
}