From: Krzysztof Jackiewicz Date: Tue, 5 Nov 2013 14:12:52 +0000 (+0100) Subject: API function try/catch decoration X-Git-Tag: submit/tizen/20140307.131547~44 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=be560b0c52b72e369c2e871b8aab9d0bbc73f72b;p=platform%2Fcore%2Fsecurity%2Fsecurity-server.git API function try/catch decoration [Issue#] SSDWSSP-636 [Feature/Bug] N/A [Problem] Lot of duplicated try/catch code in every API function [Cause] N/A [Solution] Use lambda expressions and a decorator function. [Verification] Successfull compilation and passing security-server-tests-* Conflicts: src/server2/client/client-password.cpp src/server2/client/client-socket-privilege.cpp src/server2/client/client-common.cpp src/server2/client/client-common.h Change-Id: I0097d8e02e28b8f50d88f74bed900e5761e8b4a6 --- diff --git a/src/server2/client/client-app-permissions.cpp b/src/server2/client/client-app-permissions.cpp index 35bda12..05b07d3 100644 --- a/src/server2/client/client-app-permissions.cpp +++ b/src/server2/client/client-app-permissions.cpp @@ -47,7 +47,7 @@ int security_server_app_enable_permissions(const char *app_id, app_type_t app_ty LogDebug("security_server_app_enable_permissions() called"); - try { + return try_catch([&] { if ((NULL == app_id) || (strlen(app_id) == 0)) { LogDebug("App_id is NULL"); return SECURITY_SERVER_API_ERROR_INPUT_PARAM; @@ -84,16 +84,7 @@ int security_server_app_enable_permissions(const char *app_id, app_type_t app_ty //receive response from server Deserialization::Deserialize(recv, result); return result; - - } catch (MessageBuffer::Exception::Base &e) { - LogDebug("SecurityServer::MessageBuffer::Exception " << e.DumpToString()); - } catch (std::exception &e) { - LogDebug("STD exception " << e.what()); - } catch (...) { - LogDebug("Unknown exception occured"); - } - - return SECURITY_SERVER_API_ERROR_UNKNOWN; + }); } @@ -106,7 +97,7 @@ int security_server_app_disable_permissions(const char *app_id, app_type_t app_t LogDebug("security_server_app_disable_permissions() called"); - try { + return try_catch([&] { if ((NULL == app_id) || (strlen(app_id) == 0)) { LogDebug("App_id is NULL"); return SECURITY_SERVER_API_ERROR_INPUT_PARAM; @@ -141,16 +132,7 @@ int security_server_app_disable_permissions(const char *app_id, app_type_t app_t //receive response from server Deserialization::Deserialize(recv, result); return result; - - } catch (MessageBuffer::Exception::Base &e) { - LogDebug("SecurityServer::MessageBuffer::Exception " << e.DumpToString()); - } catch (std::exception &e) { - LogDebug("STD exception " << e.what()); - } catch (...) { - LogDebug("Unknown exception occured"); - } - - return SECURITY_SERVER_API_ERROR_UNKNOWN; + }); } diff --git a/src/server2/client/client-common.cpp b/src/server2/client/client-common.cpp index 7392d4b..2bd9091 100644 --- a/src/server2/client/client-common.cpp +++ b/src/server2/client/client-common.cpp @@ -260,6 +260,19 @@ int sendToServerAncData(char const * const interface, const RawBuffer &send, str return SECURITY_SERVER_API_SUCCESS; } +int try_catch(const std::function& func) +{ + try { + return func(); + } catch (MessageBuffer::Exception::Base &e) { + LogError("SecurityServer::MessageBuffer::Exception " << e.DumpToString()); + } catch (std::exception &e) { + LogError("STD exception " << e.what()); + } catch (...) { + LogError("Unknown exception occured"); + } + return SECURITY_SERVER_API_ERROR_UNKNOWN; +} } // namespace SecurityServer diff --git a/src/server2/client/client-common.h b/src/server2/client/client-common.h index 58c155f..b24cfe9 100644 --- a/src/server2/client/client-common.h +++ b/src/server2/client/client-common.h @@ -27,6 +27,7 @@ #define _SECURITY_SERVER_CLIENT_ #include +#include #include @@ -54,6 +55,12 @@ int sendToServer(char const * const interface, const RawBuffer &send, MessageBuf */ int sendToServerAncData(char const * const interface, const RawBuffer &send, struct msghdr &hdr); +/* + * Decorator function that performs frequently repeated exception handling in + * SS client API functions. Accepts lambda expression as an argument. + */ +int try_catch(const std::function& func); + } // namespace SecuritySever #endif // _SECURITY_SERVER_CLIENT_ diff --git a/src/server2/client/client-cookie.cpp b/src/server2/client/client-cookie.cpp index efa373d..ef76a98 100644 --- a/src/server2/client/client-cookie.cpp +++ b/src/server2/client/client-cookie.cpp @@ -51,7 +51,7 @@ int security_server_request_cookie(char *cookie, size_t bufferSize) LogDebug("security_server_request_cookie() called"); - try { + return try_catch([&] { //checking parameters if (bufferSize < COOKIE_SIZE) { LogDebug("Buffer for cookie too small"); @@ -85,16 +85,7 @@ int security_server_request_cookie(char *cookie, size_t bufferSize) memcpy(cookie, &receivedCookie[0], receivedCookie.size()); return retval; - - } catch (MessageBuffer::Exception::Base &e) { - LogDebug("SecurityServer::MessageBuffer::Exception " << e.DumpToString()); - } catch (std::exception &e) { - LogDebug("STD exception " << e.what()); - } catch (...) { - LogDebug("Unknown exception occured"); - } - - return SECURITY_SERVER_API_ERROR_UNKNOWN; + }); } SECURITY_SERVER_API @@ -113,7 +104,7 @@ int security_server_get_cookie_pid(const char *cookie) //preprae cookie to send std::vector key(cookie, cookie + COOKIE_SIZE); - try { + return try_catch([&] { //put data into buffer Serialization::Serialize(send, (int)CookieCall::CHECK_PID); Serialization::Serialize(send, key); @@ -132,16 +123,7 @@ int security_server_get_cookie_pid(const char *cookie) Deserialization::Deserialize(recv, pid); return pid; - - } catch (MessageBuffer::Exception::Base &e) { - LogDebug("SecurityServer::MessageBuffer::Exception " << e.DumpToString()); - } catch (std::exception &e) { - LogDebug("STD exception " << e.what()); - } catch (...) { - LogDebug("Unknown exception occured"); - } - - return SECURITY_SERVER_API_ERROR_UNKNOWN; + }); } SECURITY_SERVER_API @@ -207,7 +189,7 @@ int security_server_check_privilege(const char *cookie, gid_t privilege) //preprae cookie to send std::vector key(cookie, cookie + COOKIE_SIZE); - try { + return try_catch([&] { //put data into buffer Serialization::Serialize(send, (int)CookieCall::CHECK_PRIVILEGE_GID); Serialization::Serialize(send, key); @@ -223,16 +205,7 @@ int security_server_check_privilege(const char *cookie, gid_t privilege) //receive response from server Deserialization::Deserialize(recv, retval); return retval; - - } catch (MessageBuffer::Exception::Base &e) { - LogDebug("SecurityServer::MessageBuffer::Exception " << e.DumpToString()); - } catch (std::exception &e) { - LogDebug("STD exception " << e.what()); - } catch (...) { - LogDebug("Unknown exception occured"); - } - - return SECURITY_SERVER_API_ERROR_UNKNOWN; + }); } SECURITY_SERVER_API @@ -253,7 +226,7 @@ int security_server_check_privilege_by_cookie(const char *cookie, const char *ob std::string obj(object); std::string access(access_rights); - try { + return try_catch([&] { //put data into buffer Serialization::Serialize(send, (int)CookieCall::CHECK_PRIVILEGE); Serialization::Serialize(send, key); @@ -270,16 +243,7 @@ int security_server_check_privilege_by_cookie(const char *cookie, const char *ob //receive response from server Deserialization::Deserialize(recv, retval); return retval; - - } catch (MessageBuffer::Exception::Base &e) { - LogDebug("SecurityServer::MessageBuffer::Exception " << e.DumpToString()); - } catch (std::exception &e) { - LogDebug("STD exception " << e.what()); - } catch (...) { - LogDebug("Unknown exception occured"); - } - - return SECURITY_SERVER_API_ERROR_UNKNOWN; + }); } SECURITY_SERVER_API @@ -297,7 +261,7 @@ int security_server_get_uid_by_cookie(const char *cookie, uid_t *uid) //preprae cookie to send std::vector key(cookie, cookie + COOKIE_SIZE); - try { + return try_catch([&] { //put data into buffer Serialization::Serialize(send, (int)CookieCall::CHECK_UID); Serialization::Serialize(send, key); @@ -318,16 +282,7 @@ int security_server_get_uid_by_cookie(const char *cookie, uid_t *uid) } return retval; - - } catch (MessageBuffer::Exception::Base &e) { - LogDebug("SecurityServer::MessageBuffer::Exception " << e.DumpToString()); - } catch (std::exception &e) { - LogDebug("STD exception " << e.what()); - } catch (...) { - LogDebug("Unknown exception occured"); - } - - return SECURITY_SERVER_API_ERROR_UNKNOWN; + }); } SECURITY_SERVER_API @@ -345,7 +300,7 @@ int security_server_get_gid_by_cookie(const char *cookie, gid_t *gid) //preprae cookie to send std::vector key(cookie, cookie + COOKIE_SIZE); - try { + return try_catch([&] { //put data into buffer Serialization::Serialize(send, (int)CookieCall::CHECK_GID); Serialization::Serialize(send, key); @@ -366,15 +321,6 @@ int security_server_get_gid_by_cookie(const char *cookie, gid_t *gid) } return retval; - - } catch (MessageBuffer::Exception::Base &e) { - LogDebug("SecurityServer::MessageBuffer::Exception " << e.DumpToString()); - } catch (std::exception &e) { - LogDebug("STD exception " << e.what()); - } catch (...) { - LogDebug("Unknown exception occured"); - } - - return SECURITY_SERVER_API_ERROR_UNKNOWN; + }); } diff --git a/src/server2/client/client-get-gid.cpp b/src/server2/client/client-get-gid.cpp index 96ac70e..e39a1eb 100644 --- a/src/server2/client/client-get-gid.cpp +++ b/src/server2/client/client-get-gid.cpp @@ -37,7 +37,8 @@ SECURITY_SERVER_API int security_server_get_gid(const char *objectName) { using namespace SecurityServer; - try { + + return try_catch([&] { if (NULL == objectName){ LogDebug("Objects name is NULL"); return SECURITY_SERVER_API_ERROR_INPUT_PARAM; @@ -69,14 +70,7 @@ int security_server_get_gid(const char *objectName) { // No errors, return gid gid_t gid; Deserialization::Deserialize(recv, gid); - return gid; - } catch (MessageBuffer::Exception::Base &e) { - LogDebug("SecurityServer::MessageBuffer::Exception " << e.DumpToString()); - } catch (std::exception &e) { - LogDebug("STD exception " << e.what()); - } catch (...) { - LogDebug("Unknown exception occured"); - } - return SECURITY_SERVER_API_ERROR_UNKNOWN; + return static_cast(gid); + }); } diff --git a/src/server2/client/client-get-object-name.cpp b/src/server2/client/client-get-object-name.cpp index f7df552..a9d57ef 100644 --- a/src/server2/client/client-get-object-name.cpp +++ b/src/server2/client/client-get-object-name.cpp @@ -38,7 +38,7 @@ SECURITY_SERVER_API int security_server_get_object_name(gid_t gid, char *pObjectName, size_t maxObjectSize) { using namespace SecurityServer; - try { + return try_catch([&] { if (pObjectName == NULL){ LogDebug("Objects name is NULL or empty"); return SECURITY_SERVER_API_ERROR_INPUT_PARAM; @@ -70,13 +70,6 @@ int security_server_get_object_name(gid_t gid, char *pObjectName, size_t maxObje return result; - } catch (MessageBuffer::Exception::Base &e) { - LogDebug("SecurityServer::MessageBuffer::Exception " << e.DumpToString()); - } catch (std::exception &e) { - LogDebug("STD exception " << e.what()); - } catch (...) { - LogDebug("Unknown exception occured"); - } - return SECURITY_SERVER_API_ERROR_UNKNOWN; + }); } diff --git a/src/server2/client/client-password.cpp b/src/server2/client/client-password.cpp index 17466f8..3ef4038 100644 --- a/src/server2/client/client-password.cpp +++ b/src/server2/client/client-password.cpp @@ -33,6 +33,7 @@ #include #include +#include inline bool isPasswordIncorrect(const char* pwd) { @@ -46,7 +47,7 @@ int security_server_is_pwd_valid(unsigned int *current_attempts, { using namespace SecurityServer; - try { + return try_catch([&] { if (NULL == current_attempts || NULL == max_attempts || NULL == valid_secs) { @@ -77,14 +78,7 @@ int security_server_is_pwd_valid(unsigned int *current_attempts, } return retCode; - } catch (MessageBuffer::Exception::Base &e) { - LogError("SecurityServer::MessageBuffer::Exception " << e.DumpToString()); - } catch (std::exception &e) { - LogError("STD exception " << e.what()); - } catch (...) { - LogError("Unknown exception occured"); - } - return SECURITY_SERVER_API_ERROR_UNKNOWN; + }); } SECURITY_SERVER_API @@ -95,7 +89,7 @@ int security_server_chk_pwd(const char *challenge, { using namespace SecurityServer; - try { + return try_catch([&] { if (current_attempts == NULL || max_attempts == NULL || valid_secs == NULL || isPasswordIncorrect(challenge)) { LogError("Wrong input param"); @@ -133,14 +127,7 @@ int security_server_chk_pwd(const char *challenge, } return retCode; - } catch (MessageBuffer::Exception::Base &e) { - LogError("SecurityServer::MessageBuffer::Exception " << e.DumpToString()); - } catch (std::exception &e) { - LogError("STD exception " << e.what()); - } catch (...) { - LogError("Unknown exception occured"); - } - return SECURITY_SERVER_API_ERROR_UNKNOWN; + }); } SECURITY_SERVER_API @@ -151,7 +138,7 @@ int security_server_set_pwd(const char *cur_pwd, { using namespace SecurityServer; - try { + return try_catch([&] { if (NULL == cur_pwd) cur_pwd = ""; @@ -177,14 +164,7 @@ int security_server_set_pwd(const char *cur_pwd, Deserialization::Deserialize(recv, retCode); return retCode; - } catch (MessageBuffer::Exception::Base &e) { - LogError("SecurityServer::MessageBuffer::Exception " << e.DumpToString()); - } catch (std::exception &e) { - LogError("STD exception " << e.what()); - } catch (...) { - LogError("Unknown exception occured"); - } - return SECURITY_SERVER_API_ERROR_UNKNOWN; + }); } SECURITY_SERVER_API @@ -192,7 +172,7 @@ int security_server_set_pwd_validity(const unsigned int valid_period_in_days) { using namespace SecurityServer; - try { + return try_catch([&] { MessageBuffer send, recv; Serialization::Serialize(send, static_cast(PasswordHdrs::HDR_SET_PWD_VALIDITY)); @@ -207,14 +187,7 @@ int security_server_set_pwd_validity(const unsigned int valid_period_in_days) Deserialization::Deserialize(recv, retCode); return retCode; - } catch (MessageBuffer::Exception::Base &e) { - LogError("SecurityServer::MessageBuffer::Exception " << e.DumpToString()); - } catch (std::exception &e) { - LogError("STD exception " << e.what()); - } catch (...) { - LogError("Unknown exception occured"); - } - return SECURITY_SERVER_API_ERROR_UNKNOWN; + }); } SECURITY_SERVER_API @@ -222,7 +195,7 @@ int security_server_set_pwd_max_challenge(const unsigned int max_challenge) { using namespace SecurityServer; - try { + return try_catch([&] { MessageBuffer send, recv; Serialization::Serialize(send, static_cast(PasswordHdrs::HDR_SET_PWD_MAX_CHALLENGE)); @@ -237,14 +210,7 @@ int security_server_set_pwd_max_challenge(const unsigned int max_challenge) Deserialization::Deserialize(recv, retCode); return retCode; - } catch (MessageBuffer::Exception::Base &e) { - LogError("SecurityServer::MessageBuffer::Exception " << e.DumpToString()); - } catch (std::exception &e) { - LogError("STD exception " << e.what()); - } catch (...) { - LogError("Unknown exception occured"); - } - return SECURITY_SERVER_API_ERROR_UNKNOWN; + }); } SECURITY_SERVER_API @@ -254,7 +220,7 @@ int security_server_reset_pwd(const char *new_pwd, { using namespace SecurityServer; - try { + return try_catch([&] { if (isPasswordIncorrect(new_pwd)) { LogError("Wrong input param."); return SECURITY_SERVER_API_ERROR_INPUT_PARAM; @@ -276,14 +242,7 @@ int security_server_reset_pwd(const char *new_pwd, Deserialization::Deserialize(recv, retCode); return retCode; - } catch (MessageBuffer::Exception::Base &e) { - LogError("SecurityServer::MessageBuffer::Exception " << e.DumpToString()); - } catch (std::exception &e) { - LogError("STD exception " << e.what()); - } catch (...) { - LogError("Unknown exception occured"); - } - return SECURITY_SERVER_API_ERROR_UNKNOWN; + }); } SECURITY_SERVER_API @@ -291,7 +250,7 @@ int security_server_set_pwd_history(int history_size) { using namespace SecurityServer; - try { + return try_catch([&] { if (history_size > static_cast(MAX_PASSWORD_HISTORY) || history_size < 0) { LogError("Wrong input param."); return SECURITY_SERVER_API_ERROR_INPUT_PARAM; @@ -311,12 +270,5 @@ int security_server_set_pwd_history(int history_size) Deserialization::Deserialize(recv, retCode); return retCode; - } catch (MessageBuffer::Exception::Base &e) { - LogError("SecurityServer::MessageBuffer::Exception " << e.DumpToString()); - } catch (std::exception &e) { - LogError("STD exception " << e.what()); - } catch (...) { - LogError("Unknown exception occured"); - } - return SECURITY_SERVER_API_ERROR_UNKNOWN; + }); } diff --git a/src/server2/client/client-privilege-by-pid.cpp b/src/server2/client/client-privilege-by-pid.cpp index 9315487..5447e34 100644 --- a/src/server2/client/client-privilege-by-pid.cpp +++ b/src/server2/client/client-privilege-by-pid.cpp @@ -43,7 +43,7 @@ int security_server_check_privilege_by_pid( const char *object, const char *access_rights) { using namespace SecurityServer; - try { + return try_catch([&] { if (1 != smack_check()) return SECURITY_SERVER_API_SUCCESS; @@ -78,13 +78,6 @@ int security_server_check_privilege_by_pid( Deserialization::Deserialize(recv, result); return result; - } catch (MessageBuffer::Exception::Base &e) { - LogDebug("SecurityServer::MessageBuffer::Exception " << e.DumpToString()); - } catch (std::exception &e) { - LogDebug("STD exception " << e.what()); - } catch (...) { - LogDebug("Unknown exception occured"); - } - return SECURITY_SERVER_API_ERROR_UNKNOWN; + }); } diff --git a/src/server2/client/client-shared-memory.cpp b/src/server2/client/client-shared-memory.cpp index 66e6fc7..f688dcd 100644 --- a/src/server2/client/client-shared-memory.cpp +++ b/src/server2/client/client-shared-memory.cpp @@ -38,7 +38,7 @@ SECURITY_SERVER_API int security_server_app_give_access(const char *customer_label, int customer_pid) { using namespace SecurityServer; - try { + return try_catch([&] { if (1 != smack_check()) return SECURITY_SERVER_API_SUCCESS; @@ -62,13 +62,6 @@ int security_server_app_give_access(const char *customer_label, int customer_pid Deserialization::Deserialize(recv, result); return result; - } catch (MessageBuffer::Exception::Base &e) { - LogDebug("SecurityServer::MessageBuffer::Exception " << e.DumpToString()); - } catch (std::exception &e) { - LogDebug("STD exception " << e.what()); - } catch (...) { - LogDebug("Unknown exception occured"); - } - return SECURITY_SERVER_API_ERROR_UNKNOWN; + }); }