खोज…


एक खाली कर्नेल मॉड्यूल

#include <linux/init.h>
#include <linux/module.h>

/**
 * This function is called when the module is first loaded.
 */    
static int __init hello_kernel_init(void)
{
    printk("Hello, World!\n");
    return 0;
}

/**
 * This function is called when is called if and when the module is unloaded.
 */
static void __exit hello_kernel_exit(void)
{
    printk("Goodbye, cruel world...\n");
}

/* The names of the init/exit functions are arbitrary, and they are bound using the following macro definitions */
module_init(hello_kernel_init);
module_exit(hello_kernel_exit);

लिनक्स डिवाइस ड्राइवर (चरित्र-डिवाइस, ब्लॉक-डिवाइस, आदि ...) लिखने के लिए, एक कर्नेल मॉड्यूल बनाना आवश्यक है जिसमें एक प्रवेश और निकास बिंदु हैं।

अपने आप से, कर्नेल मॉड्यूल कुछ भी नहीं करता है; यह उपयोगकर्ताओं के साथ संवाद करने का कोई सार्थक तरीका नहीं है। प्रवेश बिंदु का उपयोग करके एक नया चरित्र-उपकरण बनाना संभव है, उदाहरण के लिए, जो तब उपयोगकर्तास्पेस के साथ संवाद करने के लिए उपयोग किया जाता है।

मॉड्यूल का निर्माण और चलाना

ड्राइवर को संकलित करने के लिए, लिनक्स कर्नेल स्रोत का पेड़ होना आवश्यक है।

सूत्रों की मानें /lib/modules/<kernel-version> , निम्नलिखित driver.c फ़ाइल driver.c को संकलित करेगा। driver.ko कर्नेल ऑब्जेक्ट में

obj-m := driver.o
KDIR := /lib/modules/$(shell uname -r)/build/
PWD := $(shell pwd)

all:
    $(MAKE) -C $(KDIR) M=$(PWD) modules

ध्यान दें कि यह मेकफाइल कॉल कर्नेल की निर्माण निर्देशिका में कैसे make

जब संकलन चरण सफलतापूर्वक पूरा हो जाता है, तो चालक की src निर्देशिका कुछ इस तरह दिखाई देगी:

driver.c  driver.ko  driver.mod.c  driver.mod.o  driver.o  Makefile  modules.order  Module.symvers

मॉड्यूल को "रन" करने के लिए, रनिंग कर्नेल में सम्मिलित करना आवश्यक है:

$ insmod driver.ko
$ dmesg | tail -n 1
[133790.762185] Hello, World!

$ rmmod driver.ko
$ dmesg | tail -n 1
[133790.762185] Goodbye, cruel world...


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow