1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
31 
32 #include <sys/types.h>
33 #include <sys/systm.h>
34 #include <sys/param.h>
35 #include <sys/sbuf.h>
36 #include <sys/syslog.h>
37 #include <sys/vnode.h>
38 
39 #include <linux/seq_file.h>
40 #include <linux/file.h>
41 
42 #undef file
43 MALLOC_DEFINE(M_LSEQ, "seq_file", "seq_file");
44 
45 ssize_t
46 seq_read(struct linux_file *f, char *ubuf, size_t size, off_t *ppos)
47 {
48 	struct seq_file *m;
49 	struct sbuf *sbuf;
50 	void *p;
51 	ssize_t rc;
52 
53 	m = f->private_data;
54 	sbuf = m->buf;
55 
56 	p = m->op->start(m, ppos);
57 	rc = m->op->show(m, p);
58 	if (rc)
59 		return (rc);
60 
61 	rc = sbuf_finish(sbuf);
62 	if (rc)
63 		return (rc);
64 
65 	rc = sbuf_len(sbuf);
66 	if (*ppos >= rc || size < 1)
67 		return (-EINVAL);
68 
69 	size = min(rc - *ppos, size);
70 	rc = strscpy(ubuf, sbuf_data(sbuf) + *ppos, size + 1);
71 
72 	/* add 1 for null terminator */
73 	if (rc > 0)
74 		rc += 1;
75 
76 	return (rc);
77 }
78 
79 int
80 seq_write(struct seq_file *seq, const void *data, size_t len)
81 {
82 
83 	return (sbuf_bcpy(seq->buf, data, len));
84 }
85 
86 /*
87  * This only needs to be a valid address for lkpi
88  * drivers it should never actually be called
89  */
90 off_t
91 seq_lseek(struct linux_file *file, off_t offset, int whence)
92 {
93 
94 	panic("%s not supported\n", __FUNCTION__);
95 	return (0);
96 }
97 
98 static void *
99 single_start(struct seq_file *p, off_t *pos)
100 {
101 
102 	return ((void *)(uintptr_t)(*pos == 0));
103 }
104 
105 static void *
106 single_next(struct seq_file *p, void *v, off_t *pos)
107 {
108 
109 	++*pos;
110 	return (NULL);
111 }
112 
113 static void
114 single_stop(struct seq_file *p, void *v)
115 {
116 }
117 
118 int
119 seq_open(struct linux_file *f, const struct seq_operations *op)
120 {
121 	struct seq_file *p;
122 
123 	if ((p = malloc(sizeof(*p), M_LSEQ, M_NOWAIT|M_ZERO)) == NULL)
124 		return (-ENOMEM);
125 
126 	p->buf = sbuf_new_auto();
127 	p->file = f;
128 	p->op = op;
129 	f->private_data = (void *) p;
130 	return (0);
131 }
132 
133 void *
134 __seq_open_private(struct linux_file *f, const struct seq_operations *op, int size)
135 {
136 	struct seq_file *seq_file;
137 	void *private;
138 	int error;
139 
140 	private = malloc(size, M_LSEQ, M_NOWAIT|M_ZERO);
141 	if (private == NULL)
142 		return (NULL);
143 
144 	error = seq_open(f, op);
145 	if (error < 0) {
146 		free(private, M_LSEQ);
147 		return (NULL);
148 	}
149 
150 	seq_file = (struct seq_file *)f->private_data;
151 	seq_file->private = private;
152 
153 	return (private);
154 }
155 
156 int
157 single_open(struct linux_file *f, int (*show)(struct seq_file *, void *), void *d)
158 {
159 	struct seq_operations *op;
160 	int rc = -ENOMEM;
161 
162 	op = malloc(sizeof(*op), M_LSEQ, M_NOWAIT);
163 	if (op) {
164 		op->start = single_start;
165 		op->next = single_next;
166 		op->stop = single_stop;
167 		op->show = show;
168 		rc = seq_open(f, op);
169 		if (rc)
170 			free(op, M_LSEQ);
171 		else
172 			((struct seq_file *)f->private_data)->private = d;
173 	}
174 	return (rc);
175 }
176 
177 int
178 seq_release(struct inode *inode __unused, struct linux_file *file)
179 {
180 	struct seq_file *m;
181 	struct sbuf *s;
182 
183 	m = file->private_data;
184 	s = m->buf;
185 
186 	sbuf_delete(s);
187 	free(m, M_LSEQ);
188 
189 	return (0);
190 }
191 
192 int
193 seq_release_private(struct inode *inode __unused, struct linux_file *f)
194 {
195 	struct seq_file *seq;
196 
197 	seq = (struct seq_file *)f->private_data;
198 	free(seq->private, M_LSEQ);
199 	return (seq_release(inode, f));
200 }
201 
202 int
203 single_release(struct vnode *v, struct linux_file *f)
204 {
205 	const struct seq_operations *op;
206 	struct seq_file *m;
207 	int rc;
208 
209 	/* be NULL safe */
210 	if ((m = f->private_data) == NULL)
211 		return (0);
212 
213 	op = m->op;
214 	rc = seq_release(v, f);
215 	free(__DECONST(void *, op), M_LSEQ);
216 	return (rc);
217 }
218 
219 void
220 lkpi_seq_vprintf(struct seq_file *m, const char *fmt, va_list args)
221 {
222 	sbuf_vprintf(m->buf, fmt, args);
223 }
224 
225 void
226 lkpi_seq_printf(struct seq_file *m, const char *fmt, ...)
227 {
228 	va_list args;
229 
230 	va_start(args, fmt);
231 	seq_vprintf(m, fmt, args);
232 	va_end(args);
233 }
234