OSDN Git Service

Give hostapd a hint that it is about to die
[android-x86/system-connectivity-wificond.git] / ap_interface_impl.cpp
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "wificond/ap_interface_impl.h"
18
19 #include <android-base/logging.h>
20
21 #include "wificond/ap_interface_binder.h"
22
23 using android::net::wifi::IApInterface;
24 using android::wifi_system::HostapdManager;
25 using std::string;
26 using std::unique_ptr;
27 using std::vector;
28
29 using EncryptionType = android::wifi_system::HostapdManager::EncryptionType;
30
31 namespace android {
32 namespace wificond {
33
34 ApInterfaceImpl::ApInterfaceImpl(const string& interface_name,
35                                  unique_ptr<HostapdManager> hostapd_manager)
36     : interface_name_(interface_name),
37       hostapd_manager_(std::move(hostapd_manager)) {
38   binder_ = new ApInterfaceBinder(this);
39 }
40
41 ApInterfaceImpl::~ApInterfaceImpl() {
42   binder_->NotifyImplDead();
43 }
44
45 sp<IApInterface> ApInterfaceImpl::GetBinder() const {
46   return binder_;
47 }
48
49 bool ApInterfaceImpl::StartHostapd() {
50   return hostapd_manager_->StartHostapd();
51 }
52
53 bool ApInterfaceImpl::StopHostapd() {
54   pid_t hostapd_pid;
55   if (hostapd_manager_->GetHostapdPid(&hostapd_pid)) {
56     // We do this because it was found that hostapd would leave the driver in a
57     // bad state if init killed it harshly with a SIGKILL. b/30311493
58     if (kill(hostapd_pid, SIGTERM) == -1) {
59       LOG(ERROR) << "Error delivering signal to hostapd: " << strerror(errno);
60     }
61     // This should really be asynchronous: b/30465379
62     for (int tries = 0; tries < 10; tries++) {
63       // Try to give hostapd some time to go down.
64       if (!hostapd_manager_->IsHostapdRunning()) {
65         break;
66       }
67       usleep(10 * 1000);  // Don't busy wait.
68     }
69   }
70
71   // Always drop a SIGKILL on hostapd on the way out, just in case.
72   return hostapd_manager_->StopHostapd();
73 }
74
75 bool ApInterfaceImpl::WriteHostapdConfig(const vector<uint8_t>& ssid,
76                                          bool is_hidden,
77                                          int32_t channel,
78                                          EncryptionType encryption_type,
79                                          const vector<uint8_t>& passphrase) {
80   string config = hostapd_manager_->CreateHostapdConfig(
81       interface_name_, ssid, is_hidden, channel, encryption_type, passphrase);
82
83   if (config.empty()) {
84     return false;
85   }
86
87   return hostapd_manager_->WriteHostapdConfig(config);
88 }
89
90 }  // namespace wificond
91 }  // namespace android