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