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