简体   繁体   中英

How to get a file list from a directory inside the linux kernel?

I just saw there is a ROOT / directory before any rootfs (whether from initrd or form a disk)
I already know it do contains /root and /dev with a mounted devtmpfs (if CONFIG_DEVTMPFS_MOUNT is selected) .

But I wasn't able to find if there are other directories and which they are.

So the point is to insert code at init/do_mounts.c before the line 403 for printk a list to the screen.
The problem is I don't know how to use the struct direent for getting a directory list for the old readdir() ( int readdir(unsigned int fd, struct dirent *dirp, unsigned int count); )

You can open files from kernel space with filp_open().
You want to use the function iterate_dir().
You'll have to define a struct dir_context (include/linux/fs.h) and provide some sort of filldir function (maybe something like adding entries to a list).

I wasn't able to find any other examples how to do this, so here's my code:

typedef int (*readdir_t)(void *, const char *, int, loff_t, u64, unsigned);

#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0))
struct callback_context {
    struct dir_context ctx;
    readdir_t filler;
    void* context;
};

int iterate_dir_callback(struct dir_context *ctx, const char *name, int namlen,
        loff_t offset, u64 ino, unsigned int d_type)
{
    struct callback_context *buf = container_of(ctx, struct callback_context, ctx);
    return buf->filler(buf->context, name, namlen, offset, ino, d_type);
}
#endif


int readdir(const char* path, readdir_t filler, void* context)
{
    int res;
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0))
    struct callback_context buf = {
        .ctx.actor = (filldir_t)iterate_dir_callback,
        .context = context,
        .filler = filler
    };
#endif

    struct file* dir  = filp_open(path, O_DIRECTORY, S_IRWXU | S_IRWXG | S_IRWXO);
    if(!IS_ERR(dir))
    {

#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0))
        res = iterate_dir(dir, &buf.ctx);
#else
        res = vfs_readdir(dir, filler, context);
#endif
        filp_close(dir, NULL);
    } 
    else res = (int)PTR_ERR(dir);
    return res;
}

To use it, define your callback and call readdir:

int filldir_callback(void* data, const char *name, int namlen,
        loff_t offset, u64 ino, unsigned int d_type)
{
    printk(KERN_NOTICE "file: %.*s type: %d\n", namlen, name, d_type);
    if(d_type == DT_DIR) ; // do sth with your subdirs
    return 0;
}

readdir("/etc", filldir_callback, (void*)123);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM