c - 如何按用户请求删除 proc 条目?
问题描述
通常 proc 条目正在删除具有 __exit 属性的内部函数。如何根据用户请求删除它?我已经厌倦了调用remove_proc_entry
内部写回调,但内核在那里粉碎。我不能只删除模块,因为它一次管理多个 proc 条目。我只想删除我正在告诉“删除”的那个。
static int my_write(struct file *file, const char __user *userbuff,
size_t len, loff_t *ppos)
{
struct seq_file *seqf = file->private_data;
struct *my_data = seqf->private;
char *buff;
int retval;
retval = -EINVAL;
if (!userbuff || len > PAGE_SIZE)
return -EINVAL;
buff = kzalloc((len + 1), GFP_KERNEL);
if (!buff)
return -ENOMEM;
if (copy_from_user(buff, userbuff, len))
goto out;
buff[len] = '\0';
if (!strncmp("delete", buff, 6)) {
/* Cannot use remove_proc_entry inside that write callback. */
retval = 0;
}
if (!retval)
retval = len;
else
printk("Usage: echo delete > /proc/my_proc to delete that entry.\n");
return retval;
}
static const struct file_operations my_proc_fops = {
.open = my_proc_open,
.read = seq_read,
.write = my_write,
.llseek = seq_lseek,
.release = single_release,
};
解决方案
谢谢!我已经通过 schedule_work 做到了。
struct my_data {
/* data */
struct work_struct remove_proc_work;
};
static void remove_proc_work_handler(struct work_struct *work)
{
struct my_data *data;
data = container_of(work, struct my_data, remove_proc_work);
/* call remove_proc_entry */
}
static void init_data(struct my_data *data)
{
/* init data */
INIT_WORK(&data->remove_proc_work, remove_proc_work_handler);
}
static int my_write(struct file *file, const char __user *userbuff,
size_t len, loff_t *ppos)
{
struct seq_file *seqf = file->private_data;
struct my_data *data = seqf->private;
char *buff;
int retval;
retval = -EINVAL;
if (!userbuff || len > PAGE_SIZE)
return -EINVAL;
buff = kzalloc((len + 1), GFP_KERNEL);
if (!buff)
return -ENOMEM;
if (copy_from_user(buff, userbuff, len))
goto out;
buff[len] = '\0';
if (!strncmp("delete", buff, 6)) {
/* Cannot use remove_proc_entry inside that write callback. */
schedule_work(&data->remove_proc_work);
retval = 0;
}
if (!retval)
retval = len;
else
printk("Usage: echo delete > /proc/my_proc to delete that entry.\n");
return retval;
}
static const struct file_operations my_proc_fops = {
.open = my_proc_open,
.read = seq_read,
.write = my_write,
.llseek = seq_lseek,
.release = single_release,
};
推荐阅读
- c++ - 如何使用 c++ 中的 ipp 库从信号中生成 fft?
- javascript - offset().top 在附加元素上没有返回正确的值
- apache-kafka-streams - 更改 KStream 的滑动窗口应用程序的窗口大小\跳数
- xamarin.forms - 如何为 Xamarin.form ListView 实现 MVVM
- java - java.lang.ClassNotFoundException:org.springframework.web.servlet.support.DefaultFlashMapManager Weblogic 12c
- jenkins - 如何使用 Jenkins 解决 Gitlab Hook Plugin 安装问题?
- matplotlib - Matplotlib:如何让这个图例更紧凑?
- java - 如何使用 apache pio 4.1.0 设置单元格的背景颜色
- proof - 有限多集作为 Cubical Agda 中的 HIT
- angular - 角度检查json是否已更新/与当前不同