From: Tudor Bosman Date: Wed, 22 Jul 2015 19:43:36 +0000 (-0700) Subject: Add executable_path() and convert SubprocessTest to use it X-Git-Tag: v0.52.0~12 X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=cd46406972560ba52a606cb21789b969fe9188c8;p=folly.git Add executable_path() and convert SubprocessTest to use it Summary: Because I didn't want to write it again. Reviewed By: @yfeldblum Differential Revision: D2217246 --- diff --git a/folly/experimental/io/FsUtil.cpp b/folly/experimental/io/FsUtil.cpp index 4aacb975..7b0b4799 100644 --- a/folly/experimental/io/FsUtil.cpp +++ b/folly/experimental/io/FsUtil.cpp @@ -16,6 +16,9 @@ #include +#include +#include + namespace bsys = ::boost::system; namespace folly { @@ -66,5 +69,9 @@ path canonical_parent(const path& pth, const path& base) { return canonical(pth.parent_path(), base) / pth.filename(); } +path executable_path() { + return read_symlink("/proc/self/exe"); +} + } // namespace fs } // namespace folly diff --git a/folly/experimental/io/FsUtil.h b/folly/experimental/io/FsUtil.h index d0f52488..9f25febc 100644 --- a/folly/experimental/io/FsUtil.h +++ b/folly/experimental/io/FsUtil.h @@ -52,6 +52,20 @@ path remove_prefix(const path& p, const path& prefix); */ path canonical_parent(const path& p, const path& basePath = current_path()); +/** + * Get the path to the current executable. + * + * Note that this is not reliable and not recommended in general; it may not be + * implemented on your platform (in which case it will throw), the executable + * might have been moved or replaced while running, and applications comprising + * of multiple executables should use some form of configuration system to + * find the other executables rather than relying on relative paths from one + * to another. + * + * So this should only be used for tests, logging, or other innocuous purposes. + */ +path executable_path(); + } // namespace fs } // namespace folly diff --git a/folly/test/SubprocessTest.cpp b/folly/test/SubprocessTest.cpp index 489e8550..b42a0d57 100644 --- a/folly/test/SubprocessTest.cpp +++ b/folly/test/SubprocessTest.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include using namespace folly; @@ -189,15 +190,8 @@ TEST(SimpleSubprocessTest, FdLeakTest) { TEST(ParentDeathSubprocessTest, ParentDeathSignal) { // Find out where we are. - static constexpr size_t pathLength = 2048; - char buf[pathLength + 1]; - int r = readlink("/proc/self/exe", buf, pathLength); - CHECK_ERR(r); - buf[r] = '\0'; - - fs::path helper(buf); - helper.remove_filename(); - helper /= "subprocess_test_parent_death_helper"; + auto helper = fs::executable_path(); + helper.remove_filename() /= "subprocess_test_parent_death_helper"; fs::path tempFile(fs::temp_directory_path() / fs::unique_path());