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