From owner-freebsd-hackers@freebsd.org Thu Jan 21 21:17:25 2016 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id DF514A8CCBE for ; Thu, 21 Jan 2016 21:17:25 +0000 (UTC) (envelope-from slw@zxy.spb.ru) Received: from zxy.spb.ru (zxy.spb.ru [195.70.199.98]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 9FA4317F5 for ; Thu, 21 Jan 2016 21:17:25 +0000 (UTC) (envelope-from slw@zxy.spb.ru) Received: from slw by zxy.spb.ru with local (Exim 4.86 (FreeBSD)) (envelope-from ) id 1aMMbX-0004oV-14; Fri, 22 Jan 2016 00:17:19 +0300 Date: Fri, 22 Jan 2016 00:17:18 +0300 From: Slawa Olhovchenkov To: "Batutis, Ed" Cc: "freebsd-hackers@freebsd.org" Subject: Re: get number of open files in a process? Message-ID: <20160121211718.GI88527@zxy.spb.ru> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) X-SA-Exim-Connect-IP: X-SA-Exim-Mail-From: slw@zxy.spb.ru X-SA-Exim-Scanned: No (on zxy.spb.ru); SAEximRunCond expanded to false X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 21 Jan 2016 21:17:26 -0000 On Thu, Jan 21, 2016 at 01:34:18PM +0000, Batutis, Ed wrote: > Hi, > > I need to determine the number of open files in my process - all types of file handles total - sockets, files, everything. > > Does this work reliably for that purpose? > > int num_open = 0; /* number of open files? */ > kinfo_file *inf = kinfo_getfile(getpid(), &num_open); > if ( inf ) { > free(inf); > } # limits Resource limits (current): cputime infinity secs filesize infinity kB datasize 33554432 kB stacksize 524288 kB coredumpsize infinity kB memoryuse infinity kB memorylocked infinity kB maxprocesses 8485 openfiles 116442 sbsize infinity bytes vmemoryuse infinity kB pseudo-terminals infinity swapuse infinity kB # df -ki /dev/fd/ Filesystem 1024-blocks Used Avail Capacity iused ifree %iused Mounted on fdescfs 1 1 0 100% 4 116439 0% /dev/fd ^^^^^ ^^^^^^