| 1 | /* |
| 2 | * Copyright (C) 2011 EfficiOS Inc. |
| 3 | * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0-only |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #define _LGPL_SOURCE |
| 10 | #include "../command.hpp" |
| 11 | #include "../utils.hpp" |
| 12 | |
| 13 | #include <common/compat/time.hpp> |
| 14 | #include <common/defaults.hpp> |
| 15 | #include <common/mi-lttng.hpp> |
| 16 | #include <common/path.hpp> |
| 17 | #include <common/sessiond-comm/sessiond-comm.hpp> |
| 18 | #include <common/uri.hpp> |
| 19 | #include <common/utils.hpp> |
| 20 | |
| 21 | #include <lttng/lttng.h> |
| 22 | |
| 23 | #include <ctype.h> |
| 24 | #include <popt.h> |
| 25 | #include <signal.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <sys/stat.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/wait.h> |
| 32 | #include <unistd.h> |
| 33 | |
| 34 | static char *opt_output_path; |
| 35 | static char *opt_url; |
| 36 | static char *opt_ctrl_url; |
| 37 | static char *opt_data_url; |
| 38 | static char *opt_shm_path; |
| 39 | static int opt_no_consumer; |
| 40 | static int opt_no_output; |
| 41 | static int opt_snapshot; |
| 42 | static uint32_t opt_live_timer; |
| 43 | |
| 44 | #ifdef LTTNG_EMBED_HELP |
| 45 | static const char help_msg[] = |
| 46 | #include <lttng-create.1.h> |
| 47 | ; |
| 48 | #endif |
| 49 | |
| 50 | enum { |
| 51 | OPT_HELP = 1, |
| 52 | OPT_LIST_OPTIONS, |
| 53 | OPT_LIVE_TIMER, |
| 54 | }; |
| 55 | |
| 56 | enum output_type { |
| 57 | OUTPUT_NONE, |
| 58 | OUTPUT_LOCAL, |
| 59 | OUTPUT_NETWORK, |
| 60 | OUTPUT_UNSPECIFIED, |
| 61 | }; |
| 62 | |
| 63 | static struct mi_writer *writer; |
| 64 | static struct poptOption long_options[] = { |
| 65 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 66 | { "help", 'h', POPT_ARG_NONE, nullptr, OPT_HELP, nullptr, nullptr }, |
| 67 | { "output", 'o', POPT_ARG_STRING, &opt_output_path, 0, nullptr, nullptr }, |
| 68 | { "list-options", 0, POPT_ARG_NONE, nullptr, OPT_LIST_OPTIONS, nullptr, nullptr }, |
| 69 | { "set-url", 'U', POPT_ARG_STRING, &opt_url, 0, nullptr, nullptr }, |
| 70 | { "ctrl-url", 'C', POPT_ARG_STRING, &opt_ctrl_url, 0, nullptr, nullptr }, |
| 71 | { "data-url", 'D', POPT_ARG_STRING, &opt_data_url, 0, nullptr, nullptr }, |
| 72 | { "no-output", 0, POPT_ARG_VAL, &opt_no_output, 1, nullptr, nullptr }, |
| 73 | { "no-consumer", 0, POPT_ARG_VAL, &opt_no_consumer, 1, nullptr, nullptr }, |
| 74 | { "snapshot", 0, POPT_ARG_VAL, &opt_snapshot, 1, nullptr, nullptr }, |
| 75 | { "live", |
| 76 | 0, |
| 77 | POPT_ARG_INT | POPT_ARGFLAG_OPTIONAL, |
| 78 | nullptr, |
| 79 | OPT_LIVE_TIMER, |
| 80 | nullptr, |
| 81 | nullptr }, |
| 82 | { "shm-path", 0, POPT_ARG_STRING, &opt_shm_path, 0, nullptr, nullptr }, |
| 83 | { nullptr, 0, 0, nullptr, 0, nullptr, nullptr } |
| 84 | }; |
| 85 | |
| 86 | /* |
| 87 | * Retrieve the created session and mi output it based on provided argument |
| 88 | * This is currently a summary of what was pretty printed and is subject to |
| 89 | * enhancements. |
| 90 | */ |
| 91 | static int mi_created_session(const char *session_name) |
| 92 | { |
| 93 | int ret, i, count, found; |
| 94 | struct lttng_session *sessions; |
| 95 | |
| 96 | /* session_name should not be null */ |
| 97 | LTTNG_ASSERT(session_name); |
| 98 | LTTNG_ASSERT(writer); |
| 99 | |
| 100 | count = lttng_list_sessions(&sessions); |
| 101 | if (count < 0) { |
| 102 | ret = count; |
| 103 | ERR("%s", lttng_strerror(ret)); |
| 104 | goto error; |
| 105 | } |
| 106 | |
| 107 | if (count == 0) { |
| 108 | ERR("Error session creation failed: session %s not found", session_name); |
| 109 | ret = -LTTNG_ERR_SESS_NOT_FOUND; |
| 110 | goto end; |
| 111 | } |
| 112 | |
| 113 | found = 0; |
| 114 | for (i = 0; i < count; i++) { |
| 115 | if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) { |
| 116 | found = 1; |
| 117 | ret = mi_lttng_session(writer, &sessions[i], 0); |
| 118 | if (ret) { |
| 119 | goto error; |
| 120 | } |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if (!found) { |
| 126 | ret = -LTTNG_ERR_SESS_NOT_FOUND; |
| 127 | } else { |
| 128 | ret = CMD_SUCCESS; |
| 129 | } |
| 130 | |
| 131 | error: |
| 132 | free(sessions); |
| 133 | end: |
| 134 | return ret; |
| 135 | } |
| 136 | |
| 137 | static struct lttng_session_descriptor *create_session_descriptor(const char *session_name) |
| 138 | { |
| 139 | ssize_t uri_count; |
| 140 | enum output_type output_type; |
| 141 | struct lttng_uri *uris = nullptr; |
| 142 | struct lttng_session_descriptor *descriptor = nullptr; |
| 143 | const char *uri_str1 = nullptr, *uri_str2 = nullptr; |
| 144 | char local_output_path[LTTNG_PATH_MAX] = {}; |
| 145 | |
| 146 | if (opt_no_output) { |
| 147 | output_type = OUTPUT_NONE; |
| 148 | } else if (opt_output_path) { |
| 149 | char *expanded_output_path; |
| 150 | int ret; |
| 151 | |
| 152 | output_type = OUTPUT_LOCAL; |
| 153 | expanded_output_path = utils_expand_path(opt_output_path); |
| 154 | if (!expanded_output_path) { |
| 155 | ERR("Failed to expand output path."); |
| 156 | goto end; |
| 157 | } |
| 158 | ret = lttng_strncpy( |
| 159 | local_output_path, expanded_output_path, sizeof(local_output_path)); |
| 160 | free(expanded_output_path); |
| 161 | if (ret) { |
| 162 | ERR("Output path exceeds the maximal supported length (%zu bytes)", |
| 163 | sizeof(local_output_path)); |
| 164 | goto end; |
| 165 | } |
| 166 | } else if (opt_url || opt_ctrl_url) { |
| 167 | int ret; |
| 168 | |
| 169 | uri_str1 = opt_ctrl_url ? opt_ctrl_url : opt_url; |
| 170 | uri_str2 = opt_data_url; |
| 171 | |
| 172 | uri_count = uri_parse_str_urls(uri_str1, uri_str2, &uris); |
| 173 | if (uri_count != 1 && uri_count != 2) { |
| 174 | ERR("Unrecognized URL format."); |
| 175 | goto end; |
| 176 | } |
| 177 | |
| 178 | switch (uri_count) { |
| 179 | case 1: |
| 180 | output_type = OUTPUT_LOCAL; |
| 181 | if (uris[0].dtype != LTTNG_DST_PATH) { |
| 182 | ERR("Unrecognized URL format."); |
| 183 | goto end; |
| 184 | } |
| 185 | ret = lttng_strncpy( |
| 186 | local_output_path, uris[0].dst.path, sizeof(local_output_path)); |
| 187 | if (ret) { |
| 188 | ERR("Output path exceeds the maximal supported length (%zu bytes)", |
| 189 | sizeof(local_output_path)); |
| 190 | } |
| 191 | break; |
| 192 | case 2: |
| 193 | output_type = OUTPUT_NETWORK; |
| 194 | break; |
| 195 | default: |
| 196 | /* Already checked. */ |
| 197 | abort(); |
| 198 | } |
| 199 | } else { |
| 200 | output_type = OUTPUT_UNSPECIFIED; |
| 201 | } |
| 202 | |
| 203 | if (opt_snapshot) { |
| 204 | /* Snapshot session. */ |
| 205 | switch (output_type) { |
| 206 | case OUTPUT_UNSPECIFIED: |
| 207 | case OUTPUT_LOCAL: |
| 208 | descriptor = lttng_session_descriptor_snapshot_local_create( |
| 209 | session_name, |
| 210 | output_type == OUTPUT_LOCAL ? local_output_path : nullptr); |
| 211 | break; |
| 212 | case OUTPUT_NONE: |
| 213 | descriptor = lttng_session_descriptor_snapshot_create(session_name); |
| 214 | break; |
| 215 | case OUTPUT_NETWORK: |
| 216 | descriptor = lttng_session_descriptor_snapshot_network_create( |
| 217 | session_name, uri_str1, uri_str2); |
| 218 | break; |
| 219 | default: |
| 220 | abort(); |
| 221 | } |
| 222 | } else if (opt_live_timer) { |
| 223 | /* Live session. */ |
| 224 | if (output_type != OUTPUT_UNSPECIFIED && output_type != OUTPUT_NETWORK) { |
| 225 | ERR("Unsupported output type specified for live session."); |
| 226 | goto end; |
| 227 | } |
| 228 | descriptor = lttng_session_descriptor_live_network_create( |
| 229 | session_name, uri_str1, uri_str2, opt_live_timer); |
| 230 | } else { |
| 231 | /* Regular session. */ |
| 232 | switch (output_type) { |
| 233 | case OUTPUT_UNSPECIFIED: |
| 234 | case OUTPUT_LOCAL: |
| 235 | descriptor = lttng_session_descriptor_local_create( |
| 236 | session_name, |
| 237 | output_type == OUTPUT_LOCAL ? local_output_path : nullptr); |
| 238 | break; |
| 239 | case OUTPUT_NONE: |
| 240 | descriptor = lttng_session_descriptor_create(session_name); |
| 241 | break; |
| 242 | case OUTPUT_NETWORK: |
| 243 | descriptor = lttng_session_descriptor_network_create( |
| 244 | session_name, uri_str1, uri_str2); |
| 245 | break; |
| 246 | default: |
| 247 | abort(); |
| 248 | } |
| 249 | } |
| 250 | if (!descriptor) { |
| 251 | ERR("Failed to initialize session creation command."); |
| 252 | } else { |
| 253 | /* |
| 254 | * Auto-launch the relay daemon when a live session |
| 255 | * is created using default URLs. |
| 256 | */ |
| 257 | if (!opt_url && !opt_ctrl_url && !opt_data_url && opt_live_timer && |
| 258 | !check_relayd()) { |
| 259 | int ret; |
| 260 | const char *pathname = opt_relayd_path ?: INSTALL_BIN_PATH "/lttng-relayd"; |
| 261 | |
| 262 | ret = spawn_relayd(pathname, 0); |
| 263 | if (ret < 0) { |
| 264 | lttng_session_descriptor_destroy(descriptor); |
| 265 | descriptor = nullptr; |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | end: |
| 270 | free(uris); |
| 271 | return descriptor; |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | * Create a tracing session. |
| 276 | * If no name is specified, a default name is generated. |
| 277 | * |
| 278 | * Returns one of the CMD_* result constants. |
| 279 | */ |
| 280 | static int create_session(const char *session_name) |
| 281 | { |
| 282 | int ret, i; |
| 283 | char shm_path[LTTNG_PATH_MAX] = {}; |
| 284 | struct lttng_session_descriptor *session_descriptor = nullptr; |
| 285 | enum lttng_session_descriptor_status descriptor_status; |
| 286 | enum lttng_error_code ret_code; |
| 287 | struct lttng_session *sessions = nullptr; |
| 288 | const struct lttng_session *created_session = nullptr; |
| 289 | const char *created_session_name; |
| 290 | |
| 291 | /* Validate options. */ |
| 292 | if (session_name) { |
| 293 | if (strlen(session_name) > NAME_MAX) { |
| 294 | ERR("Session name too long. Length must be lower or equal to %d", NAME_MAX); |
| 295 | ret = CMD_ERROR; |
| 296 | goto error; |
| 297 | } |
| 298 | /* |
| 299 | * Check if the session name begins with "auto-" or is exactly "auto". |
| 300 | * Both are reserved for the default session name. See bug #449 to |
| 301 | * understand why we need to check both here. |
| 302 | */ |
| 303 | if ((strncmp(session_name, |
| 304 | DEFAULT_SESSION_NAME "-", |
| 305 | strlen(DEFAULT_SESSION_NAME) + 1) == 0) || |
| 306 | (strncmp(session_name, DEFAULT_SESSION_NAME, strlen(DEFAULT_SESSION_NAME)) == |
| 307 | 0 && |
| 308 | strlen(session_name) == strlen(DEFAULT_SESSION_NAME))) { |
| 309 | ERR("%s is a reserved keyword for default session(s)", |
| 310 | DEFAULT_SESSION_NAME); |
| 311 | ret = CMD_ERROR; |
| 312 | goto error; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | if (opt_snapshot && opt_live_timer) { |
| 317 | ERR("Snapshot and live modes are mutually exclusive."); |
| 318 | ret = CMD_ERROR; |
| 319 | goto error; |
| 320 | } |
| 321 | |
| 322 | if ((!opt_ctrl_url && opt_data_url) || (opt_ctrl_url && !opt_data_url)) { |
| 323 | ERR("Both control and data URLs must be specified."); |
| 324 | ret = CMD_ERROR; |
| 325 | goto error; |
| 326 | } |
| 327 | |
| 328 | session_descriptor = create_session_descriptor(session_name); |
| 329 | if (!session_descriptor) { |
| 330 | ret = CMD_ERROR; |
| 331 | goto error; |
| 332 | } |
| 333 | ret_code = lttng_create_session_ext(session_descriptor); |
| 334 | if (ret_code != LTTNG_OK) { |
| 335 | ERR("%s", lttng_strerror(-ret_code)); |
| 336 | ret = CMD_ERROR; |
| 337 | goto error; |
| 338 | } |
| 339 | |
| 340 | descriptor_status = lttng_session_descriptor_get_session_name(session_descriptor, |
| 341 | &created_session_name); |
| 342 | if (descriptor_status != LTTNG_SESSION_DESCRIPTOR_STATUS_OK) { |
| 343 | ERR("Failed to obtain created session name"); |
| 344 | ret = CMD_ERROR; |
| 345 | goto error; |
| 346 | } |
| 347 | |
| 348 | ret = lttng_list_sessions(&sessions); |
| 349 | if (ret < 0) { |
| 350 | ERR("Failed to fetch properties of created session: %s", lttng_strerror(ret)); |
| 351 | ret = CMD_ERROR; |
| 352 | goto error; |
| 353 | } |
| 354 | for (i = 0; i < ret; i++) { |
| 355 | if (!strcmp(created_session_name, sessions[i].name)) { |
| 356 | created_session = &sessions[i]; |
| 357 | break; |
| 358 | } |
| 359 | } |
| 360 | if (!created_session) { |
| 361 | ERR("Failed to fetch properties of created session"); |
| 362 | ret = CMD_ERROR; |
| 363 | goto error; |
| 364 | } |
| 365 | |
| 366 | if (opt_shm_path) { |
| 367 | char datetime_suffix[17] = {}; |
| 368 | |
| 369 | /* |
| 370 | * An auto-generated session name already includes the creation |
| 371 | * timestamp. |
| 372 | */ |
| 373 | if (session_name) { |
| 374 | uint64_t creation_time; |
| 375 | struct tm *timeinfo; |
| 376 | time_t creation_time_t; |
| 377 | size_t strftime_ret; |
| 378 | |
| 379 | ret_code = lttng_session_get_creation_time(created_session, &creation_time); |
| 380 | if (ret_code != LTTNG_OK) { |
| 381 | ERR("%s", lttng_strerror(-ret_code)); |
| 382 | ret = CMD_ERROR; |
| 383 | goto error; |
| 384 | } |
| 385 | creation_time_t = (time_t) creation_time; |
| 386 | timeinfo = localtime(&creation_time_t); |
| 387 | if (!timeinfo) { |
| 388 | PERROR("Failed to interpret session creation time"); |
| 389 | ret = CMD_ERROR; |
| 390 | goto error; |
| 391 | } |
| 392 | strftime_ret = strftime(datetime_suffix, |
| 393 | sizeof(datetime_suffix), |
| 394 | "-%Y%m%d-%H%M%S", |
| 395 | timeinfo); |
| 396 | if (strftime_ret == 0) { |
| 397 | ERR("Failed to format session creation time."); |
| 398 | ret = CMD_ERROR; |
| 399 | goto error; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | ret = snprintf(shm_path, |
| 404 | sizeof(shm_path), |
| 405 | "%s/%s%s", |
| 406 | opt_shm_path, |
| 407 | created_session_name, |
| 408 | datetime_suffix); |
| 409 | if (ret < 0 || ret >= sizeof(shm_path)) { |
| 410 | ERR("Failed to format the shared memory path."); |
| 411 | ret = CMD_ERROR; |
| 412 | goto error; |
| 413 | } |
| 414 | ret = lttng_set_session_shm_path(created_session_name, shm_path); |
| 415 | if (ret < 0) { |
| 416 | lttng_destroy_session(created_session_name); |
| 417 | ret = CMD_ERROR; |
| 418 | goto error; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | if (opt_snapshot) { |
| 423 | MSG("Snapshot session %s created.", created_session_name); |
| 424 | } else if (opt_live_timer) { |
| 425 | MSG("Live session %s created.", created_session_name); |
| 426 | } else { |
| 427 | MSG("Session %s created.", created_session_name); |
| 428 | } |
| 429 | |
| 430 | if (*created_session->path && !opt_snapshot) { |
| 431 | MSG("Traces will be output to %s", created_session->path); |
| 432 | |
| 433 | if (opt_live_timer) { |
| 434 | MSG("Live timer interval set to %u %s", opt_live_timer, USEC_UNIT); |
| 435 | } |
| 436 | } else if (opt_snapshot) { |
| 437 | struct lttng_snapshot_output_list *list; |
| 438 | struct lttng_snapshot_output *iter; |
| 439 | char snapshot_url[LTTNG_PATH_MAX] = {}; |
| 440 | |
| 441 | ret = lttng_snapshot_list_output(created_session_name, &list); |
| 442 | if (ret < 0) { |
| 443 | ERR("Failed to list snapshot outputs."); |
| 444 | ret = CMD_ERROR; |
| 445 | goto error; |
| 446 | } |
| 447 | |
| 448 | while ((iter = lttng_snapshot_output_list_get_next(list))) { |
| 449 | const char *url = nullptr; |
| 450 | |
| 451 | url = lttng_snapshot_output_get_ctrl_url(iter); |
| 452 | ret = lttng_strncpy(snapshot_url, url, sizeof(snapshot_url)); |
| 453 | if (ret) { |
| 454 | snapshot_url[0] = '\0'; |
| 455 | ERR("Failed to retrieve snapshot output destination"); |
| 456 | } |
| 457 | break; |
| 458 | } |
| 459 | lttng_snapshot_output_list_destroy(list); |
| 460 | |
| 461 | if (*snapshot_url) { |
| 462 | MSG("Default snapshot output set to %s", snapshot_url); |
| 463 | } |
| 464 | MSG("Every channel enabled for this session will be set to mmap output and default to overwrite mode."); |
| 465 | } |
| 466 | if (opt_shm_path) { |
| 467 | MSG("Shared memory path set to %s", shm_path); |
| 468 | } |
| 469 | |
| 470 | /* Mi output */ |
| 471 | if (lttng_opt_mi) { |
| 472 | ret = mi_created_session(created_session_name); |
| 473 | if (ret) { |
| 474 | ret = CMD_ERROR; |
| 475 | goto error; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | /* Init lttng session config */ |
| 480 | ret = config_init(created_session_name); |
| 481 | if (ret < 0) { |
| 482 | ret = CMD_ERROR; |
| 483 | goto error; |
| 484 | } |
| 485 | |
| 486 | ret = CMD_SUCCESS; |
| 487 | error: |
| 488 | lttng_session_descriptor_destroy(session_descriptor); |
| 489 | free(sessions); |
| 490 | return ret; |
| 491 | } |
| 492 | |
| 493 | /* |
| 494 | * spawn_sessiond |
| 495 | * |
| 496 | * Spawn a session daemon by forking and execv. |
| 497 | */ |
| 498 | static int spawn_sessiond(const char *pathname) |
| 499 | { |
| 500 | int ret = 0; |
| 501 | pid_t pid; |
| 502 | |
| 503 | MSG("Spawning a session daemon"); |
| 504 | pid = fork(); |
| 505 | if (pid == 0) { |
| 506 | /* |
| 507 | * Spawn session daemon in daemon mode. |
| 508 | */ |
| 509 | execlp(pathname, "lttng-sessiond", "--daemonize", NULL); |
| 510 | /* execlp only returns if error happened */ |
| 511 | if (errno == ENOENT) { |
| 512 | ERR("No session daemon found. Use --sessiond-path."); |
| 513 | } else { |
| 514 | PERROR("execlp"); |
| 515 | } |
| 516 | kill(getppid(), SIGTERM); /* wake parent */ |
| 517 | exit(EXIT_FAILURE); |
| 518 | } else if (pid > 0) { |
| 519 | /* |
| 520 | * In daemon mode (--daemonize), sessiond only exits when |
| 521 | * it's ready to accept commands. |
| 522 | */ |
| 523 | for (;;) { |
| 524 | int status; |
| 525 | pid_t wait_pid_ret = waitpid(pid, &status, 0); |
| 526 | |
| 527 | if (wait_pid_ret < 0) { |
| 528 | if (errno == EINTR) { |
| 529 | continue; |
| 530 | } |
| 531 | PERROR("waitpid"); |
| 532 | ret = -errno; |
| 533 | goto end; |
| 534 | } |
| 535 | |
| 536 | if (WIFSIGNALED(status)) { |
| 537 | ERR("Session daemon was killed by signal %d", WTERMSIG(status)); |
| 538 | ret = -1; |
| 539 | goto end; |
| 540 | } else if (WIFEXITED(status)) { |
| 541 | DBG("Session daemon terminated normally (exit status: %d)", |
| 542 | WEXITSTATUS(status)); |
| 543 | |
| 544 | if (WEXITSTATUS(status) != 0) { |
| 545 | ERR("Session daemon terminated with an error (exit status: %d)", |
| 546 | WEXITSTATUS(status)); |
| 547 | ret = -1; |
| 548 | goto end; |
| 549 | } |
| 550 | break; |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | goto end; |
| 555 | } else { |
| 556 | PERROR("fork"); |
| 557 | ret = -1; |
| 558 | goto end; |
| 559 | } |
| 560 | |
| 561 | end: |
| 562 | return ret; |
| 563 | } |
| 564 | |
| 565 | /* |
| 566 | * launch_sessiond |
| 567 | * |
| 568 | * Check if the session daemon is available using |
| 569 | * the liblttngctl API for the check. If not, try to |
| 570 | * spawn a daemon. |
| 571 | */ |
| 572 | static int launch_sessiond() |
| 573 | { |
| 574 | int ret; |
| 575 | const char *pathname = nullptr; |
| 576 | |
| 577 | ret = lttng_session_daemon_alive(); |
| 578 | if (ret) { |
| 579 | /* Sessiond is alive, not an error */ |
| 580 | ret = 0; |
| 581 | goto end; |
| 582 | } |
| 583 | |
| 584 | /* Try command line option path */ |
| 585 | pathname = opt_sessiond_path; |
| 586 | |
| 587 | /* Try LTTNG_SESSIOND_PATH env variable */ |
| 588 | if (pathname == nullptr) { |
| 589 | pathname = getenv(DEFAULT_SESSIOND_PATH_ENV); |
| 590 | } |
| 591 | |
| 592 | /* Try with configured path */ |
| 593 | if (pathname == nullptr) { |
| 594 | if (CONFIG_SESSIOND_BIN[0] != '\0') { |
| 595 | pathname = CONFIG_SESSIOND_BIN; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | /* Try the default path */ |
| 600 | if (pathname == nullptr) { |
| 601 | pathname = INSTALL_BIN_PATH "/lttng-sessiond"; |
| 602 | } |
| 603 | |
| 604 | DBG("Session daemon binary path: %s", pathname); |
| 605 | |
| 606 | /* Check existence and permissions */ |
| 607 | ret = access(pathname, F_OK | X_OK); |
| 608 | if (ret < 0) { |
| 609 | ERR("No such file or access denied: %s", pathname); |
| 610 | goto end; |
| 611 | } |
| 612 | |
| 613 | ret = spawn_sessiond(pathname); |
| 614 | end: |
| 615 | if (ret) { |
| 616 | ERR("Problem occurred while launching session daemon (%s)", pathname); |
| 617 | } |
| 618 | return ret; |
| 619 | } |
| 620 | |
| 621 | static int validate_url_option_combination() |
| 622 | { |
| 623 | int ret = 0; |
| 624 | int used_count = 0; |
| 625 | |
| 626 | used_count += !!opt_url; |
| 627 | used_count += !!opt_output_path; |
| 628 | used_count += (opt_data_url || opt_ctrl_url); |
| 629 | if (used_count > 1) { |
| 630 | ERR("Only one of the --set-url, --ctrl-url/data-url, or --output options may be used at once."); |
| 631 | ret = -1; |
| 632 | } |
| 633 | |
| 634 | return ret; |
| 635 | } |
| 636 | |
| 637 | /* |
| 638 | * The 'create <options>' first level command |
| 639 | * |
| 640 | * Returns one of the CMD_* result constants. |
| 641 | */ |
| 642 | int cmd_create(int argc, const char **argv) |
| 643 | { |
| 644 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
| 645 | char *opt_arg = nullptr; |
| 646 | const char *arg_session_name = nullptr; |
| 647 | const char *leftover = nullptr; |
| 648 | static poptContext pc; |
| 649 | |
| 650 | pc = poptGetContext(nullptr, argc, argv, long_options, 0); |
| 651 | poptReadDefaultConfig(pc, 0); |
| 652 | |
| 653 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 654 | switch (opt) { |
| 655 | case OPT_HELP: |
| 656 | SHOW_HELP(); |
| 657 | goto end; |
| 658 | case OPT_LIST_OPTIONS: |
| 659 | list_cmd_options(stdout, long_options); |
| 660 | goto end; |
| 661 | case OPT_LIVE_TIMER: |
| 662 | { |
| 663 | uint64_t v; |
| 664 | |
| 665 | errno = 0; |
| 666 | if (opt_arg) { |
| 667 | free(opt_arg); |
| 668 | opt_arg = nullptr; |
| 669 | } |
| 670 | |
| 671 | opt_arg = poptGetOptArg(pc); |
| 672 | if (!opt_arg) { |
| 673 | /* Set up default values. */ |
| 674 | opt_live_timer = (uint32_t) DEFAULT_LTTNG_LIVE_TIMER; |
| 675 | DBG("Session live timer interval set to default value %d", |
| 676 | opt_live_timer); |
| 677 | break; |
| 678 | } |
| 679 | |
| 680 | if (utils_parse_time_suffix(opt_arg, &v) < 0) { |
| 681 | ERR("Wrong value for --live parameter: %s", opt_arg); |
| 682 | ret = CMD_ERROR; |
| 683 | goto end; |
| 684 | } |
| 685 | |
| 686 | if (v != (uint32_t) v) { |
| 687 | ERR("32-bit overflow in --live parameter: %s", opt_arg); |
| 688 | ret = CMD_ERROR; |
| 689 | goto end; |
| 690 | } |
| 691 | |
| 692 | if (v == 0) { |
| 693 | ERR("Live timer interval must be greater than zero"); |
| 694 | ret = CMD_ERROR; |
| 695 | goto end; |
| 696 | } |
| 697 | |
| 698 | opt_live_timer = (uint32_t) v; |
| 699 | DBG("Session live timer interval set to %d", opt_live_timer); |
| 700 | break; |
| 701 | } |
| 702 | default: |
| 703 | ret = CMD_UNDEFINED; |
| 704 | goto end; |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | if (opt_no_consumer) { |
| 709 | MSG("The option --no-consumer is obsolete. Use --no-output now."); |
| 710 | ret = CMD_WARNING; |
| 711 | goto end; |
| 712 | } |
| 713 | |
| 714 | ret = validate_url_option_combination(); |
| 715 | if (ret) { |
| 716 | ret = CMD_ERROR; |
| 717 | goto end; |
| 718 | } |
| 719 | |
| 720 | /* Spawn a session daemon if needed */ |
| 721 | if (!opt_no_sessiond) { |
| 722 | ret = launch_sessiond(); |
| 723 | if (ret) { |
| 724 | ret = CMD_ERROR; |
| 725 | goto end; |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | /* MI initialization */ |
| 730 | if (lttng_opt_mi) { |
| 731 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); |
| 732 | if (!writer) { |
| 733 | ret = -LTTNG_ERR_NOMEM; |
| 734 | goto end; |
| 735 | } |
| 736 | |
| 737 | /* Open command element */ |
| 738 | ret = mi_lttng_writer_command_open(writer, mi_lttng_element_command_create); |
| 739 | if (ret) { |
| 740 | ret = CMD_ERROR; |
| 741 | goto end; |
| 742 | } |
| 743 | |
| 744 | /* Open output element */ |
| 745 | ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command_output); |
| 746 | if (ret) { |
| 747 | ret = CMD_ERROR; |
| 748 | goto end; |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | /* Get the optional session name argument. */ |
| 753 | arg_session_name = poptGetArg(pc); |
| 754 | |
| 755 | leftover = poptGetArg(pc); |
| 756 | if (leftover) { |
| 757 | ERR("Unknown argument: %s", leftover); |
| 758 | ret = CMD_ERROR; |
| 759 | goto end; |
| 760 | } |
| 761 | |
| 762 | command_ret = create_session(arg_session_name); |
| 763 | if (command_ret) { |
| 764 | success = 0; |
| 765 | } |
| 766 | |
| 767 | if (lttng_opt_mi) { |
| 768 | /* Close output element */ |
| 769 | ret = mi_lttng_writer_close_element(writer); |
| 770 | if (ret) { |
| 771 | ret = CMD_ERROR; |
| 772 | goto end; |
| 773 | } |
| 774 | |
| 775 | /* Success ? */ |
| 776 | ret = mi_lttng_writer_write_element_bool( |
| 777 | writer, mi_lttng_element_command_success, success); |
| 778 | if (ret) { |
| 779 | ret = CMD_ERROR; |
| 780 | goto end; |
| 781 | } |
| 782 | |
| 783 | /* Command element close */ |
| 784 | ret = mi_lttng_writer_command_close(writer); |
| 785 | if (ret) { |
| 786 | ret = CMD_ERROR; |
| 787 | goto end; |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | end: |
| 792 | /* Mi clean-up */ |
| 793 | if (writer && mi_lttng_writer_destroy(writer)) { |
| 794 | /* Preserve original error code */ |
| 795 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; |
| 796 | } |
| 797 | |
| 798 | /* Overwrite ret if an error occurred in create_session() */ |
| 799 | ret = command_ret ? command_ret : ret; |
| 800 | |
| 801 | free(opt_arg); |
| 802 | poptFreeContext(pc); |
| 803 | return ret; |
| 804 | } |