Page 1 of 1
kernel module ?
Posted: Mon Nov 14, 2011 9:30 am
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.
Re: kernel module ?
Posted: Mon Nov 14, 2011 9:32 am
by CrypticalCode0
The linux API uses C as API this AFAIK means you should use printf()
Re: kernel module ?
Posted: Mon Nov 14, 2011 10:07 am
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.
Re: kernel module ?
Posted: Mon Nov 14, 2011 10:48 am
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.
Re: kernel module ?
Posted: Mon Nov 14, 2011 11:56 am
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;
}
Re: kernel module ?
Posted: Mon Nov 14, 2011 12:00 pm
by Combuster
I can see them right in your post.
Define "not seeing". exact commands required.
Re: kernel module ?
Posted: Mon Nov 14, 2011 12:18 pm
by Sam111
what?
Re: kernel module ?
Posted: Mon Nov 14, 2011 1:09 pm
by Combuster
what?
Nope, that does not reproduce the problem.
Even my crystal ball doesn't quit suggesting "PEBKAC"...
Re: kernel module ?
Posted: Mon Nov 14, 2011 9:00 pm
by Sam111
the list open files command gives me
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?
Re: kernel module ?
Posted: Tue Nov 15, 2011 5:56 am
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
Re: kernel module ?
Posted: Tue Nov 15, 2011 10:35 am
by Sam111
when I kill the process rsyslogd it just returns with a different pid?
ps -A | grep rsyslogd
shows
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)
Re: kernel module ?
Posted: Tue Nov 15, 2011 10:44 am
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
Re: kernel module ?
Posted: Wed Nov 16, 2011 7:45 pm
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.