xref: /src/sys/compat/linuxkpi/common/src/linux_seq_file.c (revision 2cf15144daf7ec44cdcd9bf3ef007939b79c361e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2016-2018, Matthew Macy <mmacy@freebsd.org>
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/types.h>
30 #include <sys/systm.h>
31 #include <sys/param.h>
32 #include <sys/sbuf.h>
33 #include <sys/syslog.h>
34 #include <sys/vnode.h>
35 
36 #include <linux/seq_file.h>
37 #include <linux/file.h>
38 
39 #undef file
40 MALLOC_DEFINE(M_LSEQ, "seq_file", "seq_file");
41 
42 ssize_t
seq_read(struct linux_file * f,char __user * ubuf,size_t size,off_t * ppos)43 seq_read(struct linux_file *f, char __user *ubuf, size_t size, off_t *ppos)
44 {
45 	struct seq_file *m;
46 	struct sbuf *sbuf;
47 	void *p;
48 	ssize_t rc;
49 
50 	m = f->private_data;
51 	sbuf = m->buf;
52 	sbuf_clear(sbuf);
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 	return (simple_read_from_buffer(ubuf, size, ppos, sbuf_data(sbuf),
64 	    sbuf_len(sbuf)));
65 }
66 
67 int
seq_write(struct seq_file * seq,const void * data,size_t len)68 seq_write(struct seq_file *seq, const void *data, size_t len)
69 {
70 	int ret;
71 
72 	ret = sbuf_bcpy(seq->buf, data, len);
73 	if (ret == 0)
74 		seq->size = sbuf_len(seq->buf);
75 
76 	return (ret);
77 }
78 
79 void
seq_putc(struct seq_file * seq,char c)80 seq_putc(struct seq_file *seq, char c)
81 {
82 	int ret;
83 
84 	ret = sbuf_putc(seq->buf, c);
85 	if (ret == 0)
86 		seq->size = sbuf_len(seq->buf);
87 }
88 
89 void
seq_puts(struct seq_file * seq,const char * str)90 seq_puts(struct seq_file *seq, const char *str)
91 {
92 	int ret;
93 
94 	ret = sbuf_printf(seq->buf, "%s", str);
95 	if (ret == 0)
96 		seq->size = sbuf_len(seq->buf);
97 }
98 
99 /*
100  * This only needs to be a valid address for lkpi
101  * drivers it should never actually be called
102  */
103 off_t
seq_lseek(struct linux_file * file,off_t offset,int whence)104 seq_lseek(struct linux_file *file, off_t offset, int whence)
105 {
106 
107 	panic("%s not supported\n", __FUNCTION__);
108 	return (0);
109 }
110 
111 static void *
single_start(struct seq_file * p,off_t * pos)112 single_start(struct seq_file *p, off_t *pos)
113 {
114 
115 	return ((void *)(uintptr_t)(*pos == 0));
116 }
117 
118 static void *
single_next(struct seq_file * p,void * v,off_t * pos)119 single_next(struct seq_file *p, void *v, off_t *pos)
120 {
121 
122 	++*pos;
123 	return (NULL);
124 }
125 
126 static void
single_stop(struct seq_file * p,void * v)127 single_stop(struct seq_file *p, void *v)
128 {
129 }
130 
131 static int
_seq_open_without_sbuf(struct linux_file * f,const struct seq_operations * op)132 _seq_open_without_sbuf(struct linux_file *f, const struct seq_operations *op)
133 {
134 	struct seq_file *p;
135 
136 	if ((p = malloc(sizeof(*p), M_LSEQ, M_NOWAIT|M_ZERO)) == NULL)
137 		return (-ENOMEM);
138 
139 	p->file = f;
140 	p->op = op;
141 	f->private_data = (void *) p;
142 	return (0);
143 }
144 
145 int
seq_open(struct linux_file * f,const struct seq_operations * op)146 seq_open(struct linux_file *f, const struct seq_operations *op)
147 {
148 	int ret;
149 
150 	ret = _seq_open_without_sbuf(f, op);
151 	if (ret == 0)
152 		((struct seq_file *)f->private_data)->buf = sbuf_new_auto();
153 
154 	return (ret);
155 }
156 
157 void *
__seq_open_private(struct linux_file * f,const struct seq_operations * op,int size)158 __seq_open_private(struct linux_file *f, const struct seq_operations *op, int size)
159 {
160 	struct seq_file *seq_file;
161 	void *private;
162 	int error;
163 
164 	private = malloc(size, M_LSEQ, M_NOWAIT|M_ZERO);
165 	if (private == NULL)
166 		return (NULL);
167 
168 	error = seq_open(f, op);
169 	if (error < 0) {
170 		free(private, M_LSEQ);
171 		return (NULL);
172 	}
173 
174 	seq_file = (struct seq_file *)f->private_data;
175 	seq_file->private = private;
176 
177 	return (private);
178 }
179 
180 static int
_single_open_without_sbuf(struct linux_file * f,int (* show)(struct seq_file *,void *),void * d)181 _single_open_without_sbuf(struct linux_file *f, int (*show)(struct seq_file *, void *), void *d)
182 {
183 	struct seq_operations *op;
184 	int rc = -ENOMEM;
185 
186 	op = malloc(sizeof(*op), M_LSEQ, M_NOWAIT);
187 	if (op) {
188 		op->start = single_start;
189 		op->next = single_next;
190 		op->stop = single_stop;
191 		op->show = show;
192 		rc = _seq_open_without_sbuf(f, op);
193 		if (rc)
194 			free(op, M_LSEQ);
195 		else
196 			((struct seq_file *)f->private_data)->private = d;
197 	}
198 	return (rc);
199 }
200 
201 int
single_open(struct linux_file * f,int (* show)(struct seq_file *,void *),void * d)202 single_open(struct linux_file *f, int (*show)(struct seq_file *, void *), void *d)
203 {
204 	int ret;
205 
206 	ret = _single_open_without_sbuf(f, show, d);
207 	if (ret == 0)
208 		((struct seq_file *)f->private_data)->buf = sbuf_new_auto();
209 
210 	return (ret);
211 }
212 
213 int
single_open_size(struct linux_file * f,int (* show)(struct seq_file *,void *),void * d,size_t size)214 single_open_size(struct linux_file *f, int (*show)(struct seq_file *, void *), void *d, size_t size)
215 {
216 	int ret;
217 
218 	ret = _single_open_without_sbuf(f, show, d);
219 	if (ret == 0)
220 		((struct seq_file *)f->private_data)->buf = sbuf_new(
221 		    NULL, NULL, size, SBUF_AUTOEXTEND);
222 
223 	return (ret);
224 }
225 
226 int
seq_release(struct inode * inode __unused,struct linux_file * file)227 seq_release(struct inode *inode __unused, struct linux_file *file)
228 {
229 	struct seq_file *m;
230 	struct sbuf *s;
231 
232 	m = file->private_data;
233 	s = m->buf;
234 
235 	sbuf_delete(s);
236 	free(m, M_LSEQ);
237 
238 	return (0);
239 }
240 
241 int
seq_release_private(struct inode * inode __unused,struct linux_file * f)242 seq_release_private(struct inode *inode __unused, struct linux_file *f)
243 {
244 	struct seq_file *seq;
245 
246 	seq = (struct seq_file *)f->private_data;
247 	free(seq->private, M_LSEQ);
248 	return (seq_release(inode, f));
249 }
250 
251 int
single_release(struct vnode * v,struct linux_file * f)252 single_release(struct vnode *v, struct linux_file *f)
253 {
254 	const struct seq_operations *op;
255 	struct seq_file *m;
256 	int rc;
257 
258 	/* be NULL safe */
259 	if ((m = f->private_data) == NULL)
260 		return (0);
261 
262 	op = m->op;
263 	rc = seq_release(v, f);
264 	free(__DECONST(void *, op), M_LSEQ);
265 	return (rc);
266 }
267 
268 void
lkpi_seq_vprintf(struct seq_file * m,const char * fmt,va_list args)269 lkpi_seq_vprintf(struct seq_file *m, const char *fmt, va_list args)
270 {
271 	int ret;
272 
273 	ret = sbuf_vprintf(m->buf, fmt, args);
274 	if (ret == 0)
275 		m->size = sbuf_len(m->buf);
276 }
277 
278 void
lkpi_seq_printf(struct seq_file * m,const char * fmt,...)279 lkpi_seq_printf(struct seq_file *m, const char *fmt, ...)
280 {
281 	va_list args;
282 
283 	va_start(args, fmt);
284 	lkpi_seq_vprintf(m, fmt, args);
285 	va_end(args);
286 }
287 
288 bool
seq_has_overflowed(struct seq_file * m)289 seq_has_overflowed(struct seq_file *m)
290 {
291 	return (sbuf_len(m->buf) == -1);
292 }
293