Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp992136ybl; Wed, 4 Dec 2019 14:44:34 -0800 (PST) X-Google-Smtp-Source: APXvYqw94SeDmEert0Xg8hgVQxb3rkESQR4niAUA1oqdQ6bbJ0TN1n4XuJ8sZal2v3zl1wsSqBQP X-Received: by 2002:aca:4a41:: with SMTP id x62mr4477051oia.148.1575499474675; Wed, 04 Dec 2019 14:44:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1575499474; cv=none; d=google.com; s=arc-20160816; b=YMMTTVoGKEazOJFO/2OL10GcMIVqGN5sDvtnvqHR//45Kvfl6UOfnFcq85B2iD9Tqg PD5MqM3InU11u5lQXG0h8MLJ3fLlyUlSphKXmYoqju0tcRLyE3Y199cG97/ggNzpnNtI AXWc3JCmldccFwhbKCynu0KEcab2xKNPYBs//Ij7N9iHfCW/Xo9Ng7P/VI5/iFl+i+Bp y7QJ0syFUiJJTuV35PgYkFxMeUJuVVj+VxQZIBI/ZrjcEvPTO0Ued4ZiWCD+TuR/rzcL RJuQKnqpJEi65MzhBjcPeqoYKaRpdcNrE9HOpVxvuc+J6hpwHj+B0gi+Q9m1+eBIeG/P fFvg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=pF5bjopQFQ3A+SmApS9xhaC/5lZoT7rSCk2k4/K0fE4=; b=ejVsjedrAAClYwC18wL9jFnBVUJaF3Ou07sfddhlocQaiygVNHSxoMTa23WsnlG1HW AGL5PP6DbGuaQKWzmVNnIOIdMbSkbrWrwReiNzliu3lV0ttN+G9Q8p1ZhATToBo4hPKO tw9gg0t/f+q7rpGWVr2BIRTPFYBm5JCgUeIx6lKnVdig+icjEGsgOPsdLjexMBdwYKLL +Ww6QdftID1iN8oxkK5XQKAICp9nD8vj02SFxMLqbeqigH9kuHNbl1FIAlo0r5wdj+El ANSdRKuBnj+KhJ+0EESKwypIsjbtP2PVr8jB4CYOj6JZK9uxmbsu/Di5ffd+mmQIwHAV vbgA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Q7CxIZRt; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i185si3345282oia.187.2019.12.04.14.44.22; Wed, 04 Dec 2019 14:44:34 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Q7CxIZRt; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728368AbfLDWmx (ORCPT + 99 others); Wed, 4 Dec 2019 17:42:53 -0500 Received: from mail-pf1-f195.google.com ([209.85.210.195]:39325 "EHLO mail-pf1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727989AbfLDWmw (ORCPT ); Wed, 4 Dec 2019 17:42:52 -0500 Received: by mail-pf1-f195.google.com with SMTP id 2so562874pfx.6; Wed, 04 Dec 2019 14:42:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=pF5bjopQFQ3A+SmApS9xhaC/5lZoT7rSCk2k4/K0fE4=; b=Q7CxIZRtAxRwNYXgAnY2x8ZSABqYYcEYAir0rRuMfUunI+Fxerl//0q8FcT1DVmfjn XzXlJHh5j9uevlTyhz8h7q0o/RYfQjR0ZjoUo1D/1KGJJTkieQNF5JZ4NqchzW7b0e5o qcz616/VCrszZCijm1L7szNpT5eayAgJDHDiTgul0LaYivT7+p5yYoaZAhQSSY8UL+VK dakBEb/CPTPdW4tJVWL77/zTSxlTzK6E7k+j7lqiksgGkRFYf8y5NXzocpls2ycBrtWl XM37VPKiRXfJW9osEbPEDJKsDKQKdFjQuH40qCYHeXzudpIzHdVg84WYRO24o04dPWeK fS+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=pF5bjopQFQ3A+SmApS9xhaC/5lZoT7rSCk2k4/K0fE4=; b=gfr8SyQ6eryTJiXeTI3RsVZ3ymNKNsL8U3wwJxBlBN0EwOWder0msdmdpQODlZ5/br YEW+OZYM5PNchoY5SBEY/EpBD7OCPck5iu6memrdsAIdpLtCYHDGXJpdu6EJJB65US3l 117Q9GBWNKp+wAXgDlB1Df8DjEwNcjO5PSpIXnOE/suMglT2qagRQLYji7AfRedxg8e7 gX/oRleLnR9seubuHieZnuE/xrnkSAQC9TV4vOZzRVdieIPEGQ5QvjtMP5S4yqPoG+XP RqpPln6/pk1ZYLuWFcekAkVG5VjF4WRzLTert+z0I5pcYbufmPq+oS/h5lILsaX6ohgv WLTA== X-Gm-Message-State: APjAAAWbiatoC5da41dZNRCmE0hkCn6m76UEH6s3qQdwPi7pzctqbVyw kOye8bjSmiQRX1Ohae0emKM/PYGb2WCubev8rYs= X-Received: by 2002:a63:e14a:: with SMTP id h10mr5900277pgk.74.1575499371944; Wed, 04 Dec 2019 14:42:51 -0800 (PST) MIME-Version: 1.0 References: <20191204155941.17814-1-brgl@bgdev.pl> <20191204155941.17814-2-brgl@bgdev.pl> In-Reply-To: <20191204155941.17814-2-brgl@bgdev.pl> From: Andy Shevchenko Date: Thu, 5 Dec 2019 00:42:39 +0200 Message-ID: Subject: Re: [PATCH v2 11/11] tools: gpio: implement gpio-watch To: Bartosz Golaszewski Cc: Kent Gibson , Linus Walleij , "open list:GPIO SUBSYSTEM" , Linux Kernel Mailing List , Bartosz Golaszewski Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Dec 4, 2019 at 7:19 PM Bartosz Golaszewski wrote: > Add a simple program that allows to test the new LINECHANGED_FD ioctl(). > --- a/tools/gpio/.gitignore > +++ b/tools/gpio/.gitignore > @@ -1,4 +1,5 @@ > gpio-event-mon > gpio-hammer > lsgpio > +gpio-watch Perhaps keep it sorted? > +++ b/tools/gpio/gpio-watch.c > @@ -0,0 +1,112 @@ > +// SPDX-License-Identifier: GPL-2.0-only > +/* > + * gpio-watch - monitor unrequested lines for property changes using the > + * character device > + * > + * Copyright (C) 2019 BayLibre SAS > + * Author: Bartosz Golaszewski > + */ > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +static bool isnumber(const char *str) > +{ > + size_t sz = strlen(str); > + int i; > + > + for (i = 0; i < sz; i++) { > + if (!isdigit(str[i])) > + return false; > + } > + > + return true; > +} strtoul() will do the same. char *p; unsigned long dummy; // do we need it? dummy = strtoul(..., &p); return *p == '\0'; > +int main(int argc, char **argv) > +{ > + struct gpioline_info_changed chg; > + struct gpioline_info req; > + struct pollfd pfd; > + int fd, i, j, ret; > + char *event; > + ssize_t rd; > + > + if (argc < 3) > + goto err_usage; > + > + fd = open(argv[1], O_RDWR | O_CLOEXEC); > + if (fd < 0) { > + perror("unable to open gpiochip"); > + return EXIT_FAILURE; > + } > + > + for (i = 0, j = 2; i < argc - 2; i++, j++) { > + if (!isnumber(argv[j])) > + goto err_usage; > + > + memset(&req, 0, sizeof(req)); > + req.line_offset = atoi(argv[j]); Oh, why not to call strtoul() directly? > + > + ret = ioctl(fd, GPIO_GET_LINEINFO_WATCH_IOCTL, &req); > + if (ret) { > + perror("unable to set up line watch"); Don't you need to unwatch previously added ones? > + return EXIT_FAILURE; > + } > + } > + for (;;) { > + ret = poll(&pfd, 1, 5000); > + if (ret < 0) { > + perror("error polling the linechanged fd"); > + return EXIT_FAILURE; > + } else if (ret > 0) { > + memset(&chg, 0, sizeof(chg)); > + rd = read(pfd.fd, &chg, sizeof(chg)); > + if (rd < 0 || rd != sizeof(chg)) { > + if (rd != sizeof(chg)) > + errno = EIO; > + > + perror("error reading line change event"); > + return EXIT_FAILURE; > + } Shouldn't we handle the -EINTR? > + } > + } > + > + return 0; -- With Best Regards, Andy Shevchenko