kernel module ?

Programming, for all ages and all languages.
Post Reply
User avatar
Sam111
Member
Member
Posts: 385
Joined: Mon Nov 03, 2008 6:06 pm

kernel module ?

Post by Sam111 »

I am learning how to write kernel modules for the first time.

I am running into a few problems that I cann't figure out

question 1 )
I have compiled the hello example to get a hello.ko and then I have loaded it using sudo insmod hello.ko
But when I load or unload with (rmmod) I cann't see any messages being printed to the screen???
What is printk doing? Thought it would print to the screen.

Code: Select all

#include <linux/module.h>
#include <linux/kernel.h>
 
int init_module(void)
{
	printk(KERN_INFO "init_module() called\n");
	return 0;
}
 
void cleanup_module(void)
{
	printk(KERN_INFO "cleanup_module() called\n");
}
Question 2
Is their away to list all the kernel functions built in to the kernel that are useable in creating a kernel module?
For example are the only functions you can use in module.h and kernel.h or are their more header files that contain functions that you can use in your module making.
CrypticalCode0
Member
Member
Posts: 81
Joined: Wed Nov 09, 2011 2:21 am
Location: Behind a keyboard located in The Netherlands

Re: kernel module ?

Post by CrypticalCode0 »

The linux API uses C as API this AFAIK means you should use printf()
User avatar
Sam111
Member
Member
Posts: 385
Joined: Mon Nov 03, 2008 6:06 pm

Re: kernel module ?

Post by Sam111 »

thank you it shows up in the dmesg command.

question
I know when I was working on windows machines and using the win32 api they break functions into kernel functions and user land functions which call the kernel functions. (kernel functions are always available where as user land functions are only available in user land ...)
I am wondering does linux make this distinction or can you use any function at any time.

Basically kernel function would be the functions compiled into the kernel and always useable where as user mode functions/ libs call the kernel functions and are only useable after the kernel is loaded and userland is obtained.

Basically I am worried about if print will print to the screen when the os is loading kernel modules the very first time the os boots

And is their away to list all the functions your kernel has compiled/builtin to it. So you can see what kernel functions are available for use.
User avatar
Sam111
Member
Member
Posts: 385
Joined: Mon Nov 03, 2008 6:06 pm

Re: kernel module ?

Post by Sam111 »

Is their away to include the stdio.h and other included directories when compiling a kernel module?
I want to use printf to print a message to the console.
My make is like this

Code: Select all

obj-m += hello.o
 
all:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
 
clean:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
I have tried adding to the M macro -I /usr/include/ where the stdio.h is for printf but the make file does not work if I do that.

Also I have read this
If the priority is less than int console_loglevel, the message is printed on your current terminal. If both syslogd and klogd are running, then the message will also get appended to /var/log/messages, whether it got printed to the console or not. We use a high priority, like KERN_ALERT, to make sure the printk() messages get printed to your console rather than just logged to your logfile. When you write real modules, you'll want to use priorities that are meaningful for the situation at hand.
But when I use KERN_ALERT it doesn't print to console still?

printk( KERN_ALERT "init_module() called\n");
printk( KERN_WARNING "init_module() called\n"); ...etc non print to the console and I don't know how to include stdio.h in my make file for the kernel module. :oops:
User avatar
Sam111
Member
Member
Posts: 385
Joined: Mon Nov 03, 2008 6:06 pm

Re: kernel module ?

Post by Sam111 »

well when I use

Code: Select all

cat /proc/sys/kernel/printk
4	4	1	7
And I tried

Code: Select all

echo 8 > /proc/sys/kernel/printk
cat /proc/sys/kernel/printk
8	4	1	7

All that did is change the first number from 4 to 8 but when I add all these lines I see none of them?

Code: Select all

