No se puede acceder a la matriz de estructura desde la aplicación de espacio de usuario

In a common header, I am defining the struct as:

#define query_arg_t queryForItems

typedef struct {
   char item[50];
   char status[10];     
} queryForItems;

In the kernel driver, we define:

// inicializar

queryForItems queryForItemsArray[] = {
{
.item = "A",
.status = "TRUE"
}, 
{
.item = "B",
.status = "TRUE"
},  
};

Using ioctl in driver

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;

:

In the user app, we define the get function as :

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

However, I am not able to access the struct array from the user space app.

preguntado el 12 de febrero de 14 a las 06:02

You defined my_ioctl and calling ioctl why? -

my_ioctl() is in kernel space, ioctl() is mapped when fd = open(file_name, O_RDWR) is called. -

@Babbit q requires memory to be allocated -

Can u define an upper bound for the number of elements in the structure. I mean the maximum possible at any point of time. If you can define the max, you can always have the structure allocated for max, but have a variable to indicate the valid count. Since, the pointers move from user space to kernel space, i think this is the only way to do it. -

I saw your message in Chat, can u join again in the chat room? chat.stackoverflow.com/rooms/47079/… -

1 Respuestas

[Solution suggested by Sakthi Kumar]

En un encabezado común:

add

#define MAX_OBJ 50

 typedef struct {
int num_items;
queryForItems items[MAX_OBJ];
 } query_arg_t;

en conductor

    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;

In user app:

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

Respondido 29 Jul 19, 01:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.