Hello, world!

You now know enough to write your first KLD. Example 1-1 is the complete skeleton code for a KLD.

Example 1-1. hello.c

#include <sys/param.h>
  #include <sys/module.h>
  #include <sys/kernel.h>
  #include <sys/systm.h>

  static int
 hello_modevent(module_t mod __unused, int event, void *arg __unused)
  {
          int error = 0;

          switch (event) {
          case MOD_LOAD:
                  uprintf("Hello, world!
");
                  break;
          case MOD_UNLOAD:
                  uprintf("Good-bye, cruel world!
");
                  break;
          default:
                  error = EOPNOTSUPP;
                  break;
          }

          return (error);
  }

 static moduledata_t hello_mod = {
          "hello",
          hello_modevent,
          NULL
  };

 DECLARE_MODULE(hello, hello_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);

This code contains a module event handler—it’s identical to the one described in Module Event Handler in Loadable Kernel Modules—and a filled-out moduledata_t structure, which is passed as the second argument to the DECLARE_MODULE macro.

In short, this KLD is just a module event handler and a DECLARE_MODULE call. Simple, eh?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.172.115