Hello You

先の hello_world.c を修正して、課題だったパラメータを入力する、エラーコードを返す実装をしてみましょう。make 方法と Makefile は hello_world.c と変わらないので省略します。コンパイルが済んだと言う前提でこのページを書きます。

filehello_world.c
Expand allFold all
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 
 
 
 
 
 
 
 
 
 
 
 
-
|
|
|
!
 
-
|
!
 
-
|
|
!
 
-
!
 
 
 
 
 
 
 
#include <linux/kernel.h>
#include <linux/printk.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/stat.h>
 
static char *your_name = "Taro";
module_param(your_name, charp, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
 
static int return_value = 0;
module_param(return_value, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
 
/*
 * Tutorial hello world module init.
 * @return int == 0: Success. 
 *             < 0:  Fail, negative errno number.
 */
static int __init hello_world_init(void)
{       printk(KERN_INFO "%s: Hello %s san. return_value=%d\n", __func__, your_name, return_value);
        return return_value;
}
 
/*
 * Tutorial hello world module exit.
 * @return void
 */
static void __exit hello_world_exit(void)
{       printk(KERN_INFO "%s: Goodbye %s san.\n", __func__, your_name);
}
 
module_init(hello_world_init);
module_exit(hello_world_exit);
 
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Tutorial hello you.");
MODULE_AUTHOR("Your name or email address.");

パラメータを受け取る手段

モジュールはパラメータを受け取ることができます。module_parameter と DEV_ATTR です。

パラメータを入力する

まず、パラメータ無しで insmod, rmmod を試します。ソース・コードに書かれたデフォルト値で動作しているのがわかります。

$ sudo insmod hello_world.ko
[sudo] password for user_name:password
$ sudo rmmod hello_world.ko
$ dmesg

dmesg が出力した最後は次のようになります。your_name と return_value の値がそのまま表示されました。

[355028.315777] hello_world: module verification failed: signature and/or required key missing - tainting kernel
[355028.319261] hello_world_init: Hello Taro san. return_value=0
[355036.306520] hello_world_exit: Goodbye Taro san.

パラメータを付けて起動しましょう。your_name=Hanako とします。

$ sudo insmod hello_world.ko your_name=Hanako
$ sudo rmmod hello_world.ko
$ dmesg

dmesg が出力した最後は次のようになります。your_name に指定した Hanako が表示されました。アプリケーション・プログラムと仕組みは違いますがパラメータを受け取る手段が有ります。

[355218.292794] hello_world_init: Hello Hanako san. return_value=0
[355236.027700] hello_world_exit: Goodbye Hanako san.

エラーを返してみよう

printk の長さ制限


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS