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(ltt_session::ref 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 /* Reference of the session list. */
145 ltt_session
*weak_session_ptr
= &session
.get();
147 /* Drop the reference that stems from the look-up. */
148 const ltt_session::ref reference_to_drop
= std::move(session
);
151 session_destroy(weak_session_ptr
);
153 ret
= find_session_name(session_name
);
155 /* Success, -1 means that the sesion is NOT found */
166 * This test is supposed to fail at the second create call. If so, return 0 for
167 * test success, else -1.
169 static int two_session_same_name()
171 const auto ret
= create_one_session(SESSION1
);
178 * Mind the order of the declaration of list_lock vs session:
179 * the session list lock must always be released _after_ the release of
180 * a session's reference (the destruction of a ref/locked_ref) to ensure
181 * since the reference's release may unpublish the session from the list of
184 const auto list_lock
= lttng::sessiond::lock_session_list();
186 const auto session
= ltt_session::find_session(SESSION1
);
189 } catch (const lttng::sessiond::exceptions::session_not_found_error
& ex
) {
195 static void test_session_list()
197 session_list
= session_get_list();
198 ok(session_list
!= nullptr, "Session list: not NULL");
201 static void test_create_one_session()
203 ok(create_one_session(SESSION1
) == 0, "Create session: %s", SESSION1
);
206 static void test_validate_session()
209 * Mind the order of the declaration of list_lock vs session:
210 * the session list lock must always be released _after_ the release of
211 * a session's reference (the destruction of a ref/locked_ref) to ensure
212 * since the reference's release may unpublish the session from the list of
215 const auto list_lock
= lttng::sessiond::lock_session_list();
218 const auto session
= ltt_session::find_session(SESSION1
);
219 pass("Validating session: session found");
221 ok(session
->kernel_session
== nullptr && strlen(session
->name
),
222 "Validating session: basic sanity check");
226 } catch (const lttng::sessiond::exceptions::session_not_found_error
& ex
) {
227 fail("Validating session: session found");
228 skip(1, "Skipping session validation check as session was not found");
232 static void test_destroy_session()
235 * Mind the order of the declaration of list_lock vs session:
236 * the session list lock must always be released _after_ the release of
237 * a session's reference (the destruction of a ref/locked_ref) to ensure
238 * since the reference's release may unpublish the session from the list of
241 const auto list_lock
= lttng::sessiond::lock_session_list();
244 auto session
= ltt_session::find_session(SESSION1
);
246 pass("Destroying session: session found");
248 ok(destroy_one_session(std::move(session
)) == 0,
249 "Destroying session: %s destroyed",
252 } catch (const lttng::sessiond::exceptions::session_not_found_error
& ex
) {
253 fail("Destroying session: session found");
254 skip(1, "Skipping session destruction as it was not found");
258 static void test_duplicate_session()
260 ok(two_session_same_name() == 0, "Duplicate session creation");
263 static void test_session_name_generation()
265 struct ltt_session
*session
= nullptr;
266 enum lttng_error_code ret_code
;
267 const char *expected_session_name_prefix
= DEFAULT_SESSION_NAME
;
269 const auto list_lock
= lttng::sessiond::lock_session_list();
271 ret_code
= session_create(nullptr, geteuid(), getegid(), &session
);
272 ok(ret_code
== LTTNG_OK
, "Create session with a NULL name (auto-generate a name)");
274 skip(1, "Skipping session name generation tests as session_create() failed.");
277 diag("Automatically-generated session name: %s", *session
->name
? session
->name
: "ERROR");
279 !strncmp(expected_session_name_prefix
,
281 sizeof(DEFAULT_SESSION_NAME
) - 1),
282 "Auto-generated session name starts with %s",
283 DEFAULT_SESSION_NAME
);
285 session_put(session
);
288 static void test_large_session_number()
290 int ret
, i
, failed
= 0;
291 struct ltt_session
*iter
, *tmp
;
293 for (i
= 0; i
< MAX_SESSIONS
; i
++) {
294 char *tmp_name
= get_random_string();
295 ret
= create_one_session(tmp_name
);
297 diag("session %d (name: %s) creation failed", i
, tmp_name
);
302 ok(failed
== 0, "Large sessions number: created %u sessions", MAX_SESSIONS
);
306 const auto list_lock
= lttng::sessiond::lock_session_list();
307 for (i
= 0; i
< MAX_SESSIONS
; i
++) {
308 cds_list_for_each_entry_safe (iter
, tmp
, &session_list
->head
, list
) {
309 ret
= destroy_one_session([iter
]() {
311 return ltt_session::make_ref(*iter
);
315 diag("session %d destroy failed", i
);
321 ok(failed
== 0 && session_list_count() == 0,
322 "Large sessions number: destroyed %u sessions",
328 plan_tests(NUM_TESTS
);
330 the_health_sessiond
= health_app_create(NR_HEALTH_SESSIOND_TYPES
);
332 diag("Sessions unit tests");
334 rcu_register_thread();
338 test_create_one_session();
340 test_validate_session();
342 test_destroy_session();
344 test_duplicate_session();
346 empty_session_list();
348 test_session_name_generation();
350 test_large_session_number();
352 rcu_unregister_thread();
353 lttng_thread_list_shutdown_orphans();
355 return exit_status();