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