4 #include <linux/init.h>
5 #include <linux/sysctl.h>
6 #include <linux/poll.h>
7 #include <linux/proc_fs.h>
8 #include <linux/security.h>
9 #include <linux/namei.h>
10 #include <linux/module.h>
13 static const struct dentry_operations proc_sys_dentry_operations
;
14 static const struct file_operations proc_sys_file_operations
;
15 static const struct inode_operations proc_sys_inode_operations
;
16 static const struct file_operations proc_sys_dir_file_operations
;
17 static const struct inode_operations proc_sys_dir_operations
;
19 void proc_sys_poll_notify(struct ctl_table_poll
*poll
)
24 atomic_inc(&poll
->event
);
25 wake_up_interruptible(&poll
->wait
);
28 static struct ctl_table root_table
[] = {
31 .mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
,
35 static struct ctl_table_root sysctl_table_root
= {
36 .default_set
.dir
.header
= {
39 .ctl_table
= root_table
}},
40 .ctl_table_arg
= root_table
,
41 .root
= &sysctl_table_root
,
42 .set
= &sysctl_table_root
.default_set
,
46 static DEFINE_SPINLOCK(sysctl_lock
);
48 static void drop_sysctl_table(struct ctl_table_header
*header
);
49 static int sysctl_follow_link(struct ctl_table_header
**phead
,
50 struct ctl_table
**pentry
, struct nsproxy
*namespaces
);
51 static int insert_links(struct ctl_table_header
*head
);
52 static void put_links(struct ctl_table_header
*header
);
54 static void sysctl_print_dir(struct ctl_dir
*dir
)
56 if (dir
->header
.parent
)
57 sysctl_print_dir(dir
->header
.parent
);
58 printk(KERN_CONT
"%s/", dir
->header
.ctl_table
[0].procname
);
61 static int namecmp(const char *name1
, int len1
, const char *name2
, int len2
)
70 cmp
= memcmp(name1
, name2
, minlen
);
76 static struct ctl_table
*find_entry(struct ctl_table_header
**phead
,
77 struct ctl_dir
*dir
, const char *name
, int namelen
)
79 struct ctl_table_header
*head
;
80 struct ctl_table
*entry
;
81 struct rb_node
*node
= dir
->root
.rb_node
;
85 struct ctl_node
*ctl_node
;
89 ctl_node
= rb_entry(node
, struct ctl_node
, node
);
90 head
= ctl_node
->header
;
91 entry
= &head
->ctl_table
[ctl_node
- head
->node
];
92 procname
= entry
->procname
;
94 cmp
= namecmp(name
, namelen
, procname
, strlen(procname
));
98 node
= node
->rb_right
;
107 static int insert_entry(struct ctl_table_header
*head
, struct ctl_table
*entry
)
109 struct rb_node
*node
= &head
->node
[entry
- head
->ctl_table
].node
;
110 struct rb_node
**p
= &head
->parent
->root
.rb_node
;
111 struct rb_node
*parent
= NULL
;
112 const char *name
= entry
->procname
;
113 int namelen
= strlen(name
);
116 struct ctl_table_header
*parent_head
;
117 struct ctl_table
*parent_entry
;
118 struct ctl_node
*parent_node
;
119 const char *parent_name
;
123 parent_node
= rb_entry(parent
, struct ctl_node
, node
);
124 parent_head
= parent_node
->header
;
125 parent_entry
= &parent_head
->ctl_table
[parent_node
- parent_head
->node
];
126 parent_name
= parent_entry
->procname
;
128 cmp
= namecmp(name
, namelen
, parent_name
, strlen(parent_name
));
134 printk(KERN_ERR
"sysctl duplicate entry: ");
135 sysctl_print_dir(head
->parent
);
136 printk(KERN_CONT
"/%s\n", entry
->procname
);
141 rb_link_node(node
, parent
, p
);
145 static void erase_entry(struct ctl_table_header
*head
, struct ctl_table
*entry
)
147 struct rb_node
*node
= &head
->node
[entry
- head
->ctl_table
].node
;
149 rb_erase(node
, &head
->parent
->root
);
152 static void init_header(struct ctl_table_header
*head
,
153 struct ctl_table_root
*root
, struct ctl_table_set
*set
,
154 struct ctl_node
*node
, struct ctl_table
*table
)
156 head
->ctl_table
= table
;
157 head
->ctl_table_arg
= table
;
161 head
->unregistering
= NULL
;
167 struct ctl_table
*entry
;
168 for (entry
= table
; entry
->procname
; entry
++, node
++) {
169 rb_init_node(&node
->node
);
175 static void erase_header(struct ctl_table_header
*head
)
177 struct ctl_table
*entry
;
178 for (entry
= head
->ctl_table
; entry
->procname
; entry
++)
179 erase_entry(head
, entry
);
182 static int insert_header(struct ctl_dir
*dir
, struct ctl_table_header
*header
)
184 struct ctl_table
*entry
;
188 header
->parent
= dir
;
189 err
= insert_links(header
);
192 for (entry
= header
->ctl_table
; entry
->procname
; entry
++) {
193 err
= insert_entry(header
, entry
);
199 erase_header(header
);
202 header
->parent
= NULL
;
203 drop_sysctl_table(&dir
->header
);
207 /* called under sysctl_lock */
208 static int use_table(struct ctl_table_header
*p
)
210 if (unlikely(p
->unregistering
))
216 /* called under sysctl_lock */
217 static void unuse_table(struct ctl_table_header
*p
)
220 if (unlikely(p
->unregistering
))
221 complete(p
->unregistering
);
224 /* called under sysctl_lock, will reacquire if has to wait */
225 static void start_unregistering(struct ctl_table_header
*p
)
228 * if p->used is 0, nobody will ever touch that entry again;
229 * we'll eliminate all paths to it before dropping sysctl_lock
231 if (unlikely(p
->used
)) {
232 struct completion wait
;
233 init_completion(&wait
);
234 p
->unregistering
= &wait
;
235 spin_unlock(&sysctl_lock
);
236 wait_for_completion(&wait
);
237 spin_lock(&sysctl_lock
);
239 /* anything non-NULL; we'll never dereference it */
240 p
->unregistering
= ERR_PTR(-EINVAL
);
243 * do not remove from the list until nobody holds it; walking the
244 * list in do_sysctl() relies on that.
249 static void sysctl_head_get(struct ctl_table_header
*head
)
251 spin_lock(&sysctl_lock
);
253 spin_unlock(&sysctl_lock
);
256 void sysctl_head_put(struct ctl_table_header
*head
)
258 spin_lock(&sysctl_lock
);
260 kfree_rcu(head
, rcu
);
261 spin_unlock(&sysctl_lock
);
264 static struct ctl_table_header
*sysctl_head_grab(struct ctl_table_header
*head
)
268 spin_lock(&sysctl_lock
);
269 if (!use_table(head
))
270 head
= ERR_PTR(-ENOENT
);
271 spin_unlock(&sysctl_lock
);
275 static void sysctl_head_finish(struct ctl_table_header
*head
)
279 spin_lock(&sysctl_lock
);
281 spin_unlock(&sysctl_lock
);
284 static struct ctl_table_set
*
285 lookup_header_set(struct ctl_table_root
*root
, struct nsproxy
*namespaces
)
287 struct ctl_table_set
*set
= &root
->default_set
;
289 set
= root
->lookup(root
, namespaces
);
293 static struct ctl_table
*lookup_entry(struct ctl_table_header
**phead
,
295 const char *name
, int namelen
)
297 struct ctl_table_header
*head
;
298 struct ctl_table
*entry
;
300 spin_lock(&sysctl_lock
);
301 entry
= find_entry(&head
, dir
, name
, namelen
);
302 if (entry
&& use_table(head
))
306 spin_unlock(&sysctl_lock
);
310 static struct ctl_node
*first_usable_entry(struct rb_node
*node
)
312 struct ctl_node
*ctl_node
;
314 for (;node
; node
= rb_next(node
)) {
315 ctl_node
= rb_entry(node
, struct ctl_node
, node
);
316 if (use_table(ctl_node
->header
))
322 static void first_entry(struct ctl_dir
*dir
,
323 struct ctl_table_header
**phead
, struct ctl_table
**pentry
)
325 struct ctl_table_header
*head
= NULL
;
326 struct ctl_table
*entry
= NULL
;
327 struct ctl_node
*ctl_node
;
329 spin_lock(&sysctl_lock
);
330 ctl_node
= first_usable_entry(rb_first(&dir
->root
));
331 spin_unlock(&sysctl_lock
);
333 head
= ctl_node
->header
;
334 entry
= &head
->ctl_table
[ctl_node
- head
->node
];
340 static void next_entry(struct ctl_table_header
**phead
, struct ctl_table
**pentry
)
342 struct ctl_table_header
*head
= *phead
;
343 struct ctl_table
*entry
= *pentry
;
344 struct ctl_node
*ctl_node
= &head
->node
[entry
- head
->ctl_table
];
346 spin_lock(&sysctl_lock
);
349 ctl_node
= first_usable_entry(rb_next(&ctl_node
->node
));
350 spin_unlock(&sysctl_lock
);
353 head
= ctl_node
->header
;
354 entry
= &head
->ctl_table
[ctl_node
- head
->node
];
360 void register_sysctl_root(struct ctl_table_root
*root
)
365 * sysctl_perm does NOT grant the superuser all rights automatically, because
366 * some sysctl variables are readonly even to root.
369 static int test_perm(int mode
, int op
)
373 else if (in_egroup_p(0))
375 if ((op
& ~mode
& (MAY_READ
|MAY_WRITE
|MAY_EXEC
)) == 0)
380 static int sysctl_perm(struct ctl_table_root
*root
, struct ctl_table
*table
, int op
)
384 if (root
->permissions
)
385 mode
= root
->permissions(root
, current
->nsproxy
, table
);
389 return test_perm(mode
, op
);
392 static struct inode
*proc_sys_make_inode(struct super_block
*sb
,
393 struct ctl_table_header
*head
, struct ctl_table
*table
)
396 struct proc_inode
*ei
;
398 inode
= new_inode(sb
);
402 inode
->i_ino
= get_next_ino();
404 sysctl_head_get(head
);
407 ei
->sysctl_entry
= table
;
409 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
410 inode
->i_mode
= table
->mode
;
411 if (!S_ISDIR(table
->mode
)) {
412 inode
->i_mode
|= S_IFREG
;
413 inode
->i_op
= &proc_sys_inode_operations
;
414 inode
->i_fop
= &proc_sys_file_operations
;
416 inode
->i_mode
|= S_IFDIR
;
417 inode
->i_op
= &proc_sys_dir_operations
;
418 inode
->i_fop
= &proc_sys_dir_file_operations
;
424 static struct ctl_table_header
*grab_header(struct inode
*inode
)
426 struct ctl_table_header
*head
= PROC_I(inode
)->sysctl
;
428 head
= &sysctl_table_root
.default_set
.dir
.header
;
429 return sysctl_head_grab(head
);
432 static struct dentry
*proc_sys_lookup(struct inode
*dir
, struct dentry
*dentry
,
433 struct nameidata
*nd
)
435 struct ctl_table_header
*head
= grab_header(dir
);
436 struct ctl_table_header
*h
= NULL
;
437 struct qstr
*name
= &dentry
->d_name
;
440 struct dentry
*err
= ERR_PTR(-ENOENT
);
441 struct ctl_dir
*ctl_dir
;
445 return ERR_CAST(head
);
447 ctl_dir
= container_of(head
, struct ctl_dir
, header
);
449 p
= lookup_entry(&h
, ctl_dir
, name
->name
, name
->len
);
453 ret
= sysctl_follow_link(&h
, &p
, current
->nsproxy
);
458 err
= ERR_PTR(-ENOMEM
);
459 inode
= proc_sys_make_inode(dir
->i_sb
, h
? h
: head
, p
);
461 sysctl_head_finish(h
);
467 d_set_d_op(dentry
, &proc_sys_dentry_operations
);
468 d_add(dentry
, inode
);
471 sysctl_head_finish(head
);
475 static ssize_t
proc_sys_call_handler(struct file
*filp
, void __user
*buf
,
476 size_t count
, loff_t
*ppos
, int write
)
478 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
479 struct ctl_table_header
*head
= grab_header(inode
);
480 struct ctl_table
*table
= PROC_I(inode
)->sysctl_entry
;
485 return PTR_ERR(head
);
488 * At this point we know that the sysctl was not unregistered
489 * and won't be until we finish.
492 if (sysctl_perm(head
->root
, table
, write
? MAY_WRITE
: MAY_READ
))
495 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
497 if (!table
->proc_handler
)
500 /* careful: calling conventions are nasty here */
502 error
= table
->proc_handler(table
, write
, buf
, &res
, ppos
);
506 sysctl_head_finish(head
);
511 static ssize_t
proc_sys_read(struct file
*filp
, char __user
*buf
,
512 size_t count
, loff_t
*ppos
)
514 return proc_sys_call_handler(filp
, (void __user
*)buf
, count
, ppos
, 0);
517 static ssize_t
proc_sys_write(struct file
*filp
, const char __user
*buf
,
518 size_t count
, loff_t
*ppos
)
520 return proc_sys_call_handler(filp
, (void __user
*)buf
, count
, ppos
, 1);
523 static int proc_sys_open(struct inode
*inode
, struct file
*filp
)
525 struct ctl_table
*table
= PROC_I(inode
)->sysctl_entry
;
528 filp
->private_data
= proc_sys_poll_event(table
->poll
);
533 static unsigned int proc_sys_poll(struct file
*filp
, poll_table
*wait
)
535 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
536 struct ctl_table
*table
= PROC_I(inode
)->sysctl_entry
;
537 unsigned long event
= (unsigned long)filp
->private_data
;
538 unsigned int ret
= DEFAULT_POLLMASK
;
540 if (!table
->proc_handler
)
546 poll_wait(filp
, &table
->poll
->wait
, wait
);
548 if (event
!= atomic_read(&table
->poll
->event
)) {
549 filp
->private_data
= proc_sys_poll_event(table
->poll
);
550 ret
= POLLIN
| POLLRDNORM
| POLLERR
| POLLPRI
;
557 static int proc_sys_fill_cache(struct file
*filp
, void *dirent
,
559 struct ctl_table_header
*head
,
560 struct ctl_table
*table
)
562 struct dentry
*child
, *dir
= filp
->f_path
.dentry
;
566 unsigned type
= DT_UNKNOWN
;
568 qname
.name
= table
->procname
;
569 qname
.len
= strlen(table
->procname
);
570 qname
.hash
= full_name_hash(qname
.name
, qname
.len
);
572 child
= d_lookup(dir
, &qname
);
574 child
= d_alloc(dir
, &qname
);
576 inode
= proc_sys_make_inode(dir
->d_sb
, head
, table
);
581 d_set_d_op(child
, &proc_sys_dentry_operations
);
588 inode
= child
->d_inode
;
590 type
= inode
->i_mode
>> 12;
592 return !!filldir(dirent
, qname
.name
, qname
.len
, filp
->f_pos
, ino
, type
);
595 static int proc_sys_link_fill_cache(struct file
*filp
, void *dirent
,
597 struct ctl_table_header
*head
,
598 struct ctl_table
*table
)
601 head
= sysctl_head_grab(head
);
603 /* It is not an error if we can not follow the link ignore it */
604 err
= sysctl_follow_link(&head
, &table
, current
->nsproxy
);
608 ret
= proc_sys_fill_cache(filp
, dirent
, filldir
, head
, table
);
610 sysctl_head_finish(head
);
614 static int scan(struct ctl_table_header
*head
, ctl_table
*table
,
615 unsigned long *pos
, struct file
*file
,
616 void *dirent
, filldir_t filldir
)
620 if ((*pos
)++ < file
->f_pos
)
623 if (unlikely(S_ISLNK(table
->mode
)))
624 res
= proc_sys_link_fill_cache(file
, dirent
, filldir
, head
, table
);
626 res
= proc_sys_fill_cache(file
, dirent
, filldir
, head
, table
);
634 static int proc_sys_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
636 struct dentry
*dentry
= filp
->f_path
.dentry
;
637 struct inode
*inode
= dentry
->d_inode
;
638 struct ctl_table_header
*head
= grab_header(inode
);
639 struct ctl_table_header
*h
= NULL
;
640 struct ctl_table
*entry
;
641 struct ctl_dir
*ctl_dir
;
646 return PTR_ERR(head
);
648 ctl_dir
= container_of(head
, struct ctl_dir
, header
);
651 /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
652 if (filp
->f_pos
== 0) {
653 if (filldir(dirent
, ".", 1, filp
->f_pos
,
654 inode
->i_ino
, DT_DIR
) < 0)
658 if (filp
->f_pos
== 1) {
659 if (filldir(dirent
, "..", 2, filp
->f_pos
,
660 parent_ino(dentry
), DT_DIR
) < 0)
666 for (first_entry(ctl_dir
, &h
, &entry
); h
; next_entry(&h
, &entry
)) {
667 ret
= scan(h
, entry
, &pos
, filp
, dirent
, filldir
);
669 sysctl_head_finish(h
);
675 sysctl_head_finish(head
);
679 static int proc_sys_permission(struct inode
*inode
, int mask
)
682 * sysctl entries that are not writeable,
683 * are _NOT_ writeable, capabilities or not.
685 struct ctl_table_header
*head
;
686 struct ctl_table
*table
;
689 /* Executable files are not allowed under /proc/sys/ */
690 if ((mask
& MAY_EXEC
) && S_ISREG(inode
->i_mode
))
693 head
= grab_header(inode
);
695 return PTR_ERR(head
);
697 table
= PROC_I(inode
)->sysctl_entry
;
698 if (!table
) /* global root - r-xr-xr-x */
699 error
= mask
& MAY_WRITE
? -EACCES
: 0;
700 else /* Use the permissions on the sysctl table entry */
701 error
= sysctl_perm(head
->root
, table
, mask
& ~MAY_NOT_BLOCK
);
703 sysctl_head_finish(head
);
707 static int proc_sys_setattr(struct dentry
*dentry
, struct iattr
*attr
)
709 struct inode
*inode
= dentry
->d_inode
;
712 if (attr
->ia_valid
& (ATTR_MODE
| ATTR_UID
| ATTR_GID
))
715 error
= inode_change_ok(inode
, attr
);
719 if ((attr
->ia_valid
& ATTR_SIZE
) &&
720 attr
->ia_size
!= i_size_read(inode
)) {
721 error
= vmtruncate(inode
, attr
->ia_size
);
726 setattr_copy(inode
, attr
);
727 mark_inode_dirty(inode
);
731 static int proc_sys_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
733 struct inode
*inode
= dentry
->d_inode
;
734 struct ctl_table_header
*head
= grab_header(inode
);
735 struct ctl_table
*table
= PROC_I(inode
)->sysctl_entry
;
738 return PTR_ERR(head
);
740 generic_fillattr(inode
, stat
);
742 stat
->mode
= (stat
->mode
& S_IFMT
) | table
->mode
;
744 sysctl_head_finish(head
);
748 static const struct file_operations proc_sys_file_operations
= {
749 .open
= proc_sys_open
,
750 .poll
= proc_sys_poll
,
751 .read
= proc_sys_read
,
752 .write
= proc_sys_write
,
753 .llseek
= default_llseek
,
756 static const struct file_operations proc_sys_dir_file_operations
= {
757 .read
= generic_read_dir
,
758 .readdir
= proc_sys_readdir
,
759 .llseek
= generic_file_llseek
,
762 static const struct inode_operations proc_sys_inode_operations
= {
763 .permission
= proc_sys_permission
,
764 .setattr
= proc_sys_setattr
,
765 .getattr
= proc_sys_getattr
,
768 static const struct inode_operations proc_sys_dir_operations
= {
769 .lookup
= proc_sys_lookup
,
770 .permission
= proc_sys_permission
,
771 .setattr
= proc_sys_setattr
,
772 .getattr
= proc_sys_getattr
,
775 static int proc_sys_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
777 if (nd
->flags
& LOOKUP_RCU
)
779 return !PROC_I(dentry
->d_inode
)->sysctl
->unregistering
;
782 static int proc_sys_delete(const struct dentry
*dentry
)
784 return !!PROC_I(dentry
->d_inode
)->sysctl
->unregistering
;
787 static int sysctl_is_seen(struct ctl_table_header
*p
)
789 struct ctl_table_set
*set
= p
->set
;
791 spin_lock(&sysctl_lock
);
792 if (p
->unregistering
)
794 else if (!set
->is_seen
)
797 res
= set
->is_seen(set
);
798 spin_unlock(&sysctl_lock
);
802 static int proc_sys_compare(const struct dentry
*parent
,
803 const struct inode
*pinode
,
804 const struct dentry
*dentry
, const struct inode
*inode
,
805 unsigned int len
, const char *str
, const struct qstr
*name
)
807 struct ctl_table_header
*head
;
808 /* Although proc doesn't have negative dentries, rcu-walk means
809 * that inode here can be NULL */
810 /* AV: can it, indeed? */
813 if (name
->len
!= len
)
815 if (memcmp(name
->name
, str
, len
))
817 head
= rcu_dereference(PROC_I(inode
)->sysctl
);
818 return !head
|| !sysctl_is_seen(head
);
821 static const struct dentry_operations proc_sys_dentry_operations
= {
822 .d_revalidate
= proc_sys_revalidate
,
823 .d_delete
= proc_sys_delete
,
824 .d_compare
= proc_sys_compare
,
827 static struct ctl_dir
*find_subdir(struct ctl_dir
*dir
,
828 const char *name
, int namelen
)
830 struct ctl_table_header
*head
;
831 struct ctl_table
*entry
;
833 entry
= find_entry(&head
, dir
, name
, namelen
);
835 return ERR_PTR(-ENOENT
);
836 if (!S_ISDIR(entry
->mode
))
837 return ERR_PTR(-ENOTDIR
);
838 return container_of(head
, struct ctl_dir
, header
);
841 static struct ctl_dir
*new_dir(struct ctl_table_set
*set
,
842 const char *name
, int namelen
)
844 struct ctl_table
*table
;
846 struct ctl_node
*node
;
849 new = kzalloc(sizeof(*new) + sizeof(struct ctl_node
) +
850 sizeof(struct ctl_table
)*2 + namelen
+ 1,
855 node
= (struct ctl_node
*)(new + 1);
856 table
= (struct ctl_table
*)(node
+ 1);
857 new_name
= (char *)(table
+ 2);
858 memcpy(new_name
, name
, namelen
);
859 new_name
[namelen
] = '\0';
860 table
[0].procname
= new_name
;
861 table
[0].mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
862 init_header(&new->header
, set
->dir
.header
.root
, set
, node
, table
);
867 static struct ctl_dir
*get_subdir(struct ctl_dir
*dir
,
868 const char *name
, int namelen
)
870 struct ctl_table_set
*set
= dir
->header
.set
;
871 struct ctl_dir
*subdir
, *new = NULL
;
873 spin_lock(&sysctl_lock
);
874 subdir
= find_subdir(dir
, name
, namelen
);
877 if (PTR_ERR(subdir
) != -ENOENT
)
880 spin_unlock(&sysctl_lock
);
881 new = new_dir(set
, name
, namelen
);
882 spin_lock(&sysctl_lock
);
883 subdir
= ERR_PTR(-ENOMEM
);
887 subdir
= find_subdir(dir
, name
, namelen
);
890 if (PTR_ERR(subdir
) != -ENOENT
)
893 if (insert_header(dir
, &new->header
))
897 subdir
->header
.nreg
++;
899 if (unlikely(IS_ERR(subdir
))) {
900 printk(KERN_ERR
"sysctl could not get directory: ");
901 sysctl_print_dir(dir
);
902 printk(KERN_CONT
"/%*.*s %ld\n",
903 namelen
, namelen
, name
, PTR_ERR(subdir
));
905 drop_sysctl_table(&dir
->header
);
907 drop_sysctl_table(&new->header
);
908 spin_unlock(&sysctl_lock
);
912 static struct ctl_dir
*xlate_dir(struct ctl_table_set
*set
, struct ctl_dir
*dir
)
914 struct ctl_dir
*parent
;
915 const char *procname
;
916 if (!dir
->header
.parent
)
918 parent
= xlate_dir(set
, dir
->header
.parent
);
921 procname
= dir
->header
.ctl_table
[0].procname
;
922 return find_subdir(parent
, procname
, strlen(procname
));
925 static int sysctl_follow_link(struct ctl_table_header
**phead
,
926 struct ctl_table
**pentry
, struct nsproxy
*namespaces
)
928 struct ctl_table_header
*head
;
929 struct ctl_table_root
*root
;
930 struct ctl_table_set
*set
;
931 struct ctl_table
*entry
;
935 /* Get out quickly if not a link */
936 if (!S_ISLNK((*pentry
)->mode
))
940 spin_lock(&sysctl_lock
);
941 root
= (*pentry
)->data
;
942 set
= lookup_header_set(root
, namespaces
);
943 dir
= xlate_dir(set
, (*phead
)->parent
);
947 const char *procname
= (*pentry
)->procname
;
949 entry
= find_entry(&head
, dir
, procname
, strlen(procname
));
951 if (entry
&& use_table(head
)) {
959 spin_unlock(&sysctl_lock
);
963 static int sysctl_err(const char *path
, struct ctl_table
*table
, char *fmt
, ...)
965 struct va_format vaf
;
972 printk(KERN_ERR
"sysctl table check failed: %s/%s %pV\n",
973 path
, table
->procname
, &vaf
);
979 static int sysctl_check_table(const char *path
, struct ctl_table
*table
)
982 for (; table
->procname
; table
++) {
984 err
= sysctl_err(path
, table
, "Not a file");
986 if ((table
->proc_handler
== proc_dostring
) ||
987 (table
->proc_handler
== proc_dointvec
) ||
988 (table
->proc_handler
== proc_dointvec_minmax
) ||
989 (table
->proc_handler
== proc_dointvec_jiffies
) ||
990 (table
->proc_handler
== proc_dointvec_userhz_jiffies
) ||
991 (table
->proc_handler
== proc_dointvec_ms_jiffies
) ||
992 (table
->proc_handler
== proc_doulongvec_minmax
) ||
993 (table
->proc_handler
== proc_doulongvec_ms_jiffies_minmax
)) {
995 err
= sysctl_err(path
, table
, "No data");
997 err
= sysctl_err(path
, table
, "No maxlen");
999 if (!table
->proc_handler
)
1000 err
= sysctl_err(path
, table
, "No proc_handler");
1002 if ((table
->mode
& (S_IRUGO
|S_IWUGO
)) != table
->mode
)
1003 err
= sysctl_err(path
, table
, "bogus .mode 0%o",
1009 static struct ctl_table_header
*new_links(struct ctl_dir
*dir
, struct ctl_table
*table
,
1010 struct ctl_table_root
*link_root
)
1012 struct ctl_table
*link_table
, *entry
, *link
;
1013 struct ctl_table_header
*links
;
1014 struct ctl_node
*node
;
1016 int nr_entries
, name_bytes
;
1020 for (entry
= table
; entry
->procname
; entry
++) {
1022 name_bytes
+= strlen(entry
->procname
) + 1;
1025 links
= kzalloc(sizeof(struct ctl_table_header
) +
1026 sizeof(struct ctl_node
)*nr_entries
+
1027 sizeof(struct ctl_table
)*(nr_entries
+ 1) +
1034 node
= (struct ctl_node
*)(links
+ 1);
1035 link_table
= (struct ctl_table
*)(node
+ nr_entries
);
1036 link_name
= (char *)&link_table
[nr_entries
+ 1];
1038 for (link
= link_table
, entry
= table
; entry
->procname
; link
++, entry
++) {
1039 int len
= strlen(entry
->procname
) + 1;
1040 memcpy(link_name
, entry
->procname
, len
);
1041 link
->procname
= link_name
;
1042 link
->mode
= S_IFLNK
|S_IRWXUGO
;
1043 link
->data
= link_root
;
1046 init_header(links
, dir
->header
.root
, dir
->header
.set
, node
, link_table
);
1047 links
->nreg
= nr_entries
;
1052 static bool get_links(struct ctl_dir
*dir
,
1053 struct ctl_table
*table
, struct ctl_table_root
*link_root
)
1055 struct ctl_table_header
*head
;
1056 struct ctl_table
*entry
, *link
;
1058 /* Are there links available for every entry in table? */
1059 for (entry
= table
; entry
->procname
; entry
++) {
1060 const char *procname
= entry
->procname
;
1061 link
= find_entry(&head
, dir
, procname
, strlen(procname
));
1064 if (S_ISDIR(link
->mode
) && S_ISDIR(entry
->mode
))
1066 if (S_ISLNK(link
->mode
) && (link
->data
== link_root
))
1071 /* The checks passed. Increase the registration count on the links */
1072 for (entry
= table
; entry
->procname
; entry
++) {
1073 const char *procname
= entry
->procname
;
1074 link
= find_entry(&head
, dir
, procname
, strlen(procname
));
1080 static int insert_links(struct ctl_table_header
*head
)
1082 struct ctl_table_set
*root_set
= &sysctl_table_root
.default_set
;
1083 struct ctl_dir
*core_parent
= NULL
;
1084 struct ctl_table_header
*links
;
1087 if (head
->set
== root_set
)
1090 core_parent
= xlate_dir(root_set
, head
->parent
);
1091 if (IS_ERR(core_parent
))
1094 if (get_links(core_parent
, head
->ctl_table
, head
->root
))
1097 core_parent
->header
.nreg
++;
1098 spin_unlock(&sysctl_lock
);
1100 links
= new_links(core_parent
, head
->ctl_table
, head
->root
);
1102 spin_lock(&sysctl_lock
);
1108 if (get_links(core_parent
, head
->ctl_table
, head
->root
)) {
1113 err
= insert_header(core_parent
, links
);
1117 drop_sysctl_table(&core_parent
->header
);
1122 * __register_sysctl_table - register a leaf sysctl table
1123 * @set: Sysctl tree to register on
1124 * @path: The path to the directory the sysctl table is in.
1125 * @table: the top-level table structure
1127 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1128 * array. A completely 0 filled entry terminates the table.
1130 * The members of the &struct ctl_table structure are used as follows:
1132 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
1133 * enter a sysctl file
1135 * data - a pointer to data for use by proc_handler
1137 * maxlen - the maximum size in bytes of the data
1139 * mode - the file permissions for the /proc/sys file
1141 * child - must be %NULL.
1143 * proc_handler - the text handler routine (described below)
1145 * extra1, extra2 - extra pointers usable by the proc handler routines
1147 * Leaf nodes in the sysctl tree will be represented by a single file
1148 * under /proc; non-leaf nodes will be represented by directories.
1150 * There must be a proc_handler routine for any terminal nodes.
1151 * Several default handlers are available to cover common cases -
1153 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
1154 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
1155 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
1157 * It is the handler's job to read the input buffer from user memory
1158 * and process it. The handler should return 0 on success.
1160 * This routine returns %NULL on a failure to register, and a pointer
1161 * to the table header on success.
1163 struct ctl_table_header
*__register_sysctl_table(
1164 struct ctl_table_set
*set
,
1165 const char *path
, struct ctl_table
*table
)
1167 struct ctl_table_root
*root
= set
->dir
.header
.root
;
1168 struct ctl_table_header
*header
;
1169 const char *name
, *nextname
;
1170 struct ctl_dir
*dir
;
1171 struct ctl_table
*entry
;
1172 struct ctl_node
*node
;
1175 for (entry
= table
; entry
->procname
; entry
++)
1178 header
= kzalloc(sizeof(struct ctl_table_header
) +
1179 sizeof(struct ctl_node
)*nr_entries
, GFP_KERNEL
);
1183 node
= (struct ctl_node
*)(header
+ 1);
1184 init_header(header
, root
, set
, node
, table
);
1185 if (sysctl_check_table(path
, table
))
1188 spin_lock(&sysctl_lock
);
1191 spin_unlock(&sysctl_lock
);
1193 /* Find the directory for the ctl_table */
1194 for (name
= path
; name
; name
= nextname
) {
1196 nextname
= strchr(name
, '/');
1198 namelen
= nextname
- name
;
1201 namelen
= strlen(name
);
1206 dir
= get_subdir(dir
, name
, namelen
);
1211 spin_lock(&sysctl_lock
);
1212 if (insert_header(dir
, header
))
1213 goto fail_put_dir_locked
;
1215 drop_sysctl_table(&dir
->header
);
1216 spin_unlock(&sysctl_lock
);
1220 fail_put_dir_locked
:
1221 drop_sysctl_table(&dir
->header
);
1222 spin_unlock(&sysctl_lock
);
1230 * register_sysctl - register a sysctl table
1231 * @path: The path to the directory the sysctl table is in.
1232 * @table: the table structure
1234 * Register a sysctl table. @table should be a filled in ctl_table
1235 * array. A completely 0 filled entry terminates the table.
1237 * See __register_sysctl_table for more details.
1239 struct ctl_table_header
*register_sysctl(const char *path
, struct ctl_table
*table
)
1241 return __register_sysctl_table(&sysctl_table_root
.default_set
,
1244 EXPORT_SYMBOL(register_sysctl
);
1246 static char *append_path(const char *path
, char *pos
, const char *name
)
1249 namelen
= strlen(name
);
1250 if (((pos
- path
) + namelen
+ 2) >= PATH_MAX
)
1252 memcpy(pos
, name
, namelen
);
1254 pos
[namelen
+ 1] = '\0';
1259 static int count_subheaders(struct ctl_table
*table
)
1262 int nr_subheaders
= 0;
1263 struct ctl_table
*entry
;
1265 /* special case: no directory and empty directory */
1266 if (!table
|| !table
->procname
)
1269 for (entry
= table
; entry
->procname
; entry
++) {
1271 nr_subheaders
+= count_subheaders(entry
->child
);
1275 return nr_subheaders
+ has_files
;
1278 static int register_leaf_sysctl_tables(const char *path
, char *pos
,
1279 struct ctl_table_header
***subheader
, struct ctl_table_set
*set
,
1280 struct ctl_table
*table
)
1282 struct ctl_table
*ctl_table_arg
= NULL
;
1283 struct ctl_table
*entry
, *files
;
1288 for (entry
= table
; entry
->procname
; entry
++) {
1296 /* If there are mixed files and directories we need a new table */
1297 if (nr_dirs
&& nr_files
) {
1298 struct ctl_table
*new;
1299 files
= kzalloc(sizeof(struct ctl_table
) * (nr_files
+ 1),
1304 ctl_table_arg
= files
;
1305 for (new = files
, entry
= table
; entry
->procname
; entry
++) {
1313 /* Register everything except a directory full of subdirectories */
1314 if (nr_files
|| !nr_dirs
) {
1315 struct ctl_table_header
*header
;
1316 header
= __register_sysctl_table(set
, path
, files
);
1318 kfree(ctl_table_arg
);
1322 /* Remember if we need to free the file table */
1323 header
->ctl_table_arg
= ctl_table_arg
;
1324 **subheader
= header
;
1328 /* Recurse into the subdirectories. */
1329 for (entry
= table
; entry
->procname
; entry
++) {
1335 err
= -ENAMETOOLONG
;
1336 child_pos
= append_path(path
, pos
, entry
->procname
);
1340 err
= register_leaf_sysctl_tables(path
, child_pos
, subheader
,
1348 /* On failure our caller will unregister all registered subheaders */
1353 * __register_sysctl_paths - register a sysctl table hierarchy
1354 * @set: Sysctl tree to register on
1355 * @path: The path to the directory the sysctl table is in.
1356 * @table: the top-level table structure
1358 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1359 * array. A completely 0 filled entry terminates the table.
1361 * See __register_sysctl_table for more details.
1363 struct ctl_table_header
*__register_sysctl_paths(
1364 struct ctl_table_set
*set
,
1365 const struct ctl_path
*path
, struct ctl_table
*table
)
1367 struct ctl_table
*ctl_table_arg
= table
;
1368 int nr_subheaders
= count_subheaders(table
);
1369 struct ctl_table_header
*header
= NULL
, **subheaders
, **subheader
;
1370 const struct ctl_path
*component
;
1371 char *new_path
, *pos
;
1373 pos
= new_path
= kmalloc(PATH_MAX
, GFP_KERNEL
);
1378 for (component
= path
; component
->procname
; component
++) {
1379 pos
= append_path(new_path
, pos
, component
->procname
);
1383 while (table
->procname
&& table
->child
&& !table
[1].procname
) {
1384 pos
= append_path(new_path
, pos
, table
->procname
);
1387 table
= table
->child
;
1389 if (nr_subheaders
== 1) {
1390 header
= __register_sysctl_table(set
, new_path
, table
);
1392 header
->ctl_table_arg
= ctl_table_arg
;
1394 header
= kzalloc(sizeof(*header
) +
1395 sizeof(*subheaders
)*nr_subheaders
, GFP_KERNEL
);
1399 subheaders
= (struct ctl_table_header
**) (header
+ 1);
1400 subheader
= subheaders
;
1401 header
->ctl_table_arg
= ctl_table_arg
;
1403 if (register_leaf_sysctl_tables(new_path
, pos
, &subheader
,
1405 goto err_register_leaves
;
1412 err_register_leaves
:
1413 while (subheader
> subheaders
) {
1414 struct ctl_table_header
*subh
= *(--subheader
);
1415 struct ctl_table
*table
= subh
->ctl_table_arg
;
1416 unregister_sysctl_table(subh
);
1425 * register_sysctl_table_path - register a sysctl table hierarchy
1426 * @path: The path to the directory the sysctl table is in.
1427 * @table: the top-level table structure
1429 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1430 * array. A completely 0 filled entry terminates the table.
1432 * See __register_sysctl_paths for more details.
1434 struct ctl_table_header
*register_sysctl_paths(const struct ctl_path
*path
,
1435 struct ctl_table
*table
)
1437 return __register_sysctl_paths(&sysctl_table_root
.default_set
,
1440 EXPORT_SYMBOL(register_sysctl_paths
);
1443 * register_sysctl_table - register a sysctl table hierarchy
1444 * @table: the top-level table structure
1446 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1447 * array. A completely 0 filled entry terminates the table.
1449 * See register_sysctl_paths for more details.
1451 struct ctl_table_header
*register_sysctl_table(struct ctl_table
*table
)
1453 static const struct ctl_path null_path
[] = { {} };
1455 return register_sysctl_paths(null_path
, table
);
1457 EXPORT_SYMBOL(register_sysctl_table
);
1459 static void put_links(struct ctl_table_header
*header
)
1461 struct ctl_table_set
*root_set
= &sysctl_table_root
.default_set
;
1462 struct ctl_table_root
*root
= header
->root
;
1463 struct ctl_dir
*parent
= header
->parent
;
1464 struct ctl_dir
*core_parent
;
1465 struct ctl_table
*entry
;
1467 if (header
->set
== root_set
)
1470 core_parent
= xlate_dir(root_set
, parent
);
1471 if (IS_ERR(core_parent
))
1474 for (entry
= header
->ctl_table
; entry
->procname
; entry
++) {
1475 struct ctl_table_header
*link_head
;
1476 struct ctl_table
*link
;
1477 const char *name
= entry
->procname
;
1479 link
= find_entry(&link_head
, core_parent
, name
, strlen(name
));
1481 ((S_ISDIR(link
->mode
) && S_ISDIR(entry
->mode
)) ||
1482 (S_ISLNK(link
->mode
) && (link
->data
== root
)))) {
1483 drop_sysctl_table(link_head
);
1486 printk(KERN_ERR
"sysctl link missing during unregister: ");
1487 sysctl_print_dir(parent
);
1488 printk(KERN_CONT
"/%s\n", name
);
1493 static void drop_sysctl_table(struct ctl_table_header
*header
)
1495 struct ctl_dir
*parent
= header
->parent
;
1501 start_unregistering(header
);
1502 if (!--header
->count
)
1503 kfree_rcu(header
, rcu
);
1506 drop_sysctl_table(&parent
->header
);
1510 * unregister_sysctl_table - unregister a sysctl table hierarchy
1511 * @header: the header returned from register_sysctl_table
1513 * Unregisters the sysctl table and all children. proc entries may not
1514 * actually be removed until they are no longer used by anyone.
1516 void unregister_sysctl_table(struct ctl_table_header
* header
)
1524 nr_subheaders
= count_subheaders(header
->ctl_table_arg
);
1525 if (unlikely(nr_subheaders
> 1)) {
1526 struct ctl_table_header
**subheaders
;
1529 subheaders
= (struct ctl_table_header
**)(header
+ 1);
1530 for (i
= nr_subheaders
-1; i
>= 0; i
--) {
1531 struct ctl_table_header
*subh
= subheaders
[i
];
1532 struct ctl_table
*table
= subh
->ctl_table_arg
;
1533 unregister_sysctl_table(subh
);
1540 spin_lock(&sysctl_lock
);
1541 drop_sysctl_table(header
);
1542 spin_unlock(&sysctl_lock
);
1544 EXPORT_SYMBOL(unregister_sysctl_table
);
1546 void setup_sysctl_set(struct ctl_table_set
*set
,
1547 struct ctl_table_root
*root
,
1548 int (*is_seen
)(struct ctl_table_set
*))
1550 memset(set
, 0, sizeof(*set
));
1551 set
->is_seen
= is_seen
;
1552 init_header(&set
->dir
.header
, root
, set
, NULL
, root_table
);
1555 void retire_sysctl_set(struct ctl_table_set
*set
)
1557 WARN_ON(!RB_EMPTY_ROOT(&set
->dir
.root
));
1560 int __init
proc_sys_init(void)
1562 struct proc_dir_entry
*proc_sys_root
;
1564 proc_sys_root
= proc_mkdir("sys", NULL
);
1565 proc_sys_root
->proc_iops
= &proc_sys_dir_operations
;
1566 proc_sys_root
->proc_fops
= &proc_sys_dir_file_operations
;
1567 proc_sys_root
->nlink
= 0;
1569 return sysctl_init();