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