blob: a7708b7c957ff8a8b1d9bb7c2b3f37ee25745310 [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 Viro90434762008-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. Biederman0e47c992012-01-07 23:24:30 -080035static struct ctl_table_root sysctl_table_root = {
Eric W. Biederman0e47c992012-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. Biederman0e47c992012-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. Biederman0e47c992012-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
Eric W. Biederman60f126d2012-01-30 21:23:52 -080076/* Called under sysctl_lock */
Eric W. Biederman076c3ee2012-01-09 21:42:02 -080077static struct ctl_table *find_entry(struct ctl_table_header **phead,
Eric W. Biederman0e47c992012-01-07 23:24:30 -080078 struct ctl_dir *dir, const char *name, int namelen)
Eric W. Biederman076c3ee2012-01-09 21:42:02 -080079{
80 struct ctl_table_header *head;
81 struct ctl_table *entry;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -080082 struct rb_node *node = dir->root.rb_node;
Eric W. Biederman076c3ee2012-01-09 21:42:02 -080083
Eric W. Biedermanac13ac62012-01-09 17:24:30 -080084 while (node)
85 {
86 struct ctl_node *ctl_node;
87 const char *procname;
88 int cmp;
89
90 ctl_node = rb_entry(node, struct ctl_node, node);
91 head = ctl_node->header;
92 entry = &head->ctl_table[ctl_node - head->node];
93 procname = entry->procname;
94
95 cmp = namecmp(name, namelen, procname, strlen(procname));
96 if (cmp < 0)
97 node = node->rb_left;
98 else if (cmp > 0)
99 node = node->rb_right;
100 else {
101 *phead = head;
102 return entry;
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800103 }
104 }
105 return NULL;
106}
107
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800108static int insert_entry(struct ctl_table_header *head, struct ctl_table *entry)
109{
110 struct rb_node *node = &head->node[entry - head->ctl_table].node;
111 struct rb_node **p = &head->parent->root.rb_node;
112 struct rb_node *parent = NULL;
113 const char *name = entry->procname;
114 int namelen = strlen(name);
115
116 while (*p) {
117 struct ctl_table_header *parent_head;
118 struct ctl_table *parent_entry;
119 struct ctl_node *parent_node;
120 const char *parent_name;
121 int cmp;
122
123 parent = *p;
124 parent_node = rb_entry(parent, struct ctl_node, node);
125 parent_head = parent_node->header;
126 parent_entry = &parent_head->ctl_table[parent_node - parent_head->node];
127 parent_name = parent_entry->procname;
128
129 cmp = namecmp(name, namelen, parent_name, strlen(parent_name));
130 if (cmp < 0)
131 p = &(*p)->rb_left;
132 else if (cmp > 0)
133 p = &(*p)->rb_right;
134 else {
135 printk(KERN_ERR "sysctl duplicate entry: ");
136 sysctl_print_dir(head->parent);
137 printk(KERN_CONT "/%s\n", entry->procname);
138 return -EEXIST;
139 }
140 }
141
142 rb_link_node(node, parent, p);
143 return 0;
144}
145
146static void erase_entry(struct ctl_table_header *head, struct ctl_table *entry)
147{
148 struct rb_node *node = &head->node[entry - head->ctl_table].node;
149
150 rb_erase(node, &head->parent->root);
151}
152
Eric W. Biedermane0d04522012-01-09 22:36:41 -0800153static void init_header(struct ctl_table_header *head,
154 struct ctl_table_root *root, struct ctl_table_set *set,
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800155 struct ctl_node *node, struct ctl_table *table)
Eric W. Biedermane0d04522012-01-09 22:36:41 -0800156{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800157 head->ctl_table = table;
Eric W. Biedermane0d04522012-01-09 22:36:41 -0800158 head->ctl_table_arg = table;
Eric W. Biedermane0d04522012-01-09 22:36:41 -0800159 head->used = 0;
160 head->count = 1;
161 head->nreg = 1;
162 head->unregistering = NULL;
163 head->root = root;
164 head->set = set;
165 head->parent = NULL;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800166 head->node = node;
167 if (node) {
168 struct ctl_table *entry;
169 for (entry = table; entry->procname; entry++, node++) {
170 rb_init_node(&node->node);
171 node->header = head;
172 }
173 }
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{
267 if (!head)
268 BUG();
269 spin_lock(&sysctl_lock);
270 if (!use_table(head))
271 head = ERR_PTR(-ENOENT);
272 spin_unlock(&sysctl_lock);
273 return head;
274}
275
276static void sysctl_head_finish(struct ctl_table_header *head)
277{
278 if (!head)
279 return;
280 spin_lock(&sysctl_lock);
281 unuse_table(head);
282 spin_unlock(&sysctl_lock);
283}
284
285static struct ctl_table_set *
286lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
287{
288 struct ctl_table_set *set = &root->default_set;
289 if (root->lookup)
290 set = root->lookup(root, namespaces);
291 return set;
292}
293
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800294static struct ctl_table *lookup_entry(struct ctl_table_header **phead,
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800295 struct ctl_dir *dir,
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800296 const char *name, int namelen)
297{
298 struct ctl_table_header *head;
299 struct ctl_table *entry;
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800300
301 spin_lock(&sysctl_lock);
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800302 entry = find_entry(&head, dir, name, namelen);
303 if (entry && use_table(head))
304 *phead = head;
305 else
306 entry = NULL;
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800307 spin_unlock(&sysctl_lock);
308 return entry;
309}
310
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800311static struct ctl_node *first_usable_entry(struct rb_node *node)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800312{
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800313 struct ctl_node *ctl_node;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800314
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800315 for (;node; node = rb_next(node)) {
316 ctl_node = rb_entry(node, struct ctl_node, node);
317 if (use_table(ctl_node->header))
318 return ctl_node;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800319 }
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800320 return NULL;
321}
322
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800323static void first_entry(struct ctl_dir *dir,
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800324 struct ctl_table_header **phead, struct ctl_table **pentry)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800325{
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800326 struct ctl_table_header *head = NULL;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800327 struct ctl_table *entry = NULL;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800328 struct ctl_node *ctl_node;
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800329
330 spin_lock(&sysctl_lock);
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800331 ctl_node = first_usable_entry(rb_first(&dir->root));
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800332 spin_unlock(&sysctl_lock);
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800333 if (ctl_node) {
334 head = ctl_node->header;
335 entry = &head->ctl_table[ctl_node - head->node];
336 }
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800337 *phead = head;
338 *pentry = entry;
339}
340
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800341static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry)
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800342{
343 struct ctl_table_header *head = *phead;
344 struct ctl_table *entry = *pentry;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800345 struct ctl_node *ctl_node = &head->node[entry - head->ctl_table];
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800346
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800347 spin_lock(&sysctl_lock);
348 unuse_table(head);
349
350 ctl_node = first_usable_entry(rb_next(&ctl_node->node));
351 spin_unlock(&sysctl_lock);
352 head = NULL;
353 if (ctl_node) {
354 head = ctl_node->header;
355 entry = &head->ctl_table[ctl_node - head->node];
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800356 }
357 *phead = head;
358 *pentry = entry;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800359}
360
361void register_sysctl_root(struct ctl_table_root *root)
362{
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800363}
364
365/*
366 * sysctl_perm does NOT grant the superuser all rights automatically, because
367 * some sysctl variables are readonly even to root.
368 */
369
370static int test_perm(int mode, int op)
371{
372 if (!current_euid())
373 mode >>= 6;
374 else if (in_egroup_p(0))
375 mode >>= 3;
376 if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
377 return 0;
378 return -EACCES;
379}
380
381static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op)
382{
383 int mode;
384
385 if (root->permissions)
386 mode = root->permissions(root, current->nsproxy, table);
387 else
388 mode = table->mode;
389
390 return test_perm(mode, op);
391}
392
Al Viro90434762008-07-15 08:54:06 -0400393static struct inode *proc_sys_make_inode(struct super_block *sb,
394 struct ctl_table_header *head, struct ctl_table *table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800395{
396 struct inode *inode;
Al Viro90434762008-07-15 08:54:06 -0400397 struct proc_inode *ei;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800398
Al Viro90434762008-07-15 08:54:06 -0400399 inode = new_inode(sb);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800400 if (!inode)
401 goto out;
402
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400403 inode->i_ino = get_next_ino();
404
Al Viro90434762008-07-15 08:54:06 -0400405 sysctl_head_get(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800406 ei = PROC_I(inode);
Al Viro90434762008-07-15 08:54:06 -0400407 ei->sysctl = head;
408 ei->sysctl_entry = table;
409
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800410 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Al Viro90434762008-07-15 08:54:06 -0400411 inode->i_mode = table->mode;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800412 if (!S_ISDIR(table->mode)) {
Al Viro90434762008-07-15 08:54:06 -0400413 inode->i_mode |= S_IFREG;
414 inode->i_op = &proc_sys_inode_operations;
415 inode->i_fop = &proc_sys_file_operations;
416 } else {
417 inode->i_mode |= S_IFDIR;
Al Viro90434762008-07-15 08:54:06 -0400418 inode->i_op = &proc_sys_dir_operations;
419 inode->i_fop = &proc_sys_dir_file_operations;
420 }
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800421out:
422 return inode;
423}
424
Adrian Bunk81324362008-10-03 00:33:54 +0400425static struct ctl_table_header *grab_header(struct inode *inode)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800426{
Eric W. Biederman3cc3e042012-01-07 06:57:47 -0800427 struct ctl_table_header *head = PROC_I(inode)->sysctl;
428 if (!head)
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800429 head = &sysctl_table_root.default_set.dir.header;
Eric W. Biederman3cc3e042012-01-07 06:57:47 -0800430 return sysctl_head_grab(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800431}
432
433static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
434 struct nameidata *nd)
435{
Al Viro90434762008-07-15 08:54:06 -0400436 struct ctl_table_header *head = grab_header(dir);
Al Viro90434762008-07-15 08:54:06 -0400437 struct ctl_table_header *h = NULL;
438 struct qstr *name = &dentry->d_name;
439 struct ctl_table *p;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800440 struct inode *inode;
Al Viro90434762008-07-15 08:54:06 -0400441 struct dentry *err = ERR_PTR(-ENOENT);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800442 struct ctl_dir *ctl_dir;
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800443 int ret;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800444
Al Viro90434762008-07-15 08:54:06 -0400445 if (IS_ERR(head))
446 return ERR_CAST(head);
447
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800448 ctl_dir = container_of(head, struct ctl_dir, header);
Al Viro90434762008-07-15 08:54:06 -0400449
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800450 p = lookup_entry(&h, ctl_dir, name->name, name->len);
Al Viro90434762008-07-15 08:54:06 -0400451 if (!p)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800452 goto out;
453
Eric W. Biederman4e757322012-01-30 21:24:59 -0800454 if (S_ISLNK(p->mode)) {
455 ret = sysctl_follow_link(&h, &p, current->nsproxy);
456 err = ERR_PTR(ret);
457 if (ret)
458 goto out;
459 }
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800460
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800461 err = ERR_PTR(-ENOMEM);
Al Viro90434762008-07-15 08:54:06 -0400462 inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
463 if (h)
464 sysctl_head_finish(h);
465
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800466 if (!inode)
467 goto out;
468
469 err = NULL;
Nick Pigginfb045ad2011-01-07 17:49:55 +1100470 d_set_d_op(dentry, &proc_sys_dentry_operations);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800471 d_add(dentry, inode);
472
473out:
474 sysctl_head_finish(head);
475 return err;
476}
477
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700478static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
479 size_t count, loff_t *ppos, int write)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800480{
Al Viro90434762008-07-15 08:54:06 -0400481 struct inode *inode = filp->f_path.dentry->d_inode;
482 struct ctl_table_header *head = grab_header(inode);
483 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
David Howells2a2da532007-10-25 15:27:40 +0100484 ssize_t error;
485 size_t res;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800486
Al Viro90434762008-07-15 08:54:06 -0400487 if (IS_ERR(head))
488 return PTR_ERR(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800489
490 /*
491 * At this point we know that the sysctl was not unregistered
492 * and won't be until we finish.
493 */
494 error = -EPERM;
Pavel Emelyanovd7321cd2008-04-29 01:02:44 -0700495 if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800496 goto out;
497
Al Viro90434762008-07-15 08:54:06 -0400498 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
499 error = -EINVAL;
500 if (!table->proc_handler)
501 goto out;
502
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800503 /* careful: calling conventions are nasty here */
504 res = count;
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700505 error = table->proc_handler(table, write, buf, &res, ppos);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800506 if (!error)
507 error = res;
508out:
509 sysctl_head_finish(head);
510
511 return error;
512}
513
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700514static ssize_t proc_sys_read(struct file *filp, char __user *buf,
515 size_t count, loff_t *ppos)
516{
517 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
518}
519
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800520static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
521 size_t count, loff_t *ppos)
522{
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700523 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800524}
525
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700526static int proc_sys_open(struct inode *inode, struct file *filp)
527{
528 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
529
530 if (table->poll)
531 filp->private_data = proc_sys_poll_event(table->poll);
532
533 return 0;
534}
535
536static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
537{
538 struct inode *inode = filp->f_path.dentry->d_inode;
539 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
540 unsigned long event = (unsigned long)filp->private_data;
541 unsigned int ret = DEFAULT_POLLMASK;
542
543 if (!table->proc_handler)
544 goto out;
545
546 if (!table->poll)
547 goto out;
548
549 poll_wait(filp, &table->poll->wait, wait);
550
551 if (event != atomic_read(&table->poll->event)) {
552 filp->private_data = proc_sys_poll_event(table->poll);
553 ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
554 }
555
556out:
557 return ret;
558}
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800559
560static int proc_sys_fill_cache(struct file *filp, void *dirent,
Al Viro90434762008-07-15 08:54:06 -0400561 filldir_t filldir,
562 struct ctl_table_header *head,
563 struct ctl_table *table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800564{
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800565 struct dentry *child, *dir = filp->f_path.dentry;
566 struct inode *inode;
567 struct qstr qname;
568 ino_t ino = 0;
569 unsigned type = DT_UNKNOWN;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800570
571 qname.name = table->procname;
572 qname.len = strlen(table->procname);
573 qname.hash = full_name_hash(qname.name, qname.len);
574
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800575 child = d_lookup(dir, &qname);
576 if (!child) {
Al Viro90434762008-07-15 08:54:06 -0400577 child = d_alloc(dir, &qname);
578 if (child) {
579 inode = proc_sys_make_inode(dir->d_sb, head, table);
580 if (!inode) {
581 dput(child);
582 return -ENOMEM;
583 } else {
Nick Pigginfb045ad2011-01-07 17:49:55 +1100584 d_set_d_op(child, &proc_sys_dentry_operations);
Al Viro90434762008-07-15 08:54:06 -0400585 d_add(child, inode);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800586 }
Al Viro90434762008-07-15 08:54:06 -0400587 } else {
588 return -ENOMEM;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800589 }
590 }
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800591 inode = child->d_inode;
Al Viro90434762008-07-15 08:54:06 -0400592 ino = inode->i_ino;
593 type = inode->i_mode >> 12;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800594 dput(child);
Al Viro90434762008-07-15 08:54:06 -0400595 return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
596}
597
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800598static int proc_sys_link_fill_cache(struct file *filp, void *dirent,
599 filldir_t filldir,
600 struct ctl_table_header *head,
601 struct ctl_table *table)
602{
603 int err, ret = 0;
604 head = sysctl_head_grab(head);
605
Eric W. Biederman4e757322012-01-30 21:24:59 -0800606 if (S_ISLNK(table->mode)) {
607 /* It is not an error if we can not follow the link ignore it */
608 err = sysctl_follow_link(&head, &table, current->nsproxy);
609 if (err)
610 goto out;
611 }
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800612
613 ret = proc_sys_fill_cache(filp, dirent, filldir, head, table);
614out:
615 sysctl_head_finish(head);
616 return ret;
617}
618
Al Viro90434762008-07-15 08:54:06 -0400619static int scan(struct ctl_table_header *head, ctl_table *table,
620 unsigned long *pos, struct file *file,
621 void *dirent, filldir_t filldir)
622{
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800623 int res;
Al Viro90434762008-07-15 08:54:06 -0400624
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800625 if ((*pos)++ < file->f_pos)
626 return 0;
Al Viro90434762008-07-15 08:54:06 -0400627
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800628 if (unlikely(S_ISLNK(table->mode)))
629 res = proc_sys_link_fill_cache(file, dirent, filldir, head, table);
630 else
631 res = proc_sys_fill_cache(file, dirent, filldir, head, table);
Al Viro90434762008-07-15 08:54:06 -0400632
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800633 if (res == 0)
634 file->f_pos = *pos;
Al Viro90434762008-07-15 08:54:06 -0400635
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800636 return res;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800637}
638
639static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
640{
Al Viro90434762008-07-15 08:54:06 -0400641 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800642 struct inode *inode = dentry->d_inode;
Al Viro90434762008-07-15 08:54:06 -0400643 struct ctl_table_header *head = grab_header(inode);
Al Viro90434762008-07-15 08:54:06 -0400644 struct ctl_table_header *h = NULL;
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800645 struct ctl_table *entry;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800646 struct ctl_dir *ctl_dir;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800647 unsigned long pos;
Al Viro90434762008-07-15 08:54:06 -0400648 int ret = -EINVAL;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800649
Al Viro90434762008-07-15 08:54:06 -0400650 if (IS_ERR(head))
651 return PTR_ERR(head);
652
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800653 ctl_dir = container_of(head, struct ctl_dir, header);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800654
655 ret = 0;
656 /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
657 if (filp->f_pos == 0) {
658 if (filldir(dirent, ".", 1, filp->f_pos,
659 inode->i_ino, DT_DIR) < 0)
660 goto out;
661 filp->f_pos++;
662 }
663 if (filp->f_pos == 1) {
664 if (filldir(dirent, "..", 2, filp->f_pos,
665 parent_ino(dentry), DT_DIR) < 0)
666 goto out;
667 filp->f_pos++;
668 }
669 pos = 2;
670
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800671 for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) {
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800672 ret = scan(h, entry, &pos, filp, dirent, filldir);
Al Viro90434762008-07-15 08:54:06 -0400673 if (ret) {
674 sysctl_head_finish(h);
675 break;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800676 }
677 }
678 ret = 1;
679out:
680 sysctl_head_finish(head);
681 return ret;
682}
683
Al Viro10556cb2011-06-20 19:28:19 -0400684static int proc_sys_permission(struct inode *inode, int mask)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800685{
686 /*
687 * sysctl entries that are not writeable,
688 * are _NOT_ writeable, capabilities or not.
689 */
Miklos Szeredif696a362008-07-31 13:41:58 +0200690 struct ctl_table_header *head;
691 struct ctl_table *table;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800692 int error;
693
Miklos Szeredif696a362008-07-31 13:41:58 +0200694 /* Executable files are not allowed under /proc/sys/ */
695 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
696 return -EACCES;
697
698 head = grab_header(inode);
Al Viro90434762008-07-15 08:54:06 -0400699 if (IS_ERR(head))
700 return PTR_ERR(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800701
Miklos Szeredif696a362008-07-31 13:41:58 +0200702 table = PROC_I(inode)->sysctl_entry;
Al Viro90434762008-07-15 08:54:06 -0400703 if (!table) /* global root - r-xr-xr-x */
704 error = mask & MAY_WRITE ? -EACCES : 0;
705 else /* Use the permissions on the sysctl table entry */
Al Viro1fc0f782011-06-20 18:59:02 -0400706 error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800707
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800708 sysctl_head_finish(head);
709 return error;
710}
711
712static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
713{
714 struct inode *inode = dentry->d_inode;
715 int error;
716
717 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
718 return -EPERM;
719
720 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200721 if (error)
722 return error;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800723
Christoph Hellwig10257742010-06-04 11:30:02 +0200724 if ((attr->ia_valid & ATTR_SIZE) &&
725 attr->ia_size != i_size_read(inode)) {
726 error = vmtruncate(inode, attr->ia_size);
727 if (error)
728 return error;
729 }
730
731 setattr_copy(inode, attr);
732 mark_inode_dirty(inode);
733 return 0;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800734}
735
Al Viro90434762008-07-15 08:54:06 -0400736static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
737{
738 struct inode *inode = dentry->d_inode;
739 struct ctl_table_header *head = grab_header(inode);
740 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
741
742 if (IS_ERR(head))
743 return PTR_ERR(head);
744
745 generic_fillattr(inode, stat);
746 if (table)
747 stat->mode = (stat->mode & S_IFMT) | table->mode;
748
749 sysctl_head_finish(head);
750 return 0;
751}
752
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800753static const struct file_operations proc_sys_file_operations = {
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700754 .open = proc_sys_open,
755 .poll = proc_sys_poll,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800756 .read = proc_sys_read,
757 .write = proc_sys_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200758 .llseek = default_llseek,
Al Viro90434762008-07-15 08:54:06 -0400759};
760
761static const struct file_operations proc_sys_dir_file_operations = {
Pavel Emelyanov887df072011-11-02 13:38:42 -0700762 .read = generic_read_dir,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800763 .readdir = proc_sys_readdir,
Christoph Hellwig3222a3e2008-09-03 21:53:01 +0200764 .llseek = generic_file_llseek,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800765};
766
Jan Engelhardt03a44822008-02-08 04:21:19 -0800767static const struct inode_operations proc_sys_inode_operations = {
Al Viro90434762008-07-15 08:54:06 -0400768 .permission = proc_sys_permission,
769 .setattr = proc_sys_setattr,
770 .getattr = proc_sys_getattr,
771};
772
773static const struct inode_operations proc_sys_dir_operations = {
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800774 .lookup = proc_sys_lookup,
775 .permission = proc_sys_permission,
776 .setattr = proc_sys_setattr,
Al Viro90434762008-07-15 08:54:06 -0400777 .getattr = proc_sys_getattr,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800778};
779
780static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
781{
Nick Piggin34286d62011-01-07 17:49:57 +1100782 if (nd->flags & LOOKUP_RCU)
783 return -ECHILD;
Al Viro90434762008-07-15 08:54:06 -0400784 return !PROC_I(dentry->d_inode)->sysctl->unregistering;
785}
786
Nick Pigginfe15ce42011-01-07 17:49:23 +1100787static int proc_sys_delete(const struct dentry *dentry)
Al Viro90434762008-07-15 08:54:06 -0400788{
789 return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
790}
791
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800792static int sysctl_is_seen(struct ctl_table_header *p)
793{
794 struct ctl_table_set *set = p->set;
795 int res;
796 spin_lock(&sysctl_lock);
797 if (p->unregistering)
798 res = 0;
799 else if (!set->is_seen)
800 res = 1;
801 else
802 res = set->is_seen(set);
803 spin_unlock(&sysctl_lock);
804 return res;
805}
806
Nick Piggin621e1552011-01-07 17:49:27 +1100807static int proc_sys_compare(const struct dentry *parent,
808 const struct inode *pinode,
809 const struct dentry *dentry, const struct inode *inode,
810 unsigned int len, const char *str, const struct qstr *name)
Al Viro90434762008-07-15 08:54:06 -0400811{
Al Virodfef6dc2011-03-08 01:25:28 -0500812 struct ctl_table_header *head;
Nick Piggin31e6b012011-01-07 17:49:52 +1100813 /* Although proc doesn't have negative dentries, rcu-walk means
814 * that inode here can be NULL */
Al Virodfef6dc2011-03-08 01:25:28 -0500815 /* AV: can it, indeed? */
Nick Piggin31e6b012011-01-07 17:49:52 +1100816 if (!inode)
Al Virodfef6dc2011-03-08 01:25:28 -0500817 return 1;
Nick Piggin621e1552011-01-07 17:49:27 +1100818 if (name->len != len)
Al Viro90434762008-07-15 08:54:06 -0400819 return 1;
Nick Piggin621e1552011-01-07 17:49:27 +1100820 if (memcmp(name->name, str, len))
Al Viro90434762008-07-15 08:54:06 -0400821 return 1;
Al Virodfef6dc2011-03-08 01:25:28 -0500822 head = rcu_dereference(PROC_I(inode)->sysctl);
823 return !head || !sysctl_is_seen(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800824}
825
Al Virod72f71e2009-02-20 05:58:47 +0000826static const struct dentry_operations proc_sys_dentry_operations = {
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800827 .d_revalidate = proc_sys_revalidate,
Al Viro90434762008-07-15 08:54:06 -0400828 .d_delete = proc_sys_delete,
829 .d_compare = proc_sys_compare,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800830};
831
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800832static struct ctl_dir *find_subdir(struct ctl_dir *dir,
833 const char *name, int namelen)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800834{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800835 struct ctl_table_header *head;
836 struct ctl_table *entry;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800837
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800838 entry = find_entry(&head, dir, name, namelen);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800839 if (!entry)
840 return ERR_PTR(-ENOENT);
Eric W. Biederman51f72f42012-01-30 20:09:33 -0800841 if (!S_ISDIR(entry->mode))
842 return ERR_PTR(-ENOTDIR);
843 return container_of(head, struct ctl_dir, header);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800844}
845
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800846static struct ctl_dir *new_dir(struct ctl_table_set *set,
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800847 const char *name, int namelen)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800848{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800849 struct ctl_table *table;
850 struct ctl_dir *new;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800851 struct ctl_node *node;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800852 char *new_name;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800853
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800854 new = kzalloc(sizeof(*new) + sizeof(struct ctl_node) +
855 sizeof(struct ctl_table)*2 + namelen + 1,
856 GFP_KERNEL);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800857 if (!new)
858 return NULL;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800859
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800860 node = (struct ctl_node *)(new + 1);
861 table = (struct ctl_table *)(node + 1);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800862 new_name = (char *)(table + 2);
863 memcpy(new_name, name, namelen);
864 new_name[namelen] = '\0';
865 table[0].procname = new_name;
866 table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800867 init_header(&new->header, set->dir.header.root, set, node, table);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800868
869 return new;
870}
871
Eric W. Biederman60f126d2012-01-30 21:23:52 -0800872/**
873 * get_subdir - find or create a subdir with the specified name.
874 * @dir: Directory to create the subdirectory in
875 * @name: The name of the subdirectory to find or create
876 * @namelen: The length of name
877 *
878 * Takes a directory with an elevated reference count so we know that
879 * if we drop the lock the directory will not go away. Upon success
880 * the reference is moved from @dir to the returned subdirectory.
881 * Upon error an error code is returned and the reference on @dir is
882 * simply dropped.
883 */
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800884static struct ctl_dir *get_subdir(struct ctl_dir *dir,
885 const char *name, int namelen)
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800886{
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800887 struct ctl_table_set *set = dir->header.set;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800888 struct ctl_dir *subdir, *new = NULL;
Eric W. Biederman0eb97f32012-01-30 20:37:51 -0800889 int err;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800890
891 spin_lock(&sysctl_lock);
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800892 subdir = find_subdir(dir, name, namelen);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800893 if (!IS_ERR(subdir))
894 goto found;
895 if (PTR_ERR(subdir) != -ENOENT)
896 goto failed;
897
898 spin_unlock(&sysctl_lock);
899 new = new_dir(set, name, namelen);
900 spin_lock(&sysctl_lock);
901 subdir = ERR_PTR(-ENOMEM);
902 if (!new)
903 goto failed;
904
Eric W. Biederman60f126d2012-01-30 21:23:52 -0800905 /* Was the subdir added while we dropped the lock? */
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800906 subdir = find_subdir(dir, name, namelen);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800907 if (!IS_ERR(subdir))
908 goto found;
909 if (PTR_ERR(subdir) != -ENOENT)
910 goto failed;
911
Eric W. Biederman60f126d2012-01-30 21:23:52 -0800912 /* Nope. Use the our freshly made directory entry. */
Eric W. Biederman0eb97f32012-01-30 20:37:51 -0800913 err = insert_header(dir, &new->header);
914 subdir = ERR_PTR(err);
915 if (err)
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800916 goto failed;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800917 subdir = new;
918found:
919 subdir->header.nreg++;
920failed:
921 if (unlikely(IS_ERR(subdir))) {
Eric W. Biederman69801282012-01-21 20:09:45 -0800922 printk(KERN_ERR "sysctl could not get directory: ");
923 sysctl_print_dir(dir);
924 printk(KERN_CONT "/%*.*s %ld\n",
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800925 namelen, namelen, name, PTR_ERR(subdir));
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800926 }
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800927 drop_sysctl_table(&dir->header);
928 if (new)
929 drop_sysctl_table(&new->header);
930 spin_unlock(&sysctl_lock);
931 return subdir;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800932}
933
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800934static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir)
935{
936 struct ctl_dir *parent;
937 const char *procname;
938 if (!dir->header.parent)
939 return &set->dir;
940 parent = xlate_dir(set, dir->header.parent);
941 if (IS_ERR(parent))
942 return parent;
943 procname = dir->header.ctl_table[0].procname;
944 return find_subdir(parent, procname, strlen(procname));
945}
946
947static int sysctl_follow_link(struct ctl_table_header **phead,
948 struct ctl_table **pentry, struct nsproxy *namespaces)
949{
950 struct ctl_table_header *head;
951 struct ctl_table_root *root;
952 struct ctl_table_set *set;
953 struct ctl_table *entry;
954 struct ctl_dir *dir;
955 int ret;
956
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800957 ret = 0;
958 spin_lock(&sysctl_lock);
959 root = (*pentry)->data;
960 set = lookup_header_set(root, namespaces);
961 dir = xlate_dir(set, (*phead)->parent);
962 if (IS_ERR(dir))
963 ret = PTR_ERR(dir);
964 else {
965 const char *procname = (*pentry)->procname;
966 head = NULL;
967 entry = find_entry(&head, dir, procname, strlen(procname));
968 ret = -ENOENT;
969 if (entry && use_table(head)) {
970 unuse_table(*phead);
971 *phead = head;
972 *pentry = entry;
973 ret = 0;
974 }
975 }
976
977 spin_unlock(&sysctl_lock);
978 return ret;
979}
980
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800981static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
982{
983 struct va_format vaf;
984 va_list args;
985
986 va_start(args, fmt);
987 vaf.fmt = fmt;
988 vaf.va = &args;
989
990 printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n",
991 path, table->procname, &vaf);
992
993 va_end(args);
994 return -EINVAL;
995}
996
997static int sysctl_check_table(const char *path, struct ctl_table *table)
998{
999 int err = 0;
1000 for (; table->procname; table++) {
1001 if (table->child)
1002 err = sysctl_err(path, table, "Not a file");
1003
1004 if ((table->proc_handler == proc_dostring) ||
1005 (table->proc_handler == proc_dointvec) ||
1006 (table->proc_handler == proc_dointvec_minmax) ||
1007 (table->proc_handler == proc_dointvec_jiffies) ||
1008 (table->proc_handler == proc_dointvec_userhz_jiffies) ||
1009 (table->proc_handler == proc_dointvec_ms_jiffies) ||
1010 (table->proc_handler == proc_doulongvec_minmax) ||
1011 (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
1012 if (!table->data)
1013 err = sysctl_err(path, table, "No data");
1014 if (!table->maxlen)
1015 err = sysctl_err(path, table, "No maxlen");
1016 }
1017 if (!table->proc_handler)
1018 err = sysctl_err(path, table, "No proc_handler");
1019
1020 if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
1021 err = sysctl_err(path, table, "bogus .mode 0%o",
1022 table->mode);
1023 }
1024 return err;
1025}
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001026
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001027static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table *table,
1028 struct ctl_table_root *link_root)
1029{
1030 struct ctl_table *link_table, *entry, *link;
1031 struct ctl_table_header *links;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001032 struct ctl_node *node;
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001033 char *link_name;
1034 int nr_entries, name_bytes;
1035
1036 name_bytes = 0;
1037 nr_entries = 0;
1038 for (entry = table; entry->procname; entry++) {
1039 nr_entries++;
1040 name_bytes += strlen(entry->procname) + 1;
1041 }
1042
1043 links = kzalloc(sizeof(struct ctl_table_header) +
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001044 sizeof(struct ctl_node)*nr_entries +
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001045 sizeof(struct ctl_table)*(nr_entries + 1) +
1046 name_bytes,
1047 GFP_KERNEL);
1048
1049 if (!links)
1050 return NULL;
1051
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001052 node = (struct ctl_node *)(links + 1);
1053 link_table = (struct ctl_table *)(node + nr_entries);
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001054 link_name = (char *)&link_table[nr_entries + 1];
1055
1056 for (link = link_table, entry = table; entry->procname; link++, entry++) {
1057 int len = strlen(entry->procname) + 1;
1058 memcpy(link_name, entry->procname, len);
1059 link->procname = link_name;
1060 link->mode = S_IFLNK|S_IRWXUGO;
1061 link->data = link_root;
1062 link_name += len;
1063 }
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001064 init_header(links, dir->header.root, dir->header.set, node, link_table);
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001065 links->nreg = nr_entries;
1066
1067 return links;
1068}
1069
1070static bool get_links(struct ctl_dir *dir,
1071 struct ctl_table *table, struct ctl_table_root *link_root)
1072{
1073 struct ctl_table_header *head;
1074 struct ctl_table *entry, *link;
1075
1076 /* Are there links available for every entry in table? */
1077 for (entry = table; entry->procname; entry++) {
1078 const char *procname = entry->procname;
1079 link = find_entry(&head, dir, procname, strlen(procname));
1080 if (!link)
1081 return false;
1082 if (S_ISDIR(link->mode) && S_ISDIR(entry->mode))
1083 continue;
1084 if (S_ISLNK(link->mode) && (link->data == link_root))
1085 continue;
1086 return false;
1087 }
1088
1089 /* The checks passed. Increase the registration count on the links */
1090 for (entry = table; entry->procname; entry++) {
1091 const char *procname = entry->procname;
1092 link = find_entry(&head, dir, procname, strlen(procname));
1093 head->nreg++;
1094 }
1095 return true;
1096}
1097
1098static int insert_links(struct ctl_table_header *head)
1099{
1100 struct ctl_table_set *root_set = &sysctl_table_root.default_set;
1101 struct ctl_dir *core_parent = NULL;
1102 struct ctl_table_header *links;
1103 int err;
1104
1105 if (head->set == root_set)
1106 return 0;
1107
1108 core_parent = xlate_dir(root_set, head->parent);
1109 if (IS_ERR(core_parent))
1110 return 0;
1111
1112 if (get_links(core_parent, head->ctl_table, head->root))
1113 return 0;
1114
1115 core_parent->header.nreg++;
1116 spin_unlock(&sysctl_lock);
1117
1118 links = new_links(core_parent, head->ctl_table, head->root);
1119
1120 spin_lock(&sysctl_lock);
1121 err = -ENOMEM;
1122 if (!links)
1123 goto out;
1124
1125 err = 0;
1126 if (get_links(core_parent, head->ctl_table, head->root)) {
1127 kfree(links);
1128 goto out;
1129 }
1130
1131 err = insert_header(core_parent, links);
1132 if (err)
1133 kfree(links);
1134out:
1135 drop_sysctl_table(&core_parent->header);
1136 return err;
1137}
1138
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001139/**
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001140 * __register_sysctl_table - register a leaf sysctl table
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001141 * @set: Sysctl tree to register on
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001142 * @path: The path to the directory the sysctl table is in.
1143 * @table: the top-level table structure
1144 *
1145 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1146 * array. A completely 0 filled entry terminates the table.
1147 *
1148 * The members of the &struct ctl_table structure are used as follows:
1149 *
1150 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
1151 * enter a sysctl file
1152 *
1153 * data - a pointer to data for use by proc_handler
1154 *
1155 * maxlen - the maximum size in bytes of the data
1156 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001157 * mode - the file permissions for the /proc/sys file
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001158 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001159 * child - must be %NULL.
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001160 *
1161 * proc_handler - the text handler routine (described below)
1162 *
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001163 * extra1, extra2 - extra pointers usable by the proc handler routines
1164 *
1165 * Leaf nodes in the sysctl tree will be represented by a single file
1166 * under /proc; non-leaf nodes will be represented by directories.
1167 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001168 * There must be a proc_handler routine for any terminal nodes.
1169 * Several default handlers are available to cover common cases -
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001170 *
1171 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
1172 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
1173 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
1174 *
1175 * It is the handler's job to read the input buffer from user memory
1176 * and process it. The handler should return 0 on success.
1177 *
1178 * This routine returns %NULL on a failure to register, and a pointer
1179 * to the table header on success.
1180 */
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001181struct ctl_table_header *__register_sysctl_table(
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001182 struct ctl_table_set *set,
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001183 const char *path, struct ctl_table *table)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001184{
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001185 struct ctl_table_root *root = set->dir.header.root;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001186 struct ctl_table_header *header;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001187 const char *name, *nextname;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001188 struct ctl_dir *dir;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001189 struct ctl_table *entry;
1190 struct ctl_node *node;
1191 int nr_entries = 0;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001192
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001193 for (entry = table; entry->procname; entry++)
1194 nr_entries++;
1195
1196 header = kzalloc(sizeof(struct ctl_table_header) +
1197 sizeof(struct ctl_node)*nr_entries, GFP_KERNEL);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001198 if (!header)
1199 return NULL;
1200
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001201 node = (struct ctl_node *)(header + 1);
1202 init_header(header, root, set, node, table);
Eric W. Biederman7c60c482012-01-21 13:34:05 -08001203 if (sysctl_check_table(path, table))
1204 goto fail;
Eric W. Biederman8d6ecfc2012-01-06 11:55:30 -08001205
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001206 spin_lock(&sysctl_lock);
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001207 dir = &set->dir;
Eric W. Biederman60f126d2012-01-30 21:23:52 -08001208 /* Reference moved down the diretory tree get_subdir */
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001209 dir->header.nreg++;
1210 spin_unlock(&sysctl_lock);
1211
1212 /* Find the directory for the ctl_table */
1213 for (name = path; name; name = nextname) {
1214 int namelen;
1215 nextname = strchr(name, '/');
1216 if (nextname) {
1217 namelen = nextname - name;
1218 nextname++;
1219 } else {
1220 namelen = strlen(name);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001221 }
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001222 if (namelen == 0)
1223 continue;
1224
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001225 dir = get_subdir(dir, name, namelen);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001226 if (IS_ERR(dir))
1227 goto fail;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001228 }
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001229
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001230 spin_lock(&sysctl_lock);
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001231 if (insert_header(dir, header))
1232 goto fail_put_dir_locked;
1233
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001234 drop_sysctl_table(&dir->header);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001235 spin_unlock(&sysctl_lock);
1236
1237 return header;
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001238
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001239fail_put_dir_locked:
1240 drop_sysctl_table(&dir->header);
Eric W. Biederman7c60c482012-01-21 13:34:05 -08001241 spin_unlock(&sysctl_lock);
1242fail:
1243 kfree(header);
1244 dump_stack();
1245 return NULL;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001246}
1247
Eric W. Biedermanfea478d2012-01-20 21:47:03 -08001248/**
1249 * register_sysctl - register a sysctl table
1250 * @path: The path to the directory the sysctl table is in.
1251 * @table: the table structure
1252 *
1253 * Register a sysctl table. @table should be a filled in ctl_table
1254 * array. A completely 0 filled entry terminates the table.
1255 *
1256 * See __register_sysctl_table for more details.
1257 */
1258struct ctl_table_header *register_sysctl(const char *path, struct ctl_table *table)
1259{
1260 return __register_sysctl_table(&sysctl_table_root.default_set,
1261 path, table);
1262}
1263EXPORT_SYMBOL(register_sysctl);
1264
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001265static char *append_path(const char *path, char *pos, const char *name)
1266{
1267 int namelen;
1268 namelen = strlen(name);
1269 if (((pos - path) + namelen + 2) >= PATH_MAX)
1270 return NULL;
1271 memcpy(pos, name, namelen);
1272 pos[namelen] = '/';
1273 pos[namelen + 1] = '\0';
1274 pos += namelen + 1;
1275 return pos;
1276}
1277
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001278static int count_subheaders(struct ctl_table *table)
1279{
1280 int has_files = 0;
1281 int nr_subheaders = 0;
1282 struct ctl_table *entry;
1283
1284 /* special case: no directory and empty directory */
1285 if (!table || !table->procname)
1286 return 1;
1287
1288 for (entry = table; entry->procname; entry++) {
1289 if (entry->child)
1290 nr_subheaders += count_subheaders(entry->child);
1291 else
1292 has_files = 1;
1293 }
1294 return nr_subheaders + has_files;
1295}
1296
1297static int register_leaf_sysctl_tables(const char *path, char *pos,
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001298 struct ctl_table_header ***subheader, struct ctl_table_set *set,
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001299 struct ctl_table *table)
1300{
1301 struct ctl_table *ctl_table_arg = NULL;
1302 struct ctl_table *entry, *files;
1303 int nr_files = 0;
1304 int nr_dirs = 0;
1305 int err = -ENOMEM;
1306
1307 for (entry = table; entry->procname; entry++) {
1308 if (entry->child)
1309 nr_dirs++;
1310 else
1311 nr_files++;
1312 }
1313
1314 files = table;
1315 /* If there are mixed files and directories we need a new table */
1316 if (nr_dirs && nr_files) {
1317 struct ctl_table *new;
1318 files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1),
1319 GFP_KERNEL);
1320 if (!files)
1321 goto out;
1322
1323 ctl_table_arg = files;
1324 for (new = files, entry = table; entry->procname; entry++) {
1325 if (entry->child)
1326 continue;
1327 *new = *entry;
1328 new++;
1329 }
1330 }
1331
1332 /* Register everything except a directory full of subdirectories */
1333 if (nr_files || !nr_dirs) {
1334 struct ctl_table_header *header;
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001335 header = __register_sysctl_table(set, path, files);
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001336 if (!header) {
1337 kfree(ctl_table_arg);
1338 goto out;
1339 }
1340
1341 /* Remember if we need to free the file table */
1342 header->ctl_table_arg = ctl_table_arg;
1343 **subheader = header;
1344 (*subheader)++;
1345 }
1346
1347 /* Recurse into the subdirectories. */
1348 for (entry = table; entry->procname; entry++) {
1349 char *child_pos;
1350
1351 if (!entry->child)
1352 continue;
1353
1354 err = -ENAMETOOLONG;
1355 child_pos = append_path(path, pos, entry->procname);
1356 if (!child_pos)
1357 goto out;
1358
1359 err = register_leaf_sysctl_tables(path, child_pos, subheader,
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001360 set, entry->child);
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001361 pos[0] = '\0';
1362 if (err)
1363 goto out;
1364 }
1365 err = 0;
1366out:
1367 /* On failure our caller will unregister all registered subheaders */
1368 return err;
1369}
1370
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001371/**
1372 * __register_sysctl_paths - register a sysctl table hierarchy
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001373 * @set: Sysctl tree to register on
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001374 * @path: The path to the directory the sysctl table is in.
1375 * @table: the top-level table structure
1376 *
1377 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1378 * array. A completely 0 filled entry terminates the table.
1379 *
1380 * See __register_sysctl_table for more details.
1381 */
1382struct ctl_table_header *__register_sysctl_paths(
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001383 struct ctl_table_set *set,
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001384 const struct ctl_path *path, struct ctl_table *table)
1385{
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001386 struct ctl_table *ctl_table_arg = table;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001387 int nr_subheaders = count_subheaders(table);
1388 struct ctl_table_header *header = NULL, **subheaders, **subheader;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001389 const struct ctl_path *component;
1390 char *new_path, *pos;
1391
1392 pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
1393 if (!new_path)
1394 return NULL;
1395
1396 pos[0] = '\0';
1397 for (component = path; component->procname; component++) {
1398 pos = append_path(new_path, pos, component->procname);
1399 if (!pos)
1400 goto out;
1401 }
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001402 while (table->procname && table->child && !table[1].procname) {
1403 pos = append_path(new_path, pos, table->procname);
1404 if (!pos)
1405 goto out;
1406 table = table->child;
1407 }
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001408 if (nr_subheaders == 1) {
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001409 header = __register_sysctl_table(set, new_path, table);
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001410 if (header)
1411 header->ctl_table_arg = ctl_table_arg;
1412 } else {
1413 header = kzalloc(sizeof(*header) +
1414 sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
1415 if (!header)
1416 goto out;
1417
1418 subheaders = (struct ctl_table_header **) (header + 1);
1419 subheader = subheaders;
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001420 header->ctl_table_arg = ctl_table_arg;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001421
1422 if (register_leaf_sysctl_tables(new_path, pos, &subheader,
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001423 set, table))
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001424 goto err_register_leaves;
1425 }
1426
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001427out:
1428 kfree(new_path);
1429 return header;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001430
1431err_register_leaves:
1432 while (subheader > subheaders) {
1433 struct ctl_table_header *subh = *(--subheader);
1434 struct ctl_table *table = subh->ctl_table_arg;
1435 unregister_sysctl_table(subh);
1436 kfree(table);
1437 }
1438 kfree(header);
1439 header = NULL;
1440 goto out;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001441}
1442
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001443/**
1444 * register_sysctl_table_path - register a sysctl table hierarchy
1445 * @path: The path to the directory the sysctl table is in.
1446 * @table: the top-level table structure
1447 *
1448 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1449 * array. A completely 0 filled entry terminates the table.
1450 *
1451 * See __register_sysctl_paths for more details.
1452 */
1453struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
1454 struct ctl_table *table)
1455{
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001456 return __register_sysctl_paths(&sysctl_table_root.default_set,
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001457 path, table);
1458}
1459EXPORT_SYMBOL(register_sysctl_paths);
1460
1461/**
1462 * register_sysctl_table - register a sysctl table hierarchy
1463 * @table: the top-level table structure
1464 *
1465 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1466 * array. A completely 0 filled entry terminates the table.
1467 *
1468 * See register_sysctl_paths for more details.
1469 */
1470struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
1471{
1472 static const struct ctl_path null_path[] = { {} };
1473
1474 return register_sysctl_paths(null_path, table);
1475}
1476EXPORT_SYMBOL(register_sysctl_table);
1477
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001478static void put_links(struct ctl_table_header *header)
1479{
1480 struct ctl_table_set *root_set = &sysctl_table_root.default_set;
1481 struct ctl_table_root *root = header->root;
1482 struct ctl_dir *parent = header->parent;
1483 struct ctl_dir *core_parent;
1484 struct ctl_table *entry;
1485
1486 if (header->set == root_set)
1487 return;
1488
1489 core_parent = xlate_dir(root_set, parent);
1490 if (IS_ERR(core_parent))
1491 return;
1492
1493 for (entry = header->ctl_table; entry->procname; entry++) {
1494 struct ctl_table_header *link_head;
1495 struct ctl_table *link;
1496 const char *name = entry->procname;
1497
1498 link = find_entry(&link_head, core_parent, name, strlen(name));
1499 if (link &&
1500 ((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) ||
1501 (S_ISLNK(link->mode) && (link->data == root)))) {
1502 drop_sysctl_table(link_head);
1503 }
1504 else {
1505 printk(KERN_ERR "sysctl link missing during unregister: ");
1506 sysctl_print_dir(parent);
1507 printk(KERN_CONT "/%s\n", name);
1508 }
1509 }
1510}
1511
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001512static void drop_sysctl_table(struct ctl_table_header *header)
1513{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001514 struct ctl_dir *parent = header->parent;
1515
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001516 if (--header->nreg)
1517 return;
1518
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001519 put_links(header);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001520 start_unregistering(header);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001521 if (!--header->count)
1522 kfree_rcu(header, rcu);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001523
1524 if (parent)
1525 drop_sysctl_table(&parent->header);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001526}
1527
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001528/**
1529 * unregister_sysctl_table - unregister a sysctl table hierarchy
1530 * @header: the header returned from register_sysctl_table
1531 *
1532 * Unregisters the sysctl table and all children. proc entries may not
1533 * actually be removed until they are no longer used by anyone.
1534 */
1535void unregister_sysctl_table(struct ctl_table_header * header)
1536{
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001537 int nr_subheaders;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001538 might_sleep();
1539
1540 if (header == NULL)
1541 return;
1542
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001543 nr_subheaders = count_subheaders(header->ctl_table_arg);
1544 if (unlikely(nr_subheaders > 1)) {
1545 struct ctl_table_header **subheaders;
1546 int i;
1547
1548 subheaders = (struct ctl_table_header **)(header + 1);
1549 for (i = nr_subheaders -1; i >= 0; i--) {
1550 struct ctl_table_header *subh = subheaders[i];
1551 struct ctl_table *table = subh->ctl_table_arg;
1552 unregister_sysctl_table(subh);
1553 kfree(table);
1554 }
1555 kfree(header);
1556 return;
1557 }
1558
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001559 spin_lock(&sysctl_lock);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001560 drop_sysctl_table(header);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001561 spin_unlock(&sysctl_lock);
1562}
1563EXPORT_SYMBOL(unregister_sysctl_table);
1564
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001565void setup_sysctl_set(struct ctl_table_set *set,
Eric W. Biederman9eb47c22012-01-22 21:26:00 -08001566 struct ctl_table_root *root,
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001567 int (*is_seen)(struct ctl_table_set *))
1568{
Dan Carpenter13474402012-01-30 16:40:29 +03001569 memset(set, 0, sizeof(*set));
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001570 set->is_seen = is_seen;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001571 init_header(&set->dir.header, root, set, NULL, root_table);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001572}
1573
Eric W. Biederman97324cd2012-01-09 22:19:13 -08001574void retire_sysctl_set(struct ctl_table_set *set)
1575{
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001576 WARN_ON(!RB_EMPTY_ROOT(&set->dir.root));
Eric W. Biederman97324cd2012-01-09 22:19:13 -08001577}
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001578
Alexey Dobriyan1e0edd32008-10-17 05:07:44 +04001579int __init proc_sys_init(void)
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001580{
Alexey Dobriyane1675232008-10-03 00:23:32 +04001581 struct proc_dir_entry *proc_sys_root;
1582
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001583 proc_sys_root = proc_mkdir("sys", NULL);
Al Viro90434762008-07-15 08:54:06 -04001584 proc_sys_root->proc_iops = &proc_sys_dir_operations;
1585 proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001586 proc_sys_root->nlink = 0;
Eric W. Biedermande4e83bd2012-01-06 03:34:20 -08001587
1588 return sysctl_init();
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001589}