Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  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/init.h>
 57 #include <linux/capability.h>
 58 #include <linux/file.h>
 59 #include <linux/string.h>
 60 #include <linux/seq_file.h>
 61 #include <linux/namei.h>
 62 #include <linux/mnt_namespace.h>
 63 #include <linux/mm.h>
 64 #include <linux/rcupdate.h>
 65 #include <linux/kallsyms.h>
 66 #include <linux/resource.h>
 67 #include <linux/module.h>
 68 #include <linux/mount.h>
 69 #include <linux/security.h>
 70 #include <linux/ptrace.h>
 71 #include <linux/cgroup.h>
 72 #include <linux/cpuset.h>
 73 #include <linux/audit.h>
 74 #include <linux/poll.h>
 75 #include <linux/nsproxy.h>
 76 #include <linux/oom.h>
 77 #include <linux/elf.h>
 78 #include <linux/pid_namespace.h>
 79 #include "internal.h"
 80 
 81 /* NOTE:
 82  *      Implementing inode permission operations in /proc is almost
 83  *      certainly an error.  Permission checks need to happen during
 84  *      each system call not at open time.  The reason is that most of
 85  *      what we wish to check for permissions in /proc varies at runtime.
 86  *
 87  *      The classic example of a problem is opening file descriptors
 88  *      in /proc for a task before it execs a suid executable.
 89  */
 90 
 91 struct pid_entry {
 92         char *name;
 93         int len;
 94         mode_t mode;
 95         const struct inode_operations *iop;
 96         const struct file_operations *fop;
 97         union proc_op op;
 98 };
 99 
