blob: 722ec116208dcbdfb9e750b83c4bd83cddcf0d90 [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
76static struct ctl_table *find_entry(struct ctl_table_header **phead,
Eric W. Biederman0e47c992012-01-07 23:24:30 -080077 struct ctl_dir *dir, const char *name, int namelen)
Eric W. Biederman076c3ee2012-01-09 21:42:02 -080078{
79 struct ctl_table_header *head;
80 struct ctl_table *entry;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -080081 struct rb_node *node = dir->root.rb_node;
Eric W. Biederman076c3ee2012-01-09 21:42:02 -080082
Eric W. Biedermanac13ac62012-01-09 17:24:30 -080083 while (node)
84 {
85 struct ctl_node *ctl_node;
86 const char *procname;
87 int cmp;
88
89 ctl_node = rb_entry(node, struct ctl_node, node);
90 head = ctl_node->header;
91 entry = &head->ctl_table[ctl_node - head->node];
92 procname = entry->procname;
93
94 cmp = namecmp(name, namelen, procname, strlen(procname));
95 if (cmp < 0)
96 node = node->rb_left;
97 else if (cmp > 0)
98 node = node->rb_right;
99 else {
100 *phead = head;
101 return entry;
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800102 }
103 }
104 return NULL;
105}
106
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800107static int insert_entry(struct ctl_table_header *head, struct ctl_table *entry)
108{
109 struct rb_node *node = &head->node[entry - head->ctl_table].node;
110 struct rb_node **p = &head->parent->root.rb_node;
111 struct rb_node *parent = NULL;
112 const char *name = entry->procname;
113 int namelen = strlen(name);
114
115 while (*p) {
116 struct ctl_table_header *parent_head;
117 struct ctl_table *parent_entry;
118 struct ctl_node *parent_node;
119 const char *parent_name;
120 int cmp;
121
122 parent = *p;
123 parent_node = rb_entry(parent, struct ctl_node, node);
124 parent_head = parent_node->header;
125 parent_entry = &parent_head->ctl_table[parent_node - parent_head->node];
126 parent_name = parent_entry->procname;
127
128 cmp = namecmp(name, namelen, parent_name, strlen(parent_name));
129 if (cmp < 0)
130 p = &(*p)->rb_left;
131 else if (cmp > 0)
132 p = &(*p)->rb_right;
133 else {
134 printk(KERN_ERR "sysctl duplicate entry: ");
135 sysctl_print_dir(head->parent);
136 printk(KERN_CONT "/%s\n", entry->procname);
137 return -EEXIST;
138 }
139 }
140
141 rb_link_node(node, parent, p);
142 return 0;
143}
144
145static void erase_entry(struct ctl_table_header *head, struct ctl_table *entry)
146{
147 struct rb_node *node = &head->node[entry - head->ctl_table].node;
148
149 rb_erase(node, &head->parent->root);
150}
151
Eric W. Biedermane0d04522012-01-09 22:36:41 -0800152static void init_header(struct ctl_table_header *head,
153 struct ctl_table_root *root, struct ctl_table_set *set,
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800154 struct ctl_node *node, struct ctl_table *table)
Eric W. Biedermane0d04522012-01-09 22:36:41 -0800155{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800156 head->ctl_table = table;
Eric W. Biedermane0d04522012-01-09 22:36:41 -0800157 head->ctl_table_arg = table;
Eric W. Biedermane0d04522012-01-09 22:36:41 -0800158 head->used = 0;
159 head->count = 1;
160 head->nreg = 1;
161 head->unregistering = NULL;
162 head->root = root;
163 head->set = set;
164 head->parent = NULL;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800165 head->node = node;
166 if (node) {
167 struct ctl_table *entry;
168 for (entry = table; entry->procname; entry++, node++) {
169 rb_init_node(&node->node);
170 node->header = head;
171 }
172 }
Eric W. Biedermane0d04522012-01-09 22:36:41 -0800173}
174
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800175static void erase_header(struct ctl_table_header *head)
176{
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800177 struct ctl_table *entry;
178 for (entry = head->ctl_table; entry->procname; entry++)
179 erase_entry(head, entry);
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800180}
181
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800182static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800183{
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800184 struct ctl_table *entry;
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800185 int err;
186
187 dir->header.nreg++;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800188 header->parent = dir;
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800189 err = insert_links(header);
190 if (err)
191 goto fail_links;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800192 for (entry = header->ctl_table; entry->procname; entry++) {
193 err = insert_entry(header, entry);
194 if (err)
195 goto fail;
196 }
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800197 return 0;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800198fail:
199 erase_header(header);
200 put_links(header);
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800201fail_links:
202 header->parent = NULL;
203 drop_sysctl_table(&dir->header);
204 return err;
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800205}
206
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800207/* called under sysctl_lock */
208static int use_table(struct ctl_table_header *p)
209{
210 if (unlikely(p->unregistering))
211 return 0;
212 p->used++;
213 return 1;
214}
215
216/* called under sysctl_lock */
217static void unuse_table(struct ctl_table_header *p)
218{
219 if (!--p->used)
220 if (unlikely(p->unregistering))
221 complete(p->unregistering);
222}
223
224/* called under sysctl_lock, will reacquire if has to wait */
225static void start_unregistering(struct ctl_table_header *p)
226{
227 /*
228 * if p->used is 0, nobody will ever touch that entry again;
229 * we'll eliminate all paths to it before dropping sysctl_lock
230 */
231 if (unlikely(p->used)) {
232 struct completion wait;
233 init_completion(&wait);
234 p->unregistering = &wait;
235 spin_unlock(&sysctl_lock);
236 wait_for_completion(&wait);
237 spin_lock(&sysctl_lock);
238 } else {
239 /* anything non-NULL; we'll never dereference it */
240 p->unregistering = ERR_PTR(-EINVAL);
241 }
242 /*
243 * do not remove from the list until nobody holds it; walking the
244 * list in do_sysctl() relies on that.
245 */
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800246 erase_header(p);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800247}
248
249static void sysctl_head_get(struct ctl_table_header *head)
250{
251 spin_lock(&sysctl_lock);
252 head->count++;
253 spin_unlock(&sysctl_lock);
254}
255
256void sysctl_head_put(struct ctl_table_header *head)
257{
258 spin_lock(&sysctl_lock);
259 if (!--head->count)
260 kfree_rcu(head, rcu);
261 spin_unlock(&sysctl_lock);
262}
263
264static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
265{
266 if (!head)
267 BUG();
268 spin_lock(&sysctl_lock);
269 if (!use_table(head))
270 head = ERR_PTR(-ENOENT);
271 spin_unlock(&sysctl_lock);
272 return head;
273}
274
275static void sysctl_head_finish(struct ctl_table_header *head)
276{
277 if (!head)
278 return;
279 spin_lock(&sysctl_lock);
280 unuse_table(head);
281 spin_unlock(&sysctl_lock);
282}
283
284static struct ctl_table_set *
285lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
286{
287 struct ctl_table_set *set = &root->default_set;
288 if (root->lookup)
289 set = root->lookup(root, namespaces);
290 return set;
291}
292
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800293static struct ctl_table *lookup_entry(struct ctl_table_header **phead,
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800294 struct ctl_dir *dir,
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800295 const char *name, int namelen)
296{
297 struct ctl_table_header *head;
298 struct ctl_table *entry;
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800299
300 spin_lock(&sysctl_lock);
Eric W. 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{
371 if (!current_euid())
372 mode >>= 6;
373 else if (in_egroup_p(0))
374 mode >>= 3;
375 if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
376 return 0;
377 return -EACCES;
378}
379
380static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op)
381{
382 int mode;
383
384 if (root->permissions)
385 mode = root->permissions(root, current->nsproxy, table);
386 else
387 mode = table->mode;
388
389 return test_perm(mode, op);
390}
391
Al 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,
433 struct nameidata *nd)
434{
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. Biederman0e47c992012-01-07 23:24:30 -0800453 ret = sysctl_follow_link(&h, &p, current->nsproxy);
454 err = ERR_PTR(ret);
455 if (ret)
456 goto out;
457
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800458 err = ERR_PTR(-ENOMEM);
Al Viro90434762008-07-15 08:54:06 -0400459 inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
460 if (h)
461 sysctl_head_finish(h);
462
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800463 if (!inode)
464 goto out;
465
466 err = NULL;
Nick Pigginfb045ad2011-01-07 17:49:55 +1100467 d_set_d_op(dentry, &proc_sys_dentry_operations);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800468 d_add(dentry, inode);
469
470out:
471 sysctl_head_finish(head);
472 return err;
473}
474
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700475static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
476 size_t count, loff_t *ppos, int write)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800477{
Al Viro90434762008-07-15 08:54:06 -0400478 struct inode *inode = filp->f_path.dentry->d_inode;
479 struct ctl_table_header *head = grab_header(inode);
480 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
David Howells2a2da532007-10-25 15:27:40 +0100481 ssize_t error;
482 size_t res;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800483
Al Viro90434762008-07-15 08:54:06 -0400484 if (IS_ERR(head))
485 return PTR_ERR(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800486
487 /*
488 * At this point we know that the sysctl was not unregistered
489 * and won't be until we finish.
490 */
491 error = -EPERM;
Pavel Emelyanovd7321cd2008-04-29 01:02:44 -0700492 if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800493 goto out;
494
Al Viro90434762008-07-15 08:54:06 -0400495 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
496 error = -EINVAL;
497 if (!table->proc_handler)
498 goto out;
499
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800500 /* careful: calling conventions are nasty here */
501 res = count;
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700502 error = table->proc_handler(table, write, buf, &res, ppos);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800503 if (!error)
504 error = res;
505out:
506 sysctl_head_finish(head);
507
508 return error;
509}
510
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700511static ssize_t proc_sys_read(struct file *filp, char __user *buf,
512 size_t count, loff_t *ppos)
513{
514 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
515}
516
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800517static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
518 size_t count, loff_t *ppos)
519{
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700520 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800521}
522
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700523static int proc_sys_open(struct inode *inode, struct file *filp)
524{
525 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
526
527 if (table->poll)
528 filp->private_data = proc_sys_poll_event(table->poll);
529
530 return 0;
531}
532
533static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
534{
535 struct inode *inode = filp->f_path.dentry->d_inode;
536 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
537 unsigned long event = (unsigned long)filp->private_data;
538 unsigned int ret = DEFAULT_POLLMASK;
539
540 if (!table->proc_handler)
541 goto out;
542
543 if (!table->poll)
544 goto out;
545
546 poll_wait(filp, &table->poll->wait, wait);
547
548 if (event != atomic_read(&table->poll->event)) {
549 filp->private_data = proc_sys_poll_event(table->poll);
550 ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
551 }
552
553out:
554 return ret;
555}
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800556
557static int proc_sys_fill_cache(struct file *filp, void *dirent,
Al Viro90434762008-07-15 08:54:06 -0400558 filldir_t filldir,
559 struct ctl_table_header *head,
560 struct ctl_table *table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800561{
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800562 struct dentry *child, *dir = filp->f_path.dentry;
563 struct inode *inode;
564 struct qstr qname;
565 ino_t ino = 0;
566 unsigned type = DT_UNKNOWN;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800567
568 qname.name = table->procname;
569 qname.len = strlen(table->procname);
570 qname.hash = full_name_hash(qname.name, qname.len);
571
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800572 child = d_lookup(dir, &qname);
573 if (!child) {
Al Viro90434762008-07-15 08:54:06 -0400574 child = d_alloc(dir, &qname);
575 if (child) {
576 inode = proc_sys_make_inode(dir->d_sb, head, table);
577 if (!inode) {
578 dput(child);
579 return -ENOMEM;
580 } else {
Nick Pigginfb045ad2011-01-07 17:49:55 +1100581 d_set_d_op(child, &proc_sys_dentry_operations);
Al Viro90434762008-07-15 08:54:06 -0400582 d_add(child, inode);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800583 }
Al Viro90434762008-07-15 08:54:06 -0400584 } else {
585 return -ENOMEM;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800586 }
587 }
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800588 inode = child->d_inode;
Al Viro90434762008-07-15 08:54:06 -0400589 ino = inode->i_ino;
590 type = inode->i_mode >> 12;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800591 dput(child);
Al Viro90434762008-07-15 08:54:06 -0400592 return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
593}
594
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800595static int proc_sys_link_fill_cache(struct file *filp, void *dirent,
596 filldir_t filldir,
597 struct ctl_table_header *head,
598 struct ctl_table *table)
599{
600 int err, ret = 0;
601 head = sysctl_head_grab(head);
602
603 /* It is not an error if we can not follow the link ignore it */
604 err = sysctl_follow_link(&head, &table, current->nsproxy);
605 if (err)
606 goto out;
607
608 ret = proc_sys_fill_cache(filp, dirent, filldir, head, table);
609out:
610 sysctl_head_finish(head);
611 return ret;
612}
613
Al Viro90434762008-07-15 08:54:06 -0400614static int scan(struct ctl_table_header *head, ctl_table *table,
615 unsigned long *pos, struct file *file,
616 void *dirent, filldir_t filldir)
617{
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800618 int res;
Al Viro90434762008-07-15 08:54:06 -0400619
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800620 if ((*pos)++ < file->f_pos)
621 return 0;
Al Viro90434762008-07-15 08:54:06 -0400622
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800623 if (unlikely(S_ISLNK(table->mode)))
624 res = proc_sys_link_fill_cache(file, dirent, filldir, head, table);
625 else
626 res = proc_sys_fill_cache(file, dirent, filldir, head, table);
Al Viro90434762008-07-15 08:54:06 -0400627
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800628 if (res == 0)
629 file->f_pos = *pos;
Al Viro90434762008-07-15 08:54:06 -0400630
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800631 return res;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800632}
633
634static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
635{
Al Viro90434762008-07-15 08:54:06 -0400636 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800637 struct inode *inode = dentry->d_inode;
Al Viro90434762008-07-15 08:54:06 -0400638 struct ctl_table_header *head = grab_header(inode);
Al Viro90434762008-07-15 08:54:06 -0400639 struct ctl_table_header *h = NULL;
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800640 struct ctl_table *entry;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800641 struct ctl_dir *ctl_dir;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800642 unsigned long pos;
Al Viro90434762008-07-15 08:54:06 -0400643 int ret = -EINVAL;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800644
Al Viro90434762008-07-15 08:54:06 -0400645 if (IS_ERR(head))
646 return PTR_ERR(head);
647
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800648 ctl_dir = container_of(head, struct ctl_dir, header);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800649
650 ret = 0;
651 /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
652 if (filp->f_pos == 0) {
653 if (filldir(dirent, ".", 1, filp->f_pos,
654 inode->i_ino, DT_DIR) < 0)
655 goto out;
656 filp->f_pos++;
657 }
658 if (filp->f_pos == 1) {
659 if (filldir(dirent, "..", 2, filp->f_pos,
660 parent_ino(dentry), DT_DIR) < 0)
661 goto out;
662 filp->f_pos++;
663 }
664 pos = 2;
665
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800666 for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) {
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800667 ret = scan(h, entry, &pos, filp, dirent, filldir);
Al Viro90434762008-07-15 08:54:06 -0400668 if (ret) {
669 sysctl_head_finish(h);
670 break;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800671 }
672 }
673 ret = 1;
674out:
675 sysctl_head_finish(head);
676 return ret;
677}
678
Al Viro10556cb2011-06-20 19:28:19 -0400679static int proc_sys_permission(struct inode *inode, int mask)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800680{
681 /*
682 * sysctl entries that are not writeable,
683 * are _NOT_ writeable, capabilities or not.
684 */
Miklos Szeredif696a362008-07-31 13:41:58 +0200685 struct ctl_table_header *head;
686 struct ctl_table *table;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800687 int error;
688
Miklos Szeredif696a362008-07-31 13:41:58 +0200689 /* Executable files are not allowed under /proc/sys/ */
690 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
691 return -EACCES;
692
693 head = grab_header(inode);
Al Viro90434762008-07-15 08:54:06 -0400694 if (IS_ERR(head))
695 return PTR_ERR(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800696
Miklos Szeredif696a362008-07-31 13:41:58 +0200697 table = PROC_I(inode)->sysctl_entry;
Al Viro90434762008-07-15 08:54:06 -0400698 if (!table) /* global root - r-xr-xr-x */
699 error = mask & MAY_WRITE ? -EACCES : 0;
700 else /* Use the permissions on the sysctl table entry */
Al Viro1fc0f782011-06-20 18:59:02 -0400701 error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800702
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800703 sysctl_head_finish(head);
704 return error;
705}
706
707static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
708{
709 struct inode *inode = dentry->d_inode;
710 int error;
711
712 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
713 return -EPERM;
714
715 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200716 if (error)
717 return error;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800718
Christoph Hellwig10257742010-06-04 11:30:02 +0200719 if ((attr->ia_valid & ATTR_SIZE) &&
720 attr->ia_size != i_size_read(inode)) {
721 error = vmtruncate(inode, attr->ia_size);
722 if (error)
723 return error;
724 }
725
726 setattr_copy(inode, attr);
727 mark_inode_dirty(inode);
728 return 0;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800729}
730
Al Viro90434762008-07-15 08:54:06 -0400731static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
732{
733 struct inode *inode = dentry->d_inode;
734 struct ctl_table_header *head = grab_header(inode);
735 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
736
737 if (IS_ERR(head))
738 return PTR_ERR(head);
739
740 generic_fillattr(inode, stat);
741 if (table)
742 stat->mode = (stat->mode & S_IFMT) | table->mode;
743
744 sysctl_head_finish(head);
745 return 0;
746}
747
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800748static const struct file_operations proc_sys_file_operations = {
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700749 .open = proc_sys_open,
750 .poll = proc_sys_poll,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800751 .read = proc_sys_read,
752 .write = proc_sys_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200753 .llseek = default_llseek,
Al Viro90434762008-07-15 08:54:06 -0400754};
755
756static const struct file_operations proc_sys_dir_file_operations = {
Pavel Emelyanov887df072011-11-02 13:38:42 -0700757 .read = generic_read_dir,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800758 .readdir = proc_sys_readdir,
Christoph Hellwig3222a3e2008-09-03 21:53:01 +0200759 .llseek = generic_file_llseek,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800760};
761
Jan Engelhardt03a44822008-02-08 04:21:19 -0800762static const struct inode_operations proc_sys_inode_operations = {
Al Viro90434762008-07-15 08:54:06 -0400763 .permission = proc_sys_permission,
764 .setattr = proc_sys_setattr,
765 .getattr = proc_sys_getattr,
766};
767
768static const struct inode_operations proc_sys_dir_operations = {
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800769 .lookup = proc_sys_lookup,
770 .permission = proc_sys_permission,
771 .setattr = proc_sys_setattr,
Al Viro90434762008-07-15 08:54:06 -0400772 .getattr = proc_sys_getattr,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800773};
774
775static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
776{
Nick Piggin34286d62011-01-07 17:49:57 +1100777 if (nd->flags & LOOKUP_RCU)
778 return -ECHILD;
Al Viro90434762008-07-15 08:54:06 -0400779 return !PROC_I(dentry->d_inode)->sysctl->unregistering;
780}
781
Nick Pigginfe15ce42011-01-07 17:49:23 +1100782static int proc_sys_delete(const struct dentry *dentry)
Al Viro90434762008-07-15 08:54:06 -0400783{
784 return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
785}
786
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800787static int sysctl_is_seen(struct ctl_table_header *p)
788{
789 struct ctl_table_set *set = p->set;
790 int res;
791 spin_lock(&sysctl_lock);
792 if (p->unregistering)
793 res = 0;
794 else if (!set->is_seen)
795 res = 1;
796 else
797 res = set->is_seen(set);
798 spin_unlock(&sysctl_lock);
799 return res;
800}
801
Nick Piggin621e1552011-01-07 17:49:27 +1100802static int proc_sys_compare(const struct dentry *parent,
803 const struct inode *pinode,
804 const struct dentry *dentry, const struct inode *inode,
805 unsigned int len, const char *str, const struct qstr *name)
Al Viro90434762008-07-15 08:54:06 -0400806{
Al Virodfef6dc2011-03-08 01:25:28 -0500807 struct ctl_table_header *head;
Nick Piggin31e6b012011-01-07 17:49:52 +1100808 /* Although proc doesn't have negative dentries, rcu-walk means
809 * that inode here can be NULL */
Al Virodfef6dc2011-03-08 01:25:28 -0500810 /* AV: can it, indeed? */
Nick Piggin31e6b012011-01-07 17:49:52 +1100811 if (!inode)
Al Virodfef6dc2011-03-08 01:25:28 -0500812 return 1;
Nick Piggin621e1552011-01-07 17:49:27 +1100813 if (name->len != len)
Al Viro90434762008-07-15 08:54:06 -0400814 return 1;
Nick Piggin621e1552011-01-07 17:49:27 +1100815 if (memcmp(name->name, str, len))
Al Viro90434762008-07-15 08:54:06 -0400816 return 1;
Al Virodfef6dc2011-03-08 01:25:28 -0500817 head = rcu_dereference(PROC_I(inode)->sysctl);
818 return !head || !sysctl_is_seen(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800819}
820
Al Virod72f71e2009-02-20 05:58:47 +0000821static const struct dentry_operations proc_sys_dentry_operations = {
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800822 .d_revalidate = proc_sys_revalidate,
Al Viro90434762008-07-15 08:54:06 -0400823 .d_delete = proc_sys_delete,
824 .d_compare = proc_sys_compare,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800825};
826
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800827static struct ctl_dir *find_subdir(struct ctl_dir *dir,
828 const char *name, int namelen)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800829{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800830 struct ctl_table_header *head;
831 struct ctl_table *entry;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800832
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800833 entry = find_entry(&head, dir, name, namelen);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800834 if (!entry)
835 return ERR_PTR(-ENOENT);
Eric W. Biederman51f72f42012-01-30 20:09:33 -0800836 if (!S_ISDIR(entry->mode))
837 return ERR_PTR(-ENOTDIR);
838 return container_of(head, struct ctl_dir, header);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800839}
840
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800841static struct ctl_dir *new_dir(struct ctl_table_set *set,
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800842 const char *name, int namelen)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800843{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800844 struct ctl_table *table;
845 struct ctl_dir *new;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800846 struct ctl_node *node;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800847 char *new_name;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800848
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800849 new = kzalloc(sizeof(*new) + sizeof(struct ctl_node) +
850 sizeof(struct ctl_table)*2 + namelen + 1,
851 GFP_KERNEL);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800852 if (!new)
853 return NULL;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800854
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800855 node = (struct ctl_node *)(new + 1);
856 table = (struct ctl_table *)(node + 1);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800857 new_name = (char *)(table + 2);
858 memcpy(new_name, name, namelen);
859 new_name[namelen] = '\0';
860 table[0].procname = new_name;
861 table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800862 init_header(&new->header, set->dir.header.root, set, node, table);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800863
864 return new;
865}
866
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800867static struct ctl_dir *get_subdir(struct ctl_dir *dir,
868 const char *name, int namelen)
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800869{
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800870 struct ctl_table_set *set = dir->header.set;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800871 struct ctl_dir *subdir, *new = NULL;
Eric W. Biederman0eb97f32012-01-30 20:37:51 -0800872 int err;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800873
874 spin_lock(&sysctl_lock);
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800875 subdir = find_subdir(dir, name, namelen);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800876 if (!IS_ERR(subdir))
877 goto found;
878 if (PTR_ERR(subdir) != -ENOENT)
879 goto failed;
880
881 spin_unlock(&sysctl_lock);
882 new = new_dir(set, name, namelen);
883 spin_lock(&sysctl_lock);
884 subdir = ERR_PTR(-ENOMEM);
885 if (!new)
886 goto failed;
887
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800888 subdir = find_subdir(dir, name, namelen);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800889 if (!IS_ERR(subdir))
890 goto found;
891 if (PTR_ERR(subdir) != -ENOENT)
892 goto failed;
893
Eric W. Biederman0eb97f32012-01-30 20:37:51 -0800894 err = insert_header(dir, &new->header);
895 subdir = ERR_PTR(err);
896 if (err)
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800897 goto failed;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800898 subdir = new;
899found:
900 subdir->header.nreg++;
901failed:
902 if (unlikely(IS_ERR(subdir))) {
Eric W. Biederman69801282012-01-21 20:09:45 -0800903 printk(KERN_ERR "sysctl could not get directory: ");
904 sysctl_print_dir(dir);
905 printk(KERN_CONT "/%*.*s %ld\n",
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800906 namelen, namelen, name, PTR_ERR(subdir));
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800907 }
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800908 drop_sysctl_table(&dir->header);
909 if (new)
910 drop_sysctl_table(&new->header);
911 spin_unlock(&sysctl_lock);
912 return subdir;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800913}
914
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800915static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir)
916{
917 struct ctl_dir *parent;
918 const char *procname;
919 if (!dir->header.parent)
920 return &set->dir;
921 parent = xlate_dir(set, dir->header.parent);
922 if (IS_ERR(parent))
923 return parent;
924 procname = dir->header.ctl_table[0].procname;
925 return find_subdir(parent, procname, strlen(procname));
926}
927
928static int sysctl_follow_link(struct ctl_table_header **phead,
929 struct ctl_table **pentry, struct nsproxy *namespaces)
930{
931 struct ctl_table_header *head;
932 struct ctl_table_root *root;
933 struct ctl_table_set *set;
934 struct ctl_table *entry;
935 struct ctl_dir *dir;
936 int ret;
937
938 /* Get out quickly if not a link */
939 if (!S_ISLNK((*pentry)->mode))
940 return 0;
941
942 ret = 0;
943 spin_lock(&sysctl_lock);
944 root = (*pentry)->data;
945 set = lookup_header_set(root, namespaces);
946 dir = xlate_dir(set, (*phead)->parent);
947 if (IS_ERR(dir))
948 ret = PTR_ERR(dir);
949 else {
950 const char *procname = (*pentry)->procname;
951 head = NULL;
952 entry = find_entry(&head, dir, procname, strlen(procname));
953 ret = -ENOENT;
954 if (entry && use_table(head)) {
955 unuse_table(*phead);
956 *phead = head;
957 *pentry = entry;
958 ret = 0;
959 }
960 }
961
962 spin_unlock(&sysctl_lock);
963 return ret;
964}
965
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800966static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
967{
968 struct va_format vaf;
969 va_list args;
970
971 va_start(args, fmt);
972 vaf.fmt = fmt;
973 vaf.va = &args;
974
975 printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n",
976 path, table->procname, &vaf);
977
978 va_end(args);
979 return -EINVAL;
980}
981
982static int sysctl_check_table(const char *path, struct ctl_table *table)
983{
984 int err = 0;
985 for (; table->procname; table++) {
986 if (table->child)
987 err = sysctl_err(path, table, "Not a file");
988
989 if ((table->proc_handler == proc_dostring) ||
990 (table->proc_handler == proc_dointvec) ||
991 (table->proc_handler == proc_dointvec_minmax) ||
992 (table->proc_handler == proc_dointvec_jiffies) ||
993 (table->proc_handler == proc_dointvec_userhz_jiffies) ||
994 (table->proc_handler == proc_dointvec_ms_jiffies) ||
995 (table->proc_handler == proc_doulongvec_minmax) ||
996 (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
997 if (!table->data)
998 err = sysctl_err(path, table, "No data");
999 if (!table->maxlen)
1000 err = sysctl_err(path, table, "No maxlen");
1001 }
1002 if (!table->proc_handler)
1003 err = sysctl_err(path, table, "No proc_handler");
1004
1005 if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
1006 err = sysctl_err(path, table, "bogus .mode 0%o",
1007 table->mode);
1008 }
1009 return err;
1010}
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001011
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001012static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table *table,
1013 struct ctl_table_root *link_root)
1014{
1015 struct ctl_table *link_table, *entry, *link;
1016 struct ctl_table_header *links;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001017 struct ctl_node *node;
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001018 char *link_name;
1019 int nr_entries, name_bytes;
1020
1021 name_bytes = 0;
1022 nr_entries = 0;
1023 for (entry = table; entry->procname; entry++) {
1024 nr_entries++;
1025 name_bytes += strlen(entry->procname) + 1;
1026 }
1027
1028 links = kzalloc(sizeof(struct ctl_table_header) +
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001029 sizeof(struct ctl_node)*nr_entries +
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001030 sizeof(struct ctl_table)*(nr_entries + 1) +
1031 name_bytes,
1032 GFP_KERNEL);
1033
1034 if (!links)
1035 return NULL;
1036
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001037 node = (struct ctl_node *)(links + 1);
1038 link_table = (struct ctl_table *)(node + nr_entries);
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001039 link_name = (char *)&link_table[nr_entries + 1];
1040
1041 for (link = link_table, entry = table; entry->procname; link++, entry++) {
1042 int len = strlen(entry->procname) + 1;
1043 memcpy(link_name, entry->procname, len);
1044 link->procname = link_name;
1045 link->mode = S_IFLNK|S_IRWXUGO;
1046 link->data = link_root;
1047 link_name += len;
1048 }
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001049 init_header(links, dir->header.root, dir->header.set, node, link_table);
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001050 links->nreg = nr_entries;
1051
1052 return links;
1053}
1054
1055static bool get_links(struct ctl_dir *dir,
1056 struct ctl_table *table, struct ctl_table_root *link_root)
1057{
1058 struct ctl_table_header *head;
1059 struct ctl_table *entry, *link;
1060
1061 /* Are there links available for every entry in table? */
1062 for (entry = table; entry->procname; entry++) {
1063 const char *procname = entry->procname;
1064 link = find_entry(&head, dir, procname, strlen(procname));
1065 if (!link)
1066 return false;
1067 if (S_ISDIR(link->mode) && S_ISDIR(entry->mode))
1068 continue;
1069 if (S_ISLNK(link->mode) && (link->data == link_root))
1070 continue;
1071 return false;
1072 }
1073
1074 /* The checks passed. Increase the registration count on the links */
1075 for (entry = table; entry->procname; entry++) {
1076 const char *procname = entry->procname;
1077 link = find_entry(&head, dir, procname, strlen(procname));
1078 head->nreg++;
1079 }
1080 return true;
1081}
1082
1083static int insert_links(struct ctl_table_header *head)
1084{
1085 struct ctl_table_set *root_set = &sysctl_table_root.default_set;
1086 struct ctl_dir *core_parent = NULL;
1087 struct ctl_table_header *links;
1088 int err;
1089
1090 if (head->set == root_set)
1091 return 0;
1092
1093 core_parent = xlate_dir(root_set, head->parent);
1094 if (IS_ERR(core_parent))
1095 return 0;
1096
1097 if (get_links(core_parent, head->ctl_table, head->root))
1098 return 0;
1099
1100 core_parent->header.nreg++;
1101 spin_unlock(&sysctl_lock);
1102
1103 links = new_links(core_parent, head->ctl_table, head->root);
1104
1105 spin_lock(&sysctl_lock);
1106 err = -ENOMEM;
1107 if (!links)
1108 goto out;
1109
1110 err = 0;
1111 if (get_links(core_parent, head->ctl_table, head->root)) {
1112 kfree(links);
1113 goto out;
1114 }
1115
1116 err = insert_header(core_parent, links);
1117 if (err)
1118 kfree(links);
1119out:
1120 drop_sysctl_table(&core_parent->header);
1121 return err;
1122}
1123
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001124/**
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001125 * __register_sysctl_table - register a leaf sysctl table
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001126 * @set: Sysctl tree to register on
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001127 * @path: The path to the directory the sysctl table is in.
1128 * @table: the top-level table structure
1129 *
1130 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1131 * array. A completely 0 filled entry terminates the table.
1132 *
1133 * The members of the &struct ctl_table structure are used as follows:
1134 *
1135 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
1136 * enter a sysctl file
1137 *
1138 * data - a pointer to data for use by proc_handler
1139 *
1140 * maxlen - the maximum size in bytes of the data
1141 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001142 * mode - the file permissions for the /proc/sys file
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001143 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001144 * child - must be %NULL.
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001145 *
1146 * proc_handler - the text handler routine (described below)
1147 *
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001148 * extra1, extra2 - extra pointers usable by the proc handler routines
1149 *
1150 * Leaf nodes in the sysctl tree will be represented by a single file
1151 * under /proc; non-leaf nodes will be represented by directories.
1152 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001153 * There must be a proc_handler routine for any terminal nodes.
1154 * Several default handlers are available to cover common cases -
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001155 *
1156 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
1157 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
1158 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
1159 *
1160 * It is the handler's job to read the input buffer from user memory
1161 * and process it. The handler should return 0 on success.
1162 *
1163 * This routine returns %NULL on a failure to register, and a pointer
1164 * to the table header on success.
1165 */
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001166struct ctl_table_header *__register_sysctl_table(
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001167 struct ctl_table_set *set,
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001168 const char *path, struct ctl_table *table)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001169{
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001170 struct ctl_table_root *root = set->dir.header.root;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001171 struct ctl_table_header *header;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001172 const char *name, *nextname;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001173 struct ctl_dir *dir;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001174 struct ctl_table *entry;
1175 struct ctl_node *node;
1176 int nr_entries = 0;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001177
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001178 for (entry = table; entry->procname; entry++)
1179 nr_entries++;
1180
1181 header = kzalloc(sizeof(struct ctl_table_header) +
1182 sizeof(struct ctl_node)*nr_entries, GFP_KERNEL);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001183 if (!header)
1184 return NULL;
1185
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001186 node = (struct ctl_node *)(header + 1);
1187 init_header(header, root, set, node, table);
Eric W. Biederman7c60c482012-01-21 13:34:05 -08001188 if (sysctl_check_table(path, table))
1189 goto fail;
Eric W. Biederman8d6ecfc2012-01-06 11:55:30 -08001190
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001191 spin_lock(&sysctl_lock);
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001192 dir = &set->dir;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001193 dir->header.nreg++;
1194 spin_unlock(&sysctl_lock);
1195
1196 /* Find the directory for the ctl_table */
1197 for (name = path; name; name = nextname) {
1198 int namelen;
1199 nextname = strchr(name, '/');
1200 if (nextname) {
1201 namelen = nextname - name;
1202 nextname++;
1203 } else {
1204 namelen = strlen(name);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001205 }
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001206 if (namelen == 0)
1207 continue;
1208
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001209 dir = get_subdir(dir, name, namelen);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001210 if (IS_ERR(dir))
1211 goto fail;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001212 }
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001213
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001214 spin_lock(&sysctl_lock);
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001215 if (insert_header(dir, header))
1216 goto fail_put_dir_locked;
1217
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001218 drop_sysctl_table(&dir->header);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001219 spin_unlock(&sysctl_lock);
1220
1221 return header;
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001222
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001223fail_put_dir_locked:
1224 drop_sysctl_table(&dir->header);
Eric W. Biederman7c60c482012-01-21 13:34:05 -08001225 spin_unlock(&sysctl_lock);
1226fail:
1227 kfree(header);
1228 dump_stack();
1229 return NULL;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001230}
1231
Eric W. Biedermanfea478d2012-01-20 21:47:03 -08001232/**
1233 * register_sysctl - register a sysctl table
1234 * @path: The path to the directory the sysctl table is in.
1235 * @table: the table structure
1236 *
1237 * Register a sysctl table. @table should be a filled in ctl_table
1238 * array. A completely 0 filled entry terminates the table.
1239 *
1240 * See __register_sysctl_table for more details.
1241 */
1242struct ctl_table_header *register_sysctl(const char *path, struct ctl_table *table)
1243{
1244 return __register_sysctl_table(&sysctl_table_root.default_set,
1245 path, table);
1246}
1247EXPORT_SYMBOL(register_sysctl);
1248
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001249static char *append_path(const char *path, char *pos, const char *name)
1250{
1251 int namelen;
1252 namelen = strlen(name);
1253 if (((pos - path) + namelen + 2) >= PATH_MAX)
1254 return NULL;
1255 memcpy(pos, name, namelen);
1256 pos[namelen] = '/';
1257 pos[namelen + 1] = '\0';
1258 pos += namelen + 1;
1259 return pos;
1260}
1261
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001262static int count_subheaders(struct ctl_table *table)
1263{
1264 int has_files = 0;
1265 int nr_subheaders = 0;
1266 struct ctl_table *entry;
1267
1268 /* special case: no directory and empty directory */
1269 if (!table || !table->procname)
1270 return 1;
1271
1272 for (entry = table; entry->procname; entry++) {
1273 if (entry->child)
1274 nr_subheaders += count_subheaders(entry->child);
1275 else
1276 has_files = 1;
1277 }
1278 return nr_subheaders + has_files;
1279}
1280
1281static int register_leaf_sysctl_tables(const char *path, char *pos,
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001282 struct ctl_table_header ***subheader, struct ctl_table_set *set,
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001283 struct ctl_table *table)
1284{
1285 struct ctl_table *ctl_table_arg = NULL;
1286 struct ctl_table *entry, *files;
1287 int nr_files = 0;
1288 int nr_dirs = 0;
1289 int err = -ENOMEM;
1290
1291 for (entry = table; entry->procname; entry++) {
1292 if (entry->child)
1293 nr_dirs++;
1294 else
1295 nr_files++;
1296 }
1297
1298 files = table;
1299 /* If there are mixed files and directories we need a new table */
1300 if (nr_dirs && nr_files) {
1301 struct ctl_table *new;
1302 files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1),
1303 GFP_KERNEL);
1304 if (!files)
1305 goto out;
1306
1307 ctl_table_arg = files;
1308 for (new = files, entry = table; entry->procname; entry++) {
1309 if (entry->child)
1310 continue;
1311 *new = *entry;
1312 new++;
1313 }
1314 }
1315
1316 /* Register everything except a directory full of subdirectories */
1317 if (nr_files || !nr_dirs) {
1318 struct ctl_table_header *header;
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001319 header = __register_sysctl_table(set, path, files);
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001320 if (!header) {
1321 kfree(ctl_table_arg);
1322 goto out;
1323 }
1324
1325 /* Remember if we need to free the file table */
1326 header->ctl_table_arg = ctl_table_arg;
1327 **subheader = header;
1328 (*subheader)++;
1329 }
1330
1331 /* Recurse into the subdirectories. */
1332 for (entry = table; entry->procname; entry++) {
1333 char *child_pos;
1334
1335 if (!entry->child)
1336 continue;
1337
1338 err = -ENAMETOOLONG;
1339 child_pos = append_path(path, pos, entry->procname);
1340 if (!child_pos)
1341 goto out;
1342
1343 err = register_leaf_sysctl_tables(path, child_pos, subheader,
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001344 set, entry->child);
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001345 pos[0] = '\0';
1346 if (err)
1347 goto out;
1348 }
1349 err = 0;
1350out:
1351 /* On failure our caller will unregister all registered subheaders */
1352 return err;
1353}
1354
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001355/**
1356 * __register_sysctl_paths - register a sysctl table hierarchy
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001357 * @set: Sysctl tree to register on
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001358 * @path: The path to the directory the sysctl table is in.
1359 * @table: the top-level table structure
1360 *
1361 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1362 * array. A completely 0 filled entry terminates the table.
1363 *
1364 * See __register_sysctl_table for more details.
1365 */
1366struct ctl_table_header *__register_sysctl_paths(
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001367 struct ctl_table_set *set,
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001368 const struct ctl_path *path, struct ctl_table *table)
1369{
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001370 struct ctl_table *ctl_table_arg = table;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001371 int nr_subheaders = count_subheaders(table);
1372 struct ctl_table_header *header = NULL, **subheaders, **subheader;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001373 const struct ctl_path *component;
1374 char *new_path, *pos;
1375
1376 pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
1377 if (!new_path)
1378 return NULL;
1379
1380 pos[0] = '\0';
1381 for (component = path; component->procname; component++) {
1382 pos = append_path(new_path, pos, component->procname);
1383 if (!pos)
1384 goto out;
1385 }
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001386 while (table->procname && table->child && !table[1].procname) {
1387 pos = append_path(new_path, pos, table->procname);
1388 if (!pos)
1389 goto out;
1390 table = table->child;
1391 }
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001392 if (nr_subheaders == 1) {
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001393 header = __register_sysctl_table(set, new_path, table);
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001394 if (header)
1395 header->ctl_table_arg = ctl_table_arg;
1396 } else {
1397 header = kzalloc(sizeof(*header) +
1398 sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
1399 if (!header)
1400 goto out;
1401
1402 subheaders = (struct ctl_table_header **) (header + 1);
1403 subheader = subheaders;
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001404 header->ctl_table_arg = ctl_table_arg;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001405
1406 if (register_leaf_sysctl_tables(new_path, pos, &subheader,
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001407 set, table))
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001408 goto err_register_leaves;
1409 }
1410
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001411out:
1412 kfree(new_path);
1413 return header;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001414
1415err_register_leaves:
1416 while (subheader > subheaders) {
1417 struct ctl_table_header *subh = *(--subheader);
1418 struct ctl_table *table = subh->ctl_table_arg;
1419 unregister_sysctl_table(subh);
1420 kfree(table);
1421 }
1422 kfree(header);
1423 header = NULL;
1424 goto out;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001425}
1426
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001427/**
1428 * register_sysctl_table_path - register a sysctl table hierarchy
1429 * @path: The path to the directory the sysctl table is in.
1430 * @table: the top-level table structure
1431 *
1432 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1433 * array. A completely 0 filled entry terminates the table.
1434 *
1435 * See __register_sysctl_paths for more details.
1436 */
1437struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
1438 struct ctl_table *table)
1439{
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001440 return __register_sysctl_paths(&sysctl_table_root.default_set,
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001441 path, table);
1442}
1443EXPORT_SYMBOL(register_sysctl_paths);
1444
1445/**
1446 * register_sysctl_table - register a sysctl table hierarchy
1447 * @table: the top-level table structure
1448 *
1449 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1450 * array. A completely 0 filled entry terminates the table.
1451 *
1452 * See register_sysctl_paths for more details.
1453 */
1454struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
1455{
1456 static const struct ctl_path null_path[] = { {} };
1457
1458 return register_sysctl_paths(null_path, table);
1459}
1460EXPORT_SYMBOL(register_sysctl_table);
1461
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001462static void put_links(struct ctl_table_header *header)
1463{
1464 struct ctl_table_set *root_set = &sysctl_table_root.default_set;
1465 struct ctl_table_root *root = header->root;
1466 struct ctl_dir *parent = header->parent;
1467 struct ctl_dir *core_parent;
1468 struct ctl_table *entry;
1469
1470 if (header->set == root_set)
1471 return;
1472
1473 core_parent = xlate_dir(root_set, parent);
1474 if (IS_ERR(core_parent))
1475 return;
1476
1477 for (entry = header->ctl_table; entry->procname; entry++) {
1478 struct ctl_table_header *link_head;
1479 struct ctl_table *link;
1480 const char *name = entry->procname;
1481
1482 link = find_entry(&link_head, core_parent, name, strlen(name));
1483 if (link &&
1484 ((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) ||
1485 (S_ISLNK(link->mode) && (link->data == root)))) {
1486 drop_sysctl_table(link_head);
1487 }
1488 else {
1489 printk(KERN_ERR "sysctl link missing during unregister: ");
1490 sysctl_print_dir(parent);
1491 printk(KERN_CONT "/%s\n", name);
1492 }
1493 }
1494}
1495
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001496static void drop_sysctl_table(struct ctl_table_header *header)
1497{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001498 struct ctl_dir *parent = header->parent;
1499
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001500 if (--header->nreg)
1501 return;
1502
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001503 put_links(header);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001504 start_unregistering(header);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001505 if (!--header->count)
1506 kfree_rcu(header, rcu);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001507
1508 if (parent)
1509 drop_sysctl_table(&parent->header);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001510}
1511
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001512/**
1513 * unregister_sysctl_table - unregister a sysctl table hierarchy
1514 * @header: the header returned from register_sysctl_table
1515 *
1516 * Unregisters the sysctl table and all children. proc entries may not
1517 * actually be removed until they are no longer used by anyone.
1518 */
1519void unregister_sysctl_table(struct ctl_table_header * header)
1520{
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001521 int nr_subheaders;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001522 might_sleep();
1523
1524 if (header == NULL)
1525 return;
1526
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001527 nr_subheaders = count_subheaders(header->ctl_table_arg);
1528 if (unlikely(nr_subheaders > 1)) {
1529 struct ctl_table_header **subheaders;
1530 int i;
1531
1532 subheaders = (struct ctl_table_header **)(header + 1);
1533 for (i = nr_subheaders -1; i >= 0; i--) {
1534 struct ctl_table_header *subh = subheaders[i];
1535 struct ctl_table *table = subh->ctl_table_arg;
1536 unregister_sysctl_table(subh);
1537 kfree(table);
1538 }
1539 kfree(header);
1540 return;
1541 }
1542
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001543 spin_lock(&sysctl_lock);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001544 drop_sysctl_table(header);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001545 spin_unlock(&sysctl_lock);
1546}
1547EXPORT_SYMBOL(unregister_sysctl_table);
1548
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001549void setup_sysctl_set(struct ctl_table_set *set,
Eric W. Biederman9eb47c22012-01-22 21:26:00 -08001550 struct ctl_table_root *root,
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001551 int (*is_seen)(struct ctl_table_set *))
1552{
Dan Carpenter13474402012-01-30 16:40:29 +03001553 memset(set, 0, sizeof(*set));
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001554 set->is_seen = is_seen;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001555 init_header(&set->dir.header, root, set, NULL, root_table);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001556}
1557
Eric W. Biederman97324cd2012-01-09 22:19:13 -08001558void retire_sysctl_set(struct ctl_table_set *set)
1559{
Eric W. Biedermanac13ac62012-01-09 17:24:30 -08001560 WARN_ON(!RB_EMPTY_ROOT(&set->dir.root));
Eric W. Biederman97324cd2012-01-09 22:19:13 -08001561}
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001562
Alexey Dobriyan1e0edd32008-10-17 05:07:44 +04001563int __init proc_sys_init(void)
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001564{
Alexey Dobriyane1675232008-10-03 00:23:32 +04001565 struct proc_dir_entry *proc_sys_root;
1566
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001567 proc_sys_root = proc_mkdir("sys", NULL);
Al Viro90434762008-07-15 08:54:06 -04001568 proc_sys_root->proc_iops = &proc_sys_dir_operations;
1569 proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001570 proc_sys_root->nlink = 0;
Eric W. Biedermande4e83bd2012-01-06 03:34:20 -08001571
1572 return sysctl_init();
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001573}