[英]How can i pass the address of a buffer in user space to kernel
I want to use copy_to_user() to copy some information of a platform net device to a buffer in user space.But i don't how can kernel knows the "to" pointer which used by copy_to_user(). 我想使用copy_to_user()将平台网络设备的某些信息复制到用户空间中的缓冲区。但是我不知道内核如何知道copy_to_user()使用的“ to”指针。 The driver's ioctl() needs struct ifreq as a parameter.And i know i can initialize the ifreq.name to find the driver's ioctl().But how can i pass the "to" pointer to kernel exactly the driver'ioctl()? 驱动程序的ioctl()需要结构ifreq作为参数。我知道我可以初始化ifreq.name来查找驱动程序的ioctl()。但是如何将“ to”指针精确地传递给驱动程序的ioctl()?
struct ifreq looks like this: struct ifreq看起来像这样:
struct ifreq {
char ifr_name[IFNAMSIZ];/* Interface name */
union {
struct sockaddrifr_addr;
struct sockaddrifr_dstaddr;
struct sockaddrifr_broadaddr;
struct sockaddrifr_netmask;
struct sockaddrifr_hwaddr;
short ifr_flags;
int ifr_ifindex;
int ifr_metric;
int ifr_mtu;
struct ifmapifr_map;
char ifr_slave[IFNAMSIZ];
char ifr_newname[IFNAMSIZ];
char * ifr_data;
};
};
If you are implementing an existing ioctl command, you must figure out which of the members in the union you're supposed to use, both in user space and the kernel. 如果要实现现有的ioctl命令,则必须确定用户空间和内核中应使用联合中的哪些成员。
If you're implementing your own ioctl command, you can use ifr_data
. 如果要实现自己的ioctl命令,则可以使用ifr_data
。 The caller (in user space) sets the member to point to a local buffer, which you'd fill in with copy_to_user() in the kernel (ie ifr_data is the to that you're looking for.) 主叫方(用户空间)设置为指向一个本地缓冲区,你会填用copy_to_user()在内核中的成员(即ifr_data是到你要找的。)
ie user space does 即用户空间确实
char buf[128];
struct ifreq req;
strcpy(req.ifr_name,"eth0");
req.ifr_data = buf;
ioctl(fd, SIOCMYIOCTL, &ifr);
Here the buffer is just a fixed size array, if you need more flexibility you can ofcourse use a struct, as long as user space and your kernel ioctl() agrees on what ifr_data
is. 这里的缓冲区只是一个固定大小的数组,如果您需要更大的灵活性,则可以使用结构,只要用户空间和内核ioctl()同意ifr_data
是什么。
struct my_ioctl_data {
int a, b, c;
};
struct my_ioctl_data data;
struct ifreq req;
strcpy(req.ifr_name,"eth0");
req.ifr_data = (char*)&data;
ioctl(fd, SIOCMYIOCTL, &ifr);
In the platform net device driver change the read method : 在平台网络设备驱动程序中更改读取方法:
static ssize_t net_device_read(arg1,char __user *user_buffer,size_t count,loff_t *position)
{
copy_to_user(user_buffer, position,count) != 0
}
Access this buffer from the user space using cat command 使用cat命令从用户空间访问此缓冲区
cat /dev/net-device
>> string to pass
read more about this here : http://www.codeproject.com/Articles/112474/A-Simple-Driver-for-Linux-OS 在此处阅读有关此内容的更多信息: http : //www.codeproject.com/Articles/112474/A-Simple-Driver-for-Linux-OS
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.