커널레벨 파일 건드리기 예제 에러좀 봐주세요....
      글쓴이: chxooi / 작성시간: 화, 2010/05/11 - 4:04오후    
  
  커널 레벨에서 file 을 access 하는 코드입니다.
read 를 했는데 파일 내용이 뻔히 있는데도 못읽어와서요....
뭐가 잘못된건지 궁금해서요...
vfs_read 의 return value 도 -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
#
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");
    }실행결과...
=============
read function...
OK.....
file_read...=
data is ,ret is -14
====================
계속 data 에 NULL 이찍히는데 뭐가 문제일까요???
#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;
}Forums: 


Segment 값이 원래로 돌아와야 하지 않을까요?
set_fs(oldfs); printk("data is %s,ret is %d\n",data,ret);로 순서가 바뀌어야 mm_segment_t를 건드리고 나서 원상복귀해야 data가 살아 나지 않을까요?
물론, kernel에서 file access 하는 것을 무척 제한하고 있는데, 굳이 kernel 영역에서 file access를 꼭 해야 하는 이유라도...?
~~~~~~~~~ Signature
답변감사한데요..결과는똑같네요..
바꿨는데도 안되긴 마찬가지네요...흑...거의 된것같았는데...
파일 access 는 일단 나중에 다시 할건데 kernel mode 에서 해결을 해야 나중에 다시user mode 로 바꿀 수 있게 프로젝트 진행중입니다.
유저에서 다이렉트로 해결하는건 쉽게 해결이되서요....
커널 모드 변환이 필요한데 쉽지 않네요...
음.
vfs_read를 쓰시는 이유가 있나요?
보통은 이렇게 쓰지 않나요?
아, 그리고 밑의 분이 말씀하신데로, data의 준비 (heap에서 가져온 주소이던, stack에서 쓰는 것이던)도 확인해 보세요.
~~~~~~~~~ Signature
강추 ^^/
강추
^^/
^^/
char
char *buff=NULL;
read하는데 담는 그릇은 준비된건가요???
^^/
^^/
댓글 달기