xref: /freebsd/share/man/man9/module.9 (revision 8aac90f18aef7c9eea906c3ff9a001ca7b94f375)
1.\" -*- nroff -*-
2.\"
3.\" Copyright (c) 2000 Alexander Langer
4.\"
5.\" All rights reserved.
6.\"
7.\" This program is free software.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
19.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
22.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28.\"
29.Dd November 11, 2021
30.Dt MODULE 9
31.Os
32.Sh NAME
33.Nm module
34.Nd structure describing a kernel module
35.Sh DESCRIPTION
36Each module in the kernel is described by a
37.Vt module_t
38structure.
39The structure contains the name of the device, a unique ID number,
40a pointer to an event handler function and to an argument,
41which is given to the event handler,
42as well as some kernel internal data.
43If the event handler function is
44.Dv NULL ,
45the module will use a no-operation function handler instead.
46.Pp
47The
48.Xr DECLARE_MODULE 9
49macro
50registers the module with the system.
51.Pp
52When the module is loaded, the event handler function is called with
53the
54.Fa what
55argument set to
56.Dv MOD_LOAD .
57.Pp
58On unload it is first called with
59.Fa what
60set to
61.Dv MOD_QUIESCE .
62If the unload was not forced, a non-zero return will prevent the
63unload from happening.
64.Pp
65If the unload continues
66.Fa what
67is set to
68.Dv MOD_UNLOAD .
69If the module returns non-zero to this, the unload will not happen.
70.Pp
71The difference between
72.Dv MOD_QUIESCE
73and
74.Dv MOD_UNLOAD
75is that the module should fail
76.Dv MOD_QUIESCE
77if it is currently in use, whereas
78.Dv MOD_UNLOAD
79should only fail if it is impossible to unload the module, for instance
80because there are memory references to the module which cannot be revoked.
81.Pp
82When the system is shutting down,
83.Fa what
84contains the value of
85.Dv MOD_SHUTDOWN .
86.Pp
87The module should return
88.Er EOPNOTSUPP
89for unsupported and unrecognized values of
90.Fa what .
91.Sh EXAMPLES
92.Bd -literal
93#include <sys/param.h>
94#include <sys/kernel.h>
95#include <sys/module.h>
96
97static int foo_handler(module_t mod, int /*modeventtype_t*/ what,
98                       void *arg);
99
100static moduledata_t mod_data= {
101        "foo",
102        foo_handler,
103	NULL
104};
105
106MODULE_VERSION(foo, 1);
107MODULE_DEPEND(foo, bar, 1, 3, 4);
108
109DECLARE_MODULE(foo, mod_data, SI_SUB_EXEC, SI_ORDER_ANY);
110.Ed
111.Sh SEE ALSO
112.Xr DECLARE_MODULE 9 ,
113.Xr DEV_MODULE 9 ,
114.Xr DRIVER_MODULE 9 ,
115.Xr MODULE_DEPEND 9 ,
116.Xr MODULE_PNP_INFO 9 ,
117.Xr MODULE_VERSION 9 ,
118.Xr SYSCALL_MODULE 9
119.Pp
120.Pa /usr/share/examples/kld
121.Sh AUTHORS
122This manual page was written by
123.An Alexander Langer Aq Mt alex@FreeBSD.org .
124