summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2015-02-04 13:25:14 -0800
committerElliott Hughes <enh@google.com>2015-02-04 13:25:14 -0800
commitbd37832f1843ed78f64604e5627cf952ac9614ba (patch)
tree4822622da30658fb1a67e592ef9c40720f884a6e
parent50c6639a55b3208b64adc691b181a90e1e6de223 (diff)
downloadnetd-bd37832f1843ed78f64604e5627cf952ac9614ba.tar.gz
Switch writing to <utils/file.h>.
Change-Id: Idb2de24414f4dd8e926e625b62e4d12152dc4527
-rw-r--r--server/RouteController.cpp19
-rw-r--r--server/SoftapController.cpp65
2 files changed, 26 insertions, 58 deletions
diff --git a/server/RouteController.cpp b/server/RouteController.cpp
index cd5300dc..b47acd4a 100644
--- a/server/RouteController.cpp
+++ b/server/RouteController.cpp
@@ -23,6 +23,8 @@
#include <net/if.h>
#include <sys/stat.h>
+#include <private/android_filesystem_config.h>
+
#include <map>
#include "Fwmark.h"
@@ -31,6 +33,7 @@
#define LOG_TAG "Netd"
#include "log/log.h"
#include "logwrap/logwrap.h"
+#include "utils/file.h"
#include "resolv_netid.h"
namespace {
@@ -91,7 +94,6 @@ const bool ACTION_DEL = false;
const bool MODIFY_NON_UID_BASED_RULES = true;
const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
-const int RT_TABLES_FLAGS = O_CREAT | O_TRUNC | O_WRONLY | O_NOFOLLOW | O_CLOEXEC;
const mode_t RT_TABLES_MODE = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; // mode 0644, rw-r--r--
const unsigned ROUTE_FLUSH_ATTEMPTS = 2;
@@ -162,21 +164,10 @@ void updateTableNamesFile() {
addTableName(entry.second, entry.first, &contents);
}
- int fd = open(RT_TABLES_PATH, RT_TABLES_FLAGS, RT_TABLES_MODE);
- if (fd == -1) {
- ALOGE("failed to create %s (%s)", RT_TABLES_PATH, strerror(errno));
+ if (!android::WriteStringToFile(contents, RT_TABLES_PATH, RT_TABLES_MODE, AID_SYSTEM, AID_WIFI)) {
+ ALOGE("failed to write to %s (%s)", RT_TABLES_PATH, strerror(errno));
return;
}
- // File creation is affected by umask, so make sure the right mode bits are set.
- if (fchmod(fd, RT_TABLES_MODE) == -1) {
- ALOGE("failed to set mode 0%o on %s (%s)", RT_TABLES_MODE, RT_TABLES_PATH, strerror(errno));
- }
- ssize_t bytesWritten = write(fd, contents.data(), contents.size());
- if (bytesWritten != static_cast<ssize_t>(contents.size())) {
- ALOGE("failed to write to %s (%zd vs %zu bytes) (%s)", RT_TABLES_PATH, bytesWritten,
- contents.size(), strerror(errno));
- }
- close(fd);
}
// Sends a netlink request and expects an ack.
diff --git a/server/SoftapController.cpp b/server/SoftapController.cpp
index 17982d57..558aa06f 100644
--- a/server/SoftapController.cpp
+++ b/server/SoftapController.cpp
@@ -37,6 +37,8 @@
#include <cutils/log.h>
#include <netutils/ifc.h>
#include <private/android_filesystem_config.h>
+#include <utils/file.h>
+#include <utils/stringprintf.h>
#include "wifi.h"
#include "ResponseCode.h"
@@ -112,13 +114,8 @@ bool SoftapController::isSoftapStarted() {
* argv[7] - Key
*/
int SoftapController::setSoftap(int argc, char *argv[]) {
- char psk_str[2*SHA256_DIGEST_LENGTH+1];
- int ret = ResponseCode::SoftapStatusResult;
- int fd;
int hidden = 0;
int channel = AP_CHANNEL_DEFAULT;
- char *wbuf = NULL;
- char *fbuf = NULL;
if (argc < 5) {
ALOGE("Softap set is missing arguments. Please use:");
@@ -135,62 +132,42 @@ int SoftapController::setSoftap(int argc, char *argv[]) {
channel = AP_CHANNEL_DEFAULT;
}
- asprintf(&wbuf, "interface=%s\ndriver=nl80211\nctrl_interface="
- "/data/misc/wifi/hostapd\nssid=%s\nchannel=%d\nieee80211n=1\n"
- "hw_mode=g\nignore_broadcast_ssid=%d\nwowlan_triggers=any\n",
- argv[2], argv[3], channel, hidden);
+ std::string wbuf(android::StringPrintf("interface=%s\n"
+ "driver=nl80211\n"
+ "ctrl_interface=/data/misc/wifi/hostapd\n"
+ "ssid=%s\n"
+ "channel=%d\n"
+ "ieee80211n=1\n"
+ "hw_mode=g\n"
+ "ignore_broadcast_ssid=%d\n"
+ "wowlan_triggers=any\n",
+ argv[2], argv[3], channel, hidden));
+ std::string fbuf;
if (argc > 7) {
+ char psk_str[2*SHA256_DIGEST_LENGTH+1];
if (!strcmp(argv[6], "wpa-psk")) {
generatePsk(argv[3], argv[7], psk_str);
- asprintf(&fbuf, "%swpa=3\nwpa_pairwise=TKIP CCMP\nwpa_psk=%s\n", wbuf, psk_str);
+ fbuf = android::StringPrintf("%swpa=3\nwpa_pairwise=TKIP CCMP\nwpa_psk=%s\n", wbuf.c_str(), psk_str);
} else if (!strcmp(argv[6], "wpa2-psk")) {
generatePsk(argv[3], argv[7], psk_str);
- asprintf(&fbuf, "%swpa=2\nrsn_pairwise=CCMP\nwpa_psk=%s\n", wbuf, psk_str);
+ fbuf = android::StringPrintf("%swpa=2\nrsn_pairwise=CCMP\nwpa_psk=%s\n", wbuf.c_str(), psk_str);
} else if (!strcmp(argv[6], "open")) {
- asprintf(&fbuf, "%s", wbuf);
+ fbuf = wbuf;
}
} else if (argc > 6) {
if (!strcmp(argv[6], "open")) {
- asprintf(&fbuf, "%s", wbuf);
+ fbuf = wbuf;
}
} else {
- asprintf(&fbuf, "%s", wbuf);
+ fbuf = wbuf;
}
- fd = open(HOSTAPD_CONF_FILE, O_CREAT | O_TRUNC | O_WRONLY | O_NOFOLLOW | O_CLOEXEC, 0660);
- if (fd < 0) {
- ALOGE("Cannot update \"%s\": %s", HOSTAPD_CONF_FILE, strerror(errno));
- free(wbuf);
- free(fbuf);
- return ResponseCode::OperationFailed;
- }
- if (write(fd, fbuf, strlen(fbuf)) < 0) {
+ if (!android::WriteStringToFile(fbuf, HOSTAPD_CONF_FILE, 0660, AID_SYSTEM, AID_WIFI)) {
ALOGE("Cannot write to \"%s\": %s", HOSTAPD_CONF_FILE, strerror(errno));
- ret = ResponseCode::OperationFailed;
- }
- free(wbuf);
- free(fbuf);
-
- /* Note: apparently open can fail to set permissions correctly at times */
- if (fchmod(fd, 0660) < 0) {
- ALOGE("Error changing permissions of %s to 0660: %s",
- HOSTAPD_CONF_FILE, strerror(errno));
- close(fd);
- unlink(HOSTAPD_CONF_FILE);
- return ResponseCode::OperationFailed;
- }
-
- if (fchown(fd, AID_SYSTEM, AID_WIFI) < 0) {
- ALOGE("Error changing group ownership of %s to %d: %s",
- HOSTAPD_CONF_FILE, AID_WIFI, strerror(errno));
- close(fd);
- unlink(HOSTAPD_CONF_FILE);
return ResponseCode::OperationFailed;
}
-
- close(fd);
- return ret;
+ return ResponseCode::SoftapStatusResult;
}
/*