Merge branch 'vendor/DHCPCD'
[dragonfly.git] / share / man / man9 / make_autoclone_dev.9
1 .\"
2 .\" Copyright (c) 2009
3 .\"     The DragonFly Project.  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 .\"
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
13 .\"    the documentation and/or other materials provided with the
14 .\"    distribution.
15 .\" 3. Neither the name of The DragonFly Project nor the names of its
16 .\"    contributors may be used to endorse or promote products derived
17 .\"    from this software without specific, prior written permission.
18 .\"
19 .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 .\" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 .\" FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
23 .\" COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 .\" INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 .\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 .\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 .\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 .\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 .\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" SUCH DAMAGE.
31 .\"
32 .Dd August 22, 2019
33 .Dt MAKE_AUTOCLONE_DEV 9
34 .Os
35 .Sh NAME
36 .Nm make_autoclone_dev ,
37 .Nm destroy_autoclone_dev ,
38 .Nm devfs_clone_bitmap_init ,
39 .Nm devfs_clone_bitmap_uninit ,
40 .Nm devfs_clone_bitmap_chk ,
41 .Nm devfs_clone_bitmap_set ,
42 .Nm devfs_clone_bitmap_get ,
43 .Nm devfs_clone_bitmap_put ,
44 .Nm DEVFS_DECLARE_CLONE_BITMAP ,
45 .Nm DEVFS_DEFINE_CLONE_BITMAP ,
46 .Nm DEVFS_CLONE_BITMAP
47 .Nd device clone functions
48 .Sh SYNOPSIS
49 .In sys/types.h
50 .In sys/conf.h
51 .In sys/devfs.h
52 .Ft cdev_t
53 .Fn make_autoclone_dev "struct dev_ops *ops" "struct devfs_bitmap *bitmap" "d_clone_t *nhandler" "uid_t uid" "gid_t gid" "int perms" "const char *fmt" "..."
54 .Ft void
55 .Fn destroy_autoclone_dev "cdev_t dev" "struct devfs_bitmap *bitmap"
56 .Ft void
57 .Fn devfs_clone_bitmap_init "struct devfs_bitmap *bitmap"
58 .Ft void
59 .Fn devfs_clone_bitmap_uninit "struct devfs_bitmap *bitmap"
60 .Ft int
61 .Fn devfs_clone_bitmap_chk "struct devfs_bitmap *bitmap" "int unit"
62 .Ft int
63 .Fn devfs_clone_bitmap_set "struct devfs_bitmap *bitmap" "int unit"
64 .Ft int
65 .Fn devfs_clone_bitmap_get "struct devfs_bitmap *bitmap" "int limit"
66 .Ft void
67 .Fn devfs_clone_bitmap_put "struct devfs_bitmap *bitmap" "int unit"
68 .Fn DEVFS_DECLARE_CLONE_BITMAP "name"
69 .Fn DEVFS_DEFINE_CLONE_BITMAP "name"
70 .Fn DEVFS_CLONE_BITMAP "name"
71 .Sh DESCRIPTION
72 .Fn make_autoclone_dev
73 creates a
74 .Vt cdev_t
75 with the default
76 .Fa ops ,
77 visible in the
78 .Xr devfs 5
79 namespace, that (when opened) will invoke the clone handler specified by
80 .Fa nhandler .
81 If a
82 .Vt devfs_bitmap *
83 is specified, the given
84 .Fa bitmap
85 is initialized using
86 .Fn devfs_clone_bitmap_init .
87 .Pp
88 The clone handler must be defined as follows:
89 .Bd -literal
90 d_clone_t mydev_clone;
91
92 int
93 mydev_clone(struct dev_clone_args *ap)
94 {
95 };
96 .Ed
97 .Pp
98 When called, the handler is passed a pointer to a populated
99 .Vt dev_clone_args
100 structure, which is defined as follows:
101 .Bd -literal
102 struct dev_clone_args {
103         struct dev_generic_args a_head;
104         struct cdev     *a_dev;
105         const char      *a_name;
106         size_t           a_namelen;
107         struct ucred    *a_cred;
108         int              a_mode;
109 };
110 .Ed
111 .Pp
112 .Fa a_head.a_dev
113 is the
114 .Vt cdev_t
115 of the accessed autoclone device.
116 .Fa a_name
117 and
118 .Fa a_namelen
119 are the registered clonable base name and its length, as specified to
120 .Fn make_autoclone_dev .
121 .Fa a_mode
122 and
123 .Fa a_cred
124 contain the mode and cred passed to the autoclone device's
125 .Fn open .
126 .Pp
127 The clone handler must set
128 .Fa a_dev
129 to a new
130 .Vt cdev_t ,
131 returned by a call to
132 .Fn make_only_dev .
133 .Fa a_dev
134 may also be set to
135 .Dv NULL ,
136 in which case the
137 .Fn open
138 will ultimately fail with
139 .Er ENXIO .
140 If
141 .Fa a_dev
142 is
143 .Pf non- Ns Dv NULL ,
144 it is automatically made visible and linked into
145 .Xr devfs 5
146 as if it was created with
147 .Fn make_dev .
148 Thus,
149 .Fn destroy_dev
150 should be used to destroy the cloned
151 .Vt cdev_t
152 once it is no longer required,
153 usually during
154 .Fn close .
155 .Pp
156 .Fn destroy_autoclone_dev
157 destroys a
158 .Vt cdev_t
159 created by
160 .Fn make_autoclone_dev
161 unregistering its clone handler and (if non-NULL) also uninitializes its
162 .Fa bitmap
163 using
164 .Fn devfs_clone_bitmap_uninit .
165 .Pp
166 .Fn devfs_clone_bitmap_init
167 initializes the given clone
168 .Fa bitmap
169 so it is ready to use.
170 .Pp
171 .Fn devfs_clone_bitmap_uninit
172 frees the memory associated with the specified clone
173 .Fa bitmap
174 and leaves it unusable.
175 .Pp
176 .Fn devfs_clone_bitmap_chk
177 checks if
178 .Fa unit
179 is in use (set) and returns 1 if it is; otherwise 0 is returned.
180 .Pp
181 .Fn devfs_clone_bitmap_set
182 marks
183 .Fa unit
184 in
185 .Fa bitmap
186 as used, so further calls to
187 .Fn devfs_clone_bitmap_get
188 cannot retrieve it.
189 If one intends to use a clone handler along with preallocated devices, it
190 is recommended to block the unit numbers of the preallocated devices by
191 calling
192 .Fn devfs_clone_bitmap_set
193 on them.
194 The function returns -1 if the unit is already allocated, otherwise 0.
195 .Pp
196 .Fn devfs_clone_bitmap_put
197 marks
198 .Fa unit
199 in the
200 .Fa bitmap
201 as unused so further calls to
202 .Fn devfs_clone_bitmap_fff
203 or
204 .Fn devfs_clone_bitmap_get
205 can retrieve it again.
206 It is recommended that the associated device be destroyed prior to
207 making the
208 .Fa unit
209 available in the bitmap again, to avoid racing against a new clone.
210 .Pp
211 .Fn devfs_clone_bitmap_get
212 will return the first unused unit number and also mark it as used
213 in the
214 .Fa bitmap .
215 .Pp
216 The
217 .Fn DEVFS_DEFINE_CLONE_BITMAP
218 macro defines a clone bitmap with the specified
219 .Fa name .
220 As long as the name specified is unique, this macro can be used to define
221 global variables.
222 Similarly,
223 .Fn DEVFS_DECLARE_CLONE_BITMAP
224 declares a clone bitmap.
225 .Pp
226 The
227 .Fn DEVFS_CLONE_BITMAP
228 is a macro which expands the specified
229 .Fa name
230 to the full name of a clone bitmap.
231 It is used in conjunction with
232 .Fn DEVFS_DEFINE_CLONE_BITMAP
233 and
234 .Fn DEVFS_DECLARE_CLONE_BITMAP ,
235 as it uses the same name.
236 .Sh HISTORY
237 The
238 .Xr devfs 5
239 clone facilities and the associated functions all appeared in
240 .Dx 2.3 .
241 .Sh AUTHORS
242 .An Alex Hornung