[LINUX] A simple kernel module

A simple kernel module(example code in LDD).

ref Linux Device Drivers

hello.c

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <linux/init.h>
#include <linux/module.h>
#include <linux/sched.h>

static int hello_init(void)
{
    printk(KERN_ALERT "HELLO, WORLD\n");

    /* "current" gives the pid of the the proccess that calls the module function*/
    printk(KERN_INFO "THE PROCCESS IS \"%s\" (pid %i)\n", current->comm, current->pid);
    return 0;
}

static void hello_exit(void) { printk(KERN_ALERT "GOOD BYE WORLD\n"); }

module_init(hello_init);
module_exit(hello_exit);

/* module_init and module_exit registers the function to be called upon 
*  loading and unloading the kernel module
**/

/* MODULE_LICENSE is optional, GPL is recommended */
MODULE_LICENSE("GPL");

Makefile1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22

obj-m += hello.o

# IF THE MODULE OBJECT IS BUILT FROM MULTIPLE FILE
# e.g. f1.c, f2.c ... use the following line
# module-objs := f1.o f2,0


all:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

#  MAKE OPTIONS:
# -C specify the directory of wop-level makefile
#  here it is the kernel source directory.
# -M causes that makefile to move back into module
#  source directory before trying to build the 
#  'modules' target.This target, in turn, refers 
#  to the list of modules found in the obj-m 
#  variable.

clean:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

Makefile2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# IF KERNELRELEASE is defined, we've been invoked from
# the kernel build system and can use its language.
ifneq ($(KERNELRELEASE),)
	obj-m := hello.o

# Otherwise we were called directly from the command
# line; invoke the kernel build system.
else
	KERNELDIR ?= /lib/modules/$(shell uname -r )/build
PWD := $(shell pwd)

default:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

endif

# This makefile is read twice on a typical build.
# When the makefile is invoked from the command line,
# it notices that the KERNELRELEASE variable has not
# been set. It locates the kernel source directory by
# taking advantage of the fact that the symbolic link 
# "build" in the installed modules directory points
# back at the kernel build tree.

These two makefiles does the same thing in this case. The second is smarter, tho. See the comments in both.

Commands:

Command Description
make Make(compile) the module
make clean clear compiled files
(sudo) insmod hello.ko load the module
lsmod list the loaded modules
rmmod hello remove the module
dmesg –follow show printk outputs


[+] click to leave a comment [+]
the comment system on this blog works via email. The button
below will generate a mailto: link based on this page's url 
and invoke your email client - please edit the comment there!

[optional] even better, encrypt the email with my public key

- don't modify the subject field
- specify a nickname, otherwise your comment will be shown as   
  anonymous
- your email address will not be disclosed
- you agree that the comment is to be made public.
- to take down a comment, send the request via email.

>> SEND COMMENT <<




Voice? via Blog on luis.zip January 12, 2024

Who is the voice inside my head? I have a voice in my mind. I’m hearing it now. It dictates what I write. Sometimes when I think about something, it speaks to me as if it were not me, as if it were not a product of the imagination that belongs to my domain…

2023 in Books via Wesley's Notebook January 7, 2024

recently ~ august23 via Sindre Kjelsrud September 5, 2023

what i've been doing during the month of august.

Generated by openring from webring