~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/proc/base.c

Version: ~ [ linux-5.3-rc1 ] ~ [ linux-5.2.2 ] ~ [ linux-5.1.19 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.60 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.134 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.186 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.186 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.70 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp