Q: setting the process name for ps

From: Ulrich Windl
Date: Thu Apr 03 2014 - 07:14:00 EST


Hi!

Currently one has to fiddle with argv[] in-place when trying to change the process name "cmd") in Linux. However if you want to change the thread name ("comm"), there is a syscall (prctl(PR_SET_NAME, ...)) for it.

For comparison, in HP-UX there is also a syscall to change the process name for ps:
---
#include <sys/pstat.h>

union pstun psu;

psu.pst_command = "foobar";
pstat(PSTAT_SETCMD, psu, strlen("foobar") - 1, 0, 0);
---

To be fair, HP-XU also has syscalls to get processes, threads and arguments:
pstat_getlwp()
pstat_getproc()
pstat_getcommandline()

As Linux is different, I wonder whether there are any plans to provide a syscall to change the process name.

For those who aren't afraid of ugly code, here's a quick-and-dirty example how to change the process name in Linux (apologies, you guys know, but those who Google may not:
---
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>

static int delay(void)
{
struct timespec ts;

ts.tv_sec = 10;
ts.tv_nsec = 0;
return nanosleep(&ts, NULL);
}

int main(int argc, char *argv[])
{
int l = strlen(argv[0]);

if ( argc > 1 )
l += 1 + strlen(argv[1]);
if (l < 20 ) {
printf("provide a long argument\n");
return 1;
}
printf("look: unchanged\n"); delay();
sprintf(argv[0], "proc %d", getpid());
printf("look: process title\n"); delay();
return 0;
}
---

As I'm not subscribed to LKML, please keep me CC'd on you replies!

Thanks & regards,
Ulrich Windl


--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/