All of lore.kernel.org
 help / color / mirror / Atom feed
From: Waiman Long <longman@redhat.com>
To: "Luis R. Rodriguez" <mcgrof@kernel.org>,
	Kees Cook <keescook@chromium.org>
Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org,
	Andrew Morton <akpm@linux-foundation.org>,
	Al Viro <viro@zeniv.linux.org.uk>,
	Matthew Wilcox <willy@infradead.org>,
	Waiman Long <longman@redhat.com>
Subject: [PATCH 2/5] sysctl: Add a new handler proc_show_minmax()
Date: Wed,  7 Mar 2018 15:34:26 -0500	[thread overview]
Message-ID: <1520454869-13871-3-git-send-email-longman@redhat.com> (raw)
In-Reply-To: <1520454869-13871-1-git-send-email-longman@redhat.com>

The new handler will show the minimum and maximum values specified
in the extra1 and extra2 fields of the sysctl table entry.

Signed-off-by: Waiman Long <longman@redhat.com>
---
 include/linux/sysctl.h |  2 ++
 kernel/sysctl.c        | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 93 insertions(+)

diff --git a/include/linux/sysctl.h b/include/linux/sysctl.h
index bc09361..decb71e 100644
--- a/include/linux/sysctl.h
+++ b/include/linux/sysctl.h
@@ -63,6 +63,8 @@ extern int proc_doulongvec_ms_jiffies_minmax(struct ctl_table *table, int,
 				      void __user *, size_t *, loff_t *);
 extern int proc_do_large_bitmap(struct ctl_table *, int,
 				void __user *, size_t *, loff_t *);
+extern int proc_show_minmax(struct ctl_table *table, int write,
+			    void __user *buffer, size_t *lenp, loff_t *ppos);
 
 /*
  * Register a set of sysctl names by calling register_sysctl_table
diff --git a/kernel/sysctl.c b/kernel/sysctl.c
index 6c68e77..486126f 100644
--- a/kernel/sysctl.c
+++ b/kernel/sysctl.c
@@ -2700,6 +2700,97 @@ int proc_douintvec_minmax(struct ctl_table *table, int write,
 				 do_proc_douintvec_minmax_conv, &param);
 }
 
+/**
+ * proc_show_minmax - show the min/max values
+ * @table: the sysctl table
+ * @write: should always be %FALSE
+ * @buffer: the user buffer
+ * @lenp: the size of the user buffer
+ * @ppos: file position
+ *
+ * The table->data points to the original ctl_table which is used
+ * to determine the type of the min/max range as defined in the
+ * corresponding extra1 (min) and extra2 (max) field. The range
+ * will be displayed as "[min, max]".
+ *
+ * Returns 0 on success.
+ */
+int proc_show_minmax(struct ctl_table *table, int write,
+			  void __user *buffer, size_t *lenp, loff_t *ppos)
+{
+	struct ctl_table *ctbl = (struct ctl_table *)table->data;
+	unsigned long min, max;
+	bool min_neg, max_neg;
+	int err = 0;
+	size_t left;
+
+	if (write || !ctbl || ((ctbl->maxlen != sizeof(int)) &&
+			       (ctbl->maxlen != sizeof(long))))
+		return -EINVAL;
+
+	if (*ppos) {
+		/* Not the first time */
+		*lenp = 0;
+		return 0;
+	}
+
+	min_neg = max_neg = false;
+	if (ctbl->proc_handler == proc_douintvec_minmax) {
+		min = ctbl->extra1 ? *(unsigned int *)ctbl->extra1 : 0;
+		max = ctbl->extra2 ? *(unsigned int *)ctbl->extra2 : UINT_MAX;
+	} else if (ctbl->maxlen == sizeof(int)) {
+		/* Assume to be signed integer */
+		unsigned int val;
+
+		val = ctbl->extra1 ? *(int *)ctbl->extra1 : -INT_MAX;
+		if (val < 0) {
+			min = -val;
+			min_neg = true;
+		} else {
+			min = val;
+			min_neg = false;
+		}
+		val = ctbl->extra2 ? *(int *)ctbl->extra2 : INT_MAX;
+		if (val < 0) {
+			max = -val;
+			max_neg = true;
+		} else {
+			max = val;
+			max_neg = false;
+		}
+	} else {
+		/* Assume to be unsigned long */
+		min = ctbl->extra1 ? *(unsigned long *)ctbl->extra1 : 0;
+		max = ctbl->extra2 ? *(unsigned long *)ctbl->extra2 : ULONG_MAX;
+	}
+
+	left = *lenp;
+
+	/*
+	 * Error checks are done only for proc_put_long() and the
+	 * last proc_put_char().
+	 */
+	proc_put_char(&buffer, &left, '[');
+	err = proc_put_long(&buffer, &left, min, min_neg);
+	if (err)
+		goto out;
+
+	proc_put_char(&buffer, &left, ',');
+	proc_put_char(&buffer, &left, ' ');
+	err = proc_put_long(&buffer, &left, max, max_neg);
+	if (err)
+		goto out;
+
+	proc_put_char(&buffer, &left, ']');
+	err = proc_put_char(&buffer, &left, '\n');
+
+out:
+	*lenp -= left;
+	*ppos += *lenp;
+
+	return err;
+}
+
 static int do_proc_dopipe_max_size_conv(unsigned long *lvalp,
 					unsigned int *valp,
 					int write, void *data)
-- 
1.8.3.1

  parent reply	other threads:[~2018-03-07 20:35 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-07 20:34 [PATCH 0/5] sysctl: Enable easy addition of range showing sysctl parameters Waiman Long
2018-03-07 20:34 ` [PATCH 1/5] sysctl: Clarify how the ctl_table.flags should be set Waiman Long
2018-03-07 20:34 ` Waiman Long [this message]
2018-03-07 20:34 ` [PATCH 3/5] sysctl: Add a new ctl_table flag to show min/max range Waiman Long
2018-03-07 20:34 ` [PATCH 4/5] proc/sysctl: Handle CTL_FLAGS_SHOW_RANGE ctl_table flag Waiman Long
2018-03-07 20:34 ` [PATCH 5/5] ipc: Show ranges of msgmni and shmmni with CTL_FLAGS_SHOW_RANGE Waiman Long
2018-03-07 22:48 ` [PATCH 0/5] sysctl: Enable easy addition of range showing sysctl parameters Andrew Morton
2018-03-08 19:21   ` Waiman Long
2018-03-08 18:34 ` Luis R. Rodriguez
2018-03-08 19:11   ` Waiman Long

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=1520454869-13871-3-git-send-email-longman@redhat.com \
    --to=longman@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=keescook@chromium.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mcgrof@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=willy@infradead.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.