| 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> |
| 27 | #include <sys/types.h> |
| 28 | |
| 29 | #include <bin/lttng-sessiond/session.h> |
| 30 | #include <common/sessiond-comm/sessiond-comm.h> |
| 31 | #include <common/common.h> |
| 32 | |
| 33 | #include "utils.h" |
| 34 | |
| 35 | #define SESSION1 "test1" |
| 36 | |
| 37 | /* This path will NEVER be created in this test */ |
| 38 | #define PATH1 "/tmp/.test-junk-lttng" |
| 39 | |
| 40 | #define MAX_SESSIONS 10000 |
| 41 | #define RANDOM_STRING_LEN 11 |
| 42 | |
| 43 | /* |
| 44 | * String of 263 caracters. NAME_MAX + "OVERFLOW". If OVERFLOW appears in the |
| 45 | * session name, we have a problem. |
| 46 | * |
| 47 | * NAME_MAX = 255 |
| 48 | */ |
| 49 | #define OVERFLOW_SESSION_NAME \ |
| 50 | "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd" \ |
| 51 | "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd" \ |
| 52 | "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd" \ |
| 53 | "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabc" \ |
| 54 | "OVERFLOW" |
| 55 | |
| 56 | static struct ltt_session_list *session_list; |
| 57 | |
| 58 | /* For lttngerr.h */ |
| 59 | int lttng_opt_quiet = 1; |
| 60 | int lttng_opt_verbose = 0; |
| 61 | |
| 62 | static const char alphanum[] = |
| 63 | "0123456789" |
| 64 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 65 | "abcdefghijklmnopqrstuvwxyz"; |
| 66 | static char random_string[RANDOM_STRING_LEN]; |
| 67 | |
| 68 | /* |
| 69 | * Return random string of 10 characters. |
| 70 | * Not thread-safe. |
| 71 | */ |
| 72 | static char *get_random_string(void) |
| 73 | { |
| 74 | int i; |
| 75 | |
| 76 | for (i = 0; i < RANDOM_STRING_LEN - 1; i++) { |
| 77 | random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)]; |
| 78 | } |
| 79 | |
| 80 | random_string[RANDOM_STRING_LEN - 1] = '\0'; |
| 81 | |
| 82 | return random_string; |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * Return 0 if session name is found, else -1 |
| 87 | */ |
| 88 | static int find_session_name(char *name) |
| 89 | { |
| 90 | struct ltt_session *iter; |
| 91 | |
| 92 | cds_list_for_each_entry(iter, &session_list->head, list) { |
| 93 | if (strcmp(iter->name, name) == 0) { |
| 94 | return 0; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return -1; |
| 99 | } |
| 100 | |
| 101 | static int session_list_count(void) |
| 102 | { |
| 103 | int count = 0; |
| 104 | struct ltt_session *iter; |
| 105 | |
| 106 | cds_list_for_each_entry(iter, &session_list->head, list) { |
| 107 | count++; |
| 108 | } |
| 109 | return count; |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * Empty session list manually. |
| 114 | */ |
| 115 | static void empty_session_list(void) |
| 116 | { |
| 117 | struct ltt_session *iter, *tmp; |
| 118 | |
| 119 | cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) { |
| 120 | cds_list_del(&iter->list); |
| 121 | free(iter); |
| 122 | } |
| 123 | |
| 124 | /* Session list must be 0 */ |
| 125 | assert(!session_list_count()); |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * Test creation of 1 session |
| 130 | */ |
| 131 | static int create_one_session(char *name, char *path) |
| 132 | { |
| 133 | int ret; |
| 134 | |
| 135 | ret = session_create(name, path, geteuid(), getegid()); |
| 136 | if (ret == LTTNG_OK) { |
| 137 | /* Validate */ |
| 138 | ret = find_session_name(name); |
| 139 | if (ret < 0) { |
| 140 | /* Session not found by name */ |
| 141 | printf("session not found after creation\n"); |
| 142 | return -1; |
| 143 | } else { |
| 144 | /* Success */ |
| 145 | return 0; |
| 146 | } |
| 147 | } else { |
| 148 | if (ret == LTTNG_ERR_EXIST_SESS) { |
| 149 | printf("(session already exists) "); |
| 150 | } |
| 151 | return -1; |
| 152 | } |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * Test deletion of 1 session |
| 159 | */ |
| 160 | static int destroy_one_session(struct ltt_session *session) |
| 161 | { |
| 162 | int ret; |
| 163 | |
| 164 | ret = session_destroy(session); |
| 165 | |
| 166 | if (ret == LTTNG_OK) { |
| 167 | /* Validate */ |
| 168 | if (session == NULL) { |
| 169 | return 0; |
| 170 | } |
| 171 | ret = find_session_name(session->name); |
| 172 | if (ret < 0) { |
| 173 | /* Success, -1 means that the sesion is NOT found */ |
| 174 | return 0; |
| 175 | } else { |
| 176 | /* Fail */ |
| 177 | return -1; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | static int fuzzing_create_args(void) |
| 185 | { |
| 186 | int ret; |
| 187 | |
| 188 | ret = create_one_session(NULL, NULL); |
| 189 | if (ret > 0) { |
| 190 | printf("Session created with (null),(null)\n"); |
| 191 | return -1; |
| 192 | } |
| 193 | |
| 194 | ret = create_one_session(NULL, PATH1); |
| 195 | if (ret > 0) { |
| 196 | printf("Session created with (null), %s)\n", PATH1); |
| 197 | return -1; |
| 198 | } |
| 199 | |
| 200 | /* Session list must be 0 */ |
| 201 | assert(!session_list_count()); |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * This test is supposed to fail at the second create call. If so, return 0 for |
| 208 | * test success, else -1. |
| 209 | */ |
| 210 | static int two_session_same_name(void) |
| 211 | { |
| 212 | int ret; |
| 213 | struct ltt_session *sess; |
| 214 | |
| 215 | ret = create_one_session(SESSION1, PATH1); |
| 216 | if (ret < 0) { |
| 217 | /* Fail */ |
| 218 | return -1; |
| 219 | } |
| 220 | |
| 221 | sess = session_find_by_name(SESSION1); |
| 222 | if (sess) { |
| 223 | /* Success */ |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | /* Fail */ |
| 228 | return -1; |
| 229 | } |
| 230 | |
| 231 | int main(int argc, char **argv) |
| 232 | { |
| 233 | int ret, i; |
| 234 | struct ltt_session *iter, *tmp; |
| 235 | |
| 236 | srand(time(NULL)); |
| 237 | |
| 238 | printf("\nTesting Sessions:\n-----------\n"); |
| 239 | |
| 240 | session_list = session_get_list(); |
| 241 | if (session_list == NULL) { |
| 242 | return -1; |
| 243 | } |
| 244 | |
| 245 | printf("Create 1 session %s: ", SESSION1); |
| 246 | fflush(stdout); |
| 247 | ret = create_one_session(SESSION1, PATH1); |
| 248 | if (ret < 0) { |
| 249 | return -1; |
| 250 | } |
| 251 | PRINT_OK(); |
| 252 | |
| 253 | printf("Validating created session %s: ", SESSION1); |
| 254 | fflush(stdout); |
| 255 | tmp = session_find_by_name(SESSION1); |
| 256 | if (tmp == NULL) { |
| 257 | return -1; |
| 258 | } |
| 259 | /* Basic init session values */ |
| 260 | assert(tmp->kernel_session == NULL); |
| 261 | assert(strlen(tmp->path)); |
| 262 | assert(strlen(tmp->name)); |
| 263 | session_lock(tmp); |
| 264 | session_unlock(tmp); |
| 265 | |
| 266 | PRINT_OK(); |
| 267 | |
| 268 | printf("Destroy 1 session %s: ", SESSION1); |
| 269 | fflush(stdout); |
| 270 | ret = destroy_one_session(tmp); |
| 271 | if (ret < 0) { |
| 272 | return -1; |
| 273 | } |
| 274 | PRINT_OK(); |
| 275 | |
| 276 | printf("Two session with same name: "); |
| 277 | fflush(stdout); |
| 278 | ret = two_session_same_name(); |
| 279 | if (ret < 0) { |
| 280 | return -1; |
| 281 | } |
| 282 | PRINT_OK(); |
| 283 | |
| 284 | empty_session_list(); |
| 285 | |
| 286 | printf("Fuzzing create_session arguments: "); |
| 287 | fflush(stdout); |
| 288 | ret = fuzzing_create_args(); |
| 289 | if (ret < 0) { |
| 290 | return -1; |
| 291 | } |
| 292 | PRINT_OK(); |
| 293 | |
| 294 | printf("Creating %d sessions: ", MAX_SESSIONS); |
| 295 | fflush(stdout); |
| 296 | for (i = 0; i < MAX_SESSIONS; i++) { |
| 297 | char *tmp_name = get_random_string(); |
| 298 | |
| 299 | ret = create_one_session(tmp_name, PATH1); |
| 300 | if (ret < 0) { |
| 301 | printf("session %d (name: %s) creation failed\n", i, tmp_name); |
| 302 | return -1; |
| 303 | } |
| 304 | |
| 305 | if ((i % 1000) == 0) { |
| 306 | fprintf(stdout, "%d..", i); |
| 307 | fflush(stdout); |
| 308 | } |
| 309 | } |
| 310 | PRINT_OK(); |
| 311 | |
| 312 | printf("Destroying %d sessions: ", MAX_SESSIONS); |
| 313 | fflush(stdout); |
| 314 | for (i = 0; i < MAX_SESSIONS; i++) { |
| 315 | cds_list_for_each_entry_safe(iter, tmp, &session_list->head, list) { |
| 316 | ret = destroy_one_session(iter); |
| 317 | if (ret < 0) { |
| 318 | printf("session %d (name: %s) creation failed\n", i, iter->name); |
| 319 | return -1; |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | PRINT_OK(); |
| 324 | |
| 325 | /* Session list must be 0 */ |
| 326 | assert(!session_list_count()); |
| 327 | |
| 328 | /* Success */ |
| 329 | return 0; |
| 330 | } |