From: Zev Weiss <zev@bewilderbeest.net>
To: openbmc@lists.ozlabs.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Jeremy Kerr <jk@codeconstruct.com.au>,
Joel Stanley <joel@jms.id.au>, Rob Herring <robh+dt@kernel.org>,
devicetree@vger.kernel.org, Zev Weiss <zev@bewilderbeest.net>,
Andy Shevchenko <andy@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>,
Francis Laniel <laniel_francis@privacyrequired.com>,
Kees Cook <keescook@chromium.org>,
Jonathan Cameron <Jonathan.Cameron@huawei.com>,
Alexey Dobriyan <adobriyan@gmail.com>,
Daniel Axtens <dja@axtens.net>,
Andrey Konovalov <andreyknvl@gmail.com>,
Nick Desaulniers <ndesaulniers@google.com>,
linux-kernel@vger.kernel.org
Subject: [PATCH 3/9] lib/string: add sysfs_buf_streq()
Date: Wed, 6 Oct 2021 17:09:48 -0700 [thread overview]
Message-ID: <20211007000954.30621-4-zev@bewilderbeest.net> (raw)
In-Reply-To: <20211007000954.30621-1-zev@bewilderbeest.net>
This is intended for use with mutable device-tree string properties.
As with sysfs_streq(), it accepts a trailing linefeed for convenient
shell access (e.g. 'echo foo > /sys/firmware/devicetree/...'), but
also accepts a trailing NUL terminator to match how DT string
properties are presented when read (so that we don't reject userspace
writing back exactly what it had previously read from the file).
Signed-off-by: Zev Weiss <zev@bewilderbeest.net>
---
include/linux/string.h | 1 +
lib/string.c | 34 ++++++++++++++++++++++++++++++++++
2 files changed, 35 insertions(+)
diff --git a/include/linux/string.h b/include/linux/string.h
index 5e96d656be7a..d066ff82d1ec 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -183,6 +183,7 @@ extern char **argv_split(gfp_t gfp, const char *str, int *argcp);
extern void argv_free(char **argv);
extern bool sysfs_streq(const char *s1, const char *s2);
+extern bool sysfs_buf_streq(const void *buf, size_t blen, const char *str);
int match_string(const char * const *array, size_t n, const char *string);
int __sysfs_match_string(const char * const *array, size_t n, const char *s);
diff --git a/lib/string.c b/lib/string.c
index b2de45a581f4..aab5cadb6b98 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -715,6 +715,40 @@ bool sysfs_streq(const char *s1, const char *s2)
}
EXPORT_SYMBOL(sysfs_streq);
+/**
+ * sysfs_buf_streq - check if a buffer matches a string, modulo trailing \n or \0
+ * @buf: pointer to the buffer to check
+ * @blen: length of the buffer to check
+ * @str: string to compare against
+ *
+ * This routine returns true iff the buffer equals the string, treating a
+ * trailing \n or \0 on the buffer as an accepted terminator, the length of
+ * which (aside from the optional terminator) must match the length of the
+ * string. It's geared for use with sysfs bin_attribute inputs, which may
+ * terminate with newlines or NULs (the latter to match how device-tree string
+ * properties in particular are presented on read).
+ */
+bool sysfs_buf_streq(const void *buf, size_t blen, const char *str)
+{
+ const char *p = buf;
+ size_t slen = strlen(str);
+ if (blen < slen)
+ return false;
+
+ if (memcmp(p, str, slen))
+ return false;
+
+ switch (blen - slen) {
+ case 0:
+ return true;
+ case 1:
+ return p[slen] == '\n' || p[slen] == '\0';
+ default:
+ return false;
+ }
+}
+EXPORT_SYMBOL(sysfs_buf_streq);
+
/**
* match_string - matches given string in an array
* @array: array of strings
--
2.33.0
next prev parent reply other threads:[~2021-10-07 0:10 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-10-07 0:09 [PATCH 0/9] Dynamic DT device nodes Zev Weiss
2021-10-07 0:09 ` [PATCH 1/9] sysfs: add sysfs_remove_bin_file_self() function Zev Weiss
2021-10-07 5:23 ` Greg Kroah-Hartman
2021-10-07 5:58 ` Zev Weiss
2021-10-07 6:12 ` Greg Kroah-Hartman
2021-10-07 6:55 ` Zev Weiss
2021-10-07 0:09 ` [PATCH 2/9] sysfs: add growable flag to struct bin_attribute Zev Weiss
2021-10-07 0:09 ` Zev Weiss [this message]
2021-10-07 0:09 ` [PATCH 4/9] of: add self parameter to __of_sysfs_remove_bin_file() Zev Weiss
2021-10-07 5:25 ` Greg Kroah-Hartman
2021-10-07 0:09 ` [PATCH 5/9] of: add self parameter to of_update_property() Zev Weiss
2021-10-07 5:26 ` Greg Kroah-Hartman
2021-10-07 0:09 ` [PATCH 6/9] of: add support for 'dynamic' DT property Zev Weiss
2021-10-08 18:51 ` Frank Rowand
2021-10-08 19:19 ` Frank Rowand
2021-10-11 13:58 ` Frank Rowand
2021-10-11 14:46 ` Frank Rowand
2021-10-11 17:35 ` Zev Weiss
2021-10-07 0:09 ` [PATCH 7/9] of: make OF_DYNAMIC selectable independently of OF_UNITTEST Zev Weiss
2021-10-08 19:01 ` Frank Rowand
2021-10-07 0:09 ` [PATCH 8/9] dt-bindings: document new 'dynamic' common property Zev Weiss
2021-10-07 5:26 ` Greg Kroah-Hartman
2021-10-07 6:03 ` Zev Weiss
2021-10-07 0:09 ` [PATCH 9/9] ARM: dts: aspeed: Add e3c246d4i BIOS flash device Zev Weiss
2021-10-07 2:46 ` [PATCH 0/9] Dynamic DT device nodes Florian Fainelli
2021-10-07 5:44 ` Zev Weiss
2021-10-07 7:04 ` Andy Shevchenko
2021-10-07 9:05 ` Zev Weiss
2021-10-07 10:31 ` Greg Kroah-Hartman
2021-10-07 15:41 ` Zev Weiss
2021-10-07 20:03 ` Rob Herring
2021-10-08 5:41 ` Greg Kroah-Hartman
2021-10-08 19:43 ` Frank Rowand
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=20211007000954.30621-4-zev@bewilderbeest.net \
--to=zev@bewilderbeest.net \
--cc=Jonathan.Cameron@huawei.com \
--cc=adobriyan@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=andreyknvl@gmail.com \
--cc=andy@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=dja@axtens.net \
--cc=gregkh@linuxfoundation.org \
--cc=jk@codeconstruct.com.au \
--cc=joel@jms.id.au \
--cc=keescook@chromium.org \
--cc=laniel_francis@privacyrequired.com \
--cc=linux-kernel@vger.kernel.org \
--cc=ndesaulniers@google.com \
--cc=openbmc@lists.ozlabs.org \
--cc=robh+dt@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
all inboxes | Powered by JetHome®