internal.h 9.1 KB

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