Get notified in Linux C program as Internet connection becomes available/unavailable
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
is there a lightweight way to know the state change?
Originally Posted by Elv13
Network manager have a dbus interface, it would be the right way to do it. You can also use
if (system("ping www.google.com"))
as it will not return 0 (the right value if it work)
Thanks a lot for the answer.
. Network Manager dbus interface
In addition to standard Linux, it seems that one should run another network manager to have the dbus interface. If so, I worry it will create much additional computing demand.
. system ping
This is a good idea. However, it also involves invocation of sh, fork, exec, and ping. It also demands much additional resources.
I am wondering if there is a simple way to register or probe Linux network.
NetworkManager is used by all distributions by default (except Gentoo and Arch where, as everything, it is optional). It is the cleanest way to do it.
About ping, yes, it involve some delay (10 second if the network is not present). The fork itself is not that demanding, nor sh (if it is truly SH, if it is bash then, yes, it will slow things down). The problem is ping itself. You could of course make a unix socket and use the ping protocol by yourself, but it would be a waste of effort. NetworkManager is how every modern applications do it.
My test was on a 3GHz Pentium 4. It is far beyond the performance of pentium 100MHz. It takes 30% of the Pentium 4 computing power. I am just wondering how can it be used in cellular phone platform where the computing power should be much lower. Maybe, there is some other way to start NetworkManager in lightweight mode.
In a GPRS mobile communication environment, the communication link may be broken due to lack of base station. The communication connection could be re-established if it moves to a area that has base stations coverage again.
As we know, Linux supports network. There should be somewhere inside Linux that it knows the network connection is down or up. Furthermore, maybe a program can register and then get notified as connection state changes.
In my test, ifconfig and netstat does not reflect the change immediately. How can an application know the connection state change in a realtime manner?
I've experienced some "issues" with Network Manager but one thing I have never seen it do is use much cpu cycles. Usually when running top it is not even on the screen. In fact I just tested it now, running top while turning my wifi adapter off and back on, and NM and the user space nm-applet together never used more than 1.6% cpu. And this is on a 1.4Ghz Pentium M.
Something is wrong with your NM configuration if it using the cpu cycles you report.
30% cpu is not normal, it is not configured correctly if it use that much power. And about network link, it is handled by network manager. It is how Linux does it. Before that, the connection was static and configured with text files (/etc/network/interfaces) and was activated or unactivated using the ifup/ifodwn command manually. The system itself used a script (/etc/init.d/networking) to start or stop all interfaces. Nothing was dynamic so there is not API. NetworkManager introduced these API and applications now use them to check network availability. KDE platform also have one called "Solid", but it rely on NetworkManager too on Linux (and native API on OSX and Windows).
My test did not consider any configuration issues. I just ran NetworkManager in background inside /usr/sbin and checked its cpu percentage by top. Could anyone point out a right way to run it?
Thanks for Elv's comments. Linux has static data and one needs Network Manager to have dynamic data. By saying so, there is no way to register pure Linux only system to be notified on network state change. By introducing NetworkManager, one can register via dbus to get notified as network state changes.
Could anyone provide more NetworkManager examples on cellular mobile platform?
Some Nokia N*** use it, OpenMoko phone use it, some other too.
But NetworkManager is now part of Linux, every distribution (exept DIY distro, where nothing is installed by default) use it. An other exist called Conman, developped by Intel, but currently, nobody use it. NetworkManager should not be ressource heavy. Try running it correctly use /etc/init.d, but your distribution might enable it by default, check that before playing with it.