I'm trying to split the cmdline of a process on Linux but it seems I cannot rely on it to be separated by '\0' characters. Do you know why sometimes the '\0' character is used as separator and sometimes it is a regular space?
Do you know any other ways of retrieving the executable name and the path to it? I have been trying to get this information with 'ps' but it always returns the full command line and the executable name is truncated.
Thanks.
In linux, /proc includes a directory for each running process, including kernel processes, in directories named /proc/PID, these are the directories present: directory. description. /proc/PID/cmdline. Command line arguments.
This file shows the parameters passed to the kernel at the time it is started. A sample /proc/cmdline file looks like the following: ro root=/dev/VolGroup00/LogVol00 rhgb quiet 3. This output tells us the following: ro.
The proc file system is a pseudo-file system which is used as an interface to kernel data structures. It is commonly mounted at /proc. Most of it is read-only, but some files allow kernel variables to be changed. The following outline gives a quick tour through the /proc hierarchy.
/proc/[pid]/statm Provides information about memory usage, measured in pages.
use strings
$ cat /proc/self/cmdline | strings -1 cat /proc/self/cmdline
The /proc/PID/cmdline
is always separated by NUL characters.
To understand spaces, execute this command:
cat -v /proc/self/cmdline "a b" "c d e"
EDIT: If you really see spaces where there shouldn't be any, perhaps your executable (intentionally or inadvertently) writes to argv[]
, or is using setproctitle()
?
When the process is started by the kernel, cmdline is NUL-separated, and the kernel code simply copies the range of memory where argv[]
was at process startup into the output buffer when you read /proc/PID/cmdline
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With