7

I am trying to use prctl( PR_SET_NAME, "procname", 0, 0, 0) to set name for a process, when I am reading the Linux Manual about the PR_SET_NAME, looks like it set the name for thread if I understand it correctly.

Can prctl be used to set name for process? How to set name for process?

3

3 Answers 3

4

Yes, you may use PR_SET_NAME in the first argument and the name as the second argument to set the name of the calling thread(or process). prctl returns 0 on success. Remember, it depends where you call this prctl. If you call it inside your process, it will change the name of that process and all of its belonging threads. If you call it inside a specific thread, it will change only the name of that thread.

Example:

int s;
s = prctl(PR_SET_NAME,"myProcess\0",NULL,NULL,NULL); // name: myProcess

Now, if you are running your process in Linux, type:

top

or

ps

To see the name attached to your process id.

3
  • 2
    Wrong: doing this will indeed change the process name but you won't see any difference in PS nor TOP commands. That's because to accomplish what you want you must change argv[0] which is the one reflected by those two comands.
    – Zibri
    Commented Mar 9, 2018 at 18:36
  • 8
    Out of curiosity, is there a reason you added an extra \0 to the parameter "myProcess\0"? C strings are already terminated with \0
    – nitrogen
    Commented Apr 9, 2019 at 3:16
  • @Zibri. ps -o comm will show a difference. So will htop if you also add the COMM column. Difference is between /proc/<pid>/comm and /proc/<pid>/cmdline Commented Feb 23 at 15:27
1

Try the following code:

const char *newName = "newname";
char *baseName;

// find application base name to correct
char *appName = const_cast<char *>(argv[0]);
if (((baseName = strrchr(appName, '/')) != NULL ||
   (baseName = strrchr(appName, '\\')) != NULL) && baseName[1]) {
   appName = baseName + 1;
}

// Important! set new application name inside existing memory block.
// we want to avoid argv[0] = newName; because we don't know
// how cmd line buffer will be released during application shutdown phase
// Note: new process name has equal or shorter length than current argv[0]
size_t appNameLen;
if ((appNameLen = strlen(appName)) != 0) {
    strncpy(appName, newName, appNameLen);
    appName[appNameLen] = 0;
}

// set new current thread name
if (prctl(PR_SET_NAME, reinterpret_cast<unsigned long>(const_cast<char *>(newName)), NULL, NULL, NULL)) {
    Log::error("prctl(PR_SET_NAME, \"%s\") error - %s", newName, strerror(errno));
}
0

To set the process name you can use as you did prctl but that will show up only in /proc/pid/status (and programs using it). ps and top look elsewhere and to change the process name shown in ps and top, you must just change argv[0].

so just assigning it as argv[0]="newprocessname"; wil do.

3
  • 1
    Can you add an example and share what distribution and Kernel version you are using? Changing argv[0] did not work for me, but pthread_setname_np and prctl did work.
    – nitrogen
    Commented Apr 9, 2019 at 3:41
  • you have to do both, as I said. ubuntu 16-17-18 any kernel
    – Zibri
    Commented Apr 9, 2019 at 8:08
  • will it corrupt the program's environment variables if the new process name is long? since linux's place the argv and environ pointers side by side
    – Komgcn
    Commented May 14 at 3:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.