1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
.\" $NetBSD: modctl.2,v 1.11 2015/05/09 08:10:01 pgoyette Exp $
.\"
.\" Copyright (c) 2009 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd May 9, 2015
.Dt MODCTL 2
.Os
.Sh NAME
.Nm modctl
.Nd module control
.Sh LIBRARY
.Lb libc
.Sh SYNOPSIS
.In sys/module.h
.Ft int
.Fn modctl "int operation" "void *argp"
.Sh DESCRIPTION
.Fn modctl
provides control over loaded kernel modules.
The argument
.Fa operation
is one of
.Dv MODCTL_LOAD ,
.Dv MODCTL_UNLOAD ,
.Dv MODCTL_STAT ,
or
.Dv MODCTL_EXISTS .
The argument
.Fa argp
depends on the
.Fa operation
to be performed.
.Pp
Operations are:
.Bl -tag -width MODCTL_UNLOAD
.It Dv MODCTL_LOAD
Load a module.
The
.Fa argp
argument should be a pointer to a
.Em modctl_load_t
structure, described below.
.It Dv MODCTL_UNLOAD
Unload a module.
In this case,
.Fa argp
should be a string containing the name of the module to be unloaded.
.It Dv MODCTL_STAT
Return a list of loaded modules.
In this case, the
.Fa argp
argument should be a
.Em struct iovec
pointing to a suitable block of memory.
The kernel will fill this block with an array of
.Em modstat_t
structures, one per loaded module.
If the block is not large enough, the data returned will be truncated
to fit.
The kernel will then update the
.Fa iov_len
member of the
.Em iovec
to reflect the size of the complete report, regardless of whether this
is larger or smaller than the size passed in.
.It Dv MODCTL_EXISTS
Test to see if the kernel was compiled with
.Dq options MODULAR
and whether or
not modules may be loaded at the moment.
In this case,
.Fa argp
should be an integer.
It should be
.Dq 0
to test if a user can load a module via
.Dv MODCTL_LOAD ,
or it should be
.Dq 1
to test if the system can autoload modules.
Note that this
test does not consider the sysctl
.Li kern.module.autoload .
.El
.Ss Data Types
The
.Em modctl_load_t
structure used with
.Dv MODCTL_LOAD
contains the following elements, which should be filled in by the caller:
.Bl -tag -width aaaaaaaa
.It Fa "const char *ml_filename"
The name/path of the module to load.
.It Fa "int ml_flags"
Zero or more of the following flag values:
.Bl -tag -compact -width "MODCTL_LOAD_FORCE"
.It Dv MODCTL_NO_PROP
Don't load
.Ao module Ac Ns Pa .plist .
.It Dv MODCTL_LOAD_FORCE
Ignore kernel version mismatch.
.El
.It Fa "const char *ml_props"
Externalized proplib dictionary to pass to module.
.It Fa "size_t ml_propslen"
Size of the dictionary blob.
.Fa ml_props
may be
.Dv NULL
in which case
.Fa ml_propslen
must be
.Dv 0 .
An upper limit of 4096 bytes is imposed on the value of ml_propslen.
Attempting to load a proplib dictionary larger than this size will return
.Er ENOMEM .
.El
.Pp
The
.Em modstat_t
structure used with
.Dv MODCTL_STAT
contains the following elements, which are filled in by the kernel:
.Bl -tag -width aaaaaaaa
.It Fa "char ms_name[MAXMODNAME]"
The name of the module.
.It Fa "char ms_required[MAXMODNAME * MAXMODDEPS]"
The list of modules required by this module
as a comma-delimited list of module names.
.It Fa "modsrc_t ms_source"
One of the following enumerated constants:
.Bl -tag -compact -width "MODULE_SOURCE_FILESYS"
.It Dv MODULE_SOURCE_KERNEL
The module is compiled into the kernel.
.It Dv MODULE_SOURCE_BOOT
The module was provided by the bootstrap loader.
.It Dv MODULE_SOURCE_FILESYS
The module was loaded from the file system.
.El
.It Fa "modclass_t ms_class"
One of the following enumerated constants:
.Bl -tag -compact -width "MODULE_SOURCE_FILESYS"
.It Dv MODULE_CLASS_SECMODEL
Security model.
.It Dv MODULE_CLASS_VFS
File system.
.It Dv MODULE_CLASS_DRIVER
Device driver.
.It Dv MODULE_CLASS_EXEC
Executable file format.
.It Dv MODULE_CLASS_MISC
Miscellaneous.
.It Dv MODULE_CLASS_ANY
Any module class.
.\" XXX: is MODULE_CLASS_ANY ever returned by this interface?
.El
.It Fa "uint64_t ms_addr"
The load address within the kernel.
.It Fa "u_int ms_size"
Loaded size of the module.
.It Fa "u_int ms_refcnt"
Current number of live references to this module.
.El
.Sh RETURN VALUES
Upon successful completion, the value returned is 0.
.Pp
Otherwise, a value of \-1 is returned and
.Va errno
is set to indicate the error.
.Sh ERRORS
.Fn modctl
will fail if:
.Bl -tag -width Er
.It Bq Er EBUSY
The argument
.Fa operation
is
.Dv MODCTL_UNLOAD
and the module is in use or the module is compiled into the kernel.
.It Bq Er EDEADLK
The argument
.Fa operation
is
.Dv MODCTL_LOAD
and there is a circular dependency in the module's dependency chain.
.It Bq Er EEXIST
The argument
.Fa operation
is
.Dv MODCTL_LOAD
and the module is already loaded.
.It Bq Er EFAULT
A bad address was given for
.Fa argp .
.It Bq Er EFBIG
The argument
.Fa operation
is
.Dv MODCTL_LOAD ,
the specified module resides in the file system, and the module's default
proplib file was too large.
.It Bq Er EINVAL
The argument
.Fa operation
is invalid.
.Pp
The argument
.Fa operation
is
.Dv MODCTL_LOAD
and ml_props is not
.Dv NULL
and
.Dq ml_propslen
is
.Dv 0 ,
or
ml_props is
.Dv NULL
and
.Dq ml_propslen
is not
.Dv 0 .
The kernel is unable to internalize the plist.
Or, there is a problem with the module or \*[Lt]module\*[Gt].plist.
.It Bq Er EMLINK
The argument
.Fa operation
is
.Dv MODCTL_LOAD
and the module has too many dependencies.
.It Bq Er ENAMETOOLONG
A module name/path is too long.
.It Bq Er ENOENT
The argument
.Fa operation
is
.Dv MODCTL_LOAD
and the module or a dependency can't be found.
The argument
.Fa operation
is
.Dv MODCTL_UNLOAD
and no module by the name of
.Fa argp
is loaded.
.It Bq Er ENOEXEC
The argument
.Fa operation
is
.Dv MODCTL_LOAD
and the module is not a valid object for the system.
Most likely, one or more undefined symbols could not be resolved by the
in-kernel linker.
.It Bq Er ENOMEM
There was not enough memory to perform the
.Fa operation .
.It Bq Er EPERM
Not allowed to perform the
.Fa operation .
.It Bq Er EPROGMISMATCH
The argument
.Fa operation
is
.Dv MODCTL_LOAD ,
the
.Fa ml_flags
field in the
.Em modctl_load_t
structure does not include
.Dv MODCTL_LOAD_FORCE ,
and the requested module does not match the current kernel's version
information.
.El
.Sh SEE ALSO
.Xr module 7 ,
.Xr sysctl 7 ,
.Xr module 9
.Sh HISTORY
The
.Fn modctl
function call first appeared in
.Nx 5.0 .
|