aboutsummaryrefslogtreecommitdiffstats
path: root/fs/proc/base.c
diff options
context:
space:
mode:
authorCyrill Gorcunov <gorcunov@openvz.org>2012-08-23 06:43:24 -0400
committerAl Viro <viro@zeniv.linux.org.uk>2012-09-26 21:10:01 -0400
commitfaf60af17f8da87e1c87a6be527344791025ce9e (patch)
tree72f58a1061383a38dc6618f911d4becc95a22c03 /fs/proc/base.c
parent864bdb3b6cbd9911222543fef1cfe36f88183f44 (diff)
procfs: Move /proc/pid/fd[info] handling code to fd.[ch]
This patch prepares the ground for further extension of /proc/pid/fd[info] handling code by moving fdinfo handling code into fs/proc/fd.c. I think such move makes both fs/proc/base.c and fs/proc/fd.c easier to read. Signed-off-by: Cyrill Gorcunov <gorcunov@openvz.org> Acked-by: Pavel Emelyanov <xemul@parallels.com> CC: Al Viro <viro@ZenIV.linux.org.uk> CC: Alexey Dobriyan <adobriyan@gmail.com> CC: Andrew Morton <akpm@linux-foundation.org> CC: James Bottomley <jbottomley@parallels.com> CC: "Aneesh Kumar K.V" <aneesh.kumar@linux.vnet.ibm.com> CC: Alexey Dobriyan <adobriyan@gmail.com> CC: Matthew Helsley <matt.helsley@gmail.com> CC: "J. Bruce Fields" <bfields@fieldses.org> CC: "Aneesh Kumar K.V" <aneesh.kumar@linux.vnet.ibm.com> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
Diffstat (limited to 'fs/proc/base.c')
-rw-r--r--fs/proc/base.c388
1 files changed, 2 insertions, 386 deletions
diff --git a/fs/proc/base.c b/fs/proc/base.c
index 1b6c84cbdb73..b55c3bb298e3 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -90,6 +90,7 @@
90#endif 90#endif
91#include <trace/events/oom.h> 91#include <trace/events/oom.h>
92#include "internal.h" 92#include "internal.h"
93#include "fd.h"
93 94
94/* NOTE: 95/* NOTE:
95 * Implementing inode permission operations in /proc is almost 96 * Implementing inode permission operations in /proc is almost
@@ -136,8 +137,6 @@ struct pid_entry {
136 NULL, &proc_single_file_operations, \ 137 NULL, &proc_single_file_operations, \
137 { .proc_show = show } ) 138 { .proc_show = show } )
138 139
139static int proc_fd_permission(struct inode *inode, int mask);
140
141/* 140/*
142 * Count the number of hardlinks for the pid_entry table, excluding the . 141 * Count the number of hardlinks for the pid_entry table, excluding the .
143 * and .. links. 142 * and .. links.
@@ -1492,7 +1491,7 @@ out:
1492 return error; 1491 return error;
1493} 1492}
1494 1493
1495static const struct inode_operations proc_pid_link_inode_operations = { 1494const struct inode_operations proc_pid_link_inode_operations = {
1496 .readlink = proc_pid_readlink, 1495 .readlink = proc_pid_readlink,
1497 .follow_link = proc_pid_follow_link, 1496 .follow_link = proc_pid_follow_link,
1498 .setattr = proc_setattr, 1497 .setattr = proc_setattr,
@@ -1501,21 +1500,6 @@ static const struct inode_operations proc_pid_link_inode_operations = {
1501 1500
1502/* building an inode */ 1501/* building an inode */
1503 1502
1504static int task_dumpable(struct task_struct *task)
1505{
1506 int dumpable = 0;
1507 struct mm_struct *mm;
1508
1509 task_lock(task);
1510 mm = task->mm;
1511 if (mm)
1512 dumpable = get_dumpable(mm);
1513 task_unlock(task);
1514 if(dumpable == 1)
1515 return 1;
1516 return 0;
1517}
1518
1519struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task) 1503struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1520{ 1504{
1521 struct inode * inode; 1505 struct inode * inode;
@@ -1641,15 +1625,6 @@ int pid_revalidate(struct dentry *dentry, unsigned int flags)
1641 return 0; 1625 return 0;
1642} 1626}
1643 1627
1644static int pid_delete_dentry(const struct dentry * dentry)
1645{
1646 /* Is the task we represent dead?
1647 * If so, then don't put the dentry on the lru list,
1648 * kill it immediately.
1649 */
1650 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1651}
1652
1653const struct dentry_operations pid_dentry_operations = 1628const struct dentry_operations pid_dentry_operations =
1654{ 1629{
1655 .d_revalidate = pid_revalidate, 1630 .d_revalidate = pid_revalidate,
@@ -1712,289 +1687,6 @@ end_instantiate:
1712 return filldir(dirent, name, len, filp->f_pos, ino, type); 1687 return filldir(dirent, name, len, filp->f_pos, ino, type);
1713} 1688}
1714 1689
1715static unsigned name_to_int(struct dentry *dentry)
1716{
1717 const char *name = dentry->d_name.name;
1718 int len = dentry->d_name.len;
1719 unsigned n = 0;
1720
1721 if (len > 1 && *name == '0')
1722 goto out;
1723 while (len-- > 0) {
1724 unsigned c = *name++ - '0';
1725 if (c > 9)
1726 goto out;
1727 if (n >= (~0U-9)/10)
1728 goto out;
1729 n *= 10;
1730 n += c;
1731 }
1732 return n;
1733out:
1734 return ~0U;
1735}
1736
1737#define PROC_FDINFO_MAX 64
1738
1739static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1740{
1741 struct task_struct *task = get_proc_task(inode);
1742 struct files_struct *files = NULL;
1743 struct file *file;
1744 int fd = proc_fd(inode);
1745
1746 if (task) {
1747 files = get_files_struct(task);
1748 put_task_struct(task);
1749 }
1750 if (files) {
1751 /*
1752 * We are not taking a ref to the file structure, so we must
1753 * hold ->file_lock.
1754 */
1755 spin_lock(&files->file_lock);
1756 file = fcheck_files(files, fd);
1757 if (file) {
1758 unsigned int f_flags;
1759 struct fdtable *fdt;
1760
1761 fdt = files_fdtable(files);
1762 f_flags = file->f_flags & ~O_CLOEXEC;
1763 if (close_on_exec(fd, fdt))
1764 f_flags |= O_CLOEXEC;
1765
1766 if (path) {
1767 *path = file->f_path;
1768 path_get(&file->f_path);
1769 }
1770 if (info)
1771 snprintf(info, PROC_FDINFO_MAX,
1772 "pos:\t%lli\n"
1773 "flags:\t0%o\n",
1774 (long long) file->f_pos,
1775 f_flags);
1776 spin_unlock(&files->file_lock);
1777 put_files_struct(files);
1778 return 0;
1779 }
1780 spin_unlock(&files->file_lock);
1781 put_files_struct(files);
1782 }
1783 return -ENOENT;
1784}
1785
1786static int proc_fd_link(struct dentry *dentry, struct path *path)
1787{
1788 return proc_fd_info(dentry->d_inode, path, NULL);
1789}
1790
1791static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags)
1792{
1793 struct inode *inode;
1794 struct task_struct *task;
1795 int fd;
1796 struct files_struct *files;
1797 const struct cred *cred;
1798
1799 if (flags & LOOKUP_RCU)
1800 return -ECHILD;
1801
1802 inode = dentry->d_inode;
1803 task = get_proc_task(inode);
1804 fd = proc_fd(inode);
1805
1806 if (task) {
1807 files = get_files_struct(task);
1808 if (files) {
1809 struct file *file;
1810 rcu_read_lock();
1811 file = fcheck_files(files, fd);
1812 if (file) {
1813 unsigned f_mode = file->f_mode;
1814
1815 rcu_read_unlock();
1816 put_files_struct(files);
1817
1818 if (task_dumpable(task)) {
1819 rcu_read_lock();
1820 cred = __task_cred(task);
1821 inode->i_uid = cred->euid;
1822 inode->i_gid = cred->egid;
1823 rcu_read_unlock();
1824 } else {
1825 inode->i_uid = GLOBAL_ROOT_UID;
1826 inode->i_gid = GLOBAL_ROOT_GID;
1827 }
1828
1829 if (S_ISLNK(inode->i_mode)) {
1830 unsigned i_mode = S_IFLNK;
1831 if (f_mode & FMODE_READ)
1832 i_mode |= S_IRUSR | S_IXUSR;
1833 if (f_mode & FMODE_WRITE)
1834 i_mode |= S_IWUSR | S_IXUSR;
1835 inode->i_mode = i_mode;
1836 }
1837
1838 security_task_to_inode(task, inode);
1839 put_task_struct(task);
1840 return 1;
1841 }
1842 rcu_read_unlock();
1843 put_files_struct(files);
1844 }
1845 put_task_struct(task);
1846 }
1847 d_drop(dentry);
1848 return 0;
1849}
1850
1851static const struct dentry_operations tid_fd_dentry_operations =
1852{
1853 .d_revalidate = tid_fd_revalidate,
1854 .d_delete = pid_delete_dentry,
1855};
1856
1857static struct dentry *proc_fd_instantiate(struct inode *dir,
1858 struct dentry *dentry, struct task_struct *task, const void *ptr)
1859{
1860 unsigned fd = (unsigned long)ptr;
1861 struct inode *inode;
1862 struct proc_inode *ei;
1863 struct dentry *error = ERR_PTR(-ENOENT);
1864
1865 inode = proc_pid_make_inode(dir->i_sb, task);
1866 if (!inode)
1867 goto out;
1868 ei = PROC_I(inode);
1869 ei->fd = fd;
1870
1871 inode->i_mode = S_IFLNK;
1872 inode->i_op = &proc_pid_link_inode_operations;
1873 inode->i_size = 64;
1874 ei->op.proc_get_link = proc_fd_link;
1875 d_set_d_op(dentry, &tid_fd_dentry_operations);
1876 d_add(dentry, inode);
1877 /* Close the race of the process dying before we return the dentry */
1878 if (tid_fd_revalidate(dentry, 0))
1879 error = NULL;
1880
1881 out:
1882 return error;
1883}
1884
1885static struct dentry *proc_lookupfd_common(struct inode *dir,
1886 struct dentry *dentry,
1887 instantiate_t instantiate)
1888{
1889 struct task_struct *task = get_proc_task(dir);
1890 unsigned fd = name_to_int(dentry);
1891 struct dentry *result = ERR_PTR(-ENOENT);
1892
1893 if (!task)
1894 goto out_no_task;
1895 if (fd == ~0U)
1896 goto out;
1897
1898 result = instantiate(dir, dentry, task, (void *)(unsigned long)fd);
1899out:
1900 put_task_struct(task);
1901out_no_task:
1902 return result;
1903}
1904
1905static int proc_readfd_common(struct file * filp, void * dirent,
1906 filldir_t filldir, instantiate_t instantiate)
1907{
1908 struct dentry *dentry = filp->f_path.dentry;
1909 struct inode *inode = dentry->d_inode;
1910 struct task_struct *p = get_proc_task(inode);
1911 unsigned int fd, ino;
1912 int retval;
1913 struct files_struct * files;
1914
1915 retval = -ENOENT;
1916 if (!p)
1917 goto out_no_task;
1918 retval = 0;
1919
1920 fd = filp->f_pos;
1921 switch (fd) {
1922 case 0:
1923 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1924 goto out;
1925 filp->f_pos++;
1926 case 1:
1927 ino = parent_ino(dentry);
1928 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1929 goto out;
1930 filp->f_pos++;
1931 default:
1932 files = get_files_struct(p);
1933 if (!files)
1934 goto out;
1935 rcu_read_lock();
1936 for (fd = filp->f_pos-2;
1937 fd < files_fdtable(files)->max_fds;
1938 fd++, filp->f_pos++) {
1939 char name[PROC_NUMBUF];
1940 int len;
1941 int rv;
1942
1943 if (!fcheck_files(files, fd))
1944 continue;
1945 rcu_read_unlock();
1946
1947 len = snprintf(name, sizeof(name), "%d", fd);
1948 rv = proc_fill_cache(filp, dirent, filldir,
1949 name, len, instantiate, p,
1950 (void *)(unsigned long)fd);
1951 if (rv < 0)
1952 goto out_fd_loop;
1953 rcu_read_lock();
1954 }
1955 rcu_read_unlock();
1956out_fd_loop:
1957 put_files_struct(files);
1958 }
1959out:
1960 put_task_struct(p);
1961out_no_task:
1962 return retval;
1963}
1964
1965static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1966 unsigned int flags)
1967{
1968 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1969}
1970
1971static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1972{
1973 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1974}
1975
1976static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1977 size_t len, loff_t *ppos)
1978{
1979 char tmp[PROC_FDINFO_MAX];
1980 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
1981 if (!err)
1982 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1983 return err;
1984}
1985
1986static const struct file_operations proc_fdinfo_file_operations = {
1987 .open = nonseekable_open,
1988 .read = proc_fdinfo_read,
1989 .llseek = no_llseek,
1990};
1991
1992static const struct file_operations proc_fd_operations = {
1993 .read = generic_read_dir,
1994 .readdir = proc_readfd,
1995 .llseek = default_llseek,
1996};
1997
1998#ifdef CONFIG_CHECKPOINT_RESTORE 1690#ifdef CONFIG_CHECKPOINT_RESTORE
1999 1691
2000/* 1692/*
@@ -2337,82 +2029,6 @@ static const struct file_operations proc_map_files_operations = {
2337 2029
2338#endif /* CONFIG_CHECKPOINT_RESTORE */ 2030#endif /* CONFIG_CHECKPOINT_RESTORE */
2339 2031
2340/*
2341 * /proc/pid/fd needs a special permission handler so that a process can still
2342 * access /proc/self/fd after it has executed a setuid().
2343 */
2344static int proc_fd_permission(struct inode *inode, int mask)
2345{
2346 int rv = generic_permission(inode, mask);
2347 if (rv == 0)
2348 return 0;
2349 if (task_pid(current) == proc_pid(inode))
2350 rv = 0;
2351 return rv;
2352}
2353
2354/*
2355 * proc directories can do almost nothing..
2356 */
2357static const struct inode_operations proc_fd_inode_operations = {
2358 .lookup = proc_lookupfd,
2359 .permission = proc_fd_permission,
2360 .setattr = proc_setattr,
2361};
2362
2363static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
2364 struct dentry *dentry, struct task_struct *task, const void *ptr)
2365{
2366 unsigned fd = (unsigned long)ptr;
2367 struct inode *inode;
2368 struct proc_inode *ei;
2369 struct dentry *error = ERR_PTR(-ENOENT);
2370
2371 inode = proc_pid_make_inode(dir->i_sb, task);
2372 if (!inode)
2373 goto out;
2374 ei = PROC_I(inode);
2375 ei->fd = fd;
2376 inode->i_mode = S_IFREG | S_IRUSR;
2377 inode->i_fop = &proc_fdinfo_file_operations;
2378 d_set_d_op(dentry, &tid_fd_dentry_operations);
2379 d_add(dentry, inode);
2380 /* Close the race of the process dying before we return the dentry */
2381 if (tid_fd_revalidate(dentry, 0))
2382 error = NULL;
2383
2384 out:
2385 return error;
2386}
2387
2388static struct dentry *proc_lookupfdinfo(struct inode *dir,
2389 struct dentry *dentry,
2390 unsigned int flags)
2391{
2392 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2393}
2394
2395static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2396{
2397 return proc_readfd_common(filp, dirent, filldir,
2398 proc_fdinfo_instantiate);
2399}
2400
2401static const struct file_operations proc_fdinfo_operations = {
2402 .read = generic_read_dir,
2403 .readdir = proc_readfdinfo,
2404 .llseek = default_llseek,
2405};
2406
2407/*
2408 * proc directories can do almost nothing..
2409 */
2410static const struct inode_operations proc_fdinfo_inode_operations = {
2411 .lookup = proc_lookupfdinfo,
2412 .setattr = proc_setattr,
2413};
2414
2415
2416static struct dentry *proc_pident_instantiate(struct inode *dir, 2032static struct dentry *proc_pident_instantiate(struct inode *dir,
2417 struct dentry *dentry, struct task_struct *task, const void *ptr) 2033 struct dentry *dentry, struct task_struct *task, const void *ptr)
2418{ 2034{