From owner-freebsd-arch@FreeBSD.ORG Thu May 8 07:44:23 2014 Return-Path: Delivered-To: freebsd-arch@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 4D65286D; Thu, 8 May 2014 07:44:23 +0000 (UTC) Received: from mail-pa0-x22c.google.com (mail-pa0-x22c.google.com [IPv6:2607:f8b0:400e:c03::22c]) (using TLSv1 with cipher ECDHE-RSA-RC4-SHA (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 1D15FAAD; Thu, 8 May 2014 07:44:23 +0000 (UTC) Received: by mail-pa0-f44.google.com with SMTP id ld10so2373262pab.31 for ; Thu, 08 May 2014 00:44:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:date:from:to:cc:subject:message-id:references:mime-version :content-type:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=LTXs1u6npUSEPg3OGRlgi9hiUPDABLfIE2kmVSKTBV0=; b=QM+rM5vcr6vjfKYR5s+7bMmWyYdfcmx8vwppyxMxMFlH2iktxE8vl4thzhR1Vuj43j oqmqPjKTn3QiO9Tz+mgj0pBc5UsaiMbKfHu5HUFANxHS2HF+4+6pPobzjlgWGnLC3Xtt oO9xiVA4Fi2mpLLk4kiDXrtT96K3K0MYWvzCnyg1UBpCMVjt9XkwhFtaAPe195MZOfkR x05K341uicpv97GWRwcleH/0OXRgAWJHd6F0B7fa8uks8RUrY0fNkrW6tyV6D9P75LNm MwSiIVi6Q1SlvwhpX53iy8rtQBSgwV30iCbiIkAMTOeBCYDPJ3znTtxcJnSLSkXxjN1G AmPg== X-Received: by 10.66.150.169 with SMTP id uj9mr4616967pab.148.1399535062297; Thu, 08 May 2014 00:44:22 -0700 (PDT) Received: from localhost (c-76-21-78-151.hsd1.ca.comcast.net. [76.21.78.151]) by mx.google.com with ESMTPSA id gj9sm518389pbc.7.2014.05.08.00.44.21 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 May 2014 00:44:21 -0700 (PDT) Sender: Gleb Kurtsou Date: Thu, 8 May 2014 00:44:50 -0700 From: Gleb Kurtsou To: Thomas Quinot Subject: Re: Proposed extension to stat(1) Message-ID: <20140508074450.GA2678@reks> References: <20140507091213.GA55856@melamine.cuivre.fr.eu.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20140507091213.GA55856@melamine.cuivre.fr.eu.org> User-Agent: Mutt/1.5.22 (2013-10-16) Cc: freebsd-arch@freebsd.org X-BeenThere: freebsd-arch@freebsd.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Discussion related to FreeBSD architecture List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 08 May 2014 07:44:23 -0000 On (07/05/2014 11:12), Thomas Quinot wrote: > I'm proposing the addition of a command line switch -H to stat(1), > causing arguments to be interpreted as NFS file handles instead of file > names. This comes in handy when investigating NFS load issues. > > (A further possible extension would be to optionally include information > from statfs/fhstatfs, but that's next on my list :) ). > > Thomas. > > Index: usr.bin/stat/stat.1 > =================================================================== > --- usr.bin/stat/stat.1 (révision 265192) > +++ usr.bin/stat/stat.1 (copie de travail) > @@ -38,7 +38,7 @@ > .Nd display file status > .Sh SYNOPSIS > .Nm > -.Op Fl FLnq > +.Op Fl FHLnq > .Op Fl f Ar format | Fl l | r | s | x > .Op Fl t Ar timefmt > .Op Ar > @@ -124,6 +124,12 @@ > .Fl F > implies > .Fl l . > +.It Fl H > +Treat each argument as the hexadecimal representation of an NFS file handle, > +and use > +.Xr fhstat 2 > +instead of > +.Xr lstat 2 . Noting that it requires root may be helpful. 'each argument' confused me a lot. > .It Fl L > Use > .Xr stat 2 > Index: usr.bin/stat/stat.c > =================================================================== > --- usr.bin/stat/stat.c (révision 265192) > +++ usr.bin/stat/stat.c (copie de travail) > @@ -50,6 +50,7 @@ > #include > #include > #include > +#include > > #include > #include > @@ -203,9 +204,10 @@ > { > struct stat st; > int ch, rc, errs, am_readlink; > - int lsF, fmtchar, usestat, fn, nonl, quiet; > + int lsF, fmtchar, usestat, nfs_handle, fn, nonl, quiet; > const char *statfmt, *options, *synopsis; > char dname[sizeof _PATH_DEV + SPECNAMELEN] = _PATH_DEV; > + fhandle_t fhnd; whitespace? > const char *file; > > am_readlink = 0; > @@ -212,6 +214,7 @@ > lsF = 0; > fmtchar = '\0'; > usestat = 0; > + nfs_handle = 0; whitespace? > nonl = 0; > quiet = 0; > linkfail = 0; > @@ -226,9 +229,9 @@ > fmtchar = 'f'; > quiet = 1; > } else { > - options = "f:FlLnqrst:x"; > + options = "f:FHlLnqrst:x"; > synopsis = "[-FLnq] [-f format | -l | -r | -s | -x] " > - "[-t timefmt] [file ...]"; > + "[-t timefmt] [file|handle ...]"; > } > > while ((ch = getopt(argc, argv, options)) != -1) > @@ -236,6 +239,9 @@ > case 'F': > lsF = 1; > break; > + case 'H': > + nfs_handle = 1; > + break; Whitespace. > case 'L': > usestat = 1; > break; > @@ -320,8 +326,33 @@ > file = "(stdin)"; > rc = fstat(STDIN_FILENO, &st); > } else { > + int j; > + char *inval; > + > file = argv[0]; > - if (usestat) { > + if (nfs_handle) { > + rc = 0; > + bzero (&fhnd, sizeof fhnd); Style. Extra space after bzero. > + j = MIN(2 * sizeof fhnd, strlen(file)); sizeof style. > + if (j & 1) { > + rc = -1; > + } else { > + while (j) { > + ((char*) &fhnd)[j / 2 - 1] = strtol(&file[j - 2], &inval, 16); It's badly broken: - Result for excessive "01" vs "0102" will differ in strlen(file) > sizeof(fhnd) * 2 case. - Will "+1-2" be considered a valid file handle? - why do we care about strlen(file) != sizeof(fh) case? > + if (inval != NULL) { According to strtol(3) such error handling is insufficient. At least it won't work for strlen(file) > sizeof(fhnd) * 2 case. > + rc = -1; > + break; > + } > + argv[0][j - 2] = '\0'; First we initialize file = argv[0]. Then we set every third character in argv[0] to '\0'. Afterwards file is used to print error message or actual result. > + j -= 2; > + } > + if (!rc) style. rc is not bool. > + rc = fhstat(&fhnd, &st); > + else > + errno = EINVAL; > + } > + > + } else if (usestat) { > /* > * Try stat() and if it fails, fall back to > * lstat() just in case we're examining a