if (!filter_checker.HasPrivilegeFilter()) {
for (auto& reader : readers_) {
ShmError result;
- auto handles = reader->GetHandles(&result);
+ auto app_lock = reader->GetLock();
+ if (!app_lock) {
+ LOGW("Failed to get lock");
+ return PMINFO_R_ERROR;
+ }
+
+ auto handles = reader->GetHandlesWithoutLock(&result);
if (!handles && result != ShmError::NONE)
return PMINFO_R_ERROR;
if (!handles)
continue;
- for (const auto& [_ , handle] : handles->GetMap())
+ for (const auto& handle : handles->GetHandles())
if (filter_checker.Check(handle))
list.emplace(handle.GetAppId(), handle.Clone());
}
}
ShmError result;
- auto app_handles = (*app_it)->GetHandles(&result);
+ auto app_handles = (*app_it)->GetHandlesWithoutLock(&result);
if (!app_handles && result != ShmError::NONE)
return PMINFO_R_ERROR;
continue;
}
- auto pkg_handles = (*pkg_it)->GetHandles(&result);
- if (!pkg_handles && result != ShmError::NONE)
- return PMINFO_R_ERROR;
-
- pkg_lock->Unlock();
- app_lock->Unlock();
-
-
- const auto& pkg_maps = pkg_handles->GetMap();
- for (const auto& [_ , a_handle] : app_handles->GetMap()) {
- auto pkg_it = pkg_maps.find(a_handle.GetPackage());
- if (pkg_it == pkg_maps.end())
- continue;
-
+ for (const auto& a_handle : app_handles->GetHandles()) {
if (filter_checker.Check(a_handle) &&
- filter_checker.CheckPrivilege(pkg_it->second))
+ filter_checker.CheckPrivilege(a_handle.GetPackage(), (*pkg_it)))
list.emplace(a_handle.GetAppId(), a_handle.Clone());
}
+ pkg_lock->Unlock();
+ app_lock->Unlock();
+
app_it++;
pkg_it++;
}
}
bool ShmAppReader::FilterChecker::CheckPrivilege(
- const PkgInfoHandle& p_handle) {
- if (!CheckPrivilegeFilters(p_handle, privilege_filters_))
+ const char* pkgid, ShmReader<PkgInfoHandle>* pkg_reader) {
+ ShmError result;
+ auto p_handle = pkg_reader->GetHandleWithoutLock(pkgid, &result);
+ if (!p_handle && result != ShmError::NONE)
+ return false;
+
+ if (!CheckPrivilegeFilters(*p_handle, privilege_filters_))
return false;
return true;
FilterChecker(uid_t uid);
bool Init(pkgmgrinfo_filter_x* filter);
bool Check(const AppInfoHandle& handle);
- bool CheckPrivilege(const PkgInfoHandle& p_handle);
+ bool CheckPrivilege(const char* pkgid,
+ ShmReader<PkgInfoHandle>* pkg_reader);
const char* AppIdFilterValue();
bool HasPrivilegeFilter();
if (!handles)
continue;
- for (const auto& [_ , handle] : handles->GetMap())
+ for (const auto& handle : handles->GetHandles())
if (filter_checker.Check(handle))
list.emplace(handle.GetPackage(), handle.Clone());
}
}
template<typename T>
-std::optional<typename ShmReader<T>::HandleCollection> ShmReader<T>::GetHandles(ShmError* result) {
+std::optional<typename ShmReader<T>::HandleCollection>
+ ShmReader<T>::GetHandles(ShmError* result) {
std::shared_lock<std::shared_mutex> s(lock_);
auto lock = Load(result, s);
if (!lock)
return ReadHandles(result);
}
+template<typename T>
+std::optional<typename ShmReader<T>::HandleCollection>
+ ShmReader<T>::GetHandlesWithoutLock(ShmError* result) {
+ return ReadHandles(result);
+}
+
template<typename T>
std::optional<T> ShmReader<T>::GetHandle(const char* key, ShmError* result) {
std::shared_lock<std::shared_mutex> s(lock_);
return handle;
}
+template<typename T>
+std::optional<T> ShmReader<T>::GetHandleWithoutLock(
+ const char* key, ShmError* result) {
+ HandleMappingData* index_ptr = FindIndex(key, result);
+ if (!index_ptr) {
+ LOGD("Can't get index by key[%s]", key);
+ return std::nullopt;
+ }
+
+ auto handle = ReadHandle(index_ptr->index, index_ptr->len, false);
+ if (!handle) {
+ LOGE("Failed to read app");
+ *result = ShmError::SYSTEM;
+ return std::nullopt;
+ }
+
+ *result = ShmError::NONE;
+ return handle;
+}
+
template<typename T>
std::optional<ShmLockGuard> ShmReader<T>::GetLock() {
std::shared_lock<std::shared_mutex> s(lock_);
if (index_mapper_.GetMemSize() == 0) {
LOGD("Empty Shm Data");
- ShmReader<T>::HandleCollection({}, {});
+ ShmReader<T>::HandleCollection({});
}
- std::map<std::string, T> handles;
- std::vector<uint8_t> data(
- handle_mapper_.GetPtr(),
- handle_mapper_.GetPtr() + handle_mapper_.GetMemSize());
- uint8_t* ptr = data.data();
+ std::vector<T> handles;
+ uint8_t* ptr = handle_mapper_.GetPtr();
auto index_ptr = (HandleMappingData*)index_mapper_.GetPtr();
auto end_ptr =
(HandleMappingData*)(index_mapper_.GetPtr() + index_mapper_.GetMemSize());
for (; index_ptr != end_ptr; index_ptr++) {
T handle(std::make_unique<tizen_base::Parcel>(
ptr + index_ptr->index, index_ptr->len, false, false));
- handles.emplace(handle.GetKey(), std::move(handle));
+ handles.emplace_back(std::move(handle));
}
- return ShmReader<T>::HandleCollection(std::move(data), std::move(handles));
+ return ShmReader<T>::HandleCollection(std::move(handles));
}
template<typename T>
std::optional<T> ShmReader<T>::ReadHandle(
- uint32_t index, uint32_t len) {
- return T(std::make_unique<tizen_base::Parcel>(
- handle_mapper_.GetPtr() + index, len, true));
+ uint32_t index, uint32_t len, bool copy) {
+ if (copy)
+ return T(std::make_unique<tizen_base::Parcel>(
+ handle_mapper_.GetPtr() + index, len, true));
+ else
+ return T(std::make_unique<tizen_base::Parcel>(
+ handle_mapper_.GetPtr() + index, len, false, false));
}
template<typename T>
public:
class HandleCollection {
public:
- HandleCollection(std::vector<uint8_t> data,
- std::map<std::string, T> handles)
- : data_(std::move(data)), handles_(std::move(handles)) {}
+ HandleCollection(std::vector<T> handles) : handles_(std::move(handles)) {}
- const std::map<std::string, T>& GetMap() {
+ const std::vector<T>& GetHandles() {
return handles_;
}
private:
- std::vector<uint8_t> data_;
- std::map<std::string, T> handles_;
+ std::vector<T> handles_;
};
ShmReader(std::string base_name) :
handle_mapper_(base_name_ + "_handle"),
index_mapper_(base_name_ + "_indexs") {}
std::optional<HandleCollection> GetHandles(ShmError* result);
+ std::optional<HandleCollection> GetHandlesWithoutLock(ShmError* result);
std::optional<T> GetHandle(const char* key, ShmError* result);
+ std::optional<T> GetHandleWithoutLock(const char* key, ShmError* result);
std::optional<ShmLockGuard> GetLock();
private:
std::optional<ShmLockGuard> Load(ShmError* result,
std::shared_lock<std::shared_mutex>& s);
std::optional<HandleCollection> ReadHandles(ShmError* result);
- std::optional<T> ReadHandle(uint32_t index, uint32_t len);
+ std::optional<T> ReadHandle(uint32_t index, uint32_t len, bool copy = true);
HandleMappingData* FindIndex(const char* key, ShmError* result);
protected: