Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * /proc/sys support |
| 3 | */ |
Alexey Dobriyan | 1e0edd3 | 2008-10-17 05:07:44 +0400 | [diff] [blame] | 4 | #include <linux/init.h> |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 5 | #include <linux/sysctl.h> |
Lucas De Marchi | f1ecf06 | 2011-11-02 13:39:22 -0700 | [diff] [blame] | 6 | #include <linux/poll.h> |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 7 | #include <linux/proc_fs.h> |
| 8 | #include <linux/security.h> |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 9 | #include <linux/namei.h> |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 10 | #include <linux/module.h> |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 11 | #include "internal.h" |
| 12 | |
Al Viro | d72f71e | 2009-02-20 05:58:47 +0000 | [diff] [blame] | 13 | static const struct dentry_operations proc_sys_dentry_operations; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 14 | static const struct file_operations proc_sys_file_operations; |
Jan Engelhardt | 03a4482 | 2008-02-08 04:21:19 -0800 | [diff] [blame] | 15 | static const struct inode_operations proc_sys_inode_operations; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 16 | static const struct file_operations proc_sys_dir_file_operations; |
| 17 | static const struct inode_operations proc_sys_dir_operations; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 18 | |
Lucas De Marchi | f1ecf06 | 2011-11-02 13:39:22 -0700 | [diff] [blame] | 19 | void proc_sys_poll_notify(struct ctl_table_poll *poll) |
| 20 | { |
| 21 | if (!poll) |
| 22 | return; |
| 23 | |
| 24 | atomic_inc(&poll->event); |
| 25 | wake_up_interruptible(&poll->wait); |
| 26 | } |
| 27 | |
Eric W. Biederman | a194558 | 2012-01-21 17:51:48 -0800 | [diff] [blame] | 28 | static struct ctl_table root_table[] = { |
| 29 | { |
| 30 | .procname = "", |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 31 | .mode = S_IFDIR|S_IRUGO|S_IXUGO, |
Eric W. Biederman | a194558 | 2012-01-21 17:51:48 -0800 | [diff] [blame] | 32 | }, |
| 33 | { } |
| 34 | }; |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 35 | static struct ctl_table_root sysctl_table_root = { |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 36 | .default_set.dir.header = { |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 37 | {{.count = 1, |
| 38 | .nreg = 1, |
Eric W. Biederman | ac13ac6 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 39 | .ctl_table = root_table }}, |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 40 | .ctl_table_arg = root_table, |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 41 | .root = &sysctl_table_root, |
| 42 | .set = &sysctl_table_root.default_set, |
| 43 | }, |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 44 | }; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 45 | |
| 46 | static DEFINE_SPINLOCK(sysctl_lock); |
| 47 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 48 | static void drop_sysctl_table(struct ctl_table_header *header); |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 49 | static int sysctl_follow_link(struct ctl_table_header **phead, |
| 50 | struct ctl_table **pentry, struct nsproxy *namespaces); |
| 51 | static int insert_links(struct ctl_table_header *head); |
| 52 | static void put_links(struct ctl_table_header *header); |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 53 | |
Eric W. Biederman | 6980128 | 2012-01-21 20:09:45 -0800 | [diff] [blame] | 54 | static void sysctl_print_dir(struct ctl_dir *dir) |
| 55 | { |
| 56 | if (dir->header.parent) |
| 57 | sysctl_print_dir(dir->header.parent); |
| 58 | printk(KERN_CONT "%s/", dir->header.ctl_table[0].procname); |
| 59 | } |
| 60 | |
Eric W. Biederman | 076c3ee | 2012-01-09 21:42:02 -0800 | [diff] [blame] | 61 | static int namecmp(const char *name1, int len1, const char *name2, int len2) |
| 62 | { |
| 63 | int minlen; |
| 64 | int cmp; |
| 65 | |
| 66 | minlen = len1; |
| 67 | if (minlen > len2) |
| 68 | minlen = len2; |
| 69 | |
| 70 | cmp = memcmp(name1, name2, minlen); |
| 71 | if (cmp == 0) |
| 72 | cmp = len1 - len2; |
| 73 | return cmp; |
| 74 | } |
| 75 | |
| 76 | static struct ctl_table *find_entry(struct ctl_table_header **phead, |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 77 | struct ctl_dir *dir, const char *name, int namelen) |
Eric W. Biederman | 076c3ee | 2012-01-09 21:42:02 -0800 | [diff] [blame] | 78 | { |
| 79 | struct ctl_table_header *head; |
| 80 | struct ctl_table *entry; |
Eric W. Biederman | ac13ac6 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 81 | struct rb_node *node = dir->root.rb_node; |
Eric W. Biederman | 076c3ee | 2012-01-09 21:42:02 -0800 | [diff] [blame] | 82 | |
Eric W. Biederman | ac13ac6 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 83 | while (node) |
| 84 | { |
| 85 | struct ctl_node *ctl_node; |
| 86 | const char *procname; |
| 87 | int cmp; |
| 88 | |
| 89 | ctl_node = rb_entry(node, struct ctl_node, node); |
| 90 | head = ctl_node->header; |
| 91 | entry = &head->ctl_table[ctl_node - head->node]; |
| 92 | procname = entry->procname; |
| 93 | |
| 94 | cmp = namecmp(name, namelen, procname, strlen(procname)); |
| 95 | if (cmp < 0) |
| 96 | node = node->rb_left; |
| 97 | else if (cmp > 0) |
| 98 | node = node->rb_right; |
| 99 | else { |
| 100 | *phead = head; |
| 101 | return entry; |
Eric W. Biederman | 076c3ee | 2012-01-09 21:42:02 -0800 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | return NULL; |
| 105 | } |
| 106 | |
Eric W. Biederman | ac13ac6 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 107 | static int insert_entry(struct ctl_table_header *head, struct ctl_table *entry) |
| 108 | { |
| 109 | struct rb_node *node = &head->node[entry - head->ctl_table].node; |
| 110 | struct rb_node **p = &head->parent->root.rb_node; |
| 111 | struct rb_node *parent = NULL; |
| 112 | const char *name = entry->procname; |
| 113 | int namelen = strlen(name); |
| 114 | |
| 115 | while (*p) { |
| 116 | struct ctl_table_header *parent_head; |
| 117 | struct ctl_table *parent_entry; |
| 118 | struct ctl_node *parent_node; |
| 119 | const char *parent_name; |
| 120 | int cmp; |
| 121 | |
| 122 | parent = *p; |
| 123 | parent_node = rb_entry(parent, struct ctl_node, node); |
| 124 | parent_head = parent_node->header; |
| 125 | parent_entry = &parent_head->ctl_table[parent_node - parent_head->node]; |
| 126 | parent_name = parent_entry->procname; |
| 127 | |
| 128 | cmp = namecmp(name, namelen, parent_name, strlen(parent_name)); |
| 129 | if (cmp < 0) |
| 130 | p = &(*p)->rb_left; |
| 131 | else if (cmp > 0) |
| 132 | p = &(*p)->rb_right; |
| 133 | else { |
| 134 | printk(KERN_ERR "sysctl duplicate entry: "); |
| 135 | sysctl_print_dir(head->parent); |
| 136 | printk(KERN_CONT "/%s\n", entry->procname); |
| 137 | return -EEXIST; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | rb_link_node(node, parent, p); |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static void erase_entry(struct ctl_table_header *head, struct ctl_table *entry) |
| 146 | { |
| 147 | struct rb_node *node = &head->node[entry - head->ctl_table].node; |
| 148 | |
| 149 | rb_erase(node, &head->parent->root); |
| 150 | } |
| 151 | |
Eric W. Biederman | e0d0452 | 2012-01-09 22:36:41 -0800 | [diff] [blame] | 152 | static void init_header(struct ctl_table_header *head, |
| 153 | struct ctl_table_root *root, struct ctl_table_set *set, |
Eric W. Biederman | ac13ac6 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 154 | struct ctl_node *node, struct ctl_table *table) |
Eric W. Biederman | e0d0452 | 2012-01-09 22:36:41 -0800 | [diff] [blame] | 155 | { |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 156 | head->ctl_table = table; |
Eric W. Biederman | e0d0452 | 2012-01-09 22:36:41 -0800 | [diff] [blame] | 157 | head->ctl_table_arg = table; |
Eric W. Biederman | e0d0452 | 2012-01-09 22:36:41 -0800 | [diff] [blame] | 158 | head->used = 0; |
| 159 | head->count = 1; |
| 160 | head->nreg = 1; |
| 161 | head->unregistering = NULL; |
| 162 | head->root = root; |
| 163 | head->set = set; |
| 164 | head->parent = NULL; |
Eric W. Biederman | ac13ac6 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 165 | head->node = node; |
| 166 | if (node) { |
| 167 | struct ctl_table *entry; |
| 168 | for (entry = table; entry->procname; entry++, node++) { |
| 169 | rb_init_node(&node->node); |
| 170 | node->header = head; |
| 171 | } |
| 172 | } |
Eric W. Biederman | e0d0452 | 2012-01-09 22:36:41 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Eric W. Biederman | 8425d6a | 2012-01-09 17:35:01 -0800 | [diff] [blame] | 175 | static void erase_header(struct ctl_table_header *head) |
| 176 | { |
Eric W. Biederman | ac13ac6 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 177 | struct ctl_table *entry; |
| 178 | for (entry = head->ctl_table; entry->procname; entry++) |
| 179 | erase_entry(head, entry); |
Eric W. Biederman | 8425d6a | 2012-01-09 17:35:01 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 182 | static int insert_header(struct ctl_dir *dir, struct ctl_table_header *header) |
Eric W. Biederman | 8425d6a | 2012-01-09 17:35:01 -0800 | [diff] [blame] | 183 | { |
Eric W. Biederman | ac13ac6 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 184 | struct ctl_table *entry; |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 185 | int err; |
| 186 | |
| 187 | dir->header.nreg++; |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 188 | header->parent = dir; |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 189 | err = insert_links(header); |
| 190 | if (err) |
| 191 | goto fail_links; |
Eric W. Biederman | ac13ac6 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 192 | for (entry = header->ctl_table; entry->procname; entry++) { |
| 193 | err = insert_entry(header, entry); |
| 194 | if (err) |
| 195 | goto fail; |
| 196 | } |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 197 | return 0; |
Eric W. Biederman | ac13ac6 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 198 | fail: |
| 199 | erase_header(header); |
| 200 | put_links(header); |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 201 | fail_links: |
| 202 | header->parent = NULL; |
| 203 | drop_sysctl_table(&dir->header); |
| 204 | return err; |
Eric W. Biederman | 8425d6a | 2012-01-09 17:35:01 -0800 | [diff] [blame] | 205 | } |
| 206 | |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 207 | /* called under sysctl_lock */ |
| 208 | static int use_table(struct ctl_table_header *p) |
| 209 | { |
| 210 | if (unlikely(p->unregistering)) |
| 211 | return 0; |
| 212 | p->used++; |
| 213 | return 1; |
| 214 | } |
| 215 | |
| 216 | /* called under sysctl_lock */ |
| 217 | static void unuse_table(struct ctl_table_header *p) |
| 218 | { |
| 219 | if (!--p->used) |
| 220 | if (unlikely(p->unregistering)) |
| 221 | complete(p->unregistering); |
| 222 | } |
| 223 | |
| 224 | /* called under sysctl_lock, will reacquire if has to wait */ |
| 225 | static void start_unregistering(struct ctl_table_header *p) |
| 226 | { |
| 227 | /* |
| 228 | * if p->used is 0, nobody will ever touch that entry again; |
| 229 | * we'll eliminate all paths to it before dropping sysctl_lock |
| 230 | */ |
| 231 | if (unlikely(p->used)) { |
| 232 | struct completion wait; |
| 233 | init_completion(&wait); |
| 234 | p->unregistering = &wait; |
| 235 | spin_unlock(&sysctl_lock); |
| 236 | wait_for_completion(&wait); |
| 237 | spin_lock(&sysctl_lock); |
| 238 | } else { |
| 239 | /* anything non-NULL; we'll never dereference it */ |
| 240 | p->unregistering = ERR_PTR(-EINVAL); |
| 241 | } |
| 242 | /* |
| 243 | * do not remove from the list until nobody holds it; walking the |
| 244 | * list in do_sysctl() relies on that. |
| 245 | */ |
Eric W. Biederman | 8425d6a | 2012-01-09 17:35:01 -0800 | [diff] [blame] | 246 | erase_header(p); |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | static void sysctl_head_get(struct ctl_table_header *head) |
| 250 | { |
| 251 | spin_lock(&sysctl_lock); |
| 252 | head->count++; |
| 253 | spin_unlock(&sysctl_lock); |
| 254 | } |
| 255 | |
| 256 | void sysctl_head_put(struct ctl_table_header *head) |
| 257 | { |
| 258 | spin_lock(&sysctl_lock); |
| 259 | if (!--head->count) |
| 260 | kfree_rcu(head, rcu); |
| 261 | spin_unlock(&sysctl_lock); |
| 262 | } |
| 263 | |
| 264 | static struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head) |
| 265 | { |
| 266 | if (!head) |
| 267 | BUG(); |
| 268 | spin_lock(&sysctl_lock); |
| 269 | if (!use_table(head)) |
| 270 | head = ERR_PTR(-ENOENT); |
| 271 | spin_unlock(&sysctl_lock); |
| 272 | return head; |
| 273 | } |
| 274 | |
| 275 | static void sysctl_head_finish(struct ctl_table_header *head) |
| 276 | { |
| 277 | if (!head) |
| 278 | return; |
| 279 | spin_lock(&sysctl_lock); |
| 280 | unuse_table(head); |
| 281 | spin_unlock(&sysctl_lock); |
| 282 | } |
| 283 | |
| 284 | static struct ctl_table_set * |
| 285 | lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces) |
| 286 | { |
| 287 | struct ctl_table_set *set = &root->default_set; |
| 288 | if (root->lookup) |
| 289 | set = root->lookup(root, namespaces); |
| 290 | return set; |
| 291 | } |
| 292 | |
Eric W. Biederman | 076c3ee | 2012-01-09 21:42:02 -0800 | [diff] [blame] | 293 | static struct ctl_table *lookup_entry(struct ctl_table_header **phead, |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 294 | struct ctl_dir *dir, |
Eric W. Biederman | 076c3ee | 2012-01-09 21:42:02 -0800 | [diff] [blame] | 295 | const char *name, int namelen) |
| 296 | { |
| 297 | struct ctl_table_header *head; |
| 298 | struct ctl_table *entry; |
Eric W. Biederman | 076c3ee | 2012-01-09 21:42:02 -0800 | [diff] [blame] | 299 | |
| 300 | spin_lock(&sysctl_lock); |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 301 | entry = find_entry(&head, dir, name, namelen); |
| 302 | if (entry && use_table(head)) |
| 303 | *phead = head; |
| 304 | else |
| 305 | entry = NULL; |
Eric W. Biederman | 076c3ee | 2012-01-09 21:42:02 -0800 | [diff] [blame] | 306 | spin_unlock(&sysctl_lock); |
| 307 | return entry; |
| 308 | } |
| 309 | |
Eric W. Biederman | ac13ac6 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 310 | static struct ctl_node *first_usable_entry(struct rb_node *node) |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 311 | { |
Eric W. Biederman | ac13ac6 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 312 | struct ctl_node *ctl_node; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 313 | |
Eric W. Biederman | ac13ac6 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 314 | for (;node; node = rb_next(node)) { |
| 315 | ctl_node = rb_entry(node, struct ctl_node, node); |
| 316 | if (use_table(ctl_node->header)) |
| 317 | return ctl_node; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 318 | } |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 319 | return NULL; |
| 320 | } |
| 321 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 322 | static void first_entry(struct ctl_dir *dir, |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 323 | struct ctl_table_header **phead, struct ctl_table **pentry) |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 324 | { |
Eric W. Biederman | ac13ac6 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 325 | struct ctl_table_header *head = NULL; |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 326 | struct ctl_table *entry = NULL; |
Eric W. Biederman | ac13ac6 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 327 | struct ctl_node *ctl_node; |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 328 | |
| 329 | spin_lock(&sysctl_lock); |
Eric W. Biederman | ac13ac6 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 330 | ctl_node = first_usable_entry(rb_first(&dir->root)); |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 331 | spin_unlock(&sysctl_lock); |
Eric W. Biederman | ac13ac6 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 332 | if (ctl_node) { |
| 333 | head = ctl_node->header; |
| 334 | entry = &head->ctl_table[ctl_node - head->node]; |
| 335 | } |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 336 | *phead = head; |
| 337 | *pentry = entry; |
| 338 | } |
| 339 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 340 | static void next_entry(struct ctl_table_header **phead, struct ctl_table **pentry) |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 341 | { |
| 342 | struct ctl_table_header *head = *phead; |
| 343 | struct ctl_table *entry = *pentry; |
Eric W. Biederman | ac13ac6 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 344 | struct ctl_node *ctl_node = &head->node[entry - head->ctl_table]; |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 345 | |
Eric W. Biederman | ac13ac6 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 346 | spin_lock(&sysctl_lock); |
| 347 | unuse_table(head); |
| 348 | |
| 349 | ctl_node = first_usable_entry(rb_next(&ctl_node->node)); |
| 350 | spin_unlock(&sysctl_lock); |
| 351 | head = NULL; |
| 352 | if (ctl_node) { |
| 353 | head = ctl_node->header; |
| 354 | entry = &head->ctl_table[ctl_node - head->node]; |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 355 | } |
| 356 | *phead = head; |
| 357 | *pentry = entry; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | void register_sysctl_root(struct ctl_table_root *root) |
| 361 | { |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | /* |
| 365 | * sysctl_perm does NOT grant the superuser all rights automatically, because |
| 366 | * some sysctl variables are readonly even to root. |
| 367 | */ |
| 368 | |
| 369 | static int test_perm(int mode, int op) |
| 370 | { |
| 371 | if (!current_euid()) |
| 372 | mode >>= 6; |
| 373 | else if (in_egroup_p(0)) |
| 374 | mode >>= 3; |
| 375 | if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0) |
| 376 | return 0; |
| 377 | return -EACCES; |
| 378 | } |
| 379 | |
| 380 | static int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op) |
| 381 | { |
| 382 | int mode; |
| 383 | |
| 384 | if (root->permissions) |
| 385 | mode = root->permissions(root, current->nsproxy, table); |
| 386 | else |
| 387 | mode = table->mode; |
| 388 | |
| 389 | return test_perm(mode, op); |
| 390 | } |
| 391 | |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 392 | static struct inode *proc_sys_make_inode(struct super_block *sb, |
| 393 | struct ctl_table_header *head, struct ctl_table *table) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 394 | { |
| 395 | struct inode *inode; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 396 | struct proc_inode *ei; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 397 | |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 398 | inode = new_inode(sb); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 399 | if (!inode) |
| 400 | goto out; |
| 401 | |
Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 402 | inode->i_ino = get_next_ino(); |
| 403 | |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 404 | sysctl_head_get(head); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 405 | ei = PROC_I(inode); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 406 | ei->sysctl = head; |
| 407 | ei->sysctl_entry = table; |
| 408 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 409 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 410 | inode->i_mode = table->mode; |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 411 | if (!S_ISDIR(table->mode)) { |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 412 | inode->i_mode |= S_IFREG; |
| 413 | inode->i_op = &proc_sys_inode_operations; |
| 414 | inode->i_fop = &proc_sys_file_operations; |
| 415 | } else { |
| 416 | inode->i_mode |= S_IFDIR; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 417 | inode->i_op = &proc_sys_dir_operations; |
| 418 | inode->i_fop = &proc_sys_dir_file_operations; |
| 419 | } |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 420 | out: |
| 421 | return inode; |
| 422 | } |
| 423 | |
Adrian Bunk | 8132436 | 2008-10-03 00:33:54 +0400 | [diff] [blame] | 424 | static struct ctl_table_header *grab_header(struct inode *inode) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 425 | { |
Eric W. Biederman | 3cc3e04 | 2012-01-07 06:57:47 -0800 | [diff] [blame] | 426 | struct ctl_table_header *head = PROC_I(inode)->sysctl; |
| 427 | if (!head) |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 428 | head = &sysctl_table_root.default_set.dir.header; |
Eric W. Biederman | 3cc3e04 | 2012-01-07 06:57:47 -0800 | [diff] [blame] | 429 | return sysctl_head_grab(head); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | static struct dentry *proc_sys_lookup(struct inode *dir, struct dentry *dentry, |
| 433 | struct nameidata *nd) |
| 434 | { |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 435 | struct ctl_table_header *head = grab_header(dir); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 436 | struct ctl_table_header *h = NULL; |
| 437 | struct qstr *name = &dentry->d_name; |
| 438 | struct ctl_table *p; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 439 | struct inode *inode; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 440 | struct dentry *err = ERR_PTR(-ENOENT); |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 441 | struct ctl_dir *ctl_dir; |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 442 | int ret; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 443 | |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 444 | if (IS_ERR(head)) |
| 445 | return ERR_CAST(head); |
| 446 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 447 | ctl_dir = container_of(head, struct ctl_dir, header); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 448 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 449 | p = lookup_entry(&h, ctl_dir, name->name, name->len); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 450 | if (!p) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 451 | goto out; |
| 452 | |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 453 | ret = sysctl_follow_link(&h, &p, current->nsproxy); |
| 454 | err = ERR_PTR(ret); |
| 455 | if (ret) |
| 456 | goto out; |
| 457 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 458 | err = ERR_PTR(-ENOMEM); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 459 | inode = proc_sys_make_inode(dir->i_sb, h ? h : head, p); |
| 460 | if (h) |
| 461 | sysctl_head_finish(h); |
| 462 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 463 | if (!inode) |
| 464 | goto out; |
| 465 | |
| 466 | err = NULL; |
Nick Piggin | fb045ad | 2011-01-07 17:49:55 +1100 | [diff] [blame] | 467 | d_set_d_op(dentry, &proc_sys_dentry_operations); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 468 | d_add(dentry, inode); |
| 469 | |
| 470 | out: |
| 471 | sysctl_head_finish(head); |
| 472 | return err; |
| 473 | } |
| 474 | |
Pavel Emelyanov | 7708bfb | 2008-04-29 01:02:40 -0700 | [diff] [blame] | 475 | static ssize_t proc_sys_call_handler(struct file *filp, void __user *buf, |
| 476 | size_t count, loff_t *ppos, int write) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 477 | { |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 478 | struct inode *inode = filp->f_path.dentry->d_inode; |
| 479 | struct ctl_table_header *head = grab_header(inode); |
| 480 | struct ctl_table *table = PROC_I(inode)->sysctl_entry; |
David Howells | 2a2da53 | 2007-10-25 15:27:40 +0100 | [diff] [blame] | 481 | ssize_t error; |
| 482 | size_t res; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 483 | |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 484 | if (IS_ERR(head)) |
| 485 | return PTR_ERR(head); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 486 | |
| 487 | /* |
| 488 | * At this point we know that the sysctl was not unregistered |
| 489 | * and won't be until we finish. |
| 490 | */ |
| 491 | error = -EPERM; |
Pavel Emelyanov | d7321cd | 2008-04-29 01:02:44 -0700 | [diff] [blame] | 492 | if (sysctl_perm(head->root, table, write ? MAY_WRITE : MAY_READ)) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 493 | goto out; |
| 494 | |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 495 | /* if that can happen at all, it should be -EINVAL, not -EISDIR */ |
| 496 | error = -EINVAL; |
| 497 | if (!table->proc_handler) |
| 498 | goto out; |
| 499 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 500 | /* careful: calling conventions are nasty here */ |
| 501 | res = count; |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 502 | error = table->proc_handler(table, write, buf, &res, ppos); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 503 | if (!error) |
| 504 | error = res; |
| 505 | out: |
| 506 | sysctl_head_finish(head); |
| 507 | |
| 508 | return error; |
| 509 | } |
| 510 | |
Pavel Emelyanov | 7708bfb | 2008-04-29 01:02:40 -0700 | [diff] [blame] | 511 | static ssize_t proc_sys_read(struct file *filp, char __user *buf, |
| 512 | size_t count, loff_t *ppos) |
| 513 | { |
| 514 | return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 0); |
| 515 | } |
| 516 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 517 | static ssize_t proc_sys_write(struct file *filp, const char __user *buf, |
| 518 | size_t count, loff_t *ppos) |
| 519 | { |
Pavel Emelyanov | 7708bfb | 2008-04-29 01:02:40 -0700 | [diff] [blame] | 520 | return proc_sys_call_handler(filp, (void __user *)buf, count, ppos, 1); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 521 | } |
| 522 | |
Lucas De Marchi | f1ecf06 | 2011-11-02 13:39:22 -0700 | [diff] [blame] | 523 | static int proc_sys_open(struct inode *inode, struct file *filp) |
| 524 | { |
| 525 | struct ctl_table *table = PROC_I(inode)->sysctl_entry; |
| 526 | |
| 527 | if (table->poll) |
| 528 | filp->private_data = proc_sys_poll_event(table->poll); |
| 529 | |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | static unsigned int proc_sys_poll(struct file *filp, poll_table *wait) |
| 534 | { |
| 535 | struct inode *inode = filp->f_path.dentry->d_inode; |
| 536 | struct ctl_table *table = PROC_I(inode)->sysctl_entry; |
| 537 | unsigned long event = (unsigned long)filp->private_data; |
| 538 | unsigned int ret = DEFAULT_POLLMASK; |
| 539 | |
| 540 | if (!table->proc_handler) |
| 541 | goto out; |
| 542 | |
| 543 | if (!table->poll) |
| 544 | goto out; |
| 545 | |
| 546 | poll_wait(filp, &table->poll->wait, wait); |
| 547 | |
| 548 | if (event != atomic_read(&table->poll->event)) { |
| 549 | filp->private_data = proc_sys_poll_event(table->poll); |
| 550 | ret = POLLIN | POLLRDNORM | POLLERR | POLLPRI; |
| 551 | } |
| 552 | |
| 553 | out: |
| 554 | return ret; |
| 555 | } |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 556 | |
| 557 | static int proc_sys_fill_cache(struct file *filp, void *dirent, |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 558 | filldir_t filldir, |
| 559 | struct ctl_table_header *head, |
| 560 | struct ctl_table *table) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 561 | { |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 562 | struct dentry *child, *dir = filp->f_path.dentry; |
| 563 | struct inode *inode; |
| 564 | struct qstr qname; |
| 565 | ino_t ino = 0; |
| 566 | unsigned type = DT_UNKNOWN; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 567 | |
| 568 | qname.name = table->procname; |
| 569 | qname.len = strlen(table->procname); |
| 570 | qname.hash = full_name_hash(qname.name, qname.len); |
| 571 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 572 | child = d_lookup(dir, &qname); |
| 573 | if (!child) { |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 574 | child = d_alloc(dir, &qname); |
| 575 | if (child) { |
| 576 | inode = proc_sys_make_inode(dir->d_sb, head, table); |
| 577 | if (!inode) { |
| 578 | dput(child); |
| 579 | return -ENOMEM; |
| 580 | } else { |
Nick Piggin | fb045ad | 2011-01-07 17:49:55 +1100 | [diff] [blame] | 581 | d_set_d_op(child, &proc_sys_dentry_operations); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 582 | d_add(child, inode); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 583 | } |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 584 | } else { |
| 585 | return -ENOMEM; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 586 | } |
| 587 | } |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 588 | inode = child->d_inode; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 589 | ino = inode->i_ino; |
| 590 | type = inode->i_mode >> 12; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 591 | dput(child); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 592 | return !!filldir(dirent, qname.name, qname.len, filp->f_pos, ino, type); |
| 593 | } |
| 594 | |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 595 | static int proc_sys_link_fill_cache(struct file *filp, void *dirent, |
| 596 | filldir_t filldir, |
| 597 | struct ctl_table_header *head, |
| 598 | struct ctl_table *table) |
| 599 | { |
| 600 | int err, ret = 0; |
| 601 | head = sysctl_head_grab(head); |
| 602 | |
| 603 | /* It is not an error if we can not follow the link ignore it */ |
| 604 | err = sysctl_follow_link(&head, &table, current->nsproxy); |
| 605 | if (err) |
| 606 | goto out; |
| 607 | |
| 608 | ret = proc_sys_fill_cache(filp, dirent, filldir, head, table); |
| 609 | out: |
| 610 | sysctl_head_finish(head); |
| 611 | return ret; |
| 612 | } |
| 613 | |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 614 | static int scan(struct ctl_table_header *head, ctl_table *table, |
| 615 | unsigned long *pos, struct file *file, |
| 616 | void *dirent, filldir_t filldir) |
| 617 | { |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 618 | int res; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 619 | |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 620 | if ((*pos)++ < file->f_pos) |
| 621 | return 0; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 622 | |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 623 | if (unlikely(S_ISLNK(table->mode))) |
| 624 | res = proc_sys_link_fill_cache(file, dirent, filldir, head, table); |
| 625 | else |
| 626 | res = proc_sys_fill_cache(file, dirent, filldir, head, table); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 627 | |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 628 | if (res == 0) |
| 629 | file->f_pos = *pos; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 630 | |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 631 | return res; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | static int proc_sys_readdir(struct file *filp, void *dirent, filldir_t filldir) |
| 635 | { |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 636 | struct dentry *dentry = filp->f_path.dentry; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 637 | struct inode *inode = dentry->d_inode; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 638 | struct ctl_table_header *head = grab_header(inode); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 639 | struct ctl_table_header *h = NULL; |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 640 | struct ctl_table *entry; |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 641 | struct ctl_dir *ctl_dir; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 642 | unsigned long pos; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 643 | int ret = -EINVAL; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 644 | |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 645 | if (IS_ERR(head)) |
| 646 | return PTR_ERR(head); |
| 647 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 648 | ctl_dir = container_of(head, struct ctl_dir, header); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 649 | |
| 650 | ret = 0; |
| 651 | /* Avoid a switch here: arm builds fail with missing __cmpdi2 */ |
| 652 | if (filp->f_pos == 0) { |
| 653 | if (filldir(dirent, ".", 1, filp->f_pos, |
| 654 | inode->i_ino, DT_DIR) < 0) |
| 655 | goto out; |
| 656 | filp->f_pos++; |
| 657 | } |
| 658 | if (filp->f_pos == 1) { |
| 659 | if (filldir(dirent, "..", 2, filp->f_pos, |
| 660 | parent_ino(dentry), DT_DIR) < 0) |
| 661 | goto out; |
| 662 | filp->f_pos++; |
| 663 | } |
| 664 | pos = 2; |
| 665 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 666 | for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) { |
Eric W. Biederman | 6a75ce1 | 2012-01-18 03:15:51 -0800 | [diff] [blame] | 667 | ret = scan(h, entry, &pos, filp, dirent, filldir); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 668 | if (ret) { |
| 669 | sysctl_head_finish(h); |
| 670 | break; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | ret = 1; |
| 674 | out: |
| 675 | sysctl_head_finish(head); |
| 676 | return ret; |
| 677 | } |
| 678 | |
Al Viro | 10556cb | 2011-06-20 19:28:19 -0400 | [diff] [blame] | 679 | static int proc_sys_permission(struct inode *inode, int mask) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 680 | { |
| 681 | /* |
| 682 | * sysctl entries that are not writeable, |
| 683 | * are _NOT_ writeable, capabilities or not. |
| 684 | */ |
Miklos Szeredi | f696a36 | 2008-07-31 13:41:58 +0200 | [diff] [blame] | 685 | struct ctl_table_header *head; |
| 686 | struct ctl_table *table; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 687 | int error; |
| 688 | |
Miklos Szeredi | f696a36 | 2008-07-31 13:41:58 +0200 | [diff] [blame] | 689 | /* Executable files are not allowed under /proc/sys/ */ |
| 690 | if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) |
| 691 | return -EACCES; |
| 692 | |
| 693 | head = grab_header(inode); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 694 | if (IS_ERR(head)) |
| 695 | return PTR_ERR(head); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 696 | |
Miklos Szeredi | f696a36 | 2008-07-31 13:41:58 +0200 | [diff] [blame] | 697 | table = PROC_I(inode)->sysctl_entry; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 698 | if (!table) /* global root - r-xr-xr-x */ |
| 699 | error = mask & MAY_WRITE ? -EACCES : 0; |
| 700 | else /* Use the permissions on the sysctl table entry */ |
Al Viro | 1fc0f78 | 2011-06-20 18:59:02 -0400 | [diff] [blame] | 701 | error = sysctl_perm(head->root, table, mask & ~MAY_NOT_BLOCK); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 702 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 703 | sysctl_head_finish(head); |
| 704 | return error; |
| 705 | } |
| 706 | |
| 707 | static int proc_sys_setattr(struct dentry *dentry, struct iattr *attr) |
| 708 | { |
| 709 | struct inode *inode = dentry->d_inode; |
| 710 | int error; |
| 711 | |
| 712 | if (attr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)) |
| 713 | return -EPERM; |
| 714 | |
| 715 | error = inode_change_ok(inode, attr); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 716 | if (error) |
| 717 | return error; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 718 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 719 | if ((attr->ia_valid & ATTR_SIZE) && |
| 720 | attr->ia_size != i_size_read(inode)) { |
| 721 | error = vmtruncate(inode, attr->ia_size); |
| 722 | if (error) |
| 723 | return error; |
| 724 | } |
| 725 | |
| 726 | setattr_copy(inode, attr); |
| 727 | mark_inode_dirty(inode); |
| 728 | return 0; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 729 | } |
| 730 | |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 731 | static int proc_sys_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) |
| 732 | { |
| 733 | struct inode *inode = dentry->d_inode; |
| 734 | struct ctl_table_header *head = grab_header(inode); |
| 735 | struct ctl_table *table = PROC_I(inode)->sysctl_entry; |
| 736 | |
| 737 | if (IS_ERR(head)) |
| 738 | return PTR_ERR(head); |
| 739 | |
| 740 | generic_fillattr(inode, stat); |
| 741 | if (table) |
| 742 | stat->mode = (stat->mode & S_IFMT) | table->mode; |
| 743 | |
| 744 | sysctl_head_finish(head); |
| 745 | return 0; |
| 746 | } |
| 747 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 748 | static const struct file_operations proc_sys_file_operations = { |
Lucas De Marchi | f1ecf06 | 2011-11-02 13:39:22 -0700 | [diff] [blame] | 749 | .open = proc_sys_open, |
| 750 | .poll = proc_sys_poll, |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 751 | .read = proc_sys_read, |
| 752 | .write = proc_sys_write, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 753 | .llseek = default_llseek, |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 754 | }; |
| 755 | |
| 756 | static const struct file_operations proc_sys_dir_file_operations = { |
Pavel Emelyanov | 887df07 | 2011-11-02 13:38:42 -0700 | [diff] [blame] | 757 | .read = generic_read_dir, |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 758 | .readdir = proc_sys_readdir, |
Christoph Hellwig | 3222a3e | 2008-09-03 21:53:01 +0200 | [diff] [blame] | 759 | .llseek = generic_file_llseek, |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 760 | }; |
| 761 | |
Jan Engelhardt | 03a4482 | 2008-02-08 04:21:19 -0800 | [diff] [blame] | 762 | static const struct inode_operations proc_sys_inode_operations = { |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 763 | .permission = proc_sys_permission, |
| 764 | .setattr = proc_sys_setattr, |
| 765 | .getattr = proc_sys_getattr, |
| 766 | }; |
| 767 | |
| 768 | static const struct inode_operations proc_sys_dir_operations = { |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 769 | .lookup = proc_sys_lookup, |
| 770 | .permission = proc_sys_permission, |
| 771 | .setattr = proc_sys_setattr, |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 772 | .getattr = proc_sys_getattr, |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 773 | }; |
| 774 | |
| 775 | static int proc_sys_revalidate(struct dentry *dentry, struct nameidata *nd) |
| 776 | { |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 777 | if (nd->flags & LOOKUP_RCU) |
| 778 | return -ECHILD; |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 779 | return !PROC_I(dentry->d_inode)->sysctl->unregistering; |
| 780 | } |
| 781 | |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 782 | static int proc_sys_delete(const struct dentry *dentry) |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 783 | { |
| 784 | return !!PROC_I(dentry->d_inode)->sysctl->unregistering; |
| 785 | } |
| 786 | |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 787 | static int sysctl_is_seen(struct ctl_table_header *p) |
| 788 | { |
| 789 | struct ctl_table_set *set = p->set; |
| 790 | int res; |
| 791 | spin_lock(&sysctl_lock); |
| 792 | if (p->unregistering) |
| 793 | res = 0; |
| 794 | else if (!set->is_seen) |
| 795 | res = 1; |
| 796 | else |
| 797 | res = set->is_seen(set); |
| 798 | spin_unlock(&sysctl_lock); |
| 799 | return res; |
| 800 | } |
| 801 | |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 802 | static int proc_sys_compare(const struct dentry *parent, |
| 803 | const struct inode *pinode, |
| 804 | const struct dentry *dentry, const struct inode *inode, |
| 805 | unsigned int len, const char *str, const struct qstr *name) |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 806 | { |
Al Viro | dfef6dc | 2011-03-08 01:25:28 -0500 | [diff] [blame] | 807 | struct ctl_table_header *head; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 808 | /* Although proc doesn't have negative dentries, rcu-walk means |
| 809 | * that inode here can be NULL */ |
Al Viro | dfef6dc | 2011-03-08 01:25:28 -0500 | [diff] [blame] | 810 | /* AV: can it, indeed? */ |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 811 | if (!inode) |
Al Viro | dfef6dc | 2011-03-08 01:25:28 -0500 | [diff] [blame] | 812 | return 1; |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 813 | if (name->len != len) |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 814 | return 1; |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 815 | if (memcmp(name->name, str, len)) |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 816 | return 1; |
Al Viro | dfef6dc | 2011-03-08 01:25:28 -0500 | [diff] [blame] | 817 | head = rcu_dereference(PROC_I(inode)->sysctl); |
| 818 | return !head || !sysctl_is_seen(head); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 819 | } |
| 820 | |
Al Viro | d72f71e | 2009-02-20 05:58:47 +0000 | [diff] [blame] | 821 | static const struct dentry_operations proc_sys_dentry_operations = { |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 822 | .d_revalidate = proc_sys_revalidate, |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 823 | .d_delete = proc_sys_delete, |
| 824 | .d_compare = proc_sys_compare, |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 825 | }; |
| 826 | |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 827 | static struct ctl_dir *find_subdir(struct ctl_dir *dir, |
| 828 | const char *name, int namelen) |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 829 | { |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 830 | struct ctl_table_header *head; |
| 831 | struct ctl_table *entry; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 832 | |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 833 | entry = find_entry(&head, dir, name, namelen); |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 834 | if (!entry) |
| 835 | return ERR_PTR(-ENOENT); |
Eric W. Biederman | 51f72f4 | 2012-01-30 20:09:33 -0800 | [diff] [blame] | 836 | if (!S_ISDIR(entry->mode)) |
| 837 | return ERR_PTR(-ENOTDIR); |
| 838 | return container_of(head, struct ctl_dir, header); |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 839 | } |
| 840 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 841 | static struct ctl_dir *new_dir(struct ctl_table_set *set, |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 842 | const char *name, int namelen) |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 843 | { |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 844 | struct ctl_table *table; |
| 845 | struct ctl_dir *new; |
Eric W. Biederman | ac13ac6 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 846 | struct ctl_node *node; |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 847 | char *new_name; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 848 | |
Eric W. Biederman | ac13ac6 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 849 | new = kzalloc(sizeof(*new) + sizeof(struct ctl_node) + |
| 850 | sizeof(struct ctl_table)*2 + namelen + 1, |
| 851 | GFP_KERNEL); |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 852 | if (!new) |
| 853 | return NULL; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 854 | |
Eric W. Biederman | ac13ac6 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 855 | node = (struct ctl_node *)(new + 1); |
| 856 | table = (struct ctl_table *)(node + 1); |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 857 | new_name = (char *)(table + 2); |
| 858 | memcpy(new_name, name, namelen); |
| 859 | new_name[namelen] = '\0'; |
| 860 | table[0].procname = new_name; |
| 861 | table[0].mode = S_IFDIR|S_IRUGO|S_IXUGO; |
Eric W. Biederman | ac13ac6 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 862 | init_header(&new->header, set->dir.header.root, set, node, table); |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 863 | |
| 864 | return new; |
| 865 | } |
| 866 | |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 867 | static struct ctl_dir *get_subdir(struct ctl_dir *dir, |
| 868 | const char *name, int namelen) |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 869 | { |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 870 | struct ctl_table_set *set = dir->header.set; |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 871 | struct ctl_dir *subdir, *new = NULL; |
Eric W. Biederman | 0eb97f3 | 2012-01-30 20:37:51 -0800 | [diff] [blame^] | 872 | int err; |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 873 | |
| 874 | spin_lock(&sysctl_lock); |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 875 | subdir = find_subdir(dir, name, namelen); |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 876 | if (!IS_ERR(subdir)) |
| 877 | goto found; |
| 878 | if (PTR_ERR(subdir) != -ENOENT) |
| 879 | goto failed; |
| 880 | |
| 881 | spin_unlock(&sysctl_lock); |
| 882 | new = new_dir(set, name, namelen); |
| 883 | spin_lock(&sysctl_lock); |
| 884 | subdir = ERR_PTR(-ENOMEM); |
| 885 | if (!new) |
| 886 | goto failed; |
| 887 | |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 888 | subdir = find_subdir(dir, name, namelen); |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 889 | if (!IS_ERR(subdir)) |
| 890 | goto found; |
| 891 | if (PTR_ERR(subdir) != -ENOENT) |
| 892 | goto failed; |
| 893 | |
Eric W. Biederman | 0eb97f3 | 2012-01-30 20:37:51 -0800 | [diff] [blame^] | 894 | err = insert_header(dir, &new->header); |
| 895 | subdir = ERR_PTR(err); |
| 896 | if (err) |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 897 | goto failed; |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 898 | subdir = new; |
| 899 | found: |
| 900 | subdir->header.nreg++; |
| 901 | failed: |
| 902 | if (unlikely(IS_ERR(subdir))) { |
Eric W. Biederman | 6980128 | 2012-01-21 20:09:45 -0800 | [diff] [blame] | 903 | printk(KERN_ERR "sysctl could not get directory: "); |
| 904 | sysctl_print_dir(dir); |
| 905 | printk(KERN_CONT "/%*.*s %ld\n", |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 906 | namelen, namelen, name, PTR_ERR(subdir)); |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 907 | } |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 908 | drop_sysctl_table(&dir->header); |
| 909 | if (new) |
| 910 | drop_sysctl_table(&new->header); |
| 911 | spin_unlock(&sysctl_lock); |
| 912 | return subdir; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 913 | } |
| 914 | |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 915 | static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir) |
| 916 | { |
| 917 | struct ctl_dir *parent; |
| 918 | const char *procname; |
| 919 | if (!dir->header.parent) |
| 920 | return &set->dir; |
| 921 | parent = xlate_dir(set, dir->header.parent); |
| 922 | if (IS_ERR(parent)) |
| 923 | return parent; |
| 924 | procname = dir->header.ctl_table[0].procname; |
| 925 | return find_subdir(parent, procname, strlen(procname)); |
| 926 | } |
| 927 | |
| 928 | static int sysctl_follow_link(struct ctl_table_header **phead, |
| 929 | struct ctl_table **pentry, struct nsproxy *namespaces) |
| 930 | { |
| 931 | struct ctl_table_header *head; |
| 932 | struct ctl_table_root *root; |
| 933 | struct ctl_table_set *set; |
| 934 | struct ctl_table *entry; |
| 935 | struct ctl_dir *dir; |
| 936 | int ret; |
| 937 | |
| 938 | /* Get out quickly if not a link */ |
| 939 | if (!S_ISLNK((*pentry)->mode)) |
| 940 | return 0; |
| 941 | |
| 942 | ret = 0; |
| 943 | spin_lock(&sysctl_lock); |
| 944 | root = (*pentry)->data; |
| 945 | set = lookup_header_set(root, namespaces); |
| 946 | dir = xlate_dir(set, (*phead)->parent); |
| 947 | if (IS_ERR(dir)) |
| 948 | ret = PTR_ERR(dir); |
| 949 | else { |
| 950 | const char *procname = (*pentry)->procname; |
| 951 | head = NULL; |
| 952 | entry = find_entry(&head, dir, procname, strlen(procname)); |
| 953 | ret = -ENOENT; |
| 954 | if (entry && use_table(head)) { |
| 955 | unuse_table(*phead); |
| 956 | *phead = head; |
| 957 | *pentry = entry; |
| 958 | ret = 0; |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | spin_unlock(&sysctl_lock); |
| 963 | return ret; |
| 964 | } |
| 965 | |
Eric W. Biederman | 7c60c48 | 2012-01-21 13:34:05 -0800 | [diff] [blame] | 966 | static int sysctl_err(const char *path, struct ctl_table *table, char *fmt, ...) |
| 967 | { |
| 968 | struct va_format vaf; |
| 969 | va_list args; |
| 970 | |
| 971 | va_start(args, fmt); |
| 972 | vaf.fmt = fmt; |
| 973 | vaf.va = &args; |
| 974 | |
| 975 | printk(KERN_ERR "sysctl table check failed: %s/%s %pV\n", |
| 976 | path, table->procname, &vaf); |
| 977 | |
| 978 | va_end(args); |
| 979 | return -EINVAL; |
| 980 | } |
| 981 | |
| 982 | static int sysctl_check_table(const char *path, struct ctl_table *table) |
| 983 | { |
| 984 | int err = 0; |
| 985 | for (; table->procname; table++) { |
| 986 | if (table->child) |
| 987 | err = sysctl_err(path, table, "Not a file"); |
| 988 | |
| 989 | if ((table->proc_handler == proc_dostring) || |
| 990 | (table->proc_handler == proc_dointvec) || |
| 991 | (table->proc_handler == proc_dointvec_minmax) || |
| 992 | (table->proc_handler == proc_dointvec_jiffies) || |
| 993 | (table->proc_handler == proc_dointvec_userhz_jiffies) || |
| 994 | (table->proc_handler == proc_dointvec_ms_jiffies) || |
| 995 | (table->proc_handler == proc_doulongvec_minmax) || |
| 996 | (table->proc_handler == proc_doulongvec_ms_jiffies_minmax)) { |
| 997 | if (!table->data) |
| 998 | err = sysctl_err(path, table, "No data"); |
| 999 | if (!table->maxlen) |
| 1000 | err = sysctl_err(path, table, "No maxlen"); |
| 1001 | } |
| 1002 | if (!table->proc_handler) |
| 1003 | err = sysctl_err(path, table, "No proc_handler"); |
| 1004 | |
| 1005 | if ((table->mode & (S_IRUGO|S_IWUGO)) != table->mode) |
| 1006 | err = sysctl_err(path, table, "bogus .mode 0%o", |
| 1007 | table->mode); |
| 1008 | } |
| 1009 | return err; |
| 1010 | } |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1011 | |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 1012 | static struct ctl_table_header *new_links(struct ctl_dir *dir, struct ctl_table *table, |
| 1013 | struct ctl_table_root *link_root) |
| 1014 | { |
| 1015 | struct ctl_table *link_table, *entry, *link; |
| 1016 | struct ctl_table_header *links; |
Eric W. Biederman | ac13ac6 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 1017 | struct ctl_node *node; |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 1018 | char *link_name; |
| 1019 | int nr_entries, name_bytes; |
| 1020 | |
| 1021 | name_bytes = 0; |
| 1022 | nr_entries = 0; |
| 1023 | for (entry = table; entry->procname; entry++) { |
| 1024 | nr_entries++; |
| 1025 | name_bytes += strlen(entry->procname) + 1; |
| 1026 | } |
| 1027 | |
| 1028 | links = kzalloc(sizeof(struct ctl_table_header) + |
Eric W. Biederman | ac13ac6 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 1029 | sizeof(struct ctl_node)*nr_entries + |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 1030 | sizeof(struct ctl_table)*(nr_entries + 1) + |
| 1031 | name_bytes, |
| 1032 | GFP_KERNEL); |
| 1033 | |
| 1034 | if (!links) |
| 1035 | return NULL; |
| 1036 | |
Eric W. Biederman | ac13ac6 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 1037 | node = (struct ctl_node *)(links + 1); |
| 1038 | link_table = (struct ctl_table *)(node + nr_entries); |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 1039 | link_name = (char *)&link_table[nr_entries + 1]; |
| 1040 | |
| 1041 | for (link = link_table, entry = table; entry->procname; link++, entry++) { |
| 1042 | int len = strlen(entry->procname) + 1; |
| 1043 | memcpy(link_name, entry->procname, len); |
| 1044 | link->procname = link_name; |
| 1045 | link->mode = S_IFLNK|S_IRWXUGO; |
| 1046 | link->data = link_root; |
| 1047 | link_name += len; |
| 1048 | } |
Eric W. Biederman | ac13ac6 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 1049 | init_header(links, dir->header.root, dir->header.set, node, link_table); |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 1050 | links->nreg = nr_entries; |
| 1051 | |
| 1052 | return links; |
| 1053 | } |
| 1054 | |
| 1055 | static bool get_links(struct ctl_dir *dir, |
| 1056 | struct ctl_table *table, struct ctl_table_root *link_root) |
| 1057 | { |
| 1058 | struct ctl_table_header *head; |
| 1059 | struct ctl_table *entry, *link; |
| 1060 | |
| 1061 | /* Are there links available for every entry in table? */ |
| 1062 | for (entry = table; entry->procname; entry++) { |
| 1063 | const char *procname = entry->procname; |
| 1064 | link = find_entry(&head, dir, procname, strlen(procname)); |
| 1065 | if (!link) |
| 1066 | return false; |
| 1067 | if (S_ISDIR(link->mode) && S_ISDIR(entry->mode)) |
| 1068 | continue; |
| 1069 | if (S_ISLNK(link->mode) && (link->data == link_root)) |
| 1070 | continue; |
| 1071 | return false; |
| 1072 | } |
| 1073 | |
| 1074 | /* The checks passed. Increase the registration count on the links */ |
| 1075 | for (entry = table; entry->procname; entry++) { |
| 1076 | const char *procname = entry->procname; |
| 1077 | link = find_entry(&head, dir, procname, strlen(procname)); |
| 1078 | head->nreg++; |
| 1079 | } |
| 1080 | return true; |
| 1081 | } |
| 1082 | |
| 1083 | static int insert_links(struct ctl_table_header *head) |
| 1084 | { |
| 1085 | struct ctl_table_set *root_set = &sysctl_table_root.default_set; |
| 1086 | struct ctl_dir *core_parent = NULL; |
| 1087 | struct ctl_table_header *links; |
| 1088 | int err; |
| 1089 | |
| 1090 | if (head->set == root_set) |
| 1091 | return 0; |
| 1092 | |
| 1093 | core_parent = xlate_dir(root_set, head->parent); |
| 1094 | if (IS_ERR(core_parent)) |
| 1095 | return 0; |
| 1096 | |
| 1097 | if (get_links(core_parent, head->ctl_table, head->root)) |
| 1098 | return 0; |
| 1099 | |
| 1100 | core_parent->header.nreg++; |
| 1101 | spin_unlock(&sysctl_lock); |
| 1102 | |
| 1103 | links = new_links(core_parent, head->ctl_table, head->root); |
| 1104 | |
| 1105 | spin_lock(&sysctl_lock); |
| 1106 | err = -ENOMEM; |
| 1107 | if (!links) |
| 1108 | goto out; |
| 1109 | |
| 1110 | err = 0; |
| 1111 | if (get_links(core_parent, head->ctl_table, head->root)) { |
| 1112 | kfree(links); |
| 1113 | goto out; |
| 1114 | } |
| 1115 | |
| 1116 | err = insert_header(core_parent, links); |
| 1117 | if (err) |
| 1118 | kfree(links); |
| 1119 | out: |
| 1120 | drop_sysctl_table(&core_parent->header); |
| 1121 | return err; |
| 1122 | } |
| 1123 | |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1124 | /** |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1125 | * __register_sysctl_table - register a leaf sysctl table |
Eric W. Biederman | 60a47a2 | 2012-01-08 00:02:37 -0800 | [diff] [blame] | 1126 | * @set: Sysctl tree to register on |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1127 | * @path: The path to the directory the sysctl table is in. |
| 1128 | * @table: the top-level table structure |
| 1129 | * |
| 1130 | * Register a sysctl table hierarchy. @table should be a filled in ctl_table |
| 1131 | * array. A completely 0 filled entry terminates the table. |
| 1132 | * |
| 1133 | * The members of the &struct ctl_table structure are used as follows: |
| 1134 | * |
| 1135 | * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not |
| 1136 | * enter a sysctl file |
| 1137 | * |
| 1138 | * data - a pointer to data for use by proc_handler |
| 1139 | * |
| 1140 | * maxlen - the maximum size in bytes of the data |
| 1141 | * |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1142 | * mode - the file permissions for the /proc/sys file |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1143 | * |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1144 | * child - must be %NULL. |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1145 | * |
| 1146 | * proc_handler - the text handler routine (described below) |
| 1147 | * |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1148 | * extra1, extra2 - extra pointers usable by the proc handler routines |
| 1149 | * |
| 1150 | * Leaf nodes in the sysctl tree will be represented by a single file |
| 1151 | * under /proc; non-leaf nodes will be represented by directories. |
| 1152 | * |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1153 | * There must be a proc_handler routine for any terminal nodes. |
| 1154 | * Several default handlers are available to cover common cases - |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1155 | * |
| 1156 | * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(), |
| 1157 | * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(), |
| 1158 | * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax() |
| 1159 | * |
| 1160 | * It is the handler's job to read the input buffer from user memory |
| 1161 | * and process it. The handler should return 0 on success. |
| 1162 | * |
| 1163 | * This routine returns %NULL on a failure to register, and a pointer |
| 1164 | * to the table header on success. |
| 1165 | */ |
Eric W. Biederman | 6e9d516 | 2012-01-21 10:26:26 -0800 | [diff] [blame] | 1166 | struct ctl_table_header *__register_sysctl_table( |
Eric W. Biederman | 60a47a2 | 2012-01-08 00:02:37 -0800 | [diff] [blame] | 1167 | struct ctl_table_set *set, |
Eric W. Biederman | 6e9d516 | 2012-01-21 10:26:26 -0800 | [diff] [blame] | 1168 | const char *path, struct ctl_table *table) |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1169 | { |
Eric W. Biederman | 60a47a2 | 2012-01-08 00:02:37 -0800 | [diff] [blame] | 1170 | struct ctl_table_root *root = set->dir.header.root; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1171 | struct ctl_table_header *header; |
Eric W. Biederman | 6e9d516 | 2012-01-21 10:26:26 -0800 | [diff] [blame] | 1172 | const char *name, *nextname; |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 1173 | struct ctl_dir *dir; |
Eric W. Biederman | ac13ac6 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 1174 | struct ctl_table *entry; |
| 1175 | struct ctl_node *node; |
| 1176 | int nr_entries = 0; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1177 | |
Eric W. Biederman | ac13ac6 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 1178 | for (entry = table; entry->procname; entry++) |
| 1179 | nr_entries++; |
| 1180 | |
| 1181 | header = kzalloc(sizeof(struct ctl_table_header) + |
| 1182 | sizeof(struct ctl_node)*nr_entries, GFP_KERNEL); |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1183 | if (!header) |
| 1184 | return NULL; |
| 1185 | |
Eric W. Biederman | ac13ac6 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 1186 | node = (struct ctl_node *)(header + 1); |
| 1187 | init_header(header, root, set, node, table); |
Eric W. Biederman | 7c60c48 | 2012-01-21 13:34:05 -0800 | [diff] [blame] | 1188 | if (sysctl_check_table(path, table)) |
| 1189 | goto fail; |
Eric W. Biederman | 8d6ecfc | 2012-01-06 11:55:30 -0800 | [diff] [blame] | 1190 | |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1191 | spin_lock(&sysctl_lock); |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 1192 | dir = &set->dir; |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 1193 | dir->header.nreg++; |
| 1194 | spin_unlock(&sysctl_lock); |
| 1195 | |
| 1196 | /* Find the directory for the ctl_table */ |
| 1197 | for (name = path; name; name = nextname) { |
| 1198 | int namelen; |
| 1199 | nextname = strchr(name, '/'); |
| 1200 | if (nextname) { |
| 1201 | namelen = nextname - name; |
| 1202 | nextname++; |
| 1203 | } else { |
| 1204 | namelen = strlen(name); |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1205 | } |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 1206 | if (namelen == 0) |
| 1207 | continue; |
| 1208 | |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 1209 | dir = get_subdir(dir, name, namelen); |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 1210 | if (IS_ERR(dir)) |
| 1211 | goto fail; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1212 | } |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 1213 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 1214 | spin_lock(&sysctl_lock); |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 1215 | if (insert_header(dir, header)) |
| 1216 | goto fail_put_dir_locked; |
| 1217 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 1218 | drop_sysctl_table(&dir->header); |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1219 | spin_unlock(&sysctl_lock); |
| 1220 | |
| 1221 | return header; |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 1222 | |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 1223 | fail_put_dir_locked: |
| 1224 | drop_sysctl_table(&dir->header); |
Eric W. Biederman | 7c60c48 | 2012-01-21 13:34:05 -0800 | [diff] [blame] | 1225 | spin_unlock(&sysctl_lock); |
| 1226 | fail: |
| 1227 | kfree(header); |
| 1228 | dump_stack(); |
| 1229 | return NULL; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1230 | } |
| 1231 | |
Eric W. Biederman | fea478d | 2012-01-20 21:47:03 -0800 | [diff] [blame] | 1232 | /** |
| 1233 | * register_sysctl - register a sysctl table |
| 1234 | * @path: The path to the directory the sysctl table is in. |
| 1235 | * @table: the table structure |
| 1236 | * |
| 1237 | * Register a sysctl table. @table should be a filled in ctl_table |
| 1238 | * array. A completely 0 filled entry terminates the table. |
| 1239 | * |
| 1240 | * See __register_sysctl_table for more details. |
| 1241 | */ |
| 1242 | struct ctl_table_header *register_sysctl(const char *path, struct ctl_table *table) |
| 1243 | { |
| 1244 | return __register_sysctl_table(&sysctl_table_root.default_set, |
| 1245 | path, table); |
| 1246 | } |
| 1247 | EXPORT_SYMBOL(register_sysctl); |
| 1248 | |
Eric W. Biederman | 6e9d516 | 2012-01-21 10:26:26 -0800 | [diff] [blame] | 1249 | static char *append_path(const char *path, char *pos, const char *name) |
| 1250 | { |
| 1251 | int namelen; |
| 1252 | namelen = strlen(name); |
| 1253 | if (((pos - path) + namelen + 2) >= PATH_MAX) |
| 1254 | return NULL; |
| 1255 | memcpy(pos, name, namelen); |
| 1256 | pos[namelen] = '/'; |
| 1257 | pos[namelen + 1] = '\0'; |
| 1258 | pos += namelen + 1; |
| 1259 | return pos; |
| 1260 | } |
| 1261 | |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1262 | static int count_subheaders(struct ctl_table *table) |
| 1263 | { |
| 1264 | int has_files = 0; |
| 1265 | int nr_subheaders = 0; |
| 1266 | struct ctl_table *entry; |
| 1267 | |
| 1268 | /* special case: no directory and empty directory */ |
| 1269 | if (!table || !table->procname) |
| 1270 | return 1; |
| 1271 | |
| 1272 | for (entry = table; entry->procname; entry++) { |
| 1273 | if (entry->child) |
| 1274 | nr_subheaders += count_subheaders(entry->child); |
| 1275 | else |
| 1276 | has_files = 1; |
| 1277 | } |
| 1278 | return nr_subheaders + has_files; |
| 1279 | } |
| 1280 | |
| 1281 | static int register_leaf_sysctl_tables(const char *path, char *pos, |
Eric W. Biederman | 60a47a2 | 2012-01-08 00:02:37 -0800 | [diff] [blame] | 1282 | struct ctl_table_header ***subheader, struct ctl_table_set *set, |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1283 | struct ctl_table *table) |
| 1284 | { |
| 1285 | struct ctl_table *ctl_table_arg = NULL; |
| 1286 | struct ctl_table *entry, *files; |
| 1287 | int nr_files = 0; |
| 1288 | int nr_dirs = 0; |
| 1289 | int err = -ENOMEM; |
| 1290 | |
| 1291 | for (entry = table; entry->procname; entry++) { |
| 1292 | if (entry->child) |
| 1293 | nr_dirs++; |
| 1294 | else |
| 1295 | nr_files++; |
| 1296 | } |
| 1297 | |
| 1298 | files = table; |
| 1299 | /* If there are mixed files and directories we need a new table */ |
| 1300 | if (nr_dirs && nr_files) { |
| 1301 | struct ctl_table *new; |
| 1302 | files = kzalloc(sizeof(struct ctl_table) * (nr_files + 1), |
| 1303 | GFP_KERNEL); |
| 1304 | if (!files) |
| 1305 | goto out; |
| 1306 | |
| 1307 | ctl_table_arg = files; |
| 1308 | for (new = files, entry = table; entry->procname; entry++) { |
| 1309 | if (entry->child) |
| 1310 | continue; |
| 1311 | *new = *entry; |
| 1312 | new++; |
| 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | /* Register everything except a directory full of subdirectories */ |
| 1317 | if (nr_files || !nr_dirs) { |
| 1318 | struct ctl_table_header *header; |
Eric W. Biederman | 60a47a2 | 2012-01-08 00:02:37 -0800 | [diff] [blame] | 1319 | header = __register_sysctl_table(set, path, files); |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1320 | if (!header) { |
| 1321 | kfree(ctl_table_arg); |
| 1322 | goto out; |
| 1323 | } |
| 1324 | |
| 1325 | /* Remember if we need to free the file table */ |
| 1326 | header->ctl_table_arg = ctl_table_arg; |
| 1327 | **subheader = header; |
| 1328 | (*subheader)++; |
| 1329 | } |
| 1330 | |
| 1331 | /* Recurse into the subdirectories. */ |
| 1332 | for (entry = table; entry->procname; entry++) { |
| 1333 | char *child_pos; |
| 1334 | |
| 1335 | if (!entry->child) |
| 1336 | continue; |
| 1337 | |
| 1338 | err = -ENAMETOOLONG; |
| 1339 | child_pos = append_path(path, pos, entry->procname); |
| 1340 | if (!child_pos) |
| 1341 | goto out; |
| 1342 | |
| 1343 | err = register_leaf_sysctl_tables(path, child_pos, subheader, |
Eric W. Biederman | 60a47a2 | 2012-01-08 00:02:37 -0800 | [diff] [blame] | 1344 | set, entry->child); |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1345 | pos[0] = '\0'; |
| 1346 | if (err) |
| 1347 | goto out; |
| 1348 | } |
| 1349 | err = 0; |
| 1350 | out: |
| 1351 | /* On failure our caller will unregister all registered subheaders */ |
| 1352 | return err; |
| 1353 | } |
| 1354 | |
Eric W. Biederman | 6e9d516 | 2012-01-21 10:26:26 -0800 | [diff] [blame] | 1355 | /** |
| 1356 | * __register_sysctl_paths - register a sysctl table hierarchy |
Eric W. Biederman | 60a47a2 | 2012-01-08 00:02:37 -0800 | [diff] [blame] | 1357 | * @set: Sysctl tree to register on |
Eric W. Biederman | 6e9d516 | 2012-01-21 10:26:26 -0800 | [diff] [blame] | 1358 | * @path: The path to the directory the sysctl table is in. |
| 1359 | * @table: the top-level table structure |
| 1360 | * |
| 1361 | * Register a sysctl table hierarchy. @table should be a filled in ctl_table |
| 1362 | * array. A completely 0 filled entry terminates the table. |
| 1363 | * |
| 1364 | * See __register_sysctl_table for more details. |
| 1365 | */ |
| 1366 | struct ctl_table_header *__register_sysctl_paths( |
Eric W. Biederman | 60a47a2 | 2012-01-08 00:02:37 -0800 | [diff] [blame] | 1367 | struct ctl_table_set *set, |
Eric W. Biederman | 6e9d516 | 2012-01-21 10:26:26 -0800 | [diff] [blame] | 1368 | const struct ctl_path *path, struct ctl_table *table) |
| 1369 | { |
Eric W. Biederman | ec6a526 | 2012-01-21 12:35:23 -0800 | [diff] [blame] | 1370 | struct ctl_table *ctl_table_arg = table; |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1371 | int nr_subheaders = count_subheaders(table); |
| 1372 | struct ctl_table_header *header = NULL, **subheaders, **subheader; |
Eric W. Biederman | 6e9d516 | 2012-01-21 10:26:26 -0800 | [diff] [blame] | 1373 | const struct ctl_path *component; |
| 1374 | char *new_path, *pos; |
| 1375 | |
| 1376 | pos = new_path = kmalloc(PATH_MAX, GFP_KERNEL); |
| 1377 | if (!new_path) |
| 1378 | return NULL; |
| 1379 | |
| 1380 | pos[0] = '\0'; |
| 1381 | for (component = path; component->procname; component++) { |
| 1382 | pos = append_path(new_path, pos, component->procname); |
| 1383 | if (!pos) |
| 1384 | goto out; |
| 1385 | } |
Eric W. Biederman | ec6a526 | 2012-01-21 12:35:23 -0800 | [diff] [blame] | 1386 | while (table->procname && table->child && !table[1].procname) { |
| 1387 | pos = append_path(new_path, pos, table->procname); |
| 1388 | if (!pos) |
| 1389 | goto out; |
| 1390 | table = table->child; |
| 1391 | } |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1392 | if (nr_subheaders == 1) { |
Eric W. Biederman | 60a47a2 | 2012-01-08 00:02:37 -0800 | [diff] [blame] | 1393 | header = __register_sysctl_table(set, new_path, table); |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1394 | if (header) |
| 1395 | header->ctl_table_arg = ctl_table_arg; |
| 1396 | } else { |
| 1397 | header = kzalloc(sizeof(*header) + |
| 1398 | sizeof(*subheaders)*nr_subheaders, GFP_KERNEL); |
| 1399 | if (!header) |
| 1400 | goto out; |
| 1401 | |
| 1402 | subheaders = (struct ctl_table_header **) (header + 1); |
| 1403 | subheader = subheaders; |
Eric W. Biederman | ec6a526 | 2012-01-21 12:35:23 -0800 | [diff] [blame] | 1404 | header->ctl_table_arg = ctl_table_arg; |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1405 | |
| 1406 | if (register_leaf_sysctl_tables(new_path, pos, &subheader, |
Eric W. Biederman | 60a47a2 | 2012-01-08 00:02:37 -0800 | [diff] [blame] | 1407 | set, table)) |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1408 | goto err_register_leaves; |
| 1409 | } |
| 1410 | |
Eric W. Biederman | 6e9d516 | 2012-01-21 10:26:26 -0800 | [diff] [blame] | 1411 | out: |
| 1412 | kfree(new_path); |
| 1413 | return header; |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1414 | |
| 1415 | err_register_leaves: |
| 1416 | while (subheader > subheaders) { |
| 1417 | struct ctl_table_header *subh = *(--subheader); |
| 1418 | struct ctl_table *table = subh->ctl_table_arg; |
| 1419 | unregister_sysctl_table(subh); |
| 1420 | kfree(table); |
| 1421 | } |
| 1422 | kfree(header); |
| 1423 | header = NULL; |
| 1424 | goto out; |
Eric W. Biederman | 6e9d516 | 2012-01-21 10:26:26 -0800 | [diff] [blame] | 1425 | } |
| 1426 | |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1427 | /** |
| 1428 | * register_sysctl_table_path - register a sysctl table hierarchy |
| 1429 | * @path: The path to the directory the sysctl table is in. |
| 1430 | * @table: the top-level table structure |
| 1431 | * |
| 1432 | * Register a sysctl table hierarchy. @table should be a filled in ctl_table |
| 1433 | * array. A completely 0 filled entry terminates the table. |
| 1434 | * |
| 1435 | * See __register_sysctl_paths for more details. |
| 1436 | */ |
| 1437 | struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path, |
| 1438 | struct ctl_table *table) |
| 1439 | { |
Eric W. Biederman | 60a47a2 | 2012-01-08 00:02:37 -0800 | [diff] [blame] | 1440 | return __register_sysctl_paths(&sysctl_table_root.default_set, |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1441 | path, table); |
| 1442 | } |
| 1443 | EXPORT_SYMBOL(register_sysctl_paths); |
| 1444 | |
| 1445 | /** |
| 1446 | * register_sysctl_table - register a sysctl table hierarchy |
| 1447 | * @table: the top-level table structure |
| 1448 | * |
| 1449 | * Register a sysctl table hierarchy. @table should be a filled in ctl_table |
| 1450 | * array. A completely 0 filled entry terminates the table. |
| 1451 | * |
| 1452 | * See register_sysctl_paths for more details. |
| 1453 | */ |
| 1454 | struct ctl_table_header *register_sysctl_table(struct ctl_table *table) |
| 1455 | { |
| 1456 | static const struct ctl_path null_path[] = { {} }; |
| 1457 | |
| 1458 | return register_sysctl_paths(null_path, table); |
| 1459 | } |
| 1460 | EXPORT_SYMBOL(register_sysctl_table); |
| 1461 | |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 1462 | static void put_links(struct ctl_table_header *header) |
| 1463 | { |
| 1464 | struct ctl_table_set *root_set = &sysctl_table_root.default_set; |
| 1465 | struct ctl_table_root *root = header->root; |
| 1466 | struct ctl_dir *parent = header->parent; |
| 1467 | struct ctl_dir *core_parent; |
| 1468 | struct ctl_table *entry; |
| 1469 | |
| 1470 | if (header->set == root_set) |
| 1471 | return; |
| 1472 | |
| 1473 | core_parent = xlate_dir(root_set, parent); |
| 1474 | if (IS_ERR(core_parent)) |
| 1475 | return; |
| 1476 | |
| 1477 | for (entry = header->ctl_table; entry->procname; entry++) { |
| 1478 | struct ctl_table_header *link_head; |
| 1479 | struct ctl_table *link; |
| 1480 | const char *name = entry->procname; |
| 1481 | |
| 1482 | link = find_entry(&link_head, core_parent, name, strlen(name)); |
| 1483 | if (link && |
| 1484 | ((S_ISDIR(link->mode) && S_ISDIR(entry->mode)) || |
| 1485 | (S_ISLNK(link->mode) && (link->data == root)))) { |
| 1486 | drop_sysctl_table(link_head); |
| 1487 | } |
| 1488 | else { |
| 1489 | printk(KERN_ERR "sysctl link missing during unregister: "); |
| 1490 | sysctl_print_dir(parent); |
| 1491 | printk(KERN_CONT "/%s\n", name); |
| 1492 | } |
| 1493 | } |
| 1494 | } |
| 1495 | |
Eric W. Biederman | 938aaa4 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 1496 | static void drop_sysctl_table(struct ctl_table_header *header) |
| 1497 | { |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 1498 | struct ctl_dir *parent = header->parent; |
| 1499 | |
Eric W. Biederman | 938aaa4 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 1500 | if (--header->nreg) |
| 1501 | return; |
| 1502 | |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 1503 | put_links(header); |
Eric W. Biederman | 938aaa4 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 1504 | start_unregistering(header); |
Eric W. Biederman | 938aaa4 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 1505 | if (!--header->count) |
| 1506 | kfree_rcu(header, rcu); |
Eric W. Biederman | 7ec66d0 | 2011-12-29 08:24:29 -0800 | [diff] [blame] | 1507 | |
| 1508 | if (parent) |
| 1509 | drop_sysctl_table(&parent->header); |
Eric W. Biederman | 938aaa4 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 1510 | } |
| 1511 | |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1512 | /** |
| 1513 | * unregister_sysctl_table - unregister a sysctl table hierarchy |
| 1514 | * @header: the header returned from register_sysctl_table |
| 1515 | * |
| 1516 | * Unregisters the sysctl table and all children. proc entries may not |
| 1517 | * actually be removed until they are no longer used by anyone. |
| 1518 | */ |
| 1519 | void unregister_sysctl_table(struct ctl_table_header * header) |
| 1520 | { |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1521 | int nr_subheaders; |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1522 | might_sleep(); |
| 1523 | |
| 1524 | if (header == NULL) |
| 1525 | return; |
| 1526 | |
Eric W. Biederman | f728019 | 2012-01-22 18:22:05 -0800 | [diff] [blame] | 1527 | nr_subheaders = count_subheaders(header->ctl_table_arg); |
| 1528 | if (unlikely(nr_subheaders > 1)) { |
| 1529 | struct ctl_table_header **subheaders; |
| 1530 | int i; |
| 1531 | |
| 1532 | subheaders = (struct ctl_table_header **)(header + 1); |
| 1533 | for (i = nr_subheaders -1; i >= 0; i--) { |
| 1534 | struct ctl_table_header *subh = subheaders[i]; |
| 1535 | struct ctl_table *table = subh->ctl_table_arg; |
| 1536 | unregister_sysctl_table(subh); |
| 1537 | kfree(table); |
| 1538 | } |
| 1539 | kfree(header); |
| 1540 | return; |
| 1541 | } |
| 1542 | |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1543 | spin_lock(&sysctl_lock); |
Eric W. Biederman | 938aaa4 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 1544 | drop_sysctl_table(header); |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1545 | spin_unlock(&sysctl_lock); |
| 1546 | } |
| 1547 | EXPORT_SYMBOL(unregister_sysctl_table); |
| 1548 | |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 1549 | void setup_sysctl_set(struct ctl_table_set *set, |
Eric W. Biederman | 9eb47c2 | 2012-01-22 21:26:00 -0800 | [diff] [blame] | 1550 | struct ctl_table_root *root, |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1551 | int (*is_seen)(struct ctl_table_set *)) |
| 1552 | { |
Dan Carpenter | 1347440 | 2012-01-30 16:40:29 +0300 | [diff] [blame] | 1553 | memset(set, 0, sizeof(*set)); |
Eric W. Biederman | 0e47c99 | 2012-01-07 23:24:30 -0800 | [diff] [blame] | 1554 | set->is_seen = is_seen; |
Eric W. Biederman | ac13ac6 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 1555 | init_header(&set->dir.header, root, set, NULL, root_table); |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1556 | } |
| 1557 | |
Eric W. Biederman | 97324cd | 2012-01-09 22:19:13 -0800 | [diff] [blame] | 1558 | void retire_sysctl_set(struct ctl_table_set *set) |
| 1559 | { |
Eric W. Biederman | ac13ac6 | 2012-01-09 17:24:30 -0800 | [diff] [blame] | 1560 | WARN_ON(!RB_EMPTY_ROOT(&set->dir.root)); |
Eric W. Biederman | 97324cd | 2012-01-09 22:19:13 -0800 | [diff] [blame] | 1561 | } |
Eric W. Biederman | 1f87f0b | 2012-01-06 04:07:15 -0800 | [diff] [blame] | 1562 | |
Alexey Dobriyan | 1e0edd3 | 2008-10-17 05:07:44 +0400 | [diff] [blame] | 1563 | int __init proc_sys_init(void) |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 1564 | { |
Alexey Dobriyan | e167523 | 2008-10-03 00:23:32 +0400 | [diff] [blame] | 1565 | struct proc_dir_entry *proc_sys_root; |
| 1566 | |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 1567 | proc_sys_root = proc_mkdir("sys", NULL); |
Al Viro | 9043476 | 2008-07-15 08:54:06 -0400 | [diff] [blame] | 1568 | proc_sys_root->proc_iops = &proc_sys_dir_operations; |
| 1569 | proc_sys_root->proc_fops = &proc_sys_dir_file_operations; |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 1570 | proc_sys_root->nlink = 0; |
Eric W. Biederman | de4e83bd | 2012-01-06 03:34:20 -0800 | [diff] [blame] | 1571 | |
| 1572 | return sysctl_init(); |
Eric W. Biederman | 77b14db | 2007-02-14 00:34:12 -0800 | [diff] [blame] | 1573 | } |