Hello.

How can you implement the following task: There is a program someprogram, when you start ./someprogram, the program starts and in ps / top the process is also called someprogram. How to make it so that when you name the program someprogram the process is called anotherprogram?

I read that you need to play with argv [0]

2 answers 2

Here it is in Linux

avp@avp-ubu1:~/hashcode$ cat /etc/issue Ubuntu 12.04.2 LTS \n \l avp@avp-ubu1:~/hashcode$ uname -a Linux avp-ubu1 3.2.0-38-generic #61-Ubuntu SMP Tue Feb 19 12:18:21 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux avp@avp-ubu1:~/hashcode$ 

it works (although I did not find /proc/self/comm in man proc , but it is "calculated")

 avp@avp-ubu1:~/hashcode$ cat progname.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #define MY_NEW_NAME "new_prog_name" int main (int ac, char *av[]) { printf ("I want to change name from [%s] to [%s]\n", av[0], MY_NEW_NAME); fflush(stdout); system("ps"); puts ("Hit ENTER for continue..."); getchar(); int fd = open("/proc/self/comm",O_WRONLY); if (fd > 0) { write (fd,MY_NEW_NAME,strlen(MY_NEW_NAME)); close (fd); } else { perror("open"); } puts ("see result:"); system("ps"); return puts("End") == EOF; } avp@avp-ubu1:~/hashcode$ gcc progname.c avp@avp-ubu1:~/hashcode$ ./a.out I want to change name from [./a.out] to [new_prog_name] PID TTY TIME CMD 1919 pts/0 00:00:00 bash 2958 pts/0 00:00:00 a.out 2959 pts/0 00:00:00 sh 2960 pts/0 00:00:00 ps Hit ENTER for continue... see result: PID TTY TIME CMD 1919 pts/0 00:00:00 bash 2958 pts/0 00:00:00 new_prog_name 2961 pts/0 00:00:00 sh 2962 pts/0 00:00:00 ps End avp@avp-ubu1:~/hashcode$ 

I also want to add that the contents of /proc/self/cmdline (the first element there is argv [0]) does not change, as well as the symbolic link /proc/self/exe , which points to the executable file.

So, reliably deceiving the "serious" programs in this way (change the name for top, ps, ...) does not work.

  • For the boring g ++ you need to add #include <unistd.h> - avp
  • thanks, it helped. - Kenpachi

Another option is to create a link to the executable file (hard or symbolic) with some name and run through it. Then the process will have her name.

  • Thanks for the advice. As an option - yes, but not in my case. - Kenpachi