From 51169ac03220a1e8d7c043f4c1805fb60e0a0c39 Mon Sep 17 00:00:00 2001 From: Zhanglong Xia Date: Wed, 20 Nov 2024 23:22:47 +0800 Subject: [PATCH] [simulation] log the exit code when the program exits (#10936) Sometimes `ot-rcp` will exit without any exit information in the log. It is hard for developers to know what happens on the `ot-rcp` side when the `ot-rcp` exits abnormally. This commit calls functions defined in `lib/platform/exit_code.h` to exit the program and log the exit information. --- examples/platforms/simulation/alarm.c | 8 +- examples/platforms/simulation/ble.c | 5 +- examples/platforms/simulation/infra_if.c | 5 +- examples/platforms/simulation/mdns_socket.c | 88 ++++++++----------- examples/platforms/simulation/radio.c | 3 +- examples/platforms/simulation/simul_utils.c | 78 +++++++++------- examples/platforms/simulation/trel.c | 3 +- examples/platforms/simulation/uart.c | 13 +-- .../simulation/virtual_time/platform-sim.c | 15 ++-- 9 files changed, 113 insertions(+), 105 deletions(-) diff --git a/examples/platforms/simulation/alarm.c b/examples/platforms/simulation/alarm.c index 3a1518064..af3223974 100644 --- a/examples/platforms/simulation/alarm.c +++ b/examples/platforms/simulation/alarm.c @@ -114,7 +114,7 @@ void platformAlarmInit(uint32_t aSpeedUpFactor) if (sigaction(OPENTHREAD_CONFIG_MICRO_TIMER_SIGNAL, &sa, NULL) == -1) { perror("sigaction"); - exit(EXIT_FAILURE); + DieNow(OT_EXIT_ERROR_ERRNO); } struct sigevent sev; @@ -126,7 +126,7 @@ void platformAlarmInit(uint32_t aSpeedUpFactor) if (-1 == timer_create(CLOCK_MONOTONIC, &sev, &sMicroTimer)) { perror("timer_create"); - exit(EXIT_FAILURE); + DieNow(OT_EXIT_ERROR_ERRNO); } } #endif @@ -198,7 +198,7 @@ void otPlatAlarmMicroStartAt(otInstance *aInstance, uint32_t aT0, uint32_t aDt) if (-1 == timer_settime(sMicroTimer, 0, &its, NULL)) { perror("otPlatAlarmMicroStartAt timer_settime()"); - exit(EXIT_FAILURE); + DieNow(OT_EXIT_ERROR_ERRNO); } } #endif // __linux__ @@ -217,7 +217,7 @@ void otPlatAlarmMicroStop(otInstance *aInstance) if (-1 == timer_settime(sMicroTimer, 0, &its, NULL)) { perror("otPlatAlarmMicroStop timer_settime()"); - exit(EXIT_FAILURE); + DieNow(OT_EXIT_ERROR_ERRNO); } } #endif // __linux__ diff --git a/examples/platforms/simulation/ble.c b/examples/platforms/simulation/ble.c index e8f96daa0..b757a5eba 100644 --- a/examples/platforms/simulation/ble.c +++ b/examples/platforms/simulation/ble.c @@ -37,6 +37,7 @@ #include #include +#include "lib/platform/exit_code.h" #include "utils/code_utils.h" #define PLAT_BLE_MSG_DATA_MAX 2048 @@ -76,7 +77,7 @@ static void initFds(void) exit: if (sFd == -1) { - exit(EXIT_FAILURE); + DieNow(OT_EXIT_FAILURE); } } @@ -207,7 +208,7 @@ void platformBleProcess(otInstance *aInstance, const fd_set *aReadFdSet, const f else if (errno != EINTR && errno != EAGAIN) { perror("recvfrom BLE simulation failed"); - exit(EXIT_FAILURE); + DieNow(OT_EXIT_FAILURE); } } exit: diff --git a/examples/platforms/simulation/infra_if.c b/examples/platforms/simulation/infra_if.c index 4b904d04d..ad6d68e42 100644 --- a/examples/platforms/simulation/infra_if.c +++ b/examples/platforms/simulation/infra_if.c @@ -34,6 +34,7 @@ #include #include "simul_utils.h" +#include "lib/platform/exit_code.h" #include "utils/code_utils.h" #if OPENTHREAD_SIMULATION_IMPLEMENT_INFRA_IF && OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE @@ -252,7 +253,7 @@ void platformInfraIfInit(void) if (*endptr != '\0') { fprintf(stderr, "\r\nInvalid PORT_OFFSET: %s\r\n", str); - exit(EXIT_FAILURE); + DieNow(OT_EXIT_FAILURE); } sPortOffset *= (MAX_NETWORK_SIZE + 1); @@ -332,7 +333,7 @@ OT_TOOL_WEAK void otPlatInfraIfRecvIcmp6Nd(otInstance *aInstance, OT_UNUSED_VARIABLE(aBufferLength); fprintf(stderr, "\n\r Weak otPlatInfraIfRecvIcmp6Nd is being used\n\r"); - exit(1); + DieNow(OT_EXIT_FAILURE); } #endif // OPENTHREAD_SIMULATION_IMPLEMENT_INFRA_IF && OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE diff --git a/examples/platforms/simulation/mdns_socket.c b/examples/platforms/simulation/mdns_socket.c index 88abcb50c..fc88c3fe3 100644 --- a/examples/platforms/simulation/mdns_socket.c +++ b/examples/platforms/simulation/mdns_socket.c @@ -52,6 +52,7 @@ #include #include "simul_utils.h" +#include "lib/platform/exit_code.h" #include "utils/code_utils.h" #define MAX_BUFFER_SIZE 1600 @@ -76,26 +77,16 @@ static int sMdnsFd6 = -1; #endif #endif -#define VerifyOrDie(aCondition, aErrMsg) \ - do \ - { \ - if (!(aCondition)) \ - { \ - fprintf(stderr, "\n\r" aErrMsg ". errono:%s\n\r", strerror(errno)); \ - exit(1); \ - } \ - } while (false) - static void SetReuseAddrPort(int aFd) { int ret; int yes = 1; ret = setsockopt(aFd, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(yes)); - VerifyOrDie(ret >= 0, "setsocketopt(SO_REUSEADDR) failed"); + VerifyOrDie(ret >= 0, OT_EXIT_FAILURE); ret = setsockopt(aFd, SOL_SOCKET, SO_REUSEPORT, (char *)&yes, sizeof(yes)); - VerifyOrDie(ret >= 0, "setsocketopt(SO_REUSEPORT) failed"); + VerifyOrDie(ret >= 0, OT_EXIT_FAILURE); } static void OpenIp4Socket(uint32_t aInfraIfIndex) @@ -109,7 +100,7 @@ static void OpenIp4Socket(uint32_t aInfraIfIndex) int value; fd = socket(AF_INET, SOCK_DGRAM, 0); - VerifyOrDie(fd >= 0, "socket() failed"); + VerifyOrDie(fd >= 0, OT_EXIT_FAILURE); #ifdef __linux__ { @@ -117,10 +108,10 @@ static void OpenIp4Socket(uint32_t aInfraIfIndex) const char *ifname; ifname = if_indextoname(aInfraIfIndex, nameBuffer); - VerifyOrDie(ifname != NULL, "if_indextoname() failed"); + VerifyOrDie(ifname != NULL, OT_EXIT_ERROR_ERRNO); ret = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen(ifname)); - VerifyOrDie(ret >= 0, "setsocketopt(SO_BINDTODEVICE) failed"); + VerifyOrDie(ret >= 0, OT_EXIT_ERROR_ERRNO); } #else value = aInfraIfIndex; @@ -129,15 +120,15 @@ static void OpenIp4Socket(uint32_t aInfraIfIndex) u8 = 255; ret = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &u8, sizeof(u8)); - VerifyOrDie(ret >= 0, "setsocketopt(IP_MULTICAST_TTL) failed"); + VerifyOrDie(ret >= 0, OT_EXIT_ERROR_ERRNO); value = 255; ret = setsockopt(fd, IPPROTO_IP, IP_TTL, &value, sizeof(value)); - VerifyOrDie(ret >= 0, "setsocketopt(IP_TTL) failed"); + VerifyOrDie(ret >= 0, OT_EXIT_ERROR_ERRNO); u8 = 1; ret = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &u8, sizeof(u8)); - VerifyOrDie(ret >= 0, "setsocketopt(IP_MULTICAST_LOOP) failed"); + VerifyOrDie(ret >= 0, OT_EXIT_ERROR_ERRNO); SetReuseAddrPort(fd); @@ -149,7 +140,7 @@ static void OpenIp4Socket(uint32_t aInfraIfIndex) mreqn.imr_ifindex = aInfraIfIndex; ret = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &mreqn, sizeof(mreqn)); - VerifyOrDie(ret >= 0, "setsocketopt(IP_MULTICAST_IF) failed"); + VerifyOrDie(ret >= 0, OT_EXIT_ERROR_ERRNO); } memset(&addr, 0, sizeof(addr)); @@ -158,7 +149,7 @@ static void OpenIp4Socket(uint32_t aInfraIfIndex) addr.sin_port = htons(MDNS_PORT); ret = bind(fd, (struct sockaddr *)&addr, sizeof(addr)); - VerifyOrDie(ret >= 0, "bind() failed"); + VerifyOrDie(ret >= 0, OT_EXIT_ERROR_ERRNO); sMdnsFd4 = fd; } @@ -180,7 +171,7 @@ static void JoinOrLeaveIp4MulticastGroup(bool aJoin, uint32_t aInfraIfIndex) } ret = setsockopt(sMdnsFd4, IPPROTO_IP, aJoin ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP, &mreqn, sizeof(mreqn)); - VerifyOrDie(ret >= 0, "setsocketopt(IP_ADD/DROP_MEMBERSHIP) failed"); + VerifyOrDie(ret >= 0, OT_EXIT_ERROR_ERRNO); } static void OpenIp6Socket(uint32_t aInfraIfIndex) @@ -193,7 +184,7 @@ static void OpenIp6Socket(uint32_t aInfraIfIndex) int value; fd = socket(AF_INET6, SOCK_DGRAM, 0); - VerifyOrDie(fd >= 0, "socket() failed"); + VerifyOrDie(fd >= 0, OT_EXIT_ERROR_ERRNO); #ifdef __linux__ { @@ -201,10 +192,10 @@ static void OpenIp6Socket(uint32_t aInfraIfIndex) const char *ifname; ifname = if_indextoname(aInfraIfIndex, nameBuffer); - VerifyOrDie(ifname != NULL, "if_indextoname() failed"); + VerifyOrDie(ifname != NULL, OT_EXIT_ERROR_ERRNO); ret = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen(ifname)); - VerifyOrDie(ret >= 0, "setsocketopt(SO_BINDTODEVICE) failed"); + VerifyOrDie(ret >= 0, OT_EXIT_ERROR_ERRNO); } #else value = aInfraIfIndex; @@ -213,23 +204,23 @@ static void OpenIp6Socket(uint32_t aInfraIfIndex) value = 255; ret = setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &value, sizeof(value)); - VerifyOrDie(ret >= 0, "setsocketopt(IPV6_MULTICAST_HOPS) failed"); + VerifyOrDie(ret >= 0, OT_EXIT_ERROR_ERRNO); value = 255; ret = setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &value, sizeof(value)); - VerifyOrDie(ret >= 0, "setsocketopt(IPV6_UNICAST_HOPS) failed"); + VerifyOrDie(ret >= 0, OT_EXIT_ERROR_ERRNO); value = 1; ret = setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &value, sizeof(value)); - VerifyOrDie(ret >= 0, "setsocketopt(IPV6_V6ONLY) failed"); + VerifyOrDie(ret >= 0, OT_EXIT_ERROR_ERRNO); value = aInfraIfIndex; ret = setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &value, sizeof(value)); - VerifyOrDie(ret >= 0, "setsocketopt(IPV6_MULTICAST_IF) failed"); + VerifyOrDie(ret >= 0, OT_EXIT_ERROR_ERRNO); value = 1; ret = setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &value, sizeof(value)); - VerifyOrDie(ret >= 0, "setsocketopt(IPV6_MULTICAST_LOOP) failed"); + VerifyOrDie(ret >= 0, OT_EXIT_ERROR_ERRNO); SetReuseAddrPort(fd); @@ -238,7 +229,7 @@ static void OpenIp6Socket(uint32_t aInfraIfIndex) addr6.sin6_port = htons(MDNS_PORT); ret = bind(fd, (struct sockaddr *)&addr6, sizeof(addr6)); - VerifyOrDie(ret >= 0, "bind() failed"); + VerifyOrDie(ret >= 0, OT_EXIT_ERROR_ERRNO); sMdnsFd6 = fd; } @@ -261,7 +252,7 @@ static void JoinOrLeaveIp6MulticastGroup(bool aJoin, uint32_t aInfraIfIndex) } ret = setsockopt(sMdnsFd6, IPPROTO_IPV6, aJoin ? IPV6_ADD_MEMBERSHIP : IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)); - VerifyOrDie(ret >= 0, "setsocketopt(IP6_ADD/DROP_MEMBERSHIP) failed"); + VerifyOrDie(ret >= 0, OT_EXIT_ERROR_ERRNO); } otError otPlatMdnsSetListeningEnabled(otInstance *aInstance, bool aEnable, uint32_t aInfraIfIndex) @@ -319,7 +310,7 @@ void otPlatMdnsSendMulticast(otInstance *aInstance, otMessage *aMessage, uint32_ bytes = sendto(sMdnsFd4, buffer, length, 0, (struct sockaddr *)&addr, sizeof(addr)); - VerifyOrDie((bytes == length), "sendTo(sMdnsFd4) failed"); + VerifyOrDie(bytes == length, OT_EXIT_ERROR_ERRNO); } { @@ -332,7 +323,7 @@ void otPlatMdnsSendMulticast(otInstance *aInstance, otMessage *aMessage, uint32_ bytes = sendto(sMdnsFd6, buffer, length, 0, (struct sockaddr *)&addr6, sizeof(addr6)); - VerifyOrDie((bytes == length), "sendTo(sMdnsFd6) failed"); + VerifyOrDie(bytes == length, OT_EXIT_ERROR_ERRNO); } exit: @@ -364,7 +355,7 @@ void otPlatMdnsSendUnicast(otInstance *aInstance, otMessage *aMessage, const otP bytes = sendto(sMdnsFd4, buffer, length, 0, (struct sockaddr *)&addr, sizeof(addr)); - VerifyOrDie((bytes == length), "sendTo(sMdnsFd4) failed"); + VerifyOrDie(bytes == length, OT_EXIT_ERROR_ERRNO); } else { @@ -377,7 +368,7 @@ void otPlatMdnsSendUnicast(otInstance *aInstance, otMessage *aMessage, const otP bytes = sendto(sMdnsFd6, buffer, length, 0, (struct sockaddr *)&addr6, sizeof(addr6)); - VerifyOrDie((bytes == length), "sendTo(sMdnsFd6) failed"); + VerifyOrDie(bytes == length, OT_EXIT_ERROR_ERRNO); } exit: @@ -411,12 +402,12 @@ void platformMdnsSocketProcess(otInstance *aInstance, const fd_set *aReadFdSet) memset(&sockaddr, 0, sizeof(sockaddr)); rval = recvfrom(sMdnsFd4, (char *)&buffer, sizeof(buffer), 0, (struct sockaddr *)&sockaddr, &len); - VerifyOrDie(rval >= 0, "recvfrom() failed"); + VerifyOrDie(rval >= 0, OT_EXIT_ERROR_ERRNO); message = otIp6NewMessage(aInstance, NULL); - VerifyOrDie(message != NULL, "otIp6NewMessage() failed"); + VerifyOrDie(message != NULL, OT_EXIT_FAILURE); - VerifyOrDie(otMessageAppend(message, buffer, (uint16_t)rval) == OT_ERROR_NONE, "otMessageAppend() failed"); + VerifyOrDie(otMessageAppend(message, buffer, (uint16_t)rval) == OT_ERROR_NONE, OT_EXIT_FAILURE); memset(&addrInfo, 0, sizeof(addrInfo)); otIp4ToIp4MappedIp6Address((otIp4Address *)(&sockaddr.sin_addr.s_addr), &addrInfo.mAddress); @@ -437,12 +428,12 @@ void platformMdnsSocketProcess(otInstance *aInstance, const fd_set *aReadFdSet) memset(&sockaddr6, 0, sizeof(sockaddr6)); rval = recvfrom(sMdnsFd6, (char *)&buffer, sizeof(buffer), 0, (struct sockaddr *)&sockaddr6, &len); - VerifyOrDie(rval >= 0, "recvfrom(sMdnsFd6) failed"); + VerifyOrDie(rval >= 0, OT_EXIT_ERROR_ERRNO); message = otIp6NewMessage(aInstance, NULL); - VerifyOrDie(message != NULL, "otIp6NewMessage() failed"); + VerifyOrDie(message != NULL, OT_EXIT_FAILURE); - VerifyOrDie(otMessageAppend(message, buffer, (uint16_t)rval) == OT_ERROR_NONE, "otMessageAppend() failed"); + VerifyOrDie(otMessageAppend(message, buffer, (uint16_t)rval) == OT_ERROR_NONE, OT_EXIT_FAILURE); memset(&addrInfo, 0, sizeof(addrInfo)); memcpy(&addrInfo.mAddress, &sockaddr6.sin6_addr, sizeof(otIp6Address)); @@ -468,8 +459,7 @@ OT_TOOL_WEAK uint16_t otMessageRead(const otMessage *aMessage, uint16_t aOffset, OT_UNUSED_VARIABLE(aLength); fprintf(stderr, "\n\rWeak otMessageRead() is incorrectly used\n\r"); - exit(1); - + DieNow(OT_EXIT_FAILURE); return 0; } @@ -477,7 +467,7 @@ OT_TOOL_WEAK void otMessageFree(otMessage *aMessage) { OT_UNUSED_VARIABLE(aMessage); fprintf(stderr, "\n\rWeak otMessageFree() is incorrectly used\n\r"); - exit(1); + DieNow(OT_EXIT_FAILURE); } OT_TOOL_WEAK otMessage *otIp6NewMessage(otInstance *aInstance, const otMessageSettings *aSettings) @@ -486,7 +476,7 @@ OT_TOOL_WEAK otMessage *otIp6NewMessage(otInstance *aInstance, const otMessageSe OT_UNUSED_VARIABLE(aSettings); fprintf(stderr, "\n\rWeak otIp6NewMessage() is incorrectly used\n\r"); - exit(1); + DieNow(OT_EXIT_FAILURE); return NULL; } @@ -498,7 +488,7 @@ OT_TOOL_WEAK otError otMessageAppend(otMessage *aMessage, const void *aBuf, uint OT_UNUSED_VARIABLE(aLength); fprintf(stderr, "\n\rWeak otMessageFree() is incorrectly used\n\r"); - exit(1); + DieNow(OT_EXIT_FAILURE); return OT_ERROR_NOT_IMPLEMENTED; } @@ -509,7 +499,7 @@ OT_TOOL_WEAK void otIp4ToIp4MappedIp6Address(const otIp4Address *aIp4Address, ot OT_UNUSED_VARIABLE(aIp6Address); fprintf(stderr, "\n\rWeak otIp4ToIp4MappedIp6Address() is incorrectly used\n\r"); - exit(1); + DieNow(OT_EXIT_FAILURE); } OT_TOOL_WEAK otError otIp4FromIp4MappedIp6Address(const otIp6Address *aIp6Address, otIp4Address *aIp4Address) @@ -518,7 +508,7 @@ OT_TOOL_WEAK otError otIp4FromIp4MappedIp6Address(const otIp6Address *aIp6Addres OT_UNUSED_VARIABLE(aIp4Address); fprintf(stderr, "\n\rWeak otIp4FromIp4MappedIp6Address() is incorrectly used\n\r"); - exit(1); + DieNow(OT_EXIT_FAILURE); return OT_ERROR_NOT_IMPLEMENTED; } @@ -534,7 +524,7 @@ OT_TOOL_WEAK void otPlatMdnsHandleReceive(otInstance *aInstance OT_UNUSED_VARIABLE(aAddress); fprintf(stderr, "\n\rWeak otPlatMdnsHandleReceive() is incorrectly used\n\r"); - exit(1); + DieNow(OT_EXIT_FAILURE); } //--------------------------------------------------------------------------------------------------------------------- diff --git a/examples/platforms/simulation/radio.c b/examples/platforms/simulation/radio.c index acf4bc029..6f82210b1 100644 --- a/examples/platforms/simulation/radio.c +++ b/examples/platforms/simulation/radio.c @@ -42,6 +42,7 @@ #include #include "simul_utils.h" +#include "lib/platform/exit_code.h" #include "utils/code_utils.h" #include "utils/link_metrics.h" #include "utils/mac_frame.h" @@ -1231,7 +1232,7 @@ void parseFromEnvAsUint16(const char *aEnvName, uint16_t *aValue) if (*endptr != '\0') { fprintf(stderr, "Invalid %s: %s\n", aEnvName, env); - exit(EXIT_FAILURE); + DieNow(OT_EXIT_FAILURE); } } } diff --git a/examples/platforms/simulation/simul_utils.c b/examples/platforms/simulation/simul_utils.c index 43a6ac42d..2099f1b95 100644 --- a/examples/platforms/simulation/simul_utils.c +++ b/examples/platforms/simulation/simul_utils.c @@ -33,8 +33,20 @@ #include #include +#include "lib/platform/exit_code.h" #include "utils/code_utils.h" +#define ExpectOrExitWithErrorMsg(aCondition, aErrorMsg) \ + do \ + { \ + if (!(aCondition)) \ + { \ + perror(aErrorMsg); \ + otLogWarnPlat("%s: %s", aErrorMsg, strerror(errno)); \ + goto exit; \ + } \ + } while (false) + #define UTILS_SOCKET_LOCAL_HOST_ADDR "127.0.0.1" #define UTILS_SOCKET_GROUP_ADDR "224.0.0.116" #define UTILS_SOCKET_GROUP_ADDR6 "ff02::116" @@ -71,13 +83,13 @@ static void InitRxSocket(utilsSocket *aSocket, const struct in_addr *aIp4Address int rval; fd = socket(aIp4Address ? AF_INET : AF_INET6, SOCK_DGRAM, IPPROTO_UDP); - otEXPECT_ACTION(fd != -1, perror("socket(RxFd)")); + ExpectOrExitWithErrorMsg(fd != -1, "socket(RxFd)"); rval = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); - otEXPECT_ACTION(rval != -1, perror("setsockopt(RxFd, SO_REUSEADDR)")); + ExpectOrExitWithErrorMsg(rval != -1, "setsockopt(RxFd, SO_REUSEADDR)"); rval = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)); - otEXPECT_ACTION(rval != -1, perror("setsockopt(RxFd, SO_REUSEPORT)")); + ExpectOrExitWithErrorMsg(rval != -1, "setsockopt(RxFd, SO_REUSEPORT)"); if (aIp4Address) { @@ -85,22 +97,23 @@ static void InitRxSocket(utilsSocket *aSocket, const struct in_addr *aIp4Address struct sockaddr_in *sockaddr = &aSocket->mGroupAddr.mSockAddr4; rval = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, aIp4Address, sizeof(*aIp4Address)); - otEXPECT_ACTION(rval != -1, perror("setsockopt(RxFd, IP_MULTICAST_IF)")); + ExpectOrExitWithErrorMsg(rval != -1, "setsockopt(RxFd, IP_MULTICAST_IF)"); memset(sockaddr, 0, sizeof(*sockaddr)); sockaddr->sin_family = AF_INET; sockaddr->sin_port = htons(aSocket->mPortBase); - otEXPECT_ACTION(inet_pton(AF_INET, UTILS_SOCKET_GROUP_ADDR, &sockaddr->sin_addr), perror("inet_pton(AF_INET)")); + ExpectOrExitWithErrorMsg(inet_pton(AF_INET, UTILS_SOCKET_GROUP_ADDR, &sockaddr->sin_addr), + "inet_pton(AF_INET)"); memset(&mreq, 0, sizeof(mreq)); mreq.imr_multiaddr = sockaddr->sin_addr; mreq.imr_address = *aIp4Address; // This address is used to identify the network interface rval = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)); - otEXPECT_ACTION(rval != -1, perror("setsockopt(RxFd, IP_ADD_MEMBERSHIP)")); + ExpectOrExitWithErrorMsg(rval != -1, "setsockopt(RxFd, IP_ADD_MEMBERSHIP)"); rval = bind(fd, (struct sockaddr *)sockaddr, sizeof(*sockaddr)); - otEXPECT_ACTION(rval != -1, perror("bind(RxFd)")); + ExpectOrExitWithErrorMsg(rval != -1, "bind(RxFd)"); } else { @@ -108,24 +121,24 @@ static void InitRxSocket(utilsSocket *aSocket, const struct in_addr *aIp4Address struct sockaddr_in6 *sockaddr = &aSocket->mGroupAddr.mSockAddr6; rval = setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &aIfIndex, sizeof(aIfIndex)); - otEXPECT_ACTION(rval != -1, perror("setsockopt(RxFd, IPV6_MULTICAST_IF)")); + ExpectOrExitWithErrorMsg(rval != -1, "setsockopt(RxFd, IPV6_MULTICAST_IF)"); memset(sockaddr, 0, sizeof(*sockaddr)); sockaddr->sin6_family = AF_INET6; sockaddr->sin6_port = htons(aSocket->mPortBase); sockaddr->sin6_scope_id = aIfIndex; // This specifies network interface for link local scope - otEXPECT_ACTION(inet_pton(AF_INET6, UTILS_SOCKET_GROUP_ADDR6, &sockaddr->sin6_addr), - perror("inet_pton(AF_INET6)")); + ExpectOrExitWithErrorMsg(inet_pton(AF_INET6, UTILS_SOCKET_GROUP_ADDR6, &sockaddr->sin6_addr), + "inet_pton(AF_INET6)"); memset(&mreq, 0, sizeof(mreq)); mreq.ipv6mr_multiaddr = sockaddr->sin6_addr; mreq.ipv6mr_interface = aIfIndex; rval = setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq, sizeof(mreq)); - otEXPECT_ACTION(rval != -1, perror("setsockopt(RxFd, IPV6_JOIN_GROUP)")); + ExpectOrExitWithErrorMsg(rval != -1, "setsockopt(RxFd, IPV6_JOIN_GROUP)"); rval = bind(fd, (struct sockaddr *)sockaddr, sizeof(*sockaddr)); - otEXPECT_ACTION(rval != -1, perror("bind(RxFd)")); + ExpectOrExitWithErrorMsg(rval != -1, "bind(RxFd)"); } aSocket->mRxFd = fd; @@ -133,7 +146,7 @@ static void InitRxSocket(utilsSocket *aSocket, const struct in_addr *aIp4Address exit: if (aSocket->mRxFd == -1) { - exit(EXIT_FAILURE); + DieNow(OT_EXIT_FAILURE); } } @@ -145,7 +158,7 @@ void InitTxSocketIp6(utilsSocket *aSocket, const struct in6_addr *aAddress, unsi struct sockaddr_in6 sockaddr; fd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); - otEXPECT_ACTION(fd != -1, perror("socket(TxFd)")); + ExpectOrExitWithErrorMsg(fd != -1, "socket(TxFd)"); memset(&sockaddr, 0, sizeof(sockaddr)); sockaddr.sin6_family = AF_INET6; @@ -157,20 +170,20 @@ void InitTxSocketIp6(utilsSocket *aSocket, const struct in6_addr *aAddress, unsi } rval = setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &aIfIndex, sizeof(aIfIndex)); - otEXPECT_ACTION(rval != -1, perror("setsockopt(TxFd, IPV6_MULTICAST_IF)")); + ExpectOrExitWithErrorMsg(rval != -1, "setsockopt(TxFd, IPV6_MULTICAST_IF)"); rval = setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &one, sizeof(one)); - otEXPECT_ACTION(rval != -1, perror("setsockopt(TxFd, IPV6_MULTICAST_LOOP)")); + ExpectOrExitWithErrorMsg(rval != -1, "setsockopt(TxFd, IPV6_MULTICAST_LOOP)"); rval = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); - otEXPECT_ACTION(rval != -1, perror("bind(TxFd)")); + ExpectOrExitWithErrorMsg(rval != -1, "bind(TxFd)"); aSocket->mTxFd = fd; exit: if (aSocket->mTxFd == -1) { - exit(EXIT_FAILURE); + DieNow(OT_EXIT_FAILURE); } } @@ -185,7 +198,7 @@ static void InitTxSocketIp4(utilsSocket *aSocket, const struct in_addr *aAddress // Prepare `mTxFd` fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - otEXPECT_ACTION(fd != -1, perror("socket(TxFd)")); + ExpectOrExitWithErrorMsg(fd != -1, "socket(TxFd)"); memset(&sockaddr, 0, sizeof(sockaddr)); sockaddr.sin_family = AF_INET; @@ -193,20 +206,20 @@ static void InitTxSocketIp4(utilsSocket *aSocket, const struct in_addr *aAddress sockaddr.sin_addr = *aAddress; rval = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &sockaddr.sin_addr, sizeof(sockaddr.sin_addr)); - otEXPECT_ACTION(rval != -1, perror("setsockopt(TxFd, IP_MULTICAST_IF)")); + ExpectOrExitWithErrorMsg(rval != -1, "setsockopt(TxFd, IP_MULTICAST_IF)"); rval = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &one, sizeof(one)); - otEXPECT_ACTION(rval != -1, perror("setsockopt(TxFd, IP_MULTICAST_LOOP)")); + ExpectOrExitWithErrorMsg(rval != -1, "setsockopt(TxFd, IP_MULTICAST_LOOP)"); rval = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); - otEXPECT_ACTION(rval != -1, perror("bind(TxFd)")); + ExpectOrExitWithErrorMsg(rval != -1, "bind(TxFd)"); aSocket->mTxFd = fd; exit: if (aSocket->mTxFd == -1) { - exit(EXIT_FAILURE); + DieNow(OT_EXIT_FAILURE); } } @@ -222,8 +235,7 @@ static bool TryInitSocketIfname(utilsSocket *aSocket, const char *aLocalInterfac if (getifaddrs(&ifaddr) == -1) { - perror("getifaddrs"); - exit(EXIT_FAILURE); + DieNow(OT_EXIT_ERROR_ERRNO); } for (struct ifaddrs *ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) @@ -268,7 +280,7 @@ static bool TryInitSocketIfname(utilsSocket *aSocket, const char *aLocalInterfac else { fprintf(stderr, "No sock address for TX socket!\n"); - exit(EXIT_FAILURE); + DieNow(OT_EXIT_FAILURE); } InitRxSocket(aSocket, (addr6 ? NULL : addr4), ifIndex); @@ -284,7 +296,7 @@ static bool TryInitSocketIp4(utilsSocket *aSocket, const char *aLocalInterface) { struct in_addr addr4; - otEXPECT(inet_pton(AF_INET, aLocalInterface, &addr4)); + ExpectOrExitWithErrorMsg(inet_pton(AF_INET, aLocalInterface, &addr4), "inet_pton(AF_INET)"); InitTxSocketIp4(aSocket, &addr4); InitRxSocket(aSocket, &addr4, 0); @@ -300,12 +312,12 @@ static bool TryInitSocketIp6(utilsSocket *aSocket, const char *aLocalInterface) struct in6_addr addr6; struct ifaddrs *ifaddr = NULL; - otEXPECT(inet_pton(AF_INET6, aLocalInterface, &addr6)); + ExpectOrExitWithErrorMsg(inet_pton(AF_INET6, aLocalInterface, &addr6), "inet_pton(AF_INET6)"); if (getifaddrs(&ifaddr) == -1) { perror("getifaddrs"); - exit(EXIT_FAILURE); + DieNow(OT_EXIT_ERROR_ERRNO); } for (struct ifaddrs *ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) @@ -328,7 +340,7 @@ static bool TryInitSocketIp6(utilsSocket *aSocket, const char *aLocalInterface) if (ifIndex == 0) { perror("if_nametoindex"); - exit(EXIT_FAILURE); + DieNow(OT_EXIT_ERROR_ERRNO); } InitTxSocketIp6(aSocket, &addr6, ifIndex); @@ -355,7 +367,7 @@ void utilsInitSocket(utilsSocket *aSocket, uint16_t aPortBase) !TryInitSocketIp6(aSocket, gLocalInterface)) { fprintf(stderr, "Failed to simulate node %d on %s\n", gNodeId, gLocalInterface); - exit(EXIT_FAILURE); + DieNow(OT_EXIT_FAILURE); } } @@ -433,7 +445,7 @@ uint16_t utilsReceiveFromSocket(const utilsSocket *aSocket, else if (errno != EINTR && errno != EAGAIN) { perror("recvfrom(RxFd)"); - exit(EXIT_FAILURE); + DieNow(OT_EXIT_ERROR_ERRNO); } return len; @@ -450,6 +462,6 @@ void utilsSendOverSocket(const utilsSocket *aSocket, const void *aBuffer, uint16 if (rval < 0) { perror("sendto(sTxFd)"); - exit(EXIT_FAILURE); + DieNow(OT_EXIT_ERROR_ERRNO); } } diff --git a/examples/platforms/simulation/trel.c b/examples/platforms/simulation/trel.c index 696b1a59c..00ea9e4f9 100644 --- a/examples/platforms/simulation/trel.c +++ b/examples/platforms/simulation/trel.c @@ -32,6 +32,7 @@ #include #include "simul_utils.h" +#include "lib/platform/exit_code.h" #include "utils/code_utils.h" #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE @@ -316,7 +317,7 @@ void platformTrelInit(uint32_t aSpeedUpFactor) if (*endptr != '\0') { fprintf(stderr, "\r\nInvalid PORT_OFFSET: %s\r\n", str); - exit(EXIT_FAILURE); + DieNow(OT_EXIT_FAILURE); } sPortOffset *= (MAX_NETWORK_SIZE + 1); diff --git a/examples/platforms/simulation/uart.c b/examples/platforms/simulation/uart.c index 70d387118..79ca8732d 100644 --- a/examples/platforms/simulation/uart.c +++ b/examples/platforms/simulation/uart.c @@ -41,6 +41,7 @@ #include #include "simul_utils.h" +#include "lib/platform/exit_code.h" #include "utils/code_utils.h" #include "utils/uart.h" @@ -203,7 +204,7 @@ otError otPlatUartFlush(void) else { perror("write(UART)"); - exit(EXIT_FAILURE); + DieNow(OT_EXIT_ERROR_ERRNO); } exit: @@ -226,7 +227,7 @@ void platformUartProcess(void) if (rval < 0) { perror("poll"); - exit(EXIT_FAILURE); + DieNow(OT_EXIT_ERROR_ERRNO); } if (rval > 0) @@ -234,13 +235,13 @@ void platformUartProcess(void) if ((pollfd[0].revents & error_flags) != 0) { perror("s_in_fd"); - exit(EXIT_FAILURE); + DieNow(OT_EXIT_ERROR_ERRNO); } if ((pollfd[1].revents & error_flags) != 0) { perror("s_out_fd"); - exit(EXIT_FAILURE); + DieNow(OT_EXIT_ERROR_ERRNO); } if (pollfd[0].revents & POLLIN) @@ -250,7 +251,7 @@ void platformUartProcess(void) if (rval <= 0) { perror("read"); - exit(EXIT_FAILURE); + DieNow(OT_EXIT_ERROR_ERRNO); } otPlatUartReceived(s_receive_buffer, (uint16_t)rval); @@ -273,7 +274,7 @@ void platformUartProcess(void) else if (errno != EINTR) { perror("write"); - exit(EXIT_FAILURE); + DieNow(OT_EXIT_ERROR_ERRNO); } } } diff --git a/examples/platforms/simulation/virtual_time/platform-sim.c b/examples/platforms/simulation/virtual_time/platform-sim.c index 1c2c81894..ba475f077 100644 --- a/examples/platforms/simulation/virtual_time/platform-sim.c +++ b/examples/platforms/simulation/virtual_time/platform-sim.c @@ -49,6 +49,7 @@ #include #include +#include "lib/platform/exit_code.h" #include "utils/uart.h" uint32_t gNodeId = 1; @@ -87,7 +88,7 @@ void otSimSendEvent(const struct Event *aEvent) if (rval < 0) { perror("sendto"); - exit(EXIT_FAILURE); + DieNow(OT_EXIT_ERROR_ERRNO); } } @@ -99,7 +100,7 @@ static void receiveEvent(otInstance *aInstance) if (rval < 0 || (uint16_t)rval < offsetof(struct Event, mData)) { perror("recvfrom"); - exit(EXIT_FAILURE); + DieNow(OT_EXIT_ERROR_ERRNO); } platformAlarmAdvanceNow(event.mDelay); @@ -182,13 +183,13 @@ static void socket_init(void) if (sSockFd == -1) { perror("socket"); - exit(EXIT_FAILURE); + DieNow(OT_EXIT_ERROR_ERRNO); } if (bind(sSockFd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) == -1) { perror("bind"); - exit(EXIT_FAILURE); + DieNow(OT_EXIT_ERROR_ERRNO); } } @@ -204,7 +205,7 @@ void otSysInit(int argc, char *argv[]) if (argc != 2) { - exit(EXIT_FAILURE); + DieNow(OT_EXIT_FAILURE); } openlog(basename(argv[0]), LOG_PID, LOG_USER); @@ -218,7 +219,7 @@ void otSysInit(int argc, char *argv[]) if (*endptr != '\0' || gNodeId < 1 || gNodeId > MAX_NETWORK_SIZE) { fprintf(stderr, "Invalid NodeId: %s\n", argv[1]); - exit(EXIT_FAILURE); + DieNow(OT_EXIT_FAILURE); } socket_init(); @@ -268,7 +269,7 @@ void otSysProcessDrivers(otInstance *aInstance) if ((rval < 0) && (errno != EINTR)) { perror("select"); - exit(EXIT_FAILURE); + DieNow(OT_EXIT_ERROR_ERRNO); } if (rval > 0 && FD_ISSET(sSockFd, &read_fds))