title: Catch moment when we have wifi connection
body: <p>I need to write program which follow such steps:</p>
  
  <ol>
  <li>Start program (daemon)</li>
  <li>Wait (sleep, block) until I have wifi connection up</li>
  <li>Send/get some data from server</li>
  <li>Wait until wifi connection goes down</li>
  <li>goto 2</li>
  </ol>
  
  <p>Problem with step 2. I dont know how to catch moment when there is established network connection. There is <code>/proc/net/wireless entry</code>, where information about available wireless connections appear, but trying to monitor it with inotify have no success. Network connection is established asynchronously. </p>
  
  <p>Here is my test code with inotify (copied mostly from R.Loves book):</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/inotify.h&gt;
  #include &lt;sys/select.h&gt;
  
  #define BUF_LEN 1024
  
  int
  main() {
  
      int fd, wd, rc;
      char buf[BUF_LEN];
      ssize_t len, i = 0;
      static fd_set read_fds;
  
      fd = inotify_init();
      if (fd == -1) {
          perror("inotify_init");
          exit(EXIT_FAILURE);
      }
  
      wd = inotify_add_watch(fd, "/proc/net/wireless", IN_ALL_EVENTS);
      if (wd == -1) {
          perror("inotify_add_watch");
          exit(EXIT_FAILURE);
      }
  
      for (;;) {
  
          FD_ZERO(&amp;read_fds);
          FD_SET(wd, &amp;read_fds);
          rc = select(wd + 1, &amp;read_fds, NULL, NULL, NULL);
          if (rc == -1)
              perror("select");
  
          len = read(fd, buf, BUF_LEN);
          while (i &lt; len) {
              struct inotify_event *event = (struct inotify_event *) &amp;buf[i];
              printf("wd=%d mask=%d cookie=%d len=%d dir=%s\n",
                  event-&gt;wd, event-&gt;mask, event-&gt;cookie, event-&gt;len, 
                  (event-&gt; mask &amp; IN_ISDIR) ? "yes" : "no");
              if (event-&gt;len)
                  printf("name=%s\n", event-&gt;name);
  
              i += sizeof(struct inotify_event) + event-&gt;len;
          }
  
          sleep(1);
      }
  
      return 0;
  }
  </code></pre>
  
  <p>It only catches evernt when I do <code>cat /proc/net/wireless</code></p>
  
  <p>Question: How to catch moment, when I have network connection running (wifi), using only Linux features?</p>
  
  <p>P.S. This is my first post here, hope everything is ok.</p>