1// Copyright 2014 Renato Tegon Forti, Antony Polukhin.
2// Copyright Antony Polukhin, 2015-2024.
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
8#ifndef BOOST_DLL_DETAIL_POSIX_PROGRAM_LOCATION_IMPL_HPP
9#define BOOST_DLL_DETAIL_POSIX_PROGRAM_LOCATION_IMPL_HPP
10
11#include <boost/dll/config.hpp>
12#include <boost/dll/detail/system_error.hpp>
13#include <boost/predef/os.h>
14
15#ifdef BOOST_HAS_PRAGMA_ONCE
16# pragma once
17#endif
18
19#if BOOST_OS_MACOS || BOOST_OS_IOS
20
21#include <mach-o/dyld.h>
22
23namespace boost { namespace dll { namespace detail {
24 inline boost::dll::fs::path program_location_impl(boost::dll::fs::error_code &ec) {
25 ec.clear();
26
27 char path[1024];
28 uint32_t size = sizeof(path);
29 if (_NSGetExecutablePath(path, &size) == 0)
30 return boost::dll::fs::path(path);
31
32 char *p = new char[size];
33 if (_NSGetExecutablePath(p, &size) != 0) {
34 ec = boost::dll::fs::make_error_code(
35 boost::dll::fs::errc::bad_file_descriptor
36 );
37 }
38
39 boost::dll::fs::path ret(p);
40 delete[] p;
41 return ret;
42 }
43}}} // namespace boost::dll::detail
44
45#elif BOOST_OS_SOLARIS
46
47#include <stdlib.h>
48namespace boost { namespace dll { namespace detail {
49 inline boost::dll::fs::path program_location_impl(boost::dll::fs::error_code& ec) {
50 ec.clear();
51
52 return boost::dll::fs::path(getexecname());
53 }
54}}} // namespace boost::dll::detail
55
56#elif BOOST_OS_BSD_FREE
57
58#include <sys/types.h>
59#include <sys/sysctl.h>
60#include <stdlib.h>
61
62namespace boost { namespace dll { namespace detail {
63 inline boost::dll::fs::path program_location_impl(boost::dll::fs::error_code& ec) {
64 ec.clear();
65
66 int mib[4];
67 mib[0] = CTL_KERN;
68 mib[1] = KERN_PROC;
69 mib[2] = KERN_PROC_PATHNAME;
70 mib[3] = -1;
71 char buf[10240];
72 size_t cb = sizeof(buf);
73 sysctl(mib, 4, buf, &cb, NULL, 0);
74
75 return boost::dll::fs::path(buf);
76 }
77}}} // namespace boost::dll::detail
78
79
80
81#elif BOOST_OS_BSD_NET
82
83namespace boost { namespace dll { namespace detail {
84 inline boost::dll::fs::path program_location_impl(boost::dll::fs::error_code &ec) {
85 return boost::dll::fs::read_symlink("/proc/curproc/exe", ec);
86 }
87}}} // namespace boost::dll::detail
88
89#elif BOOST_OS_BSD_DRAGONFLY
90
91
92namespace boost { namespace dll { namespace detail {
93 inline boost::dll::fs::path program_location_impl(boost::dll::fs::error_code &ec) {
94 return boost::dll::fs::read_symlink("/proc/curproc/file", ec);
95 }
96}}} // namespace boost::dll::detail
97
98#elif BOOST_OS_QNX
99
100#include <fstream>
101#include <string> // for std::getline
102namespace boost { namespace dll { namespace detail {
103 inline boost::dll::fs::path program_location_impl(boost::dll::fs::error_code &ec) {
104 ec.clear();
105
106 std::string s;
107 std::ifstream ifs("/proc/self/exefile");
108 std::getline(ifs, s);
109
110 if (ifs.fail() || s.empty()) {
111 ec = boost::dll::fs::make_error_code(
112 boost::dll::fs::errc::bad_file_descriptor
113 );
114 }
115
116 return boost::dll::fs::path(s);
117 }
118}}} // namespace boost::dll::detail
119
120#else // BOOST_OS_LINUX || BOOST_OS_UNIX || BOOST_OS_HPUX || BOOST_OS_ANDROID
121
122namespace boost { namespace dll { namespace detail {
123 inline boost::dll::fs::path program_location_impl(boost::dll::fs::error_code &ec) {
124 // We can not use
125 // boost::dll::detail::path_from_handle(dlopen(NULL, RTLD_LAZY | RTLD_LOCAL), ignore);
126 // because such code returns empty path.
127
128 return boost::dll::fs::read_symlink(p: "/proc/self/exe", ec); // Linux specific
129 }
130}}} // namespace boost::dll::detail
131
132#endif
133
134#endif // BOOST_DLL_DETAIL_POSIX_PROGRAM_LOCATION_IMPL_HPP
135
136

source code of boost/libs/dll/include/boost/dll/detail/posix/program_location_impl.hpp