Transcript open/close

어플리케이션에서 접근하기 1
open/close
미리 알아 두기
•
•
•
•
캐릭터 디바이스 드라이버
디바이스 노드 파일 필요
주 번호와 부 번호
저 수준 파일 입출력 함수 사용.
(open/close/read/write/lseek 등으로 정해진 형식)
• 파일 오퍼레이션을 통해 지정
목차
•
•
•
•
•
•
•
•
•
open/close 설명
드라이버, 어플리케이션 예제
nodefile 만들기
우분투에서 테스트 해보기
함수 원형(open/close)
주 번호와 노드 파일
nodefile 만들기
파일 오퍼레이션
THIS_MODULE
어플리케이션에서 제어하기
어플리케이션
어플리케이션 영역
커널 영역
디바이스 드라이버
어플리케이션에서 제어하기
어플리케이션
Device nodefile
디바이스 드라이버
어플리케이션에서 제어하기
어플리케이션
Device nodefile
디바이스 드라이버
open/close 설명
반환
저_수준_입출력_함수( 인자 );
반환 드라이버_함수(인자);
open/close 설명
반환
저_수준_입출력_함수( 인자 );
sys_call
반환 드라이버_함수(인자);
open/close 설명
int open (const char *, int …);
int xxxx_open (struct inode *, struct file *);
open/close 설명
int open (const char *, int …);
#include <linux/fs>
…
struct file_operations {
.open = xxxx_open,
};
int xxxx_open (struct inode *, struct file *);
open/close 설명
int open (const char *, int …);
#include <linux/fs>
…
struct file_operations {
.open = xxxx_open,
};
int register_chrdev(
unsigned int major,
const char *name,
const struct file_operations *fops)
int xxxx_open (struct inode *, struct file *);
open/close 설명
int open (const char *, int …);
mknod /dev/xxxx_drv c 241 0
#include <linux/fs>
…
struct file_operations {
.open = xxxx_open,
};
/dev/xxxx_drv
int register_chrdev(
unsigned int major,
const char *name,
const struct file_operations *fops)
int xxxx_open (struct inode *, struct file *);
open/close 설명
int open (const char *, int …);
mknod /dev/xxxx_drv c 241 0
#include <linux/fs>
…
struct file_operations {
.open = xxxx_open,
};
/dev/xxxx_drv
int register_chrdev(
unsigned int major,
const char *name,
const struct file_operations *fops)
int xxxx_open (struct inode *, struct file *);
open/close 설명
int open (const char *, int …);
mknod /dev/xxxx_drv c 241 0
#include <linux/fs>
…
struct file_operations {
.open = xxxx_open,
};
/dev/xxxx_drv
int register_chrdev(
unsigned int major,
const char *name,
const struct file_operations *fops)
int xxxx_open (struct inode *, struct file *);
open/close 설명
int close (int fd);
mknod /dev/xxxx_drv c 241 0
#include <linux/fs>
…
struct file_operations {
.open = xxxx_close,
};
/dev/xxxx_drv
int unregister_chrdev(
unsigned int major,
const char *name,
const struct file_operations *fops)
int xxxx_close (struct inode *, struct file *);
Application file
#include <fcntl.h>
// O_RDWR
void main( int argc, char **argv )
{
int fd;
char *filename = "/dev/xxxx_drv";
fd = open(filename,O_RDWR);
close(fd);
}
return 0;
Device Driver file
#include
#include
#include
#include
<linux/init.h>
<linux/module.h>
<linux/kernel.h>
<linux/fs.h>
#include "dev_xxxx.h"
static int xxxx_open( struct inode *inode, struct file *filp )
{
printk("DEV %s)\n", __func__);
return 0;
}
static int xxxx_release( struct inode *inode, struct file *filp )
{
printk("DEV %s)\n", __func__);
return 0;
}
static struct file_operations xxxx_fops = {
.open
= xxxx_open,
.release
= xxxx_release,
};
static int xxxx_init( void )
{
register_chrdev( DEV_XXXX_MAJOR_NUMBER ,
DEV_XXXX_NAME, &xxxx_fops );
}
return 0;
static void xxxx_exit( void )
{
unregister_chrdev( DEV_XXXX_MAJOR_NUMBER,
DEV_XXXX_NAME );
}
module_init( xxxx_init );
module_exit( xxxx_exit );
MODULE_AUTHOR("Lee Dong-su");
MODULE_DESCRIPTION("Module Test");
MODULE_LICENSE( "Dual BSD/GPL" );
Device Driver header file
#define DEV_XXXX_MAJOR_NUMBER 220
#define DEV_XXXX_NAME "Open/Close Test Driver"
nodefile 만들기
• 타겟 머신 상에서….
- mknod /dev/xxxx_drv c 220 0
우분투에서 테스트 해보기
# 타겟 머신 상에서….
ubuntu#)insmod xxxx_drv.ko
ubuntu#)mknod /dev/xxxx_drv c 220 0
ubuntu#)./app_drv
ubuntu#)dmesg
디바이스 드라이버 Open/Close
전반 끝^^
App 의 open 원형
• open 함수
int open (const char *, int, … );
ARG 1 : 파일 이름.
ARG 2 : O_RDWR
O_RDONLY
O_WRONLY
O_CREAT(X)
RETURN :
파일디스크립트 또는 에러(음수)
Dev 의 open 원형
• open 함수
int xxxx_open (struct inode *, struct file *);
ARG 1 : inode 구조체.
주,부 번호를 얻어 올 수 있음.
ARG 2 : filp->f_flags
RETURN :
정상일때 0, 비 정상을 에러(음수)
App 의 close원형
• close 함수
int close (int fd);
RETURN :
파일디스크립트 또는 에러(음수)
Dev 의 close 원형
• close 함수
int xxxx_close (struct inode *, struct file *);
ARG 1 : inode 구조체.
주,부 번호를 얻어 올 수 있음.
ARG 2 : filp->f_flags
RETURN :
정상일때 0, 비 정상을 에러(음수)
주 번호와 노드 파일
• 디바이스 노드 파일 위치.
- /dev
• 주 번호 확인.
- ls –al /dev/xxxxx
crw-rw-rw- 1 root tty 5, 0 Dec 14 22:21 /dev/tty
• 등록된 디바이스 확인.
- /proc/devices
• 사용 가능 주 번호.
- 234 – 239, 240 – 254
• mknod
- 함수 형태.
- busybox 명령어.
• dev_t ( 32bits )
주번호(12bits)
부번호(20bits)
nodefile 만들기
• mknod 명령어
- mknod 파일명 c 주번호 부번호
• mknod 함수
int mknod(const char *pathname, mode_t mode, dev_t dev);
pathname
파일명
mode
S_IFCHR
S_IRWXU
: 문자 디바이스
: 사용자 일기/쓰기/실행 권한
dev
:
MKDEV( 주번호, 부번호)
RETURN
:
파일 오퍼레이션
struct file_operations {
struct module *owner;
loff_t (*llseek) (struct file *, loff_t, int);
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
int (*readdir) (struct file *, void *, filldir_t);
unsigned int (*poll) (struct file *, struct poll_table_struct *);
int (*open) (struct inode *, struct file *);
int (*release) (struct inode *, struct file *);
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
// int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
};
등록/해제 함수
int register_chrdev(unsigned int major, const char *name, struct file_operations *fops)
•
ARG 1 : 주 번호
ARG 2: 디바이스 이름
ARG 3: 파일 구조체
RETURN :
정상 일때 0이며, 에러 코드 반환.
•
int unregister_chrdev(unsigned int major, const char *name)
ARG 1 : 주 번호
ARG 2: 디바이스 이름
RETURN :
정상 일때 0이며, 에러 코드 반환.
부 번호 사용하기
• MINOR() 매크로
– #include <asm-generic/percpu.h>
– MINOR(inode->i_rdev)
static int xxxx_open( struct inode *inode, struct file *filp )
{
// Get Major/Minor
printk("DEV ) Major: %d, Minor: %d\n", MAJOR(inode->i_rdev),
MINOR(inode->i_rdev));
return 0;
}
이 중 Open 방지하기
• try_module_get()
• put_module()
static int xxxx_open( struct inode *inode, struct file *filp )
{
try_module_get(THIS_MODULE);
}
return 0;
static int xxxx_release( struct inode *inode, struct file *filp )
{
module_put(THIS_MODULE);
return 0;
}
THIS_MODULE
• 드라이버 정보 포한
– init / exit 함수
– open / close 여부
– 드라이버 이름
디바이스 드라이버 Open/Close
후반 끝^^