views:

164

answers:

2

deal all,

i am a newbie for writing Linux Kernel Module.

i used printk function in linux kernel source code (2.4.29) for debugging and display messages.

now, i have to read all the messages i added via httpd.

i tried to write the messages into a file instead of printk function, so i can read the file directly.

but it's not work very well.

so, i have a stupid question...

is it possible to write a LKM to monitor the syslog and rewrite into another file??

i mean is that possible to let a LKM the aware the messages when each time the linux kernel execute "printk"??

thanks a lot

+2  A: 

That is the wrong way to do it, because printk already does this : it writes in the file /proc/kmsg. What you want is klogd, a user space utility dealing with /proc/kmsg.

Another options is to use dmesg, which will output the whole content of the kernel buffers holding the printk messages, but I suggest you first read the linked article

shodanex
thanks for your reply.it's helpful.thanks a lot!!
douglas
@douglas, if you found his reply helpful, you might consider upvoting it.
Tim Post
A: 

You never, ever, ever want to try to open a file on a user space mounted block file system from within the kernel. Imagine if the FS aborted and the kernel was still trying to write to it .. kaboom (amongst MANY other reasons why its a bad idea) :) As shodanex said, for your purposes, its much better to use klogd.

Now, generally speaking, you have several ways to communicate meaningful data to userspace programs, such as:

  • Create a character device driver that causes userspace readers to block while waiting for data. Provide an ioctl() interface to it which lets other programs find out how many messages have been sent, etc.

  • Create a node in /proc/yourdriver to accomplish the same thing

Really, the most practical means is to just use printk()

Tim Post