From owner-freebsd-hackers Sat Nov 21 22:43:05 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id WAA08562 for freebsd-hackers-outgoing; Sat, 21 Nov 1998 22:43:05 -0800 (PST) (envelope-from owner-freebsd-hackers@FreeBSD.ORG) Received: from mail2.rochester.rr.com (mail2-1.twcny.rr.com [24.92.226.140]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id WAA08547 for ; Sat, 21 Nov 1998 22:43:03 -0800 (PST) (envelope-from leisner@rochester.rr.com) Received: from rochester.rr.com ([24.93.25.38]) by mail2.rochester.rr.com (Post.Office MTA v3.5.2 release 221 ID# 0-53939U80000L80000S0V35) with ESMTP id com; Sun, 22 Nov 1998 01:43:03 -0500 Received: from dw (localhost [127.0.0.1]) by rochester.rr.com (8.8.5/8.8.5) with ESMTP id BAA01487; Sun, 22 Nov 1998 01:42:26 -0500 Message-Id: <199811220642.BAA01487@rochester.rr.com> X-Mailer: exmh version 2.0.2 2/24/98 Reply-to: leisner@rochester.rr.com To: Robert Nordier cc: kpielorz@tdx.co.uk (Karl Pielorz), hackers@FreeBSD.ORG Subject: Re: Getting PID of parent pipe? In-reply-to: Your message of "Sat, 21 Nov 1998 14:38:37 +0200." <199811211238.OAA00989@ceia.nordier.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Date: Sun, 22 Nov 1998 01:42:26 -0500 From: "Marty Leisner" Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG 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