无法从用户空间应用
问题描述:
访问结构数组中的一个共同的头,我定义为结构:无法从用户空间应用
#define query_arg_t queryForItems
typedef struct {
char item[50];
char status[10];
} queryForItems;
在内核驱动程序,我们定义:
//初始化
queryForItems queryForItemsArray[] = {
{
.item = "A",
.status = "TRUE"
},
{
.item = "B",
.status = "TRUE"
},
};
在驱动程序中的ioctl
static long my_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
{
query_arg_t *q;
switch (cmd)
{
case QUERY_GET_VARIABLES:
memcpy(&q, (&queryListForItems), sizeof(queryForItemsArray));
if (copy_to_user((query_arg_t *)arg, &q, sizeof(query_arg_t))) {
return -EACCES;
}
break;
:
在用户的应用程序,我们定义get函数为:
void get_vars(int fd)
{
query_arg_t *q;
//q.info = kmalloc(sizeof(???), GFP_KERNEL); // may require malloc
if (ioctl(fd, QUERY_GET_VARIABLES, &q) == -1)
{
perror("query_apps ioctl get");
} else {
printf("=====================\n");
printf("option: %s \n", q[1].Item);
printf("=====================\n");
}
}
不过,我无法从用户空间应用程序访问结构数组。
答
[由Sakthi库马尔解决在一个共同的报头:
添加
#define MAX_OBJ 50
typedef struct {
int num_items;
queryForItems items[MAX_OBJ];
} query_arg_t;
在驾驶员
case QUERY_GET_VARIABLES:
q = kmalloc(sizeof(query_arg_t), GFP_KERNEL);
q->num_items = 3;
memcpy(q->items, queryForItems, sizeof(queryForItems) * q->num_items);
if (copy_to_user((query_arg_t *)arg, q, sizeof(query_arg_t))) {
return -EACCES;
}
break;
在用户应用程式:
query_arg_t *q;
q = malloc(sizeof(query_arg_t));
if (ioctl(fd, QUERY_GET_VARIABLES, q) == -1)
{
perror("query_apps ioctl get");
} else {
printf("=====================\n");
printf("option: %s \n", q->items[i].status);
你定义my_ioctl并调用ioctl为什么? – Chinna
my_ioctl()在内核空间中,当调用fd = open(file_name,O_RDWR)时,映射ioctl()。 – Babbit
@Babbit'q'需要分配内存 –