| 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, version 2 only, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | |
| 18 | #define _LGPL_SOURCE |
| 19 | #include <assert.h> |
| 20 | #include <ctype.h> |
| 21 | #include <popt.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <common/compat/time.h> |
| 28 | #include <unistd.h> |
| 29 | #include <signal.h> |
| 30 | #include <sys/wait.h> |
| 31 | |
| 32 | #include <common/mi-lttng.h> |
| 33 | |
| 34 | #include "../command.h" |
| 35 | #include "../utils.h" |
| 36 | |
| 37 | #include <common/defaults.h> |
| 38 | #include <common/sessiond-comm/sessiond-comm.h> |
| 39 | #include <common/uri.h> |
| 40 | #include <common/utils.h> |
| 41 | #include <lttng/snapshot.h> |
| 42 | |
| 43 | static char *opt_output_path; |
| 44 | static char *opt_session_name; |
| 45 | static char *opt_url; |
| 46 | static char *opt_ctrl_url; |
| 47 | static char *opt_data_url; |
| 48 | static char *opt_shm_path; |
| 49 | static int opt_no_consumer; |
| 50 | static int opt_no_output; |
| 51 | static int opt_snapshot; |
| 52 | static unsigned int opt_live_timer; |
| 53 | |
| 54 | enum { |
| 55 | OPT_HELP = 1, |
| 56 | OPT_LIST_OPTIONS, |
| 57 | OPT_LIVE_TIMER, |
| 58 | }; |
| 59 | |
| 60 | static struct mi_writer *writer; |
| 61 | |
| 62 | static struct poptOption long_options[] = { |
| 63 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 64 | {"help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL}, |
| 65 | {"output", 'o', POPT_ARG_STRING, &opt_output_path, 0, NULL, NULL}, |
| 66 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
| 67 | {"set-url", 'U', POPT_ARG_STRING, &opt_url, 0, 0, 0}, |
| 68 | {"ctrl-url", 'C', POPT_ARG_STRING, &opt_ctrl_url, 0, 0, 0}, |
| 69 | {"data-url", 'D', POPT_ARG_STRING, &opt_data_url, 0, 0, 0}, |
| 70 | {"no-output", 0, POPT_ARG_VAL, &opt_no_output, 1, 0, 0}, |
| 71 | {"no-consumer", 0, POPT_ARG_VAL, &opt_no_consumer, 1, 0, 0}, |
| 72 | {"snapshot", 0, POPT_ARG_VAL, &opt_snapshot, 1, 0, 0}, |
| 73 | {"live", 0, POPT_ARG_INT | POPT_ARGFLAG_OPTIONAL, 0, OPT_LIVE_TIMER, 0, 0}, |
| 74 | {"shm-path", 0, POPT_ARG_STRING, &opt_shm_path, 0, 0, 0}, |
| 75 | {0, 0, 0, 0, 0, 0, 0} |
| 76 | }; |
| 77 | |
| 78 | /* |
| 79 | * Please have a look at src/lib/lttng-ctl/lttng-ctl.c for more information on |
| 80 | * why this declaration exists and used ONLY in for this command. |
| 81 | */ |
| 82 | extern int _lttng_create_session_ext(const char *name, const char *url, |
| 83 | const char *datetime); |
| 84 | |
| 85 | /* |
| 86 | * Retrieve the created session and mi output it based on provided argument |
| 87 | * This is currently a summary of what was pretty printed and is subject to |
| 88 | * enhancements. |
| 89 | */ |
| 90 | static int mi_created_session(const char *session_name) |
| 91 | { |
| 92 | int ret, i, count, found; |
| 93 | struct lttng_session *sessions; |
| 94 | |
| 95 | /* session_name should not be null */ |
| 96 | assert(session_name); |
| 97 | assert(writer); |
| 98 | |
| 99 | count = lttng_list_sessions(&sessions); |
| 100 | if (count < 0) { |
| 101 | ret = count; |
| 102 | ERR("%s", lttng_strerror(ret)); |
| 103 | goto error; |
| 104 | } |
| 105 | |
| 106 | if (count == 0) { |
| 107 | ERR("Error session creation failed: session %s not found", session_name); |
| 108 | ret = -LTTNG_ERR_SESS_NOT_FOUND; |
| 109 | goto end; |
| 110 | } |
| 111 | |
| 112 | found = 0; |
| 113 | for (i = 0; i < count; i++) { |
| 114 | if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) { |
| 115 | found = 1; |
| 116 | ret = mi_lttng_session(writer, &sessions[i], 0); |
| 117 | if (ret) { |
| 118 | goto error; |
| 119 | } |
| 120 | break; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | if (!found) { |
| 125 | ret = -LTTNG_ERR_SESS_NOT_FOUND; |
| 126 | } else { |
| 127 | ret = CMD_SUCCESS; |
| 128 | } |
| 129 | |
| 130 | error: |
| 131 | free(sessions); |
| 132 | end: |
| 133 | return ret; |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * For a session name, set the consumer URLs. |
| 138 | */ |
| 139 | static int set_consumer_url(const char *session_name, const char *ctrl_url, |
| 140 | const char *data_url) |
| 141 | { |
| 142 | int ret; |
| 143 | struct lttng_handle *handle; |
| 144 | |
| 145 | assert(session_name); |
| 146 | |
| 147 | /* |
| 148 | * Set handle with the session_name, but no domain. This implies that |
| 149 | * the actions taken with this handle apply on the tracing session |
| 150 | * rather then the domain-specific session. |
| 151 | */ |
| 152 | handle = lttng_create_handle(session_name, NULL); |
| 153 | if (handle == NULL) { |
| 154 | ret = CMD_FATAL; |
| 155 | goto error; |
| 156 | } |
| 157 | |
| 158 | ret = lttng_set_consumer_url(handle, ctrl_url, data_url); |
| 159 | if (ret < 0) { |
| 160 | goto error; |
| 161 | } |
| 162 | |
| 163 | if (ctrl_url) { |
| 164 | MSG("Control URL %s set for session %s", ctrl_url, session_name); |
| 165 | } |
| 166 | |
| 167 | if (data_url) { |
| 168 | MSG("Data URL %s set for session %s", data_url, session_name); |
| 169 | } |
| 170 | |
| 171 | error: |
| 172 | lttng_destroy_handle(handle); |
| 173 | return ret; |
| 174 | } |
| 175 | |
| 176 | static int add_snapshot_output(const char *session_name, const char *ctrl_url, |
| 177 | const char *data_url) |
| 178 | { |
| 179 | int ret; |
| 180 | struct lttng_snapshot_output *output = NULL; |
| 181 | |
| 182 | assert(session_name); |
| 183 | |
| 184 | output = lttng_snapshot_output_create(); |
| 185 | if (!output) { |
| 186 | ret = CMD_FATAL; |
| 187 | goto error_create; |
| 188 | } |
| 189 | |
| 190 | if (ctrl_url) { |
| 191 | ret = lttng_snapshot_output_set_ctrl_url(ctrl_url, output); |
| 192 | if (ret < 0) { |
| 193 | goto error; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | if (data_url) { |
| 198 | ret = lttng_snapshot_output_set_data_url(data_url, output); |
| 199 | if (ret < 0) { |
| 200 | goto error; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /* This call, if successful, populates the id of the output object. */ |
| 205 | ret = lttng_snapshot_add_output(session_name, output); |
| 206 | if (ret < 0) { |
| 207 | goto error; |
| 208 | } |
| 209 | |
| 210 | error: |
| 211 | lttng_snapshot_output_destroy(output); |
| 212 | error_create: |
| 213 | return ret; |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * Create a tracing session. |
| 218 | * If no name is specified, a default name is generated. |
| 219 | * |
| 220 | * Returns one of the CMD_* result constants. |
| 221 | */ |
| 222 | static int create_session(void) |
| 223 | { |
| 224 | int ret; |
| 225 | char *session_name = NULL, *traces_path = NULL, *alloc_path = NULL; |
| 226 | char *alloc_url = NULL, *url = NULL, datetime[16]; |
| 227 | char session_name_date[NAME_MAX + 17], *print_str_url = NULL; |
| 228 | time_t rawtime; |
| 229 | struct tm *timeinfo; |
| 230 | char shm_path[PATH_MAX] = ""; |
| 231 | |
| 232 | /* Get date and time for automatic session name/path */ |
| 233 | time(&rawtime); |
| 234 | timeinfo = localtime(&rawtime); |
| 235 | strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo); |
| 236 | |
| 237 | /* Auto session name creation */ |
| 238 | if (opt_session_name == NULL) { |
| 239 | ret = snprintf(session_name_date, sizeof(session_name_date), |
| 240 | DEFAULT_SESSION_NAME "-%s", datetime); |
| 241 | if (ret < 0) { |
| 242 | PERROR("snprintf session name"); |
| 243 | goto error; |
| 244 | } |
| 245 | session_name = session_name_date; |
| 246 | DBG("Auto session name set to %s", session_name_date); |
| 247 | } else { |
| 248 | if (strlen(opt_session_name) > NAME_MAX) { |
| 249 | ERR("Session name too long. Length must be lower or equal to %d", |
| 250 | NAME_MAX); |
| 251 | ret = LTTNG_ERR_SESSION_FAIL; |
| 252 | goto error; |
| 253 | } |
| 254 | /* |
| 255 | * Check if the session name begins with "auto-" or is exactly "auto". |
| 256 | * Both are reserved for the default session name. See bug #449 to |
| 257 | * understand why we need to check both here. |
| 258 | */ |
| 259 | if ((strncmp(opt_session_name, DEFAULT_SESSION_NAME "-", |
| 260 | strlen(DEFAULT_SESSION_NAME) + 1) == 0) || |
| 261 | (strncmp(opt_session_name, DEFAULT_SESSION_NAME, |
| 262 | strlen(DEFAULT_SESSION_NAME)) == 0 && |
| 263 | strlen(opt_session_name) == strlen(DEFAULT_SESSION_NAME))) { |
| 264 | ERR("%s is a reserved keyword for default session(s)", |
| 265 | DEFAULT_SESSION_NAME); |
| 266 | ret = CMD_ERROR; |
| 267 | goto error; |
| 268 | } |
| 269 | session_name = opt_session_name; |
| 270 | ret = snprintf(session_name_date, sizeof(session_name_date), |
| 271 | "%s-%s", session_name, datetime); |
| 272 | if (ret < 0) { |
| 273 | PERROR("snprintf session name"); |
| 274 | goto error; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | if ((!opt_ctrl_url && opt_data_url) || (opt_ctrl_url && !opt_data_url)) { |
| 279 | ERR("You need both control and data URL."); |
| 280 | ret = CMD_ERROR; |
| 281 | goto error; |
| 282 | } |
| 283 | |
| 284 | if (opt_output_path != NULL) { |
| 285 | traces_path = utils_expand_path(opt_output_path); |
| 286 | if (traces_path == NULL) { |
| 287 | ret = CMD_ERROR; |
| 288 | goto error; |
| 289 | } |
| 290 | |
| 291 | /* Create URL string from the local file system path */ |
| 292 | ret = asprintf(&alloc_url, "file://%s", traces_path); |
| 293 | if (ret < 0) { |
| 294 | PERROR("asprintf url path"); |
| 295 | ret = CMD_FATAL; |
| 296 | goto error; |
| 297 | } |
| 298 | /* URL to use in the lttng_create_session() call */ |
| 299 | url = alloc_url; |
| 300 | print_str_url = traces_path; |
| 301 | } else if (opt_url) { /* Handling URL (-U opt) */ |
| 302 | url = opt_url; |
| 303 | print_str_url = url; |
| 304 | } else if (opt_data_url && opt_ctrl_url) { |
| 305 | /* |
| 306 | * With both control and data, we'll be setting the consumer URL after |
| 307 | * session creation thus use no URL. |
| 308 | */ |
| 309 | url = NULL; |
| 310 | } else if (!opt_no_output) { |
| 311 | char *tmp_path; |
| 312 | |
| 313 | /* Auto output path */ |
| 314 | tmp_path = utils_get_home_dir(); |
| 315 | if (tmp_path == NULL) { |
| 316 | ERR("HOME path not found.\n \ |
| 317 | Please specify an output path using -o, --output PATH"); |
| 318 | ret = CMD_FATAL; |
| 319 | goto error; |
| 320 | } |
| 321 | alloc_path = strdup(tmp_path); |
| 322 | if (!alloc_path) { |
| 323 | PERROR("allocating alloc_path"); |
| 324 | ret = CMD_FATAL; |
| 325 | goto error; |
| 326 | } |
| 327 | ret = asprintf(&alloc_url, |
| 328 | "file://%s/" DEFAULT_TRACE_DIR_NAME "/%s", |
| 329 | alloc_path, session_name_date); |
| 330 | if (ret < 0) { |
| 331 | PERROR("asprintf trace dir name"); |
| 332 | ret = CMD_FATAL; |
| 333 | goto error; |
| 334 | } |
| 335 | |
| 336 | url = alloc_url; |
| 337 | print_str_url = alloc_url + strlen("file://"); |
| 338 | } else { |
| 339 | /* No output means --no-output or --snapshot mode. */ |
| 340 | url = NULL; |
| 341 | } |
| 342 | |
| 343 | /* Use default live URL if NO url is/are found. */ |
| 344 | if ((opt_live_timer && !opt_url) && (opt_live_timer && !opt_data_url)) { |
| 345 | ret = asprintf(&alloc_url, "net://127.0.0.1"); |
| 346 | if (ret < 0) { |
| 347 | PERROR("asprintf default live URL"); |
| 348 | ret = CMD_FATAL; |
| 349 | goto error; |
| 350 | } |
| 351 | url = alloc_url; |
| 352 | print_str_url = url; |
| 353 | } |
| 354 | |
| 355 | if (opt_snapshot && opt_live_timer) { |
| 356 | ERR("Snapshot and live modes are mutually exclusive."); |
| 357 | ret = CMD_ERROR; |
| 358 | goto error; |
| 359 | } |
| 360 | |
| 361 | if (opt_snapshot) { |
| 362 | /* No output by default. */ |
| 363 | const char *snapshot_url = NULL; |
| 364 | |
| 365 | if (opt_url) { |
| 366 | snapshot_url = url; |
| 367 | } else if (!opt_data_url && !opt_ctrl_url) { |
| 368 | /* This is the session path that we need to use as output. */ |
| 369 | snapshot_url = url; |
| 370 | } |
| 371 | ret = lttng_create_session_snapshot(session_name, snapshot_url); |
| 372 | } else if (opt_live_timer) { |
| 373 | const char *pathname; |
| 374 | |
| 375 | if (opt_relayd_path) { |
| 376 | pathname = opt_relayd_path; |
| 377 | } else { |
| 378 | pathname = INSTALL_BIN_PATH "/lttng-relayd"; |
| 379 | } |
| 380 | if (!opt_url && !opt_data_url && !check_relayd() && |
| 381 | spawn_relayd(pathname, 0) < 0) { |
| 382 | goto error; |
| 383 | } |
| 384 | ret = lttng_create_session_live(session_name, url, opt_live_timer); |
| 385 | } else { |
| 386 | ret = _lttng_create_session_ext(session_name, url, datetime); |
| 387 | } |
| 388 | if (ret < 0) { |
| 389 | /* Don't set ret so lttng can interpret the sessiond error. */ |
| 390 | switch (-ret) { |
| 391 | case LTTNG_ERR_EXIST_SESS: |
| 392 | WARN("Session %s already exists", session_name); |
| 393 | break; |
| 394 | default: |
| 395 | break; |
| 396 | } |
| 397 | goto error; |
| 398 | } |
| 399 | |
| 400 | if (opt_ctrl_url && opt_data_url) { |
| 401 | if (opt_snapshot) { |
| 402 | ret = add_snapshot_output(session_name, opt_ctrl_url, |
| 403 | opt_data_url); |
| 404 | } else { |
| 405 | /* Setting up control URI (-C or/and -D opt) */ |
| 406 | ret = set_consumer_url(session_name, opt_ctrl_url, opt_data_url); |
| 407 | } |
| 408 | if (ret < 0) { |
| 409 | /* Destroy created session because the URL are not valid. */ |
| 410 | lttng_destroy_session(session_name); |
| 411 | goto error; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | if (opt_shm_path) { |
| 416 | ret = snprintf(shm_path, sizeof(shm_path), |
| 417 | "%s/%s", opt_shm_path, session_name_date); |
| 418 | if (ret < 0) { |
| 419 | PERROR("snprintf shm_path"); |
| 420 | goto error; |
| 421 | } |
| 422 | |
| 423 | ret = lttng_set_session_shm_path(session_name, shm_path); |
| 424 | if (ret < 0) { |
| 425 | lttng_destroy_session(session_name); |
| 426 | goto error; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | MSG("Session %s created.", session_name); |
| 431 | if (print_str_url && !opt_snapshot) { |
| 432 | MSG("Traces will be written in %s", print_str_url); |
| 433 | |
| 434 | if (opt_live_timer) { |
| 435 | MSG("Live timer set to %u usec", opt_live_timer); |
| 436 | } |
| 437 | } else if (opt_snapshot) { |
| 438 | if (print_str_url) { |
| 439 | MSG("Default snapshot output set to: %s", print_str_url); |
| 440 | } |
| 441 | MSG("Snapshot mode set. Every channel enabled for that session will " |
| 442 | "be set to mmap output, and default to overwrite mode."); |
| 443 | } |
| 444 | if (opt_shm_path) { |
| 445 | MSG("Session %s set to shm_path: %s.", session_name, |
| 446 | shm_path); |
| 447 | } |
| 448 | |
| 449 | /* Mi output */ |
| 450 | if (lttng_opt_mi) { |
| 451 | ret = mi_created_session(session_name); |
| 452 | if (ret) { |
| 453 | ret = CMD_ERROR; |
| 454 | goto error; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | /* Init lttng session config */ |
| 459 | ret = config_init(session_name); |
| 460 | if (ret < 0) { |
| 461 | ret = CMD_ERROR; |
| 462 | goto error; |
| 463 | } |
| 464 | |
| 465 | ret = CMD_SUCCESS; |
| 466 | |
| 467 | error: |
| 468 | free(alloc_url); |
| 469 | free(traces_path); |
| 470 | free(alloc_path); |
| 471 | |
| 472 | if (ret < 0) { |
| 473 | ERR("%s", lttng_strerror(ret)); |
| 474 | } |
| 475 | return ret; |
| 476 | } |
| 477 | |
| 478 | /* |
| 479 | * spawn_sessiond |
| 480 | * |
| 481 | * Spawn a session daemon by forking and execv. |
| 482 | */ |
| 483 | static int spawn_sessiond(char *pathname) |
| 484 | { |
| 485 | int ret = 0; |
| 486 | pid_t pid; |
| 487 | |
| 488 | MSG("Spawning a session daemon"); |
| 489 | pid = fork(); |
| 490 | if (pid == 0) { |
| 491 | /* |
| 492 | * Spawn session daemon in daemon mode. |
| 493 | */ |
| 494 | execlp(pathname, "lttng-sessiond", |
| 495 | "--daemonize", NULL); |
| 496 | /* execlp only returns if error happened */ |
| 497 | if (errno == ENOENT) { |
| 498 | ERR("No session daemon found. Use --sessiond-path."); |
| 499 | } else { |
| 500 | PERROR("execlp"); |
| 501 | } |
| 502 | kill(getppid(), SIGTERM); /* wake parent */ |
| 503 | exit(EXIT_FAILURE); |
| 504 | } else if (pid > 0) { |
| 505 | /* |
| 506 | * In daemon mode (--daemonize), sessiond only exits when |
| 507 | * it's ready to accept commands. |
| 508 | */ |
| 509 | for (;;) { |
| 510 | int status; |
| 511 | pid_t wait_pid_ret = waitpid(pid, &status, 0); |
| 512 | |
| 513 | if (wait_pid_ret < 0) { |
| 514 | if (errno == EINTR) { |
| 515 | continue; |
| 516 | } |
| 517 | PERROR("waitpid"); |
| 518 | ret = -errno; |
| 519 | goto end; |
| 520 | } |
| 521 | |
| 522 | if (WIFSIGNALED(status)) { |
| 523 | ERR("Session daemon was killed by signal %d", |
| 524 | WTERMSIG(status)); |
| 525 | ret = -1; |
| 526 | goto end; |
| 527 | } else if (WIFEXITED(status)) { |
| 528 | DBG("Session daemon terminated normally (exit status: %d)", |
| 529 | WEXITSTATUS(status)); |
| 530 | |
| 531 | if (WEXITSTATUS(status) != 0) { |
| 532 | ERR("Session daemon terminated with an error (exit status: %d)", |
| 533 | WEXITSTATUS(status)); |
| 534 | ret = -1; |
| 535 | goto end; |
| 536 | } |
| 537 | break; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | goto end; |
| 542 | } else { |
| 543 | PERROR("fork"); |
| 544 | ret = -1; |
| 545 | goto end; |
| 546 | } |
| 547 | |
| 548 | end: |
| 549 | return ret; |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | * launch_sessiond |
| 554 | * |
| 555 | * Check if the session daemon is available using |
| 556 | * the liblttngctl API for the check. If not, try to |
| 557 | * spawn a daemon. |
| 558 | */ |
| 559 | static int launch_sessiond(void) |
| 560 | { |
| 561 | int ret; |
| 562 | char *pathname = NULL; |
| 563 | |
| 564 | ret = lttng_session_daemon_alive(); |
| 565 | if (ret) { |
| 566 | /* Sessiond is alive, not an error */ |
| 567 | ret = 0; |
| 568 | goto end; |
| 569 | } |
| 570 | |
| 571 | /* Try command line option path */ |
| 572 | pathname = opt_sessiond_path; |
| 573 | |
| 574 | /* Try LTTNG_SESSIOND_PATH env variable */ |
| 575 | if (pathname == NULL) { |
| 576 | pathname = getenv(DEFAULT_SESSIOND_PATH_ENV); |
| 577 | } |
| 578 | |
| 579 | /* Try with configured path */ |
| 580 | if (pathname == NULL) { |
| 581 | if (CONFIG_SESSIOND_BIN[0] != '\0') { |
| 582 | pathname = CONFIG_SESSIOND_BIN; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | /* Try the default path */ |
| 587 | if (pathname == NULL) { |
| 588 | pathname = INSTALL_BIN_PATH "/lttng-sessiond"; |
| 589 | } |
| 590 | |
| 591 | DBG("Session daemon binary path: %s", pathname); |
| 592 | |
| 593 | /* Check existence and permissions */ |
| 594 | ret = access(pathname, F_OK | X_OK); |
| 595 | if (ret < 0) { |
| 596 | ERR("No such file or access denied: %s", pathname); |
| 597 | goto end; |
| 598 | } |
| 599 | |
| 600 | ret = spawn_sessiond(pathname); |
| 601 | end: |
| 602 | if (ret) { |
| 603 | ERR("Problem occurred while launching session daemon (%s)", |
| 604 | pathname); |
| 605 | } |
| 606 | return ret; |
| 607 | } |
| 608 | |
| 609 | /* |
| 610 | * The 'create <options>' first level command |
| 611 | * |
| 612 | * Returns one of the CMD_* result constants. |
| 613 | */ |
| 614 | int cmd_create(int argc, const char **argv) |
| 615 | { |
| 616 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
| 617 | char *opt_arg = NULL; |
| 618 | static poptContext pc; |
| 619 | |
| 620 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
| 621 | poptReadDefaultConfig(pc, 0); |
| 622 | |
| 623 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 624 | switch (opt) { |
| 625 | case OPT_HELP: |
| 626 | SHOW_HELP(); |
| 627 | goto end; |
| 628 | case OPT_LIST_OPTIONS: |
| 629 | list_cmd_options(stdout, long_options); |
| 630 | goto end; |
| 631 | case OPT_LIVE_TIMER: |
| 632 | { |
| 633 | unsigned long v; |
| 634 | |
| 635 | errno = 0; |
| 636 | opt_arg = poptGetOptArg(pc); |
| 637 | if (!opt_arg) { |
| 638 | /* Set up default values. */ |
| 639 | opt_live_timer = (uint32_t) DEFAULT_LTTNG_LIVE_TIMER; |
| 640 | DBG("Session live timer interval set to default value %d", |
| 641 | opt_live_timer); |
| 642 | break; |
| 643 | } |
| 644 | |
| 645 | v = strtoul(opt_arg, NULL, 0); |
| 646 | if (errno != 0 || !isdigit(opt_arg[0])) { |
| 647 | ERR("Wrong value in --live parameter: %s", opt_arg); |
| 648 | ret = CMD_ERROR; |
| 649 | goto end; |
| 650 | } |
| 651 | if (v != (uint32_t) v) { |
| 652 | ERR("32-bit overflow in --live parameter: %s", opt_arg); |
| 653 | ret = CMD_ERROR; |
| 654 | goto end; |
| 655 | } |
| 656 | if (v == 0) { |
| 657 | ERR("Live timer interval must be greater than zero"); |
| 658 | ret = CMD_ERROR; |
| 659 | goto end; |
| 660 | } |
| 661 | opt_live_timer = (uint32_t) v; |
| 662 | DBG("Session live timer interval set to %d", opt_live_timer); |
| 663 | break; |
| 664 | } |
| 665 | default: |
| 666 | ret = CMD_UNDEFINED; |
| 667 | goto end; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | if (opt_no_consumer) { |
| 672 | MSG("The option --no-consumer is obsolete. Use --no-output now."); |
| 673 | ret = CMD_WARNING; |
| 674 | goto end; |
| 675 | } |
| 676 | |
| 677 | /* Spawn a session daemon if needed */ |
| 678 | if (!opt_no_sessiond) { |
| 679 | ret = launch_sessiond(); |
| 680 | if (ret) { |
| 681 | ret = CMD_ERROR; |
| 682 | goto end; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | /* MI initialization */ |
| 687 | if (lttng_opt_mi) { |
| 688 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); |
| 689 | if (!writer) { |
| 690 | ret = -LTTNG_ERR_NOMEM; |
| 691 | goto end; |
| 692 | } |
| 693 | |
| 694 | /* Open command element */ |
| 695 | ret = mi_lttng_writer_command_open(writer, |
| 696 | mi_lttng_element_command_create); |
| 697 | if (ret) { |
| 698 | ret = CMD_ERROR; |
| 699 | goto end; |
| 700 | } |
| 701 | |
| 702 | /* Open output element */ |
| 703 | ret = mi_lttng_writer_open_element(writer, |
| 704 | mi_lttng_element_command_output); |
| 705 | if (ret) { |
| 706 | ret = CMD_ERROR; |
| 707 | goto end; |
| 708 | } |
| 709 | } |
| 710 | opt_session_name = (char*) poptGetArg(pc); |
| 711 | |
| 712 | command_ret = create_session(); |
| 713 | if (command_ret) { |
| 714 | success = 0; |
| 715 | } |
| 716 | |
| 717 | if (lttng_opt_mi) { |
| 718 | /* Close output element */ |
| 719 | ret = mi_lttng_writer_close_element(writer); |
| 720 | if (ret) { |
| 721 | ret = CMD_ERROR; |
| 722 | goto end; |
| 723 | } |
| 724 | |
| 725 | /* Success ? */ |
| 726 | ret = mi_lttng_writer_write_element_bool(writer, |
| 727 | mi_lttng_element_command_success, success); |
| 728 | if (ret) { |
| 729 | ret = CMD_ERROR; |
| 730 | goto end; |
| 731 | } |
| 732 | |
| 733 | /* Command element close */ |
| 734 | ret = mi_lttng_writer_command_close(writer); |
| 735 | if (ret) { |
| 736 | ret = CMD_ERROR; |
| 737 | goto end; |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | end: |
| 742 | /* Mi clean-up */ |
| 743 | if (writer && mi_lttng_writer_destroy(writer)) { |
| 744 | /* Preserve original error code */ |
| 745 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; |
| 746 | } |
| 747 | |
| 748 | /* Overwrite ret if an error occurred in create_session() */ |
| 749 | ret = command_ret ? command_ret : ret; |
| 750 | |
| 751 | poptFreeContext(pc); |
| 752 | return ret; |
| 753 | } |