OSDN Git Service

Simplify notification code in NetlinkHandler.
authorLorenzo Colitti <lorenzo@google.com>
Fri, 25 Oct 2013 10:53:31 +0000 (19:53 +0900)
committerLorenzo Colitti <lorenzo@google.com>
Wed, 20 Nov 2013 01:59:27 +0000 (10:59 +0900)
1. Factor most of the notification code out to a common function.
2. Use vasprintf instead of snprintf so we don't have to worry
   about clipping notifications due to fixed-size message
   buffers.

[Cherry-pick of 0b454ea4abdc8a563af6da58fa37835729220acf]

Bug: 9180552
Change-Id: Idde16ee6dd56d38dab866f0ea678b04d98b3048d

NetlinkHandler.cpp
NetlinkHandler.h

index 5d8a8b5..51720f8 100644 (file)
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -101,67 +102,52 @@ void NetlinkHandler::onEvent(NetlinkEvent *evt) {
     }
 }
 
-void NetlinkHandler::notifyInterfaceAdded(const char *name) {
-    char msg[255];
-    snprintf(msg, sizeof(msg), "Iface added %s", name);
+void NetlinkHandler::notify(int code, const char *format, ...) {
+    char *msg;
+    va_list args;
+    va_start(args, format);
+    if (vasprintf(&msg, format, args) >= 0) {
+        mNm->getBroadcaster()->sendBroadcast(code, msg, false);
+        free(msg);
+    } else {
+        SLOGE("Failed to send notification: vasprintf: %s", strerror(errno));
+    }
+    va_end(args);
+}
 
-    mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
-            msg, false);
+void NetlinkHandler::notifyInterfaceAdded(const char *name) {
+    notify(ResponseCode::InterfaceChange, "Iface added %s", name);
 }
 
 void NetlinkHandler::notifyInterfaceRemoved(const char *name) {
-    char msg[255];
-    snprintf(msg, sizeof(msg), "Iface removed %s", name);
-
-    mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
-            msg, false);
+    notify(ResponseCode::InterfaceChange, "Iface removed %s", name);
 }
 
 void NetlinkHandler::notifyInterfaceChanged(const char *name, bool isUp) {
-    char msg[255];
-    snprintf(msg, sizeof(msg), "Iface changed %s %s", name,
-             (isUp ? "up" : "down"));
-
-    mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
-            msg, false);
+    notify(ResponseCode::InterfaceChange,
+           "Iface changed %s %s", name, (isUp ? "up" : "down"));
 }
 
 void NetlinkHandler::notifyInterfaceLinkChanged(const char *name, bool isUp) {
-    char msg[255];
-    snprintf(msg, sizeof(msg), "Iface linkstate %s %s", name,
-             (isUp ? "up" : "down"));
-
-    mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceChange,
-            msg, false);
+    notify(ResponseCode::InterfaceChange,
+           "Iface linkstate %s %s", name, (isUp ? "up" : "down"));
 }
 
 void NetlinkHandler::notifyQuotaLimitReached(const char *name, const char *iface) {
-    char msg[255];
-    snprintf(msg, sizeof(msg), "limit alert %s %s", name, iface);
-
-    mNm->getBroadcaster()->sendBroadcast(ResponseCode::BandwidthControl,
-            msg, false);
+    notify(ResponseCode::BandwidthControl, "limit alert %s %s", name, iface);
 }
 
 void NetlinkHandler::notifyInterfaceClassActivity(const char *name,
                                                   bool isActive) {
-    char msg[255];
-
-    snprintf(msg, sizeof(msg), "IfaceClass %s %s",
-             isActive ? "active" : "idle", name);
-    ALOGV("Broadcasting interface activity msg: %s", msg);
-    mNm->getBroadcaster()->sendBroadcast(
-        ResponseCode::InterfaceClassActivity, msg, false);
+    notify(ResponseCode::InterfaceClassActivity,
+           "IfaceClass %s %s", isActive ? "active" : "idle", name);
 }
 
 void NetlinkHandler::notifyAddressChanged(int action, const char *addr,
                                           const char *iface, const char *flags,
                                           const char *scope) {
-    char msg[255];
-    snprintf(msg, sizeof(msg), "Address %s %s %s %s %s",
-             (action == NetlinkEvent::NlActionAddressUpdated) ?
-             "updated" : "removed", addr, iface, flags, scope);
-
-    mNm->getBroadcaster()->sendBroadcast(ResponseCode::InterfaceAddressChange,
-            msg, false);
+    notify(ResponseCode::InterfaceAddressChange,
+           "Address %s %s %s %s %s",
+           (action == NetlinkEvent::NlActionAddressUpdated) ?
+           "updated" : "removed", addr, iface, flags, scope);
 }
index 0cbf339..c0396c0 100644 (file)
@@ -33,6 +33,7 @@ public:
 protected:
     virtual void onEvent(NetlinkEvent *evt);
 
+    void notify(int code, const char *format, ...);
     void notifyInterfaceAdded(const char *name);
     void notifyInterfaceRemoved(const char *name);
     void notifyInterfaceChanged(const char *name, bool isUp);