GetCommandLine linux *true* equivalent

牧云@^-^@ 提交于 2020-01-10 20:11:50

问题


Similar question to Linux equivalent of GetCommandLine and CommandLineToArgv

Is it possible to get the raw command line in linux? The file /proc/self/cmdline is destroyd.

./a.out files="file 1","file 2" param="2"

prints

./a.outfiles=file 1,file 2param=2

which is junk

Escaping command line does work for all arguments but the first.

./a.out files=\"fil 1\",\"fil 2\"\ param=\"2\"

prints

./a.outfiles="fil1","fil2" param="2"

回答1:


You can't do that. The command line arguments are actually passed to the new process as individual strings. See the linux kernel source: kernel_execve

Note that kernel_execve(...) takes a const char *argv[] - so there is no such thing as a long string commandline in Linux - it's the layer above that needs to split the arguments into separate components.

Edit: actually, the system call is here:

excve system call

But the statement above still applies. The parameter for argv is already split by the time the kernel gets it from the C-library call to exec.

It is the responsibility of the "starter of the program" (typically a shell, but doesn't have to be) to produce the argv[] array. It will do the "globbing" (expansion of wildcard filenames to the actual files that it matches) and stripping of quotations, variable replacement and so on.

I would also point out that although there are several variants of "exec" in the C library, there is only one way into the kernel. All variants end up in the execve system call that I linked to above. The other variants are simply because the caller may not fancy splitting arguments into invdividual elements, so the C library "helps out" by doing that for the programmer. Similarly for passing an environment array to the new program - if the programmer don't need specific environment, he/she can just call the variant that automatically take the parent process env.



来源:https://stackoverflow.com/questions/14042586/getcommandline-linux-true-equivalent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!