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_IRUGO
|S_IXUGO
,
32 .child
= &root_table
[1],
36 static struct ctl_table_root sysctl_table_root
;
37 static struct ctl_table_header root_table_header
= {
40 .ctl_table
= root_table
,
41 .ctl_entry
= LIST_HEAD_INIT(sysctl_table_root
.default_set
.list
),}},
42 .root
= &sysctl_table_root
,
43 .set
= &sysctl_table_root
.default_set
,
45 static struct ctl_table_root sysctl_table_root
= {
46 .root_list
= LIST_HEAD_INIT(sysctl_table_root
.root_list
),
47 .default_set
.list
= LIST_HEAD_INIT(root_table_header
.ctl_entry
),
50 static DEFINE_SPINLOCK(sysctl_lock
);
52 static int namecmp(const char *name1
, int len1
, const char *name2
, int len2
)
61 cmp
= memcmp(name1
, name2
, minlen
);
67 static struct ctl_table
*find_entry(struct ctl_table_header
**phead
,
68 struct ctl_table_set
*set
,
69 struct ctl_table_header
*dir_head
, struct ctl_table
*dir
,
70 const char *name
, int namelen
)
72 struct ctl_table_header
*head
;
73 struct ctl_table
*entry
;
75 if (dir_head
->set
== set
) {
76 for (entry
= dir
; entry
->procname
; entry
++) {
77 const char *procname
= entry
->procname
;
78 if (namecmp(procname
, strlen(procname
), name
, namelen
) == 0) {
85 list_for_each_entry(head
, &set
->list
, ctl_entry
) {
86 if (head
->unregistering
)
88 if (head
->attached_to
!= dir
)
90 for (entry
= head
->attached_by
; entry
->procname
; entry
++) {
91 const char *procname
= entry
->procname
;
92 if (namecmp(procname
, strlen(procname
), name
, namelen
) == 0) {
101 static void init_header(struct ctl_table_header
*head
,
102 struct ctl_table_root
*root
, struct ctl_table_set
*set
,
103 struct ctl_table
*table
)
105 head
->ctl_table_arg
= table
;
106 INIT_LIST_HEAD(&head
->ctl_entry
);
110 head
->unregistering
= NULL
;
116 static void erase_header(struct ctl_table_header
*head
)
118 list_del_init(&head
->ctl_entry
);
121 static void insert_header(struct ctl_table_header
*header
)
123 header
->parent
->count
++;
124 list_add_tail(&header
->ctl_entry
, &header
->set
->list
);
127 /* called under sysctl_lock */
128 static int use_table(struct ctl_table_header
*p
)
130 if (unlikely(p
->unregistering
))
136 /* called under sysctl_lock */
137 static void unuse_table(struct ctl_table_header
*p
)
140 if (unlikely(p
->unregistering
))
141 complete(p
->unregistering
);
144 /* called under sysctl_lock, will reacquire if has to wait */
145 static void start_unregistering(struct ctl_table_header
*p
)
148 * if p->used is 0, nobody will ever touch that entry again;
149 * we'll eliminate all paths to it before dropping sysctl_lock
151 if (unlikely(p
->used
)) {
152 struct completion wait
;
153 init_completion(&wait
);
154 p
->unregistering
= &wait
;
155 spin_unlock(&sysctl_lock
);
156 wait_for_completion(&wait
);
157 spin_lock(&sysctl_lock
);
159 /* anything non-NULL; we'll never dereference it */
160 p
->unregistering
= ERR_PTR(-EINVAL
);
163 * do not remove from the list until nobody holds it; walking the
164 * list in do_sysctl() relies on that.
169 static void sysctl_head_get(struct ctl_table_header
*head
)
171 spin_lock(&sysctl_lock
);
173 spin_unlock(&sysctl_lock
);
176 void sysctl_head_put(struct ctl_table_header
*head
)
178 spin_lock(&sysctl_lock
);
180 kfree_rcu(head
, rcu
);
181 spin_unlock(&sysctl_lock
);
184 static struct ctl_table_header
*sysctl_head_grab(struct ctl_table_header
*head
)
188 spin_lock(&sysctl_lock
);
189 if (!use_table(head
))
190 head
= ERR_PTR(-ENOENT
);
191 spin_unlock(&sysctl_lock
);
195 static void sysctl_head_finish(struct ctl_table_header
*head
)
199 spin_lock(&sysctl_lock
);
201 spin_unlock(&sysctl_lock
);
204 static struct ctl_table_set
*
205 lookup_header_set(struct ctl_table_root
*root
, struct nsproxy
*namespaces
)
207 struct ctl_table_set
*set
= &root
->default_set
;
209 set
= root
->lookup(root
, namespaces
);
213 static struct list_head
*
214 lookup_header_list(struct ctl_table_root
*root
, struct nsproxy
*namespaces
)
216 struct ctl_table_set
*set
= lookup_header_set(root
, namespaces
);
220 static struct ctl_table
*lookup_entry(struct ctl_table_header
**phead
,
221 struct ctl_table_header
*dir_head
,
222 struct ctl_table
*dir
,
223 const char *name
, int namelen
)
225 struct ctl_table_header
*head
;
226 struct ctl_table
*entry
;
227 struct ctl_table_root
*root
;
228 struct ctl_table_set
*set
;
230 spin_lock(&sysctl_lock
);
231 root
= &sysctl_table_root
;
233 set
= lookup_header_set(root
, current
->nsproxy
);
234 entry
= find_entry(&head
, set
, dir_head
, dir
, name
, namelen
);
235 if (entry
&& use_table(head
))
239 root
= list_entry(root
->root_list
.next
,
240 struct ctl_table_root
, root_list
);
241 } while (!entry
&& root
!= &sysctl_table_root
);
242 spin_unlock(&sysctl_lock
);
246 static struct ctl_table_header
*__sysctl_head_next(struct nsproxy
*namespaces
,
247 struct ctl_table_header
*prev
)
249 struct ctl_table_root
*root
;
250 struct list_head
*header_list
;
251 struct ctl_table_header
*head
;
252 struct list_head
*tmp
;
254 spin_lock(&sysctl_lock
);
257 tmp
= &prev
->ctl_entry
;
261 tmp
= &root_table_header
.ctl_entry
;
263 head
= list_entry(tmp
, struct ctl_table_header
, ctl_entry
);
265 if (!use_table(head
))
267 spin_unlock(&sysctl_lock
);
272 header_list
= lookup_header_list(root
, namespaces
);
273 if (tmp
!= header_list
)
277 root
= list_entry(root
->root_list
.next
,
278 struct ctl_table_root
, root_list
);
279 if (root
== &sysctl_table_root
)
281 header_list
= lookup_header_list(root
, namespaces
);
282 } while (list_empty(header_list
));
283 tmp
= header_list
->next
;
286 spin_unlock(&sysctl_lock
);
290 static struct ctl_table_header
*sysctl_head_next(struct ctl_table_header
*prev
)
292 return __sysctl_head_next(current
->nsproxy
, prev
);
295 void register_sysctl_root(struct ctl_table_root
*root
)
297 spin_lock(&sysctl_lock
);
298 list_add_tail(&root
->root_list
, &sysctl_table_root
.root_list
);
299 spin_unlock(&sysctl_lock
);
303 * sysctl_perm does NOT grant the superuser all rights automatically, because
304 * some sysctl variables are readonly even to root.
307 static int test_perm(int mode
, int op
)
311 else if (in_egroup_p(0))
313 if ((op
& ~mode
& (MAY_READ
|MAY_WRITE
|MAY_EXEC
)) == 0)
318 static int sysctl_perm(struct ctl_table_root
*root
, struct ctl_table
*table
, int op
)
322 if (root
->permissions
)
323 mode
= root
->permissions(root
, current
->nsproxy
, table
);
327 return test_perm(mode
, op
);
330 static struct inode
*proc_sys_make_inode(struct super_block
*sb
,
331 struct ctl_table_header
*head
, struct ctl_table
*table
)
334 struct proc_inode
*ei
;
336 inode
= new_inode(sb
);
340 inode
->i_ino
= get_next_ino();
342 sysctl_head_get(head
);
345 ei
->sysctl_entry
= table
;
347 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
348 inode
->i_mode
= table
->mode
;
350 inode
->i_mode
|= S_IFREG
;
351 inode
->i_op
= &proc_sys_inode_operations
;
352 inode
->i_fop
= &proc_sys_file_operations
;
354 inode
->i_mode
|= S_IFDIR
;
355 inode
->i_op
= &proc_sys_dir_operations
;
356 inode
->i_fop
= &proc_sys_dir_file_operations
;
362 static struct ctl_table_header
*grab_header(struct inode
*inode
)
364 struct ctl_table_header
*head
= PROC_I(inode
)->sysctl
;
366 head
= &root_table_header
;
367 return sysctl_head_grab(head
);
370 static struct dentry
*proc_sys_lookup(struct inode
*dir
, struct dentry
*dentry
,
371 struct nameidata
*nd
)
373 struct ctl_table_header
*head
= grab_header(dir
);
374 struct ctl_table
*table
= PROC_I(dir
)->sysctl_entry
;
375 struct ctl_table_header
*h
= NULL
;
376 struct qstr
*name
= &dentry
->d_name
;
379 struct dentry
*err
= ERR_PTR(-ENOENT
);
382 return ERR_CAST(head
);
384 if (table
&& !table
->child
) {
389 table
= table
? table
->child
: &head
->ctl_table
[1];
391 p
= lookup_entry(&h
, head
, table
, name
->name
, name
->len
);
395 err
= ERR_PTR(-ENOMEM
);
396 inode
= proc_sys_make_inode(dir
->i_sb
, h
? h
: head
, p
);
398 sysctl_head_finish(h
);
404 d_set_d_op(dentry
, &proc_sys_dentry_operations
);
405 d_add(dentry
, inode
);
408 sysctl_head_finish(head
);
412 static ssize_t
proc_sys_call_handler(struct file
*filp
, void __user
*buf
,
413 size_t count
, loff_t
*ppos
, int write
)
415 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
416 struct ctl_table_header
*head
= grab_header(inode
);
417 struct ctl_table
*table
= PROC_I(inode
)->sysctl_entry
;
422 return PTR_ERR(head
);
425 * At this point we know that the sysctl was not unregistered
426 * and won't be until we finish.
429 if (sysctl_perm(head
->root
, table
, write
? MAY_WRITE
: MAY_READ
))
432 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
434 if (!table
->proc_handler
)
437 /* careful: calling conventions are nasty here */
439 error
= table
->proc_handler(table
, write
, buf
, &res
, ppos
);
443 sysctl_head_finish(head
);
448 static ssize_t
proc_sys_read(struct file
*filp
, char __user
*buf
,
449 size_t count
, loff_t
*ppos
)
451 return proc_sys_call_handler(filp
, (void __user
*)buf
, count
, ppos
, 0);
454 static ssize_t
proc_sys_write(struct file
*filp
, const char __user
*buf
,
455 size_t count
, loff_t
*ppos
)
457 return proc_sys_call_handler(filp
, (void __user
*)buf
, count
, ppos
, 1);
460 static int proc_sys_open(struct inode
*inode
, struct file
*filp
)
462 struct ctl_table
*table
= PROC_I(inode
)->sysctl_entry
;
465 filp
->private_data
= proc_sys_poll_event(table
->poll
);
470 static unsigned int proc_sys_poll(struct file
*filp
, poll_table
*wait
)
472 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
473 struct ctl_table
*table
= PROC_I(inode
)->sysctl_entry
;
474 unsigned long event
= (unsigned long)filp
->private_data
;
475 unsigned int ret
= DEFAULT_POLLMASK
;
477 if (!table
->proc_handler
)
483 poll_wait(filp
, &table
->poll
->wait
, wait
);
485 if (event
!= atomic_read(&table
->poll
->event
)) {
486 filp
->private_data
= proc_sys_poll_event(table
->poll
);
487 ret
= POLLIN
| POLLRDNORM
| POLLERR
| POLLPRI
;
494 static int proc_sys_fill_cache(struct file
*filp
, void *dirent
,
496 struct ctl_table_header
*head
,
497 struct ctl_table
*table
)
499 struct dentry
*child
, *dir
= filp
->f_path
.dentry
;
503 unsigned type
= DT_UNKNOWN
;
505 qname
.name
= table
->procname
;
506 qname
.len
= strlen(table
->procname
);
507 qname
.hash
= full_name_hash(qname
.name
, qname
.len
);
509 child
= d_lookup(dir
, &qname
);
511 child
= d_alloc(dir
, &qname
);
513 inode
= proc_sys_make_inode(dir
->d_sb
, head
, table
);
518 d_set_d_op(child
, &proc_sys_dentry_operations
);
525 inode
= child
->d_inode
;
527 type
= inode
->i_mode
>> 12;
529 return !!filldir(dirent
, qname
.name
, qname
.len
, filp
->f_pos
, ino
, type
);
532 static int scan(struct ctl_table_header
*head
, ctl_table
*table
,
533 unsigned long *pos
, struct file
*file
,
534 void *dirent
, filldir_t filldir
)
537 for (; table
->procname
; table
++, (*pos
)++) {
540 if (*pos
< file
->f_pos
)
543 res
= proc_sys_fill_cache(file
, dirent
, filldir
, head
, table
);
547 file
->f_pos
= *pos
+ 1;
552 static int proc_sys_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
554 struct dentry
*dentry
= filp
->f_path
.dentry
;
555 struct inode
*inode
= dentry
->d_inode
;
556 struct ctl_table_header
*head
= grab_header(inode
);
557 struct ctl_table
*table
= PROC_I(inode
)->sysctl_entry
;
558 struct ctl_table_header
*h
= NULL
;
563 return PTR_ERR(head
);
565 if (table
&& !table
->child
) {
570 table
= table
? table
->child
: &head
->ctl_table
[1];
573 /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
574 if (filp
->f_pos
== 0) {
575 if (filldir(dirent
, ".", 1, filp
->f_pos
,
576 inode
->i_ino
, DT_DIR
) < 0)
580 if (filp
->f_pos
== 1) {
581 if (filldir(dirent
, "..", 2, filp
->f_pos
,
582 parent_ino(dentry
), DT_DIR
) < 0)
588 ret
= scan(head
, table
, &pos
, filp
, dirent
, filldir
);
592 for (h
= sysctl_head_next(NULL
); h
; h
= sysctl_head_next(h
)) {
593 if (h
->attached_to
!= table
)
595 ret
= scan(h
, h
->attached_by
, &pos
, filp
, dirent
, filldir
);
597 sysctl_head_finish(h
);
603 sysctl_head_finish(head
);
607 static int proc_sys_permission(struct inode
*inode
, int mask
)
610 * sysctl entries that are not writeable,
611 * are _NOT_ writeable, capabilities or not.
613 struct ctl_table_header
*head
;
614 struct ctl_table
*table
;
617 /* Executable files are not allowed under /proc/sys/ */
618 if ((mask
& MAY_EXEC
) && S_ISREG(inode
->i_mode
))
621 head
= grab_header(inode
);
623 return PTR_ERR(head
);
625 table
= PROC_I(inode
)->sysctl_entry
;
626 if (!table
) /* global root - r-xr-xr-x */
627 error
= mask
& MAY_WRITE
? -EACCES
: 0;
628 else /* Use the permissions on the sysctl table entry */
629 error
= sysctl_perm(head
->root
, table
, mask
& ~MAY_NOT_BLOCK
);
631 sysctl_head_finish(head
);
635 static int proc_sys_setattr(struct dentry
*dentry
, struct iattr
*attr
)
637 struct inode
*inode
= dentry
->d_inode
;
640 if (attr
->ia_valid
& (ATTR_MODE
| ATTR_UID
| ATTR_GID
))
643 error
= inode_change_ok(inode
, attr
);
647 if ((attr
->ia_valid
& ATTR_SIZE
) &&
648 attr
->ia_size
!= i_size_read(inode
)) {
649 error
= vmtruncate(inode
, attr
->ia_size
);
654 setattr_copy(inode
, attr
);
655 mark_inode_dirty(inode
);
659 static int proc_sys_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
661 struct inode
*inode
= dentry
->d_inode
;
662 struct ctl_table_header
*head
= grab_header(inode
);
663 struct ctl_table
*table
= PROC_I(inode
)->sysctl_entry
;
666 return PTR_ERR(head
);
668 generic_fillattr(inode
, stat
);
670 stat
->mode
= (stat
->mode
& S_IFMT
) | table
->mode
;
672 sysctl_head_finish(head
);
676 static const struct file_operations proc_sys_file_operations
= {
677 .open
= proc_sys_open
,
678 .poll
= proc_sys_poll
,
679 .read
= proc_sys_read
,
680 .write
= proc_sys_write
,
681 .llseek
= default_llseek
,
684 static const struct file_operations proc_sys_dir_file_operations
= {
685 .read
= generic_read_dir
,
686 .readdir
= proc_sys_readdir
,
687 .llseek
= generic_file_llseek
,
690 static const struct inode_operations proc_sys_inode_operations
= {
691 .permission
= proc_sys_permission
,
692 .setattr
= proc_sys_setattr
,
693 .getattr
= proc_sys_getattr
,
696 static const struct inode_operations proc_sys_dir_operations
= {
697 .lookup
= proc_sys_lookup
,
698 .permission
= proc_sys_permission
,
699 .setattr
= proc_sys_setattr
,
700 .getattr
= proc_sys_getattr
,
703 static int proc_sys_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
705 if (nd
->flags
& LOOKUP_RCU
)
707 return !PROC_I(dentry
->d_inode
)->sysctl
->unregistering
;
710 static int proc_sys_delete(const struct dentry
*dentry
)
712 return !!PROC_I(dentry
->d_inode
)->sysctl
->unregistering
;
715 static int sysctl_is_seen(struct ctl_table_header
*p
)
717 struct ctl_table_set
*set
= p
->set
;
719 spin_lock(&sysctl_lock
);
720 if (p
->unregistering
)
722 else if (!set
->is_seen
)
725 res
= set
->is_seen(set
);
726 spin_unlock(&sysctl_lock
);
730 static int proc_sys_compare(const struct dentry
*parent
,
731 const struct inode
*pinode
,
732 const struct dentry
*dentry
, const struct inode
*inode
,
733 unsigned int len
, const char *str
, const struct qstr
*name
)
735 struct ctl_table_header
*head
;
736 /* Although proc doesn't have negative dentries, rcu-walk means
737 * that inode here can be NULL */
738 /* AV: can it, indeed? */
741 if (name
->len
!= len
)
743 if (memcmp(name
->name
, str
, len
))
745 head
= rcu_dereference(PROC_I(inode
)->sysctl
);
746 return !head
|| !sysctl_is_seen(head
);
749 static const struct dentry_operations proc_sys_dentry_operations
= {
750 .d_revalidate
= proc_sys_revalidate
,
751 .d_delete
= proc_sys_delete
,
752 .d_compare
= proc_sys_compare
,
755 static struct ctl_table
*is_branch_in(struct ctl_table
*branch
,
756 struct ctl_table
*table
)
759 const char *s
= branch
->procname
;
761 /* branch should have named subdirectory as its first element */
762 if (!s
|| !branch
->child
)
765 /* ... and nothing else */
766 if (branch
[1].procname
)
769 /* table should contain subdirectory with the same name */
770 for (p
= table
; p
->procname
; p
++) {
773 if (p
->procname
&& strcmp(p
->procname
, s
) == 0)
779 /* see if attaching q to p would be an improvement */
780 static void try_attach(struct ctl_table_header
*p
, struct ctl_table_header
*q
)
782 struct ctl_table
*to
= p
->ctl_table
, *by
= q
->ctl_table
;
783 struct ctl_table
*next
;
785 int not_in_parent
= !p
->attached_by
;
787 while ((next
= is_branch_in(by
, to
)) != NULL
) {
788 if (by
== q
->attached_by
)
790 if (to
== p
->attached_by
)
796 if (is_better
&& not_in_parent
) {
803 static int sysctl_check_table_dups(const char *path
, struct ctl_table
*old
,
804 struct ctl_table
*table
)
806 struct ctl_table
*entry
, *test
;
809 for (entry
= old
; entry
->procname
; entry
++) {
810 for (test
= table
; test
->procname
; test
++) {
811 if (strcmp(entry
->procname
, test
->procname
) == 0) {
812 printk(KERN_ERR
"sysctl duplicate entry: %s/%s\n",
813 path
, test
->procname
);
821 static int sysctl_check_dups(struct nsproxy
*namespaces
,
822 struct ctl_table_header
*header
,
823 const char *path
, struct ctl_table
*table
)
825 struct ctl_table_root
*root
;
826 struct ctl_table_set
*set
;
827 struct ctl_table_header
*dir_head
, *head
;
828 struct ctl_table
*dir_table
;
831 /* No dups if we are the only member of our directory */
832 if (header
->attached_by
!= table
)
835 dir_head
= header
->parent
;
836 dir_table
= header
->attached_to
;
838 error
= sysctl_check_table_dups(path
, dir_table
, table
);
840 root
= &sysctl_table_root
;
842 set
= lookup_header_set(root
, namespaces
);
844 list_for_each_entry(head
, &set
->list
, ctl_entry
) {
845 if (head
->unregistering
)
847 if (head
->attached_to
!= dir_table
)
849 error
= sysctl_check_table_dups(path
, head
->attached_by
,
852 root
= list_entry(root
->root_list
.next
,
853 struct ctl_table_root
, root_list
);
854 } while (root
!= &sysctl_table_root
);
858 static int sysctl_err(const char *path
, struct ctl_table
*table
, char *fmt
, ...)
860 struct va_format vaf
;
867 printk(KERN_ERR
"sysctl table check failed: %s/%s %pV\n",
868 path
, table
->procname
, &vaf
);
874 static int sysctl_check_table(const char *path
, struct ctl_table
*table
)
877 for (; table
->procname
; table
++) {
879 err
= sysctl_err(path
, table
, "Not a file");
881 if ((table
->proc_handler
== proc_dostring
) ||
882 (table
->proc_handler
== proc_dointvec
) ||
883 (table
->proc_handler
== proc_dointvec_minmax
) ||
884 (table
->proc_handler
== proc_dointvec_jiffies
) ||
885 (table
->proc_handler
== proc_dointvec_userhz_jiffies
) ||
886 (table
->proc_handler
== proc_dointvec_ms_jiffies
) ||
887 (table
->proc_handler
== proc_doulongvec_minmax
) ||
888 (table
->proc_handler
== proc_doulongvec_ms_jiffies_minmax
)) {
890 err
= sysctl_err(path
, table
, "No data");
892 err
= sysctl_err(path
, table
, "No maxlen");
894 if (!table
->proc_handler
)
895 err
= sysctl_err(path
, table
, "No proc_handler");
897 if ((table
->mode
& (S_IRUGO
|S_IWUGO
)) != table
->mode
)
898 err
= sysctl_err(path
, table
, "bogus .mode 0%o",
905 * __register_sysctl_table - register a leaf sysctl table
906 * @root: List of sysctl headers to register on
907 * @namespaces: Data to compute which lists of sysctl entries are visible
908 * @path: The path to the directory the sysctl table is in.
909 * @table: the top-level table structure
911 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
912 * array. A completely 0 filled entry terminates the table.
914 * The members of the &struct ctl_table structure are used as follows:
916 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
917 * enter a sysctl file
919 * data - a pointer to data for use by proc_handler
921 * maxlen - the maximum size in bytes of the data
923 * mode - the file permissions for the /proc/sys file
925 * child - must be %NULL.
927 * proc_handler - the text handler routine (described below)
929 * extra1, extra2 - extra pointers usable by the proc handler routines
931 * Leaf nodes in the sysctl tree will be represented by a single file
932 * under /proc; non-leaf nodes will be represented by directories.
934 * There must be a proc_handler routine for any terminal nodes.
935 * Several default handlers are available to cover common cases -
937 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
938 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
939 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
941 * It is the handler's job to read the input buffer from user memory
942 * and process it. The handler should return 0 on success.
944 * This routine returns %NULL on a failure to register, and a pointer
945 * to the table header on success.
947 struct ctl_table_header
*__register_sysctl_table(
948 struct ctl_table_root
*root
,
949 struct nsproxy
*namespaces
,
950 const char *path
, struct ctl_table
*table
)
952 struct ctl_table_header
*header
;
953 struct ctl_table
*new, **prevp
;
954 const char *name
, *nextname
;
955 unsigned int npath
= 0;
956 struct ctl_table_set
*set
;
957 size_t path_bytes
= 0;
960 /* Count the path components */
961 for (name
= path
; name
; name
= nextname
) {
963 nextname
= strchr(name
, '/');
965 namelen
= nextname
- name
;
968 namelen
= strlen(name
);
972 path_bytes
+= namelen
+ 1;
977 * For each path component, allocate a 2-element ctl_table array.
978 * The first array element will be filled with the sysctl entry
979 * for this, the second will be the sentinel (procname == 0).
981 * We allocate everything in one go so that we don't have to
982 * worry about freeing additional memory in unregister_sysctl_table.
984 header
= kzalloc(sizeof(struct ctl_table_header
) + path_bytes
+
985 (2 * npath
* sizeof(struct ctl_table
)), GFP_KERNEL
);
989 new = (struct ctl_table
*) (header
+ 1);
990 new_name
= (char *)(new + (2 * npath
));
992 /* Now connect the dots */
993 prevp
= &header
->ctl_table
;
994 for (name
= path
; name
; name
= nextname
) {
996 nextname
= strchr(name
, '/');
998 namelen
= nextname
- name
;
1001 namelen
= strlen(name
);
1005 memcpy(new_name
, name
, namelen
);
1006 new_name
[namelen
] = '\0';
1008 new->procname
= new_name
;
1012 prevp
= &new->child
;
1015 new_name
+= namelen
+ 1;
1019 init_header(header
, root
, NULL
, table
);
1020 if (sysctl_check_table(path
, table
))
1023 spin_lock(&sysctl_lock
);
1024 header
->set
= lookup_header_set(root
, namespaces
);
1025 header
->attached_by
= header
->ctl_table
;
1026 header
->attached_to
= &root_table
[1];
1027 header
->parent
= &root_table_header
;
1029 root
= header
->root
;
1031 struct ctl_table_header
*p
;
1032 list_for_each_entry(p
, &set
->list
, ctl_entry
) {
1033 if (p
->unregistering
)
1035 try_attach(p
, header
);
1037 if (root
== &sysctl_table_root
)
1039 root
= list_entry(root
->root_list
.prev
,
1040 struct ctl_table_root
, root_list
);
1041 set
= lookup_header_set(root
, namespaces
);
1043 if (sysctl_check_dups(namespaces
, header
, path
, table
))
1045 insert_header(header
);
1046 spin_unlock(&sysctl_lock
);
1050 spin_unlock(&sysctl_lock
);
1057 static char *append_path(const char *path
, char *pos
, const char *name
)
1060 namelen
= strlen(name
);
1061 if (((pos
- path
) + namelen
+ 2) >= PATH_MAX
)
1063 memcpy(pos
, name
, namelen
);
1065 pos
[namelen
+ 1] = '\0';
1070 static int count_subheaders(struct ctl_table
*table
)
1073 int nr_subheaders
= 0;
1074 struct ctl_table
*entry
;
1076 /* special case: no directory and empty directory */
1077 if (!table
|| !table
->procname
)
1080 for (entry
= table
; entry
->procname
; entry
++) {
1082 nr_subheaders
+= count_subheaders(entry
->child
);
1086 return nr_subheaders
+ has_files
;
1089 static int register_leaf_sysctl_tables(const char *path
, char *pos
,
1090 struct ctl_table_header
***subheader
,
1091 struct ctl_table_root
*root
, struct nsproxy
*namespaces
,
1092 struct ctl_table
*table
)
1094 struct ctl_table
*ctl_table_arg
= NULL
;
1095 struct ctl_table
*entry
, *files
;
1100 for (entry
= table
; entry
->procname
; entry
++) {
1108 /* If there are mixed files and directories we need a new table */
1109 if (nr_dirs
&& nr_files
) {
1110 struct ctl_table
*new;
1111 files
= kzalloc(sizeof(struct ctl_table
) * (nr_files
+ 1),
1116 ctl_table_arg
= files
;
1117 for (new = files
, entry
= table
; entry
->procname
; entry
++) {
1125 /* Register everything except a directory full of subdirectories */
1126 if (nr_files
|| !nr_dirs
) {
1127 struct ctl_table_header
*header
;
1128 header
= __register_sysctl_table(root
, namespaces
, path
, files
);
1130 kfree(ctl_table_arg
);
1134 /* Remember if we need to free the file table */
1135 header
->ctl_table_arg
= ctl_table_arg
;
1136 **subheader
= header
;
1140 /* Recurse into the subdirectories. */
1141 for (entry
= table
; entry
->procname
; entry
++) {
1147 err
= -ENAMETOOLONG
;
1148 child_pos
= append_path(path
, pos
, entry
->procname
);
1152 err
= register_leaf_sysctl_tables(path
, child_pos
, subheader
,
1153 root
, namespaces
, entry
->child
);
1160 /* On failure our caller will unregister all registered subheaders */
1165 * __register_sysctl_paths - register a sysctl table hierarchy
1166 * @root: List of sysctl headers to register on
1167 * @namespaces: Data to compute which lists of sysctl entries are visible
1168 * @path: The path to the directory the sysctl table is in.
1169 * @table: the top-level table structure
1171 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1172 * array. A completely 0 filled entry terminates the table.
1174 * See __register_sysctl_table for more details.
1176 struct ctl_table_header
*__register_sysctl_paths(
1177 struct ctl_table_root
*root
,
1178 struct nsproxy
*namespaces
,
1179 const struct ctl_path
*path
, struct ctl_table
*table
)
1181 struct ctl_table
*ctl_table_arg
= table
;
1182 int nr_subheaders
= count_subheaders(table
);
1183 struct ctl_table_header
*header
= NULL
, **subheaders
, **subheader
;
1184 const struct ctl_path
*component
;
1185 char *new_path
, *pos
;
1187 pos
= new_path
= kmalloc(PATH_MAX
, GFP_KERNEL
);
1192 for (component
= path
; component
->procname
; component
++) {
1193 pos
= append_path(new_path
, pos
, component
->procname
);
1197 while (table
->procname
&& table
->child
&& !table
[1].procname
) {
1198 pos
= append_path(new_path
, pos
, table
->procname
);
1201 table
= table
->child
;
1203 if (nr_subheaders
== 1) {
1204 header
= __register_sysctl_table(root
, namespaces
, new_path
, table
);
1206 header
->ctl_table_arg
= ctl_table_arg
;
1208 header
= kzalloc(sizeof(*header
) +
1209 sizeof(*subheaders
)*nr_subheaders
, GFP_KERNEL
);
1213 subheaders
= (struct ctl_table_header
**) (header
+ 1);
1214 subheader
= subheaders
;
1215 header
->ctl_table_arg
= ctl_table_arg
;
1217 if (register_leaf_sysctl_tables(new_path
, pos
, &subheader
,
1218 root
, namespaces
, table
))
1219 goto err_register_leaves
;
1226 err_register_leaves
:
1227 while (subheader
> subheaders
) {
1228 struct ctl_table_header
*subh
= *(--subheader
);
1229 struct ctl_table
*table
= subh
->ctl_table_arg
;
1230 unregister_sysctl_table(subh
);
1239 * register_sysctl_table_path - register a sysctl table hierarchy
1240 * @path: The path to the directory the sysctl table is in.
1241 * @table: the top-level table structure
1243 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1244 * array. A completely 0 filled entry terminates the table.
1246 * See __register_sysctl_paths for more details.
1248 struct ctl_table_header
*register_sysctl_paths(const struct ctl_path
*path
,
1249 struct ctl_table
*table
)
1251 return __register_sysctl_paths(&sysctl_table_root
, current
->nsproxy
,
1254 EXPORT_SYMBOL(register_sysctl_paths
);
1257 * register_sysctl_table - register a sysctl table hierarchy
1258 * @table: the top-level table structure
1260 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1261 * array. A completely 0 filled entry terminates the table.
1263 * See register_sysctl_paths for more details.
1265 struct ctl_table_header
*register_sysctl_table(struct ctl_table
*table
)
1267 static const struct ctl_path null_path
[] = { {} };
1269 return register_sysctl_paths(null_path
, table
);
1271 EXPORT_SYMBOL(register_sysctl_table
);
1273 static void drop_sysctl_table(struct ctl_table_header
*header
)
1278 start_unregistering(header
);
1279 if (!--header
->parent
->count
) {
1281 kfree_rcu(header
->parent
, rcu
);
1283 if (!--header
->count
)
1284 kfree_rcu(header
, rcu
);
1288 * unregister_sysctl_table - unregister a sysctl table hierarchy
1289 * @header: the header returned from register_sysctl_table
1291 * Unregisters the sysctl table and all children. proc entries may not
1292 * actually be removed until they are no longer used by anyone.
1294 void unregister_sysctl_table(struct ctl_table_header
* header
)
1302 nr_subheaders
= count_subheaders(header
->ctl_table_arg
);
1303 if (unlikely(nr_subheaders
> 1)) {
1304 struct ctl_table_header
**subheaders
;
1307 subheaders
= (struct ctl_table_header
**)(header
+ 1);
1308 for (i
= nr_subheaders
-1; i
>= 0; i
--) {
1309 struct ctl_table_header
*subh
= subheaders
[i
];
1310 struct ctl_table
*table
= subh
->ctl_table_arg
;
1311 unregister_sysctl_table(subh
);
1318 spin_lock(&sysctl_lock
);
1319 drop_sysctl_table(header
);
1320 spin_unlock(&sysctl_lock
);
1322 EXPORT_SYMBOL(unregister_sysctl_table
);
1324 void setup_sysctl_set(struct ctl_table_set
*p
,
1325 int (*is_seen
)(struct ctl_table_set
*))
1327 INIT_LIST_HEAD(&p
->list
);
1328 p
->is_seen
= is_seen
;
1331 void retire_sysctl_set(struct ctl_table_set
*set
)
1333 WARN_ON(!list_empty(&set
->list
));
1336 int __init
proc_sys_init(void)
1338 struct proc_dir_entry
*proc_sys_root
;
1340 proc_sys_root
= proc_mkdir("sys", NULL
);
1341 proc_sys_root
->proc_iops
= &proc_sys_dir_operations
;
1342 proc_sys_root
->proc_fops
= &proc_sys_dir_file_operations
;
1343 proc_sys_root
->nlink
= 0;
1345 return sysctl_init();