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
.list
= LIST_HEAD_INIT(sysctl_table_root
.default_set
.dir
.header
.ctl_entry
),
37 .default_set
.dir
.header
= {
40 .ctl_table
= root_table
,
41 .ctl_entry
= LIST_HEAD_INIT(sysctl_table_root
.default_set
.list
),}},
42 .ctl_table_arg
= root_table
,
43 .root
= &sysctl_table_root
,
44 .set
= &sysctl_table_root
.default_set
,
48 static DEFINE_SPINLOCK(sysctl_lock
);
50 static void drop_sysctl_table(struct ctl_table_header
*header
);
51 static int sysctl_follow_link(struct ctl_table_header
**phead
,
52 struct ctl_table
**pentry
, struct nsproxy
*namespaces
);
53 static int insert_links(struct ctl_table_header
*head
);
54 static void put_links(struct ctl_table_header
*header
);
55 static int sysctl_check_dups(struct ctl_dir
*dir
, struct ctl_table
*table
);
57 static void sysctl_print_dir(struct ctl_dir
*dir
)
59 if (dir
->header
.parent
)
60 sysctl_print_dir(dir
->header
.parent
);
61 printk(KERN_CONT
"%s/", dir
->header
.ctl_table
[0].procname
);
64 static int namecmp(const char *name1
, int len1
, const char *name2
, int len2
)
73 cmp
= memcmp(name1
, name2
, minlen
);
79 static struct ctl_table
*find_entry(struct ctl_table_header
**phead
,
80 struct ctl_dir
*dir
, const char *name
, int namelen
)
82 struct ctl_table_set
*set
= dir
->header
.set
;
83 struct ctl_table_header
*head
;
84 struct ctl_table
*entry
;
86 list_for_each_entry(head
, &set
->list
, ctl_entry
) {
87 if (head
->unregistering
)
89 if (head
->parent
!= dir
)
91 for (entry
= head
->ctl_table
; entry
->procname
; entry
++) {
92 const char *procname
= entry
->procname
;
93 if (namecmp(procname
, strlen(procname
), name
, namelen
) == 0) {
102 static void init_header(struct ctl_table_header
*head
,
103 struct ctl_table_root
*root
, struct ctl_table_set
*set
,
104 struct ctl_table
*table
)
106 head
->ctl_table
= table
;
107 head
->ctl_table_arg
= table
;
108 INIT_LIST_HEAD(&head
->ctl_entry
);
112 head
->unregistering
= NULL
;
118 static void erase_header(struct ctl_table_header
*head
)
120 list_del_init(&head
->ctl_entry
);
123 static int insert_header(struct ctl_dir
*dir
, struct ctl_table_header
*header
)
127 err
= sysctl_check_dups(dir
, header
->ctl_table
);
132 header
->parent
= dir
;
133 err
= insert_links(header
);
136 list_add_tail(&header
->ctl_entry
, &header
->set
->list
);
139 header
->parent
= NULL
;
140 drop_sysctl_table(&dir
->header
);
144 /* called under sysctl_lock */
145 static int use_table(struct ctl_table_header
*p
)
147 if (unlikely(p
->unregistering
))
153 /* called under sysctl_lock */
154 static void unuse_table(struct ctl_table_header
*p
)
157 if (unlikely(p
->unregistering
))
158 complete(p
->unregistering
);
161 /* called under sysctl_lock, will reacquire if has to wait */
162 static void start_unregistering(struct ctl_table_header
*p
)
165 * if p->used is 0, nobody will ever touch that entry again;
166 * we'll eliminate all paths to it before dropping sysctl_lock
168 if (unlikely(p
->used
)) {
169 struct completion wait
;
170 init_completion(&wait
);
171 p
->unregistering
= &wait
;
172 spin_unlock(&sysctl_lock
);
173 wait_for_completion(&wait
);
174 spin_lock(&sysctl_lock
);
176 /* anything non-NULL; we'll never dereference it */
177 p
->unregistering
= ERR_PTR(-EINVAL
);
180 * do not remove from the list until nobody holds it; walking the
181 * list in do_sysctl() relies on that.
186 static void sysctl_head_get(struct ctl_table_header
*head
)
188 spin_lock(&sysctl_lock
);
190 spin_unlock(&sysctl_lock
);
193 void sysctl_head_put(struct ctl_table_header
*head
)
195 spin_lock(&sysctl_lock
);
197 kfree_rcu(head
, rcu
);
198 spin_unlock(&sysctl_lock
);
201 static struct ctl_table_header
*sysctl_head_grab(struct ctl_table_header
*head
)
205 spin_lock(&sysctl_lock
);
206 if (!use_table(head
))
207 head
= ERR_PTR(-ENOENT
);
208 spin_unlock(&sysctl_lock
);
212 static void sysctl_head_finish(struct ctl_table_header
*head
)
216 spin_lock(&sysctl_lock
);
218 spin_unlock(&sysctl_lock
);
221 static struct ctl_table_set
*
222 lookup_header_set(struct ctl_table_root
*root
, struct nsproxy
*namespaces
)
224 struct ctl_table_set
*set
= &root
->default_set
;
226 set
= root
->lookup(root
, namespaces
);
230 static struct ctl_table
*lookup_entry(struct ctl_table_header
**phead
,
232 const char *name
, int namelen
)
234 struct ctl_table_header
*head
;
235 struct ctl_table
*entry
;
237 spin_lock(&sysctl_lock
);
238 entry
= find_entry(&head
, dir
, name
, namelen
);
239 if (entry
&& use_table(head
))
243 spin_unlock(&sysctl_lock
);
247 static struct ctl_table_header
*next_usable_entry(struct ctl_dir
*dir
,
248 struct list_head
*tmp
)
250 struct ctl_table_set
*set
= dir
->header
.set
;
251 struct ctl_table_header
*head
;
253 for (tmp
= tmp
->next
; tmp
!= &set
->list
; tmp
= tmp
->next
) {
254 head
= list_entry(tmp
, struct ctl_table_header
, ctl_entry
);
256 if (head
->parent
!= dir
||
257 !head
->ctl_table
->procname
||
266 static void first_entry(struct ctl_dir
*dir
,
267 struct ctl_table_header
**phead
, struct ctl_table
**pentry
)
269 struct ctl_table_header
*head
;
270 struct ctl_table
*entry
= NULL
;
272 spin_lock(&sysctl_lock
);
273 head
= next_usable_entry(dir
, &dir
->header
.set
->list
);
274 spin_unlock(&sysctl_lock
);
276 entry
= head
->ctl_table
;
281 static void next_entry(struct ctl_table_header
**phead
, struct ctl_table
**pentry
)
283 struct ctl_table_header
*head
= *phead
;
284 struct ctl_table
*entry
= *pentry
;
287 if (!entry
->procname
) {
288 spin_lock(&sysctl_lock
);
290 head
= next_usable_entry(head
->parent
, &head
->ctl_entry
);
291 spin_unlock(&sysctl_lock
);
293 entry
= head
->ctl_table
;
299 void register_sysctl_root(struct ctl_table_root
*root
)
304 * sysctl_perm does NOT grant the superuser all rights automatically, because
305 * some sysctl variables are readonly even to root.
308 static int test_perm(int mode
, int op
)
312 else if (in_egroup_p(0))
314 if ((op
& ~mode
& (MAY_READ
|MAY_WRITE
|MAY_EXEC
)) == 0)
319 static int sysctl_perm(struct ctl_table_root
*root
, struct ctl_table
*table
, int op
)
323 if (root
->permissions
)
324 mode
= root
->permissions(root
, current
->nsproxy
, table
);
328 return test_perm(mode
, op
);
331 static struct inode
*proc_sys_make_inode(struct super_block
*sb
,
332 struct ctl_table_header
*head
, struct ctl_table
*table
)
335 struct proc_inode
*ei
;
337 inode
= new_inode(sb
);
341 inode
->i_ino
= get_next_ino();
343 sysctl_head_get(head
);
346 ei
->sysctl_entry
= table
;
348 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
349 inode
->i_mode
= table
->mode
;
350 if (!S_ISDIR(table
->mode
)) {
351 inode
->i_mode
|= S_IFREG
;
352 inode
->i_op
= &proc_sys_inode_operations
;
353 inode
->i_fop
= &proc_sys_file_operations
;
355 inode
->i_mode
|= S_IFDIR
;
356 inode
->i_op
= &proc_sys_dir_operations
;
357 inode
->i_fop
= &proc_sys_dir_file_operations
;
363 static struct ctl_table_header
*grab_header(struct inode
*inode
)
365 struct ctl_table_header
*head
= PROC_I(inode
)->sysctl
;
367 head
= &sysctl_table_root
.default_set
.dir
.header
;
368 return sysctl_head_grab(head
);
371 static struct dentry
*proc_sys_lookup(struct inode
*dir
, struct dentry
*dentry
,
372 struct nameidata
*nd
)
374 struct ctl_table_header
*head
= grab_header(dir
);
375 struct ctl_table_header
*h
= NULL
;
376 struct qstr
*name
= &dentry
->d_name
;
379 struct dentry
*err
= ERR_PTR(-ENOENT
);
380 struct ctl_dir
*ctl_dir
;
384 return ERR_CAST(head
);
386 ctl_dir
= container_of(head
, struct ctl_dir
, header
);
388 p
= lookup_entry(&h
, ctl_dir
, name
->name
, name
->len
);
392 ret
= sysctl_follow_link(&h
, &p
, current
->nsproxy
);
397 err
= ERR_PTR(-ENOMEM
);
398 inode
= proc_sys_make_inode(dir
->i_sb
, h
? h
: head
, p
);
400 sysctl_head_finish(h
);
406 d_set_d_op(dentry
, &proc_sys_dentry_operations
);
407 d_add(dentry
, inode
);
410 sysctl_head_finish(head
);
414 static ssize_t
proc_sys_call_handler(struct file
*filp
, void __user
*buf
,
415 size_t count
, loff_t
*ppos
, int write
)
417 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
418 struct ctl_table_header
*head
= grab_header(inode
);
419 struct ctl_table
*table
= PROC_I(inode
)->sysctl_entry
;
424 return PTR_ERR(head
);
427 * At this point we know that the sysctl was not unregistered
428 * and won't be until we finish.
431 if (sysctl_perm(head
->root
, table
, write
? MAY_WRITE
: MAY_READ
))
434 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
436 if (!table
->proc_handler
)
439 /* careful: calling conventions are nasty here */
441 error
= table
->proc_handler(table
, write
, buf
, &res
, ppos
);
445 sysctl_head_finish(head
);
450 static ssize_t
proc_sys_read(struct file
*filp
, char __user
*buf
,
451 size_t count
, loff_t
*ppos
)
453 return proc_sys_call_handler(filp
, (void __user
*)buf
, count
, ppos
, 0);
456 static ssize_t
proc_sys_write(struct file
*filp
, const char __user
*buf
,
457 size_t count
, loff_t
*ppos
)
459 return proc_sys_call_handler(filp
, (void __user
*)buf
, count
, ppos
, 1);
462 static int proc_sys_open(struct inode
*inode
, struct file
*filp
)
464 struct ctl_table
*table
= PROC_I(inode
)->sysctl_entry
;
467 filp
->private_data
= proc_sys_poll_event(table
->poll
);
472 static unsigned int proc_sys_poll(struct file
*filp
, poll_table
*wait
)
474 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
475 struct ctl_table
*table
= PROC_I(inode
)->sysctl_entry
;
476 unsigned long event
= (unsigned long)filp
->private_data
;
477 unsigned int ret
= DEFAULT_POLLMASK
;
479 if (!table
->proc_handler
)
485 poll_wait(filp
, &table
->poll
->wait
, wait
);
487 if (event
!= atomic_read(&table
->poll
->event
)) {
488 filp
->private_data
= proc_sys_poll_event(table
->poll
);
489 ret
= POLLIN
| POLLRDNORM
| POLLERR
| POLLPRI
;
496 static int proc_sys_fill_cache(struct file
*filp
, void *dirent
,
498 struct ctl_table_header
*head
,
499 struct ctl_table
*table
)
501 struct dentry
*child
, *dir
= filp
->f_path
.dentry
;
505 unsigned type
= DT_UNKNOWN
;
507 qname
.name
= table
->procname
;
508 qname
.len
= strlen(table
->procname
);
509 qname
.hash
= full_name_hash(qname
.name
, qname
.len
);
511 child
= d_lookup(dir
, &qname
);
513 child
= d_alloc(dir
, &qname
);
515 inode
= proc_sys_make_inode(dir
->d_sb
, head
, table
);
520 d_set_d_op(child
, &proc_sys_dentry_operations
);
527 inode
= child
->d_inode
;
529 type
= inode
->i_mode
>> 12;
531 return !!filldir(dirent
, qname
.name
, qname
.len
, filp
->f_pos
, ino
, type
);
534 static int proc_sys_link_fill_cache(struct file
*filp
, void *dirent
,
536 struct ctl_table_header
*head
,
537 struct ctl_table
*table
)
540 head
= sysctl_head_grab(head
);
542 /* It is not an error if we can not follow the link ignore it */
543 err
= sysctl_follow_link(&head
, &table
, current
->nsproxy
);
547 ret
= proc_sys_fill_cache(filp
, dirent
, filldir
, head
, table
);
549 sysctl_head_finish(head
);
553 static int scan(struct ctl_table_header
*head
, ctl_table
*table
,
554 unsigned long *pos
, struct file
*file
,
555 void *dirent
, filldir_t filldir
)
559 if ((*pos
)++ < file
->f_pos
)
562 if (unlikely(S_ISLNK(table
->mode
)))
563 res
= proc_sys_link_fill_cache(file
, dirent
, filldir
, head
, table
);
565 res
= proc_sys_fill_cache(file
, dirent
, filldir
, head
, table
);
573 static int proc_sys_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
575 struct dentry
*dentry
= filp
->f_path
.dentry
;
576 struct inode
*inode
= dentry
->d_inode
;
577 struct ctl_table_header
*head
= grab_header(inode
);
578 struct ctl_table_header
*h
= NULL
;
579 struct ctl_table
*entry
;
580 struct ctl_dir
*ctl_dir
;
585 return PTR_ERR(head
);
587 ctl_dir
= container_of(head
, struct ctl_dir
, header
);
590 /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
591 if (filp
->f_pos
== 0) {
592 if (filldir(dirent
, ".", 1, filp
->f_pos
,
593 inode
->i_ino
, DT_DIR
) < 0)
597 if (filp
->f_pos
== 1) {
598 if (filldir(dirent
, "..", 2, filp
->f_pos
,
599 parent_ino(dentry
), DT_DIR
) < 0)
605 for (first_entry(ctl_dir
, &h
, &entry
); h
; next_entry(&h
, &entry
)) {
606 ret
= scan(h
, entry
, &pos
, filp
, dirent
, filldir
);
608 sysctl_head_finish(h
);
614 sysctl_head_finish(head
);
618 static int proc_sys_permission(struct inode
*inode
, int mask
)
621 * sysctl entries that are not writeable,
622 * are _NOT_ writeable, capabilities or not.
624 struct ctl_table_header
*head
;
625 struct ctl_table
*table
;
628 /* Executable files are not allowed under /proc/sys/ */
629 if ((mask
& MAY_EXEC
) && S_ISREG(inode
->i_mode
))
632 head
= grab_header(inode
);
634 return PTR_ERR(head
);
636 table
= PROC_I(inode
)->sysctl_entry
;
637 if (!table
) /* global root - r-xr-xr-x */
638 error
= mask
& MAY_WRITE
? -EACCES
: 0;
639 else /* Use the permissions on the sysctl table entry */
640 error
= sysctl_perm(head
->root
, table
, mask
& ~MAY_NOT_BLOCK
);
642 sysctl_head_finish(head
);
646 static int proc_sys_setattr(struct dentry
*dentry
, struct iattr
*attr
)
648 struct inode
*inode
= dentry
->d_inode
;
651 if (attr
->ia_valid
& (ATTR_MODE
| ATTR_UID
| ATTR_GID
))
654 error
= inode_change_ok(inode
, attr
);
658 if ((attr
->ia_valid
& ATTR_SIZE
) &&
659 attr
->ia_size
!= i_size_read(inode
)) {
660 error
= vmtruncate(inode
, attr
->ia_size
);
665 setattr_copy(inode
, attr
);
666 mark_inode_dirty(inode
);
670 static int proc_sys_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
672 struct inode
*inode
= dentry
->d_inode
;
673 struct ctl_table_header
*head
= grab_header(inode
);
674 struct ctl_table
*table
= PROC_I(inode
)->sysctl_entry
;
677 return PTR_ERR(head
);
679 generic_fillattr(inode
, stat
);
681 stat
->mode
= (stat
->mode
& S_IFMT
) | table
->mode
;
683 sysctl_head_finish(head
);
687 static const struct file_operations proc_sys_file_operations
= {
688 .open
= proc_sys_open
,
689 .poll
= proc_sys_poll
,
690 .read
= proc_sys_read
,
691 .write
= proc_sys_write
,
692 .llseek
= default_llseek
,
695 static const struct file_operations proc_sys_dir_file_operations
= {
696 .read
= generic_read_dir
,
697 .readdir
= proc_sys_readdir
,
698 .llseek
= generic_file_llseek
,
701 static const struct inode_operations proc_sys_inode_operations
= {
702 .permission
= proc_sys_permission
,
703 .setattr
= proc_sys_setattr
,
704 .getattr
= proc_sys_getattr
,
707 static const struct inode_operations proc_sys_dir_operations
= {
708 .lookup
= proc_sys_lookup
,
709 .permission
= proc_sys_permission
,
710 .setattr
= proc_sys_setattr
,
711 .getattr
= proc_sys_getattr
,
714 static int proc_sys_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
716 if (nd
->flags
& LOOKUP_RCU
)
718 return !PROC_I(dentry
->d_inode
)->sysctl
->unregistering
;
721 static int proc_sys_delete(const struct dentry
*dentry
)
723 return !!PROC_I(dentry
->d_inode
)->sysctl
->unregistering
;
726 static int sysctl_is_seen(struct ctl_table_header
*p
)
728 struct ctl_table_set
*set
= p
->set
;
730 spin_lock(&sysctl_lock
);
731 if (p
->unregistering
)
733 else if (!set
->is_seen
)
736 res
= set
->is_seen(set
);
737 spin_unlock(&sysctl_lock
);
741 static int proc_sys_compare(const struct dentry
*parent
,
742 const struct inode
*pinode
,
743 const struct dentry
*dentry
, const struct inode
*inode
,
744 unsigned int len
, const char *str
, const struct qstr
*name
)
746 struct ctl_table_header
*head
;
747 /* Although proc doesn't have negative dentries, rcu-walk means
748 * that inode here can be NULL */
749 /* AV: can it, indeed? */
752 if (name
->len
!= len
)
754 if (memcmp(name
->name
, str
, len
))
756 head
= rcu_dereference(PROC_I(inode
)->sysctl
);
757 return !head
|| !sysctl_is_seen(head
);
760 static const struct dentry_operations proc_sys_dentry_operations
= {
761 .d_revalidate
= proc_sys_revalidate
,
762 .d_delete
= proc_sys_delete
,
763 .d_compare
= proc_sys_compare
,
766 static struct ctl_dir
*find_subdir(struct ctl_dir
*dir
,
767 const char *name
, int namelen
)
769 struct ctl_table_header
*head
;
770 struct ctl_table
*entry
;
772 entry
= find_entry(&head
, dir
, name
, namelen
);
774 return ERR_PTR(-ENOENT
);
775 if (S_ISDIR(entry
->mode
))
776 return container_of(head
, struct ctl_dir
, header
);
777 return ERR_PTR(-ENOTDIR
);
780 static struct ctl_dir
*new_dir(struct ctl_table_set
*set
,
781 const char *name
, int namelen
)
783 struct ctl_table
*table
;
787 new = kzalloc(sizeof(*new) + sizeof(struct ctl_table
)*2 +
788 namelen
+ 1, GFP_KERNEL
);
792 table
= (struct ctl_table
*)(new + 1);
793 new_name
= (char *)(table
+ 2);
794 memcpy(new_name
, name
, namelen
);
795 new_name
[namelen
] = '\0';
796 table
[0].procname
= new_name
;
797 table
[0].mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
798 init_header(&new->header
, set
->dir
.header
.root
, set
, table
);
803 static struct ctl_dir
*get_subdir(struct ctl_dir
*dir
,
804 const char *name
, int namelen
)
806 struct ctl_table_set
*set
= dir
->header
.set
;
807 struct ctl_dir
*subdir
, *new = NULL
;
809 spin_lock(&sysctl_lock
);
810 subdir
= find_subdir(dir
, name
, namelen
);
813 if (PTR_ERR(subdir
) != -ENOENT
)
816 spin_unlock(&sysctl_lock
);
817 new = new_dir(set
, name
, namelen
);
818 spin_lock(&sysctl_lock
);
819 subdir
= ERR_PTR(-ENOMEM
);
823 subdir
= find_subdir(dir
, name
, namelen
);
826 if (PTR_ERR(subdir
) != -ENOENT
)
829 if (insert_header(dir
, &new->header
))
833 subdir
->header
.nreg
++;
835 if (unlikely(IS_ERR(subdir
))) {
836 printk(KERN_ERR
"sysctl could not get directory: ");
837 sysctl_print_dir(dir
);
838 printk(KERN_CONT
"/%*.*s %ld\n",
839 namelen
, namelen
, name
, PTR_ERR(subdir
));
841 drop_sysctl_table(&dir
->header
);
843 drop_sysctl_table(&new->header
);
844 spin_unlock(&sysctl_lock
);
848 static struct ctl_dir
*xlate_dir(struct ctl_table_set
*set
, struct ctl_dir
*dir
)
850 struct ctl_dir
*parent
;
851 const char *procname
;
852 if (!dir
->header
.parent
)
854 parent
= xlate_dir(set
, dir
->header
.parent
);
857 procname
= dir
->header
.ctl_table
[0].procname
;
858 return find_subdir(parent
, procname
, strlen(procname
));
861 static int sysctl_follow_link(struct ctl_table_header
**phead
,
862 struct ctl_table
**pentry
, struct nsproxy
*namespaces
)
864 struct ctl_table_header
*head
;
865 struct ctl_table_root
*root
;
866 struct ctl_table_set
*set
;
867 struct ctl_table
*entry
;
871 /* Get out quickly if not a link */
872 if (!S_ISLNK((*pentry
)->mode
))
876 spin_lock(&sysctl_lock
);
877 root
= (*pentry
)->data
;
878 set
= lookup_header_set(root
, namespaces
);
879 dir
= xlate_dir(set
, (*phead
)->parent
);
883 const char *procname
= (*pentry
)->procname
;
885 entry
= find_entry(&head
, dir
, procname
, strlen(procname
));
887 if (entry
&& use_table(head
)) {
895 spin_unlock(&sysctl_lock
);
899 static int sysctl_check_table_dups(struct ctl_dir
*dir
, struct ctl_table
*old
,
900 struct ctl_table
*table
)
902 struct ctl_table
*entry
, *test
;
905 for (entry
= old
; entry
->procname
; entry
++) {
906 for (test
= table
; test
->procname
; test
++) {
907 if (strcmp(entry
->procname
, test
->procname
) == 0) {
908 printk(KERN_ERR
"sysctl duplicate entry: ");
909 sysctl_print_dir(dir
);
910 printk(KERN_CONT
"/%s\n", test
->procname
);
918 static int sysctl_check_dups(struct ctl_dir
*dir
, struct ctl_table
*table
)
920 struct ctl_table_set
*set
;
921 struct ctl_table_header
*head
;
924 set
= dir
->header
.set
;
925 list_for_each_entry(head
, &set
->list
, ctl_entry
) {
926 if (head
->unregistering
)
928 if (head
->parent
!= dir
)
930 error
= sysctl_check_table_dups(dir
, head
->ctl_table
, table
);
935 static int sysctl_err(const char *path
, struct ctl_table
*table
, char *fmt
, ...)
937 struct va_format vaf
;
944 printk(KERN_ERR
"sysctl table check failed: %s/%s %pV\n",
945 path
, table
->procname
, &vaf
);
951 static int sysctl_check_table(const char *path
, struct ctl_table
*table
)
954 for (; table
->procname
; table
++) {
956 err
= sysctl_err(path
, table
, "Not a file");
958 if ((table
->proc_handler
== proc_dostring
) ||
959 (table
->proc_handler
== proc_dointvec
) ||
960 (table
->proc_handler
== proc_dointvec_minmax
) ||
961 (table
->proc_handler
== proc_dointvec_jiffies
) ||
962 (table
->proc_handler
== proc_dointvec_userhz_jiffies
) ||
963 (table
->proc_handler
== proc_dointvec_ms_jiffies
) ||
964 (table
->proc_handler
== proc_doulongvec_minmax
) ||
965 (table
->proc_handler
== proc_doulongvec_ms_jiffies_minmax
)) {
967 err
= sysctl_err(path
, table
, "No data");
969 err
= sysctl_err(path
, table
, "No maxlen");
971 if (!table
->proc_handler
)
972 err
= sysctl_err(path
, table
, "No proc_handler");
974 if ((table
->mode
& (S_IRUGO
|S_IWUGO
)) != table
->mode
)
975 err
= sysctl_err(path
, table
, "bogus .mode 0%o",
981 static struct ctl_table_header
*new_links(struct ctl_dir
*dir
, struct ctl_table
*table
,
982 struct ctl_table_root
*link_root
)
984 struct ctl_table
*link_table
, *entry
, *link
;
985 struct ctl_table_header
*links
;
987 int nr_entries
, name_bytes
;
991 for (entry
= table
; entry
->procname
; entry
++) {
993 name_bytes
+= strlen(entry
->procname
) + 1;
996 links
= kzalloc(sizeof(struct ctl_table_header
) +
997 sizeof(struct ctl_table
)*(nr_entries
+ 1) +
1004 link_table
= (struct ctl_table
*)(links
+ 1);
1005 link_name
= (char *)&link_table
[nr_entries
+ 1];
1007 for (link
= link_table
, entry
= table
; entry
->procname
; link
++, entry
++) {
1008 int len
= strlen(entry
->procname
) + 1;
1009 memcpy(link_name
, entry
->procname
, len
);
1010 link
->procname
= link_name
;
1011 link
->mode
= S_IFLNK
|S_IRWXUGO
;
1012 link
->data
= link_root
;
1015 init_header(links
, dir
->header
.root
, dir
->header
.set
, link_table
);
1016 links
->nreg
= nr_entries
;
1021 static bool get_links(struct ctl_dir
*dir
,
1022 struct ctl_table
*table
, struct ctl_table_root
*link_root
)
1024 struct ctl_table_header
*head
;
1025 struct ctl_table
*entry
, *link
;
1027 /* Are there links available for every entry in table? */
1028 for (entry
= table
; entry
->procname
; entry
++) {
1029 const char *procname
= entry
->procname
;
1030 link
= find_entry(&head
, dir
, procname
, strlen(procname
));
1033 if (S_ISDIR(link
->mode
) && S_ISDIR(entry
->mode
))
1035 if (S_ISLNK(link
->mode
) && (link
->data
== link_root
))
1040 /* The checks passed. Increase the registration count on the links */
1041 for (entry
= table
; entry
->procname
; entry
++) {
1042 const char *procname
= entry
->procname
;
1043 link
= find_entry(&head
, dir
, procname
, strlen(procname
));
1049 static int insert_links(struct ctl_table_header
*head
)
1051 struct ctl_table_set
*root_set
= &sysctl_table_root
.default_set
;
1052 struct ctl_dir
*core_parent
= NULL
;
1053 struct ctl_table_header
*links
;
1056 if (head
->set
== root_set
)
1059 core_parent
= xlate_dir(root_set
, head
->parent
);
1060 if (IS_ERR(core_parent
))
1063 if (get_links(core_parent
, head
->ctl_table
, head
->root
))
1066 core_parent
->header
.nreg
++;
1067 spin_unlock(&sysctl_lock
);
1069 links
= new_links(core_parent
, head
->ctl_table
, head
->root
);
1071 spin_lock(&sysctl_lock
);
1077 if (get_links(core_parent
, head
->ctl_table
, head
->root
)) {
1082 err
= insert_header(core_parent
, links
);
1086 drop_sysctl_table(&core_parent
->header
);
1091 * __register_sysctl_table - register a leaf sysctl table
1092 * @set: Sysctl tree to register on
1093 * @path: The path to the directory the sysctl table is in.
1094 * @table: the top-level table structure
1096 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1097 * array. A completely 0 filled entry terminates the table.
1099 * The members of the &struct ctl_table structure are used as follows:
1101 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
1102 * enter a sysctl file
1104 * data - a pointer to data for use by proc_handler
1106 * maxlen - the maximum size in bytes of the data
1108 * mode - the file permissions for the /proc/sys file
1110 * child - must be %NULL.
1112 * proc_handler - the text handler routine (described below)
1114 * extra1, extra2 - extra pointers usable by the proc handler routines
1116 * Leaf nodes in the sysctl tree will be represented by a single file
1117 * under /proc; non-leaf nodes will be represented by directories.
1119 * There must be a proc_handler routine for any terminal nodes.
1120 * Several default handlers are available to cover common cases -
1122 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
1123 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
1124 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
1126 * It is the handler's job to read the input buffer from user memory
1127 * and process it. The handler should return 0 on success.
1129 * This routine returns %NULL on a failure to register, and a pointer
1130 * to the table header on success.
1132 struct ctl_table_header
*__register_sysctl_table(
1133 struct ctl_table_set
*set
,
1134 const char *path
, struct ctl_table
*table
)
1136 struct ctl_table_root
*root
= set
->dir
.header
.root
;
1137 struct ctl_table_header
*links
= NULL
;
1138 struct ctl_table_header
*header
;
1139 const char *name
, *nextname
;
1140 struct ctl_dir
*dir
;
1142 header
= kzalloc(sizeof(struct ctl_table_header
), GFP_KERNEL
);
1146 init_header(header
, root
, set
, table
);
1147 if (sysctl_check_table(path
, table
))
1150 spin_lock(&sysctl_lock
);
1153 spin_unlock(&sysctl_lock
);
1155 /* Find the directory for the ctl_table */
1156 for (name
= path
; name
; name
= nextname
) {
1158 nextname
= strchr(name
, '/');
1160 namelen
= nextname
- name
;
1163 namelen
= strlen(name
);
1168 dir
= get_subdir(dir
, name
, namelen
);
1173 spin_lock(&sysctl_lock
);
1174 if (insert_header(dir
, header
))
1175 goto fail_put_dir_locked
;
1177 drop_sysctl_table(&dir
->header
);
1178 spin_unlock(&sysctl_lock
);
1182 fail_put_dir_locked
:
1183 drop_sysctl_table(&dir
->header
);
1184 spin_unlock(&sysctl_lock
);
1192 static char *append_path(const char *path
, char *pos
, const char *name
)
1195 namelen
= strlen(name
);
1196 if (((pos
- path
) + namelen
+ 2) >= PATH_MAX
)
1198 memcpy(pos
, name
, namelen
);
1200 pos
[namelen
+ 1] = '\0';
1205 static int count_subheaders(struct ctl_table
*table
)
1208 int nr_subheaders
= 0;
1209 struct ctl_table
*entry
;
1211 /* special case: no directory and empty directory */
1212 if (!table
|| !table
->procname
)
1215 for (entry
= table
; entry
->procname
; entry
++) {
1217 nr_subheaders
+= count_subheaders(entry
->child
);
1221 return nr_subheaders
+ has_files
;
1224 static int register_leaf_sysctl_tables(const char *path
, char *pos
,
1225 struct ctl_table_header
***subheader
, struct ctl_table_set
*set
,
1226 struct ctl_table
*table
)
1228 struct ctl_table
*ctl_table_arg
= NULL
;
1229 struct ctl_table
*entry
, *files
;
1234 for (entry
= table
; entry
->procname
; entry
++) {
1242 /* If there are mixed files and directories we need a new table */
1243 if (nr_dirs
&& nr_files
) {
1244 struct ctl_table
*new;
1245 files
= kzalloc(sizeof(struct ctl_table
) * (nr_files
+ 1),
1250 ctl_table_arg
= files
;
1251 for (new = files
, entry
= table
; entry
->procname
; entry
++) {
1259 /* Register everything except a directory full of subdirectories */
1260 if (nr_files
|| !nr_dirs
) {
1261 struct ctl_table_header
*header
;
1262 header
= __register_sysctl_table(set
, path
, files
);
1264 kfree(ctl_table_arg
);
1268 /* Remember if we need to free the file table */
1269 header
->ctl_table_arg
= ctl_table_arg
;
1270 **subheader
= header
;
1274 /* Recurse into the subdirectories. */
1275 for (entry
= table
; entry
->procname
; entry
++) {
1281 err
= -ENAMETOOLONG
;
1282 child_pos
= append_path(path
, pos
, entry
->procname
);
1286 err
= register_leaf_sysctl_tables(path
, child_pos
, subheader
,
1294 /* On failure our caller will unregister all registered subheaders */
1299 * __register_sysctl_paths - register a sysctl table hierarchy
1300 * @set: Sysctl tree to register on
1301 * @path: The path to the directory the sysctl table is in.
1302 * @table: the top-level table structure
1304 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1305 * array. A completely 0 filled entry terminates the table.
1307 * See __register_sysctl_table for more details.
1309 struct ctl_table_header
*__register_sysctl_paths(
1310 struct ctl_table_set
*set
,
1311 const struct ctl_path
*path
, struct ctl_table
*table
)
1313 struct ctl_table
*ctl_table_arg
= table
;
1314 int nr_subheaders
= count_subheaders(table
);
1315 struct ctl_table_header
*header
= NULL
, **subheaders
, **subheader
;
1316 const struct ctl_path
*component
;
1317 char *new_path
, *pos
;
1319 pos
= new_path
= kmalloc(PATH_MAX
, GFP_KERNEL
);
1324 for (component
= path
; component
->procname
; component
++) {
1325 pos
= append_path(new_path
, pos
, component
->procname
);
1329 while (table
->procname
&& table
->child
&& !table
[1].procname
) {
1330 pos
= append_path(new_path
, pos
, table
->procname
);
1333 table
= table
->child
;
1335 if (nr_subheaders
== 1) {
1336 header
= __register_sysctl_table(set
, new_path
, table
);
1338 header
->ctl_table_arg
= ctl_table_arg
;
1340 header
= kzalloc(sizeof(*header
) +
1341 sizeof(*subheaders
)*nr_subheaders
, GFP_KERNEL
);
1345 subheaders
= (struct ctl_table_header
**) (header
+ 1);
1346 subheader
= subheaders
;
1347 header
->ctl_table_arg
= ctl_table_arg
;
1349 if (register_leaf_sysctl_tables(new_path
, pos
, &subheader
,
1351 goto err_register_leaves
;
1358 err_register_leaves
:
1359 while (subheader
> subheaders
) {
1360 struct ctl_table_header
*subh
= *(--subheader
);
1361 struct ctl_table
*table
= subh
->ctl_table_arg
;
1362 unregister_sysctl_table(subh
);
1371 * register_sysctl_table_path - register a sysctl table hierarchy
1372 * @path: The path to the directory the sysctl table is in.
1373 * @table: the top-level table structure
1375 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1376 * array. A completely 0 filled entry terminates the table.
1378 * See __register_sysctl_paths for more details.
1380 struct ctl_table_header
*register_sysctl_paths(const struct ctl_path
*path
,
1381 struct ctl_table
*table
)
1383 return __register_sysctl_paths(&sysctl_table_root
.default_set
,
1386 EXPORT_SYMBOL(register_sysctl_paths
);
1389 * register_sysctl_table - register a sysctl table hierarchy
1390 * @table: the top-level table structure
1392 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1393 * array. A completely 0 filled entry terminates the table.
1395 * See register_sysctl_paths for more details.
1397 struct ctl_table_header
*register_sysctl_table(struct ctl_table
*table
)
1399 static const struct ctl_path null_path
[] = { {} };
1401 return register_sysctl_paths(null_path
, table
);
1403 EXPORT_SYMBOL(register_sysctl_table
);
1405 static void put_links(struct ctl_table_header
*header
)
1407 struct ctl_table_set
*root_set
= &sysctl_table_root
.default_set
;
1408 struct ctl_table_root
*root
= header
->root
;
1409 struct ctl_dir
*parent
= header
->parent
;
1410 struct ctl_dir
*core_parent
;
1411 struct ctl_table
*entry
;
1413 if (header
->set
== root_set
)
1416 core_parent
= xlate_dir(root_set
, parent
);
1417 if (IS_ERR(core_parent
))
1420 for (entry
= header
->ctl_table
; entry
->procname
; entry
++) {
1421 struct ctl_table_header
*link_head
;
1422 struct ctl_table
*link
;
1423 const char *name
= entry
->procname
;
1425 link
= find_entry(&link_head
, core_parent
, name
, strlen(name
));
1427 ((S_ISDIR(link
->mode
) && S_ISDIR(entry
->mode
)) ||
1428 (S_ISLNK(link
->mode
) && (link
->data
== root
)))) {
1429 drop_sysctl_table(link_head
);
1432 printk(KERN_ERR
"sysctl link missing during unregister: ");
1433 sysctl_print_dir(parent
);
1434 printk(KERN_CONT
"/%s\n", name
);
1439 static void drop_sysctl_table(struct ctl_table_header
*header
)
1441 struct ctl_dir
*parent
= header
->parent
;
1447 start_unregistering(header
);
1448 if (!--header
->count
)
1449 kfree_rcu(header
, rcu
);
1452 drop_sysctl_table(&parent
->header
);
1456 * unregister_sysctl_table - unregister a sysctl table hierarchy
1457 * @header: the header returned from register_sysctl_table
1459 * Unregisters the sysctl table and all children. proc entries may not
1460 * actually be removed until they are no longer used by anyone.
1462 void unregister_sysctl_table(struct ctl_table_header
* header
)
1470 nr_subheaders
= count_subheaders(header
->ctl_table_arg
);
1471 if (unlikely(nr_subheaders
> 1)) {
1472 struct ctl_table_header
**subheaders
;
1475 subheaders
= (struct ctl_table_header
**)(header
+ 1);
1476 for (i
= nr_subheaders
-1; i
>= 0; i
--) {
1477 struct ctl_table_header
*subh
= subheaders
[i
];
1478 struct ctl_table
*table
= subh
->ctl_table_arg
;
1479 unregister_sysctl_table(subh
);
1486 spin_lock(&sysctl_lock
);
1487 drop_sysctl_table(header
);
1488 spin_unlock(&sysctl_lock
);
1490 EXPORT_SYMBOL(unregister_sysctl_table
);
1492 void setup_sysctl_set(struct ctl_table_set
*set
,
1493 struct ctl_table_root
*root
,
1494 int (*is_seen
)(struct ctl_table_set
*))
1496 memset(set
, sizeof(*set
), 0);
1497 INIT_LIST_HEAD(&set
->list
);
1498 set
->is_seen
= is_seen
;
1499 init_header(&set
->dir
.header
, root
, set
, root_table
);
1502 void retire_sysctl_set(struct ctl_table_set
*set
)
1504 WARN_ON(!list_empty(&set
->list
));
1507 int __init
proc_sys_init(void)
1509 struct proc_dir_entry
*proc_sys_root
;
1511 proc_sys_root
= proc_mkdir("sys", NULL
);
1512 proc_sys_root
->proc_iops
= &proc_sys_dir_operations
;
1513 proc_sys_root
->proc_fops
= &proc_sys_dir_file_operations
;
1514 proc_sys_root
->nlink
= 0;
1516 return sysctl_init();