1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2016-2018, Matthew Macy <[email protected]>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 */
28
29 #include <sys/cdefs.h>
30 #include <sys/types.h>
31 #include <sys/systm.h>
32 #include <sys/param.h>
33 #include <sys/sbuf.h>
34 #include <sys/syslog.h>
35 #include <sys/vnode.h>
36
37 #include <linux/seq_file.h>
38 #include <linux/file.h>
39
40 #undef file
41 MALLOC_DEFINE(M_LSEQ, "seq_file", "seq_file");
42
43 ssize_t
seq_read(struct linux_file * f,char * ubuf,size_t size,off_t * ppos)44 seq_read(struct linux_file *f, char *ubuf, size_t size, off_t *ppos)
45 {
46 struct seq_file *m;
47 struct sbuf *sbuf;
48 void *p;
49 ssize_t rc;
50
51 m = f->private_data;
52 sbuf = m->buf;
53
54 p = m->op->start(m, ppos);
55 rc = m->op->show(m, p);
56 if (rc)
57 return (rc);
58
59 rc = sbuf_finish(sbuf);
60 if (rc)
61 return (rc);
62
63 rc = sbuf_len(sbuf);
64 if (*ppos >= rc || size < 1)
65 return (-EINVAL);
66
67 size = min(rc - *ppos, size);
68 rc = strscpy(ubuf, sbuf_data(sbuf) + *ppos, size + 1);
69
70 /* add 1 for null terminator */
71 if (rc > 0)
72 rc += 1;
73
74 return (rc);
75 }
76
77 int
seq_write(struct seq_file * seq,const void * data,size_t len)78 seq_write(struct seq_file *seq, const void *data, size_t len)
79 {
80 int ret;
81
82 ret = sbuf_bcpy(seq->buf, data, len);
83 if (ret == 0)
84 seq->size = sbuf_len(seq->buf);
85
86 return (ret);
87 }
88
89 void
seq_putc(struct seq_file * seq,char c)90 seq_putc(struct seq_file *seq, char c)
91 {
92 int ret;
93
94 ret = sbuf_putc(seq->buf, c);
95 if (ret == 0)
96 seq->size = sbuf_len(seq->buf);
97 }
98
99 void
seq_puts(struct seq_file * seq,const char * str)100 seq_puts(struct seq_file *seq, const char *str)
101 {
102 int ret;
103
104 ret = sbuf_printf(seq->buf, "%s", str);
105 if (ret == 0)
106 seq->size = sbuf_len(seq->buf);
107 }
108
109 /*
110 * This only needs to be a valid address for lkpi
111 * drivers it should never actually be called
112 */
113 off_t
seq_lseek(struct linux_file * file,off_t offset,int whence)114 seq_lseek(struct linux_file *file, off_t offset, int whence)
115 {
116
117 panic("%s not supported\n", __FUNCTION__);
118 return (0);
119 }
120
121 static void *
single_start(struct seq_file * p,off_t * pos)122 single_start(struct seq_file *p, off_t *pos)
123 {
124
125 return ((void *)(uintptr_t)(*pos == 0));
126 }
127
128 static void *
single_next(struct seq_file * p,void * v,off_t * pos)129 single_next(struct seq_file *p, void *v, off_t *pos)
130 {
131
132 ++*pos;
133 return (NULL);
134 }
135
136 static void
single_stop(struct seq_file * p,void * v)137 single_stop(struct seq_file *p, void *v)
138 {
139 }
140
141 static int
_seq_open_without_sbuf(struct linux_file * f,const struct seq_operations * op)142 _seq_open_without_sbuf(struct linux_file *f, const struct seq_operations *op)
143 {
144 struct seq_file *p;
145
146 if ((p = malloc(sizeof(*p), M_LSEQ, M_NOWAIT|M_ZERO)) == NULL)
147 return (-ENOMEM);
148
149 p->file = f;
150 p->op = op;
151 f->private_data = (void *) p;
152 return (0);
153 }
154
155 int
seq_open(struct linux_file * f,const struct seq_operations * op)156 seq_open(struct linux_file *f, const struct seq_operations *op)
157 {
158 int ret;
159
160 ret = _seq_open_without_sbuf(f, op);
161 if (ret == 0)
162 ((struct seq_file *)f->private_data)->buf = sbuf_new_auto();
163
164 return (ret);
165 }
166
167 void *
__seq_open_private(struct linux_file * f,const struct seq_operations * op,int size)168 __seq_open_private(struct linux_file *f, const struct seq_operations *op, int size)
169 {
170 struct seq_file *seq_file;
171 void *private;
172 int error;
173
174 private = malloc(size, M_LSEQ, M_NOWAIT|M_ZERO);
175 if (private == NULL)
176 return (NULL);
177
178 error = seq_open(f, op);
179 if (error < 0) {
180 free(private, M_LSEQ);
181 return (NULL);
182 }
183
184 seq_file = (struct seq_file *)f->private_data;
185 seq_file->private = private;
186
187 return (private);
188 }
189
190 static int
_single_open_without_sbuf(struct linux_file * f,int (* show)(struct seq_file *,void *),void * d)191 _single_open_without_sbuf(struct linux_file *f, int (*show)(struct seq_file *, void *), void *d)
192 {
193 struct seq_operations *op;
194 int rc = -ENOMEM;
195
196 op = malloc(sizeof(*op), M_LSEQ, M_NOWAIT);
197 if (op) {
198 op->start = single_start;
199 op->next = single_next;
200 op->stop = single_stop;
201 op->show = show;
202 rc = _seq_open_without_sbuf(f, op);
203 if (rc)
204 free(op, M_LSEQ);
205 else
206 ((struct seq_file *)f->private_data)->private = d;
207 }
208 return (rc);
209 }
210
211 int
single_open(struct linux_file * f,int (* show)(struct seq_file *,void *),void * d)212 single_open(struct linux_file *f, int (*show)(struct seq_file *, void *), void *d)
213 {
214 int ret;
215
216 ret = _single_open_without_sbuf(f, show, d);
217 if (ret == 0)
218 ((struct seq_file *)f->private_data)->buf = sbuf_new_auto();
219
220 return (ret);
221 }
222
223 int
single_open_size(struct linux_file * f,int (* show)(struct seq_file *,void *),void * d,size_t size)224 single_open_size(struct linux_file *f, int (*show)(struct seq_file *, void *), void *d, size_t size)
225 {
226 int ret;
227
228 ret = _single_open_without_sbuf(f, show, d);
229 if (ret == 0)
230 ((struct seq_file *)f->private_data)->buf = sbuf_new(
231 NULL, NULL, size, SBUF_AUTOEXTEND);
232
233 return (ret);
234 }
235
236 int
seq_release(struct inode * inode __unused,struct linux_file * file)237 seq_release(struct inode *inode __unused, struct linux_file *file)
238 {
239 struct seq_file *m;
240 struct sbuf *s;
241
242 m = file->private_data;
243 s = m->buf;
244
245 sbuf_delete(s);
246 free(m, M_LSEQ);
247
248 return (0);
249 }
250
251 int
seq_release_private(struct inode * inode __unused,struct linux_file * f)252 seq_release_private(struct inode *inode __unused, struct linux_file *f)
253 {
254 struct seq_file *seq;
255
256 seq = (struct seq_file *)f->private_data;
257 free(seq->private, M_LSEQ);
258 return (seq_release(inode, f));
259 }
260
261 int
single_release(struct vnode * v,struct linux_file * f)262 single_release(struct vnode *v, struct linux_file *f)
263 {
264 const struct seq_operations *op;
265 struct seq_file *m;
266 int rc;
267
268 /* be NULL safe */
269 if ((m = f->private_data) == NULL)
270 return (0);
271
272 op = m->op;
273 rc = seq_release(v, f);
274 free(__DECONST(void *, op), M_LSEQ);
275 return (rc);
276 }
277
278 void
lkpi_seq_vprintf(struct seq_file * m,const char * fmt,va_list args)279 lkpi_seq_vprintf(struct seq_file *m, const char *fmt, va_list args)
280 {
281 int ret;
282
283 ret = sbuf_vprintf(m->buf, fmt, args);
284 if (ret == 0)
285 m->size = sbuf_len(m->buf);
286 }
287
288 void
lkpi_seq_printf(struct seq_file * m,const char * fmt,...)289 lkpi_seq_printf(struct seq_file *m, const char *fmt, ...)
290 {
291 va_list args;
292
293 va_start(args, fmt);
294 lkpi_seq_vprintf(m, fmt, args);
295 va_end(args);
296 }
297
298 bool
seq_has_overflowed(struct seq_file * m)299 seq_has_overflowed(struct seq_file *m)
300 {
301 return (sbuf_len(m->buf) == -1);
302 }
303