diff options
author | Christopher Wiley <wiley@google.com> | 2016-08-02 15:49:57 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2016-08-02 15:49:57 +0000 |
commit | b816645f25d8eb983bee1588fd92bc3fe216a32a (patch) | |
tree | 53b180a2e7c1c2646216a6b3b40d17bbc2b016c2 /libwifi_system | |
parent | d3ed44d7cc5091b862bd5614a5a65c33f529ddf5 (diff) | |
parent | aa31194b9c1f41fbbba294332ba0462b3d686bc6 (diff) | |
download | android_frameworks_opt_net_wifi-b816645f25d8eb983bee1588fd92bc3fe216a32a.tar.gz android_frameworks_opt_net_wifi-b816645f25d8eb983bee1588fd92bc3fe216a32a.tar.bz2 android_frameworks_opt_net_wifi-b816645f25d8eb983bee1588fd92bc3fe216a32a.zip |
Merge "Track hostapd's life via pid files"
Diffstat (limited to 'libwifi_system')
-rw-r--r-- | libwifi_system/hostapd_manager.cpp | 47 | ||||
-rw-r--r-- | libwifi_system/include/wifi_system/hostapd_manager.h | 6 |
2 files changed, 39 insertions, 14 deletions
diff --git a/libwifi_system/hostapd_manager.cpp b/libwifi_system/hostapd_manager.cpp index 729c071d9..56951613a 100644 --- a/libwifi_system/hostapd_manager.cpp +++ b/libwifi_system/hostapd_manager.cpp @@ -23,6 +23,7 @@ #include <android-base/file.h> #include <android-base/logging.h> +#include <android-base/parseint.h> #include <android-base/stringprintf.h> #include <cutils/properties.h> #include <openssl/evp.h> @@ -31,6 +32,8 @@ #include "wifi_system/wifi.h" +using android::base::ParseInt; +using android::base::ReadFileToString; using android::base::StringPrintf; using android::base::WriteStringToFile; using std::string; @@ -44,6 +47,8 @@ namespace { const int kDefaultApChannel = 6; const char kHostapdServiceName[] = "hostapd"; const char kHostapdConfigFilePath[] = "/data/misc/wifi/hostapd.conf"; +const char kHostapdPidFile[] = "/data/misc/wifi/hostapd.pid"; + string GeneratePsk(const vector<uint8_t>& ssid, const vector<uint8_t>& passphrase) { @@ -73,35 +78,52 @@ string GeneratePsk(const vector<uint8_t>& ssid, } // namespace bool HostapdManager::StartHostapd() { - if (hostapd_is_running_) { - LOG(ERROR) << "SoftAP is already running"; - return false; - } - if (ensure_entropy_file_exists() < 0) { LOG(WARNING) << "Wi-Fi entropy file was not created"; } + unlink(kHostapdPidFile); + if (property_set("ctl.start", kHostapdServiceName) != 0) { LOG(ERROR) << "Failed to start SoftAP"; return false; } LOG(DEBUG) << "SoftAP started successfully"; - hostapd_is_running_ = true; return true; } bool HostapdManager::IsHostapdRunning() { - return hostapd_is_running_; + pid_t hostapd_pid; + if (!GetHostapdPid(&hostapd_pid)) { + return false; + } + + if (kill(hostapd_pid, 0) != 0) { + LOG(DEBUG) << "hostapd has already died."; + return false; + } + + return true; } -bool HostapdManager::StopHostapd() { - if (!hostapd_is_running_) { - LOG(DEBUG) << "SoftAP is not running"; - return true; // Not really an error, hostapd is already stopped. +bool HostapdManager::GetHostapdPid(pid_t* hostapd_pid) { + string pid_string; + if (!ReadFileToString(kHostapdPidFile, &pid_string)) { + LOG(DEBUG) << "Failed to read hostapd pid file."; + return false; + } + pid_t pid = 0; + if (!ParseInt(pid_string.c_str(), &pid) || pid <= 0) { + LOG(DEBUG) << "hostapd pid file contained bad pid: " << pid_string; + return false; } + *hostapd_pid = pid; + return true; +} + +bool HostapdManager::StopHostapd() { LOG(DEBUG) << "Stopping the SoftAP service..."; if (property_set("ctl.stop", kHostapdServiceName) < 0) { @@ -109,8 +131,9 @@ bool HostapdManager::StopHostapd() { return false; } + unlink(kHostapdPidFile); + LOG(DEBUG) << "SoftAP stopped successfully"; - hostapd_is_running_ = false; return true; } diff --git a/libwifi_system/include/wifi_system/hostapd_manager.h b/libwifi_system/include/wifi_system/hostapd_manager.h index 2bffde453..9b52711e0 100644 --- a/libwifi_system/include/wifi_system/hostapd_manager.h +++ b/libwifi_system/include/wifi_system/hostapd_manager.h @@ -43,6 +43,10 @@ class HostapdManager { // Returns true if hostapd is currently running. virtual bool IsHostapdRunning(); + // Returns true if hostapd is running, false otherwise. + // When true is returned, will write hostapd's pid to |hostapd_pid|. + virtual bool GetHostapdPid(pid_t* hostapd_pid); + // Request that a running instance of hostapd be stopped. // Returns true on success. virtual bool StopHostapd(); @@ -70,8 +74,6 @@ class HostapdManager { virtual bool WriteHostapdConfig(const std::string& config_file); private: - bool hostapd_is_running_ = false; - DISALLOW_COPY_AND_ASSIGN(HostapdManager); }; // class HostapdManager |