Skip to content
Snippets Groups Projects
sysctl.c 61.6 KiB
Newer Older
  • Learn to ignore specific revisions
  • Linus Torvalds's avatar
    Linus Torvalds committed
    		.data		= &vm_dirty_ratio,
    		.maxlen		= sizeof(vm_dirty_ratio),
    		.mode		= 0644,
    
    		.proc_handler	= dirty_ratio_handler,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		.extra1		= &zero,
    		.extra2		= &one_hundred,
    	},
    
    	{
    		.procname	= "dirty_bytes",
    		.data		= &vm_dirty_bytes,
    		.maxlen		= sizeof(vm_dirty_bytes),
    		.mode		= 0644,
    
    		.proc_handler	= dirty_bytes_handler,
    
    		.extra1		= &dirty_bytes_min,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	{
    		.procname	= "dirty_writeback_centisecs",
    
    		.data		= &dirty_writeback_interval,
    		.maxlen		= sizeof(dirty_writeback_interval),
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		.mode		= 0644,
    
    		.proc_handler	= dirty_writeback_centisecs_handler,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	},
    	{
    		.procname	= "dirty_expire_centisecs",
    
    		.data		= &dirty_expire_interval,
    		.maxlen		= sizeof(dirty_expire_interval),
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		.mode		= 0644,
    
    		.proc_handler	= proc_dointvec,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	},
    	{
    		.procname	= "nr_pdflush_threads",
    		.data		= &nr_pdflush_threads,
    		.maxlen		= sizeof nr_pdflush_threads,
    		.mode		= 0444 /* read-only*/,
    
    		.proc_handler	= proc_dointvec,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	},
    	{
    		.procname	= "swappiness",
    		.data		= &vm_swappiness,
    		.maxlen		= sizeof(vm_swappiness),
    		.mode		= 0644,
    
    		.proc_handler	= proc_dointvec_minmax,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		.extra1		= &zero,
    		.extra2		= &one_hundred,
    	},
    #ifdef CONFIG_HUGETLB_PAGE
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		.procname	= "nr_hugepages",
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		.maxlen		= sizeof(unsigned long),
    		.mode		= 0644,
    
    		.proc_handler	= hugetlb_sysctl_handler,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		.extra1		= (void *)&hugetlb_zero,
    		.extra2		= (void *)&hugetlb_infinity,
    
    	},
    #ifdef CONFIG_NUMA
    	{
    		.procname       = "nr_hugepages_mempolicy",
    		.data           = NULL,
    		.maxlen         = sizeof(unsigned long),
    		.mode           = 0644,
    		.proc_handler   = &hugetlb_mempolicy_sysctl_handler,
    		.extra1		= (void *)&hugetlb_zero,
    		.extra2		= (void *)&hugetlb_infinity,
    	},
    #endif
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	 {
    		.procname	= "hugetlb_shm_group",
    		.data		= &sysctl_hugetlb_shm_group,
    		.maxlen		= sizeof(gid_t),
    		.mode		= 0644,
    
    		.proc_handler	= proc_dointvec,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	 },
    
    	 {
    		.procname	= "hugepages_treat_as_movable",
    		.data		= &hugepages_treat_as_movable,
    		.maxlen		= sizeof(int),
    		.mode		= 0644,
    
    		.proc_handler	= hugetlb_treat_movable_handler,
    
    	{
    		.procname	= "nr_overcommit_hugepages",
    
    		.data		= NULL,
    		.maxlen		= sizeof(unsigned long),
    
    		.proc_handler	= hugetlb_overcommit_handler,
    
    		.extra1		= (void *)&hugetlb_zero,
    		.extra2		= (void *)&hugetlb_infinity,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    #endif
    	{
    		.procname	= "lowmem_reserve_ratio",
    		.data		= &sysctl_lowmem_reserve_ratio,
    		.maxlen		= sizeof(sysctl_lowmem_reserve_ratio),
    		.mode		= 0644,
    
    		.proc_handler	= lowmem_reserve_ratio_sysctl_handler,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	},
    
    Andrew Morton's avatar
    Andrew Morton committed
    	{
    		.procname	= "drop_caches",
    		.data		= &sysctl_drop_caches,
    		.maxlen		= sizeof(int),
    		.mode		= 0644,
    		.proc_handler	= drop_caches_sysctl_handler,
    	},
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	{
    		.procname	= "min_free_kbytes",
    		.data		= &min_free_kbytes,
    		.maxlen		= sizeof(min_free_kbytes),
    		.mode		= 0644,
    
    		.proc_handler	= min_free_kbytes_sysctl_handler,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		.extra1		= &zero,
    	},
    
    	{
    		.procname	= "percpu_pagelist_fraction",
    		.data		= &percpu_pagelist_fraction,
    		.maxlen		= sizeof(percpu_pagelist_fraction),
    		.mode		= 0644,
    
    		.proc_handler	= percpu_pagelist_fraction_sysctl_handler,
    
    		.extra1		= &min_percpu_pagelist_fract,
    	},
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    #ifdef CONFIG_MMU
    	{
    		.procname	= "max_map_count",
    		.data		= &sysctl_max_map_count,
    		.maxlen		= sizeof(sysctl_max_map_count),
    		.mode		= 0644,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	},
    
    #else
    	{
    		.procname	= "nr_trim_pages",
    		.data		= &sysctl_nr_trim_pages,
    		.maxlen		= sizeof(sysctl_nr_trim_pages),
    		.mode		= 0644,
    
    		.proc_handler	= proc_dointvec_minmax,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    #endif
    	{
    		.procname	= "laptop_mode",
    		.data		= &laptop_mode,
    		.maxlen		= sizeof(laptop_mode),
    		.mode		= 0644,
    
    		.proc_handler	= proc_dointvec_jiffies,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	},
    	{
    		.procname	= "block_dump",
    		.data		= &block_dump,
    		.maxlen		= sizeof(block_dump),
    		.mode		= 0644,
    
    		.proc_handler	= proc_dointvec,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		.extra1		= &zero,
    	},
    	{
    		.procname	= "vfs_cache_pressure",
    		.data		= &sysctl_vfs_cache_pressure,
    		.maxlen		= sizeof(sysctl_vfs_cache_pressure),
    		.mode		= 0644,
    
    		.proc_handler	= proc_dointvec,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		.extra1		= &zero,
    	},
    #ifdef HAVE_ARCH_PICK_MMAP_LAYOUT
    	{
    		.procname	= "legacy_va_layout",
    		.data		= &sysctl_legacy_va_layout,
    		.maxlen		= sizeof(sysctl_legacy_va_layout),
    		.mode		= 0644,
    
    		.proc_handler	= proc_dointvec,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		.extra1		= &zero,
    	},
    #endif
    
    #ifdef CONFIG_NUMA
    	{
    		.procname	= "zone_reclaim_mode",
    		.data		= &zone_reclaim_mode,
    		.maxlen		= sizeof(zone_reclaim_mode),
    		.mode		= 0644,
    
    		.proc_handler	= proc_dointvec,
    
    		.extra1		= &zero,
    
    	{
    		.procname	= "min_unmapped_ratio",
    		.data		= &sysctl_min_unmapped_ratio,
    		.maxlen		= sizeof(sysctl_min_unmapped_ratio),
    		.mode		= 0644,
    
    		.proc_handler	= sysctl_min_unmapped_ratio_sysctl_handler,
    
    	{
    		.procname	= "min_slab_ratio",
    		.data		= &sysctl_min_slab_ratio,
    		.maxlen		= sizeof(sysctl_min_slab_ratio),
    		.mode		= 0644,
    
    		.proc_handler	= sysctl_min_slab_ratio_sysctl_handler,
    
    		.extra1		= &zero,
    		.extra2		= &one_hundred,
    	},
    
    #ifdef CONFIG_SMP
    	{
    		.procname	= "stat_interval",
    		.data		= &sysctl_stat_interval,
    		.maxlen		= sizeof(sysctl_stat_interval),
    		.mode		= 0644,
    
    		.proc_handler	= proc_dointvec_jiffies,
    
    		.data		= &dac_mmap_min_addr,
    		.maxlen		= sizeof(unsigned long),
    
    		.proc_handler	= mmap_min_addr_handler,
    
    #ifdef CONFIG_NUMA
    	{
    		.procname	= "numa_zonelist_order",
    		.data		= &numa_zonelist_order,
    		.maxlen		= NUMA_ZONELIST_ORDER_LEN,
    		.mode		= 0644,
    
    		.proc_handler	= numa_zonelist_order_handler,
    
    #if (defined(CONFIG_X86_32) && !defined(CONFIG_UML))|| \
    
       (defined(CONFIG_SUPERH) && defined(CONFIG_VSYSCALL))
    
    	{
    		.procname	= "vdso_enabled",
    		.data		= &vdso_enabled,
    		.maxlen		= sizeof(vdso_enabled),
    		.mode		= 0644,
    
    		.proc_handler	= proc_dointvec,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    #endif
    
    #ifdef CONFIG_HIGHMEM
    	{
    		.procname	= "highmem_is_dirtyable",
    		.data		= &vm_highmem_is_dirtyable,
    		.maxlen		= sizeof(vm_highmem_is_dirtyable),
    		.mode		= 0644,
    
    		.proc_handler	= proc_dointvec_minmax,
    
    		.extra1		= &zero,
    		.extra2		= &one,
    	},
    #endif
    
    	{
    		.procname	= "scan_unevictable_pages",
    		.data		= &scan_unevictable_pages,
    		.maxlen		= sizeof(scan_unevictable_pages),
    		.mode		= 0644,
    
    		.proc_handler	= scan_unevictable_handler,
    
    #ifdef CONFIG_MEMORY_FAILURE
    	{
    		.procname	= "memory_failure_early_kill",
    		.data		= &sysctl_memory_failure_early_kill,
    		.maxlen		= sizeof(sysctl_memory_failure_early_kill),
    		.mode		= 0644,
    
    		.proc_handler	= proc_dointvec_minmax,
    
    		.extra1		= &zero,
    		.extra2		= &one,
    	},
    	{
    		.procname	= "memory_failure_recovery",
    		.data		= &sysctl_memory_failure_recovery,
    		.maxlen		= sizeof(sysctl_memory_failure_recovery),
    		.mode		= 0644,
    
    		.proc_handler	= proc_dointvec_minmax,
    
    /*
     * NOTE: do not add new entries to this table unless you have read
     * Documentation/sysctl/ctl_unnumbered.txt
     */
    
    #if defined(CONFIG_BINFMT_MISC) || defined(CONFIG_BINFMT_MISC_MODULE)
    
    static struct ctl_table binfmt_misc_table[] = {
    
    static struct ctl_table fs_table[] = {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	{
    		.procname	= "inode-nr",
    		.data		= &inodes_stat,
    		.maxlen		= 2*sizeof(int),
    		.mode		= 0444,
    
    		.proc_handler	= proc_dointvec,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	},
    	{
    		.procname	= "inode-state",
    		.data		= &inodes_stat,
    		.maxlen		= 7*sizeof(int),
    		.mode		= 0444,
    
    		.proc_handler	= proc_dointvec,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	},
    	{
    		.procname	= "file-nr",
    		.data		= &files_stat,
    		.maxlen		= 3*sizeof(int),
    		.mode		= 0444,
    
    		.proc_handler	= proc_nr_files,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	},
    	{
    		.procname	= "file-max",
    		.data		= &files_stat.max_files,
    		.maxlen		= sizeof(int),
    		.mode		= 0644,
    
    		.proc_handler	= proc_dointvec,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	},
    
    	{
    		.procname	= "nr_open",
    		.data		= &sysctl_nr_open,
    		.maxlen		= sizeof(int),
    		.mode		= 0644,
    
    		.proc_handler	= proc_dointvec_minmax,
    
    		.extra1		= &sysctl_nr_open_min,
    		.extra2		= &sysctl_nr_open_max,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	{
    		.procname	= "dentry-state",
    		.data		= &dentry_stat,
    		.maxlen		= 6*sizeof(int),
    		.mode		= 0444,
    
    		.proc_handler	= proc_dointvec,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	},
    	{
    		.procname	= "overflowuid",
    		.data		= &fs_overflowuid,
    		.maxlen		= sizeof(int),
    		.mode		= 0644,
    
    		.proc_handler	= proc_dointvec_minmax,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		.extra1		= &minolduid,
    		.extra2		= &maxolduid,
    	},
    	{
    		.procname	= "overflowgid",
    		.data		= &fs_overflowgid,
    		.maxlen		= sizeof(int),
    		.mode		= 0644,
    
    		.proc_handler	= proc_dointvec_minmax,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		.extra1		= &minolduid,
    		.extra2		= &maxolduid,
    	},
    
    #ifdef CONFIG_FILE_LOCKING
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	{
    		.procname	= "leases-enable",
    		.data		= &leases_enable,
    		.maxlen		= sizeof(int),
    		.mode		= 0644,
    
    		.proc_handler	= proc_dointvec,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	},
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    #ifdef CONFIG_DNOTIFY
    	{
    		.procname	= "dir-notify-enable",
    		.data		= &dir_notify_enable,
    		.maxlen		= sizeof(int),
    		.mode		= 0644,
    
    		.proc_handler	= proc_dointvec,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	},
    #endif
    #ifdef CONFIG_MMU
    
    #ifdef CONFIG_FILE_LOCKING
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	{
    		.procname	= "lease-break-time",
    		.data		= &lease_break_time,
    		.maxlen		= sizeof(int),
    		.mode		= 0644,
    
    		.proc_handler	= proc_dointvec,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	},
    
    #ifdef CONFIG_AIO
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	{
    		.procname	= "aio-nr",
    		.data		= &aio_nr,
    		.maxlen		= sizeof(aio_nr),
    		.mode		= 0444,
    
    		.proc_handler	= proc_doulongvec_minmax,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	},
    	{
    		.procname	= "aio-max-nr",
    		.data		= &aio_max_nr,
    		.maxlen		= sizeof(aio_max_nr),
    		.mode		= 0644,
    
    		.proc_handler	= proc_doulongvec_minmax,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	},
    
    #endif /* CONFIG_AIO */
    
    	{
    		.procname	= "inotify",
    		.mode		= 0555,
    		.child		= inotify_table,
    	},
    #endif	
    
    #ifdef CONFIG_EPOLL
    	{
    		.procname	= "epoll",
    		.mode		= 0555,
    		.child		= epoll_table,
    	},
    #endif
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    #endif
    
    Alan Cox's avatar
    Alan Cox committed
    	{
    		.procname	= "suid_dumpable",
    		.data		= &suid_dumpable,
    		.maxlen		= sizeof(int),
    		.mode		= 0644,
    
    		.proc_handler	= proc_dointvec_minmax,
    
    		.extra1		= &zero,
    		.extra2		= &two,
    
    Alan Cox's avatar
    Alan Cox committed
    	},
    
    #if defined(CONFIG_BINFMT_MISC) || defined(CONFIG_BINFMT_MISC_MODULE)
    	{
    		.procname	= "binfmt_misc",
    		.mode		= 0555,
    		.child		= binfmt_misc_table,
    	},
    #endif
    
    /*
     * NOTE: do not add new entries to this table unless you have read
     * Documentation/sysctl/ctl_unnumbered.txt
     */
    
    static struct ctl_table debug_table[] = {
    
    #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_SPARC)
    
    	{
    		.procname	= "exception-trace",
    		.data		= &show_unhandled_signals,
    		.maxlen		= sizeof(int),
    		.mode		= 0644,
    		.proc_handler	= proc_dointvec
    	},
    
    #endif
    #if defined(CONFIG_OPTPROBES)
    	{
    		.procname	= "kprobes-optimization",
    		.data		= &sysctl_kprobes_optimization,
    		.maxlen		= sizeof(int),
    		.mode		= 0644,
    		.proc_handler	= proc_kprobes_optimization_handler,
    		.extra1		= &zero,
    		.extra2		= &one,
    	},
    
    static struct ctl_table dev_table[] = {
    
    Robert Love's avatar
    Robert Love committed
    };
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    static DEFINE_SPINLOCK(sysctl_lock);
    
    /* called under sysctl_lock */
    static int use_table(struct ctl_table_header *p)
    {
    	if (unlikely(p->unregistering))
    		return 0;
    	p->used++;
    	return 1;
    }
    
    /* called under sysctl_lock */
    static void unuse_table(struct ctl_table_header *p)
    {
    	if (!--p->used)
    		if (unlikely(p->unregistering))
    			complete(p->unregistering);
    }
    
    /* called under sysctl_lock, will reacquire if has to wait */
    static void start_unregistering(struct ctl_table_header *p)
    {
    	/*
    	 * if p->used is 0, nobody will ever touch that entry again;
    	 * we'll eliminate all paths to it before dropping sysctl_lock
    	 */
    	if (unlikely(p->used)) {
    		struct completion wait;
    		init_completion(&wait);
    		p->unregistering = &wait;
    		spin_unlock(&sysctl_lock);
    		wait_for_completion(&wait);
    		spin_lock(&sysctl_lock);
    
    	} else {
    		/* anything non-NULL; we'll never dereference it */
    		p->unregistering = ERR_PTR(-EINVAL);
    
    	}
    	/*
    	 * do not remove from the list until nobody holds it; walking the
    	 * list in do_sysctl() relies on that.
    	 */
    	list_del_init(&p->ctl_entry);
    }
    
    
    void sysctl_head_get(struct ctl_table_header *head)
    {
    	spin_lock(&sysctl_lock);
    	head->count++;
    	spin_unlock(&sysctl_lock);
    }
    
    void sysctl_head_put(struct ctl_table_header *head)
    {
    	spin_lock(&sysctl_lock);
    	if (!--head->count)
    		kfree(head);
    	spin_unlock(&sysctl_lock);
    }
    
    struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
    {
    	if (!head)
    		BUG();
    	spin_lock(&sysctl_lock);
    	if (!use_table(head))
    		head = ERR_PTR(-ENOENT);
    	spin_unlock(&sysctl_lock);
    	return head;
    }
    
    
    void sysctl_head_finish(struct ctl_table_header *head)
    {
    	if (!head)
    		return;
    	spin_lock(&sysctl_lock);
    	unuse_table(head);
    	spin_unlock(&sysctl_lock);
    }
    
    
    static struct ctl_table_set *
    lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
    {
    	struct ctl_table_set *set = &root->default_set;
    	if (root->lookup)
    		set = root->lookup(root, namespaces);
    	return set;
    }
    
    
    static struct list_head *
    lookup_header_list(struct ctl_table_root *root, struct nsproxy *namespaces)
    
    	struct ctl_table_set *set = lookup_header_set(root, namespaces);
    	return &set->list;
    
    }
    
    struct ctl_table_header *__sysctl_head_next(struct nsproxy *namespaces,
    					    struct ctl_table_header *prev)
    {
    	struct ctl_table_root *root;
    	struct list_head *header_list;
    
    	struct ctl_table_header *head;
    	struct list_head *tmp;
    
    	spin_lock(&sysctl_lock);
    	if (prev) {
    
    		tmp = &prev->ctl_entry;
    		unuse_table(prev);
    		goto next;
    	}
    	tmp = &root_table_header.ctl_entry;
    	for (;;) {
    		head = list_entry(tmp, struct ctl_table_header, ctl_entry);
    
    		if (!use_table(head))
    			goto next;
    		spin_unlock(&sysctl_lock);
    		return head;
    	next:
    
    		header_list = lookup_header_list(root, namespaces);
    		if (tmp != header_list)
    			continue;
    
    		do {
    			root = list_entry(root->root_list.next,
    					struct ctl_table_root, root_list);
    			if (root == &sysctl_table_root)
    				goto out;
    			header_list = lookup_header_list(root, namespaces);
    		} while (list_empty(header_list));
    		tmp = header_list->next;
    
    struct ctl_table_header *sysctl_head_next(struct ctl_table_header *prev)
    {
    	return __sysctl_head_next(current->nsproxy, prev);
    }
    
    void register_sysctl_root(struct ctl_table_root *root)
    {
    	spin_lock(&sysctl_lock);
    	list_add_tail(&root->root_list, &sysctl_table_root.root_list);
    	spin_unlock(&sysctl_lock);
    }
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /*
    
     * sysctl_perm does NOT grant the superuser all rights automatically, because
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     * some sysctl variables are readonly even to root.
     */
    
    static int test_perm(int mode, int op)
    {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		mode >>= 6;
    	else if (in_egroup_p(0))
    		mode >>= 3;
    
    	if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		return 0;
    	return -EACCES;
    }
    
    
    int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	int error;
    
    	error = security_sysctl(table, op & (MAY_READ | MAY_WRITE | MAY_EXEC));
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (error)
    		return error;
    
    
    	if (root->permissions)
    		mode = root->permissions(root, current->nsproxy, table);
    	else
    		mode = table->mode;
    
    	return test_perm(mode, op);
    
    static void sysctl_set_parent(struct ctl_table *parent, struct ctl_table *table)
    {
    
    	for (; table->procname; table++) {
    
    		table->parent = parent;
    		if (table->child)
    			sysctl_set_parent(table, table->child);
    	}
    }
    
    static __init int sysctl_init(void)
    {
    	sysctl_set_parent(NULL, root_table);
    
    #ifdef CONFIG_SYSCTL_SYSCALL_CHECK
    	{
    		int err;
    		err = sysctl_check_table(current->nsproxy, root_table);
    	}
    #endif
    
    Al Viro's avatar
    Al Viro committed
    static struct ctl_table *is_branch_in(struct ctl_table *branch,
    				      struct ctl_table *table)
    
    {
    	struct ctl_table *p;
    	const char *s = branch->procname;
    
    	/* branch should have named subdirectory as its first element */
    	if (!s || !branch->child)
    
    Al Viro's avatar
    Al Viro committed
    		return NULL;
    
    
    	/* ... and nothing else */
    
    Al Viro's avatar
    Al Viro committed
    		return NULL;
    
    
    	/* table should contain subdirectory with the same name */
    
    	for (p = table; p->procname; p++) {
    
    		if (!p->child)
    			continue;
    		if (p->procname && strcmp(p->procname, s) == 0)
    
    Al Viro's avatar
    Al Viro committed
    			return p;
    
    Al Viro's avatar
    Al Viro committed
    	return NULL;
    
    }
    
    /* see if attaching q to p would be an improvement */
    static void try_attach(struct ctl_table_header *p, struct ctl_table_header *q)
    {
    	struct ctl_table *to = p->ctl_table, *by = q->ctl_table;
    
    Al Viro's avatar
    Al Viro committed
    	struct ctl_table *next;
    
    	int is_better = 0;
    	int not_in_parent = !p->attached_by;
    
    
    Al Viro's avatar
    Al Viro committed
    	while ((next = is_branch_in(by, to)) != NULL) {
    
    		if (by == q->attached_by)
    			is_better = 1;
    		if (to == p->attached_by)
    			not_in_parent = 1;
    		by = by->child;
    
    Al Viro's avatar
    Al Viro committed
    		to = next->child;
    
    	}
    
    	if (is_better && not_in_parent) {
    		q->attached_by = by;
    		q->attached_to = to;
    		q->parent = p;
    	}
    }
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /**
    
     * __register_sysctl_paths - register a sysctl hierarchy
     * @root: List of sysctl headers to register on
     * @namespaces: Data to compute which lists of sysctl entries are visible
    
     * @path: The path to the directory the sysctl table is in.
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     * @table: the top-level table structure
     *
     * Register a sysctl table hierarchy. @table should be a filled in ctl_table
    
     * array. A completely 0 filled entry terminates the table.
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     *
    
     * The members of the &struct ctl_table structure are used as follows:
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     *
     * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
     *            enter a sysctl file
     *
     * data - a pointer to data for use by proc_handler
     *
     * maxlen - the maximum size in bytes of the data
     *
     * mode - the file permissions for the /proc/sys file, and for sysctl(2)
     *
     * child - a pointer to the child sysctl table if this entry is a directory, or
     *         %NULL.
     *
     * proc_handler - the text handler routine (described below)
     *
     * de - for internal use by the sysctl routines
     *
     * extra1, extra2 - extra pointers usable by the proc handler routines
     *
     * Leaf nodes in the sysctl tree will be represented by a single file
     * under /proc; non-leaf nodes will be represented by directories.
     *
     * sysctl(2) can automatically manage read and write requests through
     * the sysctl table.  The data and maxlen fields of the ctl_table
     * struct enable minimal validation of the values being written to be
     * performed, and the mode field allows minimal authentication.
     *
     * There must be a proc_handler routine for any terminal nodes
     * mirrored under /proc/sys (non-terminals are handled by a built-in
     * directory handler).  Several default handlers are available to
     * cover common cases -
     *
     * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
     * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(), 
     * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
     *
     * It is the handler's job to read the input buffer from user memory
     * and process it. The handler should return 0 on success.
     *
     * This routine returns %NULL on a failure to register, and a pointer
     * to the table header on success.
     */
    
    struct ctl_table_header *__register_sysctl_paths(
    	struct ctl_table_root *root,
    	struct nsproxy *namespaces,
    	const struct ctl_path *path, struct ctl_table *table)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	struct ctl_table_header *header;
    	struct ctl_table *new, **prevp;
    	unsigned int n, npath;
    
    	struct ctl_table_set *set;
    
    
    	/* Count the path components */
    
    	for (npath = 0; path[npath].procname; ++npath)
    
    		;
    
    	/*
    	 * For each path component, allocate a 2-element ctl_table array.
    	 * The first array element will be filled with the sysctl entry
    
    	 * for this, the second will be the sentinel (procname == 0).
    
    	 *
    	 * We allocate everything in one go so that we don't have to
    	 * worry about freeing additional memory in unregister_sysctl_table.
    	 */
    	header = kzalloc(sizeof(struct ctl_table_header) +
    			 (2 * npath * sizeof(struct ctl_table)), GFP_KERNEL);
    	if (!header)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		return NULL;
    
    
    	new = (struct ctl_table *) (header + 1);
    
    	/* Now connect the dots */
    	prevp = &header->ctl_table;
    	for (n = 0; n < npath; ++n, ++path) {
    		/* Copy the procname */
    		new->procname = path->procname;
    		new->mode     = 0555;
    
    		*prevp = new;
    		prevp = &new->child;
    
    		new += 2;
    	}
    	*prevp = table;
    
    
    	INIT_LIST_HEAD(&header->ctl_entry);
    	header->used = 0;
    	header->unregistering = NULL;
    
    	sysctl_set_parent(NULL, header->ctl_table);
    
    	header->count = 1;
    
    #ifdef CONFIG_SYSCTL_SYSCALL_CHECK
    
    	if (sysctl_check_table(namespaces, header->ctl_table)) {
    
    	spin_lock(&sysctl_lock);
    
    	header->set = lookup_header_set(root, namespaces);
    
    	header->attached_by = header->ctl_table;
    	header->attached_to = root_table;
    	header->parent = &root_table_header;
    	for (set = header->set; set; set = set->parent) {
    		struct ctl_table_header *p;
    		list_for_each_entry(p, &set->list, ctl_entry) {
    			if (p->unregistering)
    				continue;
    			try_attach(p, header);
    		}
    	}
    	header->parent->count++;
    
    	list_add_tail(&header->ctl_entry, &header->set->list);
    
    	spin_unlock(&sysctl_lock);
    
    /**
     * register_sysctl_table_path - register a sysctl table hierarchy
     * @path: The path to the directory the sysctl table is in.
     * @table: the top-level table structure
     *
     * Register a sysctl table hierarchy. @table should be a filled in ctl_table
     * array. A completely 0 filled entry terminates the table.
     *
     * See __register_sysctl_paths for more details.
     */
    struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
    						struct ctl_table *table)
    {
    	return __register_sysctl_paths(&sysctl_table_root, current->nsproxy,
    					path, table);
    }
    
    
    /**
     * register_sysctl_table - register a sysctl table hierarchy
     * @table: the top-level table structure
     *
     * Register a sysctl table hierarchy. @table should be a filled in ctl_table
     * array. A completely 0 filled entry terminates the table.
     *
     * See register_sysctl_paths for more details.
     */
    struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
    {
    	static const struct ctl_path null_path[] = { {} };
    
    	return register_sysctl_paths(null_path, table);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    }
    
    /**
     * unregister_sysctl_table - unregister a sysctl table hierarchy
     * @header: the header returned from register_sysctl_table
     *
     * Unregisters the sysctl table and all children. proc entries may not
     * actually be removed until they are no longer used by anyone.
     */
    void unregister_sysctl_table(struct ctl_table_header * header)
    {
    
    	spin_lock(&sysctl_lock);
    	start_unregistering(header);
    
    	if (!--header->parent->count) {
    		WARN_ON(1);
    		kfree(header->parent);
    	}
    
    	if (!--header->count)
    		kfree(header);
    
    	spin_unlock(&sysctl_lock);
    
    Al Viro's avatar
    Al Viro committed
    int sysctl_is_seen(struct ctl_table_header *p)
    {
    	struct ctl_table_set *set = p->set;
    	int res;
    	spin_lock(&sysctl_lock);
    	if (p->unregistering)
    		res = 0;
    	else if (!set->is_seen)
    		res = 1;
    	else
    		res = set->is_seen(set);
    	spin_unlock(&sysctl_lock);
    	return res;
    }
    
    
    void setup_sysctl_set(struct ctl_table_set *p,
    	struct ctl_table_set *parent,
    	int (*is_seen)(struct ctl_table_set *))
    {
    	INIT_LIST_HEAD(&p->list);
    	p->parent = parent ? parent : &sysctl_table_root.default_set;
    	p->is_seen = is_seen;
    }
    
    
    #else /* !CONFIG_SYSCTL */
    
    struct ctl_table_header *register_sysctl_table(struct ctl_table * table)
    
    struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
    						    struct ctl_table *table)
    {
    	return NULL;
    }
    
    
    void unregister_sysctl_table(struct ctl_table_header * table)
    {
    }
    
    
    void setup_sysctl_set(struct ctl_table_set *p,
    	struct ctl_table_set *parent,
    	int (*is_seen)(struct ctl_table_set *))
    {
    }
    
    
    void sysctl_head_put(struct ctl_table_header *head)
    {
    }
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /*
     * /proc/sys support
     */
    
    
    #ifdef CONFIG_PROC_SYSCTL
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    static int _proc_do_string(void* data, int maxlen, int write,
    
    			   size_t *lenp, loff_t *ppos)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	size_t len;
    	char __user *p;
    	char c;
    
    
    	if (!data || !maxlen || !*lenp) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		*lenp = 0;
    		return 0;
    	}
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (write) {
    		len = 0;
    		p = buffer;
    		while (len < *lenp) {
    			if (get_user(c, p++))
    				return -EFAULT;
    			if (c == 0 || c == '\n')
    				break;
    			len++;
    		}
    
    		if (len >= maxlen)
    			len = maxlen-1;
    		if(copy_from_user(data, buffer, len))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			return -EFAULT;
    
    		((char *) data)[len] = 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		*ppos += *lenp;
    	} else {
    
    		len = strlen(data);
    		if (len > maxlen)
    			len = maxlen;
    
    
    		if (*ppos > len) {
    			*lenp = 0;
    			return 0;