internal.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 (dhowells@redhat.com)
  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. extern struct kmem_cache *proc_dir_entry_cache;
  77. void pde_free(struct proc_dir_entry *pde);
  78. union proc_op {
  79. int (*proc_get_link)(struct dentry *, struct path *);
  80. int (*proc_show)(struct seq_file *m,
  81. struct pid_namespace *ns, struct pid *pid,
  82. struct task_struct *task);
  83. const char *lsm;
  84. };
  85. struct proc_inode {
  86. struct pid *pid;
  87. unsigned int fd;
  88. union proc_op op;
  89. struct proc_dir_entry *pde;
  90. struct ctl_table_header *sysctl;
  91. struct ctl_table *sysctl_entry;
  92. struct hlist_node sibling_inodes;
  93. const struct proc_ns_operations *ns_ops;
  94. struct inode vfs_inode;
  95. } __randomize_layout;
  96. /*
  97. * General functions
  98. */
  99. static inline struct proc_inode *PROC_I(const struct inode *inode)
  100. {
  101. return container_of(inode, struct proc_inode, vfs_inode);
  102. }
  103. static inline struct proc_dir_entry *PDE(const struct inode *inode)
  104. {
  105. return PROC_I(inode)->pde;
  106. }
  107. static inline void *__PDE_DATA(const struct inode *inode)
  108. {
  109. return PDE(inode)->data;
  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(const struct path *, struct kstat *, u32, unsigned int);
  147. extern int proc_setattr(struct dentry *, struct iattr *);
  148. extern void proc_pid_evict_inode(struct proc_inode *);
  149. extern struct inode *proc_pid_make_inode(struct super_block *, struct task_struct *, umode_t);
  150. extern void pid_update_inode(struct task_struct *, struct inode *);
  151. extern int pid_delete_dentry(const struct dentry *);
  152. extern int proc_pid_readdir(struct file *, struct dir_context *);
  153. struct dentry *proc_pid_lookup(struct dentry *, unsigned int);
  154. extern loff_t mem_lseek(struct file *, loff_t, int);
  155. /* Lookups */
  156. typedef struct dentry *instantiate_t(struct dentry *,
  157. struct task_struct *, const void *);
  158. bool proc_fill_cache(struct file *, struct dir_context *, const char *, unsigned int,
  159. instantiate_t, struct task_struct *, const void *);
  160. /*
  161. * generic.c
  162. */
  163. struct proc_dir_entry *proc_create_reg(const char *name, umode_t mode,
  164. struct proc_dir_entry **parent, void *data);
  165. struct proc_dir_entry *proc_register(struct proc_dir_entry *dir,
  166. struct proc_dir_entry *dp);
  167. extern struct dentry *proc_lookup(struct inode *, struct dentry *, unsigned int);
  168. struct dentry *proc_lookup_de(struct inode *, struct dentry *, struct proc_dir_entry *);
  169. extern int proc_readdir(struct file *, struct dir_context *);
  170. int proc_readdir_de(struct file *, struct dir_context *, struct proc_dir_entry *);
  171. static inline struct proc_dir_entry *pde_get(struct proc_dir_entry *pde)
  172. {
  173. refcount_inc(&pde->refcnt);
  174. return pde;
  175. }
  176. extern void pde_put(struct proc_dir_entry *);
  177. static inline bool is_empty_pde(const struct proc_dir_entry *pde)
  178. {
  179. return S_ISDIR(pde->mode) && !pde->proc_iops;
  180. }
  181. extern ssize_t proc_simple_write(struct file *, const char __user *, size_t, loff_t *);
  182. /*
  183. * inode.c
  184. */
  185. struct pde_opener {
  186. struct list_head lh;
  187. struct file *file;
  188. bool closing;
  189. struct completion *c;
  190. } __randomize_layout;
  191. extern const struct inode_operations proc_link_inode_operations;
  192. extern const struct inode_operations proc_pid_link_inode_operations;
  193. extern const struct super_operations proc_sops;
  194. void proc_init_kmemcache(void);
  195. void proc_invalidate_siblings_dcache(struct hlist_head *inodes, spinlock_t *lock);
  196. void set_proc_pid_nlink(void);
  197. extern struct inode *proc_get_inode(struct super_block *, struct proc_dir_entry *);
  198. extern void proc_entry_rundown(struct proc_dir_entry *);
  199. /*
  200. * proc_namespaces.c
  201. */
  202. extern const struct inode_operations proc_ns_dir_inode_operations;
  203. extern const struct file_operations proc_ns_dir_operations;
  204. /*
  205. * proc_net.c
  206. */
  207. extern const struct file_operations proc_net_operations;
  208. extern const struct inode_operations proc_net_inode_operations;
  209. #ifdef CONFIG_NET
  210. extern int proc_net_init(void);
  211. #else
  212. static inline int proc_net_init(void) { return 0; }
  213. #endif
  214. /*
  215. * proc_self.c
  216. */
  217. extern int proc_setup_self(struct super_block *);
  218. /*
  219. * proc_thread_self.c
  220. */
  221. extern int proc_setup_thread_self(struct super_block *);
  222. extern void proc_thread_self_init(void);
  223. /*
  224. * proc_sysctl.c
  225. */
  226. #ifdef CONFIG_PROC_SYSCTL
  227. extern int proc_sys_init(void);
  228. extern void proc_sys_evict_inode(struct inode *inode,
  229. struct ctl_table_header *head);
  230. #else
  231. static inline void proc_sys_init(void) { }
  232. static inline void proc_sys_evict_inode(struct inode *inode,
  233. struct ctl_table_header *head) { }
  234. #endif
  235. /*
  236. * proc_tty.c
  237. */
  238. #ifdef CONFIG_TTY
  239. extern void proc_tty_init(void);
  240. #else
  241. static inline void proc_tty_init(void) {}
  242. #endif
  243. /*
  244. * root.c
  245. */
  246. extern struct proc_dir_entry proc_root;
  247. extern void proc_self_init(void);
  248. /*
  249. * task_[no]mmu.c
  250. */
  251. struct mem_size_stats;
  252. struct proc_maps_private {
  253. struct inode *inode;
  254. struct task_struct *task;
  255. struct mm_struct *mm;
  256. #ifdef CONFIG_MMU
  257. struct vm_area_struct *tail_vma;
  258. #endif
  259. #ifdef CONFIG_NUMA
  260. struct mempolicy *task_mempolicy;
  261. #endif
  262. } __randomize_layout;
  263. struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode);
  264. extern const struct file_operations proc_pid_maps_operations;
  265. extern const struct file_operations proc_pid_numa_maps_operations;
  266. extern const struct file_operations proc_pid_smaps_operations;
  267. extern const struct file_operations proc_pid_smaps_rollup_operations;
  268. extern const struct file_operations proc_clear_refs_operations;
  269. extern const struct file_operations proc_pagemap_operations;
  270. extern unsigned long task_vsize(struct mm_struct *);
  271. extern unsigned long task_statm(struct mm_struct *,
  272. unsigned long *, unsigned long *,
  273. unsigned long *, unsigned long *);
  274. extern void task_mem(struct seq_file *, struct mm_struct *);
  275. extern const struct dentry_operations proc_net_dentry_ops;
  276. static inline void pde_force_lookup(struct proc_dir_entry *pde)
  277. {
  278. /* /proc/net/ entries can be changed under us by setns(CLONE_NEWNET) */
  279. pde->proc_dops = &proc_net_dentry_ops;
  280. }