Save/load: support session trace format
[deliverable/lttng-tools.git] / src / bin / lttng / commands / create.cpp
CommitLineData
f3ed775e 1/*
21cf9b6b 2 * Copyright (C) 2011 EfficiOS Inc.
ab5be9fa 3 * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com>
f3ed775e 4 *
ab5be9fa 5 * SPDX-License-Identifier: GPL-2.0-only
f3ed775e 6 *
f3ed775e
DG
7 */
8
6c1c0768 9#define _LGPL_SOURCE
0ff3383b 10#include <common/compat/time.hpp>
ecc48a90 11#include <ctype.h>
0ff3383b 12#include <functional>
f3ed775e 13#include <popt.h>
0ff3383b 14#include <signal.h>
f3ed775e
DG
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <sys/stat.h>
19#include <sys/types.h>
bbd44cae 20#include <sys/wait.h>
0ff3383b
JR
21#include <unistd.h>
22#include <unordered_map>
f3ed775e 23
c9e313bc 24#include <common/mi-lttng.hpp>
37d03ff7 25
c9e313bc
SM
26#include "../command.hpp"
27#include "../utils.hpp"
f3ed775e 28
c9e313bc
SM
29#include <common/defaults.hpp>
30#include <common/sessiond-comm/sessiond-comm.hpp>
31#include <common/uri.hpp>
32#include <common/utils.hpp>
33#include <common/path.hpp>
050dd639 34#include <lttng/lttng.h>
42224349 35
f3ed775e
DG
36static char *opt_output_path;
37static char *opt_session_name;
a4b92340
DG
38static char *opt_url;
39static char *opt_ctrl_url;
40static char *opt_data_url;
d7ba1388 41static char *opt_shm_path;
0ff3383b 42static char *opt_trace_format;
a4b92340 43static int opt_no_consumer;
96fe6b8d 44static int opt_no_output;
16f6f820 45static int opt_snapshot;
c7219617 46static uint32_t opt_live_timer;
f3ed775e 47
4fc83d94
PP
48#ifdef LTTNG_EMBED_HELP
49static const char help_msg[] =
50#include <lttng-create.1.h>
51;
52#endif
53
f3ed775e
DG
54enum {
55 OPT_HELP = 1,
679b4943 56 OPT_LIST_OPTIONS,
ecc48a90 57 OPT_LIVE_TIMER,
f3ed775e
DG
58};
59
b178f53e
JG
60enum output_type {
61 OUTPUT_NONE,
62 OUTPUT_LOCAL,
63 OUTPUT_NETWORK,
64 OUTPUT_UNSPECIFIED,
65};
37d03ff7 66
b178f53e 67static struct mi_writer *writer;
f3ed775e
DG
68static struct poptOption long_options[] = {
69 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
679b4943
SM
70 {"help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL},
71 {"output", 'o', POPT_ARG_STRING, &opt_output_path, 0, NULL, NULL},
72 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
23d14dff
DG
73 {"set-url", 'U', POPT_ARG_STRING, &opt_url, 0, 0, 0},
74 {"ctrl-url", 'C', POPT_ARG_STRING, &opt_ctrl_url, 0, 0, 0},
75 {"data-url", 'D', POPT_ARG_STRING, &opt_data_url, 0, 0, 0},
96fe6b8d 76 {"no-output", 0, POPT_ARG_VAL, &opt_no_output, 1, 0, 0},
2bba9e53 77 {"no-consumer", 0, POPT_ARG_VAL, &opt_no_consumer, 1, 0, 0},
16f6f820 78 {"snapshot", 0, POPT_ARG_VAL, &opt_snapshot, 1, 0, 0},
d73c5802 79 {"live", 0, POPT_ARG_INT | POPT_ARGFLAG_OPTIONAL, 0, OPT_LIVE_TIMER, 0, 0},
d7ba1388 80 {"shm-path", 0, POPT_ARG_STRING, &opt_shm_path, 0, 0, 0},
0ff3383b 81 {"trace-format", 0, POPT_ARG_STRING, &opt_trace_format, 0, 0, 0},
f3ed775e
DG
82 {0, 0, 0, 0, 0, 0, 0}
83};
84
37d03ff7 85/*
485ca16f 86 * Retrieve the created session and mi output it based on provided argument
37d03ff7
JRJ
87 * This is currently a summary of what was pretty printed and is subject to
88 * enhancements.
37d03ff7
JRJ
89 */
90static 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 */
a0377dfe
FD
96 LTTNG_ASSERT(session_name);
97 LTTNG_ASSERT(writer);
37d03ff7
JRJ
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
130error:
131 free(sessions);
132end:
133 return ret;
134}
135
b178f53e
JG
136static
137struct lttng_session_descriptor *create_session_descriptor(void)
16f6f820 138{
b178f53e
JG
139 ssize_t uri_count;
140 enum output_type output_type;
141 struct lttng_uri *uris = NULL;
142 struct lttng_session_descriptor *descriptor = NULL;
143 const char *uri_str1 = NULL, *uri_str2 = NULL;
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;
21a4b056 150 int ret;
b178f53e
JG
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(local_output_path, expanded_output_path,
159 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) {
21a4b056
SM
167 int ret;
168
b178f53e
JG
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 }
16f6f820 177
b178f53e
JG
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(local_output_path, uris[0].dst.path,
186 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;
16f6f820
DG
201 }
202
b178f53e
JG
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 opt_session_name,
210 output_type == OUTPUT_LOCAL ?
211 local_output_path : NULL);
212 break;
213 case OUTPUT_NONE:
214 descriptor = lttng_session_descriptor_snapshot_create(
215 opt_session_name);
216 break;
217 case OUTPUT_NETWORK:
218 descriptor = lttng_session_descriptor_snapshot_network_create(
219 opt_session_name, uri_str1, uri_str2);
220 break;
221 default:
222 abort();
16f6f820 223 }
b178f53e
JG
224 } else if (opt_live_timer) {
225 /* Live session. */
226 if (output_type != OUTPUT_UNSPECIFIED &&
227 output_type != OUTPUT_NETWORK) {
228 ERR("Unsupported output type specified for live session.");
229 goto end;
230 }
231 descriptor = lttng_session_descriptor_live_network_create(
232 opt_session_name, uri_str1, uri_str2,
233 opt_live_timer);
b178f53e
JG
234 } else {
235 /* Regular session. */
236 switch (output_type) {
237 case OUTPUT_UNSPECIFIED:
238 case OUTPUT_LOCAL:
239 descriptor = lttng_session_descriptor_local_create(
240 opt_session_name,
241 output_type == OUTPUT_LOCAL ?
242 local_output_path : NULL);
243 break;
244 case OUTPUT_NONE:
245 descriptor = lttng_session_descriptor_create(
246 opt_session_name);
247 break;
248 case OUTPUT_NETWORK:
249 descriptor = lttng_session_descriptor_network_create(
250 opt_session_name, uri_str1, uri_str2);
251 break;
252 default:
253 abort();
16f6f820
DG
254 }
255 }
b178f53e
JG
256 if (!descriptor) {
257 ERR("Failed to initialize session creation command.");
973ad93e
JG
258 } else {
259 /*
260 * Auto-launch the relay daemon when a live session
261 * is created using default URLs.
262 */
263 if (!opt_url && !opt_ctrl_url && !opt_data_url &&
264 opt_live_timer && !check_relayd()) {
265 int ret;
266 const char *pathname = opt_relayd_path ? :
267 INSTALL_BIN_PATH "/lttng-relayd";
268
269 ret = spawn_relayd(pathname, 0);
270 if (ret < 0) {
0ff3383b 271 goto error;
973ad93e
JG
272 }
273 }
16f6f820 274 }
0ff3383b
JR
275
276 if (opt_trace_format) {
277 std::unordered_map<std::string,
278 std::function<struct lttng_trace_format_descriptor *()>>
279 factory = {
280 {"ctf1", lttng_trace_format_ctf_1_descriptor_create},
281 {"ctf2", lttng_trace_format_ctf_2_descriptor_create},
282 };
283
284 lttng_session_descriptor_status status;
285 lttng_trace_format_descriptor *trace_format_descriptor = nullptr;
286 std::string s_trace_format(opt_trace_format);
287
288 auto it = factory.find(s_trace_format);
289
290 if (it == factory.end()) {
291 ERR("Unknown trace format");
292 goto error;
293 }
294
295 trace_format_descriptor = it->second();
296 if (trace_format_descriptor == nullptr) {
297 ERR("Failed to create trace format descriptor for trace format %s",
298 opt_trace_format);
299 goto error;
300 }
301
302 status = lttng_session_descriptor_set_trace_format_descriptor(
303 descriptor, trace_format_descriptor);
304 lttng_trace_format_descriptor_destroy(trace_format_descriptor);
305 if (status != LTTNG_SESSION_DESCRIPTOR_STATUS_OK) {
306 ERR("Failed to set trace format descriptor");
307 goto error;
308 }
309 }
026a8516 310
b178f53e
JG
311end:
312 free(uris);
313 return descriptor;
0ff3383b
JR
314error:
315 lttng_session_descriptor_destroy(descriptor);
316 descriptor = NULL;
317 goto end;
16f6f820
DG
318}
319
026a8516
JR
320static int trace_format_check(struct lttng_session_descriptor& descriptor)
321{
322 int ret = 0;
323 enum lttng_error_code ret_code;
324 const lttng_trace_format_descriptor *trace_format_descriptor = nullptr;
325 enum lttng_session_descriptor_status status;
326 std::string destination;
327
328 status = lttng_session_descriptor_get_trace_format_descriptor(
329 &descriptor, &trace_format_descriptor);
330 if (status != LTTNG_SESSION_DESCRIPTOR_STATUS_OK) {
331 ret_code = LTTNG_ERR_INVALID;
332 goto end;
333 }
334
335 /*
336 * Only check kernel domain since only the kernel tracer can lag behind
337 * in term of feature.
338 */
339
340 ret_code = lttng_domain_supports_trace_format(lttng_session_daemon_command_endpoint,
341 LTTNG_DOMAIN_KERNEL, trace_format_descriptor);
342 if (ret_code == LTTNG_ERR_TRACE_FORMAT_UNSUPPORTED_KERNEL_TRACER) {
343 WARN("%s", lttng_strerror(ret_code));
344 } else if (ret_code == LTTNG_ERR_KERN_NA || ret_code == LTTNG_ERR_NEED_ROOT_SESSIOND) {
345 /*
346 * Not an error, kernel tracers is simply not available at that
347 * time. Next operation will fail as necessary.
348 */
349 DBG("Kernel tracer not available for trace format support check");
350 } else if (ret_code != LTTNG_OK) {
351 ERR("Failed to validate trace format support: %s", lttng_strerror(ret_code));
352 ret = -1;
353 goto end;
354 }
355
356 if (opt_output_path) {
357 char *tmp = utils_expand_path(opt_output_path);
358 if (!tmp) {
359 ret = -1;
360 goto end;
361 }
362 destination = tmp;
363 free(tmp);
364 } else if (opt_url || opt_ctrl_url) {
365 destination = opt_ctrl_url ? opt_ctrl_url : opt_url;
366 } else if (opt_live_timer) {
367 destination = "tcp://127.0.0.1";
368 }
369
370 if (!destination.empty()) {
371 ret_code = lttng_destination_supports_trace_format(
372 lttng_session_daemon_command_endpoint, destination.c_str(),
373 trace_format_descriptor);
374 if (ret_code == LTTNG_ERR_TRACE_FORMAT_UNSUPPORTED_RELAY_DAEMON) {
375 WARN("%s", lttng_strerror(ret_code));
376 } else if (ret_code == LTTNG_ERR_RELAYD_CONNECT_FAIL) {
377 DBG("Could not validate trace format support: %s",
378 lttng_strerror(ret_code));
379 } else if (ret_code != LTTNG_OK) {
380 ERR("Failed to validate trace format support: %s",
381 lttng_strerror(ret_code));
382 ret = -1;
383 goto end;
384 }
385 }
386
387end:
388 return ret;
389}
390
f3ed775e 391/*
1c8d13c8
TD
392 * Create a tracing session.
393 * If no name is specified, a default name is generated.
f3ed775e 394 *
1c8d13c8 395 * Returns one of the CMD_* result constants.
f3ed775e 396 */
a4b92340 397static int create_session(void)
f3ed775e 398{
b178f53e
JG
399 int ret, i;
400 char shm_path[LTTNG_PATH_MAX] = {};
401 struct lttng_session_descriptor *session_descriptor = NULL;
402 enum lttng_session_descriptor_status descriptor_status;
403 enum lttng_error_code ret_code;
404 struct lttng_session *sessions = NULL;
405 const struct lttng_session *created_session = NULL;
406 const char *created_session_name;
407
408 /* Validate options. */
409 if (opt_session_name) {
487b253b
DG
410 if (strlen(opt_session_name) > NAME_MAX) {
411 ERR("Session name too long. Length must be lower or equal to %d",
412 NAME_MAX);
b178f53e 413 ret = CMD_ERROR;
487b253b
DG
414 goto error;
415 }
4b861950
DG
416 /*
417 * Check if the session name begins with "auto-" or is exactly "auto".
418 * Both are reserved for the default session name. See bug #449 to
419 * understand why we need to check both here.
420 */
421 if ((strncmp(opt_session_name, DEFAULT_SESSION_NAME "-",
422 strlen(DEFAULT_SESSION_NAME) + 1) == 0) ||
423 (strncmp(opt_session_name, DEFAULT_SESSION_NAME,
61b35a5a 424 strlen(DEFAULT_SESSION_NAME)) == 0 &&
4b861950 425 strlen(opt_session_name) == strlen(DEFAULT_SESSION_NAME))) {
61b35a5a
DG
426 ERR("%s is a reserved keyword for default session(s)",
427 DEFAULT_SESSION_NAME);
428 ret = CMD_ERROR;
429 goto error;
430 }
f3ed775e
DG
431 }
432
b178f53e
JG
433 if (opt_snapshot && opt_live_timer) {
434 ERR("Snapshot and live modes are mutually exclusive.");
1a241656
DG
435 ret = CMD_ERROR;
436 goto error;
437 }
438
b178f53e
JG
439 if ((!opt_ctrl_url && opt_data_url) || (opt_ctrl_url && !opt_data_url)) {
440 ERR("Both control and data URLs must be specified.");
441 ret = CMD_ERROR;
442 goto error;
00e2e675
DG
443 }
444
b178f53e
JG
445 session_descriptor = create_session_descriptor();
446 if (!session_descriptor) {
447 ret = CMD_ERROR;
448 goto error;
ecc48a90 449 }
026a8516 450
b178f53e
JG
451 ret_code = lttng_create_session_ext(session_descriptor);
452 if (ret_code != LTTNG_OK) {
453 ERR("%s", lttng_strerror(-ret_code));
ecc48a90
JD
454 ret = CMD_ERROR;
455 goto error;
456 }
457
026a8516
JR
458 ret = trace_format_check(*session_descriptor);
459 if (ret < 0) {
460 ret = CMD_ERROR;
461 goto error;
462 }
463
b178f53e 464 descriptor_status = lttng_session_descriptor_get_session_name(
026a8516 465 session_descriptor, &created_session_name);
b178f53e
JG
466 if (descriptor_status != LTTNG_SESSION_DESCRIPTOR_STATUS_OK) {
467 ERR("Failed to obtain created session name");
468 ret = CMD_ERROR;
469 goto error;
16f6f820 470 }
b178f53e
JG
471
472 ret = lttng_list_sessions(&sessions);
f3ed775e 473 if (ret < 0) {
b178f53e
JG
474 ERR("Failed to fetch properties of created session: %s",
475 lttng_strerror(ret));
476 ret = CMD_ERROR;
477 goto error;
478 }
479 for (i = 0; i < ret; i++) {
480 if (!strcmp(created_session_name, sessions[i].name)) {
481 created_session = &sessions[i];
60e835ca 482 break;
42224349 483 }
b178f53e
JG
484 }
485 if (!created_session) {
486 ERR("Failed to fetch properties of created session");
487 ret = CMD_ERROR;
f3ed775e
DG
488 goto error;
489 }
490
b178f53e
JG
491 if (opt_shm_path) {
492 char datetime_suffix[17] = {};
493
494 /*
495 * An auto-generated session name already includes the creation
496 * timestamp.
497 */
498 if (opt_session_name) {
499 uint64_t creation_time;
500 struct tm *timeinfo;
501 time_t creation_time_t;
502 size_t strftime_ret;
503
504 ret_code = lttng_session_get_creation_time(
505 created_session,
506 &creation_time);
507 if (ret_code != LTTNG_OK) {
508 ERR("%s", lttng_strerror(-ret_code));
509 ret = CMD_ERROR;
510 goto error;
511 }
512 creation_time_t = (time_t) creation_time;
513 timeinfo = localtime(&creation_time_t);
514 if (!timeinfo) {
515 PERROR("Failed to interpret session creation time");
516 ret = CMD_ERROR;
517 goto error;
518 }
519 strftime_ret = strftime(datetime_suffix,
520 sizeof(datetime_suffix),
521 "-%Y%m%d-%H%M%S", timeinfo);
522 if (strftime_ret == 0) {
523 ERR("Failed to format session creation time.");
524 ret = CMD_ERROR;
525 goto error;
526 }
a4b92340 527 }
4f50c803 528
d7ba1388 529 ret = snprintf(shm_path, sizeof(shm_path),
b178f53e
JG
530 "%s/%s%s", opt_shm_path, created_session_name,
531 datetime_suffix);
532 if (ret < 0 || ret >= sizeof(shm_path)) {
533 ERR("Failed to format the shared memory path.");
534 ret = CMD_ERROR;
d7ba1388
MD
535 goto error;
536 }
b178f53e
JG
537 ret = lttng_set_session_shm_path(created_session_name,
538 shm_path);
d7ba1388 539 if (ret < 0) {
b178f53e
JG
540 lttng_destroy_session(created_session_name);
541 ret = CMD_ERROR;
d7ba1388
MD
542 goto error;
543 }
544 }
545
b178f53e
JG
546 if (opt_snapshot) {
547 MSG("Snapshot session %s created.", created_session_name);
548 } else if (opt_live_timer) {
549 MSG("Live session %s created.", created_session_name);
550 } else {
551 MSG("Session %s created.", created_session_name);
552 }
553
554 if (*created_session->path && !opt_snapshot) {
555 MSG("Traces will be output to %s", created_session->path);
d73c5802
DG
556
557 if (opt_live_timer) {
b178f53e
JG
558 MSG("Live timer interval set to %u %s", opt_live_timer,
559 USEC_UNIT);
d73c5802 560 }
16f6f820 561 } else if (opt_snapshot) {
b178f53e
JG
562 struct lttng_snapshot_output_list *list;
563 struct lttng_snapshot_output *iter;
564 char snapshot_url[LTTNG_PATH_MAX] = {};
565
566 ret = lttng_snapshot_list_output(created_session_name, &list);
567 if (ret < 0) {
568 ERR("Failed to list snapshot outputs.");
569 ret = CMD_ERROR;
570 goto error;
16f6f820 571 }
b178f53e
JG
572
573 while ((iter = lttng_snapshot_output_list_get_next(list))) {
574 const char *url = NULL;
575
576 url = lttng_snapshot_output_get_ctrl_url(
577 iter);
578 ret = lttng_strncpy(snapshot_url, url,
579 sizeof(snapshot_url));
580 if (ret) {
581 snapshot_url[0] = '\0';
582 ERR("Failed to retrieve snapshot output destination");
583 }
584 break;
585 }
586 lttng_snapshot_output_list_destroy(list);
587
588 if (*snapshot_url) {
589 MSG("Default snapshot output set to %s",
590 snapshot_url);
591 }
592 MSG("Every channel enabled for this session will be set to mmap output and default to overwrite mode.");
a4b92340 593 }
d7ba1388 594 if (opt_shm_path) {
b178f53e 595 MSG("Shared memory path set to %s", shm_path);
d7ba1388 596 }
a4b92340 597
37d03ff7
JRJ
598 /* Mi output */
599 if (lttng_opt_mi) {
b178f53e 600 ret = mi_created_session(created_session_name);
37d03ff7
JRJ
601 if (ret) {
602 ret = CMD_ERROR;
603 goto error;
604 }
605 }
606
58a97671 607 /* Init lttng session config */
b178f53e 608 ret = config_init(created_session_name);
f3ed775e 609 if (ret < 0) {
27089920 610 ret = CMD_ERROR;
f3ed775e
DG
611 goto error;
612 }
613
f3ed775e 614 ret = CMD_SUCCESS;
f3ed775e 615error:
b178f53e
JG
616 lttng_session_descriptor_destroy(session_descriptor);
617 free(sessions);
f3ed775e
DG
618 return ret;
619}
620
92360082
JG
621/*
622 * spawn_sessiond
623 *
624 * Spawn a session daemon by forking and execv.
625 */
b53d4e59 626static int spawn_sessiond(const char *pathname)
92360082
JG
627{
628 int ret = 0;
629 pid_t pid;
630
631 MSG("Spawning a session daemon");
92360082
JG
632 pid = fork();
633 if (pid == 0) {
634 /*
bbd44cae 635 * Spawn session daemon in daemon mode.
92360082 636 */
bbd44cae
PP
637 execlp(pathname, "lttng-sessiond",
638 "--daemonize", NULL);
92360082
JG
639 /* execlp only returns if error happened */
640 if (errno == ENOENT) {
641 ERR("No session daemon found. Use --sessiond-path.");
642 } else {
643 PERROR("execlp");
644 }
645 kill(getppid(), SIGTERM); /* wake parent */
646 exit(EXIT_FAILURE);
647 } else if (pid > 0) {
92360082 648 /*
bbd44cae
PP
649 * In daemon mode (--daemonize), sessiond only exits when
650 * it's ready to accept commands.
92360082 651 */
bbd44cae 652 for (;;) {
81527d36
JG
653 int status;
654 pid_t wait_pid_ret = waitpid(pid, &status, 0);
655
656 if (wait_pid_ret < 0) {
657 if (errno == EINTR) {
658 continue;
659 }
660 PERROR("waitpid");
661 ret = -errno;
662 goto end;
663 }
bbd44cae
PP
664
665 if (WIFSIGNALED(status)) {
666 ERR("Session daemon was killed by signal %d",
667 WTERMSIG(status));
668 ret = -1;
669 goto end;
670 } else if (WIFEXITED(status)) {
671 DBG("Session daemon terminated normally (exit status: %d)",
672 WEXITSTATUS(status));
673
674 if (WEXITSTATUS(status) != 0) {
675 ERR("Session daemon terminated with an error (exit status: %d)",
676 WEXITSTATUS(status));
677 ret = -1;
678 goto end;
679 }
680 break;
681 }
92360082 682 }
bbd44cae 683
92360082
JG
684 goto end;
685 } else {
686 PERROR("fork");
687 ret = -1;
688 goto end;
689 }
690
691end:
692 return ret;
693}
694
695/*
696 * launch_sessiond
697 *
698 * Check if the session daemon is available using
699 * the liblttngctl API for the check. If not, try to
700 * spawn a daemon.
701 */
702static int launch_sessiond(void)
703{
704 int ret;
b53d4e59 705 const char *pathname = NULL;
92360082
JG
706
707 ret = lttng_session_daemon_alive();
708 if (ret) {
709 /* Sessiond is alive, not an error */
710 ret = 0;
711 goto end;
712 }
713
714 /* Try command line option path */
715 pathname = opt_sessiond_path;
716
717 /* Try LTTNG_SESSIOND_PATH env variable */
718 if (pathname == NULL) {
719 pathname = getenv(DEFAULT_SESSIOND_PATH_ENV);
720 }
721
722 /* Try with configured path */
723 if (pathname == NULL) {
724 if (CONFIG_SESSIOND_BIN[0] != '\0') {
725 pathname = CONFIG_SESSIOND_BIN;
726 }
727 }
728
729 /* Try the default path */
730 if (pathname == NULL) {
731 pathname = INSTALL_BIN_PATH "/lttng-sessiond";
732 }
733
734 DBG("Session daemon binary path: %s", pathname);
735
736 /* Check existence and permissions */
737 ret = access(pathname, F_OK | X_OK);
738 if (ret < 0) {
739 ERR("No such file or access denied: %s", pathname);
740 goto end;
741 }
742
743 ret = spawn_sessiond(pathname);
92360082 744end:
0f4fa0d2
JG
745 if (ret) {
746 ERR("Problem occurred while launching session daemon (%s)",
747 pathname);
748 }
92360082
JG
749 return ret;
750}
751
7b3f7be2 752static
a8d119b5
JG
753int validate_url_option_combination(void)
754{
755 int ret = 0;
756 int used_count = 0;
757
758 used_count += !!opt_url;
759 used_count += !!opt_output_path;
760 used_count += (opt_data_url || opt_ctrl_url);
761 if (used_count > 1) {
762 ERR("Only one of the --set-url, --ctrl-url/data-url, or --output options may be used at once.");
763 ret = -1;
764 }
765
766 return ret;
767}
768
f3ed775e 769/*
74cc1d0f 770 * The 'create <options>' first level command
1c8d13c8
TD
771 *
772 * Returns one of the CMD_* result constants.
f3ed775e
DG
773 */
774int cmd_create(int argc, const char **argv)
775{
37d03ff7 776 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
ecc48a90 777 char *opt_arg = NULL;
68c7f6e5 778 const char *leftover = NULL;
f3ed775e
DG
779 static poptContext pc;
780
781 pc = poptGetContext(NULL, argc, argv, long_options, 0);
782 poptReadDefaultConfig(pc, 0);
783
784 while ((opt = poptGetNextOpt(pc)) != -1) {
785 switch (opt) {
786 case OPT_HELP:
4ba92f18 787 SHOW_HELP();
f3ed775e 788 goto end;
679b4943
SM
789 case OPT_LIST_OPTIONS:
790 list_cmd_options(stdout, long_options);
679b4943 791 goto end;
ecc48a90
JD
792 case OPT_LIVE_TIMER:
793 {
c7219617 794 uint64_t v;
ecc48a90
JD
795
796 errno = 0;
e2ecf532
JG
797 if (opt_arg) {
798 free(opt_arg);
799 opt_arg = nullptr;
800 }
801
ecc48a90 802 opt_arg = poptGetOptArg(pc);
d73c5802
DG
803 if (!opt_arg) {
804 /* Set up default values. */
805 opt_live_timer = (uint32_t) DEFAULT_LTTNG_LIVE_TIMER;
806 DBG("Session live timer interval set to default value %d",
807 opt_live_timer);
808 break;
809 }
810
c7219617
SM
811 if (utils_parse_time_suffix(opt_arg, &v) < 0) {
812 ERR("Wrong value for --live parameter: %s", opt_arg);
ecc48a90
JD
813 ret = CMD_ERROR;
814 goto end;
815 }
c7219617 816
ecc48a90
JD
817 if (v != (uint32_t) v) {
818 ERR("32-bit overflow in --live parameter: %s", opt_arg);
819 ret = CMD_ERROR;
820 goto end;
821 }
c7219617 822
0ed9e0be
JG
823 if (v == 0) {
824 ERR("Live timer interval must be greater than zero");
825 ret = CMD_ERROR;
826 goto end;
827 }
c7219617 828
ecc48a90
JD
829 opt_live_timer = (uint32_t) v;
830 DBG("Session live timer interval set to %d", opt_live_timer);
831 break;
832 }
f3ed775e 833 default:
f3ed775e
DG
834 ret = CMD_UNDEFINED;
835 goto end;
836 }
837 }
838
785d2d0d 839 if (opt_no_consumer) {
96fe6b8d 840 MSG("The option --no-consumer is obsolete. Use --no-output now.");
785d2d0d
DG
841 ret = CMD_WARNING;
842 goto end;
843 }
844
a8d119b5
JG
845 ret = validate_url_option_combination();
846 if (ret) {
847 ret = CMD_ERROR;
848 goto end;
849 }
850
92360082
JG
851 /* Spawn a session daemon if needed */
852 if (!opt_no_sessiond) {
853 ret = launch_sessiond();
854 if (ret) {
855 ret = CMD_ERROR;
856 goto end;
857 }
858 }
859
acc09215 860 /* MI initialization */
37d03ff7
JRJ
861 if (lttng_opt_mi) {
862 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
863 if (!writer) {
864 ret = -LTTNG_ERR_NOMEM;
865 goto end;
866 }
867
868 /* Open command element */
869 ret = mi_lttng_writer_command_open(writer,
870 mi_lttng_element_command_create);
871 if (ret) {
872 ret = CMD_ERROR;
873 goto end;
874 }
875
876 /* Open output element */
877 ret = mi_lttng_writer_open_element(writer,
878 mi_lttng_element_command_output);
879 if (ret) {
880 ret = CMD_ERROR;
881 goto end;
882 }
883 }
f3ed775e
DG
884 opt_session_name = (char*) poptGetArg(pc);
885
68c7f6e5
JD
886 leftover = poptGetArg(pc);
887 if (leftover) {
888 ERR("Unknown argument: %s", leftover);
889 ret = CMD_ERROR;
890 goto end;
891 }
892
37d03ff7
JRJ
893 command_ret = create_session();
894 if (command_ret) {
895 success = 0;
896 }
897
898 if (lttng_opt_mi) {
899 /* Close output element */
900 ret = mi_lttng_writer_close_element(writer);
901 if (ret) {
902 ret = CMD_ERROR;
903 goto end;
904 }
905
906 /* Success ? */
907 ret = mi_lttng_writer_write_element_bool(writer,
908 mi_lttng_element_command_success, success);
909 if (ret) {
910 ret = CMD_ERROR;
911 goto end;
912 }
913
914 /* Command element close */
915 ret = mi_lttng_writer_command_close(writer);
916 if (ret) {
917 ret = CMD_ERROR;
918 goto end;
919 }
920 }
f3ed775e
DG
921
922end:
37d03ff7
JRJ
923 /* Mi clean-up */
924 if (writer && mi_lttng_writer_destroy(writer)) {
925 /* Preserve original error code */
926 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
927 }
928
acc09215 929 /* Overwrite ret if an error occurred in create_session() */
37d03ff7
JRJ
930 ret = command_ret ? command_ret : ret;
931
e2ecf532 932 free(opt_arg);
ca1c3607 933 poptFreeContext(pc);
f3ed775e
DG
934 return ret;
935}
This page took 0.141761 seconds and 5 git commands to generate.