2014-02-12 30 views
0

訪問結構數組中的一個共同的頭,我定義爲結構:無法從用戶空間應用

#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"); 
} 
} 

不過,我無法從用戶空間應用程序訪問結構數組。

+0

你定義my_ioctl並調用ioctl爲什麼? – Chinna

+0

my_ioctl()在內核空間中,當調用fd = open(file_name,O_RDWR)時,映射ioctl()。 – Babbit

+1

@Babbit'q'需要分配內存 –

回答

0

[由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);