Date: Sun, 22 Nov 1998 01:42:26 -0500 From: "Marty Leisner" <leisner@rochester.rr.com> To: Robert Nordier <rnordier@nordier.com> Cc: kpielorz@tdx.co.uk (Karl Pielorz), hackers@FreeBSD.ORG Subject: Re: Getting PID of parent pipe? Message-ID: <199811220642.BAA01487@rochester.rr.com> In-Reply-To: Your message of "Sat, 21 Nov 1998 14:38:37 %2B0200." <199811211238.OAA00989@ceia.nordier.com>
next in thread | previous in thread | raw e-mail | index | archive | help
I suppose you can do something fancy with lsof, noting the current pid has one end of the pipe, and looking for the matching end... This is from linux, my freebsd lsof isn't working correctly now... If I do cat | wc I can:: leisner@dw;lsof -p 1464 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME wc 1464 leisner cwd DIR 0,3 1024 554027046 /a/compaq/root (compaq:/) wc 1464 leisner rtd DIR 3,5 1024 2 / wc 1464 leisner mem REG 3,7 94749 36915 /usr/gnu (/dev/hda7) wc 1464 leisner mem REG 3,5 79144 6163 / (/dev/hda5) wc 1464 leisner mem REG 8,22 1895316 80016 /usr/local/lib/libc.so.5.4.44 wc 1464 leisner 0r FIFO 0x044c7000 0 PIPE <- wc 1464 leisner 1u CHR 3,1 0t0 2996 /dev/ttyp1 wc 1464 leisner 2u CHR 3,1 0t0 2996 /dev/ttyp1 and then : : leisner@dw;lsof | grep 44c7 cat 1463 leisner 1w FIFO 0x044c7000 0 PIPE -> wc 1464 leisner 0r FIFO 0x044c7000 0 PIPE <- this should work the same way on freebsd... > Karl Pielorz wrote: > > > Is there an 'easy' way of getting the PID of a parent process feeding a > > command's stdin? > > > > e.g. > > > > cat something | foo | bar > > You can use getppid(2). But the answer will be the PID of the shell, > in this instance. > > > > > I need to get the PID of foo while running as bar... > > The problem is that foo is not the parent of bar in your example. The > parent is the process that does the fork(2). > > If foo does exec bar itself (ie. somewhat like xargs): > > cat something | foo bar > > then getppid() will work. > > Given the PID of bar, the PID of foo will be more or less predictable, > so you may be able to get by using (ps plus grep plus) heuristics. > Otherwise, this should entail doing something similar to the fstat(1) > logic. > > -- > Robert Nordier > > To Unsubscribe: send mail to majordomo@FreeBSD.org > with "unsubscribe freebsd-hackers" in the body of the message Marty Leisner To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199811220642.BAA01487>