blob: fddc507296323e747a3811e59682a2df31c264d7 [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>
Al Viro40401532012-02-13 03:58:52 +00009#include <linux/sched.h>
Nick Piggin34286d62011-01-07 17:49:57 +110010#include <linux/namei.h>
Al Viro40401532012-02-13 03:58:52 +000011#include <linux/mm.h>
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -080012#include <linux/module.h>
Eric W. Biederman77b14db2007-02-14 00:34:12 -080013#include "internal.h"
14
Al Virod72f71e2009-02-20 05:58:47 +000015static const struct dentry_operations proc_sys_dentry_operations;
Eric W. Biederman77b14db2007-02-14 00:34:12 -080016static const struct file_operations proc_sys_file_operations;
Jan Engelhardt03a44822008-02-08 04:21:19 -080017static const struct inode_operations proc_sys_inode_operations;
Al Viro90434762008-07-15 08:54:06 -040018static const struct file_operations proc_sys_dir_file_operations;
19static const struct inode_operations proc_sys_dir_operations;
Eric W. Biederman77b14db2007-02-14 00:34:12 -080020
Lucas De Marchif1ecf062011-11-02 13:39:22 -070021void proc_sys_poll_notify(struct ctl_table_poll *poll)
22{
23 if (!poll)
24 return;
25
26 atomic_inc(&poll->event);
27 wake_up_interruptible(&poll->wait);
28}
29
Eric W. Biedermana1945582012-01-21 17:51:48 -080030static struct ctl_table root_table[] = {
31 {
32 .procname = "",
Eric W. Biederman7ec66d02011-12-29 08:24:29 -080033 .mode = S_IFDIR|S_IRUGO|S_IXUGO,
Eric W. Biedermana1945582012-01-21 17:51:48 -080034 },
35 { }
36};
Eric W. Biederman0e47c992012-01-07 23:24:30 -080037static struct ctl_table_root sysctl_table_root = {
Eric W. Biederman0e47c992012-01-07 23:24:30 -080038 .default_set.dir.header = {
Eric W. Biederman7ec66d02011-12-29 08:24:29 -080039 {{.count = 1,
40 .nreg = 1,
Eric W. Biedermanac13ac62012-01-09 17:24:30 -080041 .ctl_table = root_table }},
Eric W. Biederman0e47c992012-01-07 23:24:30 -080042 .ctl_table_arg = root_table,
Eric W. Biederman7ec66d02011-12-29 08:24:29 -080043 .root = &sysctl_table_root,
44 .set = &sysctl_table_root.default_set,
45 },
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -080046};
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -080047
48static DEFINE_SPINLOCK(sysctl_lock);
49
Eric W. Biederman7ec66d02011-12-29 08:24:29 -080050static void drop_sysctl_table(struct ctl_table_header *header);
Eric W. Biederman0e47c992012-01-07 23:24:30 -080051static int sysctl_follow_link(struct ctl_table_header **phead,
52 struct ctl_table **pentry, struct nsproxy *namespaces);
53static int insert_links(struct ctl_table_header *head);
54static void put_links(struct ctl_table_header *header);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -080055
Eric W. Biederman69801282012-01-21 20:09:45 -080056static void sysctl_print_dir(struct ctl_dir *dir)
57{
58 if (dir->header.parent)
59 sysctl_print_dir(dir->header.parent);
60 printk(KERN_CONT "%s/", dir->header.ctl_table[0].procname);
61}
62
Eric W. Biederman076c3ee2012-01-09 21:42:02 -080063static int namecmp(const char *name1, int len1, const char *name2, int len2)
64{
65 int minlen;
66 int cmp;
67
68 minlen = len1;
69 if (minlen > len2)
70 minlen = len2;
71
72 cmp = memcmp(name1, name2, minlen);
73 if (cmp == 0)
74 cmp = len1 - len2;
75 return cmp;
76}
77
Eric W. Biederman60f126d2012-01-30 21:23:52 -080078/* Called under sysctl_lock */
Eric W. Biederman076c3ee2012-01-09 21:42:02 -080079static struct ctl_table *find_entry(struct ctl_table_header **phead,
Eric W. Biederman0e47c992012-01-07 23:24:30 -080080 struct ctl_dir *dir, const char *name, int namelen)
Eric W. Biederman076c3ee2012-01-09 21:42:02 -080081{
82 struct ctl_table_header *head;
83 struct ctl_table *entry;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -080084 struct rb_node *node = dir->root.rb_node;
Eric W. Biederman076c3ee2012-01-09 21:42:02 -080085
Eric W. Biedermanac13ac62012-01-09 17:24:30 -080086 while (node)
87 {
88 struct ctl_node *ctl_node;
89 const char *procname;
90 int cmp;
91
92 ctl_node = rb_entry(node, struct ctl_node, node);
93 head = ctl_node->header;
94 entry = &head->ctl_table[ctl_node - head->node];
95 procname = entry->procname;
96
97 cmp = namecmp(name, namelen, procname, strlen(procname));
98 if (cmp < 0)
99 node = node->rb_left;
100 else if (cmp > 0)
101 node = node->rb_right;
102 else {
103 *phead = head;
104 return entry;
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800105 }
106 }
107 return NULL;
108}
109
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800110static int insert_entry(struct ctl_table_header *head, struct ctl_table *entry)
111{
112 struct rb_node *node = &head->node[entry - head->ctl_table].node;
113 struct rb_node **p = &head->parent->root.rb_node;
114 struct rb_node *parent = NULL;
115 const char *name = entry->procname;
116 int namelen = strlen(name);
117
118 while (*p) {
119 struct ctl_table_header *parent_head;
120 struct ctl_table *parent_entry;
121 struct ctl_node *parent_node;
122 const char *parent_name;
123 int cmp;
124
125 parent = *p;
126 parent_node = rb_entry(parent, struct ctl_node, node);
127 parent_head = parent_node->header;
128 parent_entry = &parent_head->ctl_table[parent_node - parent_head->node];
129 parent_name = parent_entry->procname;
130
131 cmp = namecmp(name, namelen, parent_name, strlen(parent_name));
132 if (cmp < 0)
133 p = &(*p)->rb_left;
134 else if (cmp > 0)
135 p = &(*p)->rb_right;
136 else {
137 printk(KERN_ERR "sysctl duplicate entry: ");
138 sysctl_print_dir(head->parent);
139 printk(KERN_CONT "/%s\n", entry->procname);
140 return -EEXIST;
141 }
142 }
143
144 rb_link_node(node, parent, p);
145 return 0;
146}
147
148static void erase_entry(struct ctl_table_header *head, struct ctl_table *entry)
149{
150 struct rb_node *node = &head->node[entry - head->ctl_table].node;
151
152 rb_erase(node, &head->parent->root);
153}
154
Eric W. Biedermane0d04522012-01-09 22:36:41 -0800155static void init_header(struct ctl_table_header *head,
156 struct ctl_table_root *root, struct ctl_table_set *set,
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800157 struct ctl_node *node, struct ctl_table *table)
Eric W. Biedermane0d04522012-01-09 22:36:41 -0800158{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800159 head->ctl_table = table;
Eric W. Biedermane0d04522012-01-09 22:36:41 -0800160 head->ctl_table_arg = table;
Eric W. Biedermane0d04522012-01-09 22:36:41 -0800161 head->used = 0;
162 head->count = 1;
163 head->nreg = 1;
164 head->unregistering = NULL;
165 head->root = root;
166 head->set = set;
167 head->parent = NULL;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800168 head->node = node;
169 if (node) {
170 struct ctl_table *entry;
Michel Lespinasse4c199a92012-10-08 16:30:32 -0700171 for (entry = table; entry->procname; entry++, node++)
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800172 node->header = head;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800173 }
Eric W. Biedermane0d04522012-01-09 22:36:41 -0800174}
175
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800176static void erase_header(struct ctl_table_header *head)
177{
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800178 struct ctl_table *entry;
179 for (entry = head->ctl_table; entry->procname; entry++)
180 erase_entry(head, entry);
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800181}
182
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800183static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800184{
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800185 struct ctl_table *entry;
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800186 int err;
187
188 dir->header.nreg++;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800189 header->parent = dir;
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800190 err = insert_links(header);
191 if (err)
192 goto fail_links;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800193 for (entry = header->ctl_table; entry->procname; entry++) {
194 err = insert_entry(header, entry);
195 if (err)
196 goto fail;
197 }
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800198 return 0;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800199fail:
200 erase_header(header);
201 put_links(header);
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800202fail_links:
203 header->parent = NULL;
204 drop_sysctl_table(&dir->header);
205 return err;
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800206}
207
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800208/* called under sysctl_lock */
209static int use_table(struct ctl_table_header *p)
210{
211 if (unlikely(p->unregistering))
212 return 0;
213 p->used++;
214 return 1;
215}
216
217/* called under sysctl_lock */
218static void unuse_table(struct ctl_table_header *p)
219{
220 if (!--p->used)
221 if (unlikely(p->unregistering))
222 complete(p->unregistering);
223}
224
225/* called under sysctl_lock, will reacquire if has to wait */
226static void start_unregistering(struct ctl_table_header *p)
227{
228 /*
229 * if p->used is 0, nobody will ever touch that entry again;
230 * we'll eliminate all paths to it before dropping sysctl_lock
231 */
232 if (unlikely(p->used)) {
233 struct completion wait;
234 init_completion(&wait);
235 p->unregistering = &wait;
236 spin_unlock(&sysctl_lock);
237 wait_for_completion(&wait);
238 spin_lock(&sysctl_lock);
239 } else {
240 /* anything non-NULL; we'll never dereference it */
241 p->unregistering = ERR_PTR(-EINVAL);
242 }
243 /*
244 * do not remove from the list until nobody holds it; walking the
245 * list in do_sysctl() relies on that.
246 */
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800247 erase_header(p);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800248}
249
250static void sysctl_head_get(struct ctl_table_header *head)
251{
252 spin_lock(&sysctl_lock);
253 head->count++;
254 spin_unlock(&sysctl_lock);
255}
256
257void sysctl_head_put(struct ctl_table_header *head)
258{
259 spin_lock(&sysctl_lock);
260 if (!--head->count)
261 kfree_rcu(head, rcu);
262 spin_unlock(&sysctl_lock);
263}
264
265static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
266{
Prasad Joshiab4a1f22012-10-04 17:15:45 -0700267 BUG_ON(!head);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800268 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. Biederman0e47c992012-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{
Eric W. Biederman091bd3e2012-02-13 18:02:50 -0800371 if (uid_eq(current_euid(), GLOBAL_ROOT_UID))
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800372 mode >>= 6;
Eric W. Biederman091bd3e2012-02-13 18:02:50 -0800373 else if (in_egroup_p(GLOBAL_ROOT_GID))
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800374 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 Viro90434762008-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 Viro90434762008-07-15 08:54:06 -0400396 struct proc_inode *ei;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800397
Al Viro90434762008-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 Viro90434762008-07-15 08:54:06 -0400404 sysctl_head_get(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800405 ei = PROC_I(inode);
Al Viro90434762008-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 Viro90434762008-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 Viro90434762008-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 Viro90434762008-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. Biederman0e47c992012-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,
Al Viro00cd8dd2012-06-10 17:13:09 -0400433 unsigned int flags)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800434{
Al Viro90434762008-07-15 08:54:06 -0400435 struct ctl_table_header *head = grab_header(dir);
Al Viro90434762008-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 Viro90434762008-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. Biederman0e47c992012-01-07 23:24:30 -0800442 int ret;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800443
Al Viro90434762008-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 Viro90434762008-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 Viro90434762008-07-15 08:54:06 -0400450 if (!p)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800451 goto out;
452
Eric W. Biederman4e757322012-01-30 21:24:59 -0800453 if (S_ISLNK(p->mode)) {
454 ret = sysctl_follow_link(&h, &p, current->nsproxy);
455 err = ERR_PTR(ret);
456 if (ret)
457 goto out;
458 }
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800459
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800460 err = ERR_PTR(-ENOMEM);
Al Viro90434762008-07-15 08:54:06 -0400461 inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800462 if (!inode)
463 goto out;
464
465 err = NULL;
Nick Pigginfb045ad2011-01-07 17:49:55 +1100466 d_set_d_op(dentry, &proc_sys_dentry_operations);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800467 d_add(dentry, inode);
468
469out:
Francesco Ruggeri6bf61042012-09-13 15:03:37 -0700470 if (h)
471 sysctl_head_finish(h);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800472 sysctl_head_finish(head);
473 return err;
474}
475
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700476static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
477 size_t count, loff_t *ppos, int write)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800478{
Al Viro90434762008-07-15 08:54:06 -0400479 struct inode *inode = filp->f_path.dentry->d_inode;
480 struct ctl_table_header *head = grab_header(inode);
481 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
David Howells2a2da532007-10-25 15:27:40 +0100482 ssize_t error;
483 size_t res;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800484
Al Viro90434762008-07-15 08:54:06 -0400485 if (IS_ERR(head))
486 return PTR_ERR(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800487
488 /*
489 * At this point we know that the sysctl was not unregistered
490 * and won't be until we finish.
491 */
492 error = -EPERM;
Pavel Emelyanovd7321cd2008-04-29 01:02:44 -0700493 if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800494 goto out;
495
Al Viro90434762008-07-15 08:54:06 -0400496 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
497 error = -EINVAL;
498 if (!table->proc_handler)
499 goto out;
500
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800501 /* careful: calling conventions are nasty here */
502 res = count;
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700503 error = table->proc_handler(table, write, buf, &res, ppos);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800504 if (!error)
505 error = res;
506out:
507 sysctl_head_finish(head);
508
509 return error;
510}
511
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700512static ssize_t proc_sys_read(struct file *filp, char __user *buf,
513 size_t count, loff_t *ppos)
514{
515 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
516}
517
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800518static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
519 size_t count, loff_t *ppos)
520{
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700521 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800522}
523
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700524static int proc_sys_open(struct inode *inode, struct file *filp)
525{
Lucas De Marchi4e474a02012-03-22 14:42:22 -0700526 struct ctl_table_header *head = grab_header(inode);
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700527 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
528
Lucas De Marchi4e474a02012-03-22 14:42:22 -0700529 /* sysctl was unregistered */
530 if (IS_ERR(head))
531 return PTR_ERR(head);
532
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700533 if (table->poll)
534 filp->private_data = proc_sys_poll_event(table->poll);
535
Lucas De Marchi4e474a02012-03-22 14:42:22 -0700536 sysctl_head_finish(head);
537
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700538 return 0;
539}
540
541static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
542{
543 struct inode *inode = filp->f_path.dentry->d_inode;
Lucas De Marchi4e474a02012-03-22 14:42:22 -0700544 struct ctl_table_header *head = grab_header(inode);
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700545 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700546 unsigned int ret = DEFAULT_POLLMASK;
Lucas De Marchi4e474a02012-03-22 14:42:22 -0700547 unsigned long event;
548
549 /* sysctl was unregistered */
550 if (IS_ERR(head))
551 return POLLERR | POLLHUP;
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700552
553 if (!table->proc_handler)
554 goto out;
555
556 if (!table->poll)
557 goto out;
558
Lucas De Marchi4e474a02012-03-22 14:42:22 -0700559 event = (unsigned long)filp->private_data;
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700560 poll_wait(filp, &table->poll->wait, wait);
561
562 if (event != atomic_read(&table->poll->event)) {
563 filp->private_data = proc_sys_poll_event(table->poll);
564 ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
565 }
566
567out:
Lucas De Marchi4e474a02012-03-22 14:42:22 -0700568 sysctl_head_finish(head);
569
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700570 return ret;
571}
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800572
573static int proc_sys_fill_cache(struct file *filp, void *dirent,
Al Viro90434762008-07-15 08:54:06 -0400574 filldir_t filldir,
575 struct ctl_table_header *head,
576 struct ctl_table *table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800577{
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800578 struct dentry *child, *dir = filp->f_path.dentry;
579 struct inode *inode;
580 struct qstr qname;
581 ino_t ino = 0;
582 unsigned type = DT_UNKNOWN;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800583
584 qname.name = table->procname;
585 qname.len = strlen(table->procname);
586 qname.hash = full_name_hash(qname.name, qname.len);
587
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800588 child = d_lookup(dir, &qname);
589 if (!child) {
Al Viro90434762008-07-15 08:54:06 -0400590 child = d_alloc(dir, &qname);
591 if (child) {
592 inode = proc_sys_make_inode(dir->d_sb, head, table);
593 if (!inode) {
594 dput(child);
595 return -ENOMEM;
596 } else {
Nick Pigginfb045ad2011-01-07 17:49:55 +1100597 d_set_d_op(child, &proc_sys_dentry_operations);
Al Viro90434762008-07-15 08:54:06 -0400598 d_add(child, inode);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800599 }
Al Viro90434762008-07-15 08:54:06 -0400600 } else {
601 return -ENOMEM;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800602 }
603 }
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800604 inode = child->d_inode;
Al Viro90434762008-07-15 08:54:06 -0400605 ino = inode->i_ino;
606 type = inode->i_mode >> 12;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800607 dput(child);
Al Viro90434762008-07-15 08:54:06 -0400608 return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
609}
610
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800611static int proc_sys_link_fill_cache(struct file *filp, void *dirent,
612 filldir_t filldir,
613 struct ctl_table_header *head,
614 struct ctl_table *table)
615{
616 int err, ret = 0;
617 head = sysctl_head_grab(head);
618
Eric W. Biederman4e757322012-01-30 21:24:59 -0800619 if (S_ISLNK(table->mode)) {
620 /* It is not an error if we can not follow the link ignore it */
621 err = sysctl_follow_link(&head, &table, current->nsproxy);
622 if (err)
623 goto out;
624 }
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800625
626 ret = proc_sys_fill_cache(filp, dirent, filldir, head, table);
627out:
628 sysctl_head_finish(head);
629 return ret;
630}
631
Al Viro90434762008-07-15 08:54:06 -0400632static int scan(struct ctl_table_header *head, ctl_table *table,
633 unsigned long *pos, struct file *file,
634 void *dirent, filldir_t filldir)
635{
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800636 int res;
Al Viro90434762008-07-15 08:54:06 -0400637
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800638 if ((*pos)++ < file->f_pos)
639 return 0;
Al Viro90434762008-07-15 08:54:06 -0400640
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800641 if (unlikely(S_ISLNK(table->mode)))
642 res = proc_sys_link_fill_cache(file, dirent, filldir, head, table);
643 else
Al Viro90434762008-07-15 08:54:06 -0400644 res = proc_sys_fill_cache(file, dirent, filldir, head, table);
Al Viro90434762008-07-15 08:54:06 -0400645
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800646 if (res == 0)
647 file->f_pos = *pos;
Al Viro90434762008-07-15 08:54:06 -0400648
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800649 return res;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800650}
651
652static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
653{
Al Viro90434762008-07-15 08:54:06 -0400654 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800655 struct inode *inode = dentry->d_inode;
Al Viro90434762008-07-15 08:54:06 -0400656 struct ctl_table_header *head = grab_header(inode);
Al Viro90434762008-07-15 08:54:06 -0400657 struct ctl_table_header *h = NULL;
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800658 struct ctl_table *entry;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800659 struct ctl_dir *ctl_dir;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800660 unsigned long pos;
Al Viro90434762008-07-15 08:54:06 -0400661 int ret = -EINVAL;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800662
Al Viro90434762008-07-15 08:54:06 -0400663 if (IS_ERR(head))
664 return PTR_ERR(head);
665
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800666 ctl_dir = container_of(head, struct ctl_dir, header);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800667
668 ret = 0;
669 /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
670 if (filp->f_pos == 0) {
671 if (filldir(dirent, ".", 1, filp->f_pos,
672 inode->i_ino, DT_DIR) < 0)
673 goto out;
674 filp->f_pos++;
675 }
676 if (filp->f_pos == 1) {
677 if (filldir(dirent, "..", 2, filp->f_pos,
678 parent_ino(dentry), DT_DIR) < 0)
679 goto out;
680 filp->f_pos++;
681 }
682 pos = 2;
683
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800684 for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) {
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800685 ret = scan(h, entry, &pos, filp, dirent, filldir);
Al Viro90434762008-07-15 08:54:06 -0400686 if (ret) {
687 sysctl_head_finish(h);
688 break;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800689 }
690 }
691 ret = 1;
692out:
693 sysctl_head_finish(head);
694 return ret;
695}
696
Al Viro10556cb2011-06-20 19:28:19 -0400697static int proc_sys_permission(struct inode *inode, int mask)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800698{
699 /*
700 * sysctl entries that are not writeable,
701 * are _NOT_ writeable, capabilities or not.
702 */
Miklos Szeredif696a362008-07-31 13:41:58 +0200703 struct ctl_table_header *head;
704 struct ctl_table *table;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800705 int error;
706
Miklos Szeredif696a362008-07-31 13:41:58 +0200707 /* Executable files are not allowed under /proc/sys/ */
708 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
709 return -EACCES;
710
711 head = grab_header(inode);
Al Viro90434762008-07-15 08:54:06 -0400712 if (IS_ERR(head))
713 return PTR_ERR(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800714
Miklos Szeredif696a362008-07-31 13:41:58 +0200715 table = PROC_I(inode)->sysctl_entry;
Al Viro90434762008-07-15 08:54:06 -0400716 if (!table) /* global root - r-xr-xr-x */
717 error = mask & MAY_WRITE ? -EACCES : 0;
718 else /* Use the permissions on the sysctl table entry */
Al Viro1fc0f782011-06-20 18:59:02 -0400719 error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800720
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800721 sysctl_head_finish(head);
722 return error;
723}
724
725static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
726{
727 struct inode *inode = dentry->d_inode;
728 int error;
729
730 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
731 return -EPERM;
732
733 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200734 if (error)
735 return error;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800736
Christoph Hellwig10257742010-06-04 11:30:02 +0200737 if ((attr->ia_valid & ATTR_SIZE) &&
738 attr->ia_size != i_size_read(inode)) {
739 error = vmtruncate(inode, attr->ia_size);
740 if (error)
741 return error;
742 }
743
744 setattr_copy(inode, attr);
745 mark_inode_dirty(inode);
746 return 0;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800747}
748
Al Viro90434762008-07-15 08:54:06 -0400749static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
750{
751 struct inode *inode = dentry->d_inode;
752 struct ctl_table_header *head = grab_header(inode);
753 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
754
755 if (IS_ERR(head))
756 return PTR_ERR(head);
757
758 generic_fillattr(inode, stat);
759 if (table)
760 stat->mode = (stat->mode & S_IFMT) | table->mode;
761
762 sysctl_head_finish(head);
763 return 0;
764}
765
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800766static const struct file_operations proc_sys_file_operations = {
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700767 .open = proc_sys_open,
768 .poll = proc_sys_poll,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800769 .read = proc_sys_read,
770 .write = proc_sys_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200771 .llseek = default_llseek,
Al Viro90434762008-07-15 08:54:06 -0400772};
773
774static const struct file_operations proc_sys_dir_file_operations = {
Pavel Emelyanov887df072011-11-02 13:38:42 -0700775 .read = generic_read_dir,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800776 .readdir = proc_sys_readdir,
Christoph Hellwig3222a3e2008-09-03 21:53:01 +0200777 .llseek = generic_file_llseek,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800778};
779
Jan Engelhardt03a44822008-02-08 04:21:19 -0800780static const struct inode_operations proc_sys_inode_operations = {
Al Viro90434762008-07-15 08:54:06 -0400781 .permission = proc_sys_permission,
782 .setattr = proc_sys_setattr,
783 .getattr = proc_sys_getattr,
784};
785
786static const struct inode_operations proc_sys_dir_operations = {
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800787 .lookup = proc_sys_lookup,
788 .permission = proc_sys_permission,
789 .setattr = proc_sys_setattr,
Al Viro90434762008-07-15 08:54:06 -0400790 .getattr = proc_sys_getattr,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800791};
792
Al Viro0b728e12012-06-10 16:03:43 -0400793static int proc_sys_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800794{
Al Viro0b728e12012-06-10 16:03:43 -0400795 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +1100796 return -ECHILD;
Al Viro90434762008-07-15 08:54:06 -0400797 return !PROC_I(dentry->d_inode)->sysctl->unregistering;
798}
799
Nick Pigginfe15ce42011-01-07 17:49:23 +1100800static int proc_sys_delete(const struct dentry *dentry)
Al Viro90434762008-07-15 08:54:06 -0400801{
802 return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
803}
804
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800805static int sysctl_is_seen(struct ctl_table_header *p)
806{
807 struct ctl_table_set *set = p->set;
808 int res;
809 spin_lock(&sysctl_lock);
810 if (p->unregistering)
811 res = 0;
812 else if (!set->is_seen)
813 res = 1;
814 else
815 res = set->is_seen(set);
816 spin_unlock(&sysctl_lock);
817 return res;
818}
819
Nick Piggin621e1552011-01-07 17:49:27 +1100820static int proc_sys_compare(const struct dentry *parent,
821 const struct inode *pinode,
822 const struct dentry *dentry, const struct inode *inode,
823 unsigned int len, const char *str, const struct qstr *name)
Al Viro90434762008-07-15 08:54:06 -0400824{
Al Virodfef6dcd32011-03-08 01:25:28 -0500825 struct ctl_table_header *head;
Nick Piggin31e6b012011-01-07 17:49:52 +1100826 /* Although proc doesn't have negative dentries, rcu-walk means
827 * that inode here can be NULL */
Al Virodfef6dcd32011-03-08 01:25:28 -0500828 /* AV: can it, indeed? */
Nick Piggin31e6b012011-01-07 17:49:52 +1100829 if (!inode)
Al Virodfef6dcd32011-03-08 01:25:28 -0500830 return 1;
Nick Piggin621e1552011-01-07 17:49:27 +1100831 if (name->len != len)
Al Viro90434762008-07-15 08:54:06 -0400832 return 1;
Nick Piggin621e1552011-01-07 17:49:27 +1100833 if (memcmp(name->name, str, len))
Al Viro90434762008-07-15 08:54:06 -0400834 return 1;
Al Virodfef6dcd32011-03-08 01:25:28 -0500835 head = rcu_dereference(PROC_I(inode)->sysctl);
836 return !head || !sysctl_is_seen(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800837}
838
Al Virod72f71e2009-02-20 05:58:47 +0000839static const struct dentry_operations proc_sys_dentry_operations = {
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800840 .d_revalidate = proc_sys_revalidate,
Al Viro90434762008-07-15 08:54:06 -0400841 .d_delete = proc_sys_delete,
842 .d_compare = proc_sys_compare,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800843};
844
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800845static struct ctl_dir *find_subdir(struct ctl_dir *dir,
846 const char *name, int namelen)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800847{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800848 struct ctl_table_header *head;
849 struct ctl_table *entry;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800850
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800851 entry = find_entry(&head, dir, name, namelen);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800852 if (!entry)
853 return ERR_PTR(-ENOENT);
Eric W. Biederman51f72f42012-01-30 20:09:33 -0800854 if (!S_ISDIR(entry->mode))
855 return ERR_PTR(-ENOTDIR);
856 return container_of(head, struct ctl_dir, header);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800857}
858
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800859static struct ctl_dir *new_dir(struct ctl_table_set *set,
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800860 const char *name, int namelen)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800861{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800862 struct ctl_table *table;
863 struct ctl_dir *new;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800864 struct ctl_node *node;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800865 char *new_name;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800866
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800867 new = kzalloc(sizeof(*new) + sizeof(struct ctl_node) +
868 sizeof(struct ctl_table)*2 + namelen + 1,
869 GFP_KERNEL);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800870 if (!new)
871 return NULL;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800872
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800873 node = (struct ctl_node *)(new + 1);
874 table = (struct ctl_table *)(node + 1);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800875 new_name = (char *)(table + 2);
876 memcpy(new_name, name, namelen);
877 new_name[namelen] = '\0';
878 table[0].procname = new_name;
879 table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800880 init_header(&new->header, set->dir.header.root, set, node, table);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800881
882 return new;
883}
884
Eric W. Biederman60f126d2012-01-30 21:23:52 -0800885/**
886 * get_subdir - find or create a subdir with the specified name.
887 * @dir: Directory to create the subdirectory in
888 * @name: The name of the subdirectory to find or create
889 * @namelen: The length of name
890 *
891 * Takes a directory with an elevated reference count so we know that
892 * if we drop the lock the directory will not go away. Upon success
893 * the reference is moved from @dir to the returned subdirectory.
894 * Upon error an error code is returned and the reference on @dir is
895 * simply dropped.
896 */
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800897static struct ctl_dir *get_subdir(struct ctl_dir *dir,
898 const char *name, int namelen)
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800899{
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800900 struct ctl_table_set *set = dir->header.set;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800901 struct ctl_dir *subdir, *new = NULL;
Eric W. Biederman0eb97f32012-01-30 20:37:51 -0800902 int err;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800903
904 spin_lock(&sysctl_lock);
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800905 subdir = find_subdir(dir, name, namelen);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800906 if (!IS_ERR(subdir))
907 goto found;
908 if (PTR_ERR(subdir) != -ENOENT)
909 goto failed;
910
911 spin_unlock(&sysctl_lock);
912 new = new_dir(set, name, namelen);
913 spin_lock(&sysctl_lock);
914 subdir = ERR_PTR(-ENOMEM);
915 if (!new)
916 goto failed;
917
Eric W. Biederman60f126d2012-01-30 21:23:52 -0800918 /* Was the subdir added while we dropped the lock? */
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800919 subdir = find_subdir(dir, name, namelen);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800920 if (!IS_ERR(subdir))
921 goto found;
922 if (PTR_ERR(subdir) != -ENOENT)
923 goto failed;
924
Eric W. Biederman60f126d2012-01-30 21:23:52 -0800925 /* Nope. Use the our freshly made directory entry. */
Eric W. Biederman0eb97f32012-01-30 20:37:51 -0800926 err = insert_header(dir, &new->header);
927 subdir = ERR_PTR(err);
928 if (err)
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800929 goto failed;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800930 subdir = new;
931found:
932 subdir->header.nreg++;
933failed:
934 if (unlikely(IS_ERR(subdir))) {
Eric W. Biederman69801282012-01-21 20:09:45 -0800935 printk(KERN_ERR "sysctl could not get directory: ");
936 sysctl_print_dir(dir);
937 printk(KERN_CONT "/%*.*s %ld\n",
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800938 namelen, namelen, name, PTR_ERR(subdir));
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800939 }
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800940 drop_sysctl_table(&dir->header);
941 if (new)
942 drop_sysctl_table(&new->header);
943 spin_unlock(&sysctl_lock);
944 return subdir;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800945}
946
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800947static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir)
948{
949 struct ctl_dir *parent;
950 const char *procname;
951 if (!dir->header.parent)
952 return &set->dir;
953 parent = xlate_dir(set, dir->header.parent);
954 if (IS_ERR(parent))
955 return parent;
956 procname = dir->header.ctl_table[0].procname;
957 return find_subdir(parent, procname, strlen(procname));
958}
959
960static int sysctl_follow_link(struct ctl_table_header **phead,
961 struct ctl_table **pentry, struct nsproxy *namespaces)
962{
963 struct ctl_table_header *head;
964 struct ctl_table_root *root;
965 struct ctl_table_set *set;
966 struct ctl_table *entry;
967 struct ctl_dir *dir;
968 int ret;
969
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800970 ret = 0;
971 spin_lock(&sysctl_lock);
972 root = (*pentry)->data;
973 set = lookup_header_set(root, namespaces);
974 dir = xlate_dir(set, (*phead)->parent);
975 if (IS_ERR(dir))
976 ret = PTR_ERR(dir);
977 else {
978 const char *procname = (*pentry)->procname;
979 head = NULL;
980 entry = find_entry(&head, dir, procname, strlen(procname));
981 ret = -ENOENT;
982 if (entry && use_table(head)) {
983 unuse_table(*phead);
984 *phead = head;
985 *pentry = entry;
986 ret = 0;
987 }
988 }
989
990 spin_unlock(&sysctl_lock);
991 return ret;
992}
993
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800994static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
995{
996 struct va_format vaf;
997 va_list args;
998
999 va_start(args, fmt);
1000 vaf.fmt = fmt;
1001 vaf.va = &args;
1002
1003 printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n",
1004 path, table->procname, &vaf);
1005
1006 va_end(args);
1007 return -EINVAL;
1008}
1009
1010static int sysctl_check_table(const char *path, struct ctl_table *table)
1011{
1012 int err = 0;
1013 for (; table->procname; table++) {
1014 if (table->child)
1015 err = sysctl_err(path, table, "Not a file");
1016
1017 if ((table->proc_handler == proc_dostring) ||
1018 (table->proc_handler == proc_dointvec) ||
1019 (table->proc_handler == proc_dointvec_minmax) ||
1020 (table->proc_handler == proc_dointvec_jiffies) ||
1021 (table->proc_handler == proc_dointvec_userhz_jiffies) ||
1022 (table->proc_handler == proc_dointvec_ms_jiffies) ||
1023 (table->proc_handler == proc_doulongvec_minmax) ||
1024 (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
1025 if (!table->data)
1026 err = sysctl_err(path, table, "No data");
1027 if (!table->maxlen)
1028 err = sysctl_err(path, table, "No maxlen");
1029 }
1030 if (!table->proc_handler)
1031 err = sysctl_err(path, table, "No proc_handler");
1032
1033 if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
1034 err = sysctl_err(path, table, "bogus .mode 0%o",
1035 table->mode);
1036 }
1037 return err;
1038}
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001039
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001040static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table *table,
1041 struct ctl_table_root *link_root)
1042{
1043 struct ctl_table *link_table, *entry, *link;
1044 struct ctl_table_header *links;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001045 struct ctl_node *node;
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001046 char *link_name;
1047 int nr_entries, name_bytes;
1048
1049 name_bytes = 0;
1050 nr_entries = 0;
1051 for (entry = table; entry->procname; entry++) {
1052 nr_entries++;
1053 name_bytes += strlen(entry->procname) + 1;
1054 }
1055
1056 links = kzalloc(sizeof(struct ctl_table_header) +
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001057 sizeof(struct ctl_node)*nr_entries +
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001058 sizeof(struct ctl_table)*(nr_entries + 1) +
1059 name_bytes,
1060 GFP_KERNEL);
1061
1062 if (!links)
1063 return NULL;
1064
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001065 node = (struct ctl_node *)(links + 1);
1066 link_table = (struct ctl_table *)(node + nr_entries);
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001067 link_name = (char *)&link_table[nr_entries + 1];
1068
1069 for (link = link_table, entry = table; entry->procname; link++, entry++) {
1070 int len = strlen(entry->procname) + 1;
1071 memcpy(link_name, entry->procname, len);
1072 link->procname = link_name;
1073 link->mode = S_IFLNK|S_IRWXUGO;
1074 link->data = link_root;
1075 link_name += len;
1076 }
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001077 init_header(links, dir->header.root, dir->header.set, node, link_table);
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001078 links->nreg = nr_entries;
1079
1080 return links;
1081}
1082
1083static bool get_links(struct ctl_dir *dir,
1084 struct ctl_table *table, struct ctl_table_root *link_root)
1085{
1086 struct ctl_table_header *head;
1087 struct ctl_table *entry, *link;
1088
1089 /* Are there links available for every entry in table? */
1090 for (entry = table; entry->procname; entry++) {
1091 const char *procname = entry->procname;
1092 link = find_entry(&head, dir, procname, strlen(procname));
1093 if (!link)
1094 return false;
1095 if (S_ISDIR(link->mode) && S_ISDIR(entry->mode))
1096 continue;
1097 if (S_ISLNK(link->mode) && (link->data == link_root))
1098 continue;
1099 return false;
1100 }
1101
1102 /* The checks passed. Increase the registration count on the links */
1103 for (entry = table; entry->procname; entry++) {
1104 const char *procname = entry->procname;
1105 link = find_entry(&head, dir, procname, strlen(procname));
1106 head->nreg++;
1107 }
1108 return true;
1109}
1110
1111static int insert_links(struct ctl_table_header *head)
1112{
1113 struct ctl_table_set *root_set = &sysctl_table_root.default_set;
1114 struct ctl_dir *core_parent = NULL;
1115 struct ctl_table_header *links;
1116 int err;
1117
1118 if (head->set == root_set)
1119 return 0;
1120
1121 core_parent = xlate_dir(root_set, head->parent);
1122 if (IS_ERR(core_parent))
1123 return 0;
1124
1125 if (get_links(core_parent, head->ctl_table, head->root))
1126 return 0;
1127
1128 core_parent->header.nreg++;
1129 spin_unlock(&sysctl_lock);
1130
1131 links = new_links(core_parent, head->ctl_table, head->root);
1132
1133 spin_lock(&sysctl_lock);
1134 err = -ENOMEM;
1135 if (!links)
1136 goto out;
1137
1138 err = 0;
1139 if (get_links(core_parent, head->ctl_table, head->root)) {
1140 kfree(links);
1141 goto out;
1142 }
1143
1144 err = insert_header(core_parent, links);
1145 if (err)
1146 kfree(links);
1147out:
1148 drop_sysctl_table(&core_parent->header);
1149 return err;
1150}
1151
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001152/**
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001153 * __register_sysctl_table - register a leaf sysctl table
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001154 * @set: Sysctl tree to register on
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001155 * @path: The path to the directory the sysctl table is in.
1156 * @table: the top-level table structure
1157 *
1158 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1159 * array. A completely 0 filled entry terminates the table.
1160 *
1161 * The members of the &struct ctl_table structure are used as follows:
1162 *
1163 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
1164 * enter a sysctl file
1165 *
1166 * data - a pointer to data for use by proc_handler
1167 *
1168 * maxlen - the maximum size in bytes of the data
1169 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001170 * mode - the file permissions for the /proc/sys file
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001171 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001172 * child - must be %NULL.
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001173 *
1174 * proc_handler - the text handler routine (described below)
1175 *
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001176 * extra1, extra2 - extra pointers usable by the proc handler routines
1177 *
1178 * Leaf nodes in the sysctl tree will be represented by a single file
1179 * under /proc; non-leaf nodes will be represented by directories.
1180 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001181 * There must be a proc_handler routine for any terminal nodes.
1182 * Several default handlers are available to cover common cases -
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001183 *
1184 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
1185 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
1186 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
1187 *
1188 * It is the handler's job to read the input buffer from user memory
1189 * and process it. The handler should return 0 on success.
1190 *
1191 * This routine returns %NULL on a failure to register, and a pointer
1192 * to the table header on success.
1193 */
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001194struct ctl_table_header *__register_sysctl_table(
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001195 struct ctl_table_set *set,
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001196 const char *path, struct ctl_table *table)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001197{
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001198 struct ctl_table_root *root = set->dir.header.root;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001199 struct ctl_table_header *header;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001200 const char *name, *nextname;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001201 struct ctl_dir *dir;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001202 struct ctl_table *entry;
1203 struct ctl_node *node;
1204 int nr_entries = 0;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001205
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001206 for (entry = table; entry->procname; entry++)
1207 nr_entries++;
1208
1209 header = kzalloc(sizeof(struct ctl_table_header) +
1210 sizeof(struct ctl_node)*nr_entries, GFP_KERNEL);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001211 if (!header)
1212 return NULL;
1213
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001214 node = (struct ctl_node *)(header + 1);
1215 init_header(header, root, set, node, table);
Eric W. Biederman7c60c482012-01-21 13:34:05 -08001216 if (sysctl_check_table(path, table))
1217 goto fail;
Eric W. Biederman8d6ecfc2012-01-06 11:55:30 -08001218
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001219 spin_lock(&sysctl_lock);
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001220 dir = &set->dir;
Eric W. Biederman60f126d2012-01-30 21:23:52 -08001221 /* Reference moved down the diretory tree get_subdir */
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001222 dir->header.nreg++;
1223 spin_unlock(&sysctl_lock);
1224
1225 /* Find the directory for the ctl_table */
1226 for (name = path; name; name = nextname) {
1227 int namelen;
1228 nextname = strchr(name, '/');
1229 if (nextname) {
1230 namelen = nextname - name;
1231 nextname++;
1232 } else {
1233 namelen = strlen(name);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001234 }
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001235 if (namelen == 0)
1236 continue;
1237
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001238 dir = get_subdir(dir, name, namelen);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001239 if (IS_ERR(dir))
1240 goto fail;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001241 }
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001242
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001243 spin_lock(&sysctl_lock);
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001244 if (insert_header(dir, header))
1245 goto fail_put_dir_locked;
1246
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001247 drop_sysctl_table(&dir->header);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001248 spin_unlock(&sysctl_lock);
1249
1250 return header;
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001251
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001252fail_put_dir_locked:
1253 drop_sysctl_table(&dir->header);
Eric W. Biederman7c60c482012-01-21 13:34:05 -08001254 spin_unlock(&sysctl_lock);
1255fail:
1256 kfree(header);
1257 dump_stack();
1258 return NULL;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001259}
1260
Eric W. Biedermanfea478d2012-01-20 21:47:03 -08001261/**
1262 * register_sysctl - register a sysctl table
1263 * @path: The path to the directory the sysctl table is in.
1264 * @table: the table structure
1265 *
1266 * Register a sysctl table. @table should be a filled in ctl_table
1267 * array. A completely 0 filled entry terminates the table.
1268 *
1269 * See __register_sysctl_table for more details.
1270 */
1271struct ctl_table_header *register_sysctl(const char *path, struct ctl_table *table)
1272{
1273 return __register_sysctl_table(&sysctl_table_root.default_set,
1274 path, table);
1275}
1276EXPORT_SYMBOL(register_sysctl);
1277
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001278static char *append_path(const char *path, char *pos, const char *name)
1279{
1280 int namelen;
1281 namelen = strlen(name);
1282 if (((pos - path) + namelen + 2) >= PATH_MAX)
1283 return NULL;
1284 memcpy(pos, name, namelen);
1285 pos[namelen] = '/';
1286 pos[namelen + 1] = '\0';
1287 pos += namelen + 1;
1288 return pos;
1289}
1290
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001291static int count_subheaders(struct ctl_table *table)
1292{
1293 int has_files = 0;
1294 int nr_subheaders = 0;
1295 struct ctl_table *entry;
1296
1297 /* special case: no directory and empty directory */
1298 if (!table || !table->procname)
1299 return 1;
1300
1301 for (entry = table; entry->procname; entry++) {
1302 if (entry->child)
1303 nr_subheaders += count_subheaders(entry->child);
1304 else
1305 has_files = 1;
1306 }
1307 return nr_subheaders + has_files;
1308}
1309
1310static int register_leaf_sysctl_tables(const char *path, char *pos,
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001311 struct ctl_table_header ***subheader, struct ctl_table_set *set,
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001312 struct ctl_table *table)
1313{
1314 struct ctl_table *ctl_table_arg = NULL;
1315 struct ctl_table *entry, *files;
1316 int nr_files = 0;
1317 int nr_dirs = 0;
1318 int err = -ENOMEM;
1319
1320 for (entry = table; entry->procname; entry++) {
1321 if (entry->child)
1322 nr_dirs++;
1323 else
1324 nr_files++;
1325 }
1326
1327 files = table;
1328 /* If there are mixed files and directories we need a new table */
1329 if (nr_dirs && nr_files) {
1330 struct ctl_table *new;
1331 files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1),
1332 GFP_KERNEL);
1333 if (!files)
1334 goto out;
1335
1336 ctl_table_arg = files;
1337 for (new = files, entry = table; entry->procname; entry++) {
1338 if (entry->child)
1339 continue;
1340 *new = *entry;
1341 new++;
1342 }
1343 }
1344
1345 /* Register everything except a directory full of subdirectories */
1346 if (nr_files || !nr_dirs) {
1347 struct ctl_table_header *header;
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001348 header = __register_sysctl_table(set, path, files);
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001349 if (!header) {
1350 kfree(ctl_table_arg);
1351 goto out;
1352 }
1353
1354 /* Remember if we need to free the file table */
1355 header->ctl_table_arg = ctl_table_arg;
1356 **subheader = header;
1357 (*subheader)++;
1358 }
1359
1360 /* Recurse into the subdirectories. */
1361 for (entry = table; entry->procname; entry++) {
1362 char *child_pos;
1363
1364 if (!entry->child)
1365 continue;
1366
1367 err = -ENAMETOOLONG;
1368 child_pos = append_path(path, pos, entry->procname);
1369 if (!child_pos)
1370 goto out;
1371
1372 err = register_leaf_sysctl_tables(path, child_pos, subheader,
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001373 set, entry->child);
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001374 pos[0] = '\0';
1375 if (err)
1376 goto out;
1377 }
1378 err = 0;
1379out:
1380 /* On failure our caller will unregister all registered subheaders */
1381 return err;
1382}
1383
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001384/**
1385 * __register_sysctl_paths - register a sysctl table hierarchy
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001386 * @set: Sysctl tree to register on
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001387 * @path: The path to the directory the sysctl table is in.
1388 * @table: the top-level table structure
1389 *
1390 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1391 * array. A completely 0 filled entry terminates the table.
1392 *
1393 * See __register_sysctl_table for more details.
1394 */
1395struct ctl_table_header *__register_sysctl_paths(
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001396 struct ctl_table_set *set,
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001397 const struct ctl_path *path, struct ctl_table *table)
1398{
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001399 struct ctl_table *ctl_table_arg = table;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001400 int nr_subheaders = count_subheaders(table);
1401 struct ctl_table_header *header = NULL, **subheaders, **subheader;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001402 const struct ctl_path *component;
1403 char *new_path, *pos;
1404
1405 pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
1406 if (!new_path)
1407 return NULL;
1408
1409 pos[0] = '\0';
1410 for (component = path; component->procname; component++) {
1411 pos = append_path(new_path, pos, component->procname);
1412 if (!pos)
1413 goto out;
1414 }
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001415 while (table->procname && table->child && !table[1].procname) {
1416 pos = append_path(new_path, pos, table->procname);
1417 if (!pos)
1418 goto out;
1419 table = table->child;
1420 }
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001421 if (nr_subheaders == 1) {
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001422 header = __register_sysctl_table(set, new_path, table);
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001423 if (header)
1424 header->ctl_table_arg = ctl_table_arg;
1425 } else {
1426 header = kzalloc(sizeof(*header) +
1427 sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
1428 if (!header)
1429 goto out;
1430
1431 subheaders = (struct ctl_table_header **) (header + 1);
1432 subheader = subheaders;
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001433 header->ctl_table_arg = ctl_table_arg;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001434
1435 if (register_leaf_sysctl_tables(new_path, pos, &subheader,
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001436 set, table))
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001437 goto err_register_leaves;
1438 }
1439
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001440out:
1441 kfree(new_path);
1442 return header;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001443
1444err_register_leaves:
1445 while (subheader > subheaders) {
1446 struct ctl_table_header *subh = *(--subheader);
1447 struct ctl_table *table = subh->ctl_table_arg;
1448 unregister_sysctl_table(subh);
1449 kfree(table);
1450 }
1451 kfree(header);
1452 header = NULL;
1453 goto out;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001454}
1455
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001456/**
1457 * register_sysctl_table_path - register a sysctl table hierarchy
1458 * @path: The path to the directory the sysctl table is in.
1459 * @table: the top-level table structure
1460 *
1461 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1462 * array. A completely 0 filled entry terminates the table.
1463 *
1464 * See __register_sysctl_paths for more details.
1465 */
1466struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
1467 struct ctl_table *table)
1468{
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001469 return __register_sysctl_paths(&sysctl_table_root.default_set,
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001470 path, table);
1471}
1472EXPORT_SYMBOL(register_sysctl_paths);
1473
1474/**
1475 * register_sysctl_table - register a sysctl table hierarchy
1476 * @table: the top-level table structure
1477 *
1478 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1479 * array. A completely 0 filled entry terminates the table.
1480 *
1481 * See register_sysctl_paths for more details.
1482 */
1483struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
1484{
1485 static const struct ctl_path null_path[] = { {} };
1486
1487 return register_sysctl_paths(null_path, table);
1488}
1489EXPORT_SYMBOL(register_sysctl_table);
1490
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001491static void put_links(struct ctl_table_header *header)
1492{
1493 struct ctl_table_set *root_set = &sysctl_table_root.default_set;
1494 struct ctl_table_root *root = header->root;
1495 struct ctl_dir *parent = header->parent;
1496 struct ctl_dir *core_parent;
1497 struct ctl_table *entry;
1498
1499 if (header->set == root_set)
1500 return;
1501
1502 core_parent = xlate_dir(root_set, parent);
1503 if (IS_ERR(core_parent))
1504 return;
1505
1506 for (entry = header->ctl_table; entry->procname; entry++) {
1507 struct ctl_table_header *link_head;
1508 struct ctl_table *link;
1509 const char *name = entry->procname;
1510
1511 link = find_entry(&link_head, core_parent, name, strlen(name));
1512 if (link &&
1513 ((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) ||
1514 (S_ISLNK(link->mode) && (link->data == root)))) {
1515 drop_sysctl_table(link_head);
1516 }
1517 else {
1518 printk(KERN_ERR "sysctl link missing during unregister: ");
1519 sysctl_print_dir(parent);
1520 printk(KERN_CONT "/%s\n", name);
1521 }
1522 }
1523}
1524
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001525static void drop_sysctl_table(struct ctl_table_header *header)
1526{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001527 struct ctl_dir *parent = header->parent;
1528
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001529 if (--header->nreg)
1530 return;
1531
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001532 put_links(header);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001533 start_unregistering(header);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001534 if (!--header->count)
1535 kfree_rcu(header, rcu);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001536
1537 if (parent)
1538 drop_sysctl_table(&parent->header);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001539}
1540
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001541/**
1542 * unregister_sysctl_table - unregister a sysctl table hierarchy
1543 * @header: the header returned from register_sysctl_table
1544 *
1545 * Unregisters the sysctl table and all children. proc entries may not
1546 * actually be removed until they are no longer used by anyone.
1547 */
1548void unregister_sysctl_table(struct ctl_table_header * header)
1549{
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001550 int nr_subheaders;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001551 might_sleep();
1552
1553 if (header == NULL)
1554 return;
1555
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001556 nr_subheaders = count_subheaders(header->ctl_table_arg);
1557 if (unlikely(nr_subheaders > 1)) {
1558 struct ctl_table_header **subheaders;
1559 int i;
1560
1561 subheaders = (struct ctl_table_header **)(header + 1);
1562 for (i = nr_subheaders -1; i >= 0; i--) {
1563 struct ctl_table_header *subh = subheaders[i];
1564 struct ctl_table *table = subh->ctl_table_arg;
1565 unregister_sysctl_table(subh);
1566 kfree(table);
1567 }
1568 kfree(header);
1569 return;
1570 }
1571
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001572 spin_lock(&sysctl_lock);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001573 drop_sysctl_table(header);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001574 spin_unlock(&sysctl_lock);
1575}
1576EXPORT_SYMBOL(unregister_sysctl_table);
1577
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001578void setup_sysctl_set(struct ctl_table_set *set,
Eric W. Biederman9eb47c22012-01-22 21:26:00 -08001579 struct ctl_table_root *root,
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001580 int (*is_seen)(struct ctl_table_set *))
1581{
Dan Carpenter13474402012-01-30 16:40:29 +03001582 memset(set, 0, sizeof(*set));
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001583 set->is_seen = is_seen;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001584 init_header(&set->dir.header, root, set, NULL, root_table);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001585}
1586
Eric W. Biederman97324cd82012-01-09 22:19:13 -08001587void retire_sysctl_set(struct ctl_table_set *set)
1588{
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001589 WARN_ON(!RB_EMPTY_ROOT(&set->dir.root));
Eric W. Biederman97324cd82012-01-09 22:19:13 -08001590}
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001591
Alexey Dobriyan1e0edd32008-10-17 05:07:44 +04001592int __init proc_sys_init(void)
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001593{
Alexey Dobriyane1675232008-10-03 00:23:32 +04001594 struct proc_dir_entry *proc_sys_root;
1595
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001596 proc_sys_root = proc_mkdir("sys", NULL);
Al Viro90434762008-07-15 08:54:06 -04001597 proc_sys_root->proc_iops = &proc_sys_dir_operations;
1598 proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001599 proc_sys_root->nlink = 0;
Eric W. Biedermande4e83bd2012-01-06 03:34:20 -08001600
1601 return sysctl_init();
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001602}