1 // Copyright 2014 Renato Tegon Forti, Antony Polukhin.
2 // Copyright 2015 Antony Polukhin.
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)
8 #ifndef BOOST_DLL_DETAIL_POSIX_PROGRAM_LOCATION_IMPL_HPP
9 #define BOOST_DLL_DETAIL_POSIX_PROGRAM_LOCATION_IMPL_HPP
11 #include <boost/config.hpp>
12 #include <boost/dll/detail/system_error.hpp>
13 #include <boost/filesystem/path.hpp>
14 #include <boost/predef/os.h>
16 #ifdef BOOST_HAS_PRAGMA_ONCE
20 #if BOOST_OS_MACOS || BOOST_OS_IOS
22 #include <mach-o/dyld.h>
24 namespace boost { namespace dll { namespace detail {
25 inline boost::filesystem::path program_location_impl(boost::system::error_code &ec) {
29 uint32_t size = sizeof(path);
30 if (_NSGetExecutablePath(path, &size) == 0)
31 return boost::filesystem::path(path);
33 char *p = new char[size];
34 if (_NSGetExecutablePath(p, &size) != 0) {
35 ec = boost::system::error_code(
36 boost::system::errc::bad_file_descriptor,
37 boost::system::generic_category()
41 boost::filesystem::path ret(p);
45 }}} // namespace boost::dll::detail
47 #elif BOOST_OS_SOLARIS
50 namespace boost { namespace dll { namespace detail {
51 inline boost::filesystem::path program_location_impl(boost::system::error_code& ec) {
54 return boost::filesystem::path(getexecname());
56 }}} // namespace boost::dll::detail
58 #elif BOOST_OS_BSD_FREE
60 #include <sys/types.h>
61 #include <sys/sysctl.h>
64 namespace boost { namespace dll { namespace detail {
65 inline boost::filesystem::path program_location_impl(boost::system::error_code& ec) {
71 mib[2] = KERN_PROC_PATHNAME;
74 size_t cb = sizeof(buf);
75 sysctl(mib, 4, buf, &cb, NULL, 0);
77 return boost::filesystem::path(buf);
79 }}} // namespace boost::dll::detail
83 #elif BOOST_OS_BSD_NET
85 #include <boost/filesystem/operations.hpp>
86 namespace boost { namespace dll { namespace detail {
87 inline boost::filesystem::path program_location_impl(boost::system::error_code &ec) {
88 return boost::filesystem::read_symlink("/proc/curproc/exe", ec);
90 }}} // namespace boost::dll::detail
92 #elif BOOST_OS_BSD_DRAGONFLY
94 #include <boost/filesystem/operations.hpp>
95 namespace boost { namespace dll { namespace detail {
96 inline boost::filesystem::path program_location_impl(boost::system::error_code &ec) {
97 return boost::filesystem::read_symlink("/proc/curproc/file", ec);
99 }}} // namespace boost::dll::detail
104 #include <string> // for std::getline
105 namespace boost { namespace dll { namespace detail {
106 inline boost::filesystem::path program_location_impl(boost::system::error_code &ec) {
110 std::ifstream ifs("/proc/self/exefile");
111 std::getline(ifs, s);
113 if (ifs.fail() || s.empty()) {
114 ec = boost::system::error_code(
115 boost::system::errc::bad_file_descriptor,
116 boost::system::generic_category()
120 return boost::filesystem::path(s);
122 }}} // namespace boost::dll::detail
124 #else // BOOST_OS_LINUX || BOOST_OS_UNIX || BOOST_OS_HPUX || BOOST_OS_ANDROID
126 #include <boost/filesystem/operations.hpp>
127 namespace boost { namespace dll { namespace detail {
128 inline boost::filesystem::path program_location_impl(boost::system::error_code &ec) {
130 // boost::dll::detail::path_from_handle(dlopen(NULL, RTLD_LAZY | RTLD_LOCAL), ignore);
131 // because such code returns empty path.
133 return boost::filesystem::read_symlink("/proc/self/exe", ec); // Linux specific
135 }}} // namespace boost::dll::detail
139 #endif // BOOST_DLL_DETAIL_POSIX_PROGRAM_LOCATION_IMPL_HPP