[comp.lang.c] modifying parent's environment, etc.

logan@vsedev.VSE.COM (James Logan III) (04/25/89)

In article <2158@pur-phy> sho@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.

			-Jim

-- 
Jim Logan                           logan@vsedev.vse.com
VSE Software Development Lab        uucp:  ..!uunet!vsedev!logan
(703) 329-4654                      inet:  logan%vsedev.vse.com@uunet.uu.net

logan@vsedev.VSE.COM (James Logan III) (04/25/89)

[Note that followups have been redirected to to comp.unix.wizards]

In article <1030@quintus.UUCP> ok@quintus.UUCP (Richard A. O'Keefe) writes:
# In article <2158@pur-phy> sho@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.
# 
# Why do you think that is strange?  ps gets the information about the
# arguments of process #1234 by peeking into the address space of process
# #1234 to find out what argv[] looks like.

Under System V the arguments are stored in the character array
u_psargs[PSARGSZ] in the u structure.  The u structure is usually
read directly from the swap device based on addresses found in 
the process table.

# Note that you don't see I/O redirection, only the strings in argv[].

The I/O redirection symbols are taken care of by the shell and
stripped off before the command sees it.   

			-Jim
-- 
Jim Logan                           logan@vsedev.vse.com
VSE Software Development Lab        uucp:  ..!uunet!vsedev!logan
(703) 329-4654                      inet:  logan%vsedev.vse.com@uunet.uu.net

kremer@cs.odu.edu (Lloyd Kremer) (04/26/89)

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

>In article <2158@pur-phy> sho@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!

bph@buengc.BU.EDU (Blair P. Houghton) (04/26/89)

In article <8634@xanth.cs.odu.edu> kremer@cs.odu.edu (Lloyd Kremer) writes:
>
>
>
>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:
>
>Do a 'ps -f' from the child shell and see what you get.

I did.  I got "unknown switch f" (running Umax, dont'chaknow) and an
otherwise ordinary ps-listing that gave me the name, pid, etc. of the
program just as I had named it.

AND, it's odd to have your prompt go different alluvasudden.  

Try again.

				--Blair

mike@nixba.UUCP (Mike Lyons) (04/27/89)

> In article <8634@xanth.cs.odu.edu> kremer@cs.odu.edu (Lloyd Kremer) writes:
> >Yes, in System V, writing to argv[] doesn't change ps's opinion of what the
> >original args were.

I tried the original program with the three strings that were repeatedly copied
to argv[0].  I used *s[] = { "hello", "there", "people"}, and started the
program with something like 'foo &'. ps always showed 'foo' in the column 
COMMAND, but a ps -f would show the three different strings, and combinations
of *parts* of the strings like 'thople'.

---
Michael D. Lyons 		phone: +49 911 6415 609
Nixdorf Computer AG 		fax:   +49 911 6415 105
Geschaeftstelle fuer BA 	e-mail: mike@nixba.uucp
Donaustrasse 36
D-8500 Nuernberg 60
Federal Republic of Germany

r4@cbnews.ATT.COM (richard.r.grady..jr) (04/29/89)

In article <2158@pur-phy> sho@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.

We're running AT&T SVR2 here, and it doesn't work on our machine.
I already tried it a couple of months ago.
(I wanted to use the ps command to monitor the progress of a
long-running program started by cron.)

Dick Grady               r4@mvuxd.att.com          ...!att!mvuxd!r4