2007-02-14 08:34:12 +00:00
|
|
|
/*
|
|
|
|
* /proc/sys support
|
|
|
|
*/
|
2008-10-17 01:07:44 +00:00
|
|
|
#include <linux/init.h>
|
2007-02-14 08:34:12 +00:00
|
|
|
#include <linux/sysctl.h>
|
2011-11-02 20:39:22 +00:00
|
|
|
#include <linux/poll.h>
|
2007-02-14 08:34:12 +00:00
|
|
|
#include <linux/proc_fs.h>
|
2013-02-28 01:03:16 +00:00
|
|
|
#include <linux/printk.h>
|
2007-02-14 08:34:12 +00:00
|
|
|
#include <linux/security.h>
|
2012-02-13 03:58:52 +00:00
|
|
|
#include <linux/sched.h>
|
2011-01-07 06:49:57 +00:00
|
|
|
#include <linux/namei.h>
|
2012-02-13 03:58:52 +00:00
|
|
|
#include <linux/mm.h>
|
2012-01-06 12:07:15 +00:00
|
|
|
#include <linux/module.h>
|
2007-02-14 08:34:12 +00:00
|
|
|
#include "internal.h"
|
|
|
|
|
2009-02-20 05:58:47 +00:00
|
|
|
static const struct dentry_operations proc_sys_dentry_operations;
|
2007-02-14 08:34:12 +00:00
|
|
|
static const struct file_operations proc_sys_file_operations;
|
2008-02-08 12:21:19 +00:00
|
|
|
static const struct inode_operations proc_sys_inode_operations;
|
2008-07-15 12:54:06 +00:00
|
|
|
static const struct file_operations proc_sys_dir_file_operations;
|
|
|
|
static const struct inode_operations proc_sys_dir_operations;
|
2007-02-14 08:34:12 +00:00
|
|
|
|
2015-05-10 03:09:14 +00:00
|
|
|
/* Support for permanently empty directories */
|
|
|
|
|
|
|
|
struct ctl_table sysctl_mount_point[] = {
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool is_empty_dir(struct ctl_table_header *head)
|
|
|
|
{
|
|
|
|
return head->ctl_table[0].child == sysctl_mount_point;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_empty_dir(struct ctl_dir *dir)
|
|
|
|
{
|
|
|
|
dir->header.ctl_table[0].child = sysctl_mount_point;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clear_empty_dir(struct ctl_dir *dir)
|
|
|
|
|
|
|
|
{
|
|
|
|
dir->header.ctl_table[0].child = NULL;
|
|
|
|
}
|
|
|
|
|
2011-11-02 20:39:22 +00:00
|
|
|
void proc_sys_poll_notify(struct ctl_table_poll *poll)
|
|
|
|
{
|
|
|
|
if (!poll)
|
|
|
|
return;
|
|
|
|
|
|
|
|
atomic_inc(&poll->event);
|
|
|
|
wake_up_interruptible(&poll->wait);
|
|
|
|
}
|
|
|
|
|
2012-01-22 01:51:48 +00:00
|
|
|
static struct ctl_table root_table[] = {
|
|
|
|
{
|
|
|
|
.procname = "",
|
2011-12-29 16:24:29 +00:00
|
|
|
.mode = S_IFDIR|S_IRUGO|S_IXUGO,
|
2012-01-22 01:51:48 +00:00
|
|
|
},
|
|
|
|
{ }
|
|
|
|
};
|
2012-01-08 07:24:30 +00:00
|
|
|
static struct ctl_table_root sysctl_table_root = {
|
|
|
|
.default_set.dir.header = {
|
2011-12-29 16:24:29 +00:00
|
|
|
{{.count = 1,
|
|
|
|
.nreg = 1,
|
2012-01-10 01:24:30 +00:00
|
|
|
.ctl_table = root_table }},
|
2012-01-08 07:24:30 +00:00
|
|
|
.ctl_table_arg = root_table,
|
2011-12-29 16:24:29 +00:00
|
|
|
.root = &sysctl_table_root,
|
|
|
|
.set = &sysctl_table_root.default_set,
|
|
|
|
},
|
2012-01-06 12:07:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static DEFINE_SPINLOCK(sysctl_lock);
|
|
|
|
|
2011-12-29 16:24:29 +00:00
|
|
|
static void drop_sysctl_table(struct ctl_table_header *header);
|
2012-01-08 07:24:30 +00:00
|
|
|
static int sysctl_follow_link(struct ctl_table_header **phead,
|
|
|
|
struct ctl_table **pentry, struct nsproxy *namespaces);
|
|
|
|
static int insert_links(struct ctl_table_header *head);
|
|
|
|
static void put_links(struct ctl_table_header *header);
|
2011-12-29 16:24:29 +00:00
|
|
|
|
2012-01-22 04:09:45 +00:00
|
|
|
static void sysctl_print_dir(struct ctl_dir *dir)
|
|
|
|
{
|
|
|
|
if (dir->header.parent)
|
|
|
|
sysctl_print_dir(dir->header.parent);
|
2013-02-28 01:03:16 +00:00
|
|
|
pr_cont("%s/", dir->header.ctl_table[0].procname);
|
2012-01-22 04:09:45 +00:00
|
|
|
}
|
|
|
|
|
2012-01-10 05:42:02 +00:00
|
|
|
static int namecmp(const char *name1, int len1, const char *name2, int len2)
|
|
|
|
{
|
|
|
|
int minlen;
|
|
|
|
int cmp;
|
|
|
|
|
|
|
|
minlen = len1;
|
|
|
|
if (minlen > len2)
|
|
|
|
minlen = len2;
|
|
|
|
|
|
|
|
cmp = memcmp(name1, name2, minlen);
|
|
|
|
if (cmp == 0)
|
|
|
|
cmp = len1 - len2;
|
|
|
|
return cmp;
|
|
|
|
}
|
|
|
|
|
2012-01-31 05:23:52 +00:00
|
|
|
/* Called under sysctl_lock */
|
2012-01-10 05:42:02 +00:00
|
|
|
static struct ctl_table *find_entry(struct ctl_table_header **phead,
|
2012-01-08 07:24:30 +00:00
|
|
|
struct ctl_dir *dir, const char *name, int namelen)
|
2012-01-10 05:42:02 +00:00
|
|
|
{
|
|
|
|
struct ctl_table_header *head;
|
|
|
|
struct ctl_table *entry;
|
2012-01-10 01:24:30 +00:00
|
|
|
struct rb_node *node = dir->root.rb_node;
|
2012-01-10 05:42:02 +00:00
|
|
|
|
2012-01-10 01:24:30 +00:00
|
|
|
while (node)
|
|
|
|
{
|
|
|
|
struct ctl_node *ctl_node;
|
|
|
|
const char *procname;
|
|
|
|
int cmp;
|
|
|
|
|
|
|
|
ctl_node = rb_entry(node, struct ctl_node, node);
|
|
|
|
head = ctl_node->header;
|
|
|
|
entry = &head->ctl_table[ctl_node - head->node];
|
|
|
|
procname = entry->procname;
|
|
|
|
|
|
|
|
cmp = namecmp(name, namelen, procname, strlen(procname));
|
|
|
|
if (cmp < 0)
|
|
|
|
node = node->rb_left;
|
|
|
|
else if (cmp > 0)
|
|
|
|
node = node->rb_right;
|
|
|
|
else {
|
|
|
|
*phead = head;
|
|
|
|
return entry;
|
2012-01-10 05:42:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-01-10 01:24:30 +00:00
|
|
|
static int insert_entry(struct ctl_table_header *head, struct ctl_table *entry)
|
|
|
|
{
|
|
|
|
struct rb_node *node = &head->node[entry - head->ctl_table].node;
|
|
|
|
struct rb_node **p = &head->parent->root.rb_node;
|
|
|
|
struct rb_node *parent = NULL;
|
|
|
|
const char *name = entry->procname;
|
|
|
|
int namelen = strlen(name);
|
|
|
|
|
|
|
|
while (*p) {
|
|
|
|
struct ctl_table_header *parent_head;
|
|
|
|
struct ctl_table *parent_entry;
|
|
|
|
struct ctl_node *parent_node;
|
|
|
|
const char *parent_name;
|
|
|
|
int cmp;
|
|
|
|
|
|
|
|
parent = *p;
|
|
|
|
parent_node = rb_entry(parent, struct ctl_node, node);
|
|
|
|
parent_head = parent_node->header;
|
|
|
|
parent_entry = &parent_head->ctl_table[parent_node - parent_head->node];
|
|
|
|
parent_name = parent_entry->procname;
|
|
|
|
|
|
|
|
cmp = namecmp(name, namelen, parent_name, strlen(parent_name));
|
|
|
|
if (cmp < 0)
|
|
|
|
p = &(*p)->rb_left;
|
|
|
|
else if (cmp > 0)
|
|
|
|
p = &(*p)->rb_right;
|
|
|
|
else {
|
2013-02-28 01:03:16 +00:00
|
|
|
pr_err("sysctl duplicate entry: ");
|
2012-01-10 01:24:30 +00:00
|
|
|
sysctl_print_dir(head->parent);
|
2013-02-28 01:03:16 +00:00
|
|
|
pr_cont("/%s\n", entry->procname);
|
2012-01-10 01:24:30 +00:00
|
|
|
return -EEXIST;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_link_node(node, parent, p);
|
2012-10-08 23:30:35 +00:00
|
|
|
rb_insert_color(node, &head->parent->root);
|
2012-01-10 01:24:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void erase_entry(struct ctl_table_header *head, struct ctl_table *entry)
|
|
|
|
{
|
|
|
|
struct rb_node *node = &head->node[entry - head->ctl_table].node;
|
|
|
|
|
|
|
|
rb_erase(node, &head->parent->root);
|
|
|
|
}
|
|
|
|
|
2012-01-10 06:36:41 +00:00
|
|
|
static void init_header(struct ctl_table_header *head,
|
|
|
|
struct ctl_table_root *root, struct ctl_table_set *set,
|
2012-01-10 01:24:30 +00:00
|
|
|
struct ctl_node *node, struct ctl_table *table)
|
2012-01-10 06:36:41 +00:00
|
|
|
{
|
2011-12-29 16:24:29 +00:00
|
|
|
head->ctl_table = table;
|
2012-01-10 06:36:41 +00:00
|
|
|
head->ctl_table_arg = table;
|
|
|
|
head->used = 0;
|
|
|
|
head->count = 1;
|
|
|
|
head->nreg = 1;
|
|
|
|
head->unregistering = NULL;
|
|
|
|
head->root = root;
|
|
|
|
head->set = set;
|
|
|
|
head->parent = NULL;
|
2012-01-10 01:24:30 +00:00
|
|
|
head->node = node;
|
|
|
|
if (node) {
|
|
|
|
struct ctl_table *entry;
|
2012-10-08 23:30:32 +00:00
|
|
|
for (entry = table; entry->procname; entry++, node++)
|
2012-01-10 01:24:30 +00:00
|
|
|
node->header = head;
|
|
|
|
}
|
2012-01-10 06:36:41 +00:00
|
|
|
}
|
|
|
|
|
2012-01-10 01:35:01 +00:00
|
|
|
static void erase_header(struct ctl_table_header *head)
|
|
|
|
{
|
2012-01-10 01:24:30 +00:00
|
|
|
struct ctl_table *entry;
|
|
|
|
for (entry = head->ctl_table; entry->procname; entry++)
|
|
|
|
erase_entry(head, entry);
|
2012-01-10 01:35:01 +00:00
|
|
|
}
|
|
|
|
|
2012-01-08 07:24:30 +00:00
|
|
|
static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
|
2012-01-10 01:35:01 +00:00
|
|
|
{
|
2012-01-10 01:24:30 +00:00
|
|
|
struct ctl_table *entry;
|
2012-01-08 07:24:30 +00:00
|
|
|
int err;
|
|
|
|
|
2015-05-10 03:09:14 +00:00
|
|
|
/* Is this a permanently empty directory? */
|
|
|
|
if (is_empty_dir(&dir->header))
|
|
|
|
return -EROFS;
|
|
|
|
|
|
|
|
/* Am I creating a permanently empty directory? */
|
|
|
|
if (header->ctl_table == sysctl_mount_point) {
|
|
|
|
if (!RB_EMPTY_ROOT(&dir->root))
|
|
|
|
return -EINVAL;
|
|
|
|
set_empty_dir(dir);
|
|
|
|
}
|
|
|
|
|
2012-01-08 07:24:30 +00:00
|
|
|
dir->header.nreg++;
|
2011-12-29 16:24:29 +00:00
|
|
|
header->parent = dir;
|
2012-01-08 07:24:30 +00:00
|
|
|
err = insert_links(header);
|
|
|
|
if (err)
|
|
|
|
goto fail_links;
|
2012-01-10 01:24:30 +00:00
|
|
|
for (entry = header->ctl_table; entry->procname; entry++) {
|
|
|
|
err = insert_entry(header, entry);
|
|
|
|
if (err)
|
|
|
|
goto fail;
|
|
|
|
}
|
2012-01-08 07:24:30 +00:00
|
|
|
return 0;
|
2012-01-10 01:24:30 +00:00
|
|
|
fail:
|
|
|
|
erase_header(header);
|
|
|
|
put_links(header);
|
2012-01-08 07:24:30 +00:00
|
|
|
fail_links:
|
2015-05-10 03:09:14 +00:00
|
|
|
if (header->ctl_table == sysctl_mount_point)
|
|
|
|
clear_empty_dir(dir);
|
2012-01-08 07:24:30 +00:00
|
|
|
header->parent = NULL;
|
|
|
|
drop_sysctl_table(&dir->header);
|
|
|
|
return err;
|
2012-01-10 01:35:01 +00:00
|
|
|
}
|
|
|
|
|
2012-01-06 12:07:15 +00:00
|
|
|
/* called under sysctl_lock */
|
|
|
|
static int use_table(struct ctl_table_header *p)
|
|
|
|
{
|
|
|
|
if (unlikely(p->unregistering))
|
|
|
|
return 0;
|
|
|
|
p->used++;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* called under sysctl_lock */
|
|
|
|
static void unuse_table(struct ctl_table_header *p)
|
|
|
|
{
|
|
|
|
if (!--p->used)
|
|
|
|
if (unlikely(p->unregistering))
|
|
|
|
complete(p->unregistering);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* called under sysctl_lock, will reacquire if has to wait */
|
|
|
|
static void start_unregistering(struct ctl_table_header *p)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* if p->used is 0, nobody will ever touch that entry again;
|
|
|
|
* we'll eliminate all paths to it before dropping sysctl_lock
|
|
|
|
*/
|
|
|
|
if (unlikely(p->used)) {
|
|
|
|
struct completion wait;
|
|
|
|
init_completion(&wait);
|
|
|
|
p->unregistering = &wait;
|
|
|
|
spin_unlock(&sysctl_lock);
|
|
|
|
wait_for_completion(&wait);
|
|
|
|
spin_lock(&sysctl_lock);
|
|
|
|
} else {
|
|
|
|
/* anything non-NULL; we'll never dereference it */
|
|
|
|
p->unregistering = ERR_PTR(-EINVAL);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* do not remove from the list until nobody holds it; walking the
|
|
|
|
* list in do_sysctl() relies on that.
|
|
|
|
*/
|
2012-01-10 01:35:01 +00:00
|
|
|
erase_header(p);
|
2012-01-06 12:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void sysctl_head_get(struct ctl_table_header *head)
|
|
|
|
{
|
|
|
|
spin_lock(&sysctl_lock);
|
|
|
|
head->count++;
|
|
|
|
spin_unlock(&sysctl_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sysctl_head_put(struct ctl_table_header *head)
|
|
|
|
{
|
|
|
|
spin_lock(&sysctl_lock);
|
|
|
|
if (!--head->count)
|
|
|
|
kfree_rcu(head, rcu);
|
|
|
|
spin_unlock(&sysctl_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
|
|
|
|
{
|
2012-10-05 00:15:45 +00:00
|
|
|
BUG_ON(!head);
|
2012-01-06 12:07:15 +00:00
|
|
|
spin_lock(&sysctl_lock);
|
|
|
|
if (!use_table(head))
|
|
|
|
head = ERR_PTR(-ENOENT);
|
|
|
|
spin_unlock(&sysctl_lock);
|
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sysctl_head_finish(struct ctl_table_header *head)
|
|
|
|
{
|
|
|
|
if (!head)
|
|
|
|
return;
|
|
|
|
spin_lock(&sysctl_lock);
|
|
|
|
unuse_table(head);
|
|
|
|
spin_unlock(&sysctl_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct ctl_table_set *
|
|
|
|
lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
|
|
|
|
{
|
|
|
|
struct ctl_table_set *set = &root->default_set;
|
|
|
|
if (root->lookup)
|
|
|
|
set = root->lookup(root, namespaces);
|
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
2012-01-10 05:42:02 +00:00
|
|
|
static struct ctl_table *lookup_entry(struct ctl_table_header **phead,
|
2011-12-29 16:24:29 +00:00
|
|
|
struct ctl_dir *dir,
|
2012-01-10 05:42:02 +00:00
|
|
|
const char *name, int namelen)
|
|
|
|
{
|
|
|
|
struct ctl_table_header *head;
|
|
|
|
struct ctl_table *entry;
|
|
|
|
|
|
|
|
spin_lock(&sysctl_lock);
|
2012-01-08 07:24:30 +00:00
|
|
|
entry = find_entry(&head, dir, name, namelen);
|
|
|
|
if (entry && use_table(head))
|
|
|
|
*phead = head;
|
|
|
|
else
|
|
|
|
entry = NULL;
|
2012-01-10 05:42:02 +00:00
|
|
|
spin_unlock(&sysctl_lock);
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2012-01-10 01:24:30 +00:00
|
|
|
static struct ctl_node *first_usable_entry(struct rb_node *node)
|
2012-01-06 12:07:15 +00:00
|
|
|
{
|
2012-01-10 01:24:30 +00:00
|
|
|
struct ctl_node *ctl_node;
|
2012-01-06 12:07:15 +00:00
|
|
|
|
2012-01-10 01:24:30 +00:00
|
|
|
for (;node; node = rb_next(node)) {
|
|
|
|
ctl_node = rb_entry(node, struct ctl_node, node);
|
|
|
|
if (use_table(ctl_node->header))
|
|
|
|
return ctl_node;
|
2012-01-06 12:07:15 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-12-29 16:24:29 +00:00
|
|
|
static void first_entry(struct ctl_dir *dir,
|
2012-01-18 11:15:51 +00:00
|
|
|
struct ctl_table_header **phead, struct ctl_table **pentry)
|
|
|
|
{
|
2012-01-10 01:24:30 +00:00
|
|
|
struct ctl_table_header *head = NULL;
|
2011-12-29 16:24:29 +00:00
|
|
|
struct ctl_table *entry = NULL;
|
2012-01-10 01:24:30 +00:00
|
|
|
struct ctl_node *ctl_node;
|
2012-01-18 11:15:51 +00:00
|
|
|
|
|
|
|
spin_lock(&sysctl_lock);
|
2012-01-10 01:24:30 +00:00
|
|
|
ctl_node = first_usable_entry(rb_first(&dir->root));
|
2012-01-18 11:15:51 +00:00
|
|
|
spin_unlock(&sysctl_lock);
|
2012-01-10 01:24:30 +00:00
|
|
|
if (ctl_node) {
|
|
|
|
head = ctl_node->header;
|
|
|
|
entry = &head->ctl_table[ctl_node - head->node];
|
|
|
|
}
|
2012-01-18 11:15:51 +00:00
|
|
|
*phead = head;
|
|
|
|
*pentry = entry;
|
|
|
|
}
|
|
|
|
|
2011-12-29 16:24:29 +00:00
|
|
|
static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry)
|
2012-01-06 12:07:15 +00:00
|
|
|
{
|
2012-01-18 11:15:51 +00:00
|
|
|
struct ctl_table_header *head = *phead;
|
|
|
|
struct ctl_table *entry = *pentry;
|
2012-01-10 01:24:30 +00:00
|
|
|
struct ctl_node *ctl_node = &head->node[entry - head->ctl_table];
|
2012-01-18 11:15:51 +00:00
|
|
|
|
2012-01-10 01:24:30 +00:00
|
|
|
spin_lock(&sysctl_lock);
|
|
|
|
unuse_table(head);
|
|
|
|
|
|
|
|
ctl_node = first_usable_entry(rb_next(&ctl_node->node));
|
|
|
|
spin_unlock(&sysctl_lock);
|
|
|
|
head = NULL;
|
|
|
|
if (ctl_node) {
|
|
|
|
head = ctl_node->header;
|
|
|
|
entry = &head->ctl_table[ctl_node - head->node];
|
2012-01-18 11:15:51 +00:00
|
|
|
}
|
|
|
|
*phead = head;
|
|
|
|
*pentry = entry;
|
2012-01-06 12:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void register_sysctl_root(struct ctl_table_root *root)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sysctl_perm does NOT grant the superuser all rights automatically, because
|
|
|
|
* some sysctl variables are readonly even to root.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int test_perm(int mode, int op)
|
|
|
|
{
|
2012-02-14 02:02:50 +00:00
|
|
|
if (uid_eq(current_euid(), GLOBAL_ROOT_UID))
|
2012-01-06 12:07:15 +00:00
|
|
|
mode >>= 6;
|
2012-02-14 02:02:50 +00:00
|
|
|
else if (in_egroup_p(GLOBAL_ROOT_GID))
|
2012-01-06 12:07:15 +00:00
|
|
|
mode >>= 3;
|
|
|
|
if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
|
|
|
|
return 0;
|
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
|
2012-11-16 03:02:58 +00:00
|
|
|
static int sysctl_perm(struct ctl_table_header *head, struct ctl_table *table, int op)
|
2012-01-06 12:07:15 +00:00
|
|
|
{
|
2012-11-16 03:02:58 +00:00
|
|
|
struct ctl_table_root *root = head->root;
|
2012-01-06 12:07:15 +00:00
|
|
|
int mode;
|
|
|
|
|
|
|
|
if (root->permissions)
|
2012-11-16 03:02:58 +00:00
|
|
|
mode = root->permissions(head, table);
|
2012-01-06 12:07:15 +00:00
|
|
|
else
|
|
|
|
mode = table->mode;
|
|
|
|
|
|
|
|
return test_perm(mode, op);
|
|
|
|
}
|
|
|
|
|
2008-07-15 12:54:06 +00:00
|
|
|
static struct inode *proc_sys_make_inode(struct super_block *sb,
|
|
|
|
struct ctl_table_header *head, struct ctl_table *table)
|
2007-02-14 08:34:12 +00:00
|
|
|
{
|
|
|
|
struct inode *inode;
|
2008-07-15 12:54:06 +00:00
|
|
|
struct proc_inode *ei;
|
2007-02-14 08:34:12 +00:00
|
|
|
|
2008-07-15 12:54:06 +00:00
|
|
|
inode = new_inode(sb);
|
2007-02-14 08:34:12 +00:00
|
|
|
if (!inode)
|
|
|
|
goto out;
|
|
|
|
|
2010-10-23 15:19:54 +00:00
|
|
|
inode->i_ino = get_next_ino();
|
|
|
|
|
2008-07-15 12:54:06 +00:00
|
|
|
sysctl_head_get(head);
|
2007-02-14 08:34:12 +00:00
|
|
|
ei = PROC_I(inode);
|
2008-07-15 12:54:06 +00:00
|
|
|
ei->sysctl = head;
|
|
|
|
ei->sysctl_entry = table;
|
|
|
|
|
2007-02-14 08:34:12 +00:00
|
|
|
inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
|
2008-07-15 12:54:06 +00:00
|
|
|
inode->i_mode = table->mode;
|
2011-12-29 16:24:29 +00:00
|
|
|
if (!S_ISDIR(table->mode)) {
|
2008-07-15 12:54:06 +00:00
|
|
|
inode->i_mode |= S_IFREG;
|
|
|
|
inode->i_op = &proc_sys_inode_operations;
|
|
|
|
inode->i_fop = &proc_sys_file_operations;
|
|
|
|
} else {
|
|
|
|
inode->i_mode |= S_IFDIR;
|
|
|
|
inode->i_op = &proc_sys_dir_operations;
|
|
|
|
inode->i_fop = &proc_sys_dir_file_operations;
|
2015-05-10 03:09:14 +00:00
|
|
|
if (is_empty_dir(head))
|
|
|
|
make_empty_dir_inode(inode);
|
2008-07-15 12:54:06 +00:00
|
|
|
}
|
2007-02-14 08:34:12 +00:00
|
|
|
out:
|
|
|
|
return inode;
|
|
|
|
}
|
|
|
|
|
2008-10-02 20:33:54 +00:00
|
|
|
static struct ctl_table_header *grab_header(struct inode *inode)
|
2007-02-14 08:34:12 +00:00
|
|
|
{
|
2012-01-07 14:57:47 +00:00
|
|
|
struct ctl_table_header *head = PROC_I(inode)->sysctl;
|
|
|
|
if (!head)
|
2012-01-08 07:24:30 +00:00
|
|
|
head = &sysctl_table_root.default_set.dir.header;
|
2012-01-07 14:57:47 +00:00
|
|
|
return sysctl_head_grab(head);
|
2008-07-15 12:54:06 +00:00
|
|
|
}
|
2007-02-14 08:34:12 +00:00
|
|
|
|
2008-07-15 12:54:06 +00:00
|
|
|
static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
|
2012-06-10 21:13:09 +00:00
|
|
|
unsigned int flags)
|
2008-07-15 12:54:06 +00:00
|
|
|
{
|
|
|
|
struct ctl_table_header *head = grab_header(dir);
|
|
|
|
struct ctl_table_header *h = NULL;
|
2016-07-21 02:41:44 +00:00
|
|
|
const struct qstr *name = &dentry->d_name;
|
2008-07-15 12:54:06 +00:00
|
|
|
struct ctl_table *p;
|
|
|
|
struct inode *inode;
|
|
|
|
struct dentry *err = ERR_PTR(-ENOENT);
|
2011-12-29 16:24:29 +00:00
|
|
|
struct ctl_dir *ctl_dir;
|
2012-01-08 07:24:30 +00:00
|
|
|
int ret;
|
2007-02-14 08:34:12 +00:00
|
|
|
|
2008-07-15 12:54:06 +00:00
|
|
|
if (IS_ERR(head))
|
|
|
|
return ERR_CAST(head);
|
2007-02-14 08:34:12 +00:00
|
|
|
|
2011-12-29 16:24:29 +00:00
|
|
|
ctl_dir = container_of(head, struct ctl_dir, header);
|
2007-02-14 08:34:12 +00:00
|
|
|
|
2011-12-29 16:24:29 +00:00
|
|
|
p = lookup_entry(&h, ctl_dir, name->name, name->len);
|
2008-07-15 12:54:06 +00:00
|
|
|
if (!p)
|
2007-02-14 08:34:12 +00:00
|
|
|
goto out;
|
|
|
|
|
2012-01-31 05:24:59 +00:00
|
|
|
if (S_ISLNK(p->mode)) {
|
|
|
|
ret = sysctl_follow_link(&h, &p, current->nsproxy);
|
|
|
|
err = ERR_PTR(ret);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
}
|
2012-01-08 07:24:30 +00:00
|
|
|
|
2007-02-14 08:34:12 +00:00
|
|
|
err = ERR_PTR(-ENOMEM);
|
2008-07-15 12:54:06 +00:00
|
|
|
inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
|
2007-02-14 08:34:12 +00:00
|
|
|
if (!inode)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
err = NULL;
|
2011-01-07 06:49:55 +00:00
|
|
|
d_set_d_op(dentry, &proc_sys_dentry_operations);
|
2007-02-14 08:34:12 +00:00
|
|
|
d_add(dentry, inode);
|
|
|
|
|
|
|
|
out:
|
2012-09-13 22:03:37 +00:00
|
|
|
if (h)
|
|
|
|
sysctl_head_finish(h);
|
2007-02-14 08:34:12 +00:00
|
|
|
sysctl_head_finish(head);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
sysctl: merge equal proc_sys_read and proc_sys_write
Many (most of) sysctls do not have a per-container sense. E.g.
kernel.print_fatal_signals, vm.panic_on_oom, net.core.netdev_budget and so on
and so forth. Besides, tuning then from inside a container is not even
secure. On the other hand, hiding them completely from the container's tasks
sometimes causes user-space to stop working.
When developing net sysctl, the common practice was to duplicate a table and
drop the write bits in table->mode, but this approach was not very elegant,
lead to excessive memory consumption and was not suitable in general.
Here's the alternative solution. To facilitate the per-container sysctls
ctl_table_root-s were introduced. Each root contains a list of
ctl_table_header-s that are visible to different namespaces. The idea of this
set is to add the permissions() callback on the ctl_table_root to allow ctl
root limit permissions to the same ctl_table-s.
The main user of this functionality is the net-namespaces code, but later this
will (should) be used by more and more namespaces, containers and control
groups.
Actually, this idea's core is in a single hunk in the third patch. First two
patches are cleanups for sysctl code, while the third one mostly extends the
arguments set of some sysctl functions.
This patch:
These ->read and ->write callbacks act in a very similar way, so merge these
paths to reduce the number of places to patch later and shrink the .text size
(a bit).
Signed-off-by: Pavel Emelyanov <xemul@openvz.org>
Acked-by: "David S. Miller" <davem@davemloft.net>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Cc: Alexey Dobriyan <adobriyan@sw.ru>
Cc: Denis V. Lunev <den@openvz.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-04-29 08:02:40 +00:00
|
|
|
static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
|
|
|
|
size_t count, loff_t *ppos, int write)
|
2007-02-14 08:34:12 +00:00
|
|
|
{
|
2013-01-23 22:07:38 +00:00
|
|
|
struct inode *inode = file_inode(filp);
|
2008-07-15 12:54:06 +00:00
|
|
|
struct ctl_table_header *head = grab_header(inode);
|
|
|
|
struct ctl_table *table = PROC_I(inode)->sysctl_entry;
|
2007-10-25 14:27:40 +00:00
|
|
|
ssize_t error;
|
|
|
|
size_t res;
|
2007-02-14 08:34:12 +00:00
|
|
|
|
2008-07-15 12:54:06 +00:00
|
|
|
if (IS_ERR(head))
|
|
|
|
return PTR_ERR(head);
|
2007-02-14 08:34:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* At this point we know that the sysctl was not unregistered
|
|
|
|
* and won't be until we finish.
|
|
|
|
*/
|
|
|
|
error = -EPERM;
|
2012-11-16 03:02:58 +00:00
|
|
|
if (sysctl_perm(head, table, write ? MAY_WRITE : MAY_READ))
|
2007-02-14 08:34:12 +00:00
|
|
|
goto out;
|
|
|
|
|
2008-07-15 12:54:06 +00:00
|
|
|
/* if that can happen at all, it should be -EINVAL, not -EISDIR */
|
|
|
|
error = -EINVAL;
|
|
|
|
if (!table->proc_handler)
|
|
|
|
goto out;
|
|
|
|
|
2007-02-14 08:34:12 +00:00
|
|
|
/* careful: calling conventions are nasty here */
|
|
|
|
res = count;
|
2009-09-23 22:57:19 +00:00
|
|
|
error = table->proc_handler(table, write, buf, &res, ppos);
|
2007-02-14 08:34:12 +00:00
|
|
|
if (!error)
|
|
|
|
error = res;
|
|
|
|
out:
|
|
|
|
sysctl_head_finish(head);
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
sysctl: merge equal proc_sys_read and proc_sys_write
Many (most of) sysctls do not have a per-container sense. E.g.
kernel.print_fatal_signals, vm.panic_on_oom, net.core.netdev_budget and so on
and so forth. Besides, tuning then from inside a container is not even
secure. On the other hand, hiding them completely from the container's tasks
sometimes causes user-space to stop working.
When developing net sysctl, the common practice was to duplicate a table and
drop the write bits in table->mode, but this approach was not very elegant,
lead to excessive memory consumption and was not suitable in general.
Here's the alternative solution. To facilitate the per-container sysctls
ctl_table_root-s were introduced. Each root contains a list of
ctl_table_header-s that are visible to different namespaces. The idea of this
set is to add the permissions() callback on the ctl_table_root to allow ctl
root limit permissions to the same ctl_table-s.
The main user of this functionality is the net-namespaces code, but later this
will (should) be used by more and more namespaces, containers and control
groups.
Actually, this idea's core is in a single hunk in the third patch. First two
patches are cleanups for sysctl code, while the third one mostly extends the
arguments set of some sysctl functions.
This patch:
These ->read and ->write callbacks act in a very similar way, so merge these
paths to reduce the number of places to patch later and shrink the .text size
(a bit).
Signed-off-by: Pavel Emelyanov <xemul@openvz.org>
Acked-by: "David S. Miller" <davem@davemloft.net>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Cc: Alexey Dobriyan <adobriyan@sw.ru>
Cc: Denis V. Lunev <den@openvz.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-04-29 08:02:40 +00:00
|
|
|
static ssize_t proc_sys_read(struct file *filp, char __user *buf,
|
2007-02-14 08:34:12 +00:00
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
sysctl: merge equal proc_sys_read and proc_sys_write
Many (most of) sysctls do not have a per-container sense. E.g.
kernel.print_fatal_signals, vm.panic_on_oom, net.core.netdev_budget and so on
and so forth. Besides, tuning then from inside a container is not even
secure. On the other hand, hiding them completely from the container's tasks
sometimes causes user-space to stop working.
When developing net sysctl, the common practice was to duplicate a table and
drop the write bits in table->mode, but this approach was not very elegant,
lead to excessive memory consumption and was not suitable in general.
Here's the alternative solution. To facilitate the per-container sysctls
ctl_table_root-s were introduced. Each root contains a list of
ctl_table_header-s that are visible to different namespaces. The idea of this
set is to add the permissions() callback on the ctl_table_root to allow ctl
root limit permissions to the same ctl_table-s.
The main user of this functionality is the net-namespaces code, but later this
will (should) be used by more and more namespaces, containers and control
groups.
Actually, this idea's core is in a single hunk in the third patch. First two
patches are cleanups for sysctl code, while the third one mostly extends the
arguments set of some sysctl functions.
This patch:
These ->read and ->write callbacks act in a very similar way, so merge these
paths to reduce the number of places to patch later and shrink the .text size
(a bit).
Signed-off-by: Pavel Emelyanov <xemul@openvz.org>
Acked-by: "David S. Miller" <davem@davemloft.net>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Cc: Alexey Dobriyan <adobriyan@sw.ru>
Cc: Denis V. Lunev <den@openvz.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-04-29 08:02:40 +00:00
|
|
|
return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
|
|
|
|
}
|
2007-02-14 08:34:12 +00:00
|
|
|
|
sysctl: merge equal proc_sys_read and proc_sys_write
Many (most of) sysctls do not have a per-container sense. E.g.
kernel.print_fatal_signals, vm.panic_on_oom, net.core.netdev_budget and so on
and so forth. Besides, tuning then from inside a container is not even
secure. On the other hand, hiding them completely from the container's tasks
sometimes causes user-space to stop working.
When developing net sysctl, the common practice was to duplicate a table and
drop the write bits in table->mode, but this approach was not very elegant,
lead to excessive memory consumption and was not suitable in general.
Here's the alternative solution. To facilitate the per-container sysctls
ctl_table_root-s were introduced. Each root contains a list of
ctl_table_header-s that are visible to different namespaces. The idea of this
set is to add the permissions() callback on the ctl_table_root to allow ctl
root limit permissions to the same ctl_table-s.
The main user of this functionality is the net-namespaces code, but later this
will (should) be used by more and more namespaces, containers and control
groups.
Actually, this idea's core is in a single hunk in the third patch. First two
patches are cleanups for sysctl code, while the third one mostly extends the
arguments set of some sysctl functions.
This patch:
These ->read and ->write callbacks act in a very similar way, so merge these
paths to reduce the number of places to patch later and shrink the .text size
(a bit).
Signed-off-by: Pavel Emelyanov <xemul@openvz.org>
Acked-by: "David S. Miller" <davem@davemloft.net>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Cc: Alexey Dobriyan <adobriyan@sw.ru>
Cc: Denis V. Lunev <den@openvz.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2008-04-29 08:02:40 +00:00
|
|
|
static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
|
|
|
|
size_t count, loff_t *ppos)
|
|
|
|
{
|
|
|
|
return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
|
2007-02-14 08:34:12 +00:00
|
|
|
}
|
|
|
|
|
2011-11-02 20:39:22 +00:00
|
|
|
static int proc_sys_open(struct inode *inode, struct file *filp)
|
|
|
|
{
|
2012-03-22 21:42:22 +00:00
|
|
|
struct ctl_table_header *head = grab_header(inode);
|
2011-11-02 20:39:22 +00:00
|
|
|
struct ctl_table *table = PROC_I(inode)->sysctl_entry;
|
|
|
|
|
2012-03-22 21:42:22 +00:00
|
|
|
/* sysctl was unregistered */
|
|
|
|
if (IS_ERR(head))
|
|
|
|
return PTR_ERR(head);
|
|
|
|
|
2011-11-02 20:39:22 +00:00
|
|
|
if (table->poll)
|
|
|
|
filp->private_data = proc_sys_poll_event(table->poll);
|
|
|
|
|
2012-03-22 21:42:22 +00:00
|
|
|
sysctl_head_finish(head);
|
|
|
|
|
2011-11-02 20:39:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
|
|
|
|
{
|
2013-01-23 22:07:38 +00:00
|
|
|
struct inode *inode = file_inode(filp);
|
2012-03-22 21:42:22 +00:00
|
|
|
struct ctl_table_header *head = grab_header(inode);
|
2011-11-02 20:39:22 +00:00
|
|
|
struct ctl_table *table = PROC_I(inode)->sysctl_entry;
|
|
|
|
unsigned int ret = DEFAULT_POLLMASK;
|
2012-03-22 21:42:22 +00:00
|
|
|
unsigned long event;
|
|
|
|
|
|
|
|
/* sysctl was unregistered */
|
|
|
|
if (IS_ERR(head))
|
|
|
|
return POLLERR | POLLHUP;
|
2011-11-02 20:39:22 +00:00
|
|
|
|
|
|
|
if (!table->proc_handler)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (!table->poll)
|
|
|
|
goto out;
|
|
|
|
|
2012-03-22 21:42:22 +00:00
|
|
|
event = (unsigned long)filp->private_data;
|
2011-11-02 20:39:22 +00:00
|
|
|
poll_wait(filp, &table->poll->wait, wait);
|
|
|
|
|
|
|
|
if (event != atomic_read(&table->poll->event)) {
|
|
|
|
filp->private_data = proc_sys_poll_event(table->poll);
|
|
|
|
ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2012-03-22 21:42:22 +00:00
|
|
|
sysctl_head_finish(head);
|
|
|
|
|
2011-11-02 20:39:22 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2007-02-14 08:34:12 +00:00
|
|
|
|
2013-05-16 16:07:31 +00:00
|
|
|
static bool proc_sys_fill_cache(struct file *file,
|
|
|
|
struct dir_context *ctx,
|
2008-07-15 12:54:06 +00:00
|
|
|
struct ctl_table_header *head,
|
|
|
|
struct ctl_table *table)
|
2007-02-14 08:34:12 +00:00
|
|
|
{
|
2013-05-16 16:07:31 +00:00
|
|
|
struct dentry *child, *dir = file->f_path.dentry;
|
2007-02-14 08:34:12 +00:00
|
|
|
struct inode *inode;
|
|
|
|
struct qstr qname;
|
|
|
|
ino_t ino = 0;
|
|
|
|
unsigned type = DT_UNKNOWN;
|
|
|
|
|
|
|
|
qname.name = table->procname;
|
|
|
|
qname.len = strlen(table->procname);
|
2016-06-10 14:51:30 +00:00
|
|
|
qname.hash = full_name_hash(dir, qname.name, qname.len);
|
2007-02-14 08:34:12 +00:00
|
|
|
|
|
|
|
child = d_lookup(dir, &qname);
|
|
|
|
if (!child) {
|
2016-04-20 20:36:09 +00:00
|
|
|
DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
|
|
|
|
child = d_alloc_parallel(dir, &qname, &wq);
|
|
|
|
if (IS_ERR(child))
|
|
|
|
return false;
|
|
|
|
if (d_in_lookup(child)) {
|
2008-07-15 12:54:06 +00:00
|
|
|
inode = proc_sys_make_inode(dir->d_sb, head, table);
|
|
|
|
if (!inode) {
|
2016-04-20 20:36:09 +00:00
|
|
|
d_lookup_done(child);
|
2008-07-15 12:54:06 +00:00
|
|
|
dput(child);
|
2013-05-16 16:07:31 +00:00
|
|
|
return false;
|
2007-02-14 08:34:12 +00:00
|
|
|
}
|
2016-04-20 20:36:09 +00:00
|
|
|
d_set_d_op(child, &proc_sys_dentry_operations);
|
|
|
|
d_add(child, inode);
|
2007-02-14 08:34:12 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-17 22:25:59 +00:00
|
|
|
inode = d_inode(child);
|
2008-07-15 12:54:06 +00:00
|
|
|
ino = inode->i_ino;
|
|
|
|
type = inode->i_mode >> 12;
|
2007-02-14 08:34:12 +00:00
|
|
|
dput(child);
|
2013-05-16 16:07:31 +00:00
|
|
|
return dir_emit(ctx, qname.name, qname.len, ino, type);
|
2008-07-15 12:54:06 +00:00
|
|
|
}
|
|
|
|
|
2013-05-16 16:07:31 +00:00
|
|
|
static bool proc_sys_link_fill_cache(struct file *file,
|
|
|
|
struct dir_context *ctx,
|
2012-01-08 07:24:30 +00:00
|
|
|
struct ctl_table_header *head,
|
|
|
|
struct ctl_table *table)
|
|
|
|
{
|
2013-05-16 16:07:31 +00:00
|
|
|
bool ret = true;
|
2012-01-08 07:24:30 +00:00
|
|
|
head = sysctl_head_grab(head);
|
|
|
|
|
2012-01-31 05:24:59 +00:00
|
|
|
if (S_ISLNK(table->mode)) {
|
|
|
|
/* It is not an error if we can not follow the link ignore it */
|
2013-05-16 16:07:31 +00:00
|
|
|
int err = sysctl_follow_link(&head, &table, current->nsproxy);
|
2012-01-31 05:24:59 +00:00
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
}
|
2012-01-08 07:24:30 +00:00
|
|
|
|
2013-05-16 16:07:31 +00:00
|
|
|
ret = proc_sys_fill_cache(file, ctx, head, table);
|
2012-01-08 07:24:30 +00:00
|
|
|
out:
|
|
|
|
sysctl_head_finish(head);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-08-08 21:22:16 +00:00
|
|
|
static int scan(struct ctl_table_header *head, struct ctl_table *table,
|
2008-07-15 12:54:06 +00:00
|
|
|
unsigned long *pos, struct file *file,
|
2013-05-16 16:07:31 +00:00
|
|
|
struct dir_context *ctx)
|
2008-07-15 12:54:06 +00:00
|
|
|
{
|
2013-05-16 16:07:31 +00:00
|
|
|
bool res;
|
2008-07-15 12:54:06 +00:00
|
|
|
|
2013-05-16 16:07:31 +00:00
|
|
|
if ((*pos)++ < ctx->pos)
|
|
|
|
return true;
|
2008-07-15 12:54:06 +00:00
|
|
|
|
2012-01-08 07:24:30 +00:00
|
|
|
if (unlikely(S_ISLNK(table->mode)))
|
2013-05-16 16:07:31 +00:00
|
|
|
res = proc_sys_link_fill_cache(file, ctx, head, table);
|
2012-01-08 07:24:30 +00:00
|
|
|
else
|
2013-05-16 16:07:31 +00:00
|
|
|
res = proc_sys_fill_cache(file, ctx, head, table);
|
2008-07-15 12:54:06 +00:00
|
|
|
|
2013-05-16 16:07:31 +00:00
|
|
|
if (res)
|
|
|
|
ctx->pos = *pos;
|
2008-07-15 12:54:06 +00:00
|
|
|
|
2012-01-18 11:15:51 +00:00
|
|
|
return res;
|
2007-02-14 08:34:12 +00:00
|
|
|
}
|
|
|
|
|
2013-05-16 16:07:31 +00:00
|
|
|
static int proc_sys_readdir(struct file *file, struct dir_context *ctx)
|
2007-02-14 08:34:12 +00:00
|
|
|
{
|
2013-05-16 16:07:31 +00:00
|
|
|
struct ctl_table_header *head = grab_header(file_inode(file));
|
2008-07-15 12:54:06 +00:00
|
|
|
struct ctl_table_header *h = NULL;
|
2012-01-18 11:15:51 +00:00
|
|
|
struct ctl_table *entry;
|
2011-12-29 16:24:29 +00:00
|
|
|
struct ctl_dir *ctl_dir;
|
2007-02-14 08:34:12 +00:00
|
|
|
unsigned long pos;
|
2008-07-15 12:54:06 +00:00
|
|
|
|
|
|
|
if (IS_ERR(head))
|
|
|
|
return PTR_ERR(head);
|
2007-02-14 08:34:12 +00:00
|
|
|
|
2011-12-29 16:24:29 +00:00
|
|
|
ctl_dir = container_of(head, struct ctl_dir, header);
|
2007-02-14 08:34:12 +00:00
|
|
|
|
2013-05-16 16:07:31 +00:00
|
|
|
if (!dir_emit_dots(file, ctx))
|
|
|
|
return 0;
|
|
|
|
|
2007-02-14 08:34:12 +00:00
|
|
|
pos = 2;
|
|
|
|
|
2011-12-29 16:24:29 +00:00
|
|
|
for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) {
|
2013-05-16 16:07:31 +00:00
|
|
|
if (!scan(h, entry, &pos, file, ctx)) {
|
2008-07-15 12:54:06 +00:00
|
|
|
sysctl_head_finish(h);
|
|
|
|
break;
|
2007-02-14 08:34:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sysctl_head_finish(head);
|
2013-05-16 16:07:31 +00:00
|
|
|
return 0;
|
2007-02-14 08:34:12 +00:00
|
|
|
}
|
|
|
|
|
2011-06-20 23:28:19 +00:00
|
|
|
static int proc_sys_permission(struct inode *inode, int mask)
|
2007-02-14 08:34:12 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* sysctl entries that are not writeable,
|
|
|
|
* are _NOT_ writeable, capabilities or not.
|
|
|
|
*/
|
2008-07-31 11:41:58 +00:00
|
|
|
struct ctl_table_header *head;
|
|
|
|
struct ctl_table *table;
|
2007-02-14 08:34:12 +00:00
|
|
|
int error;
|
|
|
|
|
2008-07-31 11:41:58 +00:00
|
|
|
/* Executable files are not allowed under /proc/sys/ */
|
|
|
|
if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
|
|
|
|
return -EACCES;
|
|
|
|
|
|
|
|
head = grab_header(inode);
|
2008-07-15 12:54:06 +00:00
|
|
|
if (IS_ERR(head))
|
|
|
|
return PTR_ERR(head);
|
2007-02-14 08:34:12 +00:00
|
|
|
|
2008-07-31 11:41:58 +00:00
|
|
|
table = PROC_I(inode)->sysctl_entry;
|
2008-07-15 12:54:06 +00:00
|
|
|
if (!table) /* global root - r-xr-xr-x */
|
|
|
|
error = mask & MAY_WRITE ? -EACCES : 0;
|
|
|
|
else /* Use the permissions on the sysctl table entry */
|
2012-11-16 03:02:58 +00:00
|
|
|
error = sysctl_perm(head, table, mask & ~MAY_NOT_BLOCK);
|
2007-02-14 08:34:12 +00:00
|
|
|
|
|
|
|
sysctl_head_finish(head);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
|
|
|
|
{
|
2015-03-17 22:25:59 +00:00
|
|
|
struct inode *inode = d_inode(dentry);
|
2007-02-14 08:34:12 +00:00
|
|
|
int error;
|
|
|
|
|
|
|
|
if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
|
|
|
|
return -EPERM;
|
|
|
|
|
|
|
|
error = inode_change_ok(inode, attr);
|
2010-06-04 09:30:02 +00:00
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
setattr_copy(inode, attr);
|
|
|
|
mark_inode_dirty(inode);
|
|
|
|
return 0;
|
2007-02-14 08:34:12 +00:00
|
|
|
}
|
|
|
|
|
2008-07-15 12:54:06 +00:00
|
|
|
static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
|
|
|
|
{
|
2015-03-17 22:25:59 +00:00
|
|
|
struct inode *inode = d_inode(dentry);
|
2008-07-15 12:54:06 +00:00
|
|
|
struct ctl_table_header *head = grab_header(inode);
|
|
|
|
struct ctl_table *table = PROC_I(inode)->sysctl_entry;
|
|
|
|
|
|
|
|
if (IS_ERR(head))
|
|
|
|
return PTR_ERR(head);
|
|
|
|
|
|
|
|
generic_fillattr(inode, stat);
|
|
|
|
if (table)
|
|
|
|
stat->mode = (stat->mode & S_IFMT) | table->mode;
|
|
|
|
|
|
|
|
sysctl_head_finish(head);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-02-14 08:34:12 +00:00
|
|
|
static const struct file_operations proc_sys_file_operations = {
|
2011-11-02 20:39:22 +00:00
|
|
|
.open = proc_sys_open,
|
|
|
|
.poll = proc_sys_poll,
|
2007-02-14 08:34:12 +00:00
|
|
|
.read = proc_sys_read,
|
|
|
|
.write = proc_sys_write,
|
llseek: automatically add .llseek fop
All file_operations should get a .llseek operation so we can make
nonseekable_open the default for future file operations without a
.llseek pointer.
The three cases that we can automatically detect are no_llseek, seq_lseek
and default_llseek. For cases where we can we can automatically prove that
the file offset is always ignored, we use noop_llseek, which maintains
the current behavior of not returning an error from a seek.
New drivers should normally not use noop_llseek but instead use no_llseek
and call nonseekable_open at open time. Existing drivers can be converted
to do the same when the maintainer knows for certain that no user code
relies on calling seek on the device file.
The generated code is often incorrectly indented and right now contains
comments that clarify for each added line why a specific variant was
chosen. In the version that gets submitted upstream, the comments will
be gone and I will manually fix the indentation, because there does not
seem to be a way to do that using coccinelle.
Some amount of new code is currently sitting in linux-next that should get
the same modifications, which I will do at the end of the merge window.
Many thanks to Julia Lawall for helping me learn to write a semantic
patch that does all this.
===== begin semantic patch =====
// This adds an llseek= method to all file operations,
// as a preparation for making no_llseek the default.
//
// The rules are
// - use no_llseek explicitly if we do nonseekable_open
// - use seq_lseek for sequential files
// - use default_llseek if we know we access f_pos
// - use noop_llseek if we know we don't access f_pos,
// but we still want to allow users to call lseek
//
@ open1 exists @
identifier nested_open;
@@
nested_open(...)
{
<+...
nonseekable_open(...)
...+>
}
@ open exists@
identifier open_f;
identifier i, f;
identifier open1.nested_open;
@@
int open_f(struct inode *i, struct file *f)
{
<+...
(
nonseekable_open(...)
|
nested_open(...)
)
...+>
}
@ read disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ read_no_fpos disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
... when != off
}
@ write @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ write_no_fpos @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
... when != off
}
@ fops0 @
identifier fops;
@@
struct file_operations fops = {
...
};
@ has_llseek depends on fops0 @
identifier fops0.fops;
identifier llseek_f;
@@
struct file_operations fops = {
...
.llseek = llseek_f,
...
};
@ has_read depends on fops0 @
identifier fops0.fops;
identifier read_f;
@@
struct file_operations fops = {
...
.read = read_f,
...
};
@ has_write depends on fops0 @
identifier fops0.fops;
identifier write_f;
@@
struct file_operations fops = {
...
.write = write_f,
...
};
@ has_open depends on fops0 @
identifier fops0.fops;
identifier open_f;
@@
struct file_operations fops = {
...
.open = open_f,
...
};
// use no_llseek if we call nonseekable_open
////////////////////////////////////////////
@ nonseekable1 depends on !has_llseek && has_open @
identifier fops0.fops;
identifier nso ~= "nonseekable_open";
@@
struct file_operations fops = {
... .open = nso, ...
+.llseek = no_llseek, /* nonseekable */
};
@ nonseekable2 depends on !has_llseek @
identifier fops0.fops;
identifier open.open_f;
@@
struct file_operations fops = {
... .open = open_f, ...
+.llseek = no_llseek, /* open uses nonseekable */
};
// use seq_lseek for sequential files
/////////////////////////////////////
@ seq depends on !has_llseek @
identifier fops0.fops;
identifier sr ~= "seq_read";
@@
struct file_operations fops = {
... .read = sr, ...
+.llseek = seq_lseek, /* we have seq_read */
};
// use default_llseek if there is a readdir
///////////////////////////////////////////
@ fops1 depends on !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier readdir_e;
@@
// any other fop is used that changes pos
struct file_operations fops = {
... .readdir = readdir_e, ...
+.llseek = default_llseek, /* readdir is present */
};
// use default_llseek if at least one of read/write touches f_pos
/////////////////////////////////////////////////////////////////
@ fops2 depends on !fops1 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read.read_f;
@@
// read fops use offset
struct file_operations fops = {
... .read = read_f, ...
+.llseek = default_llseek, /* read accesses f_pos */
};
@ fops3 depends on !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write.write_f;
@@
// write fops use offset
struct file_operations fops = {
... .write = write_f, ...
+ .llseek = default_llseek, /* write accesses f_pos */
};
// Use noop_llseek if neither read nor write accesses f_pos
///////////////////////////////////////////////////////////
@ fops4 depends on !fops1 && !fops2 && !fops3 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
identifier write_no_fpos.write_f;
@@
// write fops use offset
struct file_operations fops = {
...
.write = write_f,
.read = read_f,
...
+.llseek = noop_llseek, /* read and write both use no f_pos */
};
@ depends on has_write && !has_read && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write_no_fpos.write_f;
@@
struct file_operations fops = {
... .write = write_f, ...
+.llseek = noop_llseek, /* write uses no f_pos */
};
@ depends on has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
@@
struct file_operations fops = {
... .read = read_f, ...
+.llseek = noop_llseek, /* read uses no f_pos */
};
@ depends on !has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
@@
struct file_operations fops = {
...
+.llseek = noop_llseek, /* no read or write fn */
};
===== End semantic patch =====
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Cc: Julia Lawall <julia@diku.dk>
Cc: Christoph Hellwig <hch@infradead.org>
2010-08-15 16:52:59 +00:00
|
|
|
.llseek = default_llseek,
|
2008-07-15 12:54:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct file_operations proc_sys_dir_file_operations = {
|
2011-11-02 20:38:42 +00:00
|
|
|
.read = generic_read_dir,
|
2016-04-20 21:13:54 +00:00
|
|
|
.iterate_shared = proc_sys_readdir,
|
2008-09-03 19:53:01 +00:00
|
|
|
.llseek = generic_file_llseek,
|
2007-02-14 08:34:12 +00:00
|
|
|
};
|
|
|
|
|
2008-02-08 12:21:19 +00:00
|
|
|
static const struct inode_operations proc_sys_inode_operations = {
|
2008-07-15 12:54:06 +00:00
|
|
|
.permission = proc_sys_permission,
|
|
|
|
.setattr = proc_sys_setattr,
|
|
|
|
.getattr = proc_sys_getattr,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct inode_operations proc_sys_dir_operations = {
|
2007-02-14 08:34:12 +00:00
|
|
|
.lookup = proc_sys_lookup,
|
|
|
|
.permission = proc_sys_permission,
|
|
|
|
.setattr = proc_sys_setattr,
|
2008-07-15 12:54:06 +00:00
|
|
|
.getattr = proc_sys_getattr,
|
2007-02-14 08:34:12 +00:00
|
|
|
};
|
|
|
|
|
2012-06-10 20:03:43 +00:00
|
|
|
static int proc_sys_revalidate(struct dentry *dentry, unsigned int flags)
|
2007-02-14 08:34:12 +00:00
|
|
|
{
|
2012-06-10 20:03:43 +00:00
|
|
|
if (flags & LOOKUP_RCU)
|
2011-01-07 06:49:57 +00:00
|
|
|
return -ECHILD;
|
2015-03-17 22:25:59 +00:00
|
|
|
return !PROC_I(d_inode(dentry))->sysctl->unregistering;
|
2008-07-15 12:54:06 +00:00
|
|
|
}
|
|
|
|
|
2011-01-07 06:49:23 +00:00
|
|
|
static int proc_sys_delete(const struct dentry *dentry)
|
2008-07-15 12:54:06 +00:00
|
|
|
{
|
2015-03-17 22:25:59 +00:00
|
|
|
return !!PROC_I(d_inode(dentry))->sysctl->unregistering;
|
2008-07-15 12:54:06 +00:00
|
|
|
}
|
|
|
|
|
2012-01-06 12:07:15 +00:00
|
|
|
static int sysctl_is_seen(struct ctl_table_header *p)
|
|
|
|
{
|
|
|
|
struct ctl_table_set *set = p->set;
|
|
|
|
int res;
|
|
|
|
spin_lock(&sysctl_lock);
|
|
|
|
if (p->unregistering)
|
|
|
|
res = 0;
|
|
|
|
else if (!set->is_seen)
|
|
|
|
res = 1;
|
|
|
|
else
|
|
|
|
res = set->is_seen(set);
|
|
|
|
spin_unlock(&sysctl_lock);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-05-21 22:22:44 +00:00
|
|
|
static int proc_sys_compare(const struct dentry *parent, const struct dentry *dentry,
|
2011-01-07 06:49:27 +00:00
|
|
|
unsigned int len, const char *str, const struct qstr *name)
|
2008-07-15 12:54:06 +00:00
|
|
|
{
|
2011-03-08 06:25:28 +00:00
|
|
|
struct ctl_table_header *head;
|
2013-05-21 22:22:44 +00:00
|
|
|
struct inode *inode;
|
|
|
|
|
fs: rcu-walk for path lookup
Perform common cases of path lookups without any stores or locking in the
ancestor dentry elements. This is called rcu-walk, as opposed to the current
algorithm which is a refcount based walk, or ref-walk.
This results in far fewer atomic operations on every path element,
significantly improving path lookup performance. It also avoids cacheline
bouncing on common dentries, significantly improving scalability.
The overall design is like this:
* LOOKUP_RCU is set in nd->flags, which distinguishes rcu-walk from ref-walk.
* Take the RCU lock for the entire path walk, starting with the acquiring
of the starting path (eg. root/cwd/fd-path). So now dentry refcounts are
not required for dentry persistence.
* synchronize_rcu is called when unregistering a filesystem, so we can
access d_ops and i_ops during rcu-walk.
* Similarly take the vfsmount lock for the entire path walk. So now mnt
refcounts are not required for persistence. Also we are free to perform mount
lookups, and to assume dentry mount points and mount roots are stable up and
down the path.
* Have a per-dentry seqlock to protect the dentry name, parent, and inode,
so we can load this tuple atomically, and also check whether any of its
members have changed.
* Dentry lookups (based on parent, candidate string tuple) recheck the parent
sequence after the child is found in case anything changed in the parent
during the path walk.
* inode is also RCU protected so we can load d_inode and use the inode for
limited things.
* i_mode, i_uid, i_gid can be tested for exec permissions during path walk.
* i_op can be loaded.
When we reach the destination dentry, we lock it, recheck lookup sequence,
and increment its refcount and mountpoint refcount. RCU and vfsmount locks
are dropped. This is termed "dropping rcu-walk". If the dentry refcount does
not match, we can not drop rcu-walk gracefully at the current point in the
lokup, so instead return -ECHILD (for want of a better errno). This signals the
path walking code to re-do the entire lookup with a ref-walk.
Aside from the final dentry, there are other situations that may be encounted
where we cannot continue rcu-walk. In that case, we drop rcu-walk (ie. take
a reference on the last good dentry) and continue with a ref-walk. Again, if
we can drop rcu-walk gracefully, we return -ECHILD and do the whole lookup
using ref-walk. But it is very important that we can continue with ref-walk
for most cases, particularly to avoid the overhead of double lookups, and to
gain the scalability advantages on common path elements (like cwd and root).
The cases where rcu-walk cannot continue are:
* NULL dentry (ie. any uncached path element)
* parent with d_inode->i_op->permission or ACLs
* dentries with d_revalidate
* Following links
In future patches, permission checks and d_revalidate become rcu-walk aware. It
may be possible eventually to make following links rcu-walk aware.
Uncached path elements will always require dropping to ref-walk mode, at the
very least because i_mutex needs to be grabbed, and objects allocated.
Signed-off-by: Nick Piggin <npiggin@kernel.dk>
2011-01-07 06:49:52 +00:00
|
|
|
/* Although proc doesn't have negative dentries, rcu-walk means
|
|
|
|
* that inode here can be NULL */
|
2011-03-08 06:25:28 +00:00
|
|
|
/* AV: can it, indeed? */
|
2015-03-17 22:25:59 +00:00
|
|
|
inode = d_inode_rcu(dentry);
|
fs: rcu-walk for path lookup
Perform common cases of path lookups without any stores or locking in the
ancestor dentry elements. This is called rcu-walk, as opposed to the current
algorithm which is a refcount based walk, or ref-walk.
This results in far fewer atomic operations on every path element,
significantly improving path lookup performance. It also avoids cacheline
bouncing on common dentries, significantly improving scalability.
The overall design is like this:
* LOOKUP_RCU is set in nd->flags, which distinguishes rcu-walk from ref-walk.
* Take the RCU lock for the entire path walk, starting with the acquiring
of the starting path (eg. root/cwd/fd-path). So now dentry refcounts are
not required for dentry persistence.
* synchronize_rcu is called when unregistering a filesystem, so we can
access d_ops and i_ops during rcu-walk.
* Similarly take the vfsmount lock for the entire path walk. So now mnt
refcounts are not required for persistence. Also we are free to perform mount
lookups, and to assume dentry mount points and mount roots are stable up and
down the path.
* Have a per-dentry seqlock to protect the dentry name, parent, and inode,
so we can load this tuple atomically, and also check whether any of its
members have changed.
* Dentry lookups (based on parent, candidate string tuple) recheck the parent
sequence after the child is found in case anything changed in the parent
during the path walk.
* inode is also RCU protected so we can load d_inode and use the inode for
limited things.
* i_mode, i_uid, i_gid can be tested for exec permissions during path walk.
* i_op can be loaded.
When we reach the destination dentry, we lock it, recheck lookup sequence,
and increment its refcount and mountpoint refcount. RCU and vfsmount locks
are dropped. This is termed "dropping rcu-walk". If the dentry refcount does
not match, we can not drop rcu-walk gracefully at the current point in the
lokup, so instead return -ECHILD (for want of a better errno). This signals the
path walking code to re-do the entire lookup with a ref-walk.
Aside from the final dentry, there are other situations that may be encounted
where we cannot continue rcu-walk. In that case, we drop rcu-walk (ie. take
a reference on the last good dentry) and continue with a ref-walk. Again, if
we can drop rcu-walk gracefully, we return -ECHILD and do the whole lookup
using ref-walk. But it is very important that we can continue with ref-walk
for most cases, particularly to avoid the overhead of double lookups, and to
gain the scalability advantages on common path elements (like cwd and root).
The cases where rcu-walk cannot continue are:
* NULL dentry (ie. any uncached path element)
* parent with d_inode->i_op->permission or ACLs
* dentries with d_revalidate
* Following links
In future patches, permission checks and d_revalidate become rcu-walk aware. It
may be possible eventually to make following links rcu-walk aware.
Uncached path elements will always require dropping to ref-walk mode, at the
very least because i_mutex needs to be grabbed, and objects allocated.
Signed-off-by: Nick Piggin <npiggin@kernel.dk>
2011-01-07 06:49:52 +00:00
|
|
|
if (!inode)
|
2011-03-08 06:25:28 +00:00
|
|
|
return 1;
|
2011-01-07 06:49:27 +00:00
|
|
|
if (name->len != len)
|
2008-07-15 12:54:06 +00:00
|
|
|
return 1;
|
2011-01-07 06:49:27 +00:00
|
|
|
if (memcmp(name->name, str, len))
|
2008-07-15 12:54:06 +00:00
|
|
|
return 1;
|
2011-03-08 06:25:28 +00:00
|
|
|
head = rcu_dereference(PROC_I(inode)->sysctl);
|
|
|
|
return !head || !sysctl_is_seen(head);
|
2007-02-14 08:34:12 +00:00
|
|
|
}
|
|
|
|
|
2009-02-20 05:58:47 +00:00
|
|
|
static const struct dentry_operations proc_sys_dentry_operations = {
|
2007-02-14 08:34:12 +00:00
|
|
|
.d_revalidate = proc_sys_revalidate,
|
2008-07-15 12:54:06 +00:00
|
|
|
.d_delete = proc_sys_delete,
|
|
|
|
.d_compare = proc_sys_compare,
|
2007-02-14 08:34:12 +00:00
|
|
|
};
|
|
|
|
|
2012-01-08 07:24:30 +00:00
|
|
|
static struct ctl_dir *find_subdir(struct ctl_dir *dir,
|
|
|
|
const char *name, int namelen)
|
2012-01-06 12:07:15 +00:00
|
|
|
{
|
2011-12-29 16:24:29 +00:00
|
|
|
struct ctl_table_header *head;
|
|
|
|
struct ctl_table *entry;
|
2012-01-06 12:07:15 +00:00
|
|
|
|
2012-01-08 07:24:30 +00:00
|
|
|
entry = find_entry(&head, dir, name, namelen);
|
2011-12-29 16:24:29 +00:00
|
|
|
if (!entry)
|
|
|
|
return ERR_PTR(-ENOENT);
|
2012-01-31 04:09:33 +00:00
|
|
|
if (!S_ISDIR(entry->mode))
|
|
|
|
return ERR_PTR(-ENOTDIR);
|
|
|
|
return container_of(head, struct ctl_dir, header);
|
2011-12-29 16:24:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct ctl_dir *new_dir(struct ctl_table_set *set,
|
2012-01-08 07:24:30 +00:00
|
|
|
const char *name, int namelen)
|
2011-12-29 16:24:29 +00:00
|
|
|
{
|
|
|
|
struct ctl_table *table;
|
|
|
|
struct ctl_dir *new;
|
2012-01-10 01:24:30 +00:00
|
|
|
struct ctl_node *node;
|
2011-12-29 16:24:29 +00:00
|
|
|
char *new_name;
|
2012-01-06 12:07:15 +00:00
|
|
|
|
2012-01-10 01:24:30 +00:00
|
|
|
new = kzalloc(sizeof(*new) + sizeof(struct ctl_node) +
|
|
|
|
sizeof(struct ctl_table)*2 + namelen + 1,
|
|
|
|
GFP_KERNEL);
|
2011-12-29 16:24:29 +00:00
|
|
|
if (!new)
|
2012-01-06 12:07:15 +00:00
|
|
|
return NULL;
|
|
|
|
|
2012-01-10 01:24:30 +00:00
|
|
|
node = (struct ctl_node *)(new + 1);
|
|
|
|
table = (struct ctl_table *)(node + 1);
|
2011-12-29 16:24:29 +00:00
|
|
|
new_name = (char *)(table + 2);
|
|
|
|
memcpy(new_name, name, namelen);
|
|
|
|
new_name[namelen] = '\0';
|
|
|
|
table[0].procname = new_name;
|
|
|
|
table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
|
2012-01-10 01:24:30 +00:00
|
|
|
init_header(&new->header, set->dir.header.root, set, node, table);
|
2011-12-29 16:24:29 +00:00
|
|
|
|
|
|
|
return new;
|
2012-01-06 12:07:15 +00:00
|
|
|
}
|
|
|
|
|
2012-01-31 05:23:52 +00:00
|
|
|
/**
|
|
|
|
* get_subdir - find or create a subdir with the specified name.
|
|
|
|
* @dir: Directory to create the subdirectory in
|
|
|
|
* @name: The name of the subdirectory to find or create
|
|
|
|
* @namelen: The length of name
|
|
|
|
*
|
|
|
|
* Takes a directory with an elevated reference count so we know that
|
|
|
|
* if we drop the lock the directory will not go away. Upon success
|
|
|
|
* the reference is moved from @dir to the returned subdirectory.
|
|
|
|
* Upon error an error code is returned and the reference on @dir is
|
|
|
|
* simply dropped.
|
|
|
|
*/
|
2012-01-08 07:24:30 +00:00
|
|
|
static struct ctl_dir *get_subdir(struct ctl_dir *dir,
|
|
|
|
const char *name, int namelen)
|
2012-01-06 12:07:15 +00:00
|
|
|
{
|
2012-01-08 07:24:30 +00:00
|
|
|
struct ctl_table_set *set = dir->header.set;
|
2011-12-29 16:24:29 +00:00
|
|
|
struct ctl_dir *subdir, *new = NULL;
|
2012-01-31 04:37:51 +00:00
|
|
|
int err;
|
2012-01-06 12:07:15 +00:00
|
|
|
|
2011-12-29 16:24:29 +00:00
|
|
|
spin_lock(&sysctl_lock);
|
2012-01-08 07:24:30 +00:00
|
|
|
subdir = find_subdir(dir, name, namelen);
|
2011-12-29 16:24:29 +00:00
|
|
|
if (!IS_ERR(subdir))
|
|
|
|
goto found;
|
|
|
|
if (PTR_ERR(subdir) != -ENOENT)
|
|
|
|
goto failed;
|
|
|
|
|
|
|
|
spin_unlock(&sysctl_lock);
|
|
|
|
new = new_dir(set, name, namelen);
|
|
|
|
spin_lock(&sysctl_lock);
|
|
|
|
subdir = ERR_PTR(-ENOMEM);
|
|
|
|
if (!new)
|
|
|
|
goto failed;
|
|
|
|
|
2012-01-31 05:23:52 +00:00
|
|
|
/* Was the subdir added while we dropped the lock? */
|
2012-01-08 07:24:30 +00:00
|
|
|
subdir = find_subdir(dir, name, namelen);
|
2011-12-29 16:24:29 +00:00
|
|
|
if (!IS_ERR(subdir))
|
|
|
|
goto found;
|
|
|
|
if (PTR_ERR(subdir) != -ENOENT)
|
|
|
|
goto failed;
|
|
|
|
|
2012-01-31 05:23:52 +00:00
|
|
|
/* Nope. Use the our freshly made directory entry. */
|
2012-01-31 04:37:51 +00:00
|
|
|
err = insert_header(dir, &new->header);
|
|
|
|
subdir = ERR_PTR(err);
|
|
|
|
if (err)
|
2012-01-08 07:24:30 +00:00
|
|
|
goto failed;
|
2011-12-29 16:24:29 +00:00
|
|
|
subdir = new;
|
|
|
|
found:
|
|
|
|
subdir->header.nreg++;
|
|
|
|
failed:
|
2015-08-12 10:29:44 +00:00
|
|
|
if (IS_ERR(subdir)) {
|
2013-02-28 01:03:16 +00:00
|
|
|
pr_err("sysctl could not get directory: ");
|
2012-01-22 04:09:45 +00:00
|
|
|
sysctl_print_dir(dir);
|
2013-02-28 01:03:16 +00:00
|
|
|
pr_cont("/%*.*s %ld\n",
|
2011-12-29 16:24:29 +00:00
|
|
|
namelen, namelen, name, PTR_ERR(subdir));
|
2012-01-06 12:07:15 +00:00
|
|
|
}
|
2011-12-29 16:24:29 +00:00
|
|
|
drop_sysctl_table(&dir->header);
|
|
|
|
if (new)
|
|
|
|
drop_sysctl_table(&new->header);
|
|
|
|
spin_unlock(&sysctl_lock);
|
|
|
|
return subdir;
|
2012-01-06 12:07:15 +00:00
|
|
|
}
|
|
|
|
|
2012-01-08 07:24:30 +00:00
|
|
|
static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir)
|
|
|
|
{
|
|
|
|
struct ctl_dir *parent;
|
|
|
|
const char *procname;
|
|
|
|
if (!dir->header.parent)
|
|
|
|
return &set->dir;
|
|
|
|
parent = xlate_dir(set, dir->header.parent);
|
|
|
|
if (IS_ERR(parent))
|
|
|
|
return parent;
|
|
|
|
procname = dir->header.ctl_table[0].procname;
|
|
|
|
return find_subdir(parent, procname, strlen(procname));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sysctl_follow_link(struct ctl_table_header **phead,
|
|
|
|
struct ctl_table **pentry, struct nsproxy *namespaces)
|
|
|
|
{
|
|
|
|
struct ctl_table_header *head;
|
|
|
|
struct ctl_table_root *root;
|
|
|
|
struct ctl_table_set *set;
|
|
|
|
struct ctl_table *entry;
|
|
|
|
struct ctl_dir *dir;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
spin_lock(&sysctl_lock);
|
|
|
|
root = (*pentry)->data;
|
|
|
|
set = lookup_header_set(root, namespaces);
|
|
|
|
dir = xlate_dir(set, (*phead)->parent);
|
|
|
|
if (IS_ERR(dir))
|
|
|
|
ret = PTR_ERR(dir);
|
|
|
|
else {
|
|
|
|
const char *procname = (*pentry)->procname;
|
|
|
|
head = NULL;
|
|
|
|
entry = find_entry(&head, dir, procname, strlen(procname));
|
|
|
|
ret = -ENOENT;
|
|
|
|
if (entry && use_table(head)) {
|
|
|
|
unuse_table(*phead);
|
|
|
|
*phead = head;
|
|
|
|
*pentry = entry;
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock(&sysctl_lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-01-21 21:34:05 +00:00
|
|
|
static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
|
2012-01-06 12:07:15 +00:00
|
|
|
{
|
2012-01-21 21:34:05 +00:00
|
|
|
struct va_format vaf;
|
|
|
|
va_list args;
|
2012-01-06 12:07:15 +00:00
|
|
|
|
2012-01-21 21:34:05 +00:00
|
|
|
va_start(args, fmt);
|
|
|
|
vaf.fmt = fmt;
|
|
|
|
vaf.va = &args;
|
|
|
|
|
2013-02-28 01:03:16 +00:00
|
|
|
pr_err("sysctl table check failed: %s/%s %pV\n",
|
|
|
|
path, table->procname, &vaf);
|
2012-01-06 12:07:15 +00:00
|
|
|
|
2012-01-21 21:34:05 +00:00
|
|
|
va_end(args);
|
|
|
|
return -EINVAL;
|
2012-01-06 12:07:15 +00:00
|
|
|
}
|
|
|
|
|
2012-01-21 21:34:05 +00:00
|
|
|
static int sysctl_check_table(const char *path, struct ctl_table *table)
|
2012-01-06 12:07:15 +00:00
|
|
|
{
|
2012-01-21 21:34:05 +00:00
|
|
|
int err = 0;
|
2012-01-06 12:07:15 +00:00
|
|
|
for (; table->procname; table++) {
|
|
|
|
if (table->child)
|
2012-01-21 21:34:05 +00:00
|
|
|
err = sysctl_err(path, table, "Not a file");
|
|
|
|
|
|
|
|
if ((table->proc_handler == proc_dostring) ||
|
|
|
|
(table->proc_handler == proc_dointvec) ||
|
|
|
|
(table->proc_handler == proc_dointvec_minmax) ||
|
|
|
|
(table->proc_handler == proc_dointvec_jiffies) ||
|
|
|
|
(table->proc_handler == proc_dointvec_userhz_jiffies) ||
|
|
|
|
(table->proc_handler == proc_dointvec_ms_jiffies) ||
|
|
|
|
(table->proc_handler == proc_doulongvec_minmax) ||
|
|
|
|
(table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
|
|
|
|
if (!table->data)
|
|
|
|
err = sysctl_err(path, table, "No data");
|
|
|
|
if (!table->maxlen)
|
|
|
|
err = sysctl_err(path, table, "No maxlen");
|
|
|
|
}
|
|
|
|
if (!table->proc_handler)
|
|
|
|
err = sysctl_err(path, table, "No proc_handler");
|
|
|
|
|
|
|
|
if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
|
|
|
|
err = sysctl_err(path, table, "bogus .mode 0%o",
|
|
|
|
table->mode);
|
2012-01-06 12:07:15 +00:00
|
|
|
}
|
2012-01-21 21:34:05 +00:00
|
|
|
return err;
|
2012-01-06 12:07:15 +00:00
|
|
|
}
|
|
|
|
|
2012-01-08 07:24:30 +00:00
|
|
|
static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table *table,
|
|
|
|
struct ctl_table_root *link_root)
|
|
|
|
{
|
|
|
|
struct ctl_table *link_table, *entry, *link;
|
|
|
|
struct ctl_table_header *links;
|
2012-01-10 01:24:30 +00:00
|
|
|
struct ctl_node *node;
|
2012-01-08 07:24:30 +00:00
|
|
|
char *link_name;
|
|
|
|
int nr_entries, name_bytes;
|
|
|
|
|
|
|
|
name_bytes = 0;
|
|
|
|
nr_entries = 0;
|
|
|
|
for (entry = table; entry->procname; entry++) {
|
|
|
|
nr_entries++;
|
|
|
|
name_bytes += strlen(entry->procname) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
links = kzalloc(sizeof(struct ctl_table_header) +
|
2012-01-10 01:24:30 +00:00
|
|
|
sizeof(struct ctl_node)*nr_entries +
|
2012-01-08 07:24:30 +00:00
|
|
|
sizeof(struct ctl_table)*(nr_entries + 1) +
|
|
|
|
name_bytes,
|
|
|
|
GFP_KERNEL);
|
|
|
|
|
|
|
|
if (!links)
|
|
|
|
return NULL;
|
|
|
|
|
2012-01-10 01:24:30 +00:00
|
|
|
node = (struct ctl_node *)(links + 1);
|
|
|
|
link_table = (struct ctl_table *)(node + nr_entries);
|
2012-01-08 07:24:30 +00:00
|
|
|
link_name = (char *)&link_table[nr_entries + 1];
|
|
|
|
|
|
|
|
for (link = link_table, entry = table; entry->procname; link++, entry++) {
|
|
|
|
int len = strlen(entry->procname) + 1;
|
|
|
|
memcpy(link_name, entry->procname, len);
|
|
|
|
link->procname = link_name;
|
|
|
|
link->mode = S_IFLNK|S_IRWXUGO;
|
|
|
|
link->data = link_root;
|
|
|
|
link_name += len;
|
|
|
|
}
|
2012-01-10 01:24:30 +00:00
|
|
|
init_header(links, dir->header.root, dir->header.set, node, link_table);
|
2012-01-08 07:24:30 +00:00
|
|
|
links->nreg = nr_entries;
|
|
|
|
|
|
|
|
return links;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool get_links(struct ctl_dir *dir,
|
|
|
|
struct ctl_table *table, struct ctl_table_root *link_root)
|
|
|
|
{
|
|
|
|
struct ctl_table_header *head;
|
|
|
|
struct ctl_table *entry, *link;
|
|
|
|
|
|
|
|
/* Are there links available for every entry in table? */
|
|
|
|
for (entry = table; entry->procname; entry++) {
|
|
|
|
const char *procname = entry->procname;
|
|
|
|
link = find_entry(&head, dir, procname, strlen(procname));
|
|
|
|
if (!link)
|
|
|
|
return false;
|
|
|
|
if (S_ISDIR(link->mode) && S_ISDIR(entry->mode))
|
|
|
|
continue;
|
|
|
|
if (S_ISLNK(link->mode) && (link->data == link_root))
|
|
|
|
continue;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The checks passed. Increase the registration count on the links */
|
|
|
|
for (entry = table; entry->procname; entry++) {
|
|
|
|
const char *procname = entry->procname;
|
|
|
|
link = find_entry(&head, dir, procname, strlen(procname));
|
|
|
|
head->nreg++;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int insert_links(struct ctl_table_header *head)
|
|
|
|
{
|
|
|
|
struct ctl_table_set *root_set = &sysctl_table_root.default_set;
|
|
|
|
struct ctl_dir *core_parent = NULL;
|
|
|
|
struct ctl_table_header *links;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (head->set == root_set)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
core_parent = xlate_dir(root_set, head->parent);
|
|
|
|
if (IS_ERR(core_parent))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (get_links(core_parent, head->ctl_table, head->root))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
core_parent->header.nreg++;
|
|
|
|
spin_unlock(&sysctl_lock);
|
|
|
|
|
|
|
|
links = new_links(core_parent, head->ctl_table, head->root);
|
|
|
|
|
|
|
|
spin_lock(&sysctl_lock);
|
|
|
|
err = -ENOMEM;
|
|
|
|
if (!links)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
err = 0;
|
|
|
|
if (get_links(core_parent, head->ctl_table, head->root)) {
|
|
|
|
kfree(links);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = insert_header(core_parent, links);
|
|
|
|
if (err)
|
|
|
|
kfree(links);
|
|
|
|
out:
|
|
|
|
drop_sysctl_table(&core_parent->header);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2012-01-06 12:07:15 +00:00
|
|
|
/**
|
2012-01-23 02:22:05 +00:00
|
|
|
* __register_sysctl_table - register a leaf sysctl table
|
2012-01-08 08:02:37 +00:00
|
|
|
* @set: Sysctl tree to register on
|
2012-01-06 12:07:15 +00:00
|
|
|
* @path: The path to the directory the sysctl table is in.
|
|
|
|
* @table: the top-level table structure
|
|
|
|
*
|
|
|
|
* Register a sysctl table hierarchy. @table should be a filled in ctl_table
|
|
|
|
* array. A completely 0 filled entry terminates the table.
|
|
|
|
*
|
|
|
|
* The members of the &struct ctl_table structure are used as follows:
|
|
|
|
*
|
|
|
|
* procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
|
|
|
|
* enter a sysctl file
|
|
|
|
*
|
|
|
|
* data - a pointer to data for use by proc_handler
|
|
|
|
*
|
|
|
|
* maxlen - the maximum size in bytes of the data
|
|
|
|
*
|
2012-01-23 02:22:05 +00:00
|
|
|
* mode - the file permissions for the /proc/sys file
|
2012-01-06 12:07:15 +00:00
|
|
|
*
|
2012-01-23 02:22:05 +00:00
|
|
|
* child - must be %NULL.
|
2012-01-06 12:07:15 +00:00
|
|
|
*
|
|
|
|
* proc_handler - the text handler routine (described below)
|
|
|
|
*
|
|
|
|
* extra1, extra2 - extra pointers usable by the proc handler routines
|
|
|
|
*
|
|
|
|
* Leaf nodes in the sysctl tree will be represented by a single file
|
|
|
|
* under /proc; non-leaf nodes will be represented by directories.
|
|
|
|
*
|
2012-01-23 02:22:05 +00:00
|
|
|
* There must be a proc_handler routine for any terminal nodes.
|
|
|
|
* Several default handlers are available to cover common cases -
|
2012-01-06 12:07:15 +00:00
|
|
|
*
|
|
|
|
* proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
|
|
|
|
* proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
|
|
|
|
* proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
|
|
|
|
*
|
|
|
|
* It is the handler's job to read the input buffer from user memory
|
|
|
|
* and process it. The handler should return 0 on success.
|
|
|
|
*
|
|
|
|
* This routine returns %NULL on a failure to register, and a pointer
|
|
|
|
* to the table header on success.
|
|
|
|
*/
|
2012-01-21 18:26:26 +00:00
|
|
|
struct ctl_table_header *__register_sysctl_table(
|
2012-01-08 08:02:37 +00:00
|
|
|
struct ctl_table_set *set,
|
2012-01-21 18:26:26 +00:00
|
|
|
const char *path, struct ctl_table *table)
|
2012-01-06 12:07:15 +00:00
|
|
|
{
|
2012-01-08 08:02:37 +00:00
|
|
|
struct ctl_table_root *root = set->dir.header.root;
|
2012-01-06 12:07:15 +00:00
|
|
|
struct ctl_table_header *header;
|
2012-01-21 18:26:26 +00:00
|
|
|
const char *name, *nextname;
|
2011-12-29 16:24:29 +00:00
|
|
|
struct ctl_dir *dir;
|
2012-01-10 01:24:30 +00:00
|
|
|
struct ctl_table *entry;
|
|
|
|
struct ctl_node *node;
|
|
|
|
int nr_entries = 0;
|
|
|
|
|
|
|
|
for (entry = table; entry->procname; entry++)
|
|
|
|
nr_entries++;
|
2012-01-06 12:07:15 +00:00
|
|
|
|
2012-01-10 01:24:30 +00:00
|
|
|
header = kzalloc(sizeof(struct ctl_table_header) +
|
|
|
|
sizeof(struct ctl_node)*nr_entries, GFP_KERNEL);
|
2012-01-06 12:07:15 +00:00
|
|
|
if (!header)
|
|
|
|
return NULL;
|
|
|
|
|
2012-01-10 01:24:30 +00:00
|
|
|
node = (struct ctl_node *)(header + 1);
|
|
|
|
init_header(header, root, set, node, table);
|
2011-12-29 16:24:29 +00:00
|
|
|
if (sysctl_check_table(path, table))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
spin_lock(&sysctl_lock);
|
2012-01-08 07:24:30 +00:00
|
|
|
dir = &set->dir;
|
2012-01-31 05:23:52 +00:00
|
|
|
/* Reference moved down the diretory tree get_subdir */
|
2011-12-29 16:24:29 +00:00
|
|
|
dir->header.nreg++;
|
|
|
|
spin_unlock(&sysctl_lock);
|
2012-01-06 12:07:15 +00:00
|
|
|
|
2011-12-29 16:24:29 +00:00
|
|
|
/* Find the directory for the ctl_table */
|
2012-01-21 18:26:26 +00:00
|
|
|
for (name = path; name; name = nextname) {
|
|
|
|
int namelen;
|
|
|
|
nextname = strchr(name, '/');
|
|
|
|
if (nextname) {
|
|
|
|
namelen = nextname - name;
|
|
|
|
nextname++;
|
|
|
|
} else {
|
|
|
|
namelen = strlen(name);
|
|
|
|
}
|
|
|
|
if (namelen == 0)
|
|
|
|
continue;
|
2012-01-06 12:07:15 +00:00
|
|
|
|
2012-01-08 07:24:30 +00:00
|
|
|
dir = get_subdir(dir, name, namelen);
|
2011-12-29 16:24:29 +00:00
|
|
|
if (IS_ERR(dir))
|
|
|
|
goto fail;
|
2012-01-06 12:07:15 +00:00
|
|
|
}
|
2012-01-08 07:24:30 +00:00
|
|
|
|
2012-01-06 12:07:15 +00:00
|
|
|
spin_lock(&sysctl_lock);
|
2012-01-08 07:24:30 +00:00
|
|
|
if (insert_header(dir, header))
|
2011-12-29 16:24:29 +00:00
|
|
|
goto fail_put_dir_locked;
|
2012-01-08 07:24:30 +00:00
|
|
|
|
2011-12-29 16:24:29 +00:00
|
|
|
drop_sysctl_table(&dir->header);
|
2012-01-06 12:07:15 +00:00
|
|
|
spin_unlock(&sysctl_lock);
|
|
|
|
|
|
|
|
return header;
|
2012-01-08 07:24:30 +00:00
|
|
|
|
2011-12-29 16:24:29 +00:00
|
|
|
fail_put_dir_locked:
|
|
|
|
drop_sysctl_table(&dir->header);
|
2012-01-21 21:34:05 +00:00
|
|
|
spin_unlock(&sysctl_lock);
|
|
|
|
fail:
|
|
|
|
kfree(header);
|
|
|
|
dump_stack();
|
|
|
|
return NULL;
|
2012-01-06 12:07:15 +00:00
|
|
|
}
|
|
|
|
|
2012-01-21 05:47:03 +00:00
|
|
|
/**
|
|
|
|
* register_sysctl - register a sysctl table
|
|
|
|
* @path: The path to the directory the sysctl table is in.
|
|
|
|
* @table: the table structure
|
|
|
|
*
|
|
|
|
* Register a sysctl table. @table should be a filled in ctl_table
|
|
|
|
* array. A completely 0 filled entry terminates the table.
|
|
|
|
*
|
|
|
|
* See __register_sysctl_table for more details.
|
|
|
|
*/
|
|
|
|
struct ctl_table_header *register_sysctl(const char *path, struct ctl_table *table)
|
|
|
|
{
|
|
|
|
return __register_sysctl_table(&sysctl_table_root.default_set,
|
|
|
|
path, table);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(register_sysctl);
|
|
|
|
|
2012-01-21 18:26:26 +00:00
|
|
|
static char *append_path(const char *path, char *pos, const char *name)
|
|
|
|
{
|
|
|
|
int namelen;
|
|
|
|
namelen = strlen(name);
|
|
|
|
if (((pos - path) + namelen + 2) >= PATH_MAX)
|
|
|
|
return NULL;
|
|
|
|
memcpy(pos, name, namelen);
|
|
|
|
pos[namelen] = '/';
|
|
|
|
pos[namelen + 1] = '\0';
|
|
|
|
pos += namelen + 1;
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2012-01-23 02:22:05 +00:00
|
|
|
static int count_subheaders(struct ctl_table *table)
|
|
|
|
{
|
|
|
|
int has_files = 0;
|
|
|
|
int nr_subheaders = 0;
|
|
|
|
struct ctl_table *entry;
|
|
|
|
|
|
|
|
/* special case: no directory and empty directory */
|
|
|
|
if (!table || !table->procname)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
for (entry = table; entry->procname; entry++) {
|
|
|
|
if (entry->child)
|
|
|
|
nr_subheaders += count_subheaders(entry->child);
|
|
|
|
else
|
|
|
|
has_files = 1;
|
|
|
|
}
|
|
|
|
return nr_subheaders + has_files;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int register_leaf_sysctl_tables(const char *path, char *pos,
|
2012-01-08 08:02:37 +00:00
|
|
|
struct ctl_table_header ***subheader, struct ctl_table_set *set,
|
2012-01-23 02:22:05 +00:00
|
|
|
struct ctl_table *table)
|
|
|
|
{
|
|
|
|
struct ctl_table *ctl_table_arg = NULL;
|
|
|
|
struct ctl_table *entry, *files;
|
|
|
|
int nr_files = 0;
|
|
|
|
int nr_dirs = 0;
|
|
|
|
int err = -ENOMEM;
|
|
|
|
|
|
|
|
for (entry = table; entry->procname; entry++) {
|
|
|
|
if (entry->child)
|
|
|
|
nr_dirs++;
|
|
|
|
else
|
|
|
|
nr_files++;
|
|
|
|
}
|
|
|
|
|
|
|
|
files = table;
|
|
|
|
/* If there are mixed files and directories we need a new table */
|
|
|
|
if (nr_dirs && nr_files) {
|
|
|
|
struct ctl_table *new;
|
|
|
|
files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!files)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
ctl_table_arg = files;
|
|
|
|
for (new = files, entry = table; entry->procname; entry++) {
|
|
|
|
if (entry->child)
|
|
|
|
continue;
|
|
|
|
*new = *entry;
|
|
|
|
new++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Register everything except a directory full of subdirectories */
|
|
|
|
if (nr_files || !nr_dirs) {
|
|
|
|
struct ctl_table_header *header;
|
2012-01-08 08:02:37 +00:00
|
|
|
header = __register_sysctl_table(set, path, files);
|
2012-01-23 02:22:05 +00:00
|
|
|
if (!header) {
|
|
|
|
kfree(ctl_table_arg);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remember if we need to free the file table */
|
|
|
|
header->ctl_table_arg = ctl_table_arg;
|
|
|
|
**subheader = header;
|
|
|
|
(*subheader)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Recurse into the subdirectories. */
|
|
|
|
for (entry = table; entry->procname; entry++) {
|
|
|
|
char *child_pos;
|
|
|
|
|
|
|
|
if (!entry->child)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
err = -ENAMETOOLONG;
|
|
|
|
child_pos = append_path(path, pos, entry->procname);
|
|
|
|
if (!child_pos)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
err = register_leaf_sysctl_tables(path, child_pos, subheader,
|
2012-01-08 08:02:37 +00:00
|
|
|
set, entry->child);
|
2012-01-23 02:22:05 +00:00
|
|
|
pos[0] = '\0';
|
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
err = 0;
|
|
|
|
out:
|
|
|
|
/* On failure our caller will unregister all registered subheaders */
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2012-01-21 18:26:26 +00:00
|
|
|
/**
|
|
|
|
* __register_sysctl_paths - register a sysctl table hierarchy
|
2012-01-08 08:02:37 +00:00
|
|
|
* @set: Sysctl tree to register on
|
2012-01-21 18:26:26 +00:00
|
|
|
* @path: The path to the directory the sysctl table is in.
|
|
|
|
* @table: the top-level table structure
|
|
|
|
*
|
|
|
|
* Register a sysctl table hierarchy. @table should be a filled in ctl_table
|
|
|
|
* array. A completely 0 filled entry terminates the table.
|
|
|
|
*
|
|
|
|
* See __register_sysctl_table for more details.
|
|
|
|
*/
|
|
|
|
struct ctl_table_header *__register_sysctl_paths(
|
2012-01-08 08:02:37 +00:00
|
|
|
struct ctl_table_set *set,
|
2012-01-21 18:26:26 +00:00
|
|
|
const struct ctl_path *path, struct ctl_table *table)
|
|
|
|
{
|
2012-01-21 20:35:23 +00:00
|
|
|
struct ctl_table *ctl_table_arg = table;
|
2012-01-23 02:22:05 +00:00
|
|
|
int nr_subheaders = count_subheaders(table);
|
|
|
|
struct ctl_table_header *header = NULL, **subheaders, **subheader;
|
2012-01-21 18:26:26 +00:00
|
|
|
const struct ctl_path *component;
|
|
|
|
char *new_path, *pos;
|
|
|
|
|
|
|
|
pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
|
|
|
|
if (!new_path)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
pos[0] = '\0';
|
|
|
|
for (component = path; component->procname; component++) {
|
|
|
|
pos = append_path(new_path, pos, component->procname);
|
|
|
|
if (!pos)
|
|
|
|
goto out;
|
|
|
|
}
|
2012-01-21 20:35:23 +00:00
|
|
|
while (table->procname && table->child && !table[1].procname) {
|
|
|
|
pos = append_path(new_path, pos, table->procname);
|
|
|
|
if (!pos)
|
|
|
|
goto out;
|
|
|
|
table = table->child;
|
|
|
|
}
|
2012-01-23 02:22:05 +00:00
|
|
|
if (nr_subheaders == 1) {
|
2012-01-08 08:02:37 +00:00
|
|
|
header = __register_sysctl_table(set, new_path, table);
|
2012-01-23 02:22:05 +00:00
|
|
|
if (header)
|
|
|
|
header->ctl_table_arg = ctl_table_arg;
|
|
|
|
} else {
|
|
|
|
header = kzalloc(sizeof(*header) +
|
|
|
|
sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
|
|
|
|
if (!header)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
subheaders = (struct ctl_table_header **) (header + 1);
|
|
|
|
subheader = subheaders;
|
2012-01-21 20:35:23 +00:00
|
|
|
header->ctl_table_arg = ctl_table_arg;
|
2012-01-23 02:22:05 +00:00
|
|
|
|
|
|
|
if (register_leaf_sysctl_tables(new_path, pos, &subheader,
|
2012-01-08 08:02:37 +00:00
|
|
|
set, table))
|
2012-01-23 02:22:05 +00:00
|
|
|
goto err_register_leaves;
|
|
|
|
}
|
|
|
|
|
2012-01-21 18:26:26 +00:00
|
|
|
out:
|
|
|
|
kfree(new_path);
|
|
|
|
return header;
|
2012-01-23 02:22:05 +00:00
|
|
|
|
|
|
|
err_register_leaves:
|
|
|
|
while (subheader > subheaders) {
|
|
|
|
struct ctl_table_header *subh = *(--subheader);
|
|
|
|
struct ctl_table *table = subh->ctl_table_arg;
|
|
|
|
unregister_sysctl_table(subh);
|
|
|
|
kfree(table);
|
|
|
|
}
|
|
|
|
kfree(header);
|
|
|
|
header = NULL;
|
|
|
|
goto out;
|
2012-01-21 18:26:26 +00:00
|
|
|
}
|
|
|
|
|
2012-01-06 12:07:15 +00:00
|
|
|
/**
|
|
|
|
* register_sysctl_table_path - register a sysctl table hierarchy
|
|
|
|
* @path: The path to the directory the sysctl table is in.
|
|
|
|
* @table: the top-level table structure
|
|
|
|
*
|
|
|
|
* Register a sysctl table hierarchy. @table should be a filled in ctl_table
|
|
|
|
* array. A completely 0 filled entry terminates the table.
|
|
|
|
*
|
|
|
|
* See __register_sysctl_paths for more details.
|
|
|
|
*/
|
|
|
|
struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
|
|
|
|
struct ctl_table *table)
|
|
|
|
{
|
2012-01-08 08:02:37 +00:00
|
|
|
return __register_sysctl_paths(&sysctl_table_root.default_set,
|
2012-01-06 12:07:15 +00:00
|
|
|
path, table);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(register_sysctl_paths);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* register_sysctl_table - register a sysctl table hierarchy
|
|
|
|
* @table: the top-level table structure
|
|
|
|
*
|
|
|
|
* Register a sysctl table hierarchy. @table should be a filled in ctl_table
|
|
|
|
* array. A completely 0 filled entry terminates the table.
|
|
|
|
*
|
|
|
|
* See register_sysctl_paths for more details.
|
|
|
|
*/
|
|
|
|
struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
|
|
|
|
{
|
|
|
|
static const struct ctl_path null_path[] = { {} };
|
|
|
|
|
|
|
|
return register_sysctl_paths(null_path, table);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(register_sysctl_table);
|
|
|
|
|
2012-01-08 07:24:30 +00:00
|
|
|
static void put_links(struct ctl_table_header *header)
|
|
|
|
{
|
|
|
|
struct ctl_table_set *root_set = &sysctl_table_root.default_set;
|
|
|
|
struct ctl_table_root *root = header->root;
|
|
|
|
struct ctl_dir *parent = header->parent;
|
|
|
|
struct ctl_dir *core_parent;
|
|
|
|
struct ctl_table *entry;
|
|
|
|
|
|
|
|
if (header->set == root_set)
|
|
|
|
return;
|
|
|
|
|
|
|
|
core_parent = xlate_dir(root_set, parent);
|
|
|
|
if (IS_ERR(core_parent))
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (entry = header->ctl_table; entry->procname; entry++) {
|
|
|
|
struct ctl_table_header *link_head;
|
|
|
|
struct ctl_table *link;
|
|
|
|
const char *name = entry->procname;
|
|
|
|
|
|
|
|
link = find_entry(&link_head, core_parent, name, strlen(name));
|
|
|
|
if (link &&
|
|
|
|
((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) ||
|
|
|
|
(S_ISLNK(link->mode) && (link->data == root)))) {
|
|
|
|
drop_sysctl_table(link_head);
|
|
|
|
}
|
|
|
|
else {
|
2013-02-28 01:03:16 +00:00
|
|
|
pr_err("sysctl link missing during unregister: ");
|
2012-01-08 07:24:30 +00:00
|
|
|
sysctl_print_dir(parent);
|
2013-02-28 01:03:16 +00:00
|
|
|
pr_cont("/%s\n", name);
|
2012-01-08 07:24:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-10 01:24:30 +00:00
|
|
|
static void drop_sysctl_table(struct ctl_table_header *header)
|
|
|
|
{
|
2011-12-29 16:24:29 +00:00
|
|
|
struct ctl_dir *parent = header->parent;
|
|
|
|
|
2012-01-10 01:24:30 +00:00
|
|
|
if (--header->nreg)
|
|
|
|
return;
|
|
|
|
|
2012-01-08 07:24:30 +00:00
|
|
|
put_links(header);
|
2012-01-10 01:24:30 +00:00
|
|
|
start_unregistering(header);
|
|
|
|
if (!--header->count)
|
|
|
|
kfree_rcu(header, rcu);
|
2011-12-29 16:24:29 +00:00
|
|
|
|
|
|
|
if (parent)
|
|
|
|
drop_sysctl_table(&parent->header);
|
2012-01-10 01:24:30 +00:00
|
|
|
}
|
|
|
|
|
2012-01-06 12:07:15 +00:00
|
|
|
/**
|
|
|
|
* unregister_sysctl_table - unregister a sysctl table hierarchy
|
|
|
|
* @header: the header returned from register_sysctl_table
|
|
|
|
*
|
|
|
|
* Unregisters the sysctl table and all children. proc entries may not
|
|
|
|
* actually be removed until they are no longer used by anyone.
|
|
|
|
*/
|
|
|
|
void unregister_sysctl_table(struct ctl_table_header * header)
|
|
|
|
{
|
2012-01-23 02:22:05 +00:00
|
|
|
int nr_subheaders;
|
2012-01-06 12:07:15 +00:00
|
|
|
might_sleep();
|
|
|
|
|
|
|
|
if (header == NULL)
|
|
|
|
return;
|
|
|
|
|
2012-01-23 02:22:05 +00:00
|
|
|
nr_subheaders = count_subheaders(header->ctl_table_arg);
|
|
|
|
if (unlikely(nr_subheaders > 1)) {
|
|
|
|
struct ctl_table_header **subheaders;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
subheaders = (struct ctl_table_header **)(header + 1);
|
|
|
|
for (i = nr_subheaders -1; i >= 0; i--) {
|
|
|
|
struct ctl_table_header *subh = subheaders[i];
|
|
|
|
struct ctl_table *table = subh->ctl_table_arg;
|
|
|
|
unregister_sysctl_table(subh);
|
|
|
|
kfree(table);
|
|
|
|
}
|
|
|
|
kfree(header);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-06 12:07:15 +00:00
|
|
|
spin_lock(&sysctl_lock);
|
2012-01-10 01:24:30 +00:00
|
|
|
drop_sysctl_table(header);
|
2012-01-06 12:07:15 +00:00
|
|
|
spin_unlock(&sysctl_lock);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(unregister_sysctl_table);
|
|
|
|
|
2012-01-08 07:24:30 +00:00
|
|
|
void setup_sysctl_set(struct ctl_table_set *set,
|
2012-01-23 05:26:00 +00:00
|
|
|
struct ctl_table_root *root,
|
2012-01-06 12:07:15 +00:00
|
|
|
int (*is_seen)(struct ctl_table_set *))
|
|
|
|
{
|
2012-01-30 13:40:29 +00:00
|
|
|
memset(set, 0, sizeof(*set));
|
2012-01-08 07:24:30 +00:00
|
|
|
set->is_seen = is_seen;
|
2012-01-10 01:24:30 +00:00
|
|
|
init_header(&set->dir.header, root, set, NULL, root_table);
|
2012-01-06 12:07:15 +00:00
|
|
|
}
|
|
|
|
|
2012-01-10 06:19:13 +00:00
|
|
|
void retire_sysctl_set(struct ctl_table_set *set)
|
|
|
|
{
|
2012-01-10 01:24:30 +00:00
|
|
|
WARN_ON(!RB_EMPTY_ROOT(&set->dir.root));
|
2012-01-10 06:19:13 +00:00
|
|
|
}
|
2012-01-06 12:07:15 +00:00
|
|
|
|
2008-10-17 01:07:44 +00:00
|
|
|
int __init proc_sys_init(void)
|
2007-02-14 08:34:12 +00:00
|
|
|
{
|
2008-10-02 20:23:32 +00:00
|
|
|
struct proc_dir_entry *proc_sys_root;
|
|
|
|
|
2007-02-14 08:34:12 +00:00
|
|
|
proc_sys_root = proc_mkdir("sys", NULL);
|
2008-07-15 12:54:06 +00:00
|
|
|
proc_sys_root->proc_iops = &proc_sys_dir_operations;
|
|
|
|
proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
|
2007-02-14 08:34:12 +00:00
|
|
|
proc_sys_root->nlink = 0;
|
2012-01-06 11:34:20 +00:00
|
|
|
|
|
|
|
return sysctl_init();
|
2007-02-14 08:34:12 +00:00
|
|
|
}
|