Newsgroups: comp.windows.x
Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!caen!hellgate.utah.edu!dog.ee.lbl.gov!elf.ee.lbl.gov!torek
From: torek@elf.ee.lbl.gov (Chris Torek)
Subject: Re: pid of grandchild or xterm -e foo process?
Organization: Lawrence Berkeley Laboratory, Berkeley
References: <444@aplcomm.JHUAPL.EDU>
Message-ID: <14149@dog.ee.lbl.gov>
X-Local-Date: Mon, 10 Jun 91 12:48:48 PDT
Reply-To: torek@elf.ee.lbl.gov (Chris Torek)
Date: Mon, 10 Jun 91 19:48:48 GMT
Distribution: na

In article <444@aplcomm.JHUAPL.EDU> randy@aplcomm.jhuapl.edu
(RANDALL SCHRICKEL) asks:
>... How can a process find the pid of its grandchild ...

In general, one cannot.

>OR how can a process that execs an xterm with the -e option find the 
>pid of the process running in the xterm?

There is, however, a way to cheat: before running the xterm, arrange
for a rendezvous point (a standard descriptor, a pipe, a named pipe,
a socket, a file, whatever) on which the `xterm -e' child is to write
its process it.  Then, instead of

	xterm -e foo bar baz

use

	xterm -e myhack foo bar baz

where `myhack' is a little program somewhat like this:

	#include <errno.h>
	#include <stdio.h>
	#include <string.h>

	int main(argc, argv) int argc; char **argv; {
		char pidbuf[40];

		if (argc < 2) {
			(void) fprintf(stderr, "usage: myhack prog args\n");
			exit(1);
		}
		(void) sprintf(pidbuf, "%d", getpid());
		<<open/write/whatever: send process id in pidbuf
		  to grandparent according to your chosen protocol>>
		execlp(argv[1], argv + 1);
		(void) fprintf(stderr, "%s: cannot run %s: %s\n",
		    argv[0], argv[1], strerror(errno));
		exit(1);
	}

This merely makes use of the fact that process IDs are retained across
exec().
-- 
In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 415 486 5427)
Berkeley, CA		Domain:	torek@ee.lbl.gov
