logging: Use reentrant ctime_r instead of ctime

It was noticed that multithreaded processes like osmo-trx can crash upon
using ctime().

Related: OS#4055
Change-Id: I19ebf29a2f1fc855bb7d56766b338c7c3432dfd1
This commit is contained in:
Pau Espin 2019-06-12 16:22:53 +02:00
parent 77021c7bec
commit cc794e993c
1 changed files with 8 additions and 5 deletions

View File

@ -359,15 +359,18 @@ static void _output(struct log_target *target, unsigned int subsys,
OSMO_SNPRINTF_RET(ret, rem, offset, len);
#endif
} else if (target->print_timestamp) {
char *timestr;
time_t tm;
if ((tm = time(NULL)) == (time_t) -1)
goto err;
timestr = ctime(&tm);
timestr[strlen(timestr)-1] = '\0';
ret = snprintf(buf + offset, rem, "%s ", timestr);
if (ret < 0)
/* Get human-readable representation of time.
man ctime: we need at least 26 bytes in buf */
if (rem < 26 || !ctime_r(&tm, buf + offset))
goto err;
ret = strlen(buf + offset);
if (ret <= 0)
goto err;
/* Get rid of useless final '\n' added by ctime_r. We want a space instead. */
buf[offset + ret - 1] = ' ';
OSMO_SNPRINTF_RET(ret, rem, offset, len);
}
if (target->print_category) {