From 6edb06e4e0472411200ce2a084a783eaf3faffe3 Mon Sep 17 00:00:00 2001 From: whd <7058128+superwhd@users.noreply.github.com> Date: Thu, 9 Nov 2023 10:20:46 +0800 Subject: [PATCH] [posix] allow OT process to run when infra netif gets lost (#9583) In `ot::Posix::InfraNetif::GetFlags()`, the OT process would die if the infra network interface is removed on the POSIX platform. On Android platform, this is not the desired behavior because the system server may tell the OT process to switch another infra network interface. It's fine for OT process to keep running when the previous infra network interface disappears because later system server will call `otSysSetInfraNetif()` to specify the new infra network interface. --- src/posix/platform/infra_if.cpp | 14 ++++++++++++-- src/posix/platform/openthread-posix-config.h | 11 +++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/posix/platform/infra_if.cpp b/src/posix/platform/infra_if.cpp index b787f4fec..2435fbdbb 100644 --- a/src/posix/platform/infra_if.cpp +++ b/src/posix/platform/infra_if.cpp @@ -289,6 +289,7 @@ uint32_t InfraNetif::GetFlags(void) const { int sock; struct ifreq ifReq; + uint32_t flags = 0; OT_ASSERT(mInfraIfIndex != 0); @@ -299,11 +300,20 @@ uint32_t InfraNetif::GetFlags(void) const static_assert(sizeof(ifReq.ifr_name) >= sizeof(mInfraIfName), "mInfraIfName is not of appropriate size."); strcpy(ifReq.ifr_name, mInfraIfName); - VerifyOrDie(ioctl(sock, SIOCGIFFLAGS, &ifReq) != -1, OT_EXIT_ERROR_ERRNO); + if (ioctl(sock, SIOCGIFFLAGS, &ifReq) == -1) + { +#if OPENTHREAD_POSIX_CONFIG_EXIT_ON_INFRA_NETIF_LOST_ENABLE + otLogCritPlat("The infra link %s may be lost. Exiting.", mInfraIfName); + DieNow(OT_EXIT_ERROR_ERRNO); +#endif + ExitNow(); + } + flags = static_cast(ifReq.ifr_flags); +exit: close(sock); - return static_cast(ifReq.ifr_flags); + return flags; } void InfraNetif::CountAddresses(otSysInfraNetIfAddressCounters &aAddressCounters) const diff --git a/src/posix/platform/openthread-posix-config.h b/src/posix/platform/openthread-posix-config.h index 262d37ca0..65d65d6a2 100644 --- a/src/posix/platform/openthread-posix-config.h +++ b/src/posix/platform/openthread-posix-config.h @@ -441,4 +441,15 @@ #ifdef OPENTHREAD_POSIX_CONFIG_RCP_TIME_SYNC_INTERVAL #error "OPENTHREAD_POSIX_CONFIG_RCP_TIME_SYNC_INTERVAL was replaced by OPENTHREAD_SPINEL_CONFIG_RCP_TIME_SYNC_INTERVAL" #endif + +/** + * @def OPENTHREAD_POSIX_CONFIG_EXIT_ON_INFRA_NETIF_LOST_ENABLE + * + * Define as 1 to let the process exit when the infra network interface is lost on the POSIX platform. + * + */ +#ifndef OPENTHREAD_POSIX_CONFIG_EXIT_ON_INFRA_NETIF_LOST_ENABLE +#define OPENTHREAD_POSIX_CONFIG_EXIT_ON_INFRA_NETIF_LOST_ENABLE 1 +#endif + #endif // OPENTHREAD_PLATFORM_CONFIG_H_