[lttng-dev] [PATCH lttng-tools] Fix: initialize sessions pointer to NULL

2019-10-25 Thread Jonathan Rajotte
lttng_list_sessions does not set the passed pointer to NULL on empty
return. This lead to deallocation of non-allocated memory (segfault).

For returns of size 0, the value of the passed argument should be
considered "undefined".

Refactor error handling a bit by removing the "error" jump. Always call
free on the sessions object.

Fixes #1205

Signed-off-by: Jonathan Rajotte 
---
 src/bin/lttng/commands/list.c | 11 +--
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/src/bin/lttng/commands/list.c b/src/bin/lttng/commands/list.c
index 28166c8be..65d8ea6f5 100644
--- a/src/bin/lttng/commands/list.c
+++ b/src/bin/lttng/commands/list.c
@@ -1825,7 +1825,7 @@ static int list_sessions(const char *session_name)
int ret = CMD_SUCCESS;
int count, i;
unsigned int session_found = 0;
-   struct lttng_session *sessions;
+   struct lttng_session *sessions = NULL;
 
count = lttng_list_sessions(&sessions);
DBG("Session count %d", count);
@@ -1838,7 +1838,7 @@ static int list_sessions(const char *session_name)
if (lttng_opt_mi) {
/* Mi */
if (session_name == NULL) {
-   /* List all session */
+   /* List all sessions */
ret = mi_list_sessions(sessions, count);
} else {
/* Note : this return an open session element */
@@ -1846,7 +1846,7 @@ static int list_sessions(const char *session_name)
}
if (ret) {
ret = CMD_ERROR;
-   goto error;
+   goto end;
}
} else {
/* Pretty print */
@@ -1893,7 +1893,7 @@ static int list_sessions(const char *session_name)
if (!session_found && session_name != NULL) {
ERR("Session '%s' not found", session_name);
ret = CMD_ERROR;
-   goto error;
+   goto end;
}
 
if (session_name == NULL) {
@@ -1901,9 +1901,8 @@ static int list_sessions(const char *session_name)
}
}
 
-error:
-   free(sessions);
 end:
+   free(sessions);
return ret;
 }
 
-- 
2.17.1

___
lttng-dev mailing list
lttng-dev@lists.lttng.org
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev


[lttng-dev] [PATCH lttng-tools 1/5] Fix: move set base_path of session to URI configuration

2019-10-25 Thread Jonathan Rajotte
The load code still use the "old" API to create and configure network
session output (lttng_create_session followed by
lttng_set_consumer_url). The session base_path is only set in the
cmd_create_session_from_descriptor function. This results in invalid
network output path when using a loaded session configuration (xml).

While we might want to move the load code to the new API, there is a case
to be made in not breaking the previous API behaviour.

To restore the expected behaviour of lttng_set_consumer_url, move the
assignation of the session base_path to the cmd_set_consumer_uri function
(LTTNG_SET_CONSUMER_URI).

Both the previous and session descriptor based creation API uses this code
path when needed (network output).

Signed-off-by: Jonathan Rajotte 
---

Note that this is the "base fix". The next 2 commits extracts the added for
loops into its own function and also remove
lttng_session_descriptor_get_base_path from the code base. Feel free to "squash"
them. I was not sure what you would prefer.

---
 src/bin/lttng-sessiond/cmd.c | 36 +++-
 src/bin/lttng-sessiond/session.c | 12 +--
 src/bin/lttng-sessiond/session.h |  2 +-
 tests/unit/test_session.c|  4 ++--
 4 files changed, 30 insertions(+), 24 deletions(-)

diff --git a/src/bin/lttng-sessiond/cmd.c b/src/bin/lttng-sessiond/cmd.c
index a164cffc0..1dec61ea1 100644
--- a/src/bin/lttng-sessiond/cmd.c
+++ b/src/bin/lttng-sessiond/cmd.c
@@ -2759,11 +2759,32 @@ int cmd_set_consumer_uri(struct ltt_session *session, 
size_t nb_uri,
goto error;
}
 
