29 #ifdef HAVE_SYS_MMAN_H 38 constexpr
bool hasRTLDNoDelete =
true;
40 constexpr
bool hasRTLDNoDelete =
false;
48 : path_(std::move(path)), pathStr_(path_.string()) {}
58 if (hasRTLDNoDelete ||
59 !loadFlag_.test(LibraryLoadHint::PreventUnloadHint)) {
60 if (dlclose(handle_)) {
69 const std::filesystem::path path_;
70 const std::string pathStr_;
71 void *handle_ =
nullptr;
76 Library::Library(
const std::string &path)
77 :
Library(std::filesystem::path(path)) {}
79 Library::Library(
const char *path) :
Library(std::filesystem::path(path)) {}
81 Library::Library(
const std::filesystem::path &path)
82 : d_ptr(std::make_unique<LibraryPrivate>(path)) {}
84 FCITX_DEFINE_DEFAULT_DTOR_AND_MOVE(
Library)
92 if (hint & LibraryLoadHint::ResolveAllSymbolsHint) {
99 if (hint & LibraryLoadHint::PreventUnloadHint) {
100 flag |= RTLD_NODELETE;
104 if (hint & LibraryLoadHint::ExportExternalSymbolsHint) {
109 if (hint & LibraryLoadHint::NewNameSpace) {
111 d->handle_ = dlmopen(
113 !d->path_.empty() ? d->path_.string().c_str() :
nullptr, flag);
118 !d->path_.empty() ? d->path_.string().c_str() :
nullptr, flag);
121 d->error_ = dlerror();
130 bool Library::loaded()
const {
135 bool Library::unload() {
140 void *Library::resolve(
const char *name) {
142 auto *result = dlsym(d->handle_, name);
144 d->error_ = dlerror();
149 bool Library::findData(
const char *slug,
const char *magic,
size_t lenOfMagic,
150 const std::function<
void(
const char *data)> &parser) {
153 void *data = dlsym(d->handle_, slug);
158 if (memcmp(data, magic, lenOfMagic) != 0) {
162 data =
static_cast<char *
>(data) + lenOfMagic;
163 parser(static_cast<const char *>(data));
169 d->error_ = strerror(errno);
173 UniqueCPtr<void> needfree;
177 int statresult = fstat(fd.
fd(), &statbuf);
178 if (statresult < 0) {
179 d->error_ = strerror(errno);
182 void *data =
nullptr;
183 #ifdef HAVE_SYS_MMAN_H 185 mmap(
nullptr, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd.
fd(), 0);
186 void *needunmap =
nullptr;
187 if (data != MAP_FAILED) {
194 data = malloc(statbuf.st_size);
195 needfree.reset(data);
199 if (read(fd.
fd(), data, statbuf.st_size) != statbuf.st_size) {
204 static_cast<char *>(data), static_cast<size_t>(statbuf.st_size),
205 magic, lenOfMagic, 0);
212 #ifdef HAVE_SYS_MMAN_H 214 munmap(needunmap, statbuf.st_size);
222 std::string Library::error() {
227 bool Library::isNewNamespaceSupported() {
235 const std::string &Library::path()
const {
240 const std::filesystem::path &Library::fspath()
const {
Class wrap around the unix fd.
bool isValid() const noexcept
Check if fd is not empty.
Class to handler dynamic library.
Utility class to handle unix file descriptor.
New Utility classes to handle application specific path.
int fd() const noexcept
Get the internal fd.
const char * backwardSearch(const char *haystack, size_t l, const char *needle, size_t ol, size_t from)
Search string needle of size ol in string haystack.
static UnixFD openPath(const std::filesystem::path &path, std::optional< int > flags=std::nullopt, std::optional< mode_t > mode=std::nullopt)
Open the path.
Helper template class to make easier to use type safe enum flags.