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