2

In the header file:

typedef struct {
char* a;         
int allowed;

struct suit {
        struct t {
                char* option;
                int count;      
        } t;

        struct inner {
                char* option; 
                int count;      
        } inner;        
} suit;
} contain;

typedef struct {
       contain info;
} query_arg_t;

In the kernel module,

// initialize

static const contain _vector = { 
.a = "John",
.allowed = 1,
.suit = {
    .t = {
        .option = "ON",
        .count = 7
    },
    .inner = {
        .option = "OFF (*)",
        .count = 7
    }           
}
};

However, as we try:

query_arg_t q;

    q.info = kmalloc(sizeof(_vector), GFP_KERNEL);

We will get this error: error: incompatible types when assigning to type ‘contain’ from type ‘void *’

The above error is solved by @SunEric and @Sakthi Kumar.

         q.info = kmalloc(sizeof(_vector), GFP_KERNEL);
        memcpy(&(q.info), &(_vector), sizeof(_vector));

It seems ok now. It builds but when it runs to that part, it states that the kernel stack is corrupted. after trying to execute:

     printf("option: %s \n", q.info->suit.t.option); 
     printf("option: %s \n", q.info->suit.t.option);

[Updated: Solved]

@Sakthi Kumar solved it by:

   //removing kmalloc
   // commenting out: q.info = &_vector;
   memcpy(&(q.info), &(_vector), sizeof(_vector));

printf("option: %s \n", q.info.suit.t.option);
printf("option: %s \n", q.info.suit.inner.option);

3 Answers3

2

void * kmalloc (size_t size, int flags);

kmalloc returns type void * which you are trying to assign to contain is in-correct.

Change to,

typedef struct {
   contain *info; // info is a pointer to struct contain
} query_arg_t;

query_arg_t q;
q.info = kmalloc(sizeof(_vector), GFP_KERNEL);

Answer to followup question:

q.info is a pointer to structure pointing to contain. You need array operator -> to access structure members through pointer. So try below options to access info as,

q.info->stat.t.option

Or

(*q.info).stat.t.option
Sunil Bojanapally
  • 12,528
  • 4
  • 33
  • 46
1

Your structure to be of the form

typedef struct {
       contain *info;
} query_arg_t;

You are trying to assign a pointer (kmalloc returns void *) to a struct variable (contain).

Sakthi Kumar
  • 3,047
  • 15
  • 28
  • Thank you, @Sakthi Kumar. Could you also guide me on the following question emended above? – Moirisa Dikaiosýni Feb 07 '14 at 16:20
  • 1
    @Babbit since `info` is a pointer you have to access it like `printf("option: %s \n", q.info->suit.t.option);` – Sakthi Kumar Feb 07 '14 at 18:24
  • It builds but when it runs to that part, it states some kernel stack problem. – Moirisa Dikaiosýni Feb 08 '14 at 02:08
  • 1
    @Babbit I think you do not have to `kmalloc` since u already have the `_vector` as `static`, u can try this `q.info = &_vector`. remove the `kmalloc` and change the next statement to the one mentioned and see if that fixes the issue. – Sakthi Kumar Feb 08 '14 at 05:34
  • The kernel is not crashing anymore, but the 2 lines is not printing. printf("option: %s \n", q.info->suit.t.option); printf("option: %s \n", q.info->suit.inner.option); – Moirisa Dikaiosýni Feb 08 '14 at 07:56
  • 1
    @Babbit Well, this seems that the pointers in the `struct` is making problems. can u plz make all the `char *` to say `char a[20]` or the max num of chars required and check this? – Sakthi Kumar Feb 08 '14 at 08:11
  • I just did a dmesg. It states "[20868.612110] query_app[5818]: segfault at ffffffffa032c06c ip 00007fa9b6ccb3b1 sp 00007fff6a624120 error 5 in libc-2.15.so[7fa9b6c80000+1b5000]". – Moirisa Dikaiosýni Feb 08 '14 at 08:52
  • 1
    let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/47079/discussion-between-sakthi-kumar-and-babbit) – Sakthi Kumar Feb 08 '14 at 08:57
0

The C language specification prohibits declaring struct types inside other structs without declaring a member of that type.

Michael
  • 979
  • 6
  • 13