Wed Apr 17 18:10:27 2024 UTC (22d)
modules/examples/fopsmapper: KNF, sprinkle comments

Missing: MP-safety; this is a kind of broken example.


(riastradh)
diff -r1.2 -r1.3 src/sys/modules/examples/fopsmapper/cmd_mapper.c
diff -r1.2 -r1.3 src/sys/modules/examples/fopsmapper/fopsmapper.c

cvs diff -r1.2 -r1.3 src/sys/modules/examples/fopsmapper/cmd_mapper.c (expand / switch to unified diff)

--- src/sys/modules/examples/fopsmapper/cmd_mapper.c 2020/04/01 13:07:32 1.2
+++ src/sys/modules/examples/fopsmapper/cmd_mapper.c 2024/04/17 18:10:27 1.3
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: cmd_mapper.c,v 1.2 2020/04/01 13:07:32 kamil Exp $ */ 1/* $NetBSD: cmd_mapper.c,v 1.3 2024/04/17 18:10:27 riastradh Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2020 The NetBSD Foundation, Inc. 4 * Copyright (c) 2020 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -17,48 +17,47 @@ @@ -17,48 +17,47 @@
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29#include <sys/cdefs.h> 29#include <sys/cdefs.h>
30__RCSID("$NetBSD: cmd_mapper.c,v 1.2 2020/04/01 13:07:32 kamil Exp $"); 30__RCSID("$NetBSD: cmd_mapper.c,v 1.3 2024/04/17 18:10:27 riastradh Exp $");
31 31
32#include <sys/mman.h> 32#include <sys/mman.h>
33 33
34#include <err.h> 34#include <err.h>
35#include <fcntl.h> 35#include <fcntl.h>
36#include <stdio.h> 36#include <stdio.h>
37#include <stdlib.h> 37#include <stdlib.h>
38#include <unistd.h> 38#include <unistd.h>
39 39
40#define _PATH_DEV_MAPPER "/dev/fopsmapper" 40#define _PATH_DEV_MAPPER "/dev/fopsmapper"
41 41
42int main(int argc, char **argv) 42int main(int argc, char **argv)
43{ 43{
44 int devfd; 44 int devfd;
45 char *map = NULL; 45 char *map = NULL;
46 46
47 if ((devfd = open(_PATH_DEV_MAPPER, O_RDONLY)) == -1) 47 if ((devfd = open(_PATH_DEV_MAPPER, O_RDONLY)) == -1)
48 err(EXIT_FAILURE, "Cannot open %s", _PATH_DEV_MAPPER); 48 err(EXIT_FAILURE, "Cannot open %s", _PATH_DEV_MAPPER);
49 49
50 map = (char *)(mmap(0, sysconf(_SC_PAGESIZE), PROT_READ, MAP_SHARED, 50 map = mmap(0, sysconf(_SC_PAGESIZE), PROT_READ, MAP_SHARED, devfd, 0);
51 devfd, 0)); 
52 if (map == MAP_FAILED) 51 if (map == MAP_FAILED)
53 err(EXIT_FAILURE, "Mapping failed"); 52 err(EXIT_FAILURE, "Mapping failed");
54 53
55 printf("Message from device: %s\n",map); 54 printf("Message from device: %s\n", map);
56 55
57 if (munmap(map, sysconf(_SC_PAGESIZE)) == -1) 56 if (munmap(map, sysconf(_SC_PAGESIZE)) == -1)
58 err(EXIT_FAILURE, "Unmap failed"); 57 err(EXIT_FAILURE, "Unmap failed");
59 58
60 if (close(devfd) == -1) 59 if (close(devfd) == -1)
61 err(EXIT_FAILURE, "Cannot close %s", _PATH_DEV_MAPPER); 60 err(EXIT_FAILURE, "Cannot close %s", _PATH_DEV_MAPPER);
62 61
63 return EXIT_SUCCESS; 62 return EXIT_SUCCESS;
64} 63}

cvs diff -r1.2 -r1.3 src/sys/modules/examples/fopsmapper/fopsmapper.c (expand / switch to unified diff)

--- src/sys/modules/examples/fopsmapper/fopsmapper.c 2020/04/01 11:45:53 1.2
+++ src/sys/modules/examples/fopsmapper/fopsmapper.c 2024/04/17 18:10:27 1.3
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: fopsmapper.c,v 1.2 2020/04/01 11:45:53 kamil Exp $ */ 1/* $NetBSD: fopsmapper.c,v 1.3 2024/04/17 18:10:27 riastradh Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2020 The NetBSD Foundation, Inc. 4 * Copyright (c) 2020 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -17,168 +17,172 @@ @@ -17,168 +17,172 @@
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29#include <sys/cdefs.h> 29#include <sys/cdefs.h>
30__KERNEL_RCSID(0, "$NetBSD: fopsmapper.c,v 1.2 2020/04/01 11:45:53 kamil Exp $"); 30__KERNEL_RCSID(0, "$NetBSD: fopsmapper.c,v 1.3 2024/04/17 18:10:27 riastradh Exp $");
31 31
32#include <sys/module.h> 32#include <sys/types.h>
33#include <sys/param.h> 
34#include <sys/systm.h> 
35#include <sys/kernel.h> 
36 33
37#include <sys/conf.h> 34#include <sys/conf.h>
38#include <sys/file.h> 35#include <sys/file.h>
39#include <sys/filedesc.h> 36#include <sys/filedesc.h>
 37#include <sys/kernel.h>
40#include <sys/kmem.h> 38#include <sys/kmem.h>
41#include <sys/mman.h> 39#include <sys/mman.h>
 40#include <sys/module.h>
42#include <sys/mutex.h> 41#include <sys/mutex.h>
 42#include <sys/param.h>
 43#include <sys/systm.h>
 44
43#include <uvm/uvm_extern.h> 45#include <uvm/uvm_extern.h>
44 46
45/* 47/*
46 * To use this module you need to: 48 * To use this module you need to:
47 * 49 *
48 * mknod /dev/fopsmapper c 351 0 50 * mknod /dev/fopsmapper c 351 0
49 * 
50 */ 51 */
51 52
52dev_type_open(fopsmapper_open); 53dev_open_t fopsmapper_open;
53 54
54const struct cdevsw fopsmapper_cdevsw = { 55const struct cdevsw fopsmapper_cdevsw = {
55 .d_open = fopsmapper_open, 56 .d_open = fopsmapper_open,
56 .d_close = noclose, 57 .d_close = noclose,
57 .d_read = noread, 58 .d_read = noread,
58 .d_write = nowrite, 59 .d_write = nowrite,
59 .d_ioctl = noioctl, 60 .d_ioctl = noioctl,
60 .d_stop = nostop, 61 .d_stop = nostop,
61 .d_tty = notty, 62 .d_tty = notty,
62 .d_poll = nopoll, 63 .d_poll = nopoll,
63 .d_mmap = nommap, 64 .d_mmap = nommap,
64 .d_kqfilter = nokqfilter, 65 .d_kqfilter = nokqfilter,
65 .d_discard = nodiscard, 66 .d_discard = nodiscard,
66 .d_flag = D_OTHER 67 .d_flag = D_OTHER
67}; 68};
68 69
69static int fopsmapper_mmap(file_t *, off_t *, size_t, 70static int fopsmapper_mmap(struct file *, off_t *, size_t, int, int *, int *,
70 int, int *, int *,struct uvm_object **, int *); 71 struct uvm_object **, int *);
71static int fopsmapper_close(file_t *); 72static int fopsmapper_close(struct file *);
72 73
73const struct fileops mapper_fileops = { 74const struct fileops mapper_fileops = {
74 .fo_read = fbadop_read, 75 .fo_read = fbadop_read,
75 .fo_write = fbadop_write, 76 .fo_write = fbadop_write,
76 .fo_ioctl = fbadop_ioctl, 77 .fo_ioctl = fbadop_ioctl,
77 .fo_fcntl = fnullop_fcntl, 78 .fo_fcntl = fnullop_fcntl,
78 .fo_poll = fnullop_poll, 79 .fo_poll = fnullop_poll,
79 .fo_stat = fbadop_stat, 80 .fo_stat = fbadop_stat,
80 .fo_close = fopsmapper_close, 81 .fo_close = fopsmapper_close,
81 .fo_kqfilter = fnullop_kqfilter, 82 .fo_kqfilter = fnullop_kqfilter,
82 .fo_restart = fnullop_restart, 83 .fo_restart = fnullop_restart,
83 .fo_mmap = fopsmapper_mmap, 84 .fo_mmap = fopsmapper_mmap,
84}; 85};
85 86
86typedef struct fopsmapper_softc { 87struct fopsmapper_softc {
87 char *buf; 88 char *buf;
88 struct uvm_object *uobj; 89 struct uvm_object *uobj;
89 size_t bufsize; 90 size_t bufsize;
90} fops_t; 91};
91 92
92int 93int
93fopsmapper_open(dev_t dev, int flag, int mode, struct lwp *l) 94fopsmapper_open(dev_t dev, int flag, int mode, struct lwp *l)
94{ 95{
95 fops_t *fo; 96 struct fopsmapper_softc *fo;
96 struct file *fp; 97 struct file *fp;
97 int fd, error; 98 int fd, error;
98 99
99 if ((error = fd_allocfile(&fp, &fd)) != 0) 100 if ((error = fd_allocfile(&fp, &fd)) != 0)
100 return error; 101 return error;
101 102
102 fo = kmem_zalloc(sizeof(*fo), KM_SLEEP); 103 fo = kmem_zalloc(sizeof(*fo), KM_SLEEP);
103 104
104 return fd_clone(fp, fd, flag, &mapper_fileops, fo); 105 return fd_clone(fp, fd, flag, &mapper_fileops, fo);
105} 106}
106 107
107int 108int
108fopsmapper_mmap(file_t * fp, off_t * offp, size_t size, int prot, 109fopsmapper_mmap(struct file *fp, off_t *offp, size_t size, int prot,
109 int *flagsp, int *advicep, struct uvm_object **uobjp, 110 int *flagsp, int *advicep, struct uvm_object **uobjp, int *maxprotp)
110 int *maxprotp) 
111{ 111{
112 fops_t *fo; 112 struct fopsmapper_softc *fo;
 113 vaddr_t va;
113 int error; 114 int error;
114 115
115 if (prot & PROT_EXEC) 116 if (prot & PROT_EXEC)
116 return EACCES; 117 return EACCES;
117 118
118 if (size != (size_t)PAGE_SIZE) 119 if (size != (size_t)PAGE_SIZE)
119 return EINVAL; 120 return EINVAL;
120 121
121 if ((fo = fp->f_data) == NULL) 122 if ((fo = fp->f_data) == NULL)
122 return ENXIO; 123 return ENXIO;
123 124
124 fo->bufsize = size; 125 fo->bufsize = size;
125 fo->uobj = uao_create(size, 0); 126 fo->uobj = uao_create(size, 0);
126 127
127 fo->buf = NULL; 128 fo->buf = NULL;
128 /* Map the uvm object into kernel */ 
129 error = uvm_map(kernel_map, (vaddr_t *) &fo->buf, fo->bufsize, 
130 fo->uobj, 0, 0, 
131 UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW, 
132 UVM_INH_SHARE,UVM_ADV_RANDOM, 0)); 
133 129
 130 /*
 131 * Map the uvm object into kernel. Consumes our reference to
 132 * uobj on success.
 133 */
 134 error = uvm_map(kernel_map, &va, fo->bufsize, fo->uobj, 0, 0,
 135 UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW,
 136 UVM_INH_SHARE, UVM_ADV_RANDOM, 0));
