1 /*
2 * WPA Supplicant - Shared utility functions and constants
3 * Copyright (c) 2024, Google Inc. All rights reserved.
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #ifndef SHARED_UTILS_H
10 #define SHARED_UTILS_H
11
12 #include <android-base/file.h>
13 #include <fcntl.h>
14
15 extern "C"
16 {
17 #include "utils/common.h"
18 }
19
20 constexpr char kIfaceDriverName[] = "nl80211";
21 constexpr mode_t kConfigFileMode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
22
23 /**
24 * Ensure that the config file at |config_file_path| exists.
25 * Returns 0 on success, or errno otherwise.
26 */
ensureConfigFileExistsAtPath(const std::string & config_file_path)27 int ensureConfigFileExistsAtPath(const std::string& config_file_path) {
28 int ret = access(config_file_path.c_str(), R_OK);
29 if (ret == 0) {
30 return 0;
31 }
32 if (errno == EACCES) {
33 ret = chmod(config_file_path.c_str(), kConfigFileMode);
34 if (ret == 0) {
35 return 0;
36 } else {
37 wpa_printf(
38 MSG_ERROR, "Cannot set RW to %s. Errno: %s",
39 config_file_path.c_str(), strerror(errno));
40 }
41 } else if (errno != ENOENT) {
42 wpa_printf(
43 MSG_ERROR, "Cannot access %s. Errno: %s",
44 config_file_path.c_str(), strerror(errno));
45 }
46 return errno;
47 }
48
49 #endif // SHARED_UTILS_H
50