From ba961a1fee229902595c13e261b1d9c0cbb79611 Mon Sep 17 00:00:00 2001 From: Edson Brandi Date: Fri, 7 Nov 2025 20:56:04 +0000 Subject: [PATCH] Added chapter 2 source file for hello_world kernel module --- src/chapters/2/hello_world/Makefile | 6 +++ src/chapters/2/hello_world/hello_world.c | 48 ++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 src/chapters/2/hello_world/Makefile create mode 100644 src/chapters/2/hello_world/hello_world.c diff --git a/src/chapters/2/hello_world/Makefile b/src/chapters/2/hello_world/Makefile new file mode 100644 index 0000000..3c3701e --- /dev/null +++ b/src/chapters/2/hello_world/Makefile @@ -0,0 +1,6 @@ +# Makefile for hello_world kernel module + +KMOD= hello_world +SRCS= hello_world.c + +.include diff --git a/src/chapters/2/hello_world/hello_world.c b/src/chapters/2/hello_world/hello_world.c new file mode 100644 index 0000000..00dbb64 --- /dev/null +++ b/src/chapters/2/hello_world/hello_world.c @@ -0,0 +1,48 @@ +/* + * hello_world.c - Simple FreeBSD kernel module + * Prints messages when loaded and unloaded + */ + +#include +#include +#include +#include + +/* + * 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);