2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2016-2018, Matthew Macy <mmacy@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
33 #include <sys/systm.h>
34 #include <sys/param.h>
36 #include <sys/syslog.h>
37 #include <sys/vnode.h>
39 #include <linux/seq_file.h>
40 #include <linux/file.h>
43 MALLOC_DEFINE(M_LSEQ, "seq_file", "seq_file");
46 seq_read(struct linux_file *f, char *ubuf, size_t size, off_t *ppos)
48 struct seq_file *m = f->private_data;
53 p = m->op->start(m, &pos);
54 rc = m->op->show(m, p);
61 seq_write(struct seq_file *seq, const void *data, size_t len)
64 return (sbuf_bcpy(seq->buf, data, len));
68 * This only needs to be a valid address for lkpi
69 * drivers it should never actually be called
72 seq_lseek(struct linux_file *file, off_t offset, int whence)
75 panic("%s not supported\n", __FUNCTION__);
80 single_start(struct seq_file *p, off_t *pos)
83 return ((void *)(uintptr_t)(*pos == 0));
87 single_next(struct seq_file *p, void *v, off_t *pos)
95 single_stop(struct seq_file *p, void *v)
100 seq_open(struct linux_file *f, const struct seq_operations *op)
104 if (f->private_data != NULL)
105 log(LOG_WARNING, "%s private_data not NULL", __func__);
107 if ((p = malloc(sizeof(*p), M_LSEQ, M_NOWAIT|M_ZERO)) == NULL)
117 single_open(struct linux_file *f, int (*show)(struct seq_file *, void *), void *d)
119 struct seq_operations *op;
122 op = malloc(sizeof(*op), M_LSEQ, M_NOWAIT);
124 op->start = single_start;
125 op->next = single_next;
126 op->stop = single_stop;
128 rc = seq_open(f, op);
132 ((struct seq_file *)f->private_data)->private = d;
139 seq_release(struct inode *inode __unused, struct linux_file *file)
143 m = file->private_data;
149 single_release(struct vnode *v, struct linux_file *f)
151 const struct seq_operations *op = ((struct seq_file *)f->private_data)->op;
154 rc = seq_release(v, f);
155 free(__DECONST(void *, op), M_LSEQ);