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
[1];
29 static struct ctl_table_root sysctl_table_root
;
30 static struct ctl_table_header root_table_header
= {
32 .ctl_table
= root_table
,
33 .ctl_entry
= LIST_HEAD_INIT(sysctl_table_root
.default_set
.list
),}},
34 .root
= &sysctl_table_root
,
35 .set
= &sysctl_table_root
.default_set
,
37 static struct ctl_table_root sysctl_table_root
= {
38 .root_list
= LIST_HEAD_INIT(sysctl_table_root
.root_list
),
39 .default_set
.list
= LIST_HEAD_INIT(root_table_header
.ctl_entry
),
42 static DEFINE_SPINLOCK(sysctl_lock
);
44 /* called under sysctl_lock */
45 static int use_table(struct ctl_table_header
*p
)
47 if (unlikely(p
->unregistering
))
53 /* called under sysctl_lock */
54 static void unuse_table(struct ctl_table_header
*p
)
57 if (unlikely(p
->unregistering
))
58 complete(p
->unregistering
);
61 /* called under sysctl_lock, will reacquire if has to wait */
62 static void start_unregistering(struct ctl_table_header
*p
)
65 * if p->used is 0, nobody will ever touch that entry again;
66 * we'll eliminate all paths to it before dropping sysctl_lock
68 if (unlikely(p
->used
)) {
69 struct completion wait
;
70 init_completion(&wait
);
71 p
->unregistering
= &wait
;
72 spin_unlock(&sysctl_lock
);
73 wait_for_completion(&wait
);
74 spin_lock(&sysctl_lock
);
76 /* anything non-NULL; we'll never dereference it */
77 p
->unregistering
= ERR_PTR(-EINVAL
);
80 * do not remove from the list until nobody holds it; walking the
81 * list in do_sysctl() relies on that.
83 list_del_init(&p
->ctl_entry
);
86 static void sysctl_head_get(struct ctl_table_header
*head
)
88 spin_lock(&sysctl_lock
);
90 spin_unlock(&sysctl_lock
);
93 void sysctl_head_put(struct ctl_table_header
*head
)
95 spin_lock(&sysctl_lock
);
98 spin_unlock(&sysctl_lock
);
101 static struct ctl_table_header
*sysctl_head_grab(struct ctl_table_header
*head
)
105 spin_lock(&sysctl_lock
);
106 if (!use_table(head
))
107 head
= ERR_PTR(-ENOENT
);
108 spin_unlock(&sysctl_lock
);
112 static void sysctl_head_finish(struct ctl_table_header
*head
)
116 spin_lock(&sysctl_lock
);
118 spin_unlock(&sysctl_lock
);
121 static struct ctl_table_set
*
122 lookup_header_set(struct ctl_table_root
*root
, struct nsproxy
*namespaces
)
124 struct ctl_table_set
*set
= &root
->default_set
;
126 set
= root
->lookup(root
, namespaces
);
130 static struct list_head
*
131 lookup_header_list(struct ctl_table_root
*root
, struct nsproxy
*namespaces
)
133 struct ctl_table_set
*set
= lookup_header_set(root
, namespaces
);
137 static struct ctl_table_header
*__sysctl_head_next(struct nsproxy
*namespaces
,
138 struct ctl_table_header
*prev
)
140 struct ctl_table_root
*root
;
141 struct list_head
*header_list
;
142 struct ctl_table_header
*head
;
143 struct list_head
*tmp
;
145 spin_lock(&sysctl_lock
);
148 tmp
= &prev
->ctl_entry
;
152 tmp
= &root_table_header
.ctl_entry
;
154 head
= list_entry(tmp
, struct ctl_table_header
, ctl_entry
);
156 if (!use_table(head
))
158 spin_unlock(&sysctl_lock
);
163 header_list
= lookup_header_list(root
, namespaces
);
164 if (tmp
!= header_list
)
168 root
= list_entry(root
->root_list
.next
,
169 struct ctl_table_root
, root_list
);
170 if (root
== &sysctl_table_root
)
172 header_list
= lookup_header_list(root
, namespaces
);
173 } while (list_empty(header_list
));
174 tmp
= header_list
->next
;
177 spin_unlock(&sysctl_lock
);
181 static struct ctl_table_header
*sysctl_head_next(struct ctl_table_header
*prev
)
183 return __sysctl_head_next(current
->nsproxy
, prev
);
186 void register_sysctl_root(struct ctl_table_root
*root
)
188 spin_lock(&sysctl_lock
);
189 list_add_tail(&root
->root_list
, &sysctl_table_root
.root_list
);
190 spin_unlock(&sysctl_lock
);
194 * sysctl_perm does NOT grant the superuser all rights automatically, because
195 * some sysctl variables are readonly even to root.
198 static int test_perm(int mode
, int op
)
202 else if (in_egroup_p(0))
204 if ((op
& ~mode
& (MAY_READ
|MAY_WRITE
|MAY_EXEC
)) == 0)
209 static int sysctl_perm(struct ctl_table_root
*root
, struct ctl_table
*table
, int op
)
213 if (root
->permissions
)
214 mode
= root
->permissions(root
, current
->nsproxy
, table
);
218 return test_perm(mode
, op
);
221 static void sysctl_set_parent(struct ctl_table
*parent
, struct ctl_table
*table
)
223 for (; table
->procname
; table
++) {
224 table
->parent
= parent
;
226 sysctl_set_parent(table
, table
->child
);
231 static struct inode
*proc_sys_make_inode(struct super_block
*sb
,
232 struct ctl_table_header
*head
, struct ctl_table
*table
)
235 struct proc_inode
*ei
;
237 inode
= new_inode(sb
);
241 inode
->i_ino
= get_next_ino();
243 sysctl_head_get(head
);
246 ei
->sysctl_entry
= table
;
248 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
249 inode
->i_mode
= table
->mode
;
251 inode
->i_mode
|= S_IFREG
;
252 inode
->i_op
= &proc_sys_inode_operations
;
253 inode
->i_fop
= &proc_sys_file_operations
;
255 inode
->i_mode
|= S_IFDIR
;
257 inode
->i_op
= &proc_sys_dir_operations
;
258 inode
->i_fop
= &proc_sys_dir_file_operations
;
264 static struct ctl_table
*find_in_table(struct ctl_table
*p
, struct qstr
*name
)
266 for ( ; p
->procname
; p
++) {
267 if (strlen(p
->procname
) != name
->len
)
270 if (memcmp(p
->procname
, name
->name
, name
->len
) != 0)
279 static struct ctl_table_header
*grab_header(struct inode
*inode
)
281 if (PROC_I(inode
)->sysctl
)
282 return sysctl_head_grab(PROC_I(inode
)->sysctl
);
284 return sysctl_head_next(NULL
);
287 static struct dentry
*proc_sys_lookup(struct inode
*dir
, struct dentry
*dentry
,
288 struct nameidata
*nd
)
290 struct ctl_table_header
*head
= grab_header(dir
);
291 struct ctl_table
*table
= PROC_I(dir
)->sysctl_entry
;
292 struct ctl_table_header
*h
= NULL
;
293 struct qstr
*name
= &dentry
->d_name
;
296 struct dentry
*err
= ERR_PTR(-ENOENT
);
299 return ERR_CAST(head
);
301 if (table
&& !table
->child
) {
306 table
= table
? table
->child
: head
->ctl_table
;
308 p
= find_in_table(table
, name
);
310 for (h
= sysctl_head_next(NULL
); h
; h
= sysctl_head_next(h
)) {
311 if (h
->attached_to
!= table
)
313 p
= find_in_table(h
->attached_by
, name
);
322 err
= ERR_PTR(-ENOMEM
);
323 inode
= proc_sys_make_inode(dir
->i_sb
, h
? h
: head
, p
);
325 sysctl_head_finish(h
);
331 d_set_d_op(dentry
, &proc_sys_dentry_operations
);
332 d_add(dentry
, inode
);
335 sysctl_head_finish(head
);
339 static ssize_t
proc_sys_call_handler(struct file
*filp
, void __user
*buf
,
340 size_t count
, loff_t
*ppos
, int write
)
342 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
343 struct ctl_table_header
*head
= grab_header(inode
);
344 struct ctl_table
*table
= PROC_I(inode
)->sysctl_entry
;
349 return PTR_ERR(head
);
352 * At this point we know that the sysctl was not unregistered
353 * and won't be until we finish.
356 if (sysctl_perm(head
->root
, table
, write
? MAY_WRITE
: MAY_READ
))
359 /* if that can happen at all, it should be -EINVAL, not -EISDIR */
361 if (!table
->proc_handler
)
364 /* careful: calling conventions are nasty here */
366 error
= table
->proc_handler(table
, write
, buf
, &res
, ppos
);
370 sysctl_head_finish(head
);
375 static ssize_t
proc_sys_read(struct file
*filp
, char __user
*buf
,
376 size_t count
, loff_t
*ppos
)
378 return proc_sys_call_handler(filp
, (void __user
*)buf
, count
, ppos
, 0);
381 static ssize_t
proc_sys_write(struct file
*filp
, const char __user
*buf
,
382 size_t count
, loff_t
*ppos
)
384 return proc_sys_call_handler(filp
, (void __user
*)buf
, count
, ppos
, 1);
387 static int proc_sys_open(struct inode
*inode
, struct file
*filp
)
389 struct ctl_table
*table
= PROC_I(inode
)->sysctl_entry
;
392 filp
->private_data
= proc_sys_poll_event(table
->poll
);
397 static unsigned int proc_sys_poll(struct file
*filp
, poll_table
*wait
)
399 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
400 struct ctl_table
*table
= PROC_I(inode
)->sysctl_entry
;
401 unsigned long event
= (unsigned long)filp
->private_data
;
402 unsigned int ret
= DEFAULT_POLLMASK
;
404 if (!table
->proc_handler
)
410 poll_wait(filp
, &table
->poll
->wait
, wait
);
412 if (event
!= atomic_read(&table
->poll
->event
)) {
413 filp
->private_data
= proc_sys_poll_event(table
->poll
);
414 ret
= POLLIN
| POLLRDNORM
| POLLERR
| POLLPRI
;
421 static int proc_sys_fill_cache(struct file
*filp
, void *dirent
,
423 struct ctl_table_header
*head
,
424 struct ctl_table
*table
)
426 struct dentry
*child
, *dir
= filp
->f_path
.dentry
;
430 unsigned type
= DT_UNKNOWN
;
432 qname
.name
= table
->procname
;
433 qname
.len
= strlen(table
->procname
);
434 qname
.hash
= full_name_hash(qname
.name
, qname
.len
);
436 child
= d_lookup(dir
, &qname
);
438 child
= d_alloc(dir
, &qname
);
440 inode
= proc_sys_make_inode(dir
->d_sb
, head
, table
);
445 d_set_d_op(child
, &proc_sys_dentry_operations
);
452 inode
= child
->d_inode
;
454 type
= inode
->i_mode
>> 12;
456 return !!filldir(dirent
, qname
.name
, qname
.len
, filp
->f_pos
, ino
, type
);
459 static int scan(struct ctl_table_header
*head
, ctl_table
*table
,
460 unsigned long *pos
, struct file
*file
,
461 void *dirent
, filldir_t filldir
)
464 for (; table
->procname
; table
++, (*pos
)++) {
467 if (*pos
< file
->f_pos
)
470 res
= proc_sys_fill_cache(file
, dirent
, filldir
, head
, table
);
474 file
->f_pos
= *pos
+ 1;
479 static int proc_sys_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
481 struct dentry
*dentry
= filp
->f_path
.dentry
;
482 struct inode
*inode
= dentry
->d_inode
;
483 struct ctl_table_header
*head
= grab_header(inode
);
484 struct ctl_table
*table
= PROC_I(inode
)->sysctl_entry
;
485 struct ctl_table_header
*h
= NULL
;
490 return PTR_ERR(head
);
492 if (table
&& !table
->child
) {
497 table
= table
? table
->child
: head
->ctl_table
;
500 /* Avoid a switch here: arm builds fail with missing __cmpdi2 */
501 if (filp
->f_pos
== 0) {
502 if (filldir(dirent
, ".", 1, filp
->f_pos
,
503 inode
->i_ino
, DT_DIR
) < 0)
507 if (filp
->f_pos
== 1) {
508 if (filldir(dirent
, "..", 2, filp
->f_pos
,
509 parent_ino(dentry
), DT_DIR
) < 0)
515 ret
= scan(head
, table
, &pos
, filp
, dirent
, filldir
);
519 for (h
= sysctl_head_next(NULL
); h
; h
= sysctl_head_next(h
)) {
520 if (h
->attached_to
!= table
)
522 ret
= scan(h
, h
->attached_by
, &pos
, filp
, dirent
, filldir
);
524 sysctl_head_finish(h
);
530 sysctl_head_finish(head
);
534 static int proc_sys_permission(struct inode
*inode
, int mask
)
537 * sysctl entries that are not writeable,
538 * are _NOT_ writeable, capabilities or not.
540 struct ctl_table_header
*head
;
541 struct ctl_table
*table
;
544 /* Executable files are not allowed under /proc/sys/ */
545 if ((mask
& MAY_EXEC
) && S_ISREG(inode
->i_mode
))
548 head
= grab_header(inode
);
550 return PTR_ERR(head
);
552 table
= PROC_I(inode
)->sysctl_entry
;
553 if (!table
) /* global root - r-xr-xr-x */
554 error
= mask
& MAY_WRITE
? -EACCES
: 0;
555 else /* Use the permissions on the sysctl table entry */
556 error
= sysctl_perm(head
->root
, table
, mask
& ~MAY_NOT_BLOCK
);
558 sysctl_head_finish(head
);
562 static int proc_sys_setattr(struct dentry
*dentry
, struct iattr
*attr
)
564 struct inode
*inode
= dentry
->d_inode
;
567 if (attr
->ia_valid
& (ATTR_MODE
| ATTR_UID
| ATTR_GID
))
570 error
= inode_change_ok(inode
, attr
);
574 if ((attr
->ia_valid
& ATTR_SIZE
) &&
575 attr
->ia_size
!= i_size_read(inode
)) {
576 error
= vmtruncate(inode
, attr
->ia_size
);
581 setattr_copy(inode
, attr
);
582 mark_inode_dirty(inode
);
586 static int proc_sys_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
588 struct inode
*inode
= dentry
->d_inode
;
589 struct ctl_table_header
*head
= grab_header(inode
);
590 struct ctl_table
*table
= PROC_I(inode
)->sysctl_entry
;
593 return PTR_ERR(head
);
595 generic_fillattr(inode
, stat
);
597 stat
->mode
= (stat
->mode
& S_IFMT
) | table
->mode
;
599 sysctl_head_finish(head
);
603 static const struct file_operations proc_sys_file_operations
= {
604 .open
= proc_sys_open
,
605 .poll
= proc_sys_poll
,
606 .read
= proc_sys_read
,
607 .write
= proc_sys_write
,
608 .llseek
= default_llseek
,
611 static const struct file_operations proc_sys_dir_file_operations
= {
612 .read
= generic_read_dir
,
613 .readdir
= proc_sys_readdir
,
614 .llseek
= generic_file_llseek
,
617 static const struct inode_operations proc_sys_inode_operations
= {
618 .permission
= proc_sys_permission
,
619 .setattr
= proc_sys_setattr
,
620 .getattr
= proc_sys_getattr
,
623 static const struct inode_operations proc_sys_dir_operations
= {
624 .lookup
= proc_sys_lookup
,
625 .permission
= proc_sys_permission
,
626 .setattr
= proc_sys_setattr
,
627 .getattr
= proc_sys_getattr
,
630 static int proc_sys_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
632 if (nd
->flags
& LOOKUP_RCU
)
634 return !PROC_I(dentry
->d_inode
)->sysctl
->unregistering
;
637 static int proc_sys_delete(const struct dentry
*dentry
)
639 return !!PROC_I(dentry
->d_inode
)->sysctl
->unregistering
;
642 static int sysctl_is_seen(struct ctl_table_header
*p
)
644 struct ctl_table_set
*set
= p
->set
;
646 spin_lock(&sysctl_lock
);
647 if (p
->unregistering
)
649 else if (!set
->is_seen
)
652 res
= set
->is_seen(set
);
653 spin_unlock(&sysctl_lock
);
657 static int proc_sys_compare(const struct dentry
*parent
,
658 const struct inode
*pinode
,
659 const struct dentry
*dentry
, const struct inode
*inode
,
660 unsigned int len
, const char *str
, const struct qstr
*name
)
662 struct ctl_table_header
*head
;
663 /* Although proc doesn't have negative dentries, rcu-walk means
664 * that inode here can be NULL */
665 /* AV: can it, indeed? */
668 if (name
->len
!= len
)
670 if (memcmp(name
->name
, str
, len
))
672 head
= rcu_dereference(PROC_I(inode
)->sysctl
);
673 return !head
|| !sysctl_is_seen(head
);
676 static const struct dentry_operations proc_sys_dentry_operations
= {
677 .d_revalidate
= proc_sys_revalidate
,
678 .d_delete
= proc_sys_delete
,
679 .d_compare
= proc_sys_compare
,
682 static struct ctl_table
*is_branch_in(struct ctl_table
*branch
,
683 struct ctl_table
*table
)
686 const char *s
= branch
->procname
;
688 /* branch should have named subdirectory as its first element */
689 if (!s
|| !branch
->child
)
692 /* ... and nothing else */
693 if (branch
[1].procname
)
696 /* table should contain subdirectory with the same name */
697 for (p
= table
; p
->procname
; p
++) {
700 if (p
->procname
&& strcmp(p
->procname
, s
) == 0)
706 /* see if attaching q to p would be an improvement */
707 static void try_attach(struct ctl_table_header
*p
, struct ctl_table_header
*q
)
709 struct ctl_table
*to
= p
->ctl_table
, *by
= q
->ctl_table
;
710 struct ctl_table
*next
;
712 int not_in_parent
= !p
->attached_by
;
714 while ((next
= is_branch_in(by
, to
)) != NULL
) {
715 if (by
== q
->attached_by
)
717 if (to
== p
->attached_by
)
723 if (is_better
&& not_in_parent
) {
730 #ifdef CONFIG_SYSCTL_SYSCALL_CHECK
731 static int sysctl_depth(struct ctl_table
*table
)
733 struct ctl_table
*tmp
;
737 for (tmp
= table
; tmp
->parent
; tmp
= tmp
->parent
)
743 static struct ctl_table
*sysctl_parent(struct ctl_table
*table
, int n
)
747 for (i
= 0; table
&& i
< n
; i
++)
748 table
= table
->parent
;
754 static void sysctl_print_path(struct ctl_table
*table
)
756 struct ctl_table
*tmp
;
758 depth
= sysctl_depth(table
);
759 if (table
->procname
) {
760 for (i
= depth
; i
>= 0; i
--) {
761 tmp
= sysctl_parent(table
, i
);
762 printk("/%s", tmp
->procname
?tmp
->procname
:"");
768 static struct ctl_table
*sysctl_check_lookup(struct nsproxy
*namespaces
,
769 struct ctl_table
*table
)
771 struct ctl_table_header
*head
;
772 struct ctl_table
*ref
, *test
;
773 int depth
, cur_depth
;
775 depth
= sysctl_depth(table
);
777 for (head
= __sysctl_head_next(namespaces
, NULL
); head
;
778 head
= __sysctl_head_next(namespaces
, head
)) {
780 ref
= head
->ctl_table
;
782 test
= sysctl_parent(table
, cur_depth
);
783 for (; ref
->procname
; ref
++) {
785 if (cur_depth
&& !ref
->child
)
788 if (test
->procname
&& ref
->procname
&&
789 (strcmp(test
->procname
, ref
->procname
) == 0))
793 if (cur_depth
!= 0) {
804 sysctl_head_finish(head
);
808 static void set_fail(const char **fail
, struct ctl_table
*table
, const char *str
)
811 printk(KERN_ERR
"sysctl table check failed: ");
812 sysctl_print_path(table
);
813 printk(" %s\n", *fail
);
819 static void sysctl_check_leaf(struct nsproxy
*namespaces
,
820 struct ctl_table
*table
, const char **fail
)
822 struct ctl_table
*ref
;
824 ref
= sysctl_check_lookup(namespaces
, table
);
825 if (ref
&& (ref
!= table
))
826 set_fail(fail
, table
, "Sysctl already exists");
829 static int sysctl_check_table(struct nsproxy
*namespaces
, struct ctl_table
*table
)
832 for (; table
->procname
; table
++) {
833 const char *fail
= NULL
;
836 if (!table
->parent
->procname
)
837 set_fail(&fail
, table
, "Parent without procname");
841 set_fail(&fail
, table
, "Directory with data?");
843 set_fail(&fail
, table
, "Directory with maxlen?");
844 if ((table
->mode
& (S_IRUGO
|S_IXUGO
)) != table
->mode
)
845 set_fail(&fail
, table
, "Writable sysctl directory");
846 if (table
->proc_handler
)
847 set_fail(&fail
, table
, "Directory with proc_handler");
849 set_fail(&fail
, table
, "Directory with extra1");
851 set_fail(&fail
, table
, "Directory with extra2");
853 if ((table
->proc_handler
== proc_dostring
) ||
854 (table
->proc_handler
== proc_dointvec
) ||
855 (table
->proc_handler
== proc_dointvec_minmax
) ||
856 (table
->proc_handler
== proc_dointvec_jiffies
) ||
857 (table
->proc_handler
== proc_dointvec_userhz_jiffies
) ||
858 (table
->proc_handler
== proc_dointvec_ms_jiffies
) ||
859 (table
->proc_handler
== proc_doulongvec_minmax
) ||
860 (table
->proc_handler
== proc_doulongvec_ms_jiffies_minmax
)) {
862 set_fail(&fail
, table
, "No data");
864 set_fail(&fail
, table
, "No maxlen");
866 #ifdef CONFIG_PROC_SYSCTL
867 if (!table
->proc_handler
)
868 set_fail(&fail
, table
, "No proc_handler");
870 sysctl_check_leaf(namespaces
, table
, &fail
);
872 if (table
->mode
> 0777)
873 set_fail(&fail
, table
, "bogus .mode");
875 set_fail(&fail
, table
, NULL
);
879 error
|= sysctl_check_table(namespaces
, table
->child
);
883 #endif /* CONFIG_SYSCTL_SYSCALL_CHECK */
886 * __register_sysctl_paths - register a sysctl hierarchy
887 * @root: List of sysctl headers to register on
888 * @namespaces: Data to compute which lists of sysctl entries are visible
889 * @path: The path to the directory the sysctl table is in.
890 * @table: the top-level table structure
892 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
893 * array. A completely 0 filled entry terminates the table.
895 * The members of the &struct ctl_table structure are used as follows:
897 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
898 * enter a sysctl file
900 * data - a pointer to data for use by proc_handler
902 * maxlen - the maximum size in bytes of the data
904 * mode - the file permissions for the /proc/sys file, and for sysctl(2)
906 * child - a pointer to the child sysctl table if this entry is a directory, or
909 * proc_handler - the text handler routine (described below)
911 * de - for internal use by the sysctl routines
913 * extra1, extra2 - extra pointers usable by the proc handler routines
915 * Leaf nodes in the sysctl tree will be represented by a single file
916 * under /proc; non-leaf nodes will be represented by directories.
918 * sysctl(2) can automatically manage read and write requests through
919 * the sysctl table. The data and maxlen fields of the ctl_table
920 * struct enable minimal validation of the values being written to be
921 * performed, and the mode field allows minimal authentication.
923 * There must be a proc_handler routine for any terminal nodes
924 * mirrored under /proc/sys (non-terminals are handled by a built-in
925 * directory handler). Several default handlers are available to
926 * cover common cases -
928 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
929 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
930 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
932 * It is the handler's job to read the input buffer from user memory
933 * and process it. The handler should return 0 on success.
935 * This routine returns %NULL on a failure to register, and a pointer
936 * to the table header on success.
938 struct ctl_table_header
*__register_sysctl_paths(
939 struct ctl_table_root
*root
,
940 struct nsproxy
*namespaces
,
941 const struct ctl_path
*path
, struct ctl_table
*table
)
943 struct ctl_table_header
*header
;
944 struct ctl_table
*new, **prevp
;
945 unsigned int n
, npath
;
946 struct ctl_table_set
*set
;
948 /* Count the path components */
949 for (npath
= 0; path
[npath
].procname
; ++npath
)
953 * For each path component, allocate a 2-element ctl_table array.
954 * The first array element will be filled with the sysctl entry
955 * for this, the second will be the sentinel (procname == 0).
957 * We allocate everything in one go so that we don't have to
958 * worry about freeing additional memory in unregister_sysctl_table.
960 header
= kzalloc(sizeof(struct ctl_table_header
) +
961 (2 * npath
* sizeof(struct ctl_table
)), GFP_KERNEL
);
965 new = (struct ctl_table
*) (header
+ 1);
967 /* Now connect the dots */
968 prevp
= &header
->ctl_table
;
969 for (n
= 0; n
< npath
; ++n
, ++path
) {
970 /* Copy the procname */
971 new->procname
= path
->procname
;
980 header
->ctl_table_arg
= table
;
982 INIT_LIST_HEAD(&header
->ctl_entry
);
984 header
->unregistering
= NULL
;
986 sysctl_set_parent(NULL
, header
->ctl_table
);
988 #ifdef CONFIG_SYSCTL_SYSCALL_CHECK
989 if (sysctl_check_table(namespaces
, header
->ctl_table
)) {
994 spin_lock(&sysctl_lock
);
995 header
->set
= lookup_header_set(root
, namespaces
);
996 header
->attached_by
= header
->ctl_table
;
997 header
->attached_to
= root_table
;
998 header
->parent
= &root_table_header
;
999 for (set
= header
->set
; set
; set
= set
->parent
) {
1000 struct ctl_table_header
*p
;
1001 list_for_each_entry(p
, &set
->list
, ctl_entry
) {
1002 if (p
->unregistering
)
1004 try_attach(p
, header
);
1007 header
->parent
->count
++;
1008 list_add_tail(&header
->ctl_entry
, &header
->set
->list
);
1009 spin_unlock(&sysctl_lock
);
1015 * register_sysctl_table_path - register a sysctl table hierarchy
1016 * @path: The path to the directory the sysctl table is in.
1017 * @table: the top-level table structure
1019 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1020 * array. A completely 0 filled entry terminates the table.
1022 * See __register_sysctl_paths for more details.
1024 struct ctl_table_header
*register_sysctl_paths(const struct ctl_path
*path
,
1025 struct ctl_table
*table
)
1027 return __register_sysctl_paths(&sysctl_table_root
, current
->nsproxy
,
1030 EXPORT_SYMBOL(register_sysctl_paths
);
1033 * register_sysctl_table - register a sysctl table hierarchy
1034 * @table: the top-level table structure
1036 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1037 * array. A completely 0 filled entry terminates the table.
1039 * See register_sysctl_paths for more details.
1041 struct ctl_table_header
*register_sysctl_table(struct ctl_table
*table
)
1043 static const struct ctl_path null_path
[] = { {} };
1045 return register_sysctl_paths(null_path
, table
);
1047 EXPORT_SYMBOL(register_sysctl_table
);
1050 * unregister_sysctl_table - unregister a sysctl table hierarchy
1051 * @header: the header returned from register_sysctl_table
1053 * Unregisters the sysctl table and all children. proc entries may not
1054 * actually be removed until they are no longer used by anyone.
1056 void unregister_sysctl_table(struct ctl_table_header
* header
)
1063 spin_lock(&sysctl_lock
);
1064 start_unregistering(header
);
1065 if (!--header
->parent
->count
) {
1067 kfree_rcu(header
->parent
, rcu
);
1069 if (!--header
->count
)
1070 kfree_rcu(header
, rcu
);
1071 spin_unlock(&sysctl_lock
);
1073 EXPORT_SYMBOL(unregister_sysctl_table
);
1075 void setup_sysctl_set(struct ctl_table_set
*p
,
1076 struct ctl_table_set
*parent
,
1077 int (*is_seen
)(struct ctl_table_set
*))
1079 INIT_LIST_HEAD(&p
->list
);
1080 p
->parent
= parent
? parent
: &sysctl_table_root
.default_set
;
1081 p
->is_seen
= is_seen
;
1085 int __init
proc_sys_init(void)
1087 struct proc_dir_entry
*proc_sys_root
;
1089 proc_sys_root
= proc_mkdir("sys", NULL
);
1090 proc_sys_root
->proc_iops
= &proc_sys_dir_operations
;
1091 proc_sys_root
->proc_fops
= &proc_sys_dir_file_operations
;
1092 proc_sys_root
->nlink
= 0;
1094 return sysctl_init();