カーネル空間で sys_read() を正しく使用する方法

カーネル空間で sys_read() を正しく使用する方法

与えられた入力に対してファイルを検索するためにシステムコールを作成しました。しかし、私のコードがsys_read()正しく機能していないため、動作しません。

#include <linux/kernel.h>
#include <linux/unistd.h>
#include <asm/unistd.h>
#include <linux/fcntl.h>
#include <linux/syscalls.h>

asmlinkage long sys_search_phrase(int fd, char *phrase, char *buffer, int line)
{
    printk("Search was here\n");
    char arr[] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
    char *temp = arr;
    int i = 0;
    int temp_line = 1;

    while(sys_read(fd, &temp[i], 1) == 1){
        if(temp[i] == '\n' || temp[i] == 0x0 || temp[i] == '\0'){
            temp[i] = 0;
            if(i != 0){
                //buffer now has a line
                if(line <= 0 && contains(temp, phrase)) {
                    copy(buffer, temp);
                    return temp_line;
                }
                else if(line == temp_line && contains(temp, phrase)) {
                    copy(buffer, temp);
                    return temp_line;
                }
                temp_line++;
            }
            i=0;
            continue;
        }
        i++;
    }
    return -1;
}

だから私はデバッグコードを試してみましたが、printk()私のプログラムがwhileループに入らないことがわかりました。

カーネル v4.4.202 Ubuntu 16.04

ベストアンサー1

sys_read()カーネルのバッファを使用して呼び出すことはできず、2番目の引数としてユーザーの仮想アドレスが必要です。

カーネルAPIがあります。kernel_read()、カーネル空間で使用できます。

おすすめ記事