반응형
simple.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | #include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h> #include <linux/fs.h> #include <asm/uaccess.h> int simple_open(struct inode* pinode, struct file* pfile) { printk(KERN_INFO "Open Simple\n"); return 0; } int simple_release(struct inode* pinode, struct file* pfile) { printk(KERN_INFO "Release Simple\n"); return 0; } ssize_t simple_read(struct file* pfile, char* ubuf, size_t length, loff_t* offset) { int result; char message[20] = "Lee Ho Jun"; result = copy_to_user(ubuf, message, length); if(result <0) { printk(KERN_ERR "Copy_to_user Error!\n"); return result; } return 0; } ssize_t simple_write(struct file* pfile, const char* ubuf, size_t length, loff_t* offset) { char message[20]; int result; result = copy_from_user(message,ubuf,length); if(result <0) { printk(KERN_ERR "Copy_to_user Error!\n"); return result; } printk(KERN_INFO "from user : %s\n",message); return 0; } struct file_operations simple_fops ={ read : simple_read, write : simple_write, open : simple_open, release : simple_release, }; int simple_init(void) { int result; result = register_chrdev(249,"simple_driver", &simple_fops); if(result<0) { printk(KERN_ERR "Registration Error\m"); return result; } printk(KERN_INFO "Hello Simple\n"); return 0; } int simple_exit(void) { unregister_chrdev(249,"simple_driver"); printk("Bye _ Simple\n"); return 0; } module_init(simple_init); module_exit(simple_exit); MODULE_LICENSE("GPL"); | cs |
simple 드라이버를 만드는 소스이다.
Read() 함수는 호출되면 user 에게 message에 저장되어 있는 문자열을 넘겨준다.
Write() 함수는 user가 호출하면서 넘겨준 문자열을 넘겨 받게 되고 해당 문자열을 커널 에서 출력한다.
test.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <stdlib.h> #include <string.h> int main(void) { int fd; char buffer[20]; char number[20] = "5172174"; fd = open("/dev/simple",O_RDWR); if(fd<0) { perror("open"); exit(1); } write(fd,number,strlen(number)+1); read(fd,buffer,20); printf("from kernel : %s\n",buffer); close(fd); return 0; } | cs |
실행 결과
반응형
'보안공부 > Linux' 카테고리의 다른 글
ssize_t , size_t (0) | 2017.07.02 |
---|---|
volatility 명령어 (0) | 2017.01.10 |
디바이스 드라이버 만들기 (0) | 2016.05.16 |
2016-05-09 심볼공유 (0) | 2016.05.09 |
2016.05.02 수업 (0) | 2016.05.02 |