blob: 160d5781638ec6d127d54780dbd8b62324d1280f [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 = {
36 .default_set.list = LIST_HEAD_INIT(sysctl_table_root.default_set.dir.header.ctl_entry),
37 .default_set.dir.header = {
Eric W. Biederman7ec66d02011-12-29 08:24:29 -080038 {{.count = 1,
39 .nreg = 1,
40 .ctl_table = root_table,
41 .ctl_entry = LIST_HEAD_INIT(sysctl_table_root.default_set.list),}},
Eric W. Biederman0e47c992012-01-07 23:24:30 -080042 .ctl_table_arg = root_table,
Eric W. Biederman7ec66d02011-12-29 08:24:29 -080043 .root = &sysctl_table_root,
44 .set = &sysctl_table_root.default_set,
45 },
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -080046};
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -080047
48static DEFINE_SPINLOCK(sysctl_lock);
49
Eric W. Biederman7ec66d02011-12-29 08:24:29 -080050static void drop_sysctl_table(struct ctl_table_header *header);
Eric W. Biederman0e47c992012-01-07 23:24:30 -080051static int sysctl_follow_link(struct ctl_table_header **phead,
52 struct ctl_table **pentry, struct nsproxy *namespaces);
53static int insert_links(struct ctl_table_header *head);
54static void put_links(struct ctl_table_header *header);
Eric W. Biedermane54012c2012-01-18 22:57:15 -080055static int sysctl_check_dups(struct ctl_dir *dir, struct ctl_table *table);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -080056
Eric W. Biederman69801282012-01-21 20:09:45 -080057static void sysctl_print_dir(struct ctl_dir *dir)
58{
59 if (dir->header.parent)
60 sysctl_print_dir(dir->header.parent);
61 printk(KERN_CONT "%s/", dir->header.ctl_table[0].procname);
62}
63
Eric W. Biederman076c3ee2012-01-09 21:42:02 -080064static int namecmp(const char *name1, int len1, const char *name2, int len2)
65{
66 int minlen;
67 int cmp;
68
69 minlen = len1;
70 if (minlen > len2)
71 minlen = len2;
72
73 cmp = memcmp(name1, name2, minlen);
74 if (cmp == 0)
75 cmp = len1 - len2;
76 return cmp;
77}
78
79static struct ctl_table *find_entry(struct ctl_table_header **phead,
Eric W. Biederman0e47c992012-01-07 23:24:30 -080080 struct ctl_dir *dir, const char *name, int namelen)
Eric W. Biederman076c3ee2012-01-09 21:42:02 -080081{
Eric W. Biederman0e47c992012-01-07 23:24:30 -080082 struct ctl_table_set *set = dir->header.set;
Eric W. Biederman076c3ee2012-01-09 21:42:02 -080083 struct ctl_table_header *head;
84 struct ctl_table *entry;
85
Eric W. Biederman076c3ee2012-01-09 21:42:02 -080086 list_for_each_entry(head, &set->list, ctl_entry) {
87 if (head->unregistering)
88 continue;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -080089 if (head->parent != dir)
Eric W. Biederman076c3ee2012-01-09 21:42:02 -080090 continue;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -080091 for (entry = head->ctl_table; entry->procname; entry++) {
Eric W. Biederman076c3ee2012-01-09 21:42:02 -080092 const char *procname = entry->procname;
93 if (namecmp(procname, strlen(procname), name, namelen) == 0) {
94 *phead = head;
95 return entry;
96 }
97 }
98 }
99 return NULL;
100}
101
Eric W. Biedermane0d04522012-01-09 22:36:41 -0800102static void init_header(struct ctl_table_header *head,
103 struct ctl_table_root *root, struct ctl_table_set *set,
104 struct ctl_table *table)
105{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800106 head->ctl_table = table;
Eric W. Biedermane0d04522012-01-09 22:36:41 -0800107 head->ctl_table_arg = table;
108 INIT_LIST_HEAD(&head->ctl_entry);
109 head->used = 0;
110 head->count = 1;
111 head->nreg = 1;
112 head->unregistering = NULL;
113 head->root = root;
114 head->set = set;
115 head->parent = NULL;
116}
117
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800118static void erase_header(struct ctl_table_header *head)
119{
120 list_del_init(&head->ctl_entry);
121}
122
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800123static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800124{
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800125 int err;
126
Eric W. Biedermane54012c2012-01-18 22:57:15 -0800127 err = sysctl_check_dups(dir, header->ctl_table);
128 if (err)
129 return err;
130
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800131 dir->header.nreg++;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800132 header->parent = dir;
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800133 err = insert_links(header);
134 if (err)
135 goto fail_links;
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800136 list_add_tail(&header->ctl_entry, &header->set->list);
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800137 return 0;
138fail_links:
139 header->parent = NULL;
140 drop_sysctl_table(&dir->header);
141 return err;
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800142}
143
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800144/* called under sysctl_lock */
145static int use_table(struct ctl_table_header *p)
146{
147 if (unlikely(p->unregistering))
148 return 0;
149 p->used++;
150 return 1;
151}
152
153/* called under sysctl_lock */
154static void unuse_table(struct ctl_table_header *p)
155{
156 if (!--p->used)
157 if (unlikely(p->unregistering))
158 complete(p->unregistering);
159}
160
161/* called under sysctl_lock, will reacquire if has to wait */
162static void start_unregistering(struct ctl_table_header *p)
163{
164 /*
165 * if p->used is 0, nobody will ever touch that entry again;
166 * we'll eliminate all paths to it before dropping sysctl_lock
167 */
168 if (unlikely(p->used)) {
169 struct completion wait;
170 init_completion(&wait);
171 p->unregistering = &wait;
172 spin_unlock(&sysctl_lock);
173 wait_for_completion(&wait);
174 spin_lock(&sysctl_lock);
175 } else {
176 /* anything non-NULL; we'll never dereference it */
177 p->unregistering = ERR_PTR(-EINVAL);
178 }
179 /*
180 * do not remove from the list until nobody holds it; walking the
181 * list in do_sysctl() relies on that.
182 */
Eric W. Biederman8425d6a2012-01-09 17:35:01 -0800183 erase_header(p);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800184}
185
186static void sysctl_head_get(struct ctl_table_header *head)
187{
188 spin_lock(&sysctl_lock);
189 head->count++;
190 spin_unlock(&sysctl_lock);
191}
192
193void sysctl_head_put(struct ctl_table_header *head)
194{
195 spin_lock(&sysctl_lock);
196 if (!--head->count)
197 kfree_rcu(head, rcu);
198 spin_unlock(&sysctl_lock);
199}
200
201static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
202{
203 if (!head)
204 BUG();
205 spin_lock(&sysctl_lock);
206 if (!use_table(head))
207 head = ERR_PTR(-ENOENT);
208 spin_unlock(&sysctl_lock);
209 return head;
210}
211
212static void sysctl_head_finish(struct ctl_table_header *head)
213{
214 if (!head)
215 return;
216 spin_lock(&sysctl_lock);
217 unuse_table(head);
218 spin_unlock(&sysctl_lock);
219}
220
221static struct ctl_table_set *
222lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
223{
224 struct ctl_table_set *set = &root->default_set;
225 if (root->lookup)
226 set = root->lookup(root, namespaces);
227 return set;
228}
229
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800230static struct ctl_table *lookup_entry(struct ctl_table_header **phead,
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800231 struct ctl_dir *dir,
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800232 const char *name, int namelen)
233{
234 struct ctl_table_header *head;
235 struct ctl_table *entry;
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800236
237 spin_lock(&sysctl_lock);
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800238 entry = find_entry(&head, dir, name, namelen);
239 if (entry && use_table(head))
240 *phead = head;
241 else
242 entry = NULL;
Eric W. Biederman076c3ee2012-01-09 21:42:02 -0800243 spin_unlock(&sysctl_lock);
244 return entry;
245}
246
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800247static struct ctl_table_header *next_usable_entry(struct ctl_dir *dir,
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800248 struct list_head *tmp)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800249{
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800250 struct ctl_table_set *set = dir->header.set;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800251 struct ctl_table_header *head;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800252
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800253 for (tmp = tmp->next; tmp != &set->list; tmp = tmp->next) {
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800254 head = list_entry(tmp, struct ctl_table_header, ctl_entry);
255
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800256 if (head->parent != dir ||
257 !head->ctl_table->procname ||
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800258 !use_table(head))
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800259 continue;
260
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800261 return head;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800262 }
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800263 return NULL;
264}
265
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800266static void first_entry(struct ctl_dir *dir,
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800267 struct ctl_table_header **phead, struct ctl_table **pentry)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800268{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800269 struct ctl_table_header *head;
270 struct ctl_table *entry = NULL;
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800271
272 spin_lock(&sysctl_lock);
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800273 head = next_usable_entry(dir, &dir->header.set->list);
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800274 spin_unlock(&sysctl_lock);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800275 if (head)
276 entry = head->ctl_table;
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800277 *phead = head;
278 *pentry = entry;
279}
280
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800281static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry)
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800282{
283 struct ctl_table_header *head = *phead;
284 struct ctl_table *entry = *pentry;
285
286 entry++;
287 if (!entry->procname) {
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800288 spin_lock(&sysctl_lock);
289 unuse_table(head);
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800290 head = next_usable_entry(head->parent, &head->ctl_entry);
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800291 spin_unlock(&sysctl_lock);
292 if (head)
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800293 entry = head->ctl_table;
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800294 }
295 *phead = head;
296 *pentry = entry;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800297}
298
299void register_sysctl_root(struct ctl_table_root *root)
300{
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800301}
302
303/*
304 * sysctl_perm does NOT grant the superuser all rights automatically, because
305 * some sysctl variables are readonly even to root.
306 */
307
308static int test_perm(int mode, int op)
309{
310 if (!current_euid())
311 mode >>= 6;
312 else if (in_egroup_p(0))
313 mode >>= 3;
314 if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
315 return 0;
316 return -EACCES;
317}
318
319static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op)
320{
321 int mode;
322
323 if (root->permissions)
324 mode = root->permissions(root, current->nsproxy, table);
325 else
326 mode = table->mode;
327
328 return test_perm(mode, op);
329}
330
Al Viro90434762008-07-15 08:54:06 -0400331static struct inode *proc_sys_make_inode(struct super_block *sb,
332 struct ctl_table_header *head, struct ctl_table *table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800333{
334 struct inode *inode;
Al Viro90434762008-07-15 08:54:06 -0400335 struct proc_inode *ei;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800336
Al Viro90434762008-07-15 08:54:06 -0400337 inode = new_inode(sb);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800338 if (!inode)
339 goto out;
340
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400341 inode->i_ino = get_next_ino();
342
Al Viro90434762008-07-15 08:54:06 -0400343 sysctl_head_get(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800344 ei = PROC_I(inode);
Al Viro90434762008-07-15 08:54:06 -0400345 ei->sysctl = head;
346 ei->sysctl_entry = table;
347
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800348 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Al Viro90434762008-07-15 08:54:06 -0400349 inode->i_mode = table->mode;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800350 if (!S_ISDIR(table->mode)) {
Al Viro90434762008-07-15 08:54:06 -0400351 inode->i_mode |= S_IFREG;
352 inode->i_op = &proc_sys_inode_operations;
353 inode->i_fop = &proc_sys_file_operations;
354 } else {
355 inode->i_mode |= S_IFDIR;
Al Viro90434762008-07-15 08:54:06 -0400356 inode->i_op = &proc_sys_dir_operations;
357 inode->i_fop = &proc_sys_dir_file_operations;
358 }
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800359out:
360 return inode;
361}
362
Adrian Bunk81324362008-10-03 00:33:54 +0400363static struct ctl_table_header *grab_header(struct inode *inode)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800364{
Eric W. Biederman3cc3e042012-01-07 06:57:47 -0800365 struct ctl_table_header *head = PROC_I(inode)->sysctl;
366 if (!head)
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800367 head = &sysctl_table_root.default_set.dir.header;
Eric W. Biederman3cc3e042012-01-07 06:57:47 -0800368 return sysctl_head_grab(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800369}
370
371static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
372 struct nameidata *nd)
373{
Al Viro90434762008-07-15 08:54:06 -0400374 struct ctl_table_header *head = grab_header(dir);
Al Viro90434762008-07-15 08:54:06 -0400375 struct ctl_table_header *h = NULL;
376 struct qstr *name = &dentry->d_name;
377 struct ctl_table *p;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800378 struct inode *inode;
Al Viro90434762008-07-15 08:54:06 -0400379 struct dentry *err = ERR_PTR(-ENOENT);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800380 struct ctl_dir *ctl_dir;
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800381 int ret;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800382
Al Viro90434762008-07-15 08:54:06 -0400383 if (IS_ERR(head))
384 return ERR_CAST(head);
385
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800386 ctl_dir = container_of(head, struct ctl_dir, header);
Al Viro90434762008-07-15 08:54:06 -0400387
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800388 p = lookup_entry(&h, ctl_dir, name->name, name->len);
Al Viro90434762008-07-15 08:54:06 -0400389 if (!p)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800390 goto out;
391
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800392 ret = sysctl_follow_link(&h, &p, current->nsproxy);
393 err = ERR_PTR(ret);
394 if (ret)
395 goto out;
396
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800397 err = ERR_PTR(-ENOMEM);
Al Viro90434762008-07-15 08:54:06 -0400398 inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
399 if (h)
400 sysctl_head_finish(h);
401
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800402 if (!inode)
403 goto out;
404
405 err = NULL;
Nick Pigginfb045ad2011-01-07 17:49:55 +1100406 d_set_d_op(dentry, &proc_sys_dentry_operations);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800407 d_add(dentry, inode);
408
409out:
410 sysctl_head_finish(head);
411 return err;
412}
413
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700414static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
415 size_t count, loff_t *ppos, int write)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800416{
Al Viro90434762008-07-15 08:54:06 -0400417 struct inode *inode = filp->f_path.dentry->d_inode;
418 struct ctl_table_header *head = grab_header(inode);
419 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
David Howells2a2da532007-10-25 15:27:40 +0100420 ssize_t error;
421 size_t res;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800422
Al Viro90434762008-07-15 08:54:06 -0400423 if (IS_ERR(head))
424 return PTR_ERR(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800425
426 /*
427 * At this point we know that the sysctl was not unregistered
428 * and won't be until we finish.
429 */
430 error = -EPERM;
Pavel Emelyanovd7321cd2008-04-29 01:02:44 -0700431 if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800432 goto out;
433
Al Viro90434762008-07-15 08:54:06 -0400434 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
435 error = -EINVAL;
436 if (!table->proc_handler)
437 goto out;
438
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800439 /* careful: calling conventions are nasty here */
440 res = count;
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700441 error = table->proc_handler(table, write, buf, &res, ppos);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800442 if (!error)
443 error = res;
444out:
445 sysctl_head_finish(head);
446
447 return error;
448}
449
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700450static ssize_t proc_sys_read(struct file *filp, char __user *buf,
451 size_t count, loff_t *ppos)
452{
453 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
454}
455
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800456static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
457 size_t count, loff_t *ppos)
458{
Pavel Emelyanov7708bfb2008-04-29 01:02:40 -0700459 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800460}
461
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700462static int proc_sys_open(struct inode *inode, struct file *filp)
463{
464 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
465
466 if (table->poll)
467 filp->private_data = proc_sys_poll_event(table->poll);
468
469 return 0;
470}
471
472static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
473{
474 struct inode *inode = filp->f_path.dentry->d_inode;
475 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
476 unsigned long event = (unsigned long)filp->private_data;
477 unsigned int ret = DEFAULT_POLLMASK;
478
479 if (!table->proc_handler)
480 goto out;
481
482 if (!table->poll)
483 goto out;
484
485 poll_wait(filp, &table->poll->wait, wait);
486
487 if (event != atomic_read(&table->poll->event)) {
488 filp->private_data = proc_sys_poll_event(table->poll);
489 ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
490 }
491
492out:
493 return ret;
494}
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800495
496static int proc_sys_fill_cache(struct file *filp, void *dirent,
Al Viro90434762008-07-15 08:54:06 -0400497 filldir_t filldir,
498 struct ctl_table_header *head,
499 struct ctl_table *table)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800500{
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800501 struct dentry *child, *dir = filp->f_path.dentry;
502 struct inode *inode;
503 struct qstr qname;
504 ino_t ino = 0;
505 unsigned type = DT_UNKNOWN;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800506
507 qname.name = table->procname;
508 qname.len = strlen(table->procname);
509 qname.hash = full_name_hash(qname.name, qname.len);
510
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800511 child = d_lookup(dir, &qname);
512 if (!child) {
Al Viro90434762008-07-15 08:54:06 -0400513 child = d_alloc(dir, &qname);
514 if (child) {
515 inode = proc_sys_make_inode(dir->d_sb, head, table);
516 if (!inode) {
517 dput(child);
518 return -ENOMEM;
519 } else {
Nick Pigginfb045ad2011-01-07 17:49:55 +1100520 d_set_d_op(child, &proc_sys_dentry_operations);
Al Viro90434762008-07-15 08:54:06 -0400521 d_add(child, inode);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800522 }
Al Viro90434762008-07-15 08:54:06 -0400523 } else {
524 return -ENOMEM;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800525 }
526 }
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800527 inode = child->d_inode;
Al Viro90434762008-07-15 08:54:06 -0400528 ino = inode->i_ino;
529 type = inode->i_mode >> 12;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800530 dput(child);
Al Viro90434762008-07-15 08:54:06 -0400531 return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
532}
533
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800534static int proc_sys_link_fill_cache(struct file *filp, void *dirent,
535 filldir_t filldir,
536 struct ctl_table_header *head,
537 struct ctl_table *table)
538{
539 int err, ret = 0;
540 head = sysctl_head_grab(head);
541
542 /* It is not an error if we can not follow the link ignore it */
543 err = sysctl_follow_link(&head, &table, current->nsproxy);
544 if (err)
545 goto out;
546
547 ret = proc_sys_fill_cache(filp, dirent, filldir, head, table);
548out:
549 sysctl_head_finish(head);
550 return ret;
551}
552
Al Viro90434762008-07-15 08:54:06 -0400553static int scan(struct ctl_table_header *head, ctl_table *table,
554 unsigned long *pos, struct file *file,
555 void *dirent, filldir_t filldir)
556{
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800557 int res;
Al Viro90434762008-07-15 08:54:06 -0400558
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800559 if ((*pos)++ < file->f_pos)
560 return 0;
Al Viro90434762008-07-15 08:54:06 -0400561
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800562 if (unlikely(S_ISLNK(table->mode)))
563 res = proc_sys_link_fill_cache(file, dirent, filldir, head, table);
564 else
565 res = proc_sys_fill_cache(file, dirent, filldir, head, table);
Al Viro90434762008-07-15 08:54:06 -0400566
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800567 if (res == 0)
568 file->f_pos = *pos;
Al Viro90434762008-07-15 08:54:06 -0400569
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800570 return res;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800571}
572
573static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
574{
Al Viro90434762008-07-15 08:54:06 -0400575 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800576 struct inode *inode = dentry->d_inode;
Al Viro90434762008-07-15 08:54:06 -0400577 struct ctl_table_header *head = grab_header(inode);
Al Viro90434762008-07-15 08:54:06 -0400578 struct ctl_table_header *h = NULL;
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800579 struct ctl_table *entry;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800580 struct ctl_dir *ctl_dir;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800581 unsigned long pos;
Al Viro90434762008-07-15 08:54:06 -0400582 int ret = -EINVAL;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800583
Al Viro90434762008-07-15 08:54:06 -0400584 if (IS_ERR(head))
585 return PTR_ERR(head);
586
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800587 ctl_dir = container_of(head, struct ctl_dir, header);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800588
589 ret = 0;
590 /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
591 if (filp->f_pos == 0) {
592 if (filldir(dirent, ".", 1, filp->f_pos,
593 inode->i_ino, DT_DIR) < 0)
594 goto out;
595 filp->f_pos++;
596 }
597 if (filp->f_pos == 1) {
598 if (filldir(dirent, "..", 2, filp->f_pos,
599 parent_ino(dentry), DT_DIR) < 0)
600 goto out;
601 filp->f_pos++;
602 }
603 pos = 2;
604
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800605 for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) {
Eric W. Biederman6a75ce12012-01-18 03:15:51 -0800606 ret = scan(h, entry, &pos, filp, dirent, filldir);
Al Viro90434762008-07-15 08:54:06 -0400607 if (ret) {
608 sysctl_head_finish(h);
609 break;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800610 }
611 }
612 ret = 1;
613out:
614 sysctl_head_finish(head);
615 return ret;
616}
617
Al Viro10556cb2011-06-20 19:28:19 -0400618static int proc_sys_permission(struct inode *inode, int mask)
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800619{
620 /*
621 * sysctl entries that are not writeable,
622 * are _NOT_ writeable, capabilities or not.
623 */
Miklos Szeredif696a362008-07-31 13:41:58 +0200624 struct ctl_table_header *head;
625 struct ctl_table *table;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800626 int error;
627
Miklos Szeredif696a362008-07-31 13:41:58 +0200628 /* Executable files are not allowed under /proc/sys/ */
629 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
630 return -EACCES;
631
632 head = grab_header(inode);
Al Viro90434762008-07-15 08:54:06 -0400633 if (IS_ERR(head))
634 return PTR_ERR(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800635
Miklos Szeredif696a362008-07-31 13:41:58 +0200636 table = PROC_I(inode)->sysctl_entry;
Al Viro90434762008-07-15 08:54:06 -0400637 if (!table) /* global root - r-xr-xr-x */
638 error = mask & MAY_WRITE ? -EACCES : 0;
639 else /* Use the permissions on the sysctl table entry */
Al Viro1fc0f782011-06-20 18:59:02 -0400640 error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800641
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800642 sysctl_head_finish(head);
643 return error;
644}
645
646static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
647{
648 struct inode *inode = dentry->d_inode;
649 int error;
650
651 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
652 return -EPERM;
653
654 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200655 if (error)
656 return error;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800657
Christoph Hellwig10257742010-06-04 11:30:02 +0200658 if ((attr->ia_valid & ATTR_SIZE) &&
659 attr->ia_size != i_size_read(inode)) {
660 error = vmtruncate(inode, attr->ia_size);
661 if (error)
662 return error;
663 }
664
665 setattr_copy(inode, attr);
666 mark_inode_dirty(inode);
667 return 0;
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800668}
669
Al Viro90434762008-07-15 08:54:06 -0400670static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
671{
672 struct inode *inode = dentry->d_inode;
673 struct ctl_table_header *head = grab_header(inode);
674 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
675
676 if (IS_ERR(head))
677 return PTR_ERR(head);
678
679 generic_fillattr(inode, stat);
680 if (table)
681 stat->mode = (stat->mode & S_IFMT) | table->mode;
682
683 sysctl_head_finish(head);
684 return 0;
685}
686
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800687static const struct file_operations proc_sys_file_operations = {
Lucas De Marchif1ecf062011-11-02 13:39:22 -0700688 .open = proc_sys_open,
689 .poll = proc_sys_poll,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800690 .read = proc_sys_read,
691 .write = proc_sys_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200692 .llseek = default_llseek,
Al Viro90434762008-07-15 08:54:06 -0400693};
694
695static const struct file_operations proc_sys_dir_file_operations = {
Pavel Emelyanov887df072011-11-02 13:38:42 -0700696 .read = generic_read_dir,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800697 .readdir = proc_sys_readdir,
Christoph Hellwig3222a3e2008-09-03 21:53:01 +0200698 .llseek = generic_file_llseek,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800699};
700
Jan Engelhardt03a44822008-02-08 04:21:19 -0800701static const struct inode_operations proc_sys_inode_operations = {
Al Viro90434762008-07-15 08:54:06 -0400702 .permission = proc_sys_permission,
703 .setattr = proc_sys_setattr,
704 .getattr = proc_sys_getattr,
705};
706
707static const struct inode_operations proc_sys_dir_operations = {
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800708 .lookup = proc_sys_lookup,
709 .permission = proc_sys_permission,
710 .setattr = proc_sys_setattr,
Al Viro90434762008-07-15 08:54:06 -0400711 .getattr = proc_sys_getattr,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800712};
713
714static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
715{
Nick Piggin34286d62011-01-07 17:49:57 +1100716 if (nd->flags & LOOKUP_RCU)
717 return -ECHILD;
Al Viro90434762008-07-15 08:54:06 -0400718 return !PROC_I(dentry->d_inode)->sysctl->unregistering;
719}
720
Nick Pigginfe15ce42011-01-07 17:49:23 +1100721static int proc_sys_delete(const struct dentry *dentry)
Al Viro90434762008-07-15 08:54:06 -0400722{
723 return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
724}
725
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800726static int sysctl_is_seen(struct ctl_table_header *p)
727{
728 struct ctl_table_set *set = p->set;
729 int res;
730 spin_lock(&sysctl_lock);
731 if (p->unregistering)
732 res = 0;
733 else if (!set->is_seen)
734 res = 1;
735 else
736 res = set->is_seen(set);
737 spin_unlock(&sysctl_lock);
738 return res;
739}
740
Nick Piggin621e1552011-01-07 17:49:27 +1100741static int proc_sys_compare(const struct dentry *parent,
742 const struct inode *pinode,
743 const struct dentry *dentry, const struct inode *inode,
744 unsigned int len, const char *str, const struct qstr *name)
Al Viro90434762008-07-15 08:54:06 -0400745{
Al Virodfef6dc2011-03-08 01:25:28 -0500746 struct ctl_table_header *head;
Nick Piggin31e6b012011-01-07 17:49:52 +1100747 /* Although proc doesn't have negative dentries, rcu-walk means
748 * that inode here can be NULL */
Al Virodfef6dc2011-03-08 01:25:28 -0500749 /* AV: can it, indeed? */
Nick Piggin31e6b012011-01-07 17:49:52 +1100750 if (!inode)
Al Virodfef6dc2011-03-08 01:25:28 -0500751 return 1;
Nick Piggin621e1552011-01-07 17:49:27 +1100752 if (name->len != len)
Al Viro90434762008-07-15 08:54:06 -0400753 return 1;
Nick Piggin621e1552011-01-07 17:49:27 +1100754 if (memcmp(name->name, str, len))
Al Viro90434762008-07-15 08:54:06 -0400755 return 1;
Al Virodfef6dc2011-03-08 01:25:28 -0500756 head = rcu_dereference(PROC_I(inode)->sysctl);
757 return !head || !sysctl_is_seen(head);
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800758}
759
Al Virod72f71e2009-02-20 05:58:47 +0000760static const struct dentry_operations proc_sys_dentry_operations = {
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800761 .d_revalidate = proc_sys_revalidate,
Al Viro90434762008-07-15 08:54:06 -0400762 .d_delete = proc_sys_delete,
763 .d_compare = proc_sys_compare,
Eric W. Biederman77b14db2007-02-14 00:34:12 -0800764};
765
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800766static struct ctl_dir *find_subdir(struct ctl_dir *dir,
767 const char *name, int namelen)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800768{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800769 struct ctl_table_header *head;
770 struct ctl_table *entry;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800771
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800772 entry = find_entry(&head, dir, name, namelen);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800773 if (!entry)
774 return ERR_PTR(-ENOENT);
775 if (S_ISDIR(entry->mode))
776 return container_of(head, struct ctl_dir, header);
777 return ERR_PTR(-ENOTDIR);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800778}
779
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800780static struct ctl_dir *new_dir(struct ctl_table_set *set,
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800781 const char *name, int namelen)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800782{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800783 struct ctl_table *table;
784 struct ctl_dir *new;
785 char *new_name;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800786
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800787 new = kzalloc(sizeof(*new) + sizeof(struct ctl_table)*2 +
788 namelen + 1, GFP_KERNEL);
789 if (!new)
790 return NULL;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800791
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800792 table = (struct ctl_table *)(new + 1);
793 new_name = (char *)(table + 2);
794 memcpy(new_name, name, namelen);
795 new_name[namelen] = '\0';
796 table[0].procname = new_name;
797 table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800798 init_header(&new->header, set->dir.header.root, set, table);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800799
800 return new;
801}
802
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800803static struct ctl_dir *get_subdir(struct ctl_dir *dir,
804 const char *name, int namelen)
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800805{
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800806 struct ctl_table_set *set = dir->header.set;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800807 struct ctl_dir *subdir, *new = NULL;
808
809 spin_lock(&sysctl_lock);
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800810 subdir = find_subdir(dir, name, namelen);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800811 if (!IS_ERR(subdir))
812 goto found;
813 if (PTR_ERR(subdir) != -ENOENT)
814 goto failed;
815
816 spin_unlock(&sysctl_lock);
817 new = new_dir(set, name, namelen);
818 spin_lock(&sysctl_lock);
819 subdir = ERR_PTR(-ENOMEM);
820 if (!new)
821 goto failed;
822
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800823 subdir = find_subdir(dir, name, namelen);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800824 if (!IS_ERR(subdir))
825 goto found;
826 if (PTR_ERR(subdir) != -ENOENT)
827 goto failed;
828
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800829 if (insert_header(dir, &new->header))
830 goto failed;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800831 subdir = new;
832found:
833 subdir->header.nreg++;
834failed:
835 if (unlikely(IS_ERR(subdir))) {
Eric W. Biederman69801282012-01-21 20:09:45 -0800836 printk(KERN_ERR "sysctl could not get directory: ");
837 sysctl_print_dir(dir);
838 printk(KERN_CONT "/%*.*s %ld\n",
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800839 namelen, namelen, name, PTR_ERR(subdir));
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800840 }
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800841 drop_sysctl_table(&dir->header);
842 if (new)
843 drop_sysctl_table(&new->header);
844 spin_unlock(&sysctl_lock);
845 return subdir;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800846}
847
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800848static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir)
849{
850 struct ctl_dir *parent;
851 const char *procname;
852 if (!dir->header.parent)
853 return &set->dir;
854 parent = xlate_dir(set, dir->header.parent);
855 if (IS_ERR(parent))
856 return parent;
857 procname = dir->header.ctl_table[0].procname;
858 return find_subdir(parent, procname, strlen(procname));
859}
860
861static int sysctl_follow_link(struct ctl_table_header **phead,
862 struct ctl_table **pentry, struct nsproxy *namespaces)
863{
864 struct ctl_table_header *head;
865 struct ctl_table_root *root;
866 struct ctl_table_set *set;
867 struct ctl_table *entry;
868 struct ctl_dir *dir;
869 int ret;
870
871 /* Get out quickly if not a link */
872 if (!S_ISLNK((*pentry)->mode))
873 return 0;
874
875 ret = 0;
876 spin_lock(&sysctl_lock);
877 root = (*pentry)->data;
878 set = lookup_header_set(root, namespaces);
879 dir = xlate_dir(set, (*phead)->parent);
880 if (IS_ERR(dir))
881 ret = PTR_ERR(dir);
882 else {
883 const char *procname = (*pentry)->procname;
884 head = NULL;
885 entry = find_entry(&head, dir, procname, strlen(procname));
886 ret = -ENOENT;
887 if (entry && use_table(head)) {
888 unuse_table(*phead);
889 *phead = head;
890 *pentry = entry;
891 ret = 0;
892 }
893 }
894
895 spin_unlock(&sysctl_lock);
896 return ret;
897}
898
Eric W. Biedermane54012c2012-01-18 22:57:15 -0800899static int sysctl_check_table_dups(struct ctl_dir *dir, struct ctl_table *old,
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800900 struct ctl_table *table)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800901{
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800902 struct ctl_table *entry, *test;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800903 int error = 0;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800904
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800905 for (entry = old; entry->procname; entry++) {
906 for (test = table; test->procname; test++) {
907 if (strcmp(entry->procname, test->procname) == 0) {
Eric W. Biedermane54012c2012-01-18 22:57:15 -0800908 printk(KERN_ERR "sysctl duplicate entry: ");
909 sysctl_print_dir(dir);
910 printk(KERN_CONT "/%s\n", test->procname);
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800911 error = -EEXIST;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800912 }
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800913 }
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800914 }
915 return error;
916}
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800917
Eric W. Biedermane54012c2012-01-18 22:57:15 -0800918static int sysctl_check_dups(struct ctl_dir *dir, struct ctl_table *table)
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800919{
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800920 struct ctl_table_set *set;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -0800921 struct ctl_table_header *head;
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800922 int error = 0;
923
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800924 set = dir->header.set;
925 list_for_each_entry(head, &set->list, ctl_entry) {
926 if (head->unregistering)
927 continue;
928 if (head->parent != dir)
929 continue;
Eric W. Biedermane54012c2012-01-18 22:57:15 -0800930 error = sysctl_check_table_dups(dir, head->ctl_table, table);
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800931 }
Eric W. Biederman7c60c482012-01-21 13:34:05 -0800932 return error;
933}
934
935static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
936{
937 struct va_format vaf;
938 va_list args;
939
940 va_start(args, fmt);
941 vaf.fmt = fmt;
942 vaf.va = &args;
943
944 printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n",
945 path, table->procname, &vaf);
946
947 va_end(args);
948 return -EINVAL;
949}
950
951static int sysctl_check_table(const char *path, struct ctl_table *table)
952{
953 int err = 0;
954 for (; table->procname; table++) {
955 if (table->child)
956 err = sysctl_err(path, table, "Not a file");
957
958 if ((table->proc_handler == proc_dostring) ||
959 (table->proc_handler == proc_dointvec) ||
960 (table->proc_handler == proc_dointvec_minmax) ||
961 (table->proc_handler == proc_dointvec_jiffies) ||
962 (table->proc_handler == proc_dointvec_userhz_jiffies) ||
963 (table->proc_handler == proc_dointvec_ms_jiffies) ||
964 (table->proc_handler == proc_doulongvec_minmax) ||
965 (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
966 if (!table->data)
967 err = sysctl_err(path, table, "No data");
968 if (!table->maxlen)
969 err = sysctl_err(path, table, "No maxlen");
970 }
971 if (!table->proc_handler)
972 err = sysctl_err(path, table, "No proc_handler");
973
974 if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
975 err = sysctl_err(path, table, "bogus .mode 0%o",
976 table->mode);
977 }
978 return err;
979}
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -0800980
Eric W. Biederman0e47c992012-01-07 23:24:30 -0800981static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table *table,
982 struct ctl_table_root *link_root)
983{
984 struct ctl_table *link_table, *entry, *link;
985 struct ctl_table_header *links;
986 char *link_name;
987 int nr_entries, name_bytes;
988
989 name_bytes = 0;
990 nr_entries = 0;
991 for (entry = table; entry->procname; entry++) {
992 nr_entries++;
993 name_bytes += strlen(entry->procname) + 1;
994 }
995
996 links = kzalloc(sizeof(struct ctl_table_header) +
997 sizeof(struct ctl_table)*(nr_entries + 1) +
998 name_bytes,
999 GFP_KERNEL);
1000
1001 if (!links)
1002 return NULL;
1003
1004 link_table = (struct ctl_table *)(links + 1);
1005 link_name = (char *)&link_table[nr_entries + 1];
1006
1007 for (link = link_table, entry = table; entry->procname; link++, entry++) {
1008 int len = strlen(entry->procname) + 1;
1009 memcpy(link_name, entry->procname, len);
1010 link->procname = link_name;
1011 link->mode = S_IFLNK|S_IRWXUGO;
1012 link->data = link_root;
1013 link_name += len;
1014 }
1015 init_header(links, dir->header.root, dir->header.set, link_table);
1016 links->nreg = nr_entries;
1017
1018 return links;
1019}
1020
1021static bool get_links(struct ctl_dir *dir,
1022 struct ctl_table *table, struct ctl_table_root *link_root)
1023{
1024 struct ctl_table_header *head;
1025 struct ctl_table *entry, *link;
1026
1027 /* Are there links available for every entry in table? */
1028 for (entry = table; entry->procname; entry++) {
1029 const char *procname = entry->procname;
1030 link = find_entry(&head, dir, procname, strlen(procname));
1031 if (!link)
1032 return false;
1033 if (S_ISDIR(link->mode) && S_ISDIR(entry->mode))
1034 continue;
1035 if (S_ISLNK(link->mode) && (link->data == link_root))
1036 continue;
1037 return false;
1038 }
1039
1040 /* The checks passed. Increase the registration count on the links */
1041 for (entry = table; entry->procname; entry++) {
1042 const char *procname = entry->procname;
1043 link = find_entry(&head, dir, procname, strlen(procname));
1044 head->nreg++;
1045 }
1046 return true;
1047}
1048
1049static int insert_links(struct ctl_table_header *head)
1050{
1051 struct ctl_table_set *root_set = &sysctl_table_root.default_set;
1052 struct ctl_dir *core_parent = NULL;
1053 struct ctl_table_header *links;
1054 int err;
1055
1056 if (head->set == root_set)
1057 return 0;
1058
1059 core_parent = xlate_dir(root_set, head->parent);
1060 if (IS_ERR(core_parent))
1061 return 0;
1062
1063 if (get_links(core_parent, head->ctl_table, head->root))
1064 return 0;
1065
1066 core_parent->header.nreg++;
1067 spin_unlock(&sysctl_lock);
1068
1069 links = new_links(core_parent, head->ctl_table, head->root);
1070
1071 spin_lock(&sysctl_lock);
1072 err = -ENOMEM;
1073 if (!links)
1074 goto out;
1075
1076 err = 0;
1077 if (get_links(core_parent, head->ctl_table, head->root)) {
1078 kfree(links);
1079 goto out;
1080 }
1081
1082 err = insert_header(core_parent, links);
1083 if (err)
1084 kfree(links);
1085out:
1086 drop_sysctl_table(&core_parent->header);
1087 return err;
1088}
1089
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001090/**
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001091 * __register_sysctl_table - register a leaf sysctl table
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001092 * @set: Sysctl tree to register on
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001093 * @path: The path to the directory the sysctl table is in.
1094 * @table: the top-level table structure
1095 *
1096 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1097 * array. A completely 0 filled entry terminates the table.
1098 *
1099 * The members of the &struct ctl_table structure are used as follows:
1100 *
1101 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
1102 * enter a sysctl file
1103 *
1104 * data - a pointer to data for use by proc_handler
1105 *
1106 * maxlen - the maximum size in bytes of the data
1107 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001108 * mode - the file permissions for the /proc/sys file
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001109 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001110 * child - must be %NULL.
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001111 *
1112 * proc_handler - the text handler routine (described below)
1113 *
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001114 * extra1, extra2 - extra pointers usable by the proc handler routines
1115 *
1116 * Leaf nodes in the sysctl tree will be represented by a single file
1117 * under /proc; non-leaf nodes will be represented by directories.
1118 *
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001119 * There must be a proc_handler routine for any terminal nodes.
1120 * Several default handlers are available to cover common cases -
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001121 *
1122 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
1123 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
1124 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
1125 *
1126 * It is the handler's job to read the input buffer from user memory
1127 * and process it. The handler should return 0 on success.
1128 *
1129 * This routine returns %NULL on a failure to register, and a pointer
1130 * to the table header on success.
1131 */
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001132struct ctl_table_header *__register_sysctl_table(
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001133 struct ctl_table_set *set,
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001134 const char *path, struct ctl_table *table)
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001135{
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001136 struct ctl_table_root *root = set->dir.header.root;
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001137 struct ctl_table_header *links = NULL;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001138 struct ctl_table_header *header;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001139 const char *name, *nextname;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001140 struct ctl_dir *dir;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001141
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001142 header = kzalloc(sizeof(struct ctl_table_header), GFP_KERNEL);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001143 if (!header)
1144 return NULL;
1145
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001146 init_header(header, root, set, table);
Eric W. Biederman7c60c482012-01-21 13:34:05 -08001147 if (sysctl_check_table(path, table))
1148 goto fail;
Eric W. Biederman8d6ecfc2012-01-06 11:55:30 -08001149
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001150 spin_lock(&sysctl_lock);
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001151 dir = &set->dir;
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001152 dir->header.nreg++;
1153 spin_unlock(&sysctl_lock);
1154
1155 /* Find the directory for the ctl_table */
1156 for (name = path; name; name = nextname) {
1157 int namelen;
1158 nextname = strchr(name, '/');
1159 if (nextname) {
1160 namelen = nextname - name;
1161 nextname++;
1162 } else {
1163 namelen = strlen(name);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001164 }
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001165 if (namelen == 0)
1166 continue;
1167
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001168 dir = get_subdir(dir, name, namelen);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001169 if (IS_ERR(dir))
1170 goto fail;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001171 }
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001172
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001173 spin_lock(&sysctl_lock);
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001174 if (insert_header(dir, header))
1175 goto fail_put_dir_locked;
1176
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001177 drop_sysctl_table(&dir->header);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001178 spin_unlock(&sysctl_lock);
1179
1180 return header;
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001181
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001182fail_put_dir_locked:
1183 drop_sysctl_table(&dir->header);
Eric W. Biederman7c60c482012-01-21 13:34:05 -08001184 spin_unlock(&sysctl_lock);
1185fail:
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001186 kfree(links);
Eric W. Biederman7c60c482012-01-21 13:34:05 -08001187 kfree(header);
1188 dump_stack();
1189 return NULL;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001190}
1191
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001192static char *append_path(const char *path, char *pos, const char *name)
1193{
1194 int namelen;
1195 namelen = strlen(name);
1196 if (((pos - path) + namelen + 2) >= PATH_MAX)
1197 return NULL;
1198 memcpy(pos, name, namelen);
1199 pos[namelen] = '/';
1200 pos[namelen + 1] = '\0';
1201 pos += namelen + 1;
1202 return pos;
1203}
1204
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001205static int count_subheaders(struct ctl_table *table)
1206{
1207 int has_files = 0;
1208 int nr_subheaders = 0;
1209 struct ctl_table *entry;
1210
1211 /* special case: no directory and empty directory */
1212 if (!table || !table->procname)
1213 return 1;
1214
1215 for (entry = table; entry->procname; entry++) {
1216 if (entry->child)
1217 nr_subheaders += count_subheaders(entry->child);
1218 else
1219 has_files = 1;
1220 }
1221 return nr_subheaders + has_files;
1222}
1223
1224static int register_leaf_sysctl_tables(const char *path, char *pos,
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001225 struct ctl_table_header ***subheader, struct ctl_table_set *set,
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001226 struct ctl_table *table)
1227{
1228 struct ctl_table *ctl_table_arg = NULL;
1229 struct ctl_table *entry, *files;
1230 int nr_files = 0;
1231 int nr_dirs = 0;
1232 int err = -ENOMEM;
1233
1234 for (entry = table; entry->procname; entry++) {
1235 if (entry->child)
1236 nr_dirs++;
1237 else
1238 nr_files++;
1239 }
1240
1241 files = table;
1242 /* If there are mixed files and directories we need a new table */
1243 if (nr_dirs && nr_files) {
1244 struct ctl_table *new;
1245 files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1),
1246 GFP_KERNEL);
1247 if (!files)
1248 goto out;
1249
1250 ctl_table_arg = files;
1251 for (new = files, entry = table; entry->procname; entry++) {
1252 if (entry->child)
1253 continue;
1254 *new = *entry;
1255 new++;
1256 }
1257 }
1258
1259 /* Register everything except a directory full of subdirectories */
1260 if (nr_files || !nr_dirs) {
1261 struct ctl_table_header *header;
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001262 header = __register_sysctl_table(set, path, files);
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001263 if (!header) {
1264 kfree(ctl_table_arg);
1265 goto out;
1266 }
1267
1268 /* Remember if we need to free the file table */
1269 header->ctl_table_arg = ctl_table_arg;
1270 **subheader = header;
1271 (*subheader)++;
1272 }
1273
1274 /* Recurse into the subdirectories. */
1275 for (entry = table; entry->procname; entry++) {
1276 char *child_pos;
1277
1278 if (!entry->child)
1279 continue;
1280
1281 err = -ENAMETOOLONG;
1282 child_pos = append_path(path, pos, entry->procname);
1283 if (!child_pos)
1284 goto out;
1285
1286 err = register_leaf_sysctl_tables(path, child_pos, subheader,
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001287 set, entry->child);
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001288 pos[0] = '\0';
1289 if (err)
1290 goto out;
1291 }
1292 err = 0;
1293out:
1294 /* On failure our caller will unregister all registered subheaders */
1295 return err;
1296}
1297
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001298/**
1299 * __register_sysctl_paths - register a sysctl table hierarchy
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001300 * @set: Sysctl tree to register on
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001301 * @path: The path to the directory the sysctl table is in.
1302 * @table: the top-level table structure
1303 *
1304 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1305 * array. A completely 0 filled entry terminates the table.
1306 *
1307 * See __register_sysctl_table for more details.
1308 */
1309struct ctl_table_header *__register_sysctl_paths(
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001310 struct ctl_table_set *set,
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001311 const struct ctl_path *path, struct ctl_table *table)
1312{
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001313 struct ctl_table *ctl_table_arg = table;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001314 int nr_subheaders = count_subheaders(table);
1315 struct ctl_table_header *header = NULL, **subheaders, **subheader;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001316 const struct ctl_path *component;
1317 char *new_path, *pos;
1318
1319 pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
1320 if (!new_path)
1321 return NULL;
1322
1323 pos[0] = '\0';
1324 for (component = path; component->procname; component++) {
1325 pos = append_path(new_path, pos, component->procname);
1326 if (!pos)
1327 goto out;
1328 }
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001329 while (table->procname && table->child && !table[1].procname) {
1330 pos = append_path(new_path, pos, table->procname);
1331 if (!pos)
1332 goto out;
1333 table = table->child;
1334 }
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001335 if (nr_subheaders == 1) {
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001336 header = __register_sysctl_table(set, new_path, table);
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001337 if (header)
1338 header->ctl_table_arg = ctl_table_arg;
1339 } else {
1340 header = kzalloc(sizeof(*header) +
1341 sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
1342 if (!header)
1343 goto out;
1344
1345 subheaders = (struct ctl_table_header **) (header + 1);
1346 subheader = subheaders;
Eric W. Biedermanec6a5262012-01-21 12:35:23 -08001347 header->ctl_table_arg = ctl_table_arg;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001348
1349 if (register_leaf_sysctl_tables(new_path, pos, &subheader,
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001350 set, table))
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001351 goto err_register_leaves;
1352 }
1353
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001354out:
1355 kfree(new_path);
1356 return header;
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001357
1358err_register_leaves:
1359 while (subheader > subheaders) {
1360 struct ctl_table_header *subh = *(--subheader);
1361 struct ctl_table *table = subh->ctl_table_arg;
1362 unregister_sysctl_table(subh);
1363 kfree(table);
1364 }
1365 kfree(header);
1366 header = NULL;
1367 goto out;
Eric W. Biederman6e9d5162012-01-21 10:26:26 -08001368}
1369
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001370/**
1371 * register_sysctl_table_path - register a sysctl table hierarchy
1372 * @path: The path to the directory the sysctl table is in.
1373 * @table: the top-level table structure
1374 *
1375 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1376 * array. A completely 0 filled entry terminates the table.
1377 *
1378 * See __register_sysctl_paths for more details.
1379 */
1380struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
1381 struct ctl_table *table)
1382{
Eric W. Biederman60a47a22012-01-08 00:02:37 -08001383 return __register_sysctl_paths(&sysctl_table_root.default_set,
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001384 path, table);
1385}
1386EXPORT_SYMBOL(register_sysctl_paths);
1387
1388/**
1389 * register_sysctl_table - register a sysctl table hierarchy
1390 * @table: the top-level table structure
1391 *
1392 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1393 * array. A completely 0 filled entry terminates the table.
1394 *
1395 * See register_sysctl_paths for more details.
1396 */
1397struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
1398{
1399 static const struct ctl_path null_path[] = { {} };
1400
1401 return register_sysctl_paths(null_path, table);
1402}
1403EXPORT_SYMBOL(register_sysctl_table);
1404
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001405static void put_links(struct ctl_table_header *header)
1406{
1407 struct ctl_table_set *root_set = &sysctl_table_root.default_set;
1408 struct ctl_table_root *root = header->root;
1409 struct ctl_dir *parent = header->parent;
1410 struct ctl_dir *core_parent;
1411 struct ctl_table *entry;
1412
1413 if (header->set == root_set)
1414 return;
1415
1416 core_parent = xlate_dir(root_set, parent);
1417 if (IS_ERR(core_parent))
1418 return;
1419
1420 for (entry = header->ctl_table; entry->procname; entry++) {
1421 struct ctl_table_header *link_head;
1422 struct ctl_table *link;
1423 const char *name = entry->procname;
1424
1425 link = find_entry(&link_head, core_parent, name, strlen(name));
1426 if (link &&
1427 ((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) ||
1428 (S_ISLNK(link->mode) && (link->data == root)))) {
1429 drop_sysctl_table(link_head);
1430 }
1431 else {
1432 printk(KERN_ERR "sysctl link missing during unregister: ");
1433 sysctl_print_dir(parent);
1434 printk(KERN_CONT "/%s\n", name);
1435 }
1436 }
1437}
1438
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001439static void drop_sysctl_table(struct ctl_table_header *header)
1440{
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001441 struct ctl_dir *parent = header->parent;
1442
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001443 if (--header->nreg)
1444 return;
1445
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001446 put_links(header);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001447 start_unregistering(header);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001448 if (!--header->count)
1449 kfree_rcu(header, rcu);
Eric W. Biederman7ec66d02011-12-29 08:24:29 -08001450
1451 if (parent)
1452 drop_sysctl_table(&parent->header);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001453}
1454
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001455/**
1456 * unregister_sysctl_table - unregister a sysctl table hierarchy
1457 * @header: the header returned from register_sysctl_table
1458 *
1459 * Unregisters the sysctl table and all children. proc entries may not
1460 * actually be removed until they are no longer used by anyone.
1461 */
1462void unregister_sysctl_table(struct ctl_table_header * header)
1463{
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001464 int nr_subheaders;
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001465 might_sleep();
1466
1467 if (header == NULL)
1468 return;
1469
Eric W. Biedermanf7280192012-01-22 18:22:05 -08001470 nr_subheaders = count_subheaders(header->ctl_table_arg);
1471 if (unlikely(nr_subheaders > 1)) {
1472 struct ctl_table_header **subheaders;
1473 int i;
1474
1475 subheaders = (struct ctl_table_header **)(header + 1);
1476 for (i = nr_subheaders -1; i >= 0; i--) {
1477 struct ctl_table_header *subh = subheaders[i];
1478 struct ctl_table *table = subh->ctl_table_arg;
1479 unregister_sysctl_table(subh);
1480 kfree(table);
1481 }
1482 kfree(header);
1483 return;
1484 }
1485
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001486 spin_lock(&sysctl_lock);
Eric W. Biederman938aaa42012-01-09 17:24:30 -08001487 drop_sysctl_table(header);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001488 spin_unlock(&sysctl_lock);
1489}
1490EXPORT_SYMBOL(unregister_sysctl_table);
1491
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001492void setup_sysctl_set(struct ctl_table_set *set,
Eric W. Biederman9eb47c22012-01-22 21:26:00 -08001493 struct ctl_table_root *root,
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001494 int (*is_seen)(struct ctl_table_set *))
1495{
Eric W. Biederman0e47c992012-01-07 23:24:30 -08001496 memset(set, sizeof(*set), 0);
1497 INIT_LIST_HEAD(&set->list);
1498 set->is_seen = is_seen;
1499 init_header(&set->dir.header, root, set, root_table);
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001500}
1501
Eric W. Biederman97324cd2012-01-09 22:19:13 -08001502void retire_sysctl_set(struct ctl_table_set *set)
1503{
1504 WARN_ON(!list_empty(&set->list));
1505}
Eric W. Biederman1f87f0b2012-01-06 04:07:15 -08001506
Alexey Dobriyan1e0edd32008-10-17 05:07:44 +04001507int __init proc_sys_init(void)
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001508{
Alexey Dobriyane1675232008-10-03 00:23:32 +04001509 struct proc_dir_entry *proc_sys_root;
1510
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001511 proc_sys_root = proc_mkdir("sys", NULL);
Al Viro90434762008-07-15 08:54:06 -04001512 proc_sys_root->proc_iops = &proc_sys_dir_operations;
1513 proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001514 proc_sys_root->nlink = 0;
Eric W. Biedermande4e83bd2012-01-06 03:34:20 -08001515
1516 return sysctl_init();
Eric W. Biederman77b14db2007-02-14 00:34:12 -08001517}