177b14db5SEric W. Biederman /* 277b14db5SEric W. Biederman * /proc/sys support 377b14db5SEric W. Biederman */ 41e0edd3fSAlexey Dobriyan #include <linux/init.h> 577b14db5SEric W. Biederman #include <linux/sysctl.h> 6f1ecf068SLucas De Marchi #include <linux/poll.h> 777b14db5SEric W. Biederman #include <linux/proc_fs.h> 877b14db5SEric W. Biederman #include <linux/security.h> 940401530SAl Viro #include <linux/sched.h> 1034286d66SNick Piggin #include <linux/namei.h> 1140401530SAl Viro #include <linux/mm.h> 121f87f0b5SEric W. Biederman #include <linux/module.h> 1377b14db5SEric W. Biederman #include "internal.h" 1477b14db5SEric W. Biederman 15d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations; 1677b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations; 1703a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations; 189043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations; 199043476fSAl Viro static const struct inode_operations proc_sys_dir_operations; 2077b14db5SEric W. Biederman 21f1ecf068SLucas De Marchi void proc_sys_poll_notify(struct ctl_table_poll *poll) 22f1ecf068SLucas De Marchi { 23f1ecf068SLucas De Marchi if (!poll) 24f1ecf068SLucas De Marchi return; 25f1ecf068SLucas De Marchi 26f1ecf068SLucas De Marchi atomic_inc(&poll->event); 27f1ecf068SLucas De Marchi wake_up_interruptible(&poll->wait); 28f1ecf068SLucas De Marchi } 29f1ecf068SLucas De Marchi 30a194558eSEric W. Biederman static struct ctl_table root_table[] = { 31a194558eSEric W. Biederman { 32a194558eSEric W. Biederman .procname = "", 337ec66d06SEric W. Biederman .mode = S_IFDIR|S_IRUGO|S_IXUGO, 34a194558eSEric W. Biederman }, 35a194558eSEric W. Biederman { } 36a194558eSEric W. Biederman }; 370e47c99dSEric W. Biederman static struct ctl_table_root sysctl_table_root = { 380e47c99dSEric W. Biederman .default_set.dir.header = { 391f87f0b5SEric W. Biederman {{.count = 1, 40938aaa4fSEric W. Biederman .nreg = 1, 41ac13ac6fSEric W. Biederman .ctl_table = root_table }}, 420e47c99dSEric W. Biederman .ctl_table_arg = root_table, 431f87f0b5SEric W. Biederman .root = &sysctl_table_root, 441f87f0b5SEric W. Biederman .set = &sysctl_table_root.default_set, 457ec66d06SEric W. Biederman }, 461f87f0b5SEric W. Biederman }; 471f87f0b5SEric W. Biederman 481f87f0b5SEric W. Biederman static DEFINE_SPINLOCK(sysctl_lock); 491f87f0b5SEric W. Biederman 507ec66d06SEric W. Biederman static void drop_sysctl_table(struct ctl_table_header *header); 510e47c99dSEric W. Biederman static int sysctl_follow_link(struct ctl_table_header **phead, 520e47c99dSEric W. Biederman struct ctl_table **pentry, struct nsproxy *namespaces); 530e47c99dSEric W. Biederman static int insert_links(struct ctl_table_header *head); 540e47c99dSEric W. Biederman static void put_links(struct ctl_table_header *header); 557ec66d06SEric W. Biederman 566980128fSEric W. Biederman static void sysctl_print_dir(struct ctl_dir *dir) 576980128fSEric W. Biederman { 586980128fSEric W. Biederman if (dir->header.parent) 596980128fSEric W. Biederman sysctl_print_dir(dir->header.parent); 606980128fSEric W. Biederman printk(KERN_CONT "%s/", dir->header.ctl_table[0].procname); 616980128fSEric W. Biederman } 626980128fSEric W. Biederman 63076c3eedSEric W. Biederman static int namecmp(const char *name1, int len1, const char *name2, int len2) 64076c3eedSEric W. Biederman { 65076c3eedSEric W. Biederman int minlen; 66076c3eedSEric W. Biederman int cmp; 67076c3eedSEric W. Biederman 68076c3eedSEric W. Biederman minlen = len1; 69076c3eedSEric W. Biederman if (minlen > len2) 70076c3eedSEric W. Biederman minlen = len2; 71076c3eedSEric W. Biederman 72076c3eedSEric W. Biederman cmp = memcmp(name1, name2, minlen); 73076c3eedSEric W. Biederman if (cmp == 0) 74076c3eedSEric W. Biederman cmp = len1 - len2; 75076c3eedSEric W. Biederman return cmp; 76076c3eedSEric W. Biederman } 77076c3eedSEric W. Biederman 7860f126d9SEric W. Biederman /* Called under sysctl_lock */ 79076c3eedSEric W. Biederman static struct ctl_table *find_entry(struct ctl_table_header **phead, 800e47c99dSEric W. Biederman struct ctl_dir *dir, const char *name, int namelen) 81076c3eedSEric W. Biederman { 82076c3eedSEric W. Biederman struct ctl_table_header *head; 83076c3eedSEric W. Biederman struct ctl_table *entry; 84ac13ac6fSEric W. Biederman struct rb_node *node = dir->root.rb_node; 85076c3eedSEric W. Biederman 86ac13ac6fSEric W. Biederman while (node) 87ac13ac6fSEric W. Biederman { 88ac13ac6fSEric W. Biederman struct ctl_node *ctl_node; 89ac13ac6fSEric W. Biederman const char *procname; 90ac13ac6fSEric W. Biederman int cmp; 91ac13ac6fSEric W. Biederman 92ac13ac6fSEric W. Biederman ctl_node = rb_entry(node, struct ctl_node, node); 93ac13ac6fSEric W. Biederman head = ctl_node->header; 94ac13ac6fSEric W. Biederman entry = &head->ctl_table[ctl_node - head->node]; 95ac13ac6fSEric W. Biederman procname = entry->procname; 96ac13ac6fSEric W. Biederman 97ac13ac6fSEric W. Biederman cmp = namecmp(name, namelen, procname, strlen(procname)); 98ac13ac6fSEric W. Biederman if (cmp < 0) 99ac13ac6fSEric W. Biederman node = node->rb_left; 100ac13ac6fSEric W. Biederman else if (cmp > 0) 101ac13ac6fSEric W. Biederman node = node->rb_right; 102ac13ac6fSEric W. Biederman else { 103076c3eedSEric W. Biederman *phead = head; 104076c3eedSEric W. Biederman return entry; 105076c3eedSEric W. Biederman } 106076c3eedSEric W. Biederman } 107076c3eedSEric W. Biederman return NULL; 108076c3eedSEric W. Biederman } 109076c3eedSEric W. Biederman 110ac13ac6fSEric W. Biederman static int insert_entry(struct ctl_table_header *head, struct ctl_table *entry) 111ac13ac6fSEric W. Biederman { 112ac13ac6fSEric W. Biederman struct rb_node *node = &head->node[entry - head->ctl_table].node; 113ac13ac6fSEric W. Biederman struct rb_node **p = &head->parent->root.rb_node; 114ac13ac6fSEric W. Biederman struct rb_node *parent = NULL; 115ac13ac6fSEric W. Biederman const char *name = entry->procname; 116ac13ac6fSEric W. Biederman int namelen = strlen(name); 117ac13ac6fSEric W. Biederman 118ac13ac6fSEric W. Biederman while (*p) { 119ac13ac6fSEric W. Biederman struct ctl_table_header *parent_head; 120ac13ac6fSEric W. Biederman struct ctl_table *parent_entry; 121ac13ac6fSEric W. Biederman struct ctl_node *parent_node; 122ac13ac6fSEric W. Biederman const char *parent_name; 123ac13ac6fSEric W. Biederman int cmp; 124ac13ac6fSEric W. Biederman 125ac13ac6fSEric W. Biederman parent = *p; 126ac13ac6fSEric W. Biederman parent_node = rb_entry(parent, struct ctl_node, node); 127ac13ac6fSEric W. Biederman parent_head = parent_node->header; 128ac13ac6fSEric W. Biederman parent_entry = &parent_head->ctl_table[parent_node - parent_head->node]; 129ac13ac6fSEric W. Biederman parent_name = parent_entry->procname; 130ac13ac6fSEric W. Biederman 131ac13ac6fSEric W. Biederman cmp = namecmp(name, namelen, parent_name, strlen(parent_name)); 132ac13ac6fSEric W. Biederman if (cmp < 0) 133ac13ac6fSEric W. Biederman p = &(*p)->rb_left; 134ac13ac6fSEric W. Biederman else if (cmp > 0) 135ac13ac6fSEric W. Biederman p = &(*p)->rb_right; 136ac13ac6fSEric W. Biederman else { 137ac13ac6fSEric W. Biederman printk(KERN_ERR "sysctl duplicate entry: "); 138ac13ac6fSEric W. Biederman sysctl_print_dir(head->parent); 139ac13ac6fSEric W. Biederman printk(KERN_CONT "/%s\n", entry->procname); 140ac13ac6fSEric W. Biederman return -EEXIST; 141ac13ac6fSEric W. Biederman } 142ac13ac6fSEric W. Biederman } 143ac13ac6fSEric W. Biederman 144ac13ac6fSEric W. Biederman rb_link_node(node, parent, p); 145ac13ac6fSEric W. Biederman return 0; 146ac13ac6fSEric W. Biederman } 147ac13ac6fSEric W. Biederman 148ac13ac6fSEric W. Biederman static void erase_entry(struct ctl_table_header *head, struct ctl_table *entry) 149ac13ac6fSEric W. Biederman { 150ac13ac6fSEric W. Biederman struct rb_node *node = &head->node[entry - head->ctl_table].node; 151ac13ac6fSEric W. Biederman 152ac13ac6fSEric W. Biederman rb_erase(node, &head->parent->root); 153ac13ac6fSEric W. Biederman } 154ac13ac6fSEric W. Biederman 155e0d04529SEric W. Biederman static void init_header(struct ctl_table_header *head, 156e0d04529SEric W. Biederman struct ctl_table_root *root, struct ctl_table_set *set, 157ac13ac6fSEric W. Biederman struct ctl_node *node, struct ctl_table *table) 158e0d04529SEric W. Biederman { 1597ec66d06SEric W. Biederman head->ctl_table = table; 160e0d04529SEric W. Biederman head->ctl_table_arg = table; 161e0d04529SEric W. Biederman head->used = 0; 162e0d04529SEric W. Biederman head->count = 1; 163e0d04529SEric W. Biederman head->nreg = 1; 164e0d04529SEric W. Biederman head->unregistering = NULL; 165e0d04529SEric W. Biederman head->root = root; 166e0d04529SEric W. Biederman head->set = set; 167e0d04529SEric W. Biederman head->parent = NULL; 168ac13ac6fSEric W. Biederman head->node = node; 169ac13ac6fSEric W. Biederman if (node) { 170ac13ac6fSEric W. Biederman struct ctl_table *entry; 171ac13ac6fSEric W. Biederman for (entry = table; entry->procname; entry++, node++) { 172ac13ac6fSEric W. Biederman rb_init_node(&node->node); 173ac13ac6fSEric W. Biederman node->header = head; 174ac13ac6fSEric W. Biederman } 175ac13ac6fSEric W. Biederman } 176e0d04529SEric W. Biederman } 177e0d04529SEric W. Biederman 1788425d6aaSEric W. Biederman static void erase_header(struct ctl_table_header *head) 1798425d6aaSEric W. Biederman { 180ac13ac6fSEric W. Biederman struct ctl_table *entry; 181ac13ac6fSEric W. Biederman for (entry = head->ctl_table; entry->procname; entry++) 182ac13ac6fSEric W. Biederman erase_entry(head, entry); 1838425d6aaSEric W. Biederman } 1848425d6aaSEric W. Biederman 1850e47c99dSEric W. Biederman static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header) 1868425d6aaSEric W. Biederman { 187ac13ac6fSEric W. Biederman struct ctl_table *entry; 1880e47c99dSEric W. Biederman int err; 1890e47c99dSEric W. Biederman 1900e47c99dSEric W. Biederman dir->header.nreg++; 1917ec66d06SEric W. Biederman header->parent = dir; 1920e47c99dSEric W. Biederman err = insert_links(header); 1930e47c99dSEric W. Biederman if (err) 1940e47c99dSEric W. Biederman goto fail_links; 195ac13ac6fSEric W. Biederman for (entry = header->ctl_table; entry->procname; entry++) { 196ac13ac6fSEric W. Biederman err = insert_entry(header, entry); 197ac13ac6fSEric W. Biederman if (err) 198ac13ac6fSEric W. Biederman goto fail; 199ac13ac6fSEric W. Biederman } 2000e47c99dSEric W. Biederman return 0; 201ac13ac6fSEric W. Biederman fail: 202ac13ac6fSEric W. Biederman erase_header(header); 203ac13ac6fSEric W. Biederman put_links(header); 2040e47c99dSEric W. Biederman fail_links: 2050e47c99dSEric W. Biederman header->parent = NULL; 2060e47c99dSEric W. Biederman drop_sysctl_table(&dir->header); 2070e47c99dSEric W. Biederman return err; 2088425d6aaSEric W. Biederman } 2098425d6aaSEric W. Biederman 2101f87f0b5SEric W. Biederman /* called under sysctl_lock */ 2111f87f0b5SEric W. Biederman static int use_table(struct ctl_table_header *p) 2121f87f0b5SEric W. Biederman { 2131f87f0b5SEric W. Biederman if (unlikely(p->unregistering)) 2141f87f0b5SEric W. Biederman return 0; 2151f87f0b5SEric W. Biederman p->used++; 2161f87f0b5SEric W. Biederman return 1; 2171f87f0b5SEric W. Biederman } 2181f87f0b5SEric W. Biederman 2191f87f0b5SEric W. Biederman /* called under sysctl_lock */ 2201f87f0b5SEric W. Biederman static void unuse_table(struct ctl_table_header *p) 2211f87f0b5SEric W. Biederman { 2221f87f0b5SEric W. Biederman if (!--p->used) 2231f87f0b5SEric W. Biederman if (unlikely(p->unregistering)) 2241f87f0b5SEric W. Biederman complete(p->unregistering); 2251f87f0b5SEric W. Biederman } 2261f87f0b5SEric W. Biederman 2271f87f0b5SEric W. Biederman /* called under sysctl_lock, will reacquire if has to wait */ 2281f87f0b5SEric W. Biederman static void start_unregistering(struct ctl_table_header *p) 2291f87f0b5SEric W. Biederman { 2301f87f0b5SEric W. Biederman /* 2311f87f0b5SEric W. Biederman * if p->used is 0, nobody will ever touch that entry again; 2321f87f0b5SEric W. Biederman * we'll eliminate all paths to it before dropping sysctl_lock 2331f87f0b5SEric W. Biederman */ 2341f87f0b5SEric W. Biederman if (unlikely(p->used)) { 2351f87f0b5SEric W. Biederman struct completion wait; 2361f87f0b5SEric W. Biederman init_completion(&wait); 2371f87f0b5SEric W. Biederman p->unregistering = &wait; 2381f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 2391f87f0b5SEric W. Biederman wait_for_completion(&wait); 2401f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 2411f87f0b5SEric W. Biederman } else { 2421f87f0b5SEric W. Biederman /* anything non-NULL; we'll never dereference it */ 2431f87f0b5SEric W. Biederman p->unregistering = ERR_PTR(-EINVAL); 2441f87f0b5SEric W. Biederman } 2451f87f0b5SEric W. Biederman /* 2461f87f0b5SEric W. Biederman * do not remove from the list until nobody holds it; walking the 2471f87f0b5SEric W. Biederman * list in do_sysctl() relies on that. 2481f87f0b5SEric W. Biederman */ 2498425d6aaSEric W. Biederman erase_header(p); 2501f87f0b5SEric W. Biederman } 2511f87f0b5SEric W. Biederman 2521f87f0b5SEric W. Biederman static void sysctl_head_get(struct ctl_table_header *head) 2531f87f0b5SEric W. Biederman { 2541f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 2551f87f0b5SEric W. Biederman head->count++; 2561f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 2571f87f0b5SEric W. Biederman } 2581f87f0b5SEric W. Biederman 2591f87f0b5SEric W. Biederman void sysctl_head_put(struct ctl_table_header *head) 2601f87f0b5SEric W. Biederman { 2611f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 2621f87f0b5SEric W. Biederman if (!--head->count) 2631f87f0b5SEric W. Biederman kfree_rcu(head, rcu); 2641f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 2651f87f0b5SEric W. Biederman } 2661f87f0b5SEric W. Biederman 2671f87f0b5SEric W. Biederman static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head) 2681f87f0b5SEric W. Biederman { 2691f87f0b5SEric W. Biederman if (!head) 2701f87f0b5SEric W. Biederman BUG(); 2711f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 2721f87f0b5SEric W. Biederman if (!use_table(head)) 2731f87f0b5SEric W. Biederman head = ERR_PTR(-ENOENT); 2741f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 2751f87f0b5SEric W. Biederman return head; 2761f87f0b5SEric W. Biederman } 2771f87f0b5SEric W. Biederman 2781f87f0b5SEric W. Biederman static void sysctl_head_finish(struct ctl_table_header *head) 2791f87f0b5SEric W. Biederman { 2801f87f0b5SEric W. Biederman if (!head) 2811f87f0b5SEric W. Biederman return; 2821f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 2831f87f0b5SEric W. Biederman unuse_table(head); 2841f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 2851f87f0b5SEric W. Biederman } 2861f87f0b5SEric W. Biederman 2871f87f0b5SEric W. Biederman static struct ctl_table_set * 2881f87f0b5SEric W. Biederman lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces) 2891f87f0b5SEric W. Biederman { 2901f87f0b5SEric W. Biederman struct ctl_table_set *set = &root->default_set; 2911f87f0b5SEric W. Biederman if (root->lookup) 2921f87f0b5SEric W. Biederman set = root->lookup(root, namespaces); 2931f87f0b5SEric W. Biederman return set; 2941f87f0b5SEric W. Biederman } 2951f87f0b5SEric W. Biederman 296076c3eedSEric W. Biederman static struct ctl_table *lookup_entry(struct ctl_table_header **phead, 2977ec66d06SEric W. Biederman struct ctl_dir *dir, 298076c3eedSEric W. Biederman const char *name, int namelen) 299076c3eedSEric W. Biederman { 300076c3eedSEric W. Biederman struct ctl_table_header *head; 301076c3eedSEric W. Biederman struct ctl_table *entry; 302076c3eedSEric W. Biederman 303076c3eedSEric W. Biederman spin_lock(&sysctl_lock); 3040e47c99dSEric W. Biederman entry = find_entry(&head, dir, name, namelen); 305076c3eedSEric W. Biederman if (entry && use_table(head)) 306076c3eedSEric W. Biederman *phead = head; 307076c3eedSEric W. Biederman else 308076c3eedSEric W. Biederman entry = NULL; 309076c3eedSEric W. Biederman spin_unlock(&sysctl_lock); 310076c3eedSEric W. Biederman return entry; 311076c3eedSEric W. Biederman } 312076c3eedSEric W. Biederman 313ac13ac6fSEric W. Biederman static struct ctl_node *first_usable_entry(struct rb_node *node) 3141f87f0b5SEric W. Biederman { 315ac13ac6fSEric W. Biederman struct ctl_node *ctl_node; 3161f87f0b5SEric W. Biederman 317ac13ac6fSEric W. Biederman for (;node; node = rb_next(node)) { 318ac13ac6fSEric W. Biederman ctl_node = rb_entry(node, struct ctl_node, node); 319ac13ac6fSEric W. Biederman if (use_table(ctl_node->header)) 320ac13ac6fSEric W. Biederman return ctl_node; 3211f87f0b5SEric W. Biederman } 3221f87f0b5SEric W. Biederman return NULL; 3231f87f0b5SEric W. Biederman } 3241f87f0b5SEric W. Biederman 3257ec66d06SEric W. Biederman static void first_entry(struct ctl_dir *dir, 3266a75ce16SEric W. Biederman struct ctl_table_header **phead, struct ctl_table **pentry) 3271f87f0b5SEric W. Biederman { 328ac13ac6fSEric W. Biederman struct ctl_table_header *head = NULL; 3297ec66d06SEric W. Biederman struct ctl_table *entry = NULL; 330ac13ac6fSEric W. Biederman struct ctl_node *ctl_node; 3316a75ce16SEric W. Biederman 3326a75ce16SEric W. Biederman spin_lock(&sysctl_lock); 333ac13ac6fSEric W. Biederman ctl_node = first_usable_entry(rb_first(&dir->root)); 3346a75ce16SEric W. Biederman spin_unlock(&sysctl_lock); 335ac13ac6fSEric W. Biederman if (ctl_node) { 336ac13ac6fSEric W. Biederman head = ctl_node->header; 337ac13ac6fSEric W. Biederman entry = &head->ctl_table[ctl_node - head->node]; 338ac13ac6fSEric W. Biederman } 3396a75ce16SEric W. Biederman *phead = head; 3406a75ce16SEric W. Biederman *pentry = entry; 3416a75ce16SEric W. Biederman } 3426a75ce16SEric W. Biederman 3437ec66d06SEric W. Biederman static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry) 3446a75ce16SEric W. Biederman { 3456a75ce16SEric W. Biederman struct ctl_table_header *head = *phead; 3466a75ce16SEric W. Biederman struct ctl_table *entry = *pentry; 347ac13ac6fSEric W. Biederman struct ctl_node *ctl_node = &head->node[entry - head->ctl_table]; 3486a75ce16SEric W. Biederman 3496a75ce16SEric W. Biederman spin_lock(&sysctl_lock); 3506a75ce16SEric W. Biederman unuse_table(head); 351ac13ac6fSEric W. Biederman 352ac13ac6fSEric W. Biederman ctl_node = first_usable_entry(rb_next(&ctl_node->node)); 3536a75ce16SEric W. Biederman spin_unlock(&sysctl_lock); 354ac13ac6fSEric W. Biederman head = NULL; 355ac13ac6fSEric W. Biederman if (ctl_node) { 356ac13ac6fSEric W. Biederman head = ctl_node->header; 357ac13ac6fSEric W. Biederman entry = &head->ctl_table[ctl_node - head->node]; 3586a75ce16SEric W. Biederman } 3596a75ce16SEric W. Biederman *phead = head; 3606a75ce16SEric W. Biederman *pentry = entry; 3611f87f0b5SEric W. Biederman } 3621f87f0b5SEric W. Biederman 3631f87f0b5SEric W. Biederman void register_sysctl_root(struct ctl_table_root *root) 3641f87f0b5SEric W. Biederman { 3651f87f0b5SEric W. Biederman } 3661f87f0b5SEric W. Biederman 3671f87f0b5SEric W. Biederman /* 3681f87f0b5SEric W. Biederman * sysctl_perm does NOT grant the superuser all rights automatically, because 3691f87f0b5SEric W. Biederman * some sysctl variables are readonly even to root. 3701f87f0b5SEric W. Biederman */ 3711f87f0b5SEric W. Biederman 3721f87f0b5SEric W. Biederman static int test_perm(int mode, int op) 3731f87f0b5SEric W. Biederman { 374091bd3eaSEric W. Biederman if (uid_eq(current_euid(), GLOBAL_ROOT_UID)) 3751f87f0b5SEric W. Biederman mode >>= 6; 376091bd3eaSEric W. Biederman else if (in_egroup_p(GLOBAL_ROOT_GID)) 3771f87f0b5SEric W. Biederman mode >>= 3; 3781f87f0b5SEric W. Biederman if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0) 3791f87f0b5SEric W. Biederman return 0; 3801f87f0b5SEric W. Biederman return -EACCES; 3811f87f0b5SEric W. Biederman } 3821f87f0b5SEric W. Biederman 3831f87f0b5SEric W. Biederman static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op) 3841f87f0b5SEric W. Biederman { 3851f87f0b5SEric W. Biederman int mode; 3861f87f0b5SEric W. Biederman 3871f87f0b5SEric W. Biederman if (root->permissions) 3881f87f0b5SEric W. Biederman mode = root->permissions(root, current->nsproxy, table); 3891f87f0b5SEric W. Biederman else 3901f87f0b5SEric W. Biederman mode = table->mode; 3911f87f0b5SEric W. Biederman 3921f87f0b5SEric W. Biederman return test_perm(mode, op); 3931f87f0b5SEric W. Biederman } 3941f87f0b5SEric W. Biederman 3959043476fSAl Viro static struct inode *proc_sys_make_inode(struct super_block *sb, 3969043476fSAl Viro struct ctl_table_header *head, struct ctl_table *table) 39777b14db5SEric W. Biederman { 39877b14db5SEric W. Biederman struct inode *inode; 3999043476fSAl Viro struct proc_inode *ei; 40077b14db5SEric W. Biederman 4019043476fSAl Viro inode = new_inode(sb); 40277b14db5SEric W. Biederman if (!inode) 40377b14db5SEric W. Biederman goto out; 40477b14db5SEric W. Biederman 40585fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 40685fe4025SChristoph Hellwig 4079043476fSAl Viro sysctl_head_get(head); 40877b14db5SEric W. Biederman ei = PROC_I(inode); 4099043476fSAl Viro ei->sysctl = head; 4109043476fSAl Viro ei->sysctl_entry = table; 4119043476fSAl Viro 41277b14db5SEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 4139043476fSAl Viro inode->i_mode = table->mode; 4147ec66d06SEric W. Biederman if (!S_ISDIR(table->mode)) { 4159043476fSAl Viro inode->i_mode |= S_IFREG; 41677b14db5SEric W. Biederman inode->i_op = &proc_sys_inode_operations; 41777b14db5SEric W. Biederman inode->i_fop = &proc_sys_file_operations; 4189043476fSAl Viro } else { 4199043476fSAl Viro inode->i_mode |= S_IFDIR; 4209043476fSAl Viro inode->i_op = &proc_sys_dir_operations; 4219043476fSAl Viro inode->i_fop = &proc_sys_dir_file_operations; 4229043476fSAl Viro } 42377b14db5SEric W. Biederman out: 42477b14db5SEric W. Biederman return inode; 42577b14db5SEric W. Biederman } 42677b14db5SEric W. Biederman 42781324364SAdrian Bunk static struct ctl_table_header *grab_header(struct inode *inode) 42877b14db5SEric W. Biederman { 4293cc3e046SEric W. Biederman struct ctl_table_header *head = PROC_I(inode)->sysctl; 4303cc3e046SEric W. Biederman if (!head) 4310e47c99dSEric W. Biederman head = &sysctl_table_root.default_set.dir.header; 4323cc3e046SEric W. Biederman return sysctl_head_grab(head); 43377b14db5SEric W. Biederman } 43477b14db5SEric W. Biederman 43577b14db5SEric W. Biederman static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry, 43600cd8dd3SAl Viro unsigned int flags) 43777b14db5SEric W. Biederman { 4389043476fSAl Viro struct ctl_table_header *head = grab_header(dir); 4399043476fSAl Viro struct ctl_table_header *h = NULL; 4409043476fSAl Viro struct qstr *name = &dentry->d_name; 4419043476fSAl Viro struct ctl_table *p; 44277b14db5SEric W. Biederman struct inode *inode; 4439043476fSAl Viro struct dentry *err = ERR_PTR(-ENOENT); 4447ec66d06SEric W. Biederman struct ctl_dir *ctl_dir; 4450e47c99dSEric W. Biederman int ret; 44677b14db5SEric W. Biederman 4479043476fSAl Viro if (IS_ERR(head)) 4489043476fSAl Viro return ERR_CAST(head); 4499043476fSAl Viro 4507ec66d06SEric W. Biederman ctl_dir = container_of(head, struct ctl_dir, header); 4519043476fSAl Viro 4527ec66d06SEric W. Biederman p = lookup_entry(&h, ctl_dir, name->name, name->len); 4539043476fSAl Viro if (!p) 45477b14db5SEric W. Biederman goto out; 45577b14db5SEric W. Biederman 4564e757320SEric W. Biederman if (S_ISLNK(p->mode)) { 4570e47c99dSEric W. Biederman ret = sysctl_follow_link(&h, &p, current->nsproxy); 4580e47c99dSEric W. Biederman err = ERR_PTR(ret); 4590e47c99dSEric W. Biederman if (ret) 4600e47c99dSEric W. Biederman goto out; 4614e757320SEric W. Biederman } 4620e47c99dSEric W. Biederman 46377b14db5SEric W. Biederman err = ERR_PTR(-ENOMEM); 4649043476fSAl Viro inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p); 46577b14db5SEric W. Biederman if (!inode) 46677b14db5SEric W. Biederman goto out; 46777b14db5SEric W. Biederman 46877b14db5SEric W. Biederman err = NULL; 469fb045adbSNick Piggin d_set_d_op(dentry, &proc_sys_dentry_operations); 47077b14db5SEric W. Biederman d_add(dentry, inode); 47177b14db5SEric W. Biederman 47277b14db5SEric W. Biederman out: 4736bf61045SFrancesco Ruggeri if (h) 4746bf61045SFrancesco Ruggeri sysctl_head_finish(h); 47577b14db5SEric W. Biederman sysctl_head_finish(head); 47677b14db5SEric W. Biederman return err; 47777b14db5SEric W. Biederman } 47877b14db5SEric W. Biederman 4797708bfb1SPavel Emelyanov static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf, 4807708bfb1SPavel Emelyanov size_t count, loff_t *ppos, int write) 48177b14db5SEric W. Biederman { 4829043476fSAl Viro struct inode *inode = filp->f_path.dentry->d_inode; 4839043476fSAl Viro struct ctl_table_header *head = grab_header(inode); 4849043476fSAl Viro struct ctl_table *table = PROC_I(inode)->sysctl_entry; 4852a2da53bSDavid Howells ssize_t error; 4862a2da53bSDavid Howells size_t res; 48777b14db5SEric W. Biederman 4889043476fSAl Viro if (IS_ERR(head)) 4899043476fSAl Viro return PTR_ERR(head); 49077b14db5SEric W. Biederman 49177b14db5SEric W. Biederman /* 49277b14db5SEric W. Biederman * At this point we know that the sysctl was not unregistered 49377b14db5SEric W. Biederman * and won't be until we finish. 49477b14db5SEric W. Biederman */ 49577b14db5SEric W. Biederman error = -EPERM; 496d7321cd6SPavel Emelyanov if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ)) 49777b14db5SEric W. Biederman goto out; 49877b14db5SEric W. Biederman 4999043476fSAl Viro /* if that can happen at all, it should be -EINVAL, not -EISDIR */ 5009043476fSAl Viro error = -EINVAL; 5019043476fSAl Viro if (!table->proc_handler) 5029043476fSAl Viro goto out; 5039043476fSAl Viro 50477b14db5SEric W. Biederman /* careful: calling conventions are nasty here */ 50577b14db5SEric W. Biederman res = count; 5068d65af78SAlexey Dobriyan error = table->proc_handler(table, write, buf, &res, ppos); 50777b14db5SEric W. Biederman if (!error) 50877b14db5SEric W. Biederman error = res; 50977b14db5SEric W. Biederman out: 51077b14db5SEric W. Biederman sysctl_head_finish(head); 51177b14db5SEric W. Biederman 51277b14db5SEric W. Biederman return error; 51377b14db5SEric W. Biederman } 51477b14db5SEric W. Biederman 5157708bfb1SPavel Emelyanov static ssize_t proc_sys_read(struct file *filp, char __user *buf, 5167708bfb1SPavel Emelyanov size_t count, loff_t *ppos) 5177708bfb1SPavel Emelyanov { 5187708bfb1SPavel Emelyanov return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0); 5197708bfb1SPavel Emelyanov } 5207708bfb1SPavel Emelyanov 52177b14db5SEric W. Biederman static ssize_t proc_sys_write(struct file *filp, const char __user *buf, 52277b14db5SEric W. Biederman size_t count, loff_t *ppos) 52377b14db5SEric W. Biederman { 5247708bfb1SPavel Emelyanov return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1); 52577b14db5SEric W. Biederman } 52677b14db5SEric W. Biederman 527f1ecf068SLucas De Marchi static int proc_sys_open(struct inode *inode, struct file *filp) 528f1ecf068SLucas De Marchi { 5294e474a00SLucas De Marchi struct ctl_table_header *head = grab_header(inode); 530f1ecf068SLucas De Marchi struct ctl_table *table = PROC_I(inode)->sysctl_entry; 531f1ecf068SLucas De Marchi 5324e474a00SLucas De Marchi /* sysctl was unregistered */ 5334e474a00SLucas De Marchi if (IS_ERR(head)) 5344e474a00SLucas De Marchi return PTR_ERR(head); 5354e474a00SLucas De Marchi 536f1ecf068SLucas De Marchi if (table->poll) 537f1ecf068SLucas De Marchi filp->private_data = proc_sys_poll_event(table->poll); 538f1ecf068SLucas De Marchi 5394e474a00SLucas De Marchi sysctl_head_finish(head); 5404e474a00SLucas De Marchi 541f1ecf068SLucas De Marchi return 0; 542f1ecf068SLucas De Marchi } 543f1ecf068SLucas De Marchi 544f1ecf068SLucas De Marchi static unsigned int proc_sys_poll(struct file *filp, poll_table *wait) 545f1ecf068SLucas De Marchi { 546f1ecf068SLucas De Marchi struct inode *inode = filp->f_path.dentry->d_inode; 5474e474a00SLucas De Marchi struct ctl_table_header *head = grab_header(inode); 548f1ecf068SLucas De Marchi struct ctl_table *table = PROC_I(inode)->sysctl_entry; 549f1ecf068SLucas De Marchi unsigned int ret = DEFAULT_POLLMASK; 5504e474a00SLucas De Marchi unsigned long event; 5514e474a00SLucas De Marchi 5524e474a00SLucas De Marchi /* sysctl was unregistered */ 5534e474a00SLucas De Marchi if (IS_ERR(head)) 5544e474a00SLucas De Marchi return POLLERR | POLLHUP; 555f1ecf068SLucas De Marchi 556f1ecf068SLucas De Marchi if (!table->proc_handler) 557f1ecf068SLucas De Marchi goto out; 558f1ecf068SLucas De Marchi 559f1ecf068SLucas De Marchi if (!table->poll) 560f1ecf068SLucas De Marchi goto out; 561f1ecf068SLucas De Marchi 5624e474a00SLucas De Marchi event = (unsigned long)filp->private_data; 563f1ecf068SLucas De Marchi poll_wait(filp, &table->poll->wait, wait); 564f1ecf068SLucas De Marchi 565f1ecf068SLucas De Marchi if (event != atomic_read(&table->poll->event)) { 566f1ecf068SLucas De Marchi filp->private_data = proc_sys_poll_event(table->poll); 567f1ecf068SLucas De Marchi ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI; 568f1ecf068SLucas De Marchi } 569f1ecf068SLucas De Marchi 570f1ecf068SLucas De Marchi out: 5714e474a00SLucas De Marchi sysctl_head_finish(head); 5724e474a00SLucas De Marchi 573f1ecf068SLucas De Marchi return ret; 574f1ecf068SLucas De Marchi } 57577b14db5SEric W. Biederman 57677b14db5SEric W. Biederman static int proc_sys_fill_cache(struct file *filp, void *dirent, 5779043476fSAl Viro filldir_t filldir, 5789043476fSAl Viro struct ctl_table_header *head, 5799043476fSAl Viro struct ctl_table *table) 58077b14db5SEric W. Biederman { 58177b14db5SEric W. Biederman struct dentry *child, *dir = filp->f_path.dentry; 58277b14db5SEric W. Biederman struct inode *inode; 58377b14db5SEric W. Biederman struct qstr qname; 58477b14db5SEric W. Biederman ino_t ino = 0; 58577b14db5SEric W. Biederman unsigned type = DT_UNKNOWN; 58677b14db5SEric W. Biederman 58777b14db5SEric W. Biederman qname.name = table->procname; 58877b14db5SEric W. Biederman qname.len = strlen(table->procname); 58977b14db5SEric W. Biederman qname.hash = full_name_hash(qname.name, qname.len); 59077b14db5SEric W. Biederman 59177b14db5SEric W. Biederman child = d_lookup(dir, &qname); 59277b14db5SEric W. Biederman if (!child) { 5939043476fSAl Viro child = d_alloc(dir, &qname); 5949043476fSAl Viro if (child) { 5959043476fSAl Viro inode = proc_sys_make_inode(dir->d_sb, head, table); 5969043476fSAl Viro if (!inode) { 5979043476fSAl Viro dput(child); 5989043476fSAl Viro return -ENOMEM; 5999043476fSAl Viro } else { 600fb045adbSNick Piggin d_set_d_op(child, &proc_sys_dentry_operations); 6019043476fSAl Viro d_add(child, inode); 60277b14db5SEric W. Biederman } 6039043476fSAl Viro } else { 6049043476fSAl Viro return -ENOMEM; 60577b14db5SEric W. Biederman } 60677b14db5SEric W. Biederman } 60777b14db5SEric W. Biederman inode = child->d_inode; 60877b14db5SEric W. Biederman ino = inode->i_ino; 60977b14db5SEric W. Biederman type = inode->i_mode >> 12; 61077b14db5SEric W. Biederman dput(child); 6119043476fSAl Viro return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type); 6129043476fSAl Viro } 6139043476fSAl Viro 6140e47c99dSEric W. Biederman static int proc_sys_link_fill_cache(struct file *filp, void *dirent, 6150e47c99dSEric W. Biederman filldir_t filldir, 6160e47c99dSEric W. Biederman struct ctl_table_header *head, 6170e47c99dSEric W. Biederman struct ctl_table *table) 6180e47c99dSEric W. Biederman { 6190e47c99dSEric W. Biederman int err, ret = 0; 6200e47c99dSEric W. Biederman head = sysctl_head_grab(head); 6210e47c99dSEric W. Biederman 6224e757320SEric W. Biederman if (S_ISLNK(table->mode)) { 6230e47c99dSEric W. Biederman /* It is not an error if we can not follow the link ignore it */ 6240e47c99dSEric W. Biederman err = sysctl_follow_link(&head, &table, current->nsproxy); 6250e47c99dSEric W. Biederman if (err) 6260e47c99dSEric W. Biederman goto out; 6274e757320SEric W. Biederman } 6280e47c99dSEric W. Biederman 6290e47c99dSEric W. Biederman ret = proc_sys_fill_cache(filp, dirent, filldir, head, table); 6300e47c99dSEric W. Biederman out: 6310e47c99dSEric W. Biederman sysctl_head_finish(head); 6320e47c99dSEric W. Biederman return ret; 6330e47c99dSEric W. Biederman } 6340e47c99dSEric W. Biederman 6359043476fSAl Viro static int scan(struct ctl_table_header *head, ctl_table *table, 6369043476fSAl Viro unsigned long *pos, struct file *file, 6379043476fSAl Viro void *dirent, filldir_t filldir) 6389043476fSAl Viro { 6399043476fSAl Viro int res; 6409043476fSAl Viro 6416a75ce16SEric W. Biederman if ((*pos)++ < file->f_pos) 6429043476fSAl Viro return 0; 64377b14db5SEric W. Biederman 6440e47c99dSEric W. Biederman if (unlikely(S_ISLNK(table->mode))) 6450e47c99dSEric W. Biederman res = proc_sys_link_fill_cache(file, dirent, filldir, head, table); 6460e47c99dSEric W. Biederman else 64777b14db5SEric W. Biederman res = proc_sys_fill_cache(file, dirent, filldir, head, table); 64877b14db5SEric W. Biederman 6496a75ce16SEric W. Biederman if (res == 0) 6506a75ce16SEric W. Biederman file->f_pos = *pos; 6516a75ce16SEric W. Biederman 6526a75ce16SEric W. Biederman return res; 65377b14db5SEric W. Biederman } 65477b14db5SEric W. Biederman 65577b14db5SEric W. Biederman static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir) 65677b14db5SEric W. Biederman { 6579043476fSAl Viro struct dentry *dentry = filp->f_path.dentry; 65877b14db5SEric W. Biederman struct inode *inode = dentry->d_inode; 6599043476fSAl Viro struct ctl_table_header *head = grab_header(inode); 6609043476fSAl Viro struct ctl_table_header *h = NULL; 6616a75ce16SEric W. Biederman struct ctl_table *entry; 6627ec66d06SEric W. Biederman struct ctl_dir *ctl_dir; 66377b14db5SEric W. Biederman unsigned long pos; 6649043476fSAl Viro int ret = -EINVAL; 66577b14db5SEric W. Biederman 6669043476fSAl Viro if (IS_ERR(head)) 6679043476fSAl Viro return PTR_ERR(head); 6689043476fSAl Viro 6697ec66d06SEric W. Biederman ctl_dir = container_of(head, struct ctl_dir, header); 67077b14db5SEric W. Biederman 67177b14db5SEric W. Biederman ret = 0; 67277b14db5SEric W. Biederman /* Avoid a switch here: arm builds fail with missing __cmpdi2 */ 67377b14db5SEric W. Biederman if (filp->f_pos == 0) { 67477b14db5SEric W. Biederman if (filldir(dirent, ".", 1, filp->f_pos, 67577b14db5SEric W. Biederman inode->i_ino, DT_DIR) < 0) 67677b14db5SEric W. Biederman goto out; 67777b14db5SEric W. Biederman filp->f_pos++; 67877b14db5SEric W. Biederman } 67977b14db5SEric W. Biederman if (filp->f_pos == 1) { 68077b14db5SEric W. Biederman if (filldir(dirent, "..", 2, filp->f_pos, 68177b14db5SEric W. Biederman parent_ino(dentry), DT_DIR) < 0) 68277b14db5SEric W. Biederman goto out; 68377b14db5SEric W. Biederman filp->f_pos++; 68477b14db5SEric W. Biederman } 68577b14db5SEric W. Biederman pos = 2; 68677b14db5SEric W. Biederman 6877ec66d06SEric W. Biederman for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) { 6886a75ce16SEric W. Biederman ret = scan(h, entry, &pos, filp, dirent, filldir); 6899043476fSAl Viro if (ret) { 6909043476fSAl Viro sysctl_head_finish(h); 6919043476fSAl Viro break; 69277b14db5SEric W. Biederman } 69377b14db5SEric W. Biederman } 69477b14db5SEric W. Biederman ret = 1; 69577b14db5SEric W. Biederman out: 69677b14db5SEric W. Biederman sysctl_head_finish(head); 69777b14db5SEric W. Biederman return ret; 69877b14db5SEric W. Biederman } 69977b14db5SEric W. Biederman 70010556cb2SAl Viro static int proc_sys_permission(struct inode *inode, int mask) 70177b14db5SEric W. Biederman { 70277b14db5SEric W. Biederman /* 70377b14db5SEric W. Biederman * sysctl entries that are not writeable, 70477b14db5SEric W. Biederman * are _NOT_ writeable, capabilities or not. 70577b14db5SEric W. Biederman */ 706f696a365SMiklos Szeredi struct ctl_table_header *head; 707f696a365SMiklos Szeredi struct ctl_table *table; 70877b14db5SEric W. Biederman int error; 70977b14db5SEric W. Biederman 710f696a365SMiklos Szeredi /* Executable files are not allowed under /proc/sys/ */ 711f696a365SMiklos Szeredi if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) 712f696a365SMiklos Szeredi return -EACCES; 713f696a365SMiklos Szeredi 714f696a365SMiklos Szeredi head = grab_header(inode); 7159043476fSAl Viro if (IS_ERR(head)) 7169043476fSAl Viro return PTR_ERR(head); 71777b14db5SEric W. Biederman 718f696a365SMiklos Szeredi table = PROC_I(inode)->sysctl_entry; 7199043476fSAl Viro if (!table) /* global root - r-xr-xr-x */ 7209043476fSAl Viro error = mask & MAY_WRITE ? -EACCES : 0; 7219043476fSAl Viro else /* Use the permissions on the sysctl table entry */ 7221fc0f78cSAl Viro error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK); 7239043476fSAl Viro 72477b14db5SEric W. Biederman sysctl_head_finish(head); 72577b14db5SEric W. Biederman return error; 72677b14db5SEric W. Biederman } 72777b14db5SEric W. Biederman 72877b14db5SEric W. Biederman static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr) 72977b14db5SEric W. Biederman { 73077b14db5SEric W. Biederman struct inode *inode = dentry->d_inode; 73177b14db5SEric W. Biederman int error; 73277b14db5SEric W. Biederman 73377b14db5SEric W. Biederman if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)) 73477b14db5SEric W. Biederman return -EPERM; 73577b14db5SEric W. Biederman 73677b14db5SEric W. Biederman error = inode_change_ok(inode, attr); 7371025774cSChristoph Hellwig if (error) 73877b14db5SEric W. Biederman return error; 7391025774cSChristoph Hellwig 7401025774cSChristoph Hellwig if ((attr->ia_valid & ATTR_SIZE) && 7411025774cSChristoph Hellwig attr->ia_size != i_size_read(inode)) { 7421025774cSChristoph Hellwig error = vmtruncate(inode, attr->ia_size); 7431025774cSChristoph Hellwig if (error) 7441025774cSChristoph Hellwig return error; 7451025774cSChristoph Hellwig } 7461025774cSChristoph Hellwig 7471025774cSChristoph Hellwig setattr_copy(inode, attr); 7481025774cSChristoph Hellwig mark_inode_dirty(inode); 7491025774cSChristoph Hellwig return 0; 75077b14db5SEric W. Biederman } 75177b14db5SEric W. Biederman 7529043476fSAl Viro static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 7539043476fSAl Viro { 7549043476fSAl Viro struct inode *inode = dentry->d_inode; 7559043476fSAl Viro struct ctl_table_header *head = grab_header(inode); 7569043476fSAl Viro struct ctl_table *table = PROC_I(inode)->sysctl_entry; 7579043476fSAl Viro 7589043476fSAl Viro if (IS_ERR(head)) 7599043476fSAl Viro return PTR_ERR(head); 7609043476fSAl Viro 7619043476fSAl Viro generic_fillattr(inode, stat); 7629043476fSAl Viro if (table) 7639043476fSAl Viro stat->mode = (stat->mode & S_IFMT) | table->mode; 7649043476fSAl Viro 7659043476fSAl Viro sysctl_head_finish(head); 7669043476fSAl Viro return 0; 7679043476fSAl Viro } 7689043476fSAl Viro 76977b14db5SEric W. Biederman static const struct file_operations proc_sys_file_operations = { 770f1ecf068SLucas De Marchi .open = proc_sys_open, 771f1ecf068SLucas De Marchi .poll = proc_sys_poll, 77277b14db5SEric W. Biederman .read = proc_sys_read, 77377b14db5SEric W. Biederman .write = proc_sys_write, 7746038f373SArnd Bergmann .llseek = default_llseek, 7759043476fSAl Viro }; 7769043476fSAl Viro 7779043476fSAl Viro static const struct file_operations proc_sys_dir_file_operations = { 778887df078SPavel Emelyanov .read = generic_read_dir, 77977b14db5SEric W. Biederman .readdir = proc_sys_readdir, 7803222a3e5SChristoph Hellwig .llseek = generic_file_llseek, 78177b14db5SEric W. Biederman }; 78277b14db5SEric W. Biederman 78303a44825SJan Engelhardt static const struct inode_operations proc_sys_inode_operations = { 7849043476fSAl Viro .permission = proc_sys_permission, 7859043476fSAl Viro .setattr = proc_sys_setattr, 7869043476fSAl Viro .getattr = proc_sys_getattr, 7879043476fSAl Viro }; 7889043476fSAl Viro 7899043476fSAl Viro static const struct inode_operations proc_sys_dir_operations = { 79077b14db5SEric W. Biederman .lookup = proc_sys_lookup, 79177b14db5SEric W. Biederman .permission = proc_sys_permission, 79277b14db5SEric W. Biederman .setattr = proc_sys_setattr, 7939043476fSAl Viro .getattr = proc_sys_getattr, 79477b14db5SEric W. Biederman }; 79577b14db5SEric W. Biederman 7960b728e19SAl Viro static int proc_sys_revalidate(struct dentry *dentry, unsigned int flags) 79777b14db5SEric W. Biederman { 7980b728e19SAl Viro if (flags & LOOKUP_RCU) 79934286d66SNick Piggin return -ECHILD; 8009043476fSAl Viro return !PROC_I(dentry->d_inode)->sysctl->unregistering; 8019043476fSAl Viro } 8029043476fSAl Viro 803fe15ce44SNick Piggin static int proc_sys_delete(const struct dentry *dentry) 8049043476fSAl Viro { 8059043476fSAl Viro return !!PROC_I(dentry->d_inode)->sysctl->unregistering; 8069043476fSAl Viro } 8079043476fSAl Viro 8081f87f0b5SEric W. Biederman static int sysctl_is_seen(struct ctl_table_header *p) 8091f87f0b5SEric W. Biederman { 8101f87f0b5SEric W. Biederman struct ctl_table_set *set = p->set; 8111f87f0b5SEric W. Biederman int res; 8121f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 8131f87f0b5SEric W. Biederman if (p->unregistering) 8141f87f0b5SEric W. Biederman res = 0; 8151f87f0b5SEric W. Biederman else if (!set->is_seen) 8161f87f0b5SEric W. Biederman res = 1; 8171f87f0b5SEric W. Biederman else 8181f87f0b5SEric W. Biederman res = set->is_seen(set); 8191f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 8201f87f0b5SEric W. Biederman return res; 8211f87f0b5SEric W. Biederman } 8221f87f0b5SEric W. Biederman 823621e155aSNick Piggin static int proc_sys_compare(const struct dentry *parent, 824621e155aSNick Piggin const struct inode *pinode, 825621e155aSNick Piggin const struct dentry *dentry, const struct inode *inode, 826621e155aSNick Piggin unsigned int len, const char *str, const struct qstr *name) 8279043476fSAl Viro { 828dfef6dcdSAl Viro struct ctl_table_header *head; 82931e6b01fSNick Piggin /* Although proc doesn't have negative dentries, rcu-walk means 83031e6b01fSNick Piggin * that inode here can be NULL */ 831dfef6dcdSAl Viro /* AV: can it, indeed? */ 83231e6b01fSNick Piggin if (!inode) 833dfef6dcdSAl Viro return 1; 834621e155aSNick Piggin if (name->len != len) 8359043476fSAl Viro return 1; 836621e155aSNick Piggin if (memcmp(name->name, str, len)) 8379043476fSAl Viro return 1; 838dfef6dcdSAl Viro head = rcu_dereference(PROC_I(inode)->sysctl); 839dfef6dcdSAl Viro return !head || !sysctl_is_seen(head); 84077b14db5SEric W. Biederman } 84177b14db5SEric W. Biederman 842d72f71ebSAl Viro static const struct dentry_operations proc_sys_dentry_operations = { 84377b14db5SEric W. Biederman .d_revalidate = proc_sys_revalidate, 8449043476fSAl Viro .d_delete = proc_sys_delete, 8459043476fSAl Viro .d_compare = proc_sys_compare, 84677b14db5SEric W. Biederman }; 84777b14db5SEric W. Biederman 8480e47c99dSEric W. Biederman static struct ctl_dir *find_subdir(struct ctl_dir *dir, 8497ec66d06SEric W. Biederman const char *name, int namelen) 8501f87f0b5SEric W. Biederman { 8517ec66d06SEric W. Biederman struct ctl_table_header *head; 8527ec66d06SEric W. Biederman struct ctl_table *entry; 8531f87f0b5SEric W. Biederman 8540e47c99dSEric W. Biederman entry = find_entry(&head, dir, name, namelen); 8557ec66d06SEric W. Biederman if (!entry) 8567ec66d06SEric W. Biederman return ERR_PTR(-ENOENT); 85751f72f4aSEric W. Biederman if (!S_ISDIR(entry->mode)) 8587ec66d06SEric W. Biederman return ERR_PTR(-ENOTDIR); 85951f72f4aSEric W. Biederman return container_of(head, struct ctl_dir, header); 8601f87f0b5SEric W. Biederman } 8611f87f0b5SEric W. Biederman 8627ec66d06SEric W. Biederman static struct ctl_dir *new_dir(struct ctl_table_set *set, 8637ec66d06SEric W. Biederman const char *name, int namelen) 8641f87f0b5SEric W. Biederman { 8657ec66d06SEric W. Biederman struct ctl_table *table; 8667ec66d06SEric W. Biederman struct ctl_dir *new; 867ac13ac6fSEric W. Biederman struct ctl_node *node; 8687ec66d06SEric W. Biederman char *new_name; 8691f87f0b5SEric W. Biederman 870ac13ac6fSEric W. Biederman new = kzalloc(sizeof(*new) + sizeof(struct ctl_node) + 871ac13ac6fSEric W. Biederman sizeof(struct ctl_table)*2 + namelen + 1, 872ac13ac6fSEric W. Biederman GFP_KERNEL); 8737ec66d06SEric W. Biederman if (!new) 8747ec66d06SEric W. Biederman return NULL; 8757ec66d06SEric W. Biederman 876ac13ac6fSEric W. Biederman node = (struct ctl_node *)(new + 1); 877ac13ac6fSEric W. Biederman table = (struct ctl_table *)(node + 1); 8787ec66d06SEric W. Biederman new_name = (char *)(table + 2); 8797ec66d06SEric W. Biederman memcpy(new_name, name, namelen); 8807ec66d06SEric W. Biederman new_name[namelen] = '\0'; 8817ec66d06SEric W. Biederman table[0].procname = new_name; 8827ec66d06SEric W. Biederman table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO; 883ac13ac6fSEric W. Biederman init_header(&new->header, set->dir.header.root, set, node, table); 8847ec66d06SEric W. Biederman 8857ec66d06SEric W. Biederman return new; 8861f87f0b5SEric W. Biederman } 8871f87f0b5SEric W. Biederman 88860f126d9SEric W. Biederman /** 88960f126d9SEric W. Biederman * get_subdir - find or create a subdir with the specified name. 89060f126d9SEric W. Biederman * @dir: Directory to create the subdirectory in 89160f126d9SEric W. Biederman * @name: The name of the subdirectory to find or create 89260f126d9SEric W. Biederman * @namelen: The length of name 89360f126d9SEric W. Biederman * 89460f126d9SEric W. Biederman * Takes a directory with an elevated reference count so we know that 89560f126d9SEric W. Biederman * if we drop the lock the directory will not go away. Upon success 89660f126d9SEric W. Biederman * the reference is moved from @dir to the returned subdirectory. 89760f126d9SEric W. Biederman * Upon error an error code is returned and the reference on @dir is 89860f126d9SEric W. Biederman * simply dropped. 89960f126d9SEric W. Biederman */ 9000e47c99dSEric W. Biederman static struct ctl_dir *get_subdir(struct ctl_dir *dir, 9010e47c99dSEric W. Biederman const char *name, int namelen) 9027ec66d06SEric W. Biederman { 9030e47c99dSEric W. Biederman struct ctl_table_set *set = dir->header.set; 9047ec66d06SEric W. Biederman struct ctl_dir *subdir, *new = NULL; 9050eb97f38SEric W. Biederman int err; 9067ec66d06SEric W. Biederman 9077ec66d06SEric W. Biederman spin_lock(&sysctl_lock); 9080e47c99dSEric W. Biederman subdir = find_subdir(dir, name, namelen); 9097ec66d06SEric W. Biederman if (!IS_ERR(subdir)) 9107ec66d06SEric W. Biederman goto found; 9117ec66d06SEric W. Biederman if (PTR_ERR(subdir) != -ENOENT) 9127ec66d06SEric W. Biederman goto failed; 9137ec66d06SEric W. Biederman 9147ec66d06SEric W. Biederman spin_unlock(&sysctl_lock); 9157ec66d06SEric W. Biederman new = new_dir(set, name, namelen); 9167ec66d06SEric W. Biederman spin_lock(&sysctl_lock); 9177ec66d06SEric W. Biederman subdir = ERR_PTR(-ENOMEM); 9187ec66d06SEric W. Biederman if (!new) 9197ec66d06SEric W. Biederman goto failed; 9207ec66d06SEric W. Biederman 92160f126d9SEric W. Biederman /* Was the subdir added while we dropped the lock? */ 9220e47c99dSEric W. Biederman subdir = find_subdir(dir, name, namelen); 9237ec66d06SEric W. Biederman if (!IS_ERR(subdir)) 9247ec66d06SEric W. Biederman goto found; 9257ec66d06SEric W. Biederman if (PTR_ERR(subdir) != -ENOENT) 9267ec66d06SEric W. Biederman goto failed; 9277ec66d06SEric W. Biederman 92860f126d9SEric W. Biederman /* Nope. Use the our freshly made directory entry. */ 9290eb97f38SEric W. Biederman err = insert_header(dir, &new->header); 9300eb97f38SEric W. Biederman subdir = ERR_PTR(err); 9310eb97f38SEric W. Biederman if (err) 9320e47c99dSEric W. Biederman goto failed; 9337ec66d06SEric W. Biederman subdir = new; 9347ec66d06SEric W. Biederman found: 9357ec66d06SEric W. Biederman subdir->header.nreg++; 9367ec66d06SEric W. Biederman failed: 9377ec66d06SEric W. Biederman if (unlikely(IS_ERR(subdir))) { 9386980128fSEric W. Biederman printk(KERN_ERR "sysctl could not get directory: "); 9396980128fSEric W. Biederman sysctl_print_dir(dir); 9406980128fSEric W. Biederman printk(KERN_CONT "/%*.*s %ld\n", 9417ec66d06SEric W. Biederman namelen, namelen, name, PTR_ERR(subdir)); 9421f87f0b5SEric W. Biederman } 9437ec66d06SEric W. Biederman drop_sysctl_table(&dir->header); 9447ec66d06SEric W. Biederman if (new) 9457ec66d06SEric W. Biederman drop_sysctl_table(&new->header); 9467ec66d06SEric W. Biederman spin_unlock(&sysctl_lock); 9477ec66d06SEric W. Biederman return subdir; 9481f87f0b5SEric W. Biederman } 9491f87f0b5SEric W. Biederman 9500e47c99dSEric W. Biederman static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir) 9510e47c99dSEric W. Biederman { 9520e47c99dSEric W. Biederman struct ctl_dir *parent; 9530e47c99dSEric W. Biederman const char *procname; 9540e47c99dSEric W. Biederman if (!dir->header.parent) 9550e47c99dSEric W. Biederman return &set->dir; 9560e47c99dSEric W. Biederman parent = xlate_dir(set, dir->header.parent); 9570e47c99dSEric W. Biederman if (IS_ERR(parent)) 9580e47c99dSEric W. Biederman return parent; 9590e47c99dSEric W. Biederman procname = dir->header.ctl_table[0].procname; 9600e47c99dSEric W. Biederman return find_subdir(parent, procname, strlen(procname)); 9610e47c99dSEric W. Biederman } 9620e47c99dSEric W. Biederman 9630e47c99dSEric W. Biederman static int sysctl_follow_link(struct ctl_table_header **phead, 9640e47c99dSEric W. Biederman struct ctl_table **pentry, struct nsproxy *namespaces) 9650e47c99dSEric W. Biederman { 9660e47c99dSEric W. Biederman struct ctl_table_header *head; 9670e47c99dSEric W. Biederman struct ctl_table_root *root; 9680e47c99dSEric W. Biederman struct ctl_table_set *set; 9690e47c99dSEric W. Biederman struct ctl_table *entry; 9700e47c99dSEric W. Biederman struct ctl_dir *dir; 9710e47c99dSEric W. Biederman int ret; 9720e47c99dSEric W. Biederman 9730e47c99dSEric W. Biederman ret = 0; 9740e47c99dSEric W. Biederman spin_lock(&sysctl_lock); 9750e47c99dSEric W. Biederman root = (*pentry)->data; 9760e47c99dSEric W. Biederman set = lookup_header_set(root, namespaces); 9770e47c99dSEric W. Biederman dir = xlate_dir(set, (*phead)->parent); 9780e47c99dSEric W. Biederman if (IS_ERR(dir)) 9790e47c99dSEric W. Biederman ret = PTR_ERR(dir); 9800e47c99dSEric W. Biederman else { 9810e47c99dSEric W. Biederman const char *procname = (*pentry)->procname; 9820e47c99dSEric W. Biederman head = NULL; 9830e47c99dSEric W. Biederman entry = find_entry(&head, dir, procname, strlen(procname)); 9840e47c99dSEric W. Biederman ret = -ENOENT; 9850e47c99dSEric W. Biederman if (entry && use_table(head)) { 9860e47c99dSEric W. Biederman unuse_table(*phead); 9870e47c99dSEric W. Biederman *phead = head; 9880e47c99dSEric W. Biederman *pentry = entry; 9890e47c99dSEric W. Biederman ret = 0; 9900e47c99dSEric W. Biederman } 9910e47c99dSEric W. Biederman } 9920e47c99dSEric W. Biederman 9930e47c99dSEric W. Biederman spin_unlock(&sysctl_lock); 9940e47c99dSEric W. Biederman return ret; 9950e47c99dSEric W. Biederman } 9960e47c99dSEric W. Biederman 9977c60c48fSEric W. Biederman static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...) 9987c60c48fSEric W. Biederman { 9997c60c48fSEric W. Biederman struct va_format vaf; 10007c60c48fSEric W. Biederman va_list args; 10017c60c48fSEric W. Biederman 10027c60c48fSEric W. Biederman va_start(args, fmt); 10037c60c48fSEric W. Biederman vaf.fmt = fmt; 10047c60c48fSEric W. Biederman vaf.va = &args; 10057c60c48fSEric W. Biederman 10067c60c48fSEric W. Biederman printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n", 10077c60c48fSEric W. Biederman path, table->procname, &vaf); 10087c60c48fSEric W. Biederman 10097c60c48fSEric W. Biederman va_end(args); 10107c60c48fSEric W. Biederman return -EINVAL; 10117c60c48fSEric W. Biederman } 10127c60c48fSEric W. Biederman 10137c60c48fSEric W. Biederman static int sysctl_check_table(const char *path, struct ctl_table *table) 10147c60c48fSEric W. Biederman { 10157c60c48fSEric W. Biederman int err = 0; 10167c60c48fSEric W. Biederman for (; table->procname; table++) { 10177c60c48fSEric W. Biederman if (table->child) 10187c60c48fSEric W. Biederman err = sysctl_err(path, table, "Not a file"); 10197c60c48fSEric W. Biederman 10201f87f0b5SEric W. Biederman if ((table->proc_handler == proc_dostring) || 10211f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec) || 10221f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec_minmax) || 10231f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec_jiffies) || 10241f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec_userhz_jiffies) || 10251f87f0b5SEric W. Biederman (table->proc_handler == proc_dointvec_ms_jiffies) || 10261f87f0b5SEric W. Biederman (table->proc_handler == proc_doulongvec_minmax) || 10271f87f0b5SEric W. Biederman (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) { 10281f87f0b5SEric W. Biederman if (!table->data) 10297c60c48fSEric W. Biederman err = sysctl_err(path, table, "No data"); 10301f87f0b5SEric W. Biederman if (!table->maxlen) 10317c60c48fSEric W. Biederman err = sysctl_err(path, table, "No maxlen"); 10321f87f0b5SEric W. Biederman } 10331f87f0b5SEric W. Biederman if (!table->proc_handler) 10347c60c48fSEric W. Biederman err = sysctl_err(path, table, "No proc_handler"); 10357c60c48fSEric W. Biederman 10367c60c48fSEric W. Biederman if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode) 10377c60c48fSEric W. Biederman err = sysctl_err(path, table, "bogus .mode 0%o", 10387c60c48fSEric W. Biederman table->mode); 10391f87f0b5SEric W. Biederman } 10407c60c48fSEric W. Biederman return err; 10411f87f0b5SEric W. Biederman } 10421f87f0b5SEric W. Biederman 10430e47c99dSEric W. Biederman static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table *table, 10440e47c99dSEric W. Biederman struct ctl_table_root *link_root) 10450e47c99dSEric W. Biederman { 10460e47c99dSEric W. Biederman struct ctl_table *link_table, *entry, *link; 10470e47c99dSEric W. Biederman struct ctl_table_header *links; 1048ac13ac6fSEric W. Biederman struct ctl_node *node; 10490e47c99dSEric W. Biederman char *link_name; 10500e47c99dSEric W. Biederman int nr_entries, name_bytes; 10510e47c99dSEric W. Biederman 10520e47c99dSEric W. Biederman name_bytes = 0; 10530e47c99dSEric W. Biederman nr_entries = 0; 10540e47c99dSEric W. Biederman for (entry = table; entry->procname; entry++) { 10550e47c99dSEric W. Biederman nr_entries++; 10560e47c99dSEric W. Biederman name_bytes += strlen(entry->procname) + 1; 10570e47c99dSEric W. Biederman } 10580e47c99dSEric W. Biederman 10590e47c99dSEric W. Biederman links = kzalloc(sizeof(struct ctl_table_header) + 1060ac13ac6fSEric W. Biederman sizeof(struct ctl_node)*nr_entries + 10610e47c99dSEric W. Biederman sizeof(struct ctl_table)*(nr_entries + 1) + 10620e47c99dSEric W. Biederman name_bytes, 10630e47c99dSEric W. Biederman GFP_KERNEL); 10640e47c99dSEric W. Biederman 10650e47c99dSEric W. Biederman if (!links) 10660e47c99dSEric W. Biederman return NULL; 10670e47c99dSEric W. Biederman 1068ac13ac6fSEric W. Biederman node = (struct ctl_node *)(links + 1); 1069ac13ac6fSEric W. Biederman link_table = (struct ctl_table *)(node + nr_entries); 10700e47c99dSEric W. Biederman link_name = (char *)&link_table[nr_entries + 1]; 10710e47c99dSEric W. Biederman 10720e47c99dSEric W. Biederman for (link = link_table, entry = table; entry->procname; link++, entry++) { 10730e47c99dSEric W. Biederman int len = strlen(entry->procname) + 1; 10740e47c99dSEric W. Biederman memcpy(link_name, entry->procname, len); 10750e47c99dSEric W. Biederman link->procname = link_name; 10760e47c99dSEric W. Biederman link->mode = S_IFLNK|S_IRWXUGO; 10770e47c99dSEric W. Biederman link->data = link_root; 10780e47c99dSEric W. Biederman link_name += len; 10790e47c99dSEric W. Biederman } 1080ac13ac6fSEric W. Biederman init_header(links, dir->header.root, dir->header.set, node, link_table); 10810e47c99dSEric W. Biederman links->nreg = nr_entries; 10820e47c99dSEric W. Biederman 10830e47c99dSEric W. Biederman return links; 10840e47c99dSEric W. Biederman } 10850e47c99dSEric W. Biederman 10860e47c99dSEric W. Biederman static bool get_links(struct ctl_dir *dir, 10870e47c99dSEric W. Biederman struct ctl_table *table, struct ctl_table_root *link_root) 10880e47c99dSEric W. Biederman { 10890e47c99dSEric W. Biederman struct ctl_table_header *head; 10900e47c99dSEric W. Biederman struct ctl_table *entry, *link; 10910e47c99dSEric W. Biederman 10920e47c99dSEric W. Biederman /* Are there links available for every entry in table? */ 10930e47c99dSEric W. Biederman for (entry = table; entry->procname; entry++) { 10940e47c99dSEric W. Biederman const char *procname = entry->procname; 10950e47c99dSEric W. Biederman link = find_entry(&head, dir, procname, strlen(procname)); 10960e47c99dSEric W. Biederman if (!link) 10970e47c99dSEric W. Biederman return false; 10980e47c99dSEric W. Biederman if (S_ISDIR(link->mode) && S_ISDIR(entry->mode)) 10990e47c99dSEric W. Biederman continue; 11000e47c99dSEric W. Biederman if (S_ISLNK(link->mode) && (link->data == link_root)) 11010e47c99dSEric W. Biederman continue; 11020e47c99dSEric W. Biederman return false; 11030e47c99dSEric W. Biederman } 11040e47c99dSEric W. Biederman 11050e47c99dSEric W. Biederman /* The checks passed. Increase the registration count on the links */ 11060e47c99dSEric W. Biederman for (entry = table; entry->procname; entry++) { 11070e47c99dSEric W. Biederman const char *procname = entry->procname; 11080e47c99dSEric W. Biederman link = find_entry(&head, dir, procname, strlen(procname)); 11090e47c99dSEric W. Biederman head->nreg++; 11100e47c99dSEric W. Biederman } 11110e47c99dSEric W. Biederman return true; 11120e47c99dSEric W. Biederman } 11130e47c99dSEric W. Biederman 11140e47c99dSEric W. Biederman static int insert_links(struct ctl_table_header *head) 11150e47c99dSEric W. Biederman { 11160e47c99dSEric W. Biederman struct ctl_table_set *root_set = &sysctl_table_root.default_set; 11170e47c99dSEric W. Biederman struct ctl_dir *core_parent = NULL; 11180e47c99dSEric W. Biederman struct ctl_table_header *links; 11190e47c99dSEric W. Biederman int err; 11200e47c99dSEric W. Biederman 11210e47c99dSEric W. Biederman if (head->set == root_set) 11220e47c99dSEric W. Biederman return 0; 11230e47c99dSEric W. Biederman 11240e47c99dSEric W. Biederman core_parent = xlate_dir(root_set, head->parent); 11250e47c99dSEric W. Biederman if (IS_ERR(core_parent)) 11260e47c99dSEric W. Biederman return 0; 11270e47c99dSEric W. Biederman 11280e47c99dSEric W. Biederman if (get_links(core_parent, head->ctl_table, head->root)) 11290e47c99dSEric W. Biederman return 0; 11300e47c99dSEric W. Biederman 11310e47c99dSEric W. Biederman core_parent->header.nreg++; 11320e47c99dSEric W. Biederman spin_unlock(&sysctl_lock); 11330e47c99dSEric W. Biederman 11340e47c99dSEric W. Biederman links = new_links(core_parent, head->ctl_table, head->root); 11350e47c99dSEric W. Biederman 11360e47c99dSEric W. Biederman spin_lock(&sysctl_lock); 11370e47c99dSEric W. Biederman err = -ENOMEM; 11380e47c99dSEric W. Biederman if (!links) 11390e47c99dSEric W. Biederman goto out; 11400e47c99dSEric W. Biederman 11410e47c99dSEric W. Biederman err = 0; 11420e47c99dSEric W. Biederman if (get_links(core_parent, head->ctl_table, head->root)) { 11430e47c99dSEric W. Biederman kfree(links); 11440e47c99dSEric W. Biederman goto out; 11450e47c99dSEric W. Biederman } 11460e47c99dSEric W. Biederman 11470e47c99dSEric W. Biederman err = insert_header(core_parent, links); 11480e47c99dSEric W. Biederman if (err) 11490e47c99dSEric W. Biederman kfree(links); 11500e47c99dSEric W. Biederman out: 11510e47c99dSEric W. Biederman drop_sysctl_table(&core_parent->header); 11520e47c99dSEric W. Biederman return err; 11530e47c99dSEric W. Biederman } 11540e47c99dSEric W. Biederman 11551f87f0b5SEric W. Biederman /** 1156f728019bSEric W. Biederman * __register_sysctl_table - register a leaf sysctl table 115760a47a2eSEric W. Biederman * @set: Sysctl tree to register on 11581f87f0b5SEric W. Biederman * @path: The path to the directory the sysctl table is in. 11591f87f0b5SEric W. Biederman * @table: the top-level table structure 11601f87f0b5SEric W. Biederman * 11611f87f0b5SEric W. Biederman * Register a sysctl table hierarchy. @table should be a filled in ctl_table 11621f87f0b5SEric W. Biederman * array. A completely 0 filled entry terminates the table. 11631f87f0b5SEric W. Biederman * 11641f87f0b5SEric W. Biederman * The members of the &struct ctl_table structure are used as follows: 11651f87f0b5SEric W. Biederman * 11661f87f0b5SEric W. Biederman * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not 11671f87f0b5SEric W. Biederman * enter a sysctl file 11681f87f0b5SEric W. Biederman * 11691f87f0b5SEric W. Biederman * data - a pointer to data for use by proc_handler 11701f87f0b5SEric W. Biederman * 11711f87f0b5SEric W. Biederman * maxlen - the maximum size in bytes of the data 11721f87f0b5SEric W. Biederman * 1173f728019bSEric W. Biederman * mode - the file permissions for the /proc/sys file 11741f87f0b5SEric W. Biederman * 1175f728019bSEric W. Biederman * child - must be %NULL. 11761f87f0b5SEric W. Biederman * 11771f87f0b5SEric W. Biederman * proc_handler - the text handler routine (described below) 11781f87f0b5SEric W. Biederman * 11791f87f0b5SEric W. Biederman * extra1, extra2 - extra pointers usable by the proc handler routines 11801f87f0b5SEric W. Biederman * 11811f87f0b5SEric W. Biederman * Leaf nodes in the sysctl tree will be represented by a single file 11821f87f0b5SEric W. Biederman * under /proc; non-leaf nodes will be represented by directories. 11831f87f0b5SEric W. Biederman * 1184f728019bSEric W. Biederman * There must be a proc_handler routine for any terminal nodes. 1185f728019bSEric W. Biederman * Several default handlers are available to cover common cases - 11861f87f0b5SEric W. Biederman * 11871f87f0b5SEric W. Biederman * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(), 11881f87f0b5SEric W. Biederman * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(), 11891f87f0b5SEric W. Biederman * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax() 11901f87f0b5SEric W. Biederman * 11911f87f0b5SEric W. Biederman * It is the handler's job to read the input buffer from user memory 11921f87f0b5SEric W. Biederman * and process it. The handler should return 0 on success. 11931f87f0b5SEric W. Biederman * 11941f87f0b5SEric W. Biederman * This routine returns %NULL on a failure to register, and a pointer 11951f87f0b5SEric W. Biederman * to the table header on success. 11961f87f0b5SEric W. Biederman */ 11976e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_table( 119860a47a2eSEric W. Biederman struct ctl_table_set *set, 11996e9d5164SEric W. Biederman const char *path, struct ctl_table *table) 12001f87f0b5SEric W. Biederman { 120160a47a2eSEric W. Biederman struct ctl_table_root *root = set->dir.header.root; 12021f87f0b5SEric W. Biederman struct ctl_table_header *header; 12036e9d5164SEric W. Biederman const char *name, *nextname; 12047ec66d06SEric W. Biederman struct ctl_dir *dir; 1205ac13ac6fSEric W. Biederman struct ctl_table *entry; 1206ac13ac6fSEric W. Biederman struct ctl_node *node; 1207ac13ac6fSEric W. Biederman int nr_entries = 0; 12081f87f0b5SEric W. Biederman 1209ac13ac6fSEric W. Biederman for (entry = table; entry->procname; entry++) 1210ac13ac6fSEric W. Biederman nr_entries++; 1211ac13ac6fSEric W. Biederman 1212ac13ac6fSEric W. Biederman header = kzalloc(sizeof(struct ctl_table_header) + 1213ac13ac6fSEric W. Biederman sizeof(struct ctl_node)*nr_entries, GFP_KERNEL); 12141f87f0b5SEric W. Biederman if (!header) 12151f87f0b5SEric W. Biederman return NULL; 12161f87f0b5SEric W. Biederman 1217ac13ac6fSEric W. Biederman node = (struct ctl_node *)(header + 1); 1218ac13ac6fSEric W. Biederman init_header(header, root, set, node, table); 12197c60c48fSEric W. Biederman if (sysctl_check_table(path, table)) 12207c60c48fSEric W. Biederman goto fail; 12218d6ecfccSEric W. Biederman 12221f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 12230e47c99dSEric W. Biederman dir = &set->dir; 122460f126d9SEric W. Biederman /* Reference moved down the diretory tree get_subdir */ 12257ec66d06SEric W. Biederman dir->header.nreg++; 12267ec66d06SEric W. Biederman spin_unlock(&sysctl_lock); 12277ec66d06SEric W. Biederman 12287ec66d06SEric W. Biederman /* Find the directory for the ctl_table */ 12297ec66d06SEric W. Biederman for (name = path; name; name = nextname) { 12307ec66d06SEric W. Biederman int namelen; 12317ec66d06SEric W. Biederman nextname = strchr(name, '/'); 12327ec66d06SEric W. Biederman if (nextname) { 12337ec66d06SEric W. Biederman namelen = nextname - name; 12347ec66d06SEric W. Biederman nextname++; 12357ec66d06SEric W. Biederman } else { 12367ec66d06SEric W. Biederman namelen = strlen(name); 12377ec66d06SEric W. Biederman } 12387ec66d06SEric W. Biederman if (namelen == 0) 12391f87f0b5SEric W. Biederman continue; 12407ec66d06SEric W. Biederman 12410e47c99dSEric W. Biederman dir = get_subdir(dir, name, namelen); 12427ec66d06SEric W. Biederman if (IS_ERR(dir)) 12437ec66d06SEric W. Biederman goto fail; 12441f87f0b5SEric W. Biederman } 12450e47c99dSEric W. Biederman 12467ec66d06SEric W. Biederman spin_lock(&sysctl_lock); 12470e47c99dSEric W. Biederman if (insert_header(dir, header)) 12480e47c99dSEric W. Biederman goto fail_put_dir_locked; 12490e47c99dSEric W. Biederman 12507ec66d06SEric W. Biederman drop_sysctl_table(&dir->header); 12511f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 12521f87f0b5SEric W. Biederman 12531f87f0b5SEric W. Biederman return header; 12540e47c99dSEric W. Biederman 12557ec66d06SEric W. Biederman fail_put_dir_locked: 12567ec66d06SEric W. Biederman drop_sysctl_table(&dir->header); 12577c60c48fSEric W. Biederman spin_unlock(&sysctl_lock); 12587c60c48fSEric W. Biederman fail: 12597c60c48fSEric W. Biederman kfree(header); 12607c60c48fSEric W. Biederman dump_stack(); 12617c60c48fSEric W. Biederman return NULL; 12621f87f0b5SEric W. Biederman } 12631f87f0b5SEric W. Biederman 1264fea478d4SEric W. Biederman /** 1265fea478d4SEric W. Biederman * register_sysctl - register a sysctl table 1266fea478d4SEric W. Biederman * @path: The path to the directory the sysctl table is in. 1267fea478d4SEric W. Biederman * @table: the table structure 1268fea478d4SEric W. Biederman * 1269fea478d4SEric W. Biederman * Register a sysctl table. @table should be a filled in ctl_table 1270fea478d4SEric W. Biederman * array. A completely 0 filled entry terminates the table. 1271fea478d4SEric W. Biederman * 1272fea478d4SEric W. Biederman * See __register_sysctl_table for more details. 1273fea478d4SEric W. Biederman */ 1274fea478d4SEric W. Biederman struct ctl_table_header *register_sysctl(const char *path, struct ctl_table *table) 1275fea478d4SEric W. Biederman { 1276fea478d4SEric W. Biederman return __register_sysctl_table(&sysctl_table_root.default_set, 1277fea478d4SEric W. Biederman path, table); 1278fea478d4SEric W. Biederman } 1279fea478d4SEric W. Biederman EXPORT_SYMBOL(register_sysctl); 1280fea478d4SEric W. Biederman 12816e9d5164SEric W. Biederman static char *append_path(const char *path, char *pos, const char *name) 12826e9d5164SEric W. Biederman { 12836e9d5164SEric W. Biederman int namelen; 12846e9d5164SEric W. Biederman namelen = strlen(name); 12856e9d5164SEric W. Biederman if (((pos - path) + namelen + 2) >= PATH_MAX) 12866e9d5164SEric W. Biederman return NULL; 12876e9d5164SEric W. Biederman memcpy(pos, name, namelen); 12886e9d5164SEric W. Biederman pos[namelen] = '/'; 12896e9d5164SEric W. Biederman pos[namelen + 1] = '\0'; 12906e9d5164SEric W. Biederman pos += namelen + 1; 12916e9d5164SEric W. Biederman return pos; 12926e9d5164SEric W. Biederman } 12936e9d5164SEric W. Biederman 1294f728019bSEric W. Biederman static int count_subheaders(struct ctl_table *table) 1295f728019bSEric W. Biederman { 1296f728019bSEric W. Biederman int has_files = 0; 1297f728019bSEric W. Biederman int nr_subheaders = 0; 1298f728019bSEric W. Biederman struct ctl_table *entry; 1299f728019bSEric W. Biederman 1300f728019bSEric W. Biederman /* special case: no directory and empty directory */ 1301f728019bSEric W. Biederman if (!table || !table->procname) 1302f728019bSEric W. Biederman return 1; 1303f728019bSEric W. Biederman 1304f728019bSEric W. Biederman for (entry = table; entry->procname; entry++) { 1305f728019bSEric W. Biederman if (entry->child) 1306f728019bSEric W. Biederman nr_subheaders += count_subheaders(entry->child); 1307f728019bSEric W. Biederman else 1308f728019bSEric W. Biederman has_files = 1; 1309f728019bSEric W. Biederman } 1310f728019bSEric W. Biederman return nr_subheaders + has_files; 1311f728019bSEric W. Biederman } 1312f728019bSEric W. Biederman 1313f728019bSEric W. Biederman static int register_leaf_sysctl_tables(const char *path, char *pos, 131460a47a2eSEric W. Biederman struct ctl_table_header ***subheader, struct ctl_table_set *set, 1315f728019bSEric W. Biederman struct ctl_table *table) 1316f728019bSEric W. Biederman { 1317f728019bSEric W. Biederman struct ctl_table *ctl_table_arg = NULL; 1318f728019bSEric W. Biederman struct ctl_table *entry, *files; 1319f728019bSEric W. Biederman int nr_files = 0; 1320f728019bSEric W. Biederman int nr_dirs = 0; 1321f728019bSEric W. Biederman int err = -ENOMEM; 1322f728019bSEric W. Biederman 1323f728019bSEric W. Biederman for (entry = table; entry->procname; entry++) { 1324f728019bSEric W. Biederman if (entry->child) 1325f728019bSEric W. Biederman nr_dirs++; 1326f728019bSEric W. Biederman else 1327f728019bSEric W. Biederman nr_files++; 1328f728019bSEric W. Biederman } 1329f728019bSEric W. Biederman 1330f728019bSEric W. Biederman files = table; 1331f728019bSEric W. Biederman /* If there are mixed files and directories we need a new table */ 1332f728019bSEric W. Biederman if (nr_dirs && nr_files) { 1333f728019bSEric W. Biederman struct ctl_table *new; 1334f728019bSEric W. Biederman files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1), 1335f728019bSEric W. Biederman GFP_KERNEL); 1336f728019bSEric W. Biederman if (!files) 1337f728019bSEric W. Biederman goto out; 1338f728019bSEric W. Biederman 1339f728019bSEric W. Biederman ctl_table_arg = files; 1340f728019bSEric W. Biederman for (new = files, entry = table; entry->procname; entry++) { 1341f728019bSEric W. Biederman if (entry->child) 1342f728019bSEric W. Biederman continue; 1343f728019bSEric W. Biederman *new = *entry; 1344f728019bSEric W. Biederman new++; 1345f728019bSEric W. Biederman } 1346f728019bSEric W. Biederman } 1347f728019bSEric W. Biederman 1348f728019bSEric W. Biederman /* Register everything except a directory full of subdirectories */ 1349f728019bSEric W. Biederman if (nr_files || !nr_dirs) { 1350f728019bSEric W. Biederman struct ctl_table_header *header; 135160a47a2eSEric W. Biederman header = __register_sysctl_table(set, path, files); 1352f728019bSEric W. Biederman if (!header) { 1353f728019bSEric W. Biederman kfree(ctl_table_arg); 1354f728019bSEric W. Biederman goto out; 1355f728019bSEric W. Biederman } 1356f728019bSEric W. Biederman 1357f728019bSEric W. Biederman /* Remember if we need to free the file table */ 1358f728019bSEric W. Biederman header->ctl_table_arg = ctl_table_arg; 1359f728019bSEric W. Biederman **subheader = header; 1360f728019bSEric W. Biederman (*subheader)++; 1361f728019bSEric W. Biederman } 1362f728019bSEric W. Biederman 1363f728019bSEric W. Biederman /* Recurse into the subdirectories. */ 1364f728019bSEric W. Biederman for (entry = table; entry->procname; entry++) { 1365f728019bSEric W. Biederman char *child_pos; 1366f728019bSEric W. Biederman 1367f728019bSEric W. Biederman if (!entry->child) 1368f728019bSEric W. Biederman continue; 1369f728019bSEric W. Biederman 1370f728019bSEric W. Biederman err = -ENAMETOOLONG; 1371f728019bSEric W. Biederman child_pos = append_path(path, pos, entry->procname); 1372f728019bSEric W. Biederman if (!child_pos) 1373f728019bSEric W. Biederman goto out; 1374f728019bSEric W. Biederman 1375f728019bSEric W. Biederman err = register_leaf_sysctl_tables(path, child_pos, subheader, 137660a47a2eSEric W. Biederman set, entry->child); 1377f728019bSEric W. Biederman pos[0] = '\0'; 1378f728019bSEric W. Biederman if (err) 1379f728019bSEric W. Biederman goto out; 1380f728019bSEric W. Biederman } 1381f728019bSEric W. Biederman err = 0; 1382f728019bSEric W. Biederman out: 1383f728019bSEric W. Biederman /* On failure our caller will unregister all registered subheaders */ 1384f728019bSEric W. Biederman return err; 1385f728019bSEric W. Biederman } 1386f728019bSEric W. Biederman 13876e9d5164SEric W. Biederman /** 13886e9d5164SEric W. Biederman * __register_sysctl_paths - register a sysctl table hierarchy 138960a47a2eSEric W. Biederman * @set: Sysctl tree to register on 13906e9d5164SEric W. Biederman * @path: The path to the directory the sysctl table is in. 13916e9d5164SEric W. Biederman * @table: the top-level table structure 13926e9d5164SEric W. Biederman * 13936e9d5164SEric W. Biederman * Register a sysctl table hierarchy. @table should be a filled in ctl_table 13946e9d5164SEric W. Biederman * array. A completely 0 filled entry terminates the table. 13956e9d5164SEric W. Biederman * 13966e9d5164SEric W. Biederman * See __register_sysctl_table for more details. 13976e9d5164SEric W. Biederman */ 13986e9d5164SEric W. Biederman struct ctl_table_header *__register_sysctl_paths( 139960a47a2eSEric W. Biederman struct ctl_table_set *set, 14006e9d5164SEric W. Biederman const struct ctl_path *path, struct ctl_table *table) 14016e9d5164SEric W. Biederman { 1402ec6a5266SEric W. Biederman struct ctl_table *ctl_table_arg = table; 1403f728019bSEric W. Biederman int nr_subheaders = count_subheaders(table); 1404f728019bSEric W. Biederman struct ctl_table_header *header = NULL, **subheaders, **subheader; 14056e9d5164SEric W. Biederman const struct ctl_path *component; 14066e9d5164SEric W. Biederman char *new_path, *pos; 14076e9d5164SEric W. Biederman 14086e9d5164SEric W. Biederman pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL); 14096e9d5164SEric W. Biederman if (!new_path) 14106e9d5164SEric W. Biederman return NULL; 14116e9d5164SEric W. Biederman 14126e9d5164SEric W. Biederman pos[0] = '\0'; 14136e9d5164SEric W. Biederman for (component = path; component->procname; component++) { 14146e9d5164SEric W. Biederman pos = append_path(new_path, pos, component->procname); 14156e9d5164SEric W. Biederman if (!pos) 14166e9d5164SEric W. Biederman goto out; 14176e9d5164SEric W. Biederman } 1418ec6a5266SEric W. Biederman while (table->procname && table->child && !table[1].procname) { 1419ec6a5266SEric W. Biederman pos = append_path(new_path, pos, table->procname); 1420ec6a5266SEric W. Biederman if (!pos) 1421ec6a5266SEric W. Biederman goto out; 1422ec6a5266SEric W. Biederman table = table->child; 1423ec6a5266SEric W. Biederman } 1424f728019bSEric W. Biederman if (nr_subheaders == 1) { 142560a47a2eSEric W. Biederman header = __register_sysctl_table(set, new_path, table); 1426ec6a5266SEric W. Biederman if (header) 1427ec6a5266SEric W. Biederman header->ctl_table_arg = ctl_table_arg; 1428f728019bSEric W. Biederman } else { 1429f728019bSEric W. Biederman header = kzalloc(sizeof(*header) + 1430f728019bSEric W. Biederman sizeof(*subheaders)*nr_subheaders, GFP_KERNEL); 1431f728019bSEric W. Biederman if (!header) 1432f728019bSEric W. Biederman goto out; 1433f728019bSEric W. Biederman 1434f728019bSEric W. Biederman subheaders = (struct ctl_table_header **) (header + 1); 1435f728019bSEric W. Biederman subheader = subheaders; 1436f728019bSEric W. Biederman header->ctl_table_arg = ctl_table_arg; 1437f728019bSEric W. Biederman 1438f728019bSEric W. Biederman if (register_leaf_sysctl_tables(new_path, pos, &subheader, 143960a47a2eSEric W. Biederman set, table)) 1440f728019bSEric W. Biederman goto err_register_leaves; 1441f728019bSEric W. Biederman } 1442f728019bSEric W. Biederman 14436e9d5164SEric W. Biederman out: 14446e9d5164SEric W. Biederman kfree(new_path); 14456e9d5164SEric W. Biederman return header; 1446f728019bSEric W. Biederman 1447f728019bSEric W. Biederman err_register_leaves: 1448f728019bSEric W. Biederman while (subheader > subheaders) { 1449f728019bSEric W. Biederman struct ctl_table_header *subh = *(--subheader); 1450f728019bSEric W. Biederman struct ctl_table *table = subh->ctl_table_arg; 1451f728019bSEric W. Biederman unregister_sysctl_table(subh); 1452f728019bSEric W. Biederman kfree(table); 1453f728019bSEric W. Biederman } 1454f728019bSEric W. Biederman kfree(header); 1455f728019bSEric W. Biederman header = NULL; 1456f728019bSEric W. Biederman goto out; 14576e9d5164SEric W. Biederman } 14586e9d5164SEric W. Biederman 14591f87f0b5SEric W. Biederman /** 14601f87f0b5SEric W. Biederman * register_sysctl_table_path - register a sysctl table hierarchy 14611f87f0b5SEric W. Biederman * @path: The path to the directory the sysctl table is in. 14621f87f0b5SEric W. Biederman * @table: the top-level table structure 14631f87f0b5SEric W. Biederman * 14641f87f0b5SEric W. Biederman * Register a sysctl table hierarchy. @table should be a filled in ctl_table 14651f87f0b5SEric W. Biederman * array. A completely 0 filled entry terminates the table. 14661f87f0b5SEric W. Biederman * 14671f87f0b5SEric W. Biederman * See __register_sysctl_paths for more details. 14681f87f0b5SEric W. Biederman */ 14691f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path, 14701f87f0b5SEric W. Biederman struct ctl_table *table) 14711f87f0b5SEric W. Biederman { 147260a47a2eSEric W. Biederman return __register_sysctl_paths(&sysctl_table_root.default_set, 14731f87f0b5SEric W. Biederman path, table); 14741f87f0b5SEric W. Biederman } 14751f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_paths); 14761f87f0b5SEric W. Biederman 14771f87f0b5SEric W. Biederman /** 14781f87f0b5SEric W. Biederman * register_sysctl_table - register a sysctl table hierarchy 14791f87f0b5SEric W. Biederman * @table: the top-level table structure 14801f87f0b5SEric W. Biederman * 14811f87f0b5SEric W. Biederman * Register a sysctl table hierarchy. @table should be a filled in ctl_table 14821f87f0b5SEric W. Biederman * array. A completely 0 filled entry terminates the table. 14831f87f0b5SEric W. Biederman * 14841f87f0b5SEric W. Biederman * See register_sysctl_paths for more details. 14851f87f0b5SEric W. Biederman */ 14861f87f0b5SEric W. Biederman struct ctl_table_header *register_sysctl_table(struct ctl_table *table) 14871f87f0b5SEric W. Biederman { 14881f87f0b5SEric W. Biederman static const struct ctl_path null_path[] = { {} }; 14891f87f0b5SEric W. Biederman 14901f87f0b5SEric W. Biederman return register_sysctl_paths(null_path, table); 14911f87f0b5SEric W. Biederman } 14921f87f0b5SEric W. Biederman EXPORT_SYMBOL(register_sysctl_table); 14931f87f0b5SEric W. Biederman 14940e47c99dSEric W. Biederman static void put_links(struct ctl_table_header *header) 14950e47c99dSEric W. Biederman { 14960e47c99dSEric W. Biederman struct ctl_table_set *root_set = &sysctl_table_root.default_set; 14970e47c99dSEric W. Biederman struct ctl_table_root *root = header->root; 14980e47c99dSEric W. Biederman struct ctl_dir *parent = header->parent; 14990e47c99dSEric W. Biederman struct ctl_dir *core_parent; 15000e47c99dSEric W. Biederman struct ctl_table *entry; 15010e47c99dSEric W. Biederman 15020e47c99dSEric W. Biederman if (header->set == root_set) 15030e47c99dSEric W. Biederman return; 15040e47c99dSEric W. Biederman 15050e47c99dSEric W. Biederman core_parent = xlate_dir(root_set, parent); 15060e47c99dSEric W. Biederman if (IS_ERR(core_parent)) 15070e47c99dSEric W. Biederman return; 15080e47c99dSEric W. Biederman 15090e47c99dSEric W. Biederman for (entry = header->ctl_table; entry->procname; entry++) { 15100e47c99dSEric W. Biederman struct ctl_table_header *link_head; 15110e47c99dSEric W. Biederman struct ctl_table *link; 15120e47c99dSEric W. Biederman const char *name = entry->procname; 15130e47c99dSEric W. Biederman 15140e47c99dSEric W. Biederman link = find_entry(&link_head, core_parent, name, strlen(name)); 15150e47c99dSEric W. Biederman if (link && 15160e47c99dSEric W. Biederman ((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) || 15170e47c99dSEric W. Biederman (S_ISLNK(link->mode) && (link->data == root)))) { 15180e47c99dSEric W. Biederman drop_sysctl_table(link_head); 15190e47c99dSEric W. Biederman } 15200e47c99dSEric W. Biederman else { 15210e47c99dSEric W. Biederman printk(KERN_ERR "sysctl link missing during unregister: "); 15220e47c99dSEric W. Biederman sysctl_print_dir(parent); 15230e47c99dSEric W. Biederman printk(KERN_CONT "/%s\n", name); 15240e47c99dSEric W. Biederman } 15250e47c99dSEric W. Biederman } 15260e47c99dSEric W. Biederman } 15270e47c99dSEric W. Biederman 1528938aaa4fSEric W. Biederman static void drop_sysctl_table(struct ctl_table_header *header) 1529938aaa4fSEric W. Biederman { 15307ec66d06SEric W. Biederman struct ctl_dir *parent = header->parent; 15317ec66d06SEric W. Biederman 1532938aaa4fSEric W. Biederman if (--header->nreg) 1533938aaa4fSEric W. Biederman return; 1534938aaa4fSEric W. Biederman 15350e47c99dSEric W. Biederman put_links(header); 1536938aaa4fSEric W. Biederman start_unregistering(header); 1537938aaa4fSEric W. Biederman if (!--header->count) 1538938aaa4fSEric W. Biederman kfree_rcu(header, rcu); 15397ec66d06SEric W. Biederman 15407ec66d06SEric W. Biederman if (parent) 15417ec66d06SEric W. Biederman drop_sysctl_table(&parent->header); 1542938aaa4fSEric W. Biederman } 1543938aaa4fSEric W. Biederman 15441f87f0b5SEric W. Biederman /** 15451f87f0b5SEric W. Biederman * unregister_sysctl_table - unregister a sysctl table hierarchy 15461f87f0b5SEric W. Biederman * @header: the header returned from register_sysctl_table 15471f87f0b5SEric W. Biederman * 15481f87f0b5SEric W. Biederman * Unregisters the sysctl table and all children. proc entries may not 15491f87f0b5SEric W. Biederman * actually be removed until they are no longer used by anyone. 15501f87f0b5SEric W. Biederman */ 15511f87f0b5SEric W. Biederman void unregister_sysctl_table(struct ctl_table_header * header) 15521f87f0b5SEric W. Biederman { 1553f728019bSEric W. Biederman int nr_subheaders; 15541f87f0b5SEric W. Biederman might_sleep(); 15551f87f0b5SEric W. Biederman 15561f87f0b5SEric W. Biederman if (header == NULL) 15571f87f0b5SEric W. Biederman return; 15581f87f0b5SEric W. Biederman 1559f728019bSEric W. Biederman nr_subheaders = count_subheaders(header->ctl_table_arg); 1560f728019bSEric W. Biederman if (unlikely(nr_subheaders > 1)) { 1561f728019bSEric W. Biederman struct ctl_table_header **subheaders; 1562f728019bSEric W. Biederman int i; 1563f728019bSEric W. Biederman 1564f728019bSEric W. Biederman subheaders = (struct ctl_table_header **)(header + 1); 1565f728019bSEric W. Biederman for (i = nr_subheaders -1; i >= 0; i--) { 1566f728019bSEric W. Biederman struct ctl_table_header *subh = subheaders[i]; 1567f728019bSEric W. Biederman struct ctl_table *table = subh->ctl_table_arg; 1568f728019bSEric W. Biederman unregister_sysctl_table(subh); 1569f728019bSEric W. Biederman kfree(table); 1570f728019bSEric W. Biederman } 1571f728019bSEric W. Biederman kfree(header); 1572f728019bSEric W. Biederman return; 1573f728019bSEric W. Biederman } 1574f728019bSEric W. Biederman 15751f87f0b5SEric W. Biederman spin_lock(&sysctl_lock); 1576938aaa4fSEric W. Biederman drop_sysctl_table(header); 15771f87f0b5SEric W. Biederman spin_unlock(&sysctl_lock); 15781f87f0b5SEric W. Biederman } 15791f87f0b5SEric W. Biederman EXPORT_SYMBOL(unregister_sysctl_table); 15801f87f0b5SEric W. Biederman 15810e47c99dSEric W. Biederman void setup_sysctl_set(struct ctl_table_set *set, 15829eb47c26SEric W. Biederman struct ctl_table_root *root, 15831f87f0b5SEric W. Biederman int (*is_seen)(struct ctl_table_set *)) 15841f87f0b5SEric W. Biederman { 15851347440dSDan Carpenter memset(set, 0, sizeof(*set)); 15860e47c99dSEric W. Biederman set->is_seen = is_seen; 1587ac13ac6fSEric W. Biederman init_header(&set->dir.header, root, set, NULL, root_table); 15881f87f0b5SEric W. Biederman } 15891f87f0b5SEric W. Biederman 159097324cd8SEric W. Biederman void retire_sysctl_set(struct ctl_table_set *set) 159197324cd8SEric W. Biederman { 1592ac13ac6fSEric W. Biederman WARN_ON(!RB_EMPTY_ROOT(&set->dir.root)); 159397324cd8SEric W. Biederman } 15941f87f0b5SEric W. Biederman 15951e0edd3fSAlexey Dobriyan int __init proc_sys_init(void) 159677b14db5SEric W. Biederman { 1597e1675231SAlexey Dobriyan struct proc_dir_entry *proc_sys_root; 1598e1675231SAlexey Dobriyan 159977b14db5SEric W. Biederman proc_sys_root = proc_mkdir("sys", NULL); 16009043476fSAl Viro proc_sys_root->proc_iops = &proc_sys_dir_operations; 16019043476fSAl Viro proc_sys_root->proc_fops = &proc_sys_dir_file_operations; 160277b14db5SEric W. Biederman proc_sys_root->nlink = 0; 1603de4e83bdSEric W. Biederman 1604de4e83bdSEric W. Biederman return sysctl_init(); 160577b14db5SEric W. Biederman } 1606