如何从"struct linux_binprm"获取argv? [英] How can I get argv from "struct linux_binprm"?
问题描述
我想从现有结构linux_binprm中提取所有argv.在内核3.4上,我尝试了以下代码: http://在do_excve_common中的www.mail-archive.com/kernelnewbies@nl.linux.org/msg00278.html 中,但这是行不通的.返回(空).有什么问题,如何获取char *字符串中的所有参数?
I want to extract all argv from an existing struct linux_binprm. On kernel 3.4, I tried this piece of code: http://www.mail-archive.com/kernelnewbies@nl.linux.org/msg00278.html in do_excve_common, but it doesn't work. It returns (null). What is the problem and how can I get ALL the arguments in a char * string?
推荐答案
.如果要在do_execve_common()中执行二进制加载器之前获取完整的命令行,可以尝试以下操作: 函数do_execve_common()参数表中只有一个参数* argv,为什么还要从"struct linux_binprm"中获取argv?您可以直接将* argv与以下代码一起使用.在do_execve_common()中,插入一些代码,如下所示:
. If you want to get the full command line before the binary loader executing in do_execve_common(), you can try following: there is one argument *argv in the function do_execve_common() parameter table, why bother to get the argv from "struct linux_binprm"? You can use the *argv directly with following codes. In the do_execve_common(), insert some codes as following:
argc = count(argv, MAX_ARG_STRINGS);
i = 0;
while (i < argc)
{
const char __user *str;
int len;
ret = -EFAULT;
str = get_user_arg_ptr(argv, i);
if (IS_ERR(str))
goto out;
len = strnlen_user(str, MAX_ARG_STRLEN);
if (!len)
goto out;
//copy the str to kernel temporary storage
//NOTE: tmp[] is a string array,
// the memory should have been allocated already for strings storage,
// each string is ended with \0
memcpy(tmp[i], str, len)
}
执行完这些代码后,我认为argv字符串将全部保存在tmp []数组中.
After executing these codes, I think the argv strings will be all saved in tmp[] array.
.虽然如果您想在执行二进制加载程序后获得完整的命令行,我认为此时参数页已正确设置,那么您可以尝试以下方法来获取完整的命令行: ./fs/proc/base.c文件中有一个函数proc_pid_cmdline(),您可以重新使用proc_pid_cmdline()函数中的大多数代码来从参数页面获取完整的命令行.
. While if you want to get the full command line after binary loader executing, I think at this time the argument page has been setup correctly, then you can try following approach to get the full command line: There is a function proc_pid_cmdline() in ./fs/proc/base.c file, you can re-use most codes in proc_pid_cmdline() function to get the full command line from the argument page.
这篇关于如何从"struct linux_binprm"获取argv?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!