Blame view

fs/proc/self.c 1.88 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
efb1a57d9   Alexey Dobriyan   fs/proc: use __ro...
2
  #include <linux/cache.h>
e656d8a6f   Eric W. Biederman   procfs: Use the p...
3
  #include <linux/sched.h>
0d01ff258   David Howells   Include missing l...
4
  #include <linux/slab.h>
021ada7df   Al Viro   procfs: switch /p...
5
6
  #include <linux/pid_namespace.h>
  #include "internal.h"
e656d8a6f   Eric W. Biederman   procfs: Use the p...
7
8
9
10
  
  /*
   * /proc/self:
   */
6b2553918   Al Viro   replace ->follow_...
11
  static const char *proc_self_get_link(struct dentry *dentry,
fceef393a   Al Viro   switch ->get_link...
12
13
  				      struct inode *inode,
  				      struct delayed_call *done)
e656d8a6f   Eric W. Biederman   procfs: Use the p...
14
  {
9d78edeae   Alexey Gladkov   proc: proc_pid_ns...
15
  	struct pid_namespace *ns = proc_pid_ns(inode->i_sb);
e656d8a6f   Eric W. Biederman   procfs: Use the p...
16
  	pid_t tgid = task_tgid_nr_ns(current, ns);
680baacbc   Al Viro   new ->follow_link...
17
  	char *name;
8d4c3e76e   Jens Axboe   proc: don't allow...
18
19
20
21
22
23
  	/*
  	 * Not currently supported. Once we can inherit all of struct pid,
  	 * we can allow this.
  	 */
  	if (current->flags & PF_KTHREAD)
  		return ERR_PTR(-EOPNOTSUPP);
680baacbc   Al Viro   new ->follow_link...
24
25
  	if (!tgid)
  		return ERR_PTR(-ENOENT);
e3912ac37   Alexey Dobriyan   proc: use %u for ...
26
27
  	/* max length of unsigned int in decimal + NULL term */
  	name = kmalloc(10 + 1, dentry ? GFP_KERNEL : GFP_ATOMIC);
1a384eaac   Al Viro   teach proc_self_g...
28
29
  	if (unlikely(!name))
  		return dentry ? ERR_PTR(-ENOMEM) : ERR_PTR(-ECHILD);
e3912ac37   Alexey Dobriyan   proc: use %u for ...
30
  	sprintf(name, "%u", tgid);
fceef393a   Al Viro   switch ->get_link...
31
32
  	set_delayed_call(done, kfree_link, name);
  	return name;
e656d8a6f   Eric W. Biederman   procfs: Use the p...
33
  }
e656d8a6f   Eric W. Biederman   procfs: Use the p...
34
  static const struct inode_operations proc_self_inode_operations = {
6b2553918   Al Viro   replace ->follow_...
35
  	.get_link	= proc_self_get_link,
e656d8a6f   Eric W. Biederman   procfs: Use the p...
36
  };
efb1a57d9   Alexey Dobriyan   fs/proc: use __ro...
37
  static unsigned self_inum __ro_after_init;
021ada7df   Al Viro   procfs: switch /p...
38
39
  
  int proc_setup_self(struct super_block *s)
e656d8a6f   Eric W. Biederman   procfs: Use the p...
40
  {
2b0143b5c   David Howells   VFS: normal files...
41
  	struct inode *root_inode = d_inode(s->s_root);
fa10fed30   Alexey Gladkov   proc: allow to mo...
42
  	struct proc_fs_info *fs_info = proc_sb_info(s);
021ada7df   Al Viro   procfs: switch /p...
43
  	struct dentry *self;
756ca74c7   Chengguang Xu   fs/proc/self.c: c...
44
  	int ret = -ENOMEM;
fa10fed30   Alexey Gladkov   proc: allow to mo...
45

5955102c9   Al Viro   wrappers for ->i_...
46
  	inode_lock(root_inode);
021ada7df   Al Viro   procfs: switch /p...
47
48
  	self = d_alloc_name(s->s_root, "self");
  	if (self) {
ef1548ada   Eric W. Biederman   proc: Use new_ino...
49
  		struct inode *inode = new_inode(s);
021ada7df   Al Viro   procfs: switch /p...
50
51
  		if (inode) {
  			inode->i_ino = self_inum;
078cd8279   Deepa Dinamani   fs: Replace CURRE...
52
  			inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
021ada7df   Al Viro   procfs: switch /p...
53
54
55
56
57
  			inode->i_mode = S_IFLNK | S_IRWXUGO;
  			inode->i_uid = GLOBAL_ROOT_UID;
  			inode->i_gid = GLOBAL_ROOT_GID;
  			inode->i_op = &proc_self_inode_operations;
  			d_add(self, inode);
756ca74c7   Chengguang Xu   fs/proc/self.c: c...
58
  			ret = 0;
021ada7df   Al Viro   procfs: switch /p...
59
60
  		} else {
  			dput(self);
021ada7df   Al Viro   procfs: switch /p...
61
  		}
021ada7df   Al Viro   procfs: switch /p...
62
  	}
5955102c9   Al Viro   wrappers for ->i_...
63
  	inode_unlock(root_inode);
756ca74c7   Chengguang Xu   fs/proc/self.c: c...
64
65
  
  	if (ret)
021ada7df   Al Viro   procfs: switch /p...
66
67
  		pr_err("proc_fill_super: can't allocate /proc/self
  ");
756ca74c7   Chengguang Xu   fs/proc/self.c: c...
68
  	else
fa10fed30   Alexey Gladkov   proc: allow to mo...
69
  		fs_info->proc_self = self;
756ca74c7   Chengguang Xu   fs/proc/self.c: c...
70
71
  
  	return ret;
021ada7df   Al Viro   procfs: switch /p...
72
  }
e656d8a6f   Eric W. Biederman   procfs: Use the p...
73

021ada7df   Al Viro   procfs: switch /p...
74
75
76
  void __init proc_self_init(void)
  {
  	proc_alloc_inum(&self_inum);
e656d8a6f   Eric W. Biederman   procfs: Use the p...
77
  }