gpt4 book ai didi

linux - 写入 securityfs 文件

转载 作者:太空宇宙 更新时间:2023-11-04 09:46:55 31 4
gpt4 key购买 nike

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

我已经使用以下代码在其中创建了一个目录和一个文件。

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);

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

最佳答案

你的问题是关于如何实现test_measurements_file_ops,我可以提供如下demo代码供你引用:

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;
}

关于linux - 写入 securityfs 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15410011/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com