如何在proc目录下增加设备文件

如何在proc目录下增加设备文件


原文网址:
http://www.linuxforum.net/forum/gshowflat.php?Cat=&Board=linuxK&Number=335192&page=7&view=expanded&sb=5&o=all&vc=1

         因为原来的代码编译时存在错误,因此我作了改动,并在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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值