Implement liblttng-ust-fd
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Mon, 12 Sep 2016 16:04:13 +0000 (12:04 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Mon, 3 Oct 2016 16:00:32 +0000 (12:00 -0400)
This library overrides close() and closeall() libc functions, and uses
lttng_ust_safe_close_fd() to check whether the application can
interact with the file descriptor or if it should be left to lttng-ust.

This takes care of bugs caused by applications doing bulk close() or
closefrom() of file descriptors soon after forking.

Fixes: #253
Fixes: #626
CC: Aravind HT <aravind.ht@gmail.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Makefile.am
configure.ac
liblttng-ust-fd/Makefile.am [new file with mode: 0644]
liblttng-ust-fd/lttng-ust-fd.c [new file with mode: 0644]

index e498719573db73f74395fb7e1a1e0c5bf9807890..232acd11369f0b3daf19c8996d269a26bdbf21fd 100644 (file)
@@ -3,6 +3,7 @@ ACLOCAL_AMFLAGS = -I config
 SUBDIRS = . include snprintf libringbuffer liblttng-ust-comm \
                liblttng-ust \
                liblttng-ust-ctl \
+               liblttng-ust-fd \
                liblttng-ust-fork \
                liblttng-ust-libc-wrapper \
                liblttng-ust-cyg-profile \
index 4256f6654a321ba3f145855273e8a72ac0303311..a59758e8e54307729101698897d422896414c417 100644 (file)
@@ -451,6 +451,7 @@ AC_CONFIG_FILES([
        liblttng-ust-ctl/Makefile
        liblttng-ust-fork/Makefile
        liblttng-ust-dl/Makefile
+       liblttng-ust-fd/Makefile
        liblttng-ust-java/Makefile
        liblttng-ust-java-agent/Makefile
        liblttng-ust-java-agent/java/Makefile
diff --git a/liblttng-ust-fd/Makefile.am b/liblttng-ust-fd/Makefile.am
new file mode 100644 (file)
index 0000000..52f897d
--- /dev/null
@@ -0,0 +1,17 @@
+AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_builddir)/include
+AM_CFLAGS = -fno-strict-aliasing
+
+lib_LTLIBRARIES = liblttng-ust-fd.la
+liblttng_ust_fd_la_SOURCES = \
+       lttng-ust-fd.c
+liblttng_ust_fd_la_LIBADD = \
+       $(top_builddir)/liblttng-ust-comm/liblttng-ust-comm.la
+
+if LTTNG_UST_BUILD_WITH_LIBDL
+liblttng_ust_fd_la_LIBADD += -ldl
+endif
+if LTTNG_UST_BUILD_WITH_LIBC_DL
+liblttng_ust_fd_la_LIBADD += -lc
+endif
+
+liblttng_ust_fd_la_CFLAGS = -DUST_COMPONENT=liblttng-ust-fd -fno-strict-aliasing
diff --git a/liblttng-ust-fd/lttng-ust-fd.c b/liblttng-ust-fd/lttng-ust-fd.c
new file mode 100644 (file)
index 0000000..6a095db
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2016  Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; version 2.1 of
+ * the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * 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
+#define _LGPL_SOURCE
+#include <limits.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <ust-fd.h>
+#include <dlfcn.h>
+
+#include <helper.h>
+#include "usterr-signal-safe.h"
+
+volatile enum ust_loglevel ust_loglevel;
+
+static int (*__lttng_ust_fd_plibc_close)(int fd);
+
+static
+int _lttng_ust_fd_libc_close(int fd)
+{
+       if (!__lttng_ust_fd_plibc_close) {
+               __lttng_ust_fd_plibc_close = dlsym(RTLD_NEXT, "close");
+               if (!__lttng_ust_fd_plibc_close) {
+                       fprintf(stderr, "%s\n", dlerror());
+                       return -1;
+               }
+       }
+       return lttng_ust_safe_close_fd(fd, __lttng_ust_fd_plibc_close);
+}
+
+int close(int fd)
+{
+       return _lttng_ust_fd_libc_close(fd);
+}
+
+#if defined(__sun__) || defined(__FreeBSD__)
+/* Solaris and FreeBSD. */
+void closefrom(int lowfd)
+{
+       (void) lttng_ust_safe_closefrom(lowfd, __lttng_ust_fd_plibc_close);
+}
+#elif defined(__NetBSD__) || defined(__OpenBSD__)
+/* NetBSD and OpenBSD. */
+int closefrom(int lowfd)
+{
+       return lttng_ust_safe_closefrom(lowfd, __lttng_ust_fd_plibc_close);
+}
+#else
+/* As far as we know, this OS does not implement closefrom. */
+#endif
This page took 0.026987 seconds and 4 git commands to generate.