[PATCH] AVR32: Implement kernel_execve
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / proc / base.c
CommitLineData
1da177e4
LT
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.
e070ad49
ML
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.
1da177e4
LT
48 */
49
50#include <asm/uaccess.h>
51
1da177e4
LT
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>
16f7e0fe 57#include <linux/capability.h>
1da177e4
LT
58#include <linux/file.h>
59#include <linux/string.h>
60#include <linux/seq_file.h>
61#include <linux/namei.h>
62#include <linux/namespace.h>
63#include <linux/mm.h>
64#include <linux/smp_lock.h>
b835996f 65#include <linux/rcupdate.h>
1da177e4
LT
66#include <linux/kallsyms.h>
67#include <linux/mount.h>
68#include <linux/security.h>
69#include <linux/ptrace.h>
70#include <linux/seccomp.h>
71#include <linux/cpuset.h>
72#include <linux/audit.h>
5addc5dd 73#include <linux/poll.h>
1651e14e 74#include <linux/nsproxy.h>
1da177e4
LT
75#include "internal.h"
76
0f2fe20f
EB
77/* NOTE:
78 * Implementing inode permission operations in /proc is almost
79 * certainly an error. Permission checks need to happen during
80 * each system call not at open time. The reason is that most of
81 * what we wish to check for permissions in /proc varies at runtime.
82 *
83 * The classic example of a problem is opening file descriptors
84 * in /proc for a task before it execs a suid executable.
85 */
86
1da177e4
LT
87/*
88 * For hysterical raisins we keep the same inumbers as in the old procfs.
89 * Feel free to change the macro below - just keep the range distinct from
90 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
91 * As soon as we'll get a separate superblock we will be able to forget
92 * about magical ranges too.
93 */
94
95#define fake_ino(pid,ino) (((pid)<<16)|(ino))
96
97enum pid_directory_inos {
98 PROC_TGID_INO = 2,
99 PROC_TGID_TASK,
100 PROC_TGID_STATUS,
101 PROC_TGID_MEM,
102#ifdef CONFIG_SECCOMP
103 PROC_TGID_SECCOMP,
104#endif
105 PROC_TGID_CWD,
106 PROC_TGID_ROOT,
107 PROC_TGID_EXE,
108 PROC_TGID_FD,
109 PROC_TGID_ENVIRON,
110 PROC_TGID_AUXV,
111 PROC_TGID_CMDLINE,
112 PROC_TGID_STAT,
113 PROC_TGID_STATM,
114 PROC_TGID_MAPS,
6e21c8f1 115 PROC_TGID_NUMA_MAPS,
1da177e4 116 PROC_TGID_MOUNTS,
b4629fe2 117 PROC_TGID_MOUNTSTATS,
1da177e4 118 PROC_TGID_WCHAN,
63c6764c 119#ifdef CONFIG_MMU
e070ad49 120 PROC_TGID_SMAPS,
63c6764c 121#endif
1da177e4
LT
122#ifdef CONFIG_SCHEDSTATS
123 PROC_TGID_SCHEDSTAT,
124#endif
125#ifdef CONFIG_CPUSETS
126 PROC_TGID_CPUSET,
127#endif
128#ifdef CONFIG_SECURITY
129 PROC_TGID_ATTR,
130 PROC_TGID_ATTR_CURRENT,
131 PROC_TGID_ATTR_PREV,
132 PROC_TGID_ATTR_EXEC,
133 PROC_TGID_ATTR_FSCREATE,
4eb582cf 134 PROC_TGID_ATTR_KEYCREATE,
42c3e03e 135 PROC_TGID_ATTR_SOCKCREATE,
1da177e4
LT
136#endif
137#ifdef CONFIG_AUDITSYSCALL
138 PROC_TGID_LOGINUID,
139#endif
1da177e4
LT
140 PROC_TGID_OOM_SCORE,
141 PROC_TGID_OOM_ADJUST,
142 PROC_TID_INO,
143 PROC_TID_STATUS,
144 PROC_TID_MEM,
145#ifdef CONFIG_SECCOMP
146 PROC_TID_SECCOMP,
147#endif
148 PROC_TID_CWD,
149 PROC_TID_ROOT,
150 PROC_TID_EXE,
151 PROC_TID_FD,
152 PROC_TID_ENVIRON,
153 PROC_TID_AUXV,
154 PROC_TID_CMDLINE,
155 PROC_TID_STAT,
156 PROC_TID_STATM,
157 PROC_TID_MAPS,
6e21c8f1 158 PROC_TID_NUMA_MAPS,
1da177e4 159 PROC_TID_MOUNTS,
b4629fe2 160 PROC_TID_MOUNTSTATS,
1da177e4 161 PROC_TID_WCHAN,
63c6764c 162#ifdef CONFIG_MMU
e070ad49 163 PROC_TID_SMAPS,
63c6764c 164#endif
1da177e4
LT
165#ifdef CONFIG_SCHEDSTATS
166 PROC_TID_SCHEDSTAT,
167#endif
168#ifdef CONFIG_CPUSETS
169 PROC_TID_CPUSET,
170#endif
171#ifdef CONFIG_SECURITY
172 PROC_TID_ATTR,
173 PROC_TID_ATTR_CURRENT,
174 PROC_TID_ATTR_PREV,
175 PROC_TID_ATTR_EXEC,
176 PROC_TID_ATTR_FSCREATE,
4eb582cf 177 PROC_TID_ATTR_KEYCREATE,
42c3e03e 178 PROC_TID_ATTR_SOCKCREATE,
1da177e4
LT
179#endif
180#ifdef CONFIG_AUDITSYSCALL
181 PROC_TID_LOGINUID,
182#endif
1da177e4
LT
183 PROC_TID_OOM_SCORE,
184 PROC_TID_OOM_ADJUST,
5e21ccb1
MS
185
186 /* Add new entries before this */
187 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
1da177e4
LT
188};
189
8578cea7
EB
190/* Worst case buffer size needed for holding an integer. */
191#define PROC_NUMBUF 10
192
1da177e4
LT
193struct pid_entry {
194 int type;
195 int len;
196 char *name;
197 mode_t mode;
20cdc894
EB
198 struct inode_operations *iop;
199 struct file_operations *fop;
200 union proc_op op;
1da177e4
LT
201};
202
20cdc894
EB
203#define NOD(TYPE, NAME, MODE, IOP, FOP, OP) { \
204 .type = (TYPE), \
205 .len = sizeof(NAME) - 1, \
206 .name = (NAME), \
207 .mode = MODE, \
208 .iop = IOP, \
209 .fop = FOP, \
210 .op = OP, \
211}
212
213#define DIR(TYPE, NAME, MODE, OTYPE) \
214 NOD(TYPE, NAME, (S_IFDIR|(MODE)), \
215 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
216 {} )
217#define LNK(TYPE, NAME, OTYPE) \
218 NOD(TYPE, NAME, (S_IFLNK|S_IRWXUGO), \
219 &proc_pid_link_inode_operations, NULL, \
220 { .proc_get_link = &proc_##OTYPE##_link } )
221#define REG(TYPE, NAME, MODE, OTYPE) \
222 NOD(TYPE, NAME, (S_IFREG|(MODE)), NULL, \
223 &proc_##OTYPE##_operations, {})
224#define INF(TYPE, NAME, MODE, OTYPE) \
225 NOD(TYPE, NAME, (S_IFREG|(MODE)), \
226 NULL, &proc_info_file_operations, \
227 { .proc_read = &proc_##OTYPE } )
1da177e4 228
0494f6ec 229static struct fs_struct *get_fs_struct(struct task_struct *task)
1da177e4
LT
230{
231 struct fs_struct *fs;
0494f6ec
MS
232 task_lock(task);
233 fs = task->fs;
1da177e4
LT
234 if(fs)
235 atomic_inc(&fs->count);
0494f6ec
MS
236 task_unlock(task);
237 return fs;
238}
239
99f89551
EB
240static int get_nr_threads(struct task_struct *tsk)
241{
242 /* Must be called with the rcu_read_lock held */
243 unsigned long flags;
244 int count = 0;
245
246 if (lock_task_sighand(tsk, &flags)) {
247 count = atomic_read(&tsk->signal->count);
248 unlock_task_sighand(tsk, &flags);
249 }
250 return count;
251}
252
0494f6ec
MS
253static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
254{
99f89551
EB
255 struct task_struct *task = get_proc_task(inode);
256 struct fs_struct *fs = NULL;
0494f6ec 257 int result = -ENOENT;
99f89551
EB
258
259 if (task) {
260 fs = get_fs_struct(task);
261 put_task_struct(task);
262 }
1da177e4
LT
263 if (fs) {
264 read_lock(&fs->lock);
265 *mnt = mntget(fs->pwdmnt);
266 *dentry = dget(fs->pwd);
267 read_unlock(&fs->lock);
268 result = 0;
269 put_fs_struct(fs);
270 }
271 return result;
272}
273
274static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
275{
99f89551
EB
276 struct task_struct *task = get_proc_task(inode);
277 struct fs_struct *fs = NULL;
1da177e4 278 int result = -ENOENT;
99f89551
EB
279
280 if (task) {
281 fs = get_fs_struct(task);
282 put_task_struct(task);
283 }
1da177e4
LT
284 if (fs) {
285 read_lock(&fs->lock);
286 *mnt = mntget(fs->rootmnt);
287 *dentry = dget(fs->root);
288 read_unlock(&fs->lock);
289 result = 0;
290 put_fs_struct(fs);
291 }
292 return result;
293}
294
295#define MAY_PTRACE(task) \
296 (task == current || \
297 (task->parent == current && \
298 (task->ptrace & PT_PTRACED) && \
299 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
300 security_ptrace(current,task) == 0))
301
1da177e4
LT
302static int proc_pid_environ(struct task_struct *task, char * buffer)
303{
304 int res = 0;
305 struct mm_struct *mm = get_task_mm(task);
306 if (mm) {
307 unsigned int len = mm->env_end - mm->env_start;
308 if (len > PAGE_SIZE)
309 len = PAGE_SIZE;
310 res = access_process_vm(task, mm->env_start, buffer, len, 0);
ab8d11be 311 if (!ptrace_may_attach(task))
1da177e4
LT
312 res = -ESRCH;
313 mmput(mm);
314 }
315 return res;
316}
317
318static int proc_pid_cmdline(struct task_struct *task, char * buffer)
319{
320 int res = 0;
321 unsigned int len;
322 struct mm_struct *mm = get_task_mm(task);
323 if (!mm)
324 goto out;
325 if (!mm->arg_end)
326 goto out_mm; /* Shh! No looking before we're done */
327
328 len = mm->arg_end - mm->arg_start;
329
330 if (len > PAGE_SIZE)
331 len = PAGE_SIZE;
332
333 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
334
335 // If the nul at the end of args has been overwritten, then
336 // assume application is using setproctitle(3).
337 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
338 len = strnlen(buffer, res);
339 if (len < res) {
340 res = len;
341 } else {
342 len = mm->env_end - mm->env_start;
343 if (len > PAGE_SIZE - res)
344 len = PAGE_SIZE - res;
345 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
346 res = strnlen(buffer, res);
347 }
348 }
349out_mm:
350 mmput(mm);
351out:
352 return res;
353}
354
355static int proc_pid_auxv(struct task_struct *task, char *buffer)
356{
357 int res = 0;
358 struct mm_struct *mm = get_task_mm(task);
359 if (mm) {
360 unsigned int nwords = 0;
361 do
362 nwords += 2;
363 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
364 res = nwords * sizeof(mm->saved_auxv[0]);
365 if (res > PAGE_SIZE)
366 res = PAGE_SIZE;
367 memcpy(buffer, mm->saved_auxv, res);
368 mmput(mm);
369 }
370 return res;
371}
372
373
374#ifdef CONFIG_KALLSYMS
375/*
376 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
377 * Returns the resolved symbol. If that fails, simply return the address.
378 */
379static int proc_pid_wchan(struct task_struct *task, char *buffer)
380{
381 char *modname;
382 const char *sym_name;
383 unsigned long wchan, size, offset;
384 char namebuf[KSYM_NAME_LEN+1];
385
386 wchan = get_wchan(task);
387
388 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
389 if (sym_name)
390 return sprintf(buffer, "%s", sym_name);
391 return sprintf(buffer, "%lu", wchan);
392}
393#endif /* CONFIG_KALLSYMS */
394
395#ifdef CONFIG_SCHEDSTATS
396/*
397 * Provides /proc/PID/schedstat
398 */
399static int proc_pid_schedstat(struct task_struct *task, char *buffer)
400{
401 return sprintf(buffer, "%lu %lu %lu\n",
402 task->sched_info.cpu_time,
403 task->sched_info.run_delay,
404 task->sched_info.pcnt);
405}
406#endif
407
408/* The badness from the OOM killer */
409unsigned long badness(struct task_struct *p, unsigned long uptime);
410static int proc_oom_score(struct task_struct *task, char *buffer)
411{
412 unsigned long points;
413 struct timespec uptime;
414
415 do_posix_clock_monotonic_gettime(&uptime);
416 points = badness(task, uptime.tv_sec);
417 return sprintf(buffer, "%lu\n", points);
418}
419
420/************************************************************************/
421/* Here the fs part begins */
422/************************************************************************/
423
424/* permission checks */
778c1144 425static int proc_fd_access_allowed(struct inode *inode)
1da177e4 426{
778c1144
EB
427 struct task_struct *task;
428 int allowed = 0;
df26c40e
EB
429 /* Allow access to a task's file descriptors if it is us or we
430 * may use ptrace attach to the process and find out that
431 * information.
778c1144
EB
432 */
433 task = get_proc_task(inode);
df26c40e
EB
434 if (task) {
435 allowed = ptrace_may_attach(task);
778c1144 436 put_task_struct(task);
df26c40e 437 }
778c1144 438 return allowed;
1da177e4
LT
439}
440
6d76fa58
LT
441static int proc_setattr(struct dentry *dentry, struct iattr *attr)
442{
443 int error;
444 struct inode *inode = dentry->d_inode;
445
446 if (attr->ia_valid & ATTR_MODE)
447 return -EPERM;
448
449 error = inode_change_ok(inode, attr);
450 if (!error) {
451 error = security_inode_setattr(dentry, attr);
452 if (!error)
453 error = inode_setattr(inode, attr);
454 }
455 return error;
456}
457
458static struct inode_operations proc_def_inode_operations = {
459 .setattr = proc_setattr,
460};
461
1da177e4 462extern struct seq_operations mounts_op;
5addc5dd
AV
463struct proc_mounts {
464 struct seq_file m;
465 int event;
466};
467
1da177e4
LT
468static int mounts_open(struct inode *inode, struct file *file)
469{
99f89551
EB
470 struct task_struct *task = get_proc_task(inode);
471 struct namespace *namespace = NULL;
5addc5dd
AV
472 struct proc_mounts *p;
473 int ret = -EINVAL;
1da177e4 474
99f89551
EB
475 if (task) {
476 task_lock(task);
1651e14e 477 namespace = task->nsproxy->namespace;
99f89551
EB
478 if (namespace)
479 get_namespace(namespace);
480 task_unlock(task);
481 put_task_struct(task);
482 }
5addc5dd
AV
483
484 if (namespace) {
485 ret = -ENOMEM;
486 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
487 if (p) {
488 file->private_data = &p->m;
489 ret = seq_open(file, &mounts_op);
490 if (!ret) {
491 p->m.private = namespace;
492 p->event = namespace->event;
493 return 0;
494 }
495 kfree(p);
1da177e4 496 }
5addc5dd 497 put_namespace(namespace);
1da177e4
LT
498 }
499 return ret;
500}
501
502static int mounts_release(struct inode *inode, struct file *file)
503{
504 struct seq_file *m = file->private_data;
505 struct namespace *namespace = m->private;
506 put_namespace(namespace);
507 return seq_release(inode, file);
508}
509
5addc5dd
AV
510static unsigned mounts_poll(struct file *file, poll_table *wait)
511{
512 struct proc_mounts *p = file->private_data;
513 struct namespace *ns = p->m.private;
514 unsigned res = 0;
515
516 poll_wait(file, &ns->poll, wait);
517
518 spin_lock(&vfsmount_lock);
519 if (p->event != ns->event) {
520 p->event = ns->event;
521 res = POLLERR;
522 }
523 spin_unlock(&vfsmount_lock);
524
525 return res;
526}
527
1da177e4
LT
528static struct file_operations proc_mounts_operations = {
529 .open = mounts_open,
530 .read = seq_read,
531 .llseek = seq_lseek,
532 .release = mounts_release,
5addc5dd 533 .poll = mounts_poll,
1da177e4
LT
534};
535
b4629fe2
CL
536extern struct seq_operations mountstats_op;
537static int mountstats_open(struct inode *inode, struct file *file)
538{
b4629fe2
CL
539 int ret = seq_open(file, &mountstats_op);
540
541 if (!ret) {
542 struct seq_file *m = file->private_data;
99f89551
EB
543 struct namespace *namespace = NULL;
544 struct task_struct *task = get_proc_task(inode);
545
546 if (task) {
547 task_lock(task);
1651e14e 548 namespace = task->nsproxy->namespace;
99f89551
EB
549 if (namespace)
550 get_namespace(namespace);
551 task_unlock(task);
552 put_task_struct(task);
553 }
b4629fe2
CL
554
555 if (namespace)
556 m->private = namespace;
557 else {
558 seq_release(inode, file);
559 ret = -EINVAL;
560 }
561 }
562 return ret;
563}
564
565static struct file_operations proc_mountstats_operations = {
566 .open = mountstats_open,
567 .read = seq_read,
568 .llseek = seq_lseek,
569 .release = mounts_release,
570};
571
1da177e4
LT
572#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
573
574static ssize_t proc_info_read(struct file * file, char __user * buf,
575 size_t count, loff_t *ppos)
576{
577 struct inode * inode = file->f_dentry->d_inode;
578 unsigned long page;
579 ssize_t length;
99f89551
EB
580 struct task_struct *task = get_proc_task(inode);
581
582 length = -ESRCH;
583 if (!task)
584 goto out_no_task;
1da177e4
LT
585
586 if (count > PROC_BLOCK_SIZE)
587 count = PROC_BLOCK_SIZE;
99f89551
EB
588
589 length = -ENOMEM;
1da177e4 590 if (!(page = __get_free_page(GFP_KERNEL)))
99f89551 591 goto out;
1da177e4
LT
592
593 length = PROC_I(inode)->op.proc_read(task, (char*)page);
594
595 if (length >= 0)
596 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
597 free_page(page);
99f89551
EB
598out:
599 put_task_struct(task);
600out_no_task:
1da177e4
LT
601 return length;
602}
603
604static struct file_operations proc_info_file_operations = {
605 .read = proc_info_read,
606};
607
608static int mem_open(struct inode* inode, struct file* file)
609{
610 file->private_data = (void*)((long)current->self_exec_id);
611 return 0;
612}
613
614static ssize_t mem_read(struct file * file, char __user * buf,
615 size_t count, loff_t *ppos)
616{
99f89551 617 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1da177e4
LT
618 char *page;
619 unsigned long src = *ppos;
620 int ret = -ESRCH;
621 struct mm_struct *mm;
622
99f89551
EB
623 if (!task)
624 goto out_no_task;
625
ab8d11be 626 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
1da177e4
LT
627 goto out;
628
629 ret = -ENOMEM;
630 page = (char *)__get_free_page(GFP_USER);
631 if (!page)
632 goto out;
633
634 ret = 0;
635
636 mm = get_task_mm(task);
637 if (!mm)
638 goto out_free;
639
640 ret = -EIO;
641
642 if (file->private_data != (void*)((long)current->self_exec_id))
643 goto out_put;
644
645 ret = 0;
646
647 while (count > 0) {
648 int this_len, retval;
649
650 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
651 retval = access_process_vm(task, src, page, this_len, 0);
ab8d11be 652 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
1da177e4
LT
653 if (!ret)
654 ret = -EIO;
655 break;
656 }
657
658 if (copy_to_user(buf, page, retval)) {
659 ret = -EFAULT;
660 break;
661 }
662
663 ret += retval;
664 src += retval;
665 buf += retval;
666 count -= retval;
667 }
668 *ppos = src;
669
670out_put:
671 mmput(mm);
672out_free:
673 free_page((unsigned long) page);
674out:
99f89551
EB
675 put_task_struct(task);
676out_no_task:
1da177e4
LT
677 return ret;
678}
679
680#define mem_write NULL
681
682#ifndef mem_write
683/* This is a security hazard */
684static ssize_t mem_write(struct file * file, const char * buf,
685 size_t count, loff_t *ppos)
686{
f7ca54f4 687 int copied;
1da177e4 688 char *page;
99f89551 689 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1da177e4
LT
690 unsigned long dst = *ppos;
691
99f89551
EB
692 copied = -ESRCH;
693 if (!task)
694 goto out_no_task;
695
ab8d11be 696 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
99f89551 697 goto out;
1da177e4 698
99f89551 699 copied = -ENOMEM;
1da177e4
LT
700 page = (char *)__get_free_page(GFP_USER);
701 if (!page)
99f89551 702 goto out;
1da177e4 703
f7ca54f4 704 copied = 0;
1da177e4
LT
705 while (count > 0) {
706 int this_len, retval;
707
708 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
709 if (copy_from_user(page, buf, this_len)) {
710 copied = -EFAULT;
711 break;
712 }
713 retval = access_process_vm(task, dst, page, this_len, 1);
714 if (!retval) {
715 if (!copied)
716 copied = -EIO;
717 break;
718 }
719 copied += retval;
720 buf += retval;
721 dst += retval;
722 count -= retval;
723 }
724 *ppos = dst;
725 free_page((unsigned long) page);
99f89551
EB
726out:
727 put_task_struct(task);
728out_no_task:
1da177e4
LT
729 return copied;
730}
731#endif
732
733static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
734{
735 switch (orig) {
736 case 0:
737 file->f_pos = offset;
738 break;
739 case 1:
740 file->f_pos += offset;
741 break;
742 default:
743 return -EINVAL;
744 }
745 force_successful_syscall_return();
746 return file->f_pos;
747}
748
749static struct file_operations proc_mem_operations = {
750 .llseek = mem_lseek,
751 .read = mem_read,
752 .write = mem_write,
753 .open = mem_open,
754};
755
756static ssize_t oom_adjust_read(struct file *file, char __user *buf,
757 size_t count, loff_t *ppos)
758{
99f89551 759 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
8578cea7 760 char buffer[PROC_NUMBUF];
1da177e4 761 size_t len;
99f89551 762 int oom_adjust;
1da177e4
LT
763 loff_t __ppos = *ppos;
764
99f89551
EB
765 if (!task)
766 return -ESRCH;
767 oom_adjust = task->oomkilladj;
768 put_task_struct(task);
769
8578cea7 770 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
1da177e4
LT
771 if (__ppos >= len)
772 return 0;
773 if (count > len-__ppos)
774 count = len-__ppos;
775 if (copy_to_user(buf, buffer + __ppos, count))
776 return -EFAULT;
777 *ppos = __ppos + count;
778 return count;
779}
780
781static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
782 size_t count, loff_t *ppos)
783{
99f89551 784 struct task_struct *task;
8578cea7 785 char buffer[PROC_NUMBUF], *end;
1da177e4
LT
786 int oom_adjust;
787
788 if (!capable(CAP_SYS_RESOURCE))
789 return -EPERM;
8578cea7
EB
790 memset(buffer, 0, sizeof(buffer));
791 if (count > sizeof(buffer) - 1)
792 count = sizeof(buffer) - 1;
1da177e4
LT
793 if (copy_from_user(buffer, buf, count))
794 return -EFAULT;
795 oom_adjust = simple_strtol(buffer, &end, 0);
79befd0c 796 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
1da177e4
LT
797 return -EINVAL;
798 if (*end == '\n')
799 end++;
99f89551
EB
800 task = get_proc_task(file->f_dentry->d_inode);
801 if (!task)
802 return -ESRCH;
1da177e4 803 task->oomkilladj = oom_adjust;
99f89551 804 put_task_struct(task);
1da177e4
LT
805 if (end - buffer == 0)
806 return -EIO;
807 return end - buffer;
808}
809
810static struct file_operations proc_oom_adjust_operations = {
811 .read = oom_adjust_read,
812 .write = oom_adjust_write,
813};
814
1da177e4
LT
815#ifdef CONFIG_AUDITSYSCALL
816#define TMPBUFLEN 21
817static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
818 size_t count, loff_t *ppos)
819{
820 struct inode * inode = file->f_dentry->d_inode;
99f89551 821 struct task_struct *task = get_proc_task(inode);
1da177e4
LT
822 ssize_t length;
823 char tmpbuf[TMPBUFLEN];
824
99f89551
EB
825 if (!task)
826 return -ESRCH;
1da177e4
LT
827 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
828 audit_get_loginuid(task->audit_context));
99f89551 829 put_task_struct(task);
1da177e4
LT
830 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
831}
832
833static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
834 size_t count, loff_t *ppos)
835{
836 struct inode * inode = file->f_dentry->d_inode;
837 char *page, *tmp;
838 ssize_t length;
1da177e4
LT
839 uid_t loginuid;
840
841 if (!capable(CAP_AUDIT_CONTROL))
842 return -EPERM;
843
13b41b09 844 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
1da177e4
LT
845 return -EPERM;
846
e0182909
AV
847 if (count >= PAGE_SIZE)
848 count = PAGE_SIZE - 1;
1da177e4
LT
849
850 if (*ppos != 0) {
851 /* No partial writes. */
852 return -EINVAL;
853 }
854 page = (char*)__get_free_page(GFP_USER);
855 if (!page)
856 return -ENOMEM;
857 length = -EFAULT;
858 if (copy_from_user(page, buf, count))
859 goto out_free_page;
860
e0182909 861 page[count] = '\0';
1da177e4
LT
862 loginuid = simple_strtoul(page, &tmp, 10);
863 if (tmp == page) {
864 length = -EINVAL;
865 goto out_free_page;
866
867 }
99f89551 868 length = audit_set_loginuid(current, loginuid);
1da177e4
LT
869 if (likely(length == 0))
870 length = count;
871
872out_free_page:
873 free_page((unsigned long) page);
874 return length;
875}
876
877static struct file_operations proc_loginuid_operations = {
878 .read = proc_loginuid_read,
879 .write = proc_loginuid_write,
880};
881#endif
882
883#ifdef CONFIG_SECCOMP
884static ssize_t seccomp_read(struct file *file, char __user *buf,
885 size_t count, loff_t *ppos)
886{
99f89551 887 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
1da177e4
LT
888 char __buf[20];
889 loff_t __ppos = *ppos;
890 size_t len;
891
99f89551
EB
892 if (!tsk)
893 return -ESRCH;
1da177e4
LT
894 /* no need to print the trailing zero, so use only len */
895 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
99f89551 896 put_task_struct(tsk);
1da177e4
LT
897 if (__ppos >= len)
898 return 0;
899 if (count > len - __ppos)
900 count = len - __ppos;
901 if (copy_to_user(buf, __buf + __ppos, count))
902 return -EFAULT;
903 *ppos = __ppos + count;
904 return count;
905}
906
907static ssize_t seccomp_write(struct file *file, const char __user *buf,
908 size_t count, loff_t *ppos)
909{
99f89551 910 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
1da177e4
LT
911 char __buf[20], *end;
912 unsigned int seccomp_mode;
99f89551
EB
913 ssize_t result;
914
915 result = -ESRCH;
916 if (!tsk)
917 goto out_no_task;
1da177e4
LT
918
919 /* can set it only once to be even more secure */
99f89551 920 result = -EPERM;
1da177e4 921 if (unlikely(tsk->seccomp.mode))
99f89551 922 goto out;
1da177e4 923
99f89551 924 result = -EFAULT;
1da177e4
LT
925 memset(__buf, 0, sizeof(__buf));
926 count = min(count, sizeof(__buf) - 1);
927 if (copy_from_user(__buf, buf, count))
99f89551
EB
928 goto out;
929
1da177e4
LT
930 seccomp_mode = simple_strtoul(__buf, &end, 0);
931 if (*end == '\n')
932 end++;
99f89551 933 result = -EINVAL;
1da177e4
LT
934 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
935 tsk->seccomp.mode = seccomp_mode;
936 set_tsk_thread_flag(tsk, TIF_SECCOMP);
937 } else
99f89551
EB
938 goto out;
939 result = -EIO;
1da177e4 940 if (unlikely(!(end - __buf)))
99f89551
EB
941 goto out;
942 result = end - __buf;
943out:
944 put_task_struct(tsk);
945out_no_task:
946 return result;
1da177e4
LT
947}
948
949static struct file_operations proc_seccomp_operations = {
950 .read = seccomp_read,
951 .write = seccomp_write,
952};
953#endif /* CONFIG_SECCOMP */
954
008b150a 955static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1da177e4
LT
956{
957 struct inode *inode = dentry->d_inode;
958 int error = -EACCES;
959
960 /* We don't need a base pointer in the /proc filesystem */
961 path_release(nd);
962
778c1144
EB
963 /* Are we allowed to snoop on the tasks file descriptors? */
964 if (!proc_fd_access_allowed(inode))
1da177e4 965 goto out;
1da177e4
LT
966
967 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
968 nd->last_type = LAST_BIND;
969out:
008b150a 970 return ERR_PTR(error);
1da177e4
LT
971}
972
973static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
974 char __user *buffer, int buflen)
975{
976 struct inode * inode;
977 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
978 int len;
979
980 if (!tmp)
981 return -ENOMEM;
982
983 inode = dentry->d_inode;
984 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
985 len = PTR_ERR(path);
986 if (IS_ERR(path))
987 goto out;
988 len = tmp + PAGE_SIZE - 1 - path;
989
990 if (len > buflen)
991 len = buflen;
992 if (copy_to_user(buffer, path, len))
993 len = -EFAULT;
994 out:
995 free_page((unsigned long)tmp);
996 return len;
997}
998
999static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1000{
1001 int error = -EACCES;
1002 struct inode *inode = dentry->d_inode;
1003 struct dentry *de;
1004 struct vfsmount *mnt = NULL;
1005
778c1144
EB
1006 /* Are we allowed to snoop on the tasks file descriptors? */
1007 if (!proc_fd_access_allowed(inode))
1da177e4 1008 goto out;
1da177e4
LT
1009
1010 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1011 if (error)
1012 goto out;
1013
1014 error = do_proc_readlink(de, mnt, buffer, buflen);
1015 dput(de);
1016 mntput(mnt);
1017out:
1da177e4
LT
1018 return error;
1019}
1020
1021static struct inode_operations proc_pid_link_inode_operations = {
1022 .readlink = proc_pid_readlink,
6d76fa58
LT
1023 .follow_link = proc_pid_follow_link,
1024 .setattr = proc_setattr,
1da177e4
LT
1025};
1026
28a6d671
EB
1027
1028/* building an inode */
1029
1030static int task_dumpable(struct task_struct *task)
1da177e4 1031{
28a6d671
EB
1032 int dumpable = 0;
1033 struct mm_struct *mm;
1da177e4 1034
28a6d671
EB
1035 task_lock(task);
1036 mm = task->mm;
1037 if (mm)
1038 dumpable = mm->dumpable;
1039 task_unlock(task);
1040 if(dumpable == 1)
1041 return 1;
1042 return 0;
1043}
1da177e4 1044
1da177e4 1045
28a6d671
EB
1046static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1047{
1048 struct inode * inode;
1049 struct proc_inode *ei;
1da177e4 1050
28a6d671 1051 /* We need a new inode */
1da177e4 1052
28a6d671
EB
1053 inode = new_inode(sb);
1054 if (!inode)
1055 goto out;
1056
1057 /* Common stuff */
1058 ei = PROC_I(inode);
1059 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1060 inode->i_ino = fake_ino(task->pid, ino);
1061 inode->i_op = &proc_def_inode_operations;
1062
1063 /*
1064 * grab the reference to task.
1065 */
1066 ei->pid = get_pid(task->pids[PIDTYPE_PID].pid);
1067 if (!ei->pid)
1068 goto out_unlock;
1069
1070 inode->i_uid = 0;
1071 inode->i_gid = 0;
1072 if (task_dumpable(task)) {
1073 inode->i_uid = task->euid;
1074 inode->i_gid = task->egid;
1da177e4 1075 }
28a6d671
EB
1076 security_task_to_inode(task, inode);
1077
1da177e4 1078out:
28a6d671
EB
1079 return inode;
1080
1081out_unlock:
1082 iput(inode);
1083 return NULL;
1da177e4
LT
1084}
1085
28a6d671 1086static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1da177e4 1087{
1da177e4 1088 struct inode *inode = dentry->d_inode;
28a6d671
EB
1089 struct task_struct *task;
1090 generic_fillattr(inode, stat);
1da177e4 1091
28a6d671
EB
1092 rcu_read_lock();
1093 stat->uid = 0;
1094 stat->gid = 0;
1095 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1096 if (task) {
1097 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1098 task_dumpable(task)) {
1099 stat->uid = task->euid;
1100 stat->gid = task->egid;
1da177e4
LT
1101 }
1102 }
28a6d671 1103 rcu_read_unlock();
d6e71144 1104 return 0;
1da177e4
LT
1105}
1106
1da177e4
LT
1107/* dentry stuff */
1108
1109/*
1110 * Exceptional case: normally we are not allowed to unhash a busy
1111 * directory. In this case, however, we can do it - no aliasing problems
1112 * due to the way we treat inodes.
1113 *
1114 * Rewrite the inode's ownerships here because the owning task may have
1115 * performed a setuid(), etc.
99f89551
EB
1116 *
1117 * Before the /proc/pid/status file was created the only way to read
1118 * the effective uid of a /process was to stat /proc/pid. Reading
1119 * /proc/pid/status is slow enough that procps and other packages
1120 * kept stating /proc/pid. To keep the rules in /proc simple I have
1121 * made this apply to all per process world readable and executable
1122 * directories.
1da177e4
LT
1123 */
1124static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1125{
1126 struct inode *inode = dentry->d_inode;
99f89551
EB
1127 struct task_struct *task = get_proc_task(inode);
1128 if (task) {
1129 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1130 task_dumpable(task)) {
1da177e4
LT
1131 inode->i_uid = task->euid;
1132 inode->i_gid = task->egid;
1133 } else {
1134 inode->i_uid = 0;
1135 inode->i_gid = 0;
1136 }
9ee8ab9f 1137 inode->i_mode &= ~(S_ISUID | S_ISGID);
1da177e4 1138 security_task_to_inode(task, inode);
99f89551 1139 put_task_struct(task);
1da177e4
LT
1140 return 1;
1141 }
1142 d_drop(dentry);
1143 return 0;
1144}
1145
28a6d671 1146static int pid_delete_dentry(struct dentry * dentry)
99f89551 1147{
28a6d671
EB
1148 /* Is the task we represent dead?
1149 * If so, then don't put the dentry on the lru list,
1150 * kill it immediately.
1151 */
1152 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1153}
1154
1155static struct dentry_operations pid_dentry_operations =
1156{
1157 .d_revalidate = pid_revalidate,
1158 .d_delete = pid_delete_dentry,
1159};
1160
1161/* Lookups */
1162
1163static unsigned name_to_int(struct dentry *dentry)
1164{
1165 const char *name = dentry->d_name.name;
1166 int len = dentry->d_name.len;
1167 unsigned n = 0;
1168
1169 if (len > 1 && *name == '0')
1170 goto out;
1171 while (len-- > 0) {
1172 unsigned c = *name++ - '0';
1173 if (c > 9)
1174 goto out;
1175 if (n >= (~0U-9)/10)
1176 goto out;
1177 n *= 10;
1178 n += c;
1179 }
1180 return n;
1181out:
1182 return ~0U;
1183}
1184
1185static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
1186{
1187 struct task_struct *task = get_proc_task(inode);
1188 struct files_struct *files = NULL;
1189 struct file *file;
1190 int fd = proc_fd(inode);
99f89551 1191
99f89551 1192 if (task) {
28a6d671
EB
1193 files = get_files_struct(task);
1194 put_task_struct(task);
1195 }
1196 if (files) {
1197 /*
1198 * We are not taking a ref to the file structure, so we must
1199 * hold ->file_lock.
1200 */
1201 spin_lock(&files->file_lock);
1202 file = fcheck_files(files, fd);
1203 if (file) {
1204 *mnt = mntget(file->f_vfsmnt);
1205 *dentry = dget(file->f_dentry);
1206 spin_unlock(&files->file_lock);
1207 put_files_struct(files);
1208 return 0;
99f89551 1209 }
28a6d671
EB
1210 spin_unlock(&files->file_lock);
1211 put_files_struct(files);
99f89551 1212 }
28a6d671 1213 return -ENOENT;
99f89551
EB
1214}
1215
1da177e4
LT
1216static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1217{
1218 struct inode *inode = dentry->d_inode;
99f89551 1219 struct task_struct *task = get_proc_task(inode);
aed7a6c4 1220 int fd = proc_fd(inode);
1da177e4
LT
1221 struct files_struct *files;
1222
99f89551
EB
1223 if (task) {
1224 files = get_files_struct(task);
1225 if (files) {
1226 rcu_read_lock();
1227 if (fcheck_files(files, fd)) {
1228 rcu_read_unlock();
1229 put_files_struct(files);
1230 if (task_dumpable(task)) {
1231 inode->i_uid = task->euid;
1232 inode->i_gid = task->egid;
1233 } else {
1234 inode->i_uid = 0;
1235 inode->i_gid = 0;
1236 }
9ee8ab9f 1237 inode->i_mode &= ~(S_ISUID | S_ISGID);
99f89551
EB
1238 security_task_to_inode(task, inode);
1239 put_task_struct(task);
1240 return 1;
1241 }
b835996f 1242 rcu_read_unlock();
1da177e4 1243 put_files_struct(files);
1da177e4 1244 }
99f89551 1245 put_task_struct(task);
1da177e4
LT
1246 }
1247 d_drop(dentry);
1248 return 0;
1249}
1250
1da177e4
LT
1251static struct dentry_operations tid_fd_dentry_operations =
1252{
1253 .d_revalidate = tid_fd_revalidate,
1254 .d_delete = pid_delete_dentry,
1255};
1256
1da177e4
LT
1257/* SMP-safe */
1258static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1259{
99f89551 1260 struct task_struct *task = get_proc_task(dir);
1da177e4 1261 unsigned fd = name_to_int(dentry);
cd6a3ce9 1262 struct dentry *result = ERR_PTR(-ENOENT);
1da177e4
LT
1263 struct file * file;
1264 struct files_struct * files;
1265 struct inode *inode;
1266 struct proc_inode *ei;
1267
99f89551
EB
1268 if (!task)
1269 goto out_no_task;
1da177e4
LT
1270 if (fd == ~0U)
1271 goto out;
1da177e4
LT
1272
1273 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1274 if (!inode)
1275 goto out;
1276 ei = PROC_I(inode);
aed7a6c4 1277 ei->fd = fd;
1da177e4
LT
1278 files = get_files_struct(task);
1279 if (!files)
1280 goto out_unlock;
1281 inode->i_mode = S_IFLNK;
ca99c1da
DS
1282
1283 /*
1284 * We are not taking a ref to the file structure, so we must
1285 * hold ->file_lock.
1286 */
1287 spin_lock(&files->file_lock);
1da177e4
LT
1288 file = fcheck_files(files, fd);
1289 if (!file)
1290 goto out_unlock2;
1291 if (file->f_mode & 1)
1292 inode->i_mode |= S_IRUSR | S_IXUSR;
1293 if (file->f_mode & 2)
1294 inode->i_mode |= S_IWUSR | S_IXUSR;
ca99c1da 1295 spin_unlock(&files->file_lock);
1da177e4
LT
1296 put_files_struct(files);
1297 inode->i_op = &proc_pid_link_inode_operations;
1298 inode->i_size = 64;
1299 ei->op.proc_get_link = proc_fd_link;
1300 dentry->d_op = &tid_fd_dentry_operations;
1301 d_add(dentry, inode);
cd6a3ce9
EB
1302 /* Close the race of the process dying before we return the dentry */
1303 if (tid_fd_revalidate(dentry, NULL))
1304 result = NULL;
1305out:
99f89551
EB
1306 put_task_struct(task);
1307out_no_task:
cd6a3ce9 1308 return result;
1da177e4
LT
1309
1310out_unlock2:
ca99c1da 1311 spin_unlock(&files->file_lock);
1da177e4
LT
1312 put_files_struct(files);
1313out_unlock:
1314 iput(inode);
cd6a3ce9 1315 goto out;
1da177e4
LT
1316}
1317
28a6d671
EB
1318static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1319{
1320 struct dentry *dentry = filp->f_dentry;
1321 struct inode *inode = dentry->d_inode;
1322 struct task_struct *p = get_proc_task(inode);
1323 unsigned int fd, tid, ino;
1324 int retval;
1325 char buf[PROC_NUMBUF];
1326 struct files_struct * files;
1327 struct fdtable *fdt;
1da177e4 1328
28a6d671
EB
1329 retval = -ENOENT;
1330 if (!p)
1331 goto out_no_task;
1332 retval = 0;
1333 tid = p->pid;
1334
1335 fd = filp->f_pos;
1336 switch (fd) {
1337 case 0:
1338 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1339 goto out;
1340 filp->f_pos++;
1341 case 1:
1342 ino = parent_ino(dentry);
1343 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1344 goto out;
1345 filp->f_pos++;
1346 default:
1347 files = get_files_struct(p);
1348 if (!files)
1349 goto out;
1350 rcu_read_lock();
1351 fdt = files_fdtable(files);
1352 for (fd = filp->f_pos-2;
1353 fd < fdt->max_fds;
1354 fd++, filp->f_pos++) {
1355 unsigned int i,j;
1356
1357 if (!fcheck_files(files, fd))
1358 continue;
1359 rcu_read_unlock();
1360
1361 j = PROC_NUMBUF;
1362 i = fd;
1363 do {
1364 j--;
1365 buf[j] = '0' + (i % 10);
1366 i /= 10;
1367 } while (i);
1368
1369 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
1370 if (filldir(dirent, buf+j, PROC_NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
1371 rcu_read_lock();
1372 break;
1373 }
1374 rcu_read_lock();
1375 }
1376 rcu_read_unlock();
1377 put_files_struct(files);
1378 }
1379out:
1380 put_task_struct(p);
1381out_no_task:
1382 return retval;
1383}
1384
1385static struct file_operations proc_fd_operations = {
1386 .read = generic_read_dir,
1387 .readdir = proc_readfd,
1da177e4
LT
1388};
1389
1390/*
1391 * proc directories can do almost nothing..
1392 */
1393static struct inode_operations proc_fd_inode_operations = {
1394 .lookup = proc_lookupfd,
6d76fa58 1395 .setattr = proc_setattr,
1da177e4
LT
1396};
1397
1da177e4
LT
1398/* SMP-safe */
1399static struct dentry *proc_pident_lookup(struct inode *dir,
1400 struct dentry *dentry,
1401 struct pid_entry *ents)
1402{
1403 struct inode *inode;
cd6a3ce9 1404 struct dentry *error;
99f89551 1405 struct task_struct *task = get_proc_task(dir);
1da177e4
LT
1406 struct pid_entry *p;
1407 struct proc_inode *ei;
1408
cd6a3ce9 1409 error = ERR_PTR(-ENOENT);
1da177e4
LT
1410 inode = NULL;
1411
99f89551
EB
1412 if (!task)
1413 goto out_no_task;
1da177e4 1414
20cdc894
EB
1415 /*
1416 * Yes, it does not scale. And it should not. Don't add
1417 * new entries into /proc/<tgid>/ without very good reasons.
1418 */
1da177e4
LT
1419 for (p = ents; p->name; p++) {
1420 if (p->len != dentry->d_name.len)
1421 continue;
1422 if (!memcmp(dentry->d_name.name, p->name, p->len))
1423 break;
1424 }
1425 if (!p->name)
1426 goto out;
1427
cd6a3ce9 1428 error = ERR_PTR(-EINVAL);
1da177e4
LT
1429 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1430 if (!inode)
1431 goto out;
1432
1433 ei = PROC_I(inode);
1434 inode->i_mode = p->mode;
20cdc894
EB
1435 if (S_ISDIR(inode->i_mode))
1436 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1437 if (p->iop)
1438 inode->i_op = p->iop;
1439 if (p->fop)
1440 inode->i_fop = p->fop;
1441 ei->op = p->op;
1da177e4
LT
1442 dentry->d_op = &pid_dentry_operations;
1443 d_add(dentry, inode);
cd6a3ce9
EB
1444 /* Close the race of the process dying before we return the dentry */
1445 if (pid_revalidate(dentry, NULL))
1446 error = NULL;
1da177e4 1447out:
99f89551
EB
1448 put_task_struct(task);
1449out_no_task:
cd6a3ce9 1450 return error;
1da177e4
LT
1451}
1452
28a6d671
EB
1453static int proc_pident_readdir(struct file *filp,
1454 void *dirent, filldir_t filldir,
1455 struct pid_entry *ents, unsigned int nents)
1456{
1457 int i;
1458 int pid;
1459 struct dentry *dentry = filp->f_dentry;
1460 struct inode *inode = dentry->d_inode;
1461 struct task_struct *task = get_proc_task(inode);
1462 struct pid_entry *p;
1463 ino_t ino;
1464 int ret;
1465
1466 ret = -ENOENT;
1467 if (!task)
1468 goto out;
1469
1470 ret = 0;
1471 pid = task->pid;
1472 put_task_struct(task);
1473 i = filp->f_pos;
1474 switch (i) {
1475 case 0:
1476 ino = inode->i_ino;
1477 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1478 goto out;
1479 i++;
1480 filp->f_pos++;
1481 /* fall through */
1482 case 1:
1483 ino = parent_ino(dentry);
1484 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1485 goto out;
1486 i++;
1487 filp->f_pos++;
1488 /* fall through */
1489 default:
1490 i -= 2;
1491 if (i >= nents) {
1492 ret = 1;
1493 goto out;
1494 }
1495 p = ents + i;
1496 while (p->name) {
1497 if (filldir(dirent, p->name, p->len, filp->f_pos,
1498 fake_ino(pid, p->type), p->mode >> 12) < 0)
1499 goto out;
1500 filp->f_pos++;
1501 p++;
1502 }
1503 }
1504
1505 ret = 1;
1506out:
1507 return ret;
1da177e4
LT
1508}
1509
28a6d671
EB
1510#ifdef CONFIG_SECURITY
1511static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1512 size_t count, loff_t *ppos)
1513{
1514 struct inode * inode = file->f_dentry->d_inode;
1515 unsigned long page;
1516 ssize_t length;
1517 struct task_struct *task = get_proc_task(inode);
1518
1519 length = -ESRCH;
1520 if (!task)
1521 goto out_no_task;
1522
1523 if (count > PAGE_SIZE)
1524 count = PAGE_SIZE;
1525 length = -ENOMEM;
1526 if (!(page = __get_free_page(GFP_KERNEL)))
1527 goto out;
1528
1529 length = security_getprocattr(task,
1530 (char*)file->f_dentry->d_name.name,
1531 (void*)page, count);
1532 if (length >= 0)
1533 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1534 free_page(page);
1535out:
1536 put_task_struct(task);
1537out_no_task:
1538 return length;
1da177e4
LT
1539}
1540
28a6d671
EB
1541static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1542 size_t count, loff_t *ppos)
1543{
1544 struct inode * inode = file->f_dentry->d_inode;
1545 char *page;
1546 ssize_t length;
1547 struct task_struct *task = get_proc_task(inode);
1548
1549 length = -ESRCH;
1550 if (!task)
1551 goto out_no_task;
1552 if (count > PAGE_SIZE)
1553 count = PAGE_SIZE;
1554
1555 /* No partial writes. */
1556 length = -EINVAL;
1557 if (*ppos != 0)
1558 goto out;
1559
1560 length = -ENOMEM;
1561 page = (char*)__get_free_page(GFP_USER);
1562 if (!page)
1563 goto out;
1564
1565 length = -EFAULT;
1566 if (copy_from_user(page, buf, count))
1567 goto out_free;
1568
1569 length = security_setprocattr(task,
1570 (char*)file->f_dentry->d_name.name,
1571 (void*)page, count);
1572out_free:
1573 free_page((unsigned long) page);
1574out:
1575 put_task_struct(task);
1576out_no_task:
1577 return length;
1578}
1579
1580static struct file_operations proc_pid_attr_operations = {
1581 .read = proc_pid_attr_read,
1582 .write = proc_pid_attr_write,
1583};
1584
1585static struct pid_entry tgid_attr_stuff[] = {
20cdc894
EB
1586 REG(PROC_TGID_ATTR_CURRENT, "current", S_IRUGO|S_IWUGO, pid_attr),
1587 REG(PROC_TGID_ATTR_PREV, "prev", S_IRUGO, pid_attr),
1588 REG(PROC_TGID_ATTR_EXEC, "exec", S_IRUGO|S_IWUGO, pid_attr),
1589 REG(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IRUGO|S_IWUGO, pid_attr),
1590 REG(PROC_TGID_ATTR_KEYCREATE, "keycreate", S_IRUGO|S_IWUGO, pid_attr),
1591 REG(PROC_TGID_ATTR_SOCKCREATE, "sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1592 {}
28a6d671
EB
1593};
1594static struct pid_entry tid_attr_stuff[] = {
20cdc894
EB
1595 REG(PROC_TID_ATTR_CURRENT, "current", S_IRUGO|S_IWUGO, pid_attr),
1596 REG(PROC_TID_ATTR_PREV, "prev", S_IRUGO, pid_attr),
1597 REG(PROC_TID_ATTR_EXEC, "exec", S_IRUGO|S_IWUGO, pid_attr),
1598 REG(PROC_TID_ATTR_FSCREATE, "fscreate", S_IRUGO|S_IWUGO, pid_attr),
1599 REG(PROC_TID_ATTR_KEYCREATE, "keycreate", S_IRUGO|S_IWUGO, pid_attr),
1600 REG(PROC_TID_ATTR_SOCKCREATE, "sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1601 {}
28a6d671
EB
1602};
1603
1604static int proc_tgid_attr_readdir(struct file * filp,
1605 void * dirent, filldir_t filldir)
1606{
1607 return proc_pident_readdir(filp,dirent,filldir,
1608 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1609}
1610
1611static int proc_tid_attr_readdir(struct file * filp,
1612 void * dirent, filldir_t filldir)
1613{
1614 return proc_pident_readdir(filp,dirent,filldir,
1615 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1616}
1617
1618static struct file_operations proc_tgid_attr_operations = {
1da177e4 1619 .read = generic_read_dir,
28a6d671 1620 .readdir = proc_tgid_attr_readdir,
1da177e4
LT
1621};
1622
28a6d671 1623static struct file_operations proc_tid_attr_operations = {
1da177e4 1624 .read = generic_read_dir,
28a6d671 1625 .readdir = proc_tid_attr_readdir,
1da177e4
LT
1626};
1627
28a6d671
EB
1628static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1629 struct dentry *dentry, struct nameidata *nd)
1630{
1631 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1632}
1633
1634static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1635 struct dentry *dentry, struct nameidata *nd)
1636{
1637 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1638}
1639
1640static struct inode_operations proc_tgid_attr_inode_operations = {
1641 .lookup = proc_tgid_attr_lookup,
99f89551 1642 .getattr = pid_getattr,
6d76fa58 1643 .setattr = proc_setattr,
1da177e4
LT
1644};
1645
28a6d671
EB
1646static struct inode_operations proc_tid_attr_inode_operations = {
1647 .lookup = proc_tid_attr_lookup,
99f89551 1648 .getattr = pid_getattr,
6d76fa58 1649 .setattr = proc_setattr,
1da177e4 1650};
28a6d671
EB
1651#endif
1652
1653/*
1654 * /proc/self:
1655 */
1656static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1657 int buflen)
1658{
1659 char tmp[PROC_NUMBUF];
1660 sprintf(tmp, "%d", current->tgid);
1661 return vfs_readlink(dentry,buffer,buflen,tmp);
1662}
1663
1664static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1665{
1666 char tmp[PROC_NUMBUF];
1667 sprintf(tmp, "%d", current->tgid);
1668 return ERR_PTR(vfs_follow_link(nd,tmp));
1669}
1670
1671static struct inode_operations proc_self_inode_operations = {
1672 .readlink = proc_self_readlink,
1673 .follow_link = proc_self_follow_link,
1674};
1675
1676/*
1677 * Thread groups
1678 */
20cdc894
EB
1679static struct file_operations proc_task_operations;
1680static struct inode_operations proc_task_inode_operations;
1681
28a6d671 1682static struct pid_entry tgid_base_stuff[] = {
20cdc894
EB
1683 DIR(PROC_TGID_TASK, "task", S_IRUGO|S_IXUGO, task),
1684 DIR(PROC_TGID_FD, "fd", S_IRUSR|S_IXUSR, fd),
1685 INF(PROC_TGID_ENVIRON, "environ", S_IRUSR, pid_environ),
1686 INF(PROC_TGID_AUXV, "auxv", S_IRUSR, pid_auxv),
1687 INF(PROC_TGID_STATUS, "status", S_IRUGO, pid_status),
1688 INF(PROC_TGID_CMDLINE, "cmdline", S_IRUGO, pid_cmdline),
1689 INF(PROC_TGID_STAT, "stat", S_IRUGO, tgid_stat),
1690 INF(PROC_TGID_STATM, "statm", S_IRUGO, pid_statm),
1691 REG(PROC_TGID_MAPS, "maps", S_IRUGO, maps),
28a6d671 1692#ifdef CONFIG_NUMA
20cdc894 1693 REG(PROC_TGID_NUMA_MAPS, "numa_maps", S_IRUGO, numa_maps),
28a6d671 1694#endif
20cdc894 1695 REG(PROC_TGID_MEM, "mem", S_IRUSR|S_IWUSR, mem),
28a6d671 1696#ifdef CONFIG_SECCOMP
20cdc894 1697 REG(PROC_TGID_SECCOMP, "seccomp", S_IRUSR|S_IWUSR, seccomp),
28a6d671 1698#endif
20cdc894
EB
1699 LNK(PROC_TGID_CWD, "cwd", cwd),
1700 LNK(PROC_TGID_ROOT, "root", root),
1701 LNK(PROC_TGID_EXE, "exe", exe),
1702 REG(PROC_TGID_MOUNTS, "mounts", S_IRUGO, mounts),
1703 REG(PROC_TGID_MOUNTSTATS, "mountstats", S_IRUSR, mountstats),
28a6d671 1704#ifdef CONFIG_MMU
20cdc894 1705 REG(PROC_TGID_SMAPS, "smaps", S_IRUGO, smaps),
28a6d671
EB
1706#endif
1707#ifdef CONFIG_SECURITY
20cdc894 1708 DIR(PROC_TGID_ATTR, "attr", S_IRUGO|S_IXUGO, tgid_attr),
28a6d671
EB
1709#endif
1710#ifdef CONFIG_KALLSYMS
20cdc894 1711 INF(PROC_TGID_WCHAN, "wchan", S_IRUGO, pid_wchan),
28a6d671
EB
1712#endif
1713#ifdef CONFIG_SCHEDSTATS
20cdc894 1714 INF(PROC_TGID_SCHEDSTAT, "schedstat", S_IRUGO, pid_schedstat),
28a6d671
EB
1715#endif
1716#ifdef CONFIG_CPUSETS
20cdc894 1717 REG(PROC_TGID_CPUSET, "cpuset", S_IRUGO, cpuset),
28a6d671 1718#endif
20cdc894
EB
1719 INF(PROC_TGID_OOM_SCORE, "oom_score", S_IRUGO, oom_score),
1720 REG(PROC_TGID_OOM_ADJUST, "oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
28a6d671 1721#ifdef CONFIG_AUDITSYSCALL
20cdc894 1722 REG(PROC_TGID_LOGINUID, "loginuid", S_IWUSR|S_IRUGO, loginuid),
28a6d671 1723#endif
20cdc894 1724 {}
28a6d671 1725};
1da177e4 1726
28a6d671 1727static int proc_tgid_base_readdir(struct file * filp,
1da177e4
LT
1728 void * dirent, filldir_t filldir)
1729{
1730 return proc_pident_readdir(filp,dirent,filldir,
28a6d671 1731 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1da177e4
LT
1732}
1733
28a6d671 1734static struct file_operations proc_tgid_base_operations = {
1da177e4 1735 .read = generic_read_dir,
28a6d671 1736 .readdir = proc_tgid_base_readdir,
1da177e4
LT
1737};
1738
28a6d671
EB
1739static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1740 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1da177e4
LT
1741}
1742
28a6d671
EB
1743static struct inode_operations proc_tgid_base_inode_operations = {
1744 .lookup = proc_tgid_base_lookup,
99f89551 1745 .getattr = pid_getattr,
6d76fa58 1746 .setattr = proc_setattr,
1da177e4 1747};
1da177e4
LT
1748
1749/**
48e6484d
EB
1750 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
1751 *
1752 * @task: task that should be flushed.
1753 *
1754 * Looks in the dcache for
1755 * /proc/@pid
1756 * /proc/@tgid/task/@pid
1757 * if either directory is present flushes it and all of it'ts children
1758 * from the dcache.
1da177e4 1759 *
48e6484d
EB
1760 * It is safe and reasonable to cache /proc entries for a task until
1761 * that task exits. After that they just clog up the dcache with
1762 * useless entries, possibly causing useful dcache entries to be
1763 * flushed instead. This routine is proved to flush those useless
1764 * dcache entries at process exit time.
1da177e4 1765 *
48e6484d
EB
1766 * NOTE: This routine is just an optimization so it does not guarantee
1767 * that no dcache entries will exist at process exit time it
1768 * just makes it very unlikely that any will persist.
1da177e4 1769 */
48e6484d 1770void proc_flush_task(struct task_struct *task)
1da177e4 1771{
48e6484d 1772 struct dentry *dentry, *leader, *dir;
8578cea7 1773 char buf[PROC_NUMBUF];
48e6484d
EB
1774 struct qstr name;
1775
1776 name.name = buf;
1777 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1778 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1779 if (dentry) {
1780 shrink_dcache_parent(dentry);
1781 d_drop(dentry);
1782 dput(dentry);
1783 }
1da177e4 1784
48e6484d
EB
1785 if (thread_group_leader(task))
1786 goto out;
1da177e4 1787
48e6484d
EB
1788 name.name = buf;
1789 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
1790 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1791 if (!leader)
1792 goto out;
1da177e4 1793
48e6484d
EB
1794 name.name = "task";
1795 name.len = strlen(name.name);
1796 dir = d_hash_and_lookup(leader, &name);
1797 if (!dir)
1798 goto out_put_leader;
1799
1800 name.name = buf;
1801 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1802 dentry = d_hash_and_lookup(dir, &name);
1803 if (dentry) {
1804 shrink_dcache_parent(dentry);
1805 d_drop(dentry);
1806 dput(dentry);
1da177e4 1807 }
48e6484d
EB
1808
1809 dput(dir);
1810out_put_leader:
1811 dput(leader);
1812out:
1813 return;
1da177e4
LT
1814}
1815
1816/* SMP-safe */
1817struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1818{
cd6a3ce9 1819 struct dentry *result = ERR_PTR(-ENOENT);
1da177e4
LT
1820 struct task_struct *task;
1821 struct inode *inode;
1822 struct proc_inode *ei;
1823 unsigned tgid;
1da177e4
LT
1824
1825 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1826 inode = new_inode(dir->i_sb);
1827 if (!inode)
1828 return ERR_PTR(-ENOMEM);
1829 ei = PROC_I(inode);
1830 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1831 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1832 ei->pde = NULL;
1833 inode->i_mode = S_IFLNK|S_IRWXUGO;
1834 inode->i_uid = inode->i_gid = 0;
1835 inode->i_size = 64;
1836 inode->i_op = &proc_self_inode_operations;
1837 d_add(dentry, inode);
1838 return NULL;
1839 }
1840 tgid = name_to_int(dentry);
1841 if (tgid == ~0U)
1842 goto out;
1843
de758734 1844 rcu_read_lock();
1da177e4
LT
1845 task = find_task_by_pid(tgid);
1846 if (task)
1847 get_task_struct(task);
de758734 1848 rcu_read_unlock();
1da177e4
LT
1849 if (!task)
1850 goto out;
1851
1852 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
cd6a3ce9
EB
1853 if (!inode)
1854 goto out_put_task;
1da177e4 1855
1da177e4
LT
1856 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1857 inode->i_op = &proc_tgid_base_inode_operations;
1858 inode->i_fop = &proc_tgid_base_operations;
1da177e4 1859 inode->i_flags|=S_IMMUTABLE;
bcf88e11
DD
1860#ifdef CONFIG_SECURITY
1861 inode->i_nlink = 5;
1862#else
1863 inode->i_nlink = 4;
1864#endif
1da177e4 1865
48e6484d 1866 dentry->d_op = &pid_dentry_operations;
1da177e4 1867
1da177e4 1868 d_add(dentry, inode);
cd6a3ce9
EB
1869 /* Close the race of the process dying before we return the dentry */
1870 if (pid_revalidate(dentry, NULL))
1871 result = NULL;
1da177e4 1872
cd6a3ce9 1873out_put_task:
1da177e4 1874 put_task_struct(task);
1da177e4 1875out:
cd6a3ce9 1876 return result;
1da177e4
LT
1877}
1878
1da177e4 1879/*
0804ef4b 1880 * Find the first task with tgid >= tgid
0bc58a91 1881 *
1da177e4 1882 */
0804ef4b 1883static struct task_struct *next_tgid(unsigned int tgid)
1da177e4 1884{
0804ef4b
EB
1885 struct task_struct *task;
1886 struct pid *pid;
1da177e4 1887
454cc105 1888 rcu_read_lock();
0804ef4b
EB
1889retry:
1890 task = NULL;
1891 pid = find_ge_pid(tgid);
1892 if (pid) {
1893 tgid = pid->nr + 1;
1894 task = pid_task(pid, PIDTYPE_PID);
1895 /* What we to know is if the pid we have find is the
1896 * pid of a thread_group_leader. Testing for task
1897 * being a thread_group_leader is the obvious thing
1898 * todo but there is a window when it fails, due to
1899 * the pid transfer logic in de_thread.
1900 *
1901 * So we perform the straight forward test of seeing
1902 * if the pid we have found is the pid of a thread
1903 * group leader, and don't worry if the task we have
1904 * found doesn't happen to be a thread group leader.
1905 * As we don't care in the case of readdir.
1906 */
1907 if (!task || !has_group_leader_pid(task))
1908 goto retry;
1909 get_task_struct(task);
0bc58a91 1910 }
454cc105 1911 rcu_read_unlock();
0804ef4b 1912 return task;
1da177e4
LT
1913}
1914
0804ef4b
EB
1915#define TGID_OFFSET (FIRST_PROCESS_ENTRY + (1 /* /proc/self */))
1916
1da177e4
LT
1917/* for the /proc/ directory itself, after non-process stuff has been done */
1918int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
1919{
1da177e4
LT
1920 char buf[PROC_NUMBUF];
1921 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
0bc58a91
EB
1922 struct task_struct *task;
1923 int tgid;
1da177e4
LT
1924
1925 if (!nr) {
1926 ino_t ino = fake_ino(0,PROC_TGID_INO);
1927 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
1928 return 0;
1929 filp->f_pos++;
1930 nr++;
1931 }
1932
0804ef4b
EB
1933 tgid = filp->f_pos - TGID_OFFSET;
1934 for (task = next_tgid(tgid);
0bc58a91 1935 task;
0804ef4b 1936 put_task_struct(task), task = next_tgid(tgid + 1)) {
0bc58a91
EB
1937 int len;
1938 ino_t ino;
1939 tgid = task->pid;
0804ef4b 1940 filp->f_pos = tgid + TGID_OFFSET;
0bc58a91
EB
1941 len = snprintf(buf, sizeof(buf), "%d", tgid);
1942 ino = fake_ino(tgid, PROC_TGID_INO);
1943 if (filldir(dirent, buf, len, filp->f_pos, ino, DT_DIR) < 0) {
0bc58a91 1944 put_task_struct(task);
0804ef4b 1945 goto out;
1da177e4 1946 }
0bc58a91 1947 }
0804ef4b
EB
1948 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
1949out:
0bc58a91
EB
1950 return 0;
1951}
1da177e4 1952
28a6d671
EB
1953/*
1954 * Tasks
1955 */
1956static struct pid_entry tid_base_stuff[] = {
20cdc894
EB
1957 DIR(PROC_TID_FD, "fd", S_IRUSR|S_IXUSR, fd),
1958 INF(PROC_TID_ENVIRON, "environ", S_IRUSR, pid_environ),
1959 INF(PROC_TID_AUXV, "auxv", S_IRUSR, pid_auxv),
1960 INF(PROC_TID_STATUS, "status", S_IRUGO, pid_status),
1961 INF(PROC_TID_CMDLINE, "cmdline", S_IRUGO, pid_cmdline),
1962 INF(PROC_TID_STAT, "stat", S_IRUGO, tid_stat),
1963 INF(PROC_TID_STATM, "statm", S_IRUGO, pid_statm),
1964 REG(PROC_TID_MAPS, "maps", S_IRUGO, maps),
28a6d671 1965#ifdef CONFIG_NUMA
20cdc894 1966 REG(PROC_TID_NUMA_MAPS, "numa_maps", S_IRUGO, numa_maps),
28a6d671 1967#endif
20cdc894 1968 REG(PROC_TID_MEM, "mem", S_IRUSR|S_IWUSR, mem),
28a6d671 1969#ifdef CONFIG_SECCOMP
20cdc894 1970 REG(PROC_TID_SECCOMP, "seccomp", S_IRUSR|S_IWUSR, seccomp),
28a6d671 1971#endif
20cdc894
EB
1972 LNK(PROC_TID_CWD, "cwd", cwd),
1973 LNK(PROC_TID_ROOT, "root", root),
1974 LNK(PROC_TID_EXE, "exe", exe),
1975 REG(PROC_TID_MOUNTS, "mounts", S_IRUGO, mounts),
28a6d671 1976#ifdef CONFIG_MMU
20cdc894 1977 REG(PROC_TID_SMAPS, "smaps", S_IRUGO, smaps),
28a6d671
EB
1978#endif
1979#ifdef CONFIG_SECURITY
20cdc894 1980 DIR(PROC_TID_ATTR, "attr", S_IRUGO|S_IXUGO, tid_attr),
28a6d671
EB
1981#endif
1982#ifdef CONFIG_KALLSYMS
20cdc894 1983 INF(PROC_TID_WCHAN, "wchan", S_IRUGO, pid_wchan),
28a6d671
EB
1984#endif
1985#ifdef CONFIG_SCHEDSTATS
20cdc894 1986 INF(PROC_TID_SCHEDSTAT, "schedstat", S_IRUGO, pid_schedstat),
28a6d671
EB
1987#endif
1988#ifdef CONFIG_CPUSETS
20cdc894 1989 REG(PROC_TID_CPUSET, "cpuset", S_IRUGO, cpuset),
28a6d671 1990#endif
20cdc894
EB
1991 INF(PROC_TID_OOM_SCORE, "oom_score", S_IRUGO, oom_score),
1992 REG(PROC_TID_OOM_ADJUST, "oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
28a6d671 1993#ifdef CONFIG_AUDITSYSCALL
20cdc894 1994 REG(PROC_TID_LOGINUID, "loginuid", S_IWUSR|S_IRUGO, loginuid),
28a6d671 1995#endif
20cdc894 1996 {}
28a6d671
EB
1997};
1998
1999static int proc_tid_base_readdir(struct file * filp,
2000 void * dirent, filldir_t filldir)
2001{
2002 return proc_pident_readdir(filp,dirent,filldir,
2003 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2004}
2005
2006static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2007 return proc_pident_lookup(dir, dentry, tid_base_stuff);
2008}
2009
2010static struct file_operations proc_tid_base_operations = {
2011 .read = generic_read_dir,
2012 .readdir = proc_tid_base_readdir,
2013};
2014
2015static struct inode_operations proc_tid_base_inode_operations = {
2016 .lookup = proc_tid_base_lookup,
2017 .getattr = pid_getattr,
2018 .setattr = proc_setattr,
2019};
2020
2021/* SMP-safe */
2022static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2023{
2024 struct dentry *result = ERR_PTR(-ENOENT);
2025 struct task_struct *task;
2026 struct task_struct *leader = get_proc_task(dir);
2027 struct inode *inode;
2028 unsigned tid;
2029
2030 if (!leader)
2031 goto out_no_task;
2032
2033 tid = name_to_int(dentry);
2034 if (tid == ~0U)
2035 goto out;
2036
2037 rcu_read_lock();
2038 task = find_task_by_pid(tid);
2039 if (task)
2040 get_task_struct(task);
2041 rcu_read_unlock();
2042 if (!task)
2043 goto out;
2044 if (leader->tgid != task->tgid)
2045 goto out_drop_task;
2046
2047 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
2048
2049
2050 if (!inode)
2051 goto out_drop_task;
2052 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2053 inode->i_op = &proc_tid_base_inode_operations;
2054 inode->i_fop = &proc_tid_base_operations;
2055 inode->i_flags|=S_IMMUTABLE;
2056#ifdef CONFIG_SECURITY
2057 inode->i_nlink = 4;
2058#else
2059 inode->i_nlink = 3;
2060#endif
2061
2062 dentry->d_op = &pid_dentry_operations;
2063
2064 d_add(dentry, inode);
2065 /* Close the race of the process dying before we return the dentry */
2066 if (pid_revalidate(dentry, NULL))
2067 result = NULL;
2068
2069out_drop_task:
2070 put_task_struct(task);
2071out:
2072 put_task_struct(leader);
2073out_no_task:
2074 return result;
2075}
2076
0bc58a91
EB
2077/*
2078 * Find the first tid of a thread group to return to user space.
2079 *
2080 * Usually this is just the thread group leader, but if the users
2081 * buffer was too small or there was a seek into the middle of the
2082 * directory we have more work todo.
2083 *
2084 * In the case of a short read we start with find_task_by_pid.
2085 *
2086 * In the case of a seek we start with the leader and walk nr
2087 * threads past it.
2088 */
cc288738
EB
2089static struct task_struct *first_tid(struct task_struct *leader,
2090 int tid, int nr)
0bc58a91 2091{
a872ff0c 2092 struct task_struct *pos;
1da177e4 2093
cc288738 2094 rcu_read_lock();
0bc58a91
EB
2095 /* Attempt to start with the pid of a thread */
2096 if (tid && (nr > 0)) {
2097 pos = find_task_by_pid(tid);
a872ff0c
ON
2098 if (pos && (pos->group_leader == leader))
2099 goto found;
0bc58a91 2100 }
1da177e4 2101
0bc58a91 2102 /* If nr exceeds the number of threads there is nothing todo */
a872ff0c
ON
2103 pos = NULL;
2104 if (nr && nr >= get_nr_threads(leader))
2105 goto out;
1da177e4 2106
a872ff0c
ON
2107 /* If we haven't found our starting place yet start
2108 * with the leader and walk nr threads forward.
0bc58a91 2109 */
a872ff0c
ON
2110 for (pos = leader; nr > 0; --nr) {
2111 pos = next_thread(pos);
2112 if (pos == leader) {
2113 pos = NULL;
2114 goto out;
2115 }
1da177e4 2116 }
a872ff0c
ON
2117found:
2118 get_task_struct(pos);
2119out:
cc288738 2120 rcu_read_unlock();
0bc58a91
EB
2121 return pos;
2122}
2123
2124/*
2125 * Find the next thread in the thread list.
2126 * Return NULL if there is an error or no next thread.
2127 *
2128 * The reference to the input task_struct is released.
2129 */
2130static struct task_struct *next_tid(struct task_struct *start)
2131{
c1df7fb8 2132 struct task_struct *pos = NULL;
cc288738 2133 rcu_read_lock();
c1df7fb8 2134 if (pid_alive(start)) {
0bc58a91 2135 pos = next_thread(start);
c1df7fb8
ON
2136 if (thread_group_leader(pos))
2137 pos = NULL;
2138 else
2139 get_task_struct(pos);
2140 }
cc288738 2141 rcu_read_unlock();
0bc58a91
EB
2142 put_task_struct(start);
2143 return pos;
1da177e4
LT
2144}
2145
2146/* for the /proc/TGID/task/ directories */
2147static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2148{
1da177e4 2149 char buf[PROC_NUMBUF];
1da177e4
LT
2150 struct dentry *dentry = filp->f_dentry;
2151 struct inode *inode = dentry->d_inode;
99f89551 2152 struct task_struct *leader = get_proc_task(inode);
0bc58a91 2153 struct task_struct *task;
1da177e4
LT
2154 int retval = -ENOENT;
2155 ino_t ino;
0bc58a91 2156 int tid;
1da177e4
LT
2157 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2158
99f89551
EB
2159 if (!leader)
2160 goto out_no_task;
1da177e4
LT
2161 retval = 0;
2162
2163 switch (pos) {
2164 case 0:
2165 ino = inode->i_ino;
2166 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2167 goto out;
2168 pos++;
2169 /* fall through */
2170 case 1:
2171 ino = parent_ino(dentry);
2172 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2173 goto out;
2174 pos++;
2175 /* fall through */
2176 }
2177
0bc58a91
EB
2178 /* f_version caches the tgid value that the last readdir call couldn't
2179 * return. lseek aka telldir automagically resets f_version to 0.
2180 */
2181 tid = filp->f_version;
2182 filp->f_version = 0;
2183 for (task = first_tid(leader, tid, pos - 2);
2184 task;
2185 task = next_tid(task), pos++) {
2186 int len;
2187 tid = task->pid;
2188 len = snprintf(buf, sizeof(buf), "%d", tid);
2189 ino = fake_ino(tid, PROC_TID_INO);
2190 if (filldir(dirent, buf, len, pos, ino, DT_DIR < 0)) {
2191 /* returning this tgid failed, save it as the first
2192 * pid for the next readir call */
2193 filp->f_version = tid;
2194 put_task_struct(task);
1da177e4 2195 break;
0bc58a91 2196 }
1da177e4
LT
2197 }
2198out:
2199 filp->f_pos = pos;
99f89551
EB
2200 put_task_struct(leader);
2201out_no_task:
1da177e4
LT
2202 return retval;
2203}
6e66b52b
EB
2204
2205static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2206{
2207 struct inode *inode = dentry->d_inode;
99f89551 2208 struct task_struct *p = get_proc_task(inode);
6e66b52b
EB
2209 generic_fillattr(inode, stat);
2210
99f89551
EB
2211 if (p) {
2212 rcu_read_lock();
2213 stat->nlink += get_nr_threads(p);
2214 rcu_read_unlock();
2215 put_task_struct(p);
6e66b52b
EB
2216 }
2217
2218 return 0;
2219}
28a6d671
EB
2220
2221static struct inode_operations proc_task_inode_operations = {
2222 .lookup = proc_task_lookup,
2223 .getattr = proc_task_getattr,
2224 .setattr = proc_setattr,
2225};
2226
2227static struct file_operations proc_task_operations = {
2228 .read = generic_read_dir,
2229 .readdir = proc_task_readdir,
2230};