]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
log: use thread-safe localtime_r() 1318/head
authorChristian Brauner <christian.brauner@ubuntu.com>
Fri, 25 Nov 2016 01:42:51 +0000 (02:42 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Fri, 25 Nov 2016 01:42:51 +0000 (02:42 +0100)
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: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/log.c

index 6775822b32a9cb79247389eeeafbd1aecc717a97..a08cc2637e61a0b55822ce46f03a976df71913ee 100644 (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 - ",