1 /*
2  * Copyright (c) 2020 iXsystems, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/list.h>
32 #include <sys/mutex.h>
33 #include <sys/procfs_list.h>
34 
35 typedef struct procfs_list_iter {
36 	procfs_list_t *pli_pl;
37 	void *pli_elt;
38 } pli_t;
39 
40 void
41 seq_printf(struct seq_file *f, const char *fmt, ...)
42 {
43 	va_list adx;
44 
45 	va_start(adx, fmt);
46 	(void) vsnprintf(f->sf_buf, f->sf_size, fmt, adx);
47 	va_end(adx);
48 }
49 
50 static int
51 procfs_list_update(kstat_t *ksp, int rw)
52 {
53 	procfs_list_t *pl = ksp->ks_private;
54 
55 	if (rw == KSTAT_WRITE)
56 		pl->pl_clear(pl);
57 
58 	return (0);
59 }
60 
61 static int
62 procfs_list_data(char *buf, size_t size, void *data)
63 {
64 	pli_t *p;
65 	void *elt;
66 	procfs_list_t *pl;
67 	struct seq_file f;
68 
69 	p = data;
70 	pl = p->pli_pl;
71 	elt = p->pli_elt;
72 	free(p, M_TEMP);
73 	f.sf_buf = buf;
74 	f.sf_size = size;
75 	return (pl->pl_show(&f, elt));
76 }
77 
78 static void *
79 procfs_list_addr(kstat_t *ksp, loff_t n)
80 {
81 	procfs_list_t *pl = ksp->ks_private;
82 	void *elt = ksp->ks_private1;
83 	pli_t *p = NULL;
84 
85 
86 	if (n == 0)
87 		ksp->ks_private1 = list_head(&pl->pl_list);
88 	else if (elt)
89 		ksp->ks_private1 = list_next(&pl->pl_list, elt);
90 
91 	if (ksp->ks_private1) {
92 		p = malloc(sizeof (*p), M_TEMP, M_WAITOK);
93 		p->pli_pl = pl;
94 		p->pli_elt = ksp->ks_private1;
95 	}
96 
97 	return (p);
98 }
99 
100 void
101 procfs_list_install(const char *module,
102     const char *submodule,
103     const char *name,
104     mode_t mode,
105     procfs_list_t *procfs_list,
106     int (*show)(struct seq_file *f, void *p),
107     int (*show_header)(struct seq_file *f),
108     int (*clear)(procfs_list_t *procfs_list),
109     size_t procfs_list_node_off)
110 {
111 	kstat_t *procfs_kstat;
112 
113 	mutex_init(&procfs_list->pl_lock, NULL, MUTEX_DEFAULT, NULL);
114 	list_create(&procfs_list->pl_list,
115 	    procfs_list_node_off + sizeof (procfs_list_node_t),
116 	    procfs_list_node_off + offsetof(procfs_list_node_t, pln_link));
117 	procfs_list->pl_show = show;
118 	procfs_list->pl_show_header = show_header;
119 	procfs_list->pl_clear = clear;
120 	procfs_list->pl_next_id = 1;
121 	procfs_list->pl_node_offset = procfs_list_node_off;
122 
123 	procfs_kstat =  kstat_create(module, 0, name, submodule,
124 	    KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VIRTUAL);
125 
126 	if (procfs_kstat) {
127 		procfs_kstat->ks_lock = &procfs_list->pl_lock;
128 		procfs_kstat->ks_ndata = UINT32_MAX;
129 		procfs_kstat->ks_private = procfs_list;
130 		procfs_kstat->ks_update = procfs_list_update;
131 		kstat_set_seq_raw_ops(procfs_kstat, show_header,
132 		    procfs_list_data, procfs_list_addr);
133 		kstat_install(procfs_kstat);
134 		procfs_list->pl_private = procfs_kstat;
135 	}
136 }
137 
138 void
139 procfs_list_uninstall(procfs_list_t *procfs_list)
140 {}
141 
142 void
143 procfs_list_destroy(procfs_list_t *procfs_list)
144 {
145 	ASSERT(list_is_empty(&procfs_list->pl_list));
146 	kstat_delete(procfs_list->pl_private);
147 	list_destroy(&procfs_list->pl_list);
148 	mutex_destroy(&procfs_list->pl_lock);
149 }
150 
151 #define	NODE_ID(procfs_list, obj) \
152 		(((procfs_list_node_t *)(((char *)obj) + \
153 		(procfs_list)->pl_node_offset))->pln_id)
154 
155 void
156 procfs_list_add(procfs_list_t *procfs_list, void *p)
157 {
158 	ASSERT(MUTEX_HELD(&procfs_list->pl_lock));
159 	NODE_ID(procfs_list, p) = procfs_list->pl_next_id++;
160 	list_insert_tail(&procfs_list->pl_list, p);
161 }
162