seq_file.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_SEQ_FILE_H
  3. #define _LINUX_SEQ_FILE_H
  4. #include <linux/types.h>
  5. #include <linux/string.h>
  6. #include <linux/string_helpers.h>
  7. #include <linux/bug.h>
  8. #include <linux/mutex.h>
  9. #include <linux/cpumask.h>
  10. #include <linux/nodemask.h>
  11. #include <linux/fs.h>
  12. #include <linux/cred.h>
  13. struct seq_operations;
  14. struct seq_file {
  15. char *buf;
  16. size_t size;
  17. size_t from;
  18. size_t count;
  19. size_t pad_until;
  20. loff_t index;
  21. loff_t read_pos;
  22. struct mutex lock;
  23. const struct seq_operations *op;
  24. int poll_event;
  25. const struct file *file;
  26. void *private;
  27. };
  28. struct seq_operations {
  29. void * (*start) (struct seq_file *m, loff_t *pos);
  30. void (*stop) (struct seq_file *m, void *v);
  31. void * (*next) (struct seq_file *m, void *v, loff_t *pos);
  32. int (*show) (struct seq_file *m, void *v);
  33. };
  34. #define SEQ_SKIP 1
  35. /**
  36. * seq_has_overflowed - check if the buffer has overflowed
  37. * @m: the seq_file handle
  38. *
  39. * seq_files have a buffer which may overflow. When this happens a larger
  40. * buffer is reallocated and all the data will be printed again.
  41. * The overflow state is true when m->count == m->size.
  42. *
  43. * Returns true if the buffer received more than it can hold.
  44. */
  45. static inline bool seq_has_overflowed(struct seq_file *m)
  46. {
  47. return m->count == m->size;
  48. }
  49. /**
  50. * seq_get_buf - get buffer to write arbitrary data to
  51. * @m: the seq_file handle
  52. * @bufp: the beginning of the buffer is stored here
  53. *
  54. * Return the number of bytes available in the buffer, or zero if
  55. * there's no space.
  56. */
  57. static inline size_t seq_get_buf(struct seq_file *m, char **bufp)
  58. {
  59. BUG_ON(m->count > m->size);
  60. if (m->count < m->size)
  61. *bufp = m->buf + m->count;
  62. else
  63. *bufp = NULL;
  64. return m->size - m->count;
  65. }
  66. /**
  67. * seq_commit - commit data to the buffer
  68. * @m: the seq_file handle
  69. * @num: the number of bytes to commit
  70. *
  71. * Commit @num bytes of data written to a buffer previously acquired
  72. * by seq_buf_get. To signal an error condition, or that the data
  73. * didn't fit in the available space, pass a negative @num value.
  74. */
  75. static inline void seq_commit(struct seq_file *m, int num)
  76. {
  77. if (num < 0) {
  78. m->count = m->size;
  79. } else {
  80. BUG_ON(m->count + num > m->size);
  81. m->count += num;
  82. }
  83. }
  84. /**
  85. * seq_setwidth - set padding width
  86. * @m: the seq_file handle
  87. * @size: the max number of bytes to pad.
  88. *
  89. * Call seq_setwidth() for setting max width, then call seq_printf() etc. and
  90. * finally call seq_pad() to pad the remaining bytes.
  91. */
  92. static inline void seq_setwidth(struct seq_file *m, size_t size)
  93. {
  94. m->pad_until = m->count + size;
  95. }
  96. void seq_pad(struct seq_file *m, char c);
  97. char *mangle_path(char *s, const char *p, const char *esc);
  98. int seq_open(struct file *, const struct seq_operations *);
  99. ssize_t seq_read(struct file *, char __user *, size_t, loff_t *);
  100. ssize_t seq_read_iter(struct kiocb *iocb, struct iov_iter *iter);
  101. loff_t seq_lseek(struct file *, loff_t, int);
  102. int seq_release(struct inode *, struct file *);
  103. int seq_write(struct seq_file *seq, const void *data, size_t len);
  104. __printf(2, 0)
  105. void seq_vprintf(struct seq_file *m, const char *fmt, va_list args);
  106. __printf(2, 3)
  107. void seq_printf(struct seq_file *m, const char *fmt, ...);
  108. void seq_putc(struct seq_file *m, char c);
  109. void seq_puts(struct seq_file *m, const char *s);
  110. void seq_put_decimal_ull_width(struct seq_file *m, const char *delimiter,
  111. unsigned long long num, unsigned int width);
  112. void seq_put_decimal_ull(struct seq_file *m, const char *delimiter,
  113. unsigned long long num);
  114. void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long num);
  115. void seq_put_hex_ll(struct seq_file *m, const char *delimiter,
  116. unsigned long long v, unsigned int width);
  117. void seq_escape_mem(struct seq_file *m, const char *src, size_t len,
  118. unsigned int flags, const char *esc);
  119. static inline void seq_escape_str(struct seq_file *m, const char *src,
  120. unsigned int flags, const char *esc)
  121. {
  122. seq_escape_mem(m, src, strlen(src), flags, esc);
  123. }
  124. /**
  125. * seq_escape - print string into buffer, escaping some characters
  126. * @m: target buffer
  127. * @s: NULL-terminated string
  128. * @esc: set of characters that need escaping
  129. *
  130. * Puts string into buffer, replacing each occurrence of character from
  131. * @esc with usual octal escape.
  132. *
  133. * Use seq_has_overflowed() to check for errors.
  134. */
  135. static inline void seq_escape(struct seq_file *m, const char *s, const char *esc)
  136. {
  137. seq_escape_str(m, s, ESCAPE_OCTAL, esc);
  138. }
  139. void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
  140. int rowsize, int groupsize, const void *buf, size_t len,
  141. bool ascii);
  142. int seq_path(struct seq_file *, const struct path *, const char *);
  143. int seq_file_path(struct seq_file *, struct file *, const char *);
  144. int seq_dentry(struct seq_file *, struct dentry *, const char *);
  145. int seq_path_root(struct seq_file *m, const struct path *path,
  146. const struct path *root, const char *esc);
  147. void *single_start(struct seq_file *, loff_t *);
  148. int single_open(struct file *, int (*)(struct seq_file *, void *), void *);
  149. int single_open_size(struct file *, int (*)(struct seq_file *, void *), void *, size_t);
  150. int single_release(struct inode *, struct file *);
  151. void *__seq_open_private(struct file *, const struct seq_operations *, int);
  152. int seq_open_private(struct file *, const struct seq_operations *, int);
  153. int seq_release_private(struct inode *, struct file *);
  154. #ifdef CONFIG_BINARY_PRINTF
  155. void seq_bprintf(struct seq_file *m, const char *f, const u32 *binary);
  156. #endif
  157. #define DEFINE_SEQ_ATTRIBUTE(__name) \
  158. static int __name ## _open(struct inode *inode, struct file *file) \
  159. { \
  160. int ret = seq_open(file, &__name ## _sops); \
  161. if (!ret && inode->i_private) { \
  162. struct seq_file *seq_f = file->private_data; \
  163. seq_f->private = inode->i_private; \
  164. } \
  165. return ret; \
  166. } \
  167. \
  168. static const struct file_operations __name ## _fops = { \
  169. .owner = THIS_MODULE, \
  170. .open = __name ## _open, \
  171. .read = seq_read, \
  172. .llseek = seq_lseek, \
  173. .release = seq_release, \
  174. }
  175. #define DEFINE_SHOW_ATTRIBUTE(__name) \
  176. static int __name ## _open(struct inode *inode, struct file *file) \
  177. { \
  178. return single_open(file, __name ## _show, inode->i_private); \
  179. } \
  180. \
  181. static const struct file_operations __name ## _fops = { \
  182. .owner = THIS_MODULE, \
  183. .open = __name ## _open, \
  184. .read = seq_read, \
  185. .llseek = seq_lseek, \
  186. .release = single_release, \
  187. }
  188. #define DEFINE_PROC_SHOW_ATTRIBUTE(__name) \
  189. static int __name ## _open(struct inode *inode, struct file *file) \
  190. { \
  191. return single_open(file, __name ## _show, pde_data(inode)); \
  192. } \
  193. \
  194. static const struct proc_ops __name ## _proc_ops = { \
  195. .proc_open = __name ## _open, \
  196. .proc_read = seq_read, \
  197. .proc_lseek = seq_lseek, \
  198. .proc_release = single_release, \
  199. }
  200. static inline struct user_namespace *seq_user_ns(struct seq_file *seq)
  201. {
  202. #ifdef CONFIG_USER_NS
  203. return seq->file->f_cred->user_ns;
  204. #else
  205. extern struct user_namespace init_user_ns;
  206. return &init_user_ns;
  207. #endif
  208. }
  209. /**
  210. * seq_show_options - display mount options with appropriate escapes.
  211. * @m: the seq_file handle
  212. * @name: the mount option name
  213. * @value: the mount option name's value, can be NULL
  214. */
  215. static inline void seq_show_option(struct seq_file *m, const char *name,
  216. const char *value)
  217. {
  218. seq_putc(m, ',');
  219. seq_escape(m, name, ",= \t\n\\");
  220. if (value) {
  221. seq_putc(m, '=');
  222. seq_escape(m, value, ", \t\n\\");
  223. }
  224. }
  225. /**
  226. * seq_show_option_n - display mount options with appropriate escapes
  227. * where @value must be a specific length.
  228. * @m: the seq_file handle
  229. * @name: the mount option name
  230. * @value: the mount option name's value, cannot be NULL
  231. * @length: the length of @value to display
  232. *
  233. * This is a macro since this uses "length" to define the size of the
  234. * stack buffer.
  235. */
  236. #define seq_show_option_n(m, name, value, length) { \
  237. char val_buf[length + 1]; \
  238. strncpy(val_buf, value, length); \
  239. val_buf[length] = '\0'; \
  240. seq_show_option(m, name, val_buf); \
  241. }
  242. #define SEQ_START_TOKEN ((void *)1)
  243. /*
  244. * Helpers for iteration over list_head-s in seq_files
  245. */
  246. extern struct list_head *seq_list_start(struct list_head *head,
  247. loff_t pos);
  248. extern struct list_head *seq_list_start_head(struct list_head *head,
  249. loff_t pos);
  250. extern struct list_head *seq_list_next(void *v, struct list_head *head,
  251. loff_t *ppos);
  252. extern struct list_head *seq_list_start_rcu(struct list_head *head, loff_t pos);
  253. extern struct list_head *seq_list_start_head_rcu(struct list_head *head, loff_t pos);
  254. extern struct list_head *seq_list_next_rcu(void *v, struct list_head *head, loff_t *ppos);
  255. /*
  256. * Helpers for iteration over hlist_head-s in seq_files
  257. */
  258. extern struct hlist_node *seq_hlist_start(struct hlist_head *head,
  259. loff_t pos);
  260. extern struct hlist_node *seq_hlist_start_head(struct hlist_head *head,
  261. loff_t pos);
  262. extern struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
  263. loff_t *ppos);
  264. extern struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
  265. loff_t pos);
  266. extern struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
  267. loff_t pos);
  268. extern struct hlist_node *seq_hlist_next_rcu(void *v,
  269. struct hlist_head *head,
  270. loff_t *ppos);
  271. /* Helpers for iterating over per-cpu hlist_head-s in seq_files */
  272. extern struct hlist_node *seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos);
  273. extern struct hlist_node *seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head, int *cpu, loff_t *pos);
  274. void seq_file_init(void);
  275. #endif