*/
#include <unistd.h>
+#include <sys/capability.h>
#include <csignal>
+#include <memory>
#include <android-base/logging.h>
+#include <android-base/macros.h>
+#include <binder/IPCThreadState.h>
+#include <binder/IServiceManager.h>
+#include <binder/ProcessState.h>
+#include <cutils/properties.h>
+#include <libminijail.h>
+#include <utils/String16.h>
+#include <wifi_system/interface_tool.h>
+
+#include "wificond/ipc_constants.h"
+#include "wificond/looper_backed_event_loop.h"
+#include "wificond/net/netlink_manager.h"
+#include "wificond/net/netlink_utils.h"
+#include "wificond/scanning/scan_utils.h"
+#include "wificond/server.h"
+
+using android::net::wifi::IWificond;
+using android::wifi_system::HostapdManager;
+using android::wifi_system::InterfaceTool;
+using android::wifi_system::SupplicantManager;
+using android::wificond::ipc_constants::kServiceName;
+using std::unique_ptr;
namespace {
-volatile bool ShouldContinue = true;
+class ScopedSignalHandler final {
+ public:
+ ScopedSignalHandler(android::wificond::LooperBackedEventLoop* event_loop) {
+ if (s_event_loop_ != nullptr) {
+ LOG(FATAL) << "Only instantiate one signal handler per process!";
+ }
+ s_event_loop_ = event_loop;
+ std::signal(SIGINT, &ScopedSignalHandler::LeaveLoop);
+ std::signal(SIGTERM, &ScopedSignalHandler::LeaveLoop);
+ }
+
+ ~ScopedSignalHandler() {
+ std::signal(SIGINT, SIG_DFL);
+ std::signal(SIGTERM, SIG_DFL);
+ s_event_loop_ = nullptr;
+ }
+
+ private:
+ static android::wificond::LooperBackedEventLoop* s_event_loop_;
+ static void LeaveLoop(int signal) {
+ if (s_event_loop_ != nullptr) {
+ s_event_loop_->TriggerExit();
+ }
+ }
+
+ DISALLOW_COPY_AND_ASSIGN(ScopedSignalHandler);
+};
+
+android::wificond::LooperBackedEventLoop*
+ ScopedSignalHandler::s_event_loop_ = nullptr;
+
+
+// Setup our interface to the Binder driver or die trying.
+int SetupBinderOrCrash() {
+ int binder_fd = -1;
+ android::ProcessState::self()->setThreadPoolMaxThreadCount(0);
+ android::IPCThreadState::self()->disableBackgroundScheduling(true);
+ int err = android::IPCThreadState::self()->setupPolling(&binder_fd);
+ CHECK_EQ(err, 0) << "Error setting up binder polling: " << strerror(-err);
+ CHECK_GE(binder_fd, 0) << "Invalid binder FD: " << binder_fd;
+ return binder_fd;
+}
+
+void RegisterServiceOrCrash(const android::sp<android::IBinder>& service) {
+ android::sp<android::IServiceManager> sm = android::defaultServiceManager();
+ CHECK_EQ(sm != NULL, true) << "Could not obtain IServiceManager";
+
+ CHECK_EQ(sm->addService(android::String16(kServiceName), service),
+ android::NO_ERROR);
+}
} // namespace
-void leave_loop(int signal) {
- ShouldContinue = false;
+void OnBinderReadReady(int fd) {
+ android::IPCThreadState::self()->handlePolledCommands();
}
int main(int argc, char** argv) {
- android::base::InitLogging(argv);
+ android::base::InitLogging(argv, android::base::LogdLogger(android::base::SYSTEM));
LOG(INFO) << "wificond is starting up...";
- std::signal(SIGINT, &leave_loop);
- std::signal(SIGTERM, &leave_loop);
- while (ShouldContinue) {
- sleep(1);
- }
- LOG(INFO) << "Leaving the loop...";
+
+ unique_ptr<android::wificond::LooperBackedEventLoop> event_dispatcher(
+ new android::wificond::LooperBackedEventLoop());
+ ScopedSignalHandler scoped_signal_handler(event_dispatcher.get());
+
+ int binder_fd = SetupBinderOrCrash();
+ CHECK(event_dispatcher->WatchFileDescriptor(
+ binder_fd,
+ android::wificond::EventLoop::kModeInput,
+ &OnBinderReadReady)) << "Failed to watch binder FD";
+
+ android::wificond::NetlinkManager netlink_manager(event_dispatcher.get());
+ CHECK(netlink_manager.Start()) << "Failed to start netlink manager";
+ android::wificond::NetlinkUtils netlink_utils(&netlink_manager);
+ android::wificond::ScanUtils scan_utils(&netlink_manager);
+
+ unique_ptr<android::wificond::Server> server(new android::wificond::Server(
+ unique_ptr<InterfaceTool>(new InterfaceTool),
+ unique_ptr<SupplicantManager>(new SupplicantManager()),
+ unique_ptr<HostapdManager>(new HostapdManager()),
+ &netlink_utils,
+ &scan_utils));
+ server->CleanUpSystemState();
+ RegisterServiceOrCrash(server.get());
+
+ event_dispatcher->Poll();
+ LOG(INFO) << "wificond is about to exit";
return 0;
}