How to export structure between two kernel modules using EXPORT_SYMBOL or equivalent?

I have a kernel module that has this structure:

struct test {
    int a;
    int b;
    .....
}

I created an array of instances of this structure as:

struct test foo[8];

I want to export this structure or the "foo" array using EXPORT_SYMBOLand access foo[0].ain another kernel module.

I tried EXPORT_SYMBOL(foo);from the provider module and extern struct test * foo;in the receiver module, but I cannot access the variable. Indicate where I am mistaken.

Here is another code:

Kernel Module 1:

#include <....>
#include "test_config.h"
....
MODULE_LICENSE("GPL");

struct test {
int a;
int b;
.....
}

test_t foo[8];
//EXPORT_SYMBOL(foo);

/*Code to create sysctl variables out of these members of the struct test*/

int init_module(void)
{
    printk(KERN_INFO "Hello World\n");
    return 0;
}

void cleanup_module(void)
{
    printk(KERN_INFO "Goodbye Cruel World\n");
}

Kerne 2 module:

#include <linux/module.h>
#include <linux/kernel.h>
#include "test_config.h"

int init_module(void)
{
    test_t foo[8];
    printk ("Value of foo is :: %d\n", foo[0].a);
    foo[0].a++;
    printk(KERN_INFO "Hello Again World\n");
    return 0;
}

void cleanup_module(void)
{
    printk(KERN_INFO "Goodbye Again Cruel World\n");
}

Here is the header file with the structure definition:

#ifndef __TEST_CONFIG
#define __TEST_CONFIG

typedef struct test
{
    int a;
    int b
    int c;
    int d;
    float e;
}test_t;

#endif
+4
source share
3 answers

, Mr.32, - . a, b, c ...

.

, :

#ifndef __TEST_CONFIG
#define __TEST_CONFIG

struct test
{
    int a;
    int b
    int c;
    int d;
    float e;
};

extern struct test foo[8];

#endif

:

struct test foo[8];
EXPORT_SYMBOL(foo);

:

extern struct test foo[8];

, , foo[0].a;.

+3

A

struct test foo[8];

EXPORT_SYMBOL(foo);

B,

extern struct test foo[8];

, B A.


, ,

a

struct test foo[8];
struct *test temp = &foo(0);
EXPORT_SYMBOL(temp);

B

extern struct *test temp;

temp[0].a


. http://lxr.free-electrons.com/source/sound/core/init.c?v=2.6.35;a=arm#L48

 48 struct snd_card *snd_cards[SNDRV_CARDS];
 49 EXPORT_SYMBOL(snd_cards);

281 extern struct snd_card *snd_cards[SNDRV_CARDS];

http://lxr.free-electrons.com/source/include/sound/core.h?v=2.6.35#L281


#include <....>
#include "test_config.h"
....
MODULE_LICENSE("GPL");


test_t foo[8];
EXPORT_SYMBOL(foo);


int init_module(void)
{
    printk(KERN_INFO "Hello World\n");
    foo[0].a = 10;  // set some value.
    return 0;
}

void cleanup_module(void)
{
    printk(KERN_INFO "Goodbye Cruel World\n");
}

moduel 2

#include <linux/module.h>
#include <linux/kernel.h>
#include "test_config.h"

extern test_t foo[8];

int init_module(void)
{
    printk ("Value of foo is :: %d\n", foo[0].a); // it should print 10
    printk(KERN_INFO "Hello Again World\n");
    return 0;
}

void cleanup_module(void)
{
    printk(KERN_INFO "Goodbye Again Cruel World\n");
}

.

#ifndef __TEST_CONFIG
#define __TEST_CONFIG

typedef struct test
{
    int a;
    int b
    int c;
    int d;
    float e;
}test_t;

#endif

1, 2.

+8

When compiling a module of a module, the Module.symvers file is generated in the same directory (along with the default source files). If the characters are exported and imported in another source with a different directory, then the Module.symvers file must be copied to the directory of other source files. Several times, in order to have a clean build, these files are deleted. If characters are exported, then deleting files will not allow external characters.

0
source

All Articles