libc/time: Return failed when lock is already held to avoid deadlock

when enable LIBC_LOCALTIME and SYSLOG_TIMESTAMP_LOCALTIME
and zoneinfo/posixrulesi isn't exist

[ EMERG] [ap] backtrace:
[ EMERG] [ap] [20] [<0xc1cc3c4>] arm_switchcontext+0xc/0x10
[ EMERG] [ap] [20] [<0xc1ac67a>] sem_wait+0x5a/0xc0
[ EMERG] [ap] [20] [<0xc1c4588>] tzset.part.0+0x8/0x160
[ EMERG] [ap] [20] [<0xc1c58de>] localtime_r+0xc6/0x114
[ EMERG] [ap] [20] [<0xc1a910a>] clock_gettime+0x16/0xa8
[ EMERG] [ap] [20] [<0xc1b7622>] nx_vsyslog+0x9e/0xc0
[ EMERG] [ap] [20] [<0xc4ba7e0>] romfs_stat+0xa4/0xf4
[ EMERG] [ap] [20] [<0xc4b47bc>] inode_semtake+0x24/0x5c
[ EMERG] [ap] [20] [<0xc1c0102>] bsearch+0x26/0x6c
[ EMERG] [ap] [20] [<0xc1c265a>] vsyslog+0x16/0x24
[ EMERG] [ap] [20] [<0xc1c2676>] syslog+0xe/0x1c
[ EMERG] [ap] [20] [<0xc4bae12>] romfs_open+0xee/0x178  <--- err log
[ EMERG] [ap] [20] [<0xc4b68ae>] file_vopen+0x116/0x144
[ EMERG] [ap] [20] [<0xc4b6ac8>] open+0x20/0x60
[ EMERG] [ap] [20] [<0xc1c3df4>] tzload+0x84/0x810
[ EMERG] [ap] [20] [<0xc1c6ef4>] mm_zalloc+0x10/0x18
[ EMERG] [ap] [20] [<0xc4bb426>] romfs_checkmount+0x22/0x58
[ EMERG] [ap] [20] [<0xc4bad74>] romfs_open+0x50/0x178
[ EMERG] [ap] [20] [<0xc1c392c>] tzparse+0x2c/0x470
[ EMERG] [ap] [20] [<0xc1ac5da>] nxsem_wait_uninterruptible+0x8a/0xd0
[ EMERG] [ap] [20] [<0xc1ac690>] sem_wait+0x70/0xc0
[ EMERG] [ap] [20] [<0xc4b47bc>] inode_semtake+0x24/0x5c
[ EMERG] [ap] [20] [<0xc4b481c>] inode_semgive+0x28/0x58
[ EMERG] [ap] [20] [<0xc4b58b4>] file_close+0x18/0x44
[ EMERG] [ap] [20] [<0xc4b4690>] close+0x80/0xb4
[ EMERG] [ap] [20] [<0xc1c43f4>] tzload+0x684/0x810
[ EMERG] [ap] [20] [<0xc1c4616>] tzset.part.0+0x96/0x160
[ EMERG] [ap] [20] [<0xc1c58de>] localtime_r+0xc6/0x114
[ EMERG] [ap] [20] [<0xc1a910a>] clock_gettime+0x16/0xa8
[ EMERG] [ap] [20] [<0xc1ea692>] cmd_date+0x276/0x37c

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
This commit is contained in:
Jiuzhu Dong 2022-08-12 21:33:46 +08:00 committed by Xiang Xiao
parent 8b84d19771
commit 1b9ed5725b

View File

@ -326,8 +326,8 @@ static int g_lcl_isset;
static int g_gmt_isset;
static FAR struct state_s *g_lcl_ptr;
static FAR struct state_s *g_gmt_ptr;
static mutex_t g_lcl_lock = NXMUTEX_INITIALIZER;
static mutex_t g_gmt_lock = NXMUTEX_INITIALIZER;
static rmutex_t g_lcl_lock = NXRMUTEX_INITIALIZER;
static rmutex_t g_gmt_lock = NXRMUTEX_INITIALIZER;
/* Section 4.12.3 of X3.159-1989 requires that
* Except for the strftime function, these functions [asctime,
@ -1775,7 +1775,8 @@ static FAR struct tm *gmtsub(FAR const time_t *timep,
}
#endif
nxmutex_lock(&g_gmt_lock);
nxrmutex_lock(&g_gmt_lock);
if (!g_gmt_isset)
{
g_gmt_ptr = lib_malloc(sizeof *g_gmt_ptr);
@ -1786,7 +1787,7 @@ static FAR struct tm *gmtsub(FAR const time_t *timep,
}
}
nxmutex_unlock(&g_gmt_lock);
nxrmutex_unlock(&g_gmt_lock);
}
tmp->tm_zone = GMT;
@ -2493,7 +2494,8 @@ void tzset(void)
}
#endif
nxmutex_lock(&g_lcl_lock);
nxrmutex_lock(&g_lcl_lock);
name = getenv("TZ");
if (name == NULL)
{
@ -2545,7 +2547,7 @@ void tzset(void)
tzname:
settzname();
out:
nxmutex_unlock(&g_lcl_lock);
nxrmutex_unlock(&g_lcl_lock);
}
FAR struct tm *localtime(FAR const time_t *timep)