Source string Read only

(itstool) path: example/title
69/690
Context English State
The kld interface is used through:
<command>kldload</command> - loads a new kernel module
<command>kldunload</command> - unloads a kernel module
<command>kldstat</command> - lists loaded modules
Skeleton Layout of a kernel module
/*
* KLD Skeleton
* Inspired by Andrew Reiter's Daemonnews article
*/

#include &lt;sys/types.h&gt;
#include &lt;sys/module.h&gt;
#include &lt;sys/systm.h&gt; /* uprintf */
#include &lt;sys/errno.h&gt;
#include &lt;sys/param.h&gt; /* defines used in kernel.h */
#include &lt;sys/kernel.h&gt; /* types used in module initialization */

/*
* Load handler that deals with the loading and unloading of a KLD.
*/

static int
skel_loader(struct module *m, int what, void *arg)
{
int err = 0;

switch (what) {
case MOD_LOAD: /* kldload */
uprintf("Skeleton KLD loaded.\n");
break;
case MOD_UNLOAD:
uprintf("Skeleton KLD unloaded.\n");
break;
default:
err = EOPNOTSUPP;
break;
}
return(err);
}

/* Declare this module to the rest of the kernel */

static moduledata_t skel_mod = {
"skel",
skel_loader,
NULL
};

DECLARE_MODULE(skeleton, skel_mod, SI_SUB_KLD, SI_ORDER_ANY);
Makefile
FreeBSD provides a system makefile to simplify compiling a kernel module.
SRCS=skeleton.c
KMOD=skeleton

.include &lt;bsd.kmod.mk&gt;
Running <command>make</command> with this makefile will create a file <filename>skeleton.ko</filename> that can be loaded into the kernel by typing:
<prompt>#</prompt> <userinput>kldload -v ./skeleton.ko</userinput>
Character Devices
<primary>character devices</primary>
A character device driver is one that transfers data directly to and from a user process. This is the most common type of device driver and there are plenty of simple examples in the source tree.
This simple example pseudo-device remembers whatever values are written to it and can then echo them back when read.
Example of a Sample Echo Pseudo-Device Driver for FreeBSD 10.X - 12.X
/*
* Simple Echo pseudo-device KLD
*
* Murray Stokely
* Søren (Xride) Straarup
* Eitan Adler
*/

#include &lt;sys/types.h&gt;
#include &lt;sys/module.h&gt;
#include &lt;sys/systm.h&gt; /* uprintf */
#include &lt;sys/param.h&gt; /* defines used in kernel.h */
#include &lt;sys/kernel.h&gt; /* types used in module initialization */
#include &lt;sys/conf.h&gt; /* cdevsw struct */
#include &lt;sys/uio.h&gt; /* uio struct */
#include &lt;sys/malloc.h&gt;

#define BUFFERSIZE 255

/* Function prototypes */
static d_open_t echo_open;
static d_close_t echo_close;
static d_read_t echo_read;
static d_write_t echo_write;

/* Character device entry points */
static struct cdevsw echo_cdevsw = {
.d_version = D_VERSION,
.d_open = echo_open,
.d_close = echo_close,
.d_read = echo_read,
.d_write = echo_write,
.d_name = "echo",
};

struct s_echo {
char msg[BUFFERSIZE + 1];
int len;
};

/* vars */
static struct cdev *echo_dev;
static struct s_echo *echomsg;

MALLOC_DECLARE(M_ECHOBUF);
MALLOC_DEFINE(M_ECHOBUF, "echobuffer", "buffer for echo module");

/*
* This function is called by the kld[un]load(2) system calls to
* determine what actions to take when a module is loaded or unloaded.
*/
static int
echo_loader(struct module *m __unused, int what, void *arg __unused)
{
int error = 0;

switch (what) {
case MOD_LOAD: /* kldload */
error = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK,
&amp;echo_dev,
&amp;echo_cdevsw,
0,
UID_ROOT,
GID_WHEEL,
0600,
"echo");
if (error != 0)
break;

echomsg = malloc(sizeof(*echomsg), M_ECHOBUF, M_WAITOK |
M_ZERO);
printf("Echo device loaded.\n");
break;
case MOD_UNLOAD:
destroy_dev(echo_dev);
free(echomsg, M_ECHOBUF);
printf("Echo device unloaded.\n");
break;
default:
error = EOPNOTSUPP;
break;
}
return (error);
}

