从用户空间应用程序调用内核空间中的用户定义函数 [英] Calling a user defined function in kernel space from user space application program

查看:84
本文介绍了从用户空间应用程序调用内核空间中的用户定义函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在设备驱动程序中编写了一个用户定义的函数,我想从用户空间应用程序中调用它.我该如何实现?

I have written a user defined function in my device driver and I want to call it from user space application program. How do I achieve this?

我定义的用户定义函数是内核定义函数以外的任何函数.在struct file_operations中定义的指针,如下所示.

What I mean by user defined function is, any function other than the kernel defined functions. Pointers to which is defined in struct file_operations, as below.

struct file_operations {
       struct module *owner;
       loff_t (*llseek) (struct file *, loff_t, int);
       ssize_t (*read) (struct file *, char *, size_t, loff_t *);
       ssize_t (*write) (struct file *, const char *, size_t, loff_t *);
       int (*readdir) (struct file *, void *, filldir_t);
       unsigned int (*poll) (struct file *, struct poll_table_struct *);
       int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
       int (*mmap) (struct file *, struct vm_area_struct *);
       int (*open) (struct inode *, struct file *);
       int (*flush) (struct file *);
       int (*release) (struct inode *, struct file *);
       int (*fsync) (struct file *, struct dentry *, int datasync);
       int (*fasync) (int, struct file *, int);
       int (*lock) (struct file *, int, struct file_lock *);
         ssize_t (*readv) (struct file *, const struct iovec *, unsigned long,
          loff_t *);
         ssize_t (*writev) (struct file *, const struct iovec *, unsigned long,
          loff_t *);
    };

例如在我的驱动程序中,我具有以下要求,

For eg. in my driver I've the following,

struct file_operations fops = {
.read = my_read,
.write = my_write,
};

,我可以使用调用readwrite从用户空间应用程序中调用这些函数.

and I can call these functions from user space application program using the calls read and write.

我在内核源代码中也有一个名为user_defined的函数,我的问题是如何从用户空间程序调用此函数?

I also have a function named user_defined in kernel source and my question is how do I call this from user space program?

推荐答案

好吧,您所拥有的通常称为"syscall".我建议您阅读内核开发人员文档,以了解如何公开一个新的syscall.在用户空间方面,libc为您提供了一个函数syscall,您可以使用它调用内核空间.但是通常您会在其周围写一个包装器.

Well, what you have there is normally referred to as "syscall". I suggest you read into the kernel developer documentation on how to expose a new syscall. On the userspace side, the libc provides you a function syscall, that you can use to call into kernel space. But normally you write a wrapper around it.

但是应避免引入新的系统调用.调用内核空间的首选方法是使用sysfs并将用户空间可调用函数作为其中的文件公开.

However introducing new syscalls should be avoided. The preferred way to call into kernel space is to use sysfs and expose the user space callable functions as files therein.

这篇关于从用户空间应用程序调用内核空间中的用户定义函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