gpt4 book ai didi

call_usermodehelper 无法正常工作

转载 作者:太空宇宙 更新时间:2023-11-03 23:40:49 25 4
gpt4 key购买 nike

我在一个内核模块上工作,用于从内核空间用 python 在文件中写入一些东西。
第一次尝试 :我为从内核运行 python 文件编写了这段代码:

int ret = -1;    
char path[] = "/bin/bash";
char *argv[] = {path, "-c", "python", "/home/samba/getter.py",NULL};
char *envp[] = {"HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL};

printk("call_usermodehelper module is starting..!\n");
ret = call_usermodehelper(path, argv, envp, UMH_WAIT_PROC);
printk("ret=%d\n", ret);

这是python代码:
# getter.py
import urllib2
import json

url = "http://172.16.189.1:8000/random/"
response = urllib2.urlopen(url)
input_file = open("/home/samba/something.txt", "w")
input_file.write(json.loads(response.read())["hash"])
input_file.close()

(上面的 python 脚本工作正常。当我从用户空间运行它时,它会创建 something.txt 文件并成功保存输出。)

加载以上模块后文件 something.txt不创建,当我运行 dmesg命令我看到了这个:
[16184.383282] call_usermodehelper module is starting..!
[16184.384389] ret=0

第二次尝试 : 我改了 python命令到 touch仍然无法创建文件,但 ret调用触摸后的值非零!
int ret = -1;    
char path[] = "/bin/bash";
char *argv[] = {path, "-c", "touch", "/home/samba/something.txt",NULL};
char *envp[] = {"HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL};

printk("call_usermodehelper module is starting..!\n");
ret = call_usermodehelper(path, argv, envp, UMH_WAIT_PROC);
printk("ret=%d\n", ret);

当我运行 dmeseg加载模块后的命令我看到这样的输出:
[16211.309975] call_usermodehelper module is starting..!
[16211.311053] ret=256

但是文件 /home/samba/something.txt未创建。我在做什么错?

更新 : 感谢通过更改 UMH_WAIT_PROC 的答案之一至 UMH_WAIT_EXEC touch命令(第二种情况)现在可以正确创建文件,但 python 代码仍然不起作用。

最佳答案

这个设计首先看起来有缺陷。只有在极其特殊的情况下才能从内核中生成东西。一个明显不那么糟糕的解决方案将使用常规用户空间进程等待例如网络链接事件。不是由 usermodehelper 产生的,而是像一个典型的守护进程一样。

关于call_usermodehelper 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46524237/

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