static int
echo_open(struct cdev *dev __unused, int oflags __unused, int devtype __unused,
struct thread *td __unused)
{
int error = 0;

uprintf("Opened device \"echo\" successfully.\n");
return (error);
}

static int
echo_close(struct cdev *dev __unused, int fflag __unused, int devtype __unused,
struct thread *td __unused)
{

uprintf("Closing device \"echo\".\n");
return (0);
}

/*
* The read function just takes the buf that was saved via
* echo_write() and returns it to userland for accessing.
* uio(9)
*/
static int
echo_read(struct cdev *dev __unused, struct uio *uio, int ioflag __unused)
{
size_t amt;
int error;

/*
* How big is this read operation? Either as big as the user wants,
* or as big as the remaining data. Note that the 'len' does not
* include the trailing null character.
*/
amt = MIN(uio-&gt;uio_resid, uio-&gt;uio_offset &gt;= echomsg-&gt;len + 1 ? 0 :
echomsg-&gt;len + 1 - uio-&gt;uio_offset);

if ((error = uiomove(echomsg-&gt;msg, amt, uio)) != 0)
uprintf("uiomove failed!\n");

return (error);
}

/*
* echo_write takes in a character string and saves it
* to buf for later accessing.
*/
static int
echo_write(struct cdev *dev __unused, struct uio *uio, int ioflag __unused)
{
size_t amt;
int error;

/*
* We either write from the beginning or are appending -- do
* not allow random access.
*/
if (uio-&gt;uio_offset != 0 &amp;&amp; (uio-&gt;uio_offset != echomsg-&gt;len))
return (EINVAL);

/* This is a new message, reset length */
if (uio-&gt;uio_offset == 0)
echomsg-&gt;len = 0;

/* Copy the string in from user memory to kernel memory */
amt = MIN(uio-&gt;uio_resid, (BUFFERSIZE - echomsg-&gt;len));

error = uiomove(echomsg-&gt;msg + uio-&gt;uio_offset, amt, uio);

/* Now we need to null terminate and record the length */
echomsg-&gt;len = uio-&gt;uio_offset;
echomsg-&gt;msg[echomsg-&gt;len] = 0;

if (error != 0)
uprintf("Write failed: bad address!\n");
return (error);
}

DEV_MODULE(echo, echo_loader, NULL);
With this driver loaded try:
<prompt>#</prompt> <userinput>echo -n "Test Data" &gt; /dev/echo</userinput>
<prompt>#</prompt> <userinput>cat /dev/echo</userinput>
Opened device "echo" successfully.
Test Data
Closing device "echo".
Real hardware devices are described in the next chapter.
Block Devices (Are Gone)
<primary>block devices</primary>
Other <trademark class="registered">UNIX</trademark> systems may support a second type of disk device known as block devices. Block devices are disk devices for which the kernel provides caching. This caching makes block-devices almost unusable, or at least dangerously unreliable. The caching will reorder the sequence of write operations, depriving the application of the ability to know the exact disk contents at any one instant in time.
This makes predictable and reliable crash recovery of on-disk data structures (filesystems, databases, etc.) impossible. Since writes may be delayed, there is no way the kernel can report to the application which particular write operation encountered a write error, this further compounds the consistency problem.
For this reason, no serious applications rely on block devices, and in fact, almost all applications which access disks directly take great pains to specify that character (or <quote>raw</quote>) devices should always be used. As the implementation of the aliasing of each disk (partition) to two devices with different semantics significantly complicated the relevant kernel code, FreeBSD dropped support for cached disk devices as part of the modernization of the disk I/O infrastructure.
Network Drivers
<primary>network devices</primary>
Drivers for network devices do not use device nodes in order to be accessed. Their selection is based on other decisions made inside the kernel and instead of calling open(), use of a network device is generally introduced by using the system call socket(2).
For more information see ifnet(9), the source of the loopback device, and Bill Paul's network drivers.
ISA Device Drivers
<personname><firstname>Sergey</firstname><surname>Babkin</surname></personname><contrib>Written by </contrib>

Loading…

User avatar None

New source string

FreeBSD Doc / books_arch-handbookEnglish

New source string 9 months ago
Browse all component changes

Things to check

Long untranslated

The string has not been translated for a long time

Reset

Glossary

English English
No related strings found in the glossary.

Source information

Source string comment
(itstool) path: example/title
Flags
read-only
Source string location
book.translate.xml:16259
String age
9 months ago
Source string age
9 months ago
Translation file
books/arch-handbook.pot, string 1705