OSDN Git Service

Switch netd over to <utils/file.h>.
authorElliott Hughes <enh@google.com>
Tue, 3 Feb 2015 02:08:59 +0000 (18:08 -0800)
committerElliott Hughes <enh@google.com>
Tue, 3 Feb 2015 22:11:06 +0000 (14:11 -0800)
Change-Id: Id79961cc4feee1c307dad06d64e3f4ffe060c4da

server/Android.mk
server/InterfaceController.cpp
server/NetdConstants.cpp
server/NetdConstants.h
server/TetherController.cpp

index e7b01a3..35e34f5 100644 (file)
@@ -19,7 +19,6 @@ include $(CLEAR_VARS)
 LOCAL_C_INCLUDES := \
         $(call include-path-for, libhardware_legacy)/hardware_legacy \
         bionic/libc/dns/include \
-        external/libcxx/include \
         external/mdnsresponder/mDNSShared \
         system/netd/include \
 
@@ -38,6 +37,7 @@ LOCAL_SHARED_LIBRARIES := \
         libnetutils \
         libnl \
         libsysutils \
+        libutils \
 
 LOCAL_STATIC_LIBRARIES := \
         libpcap \
index f0b313f..64b8453 100644 (file)
@@ -20,6 +20,7 @@
 
 #define LOG_TAG "InterfaceController"
 #include <cutils/log.h>
+#include <utils/file.h>
 
 #include "InterfaceController.h"
 #include "RouteController.h"
@@ -49,7 +50,7 @@ int InterfaceController::writeIPv6ProcPath(const char *interface, const char *se
                return -1;
        }
        asprintf(&path, "%s/%s/%s", ipv6_proc_path, interface, setting);
-       int success = writeFile(path, value, strlen(value));
+       bool success = android::WriteStringToFile(value, path);
        free(path);
        return success;
 }
@@ -119,7 +120,7 @@ int InterfaceController::setMtu(const char *interface, const char *mtu)
                return -1;
        }
        asprintf(&path, "%s/%s/mtu", sys_net_path, interface);
-       int success = writeFile(path, mtu, strlen(mtu));
+       bool success = android::WriteStringToFile(mtu, path);
        free(path);
        return success;
 }
index a6e38ca..d2e0bbe 100644 (file)
@@ -113,43 +113,6 @@ int execIptablesSilently(IptablesTarget target, ...) {
     return res;
 }
 
-int writeFile(const char *path, const char *value, int size) {
-    int fd = open(path, O_WRONLY | O_CLOEXEC);
-    if (fd < 0) {
-        ALOGE("Failed to open %s: %s", path, strerror(errno));
-        return -1;
-    }
-
-    if (write(fd, value, size) != size) {
-        ALOGE("Failed to write %s: %s", path, strerror(errno));
-        close(fd);
-        return -1;
-    }
-    close(fd);
-    return 0;
-}
-
-int readFile(const char *path, char *buf, int *sizep)
-{
-    int fd = open(path, O_RDONLY | O_CLOEXEC);
-    int size;
-
-    if (fd < 0) {
-        ALOGE("Failed to open %s: %s", path, strerror(errno));
-        return -1;
-    }
-
-    size = read(fd, buf, *sizep);
-    if (size < 0) {
-        ALOGE("Failed to write %s: %s", path, strerror(errno));
-        close(fd);
-        return -1;
-    }
-    *sizep = size;
-    close(fd);
-    return 0;
-}
-
 /*
  * Check an interface name for plausibility. This should e.g. help against
  * directory traversal.
index 7b894a8..296661d 100644 (file)
@@ -35,8 +35,6 @@ enum IptablesTarget { V4, V6, V4V6 };
 
 int execIptables(IptablesTarget target, ...);
 int execIptablesSilently(IptablesTarget target, ...);
-int writeFile(const char *path, const char *value, int size);
-int readFile(const char *path, char *buf, int *sizep);
 bool isIfaceName(const char *name);
 int parsePrefix(const char *prefix, uint8_t *family, void *address, int size, uint8_t *prefixlen);
 
index a91c744..c9a93fd 100644 (file)
@@ -30,6 +30,7 @@
 #define LOG_TAG "TetherController"
 #include <cutils/log.h>
 #include <cutils/properties.h>
+#include <utils/file.h>
 
 #include "Fwmark.h"
 #include "NetdConstants.h"
@@ -66,38 +67,22 @@ int TetherController::setIpFwdEnabled(bool enable) {
         return 0;
     }
 
-    int fd = open("/proc/sys/net/ipv4/ip_forward", O_WRONLY | O_CLOEXEC);
-    if (fd < 0) {
-        ALOGE("Failed to open ip_forward (%s)", strerror(errno));
-        return -1;
-    }
-
-    if (write(fd, (enable ? "1" : "0"), 1) != 1) {
+    if (!android::WriteStringToFile(enable ? "1" : "0", "/proc/sys/net/ipv4/ip_forward")) {
         ALOGE("Failed to write ip_forward (%s)", strerror(errno));
-        close(fd);
         return -1;
     }
-    close(fd);
+
     return 0;
 }
 
 bool TetherController::getIpFwdEnabled() {
-    int fd = open("/proc/sys/net/ipv4/ip_forward", O_RDONLY | O_CLOEXEC);
-
-    if (fd < 0) {
-        ALOGE("Failed to open ip_forward (%s)", strerror(errno));
-        return false;
-    }
-
-    char enabled;
-    if (read(fd, &enabled, 1) != 1) {
+    std::string enabled;
+    if (!android::ReadFileToString("/proc/sys/net/ipv4/ip_forward", &enabled)) {
         ALOGE("Failed to read ip_forward (%s)", strerror(errno));
-        close(fd);
         return -1;
     }
 
-    close(fd);
-    return (enabled  == '1' ? true : false);
+    return (enabled == "1" ? true : false);
 }
 
 #define TETHER_START_CONST_ARG        8