From owner-freebsd-hackers@FreeBSD.ORG Thu Nov 13 17:08:05 2014 Return-Path: Delivered-To: hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 7CDCAE26 for ; Thu, 13 Nov 2014 17:08:05 +0000 (UTC) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::1]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id E43D18BA for ; Thu, 13 Nov 2014 17:08:04 +0000 (UTC) Received: from tom.home (kostik@localhost [127.0.0.1]) by kib.kiev.ua (8.14.9/8.14.9) with ESMTP id sADH7xM6099275 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Thu, 13 Nov 2014 19:07:59 +0200 (EET) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.9.2 kib.kiev.ua sADH7xM6099275 Received: (from kostik@localhost) by tom.home (8.14.9/8.14.9/Submit) id sADH7x0h099274; Thu, 13 Nov 2014 19:07:59 +0200 (EET) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Thu, 13 Nov 2014 19:07:59 +0200 From: Konstantin Belousov To: Mike Gelfand Subject: Re: [BUG] Getting path to program binary sometimes fails Message-ID: <20141113170758.GY17068@kib.kiev.ua> References: <91809230-5E81-4A6E-BFD6-BE8815A06BB2@logicnow.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <91809230-5E81-4A6E-BFD6-BE8815A06BB2@logicnow.com> User-Agent: Mutt/1.5.23 (2014-03-12) X-Spam-Status: No, score=-2.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_ADSP_CUSTOM_MED,FREEMAIL_FROM,NML_ADSP_CUSTOM_MED autolearn=no autolearn_force=no version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on tom.home Cc: "hackers@freebsd.org" X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 13 Nov 2014 17:08:05 -0000 On Thu, Nov 13, 2014 at 10:33:47AM +0000, Mike Gelfand wrote: > Hello, > > I?ve been advised (on #freebsd @FreeNode) to send a mail here with details. > > I?m observing a weird issue where process may not able to determine path to its own binary (and maybe other process binaries as well, thought I didn?t test that) under some [not yet clear to me] conditions. The code I use in production first tries to call sysctl(KERN_PROC_PATHNAME) and if that fails fallback to call readlink(/proc/curproc/file); at some point both calls fail with ENOENT. This happens on two FreeBSD 9 (vanilla 9.1-RELEASE and 9.2-RELEASE) machines, and never happens (or at least never happened in over two years) on FreeBSD 8 (vanilla 8.3-RELEASE-p3); all machines are amd64. I?ve been able to reduce the test case to a sample program [1] which I run using a sample script [2], and the error then happens when I?m building clang-devel from ports. I?ve also recorded a screencast [3]. > > Is this a know defect? Do you need any other information other than provided above to reproduce? > > [1] http://mikedld.com/f/selfpath/selfpath.c > [2] http://mikedld.com/f/selfpath/selfpath.sh > [3] http://mikedld.com/f/selfpath/selfpath.mov This is not a defect. The vnode->path translation uses namecache, which could be purged at any time. The behaviour is typical for most unix implementations. Linux and new Solaris have 'rigid' namecache, where name entry lifetime is the same as the vnode lifetime it is attached to. I am not aware of any useful consequences of such design, except vn_fullpath() working more reliable, but at the cost of increased memory usage. Another possible reason for failed translation is the replacement of the binary while it runs. There, rigid namecache does not help. Typically, less time was spent between exec() and vn_fullpath(), more the chances to get the path back.