zsh-workers
 help / color / mirror / code / Atom feed
* [PATCH] use prctl() if available with jobs -Z
@ 2012-11-17 12:38 Han Pingtian
  2012-11-18  1:28 ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: Han Pingtian @ 2012-11-17 12:38 UTC (permalink / raw)
  To: zsh-workers; +Cc: zsh-user

Use prctl() to change argv[0]. It is more safe than modifying argv[0]
directly. After the changing, new name can be seen by 'ps -p $$'.
---
 Src/jobs.c   |    9 ++++++---
 configure.ac |    6 ++++++
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/Src/jobs.c b/Src/jobs.c
index 0464d18..1c16671 100644
--- a/Src/jobs.c
+++ b/Src/jobs.c
@@ -1698,7 +1698,7 @@ getjob(const char *s, const char *prog)
     return returnval;
 }
 
-#ifndef HAVE_SETPROCTITLE
+#if !defined(HAVE_SETPROCTITLE) && !defined(HAVE_PRCTL)
 /* For jobs -Z (which modifies the shell's name as seen in ps listings).  *
  * hackzero is the start of the safely writable space, and hackspace is   *
  * its length, excluding a final NUL terminator that will always be left. */
@@ -1714,7 +1714,6 @@ static int hackspace;
 void
 init_jobs(char **argv, char **envp)
 {
-    char *p, *q;
     size_t init_bytes = MAXJOBS_ALLOC*sizeof(struct job);
 
     /*
@@ -1728,13 +1727,14 @@ init_jobs(char **argv, char **envp)
     jobtabsize = MAXJOBS_ALLOC;
     memset(jobtab, 0, init_bytes);
 
-#ifndef HAVE_SETPROCTITLE
+#if !defined(HAVE_SETPROCTITLE) && !defined(HAVE_PRCTL)
     /*
      * Initialise the jobs -Z system.  The technique is borrowed from
      * perl: check through the argument and environment space, to see
      * how many of the strings are in contiguous space.  This determines
      * the value of hackspace.
      */
+    char *p, *q;
     hackzero = *argv;
     p = strchr(hackzero, 0);
     while(*++argv) {
@@ -1850,6 +1850,9 @@ bin_fg(char *name, char **argv, Options ops, int func)
 	unmetafy(*argv, &len);
 #ifdef HAVE_SETPROCTITLE
 	setproctitle("%s", *argv);
+#elif defined(HAVE_PRCTL)
+#include <sys/prctl.h>
+	prctl(PR_SET_NAME, *argv);
 #else
 	if(len > hackspace)
 	    len = hackspace;
diff --git a/configure.ac b/configure.ac
index 5528597..4c6d15f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2018,6 +2018,12 @@ AH_TEMPLATE([HAVE_SETPROCTITLE],
 AC_CHECK_FUNC(setproctitle,AC_DEFINE(HAVE_SETPROCTITLE),
 AC_SEARCH_LIBS(setproctitle,util,AC_DEFINE(HAVE_SETPROCTITLE)))
 
+dnl CHECK FOR prctl() FOR jobs -Z / ARGV0
+AH_TEMPLATE([HAVE_PRCTL],
+[Define to 1 if the system supports `prctl' to change process name])
+AC_CHECK_FUNC(prctl,AC_DEFINE(HAVE_PRCTL),
+AC_SEARCH_LIBS(prctl,c,AC_DEFINE(HAVE_PRCTL)))
+
 dnl -------------
 dnl CHECK FOR NIS
 dnl -------------
-- 
1.7.7.6


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-11-18  2:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-11-17 12:38 [PATCH] use prctl() if available with jobs -Z Han Pingtian
2012-11-18  1:28 ` Bart Schaefer
2012-11-18  2:16   ` Han Pingtian

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).