100 #define NOD(NAME, MODE, IOP, FOP, OP) {                 \
101         .name = (NAME),                                 \
102         .len  = sizeof(NAME) - 1,                       \
103         .mode = MODE,                                   \
104         .iop  = IOP,                                    \
105         .fop  = FOP,                                    \
106         .op   = OP,                                     \
107 }
108 
109 #define DIR(NAME, MODE, OTYPE)                                                  \
110         NOD(NAME, (S_IFDIR|(MODE)),                                             \
111                 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations,   \
112                 {} )
113 #define LNK(NAME, OTYPE)                                        \
114         NOD(NAME, (S_IFLNK|S_IRWXUGO),                          \
115                 &proc_pid_link_inode_operations, NULL,          \
116                 { .proc_get_link = &proc_##OTYPE##_link } )
117 #define REG(NAME, MODE, OTYPE)                          \
118         NOD(NAME, (S_IFREG|(MODE)), NULL,               \
119                 &proc_##OTYPE##_operations, {})
120 #define INF(NAME, MODE, OTYPE)                          \
121         NOD(NAME, (S_IFREG|(MODE)),                     \
122                 NULL, &proc_info_file_operations,       \
123                 { .proc_read = &proc_##OTYPE } )
124 #define ONE(NAME, MODE, OTYPE)                          \
125         NOD(NAME, (S_IFREG|(MODE)),                     \
126                 NULL, &proc_single_file_operations,     \
127                 { .proc_show = &proc_##OTYPE } )
128 
129 /*
130  * Count the number of hardlinks for the pid_entry table, excluding the .
131  * and .. links.
132  */
133 static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
134         unsigned int n)
135 {
136         unsigned int i;
137         unsigned int count;
138 
139         count = 0;
140         for (i = 0; i < n; ++i) {
141                 if (S_ISDIR(entries[i].mode))
142                         ++count;
143         }
144 
145         return count;
146 }
147 
148 int maps_protect;
149 EXPORT_SYMBOL(maps_protect);
150 
151 static struct fs_struct *get_fs_struct(struct task_struct *task)
152 {
153         struct fs_struct *fs;
154         task_lock(task);
155         fs = task->fs;
156         if(fs)
157                 atomic_inc(&fs->count);
158         task_unlock(task);
159         return fs;
160 }
161 
162 static int get_nr_threads(struct task_struct *tsk)
163 {
164         /* Must be called with the rcu_read_lock held */
165         unsigned long flags;
166         int count = 0;
167 
168         if (lock_task_sighand(tsk, &flags)) {
169                 count = atomic_read(&tsk->signal->count);
170                 unlock_task_sighand(tsk, &flags);
171         }
172         return count;
173 }
174 
175 static int proc_cwd_link(struct inode *inode, struct path *path)
176 {
177         struct task_struct *task = get_proc_task(inode);
178         struct fs_struct *fs = NULL;
179         int result = -ENOENT;
180 
181         if (task) {
182                 fs = get_fs_struct(task);
183                 put_task_struct(task);
184         }
185         if (fs) {
186                 read_lock(&fs->lock);
187                 *path = fs->pwd;
188                 path_get(&fs->pwd);
189                 read_unlock(&fs->lock);
190                 result = 0;
191                 put_fs_struct(fs);
192         }
193         return result;
194 }
195 
196 static int proc_root_link(struct inode *inode, struct path *path)
197 {
198         struct task_struct *task = get_proc_task(inode);
199         struct fs_struct *fs = NULL;
200         int result = -ENOENT;
201 
202         if (task) {
203                 fs = get_fs_struct(task);
204                 put_task_struct(task);
205         }
206         if (fs) {
207                 read_lock(&fs->lock);
208                 *path = fs->root;
209                 path_get(&fs->root);
210                 read_unlock(&fs->lock);
211                 result = 0;
212                 put_fs_struct(fs);
213         }
214         return result;
215 }
216 
217 #define MAY_PTRACE(task) \
218         (task == current || \
219         (task->parent == current && \
220         (task->ptrace & PT_PTRACED) && \
221          (task_is_stopped_or_traced(task)) && \
222          security_ptrace(current,task) == 0))
223 
224 struct mm_struct *mm_for_maps(struct task_struct *task)
225 {
226         struct mm_struct *mm = get_task_mm(task);
227         if (!mm)
228                 return NULL;
229         down_read(&mm->mmap_sem);
230         task_lock(task);
231         if (task->mm != mm)
232                 goto out;
233         if (task->mm != current->mm && __ptrace_may_attach(task) < 0)
234                 goto out;
235         task_unlock(task);
236         return mm;
237 out:
238         task_unlock(task);
239         up_read(&mm->mmap_sem);
240         mmput(mm);
241         return NULL;
242 }
243 
244 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
245 {
246         int res = 0;
247         unsigned int len;
248         struct mm_struct *mm = get_task_mm(task);
249         if (!mm)
250                 goto out;
251         if (!mm->arg_end)
252                 goto out_mm;    /* Shh! No looking before we're done */
253 
254         len = mm->arg_end - mm->arg_start;
255  
256         if (len > PAGE_SIZE)
257                 len = PAGE_SIZE;
258  
259         res = access_process_vm(task, mm->arg_start, buffer, len, 0);
260 
261         // If the nul at the end of args has been overwritten, then
262         // assume application is using setproctitle(3).
263         if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
264                 len = strnlen(buffer, res);
265                 if (len < res) {
266                     res = len;
267                 } else {
268                         len = mm->env_end - mm->env_start;
269                         if (len > PAGE_SIZE - res)
270                                 len = PAGE_SIZE - res;
271                         res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
272                         res = strnlen(buffer, res);
273                 }
274         }
275 out_mm:
276         mmput(mm);
277 out:
278         return res;
279 }
280 
281 static int proc_pid_auxv(struct task_struct *task, char *buffer)
282 {
283         int res = 0;
284         struct mm_struct *mm = get_task_mm(task);
285         if (mm) {
286                 unsigned int nwords = 0;
287                 do
288                         nwords += 2;
289                 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
290                 res = nwords * sizeof(mm->saved_auxv[0]);
291                 if (res > PAGE_SIZE)
292                         res = PAGE_SIZE;
293                 memcpy(buffer, mm->saved_auxv, res);
294                 mmput(mm);
295         }
296         return res;
297 }
298 
299 
300 #ifdef CONFIG_KALLSYMS
301 /*
302  * Provides a wchan file via kallsyms in a proper one-value-per-file format.
303  * Returns the resolved symbol.  If that fails, simply return the address.
304  */
305 static int proc_pid_wchan(struct task_struct *task, char *buffer)
306 {
307         unsigned long wchan;
308         char symname[KSYM_NAME_LEN];
309 
310         wchan = get_wchan(task);
311 
312         if (lookup_symbol_name(wchan, symname) < 0)
313                 return sprintf(buffer, "%lu", wchan);
314         else
315                 return sprintf(buffer, "%s", symname);
316 }
317 #endif /* CONFIG_KALLSYMS */
318 
319 #ifdef CONFIG_SCHEDSTATS
320 /*
321  * Provides /proc/PID/schedstat
322  */
323 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
324 {
325         return sprintf(buffer, "%llu %llu %lu\n",
326                         task->sched_info.cpu_time,
327                         task->sched_info.run_delay,
328                         task->sched_info.pcount);
329 }
330 #endif
331 
332 #ifdef CONFIG_LATENCYTOP
333 static int lstats_show_proc(struct seq_file *m, void *v)
334 {
335         int i;
336         struct inode *inode = m->private;
337         struct task_struct *task = get_proc_task(inode);
338 
339         if (!task)
340                 return -ESRCH;
341         seq_puts(m, "Latency Top version : v0.1\n");
342         for (i = 0; i < 32; i++) {
343                 if (task->latency_record[i].backtrace[0]) {
344                         int q;
345                         seq_printf(m, "%i %li %li ",
346                                 task->latency_record[i].count,
347                                 task->latency_record[i].time,
348                                 task->latency_record[i].max);
349                         for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
350                                 char sym[KSYM_NAME_LEN];
351                                 char *c;
352                                 if (!task->latency_record[i].backtrace[q])
353                                         break;
354                                 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
355                                         break;
356                                 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
357                                 c = strchr(sym, '+');
358                                 if (c)
359                                         *c = 0;
360                                 seq_printf(m, "%s ", sym);
361                         }
362                         seq_printf(m, "\n");
363                 }
364 
365         }
366         put_task_struct(task);
367         return 0;
368 }
369 
370 static int lstats_open(struct inode *inode, struct file *file)
371 {
372         return single_open(file, lstats_show_proc, inode);
373 }
374 
375 static ssize_t lstats_write(struct file *file, const char __user *buf,
376                             size_t count, loff_t *offs)
377 {
378         struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
379 
380         if (!task)
381                 return -ESRCH;
382         clear_all_latency_tracing(task);
383         put_task_struct(task);
384 
385         return count;
386 }
387 
388 static const struct file_operations proc_lstats_operations = {
389         .open           = lstats_open,
390         .read           = seq_read,
391         .write          = lstats_write,
392         .llseek         = seq_lseek,
393         .release        = single_release,
394 };
395 
396 #endif
397 
398 /* The badness from the OOM killer */
399 unsigned long badness(struct task_struct *p, unsigned long uptime);
400 static int proc_oom_score(struct task_struct *task, char *buffer)
401 {
402         unsigned long points;
403         struct timespec uptime;
404 
405         do_posix_clock_monotonic_gettime(&uptime);
406         read_lock(&tasklist_lock);
407         points = badness(task, uptime.tv_sec);
408         read_unlock(&tasklist_lock);
409         return sprintf(buffer, "%lu\n", points);
410 }
411 
412 struct limit_names {
413         char *name;
414         char *unit;
415 };
416 
417 static const struct limit_names lnames[RLIM_NLIMITS] = {
418         [RLIMIT_CPU] = {"Max cpu time", "ms"},
419         [RLIMIT_FSIZE] = {"Max file size", "bytes"},
420         [RLIMIT_DATA] = {"Max data size", "bytes"},
421         [RLIMIT_STACK] = {"Max stack size", "bytes"},
422         [RLIMIT_CORE] = {"Max core file size", "bytes"},
423         [RLIMIT_RSS] = {"Max resident set", "bytes"},
424         [RLIMIT_NPROC] = {"Max processes", "processes"},
425         [RLIMIT_NOFILE] = {"Max open files", "files"},
426         [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
427         [RLIMIT_AS] = {"Max address space", "bytes"},
428         [RLIMIT_LOCKS] = {"Max file locks", "locks"},
429         [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
430         [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
431         [RLIMIT_NICE] = {"Max nice priority", NULL},
432         [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
433         [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
434 };
435 
436 /* Display limits for a process */
437 static int proc_pid_limits(struct task_struct *task, char *buffer)
438 {
439         unsigned int i;
440         int count = 0;
441         unsigned long flags;
442         char *bufptr = buffer;
443 
444         struct rlimit rlim[RLIM_NLIMITS];
445 
446         rcu_read_lock();
447         if (!lock_task_sighand(task,&flags)) {
448                 rcu_read_unlock();
449                 return 0;
450         }
451         memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
452         unlock_task_sighand(task, &flags);
453         rcu_read_unlock();
454 
455         /*
456          * print the file header
457          */
458         count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
459                         "Limit", "Soft Limit", "Hard Limit", "Units");
460 
461         for (i = 0; i < RLIM_NLIMITS; i++) {
462                 if (rlim[i].rlim_cur == RLIM_INFINITY)
463                         count += sprintf(&bufptr[count], "%-25s %-20s ",
464                                          lnames[i].name, "unlimited");
465                 else
466                         count += sprintf(&bufptr[count], "%-25s %-20lu ",
467                                          lnames[i].name, rlim[i].rlim_cur);
468 
469                 if (rlim[i].rlim_max == RLIM_INFINITY)
470                         count += sprintf(&bufptr[count], "%-20s ", "unlimited");
471                 else
472                         count += sprintf(&bufptr[count], "%-20lu ",
473                                          rlim[i].rlim_max);
474 
475                 if (lnames[i].unit)
476                         count += sprintf(&bufptr[count], "%-10s\n",
477                                          lnames[i].unit);
478                 else
479                         count += sprintf(&bufptr[count], "\n");
480         }
481 
482         return count;
483 }
484 
485 /************************************************************************/
486 /*                       Here the fs part begins                        */
487 /************************************************************************/
488 
489 /* permission checks */
490 static int proc_fd_access_allowed(struct inode *inode)
491 {
492         struct task_struct *task;
493         int allowed = 0;
494         /* Allow access to a task's file descriptors if it is us or we
495          * may use ptrace attach to the process and find out that
496          * information.
497          */
498         task = get_proc_task(inode);
499         if (task) {
500                 allowed = ptrace_may_attach(task);
501                 put_task_struct(task);
502         }
503         return allowed;
504 }
505 
506 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
507 {
508         int error;
509         struct inode *inode = dentry->d_inode;
510 
511         if (attr->ia_valid & ATTR_MODE)
512                 return -EPERM;
513 
514         error = inode_change_ok(inode, attr);
515         if (!error)
516                 error = inode_setattr(inode, attr);
517         return error;
518 }
519 
520 static const struct inode_operations proc_def_inode_operations = {
521         .setattr        = proc_setattr,
522 };
523 
524 extern const struct seq_operations mounts_op;
525 struct proc_mounts {
526         struct seq_file m;
527         int event;
528 };
529 
530 static int mounts_open(struct inode *inode, struct file *file)
531 {
532         struct task_struct *task = get_proc_task(inode);
533         struct nsproxy *nsp;
534         struct mnt_namespace *ns = NULL;
535         struct proc_mounts *p;
536         int ret = -EINVAL;
537 
538         if (task) {
539                 rcu_read_lock();
540                 nsp = task_nsproxy(task);
541                 if (nsp) {
542                         ns = nsp->mnt_ns;
543                         if (ns)
544                                 get_mnt_ns(ns);
545                 }
546                 rcu_read_unlock();
547 
548                 put_task_struct(task);
549         }
550 
551         if (ns) {
552                 ret = -ENOMEM;
553                 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
554                 if (p) {
555                         file->private_data = &p->m;
556                         ret = seq_open(file, &mounts_op);
557                         if (!ret) {
558                                 p->m.private = ns;
559                                 p->event = ns->event;
560                                 return 0;
561                         }
562                         kfree(p);
563                 }
564                 put_mnt_ns(ns);
565         }
566         return ret;
567 }
568 
569 static int mounts_release(struct inode *inode, struct file *file)
570 {
571         struct seq_file *m = file->private_data;
572         struct mnt_namespace *ns = m->private;
573         put_mnt_ns(ns);
574         return seq_release(inode, file);
575 }
576 
577 static unsigned mounts_poll(struct file *file, poll_table *wait)
578 {
579         struct proc_mounts *p = file->private_data;
580         struct mnt_namespace *ns = p->m.private;
581         unsigned res = 0;
582 
583         poll_wait(file, &ns->poll, wait);
584 
585         spin_lock(&vfsmount_lock);
586         if (p->event != ns->event) {
587                 p->event = ns->event;
588                 res = POLLERR;
589         }
590         spin_unlock(&vfsmount_lock);
591 
592         return res;
593 }
594 
595 static const struct file_operations proc_mounts_operations = {
596         .open           = mounts_open,
597         .read           = seq_read,
598         .llseek         = seq_lseek,
599         .release        = mounts_release,
600         .poll           = mounts_poll,
601 };
602 
603 extern const struct seq_operations mountstats_op;
604 static int mountstats_open(struct inode *inode, struct file *file)
605 {
606         int ret = seq_open(file, &mountstats_op);
607 
608         if (!ret) {
609                 struct seq_file *m = file->private_data;
610                 struct nsproxy *nsp;
611                 struct mnt_namespace *mnt_ns = NULL;
612                 struct task_struct *task = get_proc_task(inode);
613 
614                 if (task) {
615                         rcu_read_lock();
616                         nsp = task_nsproxy(task);
617                         if (nsp) {
618                                 mnt_ns = nsp->mnt_ns;
619                                 if (mnt_ns)
620                                         get_mnt_ns(mnt_ns);
621                         }
622                         rcu_read_unlock();
623 
624                         put_task_struct(task);
625                 }
626 
627                 if (mnt_ns)
628                         m->private = mnt_ns;
629                 else {
630                         seq_release(inode, file);
631                         ret = -EINVAL;
632                 }
633         }
634         return ret;
635 }
636 
637 static const struct file_operations proc_mountstats_operations = {
638         .open           = mountstats_open,
639         .read           = seq_read,
640         .llseek         = seq_lseek,
641         .release        = mounts_release,
642 };
643 
644 #define PROC_BLOCK_SIZE (3*1024)                /* 4K page size but our output routines use some slack for overruns */
645 
646 static ssize_t proc_info_read(struct file * file, char __user * buf,
647                           size_t count, loff_t *ppos)
648 {
649         struct inode * inode = file->f_path.dentry->d_inode;
650         unsigned long page;
651         ssize_t length;
652         struct task_struct *task = get_proc_task(inode);
653 
654         length = -ESRCH;
655         if (!task)
656                 goto out_no_task;
657 
658         if (count > PROC_BLOCK_SIZE)
659                 count = PROC_BLOCK_SIZE;
660 
661         length = -ENOMEM;
662         if (!(page = __get_free_page(GFP_TEMPORARY)))
663                 goto out;
664 
665         length = PROC_I(inode)->op.proc_read(task, (char*)page);
666 
667         if (length >= 0)
668                 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
669         free_page(page);
670 out:
671         put_task_struct(task);
672 out_no_task:
673         return length;
674 }
675 
676 static const struct file_operations proc_info_file_operations = {
677         .read           = proc_info_read,
678 };
679 
680 static int proc_single_show(struct seq_file *m, void *v)
681 {
682         struct inode *inode = m->private;
683         struct pid_namespace *ns;
684         struct pid *pid;
685         struct task_struct *task;
686         int ret;
687 
688         ns = inode->i_sb->s_fs_info;
689         pid = proc_pid(inode);
690         task = get_pid_task(pid, PIDTYPE_PID);
691         if (!task)
692                 return -ESRCH;
693 
694         ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
695 
696         put_task_struct(task);
697         return ret;
698 }
699 
700 static int proc_single_open(struct inode *inode, struct file *filp)
701 {
702         int ret;
703         ret = single_open(filp, proc_single_show, NULL);
704         if (!ret) {
705                 struct seq_file *m = filp->private_data;
706 
707                 m->private = inode;
708         }
709         return ret;
710 }
711 
712 static const struct file_operations proc_single_file_operations = {
713         .open           = proc_single_open,
714         .read           = seq_read,
715         .llseek         = seq_lseek,
716         .release        = single_release,
717 };
718 
719 static int mem_open(struct inode* inode, struct file* file)
720 {
721         file->private_data = (void*)((long)current->self_exec_id);
722         return 0;
723 }
724 
725 static ssize_t mem_read(struct file * file, char __user * buf,
726                         size_t count, loff_t *ppos)
727 {
728         struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
729         char *page;
730         unsigned long src = *ppos;
731         int ret = -ESRCH;
732         struct mm_struct *mm;
733 
734         if (!task)
735                 goto out_no_task;
736 
737         if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
738                 goto out;
739 
740         ret = -ENOMEM;
741         page = (char *)__get_free_page(GFP_TEMPORARY);
742         if (!page)
743                 goto out;
744 
745         ret = 0;
746  
747         mm = get_task_mm(task);
748         if (!mm)
749                 goto out_free;
750 
751         ret = -EIO;
752  
753         if (file->private_data != (void*)((long)current->self_exec_id))
754                 goto out_put;
755 
756         ret = 0;
757  
758         while (count > 0) {
759                 int this_len, retval;
760 
761                 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
762                 retval = access_process_vm(task, src, page, this_len, 0);
763                 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
764                         if (!ret)
765                                 ret = -EIO;
766                         break;
767                 }
768 
769                 if (copy_to_user(buf, page, retval)) {
770                         ret = -EFAULT;
771                         break;
772                 }
773  
774                 ret += retval;
775                 src += retval;
776                 buf += retval;
777                 count -= retval;
778         }
779         *ppos = src;
780 
781 out_put:
782         mmput(mm);
783 out_free:
784         free_page((unsigned long) page);
785 out:
786         put_task_struct(task);
787 out_no_task:
788         return ret;
789 }
790 
791 #define mem_write NULL
792 
793 #ifndef mem_write
794 /* This is a security hazard */
795 static ssize_t mem_write(struct file * file, const char __user *buf,
796                          size_t count, loff_t *ppos)
797 {
798         int copied;
799         char *page;
800         struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
801         unsigned long dst = *ppos;
802 
803         copied = -ESRCH;
804         if (!task)
805                 goto out_no_task;
806 
807         if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
808                 goto out;
809 
810         copied = -ENOMEM;
811         page = (char *)__get_free_page(GFP_TEMPORARY);
812         if (!page)
813                 goto out;
814 
815         copied = 0;
816         while (count > 0) {
817                 int this_len, retval;
818 
819                 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
820                 if (copy_from_user(page, buf, this_len)) {
821                         copied = -EFAULT;
822                         break;
823                 }
824                 retval = access_process_vm(task, dst, page, this_len, 1);
825                 if (!retval) {
826                         if (!copied)
827                                 copied = -EIO;
828                         break;
829                 }
830                 copied += retval;
831                 buf += retval;
832                 dst += retval;
833                 count -= retval;                        
834         }
835         *ppos = dst;
836         free_page((unsigned long) page);
837 out:
838         put_task_struct(task);
839 out_no_task:
840         return copied;
841 }
842 #endif
843 
844 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
845 {
846         switch (orig) {
847         case 0:
848                 file->f_pos = offset;
849                 break;
850         case 1:
851                 file->f_pos += offset;
852                 break;
853         default:
854                 return -EINVAL;
855         }
856         force_successful_syscall_return();
857         return file->f_pos;
858 }
859 
860 static const struct file_operations proc_mem_operations = {
861         .llseek         = mem_lseek,
862         .read           = mem_read,
863         .write          = mem_write,
864         .open           = mem_open,
865 };
866 
867 static ssize_t environ_read(struct file *file, char __user *buf,
868                         size_t count, loff_t *ppos)
869 {
870         struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
871         char *page;
872         unsigned long src = *ppos;
873         int ret = -ESRCH;
874         struct mm_struct *mm;
875 
876         if (!task)
877                 goto out_no_task;
878 
879         if (!ptrace_may_attach(task))
880                 goto out;
881 
882         ret = -ENOMEM;
883         page = (char *)__get_free_page(GFP_TEMPORARY);
884         if (!page)
885                 goto out;
886 
887         ret = 0;
888 
889         mm = get_task_mm(task);
890         if (!mm)
891                 goto out_free;
892 
893         while (count > 0) {
894                 int this_len, retval, max_len;
895 
896                 this_len = mm->env_end - (mm->env_start + src);
897 
898                 if (this_len <= 0)
899                         break;
900 
901                 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
902                 this_len = (this_len > max_len) ? max_len : this_len;
903 
904                 retval = access_process_vm(task, (mm->env_start + src),
905                         page, this_len, 0);
906 
907                 if (retval <= 0) {
908                         ret = retval;
909                         break;
910                 }
911 
912                 if (copy_to_user(buf, page, retval)) {
913                         ret = -EFAULT;
914                         break;
915                 }
916 
917                 ret += retval;
918                 src += retval;
919                 buf += retval;
920                 count -= retval;
921         }
922         *ppos = src;
923 
924         mmput(mm);
925 out_free:
926         free_page((unsigned long) page);
927 out:
928         put_task_struct(task);
929 out_no_task:
930         return ret;
931 }
932 
933 static const struct file_operations proc_environ_operations = {
934         .read           = environ_read,
935 };
936 
937 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
938                                 size_t count, loff_t *ppos)
939 {
940         struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
941         char buffer[PROC_NUMBUF];
942         size_t len;
943         int oom_adjust;
944 
945         if (!task)
946                 return -ESRCH;
947         oom_adjust = task->oomkilladj;
948         put_task_struct(task);
949 
950         len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
951 
952         return simple_read_from_buffer(buf, count, ppos, buffer, len);
953 }
954 
955 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
956                                 size_t count, loff_t *ppos)
957 {
958         struct task_struct *task;
959         char buffer[PROC_NUMBUF], *end;
960         int oom_adjust;
961 
962         memset(buffer, 0, sizeof(buffer));
963         if (count > sizeof(buffer) - 1)
964                 count = sizeof(buffer) - 1;
965         if (copy_from_user(buffer, buf, count))
966                 return -EFAULT;
967         oom_adjust = simple_strtol(buffer, &end, 0);
968         if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
969              oom_adjust != OOM_DISABLE)
970                 return -EINVAL;
971         if (*end == '\n')
972                 end++;
973         task = get_proc_task(file->f_path.dentry->d_inode);
974         if (!task)
975                 return -ESRCH;
976         if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
977                 put_task_struct(task);
978                 return -EACCES;
979         }
980         task->oomkilladj = oom_adjust;
981         put_task_struct(task);
982         if (end - buffer == 0)
983                 return -EIO;
984         return end - buffer;
985 }
986 
987 static const struct file_operations proc_oom_adjust_operations = {
988         .read           = oom_adjust_read,
989         .write          = oom_adjust_write,
990 };
991 
992 #ifdef CONFIG_AUDITSYSCALL
993 #define TMPBUFLEN 21
994 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
995                                   size_t count, loff_t *ppos)
996 {
997         struct inode * inode = file->f_path.dentry->d_inode;
998         struct task_struct *task = get_proc_task(inode);
999         ssize_t length;
1000         char tmpbuf[TMPBUFLEN];
1001 
1002         if (!task)
1003                 return -ESRCH;
1004         length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1005                                 audit_get_loginuid(task));
1006         put_task_struct(task);
1007         return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1008 }
1009 
1010 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1011                                    size_t count, loff_t *ppos)
1012 {
1013         struct inode * inode = file->f_path.dentry->d_inode;
1014         char *page, *tmp;
1015         ssize_t length;
1016         uid_t loginuid;
1017 
1018         if (!capable(CAP_AUDIT_CONTROL))
1019                 return -EPERM;
1020 
1021         if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
1022                 return -EPERM;
1023 
1024         if (count >= PAGE_SIZE)
1025                 count = PAGE_SIZE - 1;
1026 
1027         if (*ppos != 0) {
1028                 /* No partial writes. */
1029                 return -EINVAL;
1030         }
1031         page = (char*)__get_free_page(GFP_TEMPORARY);
1032         if (!page)
1033                 return -ENOMEM;
1034         length = -EFAULT;
1035         if (copy_from_user(page, buf, count))
1036                 goto out_free_page;
1037 
1038         page[count] = '\0';
1039         loginuid = simple_strtoul(page, &tmp, 10);
1040         if (tmp == page) {
1041                 length = -EINVAL;
1042                 goto out_free_page;
1043 
1044         }
1045         length = audit_set_loginuid(current, loginuid);
1046         if (likely(length == 0))
1047                 length = count;
1048 
1049 out_free_page:
1050         free_page((unsigned long) page);
1051         return length;
1052 }
1053 
1054 static const struct file_operations proc_loginuid_operations = {
1055         .read           = proc_loginuid_read,
1056         .write          = proc_loginuid_write,
1057 };
1058 
1059 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1060                                   size_t count, loff_t *ppos)
1061 {
1062         struct inode * inode = file->f_path.dentry->d_inode;
1063         struct task_struct *task = get_proc_task(inode);
1064         ssize_t length;
1065         char tmpbuf[TMPBUFLEN];
1066 
1067         if (!task)
1068                 return -ESRCH;
1069         length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1070                                 audit_get_sessionid(task));
1071         put_task_struct(task);
1072         return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1073 }
1074 
1075 static const struct file_operations proc_sessionid_operations = {
1076         .read           = proc_sessionid_read,
1077 };
1078 #endif
1079 
1080 #ifdef CONFIG_FAULT_INJECTION
1081 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1082                                       size_t count, loff_t *ppos)
1083 {
1084         struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1085         char buffer[PROC_NUMBUF];
1086         size_t len;
1087         int make_it_fail;
1088 
1089         if (!task)
1090                 return -ESRCH;
1091         make_it_fail = task->make_it_fail;
1092         put_task_struct(task);
1093 
1094         len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1095 
1096         return simple_read_from_buffer(buf, count, ppos, buffer, len);
1097 }
1098 
1099 static ssize_t proc_fault_inject_write(struct file * file,
1100                         const char __user * buf, size_t count, loff_t *ppos)
1101 {
1102         struct task_struct *task;
1103         char buffer[PROC_NUMBUF], *end;
1104         int make_it_fail;
1105 
1106         if (!capable(CAP_SYS_RESOURCE))
1107                 return -EPERM;
1108         memset(buffer, 0, sizeof(buffer));
1109         if (count > sizeof(buffer) - 1)
1110                 count = sizeof(buffer) - 1;
1111         if (copy_from_user(buffer, buf, count))
1112                 return -EFAULT;
1113         make_it_fail = simple_strtol(buffer, &end, 0);
1114         if (*end == '\n')
1115                 end++;
1116         task = get_proc_task(file->f_dentry->d_inode);
1117         if (!task)
1118                 return -ESRCH;
1119         task->make_it_fail = make_it_fail;
1120         put_task_struct(task);
1121         if (end - buffer == 0)
1122                 return -EIO;
1123         return end - buffer;
1124 }
1125 
1126 static const struct file_operations proc_fault_inject_operations = {
1127         .read           = proc_fault_inject_read,
1128         .write          = proc_fault_inject_write,
1129 };
1130 #endif
1131 
1132 
1133 #ifdef CONFIG_SCHED_DEBUG
1134 /*
1135  * Print out various scheduling related per-task fields:
1136  */
1137 static int sched_show(struct seq_file *m, void *v)
1138 {
1139         struct inode *inode = m->private;
1140         struct task_struct *p;
1141 
1142         WARN_ON(!inode);
1143 
1144         p = get_proc_task(inode);
1145         if (!p)
1146                 return -ESRCH;
1147         proc_sched_show_task(p, m);
1148 
1149         put_task_struct(p);
1150 
1151         return 0;
1152 }
1153 
1154 static ssize_t
1155 sched_write(struct file *file, const char __user *buf,
1156             size_t count, loff_t *offset)
1157 {
1158         struct inode *inode = file->f_path.dentry->d_inode;
1159         struct task_struct *p;
1160 
1161         WARN_ON(!inode);
1162 
1163         p = get_proc_task(inode);
1164         if (!p)
1165                 return -ESRCH;
1166         proc_sched_set_task(p);
1167 
1168         put_task_struct(p);
1169 
1170         return count;
1171 }
1172 
1173 static int sched_open(struct inode *inode, struct file *filp)
1174 {
1175         int ret;
1176 
1177         ret = single_open(filp, sched_show, NULL);
1178         if (!ret) {
1179                 struct seq_file *m = filp->private_data;
1180 
1181                 m->private = inode;
1182         }
1183         return ret;
1184 }
1185 
1186 static const struct file_operations proc_pid_sched_operations = {
1187         .open           = sched_open,
1188         .read           = seq_read,
1189         .write          = sched_write,
1190         .llseek         = seq_lseek,
1191         .release        = single_release,
1192 };
1193 
1194 #endif
1195 
1196 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1197 {
1198         struct inode *inode = dentry->d_inode;
1199         int error = -EACCES;
1200 
1201         /* We don't need a base pointer in the /proc filesystem */
1202         path_put(&nd->path);
1203 
1204         /* Are we allowed to snoop on the tasks file descriptors? */
1205         if (!proc_fd_access_allowed(inode))
1206                 goto out;
1207 
1208         error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
1209         nd->last_type = LAST_BIND;
1210 out:
1211         return ERR_PTR(error);
1212 }
1213 
1214 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1215 {
1216         char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1217         char *pathname;
1218         int len;
1219 
1220         if (!tmp)
1221                 return -ENOMEM;
1222 
1223         pathname = d_path(path, tmp, PAGE_SIZE);
1224         len = PTR_ERR(pathname);
1225         if (IS_ERR(pathname))
1226                 goto out;
1227         len = tmp + PAGE_SIZE - 1 - pathname;
1228 
1229         if (len > buflen)
1230                 len = buflen;
1231         if (copy_to_user(buffer, pathname, len))
1232                 len = -EFAULT;
1233  out:
1234         free_page((unsigned long)tmp);
1235         return len;
1236 }
1237 
1238 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1239 {
1240         int error = -EACCES;
1241         struct inode *inode = dentry->d_inode;
1242         struct path path;
1243 
1244         /* Are we allowed to snoop on the tasks file descriptors? */
1245         if (!proc_fd_access_allowed(inode))
1246                 goto out;
1247 
1248         error = PROC_I(inode)->op.proc_get_link(inode, &path);
1249         if (error)
1250                 goto out;
1251 
1252         error = do_proc_readlink(&path, buffer, buflen);
1253         path_put(&path);
1254 out:
1255         return error;
1256 }
1257 
1258 static const struct inode_operations proc_pid_link_inode_operations = {
1259         .readlink       = proc_pid_readlink,
1260         .follow_link    = proc_pid_follow_link,
1261         .setattr        = proc_setattr,
1262 };
1263 
1264 
1265 /* building an inode */
1266 
1267 static int task_dumpable(struct task_struct *task)
1268 {
1269         int dumpable = 0;
1270         struct mm_struct *mm;
1271 
1272         task_lock(task);
1273         mm = task->mm;
1274         if (mm)
1275                 dumpable = get_dumpable(mm);
1276         task_unlock(task);
1277         if(dumpable == 1)
1278                 return 1;
1279         return 0;
1280 }
1281 
1282 
1283 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1284 {
1285         struct inode * inode;
1286         struct proc_inode *ei;
1287 
1288         /* We need a new inode */
1289 
1290         inode = new_inode(sb);
1291         if (!inode)
1292                 goto out;
1293 
1294         /* Common stuff */
1295         ei = PROC_I(inode);
1296         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1297         inode->i_op = &proc_def_inode_operations;
1298 
1299         /*
1300          * grab the reference to task.
1301          */
1302         ei->pid = get_task_pid(task, PIDTYPE_PID);
1303         if (!ei->pid)
1304                 goto out_unlock;
1305 
1306         inode->i_uid = 0;
1307         inode->i_gid = 0;
1308         if (task_dumpable(task)) {
1309                 inode->i_uid = task->euid;
1310                 inode->i_gid = task->egid;
1311         }
1312         security_task_to_inode(task, inode);
1313 
1314 out:
1315         return inode;
1316 
1317 out_unlock:
1318         iput(inode);
1319         return NULL;
1320 }
1321 
1322 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1323 {
1324         struct inode *inode = dentry->d_inode;
1325         struct task_struct *task;
1326         generic_fillattr(inode, stat);
1327 
1328         rcu_read_lock();
1329         stat->uid = 0;
1330         stat->gid = 0;
1331         task = pid_task(proc_pid(inode), PIDTYPE_PID);
1332         if (task) {
1333                 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1334                     task_dumpable(task)) {
1335                         stat->uid = task->euid;
1336                         stat->gid = task->egid;
1337                 }
1338         }
1339         rcu_read_unlock();
1340         return 0;
1341 }
1342 
1343 /* dentry stuff */
1344 
1345 /*
1346  *      Exceptional case: normally we are not allowed to unhash a busy
1347  * directory. In this case, however, we can do it - no aliasing problems
1348  * due to the way we treat inodes.
1349  *
1350  * Rewrite the inode's ownerships here because the owning task may have
1351  * performed a setuid(), etc.
1352  *
1353  * Before the /proc/pid/status file was created the only way to read
1354  * the effective uid of a /process was to stat /proc/pid.  Reading
1355  * /proc/pid/status is slow enough that procps and other packages
1356  * kept stating /proc/pid.  To keep the rules in /proc simple I have
1357  * made this apply to all per process world readable and executable
1358  * directories.
1359  */
1360 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1361 {
1362         struct inode *inode = dentry->d_inode;
1363         struct task_struct *task = get_proc_task(inode);
1364         if (task) {
1365                 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1366                     task_dumpable(task)) {
1367                         inode->i_uid = task->euid;
1368                         inode->i_gid = task->egid;
1369                 } else {
1370                         inode->i_uid = 0;
1371                         inode->i_gid = 0;
1372                 }
1373                 inode->i_mode &= ~(S_ISUID | S_ISGID);
1374                 security_task_to_inode(task, inode);
1375                 put_task_struct(task);
1376                 return 1;
1377         }
1378         d_drop(dentry);
1379         return 0;
1380 }
1381 
1382 static int pid_delete_dentry(struct dentry * dentry)
1383 {
1384         /* Is the task we represent dead?
1385          * If so, then don't put the dentry on the lru list,
1386          * kill it immediately.
1387          */
1388         return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1389 }
1390 
1391 static struct dentry_operations pid_dentry_operations =
1392 {
1393         .d_revalidate   = pid_revalidate,
1394         .d_delete       = pid_delete_dentry,
1395 };
1396 
1397 /* Lookups */
1398 
1399 typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1400                                 struct task_struct *, const void *);
1401 
1402 /*
1403  * Fill a directory entry.
1404  *
1405  * If possible create the dcache entry and derive our inode number and
1406  * file type from dcache entry.
1407  *
1408  * Since all of the proc inode numbers are dynamically generated, the inode
1409  * numbers do not exist until the inode is cache.  This means creating the
1410  * the dcache entry in readdir is necessary to keep the inode numbers
1411  * reported by readdir in sync with the inode numbers reported
1412  * by stat.
1413  */
1414 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1415         char *name, int len,
1416         instantiate_t instantiate, struct task_struct *task, const void *ptr)
1417 {
1418         struct dentry *child, *dir = filp->f_path.dentry;
1419         struct inode *inode;
1420         struct qstr qname;
1421         ino_t ino = 0;
1422         unsigned type = DT_UNKNOWN;
1423 
1424         qname.name = name;
1425         qname.len  = len;
1426         qname.hash = full_name_hash(name, len);
1427 
1428         child = d_lookup(dir, &qname);
1429         if (!child) {
1430                 struct dentry *new;
1431                 new = d_alloc(dir, &qname);
1432                 if (new) {
1433                         child = instantiate(dir->d_inode, new, task, ptr);
1434                         if (child)
1435                                 dput(new);
1436                         else
1437                                 child = new;
1438                 }
1439         }
1440         if (!child || IS_ERR(child) || !child->d_inode)
1441                 goto end_instantiate;
1442         inode = child->d_inode;
1443         if (inode) {
1444                 ino = inode->i_ino;
1445                 type = inode->i_mode >> 12;
1446         }
1447         dput(child);
1448 end_instantiate:
1449         if (!ino)
1450                 ino = find_inode_number(dir, &qname);
1451         if (!ino)
1452                 ino = 1;
1453         return filldir(dirent, name, len, filp->f_pos, ino, type);
1454 }
1455 
1456 static unsigned name_to_int(struct dentry *dentry)
1457 {
1458         const char *name = dentry->d_name.name;
1459         int len = dentry->d_name.len;
1460         unsigned n = 0;
1461 
1462         if (len > 1 && *name == '')
1463                 goto out;
1464         while (len-- > 0) {
1465                 unsigned c = *name++ - '';
1466                 if (c > 9)
1467                         goto out;
1468                 if (n >= (~0U-9)/10)
1469                         goto out;
1470                 n *= 10;
1471                 n += c;
1472         }
1473         return n;
1474 out:
1475         return ~0U;
1476 }
1477 
1478 #define PROC_FDINFO_MAX 64
1479 
1480 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1481 {
1482         struct task_struct *task = get_proc_task(inode);
1483         struct files_struct *files = NULL;
1484         struct file *file;
1485         int fd = proc_fd(inode);
1486 
1487         if (task) {
1488                 files = get_files_struct(task);
1489                 put_task_struct(task);
1490         }
1491         if (files) {
1492                 /*
1493                  * We are not taking a ref to the file structure, so we must
1494                  * hold ->file_lock.
1495                  */
1496                 spin_lock(&files->file_lock);
1497                 file = fcheck_files(files, fd);
1498                 if (file) {
1499                         if (path) {
1500                                 *path = file->f_path;
1501                                 path_get(&file->f_path);
1502                         }
1503                         if (info)
1504                                 snprintf(info, PROC_FDINFO_MAX,
1505                                          "pos:\t%lli\n"
1506                                          "flags:\t0%o\n",
1507                                          (long long) file->f_pos,
1508                                          file->f_flags);
1509                         spin_unlock(&files->file_lock);
1510                         put_files_struct(files);
1511                         return 0;
1512                 }
1513                 spin_unlock(&files->file_lock);
1514                 put_files_struct(files);
1515         }
1516         return -ENOENT;
1517 }
1518 
1519 static int proc_fd_link(struct inode *inode, struct path *path)
1520 {
1521         return proc_fd_info(inode, path, NULL);
1522 }
1523 
1524 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1525 {
1526         struct inode *inode = dentry->d_inode;
1527         struct task_struct *task = get_proc_task(inode);
1528         int fd = proc_fd(inode);
1529         struct files_struct *files;
1530 
1531         if (task) {
1532                 files = get_files_struct(task);
1533                 if (files) {
1534                         rcu_read_lock();
1535                         if (fcheck_files(files, fd)) {
1536                                 rcu_read_unlock();
1537                                 put_files_struct(files);
1538                                 if (task_dumpable(task)) {
1539                                         inode->i_uid = task->euid;
1540                                         inode->i_gid = task->egid;
1541                                 } else {
1542                                         inode->i_uid = 0;
1543                                         inode->i_gid = 0;
1544                                 }
1545                                 inode->i_mode &= ~(S_ISUID | S_ISGID);
1546                                 security_task_to_inode(task, inode);
1547                                 put_task_struct(task);
1548                                 return 1;
1549                         }
1550                         rcu_read_unlock();
1551                         put_files_struct(files);
1552                 }
1553                 put_task_struct(task);
1554         }
1555         d_drop(dentry);
1556         return 0;
1557 }
1558 
1559 static struct dentry_operations tid_fd_dentry_operations =
1560 {
1561         .d_revalidate   = tid_fd_revalidate,
1562         .d_delete       = pid_delete_dentry,
1563 };
1564 
1565 static struct dentry *proc_fd_instantiate(struct inode *dir,
1566         struct dentry *dentry, struct task_struct *task, const void *ptr)
1567 {
1568         unsigned fd = *(const unsigned *)ptr;
1569         struct file *file;
1570         struct files_struct *files;
1571         struct inode *inode;
1572         struct proc_inode *ei;
1573         struct dentry *error = ERR_PTR(-ENOENT);
1574 
1575         inode = proc_pid_make_inode(dir->i_sb, task);
1576         if (!inode)
1577                 goto out;
1578         ei = PROC_I(inode);
1579         ei->fd = fd;
1580         files = get_files_struct(task);
1581         if (!files)
1582                 goto out_iput;
1583         inode->i_mode = S_IFLNK;
1584 
1585         /*
1586          * We are not taking a ref to the file structure, so we must
1587          * hold ->file_lock.
1588          */
1589         spin_lock(&files->file_lock);
1590         file = fcheck_files(files, fd);
1591         if (!file)
1592                 goto out_unlock;
1593         if (file->f_mode & 1)
1594                 inode->i_mode |= S_IRUSR | S_IXUSR;
1595         if (file->f_mode & 2)
1596                 inode->i_mode |= S_IWUSR | S_IXUSR;
1597         spin_unlock(&files->file_lock);
1598         put_files_struct(files);
1599 
1600         inode->i_op = &proc_pid_link_inode_operations;
1601         inode->i_size = 64;
1602         ei->op.proc_get_link = proc_fd_link;
1603         dentry->d_op = &tid_fd_dentry_operations;
1604         d_add(dentry, inode);
1605         /* Close the race of the process dying before we return the dentry */
1606         if (tid_fd_revalidate(dentry, NULL))
1607                 error = NULL;
1608 
1609  out:
1610         return error;
1611 out_unlock:
1612         spin_unlock(&files->file_lock);
1613         put_files_struct(files);
1614 out_iput:
1615         iput(inode);
1616         goto out;
1617 }
1618 
1619 static struct dentry *proc_lookupfd_common(struct inode *dir,
1620                                            struct dentry *dentry,
1621                                            instantiate_t instantiate)
1622 {
1623         struct task_struct *task = get_proc_task(dir);
1624         unsigned fd = name_to_int(dentry);
1625         struct dentry *result = ERR_PTR(-ENOENT);
1626 
1627         if (!task)
1628                 goto out_no_task;
1629         if (fd == ~0U)
1630                 goto out;
1631 
1632         result = instantiate(dir, dentry, task, &fd);
1633 out:
1634         put_task_struct(task);
1635 out_no_task:
1636         return result;
1637 }
1638 
1639 static int proc_readfd_common(struct file * filp, void * dirent,
1640                               filldir_t filldir, instantiate_t instantiate)
1641 {
1642         struct dentry *dentry = filp->f_path.dentry;
1643         struct inode *inode = dentry->d_inode;
1644         struct task_struct *p = get_proc_task(inode);
1645         unsigned int fd, ino;
1646         int retval;
1647         struct files_struct * files;
1648         struct fdtable *fdt;
1649 
1650         retval = -ENOENT;
1651         if (!p)
1652                 goto out_no_task;
1653         retval = 0;
1654 
1655         fd = filp->f_pos;
1656         switch (fd) {
1657                 case 0:
1658                         if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1659                                 goto out;
1660                         filp->f_pos++;
1661                 case 1:
1662                         ino = parent_ino(dentry);
1663                         if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1664                                 goto out;
1665                         filp->f_pos++;
1666                 default:
1667                         files = get_files_struct(p);
1668                         if (!files)
1669                                 goto out;
1670                         rcu_read_lock();
1671                         fdt = files_fdtable(files);
1672                         for (fd = filp->f_pos-2;
1673                              fd < fdt->max_fds;
1674                              fd++, filp->f_pos++) {
1675                                 char name[PROC_NUMBUF];
1676                                 int len;
1677 
1678                                 if (!fcheck_files(files, fd))
1679                                         continue;
1680                                 rcu_read_unlock();
1681 
1682                                 len = snprintf(name, sizeof(name), "%d", fd);
1683                                 if (proc_fill_cache(filp, dirent, filldir,
1684                                                     name, len, instantiate,
1685                                                     p, &fd) < 0) {
1686                                         rcu_read_lock();
1687                                         break;
1688                                 }
1689                                 rcu_read_lock();
1690                         }
1691                         rcu_read_unlock();
1692                         put_files_struct(files);
1693         }
1694 out:
1695         put_task_struct(p);
1696 out_no_task:
1697         return retval;
1698 }
1699 
1700 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1701                                     struct nameidata *nd)
1702 {
1703         return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1704 }
1705 
1706 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1707 {
1708         return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1709 }
1710 
1711 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1712                                       size_t len, loff_t *ppos)
1713 {
1714         char tmp[PROC_FDINFO_MAX];
1715         int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
1716         if (!err)
1717                 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1718         return err;
1719 }
1720 
1721 static const struct file_operations proc_fdinfo_file_operations = {
1722         .open           = nonseekable_open,
1723         .read           = proc_fdinfo_read,
1724 };
1725 
1726 static const struct file_operations proc_fd_operations = {
1727         .read           = generic_read_dir,
1728         .readdir        = proc_readfd,
1729 };
1730 
1731 /*
1732  * /proc/pid/fd needs a special permission handler so that a process can still
1733  * access /proc/self/fd after it has executed a setuid().
1734  */
1735 static int proc_fd_permission(struct inode *inode, int mask,
1736                                 struct nameidata *nd)
1737 {
1738         int rv;
1739 
1740         rv = generic_permission(inode, mask, NULL);
1741         if (rv == 0)
1742                 return 0;
1743         if (task_pid(current) == proc_pid(inode))
1744                 rv = 0;
1745         return rv;
1746 }
1747 
1748 /*
1749  * proc directories can do almost nothing..
1750  */
1751 static const struct inode_operations proc_fd_inode_operations = {
1752         .lookup         = proc_lookupfd,
1753         .permission     = proc_fd_permission,
1754         .setattr        = proc_setattr,
1755 };
1756 
1757 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1758         struct dentry *dentry, struct task_struct *task, const void *ptr)
1759 {
1760         unsigned fd = *(unsigned *)ptr;
1761         struct inode *inode;
1762         struct proc_inode *ei;
1763         struct dentry *error = ERR_PTR(-ENOENT);
1764 
1765         inode = proc_pid_make_inode(dir->i_sb, task);
1766         if (!inode)
1767                 goto out;
1768         ei = PROC_I(inode);
1769         ei->fd = fd;
1770         inode->i_mode = S_IFREG | S_IRUSR;
1771         inode->i_fop = &proc_fdinfo_file_operations;
1772         dentry->d_op = &tid_fd_dentry_operations;
1773         d_add(dentry, inode);
1774         /* Close the race of the process dying before we return the dentry */
1775         if (tid_fd_revalidate(dentry, NULL))
1776                 error = NULL;
1777 
1778  out:
1779         return error;
1780 }
1781 
1782 static struct dentry *proc_lookupfdinfo(struct inode *dir,
1783                                         struct dentry *dentry,
1784                                         struct nameidata *nd)
1785 {
1786         return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1787 }
1788 
1789 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1790 {
1791         return proc_readfd_common(filp, dirent, filldir,
1792                                   proc_fdinfo_instantiate);
1793 }
1794 
1795 static const struct file_operations proc_fdinfo_operations = {
1796         .read           = generic_read_dir,
1797         .readdir        = proc_readfdinfo,
1798 };
1799 
1800 /*
1801  * proc directories can do almost nothing..
1802  */
1803 static const struct inode_operations proc_fdinfo_inode_operations = {
1804         .lookup         = proc_lookupfdinfo,
1805         .setattr        = proc_setattr,
1806 };
1807 
1808 
1809 static struct dentry *proc_pident_instantiate(struct inode *dir,
1810         struct dentry *dentry, struct task_struct *task, const void *ptr)
1811 {
1812         const struct pid_entry *p = ptr;
1813         struct inode *inode;
1814         struct proc_inode *ei;
1815         struct dentry *error = ERR_PTR(-EINVAL);
1816 
1817         inode = proc_pid_make_inode(dir->i_sb, task);
1818         if (!inode)
1819                 goto out;
1820 
1821         ei = PROC_I(inode);
1822         inode->i_mode = p->mode;
1823         if (S_ISDIR(inode->i_mode))
1824                 inode->i_nlink = 2;     /* Use getattr to fix if necessary */
1825         if (p->iop)
1826                 inode->i_op = p->iop;
1827         if (p->fop)
1828                 inode->i_fop = p->fop;
1829         ei->op = p->op;
1830         dentry->d_op = &pid_dentry_operations;
1831         d_add(dentry, inode);
1832         /* Close the race of the process dying before we return the dentry */
1833         if (pid_revalidate(dentry, NULL))
1834                 error = NULL;
1835 out:
1836         return error;
1837 }
1838 
1839 static struct dentry *proc_pident_lookup(struct inode *dir, 
1840                                          struct dentry *dentry,
1841                                          const struct pid_entry *ents,
1842                                          unsigned int nents)
1843 {
1844         struct inode *inode;
1845         struct dentry *error;
1846         struct task_struct *task = get_proc_task(dir);
1847         const struct pid_entry *p, *last;
1848 
1849         error = ERR_PTR(-ENOENT);
1850         inode = NULL;
1851 
1852         if (!task)
1853                 goto out_no_task;
1854 
1855         /*
1856          * Yes, it does not scale. And it should not. Don't add
1857          * new entries into /proc/<tgid>/ without very good reasons.
1858          */
1859         last = &ents[nents - 1];
1860         for (p = ents; p <= last; p++) {
1861                 if (p->len != dentry->d_name.len)
1862                         continue;
1863                 if (!memcmp(dentry->d_name.name, p->name, p->len))
1864                         break;
1865         }
1866         if (p > last)
1867                 goto out;
1868 
1869         error = proc_pident_instantiate(dir, dentry, task, p);
1870 out:
1871         put_task_struct(task);
1872 out_no_task:
1873         return error;
1874 }
1875 
1876 static int proc_pident_fill_cache(struct file *filp, void *dirent,
1877         filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
1878 {
1879         return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1880                                 proc_pident_instantiate, task, p);
1881 }
1882 
1883 static int proc_pident_readdir(struct file *filp,
1884                 void *dirent, filldir_t filldir,
1885                 const struct pid_entry *ents, unsigned int nents)
1886 {
1887         int i;
1888         struct dentry *dentry = filp->f_path.dentry;
1889         struct inode *inode = dentry->d_inode;
1890         struct task_struct *task = get_proc_task(inode);
1891         const struct pid_entry *p, *last;
1892         ino_t ino;
1893         int ret;
1894 
1895         ret = -ENOENT;
1896         if (!task)
1897                 goto out_no_task;
1898 
1899         ret = 0;
1900         i = filp->f_pos;
1901         switch (i) {
1902         case 0:
1903                 ino = inode->i_ino;
1904                 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1905                         goto out;
1906                 i++;
1907                 filp->f_pos++;
1908                 /* fall through */
1909         case 1:
1910                 ino = parent_ino(dentry);
1911                 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1912                         goto out;
1913                 i++;
1914                 filp->f_pos++;
1915                 /* fall through */
1916         default:
1917                 i -= 2;
1918                 if (i >= nents) {
1919                         ret = 1;
1920                         goto out;
1921                 }
1922                 p = ents + i;
1923                 last = &ents[nents - 1];
1924                 while (p <= last) {
1925                         if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
1926                                 goto out;
1927                         filp->f_pos++;
1928                         p++;
1929                 }
1930         }
1931 
1932         ret = 1;
1933 out:
1934         put_task_struct(task);
1935 out_no_task:
1936         return ret;
1937 }
1938 
1939 #ifdef CONFIG_SECURITY
1940 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1941                                   size_t count, loff_t *ppos)
1942 {
1943         struct inode * inode = file->f_path.dentry->d_inode;
1944         char *p = NULL;
1945         ssize_t length;
1946         struct task_struct *task = get_proc_task(inode);
1947 
1948         if (!task)
1949                 return -ESRCH;
1950 
1951         length = security_getprocattr(task,
1952                                       (char*)file->f_path.dentry->d_name.name,
1953                                       &p);
1954         put_task_struct(task);
1955         if (length > 0)
1956                 length = simple_read_from_buffer(buf, count, ppos, p, length);
1957         kfree(p);
1958         return length;
1959 }
1960 
1961 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1962                                    size_t count, loff_t *ppos)
1963 {
1964         struct inode * inode = file->f_path.dentry->d_inode;
1965         char *page;
1966         ssize_t length;
1967         struct task_struct *task = get_proc_task(inode);
1968 
1969         length = -ESRCH;
1970         if (!task)
1971                 goto out_no_task;
1972         if (count > PAGE_SIZE)
1973                 count = PAGE_SIZE;
1974 
1975         /* No partial writes. */
1976         length = -EINVAL;
1977         if (*ppos != 0)
1978                 goto out;
1979 
1980         length = -ENOMEM;
1981         page = (char*)__get_free_page(GFP_TEMPORARY);
1982         if (!page)
1983                 goto out;
1984 
1985         length = -EFAULT;
1986         if (copy_from_user(page, buf, count))
1987                 goto out_free;
1988 
1989         length = security_setprocattr(task,
1990                                       (char*)file->f_path.dentry->d_name.name,
1991                                       (void*)page, count);
1992 out_free:
1993         free_page((unsigned long) page);
1994 out:
1995         put_task_struct(task);
1996 out_no_task:
1997         return length;
1998 }
1999 
2000 static const struct file_operations proc_pid_attr_operations = {
2001         .read           = proc_pid_attr_read,
2002         .write          = proc_pid_attr_write,
2003 };
2004 
2005 static const struct pid_entry attr_dir_stuff[] = {
2006         REG("current",    S_IRUGO|S_IWUGO, pid_attr),
2007         REG("prev",       S_IRUGO,         pid_attr),
2008         REG("exec",       S_IRUGO|S_IWUGO, pid_attr),
2009         REG("fscreate",   S_IRUGO|S_IWUGO, pid_attr),
2010         REG("keycreate",  S_IRUGO|S_IWUGO, pid_attr),
2011         REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
2012 };
2013 
2014 static int proc_attr_dir_readdir(struct file * filp,
2015                              void * dirent, filldir_t filldir)
2016 {
2017         return proc_pident_readdir(filp,dirent,filldir,
2018                                    attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2019 }
2020 
2021 static const struct file_operations proc_attr_dir_operations = {
2022         .read           = generic_read_dir,
2023         .readdir        = proc_attr_dir_readdir,
2024 };
2025 
2026 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2027                                 struct dentry *dentry, struct nameidata *nd)
2028 {
2029         return proc_pident_lookup(dir, dentry,
2030                                   attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2031 }
2032 
2033 static const struct inode_operations proc_attr_dir_inode_operations = {
2034         .lookup         = proc_attr_dir_lookup,
2035         .getattr        = pid_getattr,
2036         .setattr        = proc_setattr,
2037 };
2038 
2039 #endif
2040 
2041 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2042 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2043                                          size_t count, loff_t *ppos)
2044 {
2045         struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2046         struct mm_struct *mm;
2047         char buffer[PROC_NUMBUF];
2048         size_t len;
2049         int ret;
2050 
2051         if (!task)
2052                 return -ESRCH;
2053 
2054         ret = 0;
2055         mm = get_task_mm(task);
2056         if (mm) {
2057                 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2058                                ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2059                                 MMF_DUMP_FILTER_SHIFT));
2060                 mmput(mm);
2061                 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2062         }
2063 
2064         put_task_struct(task);
2065 
2066         return ret;
2067 }
2068 
2069 static ssize_t proc_coredump_filter_write(struct file *file,
2070                                           const char __user *buf,
2071                                           size_t count,
2072                                           loff_t *ppos)
2073 {
2074         struct task_struct *task;
2075         struct mm_struct *mm;
2076         char buffer[PROC_NUMBUF], *end;
2077         unsigned int val;
2078         int ret;
2079         int i;
2080         unsigned long mask;
2081 
2082         ret = -EFAULT;
2083         memset(buffer, 0, sizeof(buffer));
2084         if (count > sizeof(buffer) - 1)
2085                 count = sizeof(buffer) - 1;
2086         if (copy_from_user(buffer, buf, count))
2087                 goto out_no_task;
2088 
2089         ret = -EINVAL;
2090         val = (unsigned int)simple_strtoul(buffer, &end, 0);
2091         if (*end == '\n')
2092                 end++;
2093         if (end - buffer == 0)
2094                 goto out_no_task;
2095 
2096         ret = -ESRCH;
2097         task = get_proc_task(file->f_dentry->d_inode);
2098         if (!task)
2099                 goto out_no_task;
2100 
2101         ret = end - buffer;
2102         mm = get_task_mm(task);
2103         if (!mm)
2104                 goto out_no_mm;
2105 
2106         for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2107                 if (val & mask)
2108                         set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2109                 else
2110                         clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2111         }
2112 
2113         mmput(mm);
2114  out_no_mm:
2115         put_task_struct(task);
2116  out_no_task:
2117         return ret;
2118 }
2119 
2120 static const struct file_operations proc_coredump_filter_operations = {
2121         .read           = proc_coredump_filter_read,
2122         .write          = proc_coredump_filter_write,
2123 };
2124 #endif
2125 
2126 /*
2127  * /proc/self:
2128  */
2129 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2130                               int buflen)
2131 {
2132         struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2133         pid_t tgid = task_tgid_nr_ns(current, ns);
2134         char tmp[PROC_NUMBUF];
2135         if (!tgid)
2136                 return -ENOENT;
2137         sprintf(tmp, "%d", tgid);
2138         return vfs_readlink(dentry,buffer,buflen,tmp);
2139 }
2140 
2141 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2142 {
2143         struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2144         pid_t tgid = task_tgid_nr_ns(current, ns);
2145         char tmp[PROC_NUMBUF];
2146         if (!tgid)
2147                 return ERR_PTR(-ENOENT);
2148         sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
2149         return ERR_PTR(vfs_follow_link(nd,tmp));
2150 }
2151 
2152 static const struct inode_operations proc_self_inode_operations = {
2153         .readlink       = proc_self_readlink,
2154         .follow_link    = proc_self_follow_link,
2155 };
2156 
2157 /*
2158  * proc base
2159  *
2160  * These are the directory entries in the root directory of /proc
2161  * that properly belong to the /proc filesystem, as they describe
2162  * describe something that is process related.
2163  */
2164 static const struct pid_entry proc_base_stuff[] = {
2165         NOD("self", S_IFLNK|S_IRWXUGO,
2166                 &proc_self_inode_operations, NULL, {}),
2167 };
2168 
2169 /*
2170  *      Exceptional case: normally we are not allowed to unhash a busy
2171  * directory. In this case, however, we can do it - no aliasing problems
2172  * due to the way we treat inodes.
2173  */
2174 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2175 {
2176         struct inode *inode = dentry->d_inode;
2177         struct task_struct *task = get_proc_task(inode);
2178         if (task) {
2179                 put_task_struct(task);
2180                 return 1;
2181         }
2182         d_drop(dentry);
2183         return 0;
2184 }
2185 
2186 static struct dentry_operations proc_base_dentry_operations =
2187 {
2188         .d_revalidate   = proc_base_revalidate,
2189         .d_delete       = pid_delete_dentry,
2190 };
2191 
2192 static struct dentry *proc_base_instantiate(struct inode *dir,
2193         struct dentry *dentry, struct task_struct *task, const void *ptr)
2194 {
2195         const struct pid_entry *p = ptr;
2196         struct inode *inode;
2197         struct proc_inode *ei;
2198         struct dentry *error = ERR_PTR(-EINVAL);
2199 
2200         /* Allocate the inode */
2201         error = ERR_PTR(-ENOMEM);
2202         inode = new_inode(dir->i_sb);
2203         if (!inode)
2204                 goto out;
2205 
2206         /* Initialize the inode */
2207         ei = PROC_I(inode);
2208         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2209 
2210         /*
2211          * grab the reference to the task.
2212          */
2213         ei->pid = get_task_pid(task, PIDTYPE_PID);
2214         if (!ei->pid)
2215                 goto out_iput;
2216 
2217         inode->i_uid = 0;
2218         inode->i_gid = 0;
2219         inode->i_mode = p->mode;
2220         if (S_ISDIR(inode->i_mode))
2221                 inode->i_nlink = 2;
2222         if (S_ISLNK(inode->i_mode))
2223                 inode->i_size = 64;
2224         if (p->iop)
2225                 inode->i_op = p->iop;
2226         if (p->fop)
2227                 inode->i_fop = p->fop;
2228         ei->op = p->op;
2229         dentry->d_op = &proc_base_dentry_operations;
2230         d_add(dentry, inode);
2231         error = NULL;
2232 out:
2233         return error;
2234 out_iput:
2235         iput(inode);
2236         goto out;
2237 }
2238 
2239 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2240 {
2241         struct dentry *error;
2242         struct task_struct *task = get_proc_task(dir);
2243         const struct pid_entry *p, *last;
2244 
2245         error = ERR_PTR(-ENOENT);
2246 
2247         if (!task)
2248                 goto out_no_task;
2249 
2250         /* Lookup the directory entry */
2251         last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2252         for (p = proc_base_stuff; p <= last; p++) {
2253                 if (p->len != dentry->d_name.len)
2254                         continue;
2255                 if (!memcmp(dentry->d_name.name, p->name, p->len))
2256                         break;
2257         }
2258         if (p > last)
2259                 goto out;
2260 
2261         error = proc_base_instantiate(dir, dentry, task, p);
2262 
2263 out:
2264         put_task_struct(task);
2265 out_no_task:
2266         return error;
2267 }
2268 
2269 static int proc_base_fill_cache(struct file *filp, void *dirent,
2270         filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2271 {
2272         return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2273                                 proc_base_instantiate, task, p);
2274 }
2275 
2276 #ifdef CONFIG_TASK_IO_ACCOUNTING
2277 static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
2278 {
2279         return sprintf(buffer,
2280 #ifdef CONFIG_TASK_XACCT
2281                         "rchar: %llu\n"
2282                         "wchar: %llu\n"
2283                         "syscr: %llu\n"
2284                         "syscw: %llu\n"
2285 #endif
2286                         "read_bytes: %llu\n"
2287                         "write_bytes: %llu\n"
2288                         "cancelled_write_bytes: %llu\n",
2289 #ifdef CONFIG_TASK_XACCT
2290                         (unsigned long long)task->rchar,
2291                         (unsigned long long)task->wchar,
2292                         (unsigned long long)task->syscr,
2293                         (unsigned long long)task->syscw,
2294 #endif
2295                         (unsigned long long)task->ioac.read_bytes,
2296                         (unsigned long long)task->ioac.write_bytes,
2297                         (unsigned long long)task->ioac.cancelled_write_bytes);
2298 }
2299 #endif
2300 
2301 /*
2302  * Thread groups
2303  */
2304 static const struct file_operations proc_task_operations;
2305 static const struct inode_operations proc_task_inode_operations;
2306 
2307 static const struct pid_entry tgid_base_stuff[] = {
2308         DIR("task",       S_IRUGO|S_IXUGO, task),
2309         DIR("fd",         S_IRUSR|S_IXUSR, fd),
2310         DIR("fdinfo",     S_IRUSR|S_IXUSR, fdinfo),
2311 #ifdef CONFIG_NET
2312         DIR("net",        S_IRUGO|S_IXUGO, net),
2313 #endif
2314         REG("environ",    S_IRUSR, environ),
2315         INF("auxv",       S_IRUSR, pid_auxv),
2316         ONE("status",     S_IRUGO, pid_status),
2317         INF("limits",     S_IRUSR, pid_limits),
2318 #ifdef CONFIG_SCHED_DEBUG
2319         REG("sched",      S_IRUGO|S_IWUSR, pid_sched),
2320 #endif
2321         INF("cmdline",    S_IRUGO, pid_cmdline),
2322         ONE("stat",       S_IRUGO, tgid_stat),
2323         ONE("statm",      S_IRUGO, pid_statm),
2324         REG("maps",       S_IRUGO, maps),
2325 #ifdef CONFIG_NUMA
2326         REG("numa_maps",  S_IRUGO, numa_maps),
2327 #endif
2328         REG("mem",        S_IRUSR|S_IWUSR, mem),
2329         LNK("cwd",        cwd),
2330         LNK("root",       root),
2331         LNK("exe",        exe),
2332         REG("mounts",     S_IRUGO, mounts),
2333         REG("mountstats", S_IRUSR, mountstats),
2334 #ifdef CONFIG_PROC_PAGE_MONITOR
2335         REG("clear_refs", S_IWUSR, clear_refs),
2336         REG("smaps",      S_IRUGO, smaps),
2337         REG("pagemap",    S_IRUSR, pagemap),
2338 #endif
2339 #ifdef CONFIG_SECURITY
2340         DIR("attr",       S_IRUGO|S_IXUGO, attr_dir),
2341 #endif
2342 #ifdef CONFIG_KALLSYMS
2343         INF("wchan",      S_IRUGO, pid_wchan),
2344 #endif
2345 #ifdef CONFIG_SCHEDSTATS
2346         INF("schedstat",  S_IRUGO, pid_schedstat),
2347 #endif
2348 #ifdef CONFIG_LATENCYTOP
2349         REG("latency",  S_IRUGO, lstats),
2350 #endif
2351 #ifdef CONFIG_PROC_PID_CPUSET
2352         REG("cpuset",     S_IRUGO, cpuset),
2353 #endif
2354 #ifdef CONFIG_CGROUPS
2355         REG("cgroup",  S_IRUGO, cgroup),
2356 #endif
2357         INF("oom_score",  S_IRUGO, oom_score),
2358         REG("oom_adj",    S_IRUGO|S_IWUSR, oom_adjust),
2359 #ifdef CONFIG_AUDITSYSCALL
2360         REG("loginuid",   S_IWUSR|S_IRUGO, loginuid),
2361         REG("sessionid",  S_IRUSR, sessionid),
2362 #endif
2363 #ifdef CONFIG_FAULT_INJECTION
2364         REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2365 #endif
2366 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2367         REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2368 #endif
2369 #ifdef CONFIG_TASK_IO_ACCOUNTING
2370         INF("io",       S_IRUGO, pid_io_accounting),
2371 #endif
2372 };
2373 
2374 static int proc_tgid_base_readdir(struct file * filp,
2375                              void * dirent, filldir_t filldir)
2376 {
2377         return proc_pident_readdir(filp,dirent,filldir,
2378                                    tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2379 }
2380 
2381 static const struct file_operations proc_tgid_base_operations = {
2382         .read           = generic_read_dir,
2383         .readdir        = proc_tgid_base_readdir,
2384 };
2385 
2386 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2387         return proc_pident_lookup(dir, dentry,
2388                                   tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2389 }
2390 
2391 static const struct inode_operations proc_tgid_base_inode_operations = {
2392         .lookup         = proc_tgid_base_lookup,
2393         .getattr        = pid_getattr,
2394         .setattr        = proc_setattr,
2395 };
2396 
2397 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2398 {
2399         struct dentry *dentry, *leader, *dir;
2400         char buf[PROC_NUMBUF];
2401         struct qstr name;
2402 
2403         name.name = buf;
2404         name.len = snprintf(buf, sizeof(buf), "%d", pid);
2405         dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2406         if (dentry) {
2407                 if (!(current->flags & PF_EXITING))
2408                         shrink_dcache_parent(dentry);
2409                 d_drop(dentry);
2410                 dput(dentry);
2411         }
2412 
2413         if (tgid == 0)
2414                 goto out;
2415 
2416         name.name = buf;
2417         name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2418         leader = d_hash_and_lookup(mnt->mnt_root, &name);
2419         if (!leader)
2420                 goto out;
2421 
2422         name.name = "task";
2423         name.len = strlen(name.name);
2424         dir = d_hash_and_lookup(leader, &name);
2425         if (!dir)
2426                 goto out_put_leader;
2427 
2428         name.name = buf;
2429         name.len = snprintf(buf, sizeof(buf), "%d", pid);
2430         dentry = d_hash_and_lookup(dir, &name);
2431         if (dentry) {
2432                 shrink_dcache_parent(dentry);
2433                 d_drop(dentry);
2434                 dput(dentry);
2435         }
2436 
2437         dput(dir);
2438 out_put_leader:
2439         dput(leader);
2440 out:
2441         return;
2442 }
2443 
2444 /**
2445  * proc_flush_task -  Remove dcache entries for @task from the /proc dcache.
2446  * @task: task that should be flushed.
2447  *
2448  * When flushing dentries from proc, one needs to flush them from global
2449  * proc (proc_mnt) and from all the namespaces' procs this task was seen
2450  * in. This call is supposed to do all of this job.
2451  *
2452  * Looks in the dcache for
2453  * /proc/@pid
2454  * /proc/@tgid/task/@pid
2455  * if either directory is present flushes it and all of it'ts children
2456  * from the dcache.
2457  *
2458  * It is safe and reasonable to cache /proc entries for a task until
2459  * that task exits.  After that they just clog up the dcache with
2460  * useless entries, possibly causing useful dcache entries to be
2461  * flushed instead.  This routine is proved to flush those useless
2462  * dcache entries at process exit time.
2463  *
2464  * NOTE: This routine is just an optimization so it does not guarantee
2465  *       that no dcache entries will exist at process exit time it
2466  *       just makes it very unlikely that any will persist.
2467  */
2468 
2469 void proc_flush_task(struct task_struct *task)
2470 {
2471         int i;
2472         struct pid *pid, *tgid = NULL;
2473         struct upid *upid;
2474 
2475         pid = task_pid(task);
2476         if (thread_group_leader(task))
2477                 tgid = task_tgid(task);
2478 
2479         for (i = 0; i <= pid->level; i++) {
2480                 upid = &pid->numbers[i];
2481                 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2482                         tgid ? tgid->numbers[i].nr : 0);
2483         }
2484 
2485         upid = &pid->numbers[pid->level];
2486         if (upid->nr == 1)
2487                 pid_ns_release_proc(upid->ns);
2488 }
2489 
2490 static struct dentry *proc_pid_instantiate(struct inode *dir,
2491                                            struct dentry * dentry,
2492                                            struct task_struct *task, const void *ptr)
2493 {
2494         struct dentry *error = ERR_PTR(-ENOENT);
2495         struct inode *inode;
2496 
2497         inode = proc_pid_make_inode(dir->i_sb, task);
2498         if (!inode)
2499                 goto out;
2500 
2501         inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2502         inode->i_op = &proc_tgid_base_inode_operations;
2503         inode->i_fop = &proc_tgid_base_operations;
2504         inode->i_flags|=S_IMMUTABLE;
2505 
2506         inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
2507                 ARRAY_SIZE(tgid_base_stuff));
2508 
2509         dentry->d_op = &pid_dentry_operations;
2510 
2511         d_add(dentry, inode);
2512         /* Close the race of the process dying before we return the dentry */
2513         if (pid_revalidate(dentry, NULL))
2514                 error = NULL;
2515 out:
2516         return error;
2517 }
2518 
2519 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2520 {
2521         struct dentry *result = ERR_PTR(-ENOENT);
2522         struct task_struct *task;
2523         unsigned tgid;
2524         struct pid_namespace *ns;
2525 
2526         result = proc_base_lookup(dir, dentry);
2527         if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2528                 goto out;
2529 
2530         tgid = name_to_int(dentry);
2531         if (tgid == ~0U)
2532                 goto out;
2533 
2534         ns = dentry->d_sb->s_fs_info;
2535         rcu_read_lock();
2536         task = find_task_by_pid_ns(tgid, ns);
2537         if (task)
2538                 get_task_struct(task);
2539         rcu_read_unlock();
2540         if (!task)
2541                 goto out;
2542 
2543         result = proc_pid_instantiate(dir, dentry, task, NULL);
2544         put_task_struct(task);
2545 out:
2546         return result;
2547 }
2548 
2549 /*
2550  * Find the first task with tgid >= tgid
2551  *
2552  */
2553 struct tgid_iter {
2554         unsigned int tgid;
2555         struct task_struct *task;
2556 };
2557 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2558 {
2559         struct pid *pid;
2560 
2561         if (iter.task)
2562                 put_task_struct(iter.task);
2563         rcu_read_lock();
2564 retry:
2565         iter.task = NULL;
2566         pid = find_ge_pid(iter.tgid, ns);
2567         if (pid) {
2568                 iter.tgid = pid_nr_ns(pid, ns);
2569                 iter.task = pid_task(pid, PIDTYPE_PID);
2570                 /* What we to know is if the pid we have find is the
2571                  * pid of a thread_group_leader.  Testing for task
2572                  * being a thread_group_leader is the obvious thing
2573                  * todo but there is a window when it fails, due to
2574                  * the pid transfer logic in de_thread.
2575                  *
2576                  * So we perform the straight forward test of seeing
2577                  * if the pid we have found is the pid of a thread
2578                  * group leader, and don't worry if the task we have
2579                  * found doesn't happen to be a thread group leader.
2580                  * As we don't care in the case of readdir.
2581                  */
2582                 if (!iter.task || !has_group_leader_pid(iter.task)) {
2583                         iter.tgid += 1;
2584                         goto retry;
2585                 }
2586                 get_task_struct(iter.task);
2587         }
2588         rcu_read_unlock();
2589         return iter;
2590 }
2591 
2592 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2593 
2594 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2595         struct tgid_iter iter)
2596 {
2597         char name[PROC_NUMBUF];
2598         int len = snprintf(name, sizeof(name), "%d", iter.tgid);
2599         return proc_fill_cache(filp, dirent, filldir, name, len,
2600                                 proc_pid_instantiate, iter.task, NULL);
2601 }
2602 
2603 /* for the /proc/ directory itself, after non-process stuff has been done */
2604 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2605 {
2606         unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2607         struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2608         struct tgid_iter iter;
2609         struct pid_namespace *ns;
2610 
2611         if (!reaper)
2612                 goto out_no_task;
2613 
2614         for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2615                 const struct pid_entry *p = &proc_base_stuff[nr];
2616                 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2617                         goto out;
2618         }
2619 
2620         ns = filp->f_dentry->d_sb->s_fs_info;
2621         iter.task = NULL;
2622         iter.tgid = filp->f_pos - TGID_OFFSET;
2623         for (iter = next_tgid(ns, iter);
2624              iter.task;
2625              iter.tgid += 1, iter = next_tgid(ns, iter)) {
2626                 filp->f_pos = iter.tgid + TGID_OFFSET;
2627                 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2628                         put_task_struct(iter.task);
2629                         goto out;
2630                 }
2631         }
2632         filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2633 out:
2634         put_task_struct(reaper);
2635 out_no_task:
2636         return 0;
2637 }
2638 
2639 /*
2640  * Tasks
2641  */
2642 static const struct pid_entry tid_base_stuff[] = {
2643         DIR("fd",        S_IRUSR|S_IXUSR, fd),
2644         DIR("fdinfo",    S_IRUSR|S_IXUSR, fdinfo),
2645         REG("environ",   S_IRUSR, environ),
2646         INF("auxv",      S_IRUSR, pid_auxv),
2647         ONE("status",    S_IRUGO, pid_status),
2648         INF("limits",    S_IRUSR, pid_limits),
2649 #ifdef CONFIG_SCHED_DEBUG
2650         REG("sched",     S_IRUGO|S_IWUSR, pid_sched),
2651 #endif
2652         INF("cmdline",   S_IRUGO, pid_cmdline),
2653         ONE("stat",      S_IRUGO, tid_stat),
2654         ONE("statm",     S_IRUGO, pid_statm),
2655         REG("maps",      S_IRUGO, maps),
2656 #ifdef CONFIG_NUMA
2657         REG("numa_maps", S_IRUGO, numa_maps),
2658 #endif
2659         REG("mem",       S_IRUSR|S_IWUSR, mem),
2660         LNK("cwd",       cwd),
2661         LNK("root",      root),
2662         LNK("exe",       exe),
2663         REG("mounts",    S_IRUGO, mounts),
2664 #ifdef CONFIG_PROC_PAGE_MONITOR
2665         REG("clear_refs", S_IWUSR, clear_refs),
2666         REG("smaps",     S_IRUGO, smaps),
2667         REG("pagemap",    S_IRUSR, pagemap),
2668 #endif
2669 #ifdef CONFIG_SECURITY
2670         DIR("attr",      S_IRUGO|S_IXUGO, attr_dir),
2671 #endif
2672 #ifdef CONFIG_KALLSYMS
2673         INF("wchan",     S_IRUGO, pid_wchan),
2674 #endif
2675 #ifdef CONFIG_SCHEDSTATS
2676         INF("schedstat", S_IRUGO, pid_schedstat),
2677 #endif
2678 #ifdef CONFIG_LATENCYTOP
2679         REG("latency",  S_IRUGO, lstats),
2680 #endif
2681 #ifdef CONFIG_PROC_PID_CPUSET
2682         REG("cpuset",    S_IRUGO, cpuset),
2683 #endif
2684 #ifdef CONFIG_CGROUPS
2685         REG("cgroup",  S_IRUGO, cgroup),
2686 #endif
2687         INF("oom_score", S_IRUGO, oom_score),
2688         REG("oom_adj",   S_IRUGO|S_IWUSR, oom_adjust),
2689 #ifdef CONFIG_AUDITSYSCALL
2690         REG("loginuid",  S_IWUSR|S_IRUGO, loginuid),
2691         REG("sessionid",  S_IRUSR, sessionid),
2692 #endif
2693 #ifdef CONFIG_FAULT_INJECTION
2694         REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2695 #endif
2696 };
2697 
2698 static int proc_tid_base_readdir(struct file * filp,
2699                              void * dirent, filldir_t filldir)
2700 {
2701         return proc_pident_readdir(filp,dirent,filldir,
2702                                    tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2703 }
2704 
2705 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2706         return proc_pident_lookup(dir, dentry,
2707                                   tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2708 }
2709 
2710 static const struct file_operations proc_tid_base_operations = {
2711         .read           = generic_read_dir,
2712         .readdir        = proc_tid_base_readdir,
2713 };
2714 
2715 static const struct inode_operations proc_tid_base_inode_operations = {
2716         .lookup         = proc_tid_base_lookup,
2717         .getattr        = pid_getattr,
2718         .setattr        = proc_setattr,
2719 };
2720 
2721 static struct dentry *proc_task_instantiate(struct inode *dir,
2722         struct dentry *dentry, struct task_struct *task, const void *ptr)
2723 {
2724         struct dentry *error = ERR_PTR(-ENOENT);
2725         struct inode *inode;
2726         inode = proc_pid_make_inode(dir->i_sb, task);
2727 
2728         if (!inode)
2729                 goto out;
2730         inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2731         inode->i_op = &proc_tid_base_inode_operations;
2732         inode->i_fop = &proc_tid_base_operations;
2733         inode->i_flags|=S_IMMUTABLE;
2734 
2735         inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
2736                 ARRAY_SIZE(tid_base_stuff));
2737 
2738         dentry->d_op = &pid_dentry_operations;
2739 
2740         d_add(dentry, inode);
2741         /* Close the race of the process dying before we return the dentry */
2742         if (pid_revalidate(dentry, NULL))
2743                 error = NULL;
2744 out:
2745         return error;
2746 }
2747 
2748 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2749 {
2750         struct dentry *result = ERR_PTR(-ENOENT);
2751         struct task_struct *task;
2752         struct task_struct *leader = get_proc_task(dir);
2753         unsigned tid;
2754         struct pid_namespace *ns;
2755 
2756         if (!leader)
2757                 goto out_no_task;
2758 
2759         tid = name_to_int(dentry);
2760         if (tid == ~0U)
2761                 goto out;
2762 
2763         ns = dentry->d_sb->s_fs_info;
2764         rcu_read_lock();
2765         task = find_task_by_pid_ns(tid, ns);
2766         if (task)
2767                 get_task_struct(task);
2768         rcu_read_unlock();
2769         if (!task)
2770                 goto out;
2771         if (!same_thread_group(leader, task))
2772                 goto out_drop_task;
2773 
2774         result = proc_task_instantiate(dir, dentry, task, NULL);
2775 out_drop_task:
2776         put_task_struct(task);
2777 out:
2778         put_task_struct(leader);
2779 out_no_task:
2780         return result;
2781 }
2782 
2783 /*
2784  * Find the first tid of a thread group to return to user space.
2785  *
2786  * Usually this is just the thread group leader, but if the users
2787  * buffer was too small or there was a seek into the middle of the
2788  * directory we have more work todo.
2789  *
2790  * In the case of a short read we start with find_task_by_pid.
2791  *
2792  * In the case of a seek we start with the leader and walk nr
2793  * threads past it.
2794  */
2795 static struct task_struct *first_tid(struct task_struct *leader,
2796                 int tid, int nr, struct pid_namespace *ns)
2797 {
2798         struct task_struct *pos;
2799 
2800         rcu_read_lock();
2801         /* Attempt to start with the pid of a thread */
2802         if (tid && (nr > 0)) {
2803                 pos = find_task_by_pid_ns(tid, ns);
2804                 if (pos && (pos->group_leader == leader))
2805                         goto found;
2806         }
2807 
2808         /* If nr exceeds the number of threads there is nothing todo */
2809         pos = NULL;
2810         if (nr && nr >= get_nr_threads(leader))
2811                 goto out;
2812 
2813         /* If we haven't found our starting place yet start
2814          * with the leader and walk nr threads forward.
2815          */
2816         for (pos = leader; nr > 0; --nr) {
2817                 pos = next_thread(pos);
2818                 if (pos == leader) {
2819                         pos = NULL;
2820                         goto out;
2821                 }
2822         }
2823 found:
2824         get_task_struct(pos);
2825 out:
2826         rcu_read_unlock();
2827         return pos;
2828 }
2829 
2830 /*
2831  * Find the next thread in the thread list.
2832  * Return NULL if there is an error or no next thread.
2833  *
2834  * The reference to the input task_struct is released.
2835  */
2836 static struct task_struct *next_tid(struct task_struct *start)
2837 {
2838         struct task_struct *pos = NULL;
2839         rcu_read_lock();
2840         if (pid_alive(start)) {
2841                 pos = next_thread(start);
2842                 if (thread_group_leader(pos))
2843                         pos = NULL;
2844                 else
2845                         get_task_struct(pos);
2846         }
2847         rcu_read_unlock();
2848         put_task_struct(start);
2849         return pos;
2850 }
2851 
2852 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2853         struct task_struct *task, int tid)
2854 {
2855         char name[PROC_NUMBUF];
2856         int len = snprintf(name, sizeof(name), "%d", tid);
2857         return proc_fill_cache(filp, dirent, filldir, name, len,
2858                                 proc_task_instantiate, task, NULL);
2859 }
2860 
2861 /* for the /proc/TGID/task/ directories */
2862 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2863 {
2864         struct dentry *dentry = filp->f_path.dentry;
2865         struct inode *inode = dentry->d_inode;
2866         struct task_struct *leader = NULL;
2867         struct task_struct *task;
2868         int retval = -ENOENT;
2869         ino_t ino;
2870         int tid;
2871         unsigned long pos = filp->f_pos;  /* avoiding "long long" filp->f_pos */
2872         struct pid_namespace *ns;
2873 
2874         task = get_proc_task(inode);
2875         if (!task)
2876                 goto out_no_task;
2877         rcu_read_lock();
2878         if (pid_alive(task)) {
2879                 leader = task->group_leader;
2880                 get_task_struct(leader);
2881         }
2882         rcu_read_unlock();
2883         put_task_struct(task);
2884         if (!leader)
2885                 goto out_no_task;
2886         retval = 0;
2887 
2888         switch (pos) {
2889         case 0:
2890                 ino = inode->i_ino;
2891                 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2892                         goto out;
2893                 pos++;
2894                 /* fall through */
2895         case 1:
2896                 ino = parent_ino(dentry);
2897                 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2898                         goto out;
2899                 pos++;
2900                 /* fall through */
2901         }
2902 
2903         /* f_version caches the tgid value that the last readdir call couldn't
2904          * return. lseek aka telldir automagically resets f_version to 0.
2905          */
2906         ns = filp->f_dentry->d_sb->s_fs_info;
2907         tid = (int)filp->f_version;
2908         filp->f_version = 0;
2909         for (task = first_tid(leader, tid, pos - 2, ns);
2910              task;
2911              task = next_tid(task), pos++) {
2912                 tid = task_pid_nr_ns(task, ns);
2913                 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
2914                         /* returning this tgid failed, save it as the first
2915                          * pid for the next readir call */
2916                         filp->f_version = (u64)tid;
2917                         put_task_struct(task);
2918                         break;
2919                 }
2920         }
2921 out:
2922         filp->f_pos = pos;
2923         put_task_struct(leader);
2924 out_no_task:
2925         return retval;
2926 }
2927 
2928 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2929 {
2930         struct inode *inode = dentry->d_inode;
2931         struct task_struct *p = get_proc_task(inode);
2932         generic_fillattr(inode, stat);
2933 
2934         if (p) {
2935                 rcu_read_lock();
2936                 stat->nlink += get_nr_threads(p);
2937                 rcu_read_unlock();
2938                 put_task_struct(p);
2939         }
2940 
2941         return 0;
2942 }
2943 
2944 static const struct inode_operations proc_task_inode_operations = {
2945         .lookup         = proc_task_lookup,
2946         .getattr        = proc_task_getattr,
2947         .setattr        = proc_setattr,
2948 };
2949 
2950 static const struct file_operations proc_task_operations = {
2951         .read           = generic_read_dir,
2952         .readdir        = proc_task_readdir,
2953 };
2954 
  This page was automatically generated by the LXR engine.