アプリケーションとの相互作用を作成すると読み、procの下にノードを書くことができます

カーネルのバージョン:Linuxの-4.14

 

ただ、procのノードの下にテストプログラムを書くことは、アプリケーション層と対話するために使用することができます。

使用して別の印刷をデバッグするためにも使用することができ、例えば、単独のパッケージ情報は、ノードをmy_printk、あなたは、アプリケーション層の猫を表示することができます。

1の#include <linuxの/ uaccess.h>
 2の#include <linuxの/ proc_fs.h>
 3の#include <linuxの/ module.h>
 4の#include <linuxの/ kernel.hを>
 5の#include <linuxの/ delay.h>
 6の#include <ASM / uaccess.h>
 7の#include <linuxの/ sched.h>
 8の#include <linuxの/ init.h>
 9の#include <ASM / irq.h>
 10の#include <ASM / io.h>
 11  
12  の#define MSG_BUF_LEN 1024
 13  
14  スタティック符号なしチャー msg_buf [MSG_BUF_LEN] = { 0 }。
15  静的符号なし整数msg_lenは= 0 ;
16  静的DECLARE_WAIT_QUEUE_HEAD(proc_msg_waitq)。
17  
18  静的 ssize_tののproc_msg_read(構造体ファイル*ファイル、チャー __user * user_buf、
 19               size_tのカウント、loff_t * するPPO)
 20  {
 21          の符号なし整数の RET = 0 22          
23          / * 如果msg_lenは为0、则等待数据* / 
24          wait_event_interruptible(proc_msg_waitq、msg_lenは)。
25      copy_to_user(user_buf、msg_buf、msg_lenは)。
26      RET =msg_lenは、
27      msg_lenは= 0 28      
29      リターンRET;
30  }
 31  
32  静的 ssize_tののproc_msg_write(構造体ファイル*ファイル、CONST  チャー __user * user_buf、
 33               size_tのカウント、loff_t * するPPO)
 34  {
 35      であれば(copy_from_user(msg_buf、user_buf、カウント)){
 36          リターン - EFAULT。
37      }
 38      msg_lenは= 数えます。
39      wake_up_interruptible(&proc_msg_waitq)。
40      
41      リターン回数、
42  }
 43  
44  constの 構造体 file_operations proc_msg_operations = {
 45          .read =     proc_msg_read、
 46          .WRITE = proc_msg_write、
 47  }。
48  
49  静的 int型(proc_msg_init ボイド50  {
 51      / * 在PROC下创建了proc_msg * / 
52      proc_create(" proc_msg "、S_IRUSR、NULL、&proc_msg_operations)。
53      
54      リターン 0 ;    
55  }
 56  
57  静的 ボイド proc_msg_exit(ボイド58  {
 59      remove_proc_entry(" proc_msg " 、NULL);
60  }
 61  
62  module_init(proc_msg_init)。
63  module_exit(proc_msg_exit)。
64 MODULE_LICENSE(" GPL ")。

次のようにスクリプトをコンパイルします。

KERN_DIR = /home/lance/nanopi/linux-4.14/

all:
    make -C $(KERN_DIR) M=`pwd` modules
    rm -rf *.o *.bak *.order *.mod.c *.symvers

clean:
    make -C $(KERN_DIR) M=`pwd` modules clean

obj-m    += proc_msg.o

安装模块测试:

/ # insmod proc_msg.ko 
/ # echo hello > /proc/proc_msg 
/ # cat /proc/proc_msg 
hello

成功输出节点内信息。

おすすめ

転載: www.cnblogs.com/GyForever1004/p/12044101.html