Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _LINUX_SEQ_FILE_H
0003 #define _LINUX_SEQ_FILE_H
0004 
0005 #include <linux/types.h>
0006 #include <linux/string.h>
0007 #include <linux/string_helpers.h>
0008 #include <linux/bug.h>
0009 #include <linux/mutex.h>
0010 #include <linux/cpumask.h>
0011 #include <linux/nodemask.h>
0012 #include <linux/fs.h>
0013 #include <linux/cred.h>
0014 
0015 struct seq_operations;
0016 
0017 struct seq_file {
0018     char *buf;
0019     size_t size;
0020     size_t from;
0021     size_t count;
0022     size_t pad_until;
0023     loff_t index;
0024     loff_t read_pos;
0025     struct mutex lock;
0026     const struct seq_operations *op;
0027     int poll_event;
0028     const struct file *file;
0029     void *private;
0030 };
0031 
0032 struct seq_operations {
0033     void * (*start) (struct seq_file *m, loff_t *pos);
0034     void (*stop) (struct seq_file *m, void *v);
0035     void * (*next) (struct seq_file *m, void *v, loff_t *pos);
0036     int (*show) (struct seq_file *m, void *v);
0037 };
0038 
0039 #define SEQ_SKIP 1
0040 
0041 /**
0042  * seq_has_overflowed - check if the buffer has overflowed
0043  * @m: the seq_file handle
0044  *
0045  * seq_files have a buffer which may overflow. When this happens a larger
0046  * buffer is reallocated and all the data will be printed again.
0047  * The overflow state is true when m->count == m->size.
0048  *
0049  * Returns true if the buffer received more than it can hold.
0050  */
0051 static inline bool seq_has_overflowed(struct seq_file *m)
0052 {
0053     return m->count == m->size;
0054 }
0055 
0056 /**
0057  * seq_get_buf - get buffer to write arbitrary data to
0058  * @m: the seq_file handle
0059  * @bufp: the beginning of the buffer is stored here
0060  *
0061  * Return the number of bytes available in the buffer, or zero if
0062  * there's no space.
0063  */
0064 static inline size_t seq_get_buf(struct seq_file *m, char **bufp)
0065 {
0066     BUG_ON(m->count > m->size);
0067     if (m->count < m->size)
0068         *bufp = m->buf + m->count;
0069     else
0070         *bufp = NULL;
0071 
0072     return m->size - m->count;
0073 }
0074 
0075 /**
0076  * seq_commit - commit data to the buffer
0077  * @m: the seq_file handle
0078  * @num: the number of bytes to commit
0079  *
0080  * Commit @num bytes of data written to a buffer previously acquired
0081  * by seq_buf_get.  To signal an error condition, or that the data
0082  * didn't fit in the available space, pass a negative @num value.
0083  */
0084 static inline void seq_commit(struct seq_file *m, int num)
0085 {
0086     if (num < 0) {
0087         m->count = m->size;
0088     } else {
0089         BUG_ON(m->count + num > m->size);
0090         m->count += num;
0091     }
0092 }
0093 
0094 /**
0095  * seq_setwidth - set padding width
0096  * @m: the seq_file handle
0097  * @size: the max number of bytes to pad.
0098  *
0099  * Call seq_setwidth() for setting max width, then call seq_printf() etc. and
0100  * finally call seq_pad() to pad the remaining bytes.
0101  */
0102 static inline void seq_setwidth(struct seq_file *m, size_t size)
0103 {
0104     m->pad_until = m->count + size;
0105 }
0106 void seq_pad(struct seq_file *m, char c);
0107 
0108 char *mangle_path(char *s, const char *p, const char *esc);
0109 int seq_open(struct file *, const struct seq_operations *);
0110 ssize_t seq_read(struct file *, char __user *, size_t, loff_t *);
0111 ssize_t seq_read_iter(struct kiocb *iocb, struct iov_iter *iter);
0112 loff_t seq_lseek(struct file *, loff_t, int);
0113 int seq_release(struct inode *, struct file *);
0114 int seq_write(struct seq_file *seq, const void *data, size_t len);
0115 
0116 __printf(2, 0)
0117 void seq_vprintf(struct seq_file *m, const char *fmt, va_list args);
0118 __printf(2, 3)
0119 void seq_printf(struct seq_file *m, const char *fmt, ...);
0120 void seq_putc(struct seq_file *m, char c);
0121 void seq_puts(struct seq_file *m, const char *s);
0122 void seq_put_decimal_ull_width(struct seq_file *m, const char *delimiter,
0123                    unsigned long long num, unsigned int width);
0124 void seq_put_decimal_ull(struct seq_file *m, const char *delimiter,
0125              unsigned long long num);
0126 void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long num);
0127 void seq_put_hex_ll(struct seq_file *m, const char *delimiter,
0128             unsigned long long v, unsigned int width);
0129 
0130 void seq_escape_mem(struct seq_file *m, const char *src, size_t len,
0131             unsigned int flags, const char *esc);
0132 
0133 static inline void seq_escape_str(struct seq_file *m, const char *src,
0134                   unsigned int flags, const char *esc)
0135 {
0136     seq_escape_mem(m, src, strlen(src), flags, esc);
0137 }
0138 
0139 /**
0140  * seq_escape - print string into buffer, escaping some characters
0141  * @m: target buffer
0142  * @s: NULL-terminated string
0143  * @esc: set of characters that need escaping
0144  *
0145  * Puts string into buffer, replacing each occurrence of character from
0146  * @esc with usual octal escape.
0147  *
0148  * Use seq_has_overflowed() to check for errors.
0149  */
0150 static inline void seq_escape(struct seq_file *m, const char *s, const char *esc)
0151 {
0152     seq_escape_str(m, s, ESCAPE_OCTAL, esc);
0153 }
0154 
0155 void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
0156           int rowsize, int groupsize, const void *buf, size_t len,
0157           bool ascii);
0158 
0159 int seq_path(struct seq_file *, const struct path *, const char *);
0160 int seq_file_path(struct seq_file *, struct file *, const char *);
0161 int seq_dentry(struct seq_file *, struct dentry *, const char *);
0162 int seq_path_root(struct seq_file *m, const struct path *path,
0163           const struct path *root, const char *esc);
0164 
0165 void *single_start(struct seq_file *, loff_t *);
0166 int single_open(struct file *, int (*)(struct seq_file *, void *), void *);
0167 int single_open_size(struct file *, int (*)(struct seq_file *, void *), void *, size_t);
0168 int single_release(struct inode *, struct file *);
0169 void *__seq_open_private(struct file *, const struct seq_operations *, int);
0170 int seq_open_private(struct file *, const struct seq_operations *, int);
0171 int seq_release_private(struct inode *, struct file *);
0172 
0173 #ifdef CONFIG_BINARY_PRINTF
0174 void seq_bprintf(struct seq_file *m, const char *f, const u32 *binary);
0175 #endif
0176 
0177 #define DEFINE_SEQ_ATTRIBUTE(__name)                    \
0178 static int __name ## _open(struct inode *inode, struct file *file)  \
0179 {                                   \
0180     int ret = seq_open(file, &__name ## _sops);         \
0181     if (!ret && inode->i_private) {                 \
0182         struct seq_file *seq_f = file->private_data;        \
0183         seq_f->private = inode->i_private;          \
0184     }                               \
0185     return ret;                         \
0186 }                                   \
0187                                     \
0188 static const struct file_operations __name ## _fops = {         \
0189     .owner      = THIS_MODULE,                  \
0190     .open       = __name ## _open,              \
0191     .read       = seq_read,                 \
0192     .llseek     = seq_lseek,                    \
0193     .release    = seq_release,                  \
0194 }
0195 
0196 #define DEFINE_SHOW_ATTRIBUTE(__name)                   \
0197 static int __name ## _open(struct inode *inode, struct file *file)  \
0198 {                                   \
0199     return single_open(file, __name ## _show, inode->i_private);    \
0200 }                                   \
0201                                     \
0202 static const struct file_operations __name ## _fops = {         \
0203     .owner      = THIS_MODULE,                  \
0204     .open       = __name ## _open,              \
0205     .read       = seq_read,                 \
0206     .llseek     = seq_lseek,                    \
0207     .release    = single_release,               \
0208 }
0209 
0210 #define DEFINE_PROC_SHOW_ATTRIBUTE(__name)              \
0211 static int __name ## _open(struct inode *inode, struct file *file)  \
0212 {                                   \
0213     return single_open(file, __name ## _show, pde_data(inode)); \
0214 }                                   \
0215                                     \
0216 static const struct proc_ops __name ## _proc_ops = {            \
0217     .proc_open  = __name ## _open,              \
0218     .proc_read  = seq_read,                 \
0219     .proc_lseek = seq_lseek,                    \
0220     .proc_release   = single_release,               \
0221 }
0222 
0223 static inline struct user_namespace *seq_user_ns(struct seq_file *seq)
0224 {
0225 #ifdef CONFIG_USER_NS
0226     return seq->file->f_cred->user_ns;
0227 #else
0228     extern struct user_namespace init_user_ns;
0229     return &init_user_ns;
0230 #endif
0231 }
0232 
0233 /**
0234  * seq_show_options - display mount options with appropriate escapes.
0235  * @m: the seq_file handle
0236  * @name: the mount option name
0237  * @value: the mount option name's value, can be NULL
0238  */
0239 static inline void seq_show_option(struct seq_file *m, const char *name,
0240                    const char *value)
0241 {
0242     seq_putc(m, ',');
0243     seq_escape(m, name, ",= \t\n\\");
0244     if (value) {
0245         seq_putc(m, '=');
0246         seq_escape(m, value, ", \t\n\\");
0247     }
0248 }
0249 
0250 /**
0251  * seq_show_option_n - display mount options with appropriate escapes
0252  *             where @value must be a specific length.
0253  * @m: the seq_file handle
0254  * @name: the mount option name
0255  * @value: the mount option name's value, cannot be NULL
0256  * @length: the length of @value to display
0257  *
0258  * This is a macro since this uses "length" to define the size of the
0259  * stack buffer.
0260  */
0261 #define seq_show_option_n(m, name, value, length) { \
0262     char val_buf[length + 1];           \
0263     strncpy(val_buf, value, length);        \
0264     val_buf[length] = '\0';             \
0265     seq_show_option(m, name, val_buf);      \
0266 }
0267 
0268 #define SEQ_START_TOKEN ((void *)1)
0269 /*
0270  * Helpers for iteration over list_head-s in seq_files
0271  */
0272 
0273 extern struct list_head *seq_list_start(struct list_head *head,
0274         loff_t pos);
0275 extern struct list_head *seq_list_start_head(struct list_head *head,
0276         loff_t pos);
0277 extern struct list_head *seq_list_next(void *v, struct list_head *head,
0278         loff_t *ppos);
0279 
0280 extern struct list_head *seq_list_start_rcu(struct list_head *head, loff_t pos);
0281 extern struct list_head *seq_list_start_head_rcu(struct list_head *head, loff_t pos);
0282 extern struct list_head *seq_list_next_rcu(void *v, struct list_head *head, loff_t *ppos);
0283 
0284 /*
0285  * Helpers for iteration over hlist_head-s in seq_files
0286  */
0287 
0288 extern struct hlist_node *seq_hlist_start(struct hlist_head *head,
0289                       loff_t pos);
0290 extern struct hlist_node *seq_hlist_start_head(struct hlist_head *head,
0291                            loff_t pos);
0292 extern struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
0293                      loff_t *ppos);
0294 
0295 extern struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
0296                           loff_t pos);
0297 extern struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
0298                            loff_t pos);
0299 extern struct hlist_node *seq_hlist_next_rcu(void *v,
0300                            struct hlist_head *head,
0301                            loff_t *ppos);
0302 
0303 /* Helpers for iterating over per-cpu hlist_head-s in seq_files */
0304 extern struct hlist_node *seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos);
0305 
0306 extern struct hlist_node *seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head, int *cpu, loff_t *pos);
0307 
0308 void seq_file_init(void);
0309 #endif