Linux kernel: printk from "open" syscall not working

I have doubts.

I opened the kernel and I changed the linux-3.1.1 / fs / open.c directory

I changed the following code in open.c.

SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, int, mode) { long ret; printk(KERN_EMERG "Testing\n"); ... } 

I put this line only: printk(KERN_EMERG "Testing");

And I include the libraries: <linux/kernel.h> and <linux/printk.h>

So, I compiled and rebooted my Linux (Ubuntu). During the reboot, a lot of "Testing" appeared on the screen. So, still its ok.


But now I have a problem. I created this program in c.

 int main() { size_t filedesc = open("testefile2.txt",O_CREAT | O_WRONLY,0640); printf("%d",filedesc); } 

I compiled this program and executed and works well. But I do not understand why "Testing" did not appear in the shell. I mean, if during the reboot of the PC a lot of the word "Testing" appeared, why this word does not appear when I execute the program above. Just add, I have included these libraries in this code above:

unistd.h , fcntl.h , stdio.h , stdlib.h

Thanks guys.

+6
source share
2 answers

But I do not understand why "Testing" did not appear on the shell.

I think this is the effect of suppressing printk messages. (more precisely: speed limit)

Check the message log or console for

 printk: ### messages suppressed. 

line.

This function will stop printing a message if a lot of messages have appeared recently.

Actual code is like 3.1 kernel: http://lxr.linux.no/#linux+v3.1.1/kernel/printk.c#L1621

 1621 * printk rate limiting, lifted from the networking subsystem. 1622 * 1623 * This enforces a rate limit: not more than 10 kernel messages 1624 * every 5s to make a denial-of-service attack impossible. 1625 */ 1626 DEFINE_RATELIMIT_STATE(printk_ratelimit_state, 5 * HZ, 10); 1627 1628 int __printk_ratelimit(const char *func) 

So, since syscall open very very popular (just do strace -e open /bin/ls - I will get 15 open syscalls to just run the simplest ls ), the speed limit will apply, It will limit your message to be printed only once in 5 seconds ; no more than 10 messages in one "surge".

I can only suggest creating a special user with a known UID and adding a UID check before printk in your additional printk-in-open code.

+4
source

printk calls appear in the kernel message buffer, not in your process' stdout / stderr

+6
source

Source: https://habr.com/ru/post/901863/


All Articles