I need to modify the process name of my program in C language.
I precise, this is not the name of a thread that I want to change.
I want to change the name of my program, but the only solution I found, is to modify the value of argv[0]
.
I also found another solution with prctl(PR_SET_NAME, "newname")
, but this solution doesn't work.
我需要用C语言修改程序的进程名称。我确切地说,这不是我想要改变的线程的名称。我想更改程序的名称,但我找到的唯一解决方案是修改argv [0]的值。我还发现了另一个使用prctl(PR_SET_NAME,“newname”)的解决方案,但此解决方案不起作用。
2 个解决方案
#1
17
The differences between invoking prctl
and modify argv[0]
are:
调用prctl和修改argv [0]之间的区别是:
- modify
argv[0]
changes information in/proc/$pid/cmdline
- 修改argv [0]更改/ proc / $ pid / cmdline中的信息
- invoking
prctl(PR_SET_NAME)
changes information in/proc/$pid/status
- 调用prctl(PR_SET_NAME)更改/ proc / $ pid / status中的信息
That means you will get difference name of your process issuing ps -a
and ps -ax
.
这意味着您将获得发出ps -a和ps -ax的进程的不同名称。
If you expects same process name for different arguments while executing ps, you can do them both (i.e., change argv[0]
and invoke prctl
).
如果在执行ps时期望不同参数的进程名称相同,则可以同时执行它们(即,更改argv [0]并调用prctl)。
Hope the answer helps.
希望答案有所帮助。
#2
-1
try this:
尝试这个:
char *process_name = "aaa\0";
memcpy((void *)argv[0], process_name, sizeof(process_name));
/* explain: The space allocated for argv[0] could be smaller than the name that you want to give and then you will be overwritting some other unrelated memory. argv[0] size could be just 2 and if your process name is "averylongprocessname" you will be overflowing argv[0]. You need to strlen(argv[0]) and use that in memcpy. thx @ecerulm
/ * explain:为argv [0]分配的空间可能小于你想要给出的名称,然后你将覆盖其他一些不相关的内存。 argv [0]大小可能只有2,如果你的进程名称是“averylongprocessname”,你将溢出argv [0]。你需要strlen(argv [0])并在memcpy中使用它。 thx @ecerulm
*/
* /
#1
17
The differences between invoking prctl
and modify argv[0]
are:
调用prctl和修改argv [0]之间的区别是:
- modify
argv[0]
changes information in/proc/$pid/cmdline
- 修改argv [0]更改/ proc / $ pid / cmdline中的信息
- invoking
prctl(PR_SET_NAME)
changes information in/proc/$pid/status
- 调用prctl(PR_SET_NAME)更改/ proc / $ pid / status中的信息
That means you will get difference name of your process issuing ps -a
and ps -ax
.
这意味着您将获得发出ps -a和ps -ax的进程的不同名称。
If you expects same process name for different arguments while executing ps, you can do them both (i.e., change argv[0]
and invoke prctl
).
如果在执行ps时期望不同参数的进程名称相同,则可以同时执行它们(即,更改argv [0]并调用prctl)。
Hope the answer helps.
希望答案有所帮助。
#2
-1
try this:
尝试这个:
char *process_name = "aaa\0";
memcpy((void *)argv[0], process_name, sizeof(process_name));
/* explain: The space allocated for argv[0] could be smaller than the name that you want to give and then you will be overwritting some other unrelated memory. argv[0] size could be just 2 and if your process name is "averylongprocessname" you will be overflowing argv[0]. You need to strlen(argv[0]) and use that in memcpy. thx @ecerulm
/ * explain:为argv [0]分配的空间可能小于你想要给出的名称,然后你将覆盖其他一些不相关的内存。 argv [0]大小可能只有2,如果你的进程名称是“averylongprocessname”,你将溢出argv [0]。你需要strlen(argv [0])并在memcpy中使用它。 thx @ecerulm
*/
* /