列出目录下的文件的内核模块

来源:互联网 发布:linux gdb调试 编辑:程序博客网 时间:2024/04/29 18:36

在调试一个文件系统时,需要看看挂载后文件系统的目录,写了一个列文件的内核模块。


使用方法:

    insmod fs_test.ko  path='/dev'


默认路径是/。 该模块不会被插入内核,列完目录后退出。


fs_test.h

#ifndef __FS_TEST_H__#define __FS_TEST_H__#define FS_TEST_DEBUG#define MY_MODULE_NAME             "fs_test"#define MY_MODULE_LICENSE          "GPL"#define MY_MODULE_AUTHOR           "Hansel He"#define MY_MODULE_DESCRIPTION      "Support File System Debug"/*------------------------------------------------*/#ifdef FS_TEST_DEBUG#define FM_PRINTF(fmt, arg...)   printk("[fs_test: %s():%d] " fmt, __FUNCTION__, __LINE__, ##arg)#define FM_PRINTFN(fmt, arg...)  FM_PRINTF(fmt "\n", ##arg)#else#define FM_PRINTF(fmt, arg...)#define FM_PRINTFN(fmt, arg...)#endif/*---------------------------------------*/typedef struct {    int count;}FS_TEST_DATA;#endif

fs_test_main.c

/**************************************************************************************** * File:fs_test.c * * Description:FS Debug Module * Version:1.0 *  *  * *  * Change Log: * * Date          By         Description * ===========   ========   ============================================================= * 2011.12.03    HanselCreate ****************************************************************************************/#include <linux/config.h>#include <linux/module.h>#include <linux/types.h>#include <linux/errno.h>#include <linux/version.h>#include <linux/proc_fs.h>#include <linux/init.h>#include <linux/string.h>#include <linux/fs.h>#include <linux/syscalls.h>#include <linux/dirent.h>#include <linux/file.h>#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)#include <linux/moduleparam.h>#endif#include "fs_test.h"//extern unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base);/*----------------------------------------------*/static char *path = "/";/*********************************************************************************************//*********************************************************************************************/static int ls_filldir(void * __buf, const char * name, int namlen, loff_t offset,   ino_t ino, unsigned int d_type){    FS_TEST_DATA *data = (FS_TEST_DATA*)__buf;    printk("#%d name=%s, type=%u\n", data->count++, name, d_type);    return 0;}static int fs_ls(char * dir_path){    struct file * file;int err = 0;    FS_TEST_DATA data = {0};    file = filp_open((const char __user *) dir_path, O_RDONLY|O_DIRECTORY, 0);if (IS_ERR(file)){err = -EBADF;        printk("Error: unable to get file struct for path %s \n", dir_path);        goto no_file;}err = vfs_readdir(file, ls_filldir, &data);    if (err < 0){        printk("Error: vfs_readdir() return %d.\n", err);goto ls_out;    }    err = 0;ls_out:    filp_close(file, 0);no_file:    return err;}static int __initmod_init(void){    int ret = 0;    ret = fs_ls(path);    // not insert modereturn -1;}static void mod_exit(void){}module_init(mod_init);module_exit(mod_exit);module_param(path, charp, 0);MODULE_LICENSE(MY_MODULE_LICENSE);MODULE_AUTHOR(MY_MODULE_AUTHOR);MODULE_DESCRIPTION(MY_MODULE_DESCRIPTION);





原创粉丝点击