2016-07-25 97 views
1

我正在開發兩種不同的Linux內核模塊(模塊A,模塊B)。的Linux模塊交互功能參考

模塊A使用模塊B的功能 實際上,使用extern_symbol和module.symvers對我來說很明顯。

,但我想知道如何處理這種情況下,模塊A使用模塊B的功能,並在同一時間模塊B使用模塊A.

回答

2

可以與第三內核模塊解決這個(或靜態編譯),這將導出由兩個模塊,這將是短截線,直到這兩個模塊的負載中使用的函數 - 然後,每個模塊將註冊其回調。

代碼示例

模塊集成

static int func1(int x); 
static int func2(int y); 

int xxx_func1(int x) 
{ 
    if (func1) 
     return func1(x); 
    return -EPERM; 
} 
EXPORT_SYMBOL(xxx_func1); 

int xxx_func2(int x) 
{ 
    if (func2) 
     return func2(x); 
    return -EPERM; 
} 
EXPORT_SYMBOL(xxx_func2); 

int xxx_register_func(int id, int (*func)(int)) 
{ 
    if (id == 1) 
      func1 = func; 
    else if (id ==2) 
      func2 = func; 

    return 0; 
} 
EXPORT_SYMBOL(xxx_register_func); 

int xxx_unregister_func(int id) 
{ 
    if (id == 1) 
      func1 = NULL; 
    else if (id ==2) 
      func2 = NULL; 

    return 0; 
} 
EXPORT_SYMBOL(xxx_unregister_func); 

模塊1

static int module1_func1(int x) 
{ 
    ... 
} 

static int module1_do_something(...) 
{ 
    ... 
    xxx_func2(123); 
    ... 
} 

static int module1_probe(...) 
{ 
    ... 
    xxx_register_func(1, module1_func1); 
    ... 
} 

與同爲模塊2 ...

當然,你應該添加互斥防範功能註冊,處理邊緣ca SES等

+1

謝謝,這是回答我真正想要的! –

2

你不能。模塊負載一次一個,並且所有符號具有當加載模塊解決,所以一對模塊的引用,其在每個其它符號將是不可能的加載。

找到某種方式來組織這些模塊避免了循環引用。

+1

謝謝你的好意。 –