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/sched.h>
10 #include <linux/namei.h>
12 #include <linux/module.h>
15 static const struct dentry_operations proc_sys_dentry_operations
;
16 static const struct file_operations proc_sys_file_operations
;
17 static const struct inode_operations proc_sys_inode_operations
;
18 static const struct file_operations proc_sys_dir_file_operations
;
19 static const struct inode_operations proc_sys_dir_operations
;
21 void proc_sys_poll_notify(struct ctl_table_poll
*poll
)
26 atomic_inc(&poll
->event
);
27 wake_up_interruptible(&poll
->wait
);
30 static struct ctl_table root_table
[] = {
33 .mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
,
37 static struct ctl_table_root sysctl_table_root
= {
38 .default_set
.dir
.header
= {
41 .ctl_table
= root_table
}},
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
);
56 static void sysctl_print_dir(struct ctl_dir
*dir
)
58 if (dir
->header
.parent
)
59 sysctl_print_dir(dir
->header
.parent
);
60 printk(KERN_CONT
"%s/", dir
->header
.ctl_table
[0].procname
);
63 static int namecmp(const char *name1
, int len1
, const char *name2
, int len2
)
72 cmp
= memcmp(name1
, name2
, minlen
);
78 /* Called under sysctl_lock */
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_header
*head
;
83 struct ctl_table
*entry
;
84 struct rb_node
*node
= dir
->root
.rb_node
;
88 struct ctl_node
*ctl_node
;
92 ctl_node
= rb_entry(node
, struct ctl_node
, node
);
93 head
= ctl_node
->header
;
94 entry
= &head
->ctl_table
[ctl_node
- head
->node
];
95 procname
= entry
->procname
;
97 cmp
= namecmp(name
, namelen
, procname
, strlen(procname
));
101 node
= node
->rb_right
;
110 static int insert_entry(struct ctl_table_header
*head
, struct ctl_table
*entry
)
112 struct rb_node
*node
= &head
->node
[entry
- head
->ctl_table
].node
;
113 struct rb_node
**p
= &head
->parent
->root
.rb_node
;
114 struct rb_node
*parent
= NULL
;
115 const char *name
= entry
->procname
;
116 int namelen
= strlen(name
);
119 struct ctl_table_header
*parent_head
;
120 struct ctl_table
*parent_entry
;
121 struct ctl_node
*parent_node
;
122 const char *parent_name
;
126 parent_node
= rb_entry(parent
, struct ctl_node
, node
);
127 parent_head
= parent_node
->header
;
128 parent_entry
= &parent_head
->ctl_table
[parent_node
- parent_head
->node
];
129 parent_name
= parent_entry
->procname
;
131 cmp
= namecmp(name
, namelen
, parent_name
, strlen(parent_name
));
137 printk(KERN_ERR
"sysctl duplicate entry: ");
138 sysctl_print_dir(head
->parent
);
139 printk(KERN_CONT
"/%s\n", entry
->procname
);
144 rb_link_node(node
, parent
, p
);
148 static void erase_entry(struct ctl_table_header
*head
, struct ctl_table
*entry
)
150 struct rb_node
*node
= &head
->node
[entry
- head
->ctl_table
].node
;
152 rb_erase(node
, &head
->parent
->root
);
155 static void init_header(struct ctl_table_header
*head
,
156 struct ctl_table_root
*root
, struct ctl_table_set
*set
,
157 struct ctl_node
*node
, struct ctl_table
*table
)
159 head
->ctl_table
= table
;
160 head
->ctl_table_arg
= table
;
164 head
->unregistering
= NULL
;
170 struct ctl_table
*entry
;
171 for (entry
= table
; entry
->procname
; entry
++, node
++) {
172 rb_init_node(&node
->node
);
178 static void erase_header(struct ctl_table_header
*head
)
180 struct ctl_table
*entry
;
181 for (entry
= head
->ctl_table
; entry
->procname
; entry
++)
182 erase_entry(head
, entry
);
185 static int insert_header(struct ctl_dir
*dir
, struct ctl_table_header
*header
)
187 struct ctl_table
*entry
;
191 header
->parent
= dir
;
192 err
= insert_links(header
);
195 for (entry
= header
->ctl_table
; entry
->procname
; entry
++) {
196 err
= insert_entry(header
, entry
);
202 erase_header(header
);
205 header
->parent
= NULL
;
206 drop_sysctl_table(&dir
->header
);
210 /* called under sysctl_lock */
211 static int use_table(struct ctl_table_header
*p
)
213 if (unlikely(p
->unregistering
))
219 /* called under sysctl_lock */
220 static void unuse_table(struct ctl_table_header
*p
)
223 if (unlikely(p
->unregistering
))
224 complete(p
->unregistering
);
227 /* called under sysctl_lock, will reacquire if has to wait */
228 static void start_unregistering(struct ctl_table_header
*p
)
231 * if p->used is 0, nobody will ever touch that entry again;
232 * we'll eliminate all paths to it before dropping sysctl_lock
234 if (unlikely(p
->used
)) {
235 struct completion wait
;
236 init_completion(&wait
);
237 p
->unregistering
= &wait
;
238 spin_unlock(&sysctl_lock
);
239 wait_for_completion(&wait
);
240 spin_lock(&sysctl_lock
);
242 /* anything non-NULL; we'll never dereference it */
243 p
->unregistering
= ERR_PTR(-EINVAL
);
246 * do not remove from the list until nobody holds it; walking the
247 * list in do_sysctl() relies on that.
252 static void sysctl_head_get(struct ctl_table_header
*head
)
254 spin_lock(&sysctl_lock
);
256 spin_unlock(&sysctl_lock
);
259 void sysctl_head_put(struct ctl_table_header
*head
)
261 spin_lock(&sysctl_lock
);
263 kfree_rcu(head
, rcu
);
264 spin_unlock(&sysctl_lock
);
267 static struct ctl_table_header
*sysctl_head_grab(struct ctl_table_header
*head
)
271 spin_lock(&sysctl_lock
);
272 if (!use_table(head
))
273 head
= ERR_PTR(-ENOENT
);
274 spin_unlock(&sysctl_lock
);
278 static void sysctl_head_finish(struct ctl_table_header
*head
)
282 spin_lock(&sysctl_lock
);
284 spin_unlock(&sysctl_lock
);
287 static struct ctl_table_set
*
288 lookup_header_set(struct ctl_table_root
*root
, struct nsproxy
*namespaces
)
290 struct ctl_table_set
*set
= &root
->default_set
;
292 set
= root
->lookup(root
, namespaces
);
296 static struct ctl_table
*lookup_entry(struct ctl_table_header
**phead
,
298 const char *name
, int namelen
)
300 struct ctl_table_header
*head
;
301 struct ctl_table
*entry
;
303 spin_lock(&sysctl_lock
);
304 entry
= find_entry(&head
, dir
, name
, namelen
);
305 if (entry
&& use_table(head
))
309 spin_unlock(&sysctl_lock
);
313 static struct ctl_node
*first_usable_entry(struct rb_node
*node
)
315 struct ctl_node
*ctl_node
;
317 for (;node
; node
= rb_next(node
)) {
318 ctl_node
= rb_entry(node
, struct ctl_node
, node
);
319 if (use_table(ctl_node
->header
))
325 static void first_entry(struct ctl_dir
*dir
,
326 struct ctl_table_header
**phead
, struct ctl_table
**pentry
)
328 struct ctl_table_header
*head
= NULL
;
329 struct ctl_table
*entry
= NULL
;
330 struct ctl_node
*ctl_node
;
332 spin_lock(&sysctl_lock
);
333 ctl_node
= first_usable_entry(rb_first(&dir
->root
));
334 spin_unlock(&sysctl_lock
);
336 head
= ctl_node
->header
;
337 entry
= &head
->ctl_table
[ctl_node
- head
->node
];
343 static void next_entry(struct ctl_table_header
**phead
, struct ctl_table
**pentry
)
345 struct ctl_table_header
*head
= *phead
;
346 struct ctl_table
*entry
= *pentry
;
347 struct ctl_node
*ctl_node
= &head
->node
[entry
- head
->ctl_table
];
349 spin_lock(&sysctl_lock
);
352 ctl_node
= first_usable_entry(rb_next(&ctl_node
->node
));
353 spin_unlock(&sysctl_lock
);
356 head
= ctl_node
->header
;
357 entry
= &head
->ctl_table
[ctl_node
- head
->node
];
363 void register_sysctl_root(struct ctl_table_root
*root
)
368 * sysctl_perm does NOT grant the superuser all rights automatically, because
369 * some sysctl variables are readonly even to root.
372 static int test_perm(int mode
, int op
)
374 if (uid_eq(current_euid(), GLOBAL_ROOT_UID
))
376 else if (in_egroup_p(GLOBAL_ROOT_GID
))
378 if ((op
& ~mode
& (MAY_READ
|MAY_WRITE
|MAY_EXEC
)) == 0)
383 static int sysctl_perm(struct ctl_table_root
*root
, struct ctl_table
*table
, int op
)
387 if (root
->permissions
)
388 mode
= root
->permissions(root
, current
->nsproxy
, table
);
392 return test_perm(mode
, op
);
395 static struct inode
*proc_sys_make_inode(struct super_block
*sb
,
396 struct ctl_table_header
*head
, struct ctl_table
*table
)
399 struct proc_inode
*ei
;
401 inode
= new_inode(sb
);
405 inode
->i_ino
= get_next_ino();
407 sysctl_head_get(head
);
410 ei
->sysctl_entry
= table
;
412 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
413 inode
->i_mode
= table
->mode
;
414 if (!S_ISDIR(table
->mode
)) {
415 inode
->i_mode
|= S_IFREG
;
416 inode
->i_op
= &proc_sys_inode_operations
;
417 inode
->i_fop
= &proc_sys_file_operations
;
419 inode
->i_mode
|= S_IFDIR
;
420 inode
->i_op
= &proc_sys_dir_operations
;
421 inode
->i_fop
= &proc_sys_dir_file_operations
;
427 static struct ctl_table_header
*grab_header(struct inode
*inode
)
429 struct ctl_table_header
*head
= PROC_I(inode
)->sysctl
;
431 head
= &sysctl_table_root
.default_set
.dir
.header
;
432 return sysctl_head_grab(head
);
435 static struct dentry
*proc_sys_lookup(struct inode
*dir
, struct dentry
*dentry
,
438 struct ctl_table_header
*head
= grab_header(dir
);
439 struct ctl_table_header
*h
= NULL
;
440 struct qstr
*name
= &dentry
->d_name
;
443 struct dentry
*err
= ERR_PTR(-ENOENT
);
444 struct ctl_dir
*ctl_dir
;
448 return ERR_CAST(head
);
450 ctl_dir
= container_of(head
, struct ctl_dir
, header
);
452 p
= lookup_entry(&h
, ctl_dir
, name
->name
, name
->len
);
456 if (S_ISLNK(p
->mode
)) {
457 ret
= sysctl_follow_link(&h
, &p
, current
->nsproxy
);
463 err
= ERR_PTR(-ENOMEM
);
464 inode
= proc_sys_make_inode(dir
->i_sb
, h
? h
: head
, p
);
466 sysctl_head_finish(h
);
472 d_set_d_op(dentry
, &proc_sys_dentry_operations
);
473 d_add(dentry
, inode
);
476 sysctl_head_finish(head
);
480 static ssize_t
proc_sys_call_handler(struct file
*filp
, void __user
*buf
,
481 size_t count
, loff_t
*ppos
, int write
)
483 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
484 struct ctl_table_header
*head
= grab_header(inode
);
485 struct ctl_table
*table
= PROC_I(inode
)->sysctl_entry
;
490 return PTR_ERR(head
);
493 * At this point we know that the sysctl was not unregistered
494 * and won't be until we finish.
497 if (sysctl_perm(head
->root
, table
, write
? MAY_WRITE
: MAY_READ
))
500 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
502 if (!table
->proc_handler
)
505 /* careful: calling conventions are nasty here */
507 error
= table
->proc_handler(table
, write
, buf
, &res
, ppos
);
511 sysctl_head_finish(head
);
516 static ssize_t
proc_sys_read(struct file
*filp
, char __user
*buf
,
517 size_t count
, loff_t
*ppos
)
519 return proc_sys_call_handler(filp
, (void __user
*)buf
, count
, ppos
, 0);
522 static ssize_t
proc_sys_write(struct file
*filp
, const char __user
*buf
,
523 size_t count
, loff_t
*ppos
)
525 return proc_sys_call_handler(filp
, (void __user
*)buf
, count
, ppos
, 1);
528 static int proc_sys_open(struct inode
*inode
, struct file
*filp
)
530 struct ctl_table_header
*head
= grab_header(inode
);
531 struct ctl_table
*table
= PROC_I(inode
)->sysctl_entry
;
533 /* sysctl was unregistered */
535 return PTR_ERR(head
);
538 filp
->private_data
= proc_sys_poll_event(table
->poll
);
540 sysctl_head_finish(head
);
545 static unsigned int proc_sys_poll(struct file
*filp
, poll_table
*wait
)
547 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
548 struct ctl_table_header
*head
= grab_header(inode
);
549 struct ctl_table
*table
= PROC_I(inode
)->sysctl_entry
;
550 unsigned int ret
= DEFAULT_POLLMASK
;
553 /* sysctl was unregistered */
555 return POLLERR
| POLLHUP
;
557 if (!table
->proc_handler
)
563 event
= (unsigned long)filp
->private_data
;
564 poll_wait(filp
, &table
->poll
->wait
, wait
);
566 if (event
!= atomic_read(&table
->poll
->event
)) {
567 filp
->private_data
= proc_sys_poll_event(table
->poll
);
568 ret
= POLLIN
| POLLRDNORM
| POLLERR
| POLLPRI
;
572 sysctl_head_finish(head
);
577 static int proc_sys_fill_cache(struct file
*filp
, void *dirent
,
579 struct ctl_table_header
*head
,
580 struct ctl_table
*table
)
582 struct dentry
*child
, *dir
= filp
->f_path
.dentry
;
586 unsigned type
= DT_UNKNOWN
;
588 qname
.name
= table
->procname
;
589 qname
.len
= strlen(table
->procname
);
590 qname
.hash
= full_name_hash(qname
.name
, qname
.len
);
592 child
= d_lookup(dir
, &qname
);
594 child
= d_alloc(dir
, &qname
);
596 inode
= proc_sys_make_inode(dir
->d_sb
, head
, table
);
601 d_set_d_op(child
, &proc_sys_dentry_operations
);
608 inode
= child
->d_inode
;
610 type
= inode
->i_mode
>> 12;
612 return !!filldir(dirent
, qname
.name
, qname
.len
, filp
->f_pos
, ino
, type
);
615 static int proc_sys_link_fill_cache(struct file
*filp
, void *dirent
,
617 struct ctl_table_header
*head
,
618 struct ctl_table
*table
)
621 head
= sysctl_head_grab(head
);
623 if (S_ISLNK(table
->mode
)) {
624 /* It is not an error if we can not follow the link ignore it */
625 err
= sysctl_follow_link(&head
, &table
, current
->nsproxy
);
630 ret
= proc_sys_fill_cache(filp
, dirent
, filldir
, head
, table
);
632 sysctl_head_finish(head
);
636 static int scan(struct ctl_table_header
*head
, ctl_table
*table
,
637 unsigned long *pos
, struct file
*file
,
638 void *dirent
, filldir_t filldir
)
642 if ((*pos
)++ < file
->f_pos
)
645 if (unlikely(S_ISLNK(table
->mode
)))
646 res
= proc_sys_link_fill_cache(file
, dirent
, filldir
, head
, table
);
648 res
= proc_sys_fill_cache(file
, dirent
, filldir
, head
, table
);
656 static int proc_sys_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
658 struct dentry
*dentry
= filp
->f_path
.dentry
;
659 struct inode
*inode
= dentry
->d_inode
;
660 struct ctl_table_header
*head
= grab_header(inode
);
661 struct ctl_table_header
*h
= NULL
;
662 struct ctl_table
*entry
;
663 struct ctl_dir
*ctl_dir
;
668 return PTR_ERR(head
);
670 ctl_dir
= container_of(head
, struct ctl_dir
, header
);
673 /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
674 if (filp
->f_pos
== 0) {
675 if (filldir(dirent
, ".", 1, filp
->f_pos
,
676 inode
->i_ino
, DT_DIR
) < 0)
680 if (filp
->f_pos
== 1) {
681 if (filldir(dirent
, "..", 2, filp
->f_pos
,
682 parent_ino(dentry
), DT_DIR
) < 0)
688 for (first_entry(ctl_dir
, &h
, &entry
); h
; next_entry(&h
, &entry
)) {
689 ret
= scan(h
, entry
, &pos
, filp
, dirent
, filldir
);
691 sysctl_head_finish(h
);
697 sysctl_head_finish(head
);
701 static int proc_sys_permission(struct inode
*inode
, int mask
)
704 * sysctl entries that are not writeable,
705 * are _NOT_ writeable, capabilities or not.
707 struct ctl_table_header
*head
;
708 struct ctl_table
*table
;
711 /* Executable files are not allowed under /proc/sys/ */
712 if ((mask
& MAY_EXEC
) && S_ISREG(inode
->i_mode
))
715 head
= grab_header(inode
);
717 return PTR_ERR(head
);
719 table
= PROC_I(inode
)->sysctl_entry
;
720 if (!table
) /* global root - r-xr-xr-x */
721 error
= mask
& MAY_WRITE
? -EACCES
: 0;
722 else /* Use the permissions on the sysctl table entry */
723 error
= sysctl_perm(head
->root
, table
, mask
& ~MAY_NOT_BLOCK
);
725 sysctl_head_finish(head
);
729 static int proc_sys_setattr(struct dentry
*dentry
, struct iattr
*attr
)
731 struct inode
*inode
= dentry
->d_inode
;
734 if (attr
->ia_valid
& (ATTR_MODE
| ATTR_UID
| ATTR_GID
))
737 error
= inode_change_ok(inode
, attr
);
741 if ((attr
->ia_valid
& ATTR_SIZE
) &&
742 attr
->ia_size
!= i_size_read(inode
)) {
743 error
= vmtruncate(inode
, attr
->ia_size
);
748 setattr_copy(inode
, attr
);
749 mark_inode_dirty(inode
);
753 static int proc_sys_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
755 struct inode
*inode
= dentry
->d_inode
;
756 struct ctl_table_header
*head
= grab_header(inode
);
757 struct ctl_table
*table
= PROC_I(inode
)->sysctl_entry
;
760 return PTR_ERR(head
);
762 generic_fillattr(inode
, stat
);
764 stat
->mode
= (stat
->mode
& S_IFMT
) | table
->mode
;
766 sysctl_head_finish(head
);
770 static const struct file_operations proc_sys_file_operations
= {
771 .open
= proc_sys_open
,
772 .poll
= proc_sys_poll
,
773 .read
= proc_sys_read
,
774 .write
= proc_sys_write
,
775 .llseek
= default_llseek
,
778 static const struct file_operations proc_sys_dir_file_operations
= {
779 .read
= generic_read_dir
,
780 .readdir
= proc_sys_readdir
,
781 .llseek
= generic_file_llseek
,
784 static const struct inode_operations proc_sys_inode_operations
= {
785 .permission
= proc_sys_permission
,
786 .setattr
= proc_sys_setattr
,
787 .getattr
= proc_sys_getattr
,
790 static const struct inode_operations proc_sys_dir_operations
= {
791 .lookup
= proc_sys_lookup
,
792 .permission
= proc_sys_permission
,
793 .setattr
= proc_sys_setattr
,
794 .getattr
= proc_sys_getattr
,
797 static int proc_sys_revalidate(struct dentry
*dentry
, unsigned int flags
)
799 if (flags
& LOOKUP_RCU
)
801 return !PROC_I(dentry
->d_inode
)->sysctl
->unregistering
;
804 static int proc_sys_delete(const struct dentry
*dentry
)
806 return !!PROC_I(dentry
->d_inode
)->sysctl
->unregistering
;
809 static int sysctl_is_seen(struct ctl_table_header
*p
)
811 struct ctl_table_set
*set
= p
->set
;
813 spin_lock(&sysctl_lock
);
814 if (p
->unregistering
)
816 else if (!set
->is_seen
)
819 res
= set
->is_seen(set
);
820 spin_unlock(&sysctl_lock
);
824 static int proc_sys_compare(const struct dentry
*parent
,
825 const struct inode
*pinode
,
826 const struct dentry
*dentry
, const struct inode
*inode
,
827 unsigned int len
, const char *str
, const struct qstr
*name
)
829 struct ctl_table_header
*head
;
830 /* Although proc doesn't have negative dentries, rcu-walk means
831 * that inode here can be NULL */
832 /* AV: can it, indeed? */
835 if (name
->len
!= len
)
837 if (memcmp(name
->name
, str
, len
))
839 head
= rcu_dereference(PROC_I(inode
)->sysctl
);
840 return !head
|| !sysctl_is_seen(head
);
843 static const struct dentry_operations proc_sys_dentry_operations
= {
844 .d_revalidate
= proc_sys_revalidate
,
845 .d_delete
= proc_sys_delete
,
846 .d_compare
= proc_sys_compare
,
849 static struct ctl_dir
*find_subdir(struct ctl_dir
*dir
,
850 const char *name
, int namelen
)
852 struct ctl_table_header
*head
;
853 struct ctl_table
*entry
;
855 entry
= find_entry(&head
, dir
, name
, namelen
);
857 return ERR_PTR(-ENOENT
);
858 if (!S_ISDIR(entry
->mode
))
859 return ERR_PTR(-ENOTDIR
);
860 return container_of(head
, struct ctl_dir
, header
);
863 static struct ctl_dir
*new_dir(struct ctl_table_set
*set
,
864 const char *name
, int namelen
)
866 struct ctl_table
*table
;
868 struct ctl_node
*node
;
871 new = kzalloc(sizeof(*new) + sizeof(struct ctl_node
) +
872 sizeof(struct ctl_table
)*2 + namelen
+ 1,
877 node
= (struct ctl_node
*)(new + 1);
878 table
= (struct ctl_table
*)(node
+ 1);
879 new_name
= (char *)(table
+ 2);
880 memcpy(new_name
, name
, namelen
);
881 new_name
[namelen
] = '\0';
882 table
[0].procname
= new_name
;
883 table
[0].mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
884 init_header(&new->header
, set
->dir
.header
.root
, set
, node
, table
);
890 * get_subdir - find or create a subdir with the specified name.
891 * @dir: Directory to create the subdirectory in
892 * @name: The name of the subdirectory to find or create
893 * @namelen: The length of name
895 * Takes a directory with an elevated reference count so we know that
896 * if we drop the lock the directory will not go away. Upon success
897 * the reference is moved from @dir to the returned subdirectory.
898 * Upon error an error code is returned and the reference on @dir is
901 static struct ctl_dir
*get_subdir(struct ctl_dir
*dir
,
902 const char *name
, int namelen
)
904 struct ctl_table_set
*set
= dir
->header
.set
;
905 struct ctl_dir
*subdir
, *new = NULL
;
908 spin_lock(&sysctl_lock
);
909 subdir
= find_subdir(dir
, name
, namelen
);
912 if (PTR_ERR(subdir
) != -ENOENT
)
915 spin_unlock(&sysctl_lock
);
916 new = new_dir(set
, name
, namelen
);
917 spin_lock(&sysctl_lock
);
918 subdir
= ERR_PTR(-ENOMEM
);
922 /* Was the subdir added while we dropped the lock? */
923 subdir
= find_subdir(dir
, name
, namelen
);
926 if (PTR_ERR(subdir
) != -ENOENT
)
929 /* Nope. Use the our freshly made directory entry. */
930 err
= insert_header(dir
, &new->header
);
931 subdir
= ERR_PTR(err
);
936 subdir
->header
.nreg
++;
938 if (unlikely(IS_ERR(subdir
))) {
939 printk(KERN_ERR
"sysctl could not get directory: ");
940 sysctl_print_dir(dir
);
941 printk(KERN_CONT
"/%*.*s %ld\n",
942 namelen
, namelen
, name
, PTR_ERR(subdir
));
944 drop_sysctl_table(&dir
->header
);
946 drop_sysctl_table(&new->header
);
947 spin_unlock(&sysctl_lock
);
951 static struct ctl_dir
*xlate_dir(struct ctl_table_set
*set
, struct ctl_dir
*dir
)
953 struct ctl_dir
*parent
;
954 const char *procname
;
955 if (!dir
->header
.parent
)
957 parent
= xlate_dir(set
, dir
->header
.parent
);
960 procname
= dir
->header
.ctl_table
[0].procname
;
961 return find_subdir(parent
, procname
, strlen(procname
));
964 static int sysctl_follow_link(struct ctl_table_header
**phead
,
965 struct ctl_table
**pentry
, struct nsproxy
*namespaces
)
967 struct ctl_table_header
*head
;
968 struct ctl_table_root
*root
;
969 struct ctl_table_set
*set
;
970 struct ctl_table
*entry
;
975 spin_lock(&sysctl_lock
);
976 root
= (*pentry
)->data
;
977 set
= lookup_header_set(root
, namespaces
);
978 dir
= xlate_dir(set
, (*phead
)->parent
);
982 const char *procname
= (*pentry
)->procname
;
984 entry
= find_entry(&head
, dir
, procname
, strlen(procname
));
986 if (entry
&& use_table(head
)) {
994 spin_unlock(&sysctl_lock
);
998 static int sysctl_err(const char *path
, struct ctl_table
*table
, char *fmt
, ...)
1000 struct va_format vaf
;
1003 va_start(args
, fmt
);
1007 printk(KERN_ERR
"sysctl table check failed: %s/%s %pV\n",
1008 path
, table
->procname
, &vaf
);
1014 static int sysctl_check_table(const char *path
, struct ctl_table
*table
)
1017 for (; table
->procname
; table
++) {
1019 err
= sysctl_err(path
, table
, "Not a file");
1021 if ((table
->proc_handler
== proc_dostring
) ||
1022 (table
->proc_handler
== proc_dointvec
) ||
1023 (table
->proc_handler
== proc_dointvec_minmax
) ||
1024 (table
->proc_handler
== proc_dointvec_jiffies
) ||
1025 (table
->proc_handler
== proc_dointvec_userhz_jiffies
) ||
1026 (table
->proc_handler
== proc_dointvec_ms_jiffies
) ||
1027 (table
->proc_handler
== proc_doulongvec_minmax
) ||
1028 (table
->proc_handler
== proc_doulongvec_ms_jiffies_minmax
)) {
1030 err
= sysctl_err(path
, table
, "No data");
1032 err
= sysctl_err(path
, table
, "No maxlen");
1034 if (!table
->proc_handler
)
1035 err
= sysctl_err(path
, table
, "No proc_handler");
1037 if ((table
->mode
& (S_IRUGO
|S_IWUGO
)) != table
->mode
)
1038 err
= sysctl_err(path
, table
, "bogus .mode 0%o",
1044 static struct ctl_table_header
*new_links(struct ctl_dir
*dir
, struct ctl_table
*table
,
1045 struct ctl_table_root
*link_root
)
1047 struct ctl_table
*link_table
, *entry
, *link
;
1048 struct ctl_table_header
*links
;
1049 struct ctl_node
*node
;
1051 int nr_entries
, name_bytes
;
1055 for (entry
= table
; entry
->procname
; entry
++) {
1057 name_bytes
+= strlen(entry
->procname
) + 1;
1060 links
= kzalloc(sizeof(struct ctl_table_header
) +
1061 sizeof(struct ctl_node
)*nr_entries
+
1062 sizeof(struct ctl_table
)*(nr_entries
+ 1) +
1069 node
= (struct ctl_node
*)(links
+ 1);
1070 link_table
= (struct ctl_table
*)(node
+ nr_entries
);
1071 link_name
= (char *)&link_table
[nr_entries
+ 1];
1073 for (link
= link_table
, entry
= table
; entry
->procname
; link
++, entry
++) {
1074 int len
= strlen(entry
->procname
) + 1;
1075 memcpy(link_name
, entry
->procname
, len
);
1076 link
->procname
= link_name
;
1077 link
->mode
= S_IFLNK
|S_IRWXUGO
;
1078 link
->data
= link_root
;
1081 init_header(links
, dir
->header
.root
, dir
->header
.set
, node
, link_table
);
1082 links
->nreg
= nr_entries
;
1087 static bool get_links(struct ctl_dir
*dir
,
1088 struct ctl_table
*table
, struct ctl_table_root
*link_root
)
1090 struct ctl_table_header
*head
;
1091 struct ctl_table
*entry
, *link
;
1093 /* Are there links available for every entry in table? */
1094 for (entry
= table
; entry
->procname
; entry
++) {
1095 const char *procname
= entry
->procname
;
1096 link
= find_entry(&head
, dir
, procname
, strlen(procname
));
1099 if (S_ISDIR(link
->mode
) && S_ISDIR(entry
->mode
))
1101 if (S_ISLNK(link
->mode
) && (link
->data
== link_root
))
1106 /* The checks passed. Increase the registration count on the links */
1107 for (entry
= table
; entry
->procname
; entry
++) {
1108 const char *procname
= entry
->procname
;
1109 link
= find_entry(&head
, dir
, procname
, strlen(procname
));
1115 static int insert_links(struct ctl_table_header
*head
)
1117 struct ctl_table_set
*root_set
= &sysctl_table_root
.default_set
;
1118 struct ctl_dir
*core_parent
= NULL
;
1119 struct ctl_table_header
*links
;
1122 if (head
->set
== root_set
)
1125 core_parent
= xlate_dir(root_set
, head
->parent
);
1126 if (IS_ERR(core_parent
))
1129 if (get_links(core_parent
, head
->ctl_table
, head
->root
))
1132 core_parent
->header
.nreg
++;
1133 spin_unlock(&sysctl_lock
);
1135 links
= new_links(core_parent
, head
->ctl_table
, head
->root
);
1137 spin_lock(&sysctl_lock
);
1143 if (get_links(core_parent
, head
->ctl_table
, head
->root
)) {
1148 err
= insert_header(core_parent
, links
);
1152 drop_sysctl_table(&core_parent
->header
);
1157 * __register_sysctl_table - register a leaf sysctl table
1158 * @set: Sysctl tree to register on
1159 * @path: The path to the directory the sysctl table is in.
1160 * @table: the top-level table structure
1162 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1163 * array. A completely 0 filled entry terminates the table.
1165 * The members of the &struct ctl_table structure are used as follows:
1167 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
1168 * enter a sysctl file
1170 * data - a pointer to data for use by proc_handler
1172 * maxlen - the maximum size in bytes of the data
1174 * mode - the file permissions for the /proc/sys file
1176 * child - must be %NULL.
1178 * proc_handler - the text handler routine (described below)
1180 * extra1, extra2 - extra pointers usable by the proc handler routines
1182 * Leaf nodes in the sysctl tree will be represented by a single file
1183 * under /proc; non-leaf nodes will be represented by directories.
1185 * There must be a proc_handler routine for any terminal nodes.
1186 * Several default handlers are available to cover common cases -
1188 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
1189 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
1190 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
1192 * It is the handler's job to read the input buffer from user memory
1193 * and process it. The handler should return 0 on success.
1195 * This routine returns %NULL on a failure to register, and a pointer
1196 * to the table header on success.
1198 struct ctl_table_header
*__register_sysctl_table(
1199 struct ctl_table_set
*set
,
1200 const char *path
, struct ctl_table
*table
)
1202 struct ctl_table_root
*root
= set
->dir
.header
.root
;
1203 struct ctl_table_header
*header
;
1204 const char *name
, *nextname
;
1205 struct ctl_dir
*dir
;
1206 struct ctl_table
*entry
;
1207 struct ctl_node
*node
;
1210 for (entry
= table
; entry
->procname
; entry
++)
1213 header
= kzalloc(sizeof(struct ctl_table_header
) +
1214 sizeof(struct ctl_node
)*nr_entries
, GFP_KERNEL
);
1218 node
= (struct ctl_node
*)(header
+ 1);
1219 init_header(header
, root
, set
, node
, table
);
1220 if (sysctl_check_table(path
, table
))
1223 spin_lock(&sysctl_lock
);
1225 /* Reference moved down the diretory tree get_subdir */
1227 spin_unlock(&sysctl_lock
);
1229 /* Find the directory for the ctl_table */
1230 for (name
= path
; name
; name
= nextname
) {
1232 nextname
= strchr(name
, '/');
1234 namelen
= nextname
- name
;
1237 namelen
= strlen(name
);
1242 dir
= get_subdir(dir
, name
, namelen
);
1247 spin_lock(&sysctl_lock
);
1248 if (insert_header(dir
, header
))
1249 goto fail_put_dir_locked
;
1251 drop_sysctl_table(&dir
->header
);
1252 spin_unlock(&sysctl_lock
);
1256 fail_put_dir_locked
:
1257 drop_sysctl_table(&dir
->header
);
1258 spin_unlock(&sysctl_lock
);
1266 * register_sysctl - register a sysctl table
1267 * @path: The path to the directory the sysctl table is in.
1268 * @table: the table structure
1270 * Register a sysctl table. @table should be a filled in ctl_table
1271 * array. A completely 0 filled entry terminates the table.
1273 * See __register_sysctl_table for more details.
1275 struct ctl_table_header
*register_sysctl(const char *path
, struct ctl_table
*table
)
1277 return __register_sysctl_table(&sysctl_table_root
.default_set
,
1280 EXPORT_SYMBOL(register_sysctl
);
1282 static char *append_path(const char *path
, char *pos
, const char *name
)
1285 namelen
= strlen(name
);
1286 if (((pos
- path
) + namelen
+ 2) >= PATH_MAX
)
1288 memcpy(pos
, name
, namelen
);
1290 pos
[namelen
+ 1] = '\0';
1295 static int count_subheaders(struct ctl_table
*table
)
1298 int nr_subheaders
= 0;
1299 struct ctl_table
*entry
;
1301 /* special case: no directory and empty directory */
1302 if (!table
|| !table
->procname
)
1305 for (entry
= table
; entry
->procname
; entry
++) {
1307 nr_subheaders
+= count_subheaders(entry
->child
);
1311 return nr_subheaders
+ has_files
;
1314 static int register_leaf_sysctl_tables(const char *path
, char *pos
,
1315 struct ctl_table_header
***subheader
, struct ctl_table_set
*set
,
1316 struct ctl_table
*table
)
1318 struct ctl_table
*ctl_table_arg
= NULL
;
1319 struct ctl_table
*entry
, *files
;
1324 for (entry
= table
; entry
->procname
; entry
++) {
1332 /* If there are mixed files and directories we need a new table */
1333 if (nr_dirs
&& nr_files
) {
1334 struct ctl_table
*new;
1335 files
= kzalloc(sizeof(struct ctl_table
) * (nr_files
+ 1),
1340 ctl_table_arg
= files
;
1341 for (new = files
, entry
= table
; entry
->procname
; entry
++) {
1349 /* Register everything except a directory full of subdirectories */
1350 if (nr_files
|| !nr_dirs
) {
1351 struct ctl_table_header
*header
;
1352 header
= __register_sysctl_table(set
, path
, files
);
1354 kfree(ctl_table_arg
);
1358 /* Remember if we need to free the file table */
1359 header
->ctl_table_arg
= ctl_table_arg
;
1360 **subheader
= header
;
1364 /* Recurse into the subdirectories. */
1365 for (entry
= table
; entry
->procname
; entry
++) {
1371 err
= -ENAMETOOLONG
;
1372 child_pos
= append_path(path
, pos
, entry
->procname
);
1376 err
= register_leaf_sysctl_tables(path
, child_pos
, subheader
,
1384 /* On failure our caller will unregister all registered subheaders */
1389 * __register_sysctl_paths - register a sysctl table hierarchy
1390 * @set: Sysctl tree to register on
1391 * @path: The path to the directory the sysctl table is in.
1392 * @table: the top-level table structure
1394 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1395 * array. A completely 0 filled entry terminates the table.
1397 * See __register_sysctl_table for more details.
1399 struct ctl_table_header
*__register_sysctl_paths(
1400 struct ctl_table_set
*set
,
1401 const struct ctl_path
*path
, struct ctl_table
*table
)
1403 struct ctl_table
*ctl_table_arg
= table
;
1404 int nr_subheaders
= count_subheaders(table
);
1405 struct ctl_table_header
*header
= NULL
, **subheaders
, **subheader
;
1406 const struct ctl_path
*component
;
1407 char *new_path
, *pos
;
1409 pos
= new_path
= kmalloc(PATH_MAX
, GFP_KERNEL
);
1414 for (component
= path
; component
->procname
; component
++) {
1415 pos
= append_path(new_path
, pos
, component
->procname
);
1419 while (table
->procname
&& table
->child
&& !table
[1].procname
) {
1420 pos
= append_path(new_path
, pos
, table
->procname
);
1423 table
= table
->child
;
1425 if (nr_subheaders
== 1) {
1426 header
= __register_sysctl_table(set
, new_path
, table
);
1428 header
->ctl_table_arg
= ctl_table_arg
;
1430 header
= kzalloc(sizeof(*header
) +
1431 sizeof(*subheaders
)*nr_subheaders
, GFP_KERNEL
);
1435 subheaders
= (struct ctl_table_header
**) (header
+ 1);
1436 subheader
= subheaders
;
1437 header
->ctl_table_arg
= ctl_table_arg
;
1439 if (register_leaf_sysctl_tables(new_path
, pos
, &subheader
,
1441 goto err_register_leaves
;
1448 err_register_leaves
:
1449 while (subheader
> subheaders
) {
1450 struct ctl_table_header
*subh
= *(--subheader
);
1451 struct ctl_table
*table
= subh
->ctl_table_arg
;
1452 unregister_sysctl_table(subh
);
1461 * register_sysctl_table_path - register a sysctl table hierarchy
1462 * @path: The path to the directory the sysctl table is in.
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_paths(const struct ctl_path
*path
,
1471 struct ctl_table
*table
)
1473 return __register_sysctl_paths(&sysctl_table_root
.default_set
,
1476 EXPORT_SYMBOL(register_sysctl_paths
);
1479 * register_sysctl_table - register a sysctl table hierarchy
1480 * @table: the top-level table structure
1482 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1483 * array. A completely 0 filled entry terminates the table.
1485 * See register_sysctl_paths for more details.
1487 struct ctl_table_header
*register_sysctl_table(struct ctl_table
*table
)
1489 static const struct ctl_path null_path
[] = { {} };
1491 return register_sysctl_paths(null_path
, table
);
1493 EXPORT_SYMBOL(register_sysctl_table
);
1495 static void put_links(struct ctl_table_header
*header
)
1497 struct ctl_table_set
*root_set
= &sysctl_table_root
.default_set
;
1498 struct ctl_table_root
*root
= header
->root
;
1499 struct ctl_dir
*parent
= header
->parent
;
1500 struct ctl_dir
*core_parent
;
1501 struct ctl_table
*entry
;
1503 if (header
->set
== root_set
)
1506 core_parent
= xlate_dir(root_set
, parent
);
1507 if (IS_ERR(core_parent
))
1510 for (entry
= header
->ctl_table
; entry
->procname
; entry
++) {
1511 struct ctl_table_header
*link_head
;
1512 struct ctl_table
*link
;
1513 const char *name
= entry
->procname
;
1515 link
= find_entry(&link_head
, core_parent
, name
, strlen(name
));
1517 ((S_ISDIR(link
->mode
) && S_ISDIR(entry
->mode
)) ||
1518 (S_ISLNK(link
->mode
) && (link
->data
== root
)))) {
1519 drop_sysctl_table(link_head
);
1522 printk(KERN_ERR
"sysctl link missing during unregister: ");
1523 sysctl_print_dir(parent
);
1524 printk(KERN_CONT
"/%s\n", name
);
1529 static void drop_sysctl_table(struct ctl_table_header
*header
)
1531 struct ctl_dir
*parent
= header
->parent
;
1537 start_unregistering(header
);
1538 if (!--header
->count
)
1539 kfree_rcu(header
, rcu
);
1542 drop_sysctl_table(&parent
->header
);
1546 * unregister_sysctl_table - unregister a sysctl table hierarchy
1547 * @header: the header returned from register_sysctl_table
1549 * Unregisters the sysctl table and all children. proc entries may not
1550 * actually be removed until they are no longer used by anyone.
1552 void unregister_sysctl_table(struct ctl_table_header
* header
)
1560 nr_subheaders
= count_subheaders(header
->ctl_table_arg
);
1561 if (unlikely(nr_subheaders
> 1)) {
1562 struct ctl_table_header
**subheaders
;
1565 subheaders
= (struct ctl_table_header
**)(header
+ 1);
1566 for (i
= nr_subheaders
-1; i
>= 0; i
--) {
1567 struct ctl_table_header
*subh
= subheaders
[i
];
1568 struct ctl_table
*table
= subh
->ctl_table_arg
;
1569 unregister_sysctl_table(subh
);
1576 spin_lock(&sysctl_lock
);
1577 drop_sysctl_table(header
);
1578 spin_unlock(&sysctl_lock
);
1580 EXPORT_SYMBOL(unregister_sysctl_table
);
1582 void setup_sysctl_set(struct ctl_table_set
*set
,
1583 struct ctl_table_root
*root
,
1584 int (*is_seen
)(struct ctl_table_set
*))
1586 memset(set
, 0, sizeof(*set
));
1587 set
->is_seen
= is_seen
;
1588 init_header(&set
->dir
.header
, root
, set
, NULL
, root_table
);
1591 void retire_sysctl_set(struct ctl_table_set
*set
)
1593 WARN_ON(!RB_EMPTY_ROOT(&set
->dir
.root
));
1596 int __init
proc_sys_init(void)
1598 struct proc_dir_entry
*proc_sys_root
;
1600 proc_sys_root
= proc_mkdir("sys", NULL
);
1601 proc_sys_root
->proc_iops
= &proc_sys_dir_operations
;
1602 proc_sys_root
->proc_fops
= &proc_sys_dir_file_operations
;
1603 proc_sys_root
->nlink
= 0;
1605 return sysctl_init();