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