如何在proc目录下增加设备文件
2006-05-28 22:21
316 查看
如何在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
原文网址:
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
相关文章推荐
- IOS中获取各种文件的目录路径的方法,清除沙盒缓存时,如何显示设备的总容量和可用容量
- Android应用程序如何访问/sys和/proc等目录下的系统文件
- 浅析dev目录下设备文件mknod节点gid,uid和mode的如何方便设置
- Linux入职基础-3.1_了解proc目录文件(设备管理)
- Android应用程序如何访问/sys和/proc等目录下的系统文件
- 小知识:如何在mtk功能机平台的 custom目录下增加文件,并使其被系统编译链接
- 浅析dev目录下设备文件mknod节点gid,uid和mode的如何方便设置
- linux下如何找出svn目录下新增加的文件?
- [zz] 浅析dev目录下设备文件mknod节点gid,uid和mode的如何方便设置
- 浅析dev目录下设备文件mknod节点gid,uid和mode的如何方便设置
- Android应用程序如何访问/sys和/proc等目录下的系统文件
- 在/proc文件系统中增加一个目录hello,并在这个目录中增加一个文件world,文件的内容为hello world
- 为Tiny4412设备驱动在proc目录下添加一个可读版本信息的文件
- 浅析dev目录下设备文件mknod节点gid,uid和mode的如何方便设置
- 写一个从/proc/devices 文件中获取设备号的脚本
- 如何解决IIS7上传文件大小限制,.NET 上传文件后 找不到目录解决
- /dev目录下的设备文件解析
- Android 如何利用proc有上层想kernel写文件
- 如何更改MySQL数据库文件目录
- [MapReduce] 如何向map和reduce脚本传递参数,加载文件和目录