Commit 5b3214ca by Christian Brauner Committed by Stéphane Graber

log: use thread-safe localtime_r()

This fixes a race in liblxc logging which can lead to deadlocks. The reproducer for this issue before this is to simply compile with --enable-tests and then run: lxc-test-concurrent -j 20 -m create,start,stop,destroy -D which should deadlock. Signed-off-by: 's avatarChristian Brauner <christian.brauner@ubuntu.com>
parent dfc0b35f
...@@ -20,6 +20,8 @@ ...@@ -20,6 +20,8 @@
* License along with this library; if not, write to the Free Software * License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
#define _GNU_SOURCE
#include <assert.h> #include <assert.h>
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
...@@ -31,8 +33,6 @@ ...@@ -31,8 +33,6 @@
#include <pthread.h> #include <pthread.h>
#include <time.h> #include <time.h>
#define __USE_GNU /* for *_CLOEXEC */
#include <fcntl.h> #include <fcntl.h>
#include <stdlib.h> #include <stdlib.h>
...@@ -72,7 +72,7 @@ static int log_append_logfile(const struct lxc_log_appender *appender, ...@@ -72,7 +72,7 @@ static int log_append_logfile(const struct lxc_log_appender *appender,
{ {
char date[LXC_LOG_DATEFOMAT_SIZE] = "20150427012246"; char date[LXC_LOG_DATEFOMAT_SIZE] = "20150427012246";
char buffer[LXC_LOG_BUFFER_SIZE]; char buffer[LXC_LOG_BUFFER_SIZE];
const struct tm *t; struct tm newtime;
int n; int n;
int ms; int ms;
int fd_to_use = -1; int fd_to_use = -1;
...@@ -88,8 +88,10 @@ static int log_append_logfile(const struct lxc_log_appender *appender, ...@@ -88,8 +88,10 @@ static int log_append_logfile(const struct lxc_log_appender *appender,
if (fd_to_use == -1) if (fd_to_use == -1)
return 0; return 0;
t = localtime(&event->timestamp.tv_sec); if (!localtime_r(&event->timestamp.tv_sec, &newtime))
strftime(date, sizeof(date), "%Y%m%d%H%M%S", t); return 0;
strftime(date, sizeof(date), "%Y%m%d%H%M%S", &newtime);
ms = event->timestamp.tv_usec / 1000; ms = event->timestamp.tv_usec / 1000;
n = snprintf(buffer, sizeof(buffer), n = snprintf(buffer, sizeof(buffer),
"%15s %10s.%03d %-8s %s - %s:%s:%d - ", "%15s %10s.%03d %-8s %s - %s:%s:%d - ",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment