How to parse /proc/pid/cmdline

前端 未结 7 1920
盖世英雄少女心
盖世英雄少女心 2021-02-01 18:51

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

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-01 19:23

    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.

提交回复
热议问题