From owner-freebsd-hackers Fri Apr 10 15:18:42 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id PAA26972 for freebsd-hackers-outgoing; Fri, 10 Apr 1998 15:18:42 -0700 (PDT) (envelope-from owner-freebsd-hackers@FreeBSD.ORG) Received: from smtp02.primenet.com (smtp02.primenet.com [206.165.6.132]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id PAA26958 for ; Fri, 10 Apr 1998 15:18:32 -0700 (PDT) (envelope-from tlambert@usr07.primenet.com) Received: (from daemon@localhost) by smtp02.primenet.com (8.8.8/8.8.8) id PAA29468; Fri, 10 Apr 1998 15:18:26 -0700 (MST) Received: from usr07.primenet.com(206.165.6.207) via SMTP by smtp02.primenet.com, id smtpd029438; Fri Apr 10 15:18:24 1998 Received: (from tlambert@localhost) by usr07.primenet.com (8.8.5/8.8.5) id PAA01789; Fri, 10 Apr 1998 15:18:22 -0700 (MST) From: Terry Lambert Message-Id: <199804102218.PAA01789@usr07.primenet.com> Subject: Re: filename from open file descriptor... To: scrappy@hub.org (The Hermit Hacker) Date: Fri, 10 Apr 1998 22:18:22 +0000 (GMT) Cc: freebsd-hackers@FreeBSD.ORG In-Reply-To: from "The Hermit Hacker" at Apr 10, 98 04:47:48 pm X-Mailer: ELM [version 2.4 PL25] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG > I'm trying to debug some code, and have an open file descriptor > that I want to find the file name that its associated with... > > is this possible? my first thought was fstat(), but that appears > to return everything but the name :( You remember the name when you open the file. Untested: ---------------------------------------------------------------------------- /* * This code is relased under BSD licensce in the name of Terrence * Lambert. Please place appropriate license text into any released * version of this software. */ static char **saved_open_names; /* * Overrides weak "open" symbol from libc */ int open( const char *path, int flags, mode_t mode) { int rv; if( saved_open_names == NULL) { int count; count = getdtablesize(); saved_open_names = (char **)calloc( count, sizeof(char *)); } if( saved_open_names == NULL) { rv = -1; errno = ENOMEM; } else { char *p; if( ( p = strdup( path)) == NULL) { rv = -1; errno = ENOMEM; } else { if( ( rv = syscall( SYS_open, path, flags, mode)) == -1) { free( p); } else { if( saved_open_names[ rv] != NULL) { /* could implement 'close' instead...*/ free( saved_open_names[ rv]); } saved_open_names[ rv] = p; } } } return( rv); } /* * New call... */ char * fdname( int fd) { char *rv = NULL; if( saved_open_names != NULL) { if( fd >= 0 && fd < getdtablesize()) { rv = saved_open_names[ fd]; } } return( rv); } ---------------------------------------------------------------------------- Terry Lambert terry@lambert.org --- Any opinions in this posting are my own and not those of my present or previous employers. To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message