This example code is file access(read) into the kernel level..
There is "/tmp/rambo.txt"...
and open() is ok...
but, this function is not work...
I can see the error message...<data is NULL> return -14...
# pwd
/tmp
# ls -la
drwxrwxrwt 4 root 0 140 Jan 2 05:04 .
drwxrwxrwx 23 500 501 4096 May 11 2010 ..
-rwxrwxrwx 1 root 0 40 Jan 2 05:04 rambo.txt
#
# cat rambo.txt
abcdefgzjdkzdwdadasdoda90d12334afdaafaf
#
Code:
EXAMPLE CODE.....
struct file * fd;
char * filename="/tmp/rambo.txt";
char *buff=NULL;
printk("read function...\n");
fd=sfile_open(filename,O_RDONLY,0);
if (fd != NULL) {
printk("OK.....\n");
count=10;
offset=0;
sfile_read(fd,offset,buff,count);
printk(" buff %s\n",buff);
}else{
printk("not ok\n");
}
After example excutition
=============
read function...
OK.....
file_read...=
data is <NULL>,ret is -14
====================
What is the reason of "data is <NULL>".
Return is -14 is also problem...
Code:
#if 1
struct file* sfile_open(const char* path, int flags, int rights) {
struct file* filp = NULL;
mm_segment_t oldfs;
int err = 0;
oldfs = get_fs();
set_fs(get_ds());
filp = filp_open(path, flags, rights);
set_fs(oldfs);
if(IS_ERR(filp)) {
err = PTR_ERR(filp);
return NULL;
}
return filp;
}
void file_close(struct file* file) {
filp_close(file, NULL);
}
int sfile_read(struct file* file, unsigned long long offset, unsigned char* data, unsigned int size) {
mm_segment_t oldfs;
int ret;
printk("file_read...=\n");
oldfs = get_fs();
set_fs(get_ds());
ret = vfs_read(file, data, size, &offset);
printk("data is %s,ret is %d\n",data,ret);
set_fs(oldfs);
return ret;
}