Merge pull request #1318 from brauner/2016-11-25/fix_logging_race

log: use thread-safe localtime_r()
This commit is contained in:
Stéphane Graber 2016-11-24 21:22:04 -05:00 committed by GitHub
commit b5e3950161

View File

@ -20,6 +20,8 @@
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _GNU_SOURCE
#include <assert.h>
#include <stdio.h>
#include <errno.h>
@ -31,8 +33,6 @@
#include <pthread.h>
#include <time.h>
#define __USE_GNU /* for *_CLOEXEC */
#include <syslog.h>
#include <stdio.h>
@ -139,7 +139,7 @@ static int log_append_logfile(const struct lxc_log_appender *appender,
{
char date[LXC_LOG_DATEFOMAT_SIZE] = "20150427012246";
char buffer[LXC_LOG_BUFFER_SIZE];
const struct tm *t;
struct tm newtime;
int n;
int ms;
int fd_to_use = -1;
@ -155,8 +155,10 @@ static int log_append_logfile(const struct lxc_log_appender *appender,
if (fd_to_use == -1)
return 0;
t = localtime(&event->timestamp.tv_sec);
strftime(date, sizeof(date), "%Y%m%d%H%M%S", t);
if (!localtime_r(&event->timestamp.tv_sec, &newtime))
return 0;
strftime(date, sizeof(date), "%Y%m%d%H%M%S", &newtime);
ms = event->timestamp.tv_usec / 1000;
n = snprintf(buffer, sizeof(buffer),
"%15s%s%s %10s.%03d %-8s %s - %s:%s:%d - ",