]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/proc/proc_sysctl.c
sysctl: Add sysctl_print_dir and use it in get_subdir
[mirror_ubuntu-bionic-kernel.git] / fs / proc / proc_sysctl.c
CommitLineData
77b14db5
EB
1/*
2 * /proc/sys support
3 */
1e0edd3f 4#include <linux/init.h>
77b14db5 5#include <linux/sysctl.h>
f1ecf068 6#include <linux/poll.h>
77b14db5
EB
7#include <linux/proc_fs.h>
8#include <linux/security.h>
34286d66 9#include <linux/namei.h>
1f87f0b5 10#include <linux/module.h>
77b14db5
EB
11#include "internal.h"
12
d72f71eb 13static const struct dentry_operations proc_sys_dentry_operations;
77b14db5 14static const struct file_operations proc_sys_file_operations;
03a44825 15static const struct inode_operations proc_sys_inode_operations;
9043476f
AV
16static const struct file_operations proc_sys_dir_file_operations;
17static const struct inode_operations proc_sys_dir_operations;
77b14db5 18
f1ecf068
LDM
19void 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
a194558e
EB
28static struct ctl_table root_table[] = {
29 {
30 .procname = "",
7ec66d06 31 .mode = S_IFDIR|S_IRUGO|S_IXUGO,
a194558e
EB
32 },
33 { }
34};
1f87f0b5 35static struct ctl_table_root sysctl_table_root;
7ec66d06
EB
36static struct ctl_dir sysctl_root_dir = {
37 .header = {
38 {{.count = 1,
39 .nreg = 1,
40 .ctl_table = root_table,
41 .ctl_entry = LIST_HEAD_INIT(sysctl_table_root.default_set.list),}},
42 .root = &sysctl_table_root,
43 .set = &sysctl_table_root.default_set,
44 },
1f87f0b5
EB
45};
46static struct ctl_table_root sysctl_table_root = {
47 .root_list = LIST_HEAD_INIT(sysctl_table_root.root_list),
7ec66d06 48 .default_set.list = LIST_HEAD_INIT(sysctl_root_dir.header.ctl_entry),
9eb47c26 49 .default_set.root = &sysctl_table_root,
1f87f0b5
EB
50};
51
52static DEFINE_SPINLOCK(sysctl_lock);
53
7ec66d06
EB
54static void drop_sysctl_table(struct ctl_table_header *header);
55
6980128f
EB
56static void sysctl_print_dir(struct ctl_dir *dir)
57{
58 if (dir->header.parent)
59 sysctl_print_dir(dir->header.parent);
60 printk(KERN_CONT "%s/", dir->header.ctl_table[0].procname);
61}
62
076c3eed
EB
63static int namecmp(const char *name1, int len1, const char *name2, int len2)
64{
65 int minlen;
66 int cmp;
67
68 minlen = len1;
69 if (minlen > len2)
70 minlen = len2;
71
72 cmp = memcmp(name1, name2, minlen);
73 if (cmp == 0)
74 cmp = len1 - len2;
75 return cmp;
76}
77
78static struct ctl_table *find_entry(struct ctl_table_header **phead,
7ec66d06 79 struct ctl_table_set *set, struct ctl_dir *dir,
076c3eed
EB
80 const char *name, int namelen)
81{
82 struct ctl_table_header *head;
83 struct ctl_table *entry;
84
076c3eed
EB
85 list_for_each_entry(head, &set->list, ctl_entry) {
86 if (head->unregistering)
87 continue;
7ec66d06 88 if (head->parent != dir)
076c3eed 89 continue;
7ec66d06 90 for (entry = head->ctl_table; entry->procname; entry++) {
076c3eed
EB
91 const char *procname = entry->procname;
92 if (namecmp(procname, strlen(procname), name, namelen) == 0) {
93 *phead = head;
94 return entry;
95 }
96 }
97 }
98 return NULL;
99}
100
e0d04529
EB
101static void init_header(struct ctl_table_header *head,
102 struct ctl_table_root *root, struct ctl_table_set *set,
103 struct ctl_table *table)
104{
7ec66d06 105 head->ctl_table = table;
e0d04529
EB
106 head->ctl_table_arg = table;
107 INIT_LIST_HEAD(&head->ctl_entry);
108 head->used = 0;
109 head->count = 1;
110 head->nreg = 1;
111 head->unregistering = NULL;
112 head->root = root;
113 head->set = set;
114 head->parent = NULL;
115}
116
8425d6aa
EB
117static void erase_header(struct ctl_table_header *head)
118{
119 list_del_init(&head->ctl_entry);
120}
121
7ec66d06 122static void insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
8425d6aa 123{
7ec66d06
EB
124 header->parent = dir;
125 header->parent->header.nreg++;
8425d6aa
EB
126 list_add_tail(&header->ctl_entry, &header->set->list);
127}
128
1f87f0b5
EB
129/* called under sysctl_lock */
130static int use_table(struct ctl_table_header *p)
131{
132 if (unlikely(p->unregistering))
133 return 0;
134 p->used++;
135 return 1;
136}
137
138/* called under sysctl_lock */
139static void unuse_table(struct ctl_table_header *p)
140{
141 if (!--p->used)
142 if (unlikely(p->unregistering))
143 complete(p->unregistering);
144}
145
146/* called under sysctl_lock, will reacquire if has to wait */
147static void start_unregistering(struct ctl_table_header *p)
148{
149 /*
150 * if p->used is 0, nobody will ever touch that entry again;
151 * we'll eliminate all paths to it before dropping sysctl_lock
152 */
153 if (unlikely(p->used)) {
154 struct completion wait;
155 init_completion(&wait);
156 p->unregistering = &wait;
157 spin_unlock(&sysctl_lock);
158 wait_for_completion(&wait);
159 spin_lock(&sysctl_lock);
160 } else {
161 /* anything non-NULL; we'll never dereference it */
162 p->unregistering = ERR_PTR(-EINVAL);
163 }
164 /*
165 * do not remove from the list until nobody holds it; walking the
166 * list in do_sysctl() relies on that.
167 */
8425d6aa 168 erase_header(p);
1f87f0b5
EB
169}
170
171static void sysctl_head_get(struct ctl_table_header *head)
172{
173 spin_lock(&sysctl_lock);
174 head->count++;
175 spin_unlock(&sysctl_lock);
176}
177
178void sysctl_head_put(struct ctl_table_header *head)
179{
180 spin_lock(&sysctl_lock);
181 if (!--head->count)
182 kfree_rcu(head, rcu);
183 spin_unlock(&sysctl_lock);
184}
185
186static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
187{
188 if (!head)
189 BUG();
190 spin_lock(&sysctl_lock);
191 if (!use_table(head))
192 head = ERR_PTR(-ENOENT);
193 spin_unlock(&sysctl_lock);
194 return head;
195}
196
197static void sysctl_head_finish(struct ctl_table_header *head)
198{
199 if (!head)
200 return;
201 spin_lock(&sysctl_lock);
202 unuse_table(head);
203 spin_unlock(&sysctl_lock);
204}
205
206static struct ctl_table_set *
207lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
208{
209 struct ctl_table_set *set = &root->default_set;
210 if (root->lookup)
211 set = root->lookup(root, namespaces);
212 return set;
213}
214
215static struct list_head *
216lookup_header_list(struct ctl_table_root *root, struct nsproxy *namespaces)
217{
218 struct ctl_table_set *set = lookup_header_set(root, namespaces);
219 return &set->list;
220}
221
076c3eed 222static struct ctl_table *lookup_entry(struct ctl_table_header **phead,
7ec66d06 223 struct ctl_dir *dir,
076c3eed
EB
224 const char *name, int namelen)
225{
226 struct ctl_table_header *head;
227 struct ctl_table *entry;
228 struct ctl_table_root *root;
229 struct ctl_table_set *set;
230
231 spin_lock(&sysctl_lock);
232 root = &sysctl_table_root;
233 do {
234 set = lookup_header_set(root, current->nsproxy);
7ec66d06 235 entry = find_entry(&head, set, dir, name, namelen);
076c3eed
EB
236 if (entry && use_table(head))
237 *phead = head;
238 else
239 entry = NULL;
240 root = list_entry(root->root_list.next,
241 struct ctl_table_root, root_list);
242 } while (!entry && root != &sysctl_table_root);
243 spin_unlock(&sysctl_lock);
244 return entry;
245}
246
7ec66d06 247static struct ctl_table_header *next_usable_entry(struct ctl_dir *dir,
6a75ce16 248 struct ctl_table_root *root, struct list_head *tmp)
1f87f0b5 249{
6a75ce16 250 struct nsproxy *namespaces = current->nsproxy;
1f87f0b5
EB
251 struct list_head *header_list;
252 struct ctl_table_header *head;
1f87f0b5 253
6a75ce16 254 goto next;
1f87f0b5
EB
255 for (;;) {
256 head = list_entry(tmp, struct ctl_table_header, ctl_entry);
6a75ce16 257 root = head->root;
1f87f0b5 258
7ec66d06
EB
259 if (head->parent != dir ||
260 !head->ctl_table->procname ||
6a75ce16 261 !use_table(head))
1f87f0b5 262 goto next;
6a75ce16 263
1f87f0b5
EB
264 return head;
265 next:
1f87f0b5
EB
266 tmp = tmp->next;
267 header_list = lookup_header_list(root, namespaces);
268 if (tmp != header_list)
269 continue;
270
271 do {
272 root = list_entry(root->root_list.next,
273 struct ctl_table_root, root_list);
274 if (root == &sysctl_table_root)
275 goto out;
276 header_list = lookup_header_list(root, namespaces);
277 } while (list_empty(header_list));
278 tmp = header_list->next;
279 }
280out:
1f87f0b5
EB
281 return NULL;
282}
283
7ec66d06 284static void first_entry(struct ctl_dir *dir,
6a75ce16
EB
285 struct ctl_table_header **phead, struct ctl_table **pentry)
286{
7ec66d06
EB
287 struct ctl_table_header *head;
288 struct ctl_table *entry = NULL;
6a75ce16
EB
289
290 spin_lock(&sysctl_lock);
7ec66d06
EB
291 head = next_usable_entry(dir, &sysctl_table_root,
292 &sysctl_table_root.default_set.list);
6a75ce16 293 spin_unlock(&sysctl_lock);
7ec66d06
EB
294 if (head)
295 entry = head->ctl_table;
6a75ce16
EB
296 *phead = head;
297 *pentry = entry;
298}
299
7ec66d06 300static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry)
1f87f0b5 301{
6a75ce16
EB
302 struct ctl_table_header *head = *phead;
303 struct ctl_table *entry = *pentry;
304
305 entry++;
306 if (!entry->procname) {
6a75ce16
EB
307 spin_lock(&sysctl_lock);
308 unuse_table(head);
7ec66d06 309 head = next_usable_entry(head->parent, head->root, &head->ctl_entry);
6a75ce16
EB
310 spin_unlock(&sysctl_lock);
311 if (head)
7ec66d06 312 entry = head->ctl_table;
6a75ce16
EB
313 }
314 *phead = head;
315 *pentry = entry;
1f87f0b5
EB
316}
317
318void register_sysctl_root(struct ctl_table_root *root)
319{
320 spin_lock(&sysctl_lock);
321 list_add_tail(&root->root_list, &sysctl_table_root.root_list);
322 spin_unlock(&sysctl_lock);
323}
324
325/*
326 * sysctl_perm does NOT grant the superuser all rights automatically, because
327 * some sysctl variables are readonly even to root.
328 */
329
330static int test_perm(int mode, int op)
331{
332 if (!current_euid())
333 mode >>= 6;
334 else if (in_egroup_p(0))
335 mode >>= 3;
336 if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
337 return 0;
338 return -EACCES;
339}
340
341static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op)
342{
343 int mode;
344
345 if (root->permissions)
346 mode = root->permissions(root, current->nsproxy, table);
347 else
348 mode = table->mode;
349
350 return test_perm(mode, op);
351}
352
9043476f
AV
353static struct inode *proc_sys_make_inode(struct super_block *sb,
354 struct ctl_table_header *head, struct ctl_table *table)
77b14db5
EB
355{
356 struct inode *inode;
9043476f 357 struct proc_inode *ei;
77b14db5 358
9043476f 359 inode = new_inode(sb);
77b14db5
EB
360 if (!inode)
361 goto out;
362
85fe4025
CH
363 inode->i_ino = get_next_ino();
364
9043476f 365 sysctl_head_get(head);
77b14db5 366 ei = PROC_I(inode);
9043476f
AV
367 ei->sysctl = head;
368 ei->sysctl_entry = table;
369
77b14db5 370 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
9043476f 371 inode->i_mode = table->mode;
7ec66d06 372 if (!S_ISDIR(table->mode)) {
9043476f
AV
373 inode->i_mode |= S_IFREG;
374 inode->i_op = &proc_sys_inode_operations;
375 inode->i_fop = &proc_sys_file_operations;
376 } else {
377 inode->i_mode |= S_IFDIR;
9043476f
AV
378 inode->i_op = &proc_sys_dir_operations;
379 inode->i_fop = &proc_sys_dir_file_operations;
380 }
77b14db5
EB
381out:
382 return inode;
383}
384
81324364 385static struct ctl_table_header *grab_header(struct inode *inode)
77b14db5 386{
3cc3e046
EB
387 struct ctl_table_header *head = PROC_I(inode)->sysctl;
388 if (!head)
7ec66d06 389 head = &sysctl_root_dir.header;
3cc3e046 390 return sysctl_head_grab(head);
9043476f 391}
77b14db5 392
9043476f
AV
393static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
394 struct nameidata *nd)
395{
396 struct ctl_table_header *head = grab_header(dir);
9043476f
AV
397 struct ctl_table_header *h = NULL;
398 struct qstr *name = &dentry->d_name;
399 struct ctl_table *p;
400 struct inode *inode;
401 struct dentry *err = ERR_PTR(-ENOENT);
7ec66d06 402 struct ctl_dir *ctl_dir;
77b14db5 403
9043476f
AV
404 if (IS_ERR(head))
405 return ERR_CAST(head);
77b14db5 406
7ec66d06 407 ctl_dir = container_of(head, struct ctl_dir, header);
77b14db5 408
7ec66d06 409 p = lookup_entry(&h, ctl_dir, name->name, name->len);
9043476f 410 if (!p)
77b14db5
EB
411 goto out;
412
413 err = ERR_PTR(-ENOMEM);
9043476f
AV
414 inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
415 if (h)
416 sysctl_head_finish(h);
417
77b14db5
EB
418 if (!inode)
419 goto out;
420
421 err = NULL;
fb045adb 422 d_set_d_op(dentry, &proc_sys_dentry_operations);
77b14db5
EB
423 d_add(dentry, inode);
424
425out:
426 sysctl_head_finish(head);
427 return err;
428}
429
7708bfb1
PE
430static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
431 size_t count, loff_t *ppos, int write)
77b14db5 432{
9043476f
AV
433 struct inode *inode = filp->f_path.dentry->d_inode;
434 struct ctl_table_header *head = grab_header(inode);
435 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
2a2da53b
DH
436 ssize_t error;
437 size_t res;
77b14db5 438
9043476f
AV
439 if (IS_ERR(head))
440 return PTR_ERR(head);
77b14db5
EB
441
442 /*
443 * At this point we know that the sysctl was not unregistered
444 * and won't be until we finish.
445 */
446 error = -EPERM;
d7321cd6 447 if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
77b14db5
EB
448 goto out;
449
9043476f
AV
450 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
451 error = -EINVAL;
452 if (!table->proc_handler)
453 goto out;
454
77b14db5
EB
455 /* careful: calling conventions are nasty here */
456 res = count;
8d65af78 457 error = table->proc_handler(table, write, buf, &res, ppos);
77b14db5
EB
458 if (!error)
459 error = res;
460out:
461 sysctl_head_finish(head);
462
463 return error;
464}
465
7708bfb1 466static ssize_t proc_sys_read(struct file *filp, char __user *buf,
77b14db5
EB
467 size_t count, loff_t *ppos)
468{
7708bfb1
PE
469 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
470}
77b14db5 471
7708bfb1
PE
472static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
473 size_t count, loff_t *ppos)
474{
475 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
77b14db5
EB
476}
477
f1ecf068
LDM
478static int proc_sys_open(struct inode *inode, struct file *filp)
479{
480 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
481
482 if (table->poll)
483 filp->private_data = proc_sys_poll_event(table->poll);
484
485 return 0;
486}
487
488static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
489{
490 struct inode *inode = filp->f_path.dentry->d_inode;
491 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
492 unsigned long event = (unsigned long)filp->private_data;
493 unsigned int ret = DEFAULT_POLLMASK;
494
495 if (!table->proc_handler)
496 goto out;
497
498 if (!table->poll)
499 goto out;
500
501 poll_wait(filp, &table->poll->wait, wait);
502
503 if (event != atomic_read(&table->poll->event)) {
504 filp->private_data = proc_sys_poll_event(table->poll);
505 ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
506 }
507
508out:
509 return ret;
510}
77b14db5
EB
511
512static int proc_sys_fill_cache(struct file *filp, void *dirent,
9043476f
AV
513 filldir_t filldir,
514 struct ctl_table_header *head,
515 struct ctl_table *table)
77b14db5 516{
77b14db5
EB
517 struct dentry *child, *dir = filp->f_path.dentry;
518 struct inode *inode;
519 struct qstr qname;
520 ino_t ino = 0;
521 unsigned type = DT_UNKNOWN;
77b14db5
EB
522
523 qname.name = table->procname;
524 qname.len = strlen(table->procname);
525 qname.hash = full_name_hash(qname.name, qname.len);
526
77b14db5
EB
527 child = d_lookup(dir, &qname);
528 if (!child) {
9043476f
AV
529 child = d_alloc(dir, &qname);
530 if (child) {
531 inode = proc_sys_make_inode(dir->d_sb, head, table);
532 if (!inode) {
533 dput(child);
534 return -ENOMEM;
535 } else {
fb045adb 536 d_set_d_op(child, &proc_sys_dentry_operations);
9043476f 537 d_add(child, inode);
77b14db5 538 }
9043476f
AV
539 } else {
540 return -ENOMEM;
77b14db5
EB
541 }
542 }
77b14db5 543 inode = child->d_inode;
9043476f
AV
544 ino = inode->i_ino;
545 type = inode->i_mode >> 12;
77b14db5 546 dput(child);
9043476f
AV
547 return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
548}
549
550static int scan(struct ctl_table_header *head, ctl_table *table,
551 unsigned long *pos, struct file *file,
552 void *dirent, filldir_t filldir)
553{
6a75ce16 554 int res;
9043476f 555
6a75ce16
EB
556 if ((*pos)++ < file->f_pos)
557 return 0;
9043476f 558
6a75ce16 559 res = proc_sys_fill_cache(file, dirent, filldir, head, table);
9043476f 560
6a75ce16
EB
561 if (res == 0)
562 file->f_pos = *pos;
9043476f 563
6a75ce16 564 return res;
77b14db5
EB
565}
566
567static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
568{
9043476f 569 struct dentry *dentry = filp->f_path.dentry;
77b14db5 570 struct inode *inode = dentry->d_inode;
9043476f 571 struct ctl_table_header *head = grab_header(inode);
9043476f 572 struct ctl_table_header *h = NULL;
6a75ce16 573 struct ctl_table *entry;
7ec66d06 574 struct ctl_dir *ctl_dir;
77b14db5 575 unsigned long pos;
9043476f
AV
576 int ret = -EINVAL;
577
578 if (IS_ERR(head))
579 return PTR_ERR(head);
77b14db5 580
7ec66d06 581 ctl_dir = container_of(head, struct ctl_dir, header);
77b14db5
EB
582
583 ret = 0;
584 /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
585 if (filp->f_pos == 0) {
586 if (filldir(dirent, ".", 1, filp->f_pos,
587 inode->i_ino, DT_DIR) < 0)
588 goto out;
589 filp->f_pos++;
590 }
591 if (filp->f_pos == 1) {
592 if (filldir(dirent, "..", 2, filp->f_pos,
593 parent_ino(dentry), DT_DIR) < 0)
594 goto out;
595 filp->f_pos++;
596 }
597 pos = 2;
598
7ec66d06 599 for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) {
6a75ce16 600 ret = scan(h, entry, &pos, filp, dirent, filldir);
9043476f
AV
601 if (ret) {
602 sysctl_head_finish(h);
603 break;
77b14db5
EB
604 }
605 }
606 ret = 1;
607out:
608 sysctl_head_finish(head);
609 return ret;
610}
611
10556cb2 612static int proc_sys_permission(struct inode *inode, int mask)
77b14db5
EB
613{
614 /*
615 * sysctl entries that are not writeable,
616 * are _NOT_ writeable, capabilities or not.
617 */
f696a365
MS
618 struct ctl_table_header *head;
619 struct ctl_table *table;
77b14db5
EB
620 int error;
621
f696a365
MS
622 /* Executable files are not allowed under /proc/sys/ */
623 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
624 return -EACCES;
625
626 head = grab_header(inode);
9043476f
AV
627 if (IS_ERR(head))
628 return PTR_ERR(head);
77b14db5 629
f696a365 630 table = PROC_I(inode)->sysctl_entry;
9043476f
AV
631 if (!table) /* global root - r-xr-xr-x */
632 error = mask & MAY_WRITE ? -EACCES : 0;
633 else /* Use the permissions on the sysctl table entry */
1fc0f78c 634 error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
77b14db5 635
77b14db5
EB
636 sysctl_head_finish(head);
637 return error;
638}
639
640static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
641{
642 struct inode *inode = dentry->d_inode;
643 int error;
644
645 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
646 return -EPERM;
647
648 error = inode_change_ok(inode, attr);
1025774c
CH
649 if (error)
650 return error;
651
652 if ((attr->ia_valid & ATTR_SIZE) &&
653 attr->ia_size != i_size_read(inode)) {
654 error = vmtruncate(inode, attr->ia_size);
655 if (error)
656 return error;
657 }
77b14db5 658
1025774c
CH
659 setattr_copy(inode, attr);
660 mark_inode_dirty(inode);
661 return 0;
77b14db5
EB
662}
663
9043476f
AV
664static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
665{
666 struct inode *inode = dentry->d_inode;
667 struct ctl_table_header *head = grab_header(inode);
668 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
669
670 if (IS_ERR(head))
671 return PTR_ERR(head);
672
673 generic_fillattr(inode, stat);
674 if (table)
675 stat->mode = (stat->mode & S_IFMT) | table->mode;
676
677 sysctl_head_finish(head);
678 return 0;
679}
680
77b14db5 681static const struct file_operations proc_sys_file_operations = {
f1ecf068
LDM
682 .open = proc_sys_open,
683 .poll = proc_sys_poll,
77b14db5
EB
684 .read = proc_sys_read,
685 .write = proc_sys_write,
6038f373 686 .llseek = default_llseek,
9043476f
AV
687};
688
689static const struct file_operations proc_sys_dir_file_operations = {
887df078 690 .read = generic_read_dir,
77b14db5 691 .readdir = proc_sys_readdir,
3222a3e5 692 .llseek = generic_file_llseek,
77b14db5
EB
693};
694
03a44825 695static const struct inode_operations proc_sys_inode_operations = {
9043476f
AV
696 .permission = proc_sys_permission,
697 .setattr = proc_sys_setattr,
698 .getattr = proc_sys_getattr,
699};
700
701static const struct inode_operations proc_sys_dir_operations = {
77b14db5
EB
702 .lookup = proc_sys_lookup,
703 .permission = proc_sys_permission,
704 .setattr = proc_sys_setattr,
9043476f 705 .getattr = proc_sys_getattr,
77b14db5
EB
706};
707
708static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
709{
34286d66
NP
710 if (nd->flags & LOOKUP_RCU)
711 return -ECHILD;
9043476f
AV
712 return !PROC_I(dentry->d_inode)->sysctl->unregistering;
713}
714
fe15ce44 715static int proc_sys_delete(const struct dentry *dentry)
9043476f
AV
716{
717 return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
718}
719
1f87f0b5
EB
720static int sysctl_is_seen(struct ctl_table_header *p)
721{
722 struct ctl_table_set *set = p->set;
723 int res;
724 spin_lock(&sysctl_lock);
725 if (p->unregistering)
726 res = 0;
727 else if (!set->is_seen)
728 res = 1;
729 else
730 res = set->is_seen(set);
731 spin_unlock(&sysctl_lock);
732 return res;
733}
734
621e155a
NP
735static int proc_sys_compare(const struct dentry *parent,
736 const struct inode *pinode,
737 const struct dentry *dentry, const struct inode *inode,
738 unsigned int len, const char *str, const struct qstr *name)
9043476f 739{
dfef6dcd 740 struct ctl_table_header *head;
31e6b01f
NP
741 /* Although proc doesn't have negative dentries, rcu-walk means
742 * that inode here can be NULL */
dfef6dcd 743 /* AV: can it, indeed? */
31e6b01f 744 if (!inode)
dfef6dcd 745 return 1;
621e155a 746 if (name->len != len)
9043476f 747 return 1;
621e155a 748 if (memcmp(name->name, str, len))
9043476f 749 return 1;
dfef6dcd
AV
750 head = rcu_dereference(PROC_I(inode)->sysctl);
751 return !head || !sysctl_is_seen(head);
77b14db5
EB
752}
753
d72f71eb 754static const struct dentry_operations proc_sys_dentry_operations = {
77b14db5 755 .d_revalidate = proc_sys_revalidate,
9043476f
AV
756 .d_delete = proc_sys_delete,
757 .d_compare = proc_sys_compare,
77b14db5
EB
758};
759
7ec66d06
EB
760static struct ctl_dir *find_subdir(struct ctl_table_set *set, struct ctl_dir *dir,
761 const char *name, int namelen)
1f87f0b5 762{
7ec66d06
EB
763 struct ctl_table_header *head;
764 struct ctl_table *entry;
1f87f0b5 765
7ec66d06
EB
766 entry = find_entry(&head, set, dir, name, namelen);
767 if (!entry)
768 return ERR_PTR(-ENOENT);
769 if (S_ISDIR(entry->mode))
770 return container_of(head, struct ctl_dir, header);
771 return ERR_PTR(-ENOTDIR);
772}
773
774static struct ctl_dir *new_dir(struct ctl_table_set *set,
775 const char *name, int namelen)
776{
777 struct ctl_table *table;
778 struct ctl_dir *new;
779 char *new_name;
1f87f0b5 780
7ec66d06
EB
781 new = kzalloc(sizeof(*new) + sizeof(struct ctl_table)*2 +
782 namelen + 1, GFP_KERNEL);
783 if (!new)
1f87f0b5
EB
784 return NULL;
785
7ec66d06
EB
786 table = (struct ctl_table *)(new + 1);
787 new_name = (char *)(table + 2);
788 memcpy(new_name, name, namelen);
789 new_name[namelen] = '\0';
790 table[0].procname = new_name;
791 table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
792 init_header(&new->header, set->root, set, table);
793
794 return new;
1f87f0b5
EB
795}
796
7ec66d06
EB
797static struct ctl_dir *get_subdir(struct ctl_table_set *set,
798 struct ctl_dir *dir, const char *name, int namelen)
1f87f0b5 799{
7ec66d06 800 struct ctl_dir *subdir, *new = NULL;
1f87f0b5 801
7ec66d06
EB
802 spin_lock(&sysctl_lock);
803 subdir = find_subdir(dir->header.set, dir, name, namelen);
804 if (!IS_ERR(subdir))
805 goto found;
806 if ((PTR_ERR(subdir) == -ENOENT) && set != dir->header.set)
807 subdir = find_subdir(set, dir, name, namelen);
808 if (!IS_ERR(subdir))
809 goto found;
810 if (PTR_ERR(subdir) != -ENOENT)
811 goto failed;
812
813 spin_unlock(&sysctl_lock);
814 new = new_dir(set, name, namelen);
815 spin_lock(&sysctl_lock);
816 subdir = ERR_PTR(-ENOMEM);
817 if (!new)
818 goto failed;
819
820 subdir = find_subdir(set, dir, name, namelen);
821 if (!IS_ERR(subdir))
822 goto found;
823 if (PTR_ERR(subdir) != -ENOENT)
824 goto failed;
825
826 insert_header(dir, &new->header);
827 subdir = new;
828found:
829 subdir->header.nreg++;
830failed:
831 if (unlikely(IS_ERR(subdir))) {
6980128f
EB
832 printk(KERN_ERR "sysctl could not get directory: ");
833 sysctl_print_dir(dir);
834 printk(KERN_CONT "/%*.*s %ld\n",
7ec66d06 835 namelen, namelen, name, PTR_ERR(subdir));
1f87f0b5 836 }
7ec66d06
EB
837 drop_sysctl_table(&dir->header);
838 if (new)
839 drop_sysctl_table(&new->header);
840 spin_unlock(&sysctl_lock);
841 return subdir;
1f87f0b5
EB
842}
843
7c60c48f
EB
844static int sysctl_check_table_dups(const char *path, struct ctl_table *old,
845 struct ctl_table *table)
1f87f0b5 846{
7c60c48f
EB
847 struct ctl_table *entry, *test;
848 int error = 0;
1f87f0b5 849
7c60c48f
EB
850 for (entry = old; entry->procname; entry++) {
851 for (test = table; test->procname; test++) {
852 if (strcmp(entry->procname, test->procname) == 0) {
853 printk(KERN_ERR "sysctl duplicate entry: %s/%s\n",
854 path, test->procname);
855 error = -EEXIST;
856 }
857 }
858 }
859 return error;
1f87f0b5
EB
860}
861
7c60c48f 862static int sysctl_check_dups(struct nsproxy *namespaces,
7ec66d06 863 struct ctl_dir *dir,
7c60c48f 864 const char *path, struct ctl_table *table)
1f87f0b5 865{
7c60c48f
EB
866 struct ctl_table_root *root;
867 struct ctl_table_set *set;
7ec66d06 868 struct ctl_table_header *head;
7c60c48f 869 int error = 0;
1f87f0b5 870
7c60c48f
EB
871 root = &sysctl_table_root;
872 do {
873 set = lookup_header_set(root, namespaces);
1f87f0b5 874
7c60c48f
EB
875 list_for_each_entry(head, &set->list, ctl_entry) {
876 if (head->unregistering)
1f87f0b5 877 continue;
7ec66d06 878 if (head->parent != dir)
7c60c48f 879 continue;
7ec66d06 880 error = sysctl_check_table_dups(path, head->ctl_table,
7c60c48f 881 table);
1f87f0b5 882 }
7c60c48f
EB
883 root = list_entry(root->root_list.next,
884 struct ctl_table_root, root_list);
885 } while (root != &sysctl_table_root);
886 return error;
1f87f0b5
EB
887}
888
7c60c48f 889static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
1f87f0b5 890{
7c60c48f
EB
891 struct va_format vaf;
892 va_list args;
1f87f0b5 893
7c60c48f
EB
894 va_start(args, fmt);
895 vaf.fmt = fmt;
896 vaf.va = &args;
897
898 printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n",
899 path, table->procname, &vaf);
1f87f0b5 900
7c60c48f
EB
901 va_end(args);
902 return -EINVAL;
1f87f0b5
EB
903}
904
7c60c48f 905static int sysctl_check_table(const char *path, struct ctl_table *table)
1f87f0b5 906{
7c60c48f 907 int err = 0;
1f87f0b5 908 for (; table->procname; table++) {
1f87f0b5 909 if (table->child)
7c60c48f
EB
910 err = sysctl_err(path, table, "Not a file");
911
912 if ((table->proc_handler == proc_dostring) ||
913 (table->proc_handler == proc_dointvec) ||
914 (table->proc_handler == proc_dointvec_minmax) ||
915 (table->proc_handler == proc_dointvec_jiffies) ||
916 (table->proc_handler == proc_dointvec_userhz_jiffies) ||
917 (table->proc_handler == proc_dointvec_ms_jiffies) ||
918 (table->proc_handler == proc_doulongvec_minmax) ||
919 (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
920 if (!table->data)
921 err = sysctl_err(path, table, "No data");
922 if (!table->maxlen)
923 err = sysctl_err(path, table, "No maxlen");
924 }
925 if (!table->proc_handler)
926 err = sysctl_err(path, table, "No proc_handler");
927
928 if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
929 err = sysctl_err(path, table, "bogus .mode 0%o",
930 table->mode);
1f87f0b5 931 }
7c60c48f 932 return err;
1f87f0b5 933}
1f87f0b5
EB
934
935/**
f728019b 936 * __register_sysctl_table - register a leaf sysctl table
1f87f0b5
EB
937 * @root: List of sysctl headers to register on
938 * @namespaces: Data to compute which lists of sysctl entries are visible
939 * @path: The path to the directory the sysctl table is in.
940 * @table: the top-level table structure
941 *
942 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
943 * array. A completely 0 filled entry terminates the table.
944 *
945 * The members of the &struct ctl_table structure are used as follows:
946 *
947 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
948 * enter a sysctl file
949 *
950 * data - a pointer to data for use by proc_handler
951 *
952 * maxlen - the maximum size in bytes of the data
953 *
f728019b 954 * mode - the file permissions for the /proc/sys file
1f87f0b5 955 *
f728019b 956 * child - must be %NULL.
1f87f0b5
EB
957 *
958 * proc_handler - the text handler routine (described below)
959 *
1f87f0b5
EB
960 * extra1, extra2 - extra pointers usable by the proc handler routines
961 *
962 * Leaf nodes in the sysctl tree will be represented by a single file
963 * under /proc; non-leaf nodes will be represented by directories.
964 *
f728019b
EB
965 * There must be a proc_handler routine for any terminal nodes.
966 * Several default handlers are available to cover common cases -
1f87f0b5
EB
967 *
968 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
969 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
970 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
971 *
972 * It is the handler's job to read the input buffer from user memory
973 * and process it. The handler should return 0 on success.
974 *
975 * This routine returns %NULL on a failure to register, and a pointer
976 * to the table header on success.
977 */
6e9d5164 978struct ctl_table_header *__register_sysctl_table(
1f87f0b5
EB
979 struct ctl_table_root *root,
980 struct nsproxy *namespaces,
6e9d5164 981 const char *path, struct ctl_table *table)
1f87f0b5
EB
982{
983 struct ctl_table_header *header;
6e9d5164 984 const char *name, *nextname;
1f87f0b5 985 struct ctl_table_set *set;
7ec66d06 986 struct ctl_dir *dir;
1f87f0b5 987
7ec66d06 988 header = kzalloc(sizeof(struct ctl_table_header), GFP_KERNEL);
1f87f0b5
EB
989 if (!header)
990 return NULL;
991
7ec66d06
EB
992 init_header(header, root, NULL, table);
993 if (sysctl_check_table(path, table))
994 goto fail;
995
996 spin_lock(&sysctl_lock);
997 header->set = set = lookup_header_set(root, namespaces);
998 dir = &sysctl_root_dir;
999 dir->header.nreg++;
1000 spin_unlock(&sysctl_lock);
1f87f0b5 1001
7ec66d06 1002 /* Find the directory for the ctl_table */
6e9d5164
EB
1003 for (name = path; name; name = nextname) {
1004 int namelen;
1005 nextname = strchr(name, '/');
1006 if (nextname) {
1007 namelen = nextname - name;
1008 nextname++;
1009 } else {
1010 namelen = strlen(name);
1011 }
1012 if (namelen == 0)
1013 continue;
1f87f0b5 1014
7ec66d06
EB
1015 dir = get_subdir(set, dir, name, namelen);
1016 if (IS_ERR(dir))
1017 goto fail;
1f87f0b5 1018 }
1f87f0b5 1019 spin_lock(&sysctl_lock);
7ec66d06
EB
1020 if (sysctl_check_dups(namespaces, dir, path, table))
1021 goto fail_put_dir_locked;
1022 insert_header(dir, header);
1023 drop_sysctl_table(&dir->header);
1f87f0b5
EB
1024 spin_unlock(&sysctl_lock);
1025
1026 return header;
7ec66d06
EB
1027fail_put_dir_locked:
1028 drop_sysctl_table(&dir->header);
7c60c48f
EB
1029 spin_unlock(&sysctl_lock);
1030fail:
1031 kfree(header);
1032 dump_stack();
1033 return NULL;
1f87f0b5
EB
1034}
1035
6e9d5164
EB
1036static char *append_path(const char *path, char *pos, const char *name)
1037{
1038 int namelen;
1039 namelen = strlen(name);
1040 if (((pos - path) + namelen + 2) >= PATH_MAX)
1041 return NULL;
1042 memcpy(pos, name, namelen);
1043 pos[namelen] = '/';
1044 pos[namelen + 1] = '\0';
1045 pos += namelen + 1;
1046 return pos;
1047}
1048
f728019b
EB
1049static int count_subheaders(struct ctl_table *table)
1050{
1051 int has_files = 0;
1052 int nr_subheaders = 0;
1053 struct ctl_table *entry;
1054
1055 /* special case: no directory and empty directory */
1056 if (!table || !table->procname)
1057 return 1;
1058
1059 for (entry = table; entry->procname; entry++) {
1060 if (entry->child)
1061 nr_subheaders += count_subheaders(entry->child);
1062 else
1063 has_files = 1;
1064 }
1065 return nr_subheaders + has_files;
1066}
1067
1068static int register_leaf_sysctl_tables(const char *path, char *pos,
1069 struct ctl_table_header ***subheader,
1070 struct ctl_table_root *root, struct nsproxy *namespaces,
1071 struct ctl_table *table)
1072{
1073 struct ctl_table *ctl_table_arg = NULL;
1074 struct ctl_table *entry, *files;
1075 int nr_files = 0;
1076 int nr_dirs = 0;
1077 int err = -ENOMEM;
1078
1079 for (entry = table; entry->procname; entry++) {
1080 if (entry->child)
1081 nr_dirs++;
1082 else
1083 nr_files++;
1084 }
1085
1086 files = table;
1087 /* If there are mixed files and directories we need a new table */
1088 if (nr_dirs && nr_files) {
1089 struct ctl_table *new;
1090 files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1),
1091 GFP_KERNEL);
1092 if (!files)
1093 goto out;
1094
1095 ctl_table_arg = files;
1096 for (new = files, entry = table; entry->procname; entry++) {
1097 if (entry->child)
1098 continue;
1099 *new = *entry;
1100 new++;
1101 }
1102 }
1103
1104 /* Register everything except a directory full of subdirectories */
1105 if (nr_files || !nr_dirs) {
1106 struct ctl_table_header *header;
1107 header = __register_sysctl_table(root, namespaces, path, files);
1108 if (!header) {
1109 kfree(ctl_table_arg);
1110 goto out;
1111 }
1112
1113 /* Remember if we need to free the file table */
1114 header->ctl_table_arg = ctl_table_arg;
1115 **subheader = header;
1116 (*subheader)++;
1117 }
1118
1119 /* Recurse into the subdirectories. */
1120 for (entry = table; entry->procname; entry++) {
1121 char *child_pos;
1122
1123 if (!entry->child)
1124 continue;
1125
1126 err = -ENAMETOOLONG;
1127 child_pos = append_path(path, pos, entry->procname);
1128 if (!child_pos)
1129 goto out;
1130
1131 err = register_leaf_sysctl_tables(path, child_pos, subheader,
1132 root, namespaces, entry->child);
1133 pos[0] = '\0';
1134 if (err)
1135 goto out;
1136 }
1137 err = 0;
1138out:
1139 /* On failure our caller will unregister all registered subheaders */
1140 return err;
1141}
1142
6e9d5164
EB
1143/**
1144 * __register_sysctl_paths - register a sysctl table hierarchy
1145 * @root: List of sysctl headers to register on
1146 * @namespaces: Data to compute which lists of sysctl entries are visible
1147 * @path: The path to the directory the sysctl table is in.
1148 * @table: the top-level table structure
1149 *
1150 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1151 * array. A completely 0 filled entry terminates the table.
1152 *
1153 * See __register_sysctl_table for more details.
1154 */
1155struct ctl_table_header *__register_sysctl_paths(
1156 struct ctl_table_root *root,
1157 struct nsproxy *namespaces,
1158 const struct ctl_path *path, struct ctl_table *table)
1159{
ec6a5266 1160 struct ctl_table *ctl_table_arg = table;
f728019b
EB
1161 int nr_subheaders = count_subheaders(table);
1162 struct ctl_table_header *header = NULL, **subheaders, **subheader;
6e9d5164
EB
1163 const struct ctl_path *component;
1164 char *new_path, *pos;
1165
1166 pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
1167 if (!new_path)
1168 return NULL;
1169
1170 pos[0] = '\0';
1171 for (component = path; component->procname; component++) {
1172 pos = append_path(new_path, pos, component->procname);
1173 if (!pos)
1174 goto out;
1175 }
ec6a5266
EB
1176 while (table->procname && table->child && !table[1].procname) {
1177 pos = append_path(new_path, pos, table->procname);
1178 if (!pos)
1179 goto out;
1180 table = table->child;
1181 }
f728019b
EB
1182 if (nr_subheaders == 1) {
1183 header = __register_sysctl_table(root, namespaces, new_path, table);
1184 if (header)
1185 header->ctl_table_arg = ctl_table_arg;
1186 } else {
1187 header = kzalloc(sizeof(*header) +
1188 sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
1189 if (!header)
1190 goto out;
1191
1192 subheaders = (struct ctl_table_header **) (header + 1);
1193 subheader = subheaders;
ec6a5266 1194 header->ctl_table_arg = ctl_table_arg;
f728019b
EB
1195
1196 if (register_leaf_sysctl_tables(new_path, pos, &subheader,
1197 root, namespaces, table))
1198 goto err_register_leaves;
1199 }
1200
6e9d5164
EB
1201out:
1202 kfree(new_path);
1203 return header;
f728019b
EB
1204
1205err_register_leaves:
1206 while (subheader > subheaders) {
1207 struct ctl_table_header *subh = *(--subheader);
1208 struct ctl_table *table = subh->ctl_table_arg;
1209 unregister_sysctl_table(subh);
1210 kfree(table);
1211 }
1212 kfree(header);
1213 header = NULL;
1214 goto out;
6e9d5164
EB
1215}
1216
1f87f0b5
EB
1217/**
1218 * register_sysctl_table_path - register a sysctl table hierarchy
1219 * @path: The path to the directory the sysctl table is in.
1220 * @table: the top-level table structure
1221 *
1222 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1223 * array. A completely 0 filled entry terminates the table.
1224 *
1225 * See __register_sysctl_paths for more details.
1226 */
1227struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
1228 struct ctl_table *table)
1229{
1230 return __register_sysctl_paths(&sysctl_table_root, current->nsproxy,
1231 path, table);
1232}
1233EXPORT_SYMBOL(register_sysctl_paths);
1234
1235/**
1236 * register_sysctl_table - register a sysctl table hierarchy
1237 * @table: the top-level table structure
1238 *
1239 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1240 * array. A completely 0 filled entry terminates the table.
1241 *
1242 * See register_sysctl_paths for more details.
1243 */
1244struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
1245{
1246 static const struct ctl_path null_path[] = { {} };
1247
1248 return register_sysctl_paths(null_path, table);
1249}
1250EXPORT_SYMBOL(register_sysctl_table);
1251
938aaa4f
EB
1252static void drop_sysctl_table(struct ctl_table_header *header)
1253{
7ec66d06
EB
1254 struct ctl_dir *parent = header->parent;
1255
938aaa4f
EB
1256 if (--header->nreg)
1257 return;
1258
1259 start_unregistering(header);
938aaa4f
EB
1260 if (!--header->count)
1261 kfree_rcu(header, rcu);
7ec66d06
EB
1262
1263 if (parent)
1264 drop_sysctl_table(&parent->header);
938aaa4f
EB
1265}
1266
1f87f0b5
EB
1267/**
1268 * unregister_sysctl_table - unregister a sysctl table hierarchy
1269 * @header: the header returned from register_sysctl_table
1270 *
1271 * Unregisters the sysctl table and all children. proc entries may not
1272 * actually be removed until they are no longer used by anyone.
1273 */
1274void unregister_sysctl_table(struct ctl_table_header * header)
1275{
f728019b 1276 int nr_subheaders;
1f87f0b5
EB
1277 might_sleep();
1278
1279 if (header == NULL)
1280 return;
1281
f728019b
EB
1282 nr_subheaders = count_subheaders(header->ctl_table_arg);
1283 if (unlikely(nr_subheaders > 1)) {
1284 struct ctl_table_header **subheaders;
1285 int i;
1286
1287 subheaders = (struct ctl_table_header **)(header + 1);
1288 for (i = nr_subheaders -1; i >= 0; i--) {
1289 struct ctl_table_header *subh = subheaders[i];
1290 struct ctl_table *table = subh->ctl_table_arg;
1291 unregister_sysctl_table(subh);
1292 kfree(table);
1293 }
1294 kfree(header);
1295 return;
1296 }
1297
1f87f0b5 1298 spin_lock(&sysctl_lock);
938aaa4f 1299 drop_sysctl_table(header);
1f87f0b5
EB
1300 spin_unlock(&sysctl_lock);
1301}
1302EXPORT_SYMBOL(unregister_sysctl_table);
1303
1304void setup_sysctl_set(struct ctl_table_set *p,
9eb47c26 1305 struct ctl_table_root *root,
1f87f0b5
EB
1306 int (*is_seen)(struct ctl_table_set *))
1307{
1308 INIT_LIST_HEAD(&p->list);
9eb47c26 1309 p->root = root;
1f87f0b5
EB
1310 p->is_seen = is_seen;
1311}
1312
97324cd8
EB
1313void retire_sysctl_set(struct ctl_table_set *set)
1314{
1315 WARN_ON(!list_empty(&set->list));
1316}
1f87f0b5 1317
1e0edd3f 1318int __init proc_sys_init(void)
77b14db5 1319{
e1675231
AD
1320 struct proc_dir_entry *proc_sys_root;
1321
77b14db5 1322 proc_sys_root = proc_mkdir("sys", NULL);
9043476f
AV
1323 proc_sys_root->proc_iops = &proc_sys_dir_operations;
1324 proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
77b14db5 1325 proc_sys_root->nlink = 0;
de4e83bd
EB
1326
1327 return sysctl_init();
77b14db5 1328}