作者roylee17 (把我id還我阿......)
看板LinuxDev
標題Re: [問題] kernel space呼叫user space的binary
時間Sat Aug 27 13:35:09 2011
※ 引述《cobrasgo (體重突破所有均線)》之銘言:
: 標題: [問題] kernel space呼叫user space的binary
: 時間: Fri Aug 26 21:00:42 2011
:
: 各位大神好
: 小弟現在碰到一個問題無法解決
: 狀況如下:
:
: 我在kernel space想要呼叫user space的binary file,並且傳參數給它
: 現在麻煩的是這個參數是run time才會知道
:
: 目前survey到一個類似的做法,但是無法傳參數
: 1) DECLARE_WORK產生一個work struct
: 2) PREPARE_WORK把我要執行的kernel function丟到一個work queue裡
: 這個kernel function裡呼叫call_usermodehelper來執行user space
: 的binary file
: 3) 呼叫queue_work()
:
: 請問一下
: 有辦法從kernel space執行user space的binary,並傳參數嗎?
: 謝謝
:
: --
: ※ 發信站: 批踢踢實業坊(ptt.cc)
: ◆ From: 123.195.12.202
: → roylee17:call_usermodehelper(char *path, char **argv, 08/26 21:16
: → roylee17: char **envp, enum umh_wait wait) 08/26 21:16
: → roylee17:可以傳參數不是嗎? 08/26 21:18
: 推 clanguage:do_execve()?? 08/27 00:15
: → cobrasgo:to 1F,應該是我寫的不好,我是指prepare work時無法傳 08/27 00:59
: → cobrasgo:呼叫call_usermodehelper()本身沒問題,重點是kernel 08/27 01:00
: → cobrasgo:function的部份 08/27 01:00
typedef void (*work_func_t)(struct work_struct *work);
既然 work_func 接受 work_struct 為參數, 可以試試利用這點.
1. 定義你的 private work_struct:
struct foo_work {
struct work_struct work;
void *data;
};
2. 在你的 create/schedule work 處, 傳入你的 argement/data:
foo_sched_work() {
foo_work *w = kmaloc(sizeof(foo_work));
/* setup your data/argument */
w->data = __your_data__;
/* setup the work_struct */
w->work.func = foo_work_func;
schedule_work(&w->work);
}
3. 在你的 work_func implementation 裡:
work_func_t foo_work_func(struct work_struct *work)
{
struct foo_work *w = container_of(work, struct work_struct, work);
/* access your via w->data */
...
call_usermodehelper(...);
}
我沒有驗證過, 有興趣的話可能要麻煩你試試了
不行的話, 再來想辦法
Regards,
Roy
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.104.36.213
※ 編輯: roylee17 來自: 59.104.36.213 (08/27 13:46)
推 cobrasgo:感謝,明天來試試 08/27 17:15
推 gene7299:是利用container_of 嗎? 08/29 17:05
→ roylee17:是的 08/30 20:37