Kernel system call안에서 파일 퍼미션 알아내는 방법?

highwind의 이미지

Kernel안에 새로운 system call을 만들고 있습니다. 파일 이름이 (path까지 포함한)주어졌을때 그 파일이 존제여부와 그 파일의 퍼미션을 알고싶어 sys_access와 sys_stat을 써보았지만 컴파일도 안되네요. ㅠㅠ

man access와 man stat에 써져있는 대로 해보았지만 kernel안이라 뭔가 잘못된거 같습니다.

user space에서 테스트 해봤을땐 잘 됐는데... 쩝... ㅠㅠ

	//check if the file exists and has write permission
	if (sys_access(filename, W_OK) < 0) { 
		printk("File with write permission not found.\n"); //debug
		return -ENOENT;
	}
 
	//try creating the attribute directory
	//  first get the file permission
	filestat = (struct stat*) kmalloc(sizeof(struct stat), GFP_KERNEL);
	if(sys_stat(filename, filestat) < 0) { //SYSTEM CALL!
		printk("Stat failed.\n"); //debug
		return -ENOENT;
	}

System call을 만드는 문서는 web을 찾아봐도 많이 있지도 않고... 힘드네요. 뭐가 문제이죠?

익명사용자의 이미지

오래되서 기억이 확실친 않지만,
커널 안에서는 clib 중 상당수를 사용할수 없을꺼에요.
printf대신에 printk를 쓰셔야하고... 그런것처럼.
sys_... 함수가 커널 함수에 포함되는지 모르지만,
포함되지 않는다면 다른 함수를 찾아보셔야 할듯합니다.

prether의 이미지

asm/unistd.h에 정의된

_syscall0(), _syscall1(), _syscall2() ...
이라는 wrapper macro function을 이용하면
커널에서도 시스템 콜을 부를 수 있습니다.

postfix로 붙은 숫자는 패러미터 수를 가리킵니다.

stat() 시스템 콜은 패러미터가 2개이니

_syscall2(long, stat, char *, filename, struct stat *, statbuf)

라고 부르면 될겁니다.

/***************************************
Being the one is just like being in love.
***************************************/

bushi의 이미지

vfs_stat() 사용하세요.
대강...

mm_segment_t fs;
 
fs = get_fs();
set_fs(get_ds());
vfs_stat(...., .....);
set_fs(fs);

bushi의 이미지

음... 뭐 read/write 도 아니니 set_fs()/get_fs() 는 필요없을 지도 모르겠습니다.