2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include <common/common.hpp>
9 #include <common/compat/errno.hpp>
10 #include <common/sessiond-comm/sessiond-comm.hpp>
12 #include <bin/lttng-sessiond/health-sessiond.hpp>
13 #include <bin/lttng-sessiond/session.hpp>
14 #include <bin/lttng-sessiond/thread.hpp>
15 #include <bin/lttng-sessiond/ust-app.hpp>
19 #include <sys/types.h>
25 #define SESSION1 "test1"
27 #define MAX_SESSIONS 10000
28 #define RANDOM_STRING_LEN 11
30 /* Number of TAP tests in this file */
33 static struct ltt_session_list
*session_list
;
35 static const char alphanum
[] = "0123456789"
36 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
37 "abcdefghijklmnopqrstuvwxyz";
38 static char random_string
[RANDOM_STRING_LEN
];
41 * Return random string of 10 characters.
44 static char *get_random_string()
48 for (i
= 0; i
< RANDOM_STRING_LEN
- 1; i
++) {
49 random_string
[i
] = alphanum
[rand() % (sizeof(alphanum
) - 1)];
52 random_string
[RANDOM_STRING_LEN
- 1] = '\0';
58 * Return 0 if session name is found, else -1
60 static int find_session_name(const char *name
)
62 struct ltt_session
*iter
;
64 cds_list_for_each_entry (iter
, &session_list
->head
, list
) {
65 if (strcmp(iter
->name
, name
) == 0) {
73 static int session_list_count()
76 struct ltt_session
*iter
;
78 cds_list_for_each_entry (iter
, &session_list
->head
, list
) {
85 * Empty session list manually.
87 static void empty_session_list()
89 struct ltt_session
*iter
, *tmp
;
91 const auto list_lock
= lttng::sessiond::lock_session_list();
92 cds_list_for_each_entry_safe (iter
, tmp
, &session_list
->head
, list
) {
93 session_destroy(iter
);
96 /* Session list must be 0 */
97 LTTNG_ASSERT(!session_list_count());
101 * Test creation of 1 session
103 static int create_one_session(const char *name
)
106 enum lttng_error_code ret_code
;
107 struct ltt_session
*session
= nullptr;
109 const auto list_lock
= lttng::sessiond::lock_session_list();
110 ret_code
= session_create(name
, geteuid(), getegid(), &session
);
111 session_put(session
);
112 if (ret_code
== LTTNG_OK
) {
114 ret
= find_session_name(name
);
116 /* Session not found by name */
117 printf("session not found after creation\n");
124 if (ret_code
== LTTNG_ERR_EXIST_SESS
) {
125 printf("(session already exists) ");
134 * Test deletion of 1 session
136 static int destroy_one_session(struct ltt_session
*session
)
139 char session_name
[NAME_MAX
];
141 strncpy(session_name
, session
->name
, sizeof(session_name
));
142 session_name
[sizeof(session_name
) - 1] = '\0';
144 session_destroy(session
);
145 session_put(session
);
147 ret
= find_session_name(session_name
);
149 /* Success, -1 means that the sesion is NOT found */
160 * This test is supposed to fail at the second create call. If so, return 0 for
161 * test success, else -1.
163 static int two_session_same_name()
165 const auto ret
= create_one_session(SESSION1
);
172 * Mind the order of the declaration of list_lock vs session:
173 * the session list lock must always be released _after_ the release of
174 * a session's reference (the destruction of a ref/locked_ref) to ensure
175 * since the reference's release may unpublish the session from the list of
178 const auto list_lock
= lttng::sessiond::lock_session_list();
180 const auto session
= ltt_session::find_session(SESSION1
);
183 } catch (const lttng::sessiond::exceptions::session_not_found_error
& ex
) {
189 static void test_session_list()
191 session_list
= session_get_list();
192 ok(session_list
!= nullptr, "Session list: not NULL");
195 static void test_create_one_session()
197 ok(create_one_session(SESSION1
) == 0, "Create session: %s", SESSION1
);
200 static void test_validate_session()
203 * Mind the order of the declaration of list_lock vs session:
204 * the session list lock must always be released _after_ the release of
205 * a session's reference (the destruction of a ref/locked_ref) to ensure
206 * since the reference's release may unpublish the session from the list of
209 const auto list_lock
= lttng::sessiond::lock_session_list();
210 ltt_session::ref session
;
213 session
= ltt_session::find_session(SESSION1
);
214 } catch (const lttng::sessiond::exceptions::session_not_found_error
& ex
) {
217 ok(session
, "Validating session: session found");
220 ok(session
->kernel_session
== nullptr && strlen(session
->name
),
221 "Validating session: basic sanity check");
223 skip(1, "Skipping session validation check as session was not found");
231 static void test_destroy_session()
234 * Mind the order of the declaration of list_lock vs session:
235 * the session list lock must always be released _after_ the release of
236 * a session's reference (the destruction of a ref/locked_ref) to ensure
237 * since the reference's release may unpublish the session from the list of
240 const auto list_lock
= lttng::sessiond::lock_session_list();
241 ltt_session::ref session
;
244 session
= ltt_session::find_session(SESSION1
);
245 } catch (const lttng::sessiond::exceptions::session_not_found_error
& ex
) {
248 ok(session
, "Destroying session: session found");
251 ok(destroy_one_session(session
.release()) == 0,
252 "Destroying session: %s destroyed",
255 skip(1, "Skipping session destruction as it was not found");
259 static void test_duplicate_session()
261 ok(two_session_same_name() == 0, "Duplicate session creation");
264 static void test_session_name_generation()
266 struct ltt_session
*session
= nullptr;
267 enum lttng_error_code ret_code
;
268 const char *expected_session_name_prefix
= DEFAULT_SESSION_NAME
;
270 const auto list_lock
= lttng::sessiond::lock_session_list();
272 ret_code
= session_create(nullptr, geteuid(), getegid(), &session
);
273 ok(ret_code
== LTTNG_OK
, "Create session with a NULL name (auto-generate a name)");
275 skip(1, "Skipping session name generation tests as session_create() failed.");
278 diag("Automatically-generated session name: %s", *session
->name
? session
->name
: "ERROR");
280 !strncmp(expected_session_name_prefix
,
282 sizeof(DEFAULT_SESSION_NAME
) - 1),
283 "Auto-generated session name starts with %s",
284 DEFAULT_SESSION_NAME
);
286 session_put(session
);
289 static void test_large_session_number()
291 int ret
, i
, failed
= 0;
292 struct ltt_session
*iter
, *tmp
;
294 for (i
= 0; i
< MAX_SESSIONS
; i
++) {
295 char *tmp_name
= get_random_string();
296 ret
= create_one_session(tmp_name
);
298 diag("session %d (name: %s) creation failed", i
, tmp_name
);
303 ok(failed
== 0, "Large sessions number: created %u sessions", MAX_SESSIONS
);
307 const auto list_lock
= lttng::sessiond::lock_session_list();
308 for (i
= 0; i
< MAX_SESSIONS
; i
++) {
309 cds_list_for_each_entry_safe (iter
, tmp
, &session_list
->head
, list
) {
310 LTTNG_ASSERT(session_get(iter
));
311 ret
= destroy_one_session(iter
);
313 diag("session %d destroy failed", i
);
319 ok(failed
== 0 && session_list_count() == 0,
320 "Large sessions number: destroyed %u sessions",
326 plan_tests(NUM_TESTS
);
328 the_health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
330 diag("Sessions unit tests");
332 rcu_register_thread();
336 test_create_one_session();
338 test_validate_session();
340 test_destroy_session();
342 test_duplicate_session();
344 empty_session_list();
346 test_session_name_generation();
348 test_large_session_number();
350 rcu_unregister_thread();
351 lttng_thread_list_shutdown_orphans();
353 return exit_status();