xref: /freebsd-13-stable/sys/compat/linuxkpi/common/src/linux_seq_file.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
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/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 
81 	return (sbuf_bcpy(seq->buf, data, len));
82 }
83 
84 /*
85  * This only needs to be a valid address for lkpi
86  * drivers it should never actually be called
87  */
88 off_t
seq_lseek(struct linux_file * file,off_t offset,int whence)89 seq_lseek(struct linux_file *file, off_t offset, int whence)
90 {
91 
92 	panic("%s not supported\n", __FUNCTION__);
93 	return (0);
94 }
95 
96 static void *
single_start(struct seq_file * p,off_t * pos)97 single_start(struct seq_file *p, off_t *pos)
98 {
99 
100 	return ((void *)(uintptr_t)(*pos == 0));
101 }
102 
103 static void *
single_next(struct seq_file * p,void * v,off_t * pos)104 single_next(struct seq_file *p, void *v, off_t *pos)
105 {
106 
107 	++*pos;
108 	return (NULL);
109 }
110 
111 static void
single_stop(struct seq_file * p,void * v)112 single_stop(struct seq_file *p, void *v)
113 {
114 }
115 
116 int
seq_open(struct linux_file * f,const struct seq_operations * op)117 seq_open(struct linux_file *f, const struct seq_operations *op)
118 {
119 	struct seq_file *p;
120 
121 	if ((p = malloc(sizeof(*p), M_LSEQ, M_NOWAIT|M_ZERO)) == NULL)
122 		return (-ENOMEM);
123 
124 	p->buf = sbuf_new_auto();
125 	p->file = f;
126 	p->op = op;
127 	f->private_data = (void *) p;
128 	return (0);
129 }
130 
131 void *
__seq_open_private(struct linux_file * f,const struct seq_operations * op,int size)132 __seq_open_private(struct linux_file *f, const struct seq_operations *op, int size)
133 {
134 	struct seq_file *seq_file;
135 	void *private;
136 	int error;
137 
138 	private = malloc(size, M_LSEQ, M_NOWAIT|M_ZERO);
139 	if (private == NULL)
140 		return (NULL);
141 
142 	error = seq_open(f, op);
143 	if (error < 0) {
144 		free(private, M_LSEQ);
145 		return (NULL);
146 	}
147 
148 	seq_file = (struct seq_file *)f->private_data;
149 	seq_file->private = private;
150 
151 	return (private);
152 }
153 
154 int
single_open(struct linux_file * f,int (* show)(struct seq_file *,void *),void * d)155 single_open(struct linux_file *f, int (*show)(struct seq_file *, void *), void *d)
156 {
157 	struct seq_operations *op;
158 	int rc = -ENOMEM;
159 
160 	op = malloc(sizeof(*op), M_LSEQ, M_NOWAIT);
161 	if (op) {
162 		op->start = single_start;
163 		op->next = single_next;
164 		op->stop = single_stop;
165 		op->show = show;
166 		rc = seq_open(f, op);
167 		if (rc)
168 			free(op, M_LSEQ);
169 		else
170 			((struct seq_file *)f->private_data)->private = d;
171 	}
172 	return (rc);
173 }
174 
175 int
seq_release(struct inode * inode __unused,struct linux_file * file)176 seq_release(struct inode *inode __unused, struct linux_file *file)
177 {
178 	struct seq_file *m;
179 	struct sbuf *s;
180 
181 	m = file->private_data;
182 	s = m->buf;
183 
184 	sbuf_delete(s);
185 	free(m, M_LSEQ);
186 
187 	return (0);
188 }
189 
190 int
seq_release_private(struct inode * inode __unused,struct linux_file * f)191 seq_release_private(struct inode *inode __unused, struct linux_file *f)
192 {
193 	struct seq_file *seq;
194 
195 	seq = (struct seq_file *)f->private_data;
196 	free(seq->private, M_LSEQ);
197 	return (seq_release(inode, f));
198 }
199 
200 int
single_release(struct vnode * v,struct linux_file * f)201 single_release(struct vnode *v, struct linux_file *f)
202 {
203 	const struct seq_operations *op;
204 	struct seq_file *m;
205 	int rc;
206 
207 	/* be NULL safe */
208 	if ((m = f->private_data) == NULL)
209 		return (0);
210 
211 	op = m->op;
212 	rc = seq_release(v, f);
213 	free(__DECONST(void *, op), M_LSEQ);
214 	return (rc);
215 }
216 
217 void
lkpi_seq_vprintf(struct seq_file * m,const char * fmt,va_list args)218 lkpi_seq_vprintf(struct seq_file *m, const char *fmt, va_list args)
219 {
220 	sbuf_vprintf(m->buf, fmt, args);
221 }
222 
223 void
lkpi_seq_printf(struct seq_file * m,const char * fmt,...)224 lkpi_seq_printf(struct seq_file *m, const char *fmt, ...)
225 {
226 	va_list args;
227 
228 	va_start(args, fmt);
229 	seq_vprintf(m, fmt, args);
230 	va_end(args);
231 }
232