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