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