+   for (i = 0; i < nb_uri; i++) {
+   if (uris[i].stype != LTTNG_STREAM_CONTROL ||
+   uris[i].subdir[0] == '\0') {
+   /* Not interested in these URIs */
+   continue;
+   }
+
+   if (session->base_path != NULL) {
+   free(session->base_path);
+   session->base_path = NULL;
+   }
+
+   /* Set session base_path */
+   session->base_path = strdup(uris[i].subdir);
+   if (!session->base_path) {
+   PERROR("Copying base path: %s", uris[i].subdir);
+   goto error;
+   }
+   DBG2("Setting base path for session %" PRIu64 ": %s",
+   session->id, session->base_path);
+   }
+
/* Set the "global" consumer URIs */
for (i = 0; i < nb_uri; i++) {
-   ret = add_uri_to_consumer(session,
-   session->consumer,
-   &uris[i], LTTNG_DOMAIN_NONE);
+   ret = add_uri_to_consumer(session, session->consumer, &uris[i],
+   LTTNG_DOMAIN_NONE);
if (ret != LTTNG_OK) {
goto error;
}
@@ -2894,7 +2915,6 @@ enum lttng_error_code cmd_create_session_from_descriptor(
const char *session_name;
struct ltt_session *new_session = NULL;
enum lttng_session_descriptor_status descriptor_status;
-   const char *base_path;
 
session_lock_list();
if (home_path) {
@@ -2917,13 +2937,9 @@ enum lttng_error_code cmd_create_session_from_descriptor(
ret_code = LTTNG_ERR_INVALID;
goto end;
}
-   ret = lttng_session_descriptor_get_base_path(descriptor, &base_path);
-   if (ret) {
-   ret_code = LTTNG_ERR_INVALID;
-   goto end;
-   }
+
ret_code = session_create(session_name, creds->uid, creds->gid,
-   base_path, &new_session);
+   &new_session);
if (ret_code != LTTNG_OK) {
goto end;
}
diff --git a/src/bin/lttng-sessiond/session.c b/src/bin/lttng-sessiond/session.c
index 383c6fde7..ed68d153f 100644
--- a/src/bin/lttng-sessiond/session.c
+++ b/src/bin/lttng-sessiond/session.c
@@ -962,7 +962,7 @@ end:
  * Session list lock must be held by the caller.
  */
 enum lttng_error_code session_create(const char *name, uid_t uid, gid_t gid,
-   const char *base_path, struct ltt_session **out_session)
+   struct ltt_session **out_session)
 {
int ret;
enum lttng_error_code ret_code;
@@ -1086,16 +1086,6 @@ enum lttng_error_code session_create(const char *name, 
uid_t uid, gid_t gid,
}
}
 
-   if (base_path) {
-   new_session->base_path = strdup(base_path);
-   if (!new_session->base_path) {
-   ERR("Failed to allocate base path of session \"%s\"",
-   name);
-   ret_code = LTTNG_ERR_SESSION_FAIL;
-   goto error;
-   }
-   }
-
new_session->uid = uid;
new_session->gid = gid;
 
diff --git a/src/bin/lttng-sessiond/session.h b/src/bin/lt

[lttng-dev] [PATCH lttng-tools 4/5] Tests: base path: lttng load for session configuration

2019-10-25 Thread Jonathan Rajotte
Signed-off-by: Jonathan Rajotte 
---
 .../base-path/load-stream-extra-path.lttng| 66 +++
 tests/regression/tools/base-path/test_ust | 29 +++-
 2 files changed, 92 insertions(+), 3 deletions(-)
 create mode 100644 
tests/regression/tools/base-path/load-stream-extra-path.lttng

diff --git a/tests/regression/tools/base-path/load-stream-extra-path.lttng 
b/tests/regression/tools/base-path/load-stream-extra-path.lttng
new file mode 100644
index 0..973d67a6d
--- /dev/null
+++ b/tests/regression/tools/base-path/load-stream-extra-path.lttng
@@ -0,0 +1,66 @@
+
+
+   
+   load-stream-extra-path
+   
+   
+   UST
+   PER_UID
+   
+   
+   channel0
+   true
+   
DISCARD
+   
524288
+   
4
+   
0
+   
0
+   MMAP
+   
0
+   
100
+   
0
+   
0
+   
0
+   
+   
+   
tp:tptest
+   
true
+   
TRACEPOINT
+   
ALL
+   
+   
+   
+   
+   
+   
+   
+   
+   JUL
+   PER_UID
+   
+   
+   
+   LOG4J
+   PER_UID
+   
+   
+   
+   PYTHON
+   PER_UID
+   
+   
+   
+   false
+   
+   
+   true
+   
+   
+   
tcp4://127.0.0.1:5342/my/custom/path5
+   
tcp4://127.0.0.1:5343/
+   
+   
+   
+   
+   
+
diff --git a/tests/regression/tools/base-path/test_ust 
b/tests/regression/tools/base-path/test_ust
index d7e324e7b..d60a2302a 100755
--- a/tests/regression/tools/base-path/test_ust
+++ b/tests/regression/tools/base-path/test_ust
@@ -27,7 +27,7 @@ EVENT_NAME="tp:tptest"
 
 TRACE_PATH=$(mktemp -d)
 
-NUM_TESTS=32
+NUM_TESTS=37
 
 source $TESTDIR/utils/utils.sh
 
@@ -117,7 +117,7 @@ function ust_app_snapshot_base_path ()
 function ust_app_snapshot_add_output_base_path ()
 {
local session_name=$(randstring 16 0)
-   local base_path="my/custom/path3"
+   local base_path="my/custom/path4"
 
diag "Test base path override for remote trace snapshot (URI on 
add-output)"
create_lttng_session_no_output $session_name --snapshot
@@ -142,6 +142,27 @@ function ust_app_snapshot_add_output_base_path ()
fi
 }
 
