免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1078 | 回复: 2
打印 上一主题 下一主题

[内核模块] 用户空间跟内核空间沟通问题 [复制链接]

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:49:03
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-03-16 21:30 |只看该作者 |倒序浏览
本帖最后由 shihyu 于 2016-03-16 21:50 编辑

我代码作用是想从 用户空间 appln  process 透过 ioctl  传 用户空间 process 的 pid 到 内核 , 内核再透过 send_sig 函数不断发 SIGUSR2 给用户空间的  appln prcoess , 当我不想kernel 再发  SIGUSR2 signal 我再透过 ioctl 让 kernel 停止发 SIGUSR2
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/fs.h>
  4. #include <linux/device.h>
  5. #include <linux/kernel.h>
  6. #include <linux/oom.h>
  7. #include <linux/sched.h>
  8. #include <linux/delay.h>
  9. #include <linux/kthread.h>
  10. #include "header.h"

  11. MODULE_LICENSE("Dual BSD/GPL");

  12. static int major_no;
  13. static int vrmonitor_pid;
  14. static int cccc = 100;
  15. static int send_sig_flag = 1;

  16. static struct task_struct* brook_tsk;
  17. static int vrmonitor_sig_handler(void* arg);

  18. static int vrmonitor_sig_handler(void* arg)
  19. {
  20.     struct task_struct* p;
  21.     struct pid* pp;

  22.     printk(KERN_EMERG "cccc addr=%p ,cccc=%d\n", &cccc, cccc++);

  23.     pp = find_vpid(vrmonitor_pid);
  24.     p = pid_task(pp, PIDTYPE_PID);

  25.     while (send_sig_flag) {
  26.         msleep(1000);
  27.         send_sig(SIGUSR2, p, 0);
  28.         printk(KERN_EMERG "vrmonitor_sig_handler current pid=%d send to PID=%d \n", current->pid, vrmonitor_pid);
  29.     }

  30.     send_sig_flag = 1;

  31.     return 0;
  32. }

  33. static int device_open(struct inode* inode, struct file* file)
  34. {
  35.     printk(KERN_EMERG "pid=%d, Node Opened\n", current->pid);
  36.     return 0;
  37. }

  38. int device_ioctl(struct file* filp,
  39.                  unsigned int cmd,
  40.                  unsigned long args)
  41. {
  42.     int ret;

  43.     switch (cmd) {
  44.     case IOCTL_CMD:
  45.         printk(KERN_EMERG "IOCTL_CMD");
  46.         break;

  47.     case IOCTL_SEND_PID:
  48.         printk(KERN_EMERG "appln PID=%u , current kernel pid=%d\n", (unsigned int)args, current->pid);
  49.         vrmonitor_pid = (unsigned int)args;
  50.         brook_tsk = kthread_create(vrmonitor_sig_handler, NULL, "brook");

  51.         if (IS_ERR(brook_tsk)) {
  52.             ret = PTR_ERR(brook_tsk);
  53.             brook_tsk = NULL;
  54.             goto out;
  55.         }

  56.         wake_up_process(brook_tsk);
  57.         break;

  58.     case IOCTL_STOP_SIG:
  59.         send_sig_flag = 0;
  60.         printk(KERN_EMERG "IOCTL_STOP_SIG send_sig_flag=%d\n", send_sig_flag);
  61.         break;

  62.     default:
  63.         printk(KERN_EMERG "Illegal ioctl command word\n");
  64.         break;
  65.     }

  66.     return 0;

  67. out:
  68.     return ret;
  69. }

  70. static int device_release(struct inode* inode, struct file* file)
  71. {
  72.     printk(KERN_EMERG "Module Released \n");
  73.     return 0;
  74. }

  75. static struct class* my_class;

  76. static struct file_operations fops = {
  77.     .open = device_open,
  78.     .release = device_release,
  79.     .unlocked_ioctl = device_ioctl
  80. };

  81. static int hello_init(void)
  82. {
  83.     major_no = register_chrdev(0, DEVICE_NAME, &fops);
  84.     printk(KERN_EMERG "Module Major No : %d\n", major_no);

  85.     my_class = class_create(THIS_MODULE, DEVICE_NAME);
  86.     device_create(my_class, NULL, MKDEV(major_no, 0), NULL, DEVICE_NAME);
  87.     printk(KERN_EMERG "Module loaded in kernel\n");
  88.     return 0;
  89. }

  90. static void hello_exit(void)
  91. {
  92.     printk(KERN_EMERG "Device is Released or closed \n");
  93.     device_destroy(my_class, MKDEV(major_no, 0));
  94.     class_unregister(my_class);
  95.     class_destroy(my_class);
  96.     unregister_chrdev(major_no, DEVICE_NAME);
  97. }

  98. module_init(hello_init);
  99. module_exit(hello_exit);
