/* Linux network device status watcher (C) 2002 Stefan Rompf GPL Version 2 applies compile with something like gcc -c -DMODULE -D__KERNEL__ -O2 dev_watch.c */ #include #include #include #include #include #include static volatile pid_t me; static volatile int running = 1; static int devwatcher(void *dummy) { struct net_device *dev; daemonize(); strcpy(current->comm, "devwatchd"); while(running) { rtnl_lock(); for (dev=dev_base; dev; dev = dev->next) { /* State of netif_carrier_ok() is reflected into dev_flags by this loop, and a netlink message is omitted whenever the state changes */ if (dev->flags & IFF_RUNNING) { if (!netif_carrier_ok(dev)) { write_lock(&dev_base_lock); dev->flags &= ~IFF_RUNNING; write_unlock(&dev_base_lock); netdev_state_change(dev); } } else { if (netif_carrier_ok(dev)) { write_lock(&dev_base_lock); dev->flags |= IFF_RUNNING; write_unlock(&dev_base_lock); netdev_state_change(dev); } } } rtnl_unlock(); set_current_state(TASK_INTERRUPTIBLE); schedule_timeout(HZ); } me = 0; return 0; } static int __init devwatch_init(void) { me = kernel_thread(devwatcher, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGNAL); if (!me) return 1; return 0; } static void __exit devwatch_cleanup(void) { running = 0; while(me) schedule(); } EXPORT_NO_SYMBOLS; module_init(devwatch_init); module_exit(devwatch_cleanup);