Advertisement
Guest User

Untitled

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