mirror of
https://github.com/espressif/openthread.git
synced 2026-08-08 03:37:46 +00:00
[posix] fix removing daemon socket (#6620)
This commit is contained in:
@@ -49,11 +49,26 @@
|
||||
static_assert(sizeof(OPENTHREAD_POSIX_DAEMON_SOCKET_NAME) < sizeof(sockaddr_un::sun_path),
|
||||
"OpenThread daemon socket name too long!");
|
||||
|
||||
static int sListenSocket = -1;
|
||||
static int sDaemonLock = -1;
|
||||
static int sSessionSocket = -1;
|
||||
namespace {
|
||||
|
||||
static int OutputFormatV(void *aContext, const char *aFormat, va_list aArguments)
|
||||
int sListenSocket = -1;
|
||||
int sDaemonLock = -1;
|
||||
int sSessionSocket = -1;
|
||||
|
||||
typedef char(Filename)[sizeof(sockaddr_un::sun_path)];
|
||||
|
||||
void GetFilename(Filename &aFilename, const char *aPattern)
|
||||
{
|
||||
int rval;
|
||||
|
||||
rval = snprintf(aFilename, sizeof(aFilename), aPattern, gNetifName);
|
||||
if (rval < 0 && static_cast<size_t>(rval) >= sizeof(aFilename))
|
||||
{
|
||||
DieNow(OT_EXIT_INVALID_ARGUMENTS);
|
||||
}
|
||||
}
|
||||
|
||||
int OutputFormatV(void *aContext, const char *aFormat, va_list aArguments)
|
||||
{
|
||||
OT_UNUSED_VARIABLE(aContext);
|
||||
|
||||
@@ -86,7 +101,7 @@ exit:
|
||||
return rval;
|
||||
}
|
||||
|
||||
static void InitializeSessionSocket(void)
|
||||
void InitializeSessionSocket(void)
|
||||
{
|
||||
int newSessionSocket;
|
||||
int rval;
|
||||
@@ -133,6 +148,8 @@ exit:
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void platformDaemonEnable(otInstance *aInstance)
|
||||
{
|
||||
struct sockaddr_un sockname;
|
||||
@@ -151,13 +168,10 @@ void platformDaemonEnable(otInstance *aInstance)
|
||||
{
|
||||
static_assert(sizeof(OPENTHREAD_POSIX_DAEMON_SOCKET_LOCK) == sizeof(OPENTHREAD_POSIX_DAEMON_SOCKET_NAME),
|
||||
"sock and lock file name pattern should have the same length!");
|
||||
char lockfile[sizeof(sockname.sun_path)];
|
||||
Filename lockfile;
|
||||
|
||||
GetFilename(lockfile, OPENTHREAD_POSIX_DAEMON_SOCKET_LOCK);
|
||||
|
||||
ret = snprintf(lockfile, sizeof(lockfile), OPENTHREAD_POSIX_DAEMON_SOCKET_LOCK, gNetifName);
|
||||
if (ret < 0 && static_cast<size_t>(ret) >= sizeof(lockfile))
|
||||
{
|
||||
DieNowWithMessage("lockfile", OT_EXIT_INVALID_ARGUMENTS);
|
||||
}
|
||||
sDaemonLock = open(lockfile, O_CREAT | O_RDONLY | O_CLOEXEC, 0600);
|
||||
}
|
||||
|
||||
@@ -174,11 +188,7 @@ void platformDaemonEnable(otInstance *aInstance)
|
||||
memset(&sockname, 0, sizeof(struct sockaddr_un));
|
||||
|
||||
sockname.sun_family = AF_UNIX;
|
||||
ret = snprintf(sockname.sun_path, sizeof(sockname.sun_path), OPENTHREAD_POSIX_DAEMON_SOCKET_NAME, gNetifName);
|
||||
if (ret < 0 && static_cast<size_t>(ret) >= sizeof(sockname.sun_path))
|
||||
{
|
||||
DieNowWithMessage("sockfile", OT_EXIT_INVALID_ARGUMENTS);
|
||||
}
|
||||
GetFilename(sockname.sun_path, OPENTHREAD_POSIX_DAEMON_SOCKET_NAME);
|
||||
(void)unlink(sockname.sun_path);
|
||||
|
||||
ret = bind(sListenSocket, (const struct sockaddr *)&sockname, sizeof(struct sockaddr_un));
|
||||
@@ -219,8 +229,11 @@ void platformDaemonDisable(void)
|
||||
|
||||
if (gPlatResetReason != OT_PLAT_RESET_REASON_SOFTWARE)
|
||||
{
|
||||
otLogDebgPlat("Removing daemon socket: %s", OPENTHREAD_POSIX_DAEMON_SOCKET_NAME);
|
||||
(void)unlink(OPENTHREAD_POSIX_DAEMON_SOCKET_NAME);
|
||||
Filename sockfile;
|
||||
|
||||
GetFilename(sockfile, OPENTHREAD_POSIX_DAEMON_SOCKET_NAME);
|
||||
otLogDebgPlat("Removing daemon socket: %s", sockfile);
|
||||
(void)unlink(sockfile);
|
||||
}
|
||||
|
||||
if (sDaemonLock != -1)
|
||||
|
||||
Reference in New Issue
Block a user