Tests: add kernel namespace context change tests
authorMichael Jeanson <mjeanson@efficios.com>
Tue, 2 Apr 2019 17:38:07 +0000 (13:38 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Wed, 23 Oct 2019 22:47:09 +0000 (18:47 -0400)
Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
.gitignore
configure.ac
tests/regression/kernel/Makefile.am
tests/regression/kernel/test_ns_contexts_change [new file with mode: 0755]
tests/root_regression
tests/utils/testapp/Makefile.am
tests/utils/testapp/gen-ns-events/Makefile.am [new file with mode: 0644]
tests/utils/testapp/gen-ns-events/gen-ns-events.c [new file with mode: 0644]

index c62f4d53a99126f73be2247c5c1bedb146fc0851..00a68d427a3c1045dbee4d2b33989714c44e2e76 100644 (file)
@@ -140,6 +140,7 @@ health_check
 /tests/utils/testapp/userspace-probe-elf-cxx-binary/userspace-probe-elf-cxx-binary
 /tests/utils/testapp/userspace-probe-sdt-binary/foobar_provider.h
 /tests/utils/testapp/userspace-probe-sdt-binary/userspace-probe-sdt-binary
+/tests/utils/testapp/gen-ns-events/gen-ns-events
 
 # man pages
 /doc/man/*.1
index 7b9d55b071e39ad77782e5e6fe8167f798d9c257..e57f5363195907c065772f2444361394d171efb2 100644 (file)
@@ -1175,6 +1175,7 @@ AC_CONFIG_FILES([
        tests/utils/Makefile
        tests/utils/tap/Makefile
        tests/utils/testapp/Makefile
+       tests/utils/testapp/gen-ns-events/Makefile
        tests/utils/testapp/gen-ust-events/Makefile
        tests/utils/testapp/gen-ust-events-ns/Makefile
        tests/utils/testapp/gen-syscall-events-callstack/Makefile
index 169267adb2c18291b2a45657c3abc7e62fb5f2d7..f539b80b67330b8d764457a64282c5b6aef691b4 100644 (file)
@@ -3,7 +3,7 @@ EXTRA_DIST = test_event_basic test_all_events test_syscall \
                test_select_poll_epoll test_lttng_logger \
                test_userspace_probe test_callstack \
                test_syscall validate_select_poll_epoll.py \
-               test_ns_contexts
+               test_ns_contexts test_ns_contexts_change
 
 noinst_PROGRAMS = select_poll_epoll
 select_poll_epoll_SOURCES = select_poll_epoll.c
diff --git a/tests/regression/kernel/test_ns_contexts_change b/tests/regression/kernel/test_ns_contexts_change
new file mode 100755 (executable)
index 0000000..905f6cf
--- /dev/null
@@ -0,0 +1,189 @@
+#!/bin/bash
+#
+# Copyright (C) 2019 Michael Jeanson <mjeanson@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
+
+TEST_DESC="Kernel tracer - Namespace contexts change"
+
+CURDIR=$(dirname "$0")/
+TESTDIR=$CURDIR/../..
+
+TESTAPP_PATH="$TESTDIR/utils/testapp"
+TESTAPP_NAME="gen-ns-events"
+TESTAPP_BIN="$TESTAPP_PATH/$TESTAPP_NAME/$TESTAPP_NAME"
+
+TESTS_PER_NS=19
+
+NUM_TESTS=$((TESTS_PER_NS * 6))
+
+source "$TESTDIR/utils/utils.sh"
+
+# MUST set TESTDIR before calling those functions
+
+function add_context_kernel_skip_ok()
+{
+       local session_name=$1
+       local channel_name=$2
+       local context_name=$3
+       local skip_num=$4
+
+       local ret
+
+       "$TESTDIR/../src/bin/lttng/$LTTNG_BIN" add-context -k \
+               -s "$session_name" -c "$channel_name" \
+               -t "$context_name"  1>"$OUTPUT_DEST" 2>"$ERROR_OUTPUT_DEST"
+       ret=$?
+
+       if [ "$ret" == "4" ]; then
+               skip 0 "Current kernel doesn't implement '$context_name' context" $((skip_num + 1))
+       else
+               ok $ret "Add context command for type: $context_name"
+       fi
+
+       return $ret
+}
+
+function enable_kernel_lttng_event_filter_ok()
+{
+       local session_name=$1
+       local syscall_name=$2
+       local channel_name=$3
+       local filter=$4
+
+       "$TESTDIR/../src/bin/lttng/$LTTNG_BIN" enable-event -k \
+               -c "$channel_name" -s "$session_name" \
+               --syscall "$syscall_name" \
+               -f "$filter" \
+               1>"$OUTPUT_DEST" 2>"$ERROR_OUTPUT_DEST"
+
+       ok $? "Add syscall with filter"
+}
+
+function test_ns()
+{
+       local ns=$1
+
+       local session_name="${ns}_ns"
+       local chan_name="${ns}_ns"
+       local context_name="${ns}_ns"
+
+       local trace_path
+       local ns_inode
+       local file_sync_wait_before_unshare
+       local file_sync_wait_after_unshare
+       local file_sync_signal_after_unshare
+
+       # Check if the kernel has support for this ns type
+       if [ ! -f "/proc/$$/ns/$ns" ]; then
+               skip 0 "System has no $ns namespace support" $TESTS_PER_NS
+               return
+       fi
+
+       trace_path=$(mktemp -d)
+       file_sync_wait_before_unshare=$(mktemp -u)
+       file_sync_wait_after_unshare=$(mktemp -u)
+       file_sync_signal_after_unshare=$(mktemp -u)
+
+       # Get the current ns inode number
+       ns_inode=$(stat -c '%i' -L "/proc/$$/ns/$ns")
+       ok $? "Get current $ns namespace inode: $ns_inode" || ns_inode="invalid"
+
+       $TESTAPP_BIN -n "$ns" -a "$file_sync_wait_after_unshare" -b "$file_sync_wait_before_unshare" -s "$file_sync_signal_after_unshare" &
+       ok $? "Launch test app."
+       app_pid=$!
+
+       app_ns_inode=$(stat -c '%i' -L "/proc/$app_pid/ns/$ns")
+       ok $? "Get app current $ns namespace inode: $app_ns_inode" || app_ns_inode="invalid"
+
+       start_lttng_sessiond
+
+       create_lttng_session_ok "$session_name" "$trace_path"
+       enable_kernel_lttng_channel_ok "$session_name" "$chan_name"
+       add_context_kernel_skip_ok "$session_name" "$chan_name" "$context_name" 10
+       if [ "$?" != "4" ]; then
+               lttng_enable_kernel_syscall_ok "$session_name" "unshare" "$chan_name"
+               lttng_track_pid_ok "$app_pid"
+               start_lttng_tracing_ok "$session_name"
+
+               touch "$file_sync_wait_before_unshare"
+
+               # Let the app do it's thing before entering the synchronisation loop
+               sleep 0.5
+
+               while [ ! -f "$file_sync_signal_after_unshare" ]; do
+                       # Break if the app failed / died
+                       if [ ! -f "/proc/$app_pid" ]; then
+                               break
+                       fi
+                       echo "# Waiting for app..."
+                       sleep 0.5
+               done
+
+               app_unshare_ns_inode=$(stat -c '%i' -L "/proc/$app_pid/ns/$ns")
+               ok $? "Get app current $ns namespace inode: $app_unshare_ns_inode" || app_unshare_ns_inode="invalid"
+
+               test "$app_ns_inode" != "invalid" && test "$app_unshare_ns_inode" != "invalid" && test "$app_ns_inode" != "$app_unshare_ns_inode"
+               ok $? "Reported namespace inode changed after unshare"
+
+               touch "$file_sync_wait_after_unshare"
+
+               stop_lttng_tracing_ok "$session_name"
+
+               # Check that the events contain the right namespace inode number
+               validate_trace_count "${ns}_ns = $app_ns_inode" "$trace_path" 1
+               validate_trace_count "${ns}_ns = $app_unshare_ns_inode" "$trace_path" 1
+       fi
+
+       # stop and destroy
+       destroy_lttng_session_ok "$session_name"
+       stop_lttng_sessiond
+
+       rm -rf "$trace_path"
+       rm -f "$file_sync_wait_after_unshare"
+       rm -f "$file_sync_wait_before_unshare"
+       rm -f "$file_sync_signal_after_unshare"
+}
+
+
+plan_tests $NUM_TESTS
+
+print_test_banner "$TEST_DESC"
+
+
+isroot=0
+if [ "$(id -u)" == "0" ]; then
+       isroot=1
+fi
+
+skip $isroot "Root access is needed. Skipping all tests." "$NUM_TESTS" && exit 0
+
+
+system_has_ns=0
+if [ -d "/proc/$$/ns" ]; then
+       system_has_ns=1
+fi
+
+skip $system_has_ns "System has no namespaces support" $NUM_TESTS && exit 0
+
+
+validate_lttng_modules_present
+
+test_ns cgroup
+test_ns ipc
+test_ns mnt
+test_ns net
+#test_ns pid # pid_ns is special, can't be changed that way
+test_ns user
+test_ns uts
index c9394aa2cba0914647b38a2133b40501deb5cfae..59f03440e713bc132238bd04620569acaaebbbdf 100644 (file)
@@ -9,6 +9,7 @@ regression/kernel/test_lttng_logger
 regression/kernel/test_callstack
 regression/kernel/test_userspace_probe
 regression/kernel/test_ns_contexts
+regression/kernel/test_ns_contexts_change
 regression/tools/live/test_kernel
 regression/tools/live/test_lttng_kernel
 regression/tools/streaming/test_high_throughput_limits
index e98f4b5714f414b5561cd3dc2cd308f9b933fdb2..2a4cf5c7ba1ab002532f4a9b9ab6861163a63821 100644 (file)
@@ -1,4 +1,6 @@
-SUBDIRS = gen-ust-events \
+SUBDIRS = \
+         gen-ns-events \
+         gen-ust-events \
          gen-ust-events-ns \
          gen-ust-nevents \
          gen-ust-nevents-str \
diff --git a/tests/utils/testapp/gen-ns-events/Makefile.am b/tests/utils/testapp/gen-ns-events/Makefile.am
new file mode 100644 (file)
index 0000000..17bc21f
--- /dev/null
@@ -0,0 +1,8 @@
+AM_CPPFLAGS += -I$(top_srcdir)/tests/utils -I$(srcdir) \
+              -I$(top_srcdir)/tests/utils/testapp
+
+noinst_PROGRAMS = gen-ns-events
+gen_ns_events_SOURCES = gen-ns-events.c
+gen_ns_events_LDADD = \
+               $(top_builddir)/tests/utils/libtestutils.la \
+               $(DL_LIBS) $(POPT_LIBS)
diff --git a/tests/utils/testapp/gen-ns-events/gen-ns-events.c b/tests/utils/testapp/gen-ns-events/gen-ns-events.c
new file mode 100644 (file)
index 0000000..eb1ebf5
--- /dev/null
@@ -0,0 +1,229 @@
+/*
+ * Copyright (C) 2019 Michael Jeanson <mjeanson@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
+ */
+
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+
+#include <popt.h>
+#include <sched.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <common/compat/tid.h>
+
+#include "signal-helper.h"
+#include "utils.h"
+
+#define LTTNG_PROC_NS_PATH_MAX 40
+
+static int debug = 0;
+static char *ns_opt = NULL;
+static char *before_unshare_wait_file_path = NULL;
+static char *after_unshare_wait_file_path = NULL;
+static char *after_unshare_signal_file_path = NULL;
+
+static struct poptOption opts[] = {
+       /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
+       { "debug", 'd', POPT_ARG_NONE, &debug, 0, "Enable debug output", NULL },
+       { "ns", 'n', POPT_ARG_STRING, &ns_opt, 0, "Namespace short identifier", NULL },
+       { "before", 'b', POPT_ARG_STRING, &before_unshare_wait_file_path, 0, "Wait for file before unshare", NULL },
+       { "after", 'a', POPT_ARG_STRING, &after_unshare_wait_file_path, 0, "Wait for file after unshare", NULL },
+       { "signal", 's', POPT_ARG_STRING, &after_unshare_signal_file_path, 0, "Create signal file after unshare", NULL },
+       POPT_AUTOHELP
+       { NULL, 0, 0, NULL, 0 }
+};
+
+static void debug_printf(const char *format, ...)
+{
+       va_list args;
+       va_start(args, format);
+
+       if (debug) {
+               vfprintf(stderr, format, args);
+       }
+
+       va_end(args);
+}
+
+static int get_ns_inum(const char *ns, ino_t *ns_inum)
+{
+       int ret = 0;
+       struct stat sb;
+       char proc_ns_path[LTTNG_PROC_NS_PATH_MAX];
+
+       /*
+        * /proc/thread-self was introduced in kernel v3.17
+        */
+       if (snprintf(proc_ns_path, LTTNG_PROC_NS_PATH_MAX,
+                       "/proc/thread-self/ns/%s", ns) >= 0) {
+               if (stat(proc_ns_path, &sb) == 0) {
+                       *ns_inum = sb.st_ino;
+               } else {
+                       ret = -1;
+               }
+               goto end;
+       }
+
+       if (snprintf(proc_ns_path, LTTNG_PROC_NS_PATH_MAX,
+                       "/proc/self/task/%d/%s/net", lttng_gettid(), ns) >= 0) {
+               if (stat(proc_ns_path, &sb) == 0) {
+                       *ns_inum = sb.st_ino;
+               } else {
+                       ret = -1;
+               }
+               goto end;
+       }
+end:
+       return ret;
+}
+
+static int do_the_needful(int ns_flag, const char *ns_str)
+{
+       int ret = 0;
+       ino_t ns1, ns2;
+
+       ret = get_ns_inum(ns_str, &ns1);
+       if (ret) {
+               debug_printf("Failed to get ns inode number for namespace %s",
+                               ns_str);
+               ret = -1;
+               goto end;
+       }
+       debug_printf("Initial %s ns inode number:      %lu\n", ns_str, ns1);
+
+       /* Wait on synchronization before unshare. */
+       if (before_unshare_wait_file_path) {
+               ret = wait_on_file(before_unshare_wait_file_path);
+               if (ret != 0) {
+                       goto end;
+               }
+       }
+
+       ret = unshare(ns_flag);
+       if (ret == -1) {
+               perror("Failed to unshare namespace");
+               goto end;
+       }
+
+       ret = get_ns_inum(ns_str, &ns2);
+       if (ret) {
+               debug_printf("Failed to get ns inode number for namespace %s",
+                               ns_str);
+               ret = -1;
+               goto end;
+       }
+       debug_printf("Post unshare %s ns inode number: %lu\n", ns_str, ns2);
+
+       /* Signal that the unshare call is completed. */
+       if (after_unshare_signal_file_path) {
+               ret = create_file(after_unshare_signal_file_path);
+               if (ret != 0) {
+                       goto end;
+               }
+       }
+
+       /* Wait on synchronization after unshare. */
+       if (after_unshare_wait_file_path) {
+               ret = wait_on_file(after_unshare_wait_file_path);
+               if (ret != 0) {
+                       goto end;
+               }
+       }
+
+end:
+       return ret;
+}
+
+int main(int argc, const char **argv)
+{
+       int opt;
+       int ret = EXIT_SUCCESS;
+       poptContext pc;
+
+       pc = poptGetContext(NULL, argc, argv, opts, 0);
+       poptReadDefaultConfig(pc, 0);
+
+       if (argc < 2) {
+               poptPrintHelp(pc, stderr, 0);
+               ret = EXIT_FAILURE;
+               goto end;
+       }
+
+       while ((opt = poptGetNextOpt(pc)) >= 0) {
+               switch (opt) {
+               default:
+                       poptPrintUsage(pc, stderr, 0);
+                       ret = EXIT_FAILURE;
+                       goto end;
+               }
+       }
+
+       if (opt < -1) {
+               /* an error occurred during option processing */
+               poptPrintUsage(pc, stderr, 0);
+               fprintf(stderr, "%s: %s\n",
+                       poptBadOption(pc, POPT_BADOPTION_NOALIAS),
+                       poptStrerror(opt));
+               ret = EXIT_FAILURE;
+               goto end;
+       }
+
+       if (ns_opt == NULL) {
+               poptPrintUsage(pc, stderr, 0);
+               ret = EXIT_FAILURE;
+               goto end;
+       }
+
+       if (set_signal_handler()) {
+               ret = EXIT_FAILURE;
+               goto end;
+       }
+
+       if (strncmp(ns_opt, "cgroup", 3) == 0) {
+               ret = do_the_needful(CLONE_NEWCGROUP, "cgroup");
+       } else if (strncmp(ns_opt, "ipc", 3) == 0) {
+               ret = do_the_needful(CLONE_NEWIPC, "ipc");
+       } else if (strncmp(ns_opt, "mnt", 3) == 0) {
+               ret = do_the_needful(CLONE_NEWNS, "mnt");
+       } else if (strncmp(ns_opt, "net", 3) == 0) {
+               ret = do_the_needful(CLONE_NEWNET, "net");
+       } else if (strncmp(ns_opt, "pid", 3) == 0) {
+               ret = do_the_needful(CLONE_NEWPID, "pid");
+       } else if (strncmp(ns_opt, "user", 3) == 0) {
+               /*
+                * Will always fail, requires a single threaded application,
+                * which can't happen with UST.
+                */
+               ret = do_the_needful(CLONE_NEWUSER, "user");
+       } else if (strncmp(ns_opt, "uts", 3) == 0) {
+               ret = do_the_needful(CLONE_NEWUTS, "uts");
+       } else {
+               printf("invalid ns id\n");
+               ret = EXIT_FAILURE;
+               goto end;
+       }
+       ret = ret ? EXIT_FAILURE : EXIT_SUCCESS;
+end:
+       poptFreeContext(pc);
+       return ret;
+}
This page took 0.036701 seconds and 4 git commands to generate.