Question

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:

// initialize

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.

Was it helpful?

Solution

[Solution suggested by Sakthi Kumar]

In a common header:

add

#define MAX_OBJ 50

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

In driver

    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); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top