0

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.

  • You defined my_ioctl and calling ioctl why? – Chinna Feb 12 '14 at 06:54
  • my_ioctl() is in kernel space, ioctl() is mapped when fd = open(file_name, O_RDWR) is called. – Moirisa Dikaiosýni Feb 12 '14 at 06:57
  • 2
    @Babbit `q` requires memory to be allocated – Sakthi Kumar Feb 12 '14 at 07:01
  • @Sakthi Kumar, that pose a challenge, the struct array is on the other side. It has to be calculated and passed back. I am unsure how to do so. – Moirisa Dikaiosýni Feb 12 '14 at 07:04
  • 1
    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. – Sakthi Kumar Feb 12 '14 at 07:08
  • @Sakthi Kumar, the 2nd problem is that even if I arbitary give a value, eg. q = malloc(500), it still does not print. – Moirisa Dikaiosýni Feb 12 '14 at 07:08
  • @Sakthi Kumar, can you give an example? Could you check the chat for the links? – Moirisa Dikaiosýni Feb 12 '14 at 07:10
  • 1
    I saw your message in Chat, can u join again in the chat room? http://chat.stackoverflow.com/rooms/47079/discussion-between-sakthi-kumar-and-babbit – Sakthi Kumar Feb 12 '14 at 07:11

1 Answers1

0

[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); 
Emma
  • 27,428
  • 11
  • 44
  • 69