如何在proc目录下增加设备文件
因为原来的代码编译时存在错误,因此我作了改动,并在redhat 9.0(内核版本是2.4.20-8)上测试通过。分为三个文件:myproc.c,test.c,Makefile,测试流程很简单,在当前目录中输入"./test",首先将在/proc目录下创建一个"myproc"设备文件,然后向此文件写入一个字符串,接着读出来,在输出中应该看到前后字符串是一样的。代码如下:
/* myproc.c - create a "myproc" file in /proc
*
* Copyright (C) 2005 by linfeng12
*/
#ifndef __KERNEL__
#define __KERNEL__
#endif
#ifndef MODULE
#define MODULE
#endif
#ifndef __KERNEL_SYSCALLS__
#define __KERNEL_SYSCALLS__
#endif
#include <linux/autoconf.h>
#include <linux/version.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/poll.h>
#include <linux/fs.h>
#include <linux/sched.h>
#include <linux/file.h>
#include <asm/uaccess.h>
static char msg[512];
static struct proc_dir_entry *myproc_DevEntry;
static int
myproc_DevOpen(struct inode *inode, // IN: Inode of the proc file
struct file *file) // IN: File for this process' proc file access
{
printk("myproc_DevOpen()/n");
return 0;
}
static int
myproc_DevRelease(struct inode *inode, // IN: Inode of released file
struct file *file) // IN: Released file
{
printk("myproc_DevRelease()/n");
return 0;
}
static ssize_t
myproc_DevRead(struct file *file, // IN: File pointer of file user is reading
char *buf, // OUT: User buffer to copy to
size_t count, // IN: Number of bytes to copy to user
loff_t *offset) // IN: Offset to read from. ignored.
{
printk("myproc_DevRead(): count=%d/n", count);
int i=0;
/* Copy bytes out to the user from a kernel buffer -- msg */
for (i=0; i<count && i<512; i++)
{
put_user(msg[i], buf+i);
}
return count;
}
static ssize_t
myproc_DevWrite(struct file *file, // IN: File pointer of file user is writing to
const char *buf, // IN: User buffer to copy from
size_t count, // IN: Number of bytes to copy
loff_t *offset) // IN: Offset in the file to write to. ignored.
{
printk("myproc_DevWrite(): count=%d/n", count);
int i=0;
memset(msg, 0, sizeof(msg));
/* Copy bytes in from a user buffer to a kernel buffer -- msg */
for (i=0; i<count && i<512; i++)
{
get_user(msg[i], buf+i);
}
return count;
}
struct file_operations fop = {
owner: THIS_MODULE,
open: myproc_DevOpen,
read: myproc_DevRead,
write: myproc_DevWrite,
release: myproc_DevRelease,
};
int init_module(void)
{
printk("myproc: init_module()/n");
myproc_DevEntry = create_proc_entry("myproc", S_IRUSR | S_IWUSR, NULL);
myproc_DevEntry->owner = THIS_MODULE;
myproc_DevEntry->proc_fops = &fop;
return 0;
}
void cleanup_module(void)
{
printk("myproc: cleanup_module()/n");
remove_proc_entry("myproc", myproc_DevEntry);
}
/* test.c - test the "myproc" file in /proc
*
* Copyright (C) 2005 by linfeng12
*/
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
int main(void)
{
int fd, len;
char buf[16] = "test myproc";
fd = open("/proc/myproc", O_RDWR);
if (fd < 0)
{
printf("open proc file err!/n");
return -1;
}
len = write(fd, buf, strlen(buf));
printf("write the buf : %s, len: %d/n", buf, len);
memset(buf, 0, sizeof(buf));
len = read(fd, buf, sizeof(buf));
printf("read the buf : %s, len: %d/n", buf, len);
close(fd);
return 0;
}
# Makefile for a multifile kernel module
CC=gcc
MODCFLAGS := -Wall -DMODULE -D__KERNEL__ -DLINUX -I/usr/src/linux/include
all: test myproc.o
myproc.o: myproc.c
$(CC) -O2 $(MODCFLAGS) -c myproc.c -o myproc.o
insmod myproc.o
test:
$(CC) -O2 $(MODCFLAGS) test.c -o test
clean:
rm -f test *.o
rmmod myproc