syslog: fix crash when print localtime by syslog

Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
This commit is contained in:
Jiuzhu Dong 2021-06-21 11:32:32 +08:00 committed by Xiang Xiao
parent aa43a0215d
commit a75b712e77
1 changed files with 44 additions and 50 deletions

View File

@ -67,53 +67,22 @@ static FAR const char * g_priority_str[] =
int nx_vsyslog(int priority, FAR const IPTR char *fmt, FAR va_list *ap)
{
struct lib_syslogstream_s stream;
int ret;
int ret = 0;
#if CONFIG_TASK_NAME_SIZE > 0 && defined(CONFIG_SYSLOG_PROCESS_NAME)
struct tcb_s *tcb;
#endif
#ifdef CONFIG_SYSLOG_TIMESTAMP
struct timespec ts =
{
};
#if defined(CONFIG_SYSLOG_TIMESTAMP_FORMATTED)
time_t time;
struct tm tm;
struct tm tm =
{
};
char date_buf[CONFIG_SYSLOG_TIMESTAMP_BUFFER];
#endif
#ifdef CONFIG_SYSLOG_TIMESTAMP
struct timespec ts;
/* Get the current time. Since debug output may be generated very early
* in the start-up sequence, hardware timer support may not yet be
* available.
*/
ret = -EAGAIN;
if (OSINIT_HW_READY())
{
#if defined(CONFIG_SYSLOG_TIMESTAMP_REALTIME)
/* Use CLOCK_REALTIME if so configured */
ret = clock_gettime(CLOCK_REALTIME, &ts);
#elif defined(CONFIG_CLOCK_MONOTONIC)
/* Prefer monotonic when enabled, as it can be synchronized to
* RTC with clock_resynchronize.
*/
ret = clock_gettime(CLOCK_MONOTONIC, &ts);
#else
/* Otherwise, fall back to the system timer */
ret = clock_systime_timespec(&ts);
#endif
}
if (ret < 0)
{
/* Timer hardware is not available, or clock function failed */
ts.tv_sec = 0;
ts.tv_nsec = 0;
}
#endif
/* Wrap the low-level output in a stream object and let lib_vsprintf
@ -122,17 +91,44 @@ int nx_vsyslog(int priority, FAR const IPTR char *fmt, FAR va_list *ap)
syslogstream_create(&stream);
#if defined(CONFIG_SYSLOG_TIMESTAMP)
/* Prepend the message with the current time, if available */
#ifdef CONFIG_SYSLOG_TIMESTAMP
/* Get the current time. Since debug output may be generated very early
* in the start-up sequence, hardware timer support may not yet be
* available.
*/
if (OSINIT_HW_READY())
{
#if defined(CONFIG_SYSLOG_TIMESTAMP_REALTIME)
/* Use CLOCK_REALTIME if so configured */
clock_gettime(CLOCK_REALTIME, &ts);
#elif defined(CONFIG_CLOCK_MONOTONIC)
/* Prefer monotonic when enabled, as it can be synchronized to
* RTC with clock_resynchronize.
*/
clock_gettime(CLOCK_MONOTONIC, &ts);
#if defined(CONFIG_SYSLOG_TIMESTAMP_FORMATTED)
time = ts.tv_sec;
#if defined(CONFIG_SYSLOG_TIMESTAMP_LOCALTIME)
localtime_r(&time, &tm);
#else
gmtime_r(&time, &tm);
/* Otherwise, fall back to the system timer */
clock_systime_timespec(&ts);
#endif
/* Prepend the message with the current time, if available */
#if defined(CONFIG_SYSLOG_TIMESTAMP_FORMATTED)
#if defined(CONFIG_SYSLOG_TIMESTAMP_LOCALTIME)
localtime_r(&ts.tv_sec, &tm);
#else
gmtime_r(&ts.tv_sec, &tm);
#endif
#endif
}
#if defined(CONFIG_SYSLOG_TIMESTAMP_FORMATTED)
ret = strftime(date_buf, CONFIG_SYSLOG_TIMESTAMP_BUFFER,
CONFIG_SYSLOG_TIMESTAMP_FORMAT, &tm);
@ -144,8 +140,6 @@ int nx_vsyslog(int priority, FAR const IPTR char *fmt, FAR va_list *ap)
ret = lib_sprintf(&stream.public, "[%5jd.%06ld] ",
(uintmax_t)ts.tv_sec, ts.tv_nsec / 1000);
#endif
#else
ret = 0;
#endif
#if defined(CONFIG_SMP)