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 "wifi_system/supplicant_manager.h"
18 
19 #include <android-base/logging.h>
20 #include <cutils/properties.h>
21 #include <fcntl.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <sys/system_properties.h>
25 #include <unistd.h>
26 
27 namespace android {
28 namespace wifi_system {
29 namespace {
30 
31 const char kSupplicantInitProperty[] = "init.svc.wpa_supplicant";
32 const char kSupplicantServiceName[] = "wpa_supplicant";
33 
34 }  // namespace
35 
StartSupplicant()36 bool SupplicantManager::StartSupplicant() {
37   char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
38   int count = 200; /* wait at most 20 seconds for completion */
39   const prop_info* pi;
40   unsigned serial = 0;
41 
42   /* Check whether already running */
43   if (property_get(kSupplicantInitProperty, supp_status, NULL) &&
44       strcmp(supp_status, "running") == 0) {
45     return true;
46   }
47 
48   /*
49    * Get a reference to the status property, so we can distinguish
50    * the case where it goes stopped => running => stopped (i.e.,
51    * it start up, but fails right away) from the case in which
52    * it starts in the stopped state and never manages to start
53    * running at all.
54    */
55   pi = __system_property_find(kSupplicantInitProperty);
56   if (pi != NULL) {
57     serial = __system_property_serial(pi);
58   }
59 
60   property_set("ctl.start", kSupplicantServiceName);
61   sched_yield();
62 
63   while (count-- > 0) {
64     if (pi == NULL) {
65       pi = __system_property_find(kSupplicantInitProperty);
66     }
67     if (pi != NULL) {
68       /*
69        * property serial updated means that init process is scheduled
70        * after we sched_yield, further property status checking is based on this
71        */
72       if (__system_property_serial(pi) != serial) {
73         __system_property_read(pi, NULL, supp_status);
74         if (strcmp(supp_status, "running") == 0) {
75           return true;
76         } else if (strcmp(supp_status, "stopped") == 0) {
77           return false;
78         }
79       }
80     }
81     usleep(100000);
82   }
83   return false;
84 }
85 
StopSupplicant()86 bool SupplicantManager::StopSupplicant() {
87   char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
88   int count = 200; /* wait at most 20 seconds for completion */
89 
90   /* Check whether supplicant already stopped */
91   if (property_get(kSupplicantInitProperty, supp_status, NULL) &&
92       strcmp(supp_status, "stopped") == 0) {
93     return true;
94   }
95 
96   property_set("ctl.stop", kSupplicantServiceName);
97   sched_yield();
98 
99   while (count-- > 0) {
100     if (property_get(kSupplicantInitProperty, supp_status, NULL)) {
101       if (strcmp(supp_status, "stopped") == 0) return true;
102     }
103     usleep(100000);
104   }
105   LOG(ERROR) << "Failed to stop supplicant";
106   return false;
107 }
108 
IsSupplicantRunning()109 bool SupplicantManager::IsSupplicantRunning() {
110   char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
111   if (property_get(kSupplicantInitProperty, supp_status, NULL)) {
112     return strcmp(supp_status, "running") == 0;
113   }
114   return false;  // Failed to read service status from init.
115 }
116 
117 }  // namespace wifi_system
118 }  // namespace android
119