program_location_impl.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright 2014 Renato Tegon Forti, Antony Polukhin.
  2. // Copyright 2015 Antony Polukhin.
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt
  6. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_DLL_DETAIL_POSIX_PROGRAM_LOCATION_IMPL_HPP
  8. #define BOOST_DLL_DETAIL_POSIX_PROGRAM_LOCATION_IMPL_HPP
  9. #include <boost/config.hpp>
  10. #include <boost/dll/detail/system_error.hpp>
  11. #include <boost/filesystem/path.hpp>
  12. #include <boost/predef/os.h>
  13. #ifdef BOOST_HAS_PRAGMA_ONCE
  14. # pragma once
  15. #endif
  16. #if BOOST_OS_MACOS || BOOST_OS_IOS
  17. #include <mach-o/dyld.h>
  18. namespace boost { namespace dll { namespace detail {
  19. inline boost::filesystem::path program_location_impl(boost::system::error_code &ec) {
  20. ec.clear();
  21. char path[1024];
  22. uint32_t size = sizeof(path);
  23. if (_NSGetExecutablePath(path, &size) == 0)
  24. return boost::filesystem::path(path);
  25. char *p = new char[size];
  26. if (_NSGetExecutablePath(p, &size) != 0) {
  27. ec = boost::system::error_code(
  28. boost::system::errc::bad_file_descriptor,
  29. boost::system::generic_category()
  30. );
  31. }
  32. boost::filesystem::path ret(p);
  33. delete[] p;
  34. return ret;
  35. }
  36. }}} // namespace boost::dll::detail
  37. #elif BOOST_OS_SOLARIS
  38. #include <stdlib.h>
  39. namespace boost { namespace dll { namespace detail {
  40. inline boost::filesystem::path program_location_impl(boost::system::error_code& ec) {
  41. ec.clear();
  42. return boost::filesystem::path(getexecname());
  43. }
  44. }}} // namespace boost::dll::detail
  45. #elif BOOST_OS_BSD_FREE
  46. #include <sys/types.h>
  47. #include <sys/sysctl.h>
  48. #include <stdlib.h>
  49. namespace boost { namespace dll { namespace detail {
  50. inline boost::filesystem::path program_location_impl(boost::system::error_code& ec) {
  51. ec.clear();
  52. int mib[4];
  53. mib[0] = CTL_KERN;
  54. mib[1] = KERN_PROC;
  55. mib[2] = KERN_PROC_PATHNAME;
  56. mib[3] = -1;
  57. char buf[10240];
  58. size_t cb = sizeof(buf);
  59. sysctl(mib, 4, buf, &cb, NULL, 0);
  60. return boost::filesystem::path(buf);
  61. }
  62. }}} // namespace boost::dll::detail
  63. #elif BOOST_OS_BSD_NET
  64. #include <boost/filesystem/operations.hpp>
  65. namespace boost { namespace dll { namespace detail {
  66. inline boost::filesystem::path program_location_impl(boost::system::error_code &ec) {
  67. return boost::filesystem::read_symlink("/proc/curproc/exe", ec);
  68. }
  69. }}} // namespace boost::dll::detail
  70. #elif BOOST_OS_BSD_DRAGONFLY
  71. #include <boost/filesystem/operations.hpp>
  72. namespace boost { namespace dll { namespace detail {
  73. inline boost::filesystem::path program_location_impl(boost::system::error_code &ec) {
  74. return boost::filesystem::read_symlink("/proc/curproc/file", ec);
  75. }
  76. }}} // namespace boost::dll::detail
  77. #elif BOOST_OS_QNX
  78. #include <fstream>
  79. #include <string> // for std::getline
  80. namespace boost { namespace dll { namespace detail {
  81. inline boost::filesystem::path program_location_impl(boost::system::error_code &ec) {
  82. ec.clear();
  83. std::string s;
  84. std::ifstream ifs("/proc/self/exefile");
  85. std::getline(ifs, s);
  86. if (ifs.fail() || s.empty()) {
  87. ec = boost::system::error_code(
  88. boost::system::errc::bad_file_descriptor,
  89. boost::system::generic_category()
  90. );
  91. }
  92. return boost::filesystem::path(s);
  93. }
  94. }}} // namespace boost::dll::detail
  95. #else // BOOST_OS_LINUX || BOOST_OS_UNIX || BOOST_OS_HPUX || BOOST_OS_ANDROID
  96. #include <boost/filesystem/operations.hpp>
  97. namespace boost { namespace dll { namespace detail {
  98. inline boost::filesystem::path program_location_impl(boost::system::error_code &ec) {
  99. // We can not use
  100. // boost::dll::detail::path_from_handle(dlopen(NULL, RTLD_LAZY | RTLD_LOCAL), ignore);
  101. // because such code returns empty path.
  102. return boost::filesystem::read_symlink("/proc/self/exe", ec); // Linux specific
  103. }
  104. }}} // namespace boost::dll::detail
  105. #endif
  106. #endif // BOOST_DLL_DETAIL_POSIX_PROGRAM_LOCATION_IMPL_HPP