blob: ef228cbf208a0d0e785bcc271d447dcffe7aab7d (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <dlfcn.h>
#include <algorithm>
#include <memory>
#include "opentelemetry/plugin/detail/dynamic_library_handle.h"
#include "opentelemetry/plugin/detail/loader_info.h"
#include "opentelemetry/plugin/detail/utility.h"
#include "opentelemetry/plugin/factory.h"
#include "opentelemetry/plugin/hook.h"
#include "opentelemetry/version.h"
OPENTELEMETRY_BEGIN_NAMESPACE
namespace plugin
{
class DynamicLibraryHandleUnix final : public DynamicLibraryHandle
{
public:
explicit DynamicLibraryHandleUnix(void *handle) noexcept : handle_{handle} {}
DynamicLibraryHandleUnix(const DynamicLibraryHandleUnix &) = delete;
DynamicLibraryHandleUnix(DynamicLibraryHandleUnix &&) = delete;
DynamicLibraryHandleUnix &operator=(const DynamicLibraryHandleUnix &) = delete;
DynamicLibraryHandleUnix &operator=(DynamicLibraryHandleUnix &&) = delete;
~DynamicLibraryHandleUnix() override { ::dlclose(handle_); }
private:
void *handle_;
};
inline std::unique_ptr<Factory> LoadFactory(const char *plugin, std::string &error_message) noexcept
{
dlerror(); // Clear any existing error.
auto handle = ::dlopen(plugin, RTLD_NOW | RTLD_LOCAL);
if (handle == nullptr)
{
detail::CopyErrorMessage(dlerror(), error_message);
return nullptr;
}
std::shared_ptr<DynamicLibraryHandle> library_handle{new (std::nothrow)
DynamicLibraryHandleUnix{handle}};
if (library_handle == nullptr)
{
return nullptr;
}
auto make_factory_impl =
reinterpret_cast<OpenTelemetryHook *>(::dlsym(handle, "OpenTelemetryMakeFactoryImpl"));
if (make_factory_impl == nullptr)
{
detail::CopyErrorMessage(dlerror(), error_message);
return nullptr;
}
if (*make_factory_impl == nullptr)
{
detail::CopyErrorMessage("Invalid plugin hook", error_message);
return nullptr;
}
LoaderInfo loader_info;
nostd::unique_ptr<char[]> plugin_error_message;
auto factory_impl = (**make_factory_impl)(loader_info, plugin_error_message);
if (factory_impl == nullptr)
{
detail::CopyErrorMessage(plugin_error_message.get(), error_message);
return nullptr;
}
return std::unique_ptr<Factory>{new (std::nothrow)
Factory{std::move(library_handle), std::move(factory_impl)}};
}
} // namespace plugin
OPENTELEMETRY_END_NAMESPACE
|