blob: e5601dc240883d552bbb889e02d43279b8b05c64 [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. Biederman0e47c992012-01-07 23:24:30 -0800454 ret = sysctl_follow_link(&h, &p, current->nsproxy);
455 err = ERR_PTR(ret);
456 if (ret)
457 goto out;
458
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800459 err = ERR_PTR(-ENOMEM);
Al Viro90434762008-07-15 08:54:06 -0400460 inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
461 if (h)
462 sysctl_head_finish(h);
463
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800464 if (!inode)
465 goto out;
466
467 err = NULL;
Nick Pigginfb045ad2011-01-07 17:49:55 +1100468 d_set_d_op(dentry, &proc_sys_dentry_operations);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800469 d_add(dentry, inode);
470
471out:
472 sysctl_head_finish(head);
473 return err;
474}
475
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700476static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
477 size_t count, loff_t *ppos, int write)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800478{
Al Viro90434762008-07-15 08:54:06 -0400479 struct inode *inode = filp->f_path.dentry->d_inode;
480 struct ctl_table_header *head = grab_header(inode);
481 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
David Howells2a2da532007-10-25 15:27:40 +0100482 ssize_t error;
483 size_t res;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800484
Al Viro90434762008-07-15 08:54:06 -0400485 if (IS_ERR(head))
486 return PTR_ERR(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800487
488 /*
489 * At this point we know that the sysctl was not unregistered
490 * and won't be until we finish.
491 */
492 error = -EPERM;
Pavel Emelyanovd7321cd2008-04-29 01:02:44 -0700493 if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800494 goto out;
495
Al Viro90434762008-07-15 08:54:06 -0400496 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
497 error = -EINVAL;
498 if (!table->proc_handler)
499 goto out;
500
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800501 /* careful: calling conventions are nasty here */
502 res = count;
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700503 error = table->proc_handler(table, write, buf, &res, ppos);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800504 if (!error)
505 error = res;
506out:
507 sysctl_head_finish(head);
508
509 return error;
510}
511
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700512static ssize_t proc_sys_read(struct file *filp, char __user *buf,
513 size_t count, loff_t *ppos)
514{
515 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
516}
517
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800518static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
519 size_t count, loff_t *ppos)
520{
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700521 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800522}
523
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700524static int proc_sys_open(struct inode *inode, struct file *filp)
525{
526 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
527
528 if (table->poll)
529 filp->private_data = proc_sys_poll_event(table->poll);
530
531 return 0;
532}
533
534static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
535{
536 struct inode *inode = filp->f_path.dentry->d_inode;
537 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
538 unsigned long event = (unsigned long)filp->private_data;
539 unsigned int ret = DEFAULT_POLLMASK;
540
541 if (!table->proc_handler)
542 goto out;
543
544 if (!table->poll)
545 goto out;
546
547 poll_wait(filp, &table->poll->wait, wait);
548
549 if (event != atomic_read(&table->poll->event)) {
550 filp->private_data = proc_sys_poll_event(table->poll);
551 ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
552 }
553
554out:
555 return ret;
556}
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800557
558static int proc_sys_fill_cache(struct file *filp, void *dirent,
Al Viro90434762008-07-15 08:54:06 -0400559 filldir_t filldir,
560 struct ctl_table_header *head,
561 struct ctl_table *table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800562{
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800563 struct dentry *child, *dir = filp->f_path.dentry;
564 struct inode *inode;
565 struct qstr qname;
566 ino_t ino = 0;
567 unsigned type = DT_UNKNOWN;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800568
569 qname.name = table->procname;
570 qname.len = strlen(table->procname);
571 qname.hash = full_name_hash(qname.name, qname.len);
572
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800573 child = d_lookup(dir, &qname);
574 if (!child) {
Al Viro90434762008-07-15 08:54:06 -0400575 child = d_alloc(dir, &qname);
576 if (child) {
577 inode = proc_sys_make_inode(dir->d_sb, head, table);
578 if (!inode) {
579 dput(child);
580 return -ENOMEM;
581 } else {
Nick Pigginfb045ad2011-01-07 17:49:55 +1100582 d_set_d_op(child, &proc_sys_dentry_operations);
Al Viro90434762008-07-15 08:54:06 -0400583 d_add(child, inode);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800584 }
Al Viro90434762008-07-15 08:54:06 -0400585 } else {
586 return -ENOMEM;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800587 }
588 }
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800589 inode = child->d_inode;
Al Viro90434762008-07-15 08:54:06 -0400590 ino = inode->i_ino;
591 type = inode->i_mode >> 12;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800592 dput(child);
Al Viro90434762008-07-15 08:54:06 -0400593 return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
594}
595
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800596static int proc_sys_link_fill_cache(struct file *filp, void *dirent,
597 filldir_t filldir,
598 struct ctl_table_header *head,
599 struct ctl_table *table)
600{
601 int err, ret = 0;
602 head = sysctl_head_grab(head);
603
604 /* It is not an error if we can not follow the link ignore it */
605 err = sysctl_follow_link(&head, &table, current->nsproxy);
606 if (err)
607 goto out;
608
609 ret = proc_sys_fill_cache(filp, dirent, filldir, head, table);
610out:
611 sysctl_head_finish(head);
612 return ret;
613}
614
Al Viro90434762008-07-15 08:54:06 -0400615static int scan(struct ctl_table_header *head, ctl_table *table,
616 unsigned long *pos, struct file *file,
617 void *dirent, filldir_t filldir)
618{
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800619 int res;
Al Viro90434762008-07-15 08:54:06 -0400620
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800621 if ((*pos)++ < file->f_pos)
622 return 0;
Al Viro90434762008-07-15 08:54:06 -0400623
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800624 if (unlikely(S_ISLNK(table->mode)))
625 res = proc_sys_link_fill_cache(file, dirent, filldir, head, table);
626 else
627 res = proc_sys_fill_cache(file, dirent, filldir, head, table);
Al Viro90434762008-07-15 08:54:06 -0400628
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800629 if (res == 0)
630 file->f_pos = *pos;
Al Viro90434762008-07-15 08:54:06 -0400631
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800632 return res;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800633}
634
635static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
636{
Al Viro90434762008-07-15 08:54:06 -0400637 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800638 struct inode *inode = dentry->d_inode;
Al Viro90434762008-07-15 08:54:06 -0400639 struct ctl_table_header *head = grab_header(inode);
Al Viro90434762008-07-15 08:54:06 -0400640 struct ctl_table_header *h = NULL;
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800641 struct ctl_table *entry;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800642 struct ctl_dir *ctl_dir;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800643 unsigned long pos;
Al Viro90434762008-07-15 08:54:06 -0400644 int ret = -EINVAL;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800645
Al Viro90434762008-07-15 08:54:06 -0400646 if (IS_ERR(head))
647 return PTR_ERR(head);
648
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800649 ctl_dir = container_of(head, struct ctl_dir, header);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800650
651 ret = 0;
652 /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
653 if (filp->f_pos == 0) {
654 if (filldir(dirent, ".", 1, filp->f_pos,
655 inode->i_ino, DT_DIR) < 0)
656 goto out;
657 filp->f_pos++;
658 }
659 if (filp->f_pos == 1) {
660 if (filldir(dirent, "..", 2, filp->f_pos,
661 parent_ino(dentry), DT_DIR) < 0)
662 goto out;
663 filp->f_pos++;
664 }
665 pos = 2;
666
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800667 for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) {
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800668 ret = scan(h, entry, &pos, filp, dirent, filldir);
Al Viro90434762008-07-15 08:54:06 -0400669 if (ret) {
670 sysctl_head_finish(h);
671 break;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800672 }
673 }
674 ret = 1;
675out:
676 sysctl_head_finish(head);
677 return ret;
678}
679
Al Viro10556cb2011-06-20 19:28:19 -0400680static int proc_sys_permission(struct inode *inode, int mask)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800681{
682 /*
683 * sysctl entries that are not writeable,
684 * are _NOT_ writeable, capabilities or not.
685 */
Miklos Szeredif696a362008-07-31 13:41:58 +0200686 struct ctl_table_header *head;
687 struct ctl_table *table;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800688 int error;
689
Miklos Szeredif696a362008-07-31 13:41:58 +0200690 /* Executable files are not allowed under /proc/sys/ */
691 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
692 return -EACCES;
693
694 head = grab_header(inode);
Al Viro90434762008-07-15 08:54:06 -0400695 if (IS_ERR(head))
696 return PTR_ERR(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800697
Miklos Szeredif696a362008-07-31 13:41:58 +0200698 table = PROC_I(inode)->sysctl_entry;
Al Viro90434762008-07-15 08:54:06 -0400699 if (!table) /* global root - r-xr-xr-x */
700 error = mask & MAY_WRITE ? -EACCES : 0;
701 else /* Use the permissions on the sysctl table entry */
Al Viro1fc0f782011-06-20 18:59:02 -0400702 error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800703
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800704 sysctl_head_finish(head);
705 return error;
706}
707
708static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
709{
710 struct inode *inode = dentry->d_inode;
711 int error;
712
713 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
714 return -EPERM;
715
716 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200717 if (error)
718 return error;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800719
Christoph Hellwig10257742010-06-04 11:30:02 +0200720 if ((attr->ia_valid & ATTR_SIZE) &&
721 attr->ia_size != i_size_read(inode)) {
722 error = vmtruncate(inode, attr->ia_size);
723 if (error)
724 return error;
725 }
726
727 setattr_copy(inode, attr);
728 mark_inode_dirty(inode);
729 return 0;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800730}
731
Al Viro90434762008-07-15 08:54:06 -0400732static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
733{
734 struct inode *inode = dentry->d_inode;
735 struct ctl_table_header *head = grab_header(inode);
736 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
737
738 if (IS_ERR(head))
739 return PTR_ERR(head);
740
741 generic_fillattr(inode, stat);
742 if (table)
743 stat->mode = (stat->mode & S_IFMT) | table->mode;
744
745 sysctl_head_finish(head);
746 return 0;
747}
748
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800749static const struct file_operations proc_sys_file_operations = {
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700750 .open = proc_sys_open,
751 .poll = proc_sys_poll,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800752 .read = proc_sys_read,
753 .write = proc_sys_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200754 .llseek = default_llseek,
Al Viro90434762008-07-15 08:54:06 -0400755};
756
757static const struct file_operations proc_sys_dir_file_operations = {
Pavel Emelyanov887df072011-11-02 13:38:42 -0700758 .read = generic_read_dir,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800759 .readdir = proc_sys_readdir,
Christoph Hellwig3222a3e2008-09-03 21:53:01 +0200760 .llseek = generic_file_llseek,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800761};
762
Jan Engelhardt03a44822008-02-08 04:21:19 -0800763static const struct inode_operations proc_sys_inode_operations = {
Al Viro90434762008-07-15 08:54:06 -0400764 .permission = proc_sys_permission,
765 .setattr = proc_sys_setattr,
766 .getattr = proc_sys_getattr,
767};
768
769static const struct inode_operations proc_sys_dir_operations = {
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800770 .lookup = proc_sys_lookup,
771 .permission = proc_sys_permission,
772 .setattr = proc_sys_setattr,
Al Viro90434762008-07-15 08:54:06 -0400773 .getattr = proc_sys_getattr,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800774};
775
776static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
777{
Nick Piggin34286d62011-01-07 17:49:57 +1100778 if (nd->flags & LOOKUP_RCU)
779 return -ECHILD;
Al Viro90434762008-07-15 08:54:06 -0400780 return !PROC_I(dentry->d_inode)->sysctl->unregistering;
781}
782
Nick Pigginfe15ce42011-01-07 17:49:23 +1100783static int proc_sys_delete(const struct dentry *dentry)
Al Viro90434762008-07-15 08:54:06 -0400784{
785 return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
786}
787
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800788static int sysctl_is_seen(struct ctl_table_header *p)
789{
790 struct ctl_table_set *set = p->set;
791 int res;
792 spin_lock(&sysctl_lock);
793 if (p->unregistering)
794 res = 0;
795 else if (!set->is_seen)
796 res = 1;
797 else
798 res = set->is_seen(set);
799 spin_unlock(&sysctl_lock);
800 return res;
801}
802
Nick Piggin621e1552011-01-07 17:49:27 +1100803static int proc_sys_compare(const struct dentry *parent,
804 const struct inode *pinode,
805 const struct dentry *dentry, const struct inode *inode,
806 unsigned int len, const char *str, const struct qstr *name)
Al Viro90434762008-07-15 08:54:06 -0400807{
Al Virodfef6dc2011-03-08 01:25:28 -0500808 struct ctl_table_header *head;
Nick Piggin31e6b012011-01-07 17:49:52 +1100809 /* Although proc doesn't have negative dentries, rcu-walk means
810 * that inode here can be NULL */
Al Virodfef6dc2011-03-08 01:25:28 -0500811 /* AV: can it, indeed? */
Nick Piggin31e6b012011-01-07 17:49:52 +1100812 if (!inode)
Al Virodfef6dc2011-03-08 01:25:28 -0500813 return 1;
Nick Piggin621e1552011-01-07 17:49:27 +1100814 if (name->len != len)
Al Viro90434762008-07-15 08:54:06 -0400815 return 1;
Nick Piggin621e1552011-01-07 17:49:27 +1100816 if (memcmp(name->name, str, len))
Al Viro90434762008-07-15 08:54:06 -0400817 return 1;
Al Virodfef6dc2011-03-08 01:25:28 -0500818 head = rcu_dereference(PROC_I(inode)->sysctl);
819 return !head || !sysctl_is_seen(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800820}
821
Al Virod72f71e2009-02-20 05:58:47 +0000822static const struct dentry_operations proc_sys_dentry_operations = {
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800823 .d_revalidate = proc_sys_revalidate,
Al Viro90434762008-07-15 08:54:06 -0400824 .d_delete = proc_sys_delete,
825 .d_compare = proc_sys_compare,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800826};
827
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800828static struct ctl_dir *find_subdir(struct ctl_dir *dir,
829 const char *name, int namelen)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800830{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800831 struct ctl_table_header *head;
832 struct ctl_table *entry;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800833
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800834 entry = find_entry(&head, dir, name, namelen);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800835 if (!entry)
836 return ERR_PTR(-ENOENT);
Eric W. Biederman51f72f42012-01-30 20:09:33 -0800837 if (!S_ISDIR(entry->mode))
838 return ERR_PTR(-ENOTDIR);
839 return container_of(head, struct ctl_dir, header);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800840}
841
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800842static struct ctl_dir *new_dir(struct ctl_table_set *set,
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800843 const char *name, int namelen)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800844{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800845 struct ctl_table *table;
846 struct ctl_dir *new;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800847 struct ctl_node *node;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800848 char *new_name;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800849
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800850 new = kzalloc(sizeof(*new) + sizeof(struct ctl_node) +
851 sizeof(struct ctl_table)*2 + namelen + 1,
852 GFP_KERNEL);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800853 if (!new)
854 return NULL;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800855
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800856 node = (struct ctl_node *)(new + 1);
857 table = (struct ctl_table *)(node + 1);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800858 new_name = (char *)(table + 2);
859 memcpy(new_name, name, namelen);
860 new_name[namelen] = '\0';
861 table[0].procname = new_name;
862 table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
Eric W. Biedermanac13ac62012-01-09 17:24:30 -0800863 init_header(&new->header, set->dir.header.root, set, node, table);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800864
865 return new;
866}
867
Eric W. Biederman60f126d2012-01-30 21:23:52 -0800868/**
869 * get_subdir - find or create a subdir with the specified name.
870 * @dir: Directory to create the subdirectory in
871 * @name: The name of the subdirectory to find or create
872 * @namelen: The length of name
873 *
874 * Takes a directory with an elevated reference count so we know that
875 * if we drop the lock the directory will not go away. Upon success
876 * the reference is moved from @dir to the returned subdirectory.
877 * Upon error an error code is returned and the reference on @dir is
878 * simply dropped.
879 */
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800880static struct ctl_dir *get_subdir(struct ctl_dir *dir,
881 const char *name, int namelen)
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800882{
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800883 struct ctl_table_set *set = dir->header.set;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800884 struct ctl_dir *subdir, *new = NULL;
Eric W. Biederman0eb97f32012-01-30 20:37:51 -0800885 int err;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800886
887 spin_lock(&sysctl_lock);
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
894 spin_unlock(&sysctl_lock);
895 new = new_dir(set, name, namelen);
896 spin_lock(&sysctl_lock);
897 subdir = ERR_PTR(-ENOMEM);
898 if (!new)
899 goto failed;
900
Eric W. Biederman60f126d2012-01-30 21:23:52 -0800901 /* Was the subdir added while we dropped the lock? */
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800902 subdir = find_subdir(dir, name, namelen);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800903 if (!IS_ERR(subdir))
904 goto found;
905 if (PTR_ERR(subdir) != -ENOENT)
906 goto failed;
907
Eric W. Biederman60f126d2012-01-30 21:23:52 -0800908 /* Nope. Use the our freshly made directory entry. */
Eric W. Biederman0eb97f32012-01-30 20:37:51 -0800909 err = insert_header(dir, &new->header);
910 subdir = ERR_PTR(err);
911 if (err)
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800912 goto failed;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800913 subdir = new;
914found:
915 subdir->header.nreg++;
916failed:
917 if (unlikely(IS_ERR(subdir))) {
Eric W. Biederman69801282012-01-21 20:09:45 -0800918 printk(KERN_ERR "sysctl could not get directory: ");
919 sysctl_print_dir(dir);
920 printk(KERN_CONT "/%*.*s %ld\n",
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800921 namelen, namelen, name, PTR_ERR(subdir));
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800922 }
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800923 drop_sysctl_table(&dir->header);
924 if (new)
925 drop_sysctl_table(&new->header);
926 spin_unlock(&sysctl_lock);
927 return subdir;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800928}
929
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800930static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir)
931{
932 struct ctl_dir *parent;
933 const char *procname;
934 if (!dir->header.parent)
935 return &set->dir;
936 parent = xlate_dir(set, dir->header.parent);
937 if (IS_ERR(parent))
938 return parent;
939 procname = dir->header.ctl_table[0].procname;
940 return find_subdir(parent, procname, strlen(procname));
941}
942
943static int sysctl_follow_link(struct ctl_table_header **phead,
944 struct ctl_table **pentry, struct nsproxy *namespaces)
945{
946 struct ctl_table_header *head;
947 struct ctl_table_root *root;
948 struct ctl_table_set *set;
949 struct ctl_table *entry;
950 struct ctl_dir *dir;
951 int ret;
952
953 /* Get out quickly if not a link */
954 if (!S_ISLNK((*pentry)->mode))
955 return 0;
956
957 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}