diff mbox

Re: [PATCH] Overwrite argv to set process title, eliminating 16-character prctl() limit.

Message ID 20101109192130.GA10867@boost.horde.net
State New
Headers show

Commit Message

John Morrissey Nov. 9, 2010, 7:21 p.m. UTC
On Sun, Nov 07, 2010 at 09:49:27PM +0100, Torsten Förtsch wrote:
> I am quite new to the list but why not do both call prctl(PR_SET_NAME,
> name) and overwrite argv?
> 
> The point is some tools read /proc/PID/cmdline but others the name field in 
> /proc/PID/status. The former is changed by overwriting argv the latter by 
> prctl.

Seems reasonable. This patch applies on top of the previous to re-add
prctl().

john
diff mbox

Patch

diff --git a/os-posix.c b/os-posix.c
index 3ddf7e8..38fb55e 100644
--- a/os-posix.c
+++ b/os-posix.c
@@ -39,6 +39,10 @@ 
 #include "net/slirp.h"
 #include "qemu-options.h"
 
+#ifdef CONFIG_LINUX
+#include <sys/prctl.h>
+#endif
+
 #ifdef CONFIG_EVENTFD
 #include <sys/eventfd.h>
 #endif
@@ -148,12 +152,20 @@  char *os_find_datadir(const char *argv0)
 void os_set_proc_name(int argc, char **argv, const char *name)
 {
 #ifdef CONFIG_LINUX
-    char *last_argv_byte, *p;
+    char prctl_name[16], *last_argv_byte, *p;
     int len, i;
 
     if (!name)
         return;
 
+#if defined(PR_SET_NAME)
+    strncpy(prctl_name, name, sizeof(prctl_name));
+    if (prctl(PR_SET_NAME, prctl_name)) {
+        perror("unable to change process name");
+        exit(1);
+    }
+#endif
+
     last_argv_byte = argv[argc - 1] + strlen(argv[argc - 1]);
 
     len = snprintf(argv[0], last_argv_byte - argv[0], "%s", name);