Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetCommandLine linux *true* equivalent

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"
like image 984
user877329 Avatar asked Dec 26 '12 15:12

user877329


1 Answers

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.

like image 71
Mats Petersson Avatar answered Oct 30 '22 11:10

Mats Petersson