Add executable_path() and convert SubprocessTest to use it
authorTudor Bosman <tudorb@fb.com>
Wed, 22 Jul 2015 19:43:36 +0000 (12:43 -0700)
committerfacebook-github-bot-4 <folly-bot@fb.com>
Wed, 22 Jul 2015 20:22:26 +0000 (13:22 -0700)
Summary: Because I didn't want to write it again.

Reviewed By: @yfeldblum

Differential Revision: D2217246

folly/experimental/io/FsUtil.cpp
folly/experimental/io/FsUtil.h
folly/test/SubprocessTest.cpp

index 4aacb975b9bad35d76ab7d5a23e35e73d0c98cf1..7b0b47992d9e4e75685be057b1d19d62d036200e 100644 (file)
@@ -16,6 +16,9 @@
 
 #include <folly/experimental/io/FsUtil.h>
 
+#include <unistd.h>
+#include <folly/Exception.h>
+
 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
index d0f52488c786fb4cac71f7e7bac8210e8d4971bc..9f25febc7e178be60eb141a9855749b51ee70ffa 100644 (file)
@@ -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
 
index 489e85500aff0b28d9fd21d0b3cec35b96576f11..b42a0d57d27d99588fc5e8fba2b46bc720c23d81 100644 (file)
@@ -31,6 +31,7 @@
 #include <folly/gen/Base.h>
 #include <folly/gen/File.h>
 #include <folly/gen/String.h>
+#include <folly/experimental/TestUtil.h>
 #include <folly/experimental/io/FsUtil.h>
 
 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());