134 if (error) { 137 if (error) {
135 uao_detach(fo->uobj); 138 uao_detach(fo->uobj);
136 return error; 139 goto out;
137 } 140 }
 141 fo->buf = (char *)va;
138 snprintf(fo->buf, 13, "Hey There!"); 142 snprintf(fo->buf, 13, "Hey There!");
139 143
140 /* Get the reference of uobj */ 144 /* Get the reference of uobj */
141 uao_reference(fo->uobj); 145 uao_reference(fo->uobj);
142 
143 *uobjp = fo->uobj; 146 *uobjp = fo->uobj;
144 *maxprotp = prot; 147 *maxprotp = prot;
145 *advicep = UVM_ADV_RANDOM; 148 *advicep = UVM_ADV_RANDOM;
 149 error = 0;
146 150
147 return 0; 151out: return error;
148} 152}
149 153
150int 154int
151fopsmapper_close(file_t * fp) 155fopsmapper_close(struct file *fp)
152{ 156{
153 fops_t *fo; 157 struct fopsmapper_softc *fo;
154 158
155 fo = fp->f_data; 159 fo = fp->f_data;
156 KASSERT(fo != NULL); 160 KASSERT(fo != NULL);
157 161
158 if (fo->buf != NULL) 162 if (fo->buf != NULL)
159 uvm_deallocate(kernel_map, (vaddr_t)fo->buf, fo->bufsize); 163 uvm_deallocate(kernel_map, (vaddr_t)fo->buf, fo->bufsize);
160 164
161 kmem_free(fo, sizeof(*fo)); 165 kmem_free(fo, sizeof(*fo));
162 166
163 return 0; 167 return 0;
164} 168}
165 169
166MODULE(MODULE_CLASS_MISC, fopsmapper, NULL); 170MODULE(MODULE_CLASS_MISC, fopsmapper, NULL);
167 171
168static int 172static int
169fopsmapper_modcmd(modcmd_t cmd, void *arg __unused) 173fopsmapper_modcmd(modcmd_t cmd, void *arg __unused)
170{ 174{
171 int cmajor = 351, bmajor = -1; 175 int cmajor = 351, bmajor = -1;
172 176
173 switch (cmd) { 177 switch (cmd) {
174 case MODULE_CMD_INIT: 178 case MODULE_CMD_INIT:
175 if (devsw_attach("fopsmapper", NULL, &bmajor, 179 if (devsw_attach("fopsmapper", NULL, &bmajor,
176 &fopsmapper_cdevsw, &cmajor)) 180 &fopsmapper_cdevsw, &cmajor))
177 return ENXIO; 181 return ENXIO;
178 return 0; 182 return 0;
179 case MODULE_CMD_FINI: 183 case MODULE_CMD_FINI:
180 return EBUSY; 184 return EBUSY;
181 default: 185 default:
182 return ENOTTY; 186 return ENOTTY;
183 } 187 }
184} 188}