复制代码
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <sys/ioctl.h>
  7. #include <signal.h>
  8. #include "header.h"

  9. static int fd;

  10. void stop_sig(int sig)
  11. {
  12.     unsigned int sig_flag = 0;

  13.     ioctl(fd, IOCTL_STOP_SIG, sig_flag);
  14.     printf("stop_sig=%d, pid=%d\n", sig, getpid());
  15.     (void) signal(SIGINT, SIG_DFL);

  16. #if 0
  17.     kill(getpid(), SIGINT);
  18. #endif
  19. }


  20. void signal_handler(int signum)
  21. {
  22.     printf("signal_handler %d, pid=%d\n", signum, getpid());

  23.     if (signum == SIGUSR2) {
  24.         printf("SIGUSR2\n");
  25.     } else if (signum == SIGUSR1) {
  26.         printf("SIGUSR1\n");
  27.     }
  28. }

  29. int main()
  30. {
  31.     pid_t pid = getpid();

  32.     printf("PID=%u\n", pid);

  33.     signal(SIGUSR2, signal_handler);
  34.     signal(SIGINT, stop_sig);

  35.     fd = open(DEVICE_PATH, O_RDWR);
  36.     if (fd == -1) {
  37.         printf("open fail\n");
  38.         exit(-1);
  39.     }

  40.     ioctl(fd, IOCTL_SEND_PID, pid);
  41.     printf("Ioctl executed\n");

  42.     while(1) {}

  43.     close(fd);
  44.     return 0;
  45. }
复制代码
运行步骤
sudo insmod sample.ko
sudo ./appln


下面是打印出来的log


// user space log
PID=6897
Ioctl executed
signal_handler 12, pid=6897
SIGUSR2
signal_handler 12, pid=6897
SIGUSR2
^Cstop_sig=2, pid=6897  // ctrl + c 发往 kernel 让kernel停止发送 SIGUSR2


// kernel log
[  681.335568] vrmonitor_sig_handler current pid=6898 send to PID=6897
[  682.339540] vrmonitor_sig_handler current pid=6898 send to PID=6897
[  682.871588] IOCTL_STOP_SIG send_sig_flag=0  // kernel 收到准备停止
[  682.871646] Module Released
[  683.343531] vrmonitor_sig_handler current pid=6898 send to PID=6897



我再一般pc上运行正常 ,


可是再 vmware  ubuntu kernel 只发两次 SIGUSR2 , 就打印出 Module Released
下面 log ,  user space 的 appln  prcoess 就莫名其妙结束 , 我还没按 ctrl+c
只是启动 sudo ./appln 这样




[  681.335568] vrmonitor_sig_handler current pid=6898 send to PID=6897
[  682.339540] vrmonitor_sig_handler current pid=6898 send to PID=6897
[  682.871646] Module Released

example.tar.bz2

47.81 KB, 下载次数: 1

8987

论坛徽章:
9
辰龙
日期:2014-08-18 20:38:42未羊
日期:2014-09-04 08:50:45丑牛
日期:2014-09-06 00:12:55寅虎
日期:2014-12-22 20:50:56摩羯座
日期:2015-01-14 22:28:15巳蛇
日期:2015-01-23 20:39:272015年辞旧岁徽章
日期:2015-03-03 16:54:1515-16赛季CBA联赛之青岛
日期:2016-03-13 23:37:1915-16赛季CBA联赛之深圳
日期:2016-03-29 18:52:38
2 [报告]
发表于 2016-03-17 00:52 |只看该作者
收到另的信号退出了呀。GDB一个就知道了呀。或者unlimit -c unlimited,产生个core文件看看

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:49:03
3 [报告]
发表于 2016-03-17 01:29 |只看该作者
本帖最后由 shihyu 于 2016-03-17 01:29 编辑

ok... 我明天试试看 gdb
thanks
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP