2012-05-03 21 views
0

我對Linux非常陌生,我有幾個關於內核模塊編程的問題。我使用Ubuntu和C來​​製作我的.ko文件。我正在嘗試創建一個模塊,該模塊將在程序/ a被調用時執行程序/ b而不是/ a。有小費嗎?我將如何編寫一個運行程序b的內核模塊,無論何時調用程序a

此外,即使使用printk(KERN_EMERG ...),它也不會打印到終端。有沒有我失蹤的設置,或者ubuntu不這樣做?

非常感謝!

回答

1

您可能需要剔除/proc/sys/kernel/printk中的設置,該設置控制將哪些級別打印到控制檯。從proc(5)

/proc/sys/kernel/printk 
      The four values in this file are console_loglevel, 
      default_message_loglevel, minimum_console_level, and 
      default_console_loglevel. These values influence 
      printk() behavior when printing or logging error 
      messages. See syslog(2) for more info on the 
      different loglevels. Messages with a higher priority 
      than console_loglevel will be printed to the console. 
      Messages without an explicit priority will be printed 
      with priority default_message_level. 
      minimum_console_loglevel is the minimum (highest) 
      value to which console_loglevel can be set. 
      default_console_loglevel is the default value for 
      console_loglevel. 

需要注意的是,像nice(2)數值,值具有較高優先

做出的路徑/foo/a執行/foo/bexecve()最簡單的方法是對的/foo/a頂部綁定貼裝/foo/b

mount -obind /foo/b /foo/a 

沒有內核模塊是必需的。

使用內核模塊完成相同的任務將會大大增加工作量; LSM接口可以爲您提供一些幫助,以確定您的目標是否正在執行。如果你正在尋找一個起點,do_execve()fs/exec.c是從哪裏開始閱讀。確保安裝,運行ctags,並知道如何使用編輯器的集成ctags,使代碼讀取更加容易。

0

關於printk的回答:
它打印到控制檯,而不是終端。但是什麼是控制檯?
你可以在/proc/cmdline找到它的TTY。通常它是tty0,這意味着連接到電腦的屏幕。
如果你通過SSH/TELNET連接,當然你不會看到這個。
如果您在圖形環境(Gnome/KDE)中工作,您可能需要像alt-F1/F2這樣的內容切換到文本模式TTY。

您還可以使用dmesg命令查看消息。

相關問題