This source file includes following definitions.
- seq_buf_can_fit
- seq_buf_print_seq
- seq_buf_vprintf
- seq_buf_printf
- seq_buf_bprintf
- seq_buf_puts
- seq_buf_putc
- seq_buf_putmem
- seq_buf_putmem_hex
- seq_buf_path
- seq_buf_to_user
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 #include <linux/uaccess.h>
17 #include <linux/seq_file.h>
18 #include <linux/seq_buf.h>
19
20
21
22
23
24
25
26
27
28 static bool seq_buf_can_fit(struct seq_buf *s, size_t len)
29 {
30 return s->len + len <= s->size;
31 }
32
33
34
35
36
37
38
39
40 int seq_buf_print_seq(struct seq_file *m, struct seq_buf *s)
41 {
42 unsigned int len = seq_buf_used(s);
43
44 return seq_write(m, s->buffer, len);
45 }
46
47
48
49
50
51
52
53
54
55
56
57 int seq_buf_vprintf(struct seq_buf *s, const char *fmt, va_list args)
58 {
59 int len;
60
61 WARN_ON(s->size == 0);
62
63 if (s->len < s->size) {
64 len = vsnprintf(s->buffer + s->len, s->size - s->len, fmt, args);
65 if (s->len + len < s->size) {
66 s->len += len;
67 return 0;
68 }
69 }
70 seq_buf_set_overflow(s);
71 return -1;
72 }
73
74
75
76
77
78
79
80
81
82
83 int seq_buf_printf(struct seq_buf *s, const char *fmt, ...)
84 {
85 va_list ap;
86 int ret;
87
88 va_start(ap, fmt);
89 ret = seq_buf_vprintf(s, fmt, ap);
90 va_end(ap);
91
92 return ret;
93 }
94
95 #ifdef CONFIG_BINARY_PRINTF
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113 int seq_buf_bprintf(struct seq_buf *s, const char *fmt, const u32 *binary)
114 {
115 unsigned int len = seq_buf_buffer_left(s);
116 int ret;
117
118 WARN_ON(s->size == 0);
119
120 if (s->len < s->size) {
121 ret = bstr_printf(s->buffer + s->len, len, fmt, binary);
122 if (s->len + ret < s->size) {
123 s->len += ret;
124 return 0;
125 }
126 }
127 seq_buf_set_overflow(s);
128 return -1;
129 }
130 #endif
131
132
133
134
135
136
137
138
139
140
141 int seq_buf_puts(struct seq_buf *s, const char *str)
142 {
143 size_t len = strlen(str);
144
145 WARN_ON(s->size == 0);
146
147
148 len += 1;
149
150 if (seq_buf_can_fit(s, len)) {
151 memcpy(s->buffer + s->len, str, len);
152
153 s->len += len - 1;
154 return 0;
155 }
156 seq_buf_set_overflow(s);
157 return -1;
158 }
159
160
161
162
163
164
165
166
167
168
169 int seq_buf_putc(struct seq_buf *s, unsigned char c)
170 {
171 WARN_ON(s->size == 0);
172
173 if (seq_buf_can_fit(s, 1)) {
174 s->buffer[s->len++] = c;
175 return 0;
176 }
177 seq_buf_set_overflow(s);
178 return -1;
179 }
180
181
182
183
184
185
186
187
188
189
190
191
192
193 int seq_buf_putmem(struct seq_buf *s, const void *mem, unsigned int len)
194 {
195 WARN_ON(s->size == 0);
196
197 if (seq_buf_can_fit(s, len)) {
198 memcpy(s->buffer + s->len, mem, len);
199 s->len += len;
200 return 0;
201 }
202 seq_buf_set_overflow(s);
203 return -1;
204 }
205
206 #define MAX_MEMHEX_BYTES 8U
207 #define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1)
208
209
210
211
212
213
214
215
216
217
218
219
220
221 int seq_buf_putmem_hex(struct seq_buf *s, const void *mem,
222 unsigned int len)
223 {
224 unsigned char hex[HEX_CHARS];
225 const unsigned char *data = mem;
226 unsigned int start_len;
227 int i, j;
228
229 WARN_ON(s->size == 0);
230
231 while (len) {
232 start_len = min(len, HEX_CHARS - 1);
233 #ifdef __BIG_ENDIAN
234 for (i = 0, j = 0; i < start_len; i++) {
235 #else
236 for (i = start_len-1, j = 0; i >= 0; i--) {
237 #endif
238 hex[j++] = hex_asc_hi(data[i]);
239 hex[j++] = hex_asc_lo(data[i]);
240 }
241 if (WARN_ON_ONCE(j == 0 || j/2 > len))
242 break;
243
244
245 len -= j / 2;
246 hex[j++] = ' ';
247
248 seq_buf_putmem(s, hex, j);
249 if (seq_buf_has_overflowed(s))
250 return -1;
251 }
252 return 0;
253 }
254
255
256
257
258
259
260
261
262
263
264
265 int seq_buf_path(struct seq_buf *s, const struct path *path, const char *esc)
266 {
267 char *buf;
268 size_t size = seq_buf_get_buf(s, &buf);
269 int res = -1;
270
271 WARN_ON(s->size == 0);
272
273 if (size) {
274 char *p = d_path(path, buf, size);
275 if (!IS_ERR(p)) {
276 char *end = mangle_path(buf, p, esc);
277 if (end)
278 res = end - buf;
279 }
280 }
281 seq_buf_commit(s, res);
282
283 return res;
284 }
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306 int seq_buf_to_user(struct seq_buf *s, char __user *ubuf, int cnt)
307 {
308 int len;
309 int ret;
310
311 if (!cnt)
312 return 0;
313
314 len = seq_buf_used(s);
315
316 if (len <= s->readpos)
317 return -EBUSY;
318
319 len -= s->readpos;
320 if (cnt > len)
321 cnt = len;
322 ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
323 if (ret == cnt)
324 return -EFAULT;
325
326 cnt -= ret;
327
328 s->readpos += cnt;
329 return cnt;
330 }