0

Linux カーネル用の新しいシステムコールを開発しようとしています。この syscall は、引数として取得されるユーザー バッファーに情報を書き込みます。たとえば、次のようになります。

asmlinkage int new_syscall(..., char *buffer,...){...}

ユーザー空間では、このバッファーは次のように静的に割り当てられます。

char buffer[10000];

(ユーザーレベルの sizeof() として) バッファサイズ全体 (この場合は 10000) を知る方法はありますか? 試してみstrlen_user(buffer)ましたが、現在バッファに入っている文字列の長さを返すので、バッファが空の場合は 0 を返します。

4

1 に答える 1

0

バッファポインタとバッファのサイズを含む構造体を渡すことを試みることができます。ただし、同じ構造をユーザースペースアプリケーションとカーネルのシステムコールのコード内の両方で定義する必要があります。

struct new_struct
{
   void *p;  //set this pointer to your buffer...
   int size;
};
//from user-application...

int main()
{
   ....
   struct new_struct req_kernel;
   your_system_call_function(...,(void *)&req_kernel,...);
}
........................................................................................
      //this is inside your kernel...
     your_system_call(...,char __user optval,...)
     {
            .....
            struct new_struct req;
            if (copy_from_user(&req, optval, sizeof(req)))

            return -EFAULT;
            //now you have the address or pointer & size of the buffer
            //which you want in kernel with struct req...
    }
于 2013-02-07T04:53:23.213 に答える