[comp.windows.x] pid of grandchild or xterm -e foo process?

randy@aplcomm.jhuapl.edu (RANDALL SCHRICKEL ) (06/11/91)

I think this is a single question, just phrased two different ways for the two
groups. How can a process find the pid of its grandchild (exec'd from an exec'd
child); OR how can a process that execs an xterm with the -e option find the 
pid of the process running in the xterm? I'm sure I could do it somehow with the
grandchild talking back to the original process, but then I'd have to change 
each of the (20 or so) possible grandchildren. I'm running the code on an HP 
9000 835S with HP-UX 7.0, X11R3, and Motif on an HDS X-Terminal. Thanx.

torek@elf.ee.lbl.gov (Chris Torek) (06/11/91)

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