Path::Path(){ // get the home directory _HomeDir = getEnvVar("HOME"); // get the root directory setRootDir(getEnvVar("FREESTYLE_DIR")); //setRootDir(QString(".")); _pInstance = this; }
std::shared_ptr<PlatformProcess> PlatformProcess::getLauncherProcess() { auto launcher_handle = getEnvVar("OSQUERY_LAUNCHER"); if (!launcher_handle) { return std::make_shared<PlatformProcess>(); } // Convert the environment variable into a HANDLE (the value from environment // variable should be a hex value). As a precaution, ensure that the HANDLE is // valid. HANDLE handle = INVALID_HANDLE_VALUE; try { handle = reinterpret_cast<HANDLE>(static_cast<std::uintptr_t>( std::stoull(*launcher_handle, nullptr, 16))); } catch (std::invalid_argument e) { return std::make_shared<PlatformProcess>(); } catch (std::out_of_range e) { return std::make_shared<PlatformProcess>(); } if (handle == NULL || handle == INVALID_HANDLE_VALUE) { return std::make_shared<PlatformProcess>(); } return std::make_shared<PlatformProcess>(handle); }
QueryData genChocolateyPackages(QueryContext& context) { QueryData results; auto chocoEnvInstall = getEnvVar("ChocolateyInstall"); fs::path chocoInstallPath; if (chocoEnvInstall.is_initialized()) { chocoInstallPath = fs::path(*chocoEnvInstall); } if (chocoInstallPath.empty()) { LOG(WARNING) << "Did not find chocolatey path environment variable"; return results; } auto nuspecPattern = chocoInstallPath / "lib/%/%.nuspec"; std::vector<std::string> manifests; resolveFilePattern(nuspecPattern, manifests, GLOB_FILES); for (const auto& pkg : manifests) { Row r; auto s = genPackage(pkg, r); if (!s.ok()) { VLOG(1) << "Failed to parse " << pkg << " with " << s.getMessage(); } results.push_back(r); } return results; }
QueryData genScheduledTasks(QueryContext& context) { QueryData results; // First process all tasks in the root folder enumerateTasksForFolder("\\", results); // We attempt to derive the location of the tasks folder auto sysRoot = getEnvVar("SystemRoot"); if (!sysRoot.is_initialized()) { return results; } auto sysTaskPath = *sysRoot + "\\System32\\Tasks"; // Then process all tasks in subsequent folders std::vector<std::string> taskPaths; listDirectoriesInDirectory(sysTaskPath, taskPaths, true); for (const auto& path : taskPaths) { if (sysTaskPath.size() >= path.size()) { VLOG(1) << "Invalid task path " << path; continue; } auto taskPath = "\\" + join(split(path.substr(sysTaskPath.size(), (path.size() - sysTaskPath.size())), "\\"), "\\"); enumerateTasksForFolder(taskPath, results); } return results; }
std::string generateToken(const std::map<std::string, size_t>& lengths, const std::vector<std::string>& columns) { std::string out = "+"; for (const auto& col : columns) { size_t size = ((lengths.count(col) > 0) ? lengths.at(col) : col.size()) + 2; if (getEnvVar("ENHANCE").is_initialized()) { std::string e = "\xF0\x9F\x90\x8C"; e[2] += kOffset[1]; e[3] += kOffset[0]; for (size_t i = 0; i < size; i++) { e[3] = '\x8c' + kOffset[0]++; if (e[3] == '\xbf') { e[3] = '\x80'; kOffset[1] = (kOffset[1] > 3 && kOffset[1] < 8) ? 9 : kOffset[1]; e[2] = '\x90' + ++kOffset[1]; kOffset[0] = 0; } if (kOffset[1] == ('\x97' - '\x8d')) { kOffset = {0, 0}; } out += e.c_str(); } } else { out += std::string(size, '-'); } out += "+"; } out += "\n"; return out; }
std::shared_ptr<PlatformProcess> PlatformProcess::launchTestPythonScript( const std::string& args) { std::string osquery_path; auto osquery_path_option = getEnvVar("OSQUERY_DEPS"); if (osquery_path_option) { osquery_path = *osquery_path_option; } else { if (!isPlatform(PlatformType::TYPE_FREEBSD)) { osquery_path = "/usr/local/osquery"; } else { osquery_path = "/usr/local"; } } // The whole-string, space-delimited, python process arguments. auto argv = osquery_path + "/bin/python " + args; std::shared_ptr<PlatformProcess> process; int process_pid = ::fork(); if (process_pid == 0) { // Start a Python script ::execlp("sh", "sh", "-c", argv.c_str(), nullptr); ::exit(0); } else if (process_pid > 0) { process.reset(new PlatformProcess(process_pid)); } return process; }
RpmEnvironmentManager() : config_(getEnvVar("RPM_CONFIGDIR")) { // Honor a caller's environment if (!config_.is_initialized()) { setEnvVar("RPM_CONFIGDIR", "/usr/lib/rpm"); } callback_ = rpmlogSetCallback(&RpmEnvironmentManager::Callback, nullptr); }
// Returns the absolute path to the directory that contains the cube // map sets std::string cubemapDir(void) { std::string rootDir = getEnvVar("ASSIGNMENT3_ROOT"); if (rootDir.empty()) { std::cout << "Error: ASSIGNMENT3_ROOT is not set." << std::endl; std::exit(EXIT_FAILURE); } return rootDir + "/cubemaps/"; }
MemoryManager<T>::MemoryManager(int num_devices, unsigned max_buffers, bool debug) : mem_step_size(1024) , max_buffers(max_buffers) , memory(num_devices) , logger(loggerFactory("mem")) , debug_mode(debug) { // Check for environment variables // Debug mode string env_var = getEnvVar("AF_MEM_DEBUG"); if (!env_var.empty()) this->debug_mode = env_var[0] != '0'; if (this->debug_mode) mem_step_size = 1; // Max Buffer count env_var = getEnvVar("AF_MAX_BUFFERS"); if (!env_var.empty()) this->max_buffers = max(1, stoi(env_var)); }
// Provides the absolute path to the shader directory. std::string shaderDir(void) { std::string rootDir = getEnvVar("ASSIGNMENT1_ROOT"); if (rootDir.empty()) { std::cout << "Error: ASSIGNMENT1_ROOT is not set." << std::endl; exit(EXIT_FAILURE); } return rootDir + "/part2/src/shaders/"; }
// Returns the absolute path to the 3D model directory std::string modelDir(void) { std::string rootDir = getEnvVar("ASSIGNMENT2_ROOT"); if (rootDir.empty()) { std::cout << "Error: ASSIGNMENT2_ROOT is not set." << std::endl; std::exit(EXIT_FAILURE); } return rootDir + "/part2/3d_models/"; }
TEST_F(ProcessTests, test_envVar) { auto val = getEnvVar("GTEST_OSQUERY"); EXPECT_FALSE(val); EXPECT_FALSE(val.is_initialized()); EXPECT_TRUE(setEnvVar("GTEST_OSQUERY", "true")); val = getEnvVar("GTEST_OSQUERY"); EXPECT_FALSE(!val); EXPECT_TRUE(val.is_initialized()); EXPECT_EQ(*val, "true"); EXPECT_TRUE(unsetEnvVar("GTEST_OSQUERY")); val = getEnvVar("GTEST_OSQUERY"); EXPECT_FALSE(val); EXPECT_FALSE(val.is_initialized()); }
filesystem::path getDatabaseFile() { std::string dbFileString = getEnvVar("NCBI_NT_PATH"); if (dbFileString.empty()) throw GeneHunterException("NCBI_NT_PATH not defined"); filesystem::path dbFile = filesystem::path(dbFileString) / "nt"; if (!exists(dbFile)) throw GeneHunterException(dbFile.string() + " not exist"); return dbFile; }
filesystem::path getGeneHunterRootDirectory() { std::string geneHunterRoot = getEnvVar("GENEHUNTER_ROOT"); if (geneHunterRoot.empty()) throw GeneHunterException("GENEHUNTER_ROOT not defined"); filesystem::path geneHunterRootPath = filesystem::path(geneHunterRoot); if (!exists(geneHunterRootPath)) throw GeneHunterException(geneHunterRootPath.string() + " not exist"); return geneHunterRootPath; }
filesystem::path getLocalDirectory() { std::string localScratch = getEnvVar("SCRATCH_LOCAL"); if (localScratch.empty()) throw GeneHunterException("SCRATCH_LOCAL not defined"); filesystem::path localScratchPath = filesystem::path(localScratch); if (!exists(localScratchPath)) throw GeneHunterException(localScratchPath.string() + " not exist"); return localScratchPath; }
filesystem::path getSharedDirectory() { std::string scratchShared = getEnvVar("SCRATCH_SHARED"); if (scratchShared.empty()) throw GeneHunterException("SCRATCH_SHARED not defined"); filesystem::path scratchSharedPath = filesystem::path(scratchShared); if (!exists(scratchSharedPath)) throw GeneHunterException(scratchSharedPath.string() + " not exist"); return scratchSharedPath; }
// Returns the absolute path to the shader directory std::string shaderDir(void) { std::string rootDir = getEnvVar("ASSIGNMENT3_ROOT"); if (rootDir.empty()) { std::cout << "Error: ASSIGNMENT3_ROOT is not set." << std::endl; std::getchar(); std::exit(EXIT_FAILURE); } return rootDir + "/model_viewer/src/shaders/"; }
bool Watcher::hasManagedExtensions() { if (instance().extensions_.size() > 0) { return true; } // A watchdog process may hint to a worker the number of managed extensions. // Setting this counter to 0 will prevent the worker from waiting for missing // dependent config plugins. Otherwise, its existence, will cause a worker to // wait for missing plugins to broadcast from managed extensions. return getEnvVar("OSQUERY_EXTENSIONS").is_initialized(); }
boost::optional<std::string> getHomeDirectory() { // Try to get the caller's home directory using HOME and getpwuid. auto user = ::getpwuid(getuid()); auto homedir = getEnvVar("HOME"); if (homedir.is_initialized()) { return homedir; } else if (user != nullptr && user->pw_dir != nullptr) { return std::string(user->pw_dir); } else { return boost::none; } }
unsigned getMaxJitSize() { const int MAX_JIT_LEN = 100; thread_local int length = 0; if (length == 0) { string env_var = getEnvVar("AF_CPU_MAX_JIT_LEN"); if (!env_var.empty()) { length = stoi(env_var); } else { length = MAX_JIT_LEN; } } return length; }
std::string generateHeader(const std::map<std::string, size_t>& lengths, const std::vector<std::string>& columns) { if (getEnvVar("ENHANCE").is_initialized()) { kToken = "\xF0\x9F\x91\x8D"; } std::string out = kToken; for (const auto& col : columns) { out += " " + col; if (lengths.count(col) > 0) { int buffer_size = static_cast<int>(lengths.at(col) - utf8StringSize(col)); if (buffer_size > 0) { out += std::string(buffer_size, ' '); } } out += ' '; out += kToken; } out += "\n"; return out; }
std::shared_ptr<PlatformProcess> PlatformProcess::launchTestPythonScript( const std::string& args) { STARTUPINFOA si = {0}; PROCESS_INFORMATION pi = {nullptr}; auto argv = "python " + args; std::vector<char> mutable_argv(argv.begin(), argv.end()); mutable_argv.push_back('\0'); si.cb = sizeof(si); auto pythonEnv = getEnvVar("OSQUERY_PYTHON_PATH"); std::string pythonPath; if (pythonEnv.is_initialized()) { pythonPath = *pythonEnv; } // Python is installed at this location if the provisioning script is used. // This path should work regardless of the existence of the SystemDrive // environment variable. pythonPath += "\\python.exe"; std::shared_ptr<PlatformProcess> process; if (::CreateProcessA(pythonPath.c_str(), mutable_argv.data(), nullptr, nullptr, FALSE, 0, nullptr, nullptr, &si, &pi)) { process.reset(new PlatformProcess(pi.hProcess)); ::CloseHandle(pi.hThread); ::CloseHandle(pi.hProcess); } return process; }
/* Takes the command struct and takes approproate action. * Will either execute a shell command, program, or output an error message. * @params: command - a struct representing the command * @return: false is the command was note found, true otherwise. */ bool processCommand(command_t* command, char* pEnv[]){ char* defaultDir = getenv("HOME"); if( strcmp(command->name, "cd") == CMP_OK) { if( command->argc == 1 ) { changeDirectory(defaultDir); } else { changeDirectory(command->argv[1]); } } else if( strcmp(command->name,"help") == CMP_OK) { help(); } else if( strcmp(command->name, "senv") == CMP_OK) { char* envName = NULL; char* envVal = NULL; if( command->argc > 1) { envName = command->argv[1]; } if( command->argc > 2) { envVal = command->argv[2]; } setEnvVar(envName,envVal); } else if( strcmp(command->name, "usenv") == CMP_OK) { char* envName = NULL; if( command->argc > 1) { envName = command->argv[1]; } unsetEnvVar(envName); } else if( strcmp(command->name, "genv") == CMP_OK) { char* envName = NULL; if( command-> argc > 1) { envName = command->argv[1]; } getEnvVar(envName); } else { char* filePath = getFilePath(command->name); if(filePath != NULL) { pid_t pid_command = fork(); if(pid_command < CHILD) { // ERROR fprintf(stderr, "Fork failure!\n"); } else if ( pid_command == CHILD) { if(command->isBg) { freopen("/dev/null","w",stdout); freopen("/dev/null","w",stderr); } execve(filePath, command->argv, pEnv); } else { if(!command->isBg) { int stat; waitpid(pid_command, &stat, 0); } else { printf("Process ID: %d\n", pid_command); } } } else if(strcmp(command->name, "quit") == CMP_OK || strcmp(command->name, "exit") == CMP_OK || strcmp(command->name, "q") == CMP_OK ) { return false; } else { printf("%s: Command not found\n", command->name); return false; } } return true; }
std::string getMysqlHost() { return getEnvVar("MYSQL_HOST"); }
std::string getMysqlUser() { return getEnvVar("MYSQL_USER"); }
std::string getMysqlPassword() { return getEnvVar("MYSQL_PASS"); }
std::string getMysqlTaxDatabase() { return getEnvVar("MYSQL_TAX_DB"); }
void Config::setup (std::string const& strConf, bool bQuiet) { boost::filesystem::path dataDir; boost::system::error_code ec; std::string strDbPath, strConfFile; // // Determine the config and data directories. // If the config file is found in the current working directory, use the current working directory as the config directory and // that with "db" as the data directory. // QUIET = bQuiet; NODE_SIZE = 0; strDbPath = Helpers::getDatabaseDirName (); strConfFile = strConf.empty () ? Helpers::getConfigFileName () : strConf; VALIDATORS_BASE = Helpers::getValidatorsFileName (); VALIDATORS_URI = boost::str (boost::format ("/%s") % VALIDATORS_BASE); if (!strConf.empty ()) { // --conf=<path> : everything is relative that file. CONFIG_FILE = strConfFile; CONFIG_DIR = boost::filesystem::absolute (CONFIG_FILE); CONFIG_DIR.remove_filename (); dataDir = CONFIG_DIR / strDbPath; } else { CONFIG_DIR = boost::filesystem::current_path (); CONFIG_FILE = CONFIG_DIR / strConfFile; dataDir = CONFIG_DIR / strDbPath; // Construct XDG config and data home. // http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html std::string strHome = getEnvVar ("HOME"); std::string strXdgConfigHome = getEnvVar ("XDG_CONFIG_HOME"); std::string strXdgDataHome = getEnvVar ("XDG_DATA_HOME"); if (boost::filesystem::exists (CONFIG_FILE) // Can we figure out XDG dirs? || (strHome.empty () && (strXdgConfigHome.empty () || strXdgDataHome.empty ()))) { // Current working directory is fine, put dbs in a subdir. } else { if (strXdgConfigHome.empty ()) { // $XDG_CONFIG_HOME was not set, use default based on $HOME. strXdgConfigHome = strHome + "/.config"; } if (strXdgDataHome.empty ()) { // $XDG_DATA_HOME was not set, use default based on $HOME. strXdgDataHome = strHome + "/.local/share"; } CONFIG_DIR = strXdgConfigHome + "/" + systemName (); CONFIG_FILE = CONFIG_DIR / strConfFile; dataDir = strXdgDataHome + "/" + systemName (); boost::filesystem::create_directories (CONFIG_DIR, ec); if (ec) throw std::runtime_error (boost::str (boost::format ("Can not create %s") % CONFIG_DIR)); } } HTTPClient::initializeSSLContext(); // Update default values load (); { // load() may have set a new value for the dataDir std::string const dbPath (legacy ("database_path")); if (!dbPath.empty ()) { dataDir = boost::filesystem::path (dbPath); } } boost::filesystem::create_directories (dataDir, ec); if (ec) throw std::runtime_error ( boost::str (boost::format ("Can not create %s") % dataDir)); legacy ("database_path", boost::filesystem::absolute (dataDir).string ()); }
std::string getMysqlMyDatabase() { return getEnvVar("MYSQL_MY_DB"); }
void resync() { LD_LIBRARY_PATH = getEnvVar("LD_LIBRARY_PATH"); PYTHONPATH = getEnvVar("PYTHONPATH"); CLASSPATH = getEnvVar("CLASSPATH"); OCTAVE_PATH = getEnvVar("OCTAVE_PATH"); }