Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux綜合 >> Linux 核心

Linux 內核態與使用者態通訊:用戶上下文下的 copy_from_user 方案

跑在使用者上下文(user context)中的程式碼是可以睡眠的,因此能用 copy_from_user()/copy_to_user() 這類可能引發阻塞的介面。訊息佇列與 UNIX 域 socket 也能達成同樣目的,但資料傳輸效率較低。反過來說,這兩個特殊拷貝函式絕對不能出現在硬體中斷或軟體中斷裡,因為中斷上下文不可睡眠。

最典型的應用場景就是系統呼叫一類的函式——它們天生「穿梭」於內核態與使用者態之間。下圖為此類方法的原理示意。

其中相關的系統呼叫需要自行撰寫並載入核心。本示例的做法是:核心模組註冊一組 setsockopt/getsockopt 處理函式,讓使用者空間行程得以讀寫內核態資料。原始碼共三個檔案:imp1.h 為共用標頭,imp1_k.c 為核心模組,imp1_u.c 為使用者行程。流程是使用者態送出一段字串,再由核心回送一段字串。

一、核心端 imp1_k.c

#ifndef __KERNEL__
#define __KERNEL__
#endif
#ifndef MODULE
#define MODULE
#endif

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/string.h>
#include <linux/netfilter_ipv4.h>
#include <linux/init.h>
#include <asm/uaccess.h>
#include "imp1.h"

#define KMSG        "a message from kernel\n"
#define KMSG_LEN   sizeof("a message from kernel\n")
MODULE_LICENSE("Dual BSD/GPL");

static int data_to_kernel(struct sock *sk, int cmd, void *user, unsigned int len)
{
    switch (cmd) {
    case IMP1_SET:
        {
            char umsg[64];
            memset(umsg, 0, sizeof(char) * 64);
            copy_from_user(umsg, user, sizeof(char) * 64);
            printk("umsg: %s", umsg);
        }
        break;
    }
    return 0;
}

static int data_from_kernel(struct sock *sk, int cmd, void *user, int *len)
{
    switch (cmd) {
    case IMP1_GET:
        {
            copy_to_user(user, KMSG, KMSG_LEN);
        }
        break;
    }
    return 0;
}

static struct nf_sockopt_ops imp1_sockops = {
    .pf            = PF_INET,
    .set_optmin     = IMP1_SET,
    .set_optmax     = IMP1_MAX,
    .set           = data_to_kernel,
    .get_optmin     = IMP1_GET,
    .get_optmax     = IMP1_MAX,
    .get           = data_from_kernel,
};

static int __init init(void)
{
    return nf_register_sockopt(&imp1_sockops);
}

static void __exit fini(void)
{
    nf_unregister_sockopt(&imp1_sockops);
}

module_init(init);
module_exit(fini);

二、共用標頭 imp1.h

#ifndef __IMP1_H__
#define __IMP1_H__

#define IMP1_OPS_BASIC 128
#define IMP1_SET       IMP1_OPS_BASIC
#define IMP1_GET       IMP1_OPS_BASIC
#define IMP1_MAX       IMP1_OPS_BASIC + 1

#endif

三、應用層 imp1_u.c

#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <linux/in.h>
#include "imp1.h"

#define UMSG       "a message from userspace\n"
#define UMSG_LEN   sizeof("a message from userspace\n")

char kmsg[64];

int main(void)
{
    int sockfd;
    int len;

    sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
    if (sockfd < 0) {
        printf("can not create a socket\n");
        return -1;
    }

    /* call function data_to_kernel() */
    setsockopt(sockfd, IPPROTO_IP, IMP1_SET, UMSG, UMSG_LEN);

    len = sizeof(char) * 64;

    /* call function data_from_kernel() */
    getsockopt(sockfd, IPPROTO_IP, IMP1_GET, kmsg, &len);
    printf("kmsg: %s", kmsg);

    close(sockfd);
    return 0;
}

四、執行結果

核心端 dmesg 會看到:

[541380.295993] umsg: a message from userspace

應用程式輸出:

$ sudo ./imp1_u
kmsg: a message from kernel

要特別注意的是,copy_from_user 回傳值代表「未能複製的位元組數」,正式環境中應檢查它並回傳 -EFAULT;此外此例固定複製 64 位元組,實務上應依 len 做邊界檢查,避免越界讀寫。

Copyright © Linux教程網 All Rights Reserved