Blame view

fs/proc/thread_self.c 1.85 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>
0097875bd   Eric W. Biederman   proc: Implement /...
3
  #include <linux/sched.h>
0097875bd   Eric W. Biederman   proc: Implement /...
4
5
6
7
8
9
10
  #include <linux/slab.h>
  #include <linux/pid_namespace.h>
  #include "internal.h"
  
  /*
   * /proc/thread_self:
   */
6b2553918   Al Viro   replace ->follow_...
11
  static const char *proc_thread_self_get_link(struct dentry *dentry,
fceef393a   Al Viro   switch ->get_link...
12
13
  					     struct inode *inode,
  					     struct delayed_call *done)
0097875bd   Eric W. Biederman   proc: Implement /...
14
  {
9d78edeae   Alexey Gladkov   proc: proc_pid_ns...
15
  	struct pid_namespace *ns = proc_pid_ns(inode->i_sb);
0097875bd   Eric W. Biederman   proc: Implement /...
16
17
  	pid_t tgid = task_tgid_nr_ns(current, ns);
  	pid_t pid = task_pid_nr_ns(current, ns);
680baacbc   Al Viro   new ->follow_link...
18
19
20
21
  	char *name;
  
  	if (!pid)
  		return ERR_PTR(-ENOENT);
e3912ac37   Alexey Dobriyan   proc: use %u for ...
22
  	name = kmalloc(10 + 6 + 10 + 1, dentry ? GFP_KERNEL : GFP_ATOMIC);
1a384eaac   Al Viro   teach proc_self_g...
23
24
  	if (unlikely(!name))
  		return dentry ? ERR_PTR(-ENOMEM) : ERR_PTR(-ECHILD);
e3912ac37   Alexey Dobriyan   proc: use %u for ...
25
  	sprintf(name, "%u/task/%u", tgid, pid);
fceef393a   Al Viro   switch ->get_link...
26
27
  	set_delayed_call(done, kfree_link, name);
  	return name;
0097875bd   Eric W. Biederman   proc: Implement /...
28
29
30
  }
  
  static const struct inode_operations proc_thread_self_inode_operations = {
6b2553918   Al Viro   replace ->follow_...
31
  	.get_link	= proc_thread_self_get_link,
0097875bd   Eric W. Biederman   proc: Implement /...
32
  };
efb1a57d9   Alexey Dobriyan   fs/proc: use __ro...
33
  static unsigned thread_self_inum __ro_after_init;
0097875bd   Eric W. Biederman   proc: Implement /...
34
35
36
  
  int proc_setup_thread_self(struct super_block *s)
  {
2b0143b5c   David Howells   VFS: normal files...
37
  	struct inode *root_inode = d_inode(s->s_root);
fa10fed30   Alexey Gladkov   proc: allow to mo...
38
  	struct proc_fs_info *fs_info = proc_sb_info(s);
0097875bd   Eric W. Biederman   proc: Implement /...
39
  	struct dentry *thread_self;
45f68ab50   Chengguang Xu   fs/proc/thread_se...
40
  	int ret = -ENOMEM;
0097875bd   Eric W. Biederman   proc: Implement /...
41

5955102c9   Al Viro   wrappers for ->i_...
42
  	inode_lock(root_inode);
0097875bd   Eric W. Biederman   proc: Implement /...
43
44
  	thread_self = d_alloc_name(s->s_root, "thread-self");
  	if (thread_self) {
ef1548ada   Eric W. Biederman   proc: Use new_ino...
45
  		struct inode *inode = new_inode(s);
0097875bd   Eric W. Biederman   proc: Implement /...
46
47
  		if (inode) {
  			inode->i_ino = thread_self_inum;
078cd8279   Deepa Dinamani   fs: Replace CURRE...
48
  			inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
0097875bd   Eric W. Biederman   proc: Implement /...
49
50
51
52
53
  			inode->i_mode = S_IFLNK | S_IRWXUGO;
  			inode->i_uid = GLOBAL_ROOT_UID;
  			inode->i_gid = GLOBAL_ROOT_GID;
  			inode->i_op = &proc_thread_self_inode_operations;
  			d_add(thread_self, inode);
45f68ab50   Chengguang Xu   fs/proc/thread_se...
54
  			ret = 0;
0097875bd   Eric W. Biederman   proc: Implement /...
55
56
  		} else {
  			dput(thread_self);
0097875bd   Eric W. Biederman   proc: Implement /...
57
  		}
0097875bd   Eric W. Biederman   proc: Implement /...
58
  	}
5955102c9   Al Viro   wrappers for ->i_...
59
  	inode_unlock(root_inode);
45f68ab50   Chengguang Xu   fs/proc/thread_se...
60
61
  
  	if (ret)
fa10fed30   Alexey Gladkov   proc: allow to mo...
62
63
  		pr_err("proc_fill_super: can't allocate /proc/thread-self
  ");
45f68ab50   Chengguang Xu   fs/proc/thread_se...
64
  	else
fa10fed30   Alexey Gladkov   proc: allow to mo...
65
  		fs_info->proc_thread_self = thread_self;
45f68ab50   Chengguang Xu   fs/proc/thread_se...
66
67
  
  	return ret;
0097875bd   Eric W. Biederman   proc: Implement /...
68
69
70
71
72
73
  }
  
  void __init proc_thread_self_init(void)
  {
  	proc_alloc_inum(&thread_self_inum);
  }