From: Chen Linxuan <chenlinxuan@xxxxxxxxxxxxx> In Linux, an fd (file descriptor) is a non-negative integer representing an open file or other I/O resource associated with a process. These resources are located on file systems accessed via mount points. A mount ID (mnt_id) is a unique identifier for a specific instance of a mounted file system within a mount namespace, essential for understanding the file's context, especially in complex or containerized environments. The executable (exe), pointed to by /proc/<pid>/exe, is the process's binary file, which also resides on a file system. Knowing the mount ID for both file descriptors and the executable is valuable for debugging and understanding a process's resource origins. We can easily obtain the mnt_id for an open fd by reading /proc/<pid>/fdinfo/<fd}, where it's explicitly listed. However, there isn't a direct interface (like a specific field in /proc/<pid}/ status or a dedicated exeinfo file) to easily get the mount ID of the executable file without performing additional path resolution or file operations. Signed-off-by: Chen Linxuan <chenlinxuan@xxxxxxxxxxxxx> Signed-off-by: WangYuli <wangyuli@xxxxxxxxxxxxx> --- fs/proc/base.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/fs/proc/base.c b/fs/proc/base.c index b0d4e1908b22..fe8a2d5b3bc1 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -101,6 +101,7 @@ #include <trace/events/oom.h> #include "internal.h" #include "fd.h" +#include "../mount.h" #include "../../lib/kstrtox.h" @@ -1790,6 +1791,28 @@ static int proc_exe_link(struct dentry *dentry, struct path *exe_path) return -ENOENT; } +static int proc_exe_mntid(struct seq_file *m, struct pid_namespace *ns, + struct pid *pid, struct task_struct *task) +{ + struct file *exe_file; + struct path exe_path; + + exe_file = get_task_exe_file(task); + + if (exe_file) { + exe_path = exe_file->f_path; + path_get(&exe_file->f_path); + + seq_printf(m, "%i\n", real_mount(exe_path.mnt)->mnt_id); + + path_put(&exe_file->f_path); + fput(exe_file); + + return 0; + } else + return -ENOENT; +} + static const char *proc_pid_get_link(struct dentry *dentry, struct inode *inode, struct delayed_call *done) @@ -3342,6 +3365,7 @@ static const struct pid_entry tgid_base_stuff[] = { LNK("cwd", proc_cwd_link), LNK("root", proc_root_link), LNK("exe", proc_exe_link), + ONE("exe_mntid", S_IRUGO, proc_exe_mntid), REG("mounts", S_IRUGO, proc_mounts_operations), REG("mountinfo", S_IRUGO, proc_mountinfo_operations), REG("mountstats", S_IRUSR, proc_mountstats_operations), -- 2.49.0