分享
 
 
 

/proc文件系统编程

王朝other·作者佚名  2006-11-24
窄屏简体版  字體: |||超大  

./proc文件系统

在Linux中有一个另外的机制来使内核及内核模块发送信息给进程——/proc文件系统。

/proc文件系统最初是设计使得容易得到进程的信息(从名字可以看出),现在却被任意一块

有内容需要报告的内核使用,比如拥有模块列表的/proc/modules和拥有内存使用统计信息的

/proc/meminfo。

使用proc文件系统的方法很象使用设备驱动——你创建一个数据结构,使之包含/proc

文件需要的全部信息,包括所有函数的句柄(在我们的例子里只有一个,在试图读取/proc

文件时调用)。然后,用init_module注册这个结构,用cleanup_module注销。

我们使用proc_register_dynamic(注3.1)的原因是我们不希望决定以后在文件中使用的

索引节点数,而是让内核来决定它,为了防止冲突。标准的文件系统是在磁盘上而不是在内

存(/proc的位置在内存),在这种情况下节点数是一个指向文件的索引节点所在磁盘地址的

指针。这个索引节点包含了文件的有关信息比如文件的访问权限以及指向磁盘地址的指真或

者文件数据的位置。

因为在文件打开或关闭时我们没有调用,所以在模块里无处可放宏

MOD_INC_USE_COUNT和MOD_DEC_USE_COUNT,而且如果文件被打开了或者模块被

删除了,就没有办法来避免这个结果。下一章我们将会看到一个更困难的处理/proc的方法,

但是也更加灵活,也能够解决这个问题。

ex procfs.c

/* procfs.c - create a "file" in /proc

* Copyright (C) 1998-1999 by Ori Pomerantz

*/

/* The necessary header files */

/* Standard in kernel modules */

#include <linux/kernel.h> /* We're doing kernel work */

#include <linux/module.h> /* Specifically, a module */

/* Deal with CONFIG_MODVERSIONS */

#if CONFIG_MODVERSIONS==1

#define MODVERSIONS

#include <linux/modversions.h>

#endif

/* Necessary because we use the proc fs */

#include <linux/proc_fs.h>

/* In 2.2.3 /usr/include/linux/version.h includes a

* macro for this, but 2.0.35 doesn't - so I add it

* here if necessary. */

#ifndef KERNEL_VERSION

#define KERNEL_VERSION(a,b,c) ((a)*65536+(b)*256+(c))

#endif

/* Put data into the proc fs file.

Arguments

=========

1. The buffer where the data is to be inserted, if

you decide to use it.

2. A pointer to a pointer to characters. This is

useful if you don't want to use the buffer

allocated by the kernel.

3. The current position in the file.

4. The size of the buffer in the first argument.

5. Zero (for future use?).

Usage and Return Value

======================

If you use your own buffer, like I do, put its

location in the second argument and return the

number of bytes used in the buffer.

A return value of zero means you have no further

information at this time (end of file). A negative

return value is an error condition.

For More Information

====================

The way I discovered what to do with this function

wasn't by reading documentation, but by reading the

code which used it. I just looked to see what uses

the get_info field of proc_dir_entry struct (I used a

combination of find and grep, if you're interested),

and I saw that it is used in /fs/proc/array.c.

If something is unknown about the kernel, this is

usually the way to go. In Linux we have the great

advantage of having the kernel source code for

free - use it.

*/

int procfile_read(char *buffer,

char **buffer_location,

off_t offset,

int buffer_length,

int zero)

{

int len; /* The number of bytes actually used */

/* This is static so it will still be in memory

* when we leave this function */

static char my_buffer[80];

static int count = 1;

/* We give all of our information in one go, so if the

* user asks us if we have more information the

* answer should always be no.

*

* This is important because the standard read

* function from the library would continue to issue

* the read system call until the kernel replies

* that it has no more information, or until its

* buffer is filled.

*/

if (offset > 0)

return 0;

/* Fill the buffer and get its length */

len = sprintf(my_buffer,

"For the %d%s time, go away!\n", count,

(count % 100 > 10 && count % 100 < 14) ? "th" :

(count % 10 == 1) ? "st" :

(count % 10 == 2) ? "nd" :

(count % 10 == 3) ? "rd" : "th" );

count++;

/* Tell the function which called us where the

* buffer is */

*buffer_location = my_buffer;

/* Return the length */

return len;

}

struct proc_dir_entry Our_Proc_File =

{

0, /* Inode number - ignore, it will be filled by

* proc_register[_dynamic] */

4, /* Length of the file name */

"test", /* The file name */

S_IFREG | S_IRUGO, /* File mode - this is a regular

* file which can be read by its

* owner, its group, and everybody

* else */

1, /* Number of links (directories where the

* file is referenced) */

0, 0, /* The uid and gid for the file - we give it

* to root */

80, /* The size of the file reported by ls. */

NULL, /* functions which can be done on the inode

* (linking, removing, etc.) - we don't

* support any. */

procfile_read, /* The read function for this file,

* the function called when somebody

* tries to read something from it. */

NULL /* We could have here a function to fill the

* file's inode, to enable us to play with

* permissions, ownership, etc. */

};

/* Initialize the module - register the proc file */

int init_module()

{

/* Success if proc_register[_dynamic] is a success,

* failure otherwise. */

#if LINUX_VERSION_CODE > KERNEL_VERSION(2,2,0)

/* In version 2.2, proc_register assign a dynamic

* inode number automatically if it is zero in the

* structure , so there's no more need for

* proc_register_dynamic

*/

return proc_register(&proc_root, &Our_Proc_File);

#else

return proc_register_dynamic(&proc_root, &Our_Proc_File);

#endif

/* proc_root is the root directory for the proc

* fs (/proc). This is where we want our file to be

* located.

*/

}

/* Cleanup - unregister our file from /proc */

void cleanup_module()

{

proc_unregister(&proc_root, Our_Proc_File.low_ino);

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有