Bug 924729 - [Flatfish] :Socket bind() fail in UeventPoller initialization. r=dhylands
authorVincent Liu <vliu@mozilla.com>
Wed, 04 Dec 2013 10:18:14 +0800
changeset 174679 50c0d05fa250ececabe0c54f86dd5eb8325dc367
parent 174678 21c6774bb0777e0ad8ebef1b4b909843e8df3219
child 174680 a481c9adc411b506a747575d1ff67f30a21fb935
push id445
push userffxbld
push dateMon, 10 Mar 2014 22:05:19 +0000
treeherdermozilla-release@dc38b741b04e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdhylands
bugs924729
milestone28.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 924729 - [Flatfish] :Socket bind() fail in UeventPoller initialization. r=dhylands
hal/gonk/UeventPoller.cpp
--- a/hal/gonk/UeventPoller.cpp
+++ b/hal/gonk/UeventPoller.cpp
@@ -23,16 +23,17 @@
 #include <unistd.h>
 
 #include <arpa/inet.h>
 #include <linux/types.h>
 #include <linux/netlink.h>
 #include <netinet/in.h>
 #include <sys/socket.h>
 
+#include "nsDebug.h"
 #include "base/message_loop.h"
 #include "mozilla/FileUtils.h"
 #include "nsAutoPtr.h"
 #include "nsThreadUtils.h"
 #include "nsXULAppAPI.h"
 
 #include "UeventPoller.h"
 
@@ -110,20 +111,36 @@ NetlinkPoller::OpenSocket()
   // set non-blocking
   if (fcntl(mSocket.get(), F_SETFL, O_NONBLOCK) == -1)
     return false;
 
   struct sockaddr_nl saddr;
   bzero(&saddr, sizeof(saddr));
   saddr.nl_family = AF_NETLINK;
   saddr.nl_groups = 1;
-  saddr.nl_pid = getpid();
+  saddr.nl_pid = gettid();
+
+  do {
+    if (bind(mSocket.get(), (struct sockaddr *)&saddr, sizeof(saddr)) == 0) {
+      break;
+    }
 
-  if (bind(mSocket.get(), (struct sockaddr *)&saddr, sizeof(saddr)) == -1)
-    return false;
+    if (errno != EADDRINUSE) {
+      return false;
+    }
+
+    if (saddr.nl_pid == 0) {
+      return false;
+    }
+
+    // Once there was any other place in the same process assigning saddr.nl_pid by
+    // gettid(), we can detect it and print warning message.
+    printf_stderr("The netlink socket address saddr.nl_pid=%u is in use. Let the kernel re-assign.\n", saddr.nl_pid);
+    saddr.nl_pid = 0;
+  } while (true);
 
   if (!mIOLoop->WatchFileDescriptor(mSocket.get(),
                                     true,
                                     MessageLoopForIO::WATCH_READ,
                                     &mReadWatcher,
                                     this)) {
       return false;
   }