1 #include <linux/proc_fs.h>
2 #include <linux/sched.h>
3 #include <linux/namei.h>
8 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
11 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
12 pid_t tgid = task_tgid_nr_ns(current, ns);
13 char tmp[PROC_NUMBUF];
16 sprintf(tmp, "%d", tgid);
17 return vfs_readlink(dentry,buffer,buflen,tmp);
20 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
22 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
23 pid_t tgid = task_tgid_nr_ns(current, ns);
24 char *name = ERR_PTR(-ENOENT);
26 /* 11 for max length of signed int in decimal + NULL term */
27 name = kmalloc(12, GFP_KERNEL);
29 name = ERR_PTR(-ENOMEM);
31 sprintf(name, "%d", tgid);
33 nd_set_link(nd, name);
37 static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
40 char *s = nd_get_link(nd);
45 static const struct inode_operations proc_self_inode_operations = {
46 .readlink = proc_self_readlink,
47 .follow_link = proc_self_follow_link,
48 .put_link = proc_self_put_link,
51 void __init proc_self_init(void)
53 struct proc_dir_entry *proc_self_symlink;
56 mode = S_IFLNK | S_IRWXUGO;
57 proc_self_symlink = proc_create("self", mode, NULL, NULL );
58 proc_self_symlink->proc_iops = &proc_self_inode_operations;