首页 > 系统相关 >Linux异步通知---fasync_helper()、kill_fasync()函数介绍与使用

Linux异步通知---fasync_helper()、kill_fasync()函数介绍与使用

时间:2023-08-08 17:33:08浏览次数:46  
标签:__ struct helper int --- fasync key include

转载:Linux异步通知---fasync_helper()、kill_fasync()函数介绍与使用_面朝大海0902的博客-CSDN博客

一、fasync_helper()与kill_fasync()函数
应用程序通过fcntl置FASYNC标志位,触发对应驱动文件的fasync()函数执行(上节有解释原因Linux异步通知—signal()、fcntl()函数介绍与使用),该函数需要使用fasync_helper()函数进行fasync_struct结构体初始化,函数原型:

/*
 * fasync_helper() is used by almost all character device drivers
 * to set up the fasync queue, and for regular files by the file
 * lease code. It returns negative on error, 0 if it did no changes
 * and positive if it added/deleted the entry.
 */
int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
{
    if (!on)
        return fasync_remove_entry(filp, fapp);
    return fasync_add_entry(fd, filp, fapp);
}

之后如果需要往应用层发信号,我们可以使用kill_fasync()函数,我理解该函数最终会往进程pid为fp->fa_file->f_owner->pid的所有线程发送信号。

void kill_fasync(struct fasync_struct **fp, int sig, int band)
{
    if (*fp) {
        rcu_read_lock();
        kill_fasync_rcu(rcu_dereference(*fp), sig, band);
        rcu_read_unlock();
    }
}
调用关系如下:
kill_fasync
    kill_fasync_rcu
        send_sigio
            send_sigio_to_task

看下完整的驱动代码如下:

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

#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <linux/gpio/consumer.h>
#include <linux/platform_device.h>
#include <linux/of_gpio.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/slab.h>
#include <linux/fcntl.h>
/* by 面朝大海0902 */
struct gpio_key{
    int gpio;
    struct gpio_desc *gpiod;
    int flag;
    int irq;
};

static struct gpio_key *ptr_gpio_key;

static int major =0;
static struct class *key_class;
static int g_key_value = 0;
struct fasync_struct *key_fasync;

/* 环形缓冲区 */
#define BUF_LEN 128
static int g_keys[BUF_LEN];
static int r, w;

#define NEXT_POS(x) ((x+1) % BUF_LEN)

static int is_key_buf_empty(void)
{
    return (r == w);
}

static int is_key_buf_full(void)
{
    return (r == NEXT_POS(w));
}

static void put_key(int key)
{
    if (!is_key_buf_full())
    {
        g_keys[w] = key;
        w = NEXT_POS(w);
    }
}

static int get_key(void)
{
    int key = 0;
    if (!is_key_buf_empty())
    {
        key = g_keys[r];
        r = NEXT_POS(r);
    }
    return key;
}

static DECLARE_WAIT_QUEUE_HEAD(key_wait);

static int key_drv_read(struct file *file, char __user *buf, size_t size, loff_t *offset)
{
    printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
    wait_event_interruptible(key_wait, !is_key_buf_empty());
    g_key_value = get_key();
    copy_to_user(buf, &g_key_value, 4);
    return 4;
}
/* by 面朝大海0902 */

static unsigned int key_drv_poll(struct file *fp, poll_table * wait)
{
    printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
    poll_wait(fp, &key_wait, wait);//非阻塞函数
    
    return is_key_buf_empty() ? 0 : POLLIN | POLLRDNORM;
}

static int key_drv_fasync(int fd, struct file *file, int on)
{
    printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
    if(fasync_helper(fd, file, on, &key_fasync) >= 0)
        return 0;
    else
        return -EIO;
}

static struct file_operations key_drv =
{
    .owner = THIS_MODULE,
    .read  = key_drv_read,
    .poll  = key_drv_poll,
    .fasync = key_drv_fasync,
};
/* by 面朝大海0902 */
static irqreturn_t gpio_key_isr(int irq, void *dev_id)
{
    int value;
    struct gpio_key *ptr_gpio_key_temp = dev_id;
    value = gpiod_get_value(ptr_gpio_key_temp->gpiod);
    g_key_value = (ptr_gpio_key_temp->gpio << 8) | value;
    printk(KERN_INFO "g_key_value is %d \r\n", g_key_value);
    put_key(g_key_value);
    wake_up_interruptible(&key_wait);
    kill_fasync(&key_fasync, SIGIO, POLLIN);
    return IRQ_HANDLED;
}

