GetCommandLine linux *true* equivalent

后端 未结 1 1079
独厮守ぢ
独厮守ぢ 2021-01-01 05:09

Similar question to Linux equivalent of GetCommandLine and CommandLineToArgv

Is it possible to get the raw command line in linux? The file /proc/self/cmdlin

相关标签:
1条回答
  • 2021-01-01 05:21

    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.

    0 讨论(0)
提交回复
热议问题