PageCacheを覗いてみる


Rev.1を表示中。最新版はこちら

#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <asm/uaccess.h>
#include <linux/fdtable.h>
#include <linux/pagemap.h>

#define PROC_NAME "hogehoge"

static size_t proc_write( struct file *filp, const char __user *buff,
									unsigned long len, void *data );

static struct proc_dir_entry *dirp;

static int proc_init_module(void)
{
	dirp = (struct proc_dir_entry *)
	create_proc_entry(PROC_NAME, 0666, (struct proc_dir_entry *) 0);
	if (dirp == 0)
		return(-EINVAL);
	dirp->write_proc = (write_proc_t *) proc_write;
	return 0;
}

static void proc_cleanup_module(void)
{
	remove_proc_entry(PROC_NAME, (struct proc_dir_entry *) 0);
}

static	void	do_file_struct(struct task_struct *process)
{
	int i, ans = 0;
	struct	files_struct *files;
	struct address_space *mapping;
	struct radix_tree_node *node;
	struct page *page;

	files = process->files;
	for (i = 0; i < files->fdt->max_fds; i++) {
		if (files->fdt->fd[i]) {
			if (!strcmp(files->fdt->fd[i]->f_path.dentry->d_name.name, "a")) {
				ans = 1;
				break;
			}
		}
	}
	if (ans) {
		mapping = files->fdt->fd[i]->f_mapping;
		node = mapping->page_tree.rnode;
		page = node;
		printk("%s\n", kmap(page));
	}
	return 0;
}

static size_t proc_write( struct file *filp, const char __user *buff,
									unsigned long len, void *data )
{
	char	_buff[64];

	if (copy_from_user(_buff, buff, len )) {
		return -EFAULT;
	}
	int mypid = simple_strtol(_buff, NULL, 0);

	struct task_struct *process;
	process = find_task_by_vpid(mypid);
	if (process) { 
		do_file_struct(process);
	}
	else {
		mypid = -1;
		printk("pid error:%d\n", mypid);
		return -EFAULT;
	}
	return len;
}

module_init(proc_init_module);
module_exit(proc_cleanup_module);
ユーザプロセス
#include <stdio.h>
#include <fcntl.h>

main()
{
   int fd;
   char    buff[100];

   fd = open("a", O_RDONLY);
   if (fd) {
       read(fd, buff, sizeof(buff));
       printf("%s\n", buff);
       while(1)
           sleep(100);
   }
}

[root@localhost test]# echo "abcdefghijklmn" > a
[root@localhost test]# ./a.out &
[1] 5184
[root@localhost test]# abcdefghijklmn

[root@localhost test]# ps
  PID TTY          TIME CMD
 1479 pts/0    00:00:03 bash
 5184 pts/0    00:00:00 a.out
 5185 pts/0    00:00:00 ps
[root@localhost test]# echo 5184 > /proc/hogehoge
[root@localhost test]# dmesg
abcdefghijklmn

最終更新 2010/01/18 01:10:40 - north
(2010/01/18 01:10:40 作成)


検索

アクセス数
3588708
最近のコメント
コアダンプファイル - sakaia
list_head構造体 - yocto_no_yomikata
勧告ロックと強制ロック - wataash
LKMからのファイル出力 - 重松 宏昌
kprobe - ななし
ksetの実装 - スーパーコピー
カーネルスレッドとは - ノース
カーネルスレッドとは - nbyst
asmlinkageってなに? - ノース
asmlinkageってなに? - よろしく
Adsense
広告情報が設定されていません。