Rachit Tayal
Rachit Tayal

Reputation: 1292

how to define a global variable inside linux kernel?

I am new to kernel programming and trying to implement a system call in linux kernel 3.19 which keeps track of the processes in a linked list. So every time the system call is invoked from user-space (by some wrapper function) a new process has to be added to that list. My system call looks something like

asmlinkage long sys_newcall(pid_t pid)
{
    /*
     * mytasks is the name of the structure
     * kmalloc() is invoked to create an instance 
     */
    struct mytasks newTask = kmalloc(sizeof(struct mytasks), GFP_KERNEL);
    /* various checks */
    /* now adding the new instance to the list */

    list_add_tail(&(newTask->list),&(mylist->list));
    /* i have put list_head struct in my own structure to make use of above interface */
}

Now the mylist variable used above should be defined global so as to maintain the list for subsequent system calls. How to accomplish that? Do I have to declare mylist variable in linux/init/main.c or I can simply use EXPORT_GLOBAL. I also read about using extern but couldn't figure out where to declare and define the variable.

Upvotes: 2

Views: 4472

Answers (1)

Kalu
Kalu

Reputation: 290

Using EXPORT_SYMBOL will be best as this will be visible to loadable modules also and declare it int the file in which you are using or in a locally header file

Upvotes: 0

Related Questions