xref: /linux-6.15/include/linux/proc_fs.h (revision 82d00a93)
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_write)(struct file *, const char __user *, size_t, loff_t *);
34 	loff_t	(*proc_lseek)(struct file *, loff_t, int);
35 	int	(*proc_release)(struct inode *, struct file *);
36 	__poll_t (*proc_poll)(struct file *, struct poll_table_struct *);
37 	long	(*proc_ioctl)(struct file *, unsigned int, unsigned long);
38 #ifdef CONFIG_COMPAT
39 	long	(*proc_compat_ioctl)(struct file *, unsigned int, unsigned long);
40 #endif
41 	int	(*proc_mmap)(struct file *, struct vm_area_struct *);
42 	unsigned long (*proc_get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
43 } __randomize_layout;
44 
45 #ifdef CONFIG_PROC_FS
46 
47 typedef int (*proc_write_t)(struct file *, char *, size_t);
48 
49 extern void proc_root_init(void);
50 extern void proc_flush_pid(struct pid *);
51 
52 extern struct proc_dir_entry *proc_symlink(const char *,
53 		struct proc_dir_entry *, const char *);
54 extern struct proc_dir_entry *proc_mkdir(const char *, struct proc_dir_entry *);
55 extern struct proc_dir_entry *proc_mkdir_data(const char *, umode_t,
56 					      struct proc_dir_entry *, void *);
57 extern struct proc_dir_entry *proc_mkdir_mode(const char *, umode_t,
58 					      struct proc_dir_entry *);
59 struct proc_dir_entry *proc_create_mount_point(const char *name);
60 
61 struct proc_dir_entry *proc_create_seq_private(const char *name, umode_t mode,
62 		struct proc_dir_entry *parent, const struct seq_operations *ops,
63 		unsigned int state_size, void *data);
64 #define proc_create_seq_data(name, mode, parent, ops, data) \
65 	proc_create_seq_private(name, mode, parent, ops, 0, data)
66 #define proc_create_seq(name, mode, parent, ops) \
67 	proc_create_seq_private(name, mode, parent, ops, 0, NULL)
68 struct proc_dir_entry *proc_create_single_data(const char *name, umode_t mode,
69 		struct proc_dir_entry *parent,
70 		int (*show)(struct seq_file *, void *), void *data);
71 #define proc_create_single(name, mode, parent, show) \
72 	proc_create_single_data(name, mode, parent, show, NULL)
73 
74 extern struct proc_dir_entry *proc_create_data(const char *, umode_t,
75 					       struct proc_dir_entry *,
76 					       const struct proc_ops *,
77 					       void *);
78 
79 struct proc_dir_entry *proc_create(const char *name, umode_t mode, struct proc_dir_entry *parent, const struct proc_ops *proc_ops);
80 extern void proc_set_size(struct proc_dir_entry *, loff_t);
81 extern void proc_set_user(struct proc_dir_entry *, kuid_t, kgid_t);
82 extern void *PDE_DATA(const struct inode *);
83 extern void *proc_get_parent_data(const struct inode *);
84 extern void proc_remove(struct proc_dir_entry *);
85 extern void remove_proc_entry(const char *, struct proc_dir_entry *);
86 extern int remove_proc_subtree(const char *, struct proc_dir_entry *);
87 
88 struct proc_dir_entry *proc_create_net_data(const char *name, umode_t mode,
89 		struct proc_dir_entry *parent, const struct seq_operations *ops,
90 		unsigned int state_size, void *data);
91 #define proc_create_net(name, mode, parent, ops, state_size) \
92 	proc_create_net_data(name, mode, parent, ops, state_size, NULL)
93 struct proc_dir_entry *proc_create_net_single(const char *name, umode_t mode,
94 		struct proc_dir_entry *parent,
95 		int (*show)(struct seq_file *, void *), void *data);
96 struct proc_dir_entry *proc_create_net_data_write(const char *name, umode_t mode,
97 						  struct proc_dir_entry *parent,
98 						  const struct seq_operations *ops,
99 						  proc_write_t write,
100 						  unsigned int state_size, void *data);
101 struct proc_dir_entry *proc_create_net_single_write(const char *name, umode_t mode,
102 						    struct proc_dir_entry *parent,
103 						    int (*show)(struct seq_file *, void *),
104 						    proc_write_t write,
105 						    void *data);
106 extern struct pid *tgid_pidfd_to_pid(const struct file *file);
107 
108 #ifdef CONFIG_PROC_PID_ARCH_STATUS
109 /*
110  * The architecture which selects CONFIG_PROC_PID_ARCH_STATUS must
111  * provide proc_pid_arch_status() definition.
112  */
113 int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns,
114 			struct pid *pid, struct task_struct *task);
115 #endif /* CONFIG_PROC_PID_ARCH_STATUS */
116 
117 #else /* CONFIG_PROC_FS */
118 
119 static inline void proc_root_init(void)
120 {
121 }
122 
123 static inline void proc_flush_pid(struct pid *pid)
124 {
125 }
126 
127 static inline struct proc_dir_entry *proc_symlink(const char *name,
128 		struct proc_dir_entry *parent,const char *dest) { return NULL;}
129 static inline struct proc_dir_entry *proc_mkdir(const char *name,
130 	struct proc_dir_entry *parent) {return NULL;}
131 static inline struct proc_dir_entry *proc_create_mount_point(const char *name) { return NULL; }
132 static inline struct proc_dir_entry *proc_mkdir_data(const char *name,
133 	umode_t mode, struct proc_dir_entry *parent, void *data) { return NULL; }
134 static inline struct proc_dir_entry *proc_mkdir_mode(const char *name,
135 	umode_t mode, struct proc_dir_entry *parent) { return NULL; }
136 #define proc_create_seq_private(name, mode, parent, ops, size, data) ({NULL;})
137 #define proc_create_seq_data(name, mode, parent, ops, data) ({NULL;})
138 #define proc_create_seq(name, mode, parent, ops) ({NULL;})
139 #define proc_create_single(name, mode, parent, show) ({NULL;})
140 #define proc_create_single_data(name, mode, parent, show, data) ({NULL;})
141 #define proc_create(name, mode, parent, proc_ops) ({NULL;})
142 #define proc_create_data(name, mode, parent, proc_ops, data) ({NULL;})
143 
144 static inline void proc_set_size(struct proc_dir_entry *de, loff_t size) {}
145 static inline void proc_set_user(struct proc_dir_entry *de, kuid_t uid, kgid_t gid) {}
146 static inline void *PDE_DATA(const struct inode *inode) {BUG(); return NULL;}
147 static inline void *proc_get_parent_data(const struct inode *inode) { BUG(); return NULL; }
148 
149 static inline void proc_remove(struct proc_dir_entry *de) {}
150 #define remove_proc_entry(name, parent) do {} while (0)
151 static inline int remove_proc_subtree(const char *name, struct proc_dir_entry *parent) { return 0; }
152 
153 #define proc_create_net_data(name, mode, parent, ops, state_size, data) ({NULL;})
154 #define proc_create_net(name, mode, parent, state_size, ops) ({NULL;})
155 #define proc_create_net_single(name, mode, parent, show, data) ({NULL;})
156 
157 static inline struct pid *tgid_pidfd_to_pid(const struct file *file)
158 {
159 	return ERR_PTR(-EBADF);
160 }
161 
162 #endif /* CONFIG_PROC_FS */
163 
164 struct net;
165 
166 static inline struct proc_dir_entry *proc_net_mkdir(
167 	struct net *net, const char *name, struct proc_dir_entry *parent)
168 {
169 	return proc_mkdir_data(name, 0, parent, net);
170 }
171 
172 struct ns_common;
173 int open_related_ns(struct ns_common *ns,
174 		   struct ns_common *(*get_ns)(struct ns_common *ns));
175 
176 /* get the associated pid namespace for a file in procfs */
177 static inline struct pid_namespace *proc_pid_ns(const struct inode *inode)
178 {
179 	return inode->i_sb->s_fs_info;
180 }
181 
182 #endif /* _LINUX_PROC_FS_H */
183