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

发布时间 2023-08-08 17:32:14作者: burlingame

转载: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