aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/sysctl.c
diff options
context:
space:
mode:
authorEric W. Biederman <ebiederm@xmission.com>2012-01-06 07:07:15 -0500
committerEric W. Biederman <ebiederm@xmission.com>2012-01-24 19:37:54 -0500
commit1f87f0b52b1d6581168cb80f86746bc4df918d01 (patch)
tree73576c0872c61c526ade225d6441a6610beb666b /kernel/sysctl.c
parentde4e83bd6b5e16d491ec068cd22801d5d063b07a (diff)
sysctl: Move the implementation into fs/proc/proc_sysctl.c
Move the core sysctl code from kernel/sysctl.c and kernel/sysctl_check.c into fs/proc/proc_sysctl.c. Currently sysctl maintenance is hampered by the sysctl implementation being split across 3 files with artificial layering between them. Consolidate the entire sysctl implementation into 1 file so that it is easier to see what is going on and hopefully allowing for simpler maintenance. For functions that are now only used in fs/proc/proc_sysctl.c remove their declarations from sysctl.h and make them static in fs/proc/proc_sysctl.c Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
Diffstat (limited to 'kernel/sysctl.c')
-rw-r--r--kernel/sysctl.c464
1 files changed, 0 insertions, 464 deletions
diff --git a/kernel/sysctl.c b/kernel/sysctl.c
index ad460248acc7..b774909ed46c 100644
--- a/kernel/sysctl.c
+++ b/kernel/sysctl.c
@@ -192,20 +192,6 @@ static int sysrq_sysctl_handler(ctl_table *table, int write,
192 192
193#endif 193#endif
194 194
195static struct ctl_table root_table[1];
196static struct ctl_table_root sysctl_table_root;
197static struct ctl_table_header root_table_header = {
198 {{.count = 1,
199 .ctl_table = root_table,
200 .ctl_entry = LIST_HEAD_INIT(sysctl_table_root.default_set.list),}},
201 .root = &sysctl_table_root,
202 .set = &sysctl_table_root.default_set,
203};
204static struct ctl_table_root sysctl_table_root = {
205 .root_list = LIST_HEAD_INIT(sysctl_table_root.root_list),
206 .default_set.list = LIST_HEAD_INIT(root_table_header.ctl_entry),
207};
208
209static struct ctl_table kern_table[]; 195static struct ctl_table kern_table[];
210static struct ctl_table vm_table[]; 196static struct ctl_table vm_table[];
211static struct ctl_table fs_table[]; 197static struct ctl_table fs_table[];
@@ -1559,459 +1545,12 @@ static struct ctl_table dev_table[] = {
1559 { } 1545 { }
1560}; 1546};
1561 1547
1562static DEFINE_SPINLOCK(sysctl_lock);
1563
1564/* called under sysctl_lock */
1565static int use_table(struct ctl_table_header *p)
1566{
1567 if (unlikely(p->unregistering))
1568 return 0;
1569 p->used++;
1570 return 1;
1571}
1572
1573/* called under sysctl_lock */
1574static void unuse_table(struct ctl_table_header *p)
1575{
1576 if (!--p->used)
1577 if (unlikely(p->unregistering))
1578 complete(p->unregistering);
1579}
1580
1581/* called under sysctl_lock, will reacquire if has to wait */
1582static void start_unregistering(struct ctl_table_header *p)
1583{
1584 /*
1585 * if p->used is 0, nobody will ever touch that entry again;
1586 * we'll eliminate all paths to it before dropping sysctl_lock
1587 */
1588 if (unlikely(p->used)) {
1589 struct completion wait;
1590 init_completion(&wait);
1591 p->unregistering = &wait;
1592 spin_unlock(&sysctl_lock);
1593 wait_for_completion(&wait);
1594 spin_lock(&sysctl_lock);
1595 } else {
1596 /* anything non-NULL; we'll never dereference it */
1597 p->unregistering = ERR_PTR(-EINVAL);
1598 }
1599 /*
1600 * do not remove from the list until nobody holds it; walking the
1601 * list in do_sysctl() relies on that.
1602 */
1603 list_del_init(&p->ctl_entry);
1604}
1605
1606void sysctl_head_get(struct ctl_table_header *head)
1607{
1608 spin_lock(&sysctl_lock);
1609 head->count++;
1610 spin_unlock(&sysctl_lock);
1611}
1612
1613void sysctl_head_put(struct ctl_table_header *head)
1614{
1615 spin_lock(&sysctl_lock);
1616 if (!--head->count)
1617 kfree_rcu(head, rcu);
1618 spin_unlock(&sysctl_lock);
1619}
1620
1621struct ctl_table_header *sysctl_head_grab(struct ctl_table_header *head)
1622{
1623 if (!head)
1624 BUG();
1625 spin_lock(&sysctl_lock);
1626 if (!use_table(head))
1627 head = ERR_PTR(-ENOENT);
1628 spin_unlock(&sysctl_lock);
1629 return head;
1630}
1631
1632void sysctl_head_finish(struct ctl_table_header *head)
1633{
1634 if (!head)
1635 return;
1636 spin_lock(&sysctl_lock);
1637 unuse_table(head);
1638 spin_unlock(&sysctl_lock);
1639}
1640
1641static struct ctl_table_set *
1642lookup_header_set(struct ctl_table_root *root, struct nsproxy *namespaces)
1643{
1644 struct ctl_table_set *set = &root->default_set;
1645 if (root->lookup)
1646 set = root->lookup(root, namespaces);
1647 return set;
1648}
1649
1650static struct list_head *
1651lookup_header_list(struct ctl_table_root *root, struct nsproxy *namespaces)
1652{
1653 struct ctl_table_set *set = lookup_header_set(root, namespaces);
1654 return &set->list;
1655}
1656
1657struct ctl_table_header *__sysctl_head_next(struct nsproxy *namespaces,
1658 struct ctl_table_header *prev)
1659{
1660 struct ctl_table_root *root;
1661 struct list_head *header_list;
1662 struct ctl_table_header *head;
1663 struct list_head *tmp;
1664
1665 spin_lock(&sysctl_lock);
1666 if (prev) {
1667 head = prev;
1668 tmp = &prev->ctl_entry;
1669 unuse_table(prev);
1670 goto next;
1671 }
1672 tmp = &root_table_header.ctl_entry;
1673 for (;;) {
1674 head = list_entry(tmp, struct ctl_table_header, ctl_entry);
1675
1676 if (!use_table(head))
1677 goto next;
1678 spin_unlock(&sysctl_lock);
1679 return head;
1680 next:
1681 root = head->root;
1682 tmp = tmp->next;
1683 header_list = lookup_header_list(root, namespaces);
1684 if (tmp != header_list)
1685 continue;
1686
1687 do {
1688 root = list_entry(root->root_list.next,
1689 struct ctl_table_root, root_list);
1690 if (root == &sysctl_table_root)
1691 goto out;
1692 header_list = lookup_header_list(root, namespaces);
1693 } while (list_empty(header_list));
1694 tmp = header_list->next;
1695 }
1696out:
1697 spin_unlock(&sysctl_lock);
1698 return NULL;
1699}
1700
1701struct ctl_table_header *sysctl_head_next(struct ctl_table_header *prev)
1702{
1703 return __sysctl_head_next(current->nsproxy, prev);
1704}
1705
1706void register_sysctl_root(struct ctl_table_root *root)
1707{
1708 spin_lock(&sysctl_lock);
1709 list_add_tail(&root->root_list, &sysctl_table_root.root_list);
1710 spin_unlock(&sysctl_lock);
1711}
1712
1713/*
1714 * sysctl_perm does NOT grant the superuser all rights automatically, because
1715 * some sysctl variables are readonly even to root.
1716 */
1717
1718static int test_perm(int mode, int op)
1719{
1720 if (!current_euid())
1721 mode >>= 6;
1722 else if (in_egroup_p(0))
1723 mode >>= 3;
1724 if ((op & ~mode & (MAY_READ|MAY_WRITE|MAY_EXEC)) == 0)
1725 return 0;
1726 return -EACCES;
1727}
1728
1729int sysctl_perm(struct ctl_table_root *root, struct ctl_table *table, int op)
1730{
1731 int mode;
1732
1733 if (root->permissions)
1734 mode = root->permissions(root, current->nsproxy, table);
1735 else
1736 mode = table->mode;
1737
1738 return test_perm(mode, op);
1739}
1740
1741static void sysctl_set_parent(struct ctl_table *parent, struct ctl_table *table)
1742{
1743 for (; table->procname; table++) {
1744 table->parent = parent;
1745 if (table->child)
1746 sysctl_set_parent(table, table->child);
1747 }
1748}
1749
1750int __init sysctl_init(void) 1548int __init sysctl_init(void)
1751{ 1549{
1752 register_sysctl_table(sysctl_base_table); 1550 register_sysctl_table(sysctl_base_table);
1753 return 0; 1551 return 0;
1754} 1552}
1755 1553
1756static struct ctl_table *is_branch_in(struct ctl_table *branch,
1757 struct ctl_table *table)
1758{
1759 struct ctl_table *p;
1760 const char *s = branch->procname;
1761
1762 /* branch should have named subdirectory as its first element */
1763 if (!s || !branch->child)
1764 return NULL;
1765
1766 /* ... and nothing else */
1767 if (branch[1].procname)
1768 return NULL;
1769
1770 /* table should contain subdirectory with the same name */
1771 for (p = table; p->procname; p++) {
1772 if (!p->child)
1773 continue;
1774 if (p->procname && strcmp(p->procname, s) == 0)
1775 return p;
1776 }
1777 return NULL;
1778}
1779
1780/* see if attaching q to p would be an improvement */
1781static void try_attach(struct ctl_table_header *p, struct ctl_table_header *q)
1782{
1783 struct ctl_table *to = p->ctl_table, *by = q->ctl_table;
1784 struct ctl_table *next;
1785 int is_better = 0;
1786 int not_in_parent = !p->attached_by;
1787
1788 while ((next = is_branch_in(by, to)) != NULL) {
1789 if (by == q->attached_by)
1790 is_better = 1;
1791 if (to == p->attached_by)
1792 not_in_parent = 1;
1793 by = by->child;
1794 to = next->child;
1795 }
1796
1797 if (is_better && not_in_parent) {
1798 q->attached_by = by;
1799 q->attached_to = to;
1800 q->parent = p;
1801 }
1802}
1803
1804/**
1805 * __register_sysctl_paths - register a sysctl hierarchy
1806 * @root: List of sysctl headers to register on
1807 * @namespaces: Data to compute which lists of sysctl entries are visible
1808 * @path: The path to the directory the sysctl table is in.
1809 * @table: the top-level table structure
1810 *
1811 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1812 * array. A completely 0 filled entry terminates the table.
1813 *
1814 * The members of the &struct ctl_table structure are used as follows:
1815 *
1816 * procname - the name of the sysctl file under /proc/sys. Set to %NULL to not
1817 * enter a sysctl file
1818 *
1819 * data - a pointer to data for use by proc_handler
1820 *
1821 * maxlen - the maximum size in bytes of the data
1822 *
1823 * mode - the file permissions for the /proc/sys file, and for sysctl(2)
1824 *
1825 * child - a pointer to the child sysctl table if this entry is a directory, or
1826 * %NULL.
1827 *
1828 * proc_handler - the text handler routine (described below)
1829 *
1830 * de - for internal use by the sysctl routines
1831 *
1832 * extra1, extra2 - extra pointers usable by the proc handler routines
1833 *
1834 * Leaf nodes in the sysctl tree will be represented by a single file
1835 * under /proc; non-leaf nodes will be represented by directories.
1836 *
1837 * sysctl(2) can automatically manage read and write requests through
1838 * the sysctl table. The data and maxlen fields of the ctl_table
1839 * struct enable minimal validation of the values being written to be
1840 * performed, and the mode field allows minimal authentication.
1841 *
1842 * There must be a proc_handler routine for any terminal nodes
1843 * mirrored under /proc/sys (non-terminals are handled by a built-in
1844 * directory handler). Several default handlers are available to
1845 * cover common cases -
1846 *
1847 * proc_dostring(), proc_dointvec(), proc_dointvec_jiffies(),
1848 * proc_dointvec_userhz_jiffies(), proc_dointvec_minmax(),
1849 * proc_doulongvec_ms_jiffies_minmax(), proc_doulongvec_minmax()
1850 *
1851 * It is the handler's job to read the input buffer from user memory
1852 * and process it. The handler should return 0 on success.
1853 *
1854 * This routine returns %NULL on a failure to register, and a pointer
1855 * to the table header on success.
1856 */
1857struct ctl_table_header *__register_sysctl_paths(
1858 struct ctl_table_root *root,
1859 struct nsproxy *namespaces,
1860 const struct ctl_path *path, struct ctl_table *table)
1861{
1862 struct ctl_table_header *header;
1863 struct ctl_table *new, **prevp;
1864 unsigned int n, npath;
1865 struct ctl_table_set *set;
1866
1867 /* Count the path components */
1868 for (npath = 0; path[npath].procname; ++npath)
1869 ;
1870
1871 /*
1872 * For each path component, allocate a 2-element ctl_table array.
1873 * The first array element will be filled with the sysctl entry
1874 * for this, the second will be the sentinel (procname == 0).
1875 *
1876 * We allocate everything in one go so that we don't have to
1877 * worry about freeing additional memory in unregister_sysctl_table.
1878 */
1879 header = kzalloc(sizeof(struct ctl_table_header) +
1880 (2 * npath * sizeof(struct ctl_table)), GFP_KERNEL);
1881 if (!header)
1882 return NULL;
1883
1884 new = (struct ctl_table *) (header + 1);
1885
1886 /* Now connect the dots */
1887 prevp = &header->ctl_table;
1888 for (n = 0; n < npath; ++n, ++path) {
1889 /* Copy the procname */
1890 new->procname = path->procname;
1891 new->mode = 0555;
1892
1893 *prevp = new;
1894 prevp = &new->child;
1895
1896 new += 2;
1897 }
1898 *prevp = table;
1899 header->ctl_table_arg = table;
1900
1901 INIT_LIST_HEAD(&header->ctl_entry);
1902 header->used = 0;
1903 header->unregistering = NULL;
1904 header->root = root;
1905 sysctl_set_parent(NULL, header->ctl_table);
1906 header->count = 1;
1907#ifdef CONFIG_SYSCTL_SYSCALL_CHECK
1908 if (sysctl_check_table(namespaces, header->ctl_table)) {
1909 kfree(header);
1910 return NULL;
1911 }
1912#endif
1913 spin_lock(&sysctl_lock);
1914 header->set = lookup_header_set(root, namespaces);
1915 header->attached_by = header->ctl_table;
1916 header->attached_to = root_table;
1917 header->parent = &root_table_header;
1918 for (set = header->set; set; set = set->parent) {
1919 struct ctl_table_header *p;
1920 list_for_each_entry(p, &set->list, ctl_entry) {
1921 if (p->unregistering)
1922 continue;
1923 try_attach(p, header);
1924 }
1925 }
1926 header->parent->count++;
1927 list_add_tail(&header->ctl_entry, &header->set->list);
1928 spin_unlock(&sysctl_lock);
1929
1930 return header;
1931}
1932
1933/**
1934 * register_sysctl_table_path - register a sysctl table hierarchy
1935 * @path: The path to the directory the sysctl table is in.
1936 * @table: the top-level table structure
1937 *
1938 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1939 * array. A completely 0 filled entry terminates the table.
1940 *
1941 * See __register_sysctl_paths for more details.
1942 */
1943struct ctl_table_header *register_sysctl_paths(const struct ctl_path *path,
1944 struct ctl_table *table)
1945{
1946 return __register_sysctl_paths(&sysctl_table_root, current->nsproxy,
1947 path, table);
1948}
1949
1950/**
1951 * register_sysctl_table - register a sysctl table hierarchy
1952 * @table: the top-level table structure
1953 *
1954 * Register a sysctl table hierarchy. @table should be a filled in ctl_table
1955 * array. A completely 0 filled entry terminates the table.
1956 *
1957 * See register_sysctl_paths for more details.
1958 */
1959struct ctl_table_header *register_sysctl_table(struct ctl_table *table)
1960{
1961 static const struct ctl_path null_path[] = { {} };
1962
1963 return register_sysctl_paths(null_path, table);
1964}
1965
1966/**
1967 * unregister_sysctl_table - unregister a sysctl table hierarchy
1968 * @header: the header returned from register_sysctl_table
1969 *
1970 * Unregisters the sysctl table and all children. proc entries may not
1971 * actually be removed until they are no longer used by anyone.
1972 */
1973void unregister_sysctl_table(struct ctl_table_header * header)
1974{
1975 might_sleep();
1976
1977 if (header == NULL)
1978 return;
1979
1980 spin_lock(&sysctl_lock);
1981 start_unregistering(header);
1982 if (!--header->parent->count) {
1983 WARN_ON(1);
1984 kfree_rcu(header->parent, rcu);
1985 }
1986 if (!--header->count)
1987 kfree_rcu(header, rcu);
1988 spin_unlock(&sysctl_lock);
1989}
1990
1991int sysctl_is_seen(struct ctl_table_header *p)
1992{
1993 struct ctl_table_set *set = p->set;
1994 int res;
1995 spin_lock(&sysctl_lock);
1996 if (p->unregistering)
1997 res = 0;
1998 else if (!set->is_seen)
1999 res = 1;
2000 else
2001 res = set->is_seen(set);
2002 spin_unlock(&sysctl_lock);
2003 return res;
2004}
2005
2006void setup_sysctl_set(struct ctl_table_set *p,
2007 struct ctl_table_set *parent,
2008 int (*is_seen)(struct ctl_table_set *))
2009{
2010 INIT_LIST_HEAD(&p->list);
2011 p->parent = parent ? parent : &sysctl_table_root.default_set;
2012 p->is_seen = is_seen;
2013}
2014
2015#endif /* CONFIG_SYSCTL */ 1554#endif /* CONFIG_SYSCTL */
2016 1555
2017/* 1556/*
@@ -2977,6 +2516,3 @@ EXPORT_SYMBOL(proc_dointvec_ms_jiffies);
2977EXPORT_SYMBOL(proc_dostring); 2516EXPORT_SYMBOL(proc_dostring);
2978EXPORT_SYMBOL(proc_doulongvec_minmax); 2517EXPORT_SYMBOL(proc_doulongvec_minmax);
2979EXPORT_SYMBOL(proc_doulongvec_ms_jiffies_minmax); 2518EXPORT_SYMBOL(proc_doulongvec_ms_jiffies_minmax);
2980EXPORT_SYMBOL(register_sysctl_table);
2981EXPORT_SYMBOL(register_sysctl_paths);
2982EXPORT_SYMBOL(unregister_sysctl_table);