I have a program written in C which runs on Linux only. I want to be able to change the process name as shown in the ps
command. For this I am directly changing the string in argv[0]
and also using prctl(PR_SET_NAME, argv[0])
call from the main thread. I also want to access the /proc/self/cmdline
from dynamically loaded shared libraries and in future maybe even from other programs.
I read that for this to work, I have to use the original memory space starting at argv[0]
. The ELF standard specifies that this space is \0
separated from environ
space. Looking into ps_status.c from Postgres code, one can see that they are using all this space for argv strings. And really, when I memset
this space to 'a'
, I can see over 3000 chars in ps
and read it from /proc
filesystem both. Problem starts when I try to use this space to dynamically (at runtime) create new arguments in this space. (I have read and from basic tests know that Chrome/Chromium does something similar - export status of it's fork
ed processes in ps
by command line arguments.) Anything which contains the NULL delimiter in space reaching into originally environment is treated as an end. (I originally had 105 chars in cmdline arguments, I am able to get 130 chars but others arguments up to this 3000 char mark are not read.) From this I gather that the system remembers the original size and is only letting me "read over" until the end of string. (Changing the char** argv pointer is no help.)
But the Chrome is somehow doing this. Looking into command_line.cc source I see no immediate way how.
Is it even possible to do this this way? And if so, how? To tell the Linux kernel that the size of argv memory and argc changed?
Thank you.