Added chapter 2 source file for hello_world kernel module

This commit is contained in:
Edson Brandi 2025-11-07 20:56:04 +00:00
parent 66c0446388
commit ba961a1fee
No known key found for this signature in database
GPG key ID: 604D961A8EC70166
2 changed files with 54 additions and 0 deletions

View file

@ -0,0 +1,6 @@
# Makefile for hello_world kernel module
KMOD= hello_world
SRCS= hello_world.c
.include <bsd.kmod.mk>

View file

@ -0,0 +1,48 @@
/*
* hello_world.c - Simple FreeBSD kernel module
* Prints messages when loaded and unloaded
*/
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/systm.h>
/*
* Load handler - called when the module is loaded
*/
static int
hello_world_load(module_t mod, int cmd, void *arg)
{
int error = 0;
switch (cmd) {
case MOD_LOAD:
printf("Hello World! Kernel module loaded.\n");
break;
case MOD_UNLOAD:
printf("Goodbye World! Kernel module unloaded.\n");
break;
default:
error = EOPNOTSUPP;
break;
}
return (error);
}
/*
* Module declaration
*/
static moduledata_t hello_world_mod = {
"hello_world", /* module name */
hello_world_load, /* event handler */
NULL /* extra data */
};
/*
* Register the module with the kernel
* DECLARE_MODULE(name, data, sub-system, order)
*/
DECLARE_MODULE(hello_world, hello_world_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
MODULE_VERSION(hello_world, 1);