4 #include <linux/init.h>
5 #include <linux/sysctl.h>
6 #include <linux/poll.h>
7 #include <linux/proc_fs.h>
8 #include <linux/security.h>
9 #include <linux/namei.h>
10 #include <linux/module.h>
13 static const struct dentry_operations proc_sys_dentry_operations;
14 static const struct file_operations proc_sys_file_operations;
15 static const struct inode_operations proc_sys_inode_operations;
16 static const struct file_operations proc_sys_dir_file_operations;
17 static const struct inode_operations proc_sys_dir_operations;
19 void proc_sys_poll_notify(struct ctl_table_poll *poll)
24 atomic_inc(&poll->event);
25 wake_up_interruptible(&poll->wait);
28 static struct ctl_table root_table[] = {
31 .mode = S_IFDIR|S_IRUGO|S_IXUGO,
35 static 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 = {
40 .ctl_table = root_table,
41 .ctl_entry = LIST_HEAD_INIT(sysctl_table_root.default_set.list),}},
42 .ctl_table_arg = root_table,
43 .root = &sysctl_table_root,
44 .set = &sysctl_table_root.default_set,
48 static DEFINE_SPINLOCK(sysctl_lock);
50 static void drop_sysctl_table(struct ctl_table_header *header);
51 static int sysctl_follow_link(struct ctl_table_header **phead,
52 struct ctl_table **pentry, struct nsproxy *namespaces);
53 static int insert_links(struct ctl_table_header *head);
54 static void put_links(struct ctl_table_header *header);
56 static void sysctl_print_dir(struct ctl_dir *dir)
58 if (dir->header.parent)
59 sysctl_print_dir(dir->header.parent);
60 printk(KERN_CONT "%s/", dir->header.ctl_table[0].procname);
63 static int namecmp(const char *name1, int len1, const char *name2, int len2)
72 cmp = memcmp(name1, name2, minlen);
78 static struct ctl_table *find_entry(struct ctl_table_header **phead,
79 struct ctl_dir *dir, const char *name, int namelen)
81 struct ctl_table_set *set = dir->header.set;
82 struct ctl_table_header *head;
83 struct ctl_table *entry;
85 list_for_each_entry(head, &set->list, ctl_entry) {
86 if (head->unregistering)
88 if (head->parent != dir)
90 for (entry = head->ctl_table; entry->procname; entry++) {
91 const char *procname = entry->procname;
92 if (namecmp(procname, strlen(procname), name, namelen) == 0) {
101 static void init_header(struct ctl_table_header *head,
102 struct ctl_table_root *root, struct ctl_table_set *set,
103 struct ctl_table *table)
105 head->ctl_table = table;
106 head->ctl_table_arg = table;
107 INIT_LIST_HEAD(&head->ctl_entry);
111 head->unregistering = NULL;
117 static void erase_header(struct ctl_table_header *head)
119 list_del_init(&head->ctl_entry);
122 static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header)
127 header->parent = dir;
128 err = insert_links(header);
131 list_add_tail(&header->ctl_entry, &header->set->list);
134 header->parent = NULL;
135 drop_sysctl_table(&dir->header);
139 /* called under sysctl_lock */
140 static int use_table(struct ctl_table_header *p)
142 if (unlikely(p->unregistering))
148 /* called under sysctl_lock */
149 static void unuse_table(struct ctl_table_header *p)
152 if (unlikely(p->unregistering))
153 complete(p->unregistering);
156 /* called under sysctl_lock, will reacquire if has to wait */
157 static void start_unregistering(struct ctl_table_header *p)
160 * if p->used is 0, nobody will ever touch that entry again;
161 * we'll eliminate all paths to it before dropping sysctl_lock
163 if (unlikely(p->used)) {
164 struct completion wait;
165 init_completion(&wait);
166 p->unregistering = &wait;
167 spin_unlock(&sysctl_lock);
168 wait_for_completion(&wait);
169 spin_lock(&sysctl_lock);
171 /* anything non-NULL; we'll never dereference it */
172 p->unregistering = ERR_PTR(-EINVAL);
175 * do not remove from the list until nobody holds it; walking the
176 * list in do_sysctl() relies on that.
181 static void sysctl_head_get(struct ctl_table_header *head)
183 spin_lock(&sysctl_lock);
185 spin_unlock(&sysctl_lock);
188 void sysctl_head_put(struct ctl_table_header *head)
190 spin_lock(&sysctl_lock);
192 kfree_rcu(head, rcu);
193 spin_unlock(&sysctl_lock);
196 static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
200 spin_lock(&sysctl_lock);
201 if (!use_table(head))
202 head = ERR_PTR(-ENOENT);
203 spin_unlock(&sysctl_lock);
207 static void sysctl_head_finish(struct ctl_table_header *head)
211 spin_lock(&sysctl_lock);
213 spin_unlock(&sysctl_lock);
216 static struct ctl_table_set *
217 lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
219 struct ctl_table_set *set = &root->default_set;
221 set = root->lookup(root, namespaces);
225 static struct ctl_table *lookup_entry(struct ctl_table_header **phead,
227 const char *name, int namelen)
229 struct ctl_table_header *head;
230 struct ctl_table *entry;
232 spin_lock(&sysctl_lock);
233 entry = find_entry(&head, dir, name, namelen);
234 if (entry && use_table(head))
238 spin_unlock(&sysctl_lock);
242 static struct ctl_table_header *next_usable_entry(struct ctl_dir *dir,
243 struct list_head *tmp)
245 struct ctl_table_set *set = dir->header.set;
246 struct ctl_table_header *head;
248 for (tmp = tmp->next; tmp != &set->list; tmp = tmp->next) {
249 head = list_entry(tmp, struct ctl_table_header, ctl_entry);
251 if (head->parent != dir ||
252 !head->ctl_table->procname ||
261 static void first_entry(struct ctl_dir *dir,
262 struct ctl_table_header **phead, struct ctl_table **pentry)
264 struct ctl_table_header *head;
265 struct ctl_table *entry = NULL;
267 spin_lock(&sysctl_lock);
268 head = next_usable_entry(dir, &dir->header.set->list);
269 spin_unlock(&sysctl_lock);
271 entry = head->ctl_table;
276 static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry)
278 struct ctl_table_header *head = *phead;
279 struct ctl_table *entry = *pentry;
282 if (!entry->procname) {
283 spin_lock(&sysctl_lock);
285 head = next_usable_entry(head->parent, &head->ctl_entry);
286 spin_unlock(&sysctl_lock);
288 entry = head->ctl_table;
294 void register_sysctl_root(struct ctl_table_root *root)
299 * sysctl_perm does NOT grant the superuser all rights automatically, because
300 * some sysctl variables are readonly even to root.
303 static int test_perm(int mode, int op)
307 else if (in_egroup_p(0))
309 if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
314 static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op)
318 if (root->permissions)
319 mode = root->permissions(root, current->nsproxy, table);
323 return test_perm(mode, op);
326 static struct inode *proc_sys_make_inode(struct super_block *sb,
327 struct ctl_table_header *head, struct ctl_table *table)
330 struct proc_inode *ei;
332 inode = new_inode(sb);
336 inode->i_ino = get_next_ino();
338 sysctl_head_get(head);
341 ei->sysctl_entry = table;
343 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
344 inode->i_mode = table->mode;
345 if (!S_ISDIR(table->mode)) {
346 inode->i_mode |= S_IFREG;
347 inode->i_op = &proc_sys_inode_operations;
348 inode->i_fop = &proc_sys_file_operations;
350 inode->i_mode |= S_IFDIR;
351 inode->i_op = &proc_sys_dir_operations;
352 inode->i_fop = &proc_sys_dir_file_operations;
358 static struct ctl_table_header *grab_header(struct inode *inode)
360 struct ctl_table_header *head = PROC_I(inode)->sysctl;
362 head = &sysctl_table_root.default_set.dir.header;
363 return sysctl_head_grab(head);
366 static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry,
367 struct nameidata *nd)
369 struct ctl_table_header *head = grab_header(dir);
370 struct ctl_table_header *h = NULL;
371 struct qstr *name = &dentry->d_name;
374 struct dentry *err = ERR_PTR(-ENOENT);
375 struct ctl_dir *ctl_dir;
379 return ERR_CAST(head);
381 ctl_dir = container_of(head, struct ctl_dir, header);
383 p = lookup_entry(&h, ctl_dir, name->name, name->len);
387 ret = sysctl_follow_link(&h, &p, current->nsproxy);
392 err = ERR_PTR(-ENOMEM);
393 inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p);
395 sysctl_head_finish(h);
401 d_set_d_op(dentry, &proc_sys_dentry_operations);
402 d_add(dentry, inode);
405 sysctl_head_finish(head);
409 static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf,
410 size_t count, loff_t *ppos, int write)
412 struct inode *inode = filp->f_path.dentry->d_inode;
413 struct ctl_table_header *head = grab_header(inode);
414 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
419 return PTR_ERR(head);
422 * At this point we know that the sysctl was not unregistered
423 * and won't be until we finish.
426 if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ))
429 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
431 if (!table->proc_handler)
434 /* careful: calling conventions are nasty here */
436 error = table->proc_handler(table, write, buf, &res, ppos);
440 sysctl_head_finish(head);
445 static ssize_t proc_sys_read(struct file *filp, char __user *buf,
446 size_t count, loff_t *ppos)
448 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0);
451 static ssize_t proc_sys_write(struct file *filp, const char __user *buf,
452 size_t count, loff_t *ppos)
454 return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1);
457 static int proc_sys_open(struct inode *inode, struct file *filp)
459 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
462 filp->private_data = proc_sys_poll_event(table->poll);
467 static unsigned int proc_sys_poll(struct file *filp, poll_table *wait)
469 struct inode *inode = filp->f_path.dentry->d_inode;
470 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
471 unsigned long event = (unsigned long)filp->private_data;
472 unsigned int ret = DEFAULT_POLLMASK;
474 if (!table->proc_handler)
480 poll_wait(filp, &table->poll->wait, wait);
482 if (event != atomic_read(&table->poll->event)) {
483 filp->private_data = proc_sys_poll_event(table->poll);
484 ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI;
491 static int proc_sys_fill_cache(struct file *filp, void *dirent,
493 struct ctl_table_header *head,
494 struct ctl_table *table)
496 struct dentry *child, *dir = filp->f_path.dentry;
500 unsigned type = DT_UNKNOWN;
502 qname.name = table->procname;
503 qname.len = strlen(table->procname);
504 qname.hash = full_name_hash(qname.name, qname.len);
506 child = d_lookup(dir, &qname);
508 child = d_alloc(dir, &qname);
510 inode = proc_sys_make_inode(dir->d_sb, head, table);
515 d_set_d_op(child, &proc_sys_dentry_operations);
522 inode = child->d_inode;
524 type = inode->i_mode >> 12;
526 return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type);
529 static int proc_sys_link_fill_cache(struct file *filp, void *dirent,
531 struct ctl_table_header *head,
532 struct ctl_table *table)
535 head = sysctl_head_grab(head);
537 /* It is not an error if we can not follow the link ignore it */
538 err = sysctl_follow_link(&head, &table, current->nsproxy);
542 ret = proc_sys_fill_cache(filp, dirent, filldir, head, table);
544 sysctl_head_finish(head);
548 static int scan(struct ctl_table_header *head, ctl_table *table,
549 unsigned long *pos, struct file *file,
550 void *dirent, filldir_t filldir)
554 if ((*pos)++ < file->f_pos)
557 if (unlikely(S_ISLNK(table->mode)))
558 res = proc_sys_link_fill_cache(file, dirent, filldir, head, table);
560 res = proc_sys_fill_cache(file, dirent, filldir, head, table);
568 static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir)
570 struct dentry *dentry = filp->f_path.dentry;
571 struct inode *inode = dentry->d_inode;
572 struct ctl_table_header *head = grab_header(inode);
573 struct ctl_table_header *h = NULL;
574 struct ctl_table *entry;
575 struct ctl_dir *ctl_dir;
580 return PTR_ERR(head);
582 ctl_dir = container_of(head, struct ctl_dir, header);
585 /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
586 if (filp->f_pos == 0) {
587 if (filldir(dirent, ".", 1, filp->f_pos,
588 inode->i_ino, DT_DIR) < 0)
592 if (filp->f_pos == 1) {
593 if (filldir(dirent, "..", 2, filp->f_pos,
594 parent_ino(dentry), DT_DIR) < 0)
600 for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) {
601 ret = scan(h, entry, &pos, filp, dirent, filldir);
603 sysctl_head_finish(h);
609 sysctl_head_finish(head);
613 static int proc_sys_permission(struct inode *inode, int mask)
616 * sysctl entries that are not writeable,
617 * are _NOT_ writeable, capabilities or not.
619 struct ctl_table_header *head;
620 struct ctl_table *table;
623 /* Executable files are not allowed under /proc/sys/ */
624 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))
627 head = grab_header(inode);
629 return PTR_ERR(head);
631 table = PROC_I(inode)->sysctl_entry;
632 if (!table) /* global root - r-xr-xr-x */
633 error = mask & MAY_WRITE ? -EACCES : 0;
634 else /* Use the permissions on the sysctl table entry */
635 error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK);
637 sysctl_head_finish(head);
641 static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr)
643 struct inode *inode = dentry->d_inode;
646 if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID))
649 error = inode_change_ok(inode, attr);
653 if ((attr->ia_valid & ATTR_SIZE) &&
654 attr->ia_size != i_size_read(inode)) {
655 error = vmtruncate(inode, attr->ia_size);
660 setattr_copy(inode, attr);
661 mark_inode_dirty(inode);
665 static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
667 struct inode *inode = dentry->d_inode;
668 struct ctl_table_header *head = grab_header(inode);
669 struct ctl_table *table = PROC_I(inode)->sysctl_entry;
672 return PTR_ERR(head);
674 generic_fillattr(inode, stat);
676 stat->mode = (stat->mode & S_IFMT) | table->mode;
678 sysctl_head_finish(head);
682 static const struct file_operations proc_sys_file_operations = {
683 .open = proc_sys_open,
684 .poll = proc_sys_poll,
685 .read = proc_sys_read,
686 .write = proc_sys_write,
687 .llseek = default_llseek,
690 static const struct file_operations proc_sys_dir_file_operations = {
691 .read = generic_read_dir,
692 .readdir = proc_sys_readdir,
693 .llseek = generic_file_llseek,
696 static const struct inode_operations proc_sys_inode_operations = {
697 .permission = proc_sys_permission,
698 .setattr = proc_sys_setattr,
699 .getattr = proc_sys_getattr,
702 static const struct inode_operations proc_sys_dir_operations = {
703 .lookup = proc_sys_lookup,
704 .permission = proc_sys_permission,
705 .setattr = proc_sys_setattr,
706 .getattr = proc_sys_getattr,
709 static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd)
711 if (nd->flags & LOOKUP_RCU)
713 return !PROC_I(dentry->d_inode)->sysctl->unregistering;
716 static int proc_sys_delete(const struct dentry *dentry)
718 return !!PROC_I(dentry->d_inode)->sysctl->unregistering;
721 static int sysctl_is_seen(struct ctl_table_header *p)
723 struct ctl_table_set *set = p->set;
725 spin_lock(&sysctl_lock);
726 if (p->unregistering)
728 else if (!set->is_seen)
731 res = set->is_seen(set);
732 spin_unlock(&sysctl_lock);
736 static int proc_sys_compare(const struct dentry *parent,
737 const struct inode *pinode,
738 const struct dentry *dentry, const struct inode *inode,
739 unsigned int len, const char *str, const struct qstr *name)
741 struct ctl_table_header *head;
742 /* Although proc doesn't have negative dentries, rcu-walk means
743 * that inode here can be NULL */
744 /* AV: can it, indeed? */
747 if (name->len != len)
749 if (memcmp(name->name, str, len))
751 head = rcu_dereference(PROC_I(inode)->sysctl);
752 return !head || !sysctl_is_seen(head);
755 static const struct dentry_operations proc_sys_dentry_operations = {
756 .d_revalidate = proc_sys_revalidate,
757 .d_delete = proc_sys_delete,
758 .d_compare = proc_sys_compare,
761 static struct ctl_dir *find_subdir(struct ctl_dir *dir,
762 const char *name, int namelen)
764 struct ctl_table_header *head;
765 struct ctl_table *entry;
767 entry = find_entry(&head, dir, name, namelen);
769 return ERR_PTR(-ENOENT);
770 if (S_ISDIR(entry->mode))
771 return container_of(head, struct ctl_dir, header);
772 return ERR_PTR(-ENOTDIR);
775 static struct ctl_dir *new_dir(struct ctl_table_set *set,
776 const char *name, int namelen)
778 struct ctl_table *table;
782 new = kzalloc(sizeof(*new) + sizeof(struct ctl_table)*2 +
783 namelen + 1, GFP_KERNEL);
787 table = (struct ctl_table *)(new + 1);
788 new_name = (char *)(table + 2);
789 memcpy(new_name, name, namelen);
790 new_name[namelen] = '\0';
791 table[0].procname = new_name;
792 table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO;
793 init_header(&new->header, set->dir.header.root, set, table);
798 static struct ctl_dir *get_subdir(struct ctl_dir *dir,
799 const char *name, int namelen)
801 struct ctl_table_set *set = dir->header.set;
802 struct ctl_dir *subdir, *new = NULL;
804 spin_lock(&sysctl_lock);
805 subdir = find_subdir(dir, name, namelen);
808 if (PTR_ERR(subdir) != -ENOENT)
811 spin_unlock(&sysctl_lock);
812 new = new_dir(set, name, namelen);
813 spin_lock(&sysctl_lock);
814 subdir = ERR_PTR(-ENOMEM);
818 subdir = find_subdir(dir, name, namelen);
821 if (PTR_ERR(subdir) != -ENOENT)
824 if (insert_header(dir, &new->header))
828 subdir->header.nreg++;
830 if (unlikely(IS_ERR(subdir))) {
831 printk(KERN_ERR "sysctl could not get directory: ");
832 sysctl_print_dir(dir);
833 printk(KERN_CONT "/%*.*s %ld\n",
834 namelen, namelen, name, PTR_ERR(subdir));
836 drop_sysctl_table(&dir->header);
838 drop_sysctl_table(&new->header);
839 spin_unlock(&sysctl_lock);
843 static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir)
845 struct ctl_dir *parent;
846 const char *procname;
847 if (!dir->header.parent)
849 parent = xlate_dir(set, dir->header.parent);
852 procname = dir->header.ctl_table[0].procname;
853 return find_subdir(parent, procname, strlen(procname));
856 static int sysctl_follow_link(struct ctl_table_header **phead,
857 struct ctl_table **pentry, struct nsproxy *namespaces)
859 struct ctl_table_header *head;
860 struct ctl_table_root *root;
861 struct ctl_table_set *set;
862 struct ctl_table *entry;
866 /* Get out quickly if not a link */
867 if (!S_ISLNK((*pentry)->mode))
871 spin_lock(&sysctl_lock);
872 root = (*pentry)->data;
873 set = lookup_header_set(root, namespaces);
874 dir = xlate_dir(set, (*phead)->parent);
878 const char *procname = (*pentry)->procname;
880 entry = find_entry(&head, dir, procname, strlen(procname));
882 if (entry && use_table(head)) {
890 spin_unlock(&sysctl_lock);
894 static int sysctl_check_table_dups(const char *path, struct ctl_table *old,
895 struct ctl_table *table)
897 struct ctl_table *entry, *test;
900 for (entry = old; entry->procname; entry++) {
901 for (test = table; test->procname; test++) {
902 if (strcmp(entry->procname, test->procname) == 0) {
903 printk(KERN_ERR "sysctl duplicate entry: %s/%s\n",
904 path, test->procname);
912 static int sysctl_check_dups(struct ctl_dir *dir,
913 const char *path, struct ctl_table *table)
915 struct ctl_table_set *set;
916 struct ctl_table_header *head;
919 set = dir->header.set;
920 list_for_each_entry(head, &set->list, ctl_entry) {
921 if (head->unregistering)
923 if (head->parent != dir)
925 error = sysctl_check_table_dups(path, head->ctl_table, table);
930 static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...)
932 struct va_format vaf;
939 printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n",
940 path, table->procname, &vaf);
946 static int sysctl_check_table(const char *path, struct ctl_table *table)
949 for (; table->procname; table++) {
951 err = sysctl_err(path, table, "Not a file");
953 if ((table->proc_handler == proc_dostring) ||
954 (table->proc_handler == proc_dointvec) ||
955 (table->proc_handler == proc_dointvec_minmax) ||
956 (table->proc_handler == proc_dointvec_jiffies) ||
957 (table->proc_handler == proc_dointvec_userhz_jiffies) ||
958 (table->proc_handler == proc_dointvec_ms_jiffies) ||
959 (table->proc_handler == proc_doulongvec_minmax) ||
960 (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) {
962 err = sysctl_err(path, table, "No data");
964 err = sysctl_err(path, table, "No maxlen");
966 if (!table->proc_handler)
967 err = sysctl_err(path, table, "No proc_handler");
969 if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode)
970 err = sysctl_err(path, table, "bogus .mode 0%o",
976 static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table *table,
977 struct ctl_table_root *link_root)
979 struct ctl_table *link_table, *entry, *link;
980 struct ctl_table_header *links;
982 int nr_entries, name_bytes;
986 for (entry = table; entry->procname; entry++) {
988 name_bytes += strlen(entry->procname) + 1;
991 links = kzalloc(sizeof(struct ctl_table_header) +
992 sizeof(struct ctl_table)*(nr_entries + 1) +
999 link_table = (struct ctl_table *)(links + 1);
1000 link_name = (char *)&link_table[nr_entries + 1];
1002 for (link = link_table, entry = table; entry->procname; link++, entry++) {
1003 int len = strlen(entry->procname) + 1;
1004 memcpy(link_name, entry->procname, len);
1005 link->procname = link_name;
1006 link->mode = S_IFLNK|S_IRWXUGO;
1007 link->data = link_root;
1010 init_header(links, dir->header.root, dir->header.set, link_table);
1011 links->nreg = nr_entries;
1016 static bool get_links(struct ctl_dir *dir,
1017 struct ctl_table *table, struct ctl_table_root *link_root)
1019 struct ctl_table_header *head;
1020 struct ctl_table *entry, *link;
1022 /* Are there links available for every entry in table? */
1023 for (entry = table; entry->procname; entry++) {
1024 const char *procname = entry->procname;
1025 link = find_entry(&head, dir, procname, strlen(procname));
1028 if (S_ISDIR(link->mode) && S_ISDIR(entry->mode))
1030 if (S_ISLNK(link->mode) && (link->data == link_root))
1035 /* The checks passed. Increase the registration count on the links */
1036 for (entry = table; entry->procname; entry++) {
1037 const char *procname = entry->procname;
1038 link = find_entry(&head, dir, procname, strlen(procname));
1044 static int insert_links(struct ctl_table_header *head)
1046 struct ctl_table_set *root_set = &sysctl_table_root.default_set;
1047 struct ctl_dir *core_parent = NULL;
1048 struct ctl_table_header *links;
1051 if (head->set == root_set)
1054 core_parent = xlate_dir(root_set, head->parent);
1055 if (IS_ERR(core_parent))
1058 if (get_links(core_parent, head->ctl_table, head->root))
1061 core_parent->header.nreg++;
1062 spin_unlock(&sysctl_lock);
1064 links = new_links(core_parent, head->ctl_table, head->root);
1066 spin_lock(&sysctl_lock);
1072 if (get_links(core_parent, head->ctl_table, head->root)) {
1077 err = insert_header(core_parent, links);
1081 drop_sysctl_table(&core_parent->header);
1086 * __register_sysctl_table - register a leaf sysctl table
1087 * @set: Sysctl tree to register on
1088 * @path: The path to the directory the sysctl table is in.
1089 * @table: the top-level table structure
1091 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1092 * array. A completely 0 filled entry terminates the table.
1094 * The members of the &struct ctl_table structure are used as follows:
1096 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
1097 * enter a sysctl file
1099 * data - a pointer to data for use by proc_handler
1101 * maxlen - the maximum size in bytes of the data
1103 * mode - the file permissions for the /proc/sys file
1105 * child - must be %NULL.
1107 * proc_handler - the text handler routine (described below)
1109 * extra1, extra2 - extra pointers usable by the proc handler routines
1111 * Leaf nodes in the sysctl tree will be represented by a single file
1112 * under /proc; non-leaf nodes will be represented by directories.
1114 * There must be a proc_handler routine for any terminal nodes.
1115 * Several default handlers are available to cover common cases -
1117 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
1118 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
1119 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
1121 * It is the handler's job to read the input buffer from user memory
1122 * and process it. The handler should return 0 on success.
1124 * This routine returns %NULL on a failure to register, and a pointer
1125 * to the table header on success.
1127 struct ctl_table_header *__register_sysctl_table(
1128 struct ctl_table_set *set,
1129 const char *path, struct ctl_table *table)
1131 struct ctl_table_root *root = set->dir.header.root;
1132 struct ctl_table_header *links = NULL;
1133 struct ctl_table_header *header;
1134 const char *name, *nextname;
1135 struct ctl_dir *dir;
1137 header = kzalloc(sizeof(struct ctl_table_header), GFP_KERNEL);
1141 init_header(header, root, set, table);
1142 if (sysctl_check_table(path, table))
1145 spin_lock(&sysctl_lock);
1148 spin_unlock(&sysctl_lock);
1150 /* Find the directory for the ctl_table */
1151 for (name = path; name; name = nextname) {
1153 nextname = strchr(name, '/');
1155 namelen = nextname - name;
1158 namelen = strlen(name);
1163 dir = get_subdir(dir, name, namelen);
1168 spin_lock(&sysctl_lock);
1169 if (sysctl_check_dups(dir, path, table))
1170 goto fail_put_dir_locked;
1172 if (insert_header(dir, header))
1173 goto fail_put_dir_locked;
1175 drop_sysctl_table(&dir->header);
1176 spin_unlock(&sysctl_lock);
1180 fail_put_dir_locked:
1181 drop_sysctl_table(&dir->header);
1182 spin_unlock(&sysctl_lock);
1190 static char *append_path(const char *path, char *pos, const char *name)
1193 namelen = strlen(name);
1194 if (((pos - path) + namelen + 2) >= PATH_MAX)
1196 memcpy(pos, name, namelen);
1198 pos[namelen + 1] = '\0';
1203 static int count_subheaders(struct ctl_table *table)
1206 int nr_subheaders = 0;
1207 struct ctl_table *entry;
1209 /* special case: no directory and empty directory */
1210 if (!table || !table->procname)
1213 for (entry = table; entry->procname; entry++) {
1215 nr_subheaders += count_subheaders(entry->child);
1219 return nr_subheaders + has_files;
1222 static int register_leaf_sysctl_tables(const char *path, char *pos,
1223 struct ctl_table_header ***subheader, struct ctl_table_set *set,
1224 struct ctl_table *table)
1226 struct ctl_table *ctl_table_arg = NULL;
1227 struct ctl_table *entry, *files;
1232 for (entry = table; entry->procname; entry++) {
1240 /* If there are mixed files and directories we need a new table */
1241 if (nr_dirs && nr_files) {
1242 struct ctl_table *new;
1243 files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1),
1248 ctl_table_arg = files;
1249 for (new = files, entry = table; entry->procname; entry++) {
1257 /* Register everything except a directory full of subdirectories */
1258 if (nr_files || !nr_dirs) {
1259 struct ctl_table_header *header;
1260 header = __register_sysctl_table(set, path, files);
1262 kfree(ctl_table_arg);
1266 /* Remember if we need to free the file table */
1267 header->ctl_table_arg = ctl_table_arg;
1268 **subheader = header;
1272 /* Recurse into the subdirectories. */
1273 for (entry = table; entry->procname; entry++) {
1279 err = -ENAMETOOLONG;
1280 child_pos = append_path(path, pos, entry->procname);
1284 err = register_leaf_sysctl_tables(path, child_pos, subheader,
1292 /* On failure our caller will unregister all registered subheaders */
1297 * __register_sysctl_paths - register a sysctl table hierarchy
1298 * @set: Sysctl tree to register on
1299 * @path: The path to the directory the sysctl table is in.
1300 * @table: the top-level table structure
1302 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1303 * array. A completely 0 filled entry terminates the table.
1305 * See __register_sysctl_table for more details.
1307 struct ctl_table_header *__register_sysctl_paths(
1308 struct ctl_table_set *set,
1309 const struct ctl_path *path, struct ctl_table *table)
1311 struct ctl_table *ctl_table_arg = table;
1312 int nr_subheaders = count_subheaders(table);
1313 struct ctl_table_header *header = NULL, **subheaders, **subheader;
1314 const struct ctl_path *component;
1315 char *new_path, *pos;
1317 pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL);
1322 for (component = path; component->procname; component++) {
1323 pos = append_path(new_path, pos, component->procname);
1327 while (table->procname && table->child && !table[1].procname) {
1328 pos = append_path(new_path, pos, table->procname);
1331 table = table->child;
1333 if (nr_subheaders == 1) {
1334 header = __register_sysctl_table(set, new_path, table);
1336 header->ctl_table_arg = ctl_table_arg;
1338 header = kzalloc(sizeof(*header) +
1339 sizeof(*subheaders)*nr_subheaders, GFP_KERNEL);
1343 subheaders = (struct ctl_table_header **) (header + 1);
1344 subheader = subheaders;
1345 header->ctl_table_arg = ctl_table_arg;
1347 if (register_leaf_sysctl_tables(new_path, pos, &subheader,
1349 goto err_register_leaves;
1356 err_register_leaves:
1357 while (subheader > subheaders) {
1358 struct ctl_table_header *subh = *(--subheader);
1359 struct ctl_table *table = subh->ctl_table_arg;
1360 unregister_sysctl_table(subh);
1369 * register_sysctl_table_path - register a sysctl table hierarchy
1370 * @path: The path to the directory the sysctl table is in.
1371 * @table: the top-level table structure
1373 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1374 * array. A completely 0 filled entry terminates the table.
1376 * See __register_sysctl_paths for more details.
1378 struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
1379 struct ctl_table *table)
1381 return __register_sysctl_paths(&sysctl_table_root.default_set,
1384 EXPORT_SYMBOL(register_sysctl_paths);
1387 * register_sysctl_table - register a sysctl table hierarchy
1388 * @table: the top-level table structure
1390 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1391 * array. A completely 0 filled entry terminates the table.
1393 * See register_sysctl_paths for more details.
1395 struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
1397 static const struct ctl_path null_path[] = { {} };
1399 return register_sysctl_paths(null_path, table);
1401 EXPORT_SYMBOL(register_sysctl_table);
1403 static void put_links(struct ctl_table_header *header)
1405 struct ctl_table_set *root_set = &sysctl_table_root.default_set;
1406 struct ctl_table_root *root = header->root;
1407 struct ctl_dir *parent = header->parent;
1408 struct ctl_dir *core_parent;
1409 struct ctl_table *entry;
1411 if (header->set == root_set)
1414 core_parent = xlate_dir(root_set, parent);
1415 if (IS_ERR(core_parent))
1418 for (entry = header->ctl_table; entry->procname; entry++) {
1419 struct ctl_table_header *link_head;
1420 struct ctl_table *link;
1421 const char *name = entry->procname;
1423 link = find_entry(&link_head, core_parent, name, strlen(name));
1425 ((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) ||
1426 (S_ISLNK(link->mode) && (link->data == root)))) {
1427 drop_sysctl_table(link_head);
1430 printk(KERN_ERR "sysctl link missing during unregister: ");
1431 sysctl_print_dir(parent);
1432 printk(KERN_CONT "/%s\n", name);
1437 static void drop_sysctl_table(struct ctl_table_header *header)
1439 struct ctl_dir *parent = header->parent;
1445 start_unregistering(header);
1446 if (!--header->count)
1447 kfree_rcu(header, rcu);
1450 drop_sysctl_table(&parent->header);
1454 * unregister_sysctl_table - unregister a sysctl table hierarchy
1455 * @header: the header returned from register_sysctl_table
1457 * Unregisters the sysctl table and all children. proc entries may not
1458 * actually be removed until they are no longer used by anyone.
1460 void unregister_sysctl_table(struct ctl_table_header * header)
1468 nr_subheaders = count_subheaders(header->ctl_table_arg);
1469 if (unlikely(nr_subheaders > 1)) {
1470 struct ctl_table_header **subheaders;
1473 subheaders = (struct ctl_table_header **)(header + 1);
1474 for (i = nr_subheaders -1; i >= 0; i--) {
1475 struct ctl_table_header *subh = subheaders[i];
1476 struct ctl_table *table = subh->ctl_table_arg;
1477 unregister_sysctl_table(subh);
1484 spin_lock(&sysctl_lock);
1485 drop_sysctl_table(header);
1486 spin_unlock(&sysctl_lock);
1488 EXPORT_SYMBOL(unregister_sysctl_table);
1490 void setup_sysctl_set(struct ctl_table_set *set,
1491 struct ctl_table_root *root,
1492 int (*is_seen)(struct ctl_table_set *))
1494 memset(set, sizeof(*set), 0);
1495 INIT_LIST_HEAD(&set->list);
1496 set->is_seen = is_seen;
1497 init_header(&set->dir.header, root, set, root_table);
1500 void retire_sysctl_set(struct ctl_table_set *set)
1502 WARN_ON(!list_empty(&set->list));
1505 int __init proc_sys_init(void)
1507 struct proc_dir_entry *proc_sys_root;
1509 proc_sys_root = proc_mkdir("sys", NULL);
1510 proc_sys_root->proc_iops = &proc_sys_dir_operations;
1511 proc_sys_root->proc_fops = &proc_sys_dir_file_operations;
1512 proc_sys_root->nlink = 0;
1514 return sysctl_init();