librunas: clone another process also if not root
[lttng-tools.git] / tests / test_sessions.c
CommitLineData
63371d1e
DG
1/*
2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19#define _GNU_SOURCE
20#include <assert.h>
21#include <errno.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26#include <time.h>
6df2e2c9 27#include <sys/types.h>
63371d1e 28
54d01ffb 29#include <lttng-sessiond-comm.h>
f6a9efaa 30
32258573 31#include <lttng-sessiond/session.h>
897b8e23 32#include "utils.h"
63371d1e
DG
33
34#define SESSION1 "test1"
35
36/* This path will NEVER be created in this test */
37#define PATH1 "/tmp/.test-junk-lttng"
38
39#define MAX_SESSIONS 10000
40
41/*
42 * String of 263 caracters. NAME_MAX + "OVERFLOW". If OVERFLOW appears in the
43 * session name, we have a problem.
44 *
45 * NAME_MAX = 255
46 */
47#define OVERFLOW_SESSION_NAME \
48 "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd" \
49 "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd" \
50 "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd" \
51 "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabc" \
52 "OVERFLOW"
53
54static struct ltt_session_list *session_list;
55
56/* For lttngerr.h */
57int opt_quiet = 1;
58int opt_verbose = 0;
59
60static const char alphanum[] =
61 "0123456789"
62 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
63 "abcdefghijklmnopqrstuvwxyz";
64
65/*
66 * Return random string of 10 characters.
67 */
68static char *get_random_string(void)
69{
70 int i;
71 char *str = malloc(11);
72
73 for (i = 0; i < 10; i++) {
74 str[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
75 }
76
77 str[10] = '\0';
78
79 return str;
80}
81
82/*
83 * Return 0 if session name is found, else -1
84 */
85static int find_session_name(char *name)
86{
87 struct ltt_session *iter;
88
89 cds_list_for_each_entry(iter, &session_list->head, list) {
90 if (strcmp(iter->name, name) == 0) {
91 return 0;
92 }
93 }
94
95 return -1;
96}
97
98/*
99 * Empty session list manually.
100 */
101static void empty_session_list(void)
102{
103 struct ltt_session *iter, *tmp;
104
105 cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) {
106 cds_list_del(&iter->list);
107 session_list->count--;
108 free(iter);
109 }
110
111 /* Session list must be 0 */
112 assert(!session_list->count);
113}
114
115/*
116 * Test creation of 1 session
117 */
118static int create_one_session(char *name, char *path)
119{
120 int ret;
121
6df2e2c9 122 ret = session_create(name, path, geteuid(), getegid());
54d01ffb 123 if (ret == LTTCOMM_OK) {
63371d1e
DG
124 /* Validate */
125 ret = find_session_name(name);
126 if (ret < 0) {
127 /* Session not found by name */
128 printf("session not found after creation\n");
129 return -1;
130 } else {
131 /* Success */
132 return 0;
133 }
54d01ffb
DG
134 } else {
135 if (ret == LTTCOMM_EXIST_SESS) {
63371d1e
DG
136 printf("(session already exists) ");
137 }
138 return -1;
139 }
140
141 return 0;
142}
143
144/*
145 * Test deletion of 1 session
146 */
271933a4 147static int destroy_one_session(struct ltt_session *session)
63371d1e
DG
148{
149 int ret;
150
271933a4 151 ret = session_destroy(session);
54d01ffb
DG
152
153 if (ret == LTTCOMM_OK) {
63371d1e 154 /* Validate */
271933a4
DG
155 if (session == NULL) {
156 return 0;
157 }
158 ret = find_session_name(session->name);
63371d1e
DG
159 if (ret < 0) {
160 /* Success, -1 means that the sesion is NOT found */
161 return 0;
162 } else {
163 /* Fail */
164 return -1;
165 }
63371d1e
DG
166 }
167
168 return 0;
169}
170
171static int fuzzing_create_args(void)
172{
173 int ret;
174
175 ret = create_one_session(NULL, NULL);
54d01ffb 176 if (ret > 0) {
63371d1e
DG
177 printf("Session created with (null),(null)\n");
178 return -1;
179 }
180
181 ret = create_one_session(NULL, PATH1);
54d01ffb 182 if (ret > 0) {
63371d1e
DG
183 printf("Session created with (null), %s)\n", PATH1);
184 return -1;
185 }
186
187 ret = create_one_session(SESSION1, NULL);
54d01ffb 188 if (ret > 0) {
63371d1e
DG
189 printf("Session created with %s, (null)\n", SESSION1);
190 return -1;
191 }
192
193 /* Session list must be 0 */
194 assert(!session_list->count);
195
196 return 0;
197}
198
199static int fuzzing_destroy_args(void)
200{
201 int ret;
202
203 ret = destroy_one_session(NULL);
54d01ffb 204 if (ret > 0) {
63371d1e
DG
205 printf("Session destroyed with (null)\n");
206 return -1;
207 }
208
63371d1e
DG
209 /* Session list must be 0 */
210 assert(!session_list->count);
211
212 return 0;
213}
214
215/*
216 * This test is supposed to fail at the second create call. If so, return 0 for
217 * test success, else -1.
218 */
219static int two_session_same_name(void)
220{
221 int ret;
222
223 ret = create_one_session(SESSION1, PATH1);
224 if (ret < 0) {
225 /* Fail */
226 return -1;
227 }
228
229 ret = create_one_session(SESSION1, PATH1);
230 if (ret < 0) {
231 /* Success */
232 return 0;
233 }
234
235 /* Fail */
236 return -1;
237}
238
239int main(int argc, char **argv)
240{
241 int ret, i;
242 char *tmp_name;
243 struct ltt_session *iter, *tmp;
244
245 srand(time(NULL));
246
247 printf("\nTesting Sessions:\n-----------\n");
248
54d01ffb 249 session_list = session_get_list();
63371d1e
DG
250 if (session_list == NULL) {
251 return -1;
252 }
253
254 printf("Create 1 session %s: ", SESSION1);
255 ret = create_one_session(SESSION1, PATH1);
256 if (ret < 0) {
257 return -1;
258 }
897b8e23 259 PRINT_OK();
63371d1e
DG
260
261 printf("Validating created session %s: ", SESSION1);
54d01ffb 262 tmp = session_find_by_name(SESSION1);
63371d1e
DG
263 if (tmp == NULL) {
264 return -1;
265 }
266 /* Basic init session values */
267 assert(tmp->kernel_session == NULL);
63371d1e
DG
268 assert(strlen(tmp->path));
269 assert(strlen(tmp->name));
54d01ffb
DG
270 session_lock(tmp);
271 session_unlock(tmp);
63371d1e 272
897b8e23 273 PRINT_OK();
63371d1e
DG
274
275 printf("Destroy 1 session %s: ", SESSION1);
271933a4 276 ret = destroy_one_session(tmp);
63371d1e
DG
277 if (ret < 0) {
278 return -1;
279 }
897b8e23 280 PRINT_OK();
63371d1e
DG
281
282 printf("Two session with same name: ");
283 ret = two_session_same_name();
284 if (ret < 0) {
285 return -1;
286 }
897b8e23 287 PRINT_OK();
63371d1e
DG
288
289 empty_session_list();
290
291 printf("Fuzzing create_session arguments: ");
292 ret = fuzzing_create_args();
293 if (ret < 0) {
294 return -1;
295 }
897b8e23 296 PRINT_OK();
63371d1e
DG
297
298 printf("Fuzzing destroy_session argument: ");
299 ret = fuzzing_destroy_args();
300 if (ret < 0) {
301 return -1;
302 }
897b8e23 303 PRINT_OK();
63371d1e
DG
304
305 printf("Creating %d sessions: ", MAX_SESSIONS);
306 for (i = 0; i < MAX_SESSIONS; i++) {
307 tmp_name = get_random_string();
308 ret = create_one_session(tmp_name, PATH1);
309 if (ret < 0) {
310 printf("session %d (name: %s) creation failed\n", i, tmp_name);
311 return -1;
312 }
313 free(tmp_name);
314 }
897b8e23 315 PRINT_OK();
63371d1e
DG
316
317 printf("Destroying %d sessions: ", MAX_SESSIONS);
318 for (i = 0; i < MAX_SESSIONS; i++) {
319 cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) {
271933a4 320 ret = destroy_one_session(iter);
63371d1e
DG
321 if (ret < 0) {
322 printf("session %d (name: %s) creation failed\n", i, iter->name);
323 return -1;
324 }
325 }
326 }
897b8e23 327 PRINT_OK();
63371d1e
DG
328
329 /* Session list must be 0 */
330 assert(!session_list->count);
331
332 /* Success */
333 return 0;
334}
This page took 0.037663 seconds and 4 git commands to generate.