2016-12-08 24 views
0

こんにちは私はオープンコールを傍受してファイルを読み込み、ファイルを編集せずに出力を編集する必要があるクラス割り当てがあります。これはすべてカーネル空間の読み込み可能なモジュールで行われます。私が編集を言うとき、私は単語を変更して_をアンダースコアに置き換えることを意味します。それは単語のすべてのインスタンスを_heによく変更します。これは望ましい結果です。私はこれを理解しようとしている日にオンラインで見ましたが、私は適切な例を見つけたと思っていましたが、それは私にエラーを与え続けました。モジュールをカーネルに入れると即座に死んでしまうというメッセージが表示されますが、使用していないので使用できないため、取り除くことはできません。仮想マシンを再起動する必要があります。以下はコードです。お手伝いをいただければ幸いです。ubuntu読み込み可能なモジュールを使ってファイルを読む

#include <linux/module.h> // Needed by all modules 
#include <linux/kernel.h> // Needed for KERN_INFO 
#include <linux/fs.h>  // Needed by filp 
#include <asm/uaccess.h> // Needed by segment descriptors 

int init_module(void) 
{ 
    // Create variables 
    struct file *f; 
    char buf[128]; 
    mm_segment_t fs; 
    int i; 
    // Init the buffer with 0 
    for(i=0;i<128;i++) 
    buf[i] = 0; 
    // To see in /var/log/messages that the module is operating 
    printk(KERN_INFO "My module is loaded\n"); 
    // I am using Fedora and for the test I have chosen following file 
// Obviously it is much smaller than the 128 bytes, but hell with it =) 
    f = filp_open("/etc/fedora-release", O_RDONLY, 0); 
    if(f == NULL) 
     printk(KERN_ALERT "filp_open error!!.\n"); 
    else{ 
    // Get current segment descriptor 
    fs = get_fs(); 
    // Set segment descriptor associated to kernel space 
    set_fs(get_ds()); 
    // Read the file 
    f->f_op->read(f, buf, 128, &f->f_pos); 
    // Restore segment descriptor 
    set_fs(fs); 
    // See what we read from file 
    printk(KERN_INFO "buf:%s\n",buf); 
    } 
     filp_close(f,NULL); 
     return 0; 
    } 

    void cleanup_module(void) 
    { 
     printk(KERN_INFO "My module is unloaded\n"); 
    } 

    module_init(init_module); 
    module_exit(cleanup_module); 
+0

はsys_callフック、 'open'、read''のための1のための1つを書くことです。 http://stackoverflow.com/q/2103315/1212012を参照してください。 – purplepsycho

答えて

0

読書に関しては、カーネルからファイルへの書き込み、私はあなたが以下の記事を読んで、その中にコードsnippestsになりますことを示唆している: ドライビング・ミーナッツ - カーネルで実行してはならない決して物事あなたが必要なもの のLinuxジャーナル2005年、グレッグ・クロー=ハートマン

http://m.linuxjournal.com/article/8110

関連する問題