문제

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);
도움이 되었습니까?

해결책 2

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

다른 팁

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

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top