From 25a83a54414e01784d96f56769b3a576a5314bbc Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Wed, 14 Sep 2011 14:15:54 +0300 Subject: [PATCH 1/1] Check write result and replace int with bool in return type of handlers --- .../stargazer/plugins/other/smux/handlers.cpp | 37 ++++++------ .../stargazer/plugins/other/smux/smux.cpp | 7 ++- .../stargazer/plugins/other/smux/utils.cpp | 60 ++++++++++++------- projects/stargazer/plugins/other/smux/utils.h | 14 ++--- 4 files changed, 69 insertions(+), 49 deletions(-) diff --git a/projects/stargazer/plugins/other/smux/handlers.cpp b/projects/stargazer/plugins/other/smux/handlers.cpp index 7ceb2994..01878da6 100644 --- a/projects/stargazer/plugins/other/smux/handlers.cpp +++ b/projects/stargazer/plugins/other/smux/handlers.cpp @@ -15,12 +15,12 @@ bool SMUX::CloseHandler(const SMUX_PDUs_t * pdus) { printfd(__FILE__, "SMUX::CloseHandler()\n"); asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus); -return false; +return true; } #else bool SMUX::CloseHandler(const SMUX_PDUs_t *) { -return false; +return true; } #endif @@ -29,12 +29,12 @@ bool SMUX::RegisterResponseHandler(const SMUX_PDUs_t * pdus) { printfd(__FILE__, "SMUX::RegisterResponseHandler()\n"); asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus); -return false; +return true; } #else bool SMUX::RegisterResponseHandler(const SMUX_PDUs_t *) { -return false; +return true; } #endif @@ -69,7 +69,7 @@ else } #endif } -return false; +return true; } #ifdef SMUX_DEBUG @@ -77,12 +77,12 @@ bool SMUX::CommitOrRollbackHandler(const SMUX_PDUs_t * pdus) { printfd(__FILE__, "SMUX::CommitOrRollbackHandler()\n"); asn_fprint(stderr, &asn_DEF_SMUX_PDUs, pdus); -return false; +return true; } #else bool SMUX::CommitOrRollbackHandler(const SMUX_PDUs_t *) { -return false; +return true; } #endif @@ -111,9 +111,8 @@ for (int i = 0; i < vbl->list.count; ++i) it = sensors.find(OID(&vb->name)); if (it == sensors.end()) { - SendGetResponseErrorPDU(sock, getRequest, - PDU__error_status_noSuchName, i); - return true; + return SendGetResponseErrorPDU(sock, getRequest, + PDU__error_status_noSuchName, i); } VarBind_t * newVb = static_cast(calloc(1, sizeof(VarBind_t))); @@ -125,12 +124,12 @@ for (int i = 0; i < vbl->list.count; ++i) ASN_SEQUENCE_ADD(varBindList, newVb); } -SendGetResponsePDU(sock, msg); +bool res = SendGetResponsePDU(sock, msg); #ifdef SMUX_DEBUG asn_fprint(stderr, &asn_DEF_GetResponse_PDU, msg); #endif ASN_STRUCT_FREE(asn_DEF_GetResponse_PDU, msg); -return false; +return res; } bool SMUX::GetNextRequestHandler(const PDUs_t * pdus) @@ -161,9 +160,8 @@ for (int i = 0; i < vbl->list.count; ++i) #ifdef SMUX_DEBUG printfd(__FILE__, "SMUX::GetNextRequestHandler() - '%s' not found\n", OID(&vb->name).ToString().c_str()); #endif - SendGetResponseErrorPDU(sock, getRequest, - PDU__error_status_noSuchName, i); - return true; + return SendGetResponseErrorPDU(sock, getRequest, + PDU__error_status_noSuchName, i); } VarBind_t * newVb = static_cast(calloc(1, sizeof(VarBind_t))); @@ -175,12 +173,12 @@ for (int i = 0; i < vbl->list.count; ++i) ASN_SEQUENCE_ADD(varBindList, newVb); } -SendGetResponsePDU(sock, msg); +bool res = SendGetResponsePDU(sock, msg); #ifdef SMUX_DEBUG asn_fprint(stderr, &asn_DEF_PDU, msg); #endif ASN_STRUCT_FREE(asn_DEF_GetResponse_PDU, msg); -return false; +return res; } bool SMUX::SetRequestHandler(const PDUs_t * pdus) @@ -189,7 +187,6 @@ bool SMUX::SetRequestHandler(const PDUs_t * pdus) printfd(__FILE__, "SMUX::SetRequestHandler()\n"); asn_fprint(stderr, &asn_DEF_PDUs, pdus); #endif -SendGetResponseErrorPDU(sock, &pdus->choice.set_request, - PDU__error_status_readOnly, 0); -return false; +return SendGetResponseErrorPDU(sock, &pdus->choice.set_request, + PDU__error_status_readOnly, 0); } diff --git a/projects/stargazer/plugins/other/smux/smux.cpp b/projects/stargazer/plugins/other/smux/smux.cpp index 8faccc2d..fdcaaf02 100644 --- a/projects/stargazer/plugins/other/smux/smux.cpp +++ b/projects/stargazer/plugins/other/smux/smux.cpp @@ -276,8 +276,11 @@ return NULL; void SMUX::Run() { -SendOpenPDU(sock); -SendRReqPDU(sock); +stopped = true; +if (!SendOpenPDU(sock)) + return; +if (!SendRReqPDU(sock)) + return; running = true; stopped = false; diff --git a/projects/stargazer/plugins/other/smux/utils.cpp b/projects/stargazer/plugins/other/smux/utils.cpp index 8d3988eb..18e2bf31 100644 --- a/projects/stargazer/plugins/other/smux/utils.cpp +++ b/projects/stargazer/plugins/other/smux/utils.cpp @@ -101,12 +101,16 @@ if (error.encoded == -1) } else { - write(fd, buffer, error.encoded); + if (write(fd, buffer, error.encoded) < 0) + { + printfd(__FILE__, "Failed to send OpenPDU: %s\n", strerror(errno)); + return false; + } } return true; } -int SendClosePDU(int fd) +bool SendClosePDU(int fd) { ClosePDU_t msg; @@ -124,16 +128,20 @@ if (error.encoded == -1) { printfd(__FILE__, "Could not encode ClosePDU (at %s)\n", error.failed_type ? error.failed_type->name : "unknown"); - return -1; + return false; } else { - write(fd, buffer, error.encoded); + if (write(fd, buffer, error.encoded) < 0) + { + printfd(__FILE__, "Failed to send ClosePDU: %s\n", strerror(errno)); + return false; + } } -return 0; +return true; } -int SendRReqPDU(int fd) +bool SendRReqPDU(int fd) { int oid[] = {1, 3, 6, 1, 4, 1, 38313, 1}; asn_enc_rval_t error; @@ -157,13 +165,17 @@ if (error.encoded == -1) { printfd(__FILE__, "Could not encode RReqPDU (at %s)\n", error.failed_type ? error.failed_type->name : "unknown"); - return -1; + return false; } else { - write(fd, buffer, error.encoded); + if (write(fd, buffer, error.encoded) < 0) + { + printfd(__FILE__, "Failed to send RReqPDU: %s\n", strerror(errno)); + return false; + } } -return 0; +return true; } SMUX_PDUs_t * RecvSMUXPDUs(int fd) @@ -188,7 +200,7 @@ if(error.code != RC_OK) return pdus; } -int SendGetResponsePDU(int fd, GetResponse_PDU_t * getResponse) +bool SendGetResponsePDU(int fd, GetResponse_PDU_t * getResponse) { asn_enc_rval_t error; @@ -200,19 +212,23 @@ if (error.encoded == -1) { printfd(__FILE__, "Could not encode GetResponsePDU (at %s)\n", error.failed_type ? error.failed_type->name : "unknown"); - return -1; + return false; } else { - write(fd, buffer, error.encoded); + if (write(fd, buffer, error.encoded) < 0) + { + printfd(__FILE__, "Failed to send GetResponsePDU: %s\n", strerror(errno)); + return false; + } } -return 0; +return true; } -int SendGetResponseErrorPDU(int fd, - const PDU_t * getRequest, - int errorStatus, - int errorIndex) +bool SendGetResponseErrorPDU(int fd, + const PDU_t * getRequest, + int errorStatus, + int errorIndex) { asn_enc_rval_t error; GetResponse_PDU_t msg; @@ -235,11 +251,15 @@ if (error.encoded == -1) { printfd(__FILE__, "Could not encode GetResponsePDU for error (at %s)\n", error.failed_type ? error.failed_type->name : "unknown"); - return -1; + return false; } else { - write(fd, buffer, error.encoded); + if (write(fd, buffer, error.encoded) < 0) + { + printfd(__FILE__, "Failed to send GetResponseErrorPDU: %s\n", strerror(errno)); + return false; + } } -return 0; +return true; } diff --git a/projects/stargazer/plugins/other/smux/utils.h b/projects/stargazer/plugins/other/smux/utils.h index 8007fee6..27eb3bf1 100644 --- a/projects/stargazer/plugins/other/smux/utils.h +++ b/projects/stargazer/plugins/other/smux/utils.h @@ -10,13 +10,13 @@ bool String2OI(const std::string & str, OBJECT_IDENTIFIER_t * oi); std::string OI2String(OBJECT_IDENTIFIER_t * oi); bool SendOpenPDU(int fd); -int SendClosePDU(int fd); -int SendRReqPDU(int fd); +bool SendClosePDU(int fd); +bool SendRReqPDU(int fd); SMUX_PDUs_t * RecvSMUXPDUs(int fd); -int SendGetResponsePDU(int fd, GetResponse_PDU_t * getResponse); -int SendGetResponseErrorPDU(int fd, - const PDU_t * getRequest, - int errorStatus, - int errorIndex); +bool SendGetResponsePDU(int fd, GetResponse_PDU_t * getResponse); +bool SendGetResponseErrorPDU(int fd, + const PDU_t * getRequest, + int errorStatus, + int errorIndex); #endif -- 2.44.2