Move some config file logic to a
shared utility class.
This logic is needed by the mainline
supplicant to add and remove interfaces.
Bug: 365585450
Test: m
Change-Id: I395bb69eb29023fa888ca76503b4cd657b822acd
diff --git a/wpa_supplicant/aidl/shared/shared_utils.h b/wpa_supplicant/aidl/shared/shared_utils.h
new file mode 100644
index 0000000..97676f4
--- /dev/null
+++ b/wpa_supplicant/aidl/shared/shared_utils.h
@@ -0,0 +1,49 @@
+/*
+ * WPA Supplicant - Shared utility functions and constants
+ * Copyright (c) 2024, Google Inc. All rights reserved.
+ *
+ * This software may be distributed under the terms of the BSD license.
+ * See README for more details.
+ */
+
+#ifndef SHARED_UTILS_H
+#define SHARED_UTILS_H
+
+#include <android-base/file.h>
+#include <fcntl.h>
+
+extern "C"
+{
+#include "utils/common.h"
+}
+
+constexpr char kIfaceDriverName[] = "nl80211";
+constexpr mode_t kConfigFileMode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
+
+/**
+ * Ensure that the config file at |config_file_path| exists.
+ * Returns 0 on success, or errno otherwise.
+ */
+int ensureConfigFileExistsAtPath(const std::string& config_file_path) {
+ int ret = access(config_file_path.c_str(), R_OK);
+ if (ret == 0) {
+ return 0;
+ }
+ if (errno == EACCES) {
+ ret = chmod(config_file_path.c_str(), kConfigFileMode);
+ if (ret == 0) {
+ return 0;
+ } else {
+ wpa_printf(
+ MSG_ERROR, "Cannot set RW to %s. Errno: %s",
+ config_file_path.c_str(), strerror(errno));
+ }
+ } else if (errno != ENOENT) {
+ wpa_printf(
+ MSG_ERROR, "Cannot access %s. Errno: %s",
+ config_file_path.c_str(), strerror(errno));
+ }
+ return errno;
+}
+
+#endif // SHARED_UTILS_H
diff --git a/wpa_supplicant/aidl/vendor/supplicant.cpp b/wpa_supplicant/aidl/vendor/supplicant.cpp
index ae1943f..0cb2342 100644
--- a/wpa_supplicant/aidl/vendor/supplicant.cpp
+++ b/wpa_supplicant/aidl/vendor/supplicant.cpp
@@ -12,6 +12,8 @@
#include "supplicant.h"
#include "p2p_iface.h"
+#include "aidl/shared/shared_utils.h"
+
#include <android-base/file.h>
#include <fcntl.h>
#include <sys/stat.h>
@@ -22,8 +24,6 @@
// Note: This may differ for other OEM's. So, modify this accordingly.
// When wpa_supplicant is in its APEX, overlay/template configurations should be
// loaded from the same APEX.
-constexpr char kIfaceDriverName[] = "nl80211";
-
constexpr char kStaIfaceConfPath[] =
"/data/vendor/wifi/wpa/wpa_supplicant.conf";
constexpr char kStaIfaceConfOverlayPath[] =
@@ -42,7 +42,6 @@
constexpr char kOldStaIfaceConfPath[] = "/data/misc/wifi/wpa_supplicant.conf";
constexpr char kOldP2pIfaceConfPath[] = "/data/misc/wifi/p2p_supplicant.conf";
-constexpr mode_t kConfigFileMode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
std::string resolveVendorConfPath(const std::string& conf_path)
{
@@ -114,26 +113,15 @@
const std::string& config_file_path,
const std::string& old_config_file_path)
{
- int ret = access(config_file_path.c_str(), R_OK | W_OK);
+ // Check if config file already exists at |config_file_path|
+ int ret = ensureConfigFileExistsAtPath(config_file_path);
if (ret == 0) {
+ wpa_printf(MSG_INFO, "Config file already exists at %s", config_file_path.c_str());
return 0;
- }
- if (errno == EACCES) {
- ret = chmod(config_file_path.c_str(), kConfigFileMode);
- if (ret == 0) {
- return 0;
- } else {
- wpa_printf(
- MSG_ERROR, "Cannot set RW to %s. Errno: %s",
- config_file_path.c_str(), strerror(errno));
- return -1;
- }
- } else if (errno != ENOENT) {
- wpa_printf(
- MSG_ERROR, "Cannot acces %s. Errno: %s",
- config_file_path.c_str(), strerror(errno));
+ } else if (ret != ENOENT) {
return -1;
}
+
ret = copyFileIfItExists(old_config_file_path, config_file_path);
if (ret == 0) {
wpa_printf(
@@ -145,6 +133,7 @@
unlink(config_file_path.c_str());
return -1;
}
+
std::string vendor_template_conf_path = resolveVendorConfPath(kVendorTemplateConfPath);
ret = copyFileIfItExists(vendor_template_conf_path, config_file_path);
if (ret == 0) {
@@ -156,6 +145,7 @@
unlink(config_file_path.c_str());
return -1;
}
+
ret = copyFileIfItExists(kSystemTemplateConfPath, config_file_path);
if (ret == 0) {
wpa_printf(
@@ -166,6 +156,7 @@
unlink(config_file_path.c_str());
return -1;
}
+
// Did not create the conf file.
return -1;
}