简体   繁体   中英

Linux kernel module programming

this is my first time posting a question on here, so be gentle. I'm delving into the interesting world of operating systems and wanted to try my hand at trying to write a linux kernel module. I came across this exercise in a textbook on the subject and wrote the following code in C:

#include<linux/list.h>
#include<linux/init.h>
#include<linux/kernel.h>
#include<linux/module.h>
#include<linux/types.h>
#include<linux/slab.h>

struct birthday {
    int day;
    int month;
    int year;
    struct list_head list;
}

static LIST_HEAD(birthday_list);

int simple_init(void) {
    struct birthday *ptr;
    int i;
    for(i = 0; i < 5; i++) {
        // create 5 birthday structs and add them to the list

        struct birthday *person;
        person = kmalloc(sizeof(*person), GFP_KERNEL);
        person->day = 22;
        person->month = 11;
        person->year = 1981;
        INIT_LIST_HEAD(&person->list);

        list_add_tail(&person->list, &birthday_list);
    }

    list_for_each_entry(ptr, &birthday_list, list) {
        // print the info from the structs to the log
        printk(KERN_INFO "%d, %d %d", ptr->month, ptr->day, ptr->year);
    }

    return 0;
}


void simple_exit(void) {
    struct birthday *ptr, *next;
    list_for_each_entry_safe(ptr, next, &birthday_list, list) {
        // delete structs and return memory
        list_del(&ptr->list);
        kfree(ptr);
    }
}

module_init(simple_init);
module_exit(simple_exit);

The issue I'm having is that the above code won't compile and I get the following errors:

In file included from /home/parv112281/Documents/operating-systems/chap-2/list-struct/list-struct.c:1:0:
include/linux/list.h:22:2: error: expected ‘;’, identifier or ‘(’ before ‘struct’
  struct list_head name = LIST_HEAD_INIT(name)
  ^
/home/parv112281/Documents/operating-systems/chap-2/list-struct/list-struct.c:15:8: note: in expansion of macro ‘LIST_HEAD’
 static LIST_HEAD(birthday_list);
        ^
make[2]: *** [/home/parv112281/Documents/operating-systems/chap-2/list-struct/list-struct.o] Error 1
make[1]: *** [_module_/home/parv112281/Documents/operating-systems/chap-2/list-struct] Error 2
make[1]: Leaving directory `/usr/src/linux-headers-3.16.0-30-generic'
make: *** [all] Error 2

The error that the compiler seems to be complaining about is in the list.h header file that defines a doubly linked list data structure for the linux kernel. I'm doubting there is an actual error in the kernel code here and I suspect that i'm just utilizing some function or macro here incorrectly. I would appreciate any help in fixing this problem.

Thanks, Parv

Two issues:

  1. To use kernel linked list, you need to include linux/list.h

  2. You forgot a ; when declaring struct birthday.

So this should work:

#include <linux/list.h>

struct birthday {
    int day;
    int month;
    int year;
    struct list_head list;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM