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 /* Called under sysctl_lock */
77 static struct ctl_table
*find_entry(struct ctl_table_header
**phead
,
78 struct ctl_dir
*dir
, const char *name
, int namelen
)
80 struct ctl_table_header
*head
;
81 struct ctl_table
*entry
;
82 struct rb_node
*node
= dir
->root
.rb_node
;
86 struct ctl_node
*ctl_node
;
90 ctl_node
= rb_entry(node
, struct ctl_node
, node
);
91 head
= ctl_node
->header
;
92 entry
= &head
->ctl_table
[ctl_node
- head
->node
];
93 procname
= entry
->procname
;
95 cmp
= namecmp(name
, namelen
, procname
, strlen(procname
));
99 node
= node
->rb_right
;
108 static int insert_entry(struct ctl_table_header
*head
, struct ctl_table
*entry
)
110 struct rb_node
*node
= &head
->node
[entry
- head
->ctl_table
].node
;
111 struct rb_node
**p
= &head
->parent
->root
.rb_node
;
112 struct rb_node
*parent
= NULL
;
113 const char *name
= entry
->procname
;
114 int namelen
= strlen(name
);
117 struct ctl_table_header
*parent_head
;
118 struct ctl_table
*parent_entry
;
119 struct ctl_node
*parent_node
;
120 const char *parent_name
;
124 parent_node
= rb_entry(parent
, struct ctl_node
, node
);
125 parent_head
= parent_node
->header
;
126 parent_entry
= &parent_head
->ctl_table
[parent_node
- parent_head
->node
];
127 parent_name
= parent_entry
->procname
;
129 cmp
= namecmp(name
, namelen
, parent_name
, strlen(parent_name
));
135 printk(KERN_ERR
"sysctl duplicate entry: ");
136 sysctl_print_dir(head
->parent
);
137 printk(KERN_CONT
"/%s\n", entry
->procname
);
142 rb_link_node(node
, parent
, p
);
146 static void erase_entry(struct ctl_table_header
*head
, struct ctl_table
*entry
)
148 struct rb_node
*node
= &head
->node
[entry
- head
->ctl_table
].node
;
150 rb_erase(node
, &head
->parent
->root
);
153 static void init_header(struct ctl_table_header
*head
,
154 struct ctl_table_root
*root
, struct ctl_table_set
*set
,
155 struct ctl_node
*node
, struct ctl_table
*table
)
157 head
->ctl_table
= table
;
158 head
->ctl_table_arg
= table
;
162 head
->unregistering
= NULL
;
168 struct ctl_table
*entry
;
169 for (entry
= table
; entry
->procname
; entry
++, node
++) {
170 rb_init_node(&node
->node
);
176 static void erase_header(struct ctl_table_header
*head
)
178 struct ctl_table
*entry
;
179 for (entry
= head
->ctl_table
; entry
->procname
; entry
++)
180 erase_entry(head
, entry
);
183 static int insert_header(struct ctl_dir
*dir
, struct ctl_table_header
*header
)
185 struct ctl_table
*entry
;
189 header
->parent
= dir
;
190 err
= insert_links(header
);
193 for (entry
= header
->ctl_table
; entry
->procname
; entry
++) {
194 err
= insert_entry(header
, entry
);
200 erase_header(header
);
203 header
->parent
= NULL
;
204 drop_sysctl_table(&dir
->header
);
208 /* called under sysctl_lock */
209 static int use_table(struct ctl_table_header
*p
)
211 if (unlikely(p
->unregistering
))
217 /* called under sysctl_lock */
218 static void unuse_table(struct ctl_table_header
*p
)
221 if (unlikely(p
->unregistering
))
222 complete(p
->unregistering
);
225 /* called under sysctl_lock, will reacquire if has to wait */
226 static void start_unregistering(struct ctl_table_header
*p
)
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
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
);
240 /* anything non-NULL; we'll never dereference it */
241 p
->unregistering
= ERR_PTR(-EINVAL
);
244 * do not remove from the list until nobody holds it; walking the
245 * list in do_sysctl() relies on that.
250 static void sysctl_head_get(struct ctl_table_header
*head
)
252 spin_lock(&sysctl_lock
);
254 spin_unlock(&sysctl_lock
);
257 void sysctl_head_put(struct ctl_table_header
*head
)
259 spin_lock(&sysctl_lock
);
261 kfree_rcu(head
, rcu
);
262 spin_unlock(&sysctl_lock
);
265 static struct ctl_table_header
*sysctl_head_grab(struct ctl_table_header
*head
)
269 spin_lock(&sysctl_lock
);
270 if (!use_table(head
))
271 head
= ERR_PTR(-ENOENT
);
272 spin_unlock(&sysctl_lock
);
276 static void sysctl_head_finish(struct ctl_table_header
*head
)
280 spin_lock(&sysctl_lock
);
282 spin_unlock(&sysctl_lock
);
285 static struct ctl_table_set
*
286 lookup_header_set(struct ctl_table_root
*root
, struct nsproxy
*namespaces
)
288 struct ctl_table_set
*set
= &root
->default_set
;
290 set
= root
->lookup(root
, namespaces
);
294 static struct ctl_table
*lookup_entry(struct ctl_table_header
**phead
,
296 const char *name
, int namelen
)
298 struct ctl_table_header
*head
;
299 struct ctl_table
*entry
;
301 spin_lock(&sysctl_lock
);
302 entry
= find_entry(&head
, dir
, name
, namelen
);
303 if (entry
&& use_table(head
))
307 spin_unlock(&sysctl_lock
);
311 static struct ctl_node
*first_usable_entry(struct rb_node
*node
)
313 struct ctl_node
*ctl_node
;
315 for (;node
; node
= rb_next(node
)) {
316 ctl_node
= rb_entry(node
, struct ctl_node
, node
);
317 if (use_table(ctl_node
->header
))
323 static void first_entry(struct ctl_dir
*dir
,
324 struct ctl_table_header
**phead
, struct ctl_table
**pentry
)
326 struct ctl_table_header
*head
= NULL
;
327 struct ctl_table
*entry
= NULL
;
328 struct ctl_node
*ctl_node
;
330 spin_lock(&sysctl_lock
);
331 ctl_node
= first_usable_entry(rb_first(&dir
->root
));
332 spin_unlock(&sysctl_lock
);
334 head
= ctl_node
->header
;
335 entry
= &head
->ctl_table
[ctl_node
- head
->node
];
341 static void next_entry(struct ctl_table_header
**phead
, struct ctl_table
**pentry
)
343 struct ctl_table_header
*head
= *phead
;
344 struct ctl_table
*entry
= *pentry
;
345 struct ctl_node
*ctl_node
= &head
->node
[entry
- head
->ctl_table
];
347 spin_lock(&sysctl_lock
);
350 ctl_node
= first_usable_entry(rb_next(&ctl_node
->node
));
351 spin_unlock(&sysctl_lock
);
354 head
= ctl_node
->header
;
355 entry
= &head
->ctl_table
[ctl_node
- head
->node
];
361 void register_sysctl_root(struct ctl_table_root
*root
)
366 * sysctl_perm does NOT grant the superuser all rights automatically, because
367 * some sysctl variables are readonly even to root.
370 static int test_perm(int mode
, int op
)
374 else if (in_egroup_p(0))
376 if ((op
& ~mode
& (MAY_READ
|MAY_WRITE
|MAY_EXEC
)) == 0)
381 static int sysctl_perm(struct ctl_table_root
*root
, struct ctl_table
*table
, int op
)
385 if (root
->permissions
)
386 mode
= root
->permissions(root
, current
->nsproxy
, table
);
390 return test_perm(mode
, op
);
393 static struct inode
*proc_sys_make_inode(struct super_block
*sb
,
394 struct ctl_table_header
*head
, struct ctl_table
*table
)
397 struct proc_inode
*ei
;
399 inode
= new_inode(sb
);
403 inode
->i_ino
= get_next_ino();
405 sysctl_head_get(head
);
408 ei
->sysctl_entry
= table
;
410 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
411 inode
->i_mode
= table
->mode
;
412 if (!S_ISDIR(table
->mode
)) {
413 inode
->i_mode
|= S_IFREG
;
414 inode
->i_op
= &proc_sys_inode_operations
;
415 inode
->i_fop
= &proc_sys_file_operations
;
417 inode
->i_mode
|= S_IFDIR
;
418 inode
->i_op
= &proc_sys_dir_operations
;
419 inode
->i_fop
= &proc_sys_dir_file_operations
;
425 static struct ctl_table_header
*grab_header(struct inode
*inode
)
427 struct ctl_table_header
*head
= PROC_I(inode
)->sysctl
;
429 head
= &sysctl_table_root
.default_set
.dir
.header
;
430 return sysctl_head_grab(head
);
433 static struct dentry
*proc_sys_lookup(struct inode
*dir
, struct dentry
*dentry
,
434 struct nameidata
*nd
)
436 struct ctl_table_header
*head
= grab_header(dir
);
437 struct ctl_table_header
*h
= NULL
;
438 struct qstr
*name
= &dentry
->d_name
;
441 struct dentry
*err
= ERR_PTR(-ENOENT
);
442 struct ctl_dir
*ctl_dir
;
446 return ERR_CAST(head
);
448 ctl_dir
= container_of(head
, struct ctl_dir
, header
);
450 p
= lookup_entry(&h
, ctl_dir
, name
->name
, name
->len
);
454 ret
= sysctl_follow_link(&h
, &p
, current
->nsproxy
);
459 err
= ERR_PTR(-ENOMEM
);
460 inode
= proc_sys_make_inode(dir
->i_sb
, h
? h
: head
, p
);
462 sysctl_head_finish(h
);
468 d_set_d_op(dentry
, &proc_sys_dentry_operations
);
469 d_add(dentry
, inode
);
472 sysctl_head_finish(head
);
476 static ssize_t
proc_sys_call_handler(struct file
*filp
, void __user
*buf
,
477 size_t count
, loff_t
*ppos
, int write
)
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
;
486 return PTR_ERR(head
);
489 * At this point we know that the sysctl was not unregistered
490 * and won't be until we finish.
493 if (sysctl_perm(head
->root
, table
, write
? MAY_WRITE
: MAY_READ
))
496 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
498 if (!table
->proc_handler
)
501 /* careful: calling conventions are nasty here */
503 error
= table
->proc_handler(table
, write
, buf
, &res
, ppos
);
507 sysctl_head_finish(head
);
512 static ssize_t
proc_sys_read(struct file
*filp
, char __user
*buf
,
513 size_t count
, loff_t
*ppos
)
515 return proc_sys_call_handler(filp
, (void __user
*)buf
, count
, ppos
, 0);
518 static ssize_t
proc_sys_write(struct file
*filp
, const char __user
*buf
,
519 size_t count
, loff_t
*ppos
)
521 return proc_sys_call_handler(filp
, (void __user
*)buf
, count
, ppos
, 1);
524 static int proc_sys_open(struct inode
*inode
, struct file
*filp
)
526 struct ctl_table
*table
= PROC_I(inode
)->sysctl_entry
;
529 filp
->private_data
= proc_sys_poll_event(table
->poll
);
534 static unsigned int proc_sys_poll(struct file
*filp
, poll_table
*wait
)
536 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
537 struct ctl_table
*table
= PROC_I(inode
)->sysctl_entry
;
538 unsigned long event
= (unsigned long)filp
->private_data
;
539 unsigned int ret
= DEFAULT_POLLMASK
;
541 if (!table
->proc_handler
)
547 poll_wait(filp
, &table
->poll
->wait
, wait
);
549 if (event
!= atomic_read(&table
->poll
->event
)) {
550 filp
->private_data
= proc_sys_poll_event(table
->poll
);
551 ret
= POLLIN
| POLLRDNORM
| POLLERR
| POLLPRI
;
558 static int proc_sys_fill_cache(struct file
*filp
, void *dirent
,
560 struct ctl_table_header
*head
,
561 struct ctl_table
*table
)
563 struct dentry
*child
, *dir
= filp
->f_path
.dentry
;
567 unsigned type
= DT_UNKNOWN
;
569 qname
.name
= table
->procname
;
570 qname
.len
= strlen(table
->procname
);
571 qname
.hash
= full_name_hash(qname
.name
, qname
.len
);
573 child
= d_lookup(dir
, &qname
);
575 child
= d_alloc(dir
, &qname
);
577 inode
= proc_sys_make_inode(dir
->d_sb
, head
, table
);
582 d_set_d_op(child
, &proc_sys_dentry_operations
);
589 inode
= child
->d_inode
;
591 type
= inode
->i_mode
>> 12;
593 return !!filldir(dirent
, qname
.name
, qname
.len
, filp
->f_pos
, ino
, type
);
596 static int proc_sys_link_fill_cache(struct file
*filp
, void *dirent
,
598 struct ctl_table_header
*head
,
599 struct ctl_table
*table
)
602 head
= sysctl_head_grab(head
);
604 /* It is not an error if we can not follow the link ignore it */
605 err
= sysctl_follow_link(&head
, &table
, current
->nsproxy
);
609 ret
= proc_sys_fill_cache(filp
, dirent
, filldir
, head
, table
);
611 sysctl_head_finish(head
);
615 static int scan(struct ctl_table_header
*head
, ctl_table
*table
,
616 unsigned long *pos
, struct file
*file
,
617 void *dirent
, filldir_t filldir
)
621 if ((*pos
)++ < file
->f_pos
)
624 if (unlikely(S_ISLNK(table
->mode
)))
625 res
= proc_sys_link_fill_cache(file
, dirent
, filldir
, head
, table
);
627 res
= proc_sys_fill_cache(file
, dirent
, filldir
, head
, table
);
635 static int proc_sys_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
637 struct dentry
*dentry
= filp
->f_path
.dentry
;
638 struct inode
*inode
= dentry
->d_inode
;
639 struct ctl_table_header
*head
= grab_header(inode
);
640 struct ctl_table_header
*h
= NULL
;
641 struct ctl_table
*entry
;
642 struct ctl_dir
*ctl_dir
;
647 return PTR_ERR(head
);
649 ctl_dir
= container_of(head
, struct ctl_dir
, header
);
652 /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
653 if (filp
->f_pos
== 0) {
654 if (filldir(dirent
, ".", 1, filp
->f_pos
,
655 inode
->i_ino
, DT_DIR
) < 0)
659 if (filp
->f_pos
== 1) {
660 if (filldir(dirent
, "..", 2, filp
->f_pos
,
661 parent_ino(dentry
), DT_DIR
) < 0)
667 for (first_entry(ctl_dir
, &h
, &entry
); h
; next_entry(&h
, &entry
)) {
668 ret
= scan(h
, entry
, &pos
, filp
, dirent
, filldir
);
670 sysctl_head_finish(h
);
676 sysctl_head_finish(head
);
680 static int proc_sys_permission(struct inode
*inode
, int mask
)
683 * sysctl entries that are not writeable,
684 * are _NOT_ writeable, capabilities or not.
686 struct ctl_table_header
*head
;
687 struct ctl_table
*table
;
690 /* Executable files are not allowed under /proc/sys/ */
691 if ((mask
& MAY_EXEC
) && S_ISREG(inode
->i_mode
))
694 head
= grab_header(inode
);
696 return PTR_ERR(head
);
698 table
= PROC_I(inode
)->sysctl_entry
;
699 if (!table
) /* global root - r-xr-xr-x */
700 error
= mask
& MAY_WRITE
? -EACCES
: 0;
701 else /* Use the permissions on the sysctl table entry */
702 error
= sysctl_perm(head
->root
, table
, mask
& ~MAY_NOT_BLOCK
);
704 sysctl_head_finish(head
);
708 static int proc_sys_setattr(struct dentry
*dentry
, struct iattr
*attr
)
710 struct inode
*inode
= dentry
->d_inode
;
713 if (attr
->ia_valid
& (ATTR_MODE
| ATTR_UID
| ATTR_GID
))
716 error
= inode_change_ok(inode
, attr
);
720 if ((attr
->ia_valid
& ATTR_SIZE
) &&
721 attr
->ia_size
!= i_size_read(inode
)) {
722 error
= vmtruncate(inode
, attr
->ia_size
);
727 setattr_copy(inode
, attr
);
728 mark_inode_dirty(inode
);
732 static int proc_sys_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
734 struct inode
*inode
= dentry
->d_inode
;
735 struct ctl_table_header
*head
= grab_header(inode
);
736 struct ctl_table
*table
= PROC_I(inode
)->sysctl_entry
;
739 return PTR_ERR(head
);
741 generic_fillattr(inode
, stat
);
743 stat
->mode
= (stat
->mode
& S_IFMT
) | table
->mode
;
745 sysctl_head_finish(head
);
749 static const struct file_operations proc_sys_file_operations
= {
750 .open
= proc_sys_open
,
751 .poll
= proc_sys_poll
,
752 .read
= proc_sys_read
,
753 .write
= proc_sys_write
,
754 .llseek
= default_llseek
,
757 static const struct file_operations proc_sys_dir_file_operations
= {
758 .read
= generic_read_dir
,
759 .readdir
= proc_sys_readdir
,
760 .llseek
= generic_file_llseek
,
763 static const struct inode_operations proc_sys_inode_operations
= {
764 .permission
= proc_sys_permission
,
765 .setattr
= proc_sys_setattr
,
766 .getattr
= proc_sys_getattr
,
769 static const struct inode_operations proc_sys_dir_operations
= {
770 .lookup
= proc_sys_lookup
,
771 .permission
= proc_sys_permission
,
772 .setattr
= proc_sys_setattr
,
773 .getattr
= proc_sys_getattr
,
776 static int proc_sys_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
778 if (nd
->flags
& LOOKUP_RCU
)
780 return !PROC_I(dentry
->d_inode
)->sysctl
->unregistering
;
783 static int proc_sys_delete(const struct dentry
*dentry
)
785 return !!PROC_I(dentry
->d_inode
)->sysctl
->unregistering
;
788 static int sysctl_is_seen(struct ctl_table_header
*p
)
790 struct ctl_table_set
*set
= p
->set
;
792 spin_lock(&sysctl_lock
);
793 if (p
->unregistering
)
795 else if (!set
->is_seen
)
798 res
= set
->is_seen(set
);
799 spin_unlock(&sysctl_lock
);
803 static int proc_sys_compare(const struct dentry
*parent
,
804 const struct inode
*pinode
,
805 const struct dentry
*dentry
, const struct inode
*inode
,
806 unsigned int len
, const char *str
, const struct qstr
*name
)
808 struct ctl_table_header
*head
;
809 /* Although proc doesn't have negative dentries, rcu-walk means
810 * that inode here can be NULL */
811 /* AV: can it, indeed? */
814 if (name
->len
!= len
)
816 if (memcmp(name
->name
, str
, len
))
818 head
= rcu_dereference(PROC_I(inode
)->sysctl
);
819 return !head
|| !sysctl_is_seen(head
);
822 static const struct dentry_operations proc_sys_dentry_operations
= {
823 .d_revalidate
= proc_sys_revalidate
,
824 .d_delete
= proc_sys_delete
,
825 .d_compare
= proc_sys_compare
,
828 static struct ctl_dir
*find_subdir(struct ctl_dir
*dir
,
829 const char *name
, int namelen
)
831 struct ctl_table_header
*head
;
832 struct ctl_table
*entry
;
834 entry
= find_entry(&head
, dir
, name
, namelen
);
836 return ERR_PTR(-ENOENT
);
837 if (!S_ISDIR(entry
->mode
))
838 return ERR_PTR(-ENOTDIR
);
839 return container_of(head
, struct ctl_dir
, header
);
842 static struct ctl_dir
*new_dir(struct ctl_table_set
*set
,
843 const char *name
, int namelen
)
845 struct ctl_table
*table
;
847 struct ctl_node
*node
;
850 new = kzalloc(sizeof(*new) + sizeof(struct ctl_node
) +
851 sizeof(struct ctl_table
)*2 + namelen
+ 1,
856 node
= (struct ctl_node
*)(new + 1);
857 table
= (struct ctl_table
*)(node
+ 1);
858 new_name
= (char *)(table
+ 2);
859 memcpy(new_name
, name
, namelen
);
860 new_name
[namelen
] = '\0';
861 table
[0].procname
= new_name
;
862 table
[0].mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
863 init_header(&new->header
, set
->dir
.header
.root
, set
, node
, table
);
869 * get_subdir - find or create a subdir with the specified name.
870 * @dir: Directory to create the subdirectory in
871 * @name: The name of the subdirectory to find or create
872 * @namelen: The length of name
874 * Takes a directory with an elevated reference count so we know that
875 * if we drop the lock the directory will not go away. Upon success
876 * the reference is moved from @dir to the returned subdirectory.
877 * Upon error an error code is returned and the reference on @dir is
880 static struct ctl_dir
*get_subdir(struct ctl_dir
*dir
,
881 const char *name
, int namelen
)
883 struct ctl_table_set
*set
= dir
->header
.set
;
884 struct ctl_dir
*subdir
, *new = NULL
;
887 spin_lock(&sysctl_lock
);
888 subdir
= find_subdir(dir
, name
, namelen
);
891 if (PTR_ERR(subdir
) != -ENOENT
)
894 spin_unlock(&sysctl_lock
);
895 new = new_dir(set
, name
, namelen
);
896 spin_lock(&sysctl_lock
);
897 subdir
= ERR_PTR(-ENOMEM
);
901 /* Was the subdir added while we dropped the lock? */
902 subdir
= find_subdir(dir
, name
, namelen
);
905 if (PTR_ERR(subdir
) != -ENOENT
)
908 /* Nope. Use the our freshly made directory entry. */
909 err
= insert_header(dir
, &new->header
);
910 subdir
= ERR_PTR(err
);
915 subdir
->header
.nreg
++;
917 if (unlikely(IS_ERR(subdir
))) {
918 printk(KERN_ERR
"sysctl could not get directory: ");
919 sysctl_print_dir(dir
);
920 printk(KERN_CONT
"/%*.*s %ld\n",
921 namelen
, namelen
, name
, PTR_ERR(subdir
));
923 drop_sysctl_table(&dir
->header
);
925 drop_sysctl_table(&new->header
);
926 spin_unlock(&sysctl_lock
);
930 static struct ctl_dir
*xlate_dir(struct ctl_table_set
*set
, struct ctl_dir
*dir
)
932 struct ctl_dir
*parent
;
933 const char *procname
;
934 if (!dir
->header
.parent
)
936 parent
= xlate_dir(set
, dir
->header
.parent
);
939 procname
= dir
->header
.ctl_table
[0].procname
;
940 return find_subdir(parent
, procname
, strlen(procname
));
943 static int sysctl_follow_link(struct ctl_table_header
**phead
,
944 struct ctl_table
**pentry
, struct nsproxy
*namespaces
)
946 struct ctl_table_header
*head
;
947 struct ctl_table_root
*root
;
948 struct ctl_table_set
*set
;
949 struct ctl_table
*entry
;
953 /* Get out quickly if not a link */
954 if (!S_ISLNK((*pentry
)->mode
))
958 spin_lock(&sysctl_lock
);
959 root
= (*pentry
)->data
;
960 set
= lookup_header_set(root
, namespaces
);
961 dir
= xlate_dir(set
, (*phead
)->parent
);
965 const char *procname
= (*pentry
)->procname
;
967 entry
= find_entry(&head
, dir
, procname
, strlen(procname
));
969 if (entry
&& use_table(head
)) {
977 spin_unlock(&sysctl_lock
);
981 static int sysctl_err(const char *path
, struct ctl_table
*table
, char *fmt
, ...)
983 struct va_format vaf
;
990 printk(KERN_ERR
"sysctl table check failed: %s/%s %pV\n",
991 path
, table
->procname
, &vaf
);
997 static int sysctl_check_table(const char *path
, struct ctl_table
*table
)
1000 for (; table
->procname
; table
++) {
1002 err
= sysctl_err(path
, table
, "Not a file");
1004 if ((table
->proc_handler
== proc_dostring
) ||
1005 (table
->proc_handler
== proc_dointvec
) ||
1006 (table
->proc_handler
== proc_dointvec_minmax
) ||
1007 (table
->proc_handler
== proc_dointvec_jiffies
) ||
1008 (table
->proc_handler
== proc_dointvec_userhz_jiffies
) ||
1009 (table
->proc_handler
== proc_dointvec_ms_jiffies
) ||
1010 (table
->proc_handler
== proc_doulongvec_minmax
) ||
1011 (table
->proc_handler
== proc_doulongvec_ms_jiffies_minmax
)) {
1013 err
= sysctl_err(path
, table
, "No data");
1015 err
= sysctl_err(path
, table
, "No maxlen");
1017 if (!table
->proc_handler
)
1018 err
= sysctl_err(path
, table
, "No proc_handler");
1020 if ((table
->mode
& (S_IRUGO
|S_IWUGO
)) != table
->mode
)
1021 err
= sysctl_err(path
, table
, "bogus .mode 0%o",
1027 static struct ctl_table_header
*new_links(struct ctl_dir
*dir
, struct ctl_table
*table
,
1028 struct ctl_table_root
*link_root
)
1030 struct ctl_table
*link_table
, *entry
, *link
;
1031 struct ctl_table_header
*links
;
1032 struct ctl_node
*node
;
1034 int nr_entries
, name_bytes
;
1038 for (entry
= table
; entry
->procname
; entry
++) {
1040 name_bytes
+= strlen(entry
->procname
) + 1;
1043 links
= kzalloc(sizeof(struct ctl_table_header
) +
1044 sizeof(struct ctl_node
)*nr_entries
+
1045 sizeof(struct ctl_table
)*(nr_entries
+ 1) +
1052 node
= (struct ctl_node
*)(links
+ 1);
1053 link_table
= (struct ctl_table
*)(node
+ nr_entries
);
1054 link_name
= (char *)&link_table
[nr_entries
+ 1];
1056 for (link
= link_table
, entry
= table
; entry
->procname
; link
++, entry
++) {
1057 int len
= strlen(entry
->procname
) + 1;
1058 memcpy(link_name
, entry
->procname
, len
);
1059 link
->procname
= link_name
;
1060 link
->mode
= S_IFLNK
|S_IRWXUGO
;
1061 link
->data
= link_root
;
1064 init_header(links
, dir
->header
.root
, dir
->header
.set
, node
, link_table
);
1065 links
->nreg
= nr_entries
;
1070 static bool get_links(struct ctl_dir
*dir
,
1071 struct ctl_table
*table
, struct ctl_table_root
*link_root
)
1073 struct ctl_table_header
*head
;
1074 struct ctl_table
*entry
, *link
;
1076 /* Are there links available for every entry in table? */
1077 for (entry
= table
; entry
->procname
; entry
++) {
1078 const char *procname
= entry
->procname
;
1079 link
= find_entry(&head
, dir
, procname
, strlen(procname
));
1082 if (S_ISDIR(link
->mode
) && S_ISDIR(entry
->mode
))
1084 if (S_ISLNK(link
->mode
) && (link
->data
== link_root
))
1089 /* The checks passed. Increase the registration count on the links */
1090 for (entry
= table
; entry
->procname
; entry
++) {
1091 const char *procname
= entry
->procname
;
1092 link
= find_entry(&head
, dir
, procname
, strlen(procname
));
1098 static int insert_links(struct ctl_table_header
*head
)
1100 struct ctl_table_set
*root_set
= &sysctl_table_root
.default_set
;
1101 struct ctl_dir
*core_parent
= NULL
;
1102 struct ctl_table_header
*links
;
1105 if (head
->set
== root_set
)
1108 core_parent
= xlate_dir(root_set
, head
->parent
);
1109 if (IS_ERR(core_parent
))
1112 if (get_links(core_parent
, head
->ctl_table
, head
->root
))
1115 core_parent
->header
.nreg
++;
1116 spin_unlock(&sysctl_lock
);
1118 links
= new_links(core_parent
, head
->ctl_table
, head
->root
);
1120 spin_lock(&sysctl_lock
);
1126 if (get_links(core_parent
, head
->ctl_table
, head
->root
)) {
1131 err
= insert_header(core_parent
, links
);
1135 drop_sysctl_table(&core_parent
->header
);
1140 * __register_sysctl_table - register a leaf sysctl table
1141 * @set: Sysctl tree to register on
1142 * @path: The path to the directory the sysctl table is in.
1143 * @table: the top-level table structure
1145 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1146 * array. A completely 0 filled entry terminates the table.
1148 * The members of the &struct ctl_table structure are used as follows:
1150 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
1151 * enter a sysctl file
1153 * data - a pointer to data for use by proc_handler
1155 * maxlen - the maximum size in bytes of the data
1157 * mode - the file permissions for the /proc/sys file
1159 * child - must be %NULL.
1161 * proc_handler - the text handler routine (described below)
1163 * extra1, extra2 - extra pointers usable by the proc handler routines
1165 * Leaf nodes in the sysctl tree will be represented by a single file
1166 * under /proc; non-leaf nodes will be represented by directories.
1168 * There must be a proc_handler routine for any terminal nodes.
1169 * Several default handlers are available to cover common cases -
1171 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
1172 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
1173 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
1175 * It is the handler's job to read the input buffer from user memory
1176 * and process it. The handler should return 0 on success.
1178 * This routine returns %NULL on a failure to register, and a pointer
1179 * to the table header on success.
1181 struct ctl_table_header
*__register_sysctl_table(
1182 struct ctl_table_set
*set
,
1183 const char *path
, struct ctl_table
*table
)
1185 struct ctl_table_root
*root
= set
->dir
.header
.root
;
1186 struct ctl_table_header
*header
;
1187 const char *name
, *nextname
;
1188 struct ctl_dir
*dir
;
1189 struct ctl_table
*entry
;
1190 struct ctl_node
*node
;
1193 for (entry
= table
; entry
->procname
; entry
++)
1196 header
= kzalloc(sizeof(struct ctl_table_header
) +
1197 sizeof(struct ctl_node
)*nr_entries
, GFP_KERNEL
);
1201 node
= (struct ctl_node
*)(header
+ 1);
1202 init_header(header
, root
, set
, node
, table
);
1203 if (sysctl_check_table(path
, table
))
1206 spin_lock(&sysctl_lock
);
1208 /* Reference moved down the diretory tree get_subdir */
1210 spin_unlock(&sysctl_lock
);
1212 /* Find the directory for the ctl_table */
1213 for (name
= path
; name
; name
= nextname
) {
1215 nextname
= strchr(name
, '/');
1217 namelen
= nextname
- name
;
1220 namelen
= strlen(name
);
1225 dir
= get_subdir(dir
, name
, namelen
);
1230 spin_lock(&sysctl_lock
);
1231 if (insert_header(dir
, header
))
1232 goto fail_put_dir_locked
;
1234 drop_sysctl_table(&dir
->header
);
1235 spin_unlock(&sysctl_lock
);
1239 fail_put_dir_locked
:
1240 drop_sysctl_table(&dir
->header
);
1241 spin_unlock(&sysctl_lock
);
1249 * register_sysctl - register a sysctl table
1250 * @path: The path to the directory the sysctl table is in.
1251 * @table: the table structure
1253 * Register a sysctl table. @table should be a filled in ctl_table
1254 * array. A completely 0 filled entry terminates the table.
1256 * See __register_sysctl_table for more details.
1258 struct ctl_table_header
*register_sysctl(const char *path
, struct ctl_table
*table
)
1260 return __register_sysctl_table(&sysctl_table_root
.default_set
,
1263 EXPORT_SYMBOL(register_sysctl
);
1265 static char *append_path(const char *path
, char *pos
, const char *name
)
1268 namelen
= strlen(name
);
1269 if (((pos
- path
) + namelen
+ 2) >= PATH_MAX
)
1271 memcpy(pos
, name
, namelen
);
1273 pos
[namelen
+ 1] = '\0';
1278 static int count_subheaders(struct ctl_table
*table
)
1281 int nr_subheaders
= 0;
1282 struct ctl_table
*entry
;
1284 /* special case: no directory and empty directory */
1285 if (!table
|| !table
->procname
)
1288 for (entry
= table
; entry
->procname
; entry
++) {
1290 nr_subheaders
+= count_subheaders(entry
->child
);
1294 return nr_subheaders
+ has_files
;
1297 static int register_leaf_sysctl_tables(const char *path
, char *pos
,
1298 struct ctl_table_header
***subheader
, struct ctl_table_set
*set
,
1299 struct ctl_table
*table
)
1301 struct ctl_table
*ctl_table_arg
= NULL
;
1302 struct ctl_table
*entry
, *files
;
1307 for (entry
= table
; entry
->procname
; entry
++) {
1315 /* If there are mixed files and directories we need a new table */
1316 if (nr_dirs
&& nr_files
) {
1317 struct ctl_table
*new;
1318 files
= kzalloc(sizeof(struct ctl_table
) * (nr_files
+ 1),
1323 ctl_table_arg
= files
;
1324 for (new = files
, entry
= table
; entry
->procname
; entry
++) {
1332 /* Register everything except a directory full of subdirectories */
1333 if (nr_files
|| !nr_dirs
) {
1334 struct ctl_table_header
*header
;
1335 header
= __register_sysctl_table(set
, path
, files
);
1337 kfree(ctl_table_arg
);
1341 /* Remember if we need to free the file table */
1342 header
->ctl_table_arg
= ctl_table_arg
;
1343 **subheader
= header
;
1347 /* Recurse into the subdirectories. */
1348 for (entry
= table
; entry
->procname
; entry
++) {
1354 err
= -ENAMETOOLONG
;
1355 child_pos
= append_path(path
, pos
, entry
->procname
);
1359 err
= register_leaf_sysctl_tables(path
, child_pos
, subheader
,
1367 /* On failure our caller will unregister all registered subheaders */
1372 * __register_sysctl_paths - register a sysctl table hierarchy
1373 * @set: Sysctl tree to register on
1374 * @path: The path to the directory the sysctl table is in.
1375 * @table: the top-level table structure
1377 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1378 * array. A completely 0 filled entry terminates the table.
1380 * See __register_sysctl_table for more details.
1382 struct ctl_table_header
*__register_sysctl_paths(
1383 struct ctl_table_set
*set
,
1384 const struct ctl_path
*path
, struct ctl_table
*table
)
1386 struct ctl_table
*ctl_table_arg
= table
;
1387 int nr_subheaders
= count_subheaders(table
);
1388 struct ctl_table_header
*header
= NULL
, **subheaders
, **subheader
;
1389 const struct ctl_path
*component
;
1390 char *new_path
, *pos
;
1392 pos
= new_path
= kmalloc(PATH_MAX
, GFP_KERNEL
);
1397 for (component
= path
; component
->procname
; component
++) {
1398 pos
= append_path(new_path
, pos
, component
->procname
);
1402 while (table
->procname
&& table
->child
&& !table
[1].procname
) {
1403 pos
= append_path(new_path
, pos
, table
->procname
);
1406 table
= table
->child
;
1408 if (nr_subheaders
== 1) {
1409 header
= __register_sysctl_table(set
, new_path
, table
);
1411 header
->ctl_table_arg
= ctl_table_arg
;
1413 header
= kzalloc(sizeof(*header
) +
1414 sizeof(*subheaders
)*nr_subheaders
, GFP_KERNEL
);
1418 subheaders
= (struct ctl_table_header
**) (header
+ 1);
1419 subheader
= subheaders
;
1420 header
->ctl_table_arg
= ctl_table_arg
;
1422 if (register_leaf_sysctl_tables(new_path
, pos
, &subheader
,
1424 goto err_register_leaves
;
1431 err_register_leaves
:
1432 while (subheader
> subheaders
) {
1433 struct ctl_table_header
*subh
= *(--subheader
);
1434 struct ctl_table
*table
= subh
->ctl_table_arg
;
1435 unregister_sysctl_table(subh
);
1444 * register_sysctl_table_path - register a sysctl table hierarchy
1445 * @path: The path to the directory the sysctl table is in.
1446 * @table: the top-level table structure
1448 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1449 * array. A completely 0 filled entry terminates the table.
1451 * See __register_sysctl_paths for more details.
1453 struct ctl_table_header
*register_sysctl_paths(const struct ctl_path
*path
,
1454 struct ctl_table
*table
)
1456 return __register_sysctl_paths(&sysctl_table_root
.default_set
,
1459 EXPORT_SYMBOL(register_sysctl_paths
);
1462 * register_sysctl_table - register a sysctl table hierarchy
1463 * @table: the top-level table structure
1465 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1466 * array. A completely 0 filled entry terminates the table.
1468 * See register_sysctl_paths for more details.
1470 struct ctl_table_header
*register_sysctl_table(struct ctl_table
*table
)
1472 static const struct ctl_path null_path
[] = { {} };
1474 return register_sysctl_paths(null_path
, table
);
1476 EXPORT_SYMBOL(register_sysctl_table
);
1478 static void put_links(struct ctl_table_header
*header
)
1480 struct ctl_table_set
*root_set
= &sysctl_table_root
.default_set
;
1481 struct ctl_table_root
*root
= header
->root
;
1482 struct ctl_dir
*parent
= header
->parent
;
1483 struct ctl_dir
*core_parent
;
1484 struct ctl_table
*entry
;
1486 if (header
->set
== root_set
)
1489 core_parent
= xlate_dir(root_set
, parent
);
1490 if (IS_ERR(core_parent
))
1493 for (entry
= header
->ctl_table
; entry
->procname
; entry
++) {
1494 struct ctl_table_header
*link_head
;
1495 struct ctl_table
*link
;
1496 const char *name
= entry
->procname
;
1498 link
= find_entry(&link_head
, core_parent
, name
, strlen(name
));
1500 ((S_ISDIR(link
->mode
) && S_ISDIR(entry
->mode
)) ||
1501 (S_ISLNK(link
->mode
) && (link
->data
== root
)))) {
1502 drop_sysctl_table(link_head
);
1505 printk(KERN_ERR
"sysctl link missing during unregister: ");
1506 sysctl_print_dir(parent
);
1507 printk(KERN_CONT
"/%s\n", name
);
1512 static void drop_sysctl_table(struct ctl_table_header
*header
)
1514 struct ctl_dir
*parent
= header
->parent
;
1520 start_unregistering(header
);
1521 if (!--header
->count
)
1522 kfree_rcu(header
, rcu
);
1525 drop_sysctl_table(&parent
->header
);
1529 * unregister_sysctl_table - unregister a sysctl table hierarchy
1530 * @header: the header returned from register_sysctl_table
1532 * Unregisters the sysctl table and all children. proc entries may not
1533 * actually be removed until they are no longer used by anyone.
1535 void unregister_sysctl_table(struct ctl_table_header
* header
)
1543 nr_subheaders
= count_subheaders(header
->ctl_table_arg
);
1544 if (unlikely(nr_subheaders
> 1)) {
1545 struct ctl_table_header
**subheaders
;
1548 subheaders
= (struct ctl_table_header
**)(header
+ 1);
1549 for (i
= nr_subheaders
-1; i
>= 0; i
--) {
1550 struct ctl_table_header
*subh
= subheaders
[i
];
1551 struct ctl_table
*table
= subh
->ctl_table_arg
;
1552 unregister_sysctl_table(subh
);
1559 spin_lock(&sysctl_lock
);
1560 drop_sysctl_table(header
);
1561 spin_unlock(&sysctl_lock
);
1563 EXPORT_SYMBOL(unregister_sysctl_table
);
1565 void setup_sysctl_set(struct ctl_table_set
*set
,
1566 struct ctl_table_root
*root
,
1567 int (*is_seen
)(struct ctl_table_set
*))
1569 memset(set
, 0, sizeof(*set
));
1570 set
->is_seen
= is_seen
;
1571 init_header(&set
->dir
.header
, root
, set
, NULL
, root_table
);
1574 void retire_sysctl_set(struct ctl_table_set
*set
)
1576 WARN_ON(!RB_EMPTY_ROOT(&set
->dir
.root
));
1579 int __init
proc_sys_init(void)
1581 struct proc_dir_entry
*proc_sys_root
;
1583 proc_sys_root
= proc_mkdir("sys", NULL
);
1584 proc_sys_root
->proc_iops
= &proc_sys_dir_operations
;
1585 proc_sys_root
->proc_fops
= &proc_sys_dir_file_operations
;
1586 proc_sys_root
->nlink
= 0;
1588 return sysctl_init();