1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * The proc filesystem constants/structures
4  */
5 #ifndef _LINUX_PROC_FS_H
6 #define _LINUX_PROC_FS_H
7 
8 #include <linux/compiler.h>
9 #include <linux/types.h>
10 #include <linux/fs.h>
11 
12 struct proc_dir_entry;
13 struct seq_file;
14 struct seq_operations;
15 
16 enum {
17 	/*
18 	 * All /proc entries using this ->proc_ops instance are never removed.
19 	 *
20 	 * If in doubt, ignore this flag.
21 	 */
22 #ifdef MODULE
23 	PROC_ENTRY_PERMANENT = 0U,
24 #else
25 	PROC_ENTRY_PERMANENT = 1U << 0,
26 #endif
27 };
28 
29 struct proc_ops {
30 	unsigned int proc_flags;
31 	int	(*proc_open)(struct inode *, struct file *);
32 	ssize_t	(*proc_read)(struct file *, char __user *, size_t, loff_t *);
33 	ssize_t (*proc_read_iter)(struct kiocb *, struct iov_iter *);
34 	ssize_t	(*proc_write)(struct file *, const char __user *, size_t, loff_t *);
35 	loff_t	(*proc_lseek)(struct file *, loff_t, int);
36 	int	(*proc_release)(struct inode *, struct file *);
37 	__poll_t (*proc_poll)(struct file *, struct poll_table_struct *);
38 	long	(*proc_ioctl)(struct file *, unsigned int, unsigned long);
39 #ifdef CONFIG_COMPAT
40 	long	(*proc_compat_ioctl)(struct file *, unsigned int, unsigned long);
41 #endif
42 	int	(*proc_mmap)(struct file *, struct vm_area_struct *);
43 	unsigned long (*proc_get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
44 } __randomize_layout;
45 
46 /* definitions for hide_pid field */
47 enum proc_hidepid {
48 	HIDEPID_OFF	  = 0,
49 	HIDEPID_NO_ACCESS = 1,
50 	HIDEPID_INVISIBLE = 2,
51 	HIDEPID_NOT_PTRACEABLE = 4, /* Limit pids to only ptraceable pids */
52 };
53 
54 /* definitions for proc mount option pidonly */
55 enum proc_pidonly {
56 	PROC_PIDONLY_OFF = 0,
57 	PROC_PIDONLY_ON  = 1,
58 };
59 
60 struct proc_fs_info {
61 	struct pid_namespace *pid_ns;
62 	struct dentry *proc_self;        /* For /proc/self */
63 	struct dentry *proc_thread_self; /* For /proc/thread-self */
64 	kgid_t pid_gid;
65 	enum proc_hidepid hide_pid;
66 	enum proc_pidonly pidonly;
67 };
68 
proc_sb_info(struct super_block * sb)69 static inline struct proc_fs_info *proc_sb_info(struct super_block *sb)
70 {
71 	return sb->s_fs_info;
72 }
73 
74 #ifdef CONFIG_PROC_FS
75 
76 typedef int (*proc_write_t)(struct file *, char *, size_t);
77 
78 extern void proc_root_init(void);
79 extern void proc_flush_pid(struct pid *);
80 
81 extern struct proc_dir_entry *proc_symlink(const char *,
82 		struct proc_dir_entry *, const char *);
83 extern struct proc_dir_entry *proc_mkdir(const char *, struct proc_dir_entry *);
84 extern struct proc_dir_entry *proc_mkdir_data(const char *, umode_t,
85 					      struct proc_dir_entry *, void *);
86 extern struct proc_dir_entry *proc_mkdir_mode(const char *, umode_t,
87 					      struct proc_dir_entry *);
88 struct proc_dir_entry *proc_create_mount_point(const char *name);
89 
90 struct proc_dir_entry *proc_create_seq_private(const char *name, umode_t mode,
91 		struct proc_dir_entry *parent, const struct seq_operations *ops,
92 		unsigned int state_size, void *data);
93 #define proc_create_seq_data(name, mode, parent, ops, data) \
94 	proc_create_seq_private(name, mode, parent, ops, 0, data)
95 #define proc_create_seq(name, mode, parent, ops) \
96 	proc_create_seq_private(name, mode, parent, ops, 0, NULL)
97 struct proc_dir_entry *proc_create_single_data(const char *name, umode_t mode,
98 		struct proc_dir_entry *parent,
99 		int (*show)(struct seq_file *, void *), void *data);
100 #define proc_create_single(name, mode, parent, show) \
101 	proc_create_single_data(name, mode, parent, show, NULL)
102 
103 extern struct proc_dir_entry *proc_create_data(const char *, umode_t,
104 					       struct proc_dir_entry *,
105 					       const struct proc_ops *,
106 					       void *);
107 
108 struct proc_dir_entry *proc_create(const char *name, umode_t mode, struct proc_dir_entry *parent, const struct proc_ops *proc_ops);
109 extern void proc_set_size(struct proc_dir_entry *, loff_t);
110 extern void proc_set_user(struct proc_dir_entry *, kuid_t, kgid_t);
111 extern void *PDE_DATA(const struct inode *);
112 extern void *proc_get_parent_data(const struct inode *);
113 extern void proc_remove(struct proc_dir_entry *);
114 extern void remove_proc_entry(const char *, struct proc_dir_entry *);
115 extern int remove_proc_subtree(const char *, struct proc_dir_entry *);
116 
117 struct proc_dir_entry *proc_create_net_data(const char *name, umode_t mode,
118 		struct proc_dir_entry *parent, const struct seq_operations *ops,
119 		unsigned int state_size, void *data);
120 #define proc_create_net(name, mode, parent, ops, state_size) \
121 	proc_create_net_data(name, mode, parent, ops, state_size, NULL)
122 struct proc_dir_entry *proc_create_net_single(const char *name, umode_t mode,
123 		struct proc_dir_entry *parent,
124 		int (*show)(struct seq_file *, void *), void *data);
125 struct proc_dir_entry *proc_create_net_data_write(const char *name, umode_t mode,
126 						  struct proc_dir_entry *parent,
127 						  const struct seq_operations *ops,
128 						  proc_write_t write,
129 						  unsigned int state_size, void *data);
130 struct proc_dir_entry *proc_create_net_single_write(const char *name, umode_t mode,
131 						    struct proc_dir_entry *parent,
132 						    int (*show)(struct seq_file *, void *),
133 						    proc_write_t write,
134 						    void *data);
135 extern struct pid *tgid_pidfd_to_pid(const struct file *file);
136 
137 struct bpf_iter_aux_info;
138 extern int bpf_iter_init_seq_net(void *priv_data, struct bpf_iter_aux_info *aux);
139 extern void bpf_iter_fini_seq_net(void *priv_data);
140 
141 #ifdef CONFIG_PROC_PID_ARCH_STATUS
142 /*
143  * The architecture which selects CONFIG_PROC_PID_ARCH_STATUS must
144  * provide proc_pid_arch_status() definition.
145  */
146 int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns,
147 			struct pid *pid, struct task_struct *task);
148 #endif /* CONFIG_PROC_PID_ARCH_STATUS */
149 
150 #else /* CONFIG_PROC_FS */
151 
proc_root_init(void)152 static inline void proc_root_init(void)
153 {
154 }
155 
proc_flush_pid(struct pid * pid)156 static inline void proc_flush_pid(struct pid *pid)
157 {
158 }
159 
proc_symlink(const char * name,struct proc_dir_entry * parent,const char * dest)160 static inline struct proc_dir_entry *proc_symlink(const char *name,
161 		struct proc_dir_entry *parent,const char *dest) { return NULL;}
proc_mkdir(const char * name,struct proc_dir_entry * parent)162 static inline struct proc_dir_entry *proc_mkdir(const char *name,
163 	struct proc_dir_entry *parent) {return NULL;}
proc_create_mount_point(const char * name)164 static inline struct proc_dir_entry *proc_create_mount_point(const char *name) { return NULL; }
proc_mkdir_data(const char * name,umode_t mode,struct proc_dir_entry * parent,void * data)165 static inline struct proc_dir_entry *proc_mkdir_data(const char *name,
166 	umode_t mode, struct proc_dir_entry *parent, void *data) { return NULL; }
proc_mkdir_mode(const char * name,umode_t mode,struct proc_dir_entry * parent)167 static inline struct proc_dir_entry *proc_mkdir_mode(const char *name,
168 	umode_t mode, struct proc_dir_entry *parent) { return NULL; }
169 #define proc_create_seq_private(name, mode, parent, ops, size, data) ({NULL;})
170 #define proc_create_seq_data(name, mode, parent, ops, data) ({NULL;})
171 #define proc_create_seq(name, mode, parent, ops) ({NULL;})
172 #define proc_create_single(name, mode, parent, show) ({NULL;})
173 #define proc_create_single_data(name, mode, parent, show, data) ({NULL;})
174 #define proc_create(name, mode, parent, proc_ops) ({NULL;})
175 #define proc_create_data(name, mode, parent, proc_ops, data) ({NULL;})
176 
proc_set_size(struct proc_dir_entry * de,loff_t size)177 static inline void proc_set_size(struct proc_dir_entry *de, loff_t size) {}
proc_set_user(struct proc_dir_entry * de,kuid_t uid,kgid_t gid)178 static inline void proc_set_user(struct proc_dir_entry *de, kuid_t uid, kgid_t gid) {}
PDE_DATA(const struct inode * inode)179 static inline void *PDE_DATA(const struct inode *inode) {BUG(); return NULL;}
proc_get_parent_data(const struct inode * inode)180 static inline void *proc_get_parent_data(const struct inode *inode) { BUG(); return NULL; }
181 
proc_remove(struct proc_dir_entry * de)182 static inline void proc_remove(struct proc_dir_entry *de) {}
183 #define remove_proc_entry(name, parent) do {} while (0)
remove_proc_subtree(const char * name,struct proc_dir_entry * parent)184 static inline int remove_proc_subtree(const char *name, struct proc_dir_entry *parent) { return 0; }
185 
186 #define proc_create_net_data(name, mode, parent, ops, state_size, data) ({NULL;})
187 #define proc_create_net(name, mode, parent, state_size, ops) ({NULL;})
188 #define proc_create_net_single(name, mode, parent, show, data) ({NULL;})
189 
tgid_pidfd_to_pid(const struct file * file)190 static inline struct pid *tgid_pidfd_to_pid(const struct file *file)
191 {
192 	return ERR_PTR(-EBADF);
193 }
194 
195 #endif /* CONFIG_PROC_FS */
196 
197 struct net;
198 
proc_net_mkdir(struct net * net,const char * name,struct proc_dir_entry * parent)199 static inline struct proc_dir_entry *proc_net_mkdir(
200 	struct net *net, const char *name, struct proc_dir_entry *parent)
201 {
202 	return proc_mkdir_data(name, 0, parent, net);
203 }
204 
205 struct ns_common;
206 int open_related_ns(struct ns_common *ns,
207 		   struct ns_common *(*get_ns)(struct ns_common *ns));
208 
209 /* get the associated pid namespace for a file in procfs */
proc_pid_ns(struct super_block * sb)210 static inline struct pid_namespace *proc_pid_ns(struct super_block *sb)
211 {
212 	return proc_sb_info(sb)->pid_ns;
213 }
214 
215 bool proc_ns_file(const struct file *file);
216 
217 #endif /* _LINUX_PROC_FS_H */
218