6

I'm currently trying to change the process name of a process so I can read the more easily with htop, top, .... I want to LD_PRELOAD this code into another process so it gets renamed by an environemt variable.

I found a lot of stuff in the internet, but nothing works:

prctl(PR_SET_NAME, "Test");

This does not work because htop is not honoring the name.

Nginx setproctitle (Link) doesn't work as well, because it strips the parameters (which are needed by the process).

I tried everything I found and now I'm out of ideas.

Is this even possible in linux? And how?

Hcorg
  • 11,598
  • 3
  • 31
  • 36
das_j
  • 4,444
  • 5
  • 31
  • 47
  • Do you definitely need to solve this using C code? – Useless Jul 31 '15 at 13:26
  • I'd use any other language if it can produce .so files that can be preloaded. – das_j Jul 31 '15 at 13:30
  • So the original problem is how to change the name of a process? And the first solution is writing a .so LD_PRELOAD to achieve this? If I understand your systemd setup correctly, there may be a simpler solution that doesn't require either the .so or LD_PRELOAD in the first place. – Useless Jul 31 '15 at 13:32

2 Answers2

12

Just run your program by shell script or your program through exec and pass desired name as argv[0]:

#/bin/bash
exec -a fancy_name a.out ...

or C/C++:

execl( "./a.out", "fancy_name", ... );
Slava
  • 43,454
  • 1
  • 47
  • 90
  • 1
    The question is about `c++` and not `bash` or running. – VP. Jul 31 '15 at 13:25
  • True, but it feels like an X-Y problem. Let's ask! – Useless Jul 31 '15 at 13:25
  • @VictorPolevoy I believe OP wants to solve this by C because he does not know that there is simpler solution with shell. Anyway it is done very similar by C/C++ code, I added that sample. – Slava Jul 31 '15 at 13:27
  • The problem is that all my services are called by systemd. If I overwrite the unit file, it gets rewritten with every package upgrade. So I want to define two environement variables (`LD_PRELOAD` and the title variable) through a systemd environemnt file which is not changed when upgrading. – das_j Jul 31 '15 at 13:29
  • 1
    @das_j create a copy of unit file and call it through systemd rather the one that deployed by package – Slava Jul 31 '15 at 13:31
  • @Slava hardly. Try `( exec sleep hurz 120; )` vs. `( exec -a hurz sleep 120; )` and look for the process with e.g. `ps aux|grep hurz`. – ex-bart Jul 31 '15 at 13:44
  • @Slava, I could not reproduce your example. I need to give my process a unique name like fancy_name. Please tell me how to do this with a simple complete example. Thank you. – Frank May 17 '16 at 22:22
  • 1
    Under top it still shows the actual process name and not the fancy_name – Shoaib Khan Feb 14 '19 at 07:36
7
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define NEW_NAME "hello_world"
int main(int argc, char **argv) {
  if(strcmp(argv[0], NEW_NAME)) {
    argv[0] = NEW_NAME;
    execv("/proc/self/exe", argv);
    fputs("exec failed", stderr); 
    return 1;
  }
  while(1) // so it goes to the top
    ;
}