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