diff --git a/fm-common/sources/fmAPI.cpp b/fm-common/sources/fmAPI.cpp index a52f2f86..5f3613ba 100644 --- a/fm-common/sources/fmAPI.cpp +++ b/fm-common/sources/fmAPI.cpp @@ -1,5 +1,5 @@ // -// Copyright (c) 2017 Wind River Systems, Inc. +// Copyright (c) 2017,2023 Wind River Systems, Inc. // // SPDX-License-Identifier: Apache-2.0 // @@ -106,6 +106,18 @@ static bool dequeue(fm_buff_t &req) { static bool fm_lib_reconnect() { char addr[INET6_ADDRSTRLEN]; + if (m_connected) { + // Check fd is valid + // When fm manager is restarted, it is possible to + // have broken pipe. This checks fd is valid or not. + // If not, set m_connected false, so that it will + // try to connect in the following while loop. + if (!m_client.fd_valid()) { + FM_WARNING_LOG("Invalid file descriptor. Attempting to reconnect..."); + m_connected = false; + } + } + while (!m_connected) { struct addrinfo hints; struct addrinfo *result = NULL, *rp; diff --git a/fm-common/sources/fmSocket.cpp b/fm-common/sources/fmSocket.cpp index e14e62d5..831418c1 100644 --- a/fm-common/sources/fmSocket.cpp +++ b/fm-common/sources/fmSocket.cpp @@ -1,5 +1,5 @@ // -// Copyright (c) 2017 Wind River Systems, Inc. +// Copyright (c) 2017,2023 Wind River Systems, Inc. // // SPDX-License-Identifier: Apache-2.0 // @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -259,6 +260,21 @@ int CFmSocket::select_read(int fd,int timeout, bool &timedout){ return select(&fd,1,NULL,0,timeout,0,timedout); } +bool CFmSocket::fd_valid(){ + struct pollfd pfd = {.fd = m_fd, .events = POLLRDHUP}; + if ((poll(&pfd, 1, 0)) < 0) { + return false; + } else { + if (pfd.revents & POLLRDHUP) { + // broken pipe + FM_ERROR_LOG("A broken pipe error occurred\n"); + return false; + } else { + return true; + } + } +} + bool CFmSocket::recvfrom(void *data, long &len, CFmSockAddr &addr) { socklen_t addr_len = sizeof(addr.address); int l = ::recvfrom(m_fd,data,len,0,addr.get_sockaddr(),&addr_len); diff --git a/fm-common/sources/fmSocket.h b/fm-common/sources/fmSocket.h index f960e548..48a9289b 100644 --- a/fm-common/sources/fmSocket.h +++ b/fm-common/sources/fmSocket.h @@ -1,5 +1,5 @@ // -// Copyright (c) 2014 Wind River Systems, Inc. +// Copyright (c) 2014,2023 Wind River Systems, Inc. // // SPDX-License-Identifier: Apache-2.0 // @@ -67,6 +67,7 @@ public: int get_fd() { return m_fd; } + bool fd_valid(); bool recvfrom(void *data, long &len, CFmSockAddr &addr ); static int select(int *rfd, int rlen, int *wfds, int wlen,int timeout,int timeoutusec, bool &timedout);