From 135801fce994bbed911e17d5522ca4fdb51e564d Mon Sep 17 00:00:00 2001 From: Michael Jeanson Date: Mon, 1 Apr 2019 11:53:57 -0400 Subject: [PATCH] Add UST namespace context change tests Signed-off-by: Michael Jeanson --- .gitignore | 1 + configure.ac | 1 + tests/regression/Makefile.am | 3 +- tests/regression/ust/namespaces/Makefile.am | 4 +- .../ust/namespaces/test_ns_contexts_change | 135 +++++++++++ tests/utils/testapp/Makefile.am | 1 + .../testapp/gen-ust-events-ns/Makefile.am | 10 + .../gen-ust-events-ns/gen-ust-events-ns.c | 229 ++++++++++++++++++ tests/utils/testapp/gen-ust-events-ns/tp.c | 16 ++ tests/utils/testapp/gen-ust-events-ns/tp.h | 35 +++ 10 files changed, 432 insertions(+), 3 deletions(-) create mode 100755 tests/regression/ust/namespaces/test_ns_contexts_change create mode 100644 tests/utils/testapp/gen-ust-events-ns/Makefile.am create mode 100644 tests/utils/testapp/gen-ust-events-ns/gen-ust-events-ns.c create mode 100644 tests/utils/testapp/gen-ust-events-ns/tp.c create mode 100644 tests/utils/testapp/gen-ust-events-ns/tp.h diff --git a/.gitignore b/.gitignore index 19276012e..080df57ec 100644 --- a/.gitignore +++ b/.gitignore @@ -117,6 +117,7 @@ health_check /tests/regression/ust/multi-lib/exec-without-callsites /tests/utils/testapp/gen-syscall-events-callstack/gen-syscall-events-callstack /tests/utils/testapp/gen-ust-events/gen-ust-events +/tests/utils/testapp/gen-ust-events-ns/gen-ust-events-ns /tests/utils/testapp/gen-ust-nevents-str/gen-ust-nevents-str /tests/utils/testapp/gen-ust-nevents/gen-ust-nevents /tests/utils/testapp/gen-ust-tracef/gen-ust-tracef diff --git a/configure.ac b/configure.ac index e833c9d79..324e11314 100644 --- a/configure.ac +++ b/configure.ac @@ -1167,6 +1167,7 @@ AC_CONFIG_FILES([ tests/utils/tap/Makefile tests/utils/testapp/Makefile tests/utils/testapp/gen-ust-events/Makefile + tests/utils/testapp/gen-ust-events-ns/Makefile tests/utils/testapp/gen-syscall-events-callstack/Makefile tests/utils/testapp/gen-ust-nevents/Makefile tests/utils/testapp/gen-ust-nevents-str/Makefile diff --git a/tests/regression/Makefile.am b/tests/regression/Makefile.am index 1d3cda3bb..2071b28b7 100644 --- a/tests/regression/Makefile.am +++ b/tests/regression/Makefile.am @@ -51,7 +51,8 @@ TESTS += ust/before-after/test_before_after \ ust/blocking/test_blocking \ ust/multi-lib/test_multi_lib \ ust/rotation-destroy-flush/test_rotation_destroy_flush \ - ust/namespaces/test_ns_contexts + ust/namespaces/test_ns_contexts \ + ust/namespaces/test_ns_contexts_change endif # HAVE_LIBLTTNG_UST_CTL if PYTHON_BINDING diff --git a/tests/regression/ust/namespaces/Makefile.am b/tests/regression/ust/namespaces/Makefile.am index 11f4d8508..78f659cc1 100644 --- a/tests/regression/ust/namespaces/Makefile.am +++ b/tests/regression/ust/namespaces/Makefile.am @@ -1,5 +1,5 @@ -noinst_SCRIPTS = test_ns_contexts -EXTRA_DIST = test_ns_contexts +noinst_SCRIPTS = test_ns_contexts test_ns_contexts_change +EXTRA_DIST = test_ns_contexts test_ns_contexts_change all-local: @if [ x"$(srcdir)" != x"$(builddir)" ]; then \ diff --git a/tests/regression/ust/namespaces/test_ns_contexts_change b/tests/regression/ust/namespaces/test_ns_contexts_change new file mode 100755 index 000000000..18a22e588 --- /dev/null +++ b/tests/regression/ust/namespaces/test_ns_contexts_change @@ -0,0 +1,135 @@ +#!/bin/bash +# +# Copyright (C) 2019 Michael Jeanson +# +# 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="UST - Namespace contexts change" + +CURDIR=$(dirname "$0")/ +TESTDIR=$CURDIR/../../.. + +TESTAPP_PATH="$TESTDIR/utils/testapp" +TESTAPP_NAME="gen-ust-events-ns" +TESTAPP_BIN="$TESTAPP_PATH/$TESTAPP_NAME/$TESTAPP_NAME" +NUM_EVENT=1000 +EVENT_NAME="tp:tptest" + +TESTS_PER_NS=16 + +NUM_TESTS=$((TESTS_PER_NS * 5)) + +source "$TESTDIR/utils/utils.sh" + +# MUST set TESTDIR before calling those functions + +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_before_last + local file_sync_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 + + # 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" + + trace_path=$(mktemp -d) + file_sync_before_last=$(mktemp -u) + file_sync_after_unshare=$(mktemp -u) + + start_lttng_sessiond + + create_lttng_session_ok "$session_name" "$trace_path" + enable_ust_lttng_channel_ok "$session_name" "$chan_name" + add_context_ust_ok "$session_name" "$chan_name" "$context_name" + enable_ust_lttng_event_ok "$session_name" "$EVENT_NAME" "$chan_name" + start_lttng_tracing_ok "$session_name" + + $TESTAPP_BIN -n "$ns" -i $NUM_EVENT -a "$file_sync_after_unshare" -b "$file_sync_before_last" & + app_pid=$! + + # Let the app do it's thing before entering the synchronisation loop + sleep 0.5 + + while [ ! -f "$file_sync_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_ns_inode=$(stat -c '%i' -L "/proc/$app_pid/ns/$ns") + ok $? "Get current $ns namespace inode: $app_ns_inode" || app_ns_inode="invalid" + + test "$ns_inode" != "invalid" && test "$app_ns_inode" != "invalid" && test "$ns_inode" != "$app_ns_inode" + ok $? "Reported namespace inode changed after unshare" + + touch "$file_sync_before_last" + + # stop and destroy + stop_lttng_tracing_ok "$session_name" + destroy_lttng_session_ok "$session_name" + stop_lttng_sessiond + + # Check that the events contain the right namespace inode number + validate_trace_count "${ns}_ns = $ns_inode" "$trace_path" $NUM_EVENT + validate_trace_count "${ns}_ns = $app_ns_inode" "$trace_path" $NUM_EVENT + + rm -rf "$trace_path" + rm -f "$file_sync_before_last" + rm -f "$file_sync_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 + + +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 # user_ns can only be change when the app is single threaded, this is always false for an ust instrumented app +test_ns uts diff --git a/tests/utils/testapp/Makefile.am b/tests/utils/testapp/Makefile.am index e54ac890a..e98f4b571 100644 --- a/tests/utils/testapp/Makefile.am +++ b/tests/utils/testapp/Makefile.am @@ -1,4 +1,5 @@ SUBDIRS = gen-ust-events \ + gen-ust-events-ns \ gen-ust-nevents \ gen-ust-nevents-str \ gen-ust-tracef \ diff --git a/tests/utils/testapp/gen-ust-events-ns/Makefile.am b/tests/utils/testapp/gen-ust-events-ns/Makefile.am new file mode 100644 index 000000000..39f991240 --- /dev/null +++ b/tests/utils/testapp/gen-ust-events-ns/Makefile.am @@ -0,0 +1,10 @@ +AM_CPPFLAGS += -I$(top_srcdir)/tests/utils -I$(srcdir) \ + -I$(top_srcdir)/tests/utils/testapp + +if HAVE_LIBLTTNG_UST_CTL +noinst_PROGRAMS = gen-ust-events-ns +gen_ust_events_ns_SOURCES = gen-ust-events-ns.c tp.c tp.h +gen_ust_events_ns_LDADD = -llttng-ust -lurcu-bp -llttng-ust-fork \ + $(top_builddir)/tests/utils/libtestutils.la \ + $(DL_LIBS) $(POPT_LIBS) +endif diff --git a/tests/utils/testapp/gen-ust-events-ns/gen-ust-events-ns.c b/tests/utils/testapp/gen-ust-events-ns/gen-ust-events-ns.c new file mode 100644 index 000000000..61611853b --- /dev/null +++ b/tests/utils/testapp/gen-ust-events-ns/gen-ust-events-ns.c @@ -0,0 +1,229 @@ +/* + * Copyright (C) 2019 Michael Jeanson + * + * 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 +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "utils.h" +#include "signal-helper.h" + +#define TRACEPOINT_DEFINE +#include "tp.h" + +#define LTTNG_PROC_NS_PATH_MAX 40 + +static int nr_iter = 100; +static int debug = 0; +static char *ns_opt = NULL; +static char *after_unshare_file_path = NULL; +static char *before_second_event_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 }, + { "iter", 'i', POPT_ARG_INT, &nr_iter, 0, "Number of tracepoint iterations", NULL }, + { "after", 'a', POPT_ARG_STRING, &after_unshare_file_path, 0, "after_unshare_file_path,", NULL }, + { "before", 'b', POPT_ARG_STRING, &before_second_event_file_path, 0, "before_second_event_file_path,", 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 +ino_t get_ns_inum(char ns[]) { + 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) { + return sb.st_ino; + } + } + + if (snprintf(proc_ns_path, LTTNG_PROC_NS_PATH_MAX, + "/proc/self/task/%d/%s/net", + gettid(), ns) >= 0) { + + if (stat(proc_ns_path, &sb) == 0) { + return sb.st_ino; + } + } + + return 1; +} + +static +int do_the_needful(int ns_flag, char ns_str[]) { + int ret = 0, i; + ino_t ns1, ns2; + + ns1 = get_ns_inum(ns_str); + debug_printf("Initial %s ns inode number: %lu\n", ns_str, ns1); + + for (i = 0; nr_iter < 0 || i < nr_iter; i++) { + + tracepoint(tp, tptest, ns1); + + if (should_quit) { + break; + } + } + + ret = unshare(ns_flag); + + if (ret == -1) { + perror("unshare"); + ret = 0; + } + + ns2 = get_ns_inum(ns_str); + debug_printf("Post unshare %s ns inode number: %lu\n", ns_str, ns2); + + /* + * Signal that we emited the first event group and that the + * unshare call is completed. + */ + if (after_unshare_file_path) { + ret = create_file(after_unshare_file_path); + + if (ret != 0) { + goto end; + } + } + + /* + * Wait on synchronization before writing second event group. + */ + if (before_second_event_file_path) { + ret = wait_on_file(before_second_event_file_path); + if (ret != 0) { + goto end; + } + } + + for (i = 0; nr_iter < 0 || i < nr_iter; i++) { + + tracepoint(tp, tptest, ns2); + + if (should_quit) { + break; + } + } + +end: + return ret; +} + +// Send X events, change NS, wait for file to sync with test script, send X events in new NS + + +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) { + do_the_needful(CLONE_NEWCGROUP, "cgroup"); + } else if (strncmp(ns_opt, "ipc", 3) == 0) { + do_the_needful(CLONE_NEWIPC, "ipc"); + } else if (strncmp(ns_opt, "mnt", 3) == 0) { + do_the_needful(CLONE_NEWNS, "mnt"); + } else if (strncmp(ns_opt, "net", 3) == 0) { + do_the_needful(CLONE_NEWNET, "net"); + } else if (strncmp(ns_opt, "pid", 3) == 0) { + 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. + do_the_needful(CLONE_NEWUSER, "user"); + } else if (strncmp(ns_opt, "uts", 3) == 0) { + do_the_needful(CLONE_NEWUTS, "uts"); + } else { + printf("invalid ns id\n"); + ret = EXIT_FAILURE; + } + +end: + poptFreeContext(pc); + return ret; +} diff --git a/tests/utils/testapp/gen-ust-events-ns/tp.c b/tests/utils/testapp/gen-ust-events-ns/tp.c new file mode 100644 index 000000000..6f5341436 --- /dev/null +++ b/tests/utils/testapp/gen-ust-events-ns/tp.c @@ -0,0 +1,16 @@ +/* + * Copyright (c) - 2012 David Goulet + * + * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED OR + * IMPLIED. ANY USE IS AT YOUR OWN RISK. + * + * Permission is hereby granted to use or copy this program for any purpose, + * provided the above notices are retained on all copies. Permission to modify + * the code and to distribute modified code is granted, provided the above + * notices are retained, and a notice that the code was modified is included + * with the above copyright notice. + */ + +#define _LGPL_SOURCE +#define TRACEPOINT_CREATE_PROBES +#include "tp.h" diff --git a/tests/utils/testapp/gen-ust-events-ns/tp.h b/tests/utils/testapp/gen-ust-events-ns/tp.h new file mode 100644 index 000000000..59b3f295b --- /dev/null +++ b/tests/utils/testapp/gen-ust-events-ns/tp.h @@ -0,0 +1,35 @@ +#undef TRACEPOINT_PROVIDER +#define TRACEPOINT_PROVIDER tp + +#if !defined(_TRACEPOINT_TP_H) || defined(TRACEPOINT_HEADER_MULTI_READ) +#define _TRACEPOINT_TP_H + +/* + * Copyright (C) 2011 Mathieu Desnoyers + * + * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED + * OR IMPLIED. ANY USE IS AT YOUR OWN RISK. + * + * Permission is hereby granted to use or copy this program + * for any purpose, provided the above notices are retained on all copies. + * Permission to modify the code and to distribute modified code is granted, + * provided the above notices are retained, and a notice that the code was + * modified is included with the above copyright notice. + */ + +#include + +TRACEPOINT_EVENT(tp, tptest, + TP_ARGS(ino_t, ns_ino), + TP_FIELDS( + ctf_integer(ino_t, ns_ino, ns_ino) + ) +) + +#endif /* _TRACEPOINT_TP_H */ + +#undef TRACEPOINT_INCLUDE_FILE +#define TRACEPOINT_INCLUDE_FILE ./tp.h + +/* This part must be outside ifdef protection */ +#include -- 2.34.1