blob: 27e265ba1afed9268d684db4edc4684e4e357922 [file] [log] [blame]
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001/*
2 * /proc/sys support
3 */
Alexey Dobriyan1e0edd32008-10-17 05:07:44 +04004#include <linux/init.h>
Eric W. Biederman77b14db2007-02-14 00:34:12 -08005#include <linux/sysctl.h>
Lucas De Marchif1ecf062011-11-02 13:39:22 -07006#include <linux/poll.h>
Eric W. Biederman77b14db2007-02-14 00:34:12 -08007#include <linux/proc_fs.h>
8#include <linux/security.h>
Nick Piggin34286d62011-01-07 17:49:57 +11009#include <linux/namei.h>
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -080010#include <linux/module.h>
Eric W. Biederman77b14db2007-02-14 00:34:12 -080011#include "internal.h"
12
Al Virod72f71e2009-02-20 05:58:47 +000013static const struct dentry_operations proc_sys_dentry_operations;
Eric W. Biederman77b14db2007-02-14 00:34:12 -080014static const struct file_operations proc_sys_file_operations;
Jan Engelhardt03a44822008-02-08 04:21:19 -080015static const struct inode_operations proc_sys_inode_operations;
Al Viro9043476f2008-07-15 08:54:06 -040016static const struct file_operations proc_sys_dir_file_operations;
17static const struct inode_operations proc_sys_dir_operations;
Eric W. Biederman77b14db2007-02-14 00:34:12 -080018
Lucas De Marchif1ecf062011-11-02 13:39:22 -070019void proc_sys_poll_notify(struct ctl_table_poll *poll)
20{
21 if (!poll)
22 return;
23
24 atomic_inc(&poll->event);
25 wake_up_interruptible(&poll->wait);
26}
27
Eric W. Biedermana1945582012-01-21 17:51:48 -080028static struct ctl_table root_table[] = {
29 {
30 .procname = "",
Eric W. Biederman7ec66d02011-12-29 08:24:29 -080031 .mode = S_IFDIR|S_IRUGO|S_IXUGO,
Eric W. Biedermana1945582012-01-21 17:51:48 -080032 },
33 { }
34};
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -080035static struct ctl_table_root sysctl_table_root = {
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -080036 .default_set.dir.header = {
Eric W. Biederman7ec66d02011-12-29 08:24:29 -080037 {{.count = 1,
38 .nreg = 1,
Eric W. Biedermanac13ac62012-01-09 17:24:30 -080039 .ctl_table = root_table }},
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -080040 .ctl_table_arg = root_table,
Eric W. Biederman7ec66d02011-12-29 08:24:29 -080041 .root = &sysctl_table_root,
42 .set = &sysctl_table_root.default_set,
43 },
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -080044};
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -080045
46static DEFINE_SPINLOCK(sysctl_lock);
47
Eric W. Biederman7ec66d02011-12-29 08:24:29 -080048static void drop_sysctl_table(struct ctl_table_header *header);
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -080049static int sysctl_follow_link(struct ctl_table_header **phead,
50 struct ctl_table **pentry, struct nsproxy *namespaces);
51static int insert_links(struct ctl_table_header *head);
52static void put_links(struct ctl_table_header *header);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -080053
Eric W. Biederman69801282012-01-21 20:09:45 -080054static void sysctl_print_dir(struct ctl_dir *dir)
55{
56 if (dir->header.parent)
57 sysctl_print_dir(dir->header.parent);
58 printk(KERN_CONT "%s/", dir->header.ctl_table[0].procname);
59}
60
Eric W. Biederman076c3ee2012-01-09 21:42:02 -080061static int namecmp(const char *name1, int len1, const char *name2, int len2)
62{
63 int minlen;
64 int cmp;
65
66 minlen = len1;
67 if (minlen > len2)
68 minlen = len2;
69
70 cmp = memcmp(name1, name2, minlen);
71 if (cmp == 0)
72 cmp = len1 - len2;
73 return cmp;
74}
75
76static struct ctl_table *find_entry(struct ctl_table_header **phead,
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -080077 struct ctl_dir *dir, const char *name, int namelen)
Eric W. Biederman076c3ee2012-01-09 21:42:02 -080078{
79 struct ctl_table_header *head;
80 struct ctl_table *entry;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -080081 struct rb_node *node = dir->root.rb_node;
Eric W. Biederman076c3ee2012-01-09 21:42:02 -080082
Eric W. Biedermanac13ac62012-01-09 17:24:30 -080083 while (node)
84 {
85 struct ctl_node *ctl_node;
86 const char *procname;
87 int cmp;
88
89 ctl_node = rb_entry(node, struct ctl_node, node);
90 head = ctl_node->header;
91 entry = &head->ctl_table[ctl_node - head->node];
92 procname = entry->procname;
93
94 cmp = namecmp(name, namelen, procname, strlen(procname));
95 if (cmp < 0)
96 node = node->rb_left;
97 else if (cmp > 0)
98 node = node->rb_right;
99 else {
100 *phead = head;
101 return entry;
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800102 }
103 }
104 return NULL;
105}
106
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800107static int insert_entry(struct ctl_table_header *head, struct ctl_table *entry)
108{
109 struct rb_node *node = &head->node[entry - head->ctl_table].node;
110 struct rb_node **p = &head->parent->root.rb_node;
111 struct rb_node *parent = NULL;
112 const char *name = entry->procname;
113 int namelen = strlen(name);
114
115 while (*p) {
116 struct ctl_table_header *parent_head;
117 struct ctl_table *parent_entry;
118 struct ctl_node *parent_node;
119 const char *parent_name;
120 int cmp;
121
122 parent = *p;
123 parent_node = rb_entry(parent, struct ctl_node, node);
124 parent_head = parent_node->header;
125 parent_entry = &parent_head->ctl_table[parent_node - parent_head->node];
126 parent_name = parent_entry->procname;
127
128 cmp = namecmp(name, namelen, parent_name, strlen(parent_name));
129 if (cmp < 0)
130 p = &(*p)->rb_left;
131 else if (cmp > 0)
132 p = &(*p)->rb_right;
133 else {
134 printk(KERN_ERR "sysctl duplicate entry: ");
135 sysctl_print_dir(head->parent);
136 printk(KERN_CONT "/%s\n", entry->procname);
137 return -EEXIST;
138 }
139 }
140
141 rb_link_node(node, parent, p);
142 return 0;
143}
144
145static void erase_entry(struct ctl_table_header *head, struct ctl_table *entry)
146{
147 struct rb_node *node = &head->node[entry - head->ctl_table].node;
148
149 rb_erase(node, &head->parent->root);
150}
151
Eric W. Biedermane0d04522012-01-09 22:36:41 -0800152static void init_header(struct ctl_table_header *head,
153 struct ctl_table_root *root, struct ctl_table_set *set,
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800154 struct ctl_node *node, struct ctl_table *table)
Eric W. Biedermane0d04522012-01-09 22:36:41 -0800155{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800156 head->ctl_table = table;
Eric W. Biedermane0d04522012-01-09 22:36:41 -0800157 head->ctl_table_arg = table;
Eric W. Biedermane0d04522012-01-09 22:36:41 -0800158 head->used = 0;
159 head->count = 1;
160 head->nreg = 1;
161 head->unregistering = NULL;
162 head->root = root;
163 head->set = set;
164 head->parent = NULL;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800165 head->node = node;
166 if (node) {
167 struct ctl_table *entry;
168 for (entry = table; entry->procname; entry++, node++) {
169 rb_init_node(&node->node);
170 node->header = head;
171 }
172 }
Eric W. Biedermane0d04522012-01-09 22:36:41 -0800173}
174
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800175static void erase_header(struct ctl_table_header *head)
176{
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800177 struct ctl_table *entry;
178 for (entry = head->ctl_table; entry->procname; entry++)
179 erase_entry(head, entry);
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800180}
181
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -0800182static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800183{
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800184 struct ctl_table *entry;
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -0800185 int err;
186
187 dir->header.nreg++;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800188 header->parent = dir;
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -0800189 err = insert_links(header);
190 if (err)
191 goto fail_links;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800192 for (entry = header->ctl_table; entry->procname; entry++) {
193 err = insert_entry(header, entry);
194 if (err)
195 goto fail;
196 }
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -0800197 return 0;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800198fail:
199 erase_header(header);
200 put_links(header);
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -0800201fail_links:
202 header->parent = NULL;
203 drop_sysctl_table(&dir->header);
204 return err;
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800205}
206
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800207/* called under sysctl_lock */
208static int use_table(struct ctl_table_header *p)
209{
210 if (unlikely(p->unregistering))
211 return 0;
212 p->used++;
213 return 1;
214}
215
216/* called under sysctl_lock */
217static void unuse_table(struct ctl_table_header *p)
218{
219 if (!--p->used)
220 if (unlikely(p->unregistering))
221 complete(p->unregistering);
222}
223
224/* called under sysctl_lock, will reacquire if has to wait */
225static void start_unregistering(struct ctl_table_header *p)
226{
227 /*
228 * if p->used is 0, nobody will ever touch that entry again;
229 * we'll eliminate all paths to it before dropping sysctl_lock
230 */
231 if (unlikely(p->used)) {
232 struct completion wait;
233 init_completion(&wait);
234 p->unregistering = &wait;
235 spin_unlock(&sysctl_lock);
236 wait_for_completion(&wait);
237 spin_lock(&sysctl_lock);
238 } else {
239 /* anything non-NULL; we'll never dereference it */
240 p->unregistering = ERR_PTR(-EINVAL);
241 }
242 /*
243 * do not remove from the list until nobody holds it; walking the
244 * list in do_sysctl() relies on that.
245 */
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800246 erase_header(p);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800247}
248
249static void sysctl_head_get(struct ctl_table_header *head)
250{
251 spin_lock(&sysctl_lock);
252 head->count++;
253 spin_unlock(&sysctl_lock);
254}
255
256void sysctl_head_put(struct ctl_table_header *head)
257{
258 spin_lock(&sysctl_lock);
259 if (!--head->count)
260 kfree_rcu(head, rcu);
261 spin_unlock(&sysctl_lock);
262}
263
264static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
265{
266 if (!head)
267 BUG();
268 spin_lock(&sysctl_lock);
269 if (!use_table(head))
270 head = ERR_PTR(-ENOENT);
271 spin_unlock(&sysctl_lock);
272 return head;
273}
274
275static void sysctl_head_finish(struct ctl_table_header *head)
276{
277 if (!head)
278 return;
279 spin_lock(&sysctl_lock);
280 unuse_table(head);
281 spin_unlock(&sysctl_lock);
282}
283
284static struct ctl_table_set *
285lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
286{
287 struct ctl_table_set *set = &root->default_set;
288 if (root->lookup)
289 set = root->lookup(root, namespaces);
290 return set;
291}
292
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800293static struct ctl_table *lookup_entry(struct ctl_table_header **phead,
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800294 struct ctl_dir *dir,
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800295 const char *name, int namelen)
296{
297 struct ctl_table_header *head;
298 struct ctl_table *entry;
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800299
300 spin_lock(&sysctl_lock);
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -0800301 entry = find_entry(&head, dir, name, namelen);
302 if (entry && use_table(head))
303 *phead = head;
304 else
305 entry = NULL;
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800306 spin_unlock(&sysctl_lock);
307 return entry;
308}
309
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800310static struct ctl_node *first_usable_entry(struct rb_node *node)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800311{
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800312 struct ctl_node *ctl_node;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800313
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800314 for (;node; node = rb_next(node)) {
315 ctl_node = rb_entry(node, struct ctl_node, node);
316 if (use_table(ctl_node->header))
317 return ctl_node;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800318 }
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800319 return NULL;
320}
321
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800322static void first_entry(struct ctl_dir *dir,
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800323 struct ctl_table_header **phead, struct ctl_table **pentry)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800324{
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800325 struct ctl_table_header *head = NULL;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800326 struct ctl_table *entry = NULL;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800327 struct ctl_node *ctl_node;
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800328
329 spin_lock(&sysctl_lock);
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800330 ctl_node = first_usable_entry(rb_first(&dir->root));
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800331 spin_unlock(&sysctl_lock);
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800332 if (ctl_node) {
333 head = ctl_node->header;
334 entry = &head->ctl_table[ctl_node - head->node];
335 }
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800336 *phead = head;
337 *pentry = entry;
338}
339
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800340static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry)
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800341{
342 struct ctl_table_header *head = *phead;
343 struct ctl_table *entry = *pentry;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800344 struct ctl_node *ctl_node = &head->node[entry - head->ctl_table];
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800345
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800346 spin_lock(&sysctl_lock);
347 unuse_table(head);
348
349 ctl_node = first_usable_entry(rb_next(&ctl_node->node));
350 spin_unlock(&sysctl_lock);
351 head = NULL;
352 if (ctl_node) {
353 head = ctl_node->header;
354 entry = &head->ctl_table[ctl_node - head->node];
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800355 }
356 *phead = head;
357 *pentry = entry;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800358}
359
360void register_sysctl_root(struct ctl_table_root *root)
361{
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800362}
363
364/*
365 * sysctl_perm does NOT grant the superuser all rights automatically, because
366 * some sysctl variables are readonly even to root.
367 */
368
369static int test_perm(int mode, int op)
370{
371 if (!current_euid())
372 mode >>= 6;
373 else if (in_egroup_p(0))
374 mode >>= 3;
375 if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
376 return 0;
377 return -EACCES;
378}
379
380static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op)
381{
382 int mode;
383
384 if (root->permissions)
385 mode = root->permissions(root, current->nsproxy, table);
386 else
387 mode = table->mode;
388
389 return test_perm(mode, op);
390}
391
Al Viro9043476f2008-07-15 08:54:06 -0400392static struct inode *proc_sys_make_inode(struct super_block *sb,
393 struct ctl_table_header *head, struct ctl_table *table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800394{
395 struct inode *inode;
Al Viro9043476f2008-07-15 08:54:06 -0400396 struct proc_inode *ei;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800397
Al Viro9043476f2008-07-15 08:54:06 -0400398 inode = new_inode(sb);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800399 if (!inode)
400 goto out;
401
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400402 inode->i_ino = get_next_ino();
403
Al Viro9043476f2008-07-15 08:54:06 -0400404 sysctl_head_get(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800405 ei = PROC_I(inode);
Al Viro9043476f2008-07-15 08:54:06 -0400406 ei->sysctl = head;
407 ei->sysctl_entry = table;
408
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800409 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Al Viro9043476f2008-07-15 08:54:06 -0400410 inode->i_mode = table->mode;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800411 if (!S_ISDIR(table->mode)) {
Al Viro9043476f2008-07-15 08:54:06 -0400412 inode->i_mode |= S_IFREG;
413 inode->i_op = &proc_sys_inode_operations;
414 inode->i_fop = &proc_sys_file_operations;
415 } else {
416 inode->i_mode |= S_IFDIR;
Al Viro9043476f2008-07-15 08:54:06 -0400417 inode->i_op = &proc_sys_dir_operations;
418 inode->i_fop = &proc_sys_dir_file_operations;
419 }
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800420out:
421 return inode;
422}
423
Adrian Bunk81324362008-10-03 00:33:54 +0400424static struct ctl_table_header *grab_header(struct inode *inode)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800425{
Eric W. Biederman3cc3e042012-01-07 06:57:47 -0800426 struct ctl_table_header *head = PROC_I(inode)->sysctl;
427 if (!head)
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -0800428 head = &sysctl_table_root.default_set.dir.header;
Eric W. Biederman3cc3e042012-01-07 06:57:47 -0800429 return sysctl_head_grab(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800430}
431
432static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
433 struct nameidata *nd)
434{
Al Viro9043476f2008-07-15 08:54:06 -0400435 struct ctl_table_header *head = grab_header(dir);
Al Viro9043476f2008-07-15 08:54:06 -0400436 struct ctl_table_header *h = NULL;
437 struct qstr *name = &dentry->d_name;
438 struct ctl_table *p;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800439 struct inode *inode;
Al Viro9043476f2008-07-15 08:54:06 -0400440 struct dentry *err = ERR_PTR(-ENOENT);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800441 struct ctl_dir *ctl_dir;
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -0800442 int ret;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800443
Al Viro9043476f2008-07-15 08:54:06 -0400444 if (IS_ERR(head))
445 return ERR_CAST(head);
446
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800447 ctl_dir = container_of(head, struct ctl_dir, header);
Al Viro9043476f2008-07-15 08:54:06 -0400448
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800449 p = lookup_entry(&h, ctl_dir, name->name, name->len);
Al Viro9043476f2008-07-15 08:54:06 -0400450 if (!p)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800451 goto out;
452
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -0800453 ret = sysctl_follow_link(&h, &p, current->nsproxy);
454 err = ERR_PTR(ret);
455 if (ret)
456 goto out;
457
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800458 err = ERR_PTR(-ENOMEM);
Al Viro9043476f2008-07-15 08:54:06 -0400459 inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
460 if (h)
461 sysctl_head_finish(h);
462
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800463 if (!inode)
464 goto out;
465
466 err = NULL;
Nick Pigginfb045ad2011-01-07 17:49:55 +1100467 d_set_d_op(dentry, &proc_sys_dentry_operations);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800468 d_add(dentry, inode);
469
470out:
471 sysctl_head_finish(head);
472 return err;
473}
474
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700475static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
476 size_t count, loff_t *ppos, int write)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800477{
Al Viro9043476f2008-07-15 08:54:06 -0400478 struct inode *inode = filp->f_path.dentry->d_inode;
479 struct ctl_table_header *head = grab_header(inode);
480 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
David Howells2a2da532007-10-25 15:27:40 +0100481 ssize_t error;
482 size_t res;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800483
Al Viro9043476f2008-07-15 08:54:06 -0400484 if (IS_ERR(head))
485 return PTR_ERR(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800486
487 /*
488 * At this point we know that the sysctl was not unregistered
489 * and won't be until we finish.
490 */
491 error = -EPERM;
Pavel Emelyanovd7321cd2008-04-29 01:02:44 -0700492 if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800493 goto out;
494
Al Viro9043476f2008-07-15 08:54:06 -0400495 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
496 error = -EINVAL;
497 if (!table->proc_handler)
498 goto out;
499
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800500 /* careful: calling conventions are nasty here */
501 res = count;
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700502 error = table->proc_handler(table, write, buf, &res, ppos);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800503 if (!error)
504 error = res;
505out:
506 sysctl_head_finish(head);
507
508 return error;
509}
510
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700511static ssize_t proc_sys_read(struct file *filp, char __user *buf,
512 size_t count, loff_t *ppos)
513{
514 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
515}
516
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800517static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
518 size_t count, loff_t *ppos)
519{
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700520 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800521}
522
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700523static int proc_sys_open(struct inode *inode, struct file *filp)
524{
525 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
526
527 if (table->poll)
528 filp->private_data = proc_sys_poll_event(table->poll);
529
530 return 0;
531}
532
533static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
534{
535 struct inode *inode = filp->f_path.dentry->d_inode;
536 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
537 unsigned long event = (unsigned long)filp->private_data;
538 unsigned int ret = DEFAULT_POLLMASK;
539
540 if (!table->proc_handler)
541 goto out;
542
543 if (!table->poll)
544 goto out;
545
546 poll_wait(filp, &table->poll->wait, wait);
547
548 if (event != atomic_read(&table->poll->event)) {
549 filp->private_data = proc_sys_poll_event(table->poll);
550 ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
551 }
552
553out:
554 return ret;
555}
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800556
557static int proc_sys_fill_cache(struct file *filp, void *dirent,
Al Viro9043476f2008-07-15 08:54:06 -0400558 filldir_t filldir,
559 struct ctl_table_header *head,
560 struct ctl_table *table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800561{
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800562 struct dentry *child, *dir = filp->f_path.dentry;
563 struct inode *inode;
564 struct qstr qname;
565 ino_t ino = 0;
566 unsigned type = DT_UNKNOWN;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800567
568 qname.name = table->procname;
569 qname.len = strlen(table->procname);
570 qname.hash = full_name_hash(qname.name, qname.len);
571
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800572 child = d_lookup(dir, &qname);
573 if (!child) {
Al Viro9043476f2008-07-15 08:54:06 -0400574 child = d_alloc(dir, &qname);
575 if (child) {
576 inode = proc_sys_make_inode(dir->d_sb, head, table);
577 if (!inode) {
578 dput(child);
579 return -ENOMEM;
580 } else {
Nick Pigginfb045ad2011-01-07 17:49:55 +1100581 d_set_d_op(child, &proc_sys_dentry_operations);
Al Viro9043476f2008-07-15 08:54:06 -0400582 d_add(child, inode);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800583 }
Al Viro9043476f2008-07-15 08:54:06 -0400584 } else {
585 return -ENOMEM;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800586 }
587 }
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800588 inode = child->d_inode;
Al Viro9043476f2008-07-15 08:54:06 -0400589 ino = inode->i_ino;
590 type = inode->i_mode >> 12;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800591 dput(child);
Al Viro9043476f2008-07-15 08:54:06 -0400592 return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
593}
594
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -0800595static int proc_sys_link_fill_cache(struct file *filp, void *dirent,
596 filldir_t filldir,
597 struct ctl_table_header *head,
598 struct ctl_table *table)
599{
600 int err, ret = 0;
601 head = sysctl_head_grab(head);
602
603 /* It is not an error if we can not follow the link ignore it */
604 err = sysctl_follow_link(&head, &table, current->nsproxy);
605 if (err)
606 goto out;
607
608 ret = proc_sys_fill_cache(filp, dirent, filldir, head, table);
609out:
610 sysctl_head_finish(head);
611 return ret;
612}
613
Al Viro9043476f2008-07-15 08:54:06 -0400614static int scan(struct ctl_table_header *head, ctl_table *table,
615 unsigned long *pos, struct file *file,
616 void *dirent, filldir_t filldir)
617{
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800618 int res;
Al Viro9043476f2008-07-15 08:54:06 -0400619
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800620 if ((*pos)++ < file->f_pos)
621 return 0;
Al Viro9043476f2008-07-15 08:54:06 -0400622
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -0800623 if (unlikely(S_ISLNK(table->mode)))
624 res = proc_sys_link_fill_cache(file, dirent, filldir, head, table);
625 else
626 res = proc_sys_fill_cache(file, dirent, filldir, head, table);
Al Viro9043476f2008-07-15 08:54:06 -0400627
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800628 if (res == 0)
629 file->f_pos = *pos;
Al Viro9043476f2008-07-15 08:54:06 -0400630
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800631 return res;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800632}
633
634static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
635{
Al Viro9043476f2008-07-15 08:54:06 -0400636 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800637 struct inode *inode = dentry->d_inode;
Al Viro9043476f2008-07-15 08:54:06 -0400638 struct ctl_table_header *head = grab_header(inode);
Al Viro9043476f2008-07-15 08:54:06 -0400639 struct ctl_table_header *h = NULL;
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800640 struct ctl_table *entry;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800641 struct ctl_dir *ctl_dir;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800642 unsigned long pos;
Al Viro9043476f2008-07-15 08:54:06 -0400643 int ret = -EINVAL;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800644
Al Viro9043476f2008-07-15 08:54:06 -0400645 if (IS_ERR(head))
646 return PTR_ERR(head);
647
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800648 ctl_dir = container_of(head, struct ctl_dir, header);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800649
650 ret = 0;
651 /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
652 if (filp->f_pos == 0) {
653 if (filldir(dirent, ".", 1, filp->f_pos,
654 inode->i_ino, DT_DIR) < 0)
655 goto out;
656 filp->f_pos++;
657 }
658 if (filp->f_pos == 1) {
659 if (filldir(dirent, "..", 2, filp->f_pos,
660 parent_ino(dentry), DT_DIR) < 0)
661 goto out;
662 filp->f_pos++;
663 }
664 pos = 2;
665
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800666 for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) {
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800667 ret = scan(h, entry, &pos, filp, dirent, filldir);
Al Viro9043476f2008-07-15 08:54:06 -0400668 if (ret) {
669 sysctl_head_finish(h);
670 break;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800671 }
672 }
673 ret = 1;
674out:
675 sysctl_head_finish(head);
676 return ret;
677}
678
Al Viro10556cb22011-06-20 19:28:19 -0400679static int proc_sys_permission(struct inode *inode, int mask)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800680{
681 /*
682 * sysctl entries that are not writeable,
683 * are _NOT_ writeable, capabilities or not.
684 */
Miklos Szeredif696a362008-07-31 13:41:58 +0200685 struct ctl_table_header *head;
686 struct ctl_table *table;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800687 int error;
688
Miklos Szeredif696a362008-07-31 13:41:58 +0200689 /* Executable files are not allowed under /proc/sys/ */
690 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
691 return -EACCES;
692
693 head = grab_header(inode);
Al Viro9043476f2008-07-15 08:54:06 -0400694 if (IS_ERR(head))
695 return PTR_ERR(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800696
Miklos Szeredif696a362008-07-31 13:41:58 +0200697 table = PROC_I(inode)->sysctl_entry;
Al Viro9043476f2008-07-15 08:54:06 -0400698 if (!table) /* global root - r-xr-xr-x */
699 error = mask & MAY_WRITE ? -EACCES : 0;
700 else /* Use the permissions on the sysctl table entry */
Al Viro1fc0f782011-06-20 18:59:02 -0400701 error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800702
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800703 sysctl_head_finish(head);
704 return error;
705}
706
707static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
708{
709 struct inode *inode = dentry->d_inode;
710 int error;
711
712 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
713 return -EPERM;
714
715 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200716 if (error)
717 return error;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800718
Christoph Hellwig10257742010-06-04 11:30:02 +0200719 if ((attr->ia_valid & ATTR_SIZE) &&
720 attr->ia_size != i_size_read(inode)) {
721 error = vmtruncate(inode, attr->ia_size);
722 if (error)
723 return error;
724 }
725
726 setattr_copy(inode, attr);
727 mark_inode_dirty(inode);
728 return 0;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800729}
730
Al Viro9043476f2008-07-15 08:54:06 -0400731static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
732{
733 struct inode *inode = dentry->d_inode;
734 struct ctl_table_header *head = grab_header(inode);
735 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
736
737 if (IS_ERR(head))
738 return PTR_ERR(head);
739
740 generic_fillattr(inode, stat);
741 if (table)
742 stat->mode = (stat->mode & S_IFMT) | table->mode;
743
744 sysctl_head_finish(head);
745 return 0;
746}
747
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800748static const struct file_operations proc_sys_file_operations = {
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700749 .open = proc_sys_open,
750 .poll = proc_sys_poll,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800751 .read = proc_sys_read,
752 .write = proc_sys_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200753 .llseek = default_llseek,
Al Viro9043476f2008-07-15 08:54:06 -0400754};
755
756static const struct file_operations proc_sys_dir_file_operations = {
Pavel Emelyanov887df072011-11-02 13:38:42 -0700757 .read = generic_read_dir,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800758 .readdir = proc_sys_readdir,
Christoph Hellwig3222a3e2008-09-03 21:53:01 +0200759 .llseek = generic_file_llseek,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800760};
761
Jan Engelhardt03a44822008-02-08 04:21:19 -0800762static const struct inode_operations proc_sys_inode_operations = {
Al Viro9043476f2008-07-15 08:54:06 -0400763 .permission = proc_sys_permission,
764 .setattr = proc_sys_setattr,
765 .getattr = proc_sys_getattr,
766};
767
768static const struct inode_operations proc_sys_dir_operations = {
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800769 .lookup = proc_sys_lookup,
770 .permission = proc_sys_permission,
771 .setattr = proc_sys_setattr,
Al Viro9043476f2008-07-15 08:54:06 -0400772 .getattr = proc_sys_getattr,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800773};
774
775static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
776{
Nick Piggin34286d62011-01-07 17:49:57 +1100777 if (nd->flags & LOOKUP_RCU)
778 return -ECHILD;
Al Viro9043476f2008-07-15 08:54:06 -0400779 return !PROC_I(dentry->d_inode)->sysctl->unregistering;
780}
781
Nick Pigginfe15ce42011-01-07 17:49:23 +1100782static int proc_sys_delete(const struct dentry *dentry)
Al Viro9043476f2008-07-15 08:54:06 -0400783{
784 return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
785}
786
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800787static int sysctl_is_seen(struct ctl_table_header *p)
788{
789 struct ctl_table_set *set = p->set;
790 int res;
791 spin_lock(&sysctl_lock);
792 if (p->unregistering)
793 res = 0;
794 else if (!set->is_seen)
795 res = 1;
796 else
797 res = set->is_seen(set);
798 spin_unlock(&sysctl_lock);
799 return res;
800}
801
Nick Piggin621e1552011-01-07 17:49:27 +1100802static int proc_sys_compare(const struct dentry *parent,
803 const struct inode *pinode,
804 const struct dentry *dentry, const struct inode *inode,
805 unsigned int len, const char *str, const struct qstr *name)
Al Viro9043476f2008-07-15 08:54:06 -0400806{
Al Virodfef6dcd32011-03-08 01:25:28 -0500807 struct ctl_table_header *head;
Nick Piggin31e6b012011-01-07 17:49:52 +1100808 /* Although proc doesn't have negative dentries, rcu-walk means
809 * that inode here can be NULL */
Al Virodfef6dcd32011-03-08 01:25:28 -0500810 /* AV: can it, indeed? */
Nick Piggin31e6b012011-01-07 17:49:52 +1100811 if (!inode)
Al Virodfef6dcd32011-03-08 01:25:28 -0500812 return 1;
Nick Piggin621e1552011-01-07 17:49:27 +1100813 if (name->len != len)
Al Viro9043476f2008-07-15 08:54:06 -0400814 return 1;
Nick Piggin621e1552011-01-07 17:49:27 +1100815 if (memcmp(name->name, str, len))
Al Viro9043476f2008-07-15 08:54:06 -0400816 return 1;
Al Virodfef6dcd32011-03-08 01:25:28 -0500817 head = rcu_dereference(PROC_I(inode)->sysctl);
818 return !head || !sysctl_is_seen(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800819}
820
Al Virod72f71e2009-02-20 05:58:47 +0000821static const struct dentry_operations proc_sys_dentry_operations = {
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800822 .d_revalidate = proc_sys_revalidate,
Al Viro9043476f2008-07-15 08:54:06 -0400823 .d_delete = proc_sys_delete,
824 .d_compare = proc_sys_compare,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800825};
826
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -0800827static struct ctl_dir *find_subdir(struct ctl_dir *dir,
828 const char *name, int namelen)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800829{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800830 struct ctl_table_header *head;
831 struct ctl_table *entry;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800832
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -0800833 entry = find_entry(&head, dir, name, namelen);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800834 if (!entry)
835 return ERR_PTR(-ENOENT);
836 if (S_ISDIR(entry->mode))
837 return container_of(head, struct ctl_dir, header);
838 return ERR_PTR(-ENOTDIR);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800839}
840
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800841static struct ctl_dir *new_dir(struct ctl_table_set *set,
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -0800842 const char *name, int namelen)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800843{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800844 struct ctl_table *table;
845 struct ctl_dir *new;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800846 struct ctl_node *node;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800847 char *new_name;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800848
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800849 new = kzalloc(sizeof(*new) + sizeof(struct ctl_node) +
850 sizeof(struct ctl_table)*2 + namelen + 1,
851 GFP_KERNEL);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800852 if (!new)
853 return NULL;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800854
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800855 node = (struct ctl_node *)(new + 1);
856 table = (struct ctl_table *)(node + 1);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800857 new_name = (char *)(table + 2);
858 memcpy(new_name, name, namelen);
859 new_name[namelen] = '\0';
860 table[0].procname = new_name;
861 table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800862 init_header(&new->header, set->dir.header.root, set, node, table);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800863
864 return new;
865}
866
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -0800867static struct ctl_dir *get_subdir(struct ctl_dir *dir,
868 const char *name, int namelen)
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800869{
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -0800870 struct ctl_table_set *set = dir->header.set;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800871 struct ctl_dir *subdir, *new = NULL;
872
873 spin_lock(&sysctl_lock);
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -0800874 subdir = find_subdir(dir, name, namelen);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800875 if (!IS_ERR(subdir))
876 goto found;
877 if (PTR_ERR(subdir) != -ENOENT)
878 goto failed;
879
880 spin_unlock(&sysctl_lock);
881 new = new_dir(set, name, namelen);
882 spin_lock(&sysctl_lock);
883 subdir = ERR_PTR(-ENOMEM);
884 if (!new)
885 goto failed;
886
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -0800887 subdir = find_subdir(dir, name, namelen);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800888 if (!IS_ERR(subdir))
889 goto found;
890 if (PTR_ERR(subdir) != -ENOENT)
891 goto failed;
892
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -0800893 if (insert_header(dir, &new->header))
894 goto failed;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800895 subdir = new;
896found:
897 subdir->header.nreg++;
898failed:
899 if (unlikely(IS_ERR(subdir))) {
Eric W. Biederman69801282012-01-21 20:09:45 -0800900 printk(KERN_ERR "sysctl could not get directory: ");
901 sysctl_print_dir(dir);
902 printk(KERN_CONT "/%*.*s %ld\n",
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800903 namelen, namelen, name, PTR_ERR(subdir));
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800904 }
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800905 drop_sysctl_table(&dir->header);
906 if (new)
907 drop_sysctl_table(&new->header);
908 spin_unlock(&sysctl_lock);
909 return subdir;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800910}
911
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -0800912static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir)
913{
914 struct ctl_dir *parent;
915 const char *procname;
916 if (!dir->header.parent)
917 return &set->dir;
918 parent = xlate_dir(set, dir->header.parent);
919 if (IS_ERR(parent))
920 return parent;
921 procname = dir->header.ctl_table[0].procname;
922 return find_subdir(parent, procname, strlen(procname));
923}
924
925static int sysctl_follow_link(struct ctl_table_header **phead,
926 struct ctl_table **pentry, struct nsproxy *namespaces)
927{
928 struct ctl_table_header *head;
929 struct ctl_table_root *root;
930 struct ctl_table_set *set;
931 struct ctl_table *entry;
932 struct ctl_dir *dir;
933 int ret;
934
935 /* Get out quickly if not a link */
936 if (!S_ISLNK((*pentry)->mode))
937 return 0;
938
939 ret = 0;
940 spin_lock(&sysctl_lock);
941 root = (*pentry)->data;
942 set = lookup_header_set(root, namespaces);
943 dir = xlate_dir(set, (*phead)->parent);
944 if (IS_ERR(dir))
945 ret = PTR_ERR(dir);
946 else {
947 const char *procname = (*pentry)->procname;
948 head = NULL;
949 entry = find_entry(&head, dir, procname, strlen(procname));
950 ret = -ENOENT;
951 if (entry && use_table(head)) {
952 unuse_table(*phead);
953 *phead = head;
954 *pentry = entry;
955 ret = 0;
956 }
957 }
958
959 spin_unlock(&sysctl_lock);
960 return ret;
961}
962
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800963static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
964{
965 struct va_format vaf;
966 va_list args;
967
968 va_start(args, fmt);
969 vaf.fmt = fmt;
970 vaf.va = &args;
971
972 printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n",
973 path, table->procname, &vaf);
974
975 va_end(args);
976 return -EINVAL;
977}
978
979static int sysctl_check_table(const char *path, struct ctl_table *table)
980{
981 int err = 0;
982 for (; table->procname; table++) {
983 if (table->child)
984 err = sysctl_err(path, table, "Not a file");
985
986 if ((table->proc_handler == proc_dostring) ||
987 (table->proc_handler == proc_dointvec) ||
988 (table->proc_handler == proc_dointvec_minmax) ||
989 (table->proc_handler == proc_dointvec_jiffies) ||
990 (table->proc_handler == proc_dointvec_userhz_jiffies) ||
991 (table->proc_handler == proc_dointvec_ms_jiffies) ||
992 (table->proc_handler == proc_doulongvec_minmax) ||
993 (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
994 if (!table->data)
995 err = sysctl_err(path, table, "No data");
996 if (!table->maxlen)
997 err = sysctl_err(path, table, "No maxlen");
998 }
999 if (!table->proc_handler)
1000 err = sysctl_err(path, table, "No proc_handler");
1001
1002 if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
1003 err = sysctl_err(path, table, "bogus .mode 0%o",
1004 table->mode);
1005 }
1006 return err;
1007}
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001008
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -08001009static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table *table,
1010 struct ctl_table_root *link_root)
1011{
1012 struct ctl_table *link_table, *entry, *link;
1013 struct ctl_table_header *links;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001014 struct ctl_node *node;
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -08001015 char *link_name;
1016 int nr_entries, name_bytes;
1017
1018 name_bytes = 0;
1019 nr_entries = 0;
1020 for (entry = table; entry->procname; entry++) {
1021 nr_entries++;
1022 name_bytes += strlen(entry->procname) + 1;
1023 }
1024
1025 links = kzalloc(sizeof(struct ctl_table_header) +
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001026 sizeof(struct ctl_node)*nr_entries +
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -08001027 sizeof(struct ctl_table)*(nr_entries + 1) +
1028 name_bytes,
1029 GFP_KERNEL);
1030
1031 if (!links)
1032 return NULL;
1033
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001034 node = (struct ctl_node *)(links + 1);
1035 link_table = (struct ctl_table *)(node + nr_entries);
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -08001036 link_name = (char *)&link_table[nr_entries + 1];
1037
1038 for (link = link_table, entry = table; entry->procname; link++, entry++) {
1039 int len = strlen(entry->procname) + 1;
1040 memcpy(link_name, entry->procname, len);
1041 link->procname = link_name;
1042 link->mode = S_IFLNK|S_IRWXUGO;
1043 link->data = link_root;
1044 link_name += len;
1045 }
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001046 init_header(links, dir->header.root, dir->header.set, node, link_table);
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -08001047 links->nreg = nr_entries;
1048
1049 return links;
1050}
1051
1052static bool get_links(struct ctl_dir *dir,
1053 struct ctl_table *table, struct ctl_table_root *link_root)
1054{
1055 struct ctl_table_header *head;
1056 struct ctl_table *entry, *link;
1057
1058 /* Are there links available for every entry in table? */
1059 for (entry = table; entry->procname; entry++) {
1060 const char *procname = entry->procname;
1061 link = find_entry(&head, dir, procname, strlen(procname));
1062 if (!link)
1063 return false;
1064 if (S_ISDIR(link->mode) && S_ISDIR(entry->mode))
1065 continue;
1066 if (S_ISLNK(link->mode) && (link->data == link_root))
1067 continue;
1068 return false;
1069 }
1070
1071 /* The checks passed. Increase the registration count on the links */
1072 for (entry = table; entry->procname; entry++) {
1073 const char *procname = entry->procname;
1074 link = find_entry(&head, dir, procname, strlen(procname));
1075 head->nreg++;
1076 }
1077 return true;
1078}
1079
1080static int insert_links(struct ctl_table_header *head)
1081{
1082 struct ctl_table_set *root_set = &sysctl_table_root.default_set;
1083 struct ctl_dir *core_parent = NULL;
1084 struct ctl_table_header *links;
1085 int err;
1086
1087 if (head->set == root_set)
1088 return 0;
1089
1090 core_parent = xlate_dir(root_set, head->parent);
1091 if (IS_ERR(core_parent))
1092 return 0;
1093
1094 if (get_links(core_parent, head->ctl_table, head->root))
1095 return 0;
1096
1097 core_parent->header.nreg++;
1098 spin_unlock(&sysctl_lock);
1099
1100 links = new_links(core_parent, head->ctl_table, head->root);
1101
1102 spin_lock(&sysctl_lock);
1103 err = -ENOMEM;
1104 if (!links)
1105 goto out;
1106
1107 err = 0;
1108 if (get_links(core_parent, head->ctl_table, head->root)) {
1109 kfree(links);
1110 goto out;
1111 }
1112
1113 err = insert_header(core_parent, links);
1114 if (err)
1115 kfree(links);
1116out:
1117 drop_sysctl_table(&core_parent->header);
1118 return err;
1119}
1120
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001121/**
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001122 * __register_sysctl_table - register a leaf sysctl table
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001123 * @set: Sysctl tree to register on
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001124 * @path: The path to the directory the sysctl table is in.
1125 * @table: the top-level table structure
1126 *
1127 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1128 * array. A completely 0 filled entry terminates the table.
1129 *
1130 * The members of the &struct ctl_table structure are used as follows:
1131 *
1132 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
1133 * enter a sysctl file
1134 *
1135 * data - a pointer to data for use by proc_handler
1136 *
1137 * maxlen - the maximum size in bytes of the data
1138 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001139 * mode - the file permissions for the /proc/sys file
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001140 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001141 * child - must be %NULL.
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001142 *
1143 * proc_handler - the text handler routine (described below)
1144 *
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001145 * extra1, extra2 - extra pointers usable by the proc handler routines
1146 *
1147 * Leaf nodes in the sysctl tree will be represented by a single file
1148 * under /proc; non-leaf nodes will be represented by directories.
1149 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001150 * There must be a proc_handler routine for any terminal nodes.
1151 * Several default handlers are available to cover common cases -
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001152 *
1153 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
1154 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
1155 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
1156 *
1157 * It is the handler's job to read the input buffer from user memory
1158 * and process it. The handler should return 0 on success.
1159 *
1160 * This routine returns %NULL on a failure to register, and a pointer
1161 * to the table header on success.
1162 */
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001163struct ctl_table_header *__register_sysctl_table(
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001164 struct ctl_table_set *set,
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001165 const char *path, struct ctl_table *table)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001166{
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001167 struct ctl_table_root *root = set->dir.header.root;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001168 struct ctl_table_header *header;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001169 const char *name, *nextname;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001170 struct ctl_dir *dir;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001171 struct ctl_table *entry;
1172 struct ctl_node *node;
1173 int nr_entries = 0;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001174
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001175 for (entry = table; entry->procname; entry++)
1176 nr_entries++;
1177
1178 header = kzalloc(sizeof(struct ctl_table_header) +
1179 sizeof(struct ctl_node)*nr_entries, GFP_KERNEL);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001180 if (!header)
1181 return NULL;
1182
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001183 node = (struct ctl_node *)(header + 1);
1184 init_header(header, root, set, node, table);
Eric W. Biederman7c60c482012-01-21 13:34:05 -08001185 if (sysctl_check_table(path, table))
1186 goto fail;
Eric W. Biederman8d6ecfc2012-01-06 11:55:30 -08001187
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001188 spin_lock(&sysctl_lock);
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -08001189 dir = &set->dir;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001190 dir->header.nreg++;
1191 spin_unlock(&sysctl_lock);
1192
1193 /* Find the directory for the ctl_table */
1194 for (name = path; name; name = nextname) {
1195 int namelen;
1196 nextname = strchr(name, '/');
1197 if (nextname) {
1198 namelen = nextname - name;
1199 nextname++;
1200 } else {
1201 namelen = strlen(name);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001202 }
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001203 if (namelen == 0)
1204 continue;
1205
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -08001206 dir = get_subdir(dir, name, namelen);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001207 if (IS_ERR(dir))
1208 goto fail;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001209 }
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -08001210
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001211 spin_lock(&sysctl_lock);
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -08001212 if (insert_header(dir, header))
1213 goto fail_put_dir_locked;
1214
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001215 drop_sysctl_table(&dir->header);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001216 spin_unlock(&sysctl_lock);
1217
1218 return header;
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -08001219
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001220fail_put_dir_locked:
1221 drop_sysctl_table(&dir->header);
Eric W. Biederman7c60c482012-01-21 13:34:05 -08001222 spin_unlock(&sysctl_lock);
1223fail:
1224 kfree(header);
1225 dump_stack();
1226 return NULL;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001227}
1228
Eric W. Biedermanfea478d2012-01-20 21:47:03 -08001229/**
1230 * register_sysctl - register a sysctl table
1231 * @path: The path to the directory the sysctl table is in.
1232 * @table: the table structure
1233 *
1234 * Register a sysctl table. @table should be a filled in ctl_table
1235 * array. A completely 0 filled entry terminates the table.
1236 *
1237 * See __register_sysctl_table for more details.
1238 */
1239struct ctl_table_header *register_sysctl(const char *path, struct ctl_table *table)
1240{
1241 return __register_sysctl_table(&sysctl_table_root.default_set,
1242 path, table);
1243}
1244EXPORT_SYMBOL(register_sysctl);
1245
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001246static char *append_path(const char *path, char *pos, const char *name)
1247{
1248 int namelen;
1249 namelen = strlen(name);
1250 if (((pos - path) + namelen + 2) >= PATH_MAX)
1251 return NULL;
1252 memcpy(pos, name, namelen);
1253 pos[namelen] = '/';
1254 pos[namelen + 1] = '\0';
1255 pos += namelen + 1;
1256 return pos;
1257}
1258
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001259static int count_subheaders(struct ctl_table *table)
1260{
1261 int has_files = 0;
1262 int nr_subheaders = 0;
1263 struct ctl_table *entry;
1264
1265 /* special case: no directory and empty directory */
1266 if (!table || !table->procname)
1267 return 1;
1268
1269 for (entry = table; entry->procname; entry++) {
1270 if (entry->child)
1271 nr_subheaders += count_subheaders(entry->child);
1272 else
1273 has_files = 1;
1274 }
1275 return nr_subheaders + has_files;
1276}
1277
1278static int register_leaf_sysctl_tables(const char *path, char *pos,
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001279 struct ctl_table_header ***subheader, struct ctl_table_set *set,
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001280 struct ctl_table *table)
1281{
1282 struct ctl_table *ctl_table_arg = NULL;
1283 struct ctl_table *entry, *files;
1284 int nr_files = 0;
1285 int nr_dirs = 0;
1286 int err = -ENOMEM;
1287
1288 for (entry = table; entry->procname; entry++) {
1289 if (entry->child)
1290 nr_dirs++;
1291 else
1292 nr_files++;
1293 }
1294
1295 files = table;
1296 /* If there are mixed files and directories we need a new table */
1297 if (nr_dirs && nr_files) {
1298 struct ctl_table *new;
1299 files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1),
1300 GFP_KERNEL);
1301 if (!files)
1302 goto out;
1303
1304 ctl_table_arg = files;
1305 for (new = files, entry = table; entry->procname; entry++) {
1306 if (entry->child)
1307 continue;
1308 *new = *entry;
1309 new++;
1310 }
1311 }
1312
1313 /* Register everything except a directory full of subdirectories */
1314 if (nr_files || !nr_dirs) {
1315 struct ctl_table_header *header;
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001316 header = __register_sysctl_table(set, path, files);
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001317 if (!header) {
1318 kfree(ctl_table_arg);
1319 goto out;
1320 }
1321
1322 /* Remember if we need to free the file table */
1323 header->ctl_table_arg = ctl_table_arg;
1324 **subheader = header;
1325 (*subheader)++;
1326 }
1327
1328 /* Recurse into the subdirectories. */
1329 for (entry = table; entry->procname; entry++) {
1330 char *child_pos;
1331
1332 if (!entry->child)
1333 continue;
1334
1335 err = -ENAMETOOLONG;
1336 child_pos = append_path(path, pos, entry->procname);
1337 if (!child_pos)
1338 goto out;
1339
1340 err = register_leaf_sysctl_tables(path, child_pos, subheader,
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001341 set, entry->child);
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001342 pos[0] = '\0';
1343 if (err)
1344 goto out;
1345 }
1346 err = 0;
1347out:
1348 /* On failure our caller will unregister all registered subheaders */
1349 return err;
1350}
1351
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001352/**
1353 * __register_sysctl_paths - register a sysctl table hierarchy
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001354 * @set: Sysctl tree to register on
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001355 * @path: The path to the directory the sysctl table is in.
1356 * @table: the top-level table structure
1357 *
1358 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1359 * array. A completely 0 filled entry terminates the table.
1360 *
1361 * See __register_sysctl_table for more details.
1362 */
1363struct ctl_table_header *__register_sysctl_paths(
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001364 struct ctl_table_set *set,
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001365 const struct ctl_path *path, struct ctl_table *table)
1366{
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001367 struct ctl_table *ctl_table_arg = table;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001368 int nr_subheaders = count_subheaders(table);
1369 struct ctl_table_header *header = NULL, **subheaders, **subheader;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001370 const struct ctl_path *component;
1371 char *new_path, *pos;
1372
1373 pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
1374 if (!new_path)
1375 return NULL;
1376
1377 pos[0] = '\0';
1378 for (component = path; component->procname; component++) {
1379 pos = append_path(new_path, pos, component->procname);
1380 if (!pos)
1381 goto out;
1382 }
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001383 while (table->procname && table->child && !table[1].procname) {
1384 pos = append_path(new_path, pos, table->procname);
1385 if (!pos)
1386 goto out;
1387 table = table->child;
1388 }
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001389 if (nr_subheaders == 1) {
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001390 header = __register_sysctl_table(set, new_path, table);
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001391 if (header)
1392 header->ctl_table_arg = ctl_table_arg;
1393 } else {
1394 header = kzalloc(sizeof(*header) +
1395 sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
1396 if (!header)
1397 goto out;
1398
1399 subheaders = (struct ctl_table_header **) (header + 1);
1400 subheader = subheaders;
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001401 header->ctl_table_arg = ctl_table_arg;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001402
1403 if (register_leaf_sysctl_tables(new_path, pos, &subheader,
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001404 set, table))
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001405 goto err_register_leaves;
1406 }
1407
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001408out:
1409 kfree(new_path);
1410 return header;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001411
1412err_register_leaves:
1413 while (subheader > subheaders) {
1414 struct ctl_table_header *subh = *(--subheader);
1415 struct ctl_table *table = subh->ctl_table_arg;
1416 unregister_sysctl_table(subh);
1417 kfree(table);
1418 }
1419 kfree(header);
1420 header = NULL;
1421 goto out;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001422}
1423
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001424/**
1425 * register_sysctl_table_path - register a sysctl table hierarchy
1426 * @path: The path to the directory the sysctl table is in.
1427 * @table: the top-level table structure
1428 *
1429 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1430 * array. A completely 0 filled entry terminates the table.
1431 *
1432 * See __register_sysctl_paths for more details.
1433 */
1434struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
1435 struct ctl_table *table)
1436{
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001437 return __register_sysctl_paths(&sysctl_table_root.default_set,
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001438 path, table);
1439}
1440EXPORT_SYMBOL(register_sysctl_paths);
1441
1442/**
1443 * register_sysctl_table - register a sysctl table hierarchy
1444 * @table: the top-level table structure
1445 *
1446 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1447 * array. A completely 0 filled entry terminates the table.
1448 *
1449 * See register_sysctl_paths for more details.
1450 */
1451struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
1452{
1453 static const struct ctl_path null_path[] = { {} };
1454
1455 return register_sysctl_paths(null_path, table);
1456}
1457EXPORT_SYMBOL(register_sysctl_table);
1458
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -08001459static void put_links(struct ctl_table_header *header)
1460{
1461 struct ctl_table_set *root_set = &sysctl_table_root.default_set;
1462 struct ctl_table_root *root = header->root;
1463 struct ctl_dir *parent = header->parent;
1464 struct ctl_dir *core_parent;
1465 struct ctl_table *entry;
1466
1467 if (header->set == root_set)
1468 return;
1469
1470 core_parent = xlate_dir(root_set, parent);
1471 if (IS_ERR(core_parent))
1472 return;
1473
1474 for (entry = header->ctl_table; entry->procname; entry++) {
1475 struct ctl_table_header *link_head;
1476 struct ctl_table *link;
1477 const char *name = entry->procname;
1478
1479 link = find_entry(&link_head, core_parent, name, strlen(name));
1480 if (link &&
1481 ((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) ||
1482 (S_ISLNK(link->mode) && (link->data == root)))) {
1483 drop_sysctl_table(link_head);
1484 }
1485 else {
1486 printk(KERN_ERR "sysctl link missing during unregister: ");
1487 sysctl_print_dir(parent);
1488 printk(KERN_CONT "/%s\n", name);
1489 }
1490 }
1491}
1492
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001493static void drop_sysctl_table(struct ctl_table_header *header)
1494{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001495 struct ctl_dir *parent = header->parent;
1496
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001497 if (--header->nreg)
1498 return;
1499
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -08001500 put_links(header);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001501 start_unregistering(header);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001502 if (!--header->count)
1503 kfree_rcu(header, rcu);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001504
1505 if (parent)
1506 drop_sysctl_table(&parent->header);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001507}
1508
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001509/**
1510 * unregister_sysctl_table - unregister a sysctl table hierarchy
1511 * @header: the header returned from register_sysctl_table
1512 *
1513 * Unregisters the sysctl table and all children. proc entries may not
1514 * actually be removed until they are no longer used by anyone.
1515 */
1516void unregister_sysctl_table(struct ctl_table_header * header)
1517{
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001518 int nr_subheaders;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001519 might_sleep();
1520
1521 if (header == NULL)
1522 return;
1523
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001524 nr_subheaders = count_subheaders(header->ctl_table_arg);
1525 if (unlikely(nr_subheaders > 1)) {
1526 struct ctl_table_header **subheaders;
1527 int i;
1528
1529 subheaders = (struct ctl_table_header **)(header + 1);
1530 for (i = nr_subheaders -1; i >= 0; i--) {
1531 struct ctl_table_header *subh = subheaders[i];
1532 struct ctl_table *table = subh->ctl_table_arg;
1533 unregister_sysctl_table(subh);
1534 kfree(table);
1535 }
1536 kfree(header);
1537 return;
1538 }
1539
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001540 spin_lock(&sysctl_lock);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001541 drop_sysctl_table(header);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001542 spin_unlock(&sysctl_lock);
1543}
1544EXPORT_SYMBOL(unregister_sysctl_table);
1545
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -08001546void setup_sysctl_set(struct ctl_table_set *set,
Eric W. Biederman9eb47c22012-01-22 21:26:00 -08001547 struct ctl_table_root *root,
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001548 int (*is_seen)(struct ctl_table_set *))
1549{
Dan Carpenter13474402012-01-30 16:40:29 +03001550 memset(set, 0, sizeof(*set));
Eric W. Biederman0e47c99d2012-01-07 23:24:30 -08001551 set->is_seen = is_seen;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001552 init_header(&set->dir.header, root, set, NULL, root_table);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001553}
1554
Eric W. Biederman97324cd82012-01-09 22:19:13 -08001555void retire_sysctl_set(struct ctl_table_set *set)
1556{
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001557 WARN_ON(!RB_EMPTY_ROOT(&set->dir.root));
Eric W. Biederman97324cd82012-01-09 22:19:13 -08001558}
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001559
Alexey Dobriyan1e0edd32008-10-17 05:07:44 +04001560int __init proc_sys_init(void)
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001561{
Alexey Dobriyane1675232008-10-03 00:23:32 +04001562 struct proc_dir_entry *proc_sys_root;
1563
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001564 proc_sys_root = proc_mkdir("sys", NULL);
Al Viro9043476f2008-07-15 08:54:06 -04001565 proc_sys_root->proc_iops = &proc_sys_dir_operations;
1566 proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001567 proc_sys_root->nlink = 0;
Eric W. Biedermande4e83bd2012-01-06 03:34:20 -08001568
1569 return sysctl_init();
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001570}