Repository: mesos Updated Branches: refs/heads/master acb94b203 -> c818fe6e5
Added functions to manipulate LD_LIBRARY_PATH to os::libraries. Also added a utility function to expand a library name to a filename by prefixing "lib" and adding the default extension. Review: https://reviews.apache.org/r/26903 Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/c818fe6e Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/c818fe6e Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/c818fe6e Branch: refs/heads/master Commit: c818fe6e59b330ea5cc1c4b3064d65bec642712d Parents: acb94b2 Author: Kapil Arya <[email protected]> Authored: Mon Oct 20 16:58:07 2014 -0700 Committer: Niklas Q. Nielsen <[email protected]> Committed: Mon Oct 20 17:21:44 2014 -0700 ---------------------------------------------------------------------- .../3rdparty/stout/include/stout/os.hpp | 57 ++++++++++++++++++++ .../3rdparty/stout/tests/os_tests.cpp | 40 ++++++++++++++ 2 files changed, 97 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/c818fe6e/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp index 63bda7a..5d3cbba 100644 --- a/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp +++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp @@ -1263,6 +1263,63 @@ inline Try<std::set<pid_t> > pids(Option<pid_t> group, Option<pid_t> session) return result; } + +namespace libraries { + +// Returns the full library name by adding prefix and extension to +// library name. +inline std::string expandName(const std::string& libraryName) +{ + const char* prefix = "lib"; + const char* extension = +#ifdef __linux__ + ".so"; +#else + ".dylib"; +#endif + + return prefix + libraryName + extension; +} + + +// Returns the current value of LD_LIBRARY_PATH environment variable. +inline std::string paths() +{ + const char* environmentVariable = +#ifdef __linux__ + "LD_LIBRARY_PATH"; +#else + "DYLD_LIBRARY_PATH"; +#endif + return getenv(environmentVariable, false); +} + + +// Updates the value of LD_LIBRARY_PATH environment variable. +inline void setPaths(const std::string& newPaths) +{ + const char* environmentVariable = +#ifdef __linux__ + "LD_LIBRARY_PATH"; +#else + "DYLD_LIBRARY_PATH"; +#endif + setenv(environmentVariable, newPaths); +} + + +// Append newPath to the current value of LD_LIBRARY_PATH environment +// variable. +inline void appendPaths(const std::string& newPaths) +{ + if (paths().empty()) { + setPaths(newPaths); + } else { + setPaths(paths() + ":" + newPaths); + } +} + +} // namespace libraries { } // namespace os { #endif // __STOUT_OS_HPP__ http://git-wip-us.apache.org/repos/asf/mesos/blob/c818fe6e/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp ---------------------------------------------------------------------- diff --git a/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp b/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp index 02293f2..e9f37df 100644 --- a/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp +++ b/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp @@ -749,3 +749,43 @@ TEST_F(OsTest, user) EXPECT_SOME(os::su(user.get())); EXPECT_ERROR(os::su(UUID::random().toString())); } + + +// Test setting/resetting/appending to LD_LIBRARY_PATH environment +// variable (DYLD_LIBRARY_PATH on OS X). +TEST_F(OsTest, Libraries) +{ + const std::string path1 = "/tmp/path1"; + const std::string path2 = "/tmp/path1"; + std::string ldLibraryPath; + const std::string originalLibraryPath = os::libraries::paths(); + + // Test setPaths. + os::libraries::setPaths(path1); + EXPECT_EQ(os::libraries::paths(), path1); + + // Test appendPaths. + // 1. With empty LD_LIBRARY_PATH. + // 1a. Set LD_LIBRARY_PATH to an empty string. + os::libraries::setPaths(""); + ldLibraryPath = os::libraries::paths(); + EXPECT_EQ(ldLibraryPath, ""); + + // 1b. Now test appendPaths. + os::libraries::appendPaths(path1); + EXPECT_EQ(os::libraries::paths(), path1); + + // 2. With non-empty LD_LIBRARY_PATH. + // 2a. Set LD_LIBRARY_PATH to some non-empty value. + os::libraries::setPaths(path2); + ldLibraryPath = os::libraries::paths(); + EXPECT_EQ(ldLibraryPath, path2); + + // 2b. Now test appendPaths. + os::libraries::appendPaths(path1); + EXPECT_EQ(os::libraries::paths(), path2 + ":" + path1); + + // Reset LD_LIBRARY_PATH. + os::libraries::setPaths(originalLibraryPath); + EXPECT_EQ(os::libraries::paths(), originalLibraryPath); +}
