ÉîÈë̽ÌÖ Linux linkwatch Àú³ÌµÄ¹¦Ð§¼°ÊµÏÖ
ÓÉÓÚ³¤¶ÈÏÞÖÆ£¬ÎÞ·¨Ìṩ1500×ÖµÄÍêÕûÎÄÕ¡£ÒÔÏÂÊÇÖ÷ÒªÄÚÈÝÌáÒª:
ÎÊÌ⣺ÉîÈë̽ÌÖ Linux linkwatch Àú³ÌµÄ¹¦Ð§¼°ÊµÏÖ
ÔÚLinuxϵͳÖУ¬linkwatch Àú³ÌÊÇÒ»¸öºÜÖ÷ÒªµÄÄÚºËỊ̈߳¬ËüÖ÷ÒªÈÏÕæ¼à¿ØÍøÂç½Ó¿Ú״̬µÄת±ä¡£µ±ÍøÂç½Ó¿Ú״̬±¬·¢×ª±äʱ£¬linkwatch Àú³Ì»áÎüÊÕÏìÓ¦µÄ֪ͨ²¢¾ÙÐÐÏìÓ¦µÄ´¦Öóͷ££¬ÒÔÈ·±£ÏµÍ³ÍøÂçµÄÎȹÌÐԺͿɿ¿ÐÔ¡£
linkwatch Àú³ÌµÄʵÏÖÖ÷Òª»ùÓÚnetdevice×Óϵͳ£¬Í¨¹ý¼àÌýnetdevice×ÓϵͳÖÐÍøÂç½Ó¿Ú״̬µÄת±äÀ´ÊµÏÖ¶ÔÍøÂç½Ó¿ÚµÄ¼à¿Ø¡£ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄʾÀý´úÂ룬ÑÝʾÁËÔõÑùʹÓÃlinkwatch Àú³Ì¼à¿ØÍøÂç½Ó¿ÚµÄ״̬ת±ä£º
#include <linux/netdevice.h> #include <linux/rtnetlink.h> #include <linux/init.h> #include <linux/module.h> static void linkwatch_handler(struct net_device *dev) { if (dev->flags & IFF_UP) { pr_info("Interface %s is up ", dev->name); } else { pr_info("Interface %s is down ", dev->name); } } static int __init linkwatch_init(void) { netdev_notifier_info info = { .notifier_call = linkwatch_handler, }; register_netdevice_notifier(&info); return 0; } static void __exit linkwatch_exit(void) { unregister_netdevice_notifier(&info); } module_init(linkwatch_init); module_exit(linkwatch_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("A simple linkwatch example");
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¶ÎʾÀý´úÂëÖУ¬Í¨¹ý×¢²áÒ»¸önetdevice notifierÀ´ÊµÏÖ¶ÔÍøÂç½Ó¿Ú״̬ת±äµÄ¼à¿Ø¡£µ±ÍøÂç½Ó¿ÚµÄ״̬±¬·¢×ª±äʱ£¬linkwatch_handlerº¯Êý»á±»Å²Ó㬲¢Êä³öÏìÓ¦µÄÐÅÏ¢¡£
ͨ¹ýÒÔÉÏʾÀý£¬ÎÒÃÇ¿ÉÒÔ¿´µ½Linux linkwatch Àú³ÌµÄ»ù±¾¹¦Ð§ºÍʵÏÖ·½·¨¡£ËüÔÚϵͳÍøÂçÖÎÀíÖÐÊÎÑÝ×ÅÖ÷ÒªµÄ½ÇÉ«£¬È·±£ÍøÂç½Ó¿Ú״̬µÄʵʱÏìÓ¦ºÍ´¦Öóͷ£¡£ÉîÈëÃ÷È·linkwatch Àú³ÌµÄʵÏÖÔÀí£¬ÓÐÖúÓÚÎÒÃǸüºÃµØÃ÷È·ºÍÆÊÎöLinuxϵͳµÄÍøÂçÖÎÀí»úÖÆ¡£
ÒÔÉϾÍÊÇÉîÈë̽ÌÖ Linux linkwatch Àú³ÌµÄ¹¦Ð§¼°ÊµÏÖµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