Linux 内核:来自“open”的 printk系统调用不起作用
我有一个疑问。
我打开内核并更改了目录 linux-3.1.1/fs/open.c
我更改了 open.c 中的以下代码。
SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, int, mode)
{
long ret;
printk(KERN_EMERG "Testing\n");
...
}
我只放了这一行: printk(KERN_EMERG "Testing");
并且我包含了库:
和
所以我编译并重新启动了我的linux(Ubuntu)。 重新启动期间,屏幕上出现了很多“测试”。 所以到目前为止还可以。
但现在我有一个问题。 我用c语言创建了这个程序。
int main()
{
size_t filedesc = open("testefile2.txt",O_CREAT | O_WRONLY,0640);
printf("%d",filedesc);
}
我编译了这个程序并执行并且运行良好。 但我不明白为什么“测试”没有出现在外壳上。 我的意思是,如果当我重新启动电脑时出现很多“测试”这个词,为什么当我执行上面的程序时这个词没有出现。 只是补充一下,我在上面的代码中包含了这个库:
unistd.h
、 fcntl.h
、 stdio.h
、 stdlib。 h
谢谢你们。
I have a doubt.
I opened the kernel and I changed the directory linux-3.1.1/fs/open.c
I changed the follow code in the 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 rebooting appeared a lot of "Testing" on the screen.
So up to now 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 good.
But I don´t understand why the "Testing" didn't appeared on the shell.
I mean , if when I reboot the pc appeared a lot of the word "Testing" , why this word doens´t appear when I execute the program above.
Just to add I include this libraries in this code above:
unistd.h
, fcntl.h
, stdio.h
, stdlib.h
Thank you guys.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
printk 调用出现在内核消息缓冲区中,而不是在进程的 stdout/stderr 中
printk calls appear in the kernel message buffer, not in your process' stdout/stderr
我认为,这是 printk 消息抑制的效果。 (更准确地说:速率限制)
检查消息日志或控制台中的
字符串。
如果最近有大量消息,此功能将停止打印消息。
实际代码为 3.1 内核: http://lxr.linux .no/#linux+v3.1.1/kernel/printk.c#L1621
因此,由于
open
系统调用非常非常流行(只需执行strace -e open /bin/ls
- 我将获得 15 个open
系统调用(仅启动一个最简单的ls
),速率限制为有效。它将限制您的消息在 5 秒内仅打印一次;单次“突发”消息不超过 10 条。我只能建议创建一个具有已知 UID 的特殊用户,并在附加 printk-in-open 代码中的
printk
之前添加 UID 检查。I think, this is effect of printk's messages suppression. (more exactly:rate limiting)
Check the messages log or console for
string.
This feature will stop printing a message, if there were a lot of messages in recent time.
Actual code is as 3.1 kernel: http://lxr.linux.no/#linux+v3.1.1/kernel/printk.c#L1621
So, As the
open
syscall is very-very popular (just do anstrace -e open /bin/ls
- I'll get 15open
syscalls for just starting an simplestls
), the rate limiting will be in effect. It will limit your message to be printed only one time in 5 seconds; not more than 10 messages in single "burst".I can only suggest to create a special user with known UID and add an UID checking before
printk
in your additional printk-in-open code.