Linux openstack01 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
Makefile:
obj-m:=TestModule.o
VERSION_NUM:=$(shell uname -r)
KERNELBUILD := /lib/modules/`uname -r`/build
CURRENT_PATH:=$(shell pwd)
all:
make -C $(KERNELBUILD) M=$(CURRENT_PATH) modules
clean:
make -C $(KERNELBUILD) M=$(CURRENT_PATH) clean
[root@openstack01 TestModule2]# cat TestModule.c
#ifndef _KERNEL_
#define _KERNEL_
#endif
#ifndef MODULE
#define MODULE
#endif
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/version.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/utsrelease.h>
#include <linux/errno.h>
#include <asm/segment.h>
#include <linux/sched.h>
#include <asm/uaccess.h>
#define DATA_LENGTH 10
MODULE_LICENSE("GPL");
MODULE_AUTHOR("TUO LI HENG");
char kernel_version[] = UTS_RELEASE;
unsigned int test_major = 0;
ssize_t read_test(struct file *file, char * buf, size_t count, loff_t *f_ops)
{
int left, i, *p;
int data[DATA_LENGTH];
p = data;
for(i=0; i<10; ++i)
data[i] = 61;
for(left=count; left>0; --left)
{
copy_to_user(buf, p, 1);
++buf;
++p;
}
return 0;
}
ssize_t write_test(struct file *file, char * buf, size_t count, loff_t *f_ops)
{
return 0;
}
static int open_test(struct inode* inode, struct file *file)
{
return 0;
}
static int release_test(struct inode *inode, struct file *file)
{
return 0;
}
struct file_operations test_fops = {
read: read_test,
write: write_test,
open: open_test,
release: release_test
};
static int hello_init(void)
{
printk(KERN_ALERT "hello, I am Tuotuo\n");
int result = 0;
result = register_chrdev (0, "test", &test_fops);
if(result < 0)
{
printk(KERN_ALERT "test:can't get major number\n");
return result;
}
if(test_major == 0)
test_major = result;
printk(KERN_ALERT "test major:%d/r/n", result);
return 0;
}
static void hello_exit(void)
{
unregister_chrdev(test_major, "test");
printk(KERN_ALERT "googbye Kernel module tuotuo\n");
}
module_init(hello_init);
module_exit(hello_exit);
make 报错
/data/TestModule2/TestModule.c:26:25: error: ‘UTS_RELEASE’ undeclared here (not in a function)
char kernel_version[] = UTS_RELEASE;
[root@openstack01 TestModule2]# grep 'UTS_RELEASE' /usr/src/kernels/3.10.0-514.el7.x86_64/include/generated/utsrelease.h
#define UTS_RELEASE "3.10.0-514.el7.x86_64"
UTS_RELEASE 定义转移到utsrelease.h 文件中
cp /usr/src/kernels/3.10.0-514.el7.x86_64/include/generated/utsrelease.h /usr/src/kernels/3.10.0-514.el7.x86_64/include/generated/uapi/linux
添加 #include <linux/utsrelease.h>
根据打印的
tail -f /var/log/message
Sep 13 11:12:01 localhost kernel: hello, I am Tuotuo
Sep 13 11:12:01 localhost kernel: test major:246/r/n
Sep 13 11:12:01 localhost kernel: test major:246/r/n
[root@openstack01 dev]# mknod test c 246 0
创建字符设备
测试类:
- [root@localhost module]# cat TestApp.c
- #include <stdio.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- int main()
- {
- char buf[20] = {0,};
- int i = 0;
- int p = open("/dev/test", O_RDWR);
- if(p == -1)
- {
- printf("cannot open\n");
- exit(0);
- }
- printf("buf addr %ui\r\n",buf);
- read(p, buf, 10);
- for(i = 0; i<10; i++)
- {
- printf("%s\r\n",buf+i);
- }
- close(p);
- return 1;
- }
- gcc TestApp.c -o TestApp
[root@openstack01 TestModule2]# ./TestApp
buf addr 2882151968i
==========
=========
========
=======
======
=====
====
===
==
=