从内核写入文件

时间:2011-06-18 12:49:14

标签: c linux kernel

我想从内核空间写入一个日志文件。我可以创造,开放和发展关闭文件(/var/log/my_kern_module.log)但如果我尝试写入内容,我的模块崩溃...所以我知道内核空间的读/写文件很糟糕,但我必须在这个模块中执行。你有什么建议可以帮助我吗? THX

在这里,您可以找到在我的内核模块上执行写入的代码。它代码运行到一个线程(kthread)

mutex_lock(&gl_mtx_writelog);
      printk(KERN_EMERG "Readed : %s\n", buffer);

      fd = filp_open("/var/log/my_kern_module.log", O_CREAT | O_WRONLY | O_APPEND, S_IRWXU);
      if (!IS_ERR (fd)) {
        fs = get_fs();
        set_fs(KERNEL_DS);

        do_sync_write(fd, buffer, readed, 0);

        set_fs(fs);
        filp_close(fd, NULL);
      }
      mutex_unlock(&gl_mtx_writelog);

所以,我试过使用fd-> f_op-> write(...),但模块也崩溃了。

BUG: unable to handle kernel NULL pointer dereference at (null)
IP: [<c10df83a>] do_sync_write+0x6a/0xe0
*pde = 00000000
Oops: 0000 [#1] SMP
last sysfs file: /sys/module/snd_mixer_oss/initstate
Modules linked in: trigger_server snd_seq_dummy snd_seq_oss snd_seq_midi_event s                          nd_seq snd_seq_device snd_pcm_oss snd_mixer_oss ipv6 pcmcia pcmcia_core agpgart                           lp fuse ppdev snd_intel8x0 snd_ac97_codec ac97_bus thermal i2c_piix4 snd_pcm the                          rmal_sys i2c_core snd_timer e1000 parport_pc parport snd rtc_cmos rtc_core rtc_l                          ib joydev psmouse soundcore snd_page_alloc evdev usbhid serio_raw hid hwmon ac b                          utton sg [last unloaded: pcmcia_core]

Pid: 2468, comm: kthread_cli Not tainted 2.6.33.4-smp #1 /VirtualBox
EIP: 0060:[<c10df83a>] EFLAGS: 00010287 CPU: 0
EIP is at do_sync_write+0x6a/0xe0
EAX: f6434000 EBX: 00000000 ECX: 00000200 EDX: f5c26400
ESI: f653ca80 EDI: f5cf1f14 EBP: f5cf1f98 ESP: f5cf1f0c
 DS: 007b ES: 007b FS: 00d8 GS: 00e0 SS: 0068
Process kthread_cli (pid: 2468, ti=f5cf0000 task=f6434000 task.ti=f5cf0000)
Stack:
 c103de14 00000441 00000000 00000442 00000000 00000001 ffffffff f653ca80
<0> 00000000 00000000 00000000 00000000 f6434000 00000000 00000000 c10384a6
<0> 0000052b 00000000 0000052b ffffffff f5c26400 00000400 f5cf1f98 00000442
Call Trace:
 [<c103de14>] ? vprintk+0x184/0x3d0
 [<c10384a6>] ? try_to_wake_up+0x226/0x350
 [<f887b226>] ? thread_client+0x106/0x130 [trigger_server]
 [<f887b120>] ? thread_client+0x0/0x130 [trigger_server]
 [<c1058cf4>] ? kthread+0x74/0x80
 [<c1058c80>] ? kthread+0x0/0x80
 [<c10035be>] ? kernel_thread_helper+0x6/0x10
Code: 94 00 00 00 00 c7 45 98 00 00 00 00 c7 45 9c 00 00 00 00 c7 45 a0 00 00 00                           00 89 45 a4 c7 45 a8 00 00 00 00 c7 45 ac 00 00 00 00 <8b> 03 8b 53 04 89 4d c8                           89 45 b0 89 55 b4 eb 13 8d b6 00 00 00
EIP: [<c10df83a>] do_sync_write+0x6a/0xe0 SS:ESP 0068:f5cf1f0c
CR2: 0000000000000000
---[ end trace 32d03f08f128f335 ]---

3 个答案:

答案 0 :(得分:4)

一个明显的问题是do_sync_write()的第4个参数是指向文件偏移量存储位置的指针(因此可以更新)。您传递0可能解释NULL指针问题。可以试试:

 loff_t ppos = 0;
 do_sync_write(fd, buffer, readed, &ppos);

答案 1 :(得分:3)

此处列出了执行所需操作的等效方法。

Why writing files from the kernel is bad ?

答案 2 :(得分:0)

这种情况一直在LKML上出现,答案总是“不要这样做”。总有一种更好的方式。