static int key_probe(struct platform_device *pdev)
{
    int count = 0;
    int i=0;
    enum of_gpio_flags flag;
    struct device_node *node = pdev->dev.of_node;
    
    printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
    count = of_gpio_count(node);
    ptr_gpio_key = kzalloc(sizeof(struct gpio_key)*count, GFP_KERNEL);
    
    for(i=0;i<count;i++)
    {
        ptr_gpio_key[i].gpio = of_get_gpio_flags(node, i, &flag);
        if(ptr_gpio_key[i].gpio < 0)
        {
            printk(KERN_ERR "of_get_gpio_flags is err\r\n");
        }
        ptr_gpio_key[i].gpiod = gpio_to_desc(ptr_gpio_key[i].gpio);
        ptr_gpio_key[i].flag = flag & OF_GPIO_ACTIVE_LOW;
        ptr_gpio_key[i].irq = gpio_to_irq(ptr_gpio_key[i].gpio);
        request_irq(ptr_gpio_key[i].irq, gpio_key_isr, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "gpio_key", &ptr_gpio_key[i]);
    }
    
    major = register_chrdev(0, "my_keydrv", &key_drv);
    key_class = class_create(THIS_MODULE, "my_key_class");
    if(IS_ERR(key_class))
    {
        printk(KERN_ERR "class_create is err\r\n");
    }
    
    device_create(key_class, NULL, MKDEV(major, 0), NULL, "my_key%d", 0);
    
    return 0;    
}

static int key_remove(struct platform_device *pdev)
{
    int count = 0;
    int i = 0;
    struct device_node *node = pdev->dev.of_node;

    printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
    count = of_gpio_count(node);

    device_destroy(key_class, MKDEV(major, 0));
    class_destroy(key_class);
    unregister_chrdev(major, "my_keydrv");
    for(i=0;i<count;i++)
    {
        free_irq(ptr_gpio_key[i].irq, &ptr_gpio_key[i]);
    }

    kfree(ptr_gpio_key);
    return 0;    
}
/* by 面朝大海0902 */

static const struct of_device_id my_key[] =
{
    {.compatible = "my,key_driver"},
    {},
};

/* by 面朝大海0902 */
static struct platform_driver key_driver =
{
    .probe  = key_probe,
    .remove = key_remove,
    .driver = 
    {
        .name = "key_gpio",
        .of_match_table = my_key,
    },
};


static int __init my_key_init(void)
{
    int result;
    printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
    result = platform_driver_register(&key_driver);
    return result;
}

/* by 面朝大海0902 */
static void __exit my_key_exit(void)
{
    printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
    platform_driver_unregister(&key_driver);
}

module_init(my_key_init);
module_exit(my_key_exit);

MODULE_LICENSE("GPL");

将驱动程序编译加载,并跑上面的应用程序,测试按键,通过打印可以看到应用程序刚运行起来设置文件FASYNC标志时触发驱动key_drv_fasync()函数调用;
不按键超时打印“by mianchaodahai 0902”,按键后首先中断处理函数gpio_key_isr()被调用并通过kill_fasync()函数向应用发送信号,应用注册的信号处理函数sig_func()被回调到读取key值并触发内核read函数执行,实验结果打印如下:

./key-fasync-test /dev/my_key0
[  311.353315] /home/book/code/test/key-fasync.c key_drv_fasync line is 100
by mianchaodahai 0902
by mianchaodahai 0902
[  318.209253] g_key_value is 33024
[  318.213132] /home/book/code/test/key-fasync.c key_drv_read line is 82
get button : 0x8100
by mianchaodahai 0902
[  318.391082] g_key_value is 33025
[  318.394867] /home/book/code/test/key-fasync.c key_drv_read line is 82
get button : 0x8101
by mianchaodahai 0902

 

