insmod传递带空格的字符串

Makefile内容

KVERS = $(shell uname -r)

# Kernel modules
obj-m += book.o

# Specify flags for the module compilation.
#EXTRA_CFLAGS=-g -O0

build: kernel_modules

kernel_modules:
    make -C /lib/modules/$(KVERS)/build M=$(CURDIR) modules

clean:
    make -C /lib/modules/$(KVERS)/build M=$(CURDIR) clean

book.c内容--使用宋老师的例子

 * a simple kernel module supporting parameters
 *
 * Copyright (C) 2014 Barry Song  (baohua@kernel.org)
 *
 * Licensed under GPLv2 or later.
 */

#include <linux/init.h>
#include <linux/module.h>

static char *book_name = "dissecting Linux Device Driver";
module_param(book_name, charp, S_IRUGO);

static int book_num = 4000;
module_param(book_num, int, S_IRUGO);

static int __init book_init(void)
{
        printk(KERN_INFO "book name:%s\n", book_name);
        printk(KERN_INFO "book num:%d\n", book_num);
        return 0;
}
module_init(book_init);

static void __exit book_exit(void)
{
        printk(KERN_INFO "book module exit\n ");
}
module_exit(book_exit);

MODULE_AUTHOR("Barry Song <baohua@kernel.org>");
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("A simple Module for testing module params");
MODULE_VERSION("V1.0");

默认加载

sudo insmod book.ko

输出如下

[41709.146814] book name:dissecting Linux Device Driver
[41709.146818] book num:4000

加载时传递参数

sudo insmod book.ko book_name='GoodBook' book_num=5000

输出如下

[41933.147764] book name:GoodBook
[41933.147769] book num:5000

加载时传递参数--字符串里包含空格(错误方式)

sudo insmod book.ko book_name='Professional Linux Kernel Architecture' book_num=5000
或者
sudo insmod book.ko book_name="Professional Linux Kernel Architecture" book_num=5000

提示错误

insmod: ERROR: could not insert module book.ko: Unknown symbol in module

dmesg输出

[42208.316805] book: Unknown parameter `Linux'

原因在于:shell将' '或" "吃掉了,剩下book_name=Professional Linux Kernel Architecture作为参数传递给内核,由内核函数parse_args进行解析[kernel/params.c],然后调用next_arg将其分割为param和val匹配对,以等号作为分割符号,next_arg函数如下([kernel/params.c]):

/* You can use " around spaces, but can't escape". /
/
Hyphens and underscores equivalent in parameter names. */

static char *next_arg(char *args, char **param, char **val)
{
        unsigned int i, equals = 0;
        int in_quote = 0, quoted = 0;
        char *next;

        if (*args == '"') {/* 出现双引号 */
                args++;
                in_quote = 1;
                quoted = 1;
        }

        for (i = 0; args[i]; i++) {
                if (isspace(args[i]) && !in_quote)
                        break; /*  无双引号配对后出现空格时跳出for循环 */
                if (equals == 0) {
                        if (args[i] == '=')
                                equals = i;/* 出现等号后记录其位置 */
                }
                if (args[i] == '"')/* 判断双引号" */
                        in_quote = !in_quote;/* 双引号必须配对使用 */
        }

        *param = args;
        if (!equals)
                *val = NULL;/* 无等号出现时赋值为空, */
        else {
                args[equals] = '\0';
                *val = args + equals + 1;/* 指向配对字符串的开始位置*/

                /* Don't include quotes in value. */
                if (**val == '"') {
                        (*val)++;
                        if (args[i-1] == '"')
                                args[i-1] = '\0';
                }
                if (quoted && args[i-1] == '"')
                        args[i-1] = '\0';
        }

        if (args[i]) {
                args[i] = '\0';
                next = args + i + 1;
        } else
                next = args + i;

        /* Chew up trailing spaces. */
        return skip_spaces(next);
}

根据上面函数分析,需要将传递给kernel时包括双引号内的内容是可以包含空格的,因此insmod时将"Professional Linux Kernel Architecture"外再用单引号包裹一层传递给shell即:'"Professional Linux Kernel Architecture"',最后传递给kernel时变成"Professional Linux Kernel Architecture",kernel即可正常识别。

加载时传递参数--字符串里包含空格(正确方式)

sudo insmod book.ko book_name='"Professional Linux Kernel Architecture"' book_num=5000

输出

[45955.423536] book name:Professional Linux Kernel Architecture
[45955.423540] book num:5000
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、Python简介和环境搭建以及pip的安装 4课时实验课主要内容 【Python简介】: Python 是一个...
    _小老虎_阅读 5,846评论 0 10
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,010评论 19 139
  • 用词容易,找分寸感很难,比如刚柔相济,刚习惯的人,让他柔,就容易失去分寸感,要么柔的没有刚性,要么刚性很足找不到柔...
    一笑人生阅读 416评论 2 6
  • 1.从文章中学到的重要概念: A person's personality is composed of many...
    郭静瑜37阅读 270评论 2 0
  • 昨晚接到组长通知,今天早上去学校布置会议室,为周日的表彰大会做准备。为了不迟到,决定还是骑电动车送儿子上课...
    三年级五班刘佳铭阅读 238评论 0 0