]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - fs/proc/base.c
e8084eb037e3034cb7f0fe7adc9a059edbe40fa0
[mirror_ubuntu-jammy-kernel.git] / fs / proc / base.c
1 /*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
14 *
15 *
16 * Changelog:
17 * 17-Jan-2005
18 * Allan Bezerra
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
23 *
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25 *
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
32 *
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
37 *
38 * ChangeLog:
39 * 10-Mar-2005
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
48 */
49
50 #include <asm/uaccess.h>
51
52 #include <linux/errno.h>
53 #include <linux/time.h>
54 #include <linux/proc_fs.h>
55 #include <linux/stat.h>
56 #include <linux/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/namespace.h>
63 #include <linux/mm.h>
64 #include <linux/smp_lock.h>
65 #include <linux/rcupdate.h>
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>
73 #include <linux/poll.h>
74 #include <linux/nsproxy.h>
75 #include "internal.h"
76
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
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
97 enum 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,
115 PROC_TGID_NUMA_MAPS,
116 PROC_TGID_MOUNTS,
117 PROC_TGID_MOUNTSTATS,
118 PROC_TGID_WCHAN,
119 #ifdef CONFIG_MMU
120 PROC_TGID_SMAPS,
121 #endif
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,
134 PROC_TGID_ATTR_KEYCREATE,
135 PROC_TGID_ATTR_SOCKCREATE,
136 #endif
137 #ifdef CONFIG_AUDITSYSCALL
138 PROC_TGID_LOGINUID,
139 #endif
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,
158 PROC_TID_NUMA_MAPS,
159 PROC_TID_MOUNTS,
160 PROC_TID_MOUNTSTATS,
161 PROC_TID_WCHAN,
162 #ifdef CONFIG_MMU
163 PROC_TID_SMAPS,
164 #endif
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,
177 PROC_TID_ATTR_KEYCREATE,
178 PROC_TID_ATTR_SOCKCREATE,
179 #endif
180 #ifdef CONFIG_AUDITSYSCALL
181 PROC_TID_LOGINUID,
182 #endif
183 PROC_TID_OOM_SCORE,
184 PROC_TID_OOM_ADJUST,
185
186 /* Add new entries before this */
187 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
188 };
189
190 /* Worst case buffer size needed for holding an integer. */
191 #define PROC_NUMBUF 10
192
193 struct pid_entry {
194 int type;
195 int len;
196 char *name;
197 mode_t mode;
198 struct inode_operations *iop;
199 struct file_operations *fop;
200 union proc_op op;
201 };
202
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 } )
228
229 static struct fs_struct *get_fs_struct(struct task_struct *task)
230 {
231 struct fs_struct *fs;
232 task_lock(task);
233 fs = task->fs;
234 if(fs)
235 atomic_inc(&fs->count);
236 task_unlock(task);
237 return fs;
238 }
239
240 static 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
253 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
254 {
255 struct task_struct *task = get_proc_task(inode);
256 struct fs_struct *fs = NULL;
257 int result = -ENOENT;
258
259 if (task) {
260 fs = get_fs_struct(task);
261 put_task_struct(task);
262 }
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
274 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
275 {
276 struct task_struct *task = get_proc_task(inode);
277 struct fs_struct *fs = NULL;
278 int result = -ENOENT;
279
280 if (task) {
281 fs = get_fs_struct(task);
282 put_task_struct(task);
283 }
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
302 static 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);
311 if (!ptrace_may_attach(task))
312 res = -ESRCH;
313 mmput(mm);
314 }
315 return res;
316 }
317
318 static 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 }
349 out_mm:
350 mmput(mm);
351 out:
352 return res;
353 }
354
355 static 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 */
379 static 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 */
399 static 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 */
409 unsigned long badness(struct task_struct *p, unsigned long uptime);
410 static 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 */
425 static int proc_fd_access_allowed(struct inode *inode)
426 {
427 struct task_struct *task;
428 int allowed = 0;
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.
432 */
433 task = get_proc_task(inode);
434 if (task) {
435 allowed = ptrace_may_attach(task);
436 put_task_struct(task);
437 }
438 return allowed;
439 }
440
441 static 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
458 static struct inode_operations proc_def_inode_operations = {
459 .setattr = proc_setattr,
460 };
461
462 extern struct seq_operations mounts_op;
463 struct proc_mounts {
464 struct seq_file m;
465 int event;
466 };
467
468 static int mounts_open(struct inode *inode, struct file *file)
469 {
470 struct task_struct *task = get_proc_task(inode);
471 struct namespace *namespace = NULL;
472 struct proc_mounts *p;
473 int ret = -EINVAL;
474
475 if (task) {
476 task_lock(task);
477 namespace = task->nsproxy->namespace;
478 if (namespace)
479 get_namespace(namespace);
480 task_unlock(task);
481 put_task_struct(task);
482 }
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);
496 }
497 put_namespace(namespace);
498 }
499 return ret;
500 }
501
502 static 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
510 static 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
528 static struct file_operations proc_mounts_operations = {
529 .open = mounts_open,
530 .read = seq_read,
531 .llseek = seq_lseek,
532 .release = mounts_release,
533 .poll = mounts_poll,
534 };
535
536 extern struct seq_operations mountstats_op;
537 static int mountstats_open(struct inode *inode, struct file *file)
538 {
539 int ret = seq_open(file, &mountstats_op);
540
541 if (!ret) {
542 struct seq_file *m = file->private_data;
543 struct namespace *namespace = NULL;
544 struct task_struct *task = get_proc_task(inode);
545
546 if (task) {
547 task_lock(task);
548 namespace = task->nsproxy->namespace;
549 if (namespace)
550 get_namespace(namespace);
551 task_unlock(task);
552 put_task_struct(task);
553 }
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
565 static struct file_operations proc_mountstats_operations = {
566 .open = mountstats_open,
567 .read = seq_read,
568 .llseek = seq_lseek,
569 .release = mounts_release,
570 };
571
572 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
573
574 static 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;
580 struct task_struct *task = get_proc_task(inode);
581
582 length = -ESRCH;
583 if (!task)
584 goto out_no_task;
585
586 if (count > PROC_BLOCK_SIZE)
587 count = PROC_BLOCK_SIZE;
588
589 length = -ENOMEM;
590 if (!(page = __get_free_page(GFP_KERNEL)))
591 goto out;
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);
598 out:
599 put_task_struct(task);
600 out_no_task:
601 return length;
602 }
603
604 static struct file_operations proc_info_file_operations = {
605 .read = proc_info_read,
606 };
607
608 static 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
614 static ssize_t mem_read(struct file * file, char __user * buf,
615 size_t count, loff_t *ppos)
616 {
617 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
618 char *page;
619 unsigned long src = *ppos;
620 int ret = -ESRCH;
621 struct mm_struct *mm;
622
623 if (!task)
624 goto out_no_task;
625
626 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
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);
652 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
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
670 out_put:
671 mmput(mm);
672 out_free:
673 free_page((unsigned long) page);
674 out:
675 put_task_struct(task);
676 out_no_task:
677 return ret;
678 }
679
680 #define mem_write NULL
681
682 #ifndef mem_write
683 /* This is a security hazard */
684 static ssize_t mem_write(struct file * file, const char * buf,
685 size_t count, loff_t *ppos)
686 {
687 int copied;
688 char *page;
689 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
690 unsigned long dst = *ppos;
691
692 copied = -ESRCH;
693 if (!task)
694 goto out_no_task;
695
696 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
697 goto out;
698
699 copied = -ENOMEM;
700 page = (char *)__get_free_page(GFP_USER);
701 if (!page)
702 goto out;
703
704 copied = 0;
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);
726 out:
727 put_task_struct(task);
728 out_no_task:
729 return copied;
730 }
731 #endif
732
733 static 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
749 static struct file_operations proc_mem_operations = {
750 .llseek = mem_lseek,
751 .read = mem_read,
752 .write = mem_write,
753 .open = mem_open,
754 };
755
756 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
757 size_t count, loff_t *ppos)
758 {
759 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
760 char buffer[PROC_NUMBUF];
761 size_t len;
762 int oom_adjust;
763 loff_t __ppos = *ppos;
764
765 if (!task)
766 return -ESRCH;
767 oom_adjust = task->oomkilladj;
768 put_task_struct(task);
769
770 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
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
781 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
782 size_t count, loff_t *ppos)
783 {
784 struct task_struct *task;
785 char buffer[PROC_NUMBUF], *end;
786 int oom_adjust;
787
788 if (!capable(CAP_SYS_RESOURCE))
789 return -EPERM;
790 memset(buffer, 0, sizeof(buffer));
791 if (count > sizeof(buffer) - 1)
792 count = sizeof(buffer) - 1;
793 if (copy_from_user(buffer, buf, count))
794 return -EFAULT;
795 oom_adjust = simple_strtol(buffer, &end, 0);
796 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
797 return -EINVAL;
798 if (*end == '\n')
799 end++;
800 task = get_proc_task(file->f_dentry->d_inode);
801 if (!task)
802 return -ESRCH;
803 task->oomkilladj = oom_adjust;
804 put_task_struct(task);
805 if (end - buffer == 0)
806 return -EIO;
807 return end - buffer;
808 }
809
810 static struct file_operations proc_oom_adjust_operations = {
811 .read = oom_adjust_read,
812 .write = oom_adjust_write,
813 };
814
815 #ifdef CONFIG_AUDITSYSCALL
816 #define TMPBUFLEN 21
817 static 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;
821 struct task_struct *task = get_proc_task(inode);
822 ssize_t length;
823 char tmpbuf[TMPBUFLEN];
824
825 if (!task)
826 return -ESRCH;
827 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
828 audit_get_loginuid(task->audit_context));
829 put_task_struct(task);
830 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
831 }
832
833 static 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;
839 uid_t loginuid;
840
841 if (!capable(CAP_AUDIT_CONTROL))
842 return -EPERM;
843
844 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
845 return -EPERM;
846
847 if (count >= PAGE_SIZE)
848 count = PAGE_SIZE - 1;
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
861 page[count] = '\0';
862 loginuid = simple_strtoul(page, &tmp, 10);
863 if (tmp == page) {
864 length = -EINVAL;
865 goto out_free_page;
866
867 }
868 length = audit_set_loginuid(current, loginuid);
869 if (likely(length == 0))
870 length = count;
871
872 out_free_page:
873 free_page((unsigned long) page);
874 return length;
875 }
876
877 static struct file_operations proc_loginuid_operations = {
878 .read = proc_loginuid_read,
879 .write = proc_loginuid_write,
880 };
881 #endif
882
883 #ifdef CONFIG_SECCOMP
884 static ssize_t seccomp_read(struct file *file, char __user *buf,
885 size_t count, loff_t *ppos)
886 {
887 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
888 char __buf[20];
889 loff_t __ppos = *ppos;
890 size_t len;
891
892 if (!tsk)
893 return -ESRCH;
894 /* no need to print the trailing zero, so use only len */
895 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
896 put_task_struct(tsk);
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
907 static ssize_t seccomp_write(struct file *file, const char __user *buf,
908 size_t count, loff_t *ppos)
909 {
910 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
911 char __buf[20], *end;
912 unsigned int seccomp_mode;
913 ssize_t result;
914
915 result = -ESRCH;
916 if (!tsk)
917 goto out_no_task;
918
919 /* can set it only once to be even more secure */
920 result = -EPERM;
921 if (unlikely(tsk->seccomp.mode))
922 goto out;
923
924 result = -EFAULT;
925 memset(__buf, 0, sizeof(__buf));
926 count = min(count, sizeof(__buf) - 1);
927 if (copy_from_user(__buf, buf, count))
928 goto out;
929
930 seccomp_mode = simple_strtoul(__buf, &end, 0);
931 if (*end == '\n')
932 end++;
933 result = -EINVAL;
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
938 goto out;
939 result = -EIO;
940 if (unlikely(!(end - __buf)))
941 goto out;
942 result = end - __buf;
943 out:
944 put_task_struct(tsk);
945 out_no_task:
946 return result;
947 }
948
949 static struct file_operations proc_seccomp_operations = {
950 .read = seccomp_read,
951 .write = seccomp_write,
952 };
953 #endif /* CONFIG_SECCOMP */
954
955 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
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
963 /* Are we allowed to snoop on the tasks file descriptors? */
964 if (!proc_fd_access_allowed(inode))
965 goto out;
966
967 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
968 nd->last_type = LAST_BIND;
969 out:
970 return ERR_PTR(error);
971 }
972
973 static 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
999 static 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
1006 /* Are we allowed to snoop on the tasks file descriptors? */
1007 if (!proc_fd_access_allowed(inode))
1008 goto out;
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);
1017 out:
1018 return error;
1019 }
1020
1021 static struct inode_operations proc_pid_link_inode_operations = {
1022 .readlink = proc_pid_readlink,
1023 .follow_link = proc_pid_follow_link,
1024 .setattr = proc_setattr,
1025 };
1026
1027
1028 /* building an inode */
1029
1030 static int task_dumpable(struct task_struct *task)
1031 {
1032 int dumpable = 0;
1033 struct mm_struct *mm;
1034
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 }
1044
1045
1046 static 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;
1050
1051 /* We need a new inode */
1052
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;
1075 }
1076 security_task_to_inode(task, inode);
1077
1078 out:
1079 return inode;
1080
1081 out_unlock:
1082 iput(inode);
1083 return NULL;
1084 }
1085
1086 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1087 {
1088 struct inode *inode = dentry->d_inode;
1089 struct task_struct *task;
1090 generic_fillattr(inode, stat);
1091
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;
1101 }
1102 }
1103 rcu_read_unlock();
1104 return 0;
1105 }
1106
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.
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.
1123 */
1124 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1125 {
1126 struct inode *inode = dentry->d_inode;
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)) {
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 }
1137 inode->i_mode &= ~(S_ISUID | S_ISGID);
1138 security_task_to_inode(task, inode);
1139 put_task_struct(task);
1140 return 1;
1141 }
1142 d_drop(dentry);
1143 return 0;
1144 }
1145
1146 static int pid_delete_dentry(struct dentry * dentry)
1147 {
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
1155 static struct dentry_operations pid_dentry_operations =
1156 {
1157 .d_revalidate = pid_revalidate,
1158 .d_delete = pid_delete_dentry,
1159 };
1160
1161 /* Lookups */
1162
1163 static 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;
1181 out:
1182 return ~0U;
1183 }
1184
1185 static 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);
1191
1192 if (task) {
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;
1209 }
1210 spin_unlock(&files->file_lock);
1211 put_files_struct(files);
1212 }
1213 return -ENOENT;
1214 }
1215
1216 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1217 {
1218 struct inode *inode = dentry->d_inode;
1219 struct task_struct *task = get_proc_task(inode);
1220 int fd = proc_fd(inode);
1221 struct files_struct *files;
1222
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 }
1237 inode->i_mode &= ~(S_ISUID | S_ISGID);
1238 security_task_to_inode(task, inode);
1239 put_task_struct(task);
1240 return 1;
1241 }
1242 rcu_read_unlock();
1243 put_files_struct(files);
1244 }
1245 put_task_struct(task);
1246 }
1247 d_drop(dentry);
1248 return 0;
1249 }
1250
1251 static struct dentry_operations tid_fd_dentry_operations =
1252 {
1253 .d_revalidate = tid_fd_revalidate,
1254 .d_delete = pid_delete_dentry,
1255 };
1256
1257 static struct dentry *proc_fd_instantiate(struct inode *dir,
1258 struct dentry *dentry, struct task_struct *task, void *ptr)
1259 {
1260 unsigned fd = *(unsigned *)ptr;
1261 struct file *file;
1262 struct files_struct *files;
1263 struct inode *inode;
1264 struct proc_inode *ei;
1265 struct dentry *error = ERR_PTR(-ENOENT);
1266
1267 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1268 if (!inode)
1269 goto out;
1270 ei = PROC_I(inode);
1271 ei->fd = fd;
1272 files = get_files_struct(task);
1273 if (!files)
1274 goto out_iput;
1275 inode->i_mode = S_IFLNK;
1276
1277 /*
1278 * We are not taking a ref to the file structure, so we must
1279 * hold ->file_lock.
1280 */
1281 spin_lock(&files->file_lock);
1282 file = fcheck_files(files, fd);
1283 if (!file)
1284 goto out_unlock;
1285 if (file->f_mode & 1)
1286 inode->i_mode |= S_IRUSR | S_IXUSR;
1287 if (file->f_mode & 2)
1288 inode->i_mode |= S_IWUSR | S_IXUSR;
1289 spin_unlock(&files->file_lock);
1290 put_files_struct(files);
1291
1292 inode->i_op = &proc_pid_link_inode_operations;
1293 inode->i_size = 64;
1294 ei->op.proc_get_link = proc_fd_link;
1295 dentry->d_op = &tid_fd_dentry_operations;
1296 d_add(dentry, inode);
1297 /* Close the race of the process dying before we return the dentry */
1298 if (tid_fd_revalidate(dentry, NULL))
1299 error = NULL;
1300
1301 out:
1302 return error;
1303 out_unlock:
1304 spin_unlock(&files->file_lock);
1305 put_files_struct(files);
1306 out_iput:
1307 iput(inode);
1308 goto out;
1309 }
1310
1311 /* SMP-safe */
1312 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1313 {
1314 struct task_struct *task = get_proc_task(dir);
1315 unsigned fd = name_to_int(dentry);
1316 struct dentry *result = ERR_PTR(-ENOENT);
1317
1318 if (!task)
1319 goto out_no_task;
1320 if (fd == ~0U)
1321 goto out;
1322
1323 result = proc_fd_instantiate(dir, dentry, task, &fd);
1324 out:
1325 put_task_struct(task);
1326 out_no_task:
1327 return result;
1328 }
1329
1330 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1331 {
1332 struct dentry *dentry = filp->f_dentry;
1333 struct inode *inode = dentry->d_inode;
1334 struct task_struct *p = get_proc_task(inode);
1335 unsigned int fd, tid, ino;
1336 int retval;
1337 char buf[PROC_NUMBUF];
1338 struct files_struct * files;
1339 struct fdtable *fdt;
1340
1341 retval = -ENOENT;
1342 if (!p)
1343 goto out_no_task;
1344 retval = 0;
1345 tid = p->pid;
1346
1347 fd = filp->f_pos;
1348 switch (fd) {
1349 case 0:
1350 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1351 goto out;
1352 filp->f_pos++;
1353 case 1:
1354 ino = parent_ino(dentry);
1355 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1356 goto out;
1357 filp->f_pos++;
1358 default:
1359 files = get_files_struct(p);
1360 if (!files)
1361 goto out;
1362 rcu_read_lock();
1363 fdt = files_fdtable(files);
1364 for (fd = filp->f_pos-2;
1365 fd < fdt->max_fds;
1366 fd++, filp->f_pos++) {
1367 unsigned int i,j;
1368
1369 if (!fcheck_files(files, fd))
1370 continue;
1371 rcu_read_unlock();
1372
1373 j = PROC_NUMBUF;
1374 i = fd;
1375 do {
1376 j--;
1377 buf[j] = '0' + (i % 10);
1378 i /= 10;
1379 } while (i);
1380
1381 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
1382 if (filldir(dirent, buf+j, PROC_NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
1383 rcu_read_lock();
1384 break;
1385 }
1386 rcu_read_lock();
1387 }
1388 rcu_read_unlock();
1389 put_files_struct(files);
1390 }
1391 out:
1392 put_task_struct(p);
1393 out_no_task:
1394 return retval;
1395 }
1396
1397 static struct file_operations proc_fd_operations = {
1398 .read = generic_read_dir,
1399 .readdir = proc_readfd,
1400 };
1401
1402 /*
1403 * proc directories can do almost nothing..
1404 */
1405 static struct inode_operations proc_fd_inode_operations = {
1406 .lookup = proc_lookupfd,
1407 .setattr = proc_setattr,
1408 };
1409
1410 static struct dentry *proc_pident_instantiate(struct inode *dir,
1411 struct dentry *dentry, struct task_struct *task, void *ptr)
1412 {
1413 struct pid_entry *p = ptr;
1414 struct inode *inode;
1415 struct proc_inode *ei;
1416 struct dentry *error = ERR_PTR(-EINVAL);
1417
1418 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1419 if (!inode)
1420 goto out;
1421
1422 ei = PROC_I(inode);
1423 inode->i_mode = p->mode;
1424 if (S_ISDIR(inode->i_mode))
1425 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1426 if (p->iop)
1427 inode->i_op = p->iop;
1428 if (p->fop)
1429 inode->i_fop = p->fop;
1430 ei->op = p->op;
1431 dentry->d_op = &pid_dentry_operations;
1432 d_add(dentry, inode);
1433 /* Close the race of the process dying before we return the dentry */
1434 if (pid_revalidate(dentry, NULL))
1435 error = NULL;
1436 out:
1437 return error;
1438 }
1439
1440 /* SMP-safe */
1441 static struct dentry *proc_pident_lookup(struct inode *dir,
1442 struct dentry *dentry,
1443 struct pid_entry *ents)
1444 {
1445 struct inode *inode;
1446 struct dentry *error;
1447 struct task_struct *task = get_proc_task(dir);
1448 struct pid_entry *p;
1449
1450 error = ERR_PTR(-ENOENT);
1451 inode = NULL;
1452
1453 if (!task)
1454 goto out_no_task;
1455
1456 /*
1457 * Yes, it does not scale. And it should not. Don't add
1458 * new entries into /proc/<tgid>/ without very good reasons.
1459 */
1460 for (p = ents; p->name; p++) {
1461 if (p->len != dentry->d_name.len)
1462 continue;
1463 if (!memcmp(dentry->d_name.name, p->name, p->len))
1464 break;
1465 }
1466 if (!p->name)
1467 goto out;
1468
1469 error = proc_pident_instantiate(dir, dentry, task, p);
1470 out:
1471 put_task_struct(task);
1472 out_no_task:
1473 return error;
1474 }
1475
1476 static int proc_pident_readdir(struct file *filp,
1477 void *dirent, filldir_t filldir,
1478 struct pid_entry *ents, unsigned int nents)
1479 {
1480 int i;
1481 int pid;
1482 struct dentry *dentry = filp->f_dentry;
1483 struct inode *inode = dentry->d_inode;
1484 struct task_struct *task = get_proc_task(inode);
1485 struct pid_entry *p;
1486 ino_t ino;
1487 int ret;
1488
1489 ret = -ENOENT;
1490 if (!task)
1491 goto out;
1492
1493 ret = 0;
1494 pid = task->pid;
1495 put_task_struct(task);
1496 i = filp->f_pos;
1497 switch (i) {
1498 case 0:
1499 ino = inode->i_ino;
1500 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1501 goto out;
1502 i++;
1503 filp->f_pos++;
1504 /* fall through */
1505 case 1:
1506 ino = parent_ino(dentry);
1507 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1508 goto out;
1509 i++;
1510 filp->f_pos++;
1511 /* fall through */
1512 default:
1513 i -= 2;
1514 if (i >= nents) {
1515 ret = 1;
1516 goto out;
1517 }
1518 p = ents + i;
1519 while (p->name) {
1520 if (filldir(dirent, p->name, p->len, filp->f_pos,
1521 fake_ino(pid, p->type), p->mode >> 12) < 0)
1522 goto out;
1523 filp->f_pos++;
1524 p++;
1525 }
1526 }
1527
1528 ret = 1;
1529 out:
1530 return ret;
1531 }
1532
1533 #ifdef CONFIG_SECURITY
1534 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1535 size_t count, loff_t *ppos)
1536 {
1537 struct inode * inode = file->f_dentry->d_inode;
1538 unsigned long page;
1539 ssize_t length;
1540 struct task_struct *task = get_proc_task(inode);
1541
1542 length = -ESRCH;
1543 if (!task)
1544 goto out_no_task;
1545
1546 if (count > PAGE_SIZE)
1547 count = PAGE_SIZE;
1548 length = -ENOMEM;
1549 if (!(page = __get_free_page(GFP_KERNEL)))
1550 goto out;
1551
1552 length = security_getprocattr(task,
1553 (char*)file->f_dentry->d_name.name,
1554 (void*)page, count);
1555 if (length >= 0)
1556 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1557 free_page(page);
1558 out:
1559 put_task_struct(task);
1560 out_no_task:
1561 return length;
1562 }
1563
1564 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1565 size_t count, loff_t *ppos)
1566 {
1567 struct inode * inode = file->f_dentry->d_inode;
1568 char *page;
1569 ssize_t length;
1570 struct task_struct *task = get_proc_task(inode);
1571
1572 length = -ESRCH;
1573 if (!task)
1574 goto out_no_task;
1575 if (count > PAGE_SIZE)
1576 count = PAGE_SIZE;
1577
1578 /* No partial writes. */
1579 length = -EINVAL;
1580 if (*ppos != 0)
1581 goto out;
1582
1583 length = -ENOMEM;
1584 page = (char*)__get_free_page(GFP_USER);
1585 if (!page)
1586 goto out;
1587
1588 length = -EFAULT;
1589 if (copy_from_user(page, buf, count))
1590 goto out_free;
1591
1592 length = security_setprocattr(task,
1593 (char*)file->f_dentry->d_name.name,
1594 (void*)page, count);
1595 out_free:
1596 free_page((unsigned long) page);
1597 out:
1598 put_task_struct(task);
1599 out_no_task:
1600 return length;
1601 }
1602
1603 static struct file_operations proc_pid_attr_operations = {
1604 .read = proc_pid_attr_read,
1605 .write = proc_pid_attr_write,
1606 };
1607
1608 static struct pid_entry tgid_attr_stuff[] = {
1609 REG(PROC_TGID_ATTR_CURRENT, "current", S_IRUGO|S_IWUGO, pid_attr),
1610 REG(PROC_TGID_ATTR_PREV, "prev", S_IRUGO, pid_attr),
1611 REG(PROC_TGID_ATTR_EXEC, "exec", S_IRUGO|S_IWUGO, pid_attr),
1612 REG(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IRUGO|S_IWUGO, pid_attr),
1613 REG(PROC_TGID_ATTR_KEYCREATE, "keycreate", S_IRUGO|S_IWUGO, pid_attr),
1614 REG(PROC_TGID_ATTR_SOCKCREATE, "sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1615 {}
1616 };
1617 static struct pid_entry tid_attr_stuff[] = {
1618 REG(PROC_TID_ATTR_CURRENT, "current", S_IRUGO|S_IWUGO, pid_attr),
1619 REG(PROC_TID_ATTR_PREV, "prev", S_IRUGO, pid_attr),
1620 REG(PROC_TID_ATTR_EXEC, "exec", S_IRUGO|S_IWUGO, pid_attr),
1621 REG(PROC_TID_ATTR_FSCREATE, "fscreate", S_IRUGO|S_IWUGO, pid_attr),
1622 REG(PROC_TID_ATTR_KEYCREATE, "keycreate", S_IRUGO|S_IWUGO, pid_attr),
1623 REG(PROC_TID_ATTR_SOCKCREATE, "sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1624 {}
1625 };
1626
1627 static int proc_tgid_attr_readdir(struct file * filp,
1628 void * dirent, filldir_t filldir)
1629 {
1630 return proc_pident_readdir(filp,dirent,filldir,
1631 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1632 }
1633
1634 static int proc_tid_attr_readdir(struct file * filp,
1635 void * dirent, filldir_t filldir)
1636 {
1637 return proc_pident_readdir(filp,dirent,filldir,
1638 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1639 }
1640
1641 static struct file_operations proc_tgid_attr_operations = {
1642 .read = generic_read_dir,
1643 .readdir = proc_tgid_attr_readdir,
1644 };
1645
1646 static struct file_operations proc_tid_attr_operations = {
1647 .read = generic_read_dir,
1648 .readdir = proc_tid_attr_readdir,
1649 };
1650
1651 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1652 struct dentry *dentry, struct nameidata *nd)
1653 {
1654 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1655 }
1656
1657 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1658 struct dentry *dentry, struct nameidata *nd)
1659 {
1660 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1661 }
1662
1663 static struct inode_operations proc_tgid_attr_inode_operations = {
1664 .lookup = proc_tgid_attr_lookup,
1665 .getattr = pid_getattr,
1666 .setattr = proc_setattr,
1667 };
1668
1669 static struct inode_operations proc_tid_attr_inode_operations = {
1670 .lookup = proc_tid_attr_lookup,
1671 .getattr = pid_getattr,
1672 .setattr = proc_setattr,
1673 };
1674 #endif
1675
1676 /*
1677 * /proc/self:
1678 */
1679 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1680 int buflen)
1681 {
1682 char tmp[PROC_NUMBUF];
1683 sprintf(tmp, "%d", current->tgid);
1684 return vfs_readlink(dentry,buffer,buflen,tmp);
1685 }
1686
1687 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1688 {
1689 char tmp[PROC_NUMBUF];
1690 sprintf(tmp, "%d", current->tgid);
1691 return ERR_PTR(vfs_follow_link(nd,tmp));
1692 }
1693
1694 static struct inode_operations proc_self_inode_operations = {
1695 .readlink = proc_self_readlink,
1696 .follow_link = proc_self_follow_link,
1697 };
1698
1699 /*
1700 * proc base
1701 *
1702 * These are the directory entries in the root directory of /proc
1703 * that properly belong to the /proc filesystem, as they describe
1704 * describe something that is process related.
1705 */
1706 static struct pid_entry proc_base_stuff[] = {
1707 NOD(PROC_TGID_INO, "self", S_IFLNK|S_IRWXUGO,
1708 &proc_self_inode_operations, NULL, {}),
1709 {}
1710 };
1711
1712 /*
1713 * Exceptional case: normally we are not allowed to unhash a busy
1714 * directory. In this case, however, we can do it - no aliasing problems
1715 * due to the way we treat inodes.
1716 */
1717 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1718 {
1719 struct inode *inode = dentry->d_inode;
1720 struct task_struct *task = get_proc_task(inode);
1721 if (task) {
1722 put_task_struct(task);
1723 return 1;
1724 }
1725 d_drop(dentry);
1726 return 0;
1727 }
1728
1729 static struct dentry_operations proc_base_dentry_operations =
1730 {
1731 .d_revalidate = proc_base_revalidate,
1732 .d_delete = pid_delete_dentry,
1733 };
1734
1735 static struct dentry *proc_base_instantiate(struct inode *dir,
1736 struct dentry *dentry, struct task_struct *task, void *ptr)
1737 {
1738 struct pid_entry *p = ptr;
1739 struct inode *inode;
1740 struct proc_inode *ei;
1741 struct dentry *error = ERR_PTR(-EINVAL);
1742
1743 /* Allocate the inode */
1744 error = ERR_PTR(-ENOMEM);
1745 inode = new_inode(dir->i_sb);
1746 if (!inode)
1747 goto out;
1748
1749 /* Initialize the inode */
1750 ei = PROC_I(inode);
1751 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1752 inode->i_ino = fake_ino(0, p->type);
1753
1754 /*
1755 * grab the reference to the task.
1756 */
1757 ei->pid = get_pid(task_pid(task));
1758 if (!ei->pid)
1759 goto out_iput;
1760
1761 inode->i_uid = 0;
1762 inode->i_gid = 0;
1763 inode->i_mode = p->mode;
1764 if (S_ISDIR(inode->i_mode))
1765 inode->i_nlink = 2;
1766 if (S_ISLNK(inode->i_mode))
1767 inode->i_size = 64;
1768 if (p->iop)
1769 inode->i_op = p->iop;
1770 if (p->fop)
1771 inode->i_fop = p->fop;
1772 ei->op = p->op;
1773 dentry->d_op = &proc_base_dentry_operations;
1774 d_add(dentry, inode);
1775 error = NULL;
1776 out:
1777 return error;
1778 out_iput:
1779 iput(inode);
1780 goto out;
1781 }
1782
1783 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
1784 {
1785 struct dentry *error;
1786 struct task_struct *task = get_proc_task(dir);
1787 struct pid_entry *p;
1788
1789 error = ERR_PTR(-ENOENT);
1790
1791 if (!task)
1792 goto out_no_task;
1793
1794 /* Lookup the directory entry */
1795 for (p = proc_base_stuff; p->name; p++) {
1796 if (p->len != dentry->d_name.len)
1797 continue;
1798 if (!memcmp(dentry->d_name.name, p->name, p->len))
1799 break;
1800 }
1801 if (!p->name)
1802 goto out;
1803
1804 error = proc_base_instantiate(dir, dentry, task, p);
1805
1806 out:
1807 put_task_struct(task);
1808 out_no_task:
1809 return error;
1810 }
1811
1812 /*
1813 * Thread groups
1814 */
1815 static struct file_operations proc_task_operations;
1816 static struct inode_operations proc_task_inode_operations;
1817
1818 static struct pid_entry tgid_base_stuff[] = {
1819 DIR(PROC_TGID_TASK, "task", S_IRUGO|S_IXUGO, task),
1820 DIR(PROC_TGID_FD, "fd", S_IRUSR|S_IXUSR, fd),
1821 INF(PROC_TGID_ENVIRON, "environ", S_IRUSR, pid_environ),
1822 INF(PROC_TGID_AUXV, "auxv", S_IRUSR, pid_auxv),
1823 INF(PROC_TGID_STATUS, "status", S_IRUGO, pid_status),
1824 INF(PROC_TGID_CMDLINE, "cmdline", S_IRUGO, pid_cmdline),
1825 INF(PROC_TGID_STAT, "stat", S_IRUGO, tgid_stat),
1826 INF(PROC_TGID_STATM, "statm", S_IRUGO, pid_statm),
1827 REG(PROC_TGID_MAPS, "maps", S_IRUGO, maps),
1828 #ifdef CONFIG_NUMA
1829 REG(PROC_TGID_NUMA_MAPS, "numa_maps", S_IRUGO, numa_maps),
1830 #endif
1831 REG(PROC_TGID_MEM, "mem", S_IRUSR|S_IWUSR, mem),
1832 #ifdef CONFIG_SECCOMP
1833 REG(PROC_TGID_SECCOMP, "seccomp", S_IRUSR|S_IWUSR, seccomp),
1834 #endif
1835 LNK(PROC_TGID_CWD, "cwd", cwd),
1836 LNK(PROC_TGID_ROOT, "root", root),
1837 LNK(PROC_TGID_EXE, "exe", exe),
1838 REG(PROC_TGID_MOUNTS, "mounts", S_IRUGO, mounts),
1839 REG(PROC_TGID_MOUNTSTATS, "mountstats", S_IRUSR, mountstats),
1840 #ifdef CONFIG_MMU
1841 REG(PROC_TGID_SMAPS, "smaps", S_IRUGO, smaps),
1842 #endif
1843 #ifdef CONFIG_SECURITY
1844 DIR(PROC_TGID_ATTR, "attr", S_IRUGO|S_IXUGO, tgid_attr),
1845 #endif
1846 #ifdef CONFIG_KALLSYMS
1847 INF(PROC_TGID_WCHAN, "wchan", S_IRUGO, pid_wchan),
1848 #endif
1849 #ifdef CONFIG_SCHEDSTATS
1850 INF(PROC_TGID_SCHEDSTAT, "schedstat", S_IRUGO, pid_schedstat),
1851 #endif
1852 #ifdef CONFIG_CPUSETS
1853 REG(PROC_TGID_CPUSET, "cpuset", S_IRUGO, cpuset),
1854 #endif
1855 INF(PROC_TGID_OOM_SCORE, "oom_score", S_IRUGO, oom_score),
1856 REG(PROC_TGID_OOM_ADJUST, "oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
1857 #ifdef CONFIG_AUDITSYSCALL
1858 REG(PROC_TGID_LOGINUID, "loginuid", S_IWUSR|S_IRUGO, loginuid),
1859 #endif
1860 {}
1861 };
1862
1863 static int proc_tgid_base_readdir(struct file * filp,
1864 void * dirent, filldir_t filldir)
1865 {
1866 return proc_pident_readdir(filp,dirent,filldir,
1867 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1868 }
1869
1870 static struct file_operations proc_tgid_base_operations = {
1871 .read = generic_read_dir,
1872 .readdir = proc_tgid_base_readdir,
1873 };
1874
1875 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1876 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1877 }
1878
1879 static struct inode_operations proc_tgid_base_inode_operations = {
1880 .lookup = proc_tgid_base_lookup,
1881 .getattr = pid_getattr,
1882 .setattr = proc_setattr,
1883 };
1884
1885 /**
1886 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
1887 *
1888 * @task: task that should be flushed.
1889 *
1890 * Looks in the dcache for
1891 * /proc/@pid
1892 * /proc/@tgid/task/@pid
1893 * if either directory is present flushes it and all of it'ts children
1894 * from the dcache.
1895 *
1896 * It is safe and reasonable to cache /proc entries for a task until
1897 * that task exits. After that they just clog up the dcache with
1898 * useless entries, possibly causing useful dcache entries to be
1899 * flushed instead. This routine is proved to flush those useless
1900 * dcache entries at process exit time.
1901 *
1902 * NOTE: This routine is just an optimization so it does not guarantee
1903 * that no dcache entries will exist at process exit time it
1904 * just makes it very unlikely that any will persist.
1905 */
1906 void proc_flush_task(struct task_struct *task)
1907 {
1908 struct dentry *dentry, *leader, *dir;
1909 char buf[PROC_NUMBUF];
1910 struct qstr name;
1911
1912 name.name = buf;
1913 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1914 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1915 if (dentry) {
1916 shrink_dcache_parent(dentry);
1917 d_drop(dentry);
1918 dput(dentry);
1919 }
1920
1921 if (thread_group_leader(task))
1922 goto out;
1923
1924 name.name = buf;
1925 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
1926 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1927 if (!leader)
1928 goto out;
1929
1930 name.name = "task";
1931 name.len = strlen(name.name);
1932 dir = d_hash_and_lookup(leader, &name);
1933 if (!dir)
1934 goto out_put_leader;
1935
1936 name.name = buf;
1937 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1938 dentry = d_hash_and_lookup(dir, &name);
1939 if (dentry) {
1940 shrink_dcache_parent(dentry);
1941 d_drop(dentry);
1942 dput(dentry);
1943 }
1944
1945 dput(dir);
1946 out_put_leader:
1947 dput(leader);
1948 out:
1949 return;
1950 }
1951
1952 struct dentry *proc_pid_instantiate(struct inode *dir,
1953 struct dentry * dentry, struct task_struct *task, void *ptr)
1954 {
1955 struct dentry *error = ERR_PTR(-ENOENT);
1956 struct inode *inode;
1957
1958 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
1959 if (!inode)
1960 goto out;
1961
1962 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1963 inode->i_op = &proc_tgid_base_inode_operations;
1964 inode->i_fop = &proc_tgid_base_operations;
1965 inode->i_flags|=S_IMMUTABLE;
1966 inode->i_nlink = 4;
1967 #ifdef CONFIG_SECURITY
1968 inode->i_nlink += 1;
1969 #endif
1970
1971 dentry->d_op = &pid_dentry_operations;
1972
1973 d_add(dentry, inode);
1974 /* Close the race of the process dying before we return the dentry */
1975 if (pid_revalidate(dentry, NULL))
1976 error = NULL;
1977 out:
1978 return error;
1979 }
1980
1981 /* SMP-safe */
1982 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1983 {
1984 struct dentry *result = ERR_PTR(-ENOENT);
1985 struct task_struct *task;
1986 unsigned tgid;
1987
1988 result = proc_base_lookup(dir, dentry);
1989 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
1990 goto out;
1991
1992 tgid = name_to_int(dentry);
1993 if (tgid == ~0U)
1994 goto out;
1995
1996 rcu_read_lock();
1997 task = find_task_by_pid(tgid);
1998 if (task)
1999 get_task_struct(task);
2000 rcu_read_unlock();
2001 if (!task)
2002 goto out;
2003
2004 result = proc_pid_instantiate(dir, dentry, task, NULL);
2005 put_task_struct(task);
2006 out:
2007 return result;
2008 }
2009
2010 /*
2011 * Find the first task with tgid >= tgid
2012 *
2013 */
2014 static struct task_struct *next_tgid(unsigned int tgid)
2015 {
2016 struct task_struct *task;
2017 struct pid *pid;
2018
2019 rcu_read_lock();
2020 retry:
2021 task = NULL;
2022 pid = find_ge_pid(tgid);
2023 if (pid) {
2024 tgid = pid->nr + 1;
2025 task = pid_task(pid, PIDTYPE_PID);
2026 /* What we to know is if the pid we have find is the
2027 * pid of a thread_group_leader. Testing for task
2028 * being a thread_group_leader is the obvious thing
2029 * todo but there is a window when it fails, due to
2030 * the pid transfer logic in de_thread.
2031 *
2032 * So we perform the straight forward test of seeing
2033 * if the pid we have found is the pid of a thread
2034 * group leader, and don't worry if the task we have
2035 * found doesn't happen to be a thread group leader.
2036 * As we don't care in the case of readdir.
2037 */
2038 if (!task || !has_group_leader_pid(task))
2039 goto retry;
2040 get_task_struct(task);
2041 }
2042 rcu_read_unlock();
2043 return task;
2044 }
2045
2046 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + (1 /* /proc/self */))
2047
2048 /* for the /proc/ directory itself, after non-process stuff has been done */
2049 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2050 {
2051 char buf[PROC_NUMBUF];
2052 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2053 struct task_struct *task;
2054 int tgid;
2055
2056 for (; nr < (ARRAY_SIZE(proc_base_stuff) - 1); filp->f_pos++, nr++) {
2057 struct pid_entry *p = &proc_base_stuff[nr];
2058 if (filldir(dirent, p->name, p->len, filp->f_pos,
2059 fake_ino(0, p->type), p->mode >> 12) < 0)
2060 goto out;
2061 }
2062
2063 tgid = filp->f_pos - TGID_OFFSET;
2064 for (task = next_tgid(tgid);
2065 task;
2066 put_task_struct(task), task = next_tgid(tgid + 1)) {
2067 int len;
2068 ino_t ino;
2069 tgid = task->pid;
2070 filp->f_pos = tgid + TGID_OFFSET;
2071 len = snprintf(buf, sizeof(buf), "%d", tgid);
2072 ino = fake_ino(tgid, PROC_TGID_INO);
2073 if (filldir(dirent, buf, len, filp->f_pos, ino, DT_DIR) < 0) {
2074 put_task_struct(task);
2075 goto out;
2076 }
2077 }
2078 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2079 out:
2080 return 0;
2081 }
2082
2083 /*
2084 * Tasks
2085 */
2086 static struct pid_entry tid_base_stuff[] = {
2087 DIR(PROC_TID_FD, "fd", S_IRUSR|S_IXUSR, fd),
2088 INF(PROC_TID_ENVIRON, "environ", S_IRUSR, pid_environ),
2089 INF(PROC_TID_AUXV, "auxv", S_IRUSR, pid_auxv),
2090 INF(PROC_TID_STATUS, "status", S_IRUGO, pid_status),
2091 INF(PROC_TID_CMDLINE, "cmdline", S_IRUGO, pid_cmdline),
2092 INF(PROC_TID_STAT, "stat", S_IRUGO, tid_stat),
2093 INF(PROC_TID_STATM, "statm", S_IRUGO, pid_statm),
2094 REG(PROC_TID_MAPS, "maps", S_IRUGO, maps),
2095 #ifdef CONFIG_NUMA
2096 REG(PROC_TID_NUMA_MAPS, "numa_maps", S_IRUGO, numa_maps),
2097 #endif
2098 REG(PROC_TID_MEM, "mem", S_IRUSR|S_IWUSR, mem),
2099 #ifdef CONFIG_SECCOMP
2100 REG(PROC_TID_SECCOMP, "seccomp", S_IRUSR|S_IWUSR, seccomp),
2101 #endif
2102 LNK(PROC_TID_CWD, "cwd", cwd),
2103 LNK(PROC_TID_ROOT, "root", root),
2104 LNK(PROC_TID_EXE, "exe", exe),
2105 REG(PROC_TID_MOUNTS, "mounts", S_IRUGO, mounts),
2106 #ifdef CONFIG_MMU
2107 REG(PROC_TID_SMAPS, "smaps", S_IRUGO, smaps),
2108 #endif
2109 #ifdef CONFIG_SECURITY
2110 DIR(PROC_TID_ATTR, "attr", S_IRUGO|S_IXUGO, tid_attr),
2111 #endif
2112 #ifdef CONFIG_KALLSYMS
2113 INF(PROC_TID_WCHAN, "wchan", S_IRUGO, pid_wchan),
2114 #endif
2115 #ifdef CONFIG_SCHEDSTATS
2116 INF(PROC_TID_SCHEDSTAT, "schedstat", S_IRUGO, pid_schedstat),
2117 #endif
2118 #ifdef CONFIG_CPUSETS
2119 REG(PROC_TID_CPUSET, "cpuset", S_IRUGO, cpuset),
2120 #endif
2121 INF(PROC_TID_OOM_SCORE, "oom_score", S_IRUGO, oom_score),
2122 REG(PROC_TID_OOM_ADJUST, "oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2123 #ifdef CONFIG_AUDITSYSCALL
2124 REG(PROC_TID_LOGINUID, "loginuid", S_IWUSR|S_IRUGO, loginuid),
2125 #endif
2126 {}
2127 };
2128
2129 static int proc_tid_base_readdir(struct file * filp,
2130 void * dirent, filldir_t filldir)
2131 {
2132 return proc_pident_readdir(filp,dirent,filldir,
2133 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2134 }
2135
2136 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2137 return proc_pident_lookup(dir, dentry, tid_base_stuff);
2138 }
2139
2140 static struct file_operations proc_tid_base_operations = {
2141 .read = generic_read_dir,
2142 .readdir = proc_tid_base_readdir,
2143 };
2144
2145 static struct inode_operations proc_tid_base_inode_operations = {
2146 .lookup = proc_tid_base_lookup,
2147 .getattr = pid_getattr,
2148 .setattr = proc_setattr,
2149 };
2150
2151 static struct dentry *proc_task_instantiate(struct inode *dir,
2152 struct dentry *dentry, struct task_struct *task, void *ptr)
2153 {
2154 struct dentry *error = ERR_PTR(-ENOENT);
2155 struct inode *inode;
2156 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
2157
2158 if (!inode)
2159 goto out;
2160 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2161 inode->i_op = &proc_tid_base_inode_operations;
2162 inode->i_fop = &proc_tid_base_operations;
2163 inode->i_flags|=S_IMMUTABLE;
2164 inode->i_nlink = 3;
2165 #ifdef CONFIG_SECURITY
2166 inode->i_nlink += 1;
2167 #endif
2168
2169 dentry->d_op = &pid_dentry_operations;
2170
2171 d_add(dentry, inode);
2172 /* Close the race of the process dying before we return the dentry */
2173 if (pid_revalidate(dentry, NULL))
2174 error = NULL;
2175 out:
2176 return error;
2177 }
2178
2179 /* SMP-safe */
2180 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2181 {
2182 struct dentry *result = ERR_PTR(-ENOENT);
2183 struct task_struct *task;
2184 struct task_struct *leader = get_proc_task(dir);
2185 unsigned tid;
2186
2187 if (!leader)
2188 goto out_no_task;
2189
2190 tid = name_to_int(dentry);
2191 if (tid == ~0U)
2192 goto out;
2193
2194 rcu_read_lock();
2195 task = find_task_by_pid(tid);
2196 if (task)
2197 get_task_struct(task);
2198 rcu_read_unlock();
2199 if (!task)
2200 goto out;
2201 if (leader->tgid != task->tgid)
2202 goto out_drop_task;
2203
2204 result = proc_task_instantiate(dir, dentry, task, NULL);
2205 out_drop_task:
2206 put_task_struct(task);
2207 out:
2208 put_task_struct(leader);
2209 out_no_task:
2210 return result;
2211 }
2212
2213 /*
2214 * Find the first tid of a thread group to return to user space.
2215 *
2216 * Usually this is just the thread group leader, but if the users
2217 * buffer was too small or there was a seek into the middle of the
2218 * directory we have more work todo.
2219 *
2220 * In the case of a short read we start with find_task_by_pid.
2221 *
2222 * In the case of a seek we start with the leader and walk nr
2223 * threads past it.
2224 */
2225 static struct task_struct *first_tid(struct task_struct *leader,
2226 int tid, int nr)
2227 {
2228 struct task_struct *pos;
2229
2230 rcu_read_lock();
2231 /* Attempt to start with the pid of a thread */
2232 if (tid && (nr > 0)) {
2233 pos = find_task_by_pid(tid);
2234 if (pos && (pos->group_leader == leader))
2235 goto found;
2236 }
2237
2238 /* If nr exceeds the number of threads there is nothing todo */
2239 pos = NULL;
2240 if (nr && nr >= get_nr_threads(leader))
2241 goto out;
2242
2243 /* If we haven't found our starting place yet start
2244 * with the leader and walk nr threads forward.
2245 */
2246 for (pos = leader; nr > 0; --nr) {
2247 pos = next_thread(pos);
2248 if (pos == leader) {
2249 pos = NULL;
2250 goto out;
2251 }
2252 }
2253 found:
2254 get_task_struct(pos);
2255 out:
2256 rcu_read_unlock();
2257 return pos;
2258 }
2259
2260 /*
2261 * Find the next thread in the thread list.
2262 * Return NULL if there is an error or no next thread.
2263 *
2264 * The reference to the input task_struct is released.
2265 */
2266 static struct task_struct *next_tid(struct task_struct *start)
2267 {
2268 struct task_struct *pos = NULL;
2269 rcu_read_lock();
2270 if (pid_alive(start)) {
2271 pos = next_thread(start);
2272 if (thread_group_leader(pos))
2273 pos = NULL;
2274 else
2275 get_task_struct(pos);
2276 }
2277 rcu_read_unlock();
2278 put_task_struct(start);
2279 return pos;
2280 }
2281
2282 /* for the /proc/TGID/task/ directories */
2283 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2284 {
2285 char buf[PROC_NUMBUF];
2286 struct dentry *dentry = filp->f_dentry;
2287 struct inode *inode = dentry->d_inode;
2288 struct task_struct *leader = get_proc_task(inode);
2289 struct task_struct *task;
2290 int retval = -ENOENT;
2291 ino_t ino;
2292 int tid;
2293 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2294
2295 if (!leader)
2296 goto out_no_task;
2297 retval = 0;
2298
2299 switch (pos) {
2300 case 0:
2301 ino = inode->i_ino;
2302 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2303 goto out;
2304 pos++;
2305 /* fall through */
2306 case 1:
2307 ino = parent_ino(dentry);
2308 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2309 goto out;
2310 pos++;
2311 /* fall through */
2312 }
2313
2314 /* f_version caches the tgid value that the last readdir call couldn't
2315 * return. lseek aka telldir automagically resets f_version to 0.
2316 */
2317 tid = filp->f_version;
2318 filp->f_version = 0;
2319 for (task = first_tid(leader, tid, pos - 2);
2320 task;
2321 task = next_tid(task), pos++) {
2322 int len;
2323 tid = task->pid;
2324 len = snprintf(buf, sizeof(buf), "%d", tid);
2325 ino = fake_ino(tid, PROC_TID_INO);
2326 if (filldir(dirent, buf, len, pos, ino, DT_DIR < 0)) {
2327 /* returning this tgid failed, save it as the first
2328 * pid for the next readir call */
2329 filp->f_version = tid;
2330 put_task_struct(task);
2331 break;
2332 }
2333 }
2334 out:
2335 filp->f_pos = pos;
2336 put_task_struct(leader);
2337 out_no_task:
2338 return retval;
2339 }
2340
2341 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2342 {
2343 struct inode *inode = dentry->d_inode;
2344 struct task_struct *p = get_proc_task(inode);
2345 generic_fillattr(inode, stat);
2346
2347 if (p) {
2348 rcu_read_lock();
2349 stat->nlink += get_nr_threads(p);
2350 rcu_read_unlock();
2351 put_task_struct(p);
2352 }
2353
2354 return 0;
2355 }
2356
2357 static struct inode_operations proc_task_inode_operations = {
2358 .lookup = proc_task_lookup,
2359 .getattr = proc_task_getattr,
2360 .setattr = proc_setattr,
2361 };
2362
2363 static struct file_operations proc_task_operations = {
2364 .read = generic_read_dir,
2365 .readdir = proc_task_readdir,
2366 };