int init_module(void)
{
	printk( KERN_EMERG "111111init_module() called\n");
        printk( KERN_ALERT "111111init_module() called\n");
        printk( KERN_CRIT  "111111init_module() called\n");
        printk( KERN_ERR   "111111init_module() called\n");
        printk( KERN_WARNING "111111init_module() called\n");
        printk( KERN_NOTICE "111111init_module() called\n");
        printk( KERN_INFO "111111init_module() called\n");
        printk( KERN_DEBUG "111333333111init_module() called\n");	

return 0;
}
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: kernel module ?

Post by Combuster »

I can see them right in your post.


Define "not seeing". exact commands required.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Sam111
Member
Member
Posts: 385
Joined: Mon Nov 03, 2008 6:06 pm

Re: kernel module ?

Post by Sam111 »

what?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: kernel module ?

Post by Combuster »

what?
Nope, that does not reproduce the problem.

Even my crystal ball doesn't quit suggesting "PEBKAC"...
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Sam111
Member
Member
Posts: 385
Joined: Mon Nov 03, 2008 6:06 pm

Re: kernel module ?

Post by Sam111 »

the list open files command gives me

Code: Select all

lsof /proc/kmsg

Code: Select all

lsof: WARNING: can't stat() fuse.gvfs-fuse-daemon file system /home/xxxx/.gvfs
      Output information may be incomplete.
COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF       NODE NAME
rsyslogd 404 syslog    3r   REG    0,3        0 4026532014 /proc/kmsg
What do I do next?
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

Re: kernel module ?

Post by gravaera »

Stop using linux, it's meant to confuse people who want to get "normal" things done. Use linux for your development work, and use other kernels for real everyday activites. </flamebait>

I don't think there's any real reason for you to need to write a linux module, unless your job requires it...but in that case you should probably give your employers and peers a bit more consideration and think about properly reading up on the subject, and studying kernel source a bit better.

Good luck :)
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
User avatar
Sam111
Member
Member
Posts: 385
Joined: Mon Nov 03, 2008 6:06 pm

Re: kernel module ?

Post by Sam111 »

when I kill the process rsyslogd it just returns with a different pid?

ps -A | grep rsyslogd

shows

Code: Select all

15891 ?        00:00:00 rsyslogd

when I insmod or rmmod I still don't see the message being displayed to the console?

Also man dmesg and tried sudo dmesg - n 8 that didn't work either ?

I found out by reading the man page for dmesg that if you open another terminal an issue
sudo cat /proc/kmsg then go to another terminal and insmod , rmmod ,...etc the printk messages will be displayed to the other screen everytime another printk message is issued in the code. This is a work around and away of debugging with out seeing everything from dmesg and having to type dmesg | tail all the time.

Thanks Combuster , berkus,CrypticalCode0
and gravaera just because you don't have to do it for work doesn't mean you shouldn't learn how it works.
Plus it will be nice to customize my own module/driver code and create some rootkits on linux (if I wanted to)
User avatar
Sam111
Member
Member
Posts: 385
Joined: Mon Nov 03, 2008 6:06 pm

Re: kernel module ?

Post by Sam111 »

With these kernel modules to compile them you need a simple make file like this

Code: Select all

obj-m += hello.o
 
all:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
 
clean:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
which basically just switches to use the Makefile under the /lib/modules/2.6.38-10-generic/build directory
then executes the modules target tag with the M macro equal to the current directory of where you created your module.

But is their a macro or away to set an included or library directory to use.

For instance what if I wanted to use printf instead of printk like CrypticalCode0 suggested for displaying purposes.
I am running into the problem of how to include it into the make file so when I compile/build my module it see's the stdio.h header and library files which are in /usr/include and /usr/lib?

Even if printf is not the way to go it would be nice to leverage headers and library files that are not under the /lib... directories
User avatar
Sam111
Member
Member
Posts: 385
Joined: Mon Nov 03, 2008 6:06 pm

Re: kernel module ?

Post by Sam111 »

I here you.
just want to share a few things I found out
printk does print to the console but you have to be in ctrl + alt + f1 not the kconsole running on top of a gui like gnome.

I.E printk prints to the /dev/tty1 device (your ctrl+ alt+f1 console not console running on GUI)

Thanks again everybody
all set for now.
Post Reply