+function ust_app_stream_base_path_via_load ()
+{
+   local session_name="load-stream-extra-path"
+   local base_path="my/custom/path5"
+
+   diag "Test base path override for trace streaming using lttng load"
+   lttng_load_ok "-i $CURDIR/$session_name.lttng"
+   start_lttng_tracing_ok $session_name
+
+   $TESTAPP_BIN > /dev/null 2>&1
+
+   stop_lttng_tracing_ok $session_name
+   destroy_lttng_session_ok $session_name
+
+   # validate test
+   if validate_trace $EVENT_NAME "$TRACE_PATH/$HOSTNAME/$base_path"; then
+   # only delete if successful
+   rm -rf "$TRACE_PATH"
+   fi
+}
+
 plan_tests $NUM_TESTS
 
 print_test_banner "$TEST_DESC"
@@ -152,7 +173,9 @@ start_lttng_sessiond
 tests=( ust_app_stream_base_path
ust_app_snapshot_create_base_path
ust_app_snapshot_base_path
-   ust_app_snapshot_add_output_base_path )
+   ust_app_snapshot_add_output_base_path
+   ust_app_stream_b

[lttng-dev] [PATCH lttng-tools 2/5] Refactor: Move set session path to own function

2019-10-25 Thread Jonathan Rajotte
Signed-off-by: Jonathan Rajotte 
---

Feel free to squash this into the previous commit.

---
 src/bin/lttng-sessiond/cmd.c | 64 +---
 1 file changed, 44 insertions(+), 20 deletions(-)

diff --git a/src/bin/lttng-sessiond/cmd.c b/src/bin/lttng-sessiond/cmd.c
index 1dec61ea1..3ee390698 100644
--- a/src/bin/lttng-sessiond/cmd.c
+++ b/src/bin/lttng-sessiond/cmd.c
@@ -2739,6 +2739,42 @@ error:
return ret;
 }
 
+/*
+ * Set the base_path of the session only if subdir of a control uris is set.
+ * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
+ */
+static int set_session_base_path_from_uris(struct ltt_session *session,
+   size_t nb_uri,
+   struct lttng_uri *uris)
+{
+   int ret;
+   for (int i = 0; i < nb_uri; i++) {
+   if (uris[i].stype != LTTNG_STREAM_CONTROL ||
+   uris[i].subdir[0] == '\0') {
+   /* Not interested in these URIs */
+   continue;
+   }
+
+   if (session->base_path != NULL) {
+   free(session->base_path);
+   session->base_path = NULL;
+   }
+
+   /* Set session base_path */
+   session->base_path = strdup(uris[i].subdir);
+   if (!session->base_path) {
+   PERROR("Copying base path: %s", uris[i].subdir);
+   ret = LTTNG_ERR_NOMEM;
+   goto error;
+   }
+   DBG2("Setting base path for session %" PRIu64 ": %s",
+   session->id, session->base_path);
+   }
+   ret = LTTNG_OK;
+error:
+   return ret;
+}
+
 /*
  * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
  */
