|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +#include <linux/file.h> |
| 3 | +#include <linux/fs.h> |
| 4 | +#include <linux/magic.h> |
| 5 | +#include <linux/mount.h> |
| 6 | +#include <linux/pid.h> |
| 7 | +#include <linux/pid_namespace.h> |
| 8 | +#include <linux/poll.h> |
| 9 | +#include <linux/proc_fs.h> |
| 10 | +#include <linux/proc_ns.h> |
| 11 | +#include <linux/pseudo_fs.h> |
| 12 | +#include <linux/seq_file.h> |
| 13 | +#include <uapi/linux/pidfd.h> |
| 14 | + |
| 15 | +static int pidfd_release(struct inode *inode, struct file *file) |
| 16 | +{ |
| 17 | + struct pid *pid = file->private_data; |
| 18 | + |
| 19 | + file->private_data = NULL; |
| 20 | + put_pid(pid); |
| 21 | + return 0; |
| 22 | +} |
| 23 | + |
| 24 | +#ifdef CONFIG_PROC_FS |
| 25 | +/** |
| 26 | + * pidfd_show_fdinfo - print information about a pidfd |
| 27 | + * @m: proc fdinfo file |
| 28 | + * @f: file referencing a pidfd |
| 29 | + * |
| 30 | + * Pid: |
| 31 | + * This function will print the pid that a given pidfd refers to in the |
| 32 | + * pid namespace of the procfs instance. |
| 33 | + * If the pid namespace of the process is not a descendant of the pid |
| 34 | + * namespace of the procfs instance 0 will be shown as its pid. This is |
| 35 | + * similar to calling getppid() on a process whose parent is outside of |
| 36 | + * its pid namespace. |
| 37 | + * |
| 38 | + * NSpid: |
| 39 | + * If pid namespaces are supported then this function will also print |
| 40 | + * the pid of a given pidfd refers to for all descendant pid namespaces |
| 41 | + * starting from the current pid namespace of the instance, i.e. the |
| 42 | + * Pid field and the first entry in the NSpid field will be identical. |
| 43 | + * If the pid namespace of the process is not a descendant of the pid |
| 44 | + * namespace of the procfs instance 0 will be shown as its first NSpid |
| 45 | + * entry and no others will be shown. |
| 46 | + * Note that this differs from the Pid and NSpid fields in |
| 47 | + * /proc/<pid>/status where Pid and NSpid are always shown relative to |
| 48 | + * the pid namespace of the procfs instance. The difference becomes |
| 49 | + * obvious when sending around a pidfd between pid namespaces from a |
| 50 | + * different branch of the tree, i.e. where no ancestral relation is |
| 51 | + * present between the pid namespaces: |
| 52 | + * - create two new pid namespaces ns1 and ns2 in the initial pid |
| 53 | + * namespace (also take care to create new mount namespaces in the |
| 54 | + * new pid namespace and mount procfs) |
| 55 | + * - create a process with a pidfd in ns1 |
| 56 | + * - send pidfd from ns1 to ns2 |
| 57 | + * - read /proc/self/fdinfo/<pidfd> and observe that both Pid and NSpid |
| 58 | + * have exactly one entry, which is 0 |
| 59 | + */ |
| 60 | +static void pidfd_show_fdinfo(struct seq_file *m, struct file *f) |
| 61 | +{ |
| 62 | + struct pid *pid = f->private_data; |
| 63 | + struct pid_namespace *ns; |
| 64 | + pid_t nr = -1; |
| 65 | + |
| 66 | + if (likely(pid_has_task(pid, PIDTYPE_PID))) { |
| 67 | + ns = proc_pid_ns(file_inode(m->file)->i_sb); |
| 68 | + nr = pid_nr_ns(pid, ns); |
| 69 | + } |
| 70 | + |
| 71 | + seq_put_decimal_ll(m, "Pid:\t", nr); |
| 72 | + |
| 73 | +#ifdef CONFIG_PID_NS |
| 74 | + seq_put_decimal_ll(m, "\nNSpid:\t", nr); |
| 75 | + if (nr > 0) { |
| 76 | + int i; |
| 77 | + |
| 78 | + /* If nr is non-zero it means that 'pid' is valid and that |
| 79 | + * ns, i.e. the pid namespace associated with the procfs |
| 80 | + * instance, is in the pid namespace hierarchy of pid. |
| 81 | + * Start at one below the already printed level. |
| 82 | + */ |
| 83 | + for (i = ns->level + 1; i <= pid->level; i++) |
| 84 | + seq_put_decimal_ll(m, "\t", pid->numbers[i].nr); |
| 85 | + } |
| 86 | +#endif |
| 87 | + seq_putc(m, '\n'); |
| 88 | +} |
| 89 | +#endif |
| 90 | + |
| 91 | +/* |
| 92 | + * Poll support for process exit notification. |
| 93 | + */ |
| 94 | +static __poll_t pidfd_poll(struct file *file, struct poll_table_struct *pts) |
| 95 | +{ |
| 96 | + struct pid *pid = file->private_data; |
| 97 | + bool thread = file->f_flags & PIDFD_THREAD; |
| 98 | + struct task_struct *task; |
| 99 | + __poll_t poll_flags = 0; |
| 100 | + |
| 101 | + poll_wait(file, &pid->wait_pidfd, pts); |
| 102 | + /* |
| 103 | + * Depending on PIDFD_THREAD, inform pollers when the thread |
| 104 | + * or the whole thread-group exits. |
| 105 | + */ |
| 106 | + guard(rcu)(); |
| 107 | + task = pid_task(pid, PIDTYPE_PID); |
| 108 | + if (!task) |
| 109 | + poll_flags = EPOLLIN | EPOLLRDNORM | EPOLLHUP; |
| 110 | + else if (task->exit_state && (thread || thread_group_empty(task))) |
| 111 | + poll_flags = EPOLLIN | EPOLLRDNORM; |
| 112 | + |
| 113 | + return poll_flags; |
| 114 | +} |
| 115 | + |
| 116 | +const struct file_operations pidfd_fops = { |
| 117 | + .release = pidfd_release, |
| 118 | + .poll = pidfd_poll, |
| 119 | +#ifdef CONFIG_PROC_FS |
| 120 | + .show_fdinfo = pidfd_show_fdinfo, |
| 121 | +#endif |
| 122 | +}; |
0 commit comments