标签:__,struct,helper,int,---,fasync,key,include
From: https://www.cnblogs.com/zhiminyu/p/17614969.html

相关文章

  • 【专题】2022-2023年度行业报告&新趋势洞察-消费电子报告PDF合集分享(附原数据表)
    全文链接:https://tecdat.cn/?p=33393在后疫情时代,全球经济和消费力的增长面临巨大考验。2022年,电脑、手机等产品的市场规模出现了小幅收缩调整。然而,在这样的环境下,各种消费电子的细分领域却展现出了强大的韧性。阅读原文,获取专题报告合集全文,解锁文末29份消费电子行业相关报告。......
  • 【专题】年轻人生活消费观察-数码3C篇报告PDF合集分享(附原数据表)
    全文链接:https://tecdat.cn/?p=33393在后疫情时代,全球经济和消费力的增长面临巨大考验。2022年,电脑、手机等产品的市场规模出现了小幅收缩调整。然而,在这样的环境下,各种消费电子的细分领域却展现出了强大的韧性。阅读原文,获取专题报告合集全文,解锁文末29份消费电子行业相关报告。......
  • 我的python路-python基础
    以前用的比较多的语言是java,但是自从从事测试行业以来,发现“通用的语言”竟然是python!呜呼~各种评论都说python学习很简单,but一点也不简单好吗,本次分享就是一个记录,给一些小白同学做参考,大神请帮忙指正错误~~本期学习笔记:1、python语言使用变量直接赋值即可,不用声明类型,但是使......
  • ghost-on-docker、nginx-proxy-manager install
    #ghost安装dockerrun-d--nameblog-ghost-eNODE_ENV=development-edatabase__connection__filename='/var/lib/ghost/content/data/gggdb.db'-p8080:2368-v/path/to/ghost/blog:/var/lib/ghost/contentghost#nginx-proxy-manager中文安装docker-compose.yml......
  • kube-proxy 三种模式分析
    kube-proxy三种模式分析kubernetes上面的service资源的实现方式是由kube-proxy提供的模式决定的kube-proxy提供三种模式:userspace(Kubernetes1.2版本之前)、iptables、ipvs(推荐的)如果不满足ipvs时,会自动降为iptables模式再讲三种模式前,先简单说下Service的工作原理;在......
  • new Thread().start(); - 多线程练习
     用Java创建一个线程是这样的:Threadthread=newThread();要启动Java线程,您将调用其start()方法,如下所示:   thread.start();此示例未指定要执行的线程的任何代码。线程启动后会立即再次停止。所以要往线程里写入代码。Threadthread=newThread(){@Override......
  • CF-1005A Tanya and Stairways
    TanyaandStairways#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;#defineIOSios::sync_with_stdio(0);cin.tie(0);cout.tie(0);//#defineiosios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);constintN=1e4+10......
  • switch-case 和 if-else 申明相同变量时报错
    switch-case和if-else在分支处理上的不同switchcase不同分支里申明同一个变量会报错,因为swicthcase不同的分支仍处于同一个作用域scope。但是ifelse却没有注意的问题。//Cannotredeclareblock-scopedvariable'a'.switch(num){case1:consta='a'ret......
  • 一个赚米的小方法200-100,学生,宝妈,在线兼职,远程兼职
    一个赚钱的小方法 一天200~1000 招兼职:大学生 高中生 上班族 在家带娃的宝妈 只要想赚钱的人都可以做 并且不耽误你的本职工作 操作简单 对手机了解 要求勤快一点做多做少都是你自己的 有智能手机就能做!不收任何费用!!! ......
  • WebDAV之π-Disk派盘 + Evermusic
    Evermusic:iPhone或iPad的音乐播放器和下载器。音频均衡器,低音增强器,ID3标签编辑器,播放列表管理器。支持最流行的音频格式:MP3,AAC,M4A,WAV,AIFF,M4R。有了这个程序,您可以创建自己的音乐流媒体服务。只需将您的音乐库移至云服务,然后直接从那里收听音乐。您现在可以在线上获取所有音乐,并且......