@@ -2759,26 +2795,14 @@ int cmd_set_consumer_uri(struct ltt_session *session, 
size_t nb_uri,
goto error;
}
 
-   for (i = 0; i < nb_uri; i++) {
-   if (uris[i].stype != LTTNG_STREAM_CONTROL ||
-   uris[i].subdir[0] == '\0') {
-   /* Not interested in these URIs */
-   continue;
-   }
-
-   if (session->base_path != NULL) {
-   free(session->base_path);
-   session->base_path = NULL;
-   }
-
-   /* Set session base_path */
-   session->base_path = strdup(uris[i].subdir);
-   if (!session->base_path) {
-   PERROR("Copying base path: %s", uris[i].subdir);
-   goto error;
-   }
-   DBG2("Setting base path for session %" PRIu64 ": %s",
-   session->id, session->base_path);
+   /*
+* Set the session base path if any. This is done inside
+* cmd_set_consumer_uri to preserve backward compatibility of the
+* previous session creation api vs the session descriptor api.
+*/
+   ret = set_session_base_path_from_uris(session, nb_uri, uris);
+   if (ret != LTTNG_OK) {
+   goto error;
}
 
/* Set the "global" consumer URIs */
-- 
2.17.1

___
lttng-dev mailing list
lttng-dev@lists.lttng.org
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev


[lttng-dev] [PATCH lttng-tools 3/5] Cleanup: remove unused internal lttng_session_descriptor_get_base_path

2019-10-25 Thread Jonathan Rajotte
Signed-off-by: Jonathan Rajotte 
---

Feel free to squash this into the previous commits.

---
 include/lttng/session-descriptor-internal.h |  4 
 src/common/session-descriptor.c | 23 -
 2 files changed, 27 deletions(-)

diff --git a/include/lttng/session-descriptor-internal.h 
b/include/lttng/session-descriptor-internal.h
index 18a70bad4..09d493393 100644
--- a/include/lttng/session-descriptor-internal.h
+++ b/include/lttng/session-descriptor-internal.h
@@ -109,8 +109,4 @@ int lttng_session_descriptor_assign(
struct lttng_session_descriptor *dst_descriptor,
const struct lttng_session_descriptor *src_descriptor);
 
-LTTNG_HIDDEN
-int lttng_session_descriptor_get_base_path(struct lttng_session_descriptor 
*dst,
-   const char **base_path);
-
 #endif /* LTTNG_SESSION_DESCRIPTOR_INTERNAL_H */
diff --git a/src/common/session-descriptor.c b/src/common/session-descriptor.c
index 6dea5ee7c..224138dd2 100644
--- a/src/common/session-descriptor.c
+++ b/src/common/session-descriptor.c
@@ -1176,26 +1176,3 @@ int lttng_session_descriptor_assign(
 end:
return ret;
 }
-
-LTTNG_HIDDEN
-int lttng_session_descriptor_get_base_path(struct lttng_session_descriptor 
*dst,
-   const char **_base_path)
-{
-   switch (dst->output_type) {
-   case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK:
-   {
-   if (dst->output.network.control &&
-   dst->output.network.control->subdir[0]) {
-   *_base_path = dst->output.network.control->subdir;
-   } else {
-   *_base_path = NULL;
-   }
-   break;
-   }
-   case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL:
-   case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE:
-   *_base_path = NULL;
-   break;
-   }
-   return 0;
-}
-- 
2.17.1

___
lttng-dev mailing list
lttng-dev@lists.lttng.org
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev


[lttng-dev] [PATCH lttng-tools 5/5] Tests: fix shellcheck warning

2019-10-25 Thread Jonathan Rajotte
No need to use random string for session name here, use the test name.

Signed-off-by: Jonathan Rajotte 
---
 tests/regression/tools/base-path/test_ust | 36 ++-
 1 file changed, 15 insertions(+), 21 deletions(-)

diff --git a/tests/regression/tools/base-path/test_ust 
b/tests/regression/tools/base-path/test_ust
index d60a2302a..84d434551 100755
--- a/tests/regression/tools/base-path/test_ust
+++ b/tests/regression/tools/base-path/test_ust
@@ -16,10 +16,8 @@
 # 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 TEST_DESC="Streaming Base Path Override - User space tracing"
 
-CURDIR=$(dirname $0)/
+CURDIR=$(dirname "$0")/
 TESTDIR=$CURDIR/../../..
-NR_ITER=5
-NR_USEC_WAIT=0
 TESTAPP_PATH="$TESTDIR/utils/testapp"
 TESTAPP_NAME="gen-ust-events"
 TESTAPP_BIN="$TESTAPP_PATH/$TESTAPP_NAME/$TESTAPP_NAME"
@@ -29,7 +27,7 @@ TRACE_PATH=$(mktemp -d)
 
 NUM_TESTS=37
 
-source $TESTDIR/utils/utils.sh
+source "$TESTDIR/utils/utils.sh"
 
 if [ ! -x "$TESTAPP_BIN" ]; then
BAIL_OUT "No UST events binary detected."
@@ -37,7 +35,7 @@ fi
 
 function ust_app_stream_base_path ()
 {
-   local session_name=$(randstring 16 0)
+   local session_name="ust_app_stream_base_path"
local base_path="my/custom/path1"
 
diag "Test base path override for trace streaming"
@@ -52,16 +50,15 @@ function ust_app_stream_base_path ()
destroy_lttng_session_ok $session_name
 
# validate test
-   validate_trace $EVENT_NAME $TRACE_PATH/$HOSTNAME/$base_path
-   if [ $? -eq 0 ]; then
+   if validate_trace $EVENT_NAME "$TRACE_PATH/$HOSTNAME/$base_path"; then
# only delete if successful
-   rm -rf $TRACE_PATH
+   rm -rf "$TRACE_PATH"
fi
 }
 
 function ust_app_snapshot_create_base_path ()
 {
-   local session_name=$(randstring 16 0)
+   local session_name="ust_app_snapshot_create_base_path"
local base_path="my/custom/path2"
 
diag "Test base path override for remote trace snapshot (URI on create)"
@@ -80,16 +77,15 @@ function ust_app_snapshot_create_base_path ()
destroy_lttng_session_ok $session_name
 
# validate test
-   validate_trace $EVENT_NAME $TRACE_PATH/$HOSTNAME/$base_path
-   if [ $? -eq 0 ]; then
+   if validate_trace $EVENT_NAME "$TRACE_PATH/$HOSTNAME/$base_path"; then
# only delete if successful
-   rm -rf $TRACE_PATH
+   rm -rf "$TRACE_PATH"
fi
 }
 
 function ust_app_snapshot_base_path ()
 {
-   local session_name=$(randstring 16 0)
+   local session_name="ust_app_snapshot_base_path"
local base_path="my/custom/path3"
 
diag "Test base path override for remote trace snapshot (URI on 
snapshot)"
@@ -107,16 +103,15 @@ function ust_app_snapshot_base_path ()
destroy_lttng_session_ok $session_name
 
# validate test
-   validate_trace $EVENT_NAME $TRACE_PATH/$HOSTNAME/$base_path
-   if [ $? -eq 0 ]; then
+   if validate_trace $EVENT_NAME "$TRACE_PATH/$HOSTNAME/$base_path"; then
# only delete if successful
-   rm -rf $TRACE_PATH
+   rm -rf "$TRACE_PATH"
fi
 }
 
 function ust_app_snapshot_add_output_base_path ()
 {
-   local session_name=$(randstring 16 0)
+   local session_name="ust_app_snapshot_add_output_base_path"
local base_path="my/custom/path4"
 
diag "Test base path override for remote trace snapshot (URI on 
add-output)"
@@ -135,10 +130,9 @@ function ust_app_snapshot_add_output_base_path ()
destroy_lttng_session_ok $session_name
 
# validate test
-   validate_trace $EVENT_NAME $TRACE_PATH/$HOSTNAME/$base_path
-   if [ $? -eq 0 ]; then
+   if validate_trace $EVENT_NAME "$TRACE_PATH/$HOSTNAME/$base_path"; then
# only delete if successful
-   rm -rf $TRACE_PATH
+   rm -rf "$TRACE_PATH"
fi
 }
 
@@ -176,7 +170,7 @@ tests=( ust_app_stream_base_path
ust_app_snapshot_add_output_base_path
ust_app_stream_base_path_via_load
 )
-for fct_test in ${tests[@]};
+for fct_test in "${tests[@]}";
 do
${fct_test}
 done
-- 
2.17.1

___
lttng-dev mailing list
lttng-dev@lists.lttng.org
https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev