From 758d7e7e262bc0a9a56c67eb437a7487e38470d5 Mon Sep 17 00:00:00 2001 From: Junghyun Yeon Date: Mon, 26 Jul 2021 19:13:44 +0900 Subject: [PATCH] Change codes for readability - Adjust include file order. - Add some blank lines for readability. - Apply early-return policy to reduce indentation depth. - Extract duplicated codes into function. - Remove unnecessary local variables. - Move parameter checking logics to another function. Change-Id: I45b86175e90e559c7eccf263f21841042bec1d96 Signed-off-by: Junghyun Yeon --- src/client/pkginfo_client.cc | 7 +- src/common/ready_checker.cc | 1 + src/common/request_type.cc | 1 + src/common/socket/client_socket.cc | 17 +- src/common/socket/server_socket.cc | 6 +- src/manager/pkginfo_manager.cc | 474 +++++++--------------------- src/server/cynara_checker/cynara_checker.cc | 28 +- src/server/database/abstract_db_handler.cc | 1 + src/server/database/appinfo_db_handler.cc | 2 +- src/server/database/create_db_handler.cc | 11 +- src/server/database/pkg_get_db_handler.cc | 1 + src/server/database/pkg_set_db_handler.cc | 1 + 12 files changed, 165 insertions(+), 385 deletions(-) diff --git a/src/client/pkginfo_client.cc b/src/client/pkginfo_client.cc index adb200d..37b7f03 100644 --- a/src/client/pkginfo_client.cc +++ b/src/client/pkginfo_client.cc @@ -56,6 +56,7 @@ bool PkgInfoClient::SendRequest() { is_offline_ = true; return RequestHandlerDirectAccess(); } + if (!socket_->Connect()) { LOG(ERROR) << "Failed to connect client socket, try to direct access"; is_offline_ = true; @@ -94,6 +95,7 @@ PkgInfoClient::GetResultParcel() { LOG(ERROR) << "Socket is not ready"; return nullptr; } + int len = 0; if (socket_->ReceiveData(&len, sizeof(len)) != 0 || len <= 0) { LOG(ERROR) << "Fail to receive data"; @@ -134,8 +136,9 @@ bool PkgInfoClient::RequestHandlerDirectAccess() { << ", : " << dlerror(); return false; } - dl_func = reinterpret_cast( - dlsym(handle, DIRECT_ACCESS_FUNC)); + dl_func = reinterpret_cast( + dlsym(handle, DIRECT_ACCESS_FUNC)); if (dl_func == nullptr) { LOG(ERROR) << "cannot find " << DIRECT_ACCESS_FUNC << " symbol in " << LIBPKGMGR_INFO; diff --git a/src/common/ready_checker.cc b/src/common/ready_checker.cc index b7da7a5..3dae431 100644 --- a/src/common/ready_checker.cc +++ b/src/common/ready_checker.cc @@ -35,6 +35,7 @@ gboolean ReadyChecker::OnReceiveEvent(GIOChannel* channel, GIOCondition cond, struct inotify_event* event; auto ready_checker = reinterpret_cast(user_data); int fd = g_io_channel_unix_get_fd(channel); + while ((len = read(fd, buf, sizeof(buf))) > 0) { for (ptr = buf; ptr < buf + len; ptr += sizeof(struct inotify_event) + event->len) { diff --git a/src/common/request_type.cc b/src/common/request_type.cc index eabf601..ffc06c6 100644 --- a/src/common/request_type.cc +++ b/src/common/request_type.cc @@ -31,6 +31,7 @@ const char* ReqTypeToString(ReqType type) { "CREATE_DB", "ERROR_REQ_TYPE" }; + if (type <= ReqType::REQ_TYPE_NONE || type >= ReqType::MAX) return convertArray[ReqType::MAX]; diff --git a/src/common/socket/client_socket.cc b/src/common/socket/client_socket.cc index 5c1f218..a8c75f7 100644 --- a/src/common/socket/client_socket.cc +++ b/src/common/socket/client_socket.cc @@ -14,6 +14,8 @@ * limitations under the License. */ +#include "client_socket.hh" + #include #include #include @@ -21,7 +23,6 @@ #include #include -#include "client_socket.hh" #include "utils/logging.hh" #include "pkgmgrinfo_debug.h" @@ -45,9 +46,7 @@ void ClientSocket::SetTimeout(int timeout_msec) { .tv_sec = static_cast(timeout_msec / 1000), .tv_usec = static_cast((timeout_msec % 1000) * 1000)}; - int ret = setsockopt(fd_, SOL_SOCKET, SO_RCVTIMEO, &timeout, - sizeof(timeout)); - if (ret < 0) + if (setsockopt(fd_, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0) LOG(ERROR) << "setsockopt() is failed. fd: " << fd_ << ", errno: " << errno; } @@ -58,19 +57,19 @@ bool ClientSocket::Connect() { SetTimeout(5000); - int ret = -1; int retry_cnt = 3; do { - ret = TryConnection(); + int ret = TryConnection(); if (ret == 0) { break; } else if (ret < -1) { LOG(ERROR) << "Maybe peer not launched or peer dead. path: " << GetPath() << ", fd: " << GetFd(); - if (getuid() == 0) { - // If requester is root, don't wait + + // If requester is root, don't wait + if (getuid() == 0) return false; - } + usleep(100 * 1000); --retry_cnt; } else if (ret < 0) { diff --git a/src/common/socket/server_socket.cc b/src/common/socket/server_socket.cc index 9763fa2..0d72d53 100644 --- a/src/common/socket/server_socket.cc +++ b/src/common/socket/server_socket.cc @@ -50,7 +50,7 @@ ServerSocket::ServerSocket(std::string path) : AbstractSocket(std::move(path)) { addr_.sun_family = AF_UNIX; snprintf(addr_.sun_path, sizeof(addr_.sun_path), "%s", path_.c_str()); if (access(path_.c_str(), F_OK) == 0) { - LOG(WARNING) << "socket is already used : " << path_; + LOG(WARNING) << "socket is already in use : " << path_; unlink(path_.c_str()); } @@ -58,6 +58,7 @@ ServerSocket::ServerSocket(std::string path) : AbstractSocket(std::move(path)) { LOG(ERROR) << "Fail to Bind server socket. errno: " << errno; return; } + if (Listen() < 0) { LOG(ERROR) << "Fail to Listen server socket. errno: " << errno; return; @@ -76,8 +77,7 @@ int ServerSocket::Bind() { int ServerSocket::Listen() { return listen(fd_, 128); } int ServerSocket::Accept() { - int client_fd = accept(fd_, nullptr, nullptr); - return client_fd; + return accept(fd_, nullptr, nullptr); } } // namespace socket diff --git a/src/manager/pkginfo_manager.cc b/src/manager/pkginfo_manager.cc index 72b37fa..c0d0fcc 100644 --- a/src/manager/pkginfo_manager.cc +++ b/src/manager/pkginfo_manager.cc @@ -58,6 +58,31 @@ namespace pcp = pkgmgr_common::parcel; +namespace { + +int ValidateParcelable( + std::shared_ptr parcel, + pcp::ParcelableType parcel_type) { + if (parcel == nullptr) { + LOG(ERROR) << "Failed to get return parcelable"; + return PMINFO_R_ERROR; + } + + if (parcel->GetType() != parcel_type) { + LOG(ERROR) << "Invalid parcelable Type"; + return PMINFO_R_ERROR; + } + + if (parcel->GetRequestResult() != PMINFO_R_OK) { + LOG(ERROR) << "Request fail"; + return parcel->GetRequestResult(); + } + + return PMINFO_R_OK; +} + +} // namespace + extern "C" EXPORT_API int _pkginfo_get_packages(uid_t uid, pkgmgrinfo_filter_x* filter, int flag, GHashTable* packages) { std::shared_ptr parcelable( @@ -113,20 +138,9 @@ extern "C" EXPORT_API int _pkginfo_get_depends_on(uid_t uid, return PMINFO_R_ERROR; auto ptr = client.GetResultParcel(); - if (ptr == nullptr) { - LOG(ERROR) << "Fail to get return parcelable"; - return PMINFO_R_ERROR; - } - - if (ptr->GetRequestResult() != PMINFO_R_OK) { - LOG(ERROR) << "Request fail"; - return ptr->GetRequestResult(); - } - - if (ptr->GetType() != pcp::ParcelableType::DepInfo) { - LOG(ERROR) << "Invalid parcelable type"; - return PMINFO_R_ERROR; - } + int ret = ValidateParcelable(ptr, pcp::ParcelableType::DepInfo); + if (ret != PMINFO_R_OK) + return ret; std::shared_ptr return_parcel( std::static_pointer_cast(ptr)); @@ -191,38 +205,26 @@ extern "C" EXPORT_API char* _appinfo_get_localed_label( pkgmgr_common::ReqType::QUERY); if (!client.SendRequest()) return nullptr; - auto ptr = client.GetResultParcel(); - if (ptr == nullptr) { - LOG(ERROR) << "Fail to get return parcelable"; - return nullptr; - } - - if (ptr->GetRequestResult() != PMINFO_R_OK) { - LOG(ERROR) << "Request fail"; - return nullptr; - } - if (ptr->GetType() != pcp::ParcelableType::Result) { - LOG(ERROR) << "Invalid parcelable type"; + auto ptr = client.GetResultParcel(); + if (ValidateParcelable(ptr, pcp::ParcelableType::Result) != PMINFO_R_OK) return nullptr; - } std::shared_ptr return_parcel( std::static_pointer_cast(ptr)); - - // result_list is vector of string vector char* label = nullptr; auto& result_list = return_parcel->GetResult(); + for (auto& result : result_list) { - // result is string vector - // it only has one string or not. if (!result.front() || (*result.front()).empty()) continue; + label = strdup((*result.front()).c_str()); if (label == nullptr) { LOG(ERROR) << "Out of memory"; return nullptr; } + break; } @@ -244,20 +246,9 @@ extern "C" EXPORT_API int _appinfo_get_datacontrol_info( return PMINFO_R_ERROR; auto ptr = client.GetResultParcel(); - if (ptr == nullptr) { - LOG(ERROR) << "Fail to get return parcelable"; - return PMINFO_R_ERROR; - } - - if (ptr->GetRequestResult() != PMINFO_R_OK) { - LOG(ERROR) << "Request fail"; - return ptr->GetRequestResult(); - } - - if (ptr->GetType() != pcp::ParcelableType::Result) { - LOG(ERROR) << "Invalid parcelable type"; - return PMINFO_R_ERROR; - } + int ret = ValidateParcelable(ptr, pcp::ParcelableType::Result); + if (ret != PMINFO_R_OK) + return ret; std::shared_ptr return_parcel( std::static_pointer_cast(ptr)); @@ -265,6 +256,7 @@ extern "C" EXPORT_API int _appinfo_get_datacontrol_info( auto& result_list = return_parcel->GetResult(); if (result_list.size() == 0) return PMINFO_R_ENOENT; + for (auto it = result_list.rbegin(); it != result_list.rend(); it++) { const auto& result = *it; if (result.size() != 2 || !result.front() || !result.back() || @@ -276,12 +268,14 @@ extern "C" EXPORT_API int _appinfo_get_datacontrol_info( LOG(ERROR) << "Out of memory"; return PMINFO_R_ERROR; } + char* tmp_access = strdup((*result.back()).c_str()); if (tmp_access == nullptr) { LOG(ERROR) << "Out of memory"; free(tmp_appid); return PMINFO_R_ERROR; } + *appid = tmp_appid; *access = tmp_access; @@ -305,37 +299,28 @@ extern "C" EXPORT_API int _appinfo_get_datacontrol_appid( return PMINFO_R_ERROR; auto ptr = client.GetResultParcel(); - if (ptr == nullptr) { - LOG(ERROR) << "Fail to get return parcelable"; - return PMINFO_R_ERROR; - } - - if (ptr->GetRequestResult() != PMINFO_R_OK) { - LOG(ERROR) << "Request fail"; - return ptr->GetRequestResult(); - } - - if (ptr->GetType() != pcp::ParcelableType::Result) { - LOG(ERROR) << "Invalid parcelable type"; - return PMINFO_R_ERROR; - } + int ret = ValidateParcelable(ptr, pcp::ParcelableType::Result); + if (ret != PMINFO_R_OK) + return ret; std::shared_ptr return_parcel( std::static_pointer_cast(ptr)); - // result_list is vector of string vector auto& result_list = return_parcel->GetResult(); if (result_list.size() == 0) return PMINFO_R_ENOENT; + for (auto it = result_list.rbegin(); it != result_list.rend(); it++) { const auto& result = *it; if (result.size() != 1 || !result.front() || (*result.front()).empty()) continue; + *appid = strdup((*result.front()).c_str()); if (*appid == nullptr) { LOG(ERROR) << "Out of memory"; return PMINFO_R_ERROR; } + return PMINFO_R_OK; } @@ -358,28 +343,17 @@ extern "C" EXPORT_API int _appinfo_get_datacontrol_trusted_info( return PMINFO_R_ERROR; auto ptr = client.GetResultParcel(); - if (ptr == nullptr) { - LOG(ERROR) << "Fail to get return parcelable"; - return PMINFO_R_ERROR; - } - - if (ptr->GetRequestResult() != PMINFO_R_OK) { - LOG(ERROR) << "Request fail"; - return ptr->GetRequestResult(); - } - - if (ptr->GetType() != pcp::ParcelableType::Result) { - LOG(ERROR) << "Invalid parcelable type"; - return PMINFO_R_ERROR; - } + int ret = ValidateParcelable(ptr, pcp::ParcelableType::Result); + if (ret != PMINFO_R_OK) + return ret; std::shared_ptr return_parcel( std::static_pointer_cast(ptr)); - // result_list is vector of string vector auto& result_list = return_parcel->GetResult(); if (result_list.size() == 0) return PMINFO_R_ENOENT; + for (auto it = result_list.rbegin(); it != result_list.rend(); it++) { const auto& result = *it; if (result.size() != 2 || !result.front() || !result.back() || @@ -391,14 +365,17 @@ extern "C" EXPORT_API int _appinfo_get_datacontrol_trusted_info( LOG(ERROR) << "Out of memory"; return PMINFO_R_ERROR; } + char* tmp_trusted = strdup((*result.back()).c_str()); if (tmp_trusted == nullptr) { LOG(ERROR) << "Out of memory"; free(tmp_appid); return PMINFO_R_ERROR; } + *appid = tmp_appid; *trusted = tmp_trusted; + return PMINFO_R_OK; } @@ -420,25 +397,13 @@ extern "C" EXPORT_API int _appinfo_get_datacontrol_privileges( return PMINFO_R_ERROR; auto ptr = client.GetResultParcel(); - if (ptr == nullptr) { - LOG(ERROR) << "Fail to get return parcelable"; - return PMINFO_R_ERROR; - } - - if (ptr->GetRequestResult() != PMINFO_R_OK) { - LOG(ERROR) << "Request fail"; - return ptr->GetRequestResult(); - } - - if (ptr->GetType() != pcp::ParcelableType::Result) { - LOG(ERROR) << "Invalid parcelable type"; - return PMINFO_R_ERROR; - } + int ret = ValidateParcelable(ptr, pcp::ParcelableType::Result); + if (ret != PMINFO_R_OK) + return ret; std::shared_ptr return_parcel( std::static_pointer_cast(ptr)); - // result_list is vector of string vector auto& result_list = return_parcel->GetResult(); if (result_list.size() == 0) return PMINFO_R_ENOENT; @@ -446,6 +411,7 @@ extern "C" EXPORT_API int _appinfo_get_datacontrol_privileges( for (auto& result : result_list) { if (result.size() != 1 || !result.front() || (*result.front()).empty()) return PMINFO_R_ERROR; + char* privilege = strdup((*result.front()).c_str()); if (privilege == nullptr) { LOG(ERROR) << "Out of memory"; @@ -471,20 +437,9 @@ extern "C" EXPORT_API int _appinfo_get_appcontrol_privileges( return PMINFO_R_ERROR; auto ptr = client.GetResultParcel(); - if (ptr == nullptr) { - LOG(ERROR) << "Fail to get return parcelable"; - return PMINFO_R_ERROR; - } - - if (ptr->GetRequestResult() != PMINFO_R_OK) { - LOG(ERROR) << "Request fail"; - return ptr->GetRequestResult(); - } - - if (ptr->GetType() != pcp::ParcelableType::Result) { - LOG(ERROR) << "Invalid parcelable type"; - return PMINFO_R_ERROR; - } + int ret = ValidateParcelable(ptr, pcp::ParcelableType::Result); + if (ret != PMINFO_R_OK) + return ret; std::shared_ptr return_parcel( std::static_pointer_cast(ptr)); @@ -498,6 +453,7 @@ extern "C" EXPORT_API int _appinfo_get_appcontrol_privileges( if (result.size() != 2 || !result.front() || !result.back() || (*result.front()).empty() || (*result.back()).empty()) return PMINFO_R_ERROR; + std::stringstream ss((*result.front())); std::string token; while (std::getline(ss, token, '|')) { @@ -535,20 +491,9 @@ extern "C" EXPORT_API int _plugininfo_get_appids( return PMINFO_R_ERROR; auto ptr = client.GetResultParcel(); - if (ptr == nullptr) { - LOG(ERROR) << "Fail to get return parcelable"; - return PMINFO_R_ERROR; - } - - if (ptr->GetRequestResult() != PMINFO_R_OK) { - LOG(ERROR) << "Request fail"; - return ptr->GetRequestResult(); - } - - if (ptr->GetType() != pcp::ParcelableType::Result) { - LOG(ERROR) << "Invalid parcelable type"; - return PMINFO_R_ERROR; - } + int ret = ValidateParcelable(ptr, pcp::ParcelableType::Result); + if (ret != PMINFO_R_OK) + return ret; std::shared_ptr return_parcel( std::static_pointer_cast(ptr)); @@ -557,7 +502,7 @@ extern "C" EXPORT_API int _plugininfo_get_appids( LOG(ERROR) << "Invalid result"; return PMINFO_R_ERROR; } - // result_list is vector of string vector + auto& result_list = return_parcel->GetResult(); if (result_list.size() == 0) return PMINFO_R_ENOENT; @@ -568,6 +513,7 @@ extern "C" EXPORT_API int _plugininfo_get_appids( g_list_free_full(*list, free); return PMINFO_R_ERROR; } + *list = g_list_append(*list, strdup((*result[0]).c_str())); } @@ -587,6 +533,7 @@ static int __convert_update_type(const char* type, *convert_type = PMINFO_UPDATEINFO_OPTIONAL; else return -1; + return 0; } @@ -597,8 +544,10 @@ static void __free_update_info(gpointer data) { if (update_info->pkgid) free(reinterpret_cast(update_info->pkgid)); + if (update_info->version) free(reinterpret_cast(update_info->version)); + free(reinterpret_cast(update_info)); } @@ -627,20 +576,9 @@ extern "C" EXPORT_API int _get_pkg_updateinfo(const char* pkgid, return PMINFO_R_ERROR; auto ptr = client.GetResultParcel(); - if (ptr == nullptr) { - LOG(ERROR) << "Fail to get return parcelable"; - return PMINFO_R_ERROR; - } - - if (ptr->GetRequestResult() != PMINFO_R_OK) { - LOG(ERROR) << "Request fail"; - return ptr->GetRequestResult(); - } - - if (ptr->GetType() != pcp::ParcelableType::Result) { - LOG(ERROR) << "Invalid parcelable type"; - return PMINFO_R_ERROR; - } + ret = ValidateParcelable(ptr, pcp::ParcelableType::Result); + if (ret != PMINFO_R_OK) + return ret; std::shared_ptr return_parcel( std::static_pointer_cast(ptr)); @@ -661,6 +599,7 @@ extern "C" EXPORT_API int _get_pkg_updateinfo(const char* pkgid, g_slist_free_full(tmp_list, __free_update_info); return PMINFO_R_ERROR; } + updateinfo_x* update_info = reinterpret_cast( calloc(1, sizeof(updateinfo_x))); if (update_info == nullptr) { @@ -668,15 +607,18 @@ extern "C" EXPORT_API int _get_pkg_updateinfo(const char* pkgid, g_slist_free_full(tmp_list, __free_update_info); return PMINFO_R_ERROR; } + update_info->pkgid = strdup((*result[0]).c_str()); update_info->version = strdup((*result[1]).c_str()); pkgmgrinfo_updateinfo_update_type convert_type; + ret = __convert_update_type((*result[2]).c_str(), &convert_type); if (ret != 0) { __free_update_info(update_info); g_slist_free_full(tmp_list, __free_update_info); return PMINFO_R_ERROR; } + update_info->type = static_cast(convert_type); tmp_list = g_slist_append(tmp_list, update_info); } @@ -719,23 +661,8 @@ extern "C" EXPORT_API int _pkginfo_set_usr_installed_storage(const char* pkgid, if (!client.SendRequest()) return PMINFO_R_ERROR; - auto ptr = client.GetResultParcel(); - if (ptr == nullptr) { - LOG(ERROR) << "Fail to get return parcelable"; - return PMINFO_R_ERROR; - } - - if (ptr->GetRequestResult() != PMINFO_R_OK) { - LOG(ERROR) << "Request fail"; - return ptr->GetRequestResult(); - } - - if (ptr->GetType() != pcp::ParcelableType::Result) { - LOG(ERROR) << "Invalid parcelable type"; - return PMINFO_R_ERROR; - } - - return PMINFO_R_OK; + return ValidateParcelable( + client.GetResultParcel(), pcp::ParcelableType::Result); } extern "C" EXPORT_API int _certinfo_compare_pkg_certinfo(const char* l_pkgid, @@ -751,20 +678,9 @@ extern "C" EXPORT_API int _certinfo_compare_pkg_certinfo(const char* l_pkgid, return PMINFO_R_ERROR; auto ptr = client.GetResultParcel(); - if (ptr == nullptr) { - LOG(ERROR) << "Fail to get return parcelable"; - return PMINFO_R_ERROR; - } - - if (ptr->GetRequestResult() != PMINFO_R_OK) { - LOG(ERROR) << "Request fail"; - return ptr->GetRequestResult(); - } - - if (ptr->GetType() != pcp::ParcelableType::Result) { - LOG(ERROR) << "Invalid parcelable type"; - return PMINFO_R_ERROR; - } + int ret = ValidateParcelable(ptr, pcp::ParcelableType::Result); + if (ret != PMINFO_R_OK) + return ret; std::shared_ptr return_parcel( std::static_pointer_cast( @@ -809,20 +725,9 @@ extern "C" EXPORT_API int _certinfo_compare_app_certinfo(uid_t uid, return PMINFO_R_ERROR; auto ptr = client.GetResultParcel(); - if (ptr == nullptr) { - LOG(ERROR) << "Fail to get return parcelable"; - return PMINFO_R_ERROR; - } - - if (ptr->GetRequestResult() != PMINFO_R_OK) { - LOG(ERROR) << "Request fail"; - return ptr->GetRequestResult(); - } - - if (ptr->GetType() != pcp::ParcelableType::Result) { - LOG(ERROR) << "Invalid parcelable type"; - return PMINFO_R_ERROR; - } + int ret = ValidateParcelable(ptr, pcp::ParcelableType::Result); + if (ret != PMINFO_R_OK) + return ret; std::shared_ptr return_parcel( std::static_pointer_cast(ptr)); @@ -866,24 +771,10 @@ extern "C" EXPORT_API int _parser_execute_write_query( pkgmgr_client::PkgInfoClient client(parcelable, uid, pkgmgr_common::ReqType::QUERY); if (!client.SendRequest()) - return -1; - - auto ptr = client.GetResultParcel(); - if (ptr == nullptr) { - LOG(ERROR) << "Fail to get return parcelable"; - return -1; - } - - if (ptr->GetRequestResult() != PMINFO_R_OK) { - LOG(ERROR) << "Request fail"; - return -1; - } + return PMINFO_R_ERROR; - if (ptr->GetType() != pcp::ParcelableType::Result) { - LOG(ERROR) << "Invalid parcelable type"; - return -1; - } - return 0; + return ValidateParcelable( + client.GetResultParcel(), pcp::ParcelableType::Result); } extern "C" EXPORT_API int _parser_execute_write_queries( @@ -906,115 +797,58 @@ extern "C" EXPORT_API int _parser_execute_write_queries( pkgmgr_client::PkgInfoClient client(parcelable, uid, pkgmgr_common::ReqType::QUERY); if (!client.SendRequest()) - return -1; - - auto ptr = client.GetResultParcel(); - if (ptr == nullptr) { - LOG(ERROR) << "Fail to get return parcelable"; - return -1; - } - - if (ptr->GetRequestResult() != PMINFO_R_OK) { - LOG(ERROR) << "Request fail"; - return -1; - } - - if (ptr->GetType() != pcp::ParcelableType::Result) { - LOG(ERROR) << "Invalid parcelable type"; - return -1; - } + return PMINFO_R_ERROR; - return 0; + return ValidateParcelable( + client.GetResultParcel(), pcp::ParcelableType::Result); } extern "C" EXPORT_API int _parser_insert_manifest_info( manifest_x* mfx, uid_t uid) { auto parcelable = std::make_shared(uid, - std::vector{mfx}, pkgmgr_common::PkgWriteType::Insert, false); + std::vector{mfx}, + pkgmgr_common::PkgWriteType::Insert, false); pkgmgr_client::PkgInfoClient client(parcelable, uid, pkgmgr_common::ReqType::SET_PKG_INFO); if (!client.SendRequest()) - return -1; - - auto ptr = client.GetResultParcel(); - if (ptr == nullptr) { - LOG(ERROR) << "Fail to get return parcelable"; - return -1; - } - - if (ptr->GetRequestResult() != PMINFO_R_OK) { - LOG(ERROR) << "Request fail"; - return -1; - } - - if (ptr->GetType() != pcp::ParcelableType::Result) { - LOG(ERROR) << "Invalid parcelable type"; - return -1; - } + return PMINFO_R_ERROR; - return 0; + return ValidateParcelable( + client.GetResultParcel(), pcp::ParcelableType::Result); } extern "C" EXPORT_API int _parser_update_manifest_info( manifest_x* mfx, uid_t uid) { auto parcelable = std::make_shared(uid, - std::vector{mfx}, pkgmgr_common::PkgWriteType::Update, false); + std::vector{mfx}, + pkgmgr_common::PkgWriteType::Update, false); pkgmgr_client::PkgInfoClient client(parcelable, uid, pkgmgr_common::ReqType::SET_PKG_INFO); if (!client.SendRequest()) - return -1; - - auto ptr = client.GetResultParcel(); - if (ptr == nullptr) { - LOG(ERROR) << "Fail to get return parcelable"; - return -1; - } - - if (ptr->GetRequestResult() != PMINFO_R_OK) { - LOG(ERROR) << "Request fail"; - return -1; - } - - if (ptr->GetType() != pcp::ParcelableType::Result) { - LOG(ERROR) << "Invalid parcelable type"; - return -1; - } + return PMINFO_R_ERROR; - return 0; + return ValidateParcelable( + client.GetResultParcel(), pcp::ParcelableType::Result); } extern "C" EXPORT_API int _parser_delete_manifest_info( manifest_x* mfx, uid_t uid) { auto parcelable = std::make_shared(uid, - std::vector{mfx}, pkgmgr_common::PkgWriteType::Delete, false); + std::vector{mfx}, + pkgmgr_common::PkgWriteType::Delete, false); pkgmgr_client::PkgInfoClient client(parcelable, uid, pkgmgr_common::ReqType::SET_PKG_INFO); if (!client.SendRequest()) - return -1; - - auto ptr = client.GetResultParcel(); - if (ptr == nullptr) { - LOG(ERROR) << "Fail to get return parcelable"; - return -1; - } - - if (ptr->GetRequestResult() != PMINFO_R_OK) { - LOG(ERROR) << "Request fail"; - return -1; - } - - if (ptr->GetType() != pcp::ParcelableType::Result) { - LOG(ERROR) << "Invalid parcelable type"; - return -1; - } + return PMINFO_R_ERROR; - return 0; + return ValidateParcelable( + client.GetResultParcel(), pcp::ParcelableType::Result); } extern "C" EXPORT_API int _pkginfo_insert_certinfo(const char* pkgid, @@ -1027,23 +861,8 @@ extern "C" EXPORT_API int _pkginfo_insert_certinfo(const char* pkgid, if (!client.SendRequest()) return PMINFO_R_ERROR; - auto ptr = client.GetResultParcel(); - if (ptr == nullptr) { - LOG(ERROR) << "Fail to get return parcelable"; - return PMINFO_R_ERROR; - } - - if (ptr->GetRequestResult() != PMINFO_R_OK) { - LOG(ERROR) << "Request fail"; - return ptr->GetRequestResult(); - } - - if (ptr->GetType() != pcp::ParcelableType::Result) { - LOG(ERROR) << "Invalid parcelable type"; - return PMINFO_R_ERROR; - } - - return PMINFO_R_OK; + return ValidateParcelable( + client.GetResultParcel(), pcp::ParcelableType::Result); } extern "C" EXPORT_API int _pkginfo_get_certinfo(const char* pkgid, @@ -1058,20 +877,9 @@ extern "C" EXPORT_API int _pkginfo_get_certinfo(const char* pkgid, return PMINFO_R_ERROR; auto ptr = client.GetResultParcel(); - if (ptr == nullptr) { - LOG(ERROR) << "Fail to get return parcelable"; - return PMINFO_R_ERROR; - } - - if (ptr->GetRequestResult() != PMINFO_R_OK) { - LOG(ERROR) << "Request fail"; - return ptr->GetRequestResult(); - } - - if (ptr->GetType() != pcp::ParcelableType::CertInfo) { - LOG(ERROR) << "Invalid parcelable type"; - return PMINFO_R_ERROR; - } + int ret = ValidateParcelable(ptr, pcp::ParcelableType::CertInfo); + if (ret != PMINFO_R_OK) + return ret; std::shared_ptr return_parcel( std::static_pointer_cast(ptr)); @@ -1089,6 +897,7 @@ extern "C" EXPORT_API int _pkginfo_get_certinfo(const char* pkgid, cert->cert_info[i] = certinfo->cert_info[i]; certinfo->cert_info[i] = nullptr; } + for (int i = 0; i < MAX_CERT_TYPE; i++) cert->cert_id[i] = certinfo->cert_id[i]; @@ -1109,23 +918,8 @@ extern "C" EXPORT_API int _pkginfo_delete_certinfo(const char* pkgid) { if (!client.SendRequest()) return PMINFO_R_ERROR; - auto ptr = client.GetResultParcel(); - if (ptr == nullptr) { - LOG(ERROR) << "Fail to get return parcelable"; - return PMINFO_R_ERROR; - } - - if (ptr->GetRequestResult() != PMINFO_R_OK) { - LOG(ERROR) << "Request fail"; - return PMINFO_R_ERROR; - } - - if (ptr->GetType() != pcp::ParcelableType::Result) { - LOG(ERROR) << "Invalid parcelable type"; - return PMINFO_R_ERROR; - } - - return PMINFO_R_OK; + return ValidateParcelable( + client.GetResultParcel(), pcp::ParcelableType::Result); } extern "C" EXPORT_API int _parser_clear_cache_memory_db(uid_t uid) { @@ -1138,23 +932,8 @@ extern "C" EXPORT_API int _parser_clear_cache_memory_db(uid_t uid) { if (!client.SendRequest()) return PMINFO_R_ERROR; - auto ptr = client.GetResultParcel(); - if (ptr == nullptr) { - LOG(ERROR) << "Fail to get return parcelable"; - return PMINFO_R_ERROR; - } - - if (ptr->GetRequestResult() != PMINFO_R_OK) { - LOG(ERROR) << "Request fail"; - return PMINFO_R_ERROR; - } - - if (ptr->GetType() != pcp::ParcelableType::Result) { - LOG(ERROR) << "Invalid parcelable type"; - return PMINFO_R_ERROR; - } - - return PMINFO_R_OK; + return ValidateParcelable( + client.GetResultParcel(), pcp::ParcelableType::CertInfo); } static int __create_and_initialize_db(uid_t uid) { @@ -1165,32 +944,17 @@ static int __create_and_initialize_db(uid_t uid) { pkgmgr_common::ReqType::CREATE_DB); if (!client.SendRequest()) - return -1; - - auto ptr = client.GetResultParcel(); - if (ptr == nullptr) { - LOG(ERROR) << "Fail to get return parcelable"; - return -1; - } - - if (ptr->GetRequestResult() != PMINFO_R_OK) { - LOG(ERROR) << "Request fail"; - return -1; - } - - if (ptr->GetType() != pcp::ParcelableType::Result) { - LOG(ERROR) << "Invalid parcelable type"; - return -1; - } + return PMINFO_R_ERROR; - return 0; + return ValidateParcelable( + client.GetResultParcel(), pcp::ParcelableType::CertInfo); } extern "C" EXPORT_API int _parser_create_and_initialize_db(uid_t uid) { if (__create_and_initialize_db(uid) < 0) { LOG(ERROR) << "Fail to initialize db"; - return -1; + return PMINFO_R_ERROR; } - return 0; + return PMINFO_R_OK; } diff --git a/src/server/cynara_checker/cynara_checker.cc b/src/server/cynara_checker/cynara_checker.cc index 660aea8..7fdc588 100644 --- a/src/server/cynara_checker/cynara_checker.cc +++ b/src/server/cynara_checker/cynara_checker.cc @@ -53,18 +53,22 @@ void CynaraChecker::ReplyCb(cynara_check_id id, cynara_async_call_cause cause, auto runner = static_cast(data); auto& inst = CynaraChecker::GetInst(); switch (cause) { - case CYNARA_CALL_CAUSE_ANSWER: - if (resp == CYNARA_API_ACCESS_ALLOWED) { - auto it = inst.cynara_id_map_.find(id); - if (it == inst.cynara_id_map_.end()) { - LOG(ERROR) << "Invalid request"; - break; - } - LOG(DEBUG) << "Allowed request"; - runner->QueueRequest(it->second); - inst.cynara_id_map_.erase(it); + case CYNARA_CALL_CAUSE_ANSWER: { + if (resp != CYNARA_API_ACCESS_ALLOWED) + break; + + auto it = inst.cynara_id_map_.find(id); + if (it == inst.cynara_id_map_.end()) { + LOG(ERROR) << "Invalid request"; + break; } + + LOG(DEBUG) << "Allowed request"; + runner->QueueRequest(it->second); + inst.cynara_id_map_.erase(it); + break; + } case CYNARA_CALL_CAUSE_CANCEL: case CYNARA_CALL_CAUSE_FINISH: case CYNARA_CALL_CAUSE_SERVICE_NOT_AVAILABLE: @@ -97,6 +101,7 @@ void CynaraChecker::StatusCb(int old_fd, int new_fd, inst.cynara_sid_ = 0; } } + if (new_fd != -1) { auto cond = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL; if (status == CYNARA_STATUS_FOR_RW) @@ -124,6 +129,7 @@ void CynaraChecker::CheckPrivilege(Runner* runner, return; } } + char* smack_label; ret = cynara_creds_socket_get_client(req->GetFd(), CLIENT_METHOD_SMACK, &smack_label); @@ -131,6 +137,7 @@ void CynaraChecker::CheckPrivilege(Runner* runner, LOG(ERROR) << "Failed to get smack label"; return; } + std::unique_ptr lblPtr(smack_label, std::free); char* session = cynara_session_from_pid(req->GetSenderPID()); if (session == nullptr) { @@ -138,6 +145,7 @@ void CynaraChecker::CheckPrivilege(Runner* runner, << req->GetSenderPID(); return; } + std::unique_ptr sessPtr(session, std::free); cynara_check_id id; bool check = false; diff --git a/src/server/database/abstract_db_handler.cc b/src/server/database/abstract_db_handler.cc index de4f11e..3bdb75b 100644 --- a/src/server/database/abstract_db_handler.cc +++ b/src/server/database/abstract_db_handler.cc @@ -181,6 +181,7 @@ bool AbstractDBHandler::Connect() { LOG(ERROR) << "Invalid parameter"; return false; } + auto dbpath_list = GetDBPath(); sqlite3* db; for (auto& dbpath : dbpath_list) { diff --git a/src/server/database/appinfo_db_handler.cc b/src/server/database/appinfo_db_handler.cc index fafb2f1..9434159 100644 --- a/src/server/database/appinfo_db_handler.cc +++ b/src/server/database/appinfo_db_handler.cc @@ -16,8 +16,8 @@ #include "appinfo_db_handler.hh" -#include #include +#include #include "utils/logging.hh" diff --git a/src/server/database/create_db_handler.cc b/src/server/database/create_db_handler.cc index da918fe..8ad7f82 100644 --- a/src/server/database/create_db_handler.cc +++ b/src/server/database/create_db_handler.cc @@ -51,11 +51,9 @@ int CreateDBHandler::Execute() { return PMINFO_R_ERROR; } - if (GetUID() == GLOBAL_USER || GetUID() == OWNER_ROOT) { - if (CreateCertDB() < 0) { - LOG(ERROR) << "Failed to create cert db"; - return PMINFO_R_ERROR; - } + if (CreateCertDB() < 0) { + LOG(ERROR) << "Failed to create cert db"; + return PMINFO_R_ERROR; } return PMINFO_R_OK; @@ -76,6 +74,9 @@ int CreateDBHandler::CreateParserDB() { } int CreateDBHandler::CreateCertDB() { + if (GetUID() != GLOBAL_USER && GetUID() != OWNER_ROOT) + return PMINFO_R_OK; + SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_CERTDB); ClearDBHandle(); diff --git a/src/server/database/pkg_get_db_handler.cc b/src/server/database/pkg_get_db_handler.cc index dd8518b..c151983 100644 --- a/src/server/database/pkg_get_db_handler.cc +++ b/src/server/database/pkg_get_db_handler.cc @@ -86,6 +86,7 @@ int PkgGetDBHandler::Execute() { g_hash_table_foreach_steal(list, _move_func, &handle_list_); g_hash_table_destroy(list); + return ret; } diff --git a/src/server/database/pkg_set_db_handler.cc b/src/server/database/pkg_set_db_handler.cc index 170a0f7..73cb138 100644 --- a/src/server/database/pkg_set_db_handler.cc +++ b/src/server/database/pkg_set_db_handler.cc @@ -60,6 +60,7 @@ int PkgSetDBHandler::Execute() { std::vector> conn_list = GetConnection(); sqlite3* conn = conn_list.front().first; int ret = 0; + if (write_type_ == pkgmgr_common::PkgWriteType::Insert) ret = pkgmgr_parser_insert_pkg_info(conn, package_, uid_); else if (write_type_ == pkgmgr_common::PkgWriteType::Update) -- 2.7.4