modifying parent's environment, etc.

Lloyd Kremer kremer at cs.odu.edu
Wed Apr 26 06:34:21 AEST 1989


In article <1494 at vsedev.VSE.COM> logan at vsedev.VSE.COM (James Logan III) writes:

>In article <2158 at pur-phy> sho at newton.physics.purdue.edu.UUCP
>(Sho Kuwamoto) writes:
># This thread got me to thinking.  I wrote a quickie program which,
># for reasons I don't need to go into now, modified argv[i].  The
># strangest thing happened: if you run it in the background and look
># at it using ps, the line where it tells you what you typed in as
># your command line changes.  I'm interested to know if this works
># on all versions of UNIX.  Compile the following, run it in the
># background, and do a PS.  Over here, we are running BSD 4.3.
>
>I tried it under System V Release 2.  It doesn't work.


Yes, in System V, writing to argv[] doesn't change ps's opinion of what the
original args were.  You must use some form of the exec() call to change the
contents of the u_area.  The following should work:

	main(argc, argv)
	int argc;
	char **argv;
	{
		char *orig_name;
	
		if(strcmp(argv[0], "fakename")){
			orig_name = argv[0];
			argv[0] = "fakename";
			execvp(orig_name, argv);  /* this will "fool" ps */
		}
		if(!fork()){
			execl("/bin/sh", "sh", (char *)0);  /* child shell */
			return(1);
		}
		wait((int *)0);
		return(0);
	}

Do a 'ps -f' from the child shell and see what you get.

-- 
					Lloyd Kremer
					Brooks Financial Systems
					...!uunet!xanth!brooks!lloyd
					Have terminal...will hack!



More information about the Comp.lang.c mailing list