我想從內核空間寫入日誌文件。我可以創建,打開&關閉文件(/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->寫(...),但模塊崩潰了。
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 ]---
不要在標題中放置[Linux],應該使用標記。編輯。 – orlp
這不應該有C++標籤。 – Dipstick
@ 0xBAADF00D我可以知道這個模塊的用途嗎? –