Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /* Internal procfs definitions
0003  *
0004  * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
0005  * Written by David Howells (dhowells@redhat.com)
0006  */
0007 
0008 #include <linux/proc_fs.h>
0009 #include <linux/proc_ns.h>
0010 #include <linux/refcount.h>
0011 #include <linux/spinlock.h>
0012 #include <linux/atomic.h>
0013 #include <linux/binfmts.h>
0014 #include <linux/sched/coredump.h>
0015 #include <linux/sched/task.h>
0016 
0017 struct ctl_table_header;
0018 struct mempolicy;
0019 
0020 /*
0021  * This is not completely implemented yet. The idea is to
0022  * create an in-memory tree (like the actual /proc filesystem
0023  * tree) of these proc_dir_entries, so that we can dynamically
0024  * add new files to /proc.
0025  *
0026  * parent/subdir are used for the directory structure (every /proc file has a
0027  * parent, but "subdir" is empty for all non-directory entries).
0028  * subdir_node is used to build the rb tree "subdir" of the parent.
0029  */
0030 struct proc_dir_entry {
0031     /*
0032      * number of callers into module in progress;
0033      * negative -> it's going away RSN
0034      */
0035     atomic_t in_use;
0036     refcount_t refcnt;
0037     struct list_head pde_openers;   /* who did ->open, but not ->release */
0038     /* protects ->pde_openers and all struct pde_opener instances */
0039     spinlock_t pde_unload_lock;
0040     struct completion *pde_unload_completion;
0041     const struct inode_operations *proc_iops;
0042     union {
0043         const struct proc_ops *proc_ops;
0044         const struct file_operations *proc_dir_ops;
0045     };
0046     const struct dentry_operations *proc_dops;
0047     union {
0048         const struct seq_operations *seq_ops;
0049         int (*single_show)(struct seq_file *, void *);
0050     };
0051     proc_write_t write;
0052     void *data;
0053     unsigned int state_size;
0054     unsigned int low_ino;
0055     nlink_t nlink;
0056     kuid_t uid;
0057     kgid_t gid;
0058     loff_t size;
0059     struct proc_dir_entry *parent;
0060     struct rb_root subdir;
0061     struct rb_node subdir_node;
0062     char *name;
0063     umode_t mode;
0064     u8 flags;
0065     u8 namelen;
0066     char inline_name[];
0067 } __randomize_layout;
0068 
0069 #define SIZEOF_PDE  (               \
0070     sizeof(struct proc_dir_entry) < 128 ? 128 : \
0071     sizeof(struct proc_dir_entry) < 192 ? 192 : \
0072     sizeof(struct proc_dir_entry) < 256 ? 256 : \
0073     sizeof(struct proc_dir_entry) < 512 ? 512 : \
0074     0)
0075 #define SIZEOF_PDE_INLINE_NAME (SIZEOF_PDE - sizeof(struct proc_dir_entry))
0076 
0077 static inline bool pde_is_permanent(const struct proc_dir_entry *pde)
0078 {
0079     return pde->flags & PROC_ENTRY_PERMANENT;
0080 }
0081 
0082 extern struct kmem_cache *proc_dir_entry_cache;
0083 void pde_free(struct proc_dir_entry *pde);
0084 
0085 union proc_op {
0086     int (*proc_get_link)(struct dentry *, struct path *);
0087     int (*proc_show)(struct seq_file *m,
0088         struct pid_namespace *ns, struct pid *pid,
0089         struct task_struct *task);
0090     const char *lsm;
0091 };
0092 
0093 struct proc_inode {
0094     struct pid *pid;
0095     unsigned int fd;
0096     union proc_op op;
0097     struct proc_dir_entry *pde;
0098     struct ctl_table_header *sysctl;
0099     struct ctl_table *sysctl_entry;
0100     struct hlist_node sibling_inodes;
0101     const struct proc_ns_operations *ns_ops;
0102     struct inode vfs_inode;
0103 } __randomize_layout;
0104 
0105 /*
0106  * General functions
0107  */
0108 static inline struct proc_inode *PROC_I(const struct inode *inode)
0109 {
0110     return container_of(inode, struct proc_inode, vfs_inode);
0111 }
0112 
0113 static inline struct proc_dir_entry *PDE(const struct inode *inode)
0114 {
0115     return PROC_I(inode)->pde;
0116 }
0117 
0118 static inline struct pid *proc_pid(const struct inode *inode)
0119 {
0120     return PROC_I(inode)->pid;
0121 }
0122 
0123 static inline struct task_struct *get_proc_task(const struct inode *inode)
0124 {
0125     return get_pid_task(proc_pid(inode), PIDTYPE_PID);
0126 }
0127 
0128 void task_dump_owner(struct task_struct *task, umode_t mode,
0129              kuid_t *ruid, kgid_t *rgid);
0130 
0131 unsigned name_to_int(const struct qstr *qstr);
0132 /*
0133  * Offset of the first process in the /proc root directory..
0134  */
0135 #define FIRST_PROCESS_ENTRY 256
0136 
0137 /* Worst case buffer size needed for holding an integer. */
0138 #define PROC_NUMBUF 13
0139 
0140 /*
0141  * array.c
0142  */
0143 extern const struct file_operations proc_tid_children_operations;
0144 
0145 extern void proc_task_name(struct seq_file *m, struct task_struct *p,
0146                bool escape);
0147 extern int proc_tid_stat(struct seq_file *, struct pid_namespace *,
0148              struct pid *, struct task_struct *);
0149 extern int proc_tgid_stat(struct seq_file *, struct pid_namespace *,
0150               struct pid *, struct task_struct *);
0151 extern int proc_pid_status(struct seq_file *, struct pid_namespace *,
0152                struct pid *, struct task_struct *);
0153 extern int proc_pid_statm(struct seq_file *, struct pid_namespace *,
0154               struct pid *, struct task_struct *);
0155 
0156 /*
0157  * base.c
0158  */
0159 extern const struct dentry_operations pid_dentry_operations;
0160 extern int pid_getattr(struct user_namespace *, const struct path *,
0161                struct kstat *, u32, unsigned int);
0162 extern int proc_setattr(struct user_namespace *, struct dentry *,
0163             struct iattr *);
0164 extern void proc_pid_evict_inode(struct proc_inode *);
0165 extern struct inode *proc_pid_make_inode(struct super_block *, struct task_struct *, umode_t);
0166 extern void pid_update_inode(struct task_struct *, struct inode *);
0167 extern int pid_delete_dentry(const struct dentry *);
0168 extern int proc_pid_readdir(struct file *, struct dir_context *);
0169 struct dentry *proc_pid_lookup(struct dentry *, unsigned int);
0170 extern loff_t mem_lseek(struct file *, loff_t, int);
0171 
0172 /* Lookups */
0173 typedef struct dentry *instantiate_t(struct dentry *,
0174                      struct task_struct *, const void *);
0175 bool proc_fill_cache(struct file *, struct dir_context *, const char *, unsigned int,
0176                instantiate_t, struct task_struct *, const void *);
0177 
0178 /*
0179  * generic.c
0180  */
0181 struct proc_dir_entry *proc_create_reg(const char *name, umode_t mode,
0182         struct proc_dir_entry **parent, void *data);
0183 struct proc_dir_entry *proc_register(struct proc_dir_entry *dir,
0184         struct proc_dir_entry *dp);
0185 extern struct dentry *proc_lookup(struct inode *, struct dentry *, unsigned int);
0186 struct dentry *proc_lookup_de(struct inode *, struct dentry *, struct proc_dir_entry *);
0187 extern int proc_readdir(struct file *, struct dir_context *);
0188 int proc_readdir_de(struct file *, struct dir_context *, struct proc_dir_entry *);
0189 
0190 static inline void pde_get(struct proc_dir_entry *pde)
0191 {
0192     refcount_inc(&pde->refcnt);
0193 }
0194 extern void pde_put(struct proc_dir_entry *);
0195 
0196 static inline bool is_empty_pde(const struct proc_dir_entry *pde)
0197 {
0198     return S_ISDIR(pde->mode) && !pde->proc_iops;
0199 }
0200 extern ssize_t proc_simple_write(struct file *, const char __user *, size_t, loff_t *);
0201 
0202 /*
0203  * inode.c
0204  */
0205 struct pde_opener {
0206     struct list_head lh;
0207     struct file *file;
0208     bool closing;
0209     struct completion *c;
0210 } __randomize_layout;
0211 extern const struct inode_operations proc_link_inode_operations;
0212 extern const struct inode_operations proc_pid_link_inode_operations;
0213 extern const struct super_operations proc_sops;
0214 
0215 void proc_init_kmemcache(void);
0216 void proc_invalidate_siblings_dcache(struct hlist_head *inodes, spinlock_t *lock);
0217 void set_proc_pid_nlink(void);
0218 extern struct inode *proc_get_inode(struct super_block *, struct proc_dir_entry *);
0219 extern void proc_entry_rundown(struct proc_dir_entry *);
0220 
0221 /*
0222  * proc_namespaces.c
0223  */
0224 extern const struct inode_operations proc_ns_dir_inode_operations;
0225 extern const struct file_operations proc_ns_dir_operations;
0226 
0227 /*
0228  * proc_net.c
0229  */
0230 extern const struct file_operations proc_net_operations;
0231 extern const struct inode_operations proc_net_inode_operations;
0232 
0233 #ifdef CONFIG_NET
0234 extern int proc_net_init(void);
0235 #else
0236 static inline int proc_net_init(void) { return 0; }
0237 #endif
0238 
0239 /*
0240  * proc_self.c
0241  */
0242 extern int proc_setup_self(struct super_block *);
0243 
0244 /*
0245  * proc_thread_self.c
0246  */
0247 extern int proc_setup_thread_self(struct super_block *);
0248 extern void proc_thread_self_init(void);
0249 
0250 /*
0251  * proc_sysctl.c
0252  */
0253 #ifdef CONFIG_PROC_SYSCTL
0254 extern int proc_sys_init(void);
0255 extern void proc_sys_evict_inode(struct inode *inode,
0256                  struct ctl_table_header *head);
0257 #else
0258 static inline void proc_sys_init(void) { }
0259 static inline void proc_sys_evict_inode(struct  inode *inode,
0260                     struct ctl_table_header *head) { }
0261 #endif
0262 
0263 /*
0264  * proc_tty.c
0265  */
0266 #ifdef CONFIG_TTY
0267 extern void proc_tty_init(void);
0268 #else
0269 static inline void proc_tty_init(void) {}
0270 #endif
0271 
0272 /*
0273  * root.c
0274  */
0275 extern struct proc_dir_entry proc_root;
0276 
0277 extern void proc_self_init(void);
0278 
0279 /*
0280  * task_[no]mmu.c
0281  */
0282 struct mem_size_stats;
0283 struct proc_maps_private {
0284     struct inode *inode;
0285     struct task_struct *task;
0286     struct mm_struct *mm;
0287 #ifdef CONFIG_MMU
0288     struct vm_area_struct *tail_vma;
0289 #endif
0290 #ifdef CONFIG_NUMA
0291     struct mempolicy *task_mempolicy;
0292 #endif
0293 } __randomize_layout;
0294 
0295 struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode);
0296 
0297 extern const struct file_operations proc_pid_maps_operations;
0298 extern const struct file_operations proc_pid_numa_maps_operations;
0299 extern const struct file_operations proc_pid_smaps_operations;
0300 extern const struct file_operations proc_pid_smaps_rollup_operations;
0301 extern const struct file_operations proc_clear_refs_operations;
0302 extern const struct file_operations proc_pagemap_operations;
0303 
0304 extern unsigned long task_vsize(struct mm_struct *);
0305 extern unsigned long task_statm(struct mm_struct *,
0306                 unsigned long *, unsigned long *,
0307                 unsigned long *, unsigned long *);
0308 extern void task_mem(struct seq_file *, struct mm_struct *);
0309 
0310 extern const struct dentry_operations proc_net_dentry_ops;
0311 static inline void pde_force_lookup(struct proc_dir_entry *pde)
0312 {
0313     /* /proc/net/ entries can be changed under us by setns(CLONE_NEWNET) */
0314     pde->proc_dops = &proc_net_dentry_ops;
0315 }