From 5d2e8e1e57b5569eecbed5a6332a3f15bcce275b Mon Sep 17 00:00:00 2001 From: Zhanglong Xia Date: Tue, 23 Jul 2019 09:51:07 +0800 Subject: [PATCH] [posix-app] add platform log to record when program exits (#3989) --- src/posix/platform/hdlc_interface.cpp | 25 +++---- src/posix/platform/misc.c | 73 ++++++++++-------- src/posix/platform/netif.cpp | 28 ++----- src/posix/platform/platform-posix.h | 104 +++++++++++++++++++++----- src/posix/platform/radio_spinel.cpp | 17 ++--- src/posix/platform/settings.cpp | 56 +++++++------- src/posix/platform/sim.c | 19 +++-- src/posix/platform/uart.c | 37 ++++----- src/posix/platform/udp.cpp | 2 +- 9 files changed, 202 insertions(+), 159 deletions(-) diff --git a/src/posix/platform/hdlc_interface.cpp b/src/posix/platform/hdlc_interface.cpp index c6188f6a5..c1b129178 100644 --- a/src/posix/platform/hdlc_interface.cpp +++ b/src/posix/platform/hdlc_interface.cpp @@ -193,8 +193,7 @@ void HdlcInterface::Read(void) } else if ((rval < 0) && (errno != EAGAIN) && (errno != EINTR)) { - perror("HdlcInterface::Read()"); - exit(OT_EXIT_FAILURE); + DieNow(OT_EXIT_ERROR_ERRNO); } } @@ -240,8 +239,7 @@ otError HdlcInterface::Write(const uint8_t *aFrame, uint16_t aLength) if ((rval < 0) && (errno != EAGAIN) && (errno != EWOULDBLOCK) && (errno != EINTR)) { - perror("HdlcInterface::Write()"); - exit(OT_EXIT_FAILURE); + DieNow(OT_EXIT_ERROR_ERRNO); } SuccessOrExit(error = WaitForWritable()); @@ -279,19 +277,16 @@ otError HdlcInterface::WaitForWritable(void) } else if (FD_ISSET(mSockFd, &errorFds)) { - fprintf(stderr, "HdlcInterface::WaitForWritable(): socket error\n\r"); - exit(OT_EXIT_FAILURE); + DieNowWithMessage("socket error", OT_EXIT_FAILURE); } else { - fprintf(stderr, "HdlcInterface::WaitForWritable(): select error\n\r"); - exit(OT_EXIT_FAILURE); + DieNowWithMessage("select error", OT_EXIT_FAILURE); } } else if ((rval < 0) && (errno != EINTR)) { - perror("HdlcInterface::WaitForWritable()"); - exit(OT_EXIT_FAILURE); + DieNow(OT_EXIT_ERROR_ERRNO); } now = otSysGetTime(); @@ -357,7 +352,7 @@ int HdlcInterface::OpenFile(const char *aFile, const char *aConfig) default: // not supported assert(false); - exit(OT_EXIT_INVALID_ARGUMENTS); + DieNow(OT_EXIT_INVALID_ARGUMENTS); break; } @@ -371,7 +366,7 @@ int HdlcInterface::OpenFile(const char *aFile, const char *aConfig) break; default: assert(false); - exit(OT_EXIT_INVALID_ARGUMENTS); + DieNow(OT_EXIT_INVALID_ARGUMENTS); break; } @@ -459,7 +454,7 @@ int HdlcInterface::OpenFile(const char *aFile, const char *aConfig) #endif default: assert(false); - exit(OT_EXIT_INVALID_ARGUMENTS); + DieNow(OT_EXIT_INVALID_ARGUMENTS); break; } @@ -471,7 +466,7 @@ int HdlcInterface::OpenFile(const char *aFile, const char *aConfig) exit: if (rval != 0) { - exit(OT_EXIT_FAILURE); + DieNow(OT_EXIT_FAILURE); } return fd; @@ -514,7 +509,7 @@ int HdlcInterface::ForkPty(const char *aCommand, const char *aArguments) } exit: - VerifyOrDie(rval == 0, OT_EXIT_FAILURE); + VerifyOrDie(rval == 0, OT_EXIT_ERROR_ERRNO); return fd; } #endif // OPENTHREAD_CONFIG_POSIX_APP_ENABLE_PTY_DEVICE diff --git a/src/posix/platform/misc.c b/src/posix/platform/misc.c index e0a67ca56..48f10ef73 100644 --- a/src/posix/platform/misc.c +++ b/src/posix/platform/misc.c @@ -95,38 +95,6 @@ otPlatMcuPowerState otPlatGetMcuPowerState(otInstance *aInstance) return gPlatMcuPowerState; } -void SuccessOrDie(otError aError) -{ - int exitCode; - - switch (aError) - { - case OT_ERROR_NONE: - return; - - case OT_ERROR_INVALID_ARGS: - exitCode = OT_EXIT_INVALID_ARGUMENTS; - break; - - default: - exitCode = OT_EXIT_FAILURE; - break; - } - - otLogCritPlat("Error: %s", otThreadErrorToString(aError)); - // For better user experience. - fprintf(stderr, "Error: %s\r\n", otThreadErrorToString(aError)); - exit(exitCode); -} - -void VerifyOrDie(bool aCondition, int aExitCode) -{ - if (!aCondition) - { - exit(aExitCode); - } -} - int SocketWithCloseExec(int aDomain, int aType, int aProtocol) { int rval = 0; @@ -144,9 +112,48 @@ int SocketWithCloseExec(int aDomain, int aType, int aProtocol) exit: if (rval == -1 && fd != -1) { - VerifyOrDie(close(fd) == 0, OT_EXIT_FAILURE); + VerifyOrDie(close(fd) == 0, OT_EXIT_ERROR_ERRNO); fd = -1; } return fd; } + +const char *otExitCodeToString(uint8_t aExitCode) +{ + const char *retval = NULL; + + switch (aExitCode) + { + case OT_EXIT_SUCCESS: + retval = "Success"; + break; + + case OT_EXIT_FAILURE: + retval = "Failure"; + break; + + case OT_EXIT_INVALID_ARGUMENTS: + retval = "InvalidArgument"; + break; + + case OT_EXIT_RADIO_SPINEL_INCOMPATIBLE: + retval = "RadioSpinelIncompatible"; + break; + + case OT_EXIT_RADIO_SPINEL_RESET: + retval = "RadioSpinelReset"; + break; + + case OT_EXIT_ERROR_ERRNO: + retval = strerror(errno); + break; + + default: + assert(false); + retval = "UnknownExitCode"; + break; + } + + return retval; +} diff --git a/src/posix/platform/netif.cpp b/src/posix/platform/netif.cpp index a44772a9b..280a182ef 100644 --- a/src/posix/platform/netif.cpp +++ b/src/posix/platform/netif.cpp @@ -102,15 +102,8 @@ static void UpdateUnicast(otInstance *aInstance, const otIp6Address &aAddress, u error = OT_ERROR_FAILED); exit: - if (error == OT_ERROR_NONE) - { - otLogInfoPlat("%s: %s", __func__, otThreadErrorToString(error)); - } - else - { - otLogCritPlat("%s: %s", __func__, otThreadErrorToString(error)); - exit(OT_EXIT_FAILURE); - } + SuccessOrDie(error); + otLogInfoPlat("%s: %s", __func__, otThreadErrorToString(error)); } static void UpdateMulticast(otInstance *aInstance, const otIp6Address &aAddress, bool aIsAdded) @@ -130,15 +123,8 @@ static void UpdateMulticast(otInstance *aInstance, const otIp6Address &aAddress, error = OT_ERROR_FAILED); exit: - if (error == OT_ERROR_NONE) - { - otLogInfoPlat("%s: %s", __func__, otThreadErrorToString(error)); - } - else - { - otLogCritPlat("%s: %s", __func__, otThreadErrorToString(error)); - exit(OT_EXIT_FAILURE); - } + SuccessOrDie(error); + otLogInfoPlat("%s: %s", __func__, otThreadErrorToString(error)); } static void UpdateLink(otInstance *aInstance) @@ -454,7 +440,7 @@ exit: sNetlinkFd = -1; } - exit(OT_EXIT_FAILURE); + DieNow(OT_EXIT_FAILURE); } } @@ -495,13 +481,13 @@ void platformNetifProcess(const fd_set *aReadFdSet, const fd_set *aWriteFdSet, c if (FD_ISSET(sTunFd, aErrorFdSet)) { close(sTunFd); - exit(OT_EXIT_FAILURE); + DieNow(OT_EXIT_FAILURE); } if (FD_ISSET(sNetlinkFd, aErrorFdSet)) { close(sNetlinkFd); - exit(OT_EXIT_FAILURE); + DieNow(OT_EXIT_FAILURE); } if (FD_ISSET(sTunFd, aReadFdSet)) diff --git a/src/posix/platform/platform-posix.h b/src/posix/platform/platform-posix.h index f1526060f..f17f50726 100644 --- a/src/posix/platform/platform-posix.h +++ b/src/posix/platform/platform-posix.h @@ -35,12 +35,17 @@ #ifndef PLATFORM_POSIX_H_ #define PLATFORM_POSIX_H_ +#include +#include +#include #include #include +#include #include #include "platform-config.h" +#include "common/logging.hpp" /** * This is the socket name used by daemon mode. @@ -100,8 +105,89 @@ enum * Unexpected radio spinel reset. */ OT_EXIT_RADIO_SPINEL_RESET = 4, + + /** + * System call or library function error. + */ + OT_EXIT_ERROR_ERRNO = 5, }; +/** + * This function converts an exit code into a string. + * + * @param[in] aExitCode An exit code. + * + * @returns A string representation of an exit code. + * + */ +const char *otExitCodeToString(uint8_t aExitCode); + +/** + * This macro checks for the specified condition, which is expected to commonly be true, + * and both records exit status and terminates the program if the condition is false. + * + * @param[in] aCondition The condition to verify + * @param[in] aExitCode The exit code. + * + */ +#define VerifyOrDie(aCondition, aExitCode) \ + do \ + { \ + if (!(aCondition)) \ + { \ + fprintf(stderr, "exit(%d): %s line %d, %s\r\n", aExitCode, __func__, __LINE__, \ + otExitCodeToString(aExitCode)); \ + otLogCritPlat("exit(%d): %s line %d, %s", aExitCode, __func__, __LINE__, otExitCodeToString(aExitCode)); \ + exit(aExitCode); \ + } \ + } while (false) + +/** + * This macro checks for the specified error code, which is expected to commonly be successful, + * and both records exit status and terminates the program if the error code is unsuccessful. + * + * @param[in] aError An error code to be evaluated against OT_ERROR_NONE. + * + */ +#define SuccessOrDie(aError) \ + do \ + { \ + if (aError != OT_ERROR_NONE) \ + { \ + uint8_t exitCode; \ + exitCode = (aError == OT_ERROR_INVALID_ARGS) ? OT_EXIT_INVALID_ARGUMENTS : OT_EXIT_FAILURE; \ + fprintf(stderr, "exit(%d): %s line %d, %s\r\n", exitCode, __func__, __LINE__, \ + otThreadErrorToString(aError)); \ + otLogCritPlat("exit(%d): %s line %d, %s", exitCode, __func__, __LINE__, otThreadErrorToString(aError)); \ + exit(exitCode); \ + } \ + } while (false) + +/** + * This macro unconditionally both records exit status and terminates the program. + * + * @param[in] aExitCode The exit code. + * + */ +#define DieNow(aExitCode) VerifyOrDie(false, aExitCode) + +/** + * This macro unconditionally both records exit status and exit message and terminates the program. + * + * @param[in] aMessage The exit message. + * @param[in] aExitCode The exit code. + * + */ +#define DieNowWithMessage(aMessage, aExitCode) \ + do \ + { \ + fprintf(stderr, "exit(%d): %s line %d, %s, %s\r\n", aExitCode, __func__, __LINE__, aMessage, \ + otExitCodeToString(aExitCode)); \ + otLogCritPlat("exit(%d): %s line %d, %s, %s", aExitCode, __func__, __LINE__, aMessage, \ + otExitCodeToString(aExitCode)); \ + exit(aExitCode); \ + } while (false) + /** * Unique node ID. * @@ -381,24 +467,6 @@ void platformUdpProcess(otInstance *aInstance, const fd_set *aReadSet); */ void platformUdpUpdateFdSet(otInstance *aInstance, fd_set *aReadFdSet, int *aMaxFd); -/** - * This function ends the current process with exit code @p aExitCode if @p aCondition is false. - * - * @param[in] aCondition The condition to verify - * @param[in] aExitCode The exit code if exits. - * - */ -void VerifyOrDie(bool aCondition, int aExitCode); - -/** - * This function ends the current process if @p aError is not OT_ERROR_NONE. - * The error code will be mapped from @p aError. - * - * @param[in] aError The OpenThread error code. - * - */ -void SuccessOrDie(otError aError); - /** * This function creates a socket with SOCK_CLOEXEC flag set. * diff --git a/src/posix/platform/radio_spinel.cpp b/src/posix/platform/radio_spinel.cpp index 38cfa7b2b..79fb6cd02 100644 --- a/src/posix/platform/radio_spinel.cpp +++ b/src/posix/platform/radio_spinel.cpp @@ -231,7 +231,7 @@ otError RadioSpinel::CheckSpinelVersion(void) { otLogCritPlat("Spinel version mismatch - PosixApp:%d.%d, RCP:%d.%d", SPINEL_PROTOCOL_VERSION_THREAD_MAJOR, SPINEL_PROTOCOL_VERSION_THREAD_MINOR, versionMajor, versionMinor); - exit(OT_EXIT_RADIO_SPINEL_INCOMPATIBLE); + DieNow(OT_EXIT_RADIO_SPINEL_INCOMPATIBLE); } exit: @@ -273,7 +273,7 @@ otError RadioSpinel::CheckCapabilities(void) if (!supportsRawRadio) { otLogCritPlat("RCP capability list does not include support for radio/raw mode"); - exit(OT_EXIT_RADIO_SPINEL_INCOMPATIBLE); + DieNow(OT_EXIT_RADIO_SPINEL_INCOMPATIBLE); } exit: @@ -298,7 +298,7 @@ otError RadioSpinel::CheckRadioCapabilities(void) (mRadioCaps & OT_RADIO_CAPS_TRANSMIT_RETRIES) ? "yes" : "no", (mRadioCaps & OT_RADIO_CAPS_CSMA_BACKOFF) ? "yes" : "no"); - exit(OT_EXIT_RADIO_SPINEL_INCOMPATIBLE); + DieNow(OT_EXIT_RADIO_SPINEL_INCOMPATIBLE); } exit: @@ -778,8 +778,7 @@ void RadioSpinel::Process(const fd_set &aReadFdSet, const fd_set &aWriteFdSet) } else if (mState == kStateTransmitting && otSysGetTime() >= mTxRadioEndUs) { - otLogCritPlat("radio tx timeout, exit"); - exit(OT_EXIT_FAILURE); + DieNowWithMessage("radio tx timeout", OT_EXIT_FAILURE); } if (FD_ISSET(mHdlcInterface.GetSocket(), &aWriteFdSet)) @@ -1027,13 +1026,12 @@ otError RadioSpinel::WaitResponse(void) } else if (FD_ISSET(sockFd, &error_fds)) { - fprintf(stderr, "NCP error\r\n"); - exit(OT_EXIT_FAILURE); + DieNowWithMessage("NCP error", OT_EXIT_FAILURE); } else { assert(false); - exit(OT_EXIT_FAILURE); + DieNow(OT_EXIT_FAILURE); } } else if (rval == 0) @@ -1044,8 +1042,7 @@ otError RadioSpinel::WaitResponse(void) } else if (errno != EINTR) { - perror("wait response"); - exit(OT_EXIT_FAILURE); + DieNowWithMessage("wait response", OT_EXIT_FAILURE); } #endif // OPENTHREAD_POSIX_VIRTUAL_TIME diff --git a/src/posix/platform/settings.cpp b/src/posix/platform/settings.cpp index d8d78b629..87258d49b 100644 --- a/src/posix/platform/settings.cpp +++ b/src/posix/platform/settings.cpp @@ -49,16 +49,6 @@ #include "common/code_utils.hpp" -#define VerifyOrDie(aCondition) \ - do \ - { \ - if (!(aCondition)) \ - { \ - perror(__func__); \ - exit(OT_EXIT_FAILURE); \ - } \ - } while (false) - static const size_t kMaxFileNameSize = sizeof(OPENTHREAD_CONFIG_POSIX_SETTINGS_PATH) + 32; static int sSettingsFd = -1; @@ -77,8 +67,9 @@ static int swapOpen(void) int fd; getSettingsFileName(fileName, true); + fd = open(fileName, O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0600); - VerifyOrDie(fd != -1); + VerifyOrDie(fd != -1, OT_EXIT_ERROR_ERRNO); return fd; } @@ -100,11 +91,11 @@ static void swapWrite(int aFd, uint16_t aLength) uint16_t count = aLength >= sizeof(buffer) ? sizeof(buffer) : aLength; ssize_t rval = read(sSettingsFd, buffer, count); - VerifyOrDie(rval > 0); + VerifyOrDie(rval > 0, OT_EXIT_FAILURE); count = static_cast(rval); rval = write(aFd, buffer, count); assert(rval == count); - VerifyOrDie(rval == count); + VerifyOrDie(rval == count, OT_EXIT_FAILURE); aLength -= count; } } @@ -117,9 +108,9 @@ static void swapPersist(int aFd) getSettingsFileName(swapFile, true); getSettingsFileName(dataFile, false); - VerifyOrDie(0 == close(sSettingsFd)); - VerifyOrDie(0 == rename(swapFile, dataFile)); - VerifyOrDie(0 == fsync(aFd)); + VerifyOrDie(0 == close(sSettingsFd), OT_EXIT_ERROR_ERRNO); + VerifyOrDie(0 == rename(swapFile, dataFile), OT_EXIT_ERROR_ERRNO); + VerifyOrDie(0 == fsync(aFd), OT_EXIT_ERROR_ERRNO); sSettingsFd = aFd; } @@ -128,9 +119,9 @@ static void swapDiscard(int aFd) { char swapFileName[kMaxFileNameSize]; - VerifyOrDie(0 == close(aFd)); + VerifyOrDie(0 == close(aFd), OT_EXIT_ERROR_ERRNO); getSettingsFileName(swapFileName, true); - VerifyOrDie(0 == unlink(swapFileName)); + VerifyOrDie(0 == unlink(swapFileName), OT_EXIT_ERROR_ERRNO); } void otPlatSettingsInit(otInstance *aInstance) @@ -155,7 +146,7 @@ void otPlatSettingsInit(otInstance *aInstance) sSettingsFd = open(fileName, O_RDWR | O_CREAT | O_CLOEXEC, 0600); } - VerifyOrDie(sSettingsFd != -1); + VerifyOrDie(sSettingsFd != -1, OT_EXIT_ERROR_ERRNO); for (off_t size = lseek(sSettingsFd, 0, SEEK_END), offset = lseek(sSettingsFd, 0, SEEK_SET); offset < size;) { @@ -176,7 +167,7 @@ void otPlatSettingsInit(otInstance *aInstance) exit: if (error == OT_ERROR_PARSE) { - VerifyOrDie(ftruncate(sSettingsFd, 0) == 0); + VerifyOrDie(ftruncate(sSettingsFd, 0) == 0, OT_EXIT_ERROR_ERRNO); } } @@ -185,7 +176,7 @@ void otPlatSettingsDeinit(otInstance *aInstance) OT_UNUSED_VARIABLE(aInstance); assert(sSettingsFd != -1); - VerifyOrDie(close(sSettingsFd) == 0); + VerifyOrDie(close(sSettingsFd) == 0, OT_EXIT_ERROR_ERRNO); } otError otPlatSettingsGet(otInstance *aInstance, uint16_t aKey, int aIndex, uint8_t *aValue, uint16_t *aValueLength) @@ -241,7 +232,7 @@ otError otPlatSettingsGet(otInstance *aInstance, uint16_t aKey, int aIndex, uint } exit: - VerifyOrDie(error != OT_ERROR_PARSE); + VerifyOrDie(error != OT_ERROR_PARSE, OT_EXIT_FAILURE); return error; } @@ -260,13 +251,14 @@ otError otPlatSettingsAdd(otInstance *aInstance, uint16_t aKey, const uint8_t *a if (size > 0) { - VerifyOrDie(0 == lseek(sSettingsFd, 0, SEEK_SET)); + VerifyOrDie(0 == lseek(sSettingsFd, 0, SEEK_SET), OT_EXIT_ERROR_ERRNO); swapWrite(swapFd, static_cast(size)); } VerifyOrDie(write(swapFd, &aKey, sizeof(aKey)) == sizeof(aKey) && - write(swapFd, &aValueLength, sizeof(aValueLength)) == sizeof(aValueLength) && - write(swapFd, aValue, aValueLength) == aValueLength); + write(swapFd, &aValueLength, sizeof(aValueLength)) == sizeof(aValueLength) && + write(swapFd, aValue, aValueLength) == aValueLength, + OT_EXIT_FAILURE); swapPersist(swapFd); @@ -323,17 +315,17 @@ otError otPlatSettingsDelete(otInstance *aInstance, uint16_t aKey, int aIndex) rval = write(swapFd, &key, sizeof(key)); assert(rval == sizeof(key)); - VerifyOrDie(rval == sizeof(key)); + VerifyOrDie(rval == sizeof(key), OT_EXIT_FAILURE); rval = write(swapFd, &length, sizeof(length)); assert(rval == sizeof(length)); - VerifyOrDie(rval == sizeof(length)); + VerifyOrDie(rval == sizeof(length), OT_EXIT_FAILURE); swapWrite(swapFd, length); } exit: - VerifyOrDie(error != OT_ERROR_PARSE); + VerifyOrDie(error != OT_ERROR_PARSE, OT_EXIT_FAILURE); if (error == OT_ERROR_NONE) { @@ -350,13 +342,19 @@ exit: void otPlatSettingsWipe(otInstance *aInstance) { OT_UNUSED_VARIABLE(aInstance); - VerifyOrDie(0 == ftruncate(sSettingsFd, 0)); + VerifyOrDie(0 == ftruncate(sSettingsFd, 0), OT_EXIT_ERROR_ERRNO); } #if SELF_TEST uint64_t gNodeId = 1; +const char *otExitCodeToString(uint8_t aExitCode) +{ + OT_UNUSED_VARIABLE(aExitCode); + return "SELF_TEST"; +} + int main() { otInstance *instance = NULL; diff --git a/src/posix/platform/sim.c b/src/posix/platform/sim.c index 72fb9b3e5..5558c9445 100644 --- a/src/posix/platform/sim.c +++ b/src/posix/platform/sim.c @@ -71,8 +71,11 @@ void otSimInit(void) if (*endptr != '\0') { - fprintf(stderr, "Invalid PORT_OFFSET: %s\n", offset); - exit(OT_EXIT_INVALID_ARGUMENTS); + const uint8_t kMsgSize = 40; + char msg[kMsgSize]; + + snprintf(msg, sizeof(msg), "Invalid PORT_OFFSET: %s", offset); + DieNowWithMessage(msg, OT_EXIT_INVALID_ARGUMENTS); } sPortOffset *= kWellKnownNodeId; @@ -88,14 +91,12 @@ void otSimInit(void) if (sSockFd == -1) { - perror("socket"); - exit(OT_EXIT_FAILURE); + DieNowWithMessage("socket", OT_EXIT_ERROR_ERRNO); } if (bind(sSockFd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) == -1) { - perror("bind"); - exit(OT_EXIT_FAILURE); + DieNowWithMessage("bind", OT_EXIT_ERROR_ERRNO); } } @@ -122,8 +123,7 @@ static void otSimSendEvent(struct Event *aEvent, size_t aLength) if (rval < 0) { - perror("sendto"); - exit(OT_EXIT_FAILURE); + DieNowWithMessage("sendto", OT_EXIT_ERROR_ERRNO); } } @@ -133,8 +133,7 @@ void otSimReceiveEvent(struct Event *aEvent) if (rval < 0 || (uint16_t)rval < offsetof(struct Event, mData)) { - perror("recvfrom"); - exit(OT_EXIT_FAILURE); + DieNowWithMessage("recvfrom", (rval < 0) ? OT_EXIT_ERROR_ERRNO : OT_EXIT_FAILURE); } sNow += aEvent->mDelay; diff --git a/src/posix/platform/uart.c b/src/posix/platform/uart.c index a4d7678a2..93859ff82 100644 --- a/src/posix/platform/uart.c +++ b/src/posix/platform/uart.c @@ -71,21 +71,19 @@ otError otPlatUartEnable(void) if (sUartSocket == -1) { - exit(OT_EXIT_FAILURE); + DieNow(OT_EXIT_FAILURE); } sUartLock = open(OPENTHREAD_POSIX_APP_SOCKET_LOCK, O_CREAT | O_RDONLY | O_CLOEXEC, 0600); if (sUartLock == -1) { - perror("open"); - exit(OT_EXIT_FAILURE); + DieNowWithMessage("open", OT_EXIT_ERROR_ERRNO); } if (flock(sUartLock, LOCK_EX | LOCK_NB) == -1) { - perror("flock"); - exit(OT_EXIT_FAILURE); + DieNowWithMessage("flock", OT_EXIT_ERROR_ERRNO); } memset(&sockname, 0, sizeof(struct sockaddr_un)); @@ -100,8 +98,7 @@ otError otPlatUartEnable(void) if (ret == -1) { - perror("bind"); - exit(OT_EXIT_FAILURE); + DieNowWithMessage("bind", OT_EXIT_ERROR_ERRNO); } // @@ -110,8 +107,7 @@ otError otPlatUartEnable(void) ret = listen(sUartSocket, 1); if (ret == -1) { - perror("listen"); - exit(OT_EXIT_FAILURE); + DieNowWithMessage("listen", OT_EXIT_ERROR_ERRNO); } #endif // OPENTHREAD_ENABLE_POSIX_APP_DAEMON @@ -219,8 +215,7 @@ void platformUartProcess(const fd_set *aReadFdSet, const fd_set *aWriteFdSet, co #if OPENTHREAD_ENABLE_POSIX_APP_DAEMON if (FD_ISSET(sUartSocket, aErrorFdSet)) { - perror("socket error"); - exit(OT_EXIT_FAILURE); + DieNowWithMessage("socket", OT_EXIT_FAILURE); } else if (FD_ISSET(sUartSocket, aReadFdSet)) { @@ -249,14 +244,12 @@ void platformUartProcess(const fd_set *aReadFdSet, const fd_set *aWriteFdSet, co #else // OPENTHREAD_ENABLE_POSIX_APP_DAEMON if (FD_ISSET(STDIN_FILENO, aErrorFdSet)) { - perror("stdin"); - exit(OT_EXIT_FAILURE); + DieNowWithMessage("stdin", OT_EXIT_FAILURE); } if (FD_ISSET(STDOUT_FILENO, aErrorFdSet)) { - perror("stdout"); - exit(OT_EXIT_FAILURE); + DieNowWithMessage("stdout", OT_EXIT_FAILURE); } fd = STDIN_FILENO; @@ -274,16 +267,16 @@ void platformUartProcess(const fd_set *aReadFdSet, const fd_set *aWriteFdSet, co } else if (rval <= 0) { - if (rval != 0) - { - perror("read(UART)"); - } #if OPENTHREAD_ENABLE_POSIX_APP_DAEMON + if (rval < 0) + { + perror("UART read"); + } close(sSessionSocket); sSessionSocket = -1; otEXIT_NOW(); #else - exit(OT_EXIT_FAILURE); + DieNowWithMessage("UART read", (rval < 0) ? OT_EXIT_ERROR_ERRNO : OT_EXIT_FAILURE); #endif } } @@ -298,13 +291,13 @@ void platformUartProcess(const fd_set *aReadFdSet, const fd_set *aWriteFdSet, co if (rval < 0) { - perror("UART write"); #if OPENTHREAD_ENABLE_POSIX_APP_DAEMON + perror("UART write"); close(sSessionSocket); sSessionSocket = -1; otEXIT_NOW(); #else - exit(OT_EXIT_FAILURE); + DieNowWithMessage("UART write", OT_EXIT_ERROR_ERRNO); #endif } diff --git a/src/posix/platform/udp.cpp b/src/posix/platform/udp.cpp index 53abb9233..935e43314 100644 --- a/src/posix/platform/udp.cpp +++ b/src/posix/platform/udp.cpp @@ -388,7 +388,7 @@ void platformUdpInit(const char *aIfName) { if (aIfName == NULL) { - exit(OT_EXIT_INVALID_ARGUMENTS); + DieNow(OT_EXIT_INVALID_ARGUMENTS); } sPlatNetifIndex = if_nametoindex(aIfName);