Linux GPIO subsystem development
 help / color / mirror / Atom feed
From: Ben Hutchings <ben.hutchings@essensium.com>
To: Bartosz Golaszewski <brgl@bgdev.pl>
Cc: "open list:GPIO SUBSYSTEM" <linux-gpio@vger.kernel.org>
Subject: Re: [PATCH libgpiod-v2] tools: Restore support for opening chips by label
Date: Wed, 22 Sep 2021 13:59:12 +0200	[thread overview]
Message-ID: <20210922115912.GB24226@cephalopod> (raw)
In-Reply-To: <CAMRc=McdCUmqny7d11pdf6ShSEbMRf+V0od-SrKmmE7NrjG2Qg@mail.gmail.com>

On Mon, Sep 20, 2021 at 04:32:22PM +0200, Bartosz Golaszewski wrote:
> On Wed, Jul 28, 2021 at 11:19 PM Ben Hutchings <ben.hutchings@mind.be> wrote:
> >
> > Support for opening chips by label was removed because labels
> > are not necessarily unique and lookup by label requires opening
> > every GPIO device.
> >
> > I don't think these concerns apply to the tools.  They will normally
> > be run by root, and if a label is specified it's because it's known to
> > be unique.
[...]
> > +struct gpiod_chip *chip_open_by_label(const char *label)
> > +{
> > +       struct gpiod_chip *chip = NULL, *next = NULL;
> > +       struct dirent **entries;
> > +       int num_chips, i, error = 0;
> > +
> > +       num_chips = scandir("/dev/", &entries, chip_dir_filter, alphasort);
> > +       if (num_chips < 0) {
> > +               error = errno;
> > +               fprintf(stderr, "unable to scan /dev: %s\n", strerror(error));
> > +               goto out;
> > +       }
> > +
> > +       for (i = 0; i < num_chips; i++) {
> > +               next = chip_open_by_name(entries[i]->d_name);
> > +               if (!next) {
> > +                       error = errno;
> > +                       fprintf(stderr, "unable to open %s: %s\n",
> > +                               entries[i]->d_name, strerror(error));
> 
> How about using access() to check the permissions first? This way we
> wouldn't need to spam the user with error messages - we'd just
> silently ignore chips we don't have access to.
[...]

I don't see any benefit in using access() rather than checking for
EACCES; that just seems to add a race condition.

As for whether the error should be reported, this is consistent with
the old behaviour and I did not want to report that "chip label does
not exist" in case of missing permission.  And again, I would expect
the tools to be run as root, so this shouldn't happen in practice.

Perhaps a better approach would be to record any access failure and
then report it at the end only if the label was not found?

Ben.

-- 
Ben Hutchings · Senior Embedded Software Engineer, Essensium-Mind · mind.be

      reply	other threads:[~2021-09-22 11:59 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-28 21:19 [PATCH libgpiod-v2] tools: Restore support for opening chips by label Ben Hutchings
2021-09-20 14:32 ` Bartosz Golaszewski
2021-09-22 11:59   ` Ben Hutchings [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210922115912.GB24226@cephalopod \
    --to=ben.hutchings@essensium.com \
    --cc=brgl@bgdev.pl \
    --cc=linux-gpio@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox