0

我是开发新手,我手头的任务是创建一个 securityfs 文件并通过内核安全模块向它写入一些字符串。

我使用以下代码在其中创建了 dir 和一个文件。

test_dir = securityfs_create_dir("test", NULL);

basc_output_file = securityfs_create_file("basc_output_file",S_IRUSR | S_IRGRP, test, NULL, &test_measurements_file_ops);

但现在我无法获得任何示例或文档如何将一些字符串写入该文件。任何帮助将不胜感激。

4

1 回答 1

2

您的问题是关于如何实现 test_measurements_file_ops,我可以提供以下演示代码供您参考:

static const struct file_operations test_measurements_file_ops = {
    .write = test_write,
    .read  = test_read,
};

static ssize_t test_write(struct file *file, const char __user *buf,
                            size_t count, loff_t *ppos)
{
    char *data;
    int error;
    if (!count || count >= MAXIMUM_SIZE)
        return -ENOMEM;
    data = kzalloc(count + 1, GFP_NOFS);
    if (!data)
        return -ENOMEM;
    if (copy_from_user(data, buf, count)) {
        error = -EFAULT;
        goto out;
    }

    /* handling kaddr */

out:
    kfree(data);
    return error ? error : count;
}

static ssize_t test_read(struct file *file, char __user *buf, 
                            size_t count, loff_t *ppos)
{
    void *kaddr;
    loff_t pos = *ppos;
    loff_t len = /* strlen(kernel strings need to be copied) */;

    if (pos >= len || !count)
        return 0;
    len -= pos;
    if (count < len)
        len = count;

    /* handling */

    if (copy_to_user(buf, kaddr, len))
        return -EFAULT;
    *ppos += len;
    return len;
}
于 2013-03-15T06:21:59.307 回答