From owner-freebsd-hackers@freebsd.org Wed Jul 8 16:59:38 2020 Return-Path: Delivered-To: freebsd-hackers@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id DA5B536776D for ; Wed, 8 Jul 2020 16:59:38 +0000 (UTC) (envelope-from mjguzik@gmail.com) Received: from mail-wr1-x441.google.com (mail-wr1-x441.google.com [IPv6:2a00:1450:4864:20::441]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "GTS CA 1O1" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4B25Bf00l1z4JW2 for ; Wed, 8 Jul 2020 16:59:37 +0000 (UTC) (envelope-from mjguzik@gmail.com) Received: by mail-wr1-x441.google.com with SMTP id o11so49800615wrv.9 for ; Wed, 08 Jul 2020 09:59:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=aQKJiOZhorhX3QPFtOleDHwIyfKY2C88CiRdr7Eeo5U=; b=F59Yci+UIu+5dgPZtaTwr4vcgPeKFcym5ku7lkaZA9xsOnIIhBhqmo4DofU2DwDIC3 eHVw/svWbedLqMZE9dViqhZBsYWXnAGz19Tbh+z1mUQNSSroKAzzaKpGZ07zoeNPlmGG spAs82Ay2+psVFb1ZyznVDkg+RAD6FV32+n2SvsSXoFCgQLcfrpiFZpbRI5BkRbRdI5o EjWILz7h5GqVnWiG8br7dffLF7suiPLomdSXJAIrXDpta3VgxMhLHqQk163uxV5Co3RN 23/N5QbXvMeHkcLiwcXhMe/3kJ/06FRJNjHJuMaUhc7M70RvqGkRQ9YyG9ruQ+WGQNb3 Uqzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=aQKJiOZhorhX3QPFtOleDHwIyfKY2C88CiRdr7Eeo5U=; b=FRPwTWh249UgDvXiN7qf2n6sYF8oRDQ1jU7y/xWfg2xWiFn3+0CArne25nQXyCxkQp L3OKyR6f8ztNRIsK/Xb24LiCvRsM1YLoP81F0MCPc1gE7zOjV9lmFUrVBhx9KE209sE0 Ss8GdoTRS+4XKqnOzt/CzDPCR+/HCCMbgONsc/nxNnggOegEjhjGUznAlG372DAuGkZc /ITuHR9So6L1DWwFUSEgoXb0AJb76re8y6tkDljDrUvZT2vsICGMrCcPtQZYSWV4lxib Wjf0pEJLSpS3huxmWvXfZNlKtM5McClw4ZMa7JXf2ymbMxzDcy5cnztFOI1iSO2C9d9q ecBw== X-Gm-Message-State: AOAM531S6gjNqUYiF6FDW9+3deFyiAf1kF8aPcOdMkAhFX7GyWRCrRt9 dJoE3Znz41BtuPMSigGg5rms+/c0lyTstXTB/Y1+AQ== X-Google-Smtp-Source: ABdhPJyxF2skPd4oK0D8L5hb44mt3X45wgW4PXmghtJr++TNCbuIQURYnoEFoc1A0vb7ZS/gJu3d7RXGF45wl/YcH4o= X-Received: by 2002:adf:f311:: with SMTP id i17mr61932191wro.237.1594227576373; Wed, 08 Jul 2020 09:59:36 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:adf:9d84:0:0:0:0:0 with HTTP; Wed, 8 Jul 2020 09:59:35 -0700 (PDT) In-Reply-To: References: <21b0280d-c290-f27f-98a9-0c2242380718@rawbw.com> From: Mateusz Guzik Date: Wed, 8 Jul 2020 18:59:35 +0200 Message-ID: Subject: Re: Is it possible to determine the open file path based on the file descriptor? To: Yuri Cc: Freebsd hackers list Content-Type: text/plain; charset="UTF-8" X-Rspamd-Queue-Id: 4B25Bf00l1z4JW2 X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org; dkim=pass header.d=gmail.com header.s=20161025 header.b=F59Yci+U; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (mx1.freebsd.org: domain of mjguzik@gmail.com designates 2a00:1450:4864:20::441 as permitted sender) smtp.mailfrom=mjguzik@gmail.com X-Spamd-Result: default: False [-2.10 / 15.00]; RCVD_TLS_ALL(0.00)[]; ARC_NA(0.00)[]; R_DKIM_ALLOW(-0.20)[gmail.com:s=20161025]; NEURAL_HAM_MEDIUM(-0.98)[-0.981]; FROM_HAS_DN(0.00)[]; FREEMAIL_FROM(0.00)[gmail.com]; R_SPF_ALLOW(-0.20)[+ip6:2a00:1450:4000::/36]; MIME_GOOD(-0.10)[text/plain]; PREVIOUSLY_DELIVERED(0.00)[freebsd-hackers@freebsd.org]; NEURAL_HAM_LONG(-1.05)[-1.048]; TO_MATCH_ENVRCPT_SOME(0.00)[]; TO_DN_ALL(0.00)[]; DKIM_TRACE(0.00)[gmail.com:+]; RCPT_COUNT_TWO(0.00)[2]; RCVD_IN_DNSWL_NONE(0.00)[2a00:1450:4864:20::441:from]; NEURAL_HAM_SHORT(-0.07)[-0.072]; DMARC_POLICY_ALLOW(-0.50)[gmail.com,none]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; SUBJECT_ENDS_QUESTION(1.00)[]; ASN(0.00)[asn:15169, ipnet:2a00:1450::/32, country:US]; RCVD_COUNT_TWO(0.00)[2]; FREEMAIL_ENVFROM(0.00)[gmail.com]; DWL_DNSWL_NONE(0.00)[gmail.com:dkim] X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Jul 2020 16:59:38 -0000 On 7/8/20, Mateusz Guzik wrote: > On 7/8/20, Yuri wrote: >> Hi Hackers, >> >> >> One of the ports (sysutils/watchman) attempts to find the path of the >> file based on the file descriptor assuming that this descriptor >> represents an open file. >> >> >> Is this possible? >> >> > > Yes and no. procstat -f can report that, but not always. The crux is > that the namecache is optional -- no name may be present and then we > don't have good means to find it. There is a side corner case where > you can have *more* than one name and then the kernel has no idea > which was used to reach the file for given fd. > > I think the namecache should start getting new names in all cases and > that's perhaps something to change for 13. > > Even for the working case the interface used by procstat is a bad pick. > > That said, I can add a fcntl which will translate the fd into a full > path if the name can be found, but I don't know how useful it can be > with this program given the stipulation above. > Now that I wrote that e-mail I remembered I already implemented this, here is a forward version against head: https://people.freebsd.org/~mjg/F_GETPATH.diff #include #define MAXPATHLEN 1024 #define F_GETPATH 22 int main(int argc, char **argv) { char buf[MAXPATHLEN]; int fd; if (argc != 2) return (1); fd = open(argv[1], O_RDONLY); if (fd == -1) err(1, "open"); if (fcntl(fd, F_GETPATH, buf) == -1) err(1, "fcntl"); printf("[%s]\n", buf); } I can't be arsed to find all the proper headers, but you get the idea. If this makes the program work well enough I can commit and merge to stable/12. -- Mateusz Guzik