Linux 内核:来自“open”的 printk系统调用不起作用

发布于 2024-12-17 01:57:58 字数 945 浏览 4 评论 0原文

我有一个疑问。

我打开内核并更改了目录 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.hfcntl.hstdio.hstdlib。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鱼忆七猫命九 2024-12-24 01:57:58

printk 调用出现在内核消息缓冲区中,而不是在进程的 stdout/stderr 中

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

羅雙樹 2024-12-24 01:57:58

但我不明白为什么“Testing”没有出现在 shell 上。

我认为,这是 printk 消息抑制的效果。 (更准确地说:速率限制

检查消息日志或控制台中的

printk: ### messages suppressed.

字符串。

如果最近有大量消息,此功能将停止打印消息。

实际代码为 3.1 内核: 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)

因此,由于 open 系统调用非常非常流行(只需执行strace -e open /bin/ls - 我将获得 15 个 open 系统调用(仅启动一个最简单的 ls),速率限制为有效。它将限制您的消息在 5 秒内仅打印一次;单次“突发”消息不超过 10 条。

我只能建议创建一个具有已知 UID 的特殊用户,并在附加 printk-in-open 代码中的 printk 之前添加 UID 检查。

But I don´t understand why the "Testing" didn't appeared on the shell.

I think, this is effect of printk's messages suppression. (more exactly:rate limiting)

Check the messages log or console for

printk: ### messages suppressed.

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

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, As the open syscall is very-very popular (just do an strace -e open /bin/ls - I'll get 15 open syscalls for just starting an simplest ls), 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文