xref: /linux/fs/proc/internal.h (revision fddda2b7b521185f3aa018f9559eb33b0aee53a9)
1 /* Internal procfs definitions
2  *
3  * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11 
12 #include <linux/proc_fs.h>
13 #include <linux/proc_ns.h>
14 #include <linux/refcount.h>
15 #include <linux/spinlock.h>
16 #include <linux/atomic.h>
17 #include <linux/binfmts.h>
18 #include <linux/sched/coredump.h>
19 #include <linux/sched/task.h>
20 
21 struct ctl_table_header;
22 struct mempolicy;
23 
24 /*
25  * This is not completely implemented yet. The idea is to
26  * create an in-memory tree (like the actual /proc filesystem
27  * tree) of these proc_dir_entries, so that we can dynamically
28  * add new files to /proc.
29  *
30  * parent/subdir are used for the directory structure (every /proc file has a
31  * parent, but "subdir" is empty for all non-directory entries).
32  * subdir_node is used to build the rb tree "subdir" of the parent.
33  */
34 struct proc_dir_entry {
35 	/*
36 	 * number of callers into module in progress;
37 	 * negative -> it's going away RSN
38 	 */
39 	atomic_t in_use;
40 	refcount_t refcnt;
41 	struct list_head pde_openers;	/* who did ->open, but not ->release */
42 	/* protects ->pde_openers and all struct pde_opener instances */
43 	spinlock_t pde_unload_lock;
44 	struct completion *pde_unload_completion;
45 	const struct inode_operations *proc_iops;
46 	const struct file_operations *proc_fops;
47 	const struct seq_operations *seq_ops;
48 	void *data;
49 	unsigned int low_ino;
50 	nlink_t nlink;
51 	kuid_t uid;
52 	kgid_t gid;
53 	loff_t size;
54 	struct proc_dir_entry *parent;
55 	struct rb_root subdir;
56 	struct rb_node subdir_node;
57 	char *name;
58 	umode_t mode;
59 	u8 namelen;
60 #ifdef CONFIG_64BIT
61 #define SIZEOF_PDE_INLINE_NAME	(192-139)
62 #else
63 #define SIZEOF_PDE_INLINE_NAME	(128-87)
64 #endif
65 	char inline_name[SIZEOF_PDE_INLINE_NAME];
66 } __randomize_layout;
67 
68 extern struct kmem_cache *proc_dir_entry_cache;
69 void pde_free(struct proc_dir_entry *pde);
70 
71 union proc_op {
72 	int (*proc_get_link)(struct dentry *, struct path *);
73 	int (*proc_show)(struct seq_file *m,
74 		struct pid_namespace *ns, struct pid *pid,
75 		struct task_struct *task);
76 };
77 
78 struct proc_inode {
79 	struct pid *pid;
80 	unsigned int fd;
81 	union proc_op op;
82 	struct proc_dir_entry *pde;
83 	struct ctl_table_header *sysctl;
84 	struct ctl_table *sysctl_entry;
85 	struct hlist_node sysctl_inodes;
86 	const struct proc_ns_operations *ns_ops;
87 	struct inode vfs_inode;
88 } __randomize_layout;
89 
90 /*
91  * General functions
92  */
93 static inline struct proc_inode *PROC_I(const struct inode *inode)
94 {
95 	return container_of(inode, struct proc_inode, vfs_inode);
96 }
97 
98 static inline struct proc_dir_entry *PDE(const struct inode *inode)
99 {
100 	return PROC_I(inode)->pde;
101 }
102 
103 static inline void *__PDE_DATA(const struct inode *inode)
104 {
105 	return PDE(inode)->data;
106 }
107 
108 static inline struct pid *proc_pid(struct inode *inode)
109 {
110 	return PROC_I(inode)->pid;
111 }
112 
113 static inline struct task_struct *get_proc_task(struct inode *inode)
114 {
115 	return get_pid_task(proc_pid(inode), PIDTYPE_PID);
116 }
117 
118 void task_dump_owner(struct task_struct *task, umode_t mode,
119 		     kuid_t *ruid, kgid_t *rgid);
120 
121 unsigned name_to_int(const struct qstr *qstr);
122 /*
123  * Offset of the first process in the /proc root directory..
124  */
125 #define FIRST_PROCESS_ENTRY 256
126 
127 /* Worst case buffer size needed for holding an integer. */
128 #define PROC_NUMBUF 13
129 
130 /*
131  * array.c
132  */
133 extern const struct file_operations proc_tid_children_operations;
134 
135 extern int proc_tid_stat(struct seq_file *, struct pid_namespace *,
136 			 struct pid *, struct task_struct *);
137 extern int proc_tgid_stat(struct seq_file *, struct pid_namespace *,
138 			  struct pid *, struct task_struct *);
139 extern int proc_pid_status(struct seq_file *, struct pid_namespace *,
140 			   struct pid *, struct task_struct *);
141 extern int proc_pid_statm(struct seq_file *, struct pid_namespace *,
142 			  struct pid *, struct task_struct *);
143 
144 /*
145  * base.c
146  */
147 extern const struct dentry_operations pid_dentry_operations;
148 extern int pid_getattr(const struct path *, struct kstat *, u32, unsigned int);
149 extern int proc_setattr(struct dentry *, struct iattr *);
150 extern struct inode *proc_pid_make_inode(struct super_block *, struct task_struct *, umode_t);
151 extern int pid_revalidate(struct dentry *, unsigned int);
152 extern int pid_delete_dentry(const struct dentry *);
153 extern int proc_pid_readdir(struct file *, struct dir_context *);
154 extern struct dentry *proc_pid_lookup(struct inode *, struct dentry *, unsigned int);
155 extern loff_t mem_lseek(struct file *, loff_t, int);
156 
157 /* Lookups */
158 typedef int instantiate_t(struct inode *, struct dentry *,
159 				     struct task_struct *, const void *);
160 extern bool proc_fill_cache(struct file *, struct dir_context *, const char *, int,
161 			   instantiate_t, struct task_struct *, const void *);
162 
163 /*
164  * generic.c
165  */
166 struct proc_dir_entry *proc_create_reg(const char *name, umode_t mode,
167 		struct proc_dir_entry **parent, void *data);
168 struct proc_dir_entry *proc_register(struct proc_dir_entry *dir,
169 		struct proc_dir_entry *dp);
170 extern struct dentry *proc_lookup(struct inode *, struct dentry *, unsigned int);
171 struct dentry *proc_lookup_de(struct inode *, struct dentry *, struct proc_dir_entry *);
172 extern int proc_readdir(struct file *, struct dir_context *);
173 int proc_readdir_de(struct file *, struct dir_context *, struct proc_dir_entry *);
174 
175 static inline struct proc_dir_entry *pde_get(struct proc_dir_entry *pde)
176 {
177 	refcount_inc(&pde->refcnt);
178 	return pde;
179 }
180 extern void pde_put(struct proc_dir_entry *);
181 
182 static inline bool is_empty_pde(const struct proc_dir_entry *pde)
183 {
184 	return S_ISDIR(pde->mode) && !pde->proc_iops;
185 }
186 
187 /*
188  * inode.c
189  */
190 struct pde_opener {
191 	struct file *file;
192 	struct list_head lh;
193 	bool closing;
194 	struct completion *c;
195 } __randomize_layout;
196 extern const struct inode_operations proc_link_inode_operations;
197 
198 extern const struct inode_operations proc_pid_link_inode_operations;
199 
200 void proc_init_kmemcache(void);
201 void set_proc_pid_nlink(void);
202 extern struct inode *proc_get_inode(struct super_block *, struct proc_dir_entry *);
203 extern int proc_fill_super(struct super_block *, void *data, int flags);
204 extern void proc_entry_rundown(struct proc_dir_entry *);
205 
206 /*
207  * proc_namespaces.c
208  */
209 extern const struct inode_operations proc_ns_dir_inode_operations;
210 extern const struct file_operations proc_ns_dir_operations;
211 
212 /*
213  * proc_net.c
214  */
215 extern const struct file_operations proc_net_operations;
216 extern const struct inode_operations proc_net_inode_operations;
217 
218 #ifdef CONFIG_NET
219 extern int proc_net_init(void);
220 #else
221 static inline int proc_net_init(void) { return 0; }
222 #endif
223 
224 /*
225  * proc_self.c
226  */
227 extern int proc_setup_self(struct super_block *);
228 
229 /*
230  * proc_thread_self.c
231  */
232 extern int proc_setup_thread_self(struct super_block *);
233 extern void proc_thread_self_init(void);
234 
235 /*
236  * proc_sysctl.c
237  */
238 #ifdef CONFIG_PROC_SYSCTL
239 extern int proc_sys_init(void);
240 extern void proc_sys_evict_inode(struct inode *inode,
241 				 struct ctl_table_header *head);
242 #else
243 static inline void proc_sys_init(void) { }
244 static inline void proc_sys_evict_inode(struct  inode *inode,
245 					struct ctl_table_header *head) { }
246 #endif
247 
248 /*
249  * proc_tty.c
250  */
251 #ifdef CONFIG_TTY
252 extern void proc_tty_init(void);
253 #else
254 static inline void proc_tty_init(void) {}
255 #endif
256 
257 /*
258  * root.c
259  */
260 extern struct proc_dir_entry proc_root;
261 extern int proc_parse_options(char *options, struct pid_namespace *pid);
262 
263 extern void proc_self_init(void);
264 extern int proc_remount(struct super_block *, int *, char *);
265 
266 /*
267  * task_[no]mmu.c
268  */
269 struct mem_size_stats;
270 struct proc_maps_private {
271 	struct inode *inode;
272 	struct task_struct *task;
273 	struct mm_struct *mm;
274 	struct mem_size_stats *rollup;
275 #ifdef CONFIG_MMU
276 	struct vm_area_struct *tail_vma;
277 #endif
278 #ifdef CONFIG_NUMA
279 	struct mempolicy *task_mempolicy;
280 #endif
281 } __randomize_layout;
282 
283 struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode);
284 
285 extern const struct file_operations proc_pid_maps_operations;
286 extern const struct file_operations proc_tid_maps_operations;
287 extern const struct file_operations proc_pid_numa_maps_operations;
288 extern const struct file_operations proc_tid_numa_maps_operations;
289 extern const struct file_operations proc_pid_smaps_operations;
290 extern const struct file_operations proc_pid_smaps_rollup_operations;
291 extern const struct file_operations proc_tid_smaps_operations;
292 extern const struct file_operations proc_clear_refs_operations;
293 extern const struct file_operations proc_pagemap_operations;
294 
295 extern unsigned long task_vsize(struct mm_struct *);
296 extern unsigned long task_statm(struct mm_struct *,
297 				unsigned long *, unsigned long *,
298 				unsigned long *, unsigned long *);
299 extern void task_mem(struct seq_file *, struct mm_struct *);
300