Re: [PATCH] sysctl: add support for poll()

From: Eric W. Biederman
Date: Wed Jun 01 2011 - 23:32:14 EST


Lucas De Marchi <lucas.demarchi@xxxxxxxxxxxxxx> writes:

> CC'ing people as suggested by get_maintainer.pl
>
> On Wed, Jun 1, 2011 at 9:14 AM, Lucas De Marchi
> <lucas.demarchi@xxxxxxxxxxxxxx> wrote:
>> Adding support for poll() in sysctl fs allows userspace to receive
>> notifications when an entry in sysctl changes. This way it's possible to
>> know when hostname/domainname is changed due to the respective syscall
>> has been called or its file under /proc/sys has been written to.

Why do you want to do this? What advantage does this bring to
userspace?

That feels like a pretty big special case.

Eric


>> Signed-off-by: Lucas De Marchi <lucas.demarchi@xxxxxxxxxxxxxx>
>> ---
>> Âfs/proc/proc_sysctl.c  |  40 ++++++++++++++++++++++++++++++++++++++++
>> Âinclude/linux/sysctl.h Â| Â Â8 ++++++++
>> Âinclude/linux/utsname.h | Â 16 ++++++++++++++++
>> Âkernel/sys.c      Â|  Â2 ++
>> Âkernel/utsname_sysctl.c | Â 36 ++++++++++++++++++++++++++++++++++++
>> Â5 files changed, 102 insertions(+), 0 deletions(-)
>>
>> diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
>> index f50133c..2e5d3ec 100644
>> --- a/fs/proc/proc_sysctl.c
>> +++ b/fs/proc/proc_sysctl.c
>> @@ -3,6 +3,7 @@
>> Â*/
>> Â#include <linux/init.h>
>> Â#include <linux/sysctl.h>
>> +#include <linux/poll.h>
>> Â#include <linux/proc_fs.h>
>> Â#include <linux/security.h>
>> Â#include <linux/namei.h>
>> @@ -176,6 +177,43 @@ static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
>> Â Â Â Âreturn proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
>> Â}
>>
>> +static int proc_sys_open(struct inode *inode, struct file *filp)
>> +{
>> + Â Â Â struct ctl_table *table = PROC_I(inode)->sysctl_entry;
>> +
>> + Â Â Â if (table->poll) {
>> + Â Â Â Â Â Â Â unsigned long event = atomic_read(&table->poll->event);
>> +
>> + Â Â Â Â Â Â Â filp->private_data = (void *)event;
>> + Â Â Â }
>> +
>> + Â Â Â return 0;
>> +}
>> +
>> +static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
>> +{
>> + Â Â Â struct inode *inode = filp->f_path.dentry->d_inode;
>> + Â Â Â struct ctl_table *table = PROC_I(inode)->sysctl_entry;
>> + Â Â Â unsigned long event = (unsigned long)filp->private_data;
>> + Â Â Â unsigned int ret = POLLIN | POLLRDNORM;
>> +
>> + Â Â Â if (!table->proc_handler)
>> + Â Â Â Â Â Â Â goto out;
>> +
>> + Â Â Â if (!table->poll)
>> + Â Â Â Â Â Â Â goto out;
>> +
>> + Â Â Â poll_wait(filp, &table->poll->wait, wait);
>> +
>> + Â Â Â if (event != atomic_read(&table->poll->event)) {
>> + Â Â Â Â Â Â Â filp->private_data = (void *)(unsigned long)atomic_read(
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â &table->poll->event);
>> + Â Â Â Â Â Â Â ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
>> + Â Â Â }
>> +
>> +out:
>> + Â Â Â return ret;
>> +}
>>
>> Âstatic int proc_sys_fill_cache(struct file *filp, void *dirent,
>> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âfilldir_t filldir,
>> @@ -367,6 +405,8 @@ static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct
>> Â}
>>
>> Âstatic const struct file_operations proc_sys_file_operations = {
>> +    .open      = proc_sys_open,
>> +    .poll      = proc_sys_poll,
>>    Â.read      = proc_sys_read,
>>    Â.write     Â= proc_sys_write,
>>    Â.llseek     = default_llseek,
>> diff --git a/include/linux/sysctl.h b/include/linux/sysctl.h
>> index 11684d9..96c89ba 100644
>> --- a/include/linux/sysctl.h
>> +++ b/include/linux/sysctl.h
>> @@ -25,6 +25,7 @@
>> Â#include <linux/kernel.h>
>> Â#include <linux/types.h>
>> Â#include <linux/compiler.h>
>> +#include <linux/wait.h>
>>
>> Âstruct completion;
>>
>> @@ -1011,6 +1012,12 @@ extern int proc_do_large_bitmap(struct ctl_table *, int,
>> Â* cover common cases.
>> Â*/
>>
>> +/* Support for userspace poll() to watch for changes */
>> +struct ctl_table_poll {
>> + Â Â Â atomic_t event;
>> + Â Â Â wait_queue_head_t wait;
>> +};
>> +
>> Â/* A sysctl table is an array of struct ctl_table: */
>> Âstruct ctl_table
>> Â{
>> @@ -1021,6 +1028,7 @@ struct ctl_table
>> Â Â Â Âstruct ctl_table *child;
>> Â Â Â Âstruct ctl_table *parent; Â Â Â /* Automatically set */
>> Â Â Â Âproc_handler *proc_handler; Â Â /* Callback for text formatting */
>> + Â Â Â struct ctl_table_poll *poll;
>> Â Â Â Âvoid *extra1;
>> Â Â Â Âvoid *extra2;
>> Â};
>> diff --git a/include/linux/utsname.h b/include/linux/utsname.h
>> index 4e5b021..c714ed7 100644
>> --- a/include/linux/utsname.h
>> +++ b/include/linux/utsname.h
>> @@ -37,6 +37,14 @@ struct new_utsname {
>> Â#include <linux/nsproxy.h>
>> Â#include <linux/err.h>
>>
>> +enum uts_proc {
>> + Â Â Â UTS_PROC_OSTYPE,
>> + Â Â Â UTS_PROC_OSRELEASE,
>> + Â Â Â UTS_PROC_VERSION,
>> + Â Â Â UTS_PROC_HOSTNAME,
>> + Â Â Â UTS_PROC_DOMAINNAME,
>> +};
>> +
>> Âstruct user_namespace;
>> Âextern struct user_namespace init_user_ns;
>>
>> @@ -80,6 +88,14 @@ static inline struct uts_namespace *copy_utsname(unsigned long flags,
>> Â}
>> Â#endif
>>
>> +#ifdef CONFIG_PROC_SYSCTL
>> +extern void uts_proc_notify(enum uts_proc proc);
>> +#else
>> +static inline void uts_proc_notify(enum uts_proc proc)
>> +{
>> +}
>> +#endif
>> +
>> Âstatic inline struct new_utsname *utsname(void)
>> Â{
>> Â Â Â Âreturn &current->nsproxy->uts_ns->name;
>> diff --git a/kernel/sys.c b/kernel/sys.c
>> index e4128b2..ada9cd7 100644
>> --- a/kernel/sys.c
>> +++ b/kernel/sys.c
>> @@ -1211,6 +1211,7 @@ SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
>> Â Â Â Â Â Â Â Âmemset(u->nodename + len, 0, sizeof(u->nodename) - len);
>> Â Â Â Â Â Â Â Âerrno = 0;
>> Â Â Â Â}
>> + Â Â Â uts_proc_notify(UTS_PROC_HOSTNAME);
>> Â Â Â Âup_write(&uts_sem);
>> Â Â Â Âreturn errno;
>> Â}
>> @@ -1261,6 +1262,7 @@ SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
>> Â Â Â Â Â Â Â Âmemset(u->domainname + len, 0, sizeof(u->domainname) - len);
>> Â Â Â Â Â Â Â Âerrno = 0;
>> Â Â Â Â}
>> + Â Â Â uts_proc_notify(UTS_PROC_DOMAINNAME);
>> Â Â Â Âup_write(&uts_sem);
>> Â Â Â Âreturn errno;
>> Â}
>> diff --git a/kernel/utsname_sysctl.c b/kernel/utsname_sysctl.c
>> index a2cd77e..e96b766 100644
>> --- a/kernel/utsname_sysctl.c
>> +++ b/kernel/utsname_sysctl.c
>> @@ -13,6 +13,7 @@
>> Â#include <linux/uts.h>
>> Â#include <linux/utsname.h>
>> Â#include <linux/sysctl.h>
>> +#include <linux/wait.h>
>>
>> Âstatic void *get_uts(ctl_table *table, int write)
>> Â{
>> @@ -51,12 +52,28 @@ static int proc_do_uts_string(ctl_table *table, int write,
>> Â Â Â Âuts_table.data = get_uts(table, write);
>> Â Â Â Âr = proc_dostring(&uts_table,write,buffer,lenp, ppos);
>> Â Â Â Âput_uts(table, write, uts_table.data);
>> +
>> + Â Â Â if (write) {
>> + Â Â Â Â Â Â Â atomic_inc(&table->poll->event);
>> + Â Â Â Â Â Â Â wake_up_interruptible(&table->poll->wait);
>> + Â Â Â }
>> +
>> Â Â Â Âreturn r;
>> Â}
>> Â#else
>> Â#define proc_do_uts_string NULL
>> Â#endif
>>
>> +static struct ctl_table_poll hostname_poll = {
>> +    .event     Â= ATOMIC_INIT(0),
>> +    .wait      = __WAIT_QUEUE_HEAD_INITIALIZER(hostname_poll.wait),
>> +};
>> +
>> +static struct ctl_table_poll domainname_poll = {
>> +    .event     Â= ATOMIC_INIT(0),
>> +    .wait      = __WAIT_QUEUE_HEAD_INITIALIZER(domainname_poll.wait),
>> +};
>> +
>> Âstatic struct ctl_table uts_kern_table[] = {
>> Â Â Â Â{
>>        Â.procname    = "ostype",
>> @@ -85,6 +102,7 @@ static struct ctl_table uts_kern_table[] = {
>>        Â.maxlen     = sizeof(init_uts_ns.name.nodename),
>>        Â.mode      = 0644,
>>        Â.proc_handler  = proc_do_uts_string,
>> +        .poll      = &hostname_poll,
>> Â Â Â Â},
>> Â Â Â Â{
>>        Â.procname    = "domainname",
>> @@ -92,6 +110,7 @@ static struct ctl_table uts_kern_table[] = {
>>        Â.maxlen     = sizeof(init_uts_ns.name.domainname),
>>        Â.mode      = 0644,
>>        Â.proc_handler  = proc_do_uts_string,
>> +        .poll      = &domainname_poll,
>> Â Â Â Â},
>> Â Â Â Â{}
>> Â};
>> @@ -105,6 +124,23 @@ static struct ctl_table uts_root_table[] = {
>> Â Â Â Â{}
>> Â};
>>
>> +#ifdef CONFIG_PROC_SYSCTL
>> +/*
>> + * Notify userspace about a change in a certain entry of uts_kern_table,
>> + * identified by the parameter proc.
>> + */
>> +void uts_proc_notify(enum uts_proc proc)
>> +{
>> + Â Â Â struct ctl_table *table = &uts_kern_table[proc];
>> +
>> + Â Â Â if (!table->poll)
>> + Â Â Â Â Â Â Â return;
>> +
>> + Â Â Â atomic_inc(&table->poll->event);
>> + Â Â Â wake_up_interruptible(&table->poll->wait);
>> +}
>> +#endif
>> +
>> Âstatic int __init utsname_sysctl_init(void)
>> Â{
>> Â Â Â Âregister_sysctl_table(uts_root_table);
>> --
>> 1.7.5.2
>>
>>
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/