proc_fs.h 8.9 KB

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