• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "util/os_utils.h"
16 
17 #include <fcntl.h>
18 #include <stdio.h>
19 #include <unistd.h>
20 
21 #include <string>
22 
23 #include "util/filesystem.h"
24 #include "util/ini_file.h"
25 #include "util/log.h"
26 
27 namespace netsim {
28 namespace osutils {
29 namespace {
30 
31 struct DiscoveryDir {
32   const char *root_env;
33   const char *subdir;
34 };
35 
36 DiscoveryDir discovery {
37 #if defined(_WIN32)
38   "LOCALAPPDATA", "Temp"
39 #elif defined(__linux__)
40   "XDG_RUNTIME_DIR", ""
41 #elif defined(__APPLE__)
42   "HOME", "Library/Caches/TemporaryItems"
43 #else
44 #error This platform is not supported.
45 #endif
46 };
47 
48 }  // namespace
49 
GetEnv(const std::string & name,const std::string & default_value)50 std::string GetEnv(const std::string &name, const std::string &default_value) {
51   auto val = std::getenv(name.c_str());
52   if (!val) {
53     return default_value;
54   }
55   return val;
56 }
57 
GetDiscoveryDirectory()58 std::string GetDiscoveryDirectory() {
59   // $TMPDIR is the temp directory on buildbots.
60   const char *test_env_p = std::getenv("TMPDIR");
61   if (test_env_p && *test_env_p) {
62     return std::string(test_env_p);
63   }
64   const char *env_p = std::getenv(discovery.root_env);
65   if (!env_p) {
66     BtsLog("No discovery env for %s, using tmp/", discovery.root_env);
67     env_p = "/tmp";
68   }
69   return std::string(env_p) + netsim::filesystem::slash + discovery.subdir;
70 }
71 
GetNetsimIniFilepath()72 std::string GetNetsimIniFilepath() {
73   auto discovery_dir = GetDiscoveryDirectory();
74   // Check if directory has a trailing slash.
75   if (discovery_dir.back() != netsim::filesystem::slash.back())
76     discovery_dir.append(netsim::filesystem::slash);
77   return discovery_dir.append("netsim.ini");
78 }
79 
GetServerAddress(bool frontend_server)80 std::optional<std::string> GetServerAddress(bool frontend_server) {
81   auto filepath = GetNetsimIniFilepath();
82   if (!netsim::filesystem::exists(filepath)) {
83     BtsLog("Unable to find netsim ini file: %s", filepath.c_str());
84     return std::nullopt;
85   }
86   if (!netsim::filesystem::is_regular_file(filepath)) {
87     BtsLog("Not a regular file: %s", filepath.c_str());
88     return std::nullopt;
89   }
90   IniFile iniFile(filepath);
91   iniFile.Read();
92   return iniFile.Get("grpc.port");
93 }
94 }  // namespace osutils
95 }  // namespace netsim
96