Skip to content

Commit

Permalink
[simulation] log the exit code when the program exits (openthread#10936)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
zhanglongxia authored Nov 20, 2024
1 parent 8d39758 commit 51169ac
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 105 deletions.
8 changes: 4 additions & 4 deletions examples/platforms/simulation/alarm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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__
Expand All @@ -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__
Expand Down
5 changes: 3 additions & 2 deletions examples/platforms/simulation/ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <openthread/tcat.h>
#include <openthread/platform/ble.h>

#include "lib/platform/exit_code.h"
#include "utils/code_utils.h"

#define PLAT_BLE_MSG_DATA_MAX 2048
Expand Down Expand Up @@ -76,7 +77,7 @@ static void initFds(void)
exit:
if (sFd == -1)
{
exit(EXIT_FAILURE);
DieNow(OT_EXIT_FAILURE);
}
}

Expand Down Expand Up @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions examples/platforms/simulation/infra_if.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <openthread/platform/infra_if.h>

#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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
88 changes: 39 additions & 49 deletions examples/platforms/simulation/mdns_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#include <sys/types.h>

#include "simul_utils.h"
#include "lib/platform/exit_code.h"
#include "utils/code_utils.h"

#define MAX_BUFFER_SIZE 1600
Expand All @@ -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)
Expand All @@ -109,18 +100,18 @@ 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__
{
char nameBuffer[IF_NAMESIZE];
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;
Expand All @@ -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);

Expand All @@ -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));
Expand All @@ -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;
}
Expand All @@ -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)
Expand All @@ -193,18 +184,18 @@ 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__
{
char nameBuffer[IF_NAMESIZE];
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;
Expand All @@ -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);

Expand All @@ -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;
}
Expand All @@ -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)
Expand Down Expand Up @@ -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);
}

{
Expand All @@ -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:
Expand Down Expand Up @@ -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
{
Expand All @@ -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:
Expand Down Expand Up @@ -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);
Expand All @@ -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));
Expand All @@ -468,16 +459,15 @@ 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;
}

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)
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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)
Expand All @@ -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;
}
Expand All @@ -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);
}

//---------------------------------------------------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit 51169ac

Please sign in to comment.