Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/proc/base.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | * |
| 6 | * proc base directory handling functions |
| 7 | * |
| 8 | * 1999, Al Viro. Rewritten. Now it covers the whole per-process part. |
| 9 | * Instead of using magical inumbers to determine the kind of object |
| 10 | * we allocate and fill in-core inodes upon lookup. They don't even |
| 11 | * go into icache. We cache the reference to task_struct upon lookup too. |
| 12 | * Eventually it should become a filesystem in its own. We don't use the |
| 13 | * rest of procfs anymore. |
Mauricio Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 14 | * |
| 15 | * |
| 16 | * Changelog: |
| 17 | * 17-Jan-2005 |
| 18 | * Allan Bezerra |
| 19 | * Bruna Moreira <bruna.moreira@indt.org.br> |
| 20 | * Edjard Mota <edjard.mota@indt.org.br> |
| 21 | * Ilias Biris <ilias.biris@indt.org.br> |
| 22 | * Mauricio Lin <mauricio.lin@indt.org.br> |
| 23 | * |
| 24 | * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT |
| 25 | * |
| 26 | * A new process specific entry (smaps) included in /proc. It shows the |
| 27 | * size of rss for each memory area. The maps entry lacks information |
| 28 | * about physical memory size (rss) for each mapped file, i.e., |
| 29 | * rss information for executables and library files. |
| 30 | * This additional information is useful for any tools that need to know |
| 31 | * about physical memory consumption for a process specific library. |
| 32 | * |
| 33 | * Changelog: |
| 34 | * 21-Feb-2005 |
| 35 | * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT |
| 36 | * Pud inclusion in the page table walking. |
| 37 | * |
| 38 | * ChangeLog: |
| 39 | * 10-Mar-2005 |
| 40 | * 10LE Instituto Nokia de Tecnologia - INdT: |
| 41 | * A better way to walks through the page table as suggested by Hugh Dickins. |
| 42 | * |
| 43 | * Simo Piiroinen <simo.piiroinen@nokia.com>: |
| 44 | * Smaps information related to shared, private, clean and dirty pages. |
| 45 | * |
| 46 | * Paul Mundt <paul.mundt@nokia.com>: |
| 47 | * Overall revision about smaps. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | */ |
| 49 | |
| 50 | #include <asm/uaccess.h> |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include <linux/errno.h> |
| 53 | #include <linux/time.h> |
| 54 | #include <linux/proc_fs.h> |
| 55 | #include <linux/stat.h> |
Andrea Righi | 5995477 | 2008-07-27 17:29:15 +0200 | [diff] [blame] | 56 | #include <linux/task_io_accounting_ops.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | #include <linux/init.h> |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 58 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #include <linux/file.h> |
Al Viro | 9f3acc3 | 2008-04-24 07:44:08 -0400 | [diff] [blame] | 60 | #include <linux/fdtable.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | #include <linux/string.h> |
| 62 | #include <linux/seq_file.h> |
| 63 | #include <linux/namei.h> |
Kirill Korotaev | 6b3286e | 2006-12-08 02:37:56 -0800 | [diff] [blame] | 64 | #include <linux/mnt_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | #include <linux/mm.h> |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 66 | #include <linux/swap.h> |
Dipankar Sarma | b835996 | 2005-09-09 13:04:14 -0700 | [diff] [blame] | 67 | #include <linux/rcupdate.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | #include <linux/kallsyms.h> |
Ken Chen | 2ec220e | 2008-11-10 11:26:08 +0300 | [diff] [blame] | 69 | #include <linux/stacktrace.h> |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 70 | #include <linux/resource.h> |
Kees Cook | 5096add | 2007-05-08 00:26:04 -0700 | [diff] [blame] | 71 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | #include <linux/mount.h> |
| 73 | #include <linux/security.h> |
| 74 | #include <linux/ptrace.h> |
Roland McGrath | 0d094ef | 2008-07-25 19:45:49 -0700 | [diff] [blame] | 75 | #include <linux/tracehook.h> |
Andrew Morton | 87ebdc0 | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 76 | #include <linux/printk.h> |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 77 | #include <linux/cgroup.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | #include <linux/cpuset.h> |
| 79 | #include <linux/audit.h> |
Al Viro | 5addc5d | 2005-11-07 17:15:49 -0500 | [diff] [blame] | 80 | #include <linux/poll.h> |
Serge E. Hallyn | 1651e14 | 2006-10-02 02:18:08 -0700 | [diff] [blame] | 81 | #include <linux/nsproxy.h> |
Alexey Dobriyan | 8ac773b | 2006-10-19 23:28:32 -0700 | [diff] [blame] | 82 | #include <linux/oom.h> |
Kawai, Hidehiro | 3cb4a0b | 2007-07-19 01:48:28 -0700 | [diff] [blame] | 83 | #include <linux/elf.h> |
Pavel Emelyanov | 60347f6 | 2007-10-18 23:40:03 -0700 | [diff] [blame] | 84 | #include <linux/pid_namespace.h> |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 85 | #include <linux/user_namespace.h> |
Al Viro | 5ad4e53 | 2009-03-29 19:50:06 -0400 | [diff] [blame] | 86 | #include <linux/fs_struct.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 87 | #include <linux/slab.h> |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 88 | #include <linux/flex_array.h> |
Pavel Emelyanov | 48f6a7a | 2013-03-11 13:12:45 +0400 | [diff] [blame] | 89 | #include <linux/posix-timers.h> |
Chris Metcalf | f133ecc | 2011-05-26 12:40:09 -0400 | [diff] [blame] | 90 | #ifdef CONFIG_HARDWALL |
| 91 | #include <asm/hardwall.h> |
| 92 | #endif |
KAMEZAWA Hiroyuki | 43d2b11 | 2012-01-10 15:08:09 -0800 | [diff] [blame] | 93 | #include <trace/events/oom.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | #include "internal.h" |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 95 | #include "fd.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Eric W. Biederman | 0f2fe20 | 2006-06-26 00:25:46 -0700 | [diff] [blame] | 97 | /* NOTE: |
| 98 | * Implementing inode permission operations in /proc is almost |
| 99 | * certainly an error. Permission checks need to happen during |
| 100 | * each system call not at open time. The reason is that most of |
| 101 | * what we wish to check for permissions in /proc varies at runtime. |
| 102 | * |
| 103 | * The classic example of a problem is opening file descriptors |
| 104 | * in /proc for a task before it execs a suid executable. |
| 105 | */ |
| 106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | struct pid_entry { |
Alexey Dobriyan | cedbcca | 2014-08-08 14:21:33 -0700 | [diff] [blame] | 108 | const char *name; |
Eric Dumazet | c5141e6 | 2007-05-08 00:26:15 -0700 | [diff] [blame] | 109 | int len; |
Al Viro | d161a13 | 2011-07-24 03:36:29 -0400 | [diff] [blame] | 110 | umode_t mode; |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 111 | const struct inode_operations *iop; |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 112 | const struct file_operations *fop; |
Eric W. Biederman | 20cdc89 | 2006-10-02 02:17:07 -0700 | [diff] [blame] | 113 | union proc_op op; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 116 | #define NOD(NAME, MODE, IOP, FOP, OP) { \ |
Eric W. Biederman | 20cdc89 | 2006-10-02 02:17:07 -0700 | [diff] [blame] | 117 | .name = (NAME), \ |
Eric Dumazet | c5141e6 | 2007-05-08 00:26:15 -0700 | [diff] [blame] | 118 | .len = sizeof(NAME) - 1, \ |
Eric W. Biederman | 20cdc89 | 2006-10-02 02:17:07 -0700 | [diff] [blame] | 119 | .mode = MODE, \ |
| 120 | .iop = IOP, \ |
| 121 | .fop = FOP, \ |
| 122 | .op = OP, \ |
| 123 | } |
| 124 | |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 125 | #define DIR(NAME, MODE, iops, fops) \ |
| 126 | NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} ) |
| 127 | #define LNK(NAME, get_link) \ |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 128 | NOD(NAME, (S_IFLNK|S_IRWXUGO), \ |
Eric W. Biederman | 20cdc89 | 2006-10-02 02:17:07 -0700 | [diff] [blame] | 129 | &proc_pid_link_inode_operations, NULL, \ |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 130 | { .proc_get_link = get_link } ) |
| 131 | #define REG(NAME, MODE, fops) \ |
| 132 | NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {}) |
| 133 | #define INF(NAME, MODE, read) \ |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 134 | NOD(NAME, (S_IFREG|(MODE)), \ |
Eric W. Biederman | 20cdc89 | 2006-10-02 02:17:07 -0700 | [diff] [blame] | 135 | NULL, &proc_info_file_operations, \ |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 136 | { .proc_read = read } ) |
| 137 | #define ONE(NAME, MODE, show) \ |
Eric W. Biederman | be61408 | 2008-02-08 04:18:30 -0800 | [diff] [blame] | 138 | NOD(NAME, (S_IFREG|(MODE)), \ |
| 139 | NULL, &proc_single_file_operations, \ |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 140 | { .proc_show = show } ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Vegard Nossum | aed5417 | 2008-06-05 22:46:53 -0700 | [diff] [blame] | 142 | /* |
| 143 | * Count the number of hardlinks for the pid_entry table, excluding the . |
| 144 | * and .. links. |
| 145 | */ |
| 146 | static unsigned int pid_entry_count_dirs(const struct pid_entry *entries, |
| 147 | unsigned int n) |
| 148 | { |
| 149 | unsigned int i; |
| 150 | unsigned int count; |
| 151 | |
| 152 | count = 0; |
| 153 | for (i = 0; i < n; ++i) { |
| 154 | if (S_ISDIR(entries[i].mode)) |
| 155 | ++count; |
| 156 | } |
| 157 | |
| 158 | return count; |
| 159 | } |
| 160 | |
Miklos Szeredi | f7ad3c6 | 2010-08-10 11:41:36 +0200 | [diff] [blame] | 161 | static int get_task_root(struct task_struct *task, struct path *root) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | { |
Hugh Dickins | 7c2c7d9 | 2009-03-28 23:21:27 +0000 | [diff] [blame] | 163 | int result = -ENOENT; |
| 164 | |
Miklos Szeredi | 0494f6e | 2005-09-06 15:18:22 -0700 | [diff] [blame] | 165 | task_lock(task); |
Miklos Szeredi | f7ad3c6 | 2010-08-10 11:41:36 +0200 | [diff] [blame] | 166 | if (task->fs) { |
| 167 | get_fs_root(task->fs, root); |
Hugh Dickins | 7c2c7d9 | 2009-03-28 23:21:27 +0000 | [diff] [blame] | 168 | result = 0; |
| 169 | } |
Miklos Szeredi | 0494f6e | 2005-09-06 15:18:22 -0700 | [diff] [blame] | 170 | task_unlock(task); |
Hugh Dickins | 7c2c7d9 | 2009-03-28 23:21:27 +0000 | [diff] [blame] | 171 | return result; |
Miklos Szeredi | 0494f6e | 2005-09-06 15:18:22 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Cyrill Gorcunov | 7773fbc | 2012-01-10 15:11:20 -0800 | [diff] [blame] | 174 | static int proc_cwd_link(struct dentry *dentry, struct path *path) |
Miklos Szeredi | 0494f6e | 2005-09-06 15:18:22 -0700 | [diff] [blame] | 175 | { |
Cyrill Gorcunov | 7773fbc | 2012-01-10 15:11:20 -0800 | [diff] [blame] | 176 | struct task_struct *task = get_proc_task(dentry->d_inode); |
Miklos Szeredi | 0494f6e | 2005-09-06 15:18:22 -0700 | [diff] [blame] | 177 | int result = -ENOENT; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 178 | |
| 179 | if (task) { |
Miklos Szeredi | f7ad3c6 | 2010-08-10 11:41:36 +0200 | [diff] [blame] | 180 | task_lock(task); |
| 181 | if (task->fs) { |
| 182 | get_fs_pwd(task->fs, path); |
| 183 | result = 0; |
| 184 | } |
| 185 | task_unlock(task); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 186 | put_task_struct(task); |
| 187 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | return result; |
| 189 | } |
| 190 | |
Cyrill Gorcunov | 7773fbc | 2012-01-10 15:11:20 -0800 | [diff] [blame] | 191 | static int proc_root_link(struct dentry *dentry, struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | { |
Cyrill Gorcunov | 7773fbc | 2012-01-10 15:11:20 -0800 | [diff] [blame] | 193 | struct task_struct *task = get_proc_task(dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | int result = -ENOENT; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 195 | |
| 196 | if (task) { |
Miklos Szeredi | f7ad3c6 | 2010-08-10 11:41:36 +0200 | [diff] [blame] | 197 | result = get_task_root(task, path); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 198 | put_task_struct(task); |
| 199 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | return result; |
| 201 | } |
| 202 | |
Alexey Dobriyan | 2ca66ff | 2014-08-08 14:21:41 -0700 | [diff] [blame] | 203 | static int proc_pid_cmdline(struct seq_file *m, struct pid_namespace *ns, |
| 204 | struct pid *pid, struct task_struct *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | { |
Alexey Dobriyan | 2ca66ff | 2014-08-08 14:21:41 -0700 | [diff] [blame] | 206 | /* |
| 207 | * Rely on struct seq_operations::show() being called once |
| 208 | * per internal buffer allocation. See single_open(), traverse(). |
| 209 | */ |
| 210 | BUG_ON(m->size < PAGE_SIZE); |
| 211 | m->count += get_cmdline(task, m->buf, PAGE_SIZE); |
| 212 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Alexey Dobriyan | f9ea536 | 2014-08-08 14:21:35 -0700 | [diff] [blame] | 215 | static int proc_pid_auxv(struct seq_file *m, struct pid_namespace *ns, |
| 216 | struct pid *pid, struct task_struct *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | { |
Cong Wang | e7dcd99 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 218 | struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ); |
Al Viro | 2fadaef | 2011-02-15 22:52:11 -0500 | [diff] [blame] | 219 | if (mm && !IS_ERR(mm)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | unsigned int nwords = 0; |
Hannes Eder | dfe6b7d | 2008-12-30 18:49:13 +0300 | [diff] [blame] | 221 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | nwords += 2; |
Hannes Eder | dfe6b7d | 2008-12-30 18:49:13 +0300 | [diff] [blame] | 223 | } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */ |
Alexey Dobriyan | f9ea536 | 2014-08-08 14:21:35 -0700 | [diff] [blame] | 224 | seq_write(m, mm->saved_auxv, nwords * sizeof(mm->saved_auxv[0])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | mmput(mm); |
Alexey Dobriyan | f9ea536 | 2014-08-08 14:21:35 -0700 | [diff] [blame] | 226 | return 0; |
| 227 | } else |
| 228 | return PTR_ERR(mm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | |
| 232 | #ifdef CONFIG_KALLSYMS |
| 233 | /* |
| 234 | * Provides a wchan file via kallsyms in a proper one-value-per-file format. |
| 235 | * Returns the resolved symbol. If that fails, simply return the address. |
| 236 | */ |
Alexey Dobriyan | edfcd60 | 2014-08-08 14:21:44 -0700 | [diff] [blame] | 237 | static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns, |
| 238 | struct pid *pid, struct task_struct *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | { |
Alexey Dobriyan | ffb4512 | 2007-05-08 00:28:41 -0700 | [diff] [blame] | 240 | unsigned long wchan; |
Tejun Heo | 9281ace | 2007-07-17 04:03:51 -0700 | [diff] [blame] | 241 | char symname[KSYM_NAME_LEN]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
| 243 | wchan = get_wchan(task); |
| 244 | |
Alexey Dobriyan | 9d65cb4 | 2007-05-08 00:28:43 -0700 | [diff] [blame] | 245 | if (lookup_symbol_name(wchan, symname) < 0) |
Jake Edge | f83ce3e | 2009-05-04 12:51:14 -0600 | [diff] [blame] | 246 | if (!ptrace_may_access(task, PTRACE_MODE_READ)) |
| 247 | return 0; |
| 248 | else |
Alexey Dobriyan | edfcd60 | 2014-08-08 14:21:44 -0700 | [diff] [blame] | 249 | return seq_printf(m, "%lu", wchan); |
Alexey Dobriyan | 9d65cb4 | 2007-05-08 00:28:43 -0700 | [diff] [blame] | 250 | else |
Alexey Dobriyan | edfcd60 | 2014-08-08 14:21:44 -0700 | [diff] [blame] | 251 | return seq_printf(m, "%s", symname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | } |
| 253 | #endif /* CONFIG_KALLSYMS */ |
| 254 | |
Al Viro | a9712bc | 2011-03-23 15:52:50 -0400 | [diff] [blame] | 255 | static int lock_trace(struct task_struct *task) |
| 256 | { |
| 257 | int err = mutex_lock_killable(&task->signal->cred_guard_mutex); |
| 258 | if (err) |
| 259 | return err; |
| 260 | if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) { |
| 261 | mutex_unlock(&task->signal->cred_guard_mutex); |
| 262 | return -EPERM; |
| 263 | } |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | static void unlock_trace(struct task_struct *task) |
| 268 | { |
| 269 | mutex_unlock(&task->signal->cred_guard_mutex); |
| 270 | } |
| 271 | |
Ken Chen | 2ec220e | 2008-11-10 11:26:08 +0300 | [diff] [blame] | 272 | #ifdef CONFIG_STACKTRACE |
| 273 | |
| 274 | #define MAX_STACK_TRACE_DEPTH 64 |
| 275 | |
| 276 | static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns, |
| 277 | struct pid *pid, struct task_struct *task) |
| 278 | { |
| 279 | struct stack_trace trace; |
| 280 | unsigned long *entries; |
Al Viro | a9712bc | 2011-03-23 15:52:50 -0400 | [diff] [blame] | 281 | int err; |
Ken Chen | 2ec220e | 2008-11-10 11:26:08 +0300 | [diff] [blame] | 282 | int i; |
| 283 | |
| 284 | entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL); |
| 285 | if (!entries) |
| 286 | return -ENOMEM; |
| 287 | |
| 288 | trace.nr_entries = 0; |
| 289 | trace.max_entries = MAX_STACK_TRACE_DEPTH; |
| 290 | trace.entries = entries; |
| 291 | trace.skip = 0; |
Ken Chen | 2ec220e | 2008-11-10 11:26:08 +0300 | [diff] [blame] | 292 | |
Al Viro | a9712bc | 2011-03-23 15:52:50 -0400 | [diff] [blame] | 293 | err = lock_trace(task); |
| 294 | if (!err) { |
| 295 | save_stack_trace_tsk(task, &trace); |
| 296 | |
| 297 | for (i = 0; i < trace.nr_entries; i++) { |
Linus Torvalds | b81a618 | 2011-03-23 20:51:42 -0700 | [diff] [blame] | 298 | seq_printf(m, "[<%pK>] %pS\n", |
Al Viro | a9712bc | 2011-03-23 15:52:50 -0400 | [diff] [blame] | 299 | (void *)entries[i], (void *)entries[i]); |
| 300 | } |
| 301 | unlock_trace(task); |
Ken Chen | 2ec220e | 2008-11-10 11:26:08 +0300 | [diff] [blame] | 302 | } |
| 303 | kfree(entries); |
| 304 | |
Al Viro | a9712bc | 2011-03-23 15:52:50 -0400 | [diff] [blame] | 305 | return err; |
Ken Chen | 2ec220e | 2008-11-10 11:26:08 +0300 | [diff] [blame] | 306 | } |
| 307 | #endif |
| 308 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | #ifdef CONFIG_SCHEDSTATS |
| 310 | /* |
| 311 | * Provides /proc/PID/schedstat |
| 312 | */ |
Alexey Dobriyan | f6e826c | 2014-08-08 14:21:46 -0700 | [diff] [blame] | 313 | static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns, |
| 314 | struct pid *pid, struct task_struct *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | { |
Alexey Dobriyan | f6e826c | 2014-08-08 14:21:46 -0700 | [diff] [blame] | 316 | return seq_printf(m, "%llu %llu %lu\n", |
Ingo Molnar | 826e08b | 2008-12-22 07:37:41 +0100 | [diff] [blame] | 317 | (unsigned long long)task->se.sum_exec_runtime, |
| 318 | (unsigned long long)task->sched_info.run_delay, |
Ingo Molnar | 2d72376 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 319 | task->sched_info.pcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | } |
| 321 | #endif |
| 322 | |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 323 | #ifdef CONFIG_LATENCYTOP |
| 324 | static int lstats_show_proc(struct seq_file *m, void *v) |
| 325 | { |
| 326 | int i; |
Hiroshi Shimamoto | 13d77c3 | 2008-02-20 16:53:29 -0800 | [diff] [blame] | 327 | struct inode *inode = m->private; |
| 328 | struct task_struct *task = get_proc_task(inode); |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 329 | |
Hiroshi Shimamoto | 13d77c3 | 2008-02-20 16:53:29 -0800 | [diff] [blame] | 330 | if (!task) |
| 331 | return -ESRCH; |
| 332 | seq_puts(m, "Latency Top version : v0.1\n"); |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 333 | for (i = 0; i < 32; i++) { |
Joe Perches | 34e49d4 | 2011-01-12 17:00:30 -0800 | [diff] [blame] | 334 | struct latency_record *lr = &task->latency_record[i]; |
| 335 | if (lr->backtrace[0]) { |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 336 | int q; |
Joe Perches | 34e49d4 | 2011-01-12 17:00:30 -0800 | [diff] [blame] | 337 | seq_printf(m, "%i %li %li", |
| 338 | lr->count, lr->time, lr->max); |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 339 | for (q = 0; q < LT_BACKTRACEDEPTH; q++) { |
Joe Perches | 34e49d4 | 2011-01-12 17:00:30 -0800 | [diff] [blame] | 340 | unsigned long bt = lr->backtrace[q]; |
| 341 | if (!bt) |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 342 | break; |
Joe Perches | 34e49d4 | 2011-01-12 17:00:30 -0800 | [diff] [blame] | 343 | if (bt == ULONG_MAX) |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 344 | break; |
Joe Perches | 34e49d4 | 2011-01-12 17:00:30 -0800 | [diff] [blame] | 345 | seq_printf(m, " %ps", (void *)bt); |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 346 | } |
Alexey Dobriyan | 9d6de12 | 2011-01-12 17:00:32 -0800 | [diff] [blame] | 347 | seq_putc(m, '\n'); |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | } |
Hiroshi Shimamoto | 13d77c3 | 2008-02-20 16:53:29 -0800 | [diff] [blame] | 351 | put_task_struct(task); |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | static int lstats_open(struct inode *inode, struct file *file) |
| 356 | { |
Hiroshi Shimamoto | 13d77c3 | 2008-02-20 16:53:29 -0800 | [diff] [blame] | 357 | return single_open(file, lstats_show_proc, inode); |
Hiroshi Shimamoto | d6643d1 | 2008-02-14 10:27:00 -0800 | [diff] [blame] | 358 | } |
| 359 | |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 360 | static ssize_t lstats_write(struct file *file, const char __user *buf, |
| 361 | size_t count, loff_t *offs) |
| 362 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 363 | struct task_struct *task = get_proc_task(file_inode(file)); |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 364 | |
Hiroshi Shimamoto | 13d77c3 | 2008-02-20 16:53:29 -0800 | [diff] [blame] | 365 | if (!task) |
| 366 | return -ESRCH; |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 367 | clear_all_latency_tracing(task); |
Hiroshi Shimamoto | 13d77c3 | 2008-02-20 16:53:29 -0800 | [diff] [blame] | 368 | put_task_struct(task); |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 369 | |
| 370 | return count; |
| 371 | } |
| 372 | |
| 373 | static const struct file_operations proc_lstats_operations = { |
| 374 | .open = lstats_open, |
| 375 | .read = seq_read, |
| 376 | .write = lstats_write, |
| 377 | .llseek = seq_lseek, |
Hiroshi Shimamoto | 13d77c3 | 2008-02-20 16:53:29 -0800 | [diff] [blame] | 378 | .release = single_release, |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 379 | }; |
| 380 | |
| 381 | #endif |
| 382 | |
Al Viro | 8d8b97b | 2013-04-19 23:11:24 -0400 | [diff] [blame] | 383 | #ifdef CONFIG_CGROUPS |
| 384 | static int cgroup_open(struct inode *inode, struct file *file) |
| 385 | { |
| 386 | struct pid *pid = PROC_I(inode)->pid; |
| 387 | return single_open(file, proc_cgroup_show, pid); |
| 388 | } |
| 389 | |
| 390 | static const struct file_operations proc_cgroup_operations = { |
| 391 | .open = cgroup_open, |
| 392 | .read = seq_read, |
| 393 | .llseek = seq_lseek, |
| 394 | .release = single_release, |
| 395 | }; |
| 396 | #endif |
| 397 | |
| 398 | #ifdef CONFIG_PROC_PID_CPUSET |
| 399 | |
| 400 | static int cpuset_open(struct inode *inode, struct file *file) |
| 401 | { |
| 402 | struct pid *pid = PROC_I(inode)->pid; |
| 403 | return single_open(file, proc_cpuset_show, pid); |
| 404 | } |
| 405 | |
| 406 | static const struct file_operations proc_cpuset_operations = { |
| 407 | .open = cpuset_open, |
| 408 | .read = seq_read, |
| 409 | .llseek = seq_lseek, |
| 410 | .release = single_release, |
| 411 | }; |
| 412 | #endif |
| 413 | |
Alexey Dobriyan | 6ba51e3 | 2014-08-08 14:21:48 -0700 | [diff] [blame] | 414 | static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns, |
| 415 | struct pid *pid, struct task_struct *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | { |
David Rientjes | a7f638f | 2012-05-29 15:06:47 -0700 | [diff] [blame] | 417 | unsigned long totalpages = totalram_pages + total_swap_pages; |
Oleg Nesterov | b95c35e | 2010-04-01 15:13:57 +0200 | [diff] [blame] | 418 | unsigned long points = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
Alexey Dobriyan | 19c5d45 | 2007-05-08 00:26:46 -0700 | [diff] [blame] | 420 | read_lock(&tasklist_lock); |
Oleg Nesterov | b95c35e | 2010-04-01 15:13:57 +0200 | [diff] [blame] | 421 | if (pid_alive(task)) |
David Rientjes | a7f638f | 2012-05-29 15:06:47 -0700 | [diff] [blame] | 422 | points = oom_badness(task, NULL, NULL, totalpages) * |
| 423 | 1000 / totalpages; |
Alexey Dobriyan | 19c5d45 | 2007-05-08 00:26:46 -0700 | [diff] [blame] | 424 | read_unlock(&tasklist_lock); |
Alexey Dobriyan | 6ba51e3 | 2014-08-08 14:21:48 -0700 | [diff] [blame] | 425 | return seq_printf(m, "%lu\n", points); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 428 | struct limit_names { |
Alexey Dobriyan | cedbcca | 2014-08-08 14:21:33 -0700 | [diff] [blame] | 429 | const char *name; |
| 430 | const char *unit; |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 431 | }; |
| 432 | |
| 433 | static const struct limit_names lnames[RLIM_NLIMITS] = { |
Kees Cook | cff4edb | 2009-09-22 16:45:32 -0700 | [diff] [blame] | 434 | [RLIMIT_CPU] = {"Max cpu time", "seconds"}, |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 435 | [RLIMIT_FSIZE] = {"Max file size", "bytes"}, |
| 436 | [RLIMIT_DATA] = {"Max data size", "bytes"}, |
| 437 | [RLIMIT_STACK] = {"Max stack size", "bytes"}, |
| 438 | [RLIMIT_CORE] = {"Max core file size", "bytes"}, |
| 439 | [RLIMIT_RSS] = {"Max resident set", "bytes"}, |
| 440 | [RLIMIT_NPROC] = {"Max processes", "processes"}, |
| 441 | [RLIMIT_NOFILE] = {"Max open files", "files"}, |
| 442 | [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"}, |
| 443 | [RLIMIT_AS] = {"Max address space", "bytes"}, |
| 444 | [RLIMIT_LOCKS] = {"Max file locks", "locks"}, |
| 445 | [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"}, |
| 446 | [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"}, |
| 447 | [RLIMIT_NICE] = {"Max nice priority", NULL}, |
| 448 | [RLIMIT_RTPRIO] = {"Max realtime priority", NULL}, |
Eugene Teo | 8808117 | 2008-02-23 15:23:52 -0800 | [diff] [blame] | 449 | [RLIMIT_RTTIME] = {"Max realtime timeout", "us"}, |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 450 | }; |
| 451 | |
| 452 | /* Display limits for a process */ |
Alexey Dobriyan | 1c963eb | 2014-08-08 14:21:37 -0700 | [diff] [blame] | 453 | static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns, |
| 454 | struct pid *pid, struct task_struct *task) |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 455 | { |
| 456 | unsigned int i; |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 457 | unsigned long flags; |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 458 | |
| 459 | struct rlimit rlim[RLIM_NLIMITS]; |
| 460 | |
Lai Jiangshan | a6bebbc | 2008-10-05 00:51:15 +0400 | [diff] [blame] | 461 | if (!lock_task_sighand(task, &flags)) |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 462 | return 0; |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 463 | memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS); |
| 464 | unlock_task_sighand(task, &flags); |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 465 | |
| 466 | /* |
| 467 | * print the file header |
| 468 | */ |
Alexey Dobriyan | 1c963eb | 2014-08-08 14:21:37 -0700 | [diff] [blame] | 469 | seq_printf(m, "%-25s %-20s %-20s %-10s\n", |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 470 | "Limit", "Soft Limit", "Hard Limit", "Units"); |
| 471 | |
| 472 | for (i = 0; i < RLIM_NLIMITS; i++) { |
| 473 | if (rlim[i].rlim_cur == RLIM_INFINITY) |
Alexey Dobriyan | 1c963eb | 2014-08-08 14:21:37 -0700 | [diff] [blame] | 474 | seq_printf(m, "%-25s %-20s ", |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 475 | lnames[i].name, "unlimited"); |
| 476 | else |
Alexey Dobriyan | 1c963eb | 2014-08-08 14:21:37 -0700 | [diff] [blame] | 477 | seq_printf(m, "%-25s %-20lu ", |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 478 | lnames[i].name, rlim[i].rlim_cur); |
| 479 | |
| 480 | if (rlim[i].rlim_max == RLIM_INFINITY) |
Alexey Dobriyan | 1c963eb | 2014-08-08 14:21:37 -0700 | [diff] [blame] | 481 | seq_printf(m, "%-20s ", "unlimited"); |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 482 | else |
Alexey Dobriyan | 1c963eb | 2014-08-08 14:21:37 -0700 | [diff] [blame] | 483 | seq_printf(m, "%-20lu ", rlim[i].rlim_max); |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 484 | |
| 485 | if (lnames[i].unit) |
Alexey Dobriyan | 1c963eb | 2014-08-08 14:21:37 -0700 | [diff] [blame] | 486 | seq_printf(m, "%-10s\n", lnames[i].unit); |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 487 | else |
Alexey Dobriyan | 1c963eb | 2014-08-08 14:21:37 -0700 | [diff] [blame] | 488 | seq_putc(m, '\n'); |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Alexey Dobriyan | 1c963eb | 2014-08-08 14:21:37 -0700 | [diff] [blame] | 491 | return 0; |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Roland McGrath | ebcb673 | 2008-07-25 19:46:00 -0700 | [diff] [blame] | 494 | #ifdef CONFIG_HAVE_ARCH_TRACEHOOK |
Alexey Dobriyan | 09d93bd | 2014-08-08 14:21:39 -0700 | [diff] [blame] | 495 | static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns, |
| 496 | struct pid *pid, struct task_struct *task) |
Roland McGrath | ebcb673 | 2008-07-25 19:46:00 -0700 | [diff] [blame] | 497 | { |
| 498 | long nr; |
| 499 | unsigned long args[6], sp, pc; |
Al Viro | a9712bc | 2011-03-23 15:52:50 -0400 | [diff] [blame] | 500 | int res = lock_trace(task); |
| 501 | if (res) |
| 502 | return res; |
Roland McGrath | ebcb673 | 2008-07-25 19:46:00 -0700 | [diff] [blame] | 503 | |
| 504 | if (task_current_syscall(task, &nr, args, 6, &sp, &pc)) |
Alexey Dobriyan | 09d93bd | 2014-08-08 14:21:39 -0700 | [diff] [blame] | 505 | seq_puts(m, "running\n"); |
Al Viro | a9712bc | 2011-03-23 15:52:50 -0400 | [diff] [blame] | 506 | else if (nr < 0) |
Alexey Dobriyan | 09d93bd | 2014-08-08 14:21:39 -0700 | [diff] [blame] | 507 | seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc); |
Al Viro | a9712bc | 2011-03-23 15:52:50 -0400 | [diff] [blame] | 508 | else |
Alexey Dobriyan | 09d93bd | 2014-08-08 14:21:39 -0700 | [diff] [blame] | 509 | seq_printf(m, |
Roland McGrath | ebcb673 | 2008-07-25 19:46:00 -0700 | [diff] [blame] | 510 | "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n", |
| 511 | nr, |
| 512 | args[0], args[1], args[2], args[3], args[4], args[5], |
| 513 | sp, pc); |
Al Viro | a9712bc | 2011-03-23 15:52:50 -0400 | [diff] [blame] | 514 | unlock_trace(task); |
| 515 | return res; |
Roland McGrath | ebcb673 | 2008-07-25 19:46:00 -0700 | [diff] [blame] | 516 | } |
| 517 | #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */ |
| 518 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | /************************************************************************/ |
| 520 | /* Here the fs part begins */ |
| 521 | /************************************************************************/ |
| 522 | |
| 523 | /* permission checks */ |
Eric W. Biederman | 778c114 | 2006-06-26 00:25:58 -0700 | [diff] [blame] | 524 | static int proc_fd_access_allowed(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | { |
Eric W. Biederman | 778c114 | 2006-06-26 00:25:58 -0700 | [diff] [blame] | 526 | struct task_struct *task; |
| 527 | int allowed = 0; |
Eric W. Biederman | df26c40 | 2006-06-26 00:25:59 -0700 | [diff] [blame] | 528 | /* Allow access to a task's file descriptors if it is us or we |
| 529 | * may use ptrace attach to the process and find out that |
| 530 | * information. |
Eric W. Biederman | 778c114 | 2006-06-26 00:25:58 -0700 | [diff] [blame] | 531 | */ |
| 532 | task = get_proc_task(inode); |
Eric W. Biederman | df26c40 | 2006-06-26 00:25:59 -0700 | [diff] [blame] | 533 | if (task) { |
Stephen Smalley | 006ebb4 | 2008-05-19 08:32:49 -0400 | [diff] [blame] | 534 | allowed = ptrace_may_access(task, PTRACE_MODE_READ); |
Eric W. Biederman | 778c114 | 2006-06-26 00:25:58 -0700 | [diff] [blame] | 535 | put_task_struct(task); |
Eric W. Biederman | df26c40 | 2006-06-26 00:25:59 -0700 | [diff] [blame] | 536 | } |
Eric W. Biederman | 778c114 | 2006-06-26 00:25:58 -0700 | [diff] [blame] | 537 | return allowed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 540 | int proc_setattr(struct dentry *dentry, struct iattr *attr) |
Linus Torvalds | 6d76fa5 | 2006-07-15 12:26:45 -0700 | [diff] [blame] | 541 | { |
| 542 | int error; |
| 543 | struct inode *inode = dentry->d_inode; |
| 544 | |
| 545 | if (attr->ia_valid & ATTR_MODE) |
| 546 | return -EPERM; |
| 547 | |
| 548 | error = inode_change_ok(inode, attr); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 549 | if (error) |
| 550 | return error; |
| 551 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 552 | setattr_copy(inode, attr); |
| 553 | mark_inode_dirty(inode); |
| 554 | return 0; |
Linus Torvalds | 6d76fa5 | 2006-07-15 12:26:45 -0700 | [diff] [blame] | 555 | } |
| 556 | |
Vasiliy Kulikov | 0499680 | 2012-01-10 15:11:31 -0800 | [diff] [blame] | 557 | /* |
| 558 | * May current process learn task's sched/cmdline info (for hide_pid_min=1) |
| 559 | * or euid/egid (for hide_pid_min=2)? |
| 560 | */ |
| 561 | static bool has_pid_permissions(struct pid_namespace *pid, |
| 562 | struct task_struct *task, |
| 563 | int hide_pid_min) |
| 564 | { |
| 565 | if (pid->hide_pid < hide_pid_min) |
| 566 | return true; |
| 567 | if (in_group_p(pid->pid_gid)) |
| 568 | return true; |
| 569 | return ptrace_may_access(task, PTRACE_MODE_READ); |
| 570 | } |
| 571 | |
| 572 | |
| 573 | static int proc_pid_permission(struct inode *inode, int mask) |
| 574 | { |
| 575 | struct pid_namespace *pid = inode->i_sb->s_fs_info; |
| 576 | struct task_struct *task; |
| 577 | bool has_perms; |
| 578 | |
| 579 | task = get_proc_task(inode); |
Xiaotian Feng | a2ef990 | 2012-01-12 17:17:08 -0800 | [diff] [blame] | 580 | if (!task) |
| 581 | return -ESRCH; |
Vasiliy Kulikov | 0499680 | 2012-01-10 15:11:31 -0800 | [diff] [blame] | 582 | has_perms = has_pid_permissions(pid, task, 1); |
| 583 | put_task_struct(task); |
| 584 | |
| 585 | if (!has_perms) { |
| 586 | if (pid->hide_pid == 2) { |
| 587 | /* |
| 588 | * Let's make getdents(), stat(), and open() |
| 589 | * consistent with each other. If a process |
| 590 | * may not stat() a file, it shouldn't be seen |
| 591 | * in procfs at all. |
| 592 | */ |
| 593 | return -ENOENT; |
| 594 | } |
| 595 | |
| 596 | return -EPERM; |
| 597 | } |
| 598 | return generic_permission(inode, mask); |
| 599 | } |
| 600 | |
| 601 | |
| 602 | |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 603 | static const struct inode_operations proc_def_inode_operations = { |
Linus Torvalds | 6d76fa5 | 2006-07-15 12:26:45 -0700 | [diff] [blame] | 604 | .setattr = proc_setattr, |
| 605 | }; |
| 606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */ |
| 608 | |
| 609 | static ssize_t proc_info_read(struct file * file, char __user * buf, |
| 610 | size_t count, loff_t *ppos) |
| 611 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 612 | struct inode * inode = file_inode(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | unsigned long page; |
| 614 | ssize_t length; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 615 | struct task_struct *task = get_proc_task(inode); |
| 616 | |
| 617 | length = -ESRCH; |
| 618 | if (!task) |
| 619 | goto out_no_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | |
| 621 | if (count > PROC_BLOCK_SIZE) |
| 622 | count = PROC_BLOCK_SIZE; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 623 | |
| 624 | length = -ENOMEM; |
Mel Gorman | e12ba74 | 2007-10-16 01:25:52 -0700 | [diff] [blame] | 625 | if (!(page = __get_free_page(GFP_TEMPORARY))) |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 626 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | |
| 628 | length = PROC_I(inode)->op.proc_read(task, (char*)page); |
| 629 | |
| 630 | if (length >= 0) |
| 631 | length = simple_read_from_buffer(buf, count, ppos, (char *)page, length); |
| 632 | free_page(page); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 633 | out: |
| 634 | put_task_struct(task); |
| 635 | out_no_task: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | return length; |
| 637 | } |
| 638 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 639 | static const struct file_operations proc_info_file_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | .read = proc_info_read, |
Arnd Bergmann | 87df842 | 2010-03-17 23:06:02 +0100 | [diff] [blame] | 641 | .llseek = generic_file_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | }; |
| 643 | |
Eric W. Biederman | be61408 | 2008-02-08 04:18:30 -0800 | [diff] [blame] | 644 | static int proc_single_show(struct seq_file *m, void *v) |
| 645 | { |
| 646 | struct inode *inode = m->private; |
| 647 | struct pid_namespace *ns; |
| 648 | struct pid *pid; |
| 649 | struct task_struct *task; |
| 650 | int ret; |
| 651 | |
| 652 | ns = inode->i_sb->s_fs_info; |
| 653 | pid = proc_pid(inode); |
| 654 | task = get_pid_task(pid, PIDTYPE_PID); |
| 655 | if (!task) |
| 656 | return -ESRCH; |
| 657 | |
| 658 | ret = PROC_I(inode)->op.proc_show(m, ns, pid, task); |
| 659 | |
| 660 | put_task_struct(task); |
| 661 | return ret; |
| 662 | } |
| 663 | |
| 664 | static int proc_single_open(struct inode *inode, struct file *filp) |
| 665 | { |
Jovi Zhang | c6a3405 | 2011-01-12 17:00:34 -0800 | [diff] [blame] | 666 | return single_open(filp, proc_single_show, inode); |
Eric W. Biederman | be61408 | 2008-02-08 04:18:30 -0800 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | static const struct file_operations proc_single_file_operations = { |
| 670 | .open = proc_single_open, |
| 671 | .read = seq_read, |
| 672 | .llseek = seq_lseek, |
| 673 | .release = single_release, |
| 674 | }; |
| 675 | |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 676 | static int __mem_open(struct inode *inode, struct file *file, unsigned int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 678 | struct task_struct *task = get_proc_task(file_inode(file)); |
Linus Torvalds | e268337 | 2012-01-17 15:21:19 -0800 | [diff] [blame] | 679 | struct mm_struct *mm; |
| 680 | |
| 681 | if (!task) |
| 682 | return -ESRCH; |
| 683 | |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 684 | mm = mm_access(task, mode); |
Linus Torvalds | e268337 | 2012-01-17 15:21:19 -0800 | [diff] [blame] | 685 | put_task_struct(task); |
| 686 | |
| 687 | if (IS_ERR(mm)) |
| 688 | return PTR_ERR(mm); |
| 689 | |
Oleg Nesterov | 6d08f2c | 2012-01-31 17:15:11 +0100 | [diff] [blame] | 690 | if (mm) { |
| 691 | /* ensure this mm_struct can't be freed */ |
| 692 | atomic_inc(&mm->mm_count); |
| 693 | /* but do not pin its memory */ |
| 694 | mmput(mm); |
| 695 | } |
| 696 | |
Linus Torvalds | e268337 | 2012-01-17 15:21:19 -0800 | [diff] [blame] | 697 | file->private_data = mm; |
| 698 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | return 0; |
| 700 | } |
| 701 | |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 702 | static int mem_open(struct inode *inode, struct file *file) |
| 703 | { |
Djalal Harouni | bc452b4 | 2012-07-30 14:42:28 -0700 | [diff] [blame] | 704 | int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH); |
| 705 | |
| 706 | /* OK to pass negative loff_t, we can catch out-of-range */ |
| 707 | file->f_mode |= FMODE_UNSIGNED_OFFSET; |
| 708 | |
| 709 | return ret; |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Oleg Nesterov | 572d34b | 2012-01-31 17:14:54 +0100 | [diff] [blame] | 712 | static ssize_t mem_rw(struct file *file, char __user *buf, |
| 713 | size_t count, loff_t *ppos, int write) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | { |
Linus Torvalds | e268337 | 2012-01-17 15:21:19 -0800 | [diff] [blame] | 715 | struct mm_struct *mm = file->private_data; |
Oleg Nesterov | 572d34b | 2012-01-31 17:14:54 +0100 | [diff] [blame] | 716 | unsigned long addr = *ppos; |
| 717 | ssize_t copied; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | char *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | |
Linus Torvalds | e268337 | 2012-01-17 15:21:19 -0800 | [diff] [blame] | 720 | if (!mm) |
| 721 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | |
Mel Gorman | e12ba74 | 2007-10-16 01:25:52 -0700 | [diff] [blame] | 723 | page = (char *)__get_free_page(GFP_TEMPORARY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | if (!page) |
Linus Torvalds | e268337 | 2012-01-17 15:21:19 -0800 | [diff] [blame] | 725 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | |
Frederik Deweerdt | f7ca54f | 2006-09-29 02:01:02 -0700 | [diff] [blame] | 727 | copied = 0; |
Oleg Nesterov | 6d08f2c | 2012-01-31 17:15:11 +0100 | [diff] [blame] | 728 | if (!atomic_inc_not_zero(&mm->mm_users)) |
| 729 | goto free; |
| 730 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | while (count > 0) { |
Oleg Nesterov | 572d34b | 2012-01-31 17:14:54 +0100 | [diff] [blame] | 732 | int this_len = min_t(int, count, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | |
Oleg Nesterov | 572d34b | 2012-01-31 17:14:54 +0100 | [diff] [blame] | 734 | if (write && copy_from_user(page, buf, this_len)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | copied = -EFAULT; |
| 736 | break; |
| 737 | } |
Oleg Nesterov | 572d34b | 2012-01-31 17:14:54 +0100 | [diff] [blame] | 738 | |
| 739 | this_len = access_remote_vm(mm, addr, page, this_len, write); |
| 740 | if (!this_len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | if (!copied) |
| 742 | copied = -EIO; |
| 743 | break; |
| 744 | } |
Oleg Nesterov | 572d34b | 2012-01-31 17:14:54 +0100 | [diff] [blame] | 745 | |
| 746 | if (!write && copy_to_user(buf, page, this_len)) { |
| 747 | copied = -EFAULT; |
| 748 | break; |
| 749 | } |
| 750 | |
| 751 | buf += this_len; |
| 752 | addr += this_len; |
| 753 | copied += this_len; |
| 754 | count -= this_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | } |
Oleg Nesterov | 572d34b | 2012-01-31 17:14:54 +0100 | [diff] [blame] | 756 | *ppos = addr; |
KOSAKI Motohiro | 30cd890 | 2011-05-26 16:25:52 -0700 | [diff] [blame] | 757 | |
Oleg Nesterov | 6d08f2c | 2012-01-31 17:15:11 +0100 | [diff] [blame] | 758 | mmput(mm); |
| 759 | free: |
KOSAKI Motohiro | 30cd890 | 2011-05-26 16:25:52 -0700 | [diff] [blame] | 760 | free_page((unsigned long) page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | return copied; |
| 762 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | |
Oleg Nesterov | 572d34b | 2012-01-31 17:14:54 +0100 | [diff] [blame] | 764 | static ssize_t mem_read(struct file *file, char __user *buf, |
| 765 | size_t count, loff_t *ppos) |
| 766 | { |
| 767 | return mem_rw(file, buf, count, ppos, 0); |
| 768 | } |
| 769 | |
| 770 | static ssize_t mem_write(struct file *file, const char __user *buf, |
| 771 | size_t count, loff_t *ppos) |
| 772 | { |
| 773 | return mem_rw(file, (char __user*)buf, count, ppos, 1); |
| 774 | } |
| 775 | |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 776 | loff_t mem_lseek(struct file *file, loff_t offset, int orig) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | { |
| 778 | switch (orig) { |
| 779 | case 0: |
| 780 | file->f_pos = offset; |
| 781 | break; |
| 782 | case 1: |
| 783 | file->f_pos += offset; |
| 784 | break; |
| 785 | default: |
| 786 | return -EINVAL; |
| 787 | } |
| 788 | force_successful_syscall_return(); |
| 789 | return file->f_pos; |
| 790 | } |
| 791 | |
Linus Torvalds | e268337 | 2012-01-17 15:21:19 -0800 | [diff] [blame] | 792 | static int mem_release(struct inode *inode, struct file *file) |
| 793 | { |
| 794 | struct mm_struct *mm = file->private_data; |
Oleg Nesterov | 71879d3 | 2012-01-31 17:14:38 +0100 | [diff] [blame] | 795 | if (mm) |
Oleg Nesterov | 6d08f2c | 2012-01-31 17:15:11 +0100 | [diff] [blame] | 796 | mmdrop(mm); |
Linus Torvalds | e268337 | 2012-01-17 15:21:19 -0800 | [diff] [blame] | 797 | return 0; |
| 798 | } |
| 799 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 800 | static const struct file_operations proc_mem_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | .llseek = mem_lseek, |
| 802 | .read = mem_read, |
| 803 | .write = mem_write, |
| 804 | .open = mem_open, |
Linus Torvalds | e268337 | 2012-01-17 15:21:19 -0800 | [diff] [blame] | 805 | .release = mem_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | }; |
| 807 | |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 808 | static int environ_open(struct inode *inode, struct file *file) |
| 809 | { |
| 810 | return __mem_open(inode, file, PTRACE_MODE_READ); |
| 811 | } |
| 812 | |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 813 | static ssize_t environ_read(struct file *file, char __user *buf, |
| 814 | size_t count, loff_t *ppos) |
| 815 | { |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 816 | char *page; |
| 817 | unsigned long src = *ppos; |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 818 | int ret = 0; |
| 819 | struct mm_struct *mm = file->private_data; |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 820 | |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 821 | if (!mm) |
| 822 | return 0; |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 823 | |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 824 | page = (char *)__get_free_page(GFP_TEMPORARY); |
| 825 | if (!page) |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 826 | return -ENOMEM; |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 827 | |
Al Viro | d6f64b8 | 2011-02-15 22:26:01 -0500 | [diff] [blame] | 828 | ret = 0; |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 829 | if (!atomic_inc_not_zero(&mm->mm_users)) |
| 830 | goto free; |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 831 | while (count > 0) { |
Djalal Harouni | e8905ec | 2012-07-30 14:42:26 -0700 | [diff] [blame] | 832 | size_t this_len, max_len; |
| 833 | int retval; |
| 834 | |
| 835 | if (src >= (mm->env_end - mm->env_start)) |
| 836 | break; |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 837 | |
| 838 | this_len = mm->env_end - (mm->env_start + src); |
| 839 | |
Djalal Harouni | e8905ec | 2012-07-30 14:42:26 -0700 | [diff] [blame] | 840 | max_len = min_t(size_t, PAGE_SIZE, count); |
| 841 | this_len = min(max_len, this_len); |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 842 | |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 843 | retval = access_remote_vm(mm, (mm->env_start + src), |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 844 | page, this_len, 0); |
| 845 | |
| 846 | if (retval <= 0) { |
| 847 | ret = retval; |
| 848 | break; |
| 849 | } |
| 850 | |
| 851 | if (copy_to_user(buf, page, retval)) { |
| 852 | ret = -EFAULT; |
| 853 | break; |
| 854 | } |
| 855 | |
| 856 | ret += retval; |
| 857 | src += retval; |
| 858 | buf += retval; |
| 859 | count -= retval; |
| 860 | } |
| 861 | *ppos = src; |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 862 | mmput(mm); |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 863 | |
| 864 | free: |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 865 | free_page((unsigned long) page); |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 866 | return ret; |
| 867 | } |
| 868 | |
| 869 | static const struct file_operations proc_environ_operations = { |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 870 | .open = environ_open, |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 871 | .read = environ_read, |
Arnd Bergmann | 87df842 | 2010-03-17 23:06:02 +0100 | [diff] [blame] | 872 | .llseek = generic_file_llseek, |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 873 | .release = mem_release, |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 874 | }; |
| 875 | |
David Rientjes | fa0cbbf | 2012-11-12 17:53:04 -0800 | [diff] [blame] | 876 | static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count, |
| 877 | loff_t *ppos) |
| 878 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 879 | struct task_struct *task = get_proc_task(file_inode(file)); |
David Rientjes | fa0cbbf | 2012-11-12 17:53:04 -0800 | [diff] [blame] | 880 | char buffer[PROC_NUMBUF]; |
| 881 | int oom_adj = OOM_ADJUST_MIN; |
| 882 | size_t len; |
| 883 | unsigned long flags; |
| 884 | |
| 885 | if (!task) |
| 886 | return -ESRCH; |
| 887 | if (lock_task_sighand(task, &flags)) { |
| 888 | if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX) |
| 889 | oom_adj = OOM_ADJUST_MAX; |
| 890 | else |
| 891 | oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) / |
| 892 | OOM_SCORE_ADJ_MAX; |
| 893 | unlock_task_sighand(task, &flags); |
| 894 | } |
| 895 | put_task_struct(task); |
| 896 | len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj); |
| 897 | return simple_read_from_buffer(buf, count, ppos, buffer, len); |
| 898 | } |
| 899 | |
| 900 | static ssize_t oom_adj_write(struct file *file, const char __user *buf, |
| 901 | size_t count, loff_t *ppos) |
| 902 | { |
| 903 | struct task_struct *task; |
| 904 | char buffer[PROC_NUMBUF]; |
| 905 | int oom_adj; |
| 906 | unsigned long flags; |
| 907 | int err; |
| 908 | |
| 909 | memset(buffer, 0, sizeof(buffer)); |
| 910 | if (count > sizeof(buffer) - 1) |
| 911 | count = sizeof(buffer) - 1; |
| 912 | if (copy_from_user(buffer, buf, count)) { |
| 913 | err = -EFAULT; |
| 914 | goto out; |
| 915 | } |
| 916 | |
| 917 | err = kstrtoint(strstrip(buffer), 0, &oom_adj); |
| 918 | if (err) |
| 919 | goto out; |
| 920 | if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) && |
| 921 | oom_adj != OOM_DISABLE) { |
| 922 | err = -EINVAL; |
| 923 | goto out; |
| 924 | } |
| 925 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 926 | task = get_proc_task(file_inode(file)); |
David Rientjes | fa0cbbf | 2012-11-12 17:53:04 -0800 | [diff] [blame] | 927 | if (!task) { |
| 928 | err = -ESRCH; |
| 929 | goto out; |
| 930 | } |
| 931 | |
| 932 | task_lock(task); |
| 933 | if (!task->mm) { |
| 934 | err = -EINVAL; |
| 935 | goto err_task_lock; |
| 936 | } |
| 937 | |
| 938 | if (!lock_task_sighand(task, &flags)) { |
| 939 | err = -ESRCH; |
| 940 | goto err_task_lock; |
| 941 | } |
| 942 | |
| 943 | /* |
| 944 | * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum |
| 945 | * value is always attainable. |
| 946 | */ |
| 947 | if (oom_adj == OOM_ADJUST_MAX) |
| 948 | oom_adj = OOM_SCORE_ADJ_MAX; |
| 949 | else |
| 950 | oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE; |
| 951 | |
| 952 | if (oom_adj < task->signal->oom_score_adj && |
| 953 | !capable(CAP_SYS_RESOURCE)) { |
| 954 | err = -EACCES; |
| 955 | goto err_sighand; |
| 956 | } |
| 957 | |
| 958 | /* |
| 959 | * /proc/pid/oom_adj is provided for legacy purposes, ask users to use |
| 960 | * /proc/pid/oom_score_adj instead. |
| 961 | */ |
Andrew Morton | 87ebdc0 | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 962 | pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n", |
David Rientjes | fa0cbbf | 2012-11-12 17:53:04 -0800 | [diff] [blame] | 963 | current->comm, task_pid_nr(current), task_pid_nr(task), |
| 964 | task_pid_nr(task)); |
| 965 | |
| 966 | task->signal->oom_score_adj = oom_adj; |
| 967 | trace_oom_score_adj_update(task); |
| 968 | err_sighand: |
| 969 | unlock_task_sighand(task, &flags); |
| 970 | err_task_lock: |
| 971 | task_unlock(task); |
| 972 | put_task_struct(task); |
| 973 | out: |
| 974 | return err < 0 ? err : count; |
| 975 | } |
| 976 | |
| 977 | static const struct file_operations proc_oom_adj_operations = { |
| 978 | .read = oom_adj_read, |
| 979 | .write = oom_adj_write, |
| 980 | .llseek = generic_file_llseek, |
| 981 | }; |
| 982 | |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 983 | static ssize_t oom_score_adj_read(struct file *file, char __user *buf, |
| 984 | size_t count, loff_t *ppos) |
| 985 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 986 | struct task_struct *task = get_proc_task(file_inode(file)); |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 987 | char buffer[PROC_NUMBUF]; |
David Rientjes | a9c58b90 | 2012-12-11 16:02:54 -0800 | [diff] [blame] | 988 | short oom_score_adj = OOM_SCORE_ADJ_MIN; |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 989 | unsigned long flags; |
| 990 | size_t len; |
| 991 | |
| 992 | if (!task) |
| 993 | return -ESRCH; |
| 994 | if (lock_task_sighand(task, &flags)) { |
| 995 | oom_score_adj = task->signal->oom_score_adj; |
| 996 | unlock_task_sighand(task, &flags); |
| 997 | } |
| 998 | put_task_struct(task); |
David Rientjes | a9c58b90 | 2012-12-11 16:02:54 -0800 | [diff] [blame] | 999 | len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj); |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1000 | return simple_read_from_buffer(buf, count, ppos, buffer, len); |
| 1001 | } |
| 1002 | |
| 1003 | static ssize_t oom_score_adj_write(struct file *file, const char __user *buf, |
| 1004 | size_t count, loff_t *ppos) |
| 1005 | { |
| 1006 | struct task_struct *task; |
| 1007 | char buffer[PROC_NUMBUF]; |
| 1008 | unsigned long flags; |
Alexey Dobriyan | 0a8cb8e | 2011-05-26 16:25:50 -0700 | [diff] [blame] | 1009 | int oom_score_adj; |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1010 | int err; |
| 1011 | |
| 1012 | memset(buffer, 0, sizeof(buffer)); |
| 1013 | if (count > sizeof(buffer) - 1) |
| 1014 | count = sizeof(buffer) - 1; |
David Rientjes | 723548b | 2010-10-26 14:21:25 -0700 | [diff] [blame] | 1015 | if (copy_from_user(buffer, buf, count)) { |
| 1016 | err = -EFAULT; |
| 1017 | goto out; |
| 1018 | } |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1019 | |
Alexey Dobriyan | 0a8cb8e | 2011-05-26 16:25:50 -0700 | [diff] [blame] | 1020 | err = kstrtoint(strstrip(buffer), 0, &oom_score_adj); |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1021 | if (err) |
David Rientjes | 723548b | 2010-10-26 14:21:25 -0700 | [diff] [blame] | 1022 | goto out; |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1023 | if (oom_score_adj < OOM_SCORE_ADJ_MIN || |
David Rientjes | 723548b | 2010-10-26 14:21:25 -0700 | [diff] [blame] | 1024 | oom_score_adj > OOM_SCORE_ADJ_MAX) { |
| 1025 | err = -EINVAL; |
| 1026 | goto out; |
| 1027 | } |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1028 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1029 | task = get_proc_task(file_inode(file)); |
David Rientjes | 723548b | 2010-10-26 14:21:25 -0700 | [diff] [blame] | 1030 | if (!task) { |
| 1031 | err = -ESRCH; |
| 1032 | goto out; |
| 1033 | } |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1034 | |
Ying Han | 3d5992d | 2010-10-26 14:21:23 -0700 | [diff] [blame] | 1035 | task_lock(task); |
| 1036 | if (!task->mm) { |
David Rientjes | 723548b | 2010-10-26 14:21:25 -0700 | [diff] [blame] | 1037 | err = -EINVAL; |
| 1038 | goto err_task_lock; |
Ying Han | 3d5992d | 2010-10-26 14:21:23 -0700 | [diff] [blame] | 1039 | } |
David Rientjes | d19d547 | 2010-10-26 14:21:26 -0700 | [diff] [blame] | 1040 | |
| 1041 | if (!lock_task_sighand(task, &flags)) { |
| 1042 | err = -ESRCH; |
| 1043 | goto err_task_lock; |
| 1044 | } |
| 1045 | |
David Rientjes | a9c58b90 | 2012-12-11 16:02:54 -0800 | [diff] [blame] | 1046 | if ((short)oom_score_adj < task->signal->oom_score_adj_min && |
David Rientjes | d19d547 | 2010-10-26 14:21:26 -0700 | [diff] [blame] | 1047 | !capable(CAP_SYS_RESOURCE)) { |
| 1048 | err = -EACCES; |
| 1049 | goto err_sighand; |
| 1050 | } |
| 1051 | |
David Rientjes | a9c58b90 | 2012-12-11 16:02:54 -0800 | [diff] [blame] | 1052 | task->signal->oom_score_adj = (short)oom_score_adj; |
Mandeep Singh Baines | dabb16f | 2011-01-13 15:46:05 -0800 | [diff] [blame] | 1053 | if (has_capability_noaudit(current, CAP_SYS_RESOURCE)) |
David Rientjes | a9c58b90 | 2012-12-11 16:02:54 -0800 | [diff] [blame] | 1054 | task->signal->oom_score_adj_min = (short)oom_score_adj; |
KAMEZAWA Hiroyuki | 43d2b11 | 2012-01-10 15:08:09 -0800 | [diff] [blame] | 1055 | trace_oom_score_adj_update(task); |
Davidlohr Bueso | 01dc52e | 2012-10-08 16:29:30 -0700 | [diff] [blame] | 1056 | |
David Rientjes | 723548b | 2010-10-26 14:21:25 -0700 | [diff] [blame] | 1057 | err_sighand: |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1058 | unlock_task_sighand(task, &flags); |
David Rientjes | d19d547 | 2010-10-26 14:21:26 -0700 | [diff] [blame] | 1059 | err_task_lock: |
| 1060 | task_unlock(task); |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1061 | put_task_struct(task); |
David Rientjes | 723548b | 2010-10-26 14:21:25 -0700 | [diff] [blame] | 1062 | out: |
| 1063 | return err < 0 ? err : count; |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | static const struct file_operations proc_oom_score_adj_operations = { |
| 1067 | .read = oom_score_adj_read, |
| 1068 | .write = oom_score_adj_write, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 1069 | .llseek = default_llseek, |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1070 | }; |
| 1071 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | #ifdef CONFIG_AUDITSYSCALL |
| 1073 | #define TMPBUFLEN 21 |
| 1074 | static ssize_t proc_loginuid_read(struct file * file, char __user * buf, |
| 1075 | size_t count, loff_t *ppos) |
| 1076 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1077 | struct inode * inode = file_inode(file); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1078 | struct task_struct *task = get_proc_task(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | ssize_t length; |
| 1080 | char tmpbuf[TMPBUFLEN]; |
| 1081 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1082 | if (!task) |
| 1083 | return -ESRCH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | length = scnprintf(tmpbuf, TMPBUFLEN, "%u", |
Eric W. Biederman | e1760bd | 2012-09-10 22:39:43 -0700 | [diff] [blame] | 1085 | from_kuid(file->f_cred->user_ns, |
| 1086 | audit_get_loginuid(task))); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1087 | put_task_struct(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); |
| 1089 | } |
| 1090 | |
| 1091 | static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, |
| 1092 | size_t count, loff_t *ppos) |
| 1093 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1094 | struct inode * inode = file_inode(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | char *page, *tmp; |
| 1096 | ssize_t length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | uid_t loginuid; |
Eric W. Biederman | e1760bd | 2012-09-10 22:39:43 -0700 | [diff] [blame] | 1098 | kuid_t kloginuid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | |
Paul E. McKenney | 7dc5215 | 2010-02-22 17:04:52 -0800 | [diff] [blame] | 1100 | rcu_read_lock(); |
| 1101 | if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) { |
| 1102 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | return -EPERM; |
Paul E. McKenney | 7dc5215 | 2010-02-22 17:04:52 -0800 | [diff] [blame] | 1104 | } |
| 1105 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | |
Al Viro | e018290 | 2006-05-18 08:28:02 -0400 | [diff] [blame] | 1107 | if (count >= PAGE_SIZE) |
| 1108 | count = PAGE_SIZE - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | |
| 1110 | if (*ppos != 0) { |
| 1111 | /* No partial writes. */ |
| 1112 | return -EINVAL; |
| 1113 | } |
Mel Gorman | e12ba74 | 2007-10-16 01:25:52 -0700 | [diff] [blame] | 1114 | page = (char*)__get_free_page(GFP_TEMPORARY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | if (!page) |
| 1116 | return -ENOMEM; |
| 1117 | length = -EFAULT; |
| 1118 | if (copy_from_user(page, buf, count)) |
| 1119 | goto out_free_page; |
| 1120 | |
Al Viro | e018290 | 2006-05-18 08:28:02 -0400 | [diff] [blame] | 1121 | page[count] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | loginuid = simple_strtoul(page, &tmp, 10); |
| 1123 | if (tmp == page) { |
| 1124 | length = -EINVAL; |
| 1125 | goto out_free_page; |
| 1126 | |
| 1127 | } |
Eric Paris | 81407c8 | 2013-05-24 09:49:14 -0400 | [diff] [blame] | 1128 | |
| 1129 | /* is userspace tring to explicitly UNSET the loginuid? */ |
| 1130 | if (loginuid == AUDIT_UID_UNSET) { |
| 1131 | kloginuid = INVALID_UID; |
| 1132 | } else { |
| 1133 | kloginuid = make_kuid(file->f_cred->user_ns, loginuid); |
| 1134 | if (!uid_valid(kloginuid)) { |
| 1135 | length = -EINVAL; |
| 1136 | goto out_free_page; |
| 1137 | } |
Eric W. Biederman | e1760bd | 2012-09-10 22:39:43 -0700 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | length = audit_set_loginuid(kloginuid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | if (likely(length == 0)) |
| 1142 | length = count; |
| 1143 | |
| 1144 | out_free_page: |
| 1145 | free_page((unsigned long) page); |
| 1146 | return length; |
| 1147 | } |
| 1148 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 1149 | static const struct file_operations proc_loginuid_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | .read = proc_loginuid_read, |
| 1151 | .write = proc_loginuid_write, |
Arnd Bergmann | 87df842 | 2010-03-17 23:06:02 +0100 | [diff] [blame] | 1152 | .llseek = generic_file_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | }; |
Eric Paris | 1e0bd75 | 2008-03-13 08:15:31 -0400 | [diff] [blame] | 1154 | |
| 1155 | static ssize_t proc_sessionid_read(struct file * file, char __user * buf, |
| 1156 | size_t count, loff_t *ppos) |
| 1157 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1158 | struct inode * inode = file_inode(file); |
Eric Paris | 1e0bd75 | 2008-03-13 08:15:31 -0400 | [diff] [blame] | 1159 | struct task_struct *task = get_proc_task(inode); |
| 1160 | ssize_t length; |
| 1161 | char tmpbuf[TMPBUFLEN]; |
| 1162 | |
| 1163 | if (!task) |
| 1164 | return -ESRCH; |
| 1165 | length = scnprintf(tmpbuf, TMPBUFLEN, "%u", |
| 1166 | audit_get_sessionid(task)); |
| 1167 | put_task_struct(task); |
| 1168 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); |
| 1169 | } |
| 1170 | |
| 1171 | static const struct file_operations proc_sessionid_operations = { |
| 1172 | .read = proc_sessionid_read, |
Arnd Bergmann | 87df842 | 2010-03-17 23:06:02 +0100 | [diff] [blame] | 1173 | .llseek = generic_file_llseek, |
Eric Paris | 1e0bd75 | 2008-03-13 08:15:31 -0400 | [diff] [blame] | 1174 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | #endif |
| 1176 | |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 1177 | #ifdef CONFIG_FAULT_INJECTION |
| 1178 | static ssize_t proc_fault_inject_read(struct file * file, char __user * buf, |
| 1179 | size_t count, loff_t *ppos) |
| 1180 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1181 | struct task_struct *task = get_proc_task(file_inode(file)); |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 1182 | char buffer[PROC_NUMBUF]; |
| 1183 | size_t len; |
| 1184 | int make_it_fail; |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 1185 | |
| 1186 | if (!task) |
| 1187 | return -ESRCH; |
| 1188 | make_it_fail = task->make_it_fail; |
| 1189 | put_task_struct(task); |
| 1190 | |
| 1191 | len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail); |
Akinobu Mita | 0c28f28 | 2007-05-08 00:31:41 -0700 | [diff] [blame] | 1192 | |
| 1193 | return simple_read_from_buffer(buf, count, ppos, buffer, len); |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | static ssize_t proc_fault_inject_write(struct file * file, |
| 1197 | const char __user * buf, size_t count, loff_t *ppos) |
| 1198 | { |
| 1199 | struct task_struct *task; |
| 1200 | char buffer[PROC_NUMBUF], *end; |
| 1201 | int make_it_fail; |
| 1202 | |
| 1203 | if (!capable(CAP_SYS_RESOURCE)) |
| 1204 | return -EPERM; |
| 1205 | memset(buffer, 0, sizeof(buffer)); |
| 1206 | if (count > sizeof(buffer) - 1) |
| 1207 | count = sizeof(buffer) - 1; |
| 1208 | if (copy_from_user(buffer, buf, count)) |
| 1209 | return -EFAULT; |
Vincent Li | cba8aaf | 2009-09-22 16:45:38 -0700 | [diff] [blame] | 1210 | make_it_fail = simple_strtol(strstrip(buffer), &end, 0); |
| 1211 | if (*end) |
| 1212 | return -EINVAL; |
Dave Jones | 16caed3 | 2014-04-07 15:39:15 -0700 | [diff] [blame] | 1213 | if (make_it_fail < 0 || make_it_fail > 1) |
| 1214 | return -EINVAL; |
| 1215 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1216 | task = get_proc_task(file_inode(file)); |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 1217 | if (!task) |
| 1218 | return -ESRCH; |
| 1219 | task->make_it_fail = make_it_fail; |
| 1220 | put_task_struct(task); |
Vincent Li | cba8aaf | 2009-09-22 16:45:38 -0700 | [diff] [blame] | 1221 | |
| 1222 | return count; |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 1223 | } |
| 1224 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 1225 | static const struct file_operations proc_fault_inject_operations = { |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 1226 | .read = proc_fault_inject_read, |
| 1227 | .write = proc_fault_inject_write, |
Arnd Bergmann | 87df842 | 2010-03-17 23:06:02 +0100 | [diff] [blame] | 1228 | .llseek = generic_file_llseek, |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 1229 | }; |
| 1230 | #endif |
| 1231 | |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 1232 | |
Ingo Molnar | 43ae34c | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 1233 | #ifdef CONFIG_SCHED_DEBUG |
| 1234 | /* |
| 1235 | * Print out various scheduling related per-task fields: |
| 1236 | */ |
| 1237 | static int sched_show(struct seq_file *m, void *v) |
| 1238 | { |
| 1239 | struct inode *inode = m->private; |
| 1240 | struct task_struct *p; |
| 1241 | |
Ingo Molnar | 43ae34c | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 1242 | p = get_proc_task(inode); |
| 1243 | if (!p) |
| 1244 | return -ESRCH; |
| 1245 | proc_sched_show_task(p, m); |
| 1246 | |
| 1247 | put_task_struct(p); |
| 1248 | |
| 1249 | return 0; |
| 1250 | } |
| 1251 | |
| 1252 | static ssize_t |
| 1253 | sched_write(struct file *file, const char __user *buf, |
| 1254 | size_t count, loff_t *offset) |
| 1255 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1256 | struct inode *inode = file_inode(file); |
Ingo Molnar | 43ae34c | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 1257 | struct task_struct *p; |
| 1258 | |
Ingo Molnar | 43ae34c | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 1259 | p = get_proc_task(inode); |
| 1260 | if (!p) |
| 1261 | return -ESRCH; |
| 1262 | proc_sched_set_task(p); |
| 1263 | |
| 1264 | put_task_struct(p); |
| 1265 | |
| 1266 | return count; |
| 1267 | } |
| 1268 | |
| 1269 | static int sched_open(struct inode *inode, struct file *filp) |
| 1270 | { |
Jovi Zhang | c6a3405 | 2011-01-12 17:00:34 -0800 | [diff] [blame] | 1271 | return single_open(filp, sched_show, inode); |
Ingo Molnar | 43ae34c | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | static const struct file_operations proc_pid_sched_operations = { |
| 1275 | .open = sched_open, |
| 1276 | .read = seq_read, |
| 1277 | .write = sched_write, |
| 1278 | .llseek = seq_lseek, |
Alexey Dobriyan | 5ea473a | 2007-07-31 00:38:50 -0700 | [diff] [blame] | 1279 | .release = single_release, |
Ingo Molnar | 43ae34c | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 1280 | }; |
| 1281 | |
| 1282 | #endif |
| 1283 | |
Mike Galbraith | 5091faa | 2010-11-30 14:18:03 +0100 | [diff] [blame] | 1284 | #ifdef CONFIG_SCHED_AUTOGROUP |
| 1285 | /* |
| 1286 | * Print out autogroup related information: |
| 1287 | */ |
| 1288 | static int sched_autogroup_show(struct seq_file *m, void *v) |
| 1289 | { |
| 1290 | struct inode *inode = m->private; |
| 1291 | struct task_struct *p; |
| 1292 | |
| 1293 | p = get_proc_task(inode); |
| 1294 | if (!p) |
| 1295 | return -ESRCH; |
| 1296 | proc_sched_autogroup_show_task(p, m); |
| 1297 | |
| 1298 | put_task_struct(p); |
| 1299 | |
| 1300 | return 0; |
| 1301 | } |
| 1302 | |
| 1303 | static ssize_t |
| 1304 | sched_autogroup_write(struct file *file, const char __user *buf, |
| 1305 | size_t count, loff_t *offset) |
| 1306 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1307 | struct inode *inode = file_inode(file); |
Mike Galbraith | 5091faa | 2010-11-30 14:18:03 +0100 | [diff] [blame] | 1308 | struct task_struct *p; |
| 1309 | char buffer[PROC_NUMBUF]; |
Alexey Dobriyan | 0a8cb8e | 2011-05-26 16:25:50 -0700 | [diff] [blame] | 1310 | int nice; |
Mike Galbraith | 5091faa | 2010-11-30 14:18:03 +0100 | [diff] [blame] | 1311 | int err; |
| 1312 | |
| 1313 | memset(buffer, 0, sizeof(buffer)); |
| 1314 | if (count > sizeof(buffer) - 1) |
| 1315 | count = sizeof(buffer) - 1; |
| 1316 | if (copy_from_user(buffer, buf, count)) |
| 1317 | return -EFAULT; |
| 1318 | |
Alexey Dobriyan | 0a8cb8e | 2011-05-26 16:25:50 -0700 | [diff] [blame] | 1319 | err = kstrtoint(strstrip(buffer), 0, &nice); |
| 1320 | if (err < 0) |
| 1321 | return err; |
Mike Galbraith | 5091faa | 2010-11-30 14:18:03 +0100 | [diff] [blame] | 1322 | |
| 1323 | p = get_proc_task(inode); |
| 1324 | if (!p) |
| 1325 | return -ESRCH; |
| 1326 | |
Hiroshi Shimamoto | 2e5b5b3 | 2012-02-23 17:41:27 +0900 | [diff] [blame] | 1327 | err = proc_sched_autogroup_set_nice(p, nice); |
Mike Galbraith | 5091faa | 2010-11-30 14:18:03 +0100 | [diff] [blame] | 1328 | if (err) |
| 1329 | count = err; |
| 1330 | |
| 1331 | put_task_struct(p); |
| 1332 | |
| 1333 | return count; |
| 1334 | } |
| 1335 | |
| 1336 | static int sched_autogroup_open(struct inode *inode, struct file *filp) |
| 1337 | { |
| 1338 | int ret; |
| 1339 | |
| 1340 | ret = single_open(filp, sched_autogroup_show, NULL); |
| 1341 | if (!ret) { |
| 1342 | struct seq_file *m = filp->private_data; |
| 1343 | |
| 1344 | m->private = inode; |
| 1345 | } |
| 1346 | return ret; |
| 1347 | } |
| 1348 | |
| 1349 | static const struct file_operations proc_pid_sched_autogroup_operations = { |
| 1350 | .open = sched_autogroup_open, |
| 1351 | .read = seq_read, |
| 1352 | .write = sched_autogroup_write, |
| 1353 | .llseek = seq_lseek, |
| 1354 | .release = single_release, |
| 1355 | }; |
| 1356 | |
| 1357 | #endif /* CONFIG_SCHED_AUTOGROUP */ |
| 1358 | |
john stultz | 4614a696b | 2009-12-14 18:00:05 -0800 | [diff] [blame] | 1359 | static ssize_t comm_write(struct file *file, const char __user *buf, |
| 1360 | size_t count, loff_t *offset) |
| 1361 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1362 | struct inode *inode = file_inode(file); |
john stultz | 4614a696b | 2009-12-14 18:00:05 -0800 | [diff] [blame] | 1363 | struct task_struct *p; |
| 1364 | char buffer[TASK_COMM_LEN]; |
David Rientjes | 830e0fc | 2013-04-30 15:28:18 -0700 | [diff] [blame] | 1365 | const size_t maxlen = sizeof(buffer) - 1; |
john stultz | 4614a696b | 2009-12-14 18:00:05 -0800 | [diff] [blame] | 1366 | |
| 1367 | memset(buffer, 0, sizeof(buffer)); |
David Rientjes | 830e0fc | 2013-04-30 15:28:18 -0700 | [diff] [blame] | 1368 | if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count)) |
john stultz | 4614a696b | 2009-12-14 18:00:05 -0800 | [diff] [blame] | 1369 | return -EFAULT; |
| 1370 | |
| 1371 | p = get_proc_task(inode); |
| 1372 | if (!p) |
| 1373 | return -ESRCH; |
| 1374 | |
| 1375 | if (same_thread_group(current, p)) |
| 1376 | set_task_comm(p, buffer); |
| 1377 | else |
| 1378 | count = -EINVAL; |
| 1379 | |
| 1380 | put_task_struct(p); |
| 1381 | |
| 1382 | return count; |
| 1383 | } |
| 1384 | |
| 1385 | static int comm_show(struct seq_file *m, void *v) |
| 1386 | { |
| 1387 | struct inode *inode = m->private; |
| 1388 | struct task_struct *p; |
| 1389 | |
| 1390 | p = get_proc_task(inode); |
| 1391 | if (!p) |
| 1392 | return -ESRCH; |
| 1393 | |
| 1394 | task_lock(p); |
| 1395 | seq_printf(m, "%s\n", p->comm); |
| 1396 | task_unlock(p); |
| 1397 | |
| 1398 | put_task_struct(p); |
| 1399 | |
| 1400 | return 0; |
| 1401 | } |
| 1402 | |
| 1403 | static int comm_open(struct inode *inode, struct file *filp) |
| 1404 | { |
Jovi Zhang | c6a3405 | 2011-01-12 17:00:34 -0800 | [diff] [blame] | 1405 | return single_open(filp, comm_show, inode); |
john stultz | 4614a696b | 2009-12-14 18:00:05 -0800 | [diff] [blame] | 1406 | } |
| 1407 | |
| 1408 | static const struct file_operations proc_pid_set_comm_operations = { |
| 1409 | .open = comm_open, |
| 1410 | .read = seq_read, |
| 1411 | .write = comm_write, |
| 1412 | .llseek = seq_lseek, |
| 1413 | .release = single_release, |
| 1414 | }; |
| 1415 | |
Cyrill Gorcunov | 7773fbc | 2012-01-10 15:11:20 -0800 | [diff] [blame] | 1416 | static int proc_exe_link(struct dentry *dentry, struct path *exe_path) |
Matt Helsley | 925d1c4 | 2008-04-29 01:01:36 -0700 | [diff] [blame] | 1417 | { |
| 1418 | struct task_struct *task; |
| 1419 | struct mm_struct *mm; |
| 1420 | struct file *exe_file; |
| 1421 | |
Cyrill Gorcunov | 7773fbc | 2012-01-10 15:11:20 -0800 | [diff] [blame] | 1422 | task = get_proc_task(dentry->d_inode); |
Matt Helsley | 925d1c4 | 2008-04-29 01:01:36 -0700 | [diff] [blame] | 1423 | if (!task) |
| 1424 | return -ENOENT; |
| 1425 | mm = get_task_mm(task); |
| 1426 | put_task_struct(task); |
| 1427 | if (!mm) |
| 1428 | return -ENOENT; |
| 1429 | exe_file = get_mm_exe_file(mm); |
| 1430 | mmput(mm); |
| 1431 | if (exe_file) { |
| 1432 | *exe_path = exe_file->f_path; |
| 1433 | path_get(&exe_file->f_path); |
| 1434 | fput(exe_file); |
| 1435 | return 0; |
| 1436 | } else |
| 1437 | return -ENOENT; |
| 1438 | } |
| 1439 | |
Al Viro | 008b150 | 2005-08-20 00:17:39 +0100 | [diff] [blame] | 1440 | static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | { |
| 1442 | struct inode *inode = dentry->d_inode; |
Christoph Hellwig | 408ef01 | 2012-06-18 10:47:03 -0400 | [diff] [blame] | 1443 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | int error = -EACCES; |
| 1445 | |
Eric W. Biederman | 778c114 | 2006-06-26 00:25:58 -0700 | [diff] [blame] | 1446 | /* Are we allowed to snoop on the tasks file descriptors? */ |
| 1447 | if (!proc_fd_access_allowed(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | |
Christoph Hellwig | 408ef01 | 2012-06-18 10:47:03 -0400 | [diff] [blame] | 1450 | error = PROC_I(inode)->op.proc_get_link(dentry, &path); |
| 1451 | if (error) |
| 1452 | goto out; |
| 1453 | |
Christoph Hellwig | b5fb63c1 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 1454 | nd_jump_link(nd, &path); |
Christoph Hellwig | 408ef01 | 2012-06-18 10:47:03 -0400 | [diff] [blame] | 1455 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | out: |
Al Viro | 008b150 | 2005-08-20 00:17:39 +0100 | [diff] [blame] | 1457 | return ERR_PTR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | } |
| 1459 | |
Jan Blunck | 3dcd25f | 2008-02-14 19:38:35 -0800 | [diff] [blame] | 1460 | static int do_proc_readlink(struct path *path, char __user *buffer, int buflen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | { |
Mel Gorman | e12ba74 | 2007-10-16 01:25:52 -0700 | [diff] [blame] | 1462 | char *tmp = (char*)__get_free_page(GFP_TEMPORARY); |
Jan Blunck | 3dcd25f | 2008-02-14 19:38:35 -0800 | [diff] [blame] | 1463 | char *pathname; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | int len; |
| 1465 | |
| 1466 | if (!tmp) |
| 1467 | return -ENOMEM; |
Akinobu Mita | 0c28f28 | 2007-05-08 00:31:41 -0700 | [diff] [blame] | 1468 | |
Eric W. Biederman | 7b2a69b | 2010-12-05 15:51:21 -0800 | [diff] [blame] | 1469 | pathname = d_path(path, tmp, PAGE_SIZE); |
Jan Blunck | 3dcd25f | 2008-02-14 19:38:35 -0800 | [diff] [blame] | 1470 | len = PTR_ERR(pathname); |
| 1471 | if (IS_ERR(pathname)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | goto out; |
Jan Blunck | 3dcd25f | 2008-02-14 19:38:35 -0800 | [diff] [blame] | 1473 | len = tmp + PAGE_SIZE - 1 - pathname; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | |
| 1475 | if (len > buflen) |
| 1476 | len = buflen; |
Jan Blunck | 3dcd25f | 2008-02-14 19:38:35 -0800 | [diff] [blame] | 1477 | if (copy_to_user(buffer, pathname, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | len = -EFAULT; |
| 1479 | out: |
| 1480 | free_page((unsigned long)tmp); |
| 1481 | return len; |
| 1482 | } |
| 1483 | |
| 1484 | static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen) |
| 1485 | { |
| 1486 | int error = -EACCES; |
| 1487 | struct inode *inode = dentry->d_inode; |
Jan Blunck | 3dcd25f | 2008-02-14 19:38:35 -0800 | [diff] [blame] | 1488 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | |
Eric W. Biederman | 778c114 | 2006-06-26 00:25:58 -0700 | [diff] [blame] | 1490 | /* Are we allowed to snoop on the tasks file descriptors? */ |
| 1491 | if (!proc_fd_access_allowed(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | |
Cyrill Gorcunov | 7773fbc | 2012-01-10 15:11:20 -0800 | [diff] [blame] | 1494 | error = PROC_I(inode)->op.proc_get_link(dentry, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | if (error) |
| 1496 | goto out; |
| 1497 | |
Jan Blunck | 3dcd25f | 2008-02-14 19:38:35 -0800 | [diff] [blame] | 1498 | error = do_proc_readlink(&path, buffer, buflen); |
| 1499 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | return error; |
| 1502 | } |
| 1503 | |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 1504 | const struct inode_operations proc_pid_link_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | .readlink = proc_pid_readlink, |
Linus Torvalds | 6d76fa5 | 2006-07-15 12:26:45 -0700 | [diff] [blame] | 1506 | .follow_link = proc_pid_follow_link, |
| 1507 | .setattr = proc_setattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | }; |
| 1509 | |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1510 | |
| 1511 | /* building an inode */ |
| 1512 | |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 1513 | struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task) |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1514 | { |
| 1515 | struct inode * inode; |
| 1516 | struct proc_inode *ei; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1517 | const struct cred *cred; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1518 | |
| 1519 | /* We need a new inode */ |
| 1520 | |
| 1521 | inode = new_inode(sb); |
| 1522 | if (!inode) |
| 1523 | goto out; |
| 1524 | |
| 1525 | /* Common stuff */ |
| 1526 | ei = PROC_I(inode); |
Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 1527 | inode->i_ino = get_next_ino(); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1528 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1529 | inode->i_op = &proc_def_inode_operations; |
| 1530 | |
| 1531 | /* |
| 1532 | * grab the reference to task. |
| 1533 | */ |
Oleg Nesterov | 1a657f78 | 2006-10-02 02:18:59 -0700 | [diff] [blame] | 1534 | ei->pid = get_task_pid(task, PIDTYPE_PID); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1535 | if (!ei->pid) |
| 1536 | goto out_unlock; |
| 1537 | |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1538 | if (task_dumpable(task)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1539 | rcu_read_lock(); |
| 1540 | cred = __task_cred(task); |
| 1541 | inode->i_uid = cred->euid; |
| 1542 | inode->i_gid = cred->egid; |
| 1543 | rcu_read_unlock(); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1544 | } |
| 1545 | security_task_to_inode(task, inode); |
| 1546 | |
| 1547 | out: |
| 1548 | return inode; |
| 1549 | |
| 1550 | out_unlock: |
| 1551 | iput(inode); |
| 1552 | return NULL; |
| 1553 | } |
| 1554 | |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 1555 | int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1556 | { |
| 1557 | struct inode *inode = dentry->d_inode; |
| 1558 | struct task_struct *task; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1559 | const struct cred *cred; |
Vasiliy Kulikov | 0499680 | 2012-01-10 15:11:31 -0800 | [diff] [blame] | 1560 | struct pid_namespace *pid = dentry->d_sb->s_fs_info; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1561 | |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1562 | generic_fillattr(inode, stat); |
| 1563 | |
| 1564 | rcu_read_lock(); |
Eric W. Biederman | dcb0f22 | 2012-02-09 08:48:21 -0800 | [diff] [blame] | 1565 | stat->uid = GLOBAL_ROOT_UID; |
| 1566 | stat->gid = GLOBAL_ROOT_GID; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1567 | task = pid_task(proc_pid(inode), PIDTYPE_PID); |
| 1568 | if (task) { |
Vasiliy Kulikov | 0499680 | 2012-01-10 15:11:31 -0800 | [diff] [blame] | 1569 | if (!has_pid_permissions(pid, task, 2)) { |
| 1570 | rcu_read_unlock(); |
| 1571 | /* |
| 1572 | * This doesn't prevent learning whether PID exists, |
| 1573 | * it only makes getattr() consistent with readdir(). |
| 1574 | */ |
| 1575 | return -ENOENT; |
| 1576 | } |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1577 | if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || |
| 1578 | task_dumpable(task)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1579 | cred = __task_cred(task); |
| 1580 | stat->uid = cred->euid; |
| 1581 | stat->gid = cred->egid; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1582 | } |
| 1583 | } |
| 1584 | rcu_read_unlock(); |
| 1585 | return 0; |
| 1586 | } |
| 1587 | |
| 1588 | /* dentry stuff */ |
| 1589 | |
| 1590 | /* |
| 1591 | * Exceptional case: normally we are not allowed to unhash a busy |
| 1592 | * directory. In this case, however, we can do it - no aliasing problems |
| 1593 | * due to the way we treat inodes. |
| 1594 | * |
| 1595 | * Rewrite the inode's ownerships here because the owning task may have |
| 1596 | * performed a setuid(), etc. |
| 1597 | * |
| 1598 | * Before the /proc/pid/status file was created the only way to read |
| 1599 | * the effective uid of a /process was to stat /proc/pid. Reading |
| 1600 | * /proc/pid/status is slow enough that procps and other packages |
| 1601 | * kept stating /proc/pid. To keep the rules in /proc simple I have |
| 1602 | * made this apply to all per process world readable and executable |
| 1603 | * directories. |
| 1604 | */ |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 1605 | int pid_revalidate(struct dentry *dentry, unsigned int flags) |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1606 | { |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 1607 | struct inode *inode; |
| 1608 | struct task_struct *task; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1609 | const struct cred *cred; |
| 1610 | |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 1611 | if (flags & LOOKUP_RCU) |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 1612 | return -ECHILD; |
| 1613 | |
| 1614 | inode = dentry->d_inode; |
| 1615 | task = get_proc_task(inode); |
| 1616 | |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1617 | if (task) { |
| 1618 | if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || |
| 1619 | task_dumpable(task)) { |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1620 | rcu_read_lock(); |
| 1621 | cred = __task_cred(task); |
| 1622 | inode->i_uid = cred->euid; |
| 1623 | inode->i_gid = cred->egid; |
| 1624 | rcu_read_unlock(); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1625 | } else { |
Eric W. Biederman | dcb0f22 | 2012-02-09 08:48:21 -0800 | [diff] [blame] | 1626 | inode->i_uid = GLOBAL_ROOT_UID; |
| 1627 | inode->i_gid = GLOBAL_ROOT_GID; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1628 | } |
| 1629 | inode->i_mode &= ~(S_ISUID | S_ISGID); |
| 1630 | security_task_to_inode(task, inode); |
| 1631 | put_task_struct(task); |
| 1632 | return 1; |
| 1633 | } |
| 1634 | d_drop(dentry); |
| 1635 | return 0; |
| 1636 | } |
| 1637 | |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 1638 | static inline bool proc_inode_is_dead(struct inode *inode) |
| 1639 | { |
| 1640 | return !proc_pid(inode)->tasks[PIDTYPE_PID].first; |
| 1641 | } |
| 1642 | |
David Howells | 1dd704b | 2013-04-12 01:08:50 +0100 | [diff] [blame] | 1643 | int pid_delete_dentry(const struct dentry *dentry) |
| 1644 | { |
| 1645 | /* Is the task we represent dead? |
| 1646 | * If so, then don't put the dentry on the lru list, |
| 1647 | * kill it immediately. |
| 1648 | */ |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 1649 | return proc_inode_is_dead(dentry->d_inode); |
David Howells | 1dd704b | 2013-04-12 01:08:50 +0100 | [diff] [blame] | 1650 | } |
| 1651 | |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 1652 | const struct dentry_operations pid_dentry_operations = |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1653 | { |
| 1654 | .d_revalidate = pid_revalidate, |
| 1655 | .d_delete = pid_delete_dentry, |
| 1656 | }; |
| 1657 | |
| 1658 | /* Lookups */ |
| 1659 | |
Eric W. Biederman | 1c0d04c | 2006-10-02 02:18:57 -0700 | [diff] [blame] | 1660 | /* |
| 1661 | * Fill a directory entry. |
| 1662 | * |
| 1663 | * If possible create the dcache entry and derive our inode number and |
| 1664 | * file type from dcache entry. |
| 1665 | * |
| 1666 | * Since all of the proc inode numbers are dynamically generated, the inode |
| 1667 | * numbers do not exist until the inode is cache. This means creating the |
| 1668 | * the dcache entry in readdir is necessary to keep the inode numbers |
| 1669 | * reported by readdir in sync with the inode numbers reported |
| 1670 | * by stat. |
| 1671 | */ |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 1672 | bool proc_fill_cache(struct file *file, struct dir_context *ctx, |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 1673 | const char *name, int len, |
Eric Dumazet | c5141e6 | 2007-05-08 00:26:15 -0700 | [diff] [blame] | 1674 | instantiate_t instantiate, struct task_struct *task, const void *ptr) |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 1675 | { |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 1676 | struct dentry *child, *dir = file->f_path.dentry; |
Al Viro | 1df98b8 | 2013-06-15 11:33:10 +0400 | [diff] [blame] | 1677 | struct qstr qname = QSTR_INIT(name, len); |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 1678 | struct inode *inode; |
Al Viro | 1df98b8 | 2013-06-15 11:33:10 +0400 | [diff] [blame] | 1679 | unsigned type; |
| 1680 | ino_t ino; |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 1681 | |
Al Viro | 1df98b8 | 2013-06-15 11:33:10 +0400 | [diff] [blame] | 1682 | child = d_hash_and_lookup(dir, &qname); |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 1683 | if (!child) { |
Al Viro | 1df98b8 | 2013-06-15 11:33:10 +0400 | [diff] [blame] | 1684 | child = d_alloc(dir, &qname); |
| 1685 | if (!child) |
| 1686 | goto end_instantiate; |
| 1687 | if (instantiate(dir->d_inode, child, task, ptr) < 0) { |
| 1688 | dput(child); |
| 1689 | goto end_instantiate; |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 1690 | } |
| 1691 | } |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 1692 | inode = child->d_inode; |
Al Viro | 147ce69 | 2013-06-15 10:26:35 +0400 | [diff] [blame] | 1693 | ino = inode->i_ino; |
| 1694 | type = inode->i_mode >> 12; |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 1695 | dput(child); |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 1696 | return dir_emit(ctx, name, len, ino, type); |
Al Viro | 1df98b8 | 2013-06-15 11:33:10 +0400 | [diff] [blame] | 1697 | |
| 1698 | end_instantiate: |
| 1699 | return dir_emit(ctx, name, len, 1, DT_UNKNOWN); |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 1700 | } |
| 1701 | |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1702 | #ifdef CONFIG_CHECKPOINT_RESTORE |
| 1703 | |
| 1704 | /* |
| 1705 | * dname_to_vma_addr - maps a dentry name into two unsigned longs |
| 1706 | * which represent vma start and end addresses. |
| 1707 | */ |
| 1708 | static int dname_to_vma_addr(struct dentry *dentry, |
| 1709 | unsigned long *start, unsigned long *end) |
| 1710 | { |
| 1711 | if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2) |
| 1712 | return -EINVAL; |
| 1713 | |
| 1714 | return 0; |
| 1715 | } |
| 1716 | |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 1717 | static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags) |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1718 | { |
| 1719 | unsigned long vm_start, vm_end; |
| 1720 | bool exact_vma_exists = false; |
| 1721 | struct mm_struct *mm = NULL; |
| 1722 | struct task_struct *task; |
| 1723 | const struct cred *cred; |
| 1724 | struct inode *inode; |
| 1725 | int status = 0; |
| 1726 | |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 1727 | if (flags & LOOKUP_RCU) |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1728 | return -ECHILD; |
| 1729 | |
| 1730 | if (!capable(CAP_SYS_ADMIN)) { |
Zhao Hongjiang | 4173581 | 2013-02-20 13:13:55 +1100 | [diff] [blame] | 1731 | status = -EPERM; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1732 | goto out_notask; |
| 1733 | } |
| 1734 | |
| 1735 | inode = dentry->d_inode; |
| 1736 | task = get_proc_task(inode); |
| 1737 | if (!task) |
| 1738 | goto out_notask; |
| 1739 | |
Cong Wang | 2344bec | 2012-05-31 16:26:18 -0700 | [diff] [blame] | 1740 | mm = mm_access(task, PTRACE_MODE_READ); |
| 1741 | if (IS_ERR_OR_NULL(mm)) |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1742 | goto out; |
| 1743 | |
| 1744 | if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) { |
| 1745 | down_read(&mm->mmap_sem); |
| 1746 | exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end); |
| 1747 | up_read(&mm->mmap_sem); |
| 1748 | } |
| 1749 | |
| 1750 | mmput(mm); |
| 1751 | |
| 1752 | if (exact_vma_exists) { |
| 1753 | if (task_dumpable(task)) { |
| 1754 | rcu_read_lock(); |
| 1755 | cred = __task_cred(task); |
| 1756 | inode->i_uid = cred->euid; |
| 1757 | inode->i_gid = cred->egid; |
| 1758 | rcu_read_unlock(); |
| 1759 | } else { |
Eric W. Biederman | dcb0f22 | 2012-02-09 08:48:21 -0800 | [diff] [blame] | 1760 | inode->i_uid = GLOBAL_ROOT_UID; |
| 1761 | inode->i_gid = GLOBAL_ROOT_GID; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1762 | } |
| 1763 | security_task_to_inode(task, inode); |
| 1764 | status = 1; |
| 1765 | } |
| 1766 | |
| 1767 | out: |
| 1768 | put_task_struct(task); |
| 1769 | |
| 1770 | out_notask: |
| 1771 | if (status <= 0) |
| 1772 | d_drop(dentry); |
| 1773 | |
| 1774 | return status; |
| 1775 | } |
| 1776 | |
| 1777 | static const struct dentry_operations tid_map_files_dentry_operations = { |
| 1778 | .d_revalidate = map_files_d_revalidate, |
| 1779 | .d_delete = pid_delete_dentry, |
| 1780 | }; |
| 1781 | |
| 1782 | static int proc_map_files_get_link(struct dentry *dentry, struct path *path) |
| 1783 | { |
| 1784 | unsigned long vm_start, vm_end; |
| 1785 | struct vm_area_struct *vma; |
| 1786 | struct task_struct *task; |
| 1787 | struct mm_struct *mm; |
| 1788 | int rc; |
| 1789 | |
| 1790 | rc = -ENOENT; |
| 1791 | task = get_proc_task(dentry->d_inode); |
| 1792 | if (!task) |
| 1793 | goto out; |
| 1794 | |
| 1795 | mm = get_task_mm(task); |
| 1796 | put_task_struct(task); |
| 1797 | if (!mm) |
| 1798 | goto out; |
| 1799 | |
| 1800 | rc = dname_to_vma_addr(dentry, &vm_start, &vm_end); |
| 1801 | if (rc) |
| 1802 | goto out_mmput; |
| 1803 | |
Artem Fetishev | 70335ab | 2014-03-10 15:49:45 -0700 | [diff] [blame] | 1804 | rc = -ENOENT; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1805 | down_read(&mm->mmap_sem); |
| 1806 | vma = find_exact_vma(mm, vm_start, vm_end); |
| 1807 | if (vma && vma->vm_file) { |
| 1808 | *path = vma->vm_file->f_path; |
| 1809 | path_get(path); |
| 1810 | rc = 0; |
| 1811 | } |
| 1812 | up_read(&mm->mmap_sem); |
| 1813 | |
| 1814 | out_mmput: |
| 1815 | mmput(mm); |
| 1816 | out: |
| 1817 | return rc; |
| 1818 | } |
| 1819 | |
| 1820 | struct map_files_info { |
Al Viro | 7b540d0 | 2012-08-27 14:55:26 -0400 | [diff] [blame] | 1821 | fmode_t mode; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1822 | unsigned long len; |
| 1823 | unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */ |
| 1824 | }; |
| 1825 | |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 1826 | static int |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1827 | proc_map_files_instantiate(struct inode *dir, struct dentry *dentry, |
| 1828 | struct task_struct *task, const void *ptr) |
| 1829 | { |
Al Viro | 7b540d0 | 2012-08-27 14:55:26 -0400 | [diff] [blame] | 1830 | fmode_t mode = (fmode_t)(unsigned long)ptr; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1831 | struct proc_inode *ei; |
| 1832 | struct inode *inode; |
| 1833 | |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1834 | inode = proc_pid_make_inode(dir->i_sb, task); |
| 1835 | if (!inode) |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 1836 | return -ENOENT; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1837 | |
| 1838 | ei = PROC_I(inode); |
| 1839 | ei->op.proc_get_link = proc_map_files_get_link; |
| 1840 | |
| 1841 | inode->i_op = &proc_pid_link_inode_operations; |
| 1842 | inode->i_size = 64; |
| 1843 | inode->i_mode = S_IFLNK; |
| 1844 | |
Al Viro | 7b540d0 | 2012-08-27 14:55:26 -0400 | [diff] [blame] | 1845 | if (mode & FMODE_READ) |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1846 | inode->i_mode |= S_IRUSR; |
Al Viro | 7b540d0 | 2012-08-27 14:55:26 -0400 | [diff] [blame] | 1847 | if (mode & FMODE_WRITE) |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1848 | inode->i_mode |= S_IWUSR; |
| 1849 | |
| 1850 | d_set_d_op(dentry, &tid_map_files_dentry_operations); |
| 1851 | d_add(dentry, inode); |
| 1852 | |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 1853 | return 0; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1854 | } |
| 1855 | |
| 1856 | static struct dentry *proc_map_files_lookup(struct inode *dir, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 1857 | struct dentry *dentry, unsigned int flags) |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1858 | { |
| 1859 | unsigned long vm_start, vm_end; |
| 1860 | struct vm_area_struct *vma; |
| 1861 | struct task_struct *task; |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 1862 | int result; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1863 | struct mm_struct *mm; |
| 1864 | |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 1865 | result = -EPERM; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1866 | if (!capable(CAP_SYS_ADMIN)) |
| 1867 | goto out; |
| 1868 | |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 1869 | result = -ENOENT; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1870 | task = get_proc_task(dir); |
| 1871 | if (!task) |
| 1872 | goto out; |
| 1873 | |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 1874 | result = -EACCES; |
Cyrill Gorcunov | eb94cd9 | 2012-05-17 17:03:25 -0700 | [diff] [blame] | 1875 | if (!ptrace_may_access(task, PTRACE_MODE_READ)) |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1876 | goto out_put_task; |
| 1877 | |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 1878 | result = -ENOENT; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1879 | if (dname_to_vma_addr(dentry, &vm_start, &vm_end)) |
Cyrill Gorcunov | eb94cd9 | 2012-05-17 17:03:25 -0700 | [diff] [blame] | 1880 | goto out_put_task; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1881 | |
| 1882 | mm = get_task_mm(task); |
| 1883 | if (!mm) |
Cyrill Gorcunov | eb94cd9 | 2012-05-17 17:03:25 -0700 | [diff] [blame] | 1884 | goto out_put_task; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1885 | |
| 1886 | down_read(&mm->mmap_sem); |
| 1887 | vma = find_exact_vma(mm, vm_start, vm_end); |
| 1888 | if (!vma) |
| 1889 | goto out_no_vma; |
| 1890 | |
Stanislav Kinsbursky | 05f5648 | 2012-11-26 16:29:42 -0800 | [diff] [blame] | 1891 | if (vma->vm_file) |
| 1892 | result = proc_map_files_instantiate(dir, dentry, task, |
| 1893 | (void *)(unsigned long)vma->vm_file->f_mode); |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1894 | |
| 1895 | out_no_vma: |
| 1896 | up_read(&mm->mmap_sem); |
| 1897 | mmput(mm); |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1898 | out_put_task: |
| 1899 | put_task_struct(task); |
| 1900 | out: |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 1901 | return ERR_PTR(result); |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1902 | } |
| 1903 | |
| 1904 | static const struct inode_operations proc_map_files_inode_operations = { |
| 1905 | .lookup = proc_map_files_lookup, |
| 1906 | .permission = proc_fd_permission, |
| 1907 | .setattr = proc_setattr, |
| 1908 | }; |
| 1909 | |
| 1910 | static int |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 1911 | proc_map_files_readdir(struct file *file, struct dir_context *ctx) |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1912 | { |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1913 | struct vm_area_struct *vma; |
| 1914 | struct task_struct *task; |
| 1915 | struct mm_struct *mm; |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 1916 | unsigned long nr_files, pos, i; |
| 1917 | struct flex_array *fa = NULL; |
| 1918 | struct map_files_info info; |
| 1919 | struct map_files_info *p; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1920 | int ret; |
| 1921 | |
Zhao Hongjiang | 4173581 | 2013-02-20 13:13:55 +1100 | [diff] [blame] | 1922 | ret = -EPERM; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1923 | if (!capable(CAP_SYS_ADMIN)) |
| 1924 | goto out; |
| 1925 | |
| 1926 | ret = -ENOENT; |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 1927 | task = get_proc_task(file_inode(file)); |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1928 | if (!task) |
| 1929 | goto out; |
| 1930 | |
| 1931 | ret = -EACCES; |
Cyrill Gorcunov | eb94cd9 | 2012-05-17 17:03:25 -0700 | [diff] [blame] | 1932 | if (!ptrace_may_access(task, PTRACE_MODE_READ)) |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1933 | goto out_put_task; |
| 1934 | |
| 1935 | ret = 0; |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 1936 | if (!dir_emit_dots(file, ctx)) |
| 1937 | goto out_put_task; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1938 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 1939 | mm = get_task_mm(task); |
| 1940 | if (!mm) |
| 1941 | goto out_put_task; |
| 1942 | down_read(&mm->mmap_sem); |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1943 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 1944 | nr_files = 0; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1945 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 1946 | /* |
| 1947 | * We need two passes here: |
| 1948 | * |
| 1949 | * 1) Collect vmas of mapped files with mmap_sem taken |
| 1950 | * 2) Release mmap_sem and instantiate entries |
| 1951 | * |
| 1952 | * otherwise we get lockdep complained, since filldir() |
| 1953 | * routine might require mmap_sem taken in might_fault(). |
| 1954 | */ |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1955 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 1956 | for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) { |
| 1957 | if (vma->vm_file && ++pos > ctx->pos) |
| 1958 | nr_files++; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1959 | } |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 1960 | |
| 1961 | if (nr_files) { |
| 1962 | fa = flex_array_alloc(sizeof(info), nr_files, |
| 1963 | GFP_KERNEL); |
| 1964 | if (!fa || flex_array_prealloc(fa, 0, nr_files, |
| 1965 | GFP_KERNEL)) { |
| 1966 | ret = -ENOMEM; |
| 1967 | if (fa) |
| 1968 | flex_array_free(fa); |
| 1969 | up_read(&mm->mmap_sem); |
| 1970 | mmput(mm); |
| 1971 | goto out_put_task; |
| 1972 | } |
| 1973 | for (i = 0, vma = mm->mmap, pos = 2; vma; |
| 1974 | vma = vma->vm_next) { |
| 1975 | if (!vma->vm_file) |
| 1976 | continue; |
| 1977 | if (++pos <= ctx->pos) |
| 1978 | continue; |
| 1979 | |
| 1980 | info.mode = vma->vm_file->f_mode; |
| 1981 | info.len = snprintf(info.name, |
| 1982 | sizeof(info.name), "%lx-%lx", |
| 1983 | vma->vm_start, vma->vm_end); |
| 1984 | if (flex_array_put(fa, i++, &info, GFP_KERNEL)) |
| 1985 | BUG(); |
| 1986 | } |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1987 | } |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 1988 | up_read(&mm->mmap_sem); |
| 1989 | |
| 1990 | for (i = 0; i < nr_files; i++) { |
| 1991 | p = flex_array_get(fa, i); |
| 1992 | if (!proc_fill_cache(file, ctx, |
| 1993 | p->name, p->len, |
| 1994 | proc_map_files_instantiate, |
| 1995 | task, |
| 1996 | (void *)(unsigned long)p->mode)) |
| 1997 | break; |
| 1998 | ctx->pos++; |
| 1999 | } |
| 2000 | if (fa) |
| 2001 | flex_array_free(fa); |
| 2002 | mmput(mm); |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2003 | |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2004 | out_put_task: |
| 2005 | put_task_struct(task); |
| 2006 | out: |
| 2007 | return ret; |
| 2008 | } |
| 2009 | |
| 2010 | static const struct file_operations proc_map_files_operations = { |
| 2011 | .read = generic_read_dir, |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2012 | .iterate = proc_map_files_readdir, |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2013 | .llseek = default_llseek, |
| 2014 | }; |
| 2015 | |
Pavel Emelyanov | 48f6a7a | 2013-03-11 13:12:45 +0400 | [diff] [blame] | 2016 | struct timers_private { |
| 2017 | struct pid *pid; |
| 2018 | struct task_struct *task; |
| 2019 | struct sighand_struct *sighand; |
Pavel Emelyanov | 57b8015 | 2013-03-11 13:13:08 +0400 | [diff] [blame] | 2020 | struct pid_namespace *ns; |
Pavel Emelyanov | 48f6a7a | 2013-03-11 13:12:45 +0400 | [diff] [blame] | 2021 | unsigned long flags; |
| 2022 | }; |
| 2023 | |
| 2024 | static void *timers_start(struct seq_file *m, loff_t *pos) |
| 2025 | { |
| 2026 | struct timers_private *tp = m->private; |
| 2027 | |
| 2028 | tp->task = get_pid_task(tp->pid, PIDTYPE_PID); |
| 2029 | if (!tp->task) |
| 2030 | return ERR_PTR(-ESRCH); |
| 2031 | |
| 2032 | tp->sighand = lock_task_sighand(tp->task, &tp->flags); |
| 2033 | if (!tp->sighand) |
| 2034 | return ERR_PTR(-ESRCH); |
| 2035 | |
| 2036 | return seq_list_start(&tp->task->signal->posix_timers, *pos); |
| 2037 | } |
| 2038 | |
| 2039 | static void *timers_next(struct seq_file *m, void *v, loff_t *pos) |
| 2040 | { |
| 2041 | struct timers_private *tp = m->private; |
| 2042 | return seq_list_next(v, &tp->task->signal->posix_timers, pos); |
| 2043 | } |
| 2044 | |
| 2045 | static void timers_stop(struct seq_file *m, void *v) |
| 2046 | { |
| 2047 | struct timers_private *tp = m->private; |
| 2048 | |
| 2049 | if (tp->sighand) { |
| 2050 | unlock_task_sighand(tp->task, &tp->flags); |
| 2051 | tp->sighand = NULL; |
| 2052 | } |
| 2053 | |
| 2054 | if (tp->task) { |
| 2055 | put_task_struct(tp->task); |
| 2056 | tp->task = NULL; |
| 2057 | } |
| 2058 | } |
| 2059 | |
| 2060 | static int show_timer(struct seq_file *m, void *v) |
| 2061 | { |
| 2062 | struct k_itimer *timer; |
Pavel Emelyanov | 57b8015 | 2013-03-11 13:13:08 +0400 | [diff] [blame] | 2063 | struct timers_private *tp = m->private; |
| 2064 | int notify; |
Alexey Dobriyan | cedbcca | 2014-08-08 14:21:33 -0700 | [diff] [blame] | 2065 | static const char * const nstr[] = { |
Pavel Emelyanov | 57b8015 | 2013-03-11 13:13:08 +0400 | [diff] [blame] | 2066 | [SIGEV_SIGNAL] = "signal", |
| 2067 | [SIGEV_NONE] = "none", |
| 2068 | [SIGEV_THREAD] = "thread", |
| 2069 | }; |
Pavel Emelyanov | 48f6a7a | 2013-03-11 13:12:45 +0400 | [diff] [blame] | 2070 | |
| 2071 | timer = list_entry((struct list_head *)v, struct k_itimer, list); |
Pavel Emelyanov | 57b8015 | 2013-03-11 13:13:08 +0400 | [diff] [blame] | 2072 | notify = timer->it_sigev_notify; |
| 2073 | |
Pavel Emelyanov | 48f6a7a | 2013-03-11 13:12:45 +0400 | [diff] [blame] | 2074 | seq_printf(m, "ID: %d\n", timer->it_id); |
Pavel Emelyanov | 57b8015 | 2013-03-11 13:13:08 +0400 | [diff] [blame] | 2075 | seq_printf(m, "signal: %d/%p\n", timer->sigq->info.si_signo, |
| 2076 | timer->sigq->info.si_value.sival_ptr); |
| 2077 | seq_printf(m, "notify: %s/%s.%d\n", |
| 2078 | nstr[notify & ~SIGEV_THREAD_ID], |
| 2079 | (notify & SIGEV_THREAD_ID) ? "tid" : "pid", |
| 2080 | pid_nr_ns(timer->it_pid, tp->ns)); |
Pavel Tikhomirov | 15ef029 | 2013-05-17 02:12:03 +0400 | [diff] [blame] | 2081 | seq_printf(m, "ClockID: %d\n", timer->it_clock); |
Pavel Emelyanov | 48f6a7a | 2013-03-11 13:12:45 +0400 | [diff] [blame] | 2082 | |
| 2083 | return 0; |
| 2084 | } |
| 2085 | |
| 2086 | static const struct seq_operations proc_timers_seq_ops = { |
| 2087 | .start = timers_start, |
| 2088 | .next = timers_next, |
| 2089 | .stop = timers_stop, |
| 2090 | .show = show_timer, |
| 2091 | }; |
| 2092 | |
| 2093 | static int proc_timers_open(struct inode *inode, struct file *file) |
| 2094 | { |
| 2095 | struct timers_private *tp; |
| 2096 | |
| 2097 | tp = __seq_open_private(file, &proc_timers_seq_ops, |
| 2098 | sizeof(struct timers_private)); |
| 2099 | if (!tp) |
| 2100 | return -ENOMEM; |
| 2101 | |
| 2102 | tp->pid = proc_pid(inode); |
Pavel Emelyanov | 57b8015 | 2013-03-11 13:13:08 +0400 | [diff] [blame] | 2103 | tp->ns = inode->i_sb->s_fs_info; |
Pavel Emelyanov | 48f6a7a | 2013-03-11 13:12:45 +0400 | [diff] [blame] | 2104 | return 0; |
| 2105 | } |
| 2106 | |
| 2107 | static const struct file_operations proc_timers_operations = { |
| 2108 | .open = proc_timers_open, |
| 2109 | .read = seq_read, |
| 2110 | .llseek = seq_lseek, |
| 2111 | .release = seq_release_private, |
| 2112 | }; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2113 | #endif /* CONFIG_CHECKPOINT_RESTORE */ |
| 2114 | |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 2115 | static int proc_pident_instantiate(struct inode *dir, |
Eric Dumazet | c5141e6 | 2007-05-08 00:26:15 -0700 | [diff] [blame] | 2116 | struct dentry *dentry, struct task_struct *task, const void *ptr) |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2117 | { |
Eric Dumazet | c5141e6 | 2007-05-08 00:26:15 -0700 | [diff] [blame] | 2118 | const struct pid_entry *p = ptr; |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2119 | struct inode *inode; |
| 2120 | struct proc_inode *ei; |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2121 | |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2122 | inode = proc_pid_make_inode(dir->i_sb, task); |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2123 | if (!inode) |
| 2124 | goto out; |
| 2125 | |
| 2126 | ei = PROC_I(inode); |
| 2127 | inode->i_mode = p->mode; |
| 2128 | if (S_ISDIR(inode->i_mode)) |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 2129 | set_nlink(inode, 2); /* Use getattr to fix if necessary */ |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2130 | if (p->iop) |
| 2131 | inode->i_op = p->iop; |
| 2132 | if (p->fop) |
| 2133 | inode->i_fop = p->fop; |
| 2134 | ei->op = p->op; |
Nick Piggin | fb045ad | 2011-01-07 17:49:55 +1100 | [diff] [blame] | 2135 | d_set_d_op(dentry, &pid_dentry_operations); |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2136 | d_add(dentry, inode); |
| 2137 | /* Close the race of the process dying before we return the dentry */ |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 2138 | if (pid_revalidate(dentry, 0)) |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 2139 | return 0; |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2140 | out: |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 2141 | return -ENOENT; |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2142 | } |
| 2143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | static struct dentry *proc_pident_lookup(struct inode *dir, |
| 2145 | struct dentry *dentry, |
Eric Dumazet | c5141e6 | 2007-05-08 00:26:15 -0700 | [diff] [blame] | 2146 | const struct pid_entry *ents, |
Eric W. Biederman | 7bcd6b0 | 2006-10-02 02:18:56 -0700 | [diff] [blame] | 2147 | unsigned int nents) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | { |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 2149 | int error; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2150 | struct task_struct *task = get_proc_task(dir); |
Eric Dumazet | c5141e6 | 2007-05-08 00:26:15 -0700 | [diff] [blame] | 2151 | const struct pid_entry *p, *last; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2152 | |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 2153 | error = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2155 | if (!task) |
| 2156 | goto out_no_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | |
Eric W. Biederman | 20cdc89 | 2006-10-02 02:17:07 -0700 | [diff] [blame] | 2158 | /* |
| 2159 | * Yes, it does not scale. And it should not. Don't add |
| 2160 | * new entries into /proc/<tgid>/ without very good reasons. |
| 2161 | */ |
Eric W. Biederman | 7bcd6b0 | 2006-10-02 02:18:56 -0700 | [diff] [blame] | 2162 | last = &ents[nents - 1]; |
| 2163 | for (p = ents; p <= last; p++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | if (p->len != dentry->d_name.len) |
| 2165 | continue; |
| 2166 | if (!memcmp(dentry->d_name.name, p->name, p->len)) |
| 2167 | break; |
| 2168 | } |
Eric W. Biederman | 7bcd6b0 | 2006-10-02 02:18:56 -0700 | [diff] [blame] | 2169 | if (p > last) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2170 | goto out; |
| 2171 | |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2172 | error = proc_pident_instantiate(dir, dentry, task, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | out: |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2174 | put_task_struct(task); |
| 2175 | out_no_task: |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 2176 | return ERR_PTR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | } |
| 2178 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2179 | static int proc_pident_readdir(struct file *file, struct dir_context *ctx, |
Eric Dumazet | c5141e6 | 2007-05-08 00:26:15 -0700 | [diff] [blame] | 2180 | const struct pid_entry *ents, unsigned int nents) |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2181 | { |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2182 | struct task_struct *task = get_proc_task(file_inode(file)); |
| 2183 | const struct pid_entry *p; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2184 | |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2185 | if (!task) |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2186 | return -ENOENT; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2187 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2188 | if (!dir_emit_dots(file, ctx)) |
| 2189 | goto out; |
| 2190 | |
| 2191 | if (ctx->pos >= nents + 2) |
| 2192 | goto out; |
| 2193 | |
| 2194 | for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) { |
| 2195 | if (!proc_fill_cache(file, ctx, p->name, p->len, |
| 2196 | proc_pident_instantiate, task, p)) |
| 2197 | break; |
| 2198 | ctx->pos++; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2199 | } |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2200 | out: |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2201 | put_task_struct(task); |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2202 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 | } |
| 2204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2205 | #ifdef CONFIG_SECURITY |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2206 | static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, |
| 2207 | size_t count, loff_t *ppos) |
| 2208 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2209 | struct inode * inode = file_inode(file); |
Al Viro | 04ff970 | 2007-03-12 16:17:58 +0000 | [diff] [blame] | 2210 | char *p = NULL; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2211 | ssize_t length; |
| 2212 | struct task_struct *task = get_proc_task(inode); |
| 2213 | |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2214 | if (!task) |
Al Viro | 04ff970 | 2007-03-12 16:17:58 +0000 | [diff] [blame] | 2215 | return -ESRCH; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2216 | |
| 2217 | length = security_getprocattr(task, |
Josef "Jeff" Sipek | 2fddfee | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 2218 | (char*)file->f_path.dentry->d_name.name, |
Al Viro | 04ff970 | 2007-03-12 16:17:58 +0000 | [diff] [blame] | 2219 | &p); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2220 | put_task_struct(task); |
Al Viro | 04ff970 | 2007-03-12 16:17:58 +0000 | [diff] [blame] | 2221 | if (length > 0) |
| 2222 | length = simple_read_from_buffer(buf, count, ppos, p, length); |
| 2223 | kfree(p); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2224 | return length; |
| 2225 | } |
| 2226 | |
| 2227 | static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, |
| 2228 | size_t count, loff_t *ppos) |
| 2229 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2230 | struct inode * inode = file_inode(file); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2231 | char *page; |
| 2232 | ssize_t length; |
| 2233 | struct task_struct *task = get_proc_task(inode); |
| 2234 | |
| 2235 | length = -ESRCH; |
| 2236 | if (!task) |
| 2237 | goto out_no_task; |
| 2238 | if (count > PAGE_SIZE) |
| 2239 | count = PAGE_SIZE; |
| 2240 | |
| 2241 | /* No partial writes. */ |
| 2242 | length = -EINVAL; |
| 2243 | if (*ppos != 0) |
| 2244 | goto out; |
| 2245 | |
| 2246 | length = -ENOMEM; |
Mel Gorman | e12ba74 | 2007-10-16 01:25:52 -0700 | [diff] [blame] | 2247 | page = (char*)__get_free_page(GFP_TEMPORARY); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2248 | if (!page) |
| 2249 | goto out; |
| 2250 | |
| 2251 | length = -EFAULT; |
| 2252 | if (copy_from_user(page, buf, count)) |
| 2253 | goto out_free; |
| 2254 | |
David Howells | 107db7c | 2009-05-08 13:55:27 +0100 | [diff] [blame] | 2255 | /* Guard against adverse ptrace interaction */ |
KOSAKI Motohiro | 9b1bf12 | 2010-10-27 15:34:08 -0700 | [diff] [blame] | 2256 | length = mutex_lock_interruptible(&task->signal->cred_guard_mutex); |
David Howells | 107db7c | 2009-05-08 13:55:27 +0100 | [diff] [blame] | 2257 | if (length < 0) |
| 2258 | goto out_free; |
| 2259 | |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2260 | length = security_setprocattr(task, |
Josef "Jeff" Sipek | 2fddfee | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 2261 | (char*)file->f_path.dentry->d_name.name, |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2262 | (void*)page, count); |
KOSAKI Motohiro | 9b1bf12 | 2010-10-27 15:34:08 -0700 | [diff] [blame] | 2263 | mutex_unlock(&task->signal->cred_guard_mutex); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2264 | out_free: |
| 2265 | free_page((unsigned long) page); |
| 2266 | out: |
| 2267 | put_task_struct(task); |
| 2268 | out_no_task: |
| 2269 | return length; |
| 2270 | } |
| 2271 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 2272 | static const struct file_operations proc_pid_attr_operations = { |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2273 | .read = proc_pid_attr_read, |
| 2274 | .write = proc_pid_attr_write, |
Arnd Bergmann | 87df842 | 2010-03-17 23:06:02 +0100 | [diff] [blame] | 2275 | .llseek = generic_file_llseek, |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2276 | }; |
| 2277 | |
Eric Dumazet | c5141e6 | 2007-05-08 00:26:15 -0700 | [diff] [blame] | 2278 | static const struct pid_entry attr_dir_stuff[] = { |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2279 | REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations), |
| 2280 | REG("prev", S_IRUGO, proc_pid_attr_operations), |
| 2281 | REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations), |
| 2282 | REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), |
| 2283 | REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), |
| 2284 | REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2285 | }; |
| 2286 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2287 | static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2288 | { |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2289 | return proc_pident_readdir(file, ctx, |
| 2290 | attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2291 | } |
| 2292 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 2293 | static const struct file_operations proc_attr_dir_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2294 | .read = generic_read_dir, |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2295 | .iterate = proc_attr_dir_readdir, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 2296 | .llseek = default_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2297 | }; |
| 2298 | |
Eric W. Biederman | 72d9dcf | 2006-10-02 02:18:50 -0700 | [diff] [blame] | 2299 | static struct dentry *proc_attr_dir_lookup(struct inode *dir, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 2300 | struct dentry *dentry, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2301 | { |
Eric W. Biederman | 7bcd6b0 | 2006-10-02 02:18:56 -0700 | [diff] [blame] | 2302 | return proc_pident_lookup(dir, dentry, |
| 2303 | attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2304 | } |
| 2305 | |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 2306 | static const struct inode_operations proc_attr_dir_inode_operations = { |
Eric W. Biederman | 72d9dcf | 2006-10-02 02:18:50 -0700 | [diff] [blame] | 2307 | .lookup = proc_attr_dir_lookup, |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2308 | .getattr = pid_getattr, |
Linus Torvalds | 6d76fa5 | 2006-07-15 12:26:45 -0700 | [diff] [blame] | 2309 | .setattr = proc_setattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | }; |
| 2311 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2312 | #endif |
| 2313 | |
Christoph Hellwig | 698ba7b | 2009-12-15 16:47:37 -0800 | [diff] [blame] | 2314 | #ifdef CONFIG_ELF_CORE |
Kawai, Hidehiro | 3cb4a0b | 2007-07-19 01:48:28 -0700 | [diff] [blame] | 2315 | static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf, |
| 2316 | size_t count, loff_t *ppos) |
| 2317 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2318 | struct task_struct *task = get_proc_task(file_inode(file)); |
Kawai, Hidehiro | 3cb4a0b | 2007-07-19 01:48:28 -0700 | [diff] [blame] | 2319 | struct mm_struct *mm; |
| 2320 | char buffer[PROC_NUMBUF]; |
| 2321 | size_t len; |
| 2322 | int ret; |
| 2323 | |
| 2324 | if (!task) |
| 2325 | return -ESRCH; |
| 2326 | |
| 2327 | ret = 0; |
| 2328 | mm = get_task_mm(task); |
| 2329 | if (mm) { |
| 2330 | len = snprintf(buffer, sizeof(buffer), "%08lx\n", |
| 2331 | ((mm->flags & MMF_DUMP_FILTER_MASK) >> |
| 2332 | MMF_DUMP_FILTER_SHIFT)); |
| 2333 | mmput(mm); |
| 2334 | ret = simple_read_from_buffer(buf, count, ppos, buffer, len); |
| 2335 | } |
| 2336 | |
| 2337 | put_task_struct(task); |
| 2338 | |
| 2339 | return ret; |
| 2340 | } |
| 2341 | |
| 2342 | static ssize_t proc_coredump_filter_write(struct file *file, |
| 2343 | const char __user *buf, |
| 2344 | size_t count, |
| 2345 | loff_t *ppos) |
| 2346 | { |
| 2347 | struct task_struct *task; |
| 2348 | struct mm_struct *mm; |
| 2349 | char buffer[PROC_NUMBUF], *end; |
| 2350 | unsigned int val; |
| 2351 | int ret; |
| 2352 | int i; |
| 2353 | unsigned long mask; |
| 2354 | |
| 2355 | ret = -EFAULT; |
| 2356 | memset(buffer, 0, sizeof(buffer)); |
| 2357 | if (count > sizeof(buffer) - 1) |
| 2358 | count = sizeof(buffer) - 1; |
| 2359 | if (copy_from_user(buffer, buf, count)) |
| 2360 | goto out_no_task; |
| 2361 | |
| 2362 | ret = -EINVAL; |
| 2363 | val = (unsigned int)simple_strtoul(buffer, &end, 0); |
| 2364 | if (*end == '\n') |
| 2365 | end++; |
| 2366 | if (end - buffer == 0) |
| 2367 | goto out_no_task; |
| 2368 | |
| 2369 | ret = -ESRCH; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2370 | task = get_proc_task(file_inode(file)); |
Kawai, Hidehiro | 3cb4a0b | 2007-07-19 01:48:28 -0700 | [diff] [blame] | 2371 | if (!task) |
| 2372 | goto out_no_task; |
| 2373 | |
| 2374 | ret = end - buffer; |
| 2375 | mm = get_task_mm(task); |
| 2376 | if (!mm) |
| 2377 | goto out_no_mm; |
| 2378 | |
| 2379 | for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) { |
| 2380 | if (val & mask) |
| 2381 | set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); |
| 2382 | else |
| 2383 | clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); |
| 2384 | } |
| 2385 | |
| 2386 | mmput(mm); |
| 2387 | out_no_mm: |
| 2388 | put_task_struct(task); |
| 2389 | out_no_task: |
| 2390 | return ret; |
| 2391 | } |
| 2392 | |
| 2393 | static const struct file_operations proc_coredump_filter_operations = { |
| 2394 | .read = proc_coredump_filter_read, |
| 2395 | .write = proc_coredump_filter_write, |
Arnd Bergmann | 87df842 | 2010-03-17 23:06:02 +0100 | [diff] [blame] | 2396 | .llseek = generic_file_llseek, |
Kawai, Hidehiro | 3cb4a0b | 2007-07-19 01:48:28 -0700 | [diff] [blame] | 2397 | }; |
| 2398 | #endif |
| 2399 | |
Andrew Morton | aba76fd | 2006-12-10 02:19:48 -0800 | [diff] [blame] | 2400 | #ifdef CONFIG_TASK_IO_ACCOUNTING |
Alexey Dobriyan | 19aadc9 | 2014-08-08 14:21:50 -0700 | [diff] [blame] | 2401 | static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole) |
Andrew Morton | aba76fd | 2006-12-10 02:19:48 -0800 | [diff] [blame] | 2402 | { |
Andrea Righi | 940389b | 2008-07-28 00:48:12 +0200 | [diff] [blame] | 2403 | struct task_io_accounting acct = task->ioac; |
Andrea Righi | 5995477 | 2008-07-27 17:29:15 +0200 | [diff] [blame] | 2404 | unsigned long flags; |
Vasiliy Kulikov | 293eb1e | 2011-07-26 16:08:38 -0700 | [diff] [blame] | 2405 | int result; |
Andrea Righi | 297c5d9 | 2008-07-25 01:48:49 -0700 | [diff] [blame] | 2406 | |
Vasiliy Kulikov | 293eb1e | 2011-07-26 16:08:38 -0700 | [diff] [blame] | 2407 | result = mutex_lock_killable(&task->signal->cred_guard_mutex); |
| 2408 | if (result) |
| 2409 | return result; |
| 2410 | |
| 2411 | if (!ptrace_may_access(task, PTRACE_MODE_READ)) { |
| 2412 | result = -EACCES; |
| 2413 | goto out_unlock; |
| 2414 | } |
Vasiliy Kulikov | 1d1221f | 2011-06-24 16:08:38 +0400 | [diff] [blame] | 2415 | |
Andrea Righi | 5995477 | 2008-07-27 17:29:15 +0200 | [diff] [blame] | 2416 | if (whole && lock_task_sighand(task, &flags)) { |
| 2417 | struct task_struct *t = task; |
Andrea Righi | b2d002d | 2008-07-26 15:22:27 -0700 | [diff] [blame] | 2418 | |
Andrea Righi | 5995477 | 2008-07-27 17:29:15 +0200 | [diff] [blame] | 2419 | task_io_accounting_add(&acct, &task->signal->ioac); |
| 2420 | while_each_thread(task, t) |
| 2421 | task_io_accounting_add(&acct, &t->ioac); |
Andrea Righi | 297c5d9 | 2008-07-25 01:48:49 -0700 | [diff] [blame] | 2422 | |
Andrea Righi | 5995477 | 2008-07-27 17:29:15 +0200 | [diff] [blame] | 2423 | unlock_task_sighand(task, &flags); |
Andrea Righi | 297c5d9 | 2008-07-25 01:48:49 -0700 | [diff] [blame] | 2424 | } |
Alexey Dobriyan | 19aadc9 | 2014-08-08 14:21:50 -0700 | [diff] [blame] | 2425 | result = seq_printf(m, |
Andrew Morton | aba76fd | 2006-12-10 02:19:48 -0800 | [diff] [blame] | 2426 | "rchar: %llu\n" |
| 2427 | "wchar: %llu\n" |
| 2428 | "syscr: %llu\n" |
| 2429 | "syscw: %llu\n" |
| 2430 | "read_bytes: %llu\n" |
| 2431 | "write_bytes: %llu\n" |
| 2432 | "cancelled_write_bytes: %llu\n", |
Alexander Beregalov | 7c44319 | 2008-08-05 13:01:34 -0700 | [diff] [blame] | 2433 | (unsigned long long)acct.rchar, |
| 2434 | (unsigned long long)acct.wchar, |
| 2435 | (unsigned long long)acct.syscr, |
| 2436 | (unsigned long long)acct.syscw, |
| 2437 | (unsigned long long)acct.read_bytes, |
| 2438 | (unsigned long long)acct.write_bytes, |
| 2439 | (unsigned long long)acct.cancelled_write_bytes); |
Vasiliy Kulikov | 293eb1e | 2011-07-26 16:08:38 -0700 | [diff] [blame] | 2440 | out_unlock: |
| 2441 | mutex_unlock(&task->signal->cred_guard_mutex); |
| 2442 | return result; |
Andrew Morton | aba76fd | 2006-12-10 02:19:48 -0800 | [diff] [blame] | 2443 | } |
Andrea Righi | 297c5d9 | 2008-07-25 01:48:49 -0700 | [diff] [blame] | 2444 | |
Alexey Dobriyan | 19aadc9 | 2014-08-08 14:21:50 -0700 | [diff] [blame] | 2445 | static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns, |
| 2446 | struct pid *pid, struct task_struct *task) |
Andrea Righi | 297c5d9 | 2008-07-25 01:48:49 -0700 | [diff] [blame] | 2447 | { |
Alexey Dobriyan | 19aadc9 | 2014-08-08 14:21:50 -0700 | [diff] [blame] | 2448 | return do_io_accounting(task, m, 0); |
Andrea Righi | 297c5d9 | 2008-07-25 01:48:49 -0700 | [diff] [blame] | 2449 | } |
| 2450 | |
Alexey Dobriyan | 19aadc9 | 2014-08-08 14:21:50 -0700 | [diff] [blame] | 2451 | static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns, |
| 2452 | struct pid *pid, struct task_struct *task) |
Andrea Righi | 297c5d9 | 2008-07-25 01:48:49 -0700 | [diff] [blame] | 2453 | { |
Alexey Dobriyan | 19aadc9 | 2014-08-08 14:21:50 -0700 | [diff] [blame] | 2454 | return do_io_accounting(task, m, 1); |
Andrea Righi | 297c5d9 | 2008-07-25 01:48:49 -0700 | [diff] [blame] | 2455 | } |
| 2456 | #endif /* CONFIG_TASK_IO_ACCOUNTING */ |
Andrew Morton | aba76fd | 2006-12-10 02:19:48 -0800 | [diff] [blame] | 2457 | |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 2458 | #ifdef CONFIG_USER_NS |
| 2459 | static int proc_id_map_open(struct inode *inode, struct file *file, |
Fabian Frederick | ccf94f1 | 2014-08-08 14:21:22 -0700 | [diff] [blame] | 2460 | const struct seq_operations *seq_ops) |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 2461 | { |
| 2462 | struct user_namespace *ns = NULL; |
| 2463 | struct task_struct *task; |
| 2464 | struct seq_file *seq; |
| 2465 | int ret = -EINVAL; |
| 2466 | |
| 2467 | task = get_proc_task(inode); |
| 2468 | if (task) { |
| 2469 | rcu_read_lock(); |
| 2470 | ns = get_user_ns(task_cred_xxx(task, user_ns)); |
| 2471 | rcu_read_unlock(); |
| 2472 | put_task_struct(task); |
| 2473 | } |
| 2474 | if (!ns) |
| 2475 | goto err; |
| 2476 | |
| 2477 | ret = seq_open(file, seq_ops); |
| 2478 | if (ret) |
| 2479 | goto err_put_ns; |
| 2480 | |
| 2481 | seq = file->private_data; |
| 2482 | seq->private = ns; |
| 2483 | |
| 2484 | return 0; |
| 2485 | err_put_ns: |
| 2486 | put_user_ns(ns); |
| 2487 | err: |
| 2488 | return ret; |
| 2489 | } |
| 2490 | |
| 2491 | static int proc_id_map_release(struct inode *inode, struct file *file) |
| 2492 | { |
| 2493 | struct seq_file *seq = file->private_data; |
| 2494 | struct user_namespace *ns = seq->private; |
| 2495 | put_user_ns(ns); |
| 2496 | return seq_release(inode, file); |
| 2497 | } |
| 2498 | |
| 2499 | static int proc_uid_map_open(struct inode *inode, struct file *file) |
| 2500 | { |
| 2501 | return proc_id_map_open(inode, file, &proc_uid_seq_operations); |
| 2502 | } |
| 2503 | |
| 2504 | static int proc_gid_map_open(struct inode *inode, struct file *file) |
| 2505 | { |
| 2506 | return proc_id_map_open(inode, file, &proc_gid_seq_operations); |
| 2507 | } |
| 2508 | |
Eric W. Biederman | f76d207 | 2012-08-30 01:24:05 -0700 | [diff] [blame] | 2509 | static int proc_projid_map_open(struct inode *inode, struct file *file) |
| 2510 | { |
| 2511 | return proc_id_map_open(inode, file, &proc_projid_seq_operations); |
| 2512 | } |
| 2513 | |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 2514 | static const struct file_operations proc_uid_map_operations = { |
| 2515 | .open = proc_uid_map_open, |
| 2516 | .write = proc_uid_map_write, |
| 2517 | .read = seq_read, |
| 2518 | .llseek = seq_lseek, |
| 2519 | .release = proc_id_map_release, |
| 2520 | }; |
| 2521 | |
| 2522 | static const struct file_operations proc_gid_map_operations = { |
| 2523 | .open = proc_gid_map_open, |
| 2524 | .write = proc_gid_map_write, |
| 2525 | .read = seq_read, |
| 2526 | .llseek = seq_lseek, |
| 2527 | .release = proc_id_map_release, |
| 2528 | }; |
Eric W. Biederman | f76d207 | 2012-08-30 01:24:05 -0700 | [diff] [blame] | 2529 | |
| 2530 | static const struct file_operations proc_projid_map_operations = { |
| 2531 | .open = proc_projid_map_open, |
| 2532 | .write = proc_projid_map_write, |
| 2533 | .read = seq_read, |
| 2534 | .llseek = seq_lseek, |
| 2535 | .release = proc_id_map_release, |
| 2536 | }; |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 2537 | #endif /* CONFIG_USER_NS */ |
| 2538 | |
Kees Cook | 4783072 | 2008-10-06 03:11:58 +0400 | [diff] [blame] | 2539 | static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns, |
| 2540 | struct pid *pid, struct task_struct *task) |
| 2541 | { |
Al Viro | a9712bc | 2011-03-23 15:52:50 -0400 | [diff] [blame] | 2542 | int err = lock_trace(task); |
| 2543 | if (!err) { |
| 2544 | seq_printf(m, "%08x\n", task->personality); |
| 2545 | unlock_trace(task); |
| 2546 | } |
| 2547 | return err; |
Kees Cook | 4783072 | 2008-10-06 03:11:58 +0400 | [diff] [blame] | 2548 | } |
| 2549 | |
Eric W. Biederman | 801199c | 2006-10-02 02:18:48 -0700 | [diff] [blame] | 2550 | /* |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2551 | * Thread groups |
| 2552 | */ |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 2553 | static const struct file_operations proc_task_operations; |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 2554 | static const struct inode_operations proc_task_inode_operations; |
Eric W. Biederman | 20cdc89 | 2006-10-02 02:17:07 -0700 | [diff] [blame] | 2555 | |
Eric Dumazet | c5141e6 | 2007-05-08 00:26:15 -0700 | [diff] [blame] | 2556 | static const struct pid_entry tgid_base_stuff[] = { |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2557 | DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations), |
| 2558 | DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations), |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2559 | #ifdef CONFIG_CHECKPOINT_RESTORE |
| 2560 | DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations), |
| 2561 | #endif |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2562 | DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations), |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 2563 | DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations), |
Andrew Morton | b2211a3 | 2008-03-11 18:03:35 -0700 | [diff] [blame] | 2564 | #ifdef CONFIG_NET |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2565 | DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations), |
Andrew Morton | b2211a3 | 2008-03-11 18:03:35 -0700 | [diff] [blame] | 2566 | #endif |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2567 | REG("environ", S_IRUSR, proc_environ_operations), |
Alexey Dobriyan | f9ea536 | 2014-08-08 14:21:35 -0700 | [diff] [blame] | 2568 | ONE("auxv", S_IRUSR, proc_pid_auxv), |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2569 | ONE("status", S_IRUGO, proc_pid_status), |
Djalal Harouni | 35a3504 | 2014-04-07 15:38:36 -0700 | [diff] [blame] | 2570 | ONE("personality", S_IRUSR, proc_pid_personality), |
Alexey Dobriyan | 1c963eb | 2014-08-08 14:21:37 -0700 | [diff] [blame] | 2571 | ONE("limits", S_IRUGO, proc_pid_limits), |
Ingo Molnar | 43ae34c | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 2572 | #ifdef CONFIG_SCHED_DEBUG |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2573 | REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations), |
Ingo Molnar | 43ae34c | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 2574 | #endif |
Mike Galbraith | 5091faa | 2010-11-30 14:18:03 +0100 | [diff] [blame] | 2575 | #ifdef CONFIG_SCHED_AUTOGROUP |
| 2576 | REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations), |
| 2577 | #endif |
john stultz | 4614a696b | 2009-12-14 18:00:05 -0800 | [diff] [blame] | 2578 | REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations), |
Roland McGrath | ebcb673 | 2008-07-25 19:46:00 -0700 | [diff] [blame] | 2579 | #ifdef CONFIG_HAVE_ARCH_TRACEHOOK |
Alexey Dobriyan | 09d93bd | 2014-08-08 14:21:39 -0700 | [diff] [blame] | 2580 | ONE("syscall", S_IRUSR, proc_pid_syscall), |
Roland McGrath | ebcb673 | 2008-07-25 19:46:00 -0700 | [diff] [blame] | 2581 | #endif |
Alexey Dobriyan | 2ca66ff | 2014-08-08 14:21:41 -0700 | [diff] [blame] | 2582 | ONE("cmdline", S_IRUGO, proc_pid_cmdline), |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2583 | ONE("stat", S_IRUGO, proc_tgid_stat), |
| 2584 | ONE("statm", S_IRUGO, proc_pid_statm), |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 2585 | REG("maps", S_IRUGO, proc_pid_maps_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2586 | #ifdef CONFIG_NUMA |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 2587 | REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2588 | #endif |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2589 | REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations), |
| 2590 | LNK("cwd", proc_cwd_link), |
| 2591 | LNK("root", proc_root_link), |
| 2592 | LNK("exe", proc_exe_link), |
| 2593 | REG("mounts", S_IRUGO, proc_mounts_operations), |
| 2594 | REG("mountinfo", S_IRUGO, proc_mountinfo_operations), |
| 2595 | REG("mountstats", S_IRUSR, proc_mountstats_operations), |
Matt Mackall | 1e88328 | 2008-02-04 22:29:07 -0800 | [diff] [blame] | 2596 | #ifdef CONFIG_PROC_PAGE_MONITOR |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2597 | REG("clear_refs", S_IWUSR, proc_clear_refs_operations), |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 2598 | REG("smaps", S_IRUGO, proc_pid_smaps_operations), |
Djalal Harouni | 32ed74a | 2014-04-07 15:38:38 -0700 | [diff] [blame] | 2599 | REG("pagemap", S_IRUSR, proc_pagemap_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2600 | #endif |
| 2601 | #ifdef CONFIG_SECURITY |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2602 | DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2603 | #endif |
| 2604 | #ifdef CONFIG_KALLSYMS |
Alexey Dobriyan | edfcd60 | 2014-08-08 14:21:44 -0700 | [diff] [blame] | 2605 | ONE("wchan", S_IRUGO, proc_pid_wchan), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2606 | #endif |
Ken Chen | 2ec220e | 2008-11-10 11:26:08 +0300 | [diff] [blame] | 2607 | #ifdef CONFIG_STACKTRACE |
Djalal Harouni | 35a3504 | 2014-04-07 15:38:36 -0700 | [diff] [blame] | 2608 | ONE("stack", S_IRUSR, proc_pid_stack), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2609 | #endif |
| 2610 | #ifdef CONFIG_SCHEDSTATS |
Alexey Dobriyan | f6e826c | 2014-08-08 14:21:46 -0700 | [diff] [blame] | 2611 | ONE("schedstat", S_IRUGO, proc_pid_schedstat), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2612 | #endif |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 2613 | #ifdef CONFIG_LATENCYTOP |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2614 | REG("latency", S_IRUGO, proc_lstats_operations), |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 2615 | #endif |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 2616 | #ifdef CONFIG_PROC_PID_CPUSET |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2617 | REG("cpuset", S_IRUGO, proc_cpuset_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2618 | #endif |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 2619 | #ifdef CONFIG_CGROUPS |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2620 | REG("cgroup", S_IRUGO, proc_cgroup_operations), |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 2621 | #endif |
Alexey Dobriyan | 6ba51e3 | 2014-08-08 14:21:48 -0700 | [diff] [blame] | 2622 | ONE("oom_score", S_IRUGO, proc_oom_score), |
David Rientjes | fa0cbbf | 2012-11-12 17:53:04 -0800 | [diff] [blame] | 2623 | REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations), |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 2624 | REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2625 | #ifdef CONFIG_AUDITSYSCALL |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2626 | REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations), |
| 2627 | REG("sessionid", S_IRUGO, proc_sessionid_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2628 | #endif |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 2629 | #ifdef CONFIG_FAULT_INJECTION |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2630 | REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations), |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 2631 | #endif |
Christoph Hellwig | 698ba7b | 2009-12-15 16:47:37 -0800 | [diff] [blame] | 2632 | #ifdef CONFIG_ELF_CORE |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2633 | REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations), |
Kawai, Hidehiro | 3cb4a0b | 2007-07-19 01:48:28 -0700 | [diff] [blame] | 2634 | #endif |
Andrew Morton | aba76fd | 2006-12-10 02:19:48 -0800 | [diff] [blame] | 2635 | #ifdef CONFIG_TASK_IO_ACCOUNTING |
Alexey Dobriyan | 19aadc9 | 2014-08-08 14:21:50 -0700 | [diff] [blame] | 2636 | ONE("io", S_IRUSR, proc_tgid_io_accounting), |
Andrew Morton | aba76fd | 2006-12-10 02:19:48 -0800 | [diff] [blame] | 2637 | #endif |
Chris Metcalf | f133ecc | 2011-05-26 12:40:09 -0400 | [diff] [blame] | 2638 | #ifdef CONFIG_HARDWALL |
Alexey Dobriyan | d962c14 | 2014-08-08 14:21:52 -0700 | [diff] [blame^] | 2639 | ONE("hardwall", S_IRUGO, proc_pid_hardwall), |
Chris Metcalf | f133ecc | 2011-05-26 12:40:09 -0400 | [diff] [blame] | 2640 | #endif |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 2641 | #ifdef CONFIG_USER_NS |
| 2642 | REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations), |
| 2643 | REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations), |
Eric W. Biederman | f76d207 | 2012-08-30 01:24:05 -0700 | [diff] [blame] | 2644 | REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations), |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 2645 | #endif |
Pavel Emelyanov | 48f6a7a | 2013-03-11 13:12:45 +0400 | [diff] [blame] | 2646 | #ifdef CONFIG_CHECKPOINT_RESTORE |
| 2647 | REG("timers", S_IRUGO, proc_timers_operations), |
| 2648 | #endif |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2649 | }; |
| 2650 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2651 | static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx) |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2652 | { |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2653 | return proc_pident_readdir(file, ctx, |
| 2654 | tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2655 | } |
| 2656 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 2657 | static const struct file_operations proc_tgid_base_operations = { |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2658 | .read = generic_read_dir, |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2659 | .iterate = proc_tgid_base_readdir, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 2660 | .llseek = default_llseek, |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2661 | }; |
| 2662 | |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 2663 | static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) |
| 2664 | { |
Eric W. Biederman | 7bcd6b0 | 2006-10-02 02:18:56 -0700 | [diff] [blame] | 2665 | return proc_pident_lookup(dir, dentry, |
| 2666 | tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2667 | } |
| 2668 | |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 2669 | static const struct inode_operations proc_tgid_base_inode_operations = { |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2670 | .lookup = proc_tgid_base_lookup, |
| 2671 | .getattr = pid_getattr, |
| 2672 | .setattr = proc_setattr, |
Vasiliy Kulikov | 0499680 | 2012-01-10 15:11:31 -0800 | [diff] [blame] | 2673 | .permission = proc_pid_permission, |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2674 | }; |
| 2675 | |
Pavel Emelyanov | 60347f6 | 2007-10-18 23:40:03 -0700 | [diff] [blame] | 2676 | static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2677 | { |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 2678 | struct dentry *dentry, *leader, *dir; |
Eric W. Biederman | 8578cea | 2006-06-26 00:25:54 -0700 | [diff] [blame] | 2679 | char buf[PROC_NUMBUF]; |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 2680 | struct qstr name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2681 | |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 2682 | name.name = buf; |
Pavel Emelyanov | 60347f6 | 2007-10-18 23:40:03 -0700 | [diff] [blame] | 2683 | name.len = snprintf(buf, sizeof(buf), "%d", pid); |
Al Viro | 4f522a2 | 2013-02-11 23:20:37 -0500 | [diff] [blame] | 2684 | /* no ->d_hash() rejects on procfs */ |
Pavel Emelyanov | 60347f6 | 2007-10-18 23:40:03 -0700 | [diff] [blame] | 2685 | dentry = d_hash_and_lookup(mnt->mnt_root, &name); |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 2686 | if (dentry) { |
Sukadev Bhattiprolu | 29f12ca | 2009-11-11 14:26:32 -0800 | [diff] [blame] | 2687 | shrink_dcache_parent(dentry); |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 2688 | d_drop(dentry); |
| 2689 | dput(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2690 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2691 | |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 2692 | name.name = buf; |
Pavel Emelyanov | 60347f6 | 2007-10-18 23:40:03 -0700 | [diff] [blame] | 2693 | name.len = snprintf(buf, sizeof(buf), "%d", tgid); |
| 2694 | leader = d_hash_and_lookup(mnt->mnt_root, &name); |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 2695 | if (!leader) |
| 2696 | goto out; |
| 2697 | |
| 2698 | name.name = "task"; |
| 2699 | name.len = strlen(name.name); |
| 2700 | dir = d_hash_and_lookup(leader, &name); |
| 2701 | if (!dir) |
| 2702 | goto out_put_leader; |
| 2703 | |
| 2704 | name.name = buf; |
Pavel Emelyanov | 60347f6 | 2007-10-18 23:40:03 -0700 | [diff] [blame] | 2705 | name.len = snprintf(buf, sizeof(buf), "%d", pid); |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 2706 | dentry = d_hash_and_lookup(dir, &name); |
| 2707 | if (dentry) { |
| 2708 | shrink_dcache_parent(dentry); |
| 2709 | d_drop(dentry); |
| 2710 | dput(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2711 | } |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 2712 | |
| 2713 | dput(dir); |
| 2714 | out_put_leader: |
| 2715 | dput(leader); |
| 2716 | out: |
| 2717 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2718 | } |
| 2719 | |
Randy Dunlap | 0895e91 | 2007-10-21 21:00:10 -0700 | [diff] [blame] | 2720 | /** |
| 2721 | * proc_flush_task - Remove dcache entries for @task from the /proc dcache. |
| 2722 | * @task: task that should be flushed. |
| 2723 | * |
| 2724 | * When flushing dentries from proc, one needs to flush them from global |
Pavel Emelyanov | 60347f6 | 2007-10-18 23:40:03 -0700 | [diff] [blame] | 2725 | * proc (proc_mnt) and from all the namespaces' procs this task was seen |
Randy Dunlap | 0895e91 | 2007-10-21 21:00:10 -0700 | [diff] [blame] | 2726 | * in. This call is supposed to do all of this job. |
| 2727 | * |
| 2728 | * Looks in the dcache for |
| 2729 | * /proc/@pid |
| 2730 | * /proc/@tgid/task/@pid |
| 2731 | * if either directory is present flushes it and all of it'ts children |
| 2732 | * from the dcache. |
| 2733 | * |
| 2734 | * It is safe and reasonable to cache /proc entries for a task until |
| 2735 | * that task exits. After that they just clog up the dcache with |
| 2736 | * useless entries, possibly causing useful dcache entries to be |
| 2737 | * flushed instead. This routine is proved to flush those useless |
| 2738 | * dcache entries at process exit time. |
| 2739 | * |
| 2740 | * NOTE: This routine is just an optimization so it does not guarantee |
| 2741 | * that no dcache entries will exist at process exit time it |
| 2742 | * just makes it very unlikely that any will persist. |
Pavel Emelyanov | 60347f6 | 2007-10-18 23:40:03 -0700 | [diff] [blame] | 2743 | */ |
| 2744 | |
| 2745 | void proc_flush_task(struct task_struct *task) |
| 2746 | { |
Eric W. Biederman | 9fcc2d1 | 2007-11-14 17:00:07 -0800 | [diff] [blame] | 2747 | int i; |
Oleg Nesterov | 9b4d1cbe | 2009-09-22 16:45:34 -0700 | [diff] [blame] | 2748 | struct pid *pid, *tgid; |
Pavel Emelyanov | 130f77e | 2007-10-18 23:40:11 -0700 | [diff] [blame] | 2749 | struct upid *upid; |
| 2750 | |
Pavel Emelyanov | 130f77e | 2007-10-18 23:40:11 -0700 | [diff] [blame] | 2751 | pid = task_pid(task); |
Oleg Nesterov | 9b4d1cbe | 2009-09-22 16:45:34 -0700 | [diff] [blame] | 2752 | tgid = task_tgid(task); |
Pavel Emelyanov | 130f77e | 2007-10-18 23:40:11 -0700 | [diff] [blame] | 2753 | |
Eric W. Biederman | 9fcc2d1 | 2007-11-14 17:00:07 -0800 | [diff] [blame] | 2754 | for (i = 0; i <= pid->level; i++) { |
Pavel Emelyanov | 130f77e | 2007-10-18 23:40:11 -0700 | [diff] [blame] | 2755 | upid = &pid->numbers[i]; |
| 2756 | proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr, |
Oleg Nesterov | 9b4d1cbe | 2009-09-22 16:45:34 -0700 | [diff] [blame] | 2757 | tgid->numbers[i].nr); |
Pavel Emelyanov | 130f77e | 2007-10-18 23:40:11 -0700 | [diff] [blame] | 2758 | } |
Pavel Emelyanov | 60347f6 | 2007-10-18 23:40:03 -0700 | [diff] [blame] | 2759 | } |
| 2760 | |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 2761 | static int proc_pid_instantiate(struct inode *dir, |
| 2762 | struct dentry * dentry, |
| 2763 | struct task_struct *task, const void *ptr) |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2764 | { |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2765 | struct inode *inode; |
| 2766 | |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2767 | inode = proc_pid_make_inode(dir->i_sb, task); |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2768 | if (!inode) |
| 2769 | goto out; |
| 2770 | |
| 2771 | inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; |
| 2772 | inode->i_op = &proc_tgid_base_inode_operations; |
| 2773 | inode->i_fop = &proc_tgid_base_operations; |
| 2774 | inode->i_flags|=S_IMMUTABLE; |
Vegard Nossum | aed5417 | 2008-06-05 22:46:53 -0700 | [diff] [blame] | 2775 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 2776 | set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff, |
| 2777 | ARRAY_SIZE(tgid_base_stuff))); |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2778 | |
Nick Piggin | fb045ad | 2011-01-07 17:49:55 +1100 | [diff] [blame] | 2779 | d_set_d_op(dentry, &pid_dentry_operations); |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2780 | |
| 2781 | d_add(dentry, inode); |
| 2782 | /* Close the race of the process dying before we return the dentry */ |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 2783 | if (pid_revalidate(dentry, 0)) |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 2784 | return 0; |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2785 | out: |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 2786 | return -ENOENT; |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2787 | } |
| 2788 | |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 2789 | struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2790 | { |
Alexey Dobriyan | 335eb53 | 2014-08-08 14:21:27 -0700 | [diff] [blame] | 2791 | int result = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2792 | struct task_struct *task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2793 | unsigned tgid; |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 2794 | struct pid_namespace *ns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2795 | |
Alexey Dobriyan | dbcdb50 | 2014-08-08 14:21:25 -0700 | [diff] [blame] | 2796 | tgid = name_to_int(&dentry->d_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2797 | if (tgid == ~0U) |
| 2798 | goto out; |
| 2799 | |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 2800 | ns = dentry->d_sb->s_fs_info; |
Eric W. Biederman | de75873 | 2006-06-26 00:25:51 -0700 | [diff] [blame] | 2801 | rcu_read_lock(); |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 2802 | task = find_task_by_pid_ns(tgid, ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2803 | if (task) |
| 2804 | get_task_struct(task); |
Eric W. Biederman | de75873 | 2006-06-26 00:25:51 -0700 | [diff] [blame] | 2805 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2806 | if (!task) |
| 2807 | goto out; |
| 2808 | |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2809 | result = proc_pid_instantiate(dir, dentry, task, NULL); |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 2810 | put_task_struct(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2811 | out: |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 2812 | return ERR_PTR(result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2813 | } |
| 2814 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2815 | /* |
Eric W. Biederman | 0804ef4 | 2006-10-02 02:17:04 -0700 | [diff] [blame] | 2816 | * Find the first task with tgid >= tgid |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 2817 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2818 | */ |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 2819 | struct tgid_iter { |
| 2820 | unsigned int tgid; |
Eric W. Biederman | 0804ef4 | 2006-10-02 02:17:04 -0700 | [diff] [blame] | 2821 | struct task_struct *task; |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 2822 | }; |
| 2823 | static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter) |
| 2824 | { |
Eric W. Biederman | 0804ef4 | 2006-10-02 02:17:04 -0700 | [diff] [blame] | 2825 | struct pid *pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2826 | |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 2827 | if (iter.task) |
| 2828 | put_task_struct(iter.task); |
Eric W. Biederman | 0804ef4 | 2006-10-02 02:17:04 -0700 | [diff] [blame] | 2829 | rcu_read_lock(); |
| 2830 | retry: |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 2831 | iter.task = NULL; |
| 2832 | pid = find_ge_pid(iter.tgid, ns); |
Eric W. Biederman | 0804ef4 | 2006-10-02 02:17:04 -0700 | [diff] [blame] | 2833 | if (pid) { |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 2834 | iter.tgid = pid_nr_ns(pid, ns); |
| 2835 | iter.task = pid_task(pid, PIDTYPE_PID); |
Eric W. Biederman | 0804ef4 | 2006-10-02 02:17:04 -0700 | [diff] [blame] | 2836 | /* What we to know is if the pid we have find is the |
| 2837 | * pid of a thread_group_leader. Testing for task |
| 2838 | * being a thread_group_leader is the obvious thing |
| 2839 | * todo but there is a window when it fails, due to |
| 2840 | * the pid transfer logic in de_thread. |
| 2841 | * |
| 2842 | * So we perform the straight forward test of seeing |
| 2843 | * if the pid we have found is the pid of a thread |
| 2844 | * group leader, and don't worry if the task we have |
| 2845 | * found doesn't happen to be a thread group leader. |
| 2846 | * As we don't care in the case of readdir. |
| 2847 | */ |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 2848 | if (!iter.task || !has_group_leader_pid(iter.task)) { |
| 2849 | iter.tgid += 1; |
Eric W. Biederman | 0804ef4 | 2006-10-02 02:17:04 -0700 | [diff] [blame] | 2850 | goto retry; |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 2851 | } |
| 2852 | get_task_struct(iter.task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2853 | } |
Eric W. Biederman | 454cc10 | 2006-06-26 00:25:51 -0700 | [diff] [blame] | 2854 | rcu_read_unlock(); |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 2855 | return iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2856 | } |
| 2857 | |
Al Viro | 021ada7 | 2013-03-29 19:27:05 -0400 | [diff] [blame] | 2858 | #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2859 | |
| 2860 | /* for the /proc/ directory itself, after non-process stuff has been done */ |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2861 | int proc_pid_readdir(struct file *file, struct dir_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2862 | { |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 2863 | struct tgid_iter iter; |
Al Viro | db96316 | 2013-06-15 10:45:10 +0400 | [diff] [blame] | 2864 | struct pid_namespace *ns = file->f_dentry->d_sb->s_fs_info; |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2865 | loff_t pos = ctx->pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2866 | |
Al Viro | 021ada7 | 2013-03-29 19:27:05 -0400 | [diff] [blame] | 2867 | if (pos >= PID_MAX_LIMIT + TGID_OFFSET) |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2868 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2869 | |
Al Viro | 021ada7 | 2013-03-29 19:27:05 -0400 | [diff] [blame] | 2870 | if (pos == TGID_OFFSET - 1) { |
Al Viro | db96316 | 2013-06-15 10:45:10 +0400 | [diff] [blame] | 2871 | struct inode *inode = ns->proc_self->d_inode; |
| 2872 | if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK)) |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2873 | return 0; |
Al Viro | 021ada7 | 2013-03-29 19:27:05 -0400 | [diff] [blame] | 2874 | iter.tgid = 0; |
| 2875 | } else { |
| 2876 | iter.tgid = pos - TGID_OFFSET; |
| 2877 | } |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 2878 | iter.task = NULL; |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 2879 | for (iter = next_tgid(ns, iter); |
| 2880 | iter.task; |
| 2881 | iter.tgid += 1, iter = next_tgid(ns, iter)) { |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2882 | char name[PROC_NUMBUF]; |
| 2883 | int len; |
| 2884 | if (!has_pid_permissions(ns, iter.task, 2)) |
| 2885 | continue; |
Vasiliy Kulikov | 0499680 | 2012-01-10 15:11:31 -0800 | [diff] [blame] | 2886 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2887 | len = snprintf(name, sizeof(name), "%d", iter.tgid); |
| 2888 | ctx->pos = iter.tgid + TGID_OFFSET; |
| 2889 | if (!proc_fill_cache(file, ctx, name, len, |
| 2890 | proc_pid_instantiate, iter.task, NULL)) { |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 2891 | put_task_struct(iter.task); |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2892 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2893 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2894 | } |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2895 | ctx->pos = PID_MAX_LIMIT + TGID_OFFSET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2896 | return 0; |
| 2897 | } |
| 2898 | |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 2899 | /* |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2900 | * Tasks |
| 2901 | */ |
Eric Dumazet | c5141e6 | 2007-05-08 00:26:15 -0700 | [diff] [blame] | 2902 | static const struct pid_entry tid_base_stuff[] = { |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2903 | DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations), |
Jerome Marchand | 3835541 | 2010-04-27 13:13:06 -0700 | [diff] [blame] | 2904 | DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations), |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 2905 | DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations), |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2906 | REG("environ", S_IRUSR, proc_environ_operations), |
Alexey Dobriyan | f9ea536 | 2014-08-08 14:21:35 -0700 | [diff] [blame] | 2907 | ONE("auxv", S_IRUSR, proc_pid_auxv), |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2908 | ONE("status", S_IRUGO, proc_pid_status), |
Djalal Harouni | 35a3504 | 2014-04-07 15:38:36 -0700 | [diff] [blame] | 2909 | ONE("personality", S_IRUSR, proc_pid_personality), |
Alexey Dobriyan | 1c963eb | 2014-08-08 14:21:37 -0700 | [diff] [blame] | 2910 | ONE("limits", S_IRUGO, proc_pid_limits), |
Ingo Molnar | 43ae34c | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 2911 | #ifdef CONFIG_SCHED_DEBUG |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2912 | REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations), |
Ingo Molnar | 43ae34c | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 2913 | #endif |
john stultz | 4614a696b | 2009-12-14 18:00:05 -0800 | [diff] [blame] | 2914 | REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations), |
Roland McGrath | ebcb673 | 2008-07-25 19:46:00 -0700 | [diff] [blame] | 2915 | #ifdef CONFIG_HAVE_ARCH_TRACEHOOK |
Alexey Dobriyan | 09d93bd | 2014-08-08 14:21:39 -0700 | [diff] [blame] | 2916 | ONE("syscall", S_IRUSR, proc_pid_syscall), |
Roland McGrath | ebcb673 | 2008-07-25 19:46:00 -0700 | [diff] [blame] | 2917 | #endif |
Alexey Dobriyan | 2ca66ff | 2014-08-08 14:21:41 -0700 | [diff] [blame] | 2918 | ONE("cmdline", S_IRUGO, proc_pid_cmdline), |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2919 | ONE("stat", S_IRUGO, proc_tid_stat), |
| 2920 | ONE("statm", S_IRUGO, proc_pid_statm), |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 2921 | REG("maps", S_IRUGO, proc_tid_maps_operations), |
Cyrill Gorcunov | 81841161 | 2012-05-31 16:26:43 -0700 | [diff] [blame] | 2922 | #ifdef CONFIG_CHECKPOINT_RESTORE |
| 2923 | REG("children", S_IRUGO, proc_tid_children_operations), |
| 2924 | #endif |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2925 | #ifdef CONFIG_NUMA |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 2926 | REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2927 | #endif |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2928 | REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations), |
| 2929 | LNK("cwd", proc_cwd_link), |
| 2930 | LNK("root", proc_root_link), |
| 2931 | LNK("exe", proc_exe_link), |
| 2932 | REG("mounts", S_IRUGO, proc_mounts_operations), |
| 2933 | REG("mountinfo", S_IRUGO, proc_mountinfo_operations), |
Matt Mackall | 1e88328 | 2008-02-04 22:29:07 -0800 | [diff] [blame] | 2934 | #ifdef CONFIG_PROC_PAGE_MONITOR |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2935 | REG("clear_refs", S_IWUSR, proc_clear_refs_operations), |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 2936 | REG("smaps", S_IRUGO, proc_tid_smaps_operations), |
Djalal Harouni | 32ed74a | 2014-04-07 15:38:38 -0700 | [diff] [blame] | 2937 | REG("pagemap", S_IRUSR, proc_pagemap_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2938 | #endif |
| 2939 | #ifdef CONFIG_SECURITY |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2940 | DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2941 | #endif |
| 2942 | #ifdef CONFIG_KALLSYMS |
Alexey Dobriyan | edfcd60 | 2014-08-08 14:21:44 -0700 | [diff] [blame] | 2943 | ONE("wchan", S_IRUGO, proc_pid_wchan), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2944 | #endif |
Ken Chen | 2ec220e | 2008-11-10 11:26:08 +0300 | [diff] [blame] | 2945 | #ifdef CONFIG_STACKTRACE |
Djalal Harouni | 35a3504 | 2014-04-07 15:38:36 -0700 | [diff] [blame] | 2946 | ONE("stack", S_IRUSR, proc_pid_stack), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2947 | #endif |
| 2948 | #ifdef CONFIG_SCHEDSTATS |
Alexey Dobriyan | f6e826c | 2014-08-08 14:21:46 -0700 | [diff] [blame] | 2949 | ONE("schedstat", S_IRUGO, proc_pid_schedstat), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2950 | #endif |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 2951 | #ifdef CONFIG_LATENCYTOP |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2952 | REG("latency", S_IRUGO, proc_lstats_operations), |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 2953 | #endif |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 2954 | #ifdef CONFIG_PROC_PID_CPUSET |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2955 | REG("cpuset", S_IRUGO, proc_cpuset_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2956 | #endif |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 2957 | #ifdef CONFIG_CGROUPS |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2958 | REG("cgroup", S_IRUGO, proc_cgroup_operations), |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 2959 | #endif |
Alexey Dobriyan | 6ba51e3 | 2014-08-08 14:21:48 -0700 | [diff] [blame] | 2960 | ONE("oom_score", S_IRUGO, proc_oom_score), |
David Rientjes | fa0cbbf | 2012-11-12 17:53:04 -0800 | [diff] [blame] | 2961 | REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations), |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 2962 | REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2963 | #ifdef CONFIG_AUDITSYSCALL |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2964 | REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations), |
Al Viro | 26ec3c6 | 2011-02-15 21:24:05 -0500 | [diff] [blame] | 2965 | REG("sessionid", S_IRUGO, proc_sessionid_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2966 | #endif |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 2967 | #ifdef CONFIG_FAULT_INJECTION |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2968 | REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations), |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 2969 | #endif |
Andrea Righi | 297c5d9 | 2008-07-25 01:48:49 -0700 | [diff] [blame] | 2970 | #ifdef CONFIG_TASK_IO_ACCOUNTING |
Alexey Dobriyan | 19aadc9 | 2014-08-08 14:21:50 -0700 | [diff] [blame] | 2971 | ONE("io", S_IRUSR, proc_tid_io_accounting), |
Andrea Righi | 297c5d9 | 2008-07-25 01:48:49 -0700 | [diff] [blame] | 2972 | #endif |
Chris Metcalf | f133ecc | 2011-05-26 12:40:09 -0400 | [diff] [blame] | 2973 | #ifdef CONFIG_HARDWALL |
Alexey Dobriyan | d962c14 | 2014-08-08 14:21:52 -0700 | [diff] [blame^] | 2974 | ONE("hardwall", S_IRUGO, proc_pid_hardwall), |
Chris Metcalf | f133ecc | 2011-05-26 12:40:09 -0400 | [diff] [blame] | 2975 | #endif |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 2976 | #ifdef CONFIG_USER_NS |
| 2977 | REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations), |
| 2978 | REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations), |
Eric W. Biederman | f76d207 | 2012-08-30 01:24:05 -0700 | [diff] [blame] | 2979 | REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations), |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 2980 | #endif |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2981 | }; |
| 2982 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2983 | static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx) |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2984 | { |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2985 | return proc_pident_readdir(file, ctx, |
| 2986 | tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2987 | } |
| 2988 | |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 2989 | static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) |
| 2990 | { |
Eric W. Biederman | 7bcd6b0 | 2006-10-02 02:18:56 -0700 | [diff] [blame] | 2991 | return proc_pident_lookup(dir, dentry, |
| 2992 | tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2993 | } |
| 2994 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 2995 | static const struct file_operations proc_tid_base_operations = { |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2996 | .read = generic_read_dir, |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2997 | .iterate = proc_tid_base_readdir, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 2998 | .llseek = default_llseek, |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2999 | }; |
| 3000 | |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 3001 | static const struct inode_operations proc_tid_base_inode_operations = { |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3002 | .lookup = proc_tid_base_lookup, |
| 3003 | .getattr = pid_getattr, |
| 3004 | .setattr = proc_setattr, |
| 3005 | }; |
| 3006 | |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 3007 | static int proc_task_instantiate(struct inode *dir, |
Eric Dumazet | c5141e6 | 2007-05-08 00:26:15 -0700 | [diff] [blame] | 3008 | struct dentry *dentry, struct task_struct *task, const void *ptr) |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 3009 | { |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 3010 | struct inode *inode; |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 3011 | inode = proc_pid_make_inode(dir->i_sb, task); |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 3012 | |
| 3013 | if (!inode) |
| 3014 | goto out; |
| 3015 | inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; |
| 3016 | inode->i_op = &proc_tid_base_inode_operations; |
| 3017 | inode->i_fop = &proc_tid_base_operations; |
| 3018 | inode->i_flags|=S_IMMUTABLE; |
Vegard Nossum | aed5417 | 2008-06-05 22:46:53 -0700 | [diff] [blame] | 3019 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 3020 | set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff, |
| 3021 | ARRAY_SIZE(tid_base_stuff))); |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 3022 | |
Nick Piggin | fb045ad | 2011-01-07 17:49:55 +1100 | [diff] [blame] | 3023 | d_set_d_op(dentry, &pid_dentry_operations); |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 3024 | |
| 3025 | d_add(dentry, inode); |
| 3026 | /* Close the race of the process dying before we return the dentry */ |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 3027 | if (pid_revalidate(dentry, 0)) |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 3028 | return 0; |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 3029 | out: |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 3030 | return -ENOENT; |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 3031 | } |
| 3032 | |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 3033 | static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3034 | { |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 3035 | int result = -ENOENT; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3036 | struct task_struct *task; |
| 3037 | struct task_struct *leader = get_proc_task(dir); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3038 | unsigned tid; |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 3039 | struct pid_namespace *ns; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3040 | |
| 3041 | if (!leader) |
| 3042 | goto out_no_task; |
| 3043 | |
Alexey Dobriyan | dbcdb50 | 2014-08-08 14:21:25 -0700 | [diff] [blame] | 3044 | tid = name_to_int(&dentry->d_name); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3045 | if (tid == ~0U) |
| 3046 | goto out; |
| 3047 | |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 3048 | ns = dentry->d_sb->s_fs_info; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3049 | rcu_read_lock(); |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 3050 | task = find_task_by_pid_ns(tid, ns); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3051 | if (task) |
| 3052 | get_task_struct(task); |
| 3053 | rcu_read_unlock(); |
| 3054 | if (!task) |
| 3055 | goto out; |
Pavel Emelyanov | bac0abd | 2007-10-18 23:40:18 -0700 | [diff] [blame] | 3056 | if (!same_thread_group(leader, task)) |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3057 | goto out_drop_task; |
| 3058 | |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 3059 | result = proc_task_instantiate(dir, dentry, task, NULL); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3060 | out_drop_task: |
| 3061 | put_task_struct(task); |
| 3062 | out: |
| 3063 | put_task_struct(leader); |
| 3064 | out_no_task: |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 3065 | return ERR_PTR(result); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3066 | } |
| 3067 | |
| 3068 | /* |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3069 | * Find the first tid of a thread group to return to user space. |
| 3070 | * |
| 3071 | * Usually this is just the thread group leader, but if the users |
| 3072 | * buffer was too small or there was a seek into the middle of the |
| 3073 | * directory we have more work todo. |
| 3074 | * |
| 3075 | * In the case of a short read we start with find_task_by_pid. |
| 3076 | * |
| 3077 | * In the case of a seek we start with the leader and walk nr |
| 3078 | * threads past it. |
| 3079 | */ |
Oleg Nesterov | 9f6e963 | 2014-01-23 15:55:40 -0800 | [diff] [blame] | 3080 | static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos, |
| 3081 | struct pid_namespace *ns) |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3082 | { |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 3083 | struct task_struct *pos, *task; |
Oleg Nesterov | 9f6e963 | 2014-01-23 15:55:40 -0800 | [diff] [blame] | 3084 | unsigned long nr = f_pos; |
| 3085 | |
| 3086 | if (nr != f_pos) /* 32bit overflow? */ |
| 3087 | return NULL; |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3088 | |
Eric W. Biederman | cc28873 | 2006-06-26 00:26:01 -0700 | [diff] [blame] | 3089 | rcu_read_lock(); |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 3090 | task = pid_task(pid, PIDTYPE_PID); |
| 3091 | if (!task) |
| 3092 | goto fail; |
| 3093 | |
| 3094 | /* Attempt to start with the tid of a thread */ |
Oleg Nesterov | 9f6e963 | 2014-01-23 15:55:40 -0800 | [diff] [blame] | 3095 | if (tid && nr) { |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 3096 | pos = find_task_by_pid_ns(tid, ns); |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 3097 | if (pos && same_thread_group(pos, task)) |
Oleg Nesterov | a872ff0 | 2006-06-26 00:26:01 -0700 | [diff] [blame] | 3098 | goto found; |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3099 | } |
| 3100 | |
| 3101 | /* If nr exceeds the number of threads there is nothing todo */ |
Oleg Nesterov | 9f6e963 | 2014-01-23 15:55:40 -0800 | [diff] [blame] | 3102 | if (nr >= get_nr_threads(task)) |
Oleg Nesterov | c986c14 | 2014-01-23 15:55:38 -0800 | [diff] [blame] | 3103 | goto fail; |
Oleg Nesterov | a872ff0 | 2006-06-26 00:26:01 -0700 | [diff] [blame] | 3104 | |
| 3105 | /* If we haven't found our starting place yet start |
| 3106 | * with the leader and walk nr threads forward. |
| 3107 | */ |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 3108 | pos = task = task->group_leader; |
Oleg Nesterov | c986c14 | 2014-01-23 15:55:38 -0800 | [diff] [blame] | 3109 | do { |
Oleg Nesterov | 9f6e963 | 2014-01-23 15:55:40 -0800 | [diff] [blame] | 3110 | if (!nr--) |
Oleg Nesterov | c986c14 | 2014-01-23 15:55:38 -0800 | [diff] [blame] | 3111 | goto found; |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 3112 | } while_each_thread(task, pos); |
Oleg Nesterov | c986c14 | 2014-01-23 15:55:38 -0800 | [diff] [blame] | 3113 | fail: |
| 3114 | pos = NULL; |
| 3115 | goto out; |
Oleg Nesterov | a872ff0 | 2006-06-26 00:26:01 -0700 | [diff] [blame] | 3116 | found: |
| 3117 | get_task_struct(pos); |
| 3118 | out: |
Eric W. Biederman | cc28873 | 2006-06-26 00:26:01 -0700 | [diff] [blame] | 3119 | rcu_read_unlock(); |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3120 | return pos; |
| 3121 | } |
| 3122 | |
| 3123 | /* |
| 3124 | * Find the next thread in the thread list. |
| 3125 | * Return NULL if there is an error or no next thread. |
| 3126 | * |
| 3127 | * The reference to the input task_struct is released. |
| 3128 | */ |
| 3129 | static struct task_struct *next_tid(struct task_struct *start) |
| 3130 | { |
Oleg Nesterov | c1df7fb | 2006-06-26 00:26:02 -0700 | [diff] [blame] | 3131 | struct task_struct *pos = NULL; |
Eric W. Biederman | cc28873 | 2006-06-26 00:26:01 -0700 | [diff] [blame] | 3132 | rcu_read_lock(); |
Oleg Nesterov | c1df7fb | 2006-06-26 00:26:02 -0700 | [diff] [blame] | 3133 | if (pid_alive(start)) { |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3134 | pos = next_thread(start); |
Oleg Nesterov | c1df7fb | 2006-06-26 00:26:02 -0700 | [diff] [blame] | 3135 | if (thread_group_leader(pos)) |
| 3136 | pos = NULL; |
| 3137 | else |
| 3138 | get_task_struct(pos); |
| 3139 | } |
Eric W. Biederman | cc28873 | 2006-06-26 00:26:01 -0700 | [diff] [blame] | 3140 | rcu_read_unlock(); |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3141 | put_task_struct(start); |
| 3142 | return pos; |
| 3143 | } |
| 3144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3145 | /* for the /proc/TGID/task/ directories */ |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3146 | static int proc_task_readdir(struct file *file, struct dir_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3147 | { |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 3148 | struct inode *inode = file_inode(file); |
| 3149 | struct task_struct *task; |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 3150 | struct pid_namespace *ns; |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3151 | int tid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3152 | |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 3153 | if (proc_inode_is_dead(inode)) |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3154 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3155 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3156 | if (!dir_emit_dots(file, ctx)) |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 3157 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3158 | |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3159 | /* f_version caches the tgid value that the last readdir call couldn't |
| 3160 | * return. lseek aka telldir automagically resets f_version to 0. |
| 3161 | */ |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3162 | ns = file->f_dentry->d_sb->s_fs_info; |
| 3163 | tid = (int)file->f_version; |
| 3164 | file->f_version = 0; |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 3165 | for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns); |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3166 | task; |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3167 | task = next_tid(task), ctx->pos++) { |
| 3168 | char name[PROC_NUMBUF]; |
| 3169 | int len; |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 3170 | tid = task_pid_nr_ns(task, ns); |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3171 | len = snprintf(name, sizeof(name), "%d", tid); |
| 3172 | if (!proc_fill_cache(file, ctx, name, len, |
| 3173 | proc_task_instantiate, task, NULL)) { |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3174 | /* returning this tgid failed, save it as the first |
| 3175 | * pid for the next readir call */ |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3176 | file->f_version = (u64)tid; |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3177 | put_task_struct(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3178 | break; |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3179 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3180 | } |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 3181 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3182 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3183 | } |
Eric W. Biederman | 6e66b52 | 2006-06-26 00:25:47 -0700 | [diff] [blame] | 3184 | |
| 3185 | static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) |
| 3186 | { |
| 3187 | struct inode *inode = dentry->d_inode; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 3188 | struct task_struct *p = get_proc_task(inode); |
Eric W. Biederman | 6e66b52 | 2006-06-26 00:25:47 -0700 | [diff] [blame] | 3189 | generic_fillattr(inode, stat); |
| 3190 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 3191 | if (p) { |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 3192 | stat->nlink += get_nr_threads(p); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 3193 | put_task_struct(p); |
Eric W. Biederman | 6e66b52 | 2006-06-26 00:25:47 -0700 | [diff] [blame] | 3194 | } |
| 3195 | |
| 3196 | return 0; |
| 3197 | } |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3198 | |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 3199 | static const struct inode_operations proc_task_inode_operations = { |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3200 | .lookup = proc_task_lookup, |
| 3201 | .getattr = proc_task_getattr, |
| 3202 | .setattr = proc_setattr, |
Vasiliy Kulikov | 0499680 | 2012-01-10 15:11:31 -0800 | [diff] [blame] | 3203 | .permission = proc_pid_permission, |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3204 | }; |
| 3205 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 3206 | static const struct file_operations proc_task_operations = { |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3207 | .read = generic_read_dir, |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3208 | .iterate = proc_task_readdir, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 3209 | .llseek = default_llseek, |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3210 | }; |