Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 20 Jun 2018 06:57:59 +0000 (UTC)
From:      Hans Petter Selasky <hselasky@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-11@freebsd.org
Subject:   svn commit: r335433 - stable/11/sys/compat/linuxkpi/common/include/linux
Message-ID:  <201806200657.w5K6vxe2032055@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: hselasky
Date: Wed Jun 20 06:57:59 2018
New Revision: 335433
URL: https://svnweb.freebsd.org/changeset/base/335433

Log:
  MFC r334958:
  Implement the kstrtobool() and kstrtobool_from_user() functions
  in the LinuxKPI.
  
  Submitted by:	Johannes Lundberg <johalun0@gmail.com>
  Sponsored by:	Mellanox Technologies
  Sponsored by:	Limelight Networks

Modified:
  stable/11/sys/compat/linuxkpi/common/include/linux/kernel.h
Directory Properties:
  stable/11/   (props changed)

Modified: stable/11/sys/compat/linuxkpi/common/include/linux/kernel.h
==============================================================================
--- stable/11/sys/compat/linuxkpi/common/include/linux/kernel.h	Wed Jun 20 06:57:14 2018	(r335432)
+++ stable/11/sys/compat/linuxkpi/common/include/linux/kernel.h	Wed Jun 20 06:57:59 2018	(r335433)
@@ -50,7 +50,9 @@
 #include <linux/types.h>
 #include <linux/jiffies.h>
 #include <linux/log2.h>
+
 #include <asm/byteorder.h>
+#include <asm/uaccess.h>
 
 #include <machine/stdarg.h>
 
@@ -370,6 +372,46 @@ kstrtou32(const char *cp, unsigned int base, u32 *res)
 	if (temp != (u32)temp)
 		return (-ERANGE);
 	return (0);
+}
+
+static inline int
+kstrtobool(const char *s, bool *res)
+{
+	int len;
+
+	if (s == NULL || (len = strlen(s)) == 0 || res == NULL)
+		return (-EINVAL);
+
+	/* skip newline character, if any */
+	if (s[len - 1] == '\n')
+		len--;
+
+	if (len == 1 && strchr("yY1", s[0]) != NULL)
+		*res = true;
+	else if (len == 1 && strchr("nN0", s[0]) != NULL)
+		*res = false;
+	else if (strncasecmp("on", s, len) == 0)
+		*res = true;
+	else if (strncasecmp("off", s, len) == 0)
+		*res = false;
+	else
+		return (-EINVAL);
+
+	return (0);
+}
+
+static inline int
+kstrtobool_from_user(const char __user *s, size_t count, bool *res)
+{
+	char buf[8] = {};
+
+	if (count > (sizeof(buf) - 1))
+		count = (sizeof(buf) - 1);
+
+	if (copy_from_user(buf, s, count))
+		return (-EFAULT);
+
+	return (kstrtobool(buf, res));
 }
 
 #define min(x, y)	((x) < (y) ? (x) : (y))



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201806200657.w5K6vxe2032055>