Linux kernel notifier mechanism Linux notification chain

Source: Internet
Author: User

in the Linux kernel system, each module, subsystem is independent of each other. The Linux kernel can pass through theA chain of knowledge to obtain some of the events that are of interest to it by other modules or subsystems. use Notifier by Notifierparameters and pointers that can be passed to the notified person with a long integer. There is a lot of ground in Linuxuse, such as reboot notification, CPU FM notification, network card events, battery low alarm and so on. CookedNote that using notifier helps Linux kernel-driven development.

notifier_block Structure:
struct Notifier_block {     int (*notifier_call) (struct Notifier_block *, unsigned long, void *);     struct Notifier_block __rcu *next;     int priority;};


among them,
1. Notifier_call: Notification chain callback function, provided by the notified party, the firstparameter notifier_block struct pointer, the second third parameter is by call chain, respectively,that is, the notification chain initiator passed over;
2. Notifier_block *next: A pointer for linking to a linked list;
3. Priority: The precedence of the callback function, which generally defaults to 0.

when to call to Notifier_call? Usually in the notification chain register and the unregister definition file, there isnotifier call initiation function;


here are the specific application examples:

in the notified file:
1: Define a notifier call function in the chain file that is notified:
static int xxxx_notify (struct notifier_block *nb,         unsigned long status, void *unused) {     int rc;     if (!the_chip) {         pr_err ("not initialized\n");         Return-einval;     }     Switch (status) {case     0:         pr_debug ("0 received\n");         break;     Case 1:         pr_debug ("1 received\n");         break;     Case 2: Break         ;     Default:         pr_err ("Error received\n");         break;     }     return 0;};



2: Define the Alarm_notifier notification chain and assign the function entry defined above to the function pointer

Notifier_call;

static struct Notifier_block Xxxx_notifier = {     . Notifier_call = xxxx_notify,};



3: Register alarm_notifier notification chain:
inside the probe function:
     {     -----     rc = Xxxx_register_notifier (&alarm_notifier);     if (RC) {         Pr_err ("Unable to register alarm notifier rc=%d\n", RC);         return RC;     ------     }




in the notification file:

1: Define the notifier register function, which is provided to the notified chain call:
int xxxx_register_notifier (struct notifier_block *nb) {     -------     rc = Srcu_notifier_chain_register (&chip- >irq_notifier_list, NB);     -------     return RC;} Export_symbol (Xxxx_register_notifier);



where 2:notifier call originated, this is called in the interrupted queue work
Srcu_notifier_call_chain (), which is defined by the kernel in the
NOTIFIER.C, note that the following 2 arguments are passed to the callback function.
static void Xxxx_isr_work (struct work_struct *work) {     struct xxxx_chip *chip         = container_of (work, struct XXXX_ Chip, irq_work);     int status;     if (!chip)         return;     Status = Xxxx_status_read ();     Srcu_notifier_call_chain (&chip->irq_notifier_list,                         status, NULL);}



Linux kernel notifier mechanism Linux notification chain

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.