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

* Re: [PATCH] use prctl() if available with jobs -Z
  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
  0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2012-11-18  1:28 UTC (permalink / raw)
  To: Han Pingtian, zsh-workers

Does this prctl call really have the same/desired effect of covering up
the entire argument list in the output of "ps"?  I don't have a system
available where I can test it.

E.g. try this:
    zsh -fc 'ps p$$; jobs -Z HIDEME; ps p$$; :'

(the trailing ":" is to avoid tail-call optimization on forking "ps").

The whole point of "jobs -Z" was to masquerade your process when using
a time-sharing system, not merely to change the value of argv[0].  You
know, so the prof can't tell you're spending all day playing nethack
instead of doing your CS303 homework.

I'm all for using standard interfaces to accomplish standard actions,
but if we're going to include a hack like this one then we ought to do
our best to have the hack actually be a hack.

;-)


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

* Re: [PATCH] use prctl() if available with jobs -Z
  2012-11-18  1:28 ` Bart Schaefer
@ 2012-11-18  2:16   ` Han Pingtian
  0 siblings, 0 replies; 3+ messages in thread
From: Han Pingtian @ 2012-11-18  2:16 UTC (permalink / raw)
  To: zsh-workers, schaefer

On Sat, Nov 17, 2012 at 05:28:19PM -0800, Bart Schaefer wrote:
> Does this prctl call really have the same/desired effect of covering up
> the entire argument list in the output of "ps"?  I don't have a system
> available where I can test it.
> 
> E.g. try this:
>     zsh -fc 'ps p$$; jobs -Z HIDEME; ps p$$; :'
> 
Looks like it only works with 'ps -p $$':

% ~/temp/zsh-debug3/bin/zsh -fc 'ps p$$; jobs -Z HIDEME; ps p$$; :'
  PID TTY      STAT   TIME COMMAND
26701 pts/7    S+     0:00 /home/hpt/temp/zsh-debug3/bin/zsh -fc ps p$$; jobs -Z HIDEME; ps p$$; :
  PID TTY      STAT   TIME COMMAND
26701 pts/7    S+     0:00 /home/hpt/temp/zsh-debug3/bin/zsh -fc ps p$$; jobs -Z HIDEME; ps p$$; :
% ~/temp/zsh-debug3/bin/zsh -fc 'ps -p$$; jobs -Z HIDEME; ps -p$$; :'
  PID TTY          TIME CMD
26713 pts/7    00:00:00 zsh
  PID TTY          TIME CMD
26713 pts/7    00:00:00 HIDEME
%


^ 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).