Fix: loading of live session within userspace domains
[lttng-tools.git] / src / bin / lttng / commands / create.c
CommitLineData
f3ed775e
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
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.
f3ed775e
DG
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 *
d14d33bf
AM
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.
f3ed775e
DG
16 */
17
18#define _GNU_SOURCE
6c1c0768 19#define _LGPL_SOURCE
a4b92340 20#include <assert.h>
ecc48a90 21#include <ctype.h>
f3ed775e
DG
22#include <popt.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/stat.h>
27#include <sys/types.h>
28#include <time.h>
29#include <unistd.h>
96ec43a8 30#include <signal.h>
550b3e4b 31#include <sys/wait.h>
f3ed775e 32
37d03ff7
JRJ
33#include <common/mi-lttng.h>
34
c399183f 35#include "../command.h"
679b4943 36#include "../utils.h"
f3ed775e 37
00e2e675 38#include <common/defaults.h>
42224349 39#include <common/sessiond-comm/sessiond-comm.h>
00e2e675 40#include <common/uri.h>
81b86775 41#include <common/utils.h>
16f6f820 42#include <lttng/snapshot.h>
42224349 43
f3ed775e
DG
44static char *opt_output_path;
45static char *opt_session_name;
a4b92340
DG
46static char *opt_url;
47static char *opt_ctrl_url;
48static char *opt_data_url;
d7ba1388 49static char *opt_shm_path;
a4b92340 50static int opt_no_consumer;
96fe6b8d 51static int opt_no_output;
16f6f820 52static int opt_snapshot;
ecc48a90 53static unsigned int opt_live_timer;
f3ed775e
DG
54
55enum {
56 OPT_HELP = 1,
679b4943 57 OPT_LIST_OPTIONS,
ecc48a90 58 OPT_LIVE_TIMER,
f3ed775e
DG
59};
60
37d03ff7
JRJ
61static struct mi_writer *writer;
62
f3ed775e
DG
63static struct poptOption long_options[] = {
64 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
679b4943
SM
65 {"help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL},
66 {"output", 'o', POPT_ARG_STRING, &opt_output_path, 0, NULL, NULL},
67 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
23d14dff
DG
68 {"set-url", 'U', POPT_ARG_STRING, &opt_url, 0, 0, 0},
69 {"ctrl-url", 'C', POPT_ARG_STRING, &opt_ctrl_url, 0, 0, 0},
70 {"data-url", 'D', POPT_ARG_STRING, &opt_data_url, 0, 0, 0},
96fe6b8d 71 {"no-output", 0, POPT_ARG_VAL, &opt_no_output, 1, 0, 0},
2bba9e53 72 {"no-consumer", 0, POPT_ARG_VAL, &opt_no_consumer, 1, 0, 0},
16f6f820 73 {"snapshot", 0, POPT_ARG_VAL, &opt_snapshot, 1, 0, 0},
d73c5802 74 {"live", 0, POPT_ARG_INT | POPT_ARGFLAG_OPTIONAL, 0, OPT_LIVE_TIMER, 0, 0},
d7ba1388 75 {"shm-path", 0, POPT_ARG_STRING, &opt_shm_path, 0, 0, 0},
f3ed775e
DG
76 {0, 0, 0, 0, 0, 0, 0}
77};
78
16de1a24
DG
79/*
80 * Please have a look at src/lib/lttng-ctl/lttng-ctl.c for more information on
81 * why this declaration exists and used ONLY in for this command.
82 */
07424f16 83extern int _lttng_create_session_ext(const char *name, const char *url,
548527ad 84 const char *datetime);
07424f16 85
f3ed775e
DG
86/*
87 * usage
88 */
89static void usage(FILE *ofp)
90{
a4b92340 91 fprintf(ofp, "usage: lttng create [NAME] [OPTIONS] \n");
f3ed775e 92 fprintf(ofp, "\n");
a4b92340
DG
93 fprintf(ofp, "Without a given NAME, the default is 'auto-<yyyymmdd>-<hhmmss>'\n");
94 fprintf(ofp, "\n");
95 fprintf(ofp, "Options:\n");
f3ed775e 96 fprintf(ofp, " -h, --help Show this help\n");
1c8d13c8 97 fprintf(ofp, " --list-options Simple listing of options\n");
58a97671 98 fprintf(ofp, " -o, --output PATH Specify output path for traces\n");
96fe6b8d 99 fprintf(ofp, " --no-output Traces will not be outputted\n");
a2d6893b 100 fprintf(ofp, " --snapshot Set the session in snapshot mode.\n");
96fe6b8d 101 fprintf(ofp, " Created in no-output mode and uses the URL,\n");
16f6f820
DG
102 fprintf(ofp, " if one, as the default snapshot output.\n");
103 fprintf(ofp, " Every channel will be set in overwrite mode\n");
104 fprintf(ofp, " and with mmap output (splice not supported).\n");
d73c5802 105 fprintf(ofp, " --live [USEC] Set the session in live-reading mode.\n");
ecc48a90
JD
106 fprintf(ofp, " The delay parameter in micro-seconds is the\n");
107 fprintf(ofp, " maximum time the user can wait for the data\n");
d73c5802
DG
108 fprintf(ofp, " to be flushed. Can be set with a network\n");
109 fprintf(ofp, " URL (-U or -C/-D) and must have a relayd listening.\n");
110 fprintf(ofp, " By default, %u is used for the timer and the\n",
111 DEFAULT_LTTNG_LIVE_TIMER);
112 fprintf(ofp, " network URL is set to net://127.0.0.1.\n");
d7ba1388
MD
113 fprintf(ofp, " --shm-path PATH Path where shared memory holding buffers\n");
114 fprintf(ofp, " should be created. Useful when used with pramfs\n");
115 fprintf(ofp, " to extract trace data after crash.\n");
a4b92340
DG
116 fprintf(ofp, "\n");
117 fprintf(ofp, "Extended Options:\n");
118 fprintf(ofp, "\n");
119 fprintf(ofp, "Using these options, each API call can be controlled individually.\n");
120 fprintf(ofp, "\n");
121 fprintf(ofp, " -U, --set-url=URL Set URL destination of the trace data.\n");
00e2e675 122 fprintf(ofp, " It is persistent for the session lifetime.\n");
a4b92340 123 fprintf(ofp, " This will set both data and control URL.\n");
a4b92340
DG
124 fprintf(ofp, " -C, --ctrl-url=URL Set control path URL. (Must use -D also)\n");
125 fprintf(ofp, " -D, --data-url=URL Set data path URL. (Must use -C also)\n");
a4b92340
DG
126 fprintf(ofp, "\n");
127 fprintf(ofp, "Please refer to the man page (lttng(1)) for more information on network\n");
128 fprintf(ofp, "streaming mechanisms and explanation of the control and data port\n");
129 fprintf(ofp, "You must have a running remote lttng-relayd for network streaming\n");
130 fprintf(ofp, "\n");
131 fprintf(ofp, "URL format is has followed:\n");
132 fprintf(ofp, "\n");
133 fprintf(ofp, " proto://[HOST|IP][:PORT1[:PORT2]][/TRACE_PATH]\n");
134 fprintf(ofp, "\n");
135 fprintf(ofp, " Supported protocols are (proto):\n");
136 fprintf(ofp, " > file://...\n");
137 fprintf(ofp, " Local filesystem full path.\n");
138 fprintf(ofp, "\n");
16de1a24 139 fprintf(ofp, " > net[6]://...\n");
a4b92340
DG
140 fprintf(ofp, " This will use the default network transport layer which is\n");
141 fprintf(ofp, " TCP for both control (PORT1) and data port (PORT2).\n");
142 fprintf(ofp, " The default ports are respectively 5342 and 5343.\n");
143 fprintf(ofp, "\n");
0ab61906 144 fprintf(ofp, " > tcp[6]://...\n");
a4b92340
DG
145 fprintf(ofp, " Can only be used with -C and -D together\n");
146 fprintf(ofp, "\n");
147 fprintf(ofp, "NOTE: IPv6 address MUST be enclosed in brackets '[]' (rfc2732)\n");
148 fprintf(ofp, "\n");
149 fprintf(ofp, "Examples:\n");
150 fprintf(ofp, " # lttng create -U net://192.168.1.42\n");
151 fprintf(ofp, " Uses TCP and default ports for the given destination.\n");
152 fprintf(ofp, " # lttng create -U net6://[fe80::f66d:4ff:fe53:d220]\n");
153 fprintf(ofp, " Uses TCP, default ports and IPv6.\n");
154 fprintf(ofp, " # lttng create s1 -U net://myhost.com:3229\n");
155 fprintf(ofp, " Set the consumer to the remote HOST on port 3229 for control.\n");
f3ed775e
DG
156 fprintf(ofp, "\n");
157}
158
37d03ff7 159/*
485ca16f 160 * Retrieve the created session and mi output it based on provided argument
37d03ff7
JRJ
161 * This is currently a summary of what was pretty printed and is subject to
162 * enhancements.
37d03ff7
JRJ
163 */
164static int mi_created_session(const char *session_name)
165{
166 int ret, i, count, found;
167 struct lttng_session *sessions;
168
169 /* session_name should not be null */
170 assert(session_name);
171 assert(writer);
172
173 count = lttng_list_sessions(&sessions);
174 if (count < 0) {
175 ret = count;
176 ERR("%s", lttng_strerror(ret));
177 goto error;
178 }
179
180 if (count == 0) {
181 ERR("Error session creation failed: session %s not found", session_name);
182 ret = -LTTNG_ERR_SESS_NOT_FOUND;
183 goto end;
184 }
185
186 found = 0;
187 for (i = 0; i < count; i++) {
188 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
189 found = 1;
190 ret = mi_lttng_session(writer, &sessions[i], 0);
191 if (ret) {
192 goto error;
193 }
194 break;
195 }
196 }
197
198 if (!found) {
199 ret = -LTTNG_ERR_SESS_NOT_FOUND;
200 } else {
201 ret = CMD_SUCCESS;
202 }
203
204error:
205 free(sessions);
206end:
207 return ret;
208}
209
00e2e675 210/*
a4b92340 211 * For a session name, set the consumer URLs.
00e2e675 212 */
a4b92340
DG
213static int set_consumer_url(const char *session_name, const char *ctrl_url,
214 const char *data_url)
00e2e675 215{
a4b92340
DG
216 int ret;
217 struct lttng_handle *handle;
a4b92340
DG
218
219 assert(session_name);
220
221 /*
064e8bfd
JG
222 * Set handle with the session_name, but no domain. This implies that
223 * the actions taken with this handle apply on the tracing session
224 * rather then the domain-specific session.
a4b92340 225 */
064e8bfd 226 handle = lttng_create_handle(session_name, NULL);
a4b92340
DG
227 if (handle == NULL) {
228 ret = CMD_FATAL;
229 goto error;
230 }
00e2e675 231
a4b92340
DG
232 ret = lttng_set_consumer_url(handle, ctrl_url, data_url);
233 if (ret < 0) {
234 goto error;
00e2e675
DG
235 }
236
a4b92340
DG
237 if (ctrl_url) {
238 MSG("Control URL %s set for session %s", ctrl_url, session_name);
00e2e675
DG
239 }
240
a4b92340
DG
241 if (data_url) {
242 MSG("Data URL %s set for session %s", data_url, session_name);
243 }
244
245error:
246 lttng_destroy_handle(handle);
247 return ret;
248}
249
16f6f820
DG
250static int add_snapshot_output(const char *session_name, const char *ctrl_url,
251 const char *data_url)
252{
253 int ret;
254 struct lttng_snapshot_output *output = NULL;
255
256 assert(session_name);
257
258 output = lttng_snapshot_output_create();
259 if (!output) {
260 ret = CMD_FATAL;
261 goto error_create;
262 }
263
264 if (ctrl_url) {
265 ret = lttng_snapshot_output_set_ctrl_url(ctrl_url, output);
266 if (ret < 0) {
267 goto error;
268 }
269 }
270
271 if (data_url) {
272 ret = lttng_snapshot_output_set_data_url(data_url, output);
273 if (ret < 0) {
274 goto error;
275 }
276 }
277
278 /* This call, if successful, populates the id of the output object. */
279 ret = lttng_snapshot_add_output(session_name, output);
280 if (ret < 0) {
281 goto error;
282 }
283
284error:
285 lttng_snapshot_output_destroy(output);
286error_create:
287 return ret;
288}
289
f3ed775e 290/*
1c8d13c8
TD
291 * Create a tracing session.
292 * If no name is specified, a default name is generated.
f3ed775e 293 *
1c8d13c8 294 * Returns one of the CMD_* result constants.
f3ed775e 295 */
a4b92340 296static int create_session(void)
f3ed775e 297{
a4b92340 298 int ret;
07424f16 299 char *session_name = NULL, *traces_path = NULL, *alloc_path = NULL;
a4b92340 300 char *alloc_url = NULL, *url = NULL, datetime[16];
487b253b 301 char session_name_date[NAME_MAX + 17], *print_str_url = NULL;
f3ed775e
DG
302 time_t rawtime;
303 struct tm *timeinfo;
d7ba1388 304 char shm_path[PATH_MAX] = "";
f3ed775e 305
d6175221
DG
306 /* Get date and time for automatic session name/path */
307 time(&rawtime);
308 timeinfo = localtime(&rawtime);
309 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
310
f3ed775e
DG
311 /* Auto session name creation */
312 if (opt_session_name == NULL) {
07424f16
DG
313 ret = snprintf(session_name_date, sizeof(session_name_date),
314 DEFAULT_SESSION_NAME "-%s", datetime);
d6175221 315 if (ret < 0) {
07424f16
DG
316 PERROR("snprintf session name");
317 goto error;
318 }
04965770 319 session_name = session_name_date;
07424f16 320 DBG("Auto session name set to %s", session_name_date);
f3ed775e 321 } else {
487b253b
DG
322 if (strlen(opt_session_name) > NAME_MAX) {
323 ERR("Session name too long. Length must be lower or equal to %d",
324 NAME_MAX);
325 ret = LTTNG_ERR_SESSION_FAIL;
326 goto error;
327 }
4b861950
DG
328 /*
329 * Check if the session name begins with "auto-" or is exactly "auto".
330 * Both are reserved for the default session name. See bug #449 to
331 * understand why we need to check both here.
332 */
333 if ((strncmp(opt_session_name, DEFAULT_SESSION_NAME "-",
334 strlen(DEFAULT_SESSION_NAME) + 1) == 0) ||
335 (strncmp(opt_session_name, DEFAULT_SESSION_NAME,
61b35a5a 336 strlen(DEFAULT_SESSION_NAME)) == 0 &&
4b861950 337 strlen(opt_session_name) == strlen(DEFAULT_SESSION_NAME))) {
61b35a5a
DG
338 ERR("%s is a reserved keyword for default session(s)",
339 DEFAULT_SESSION_NAME);
340 ret = CMD_ERROR;
341 goto error;
342 }
f3ed775e 343 session_name = opt_session_name;
07424f16
DG
344 ret = snprintf(session_name_date, sizeof(session_name_date),
345 "%s-%s", session_name, datetime);
346 if (ret < 0) {
347 PERROR("snprintf session name");
348 goto error;
349 }
f3ed775e
DG
350 }
351
1a241656
DG
352 if ((!opt_ctrl_url && opt_data_url) || (opt_ctrl_url && !opt_data_url)) {
353 ERR("You need both control and data URL.");
354 ret = CMD_ERROR;
355 goto error;
356 }
357
785d2d0d 358 if (opt_output_path != NULL) {
81b86775 359 traces_path = utils_expand_path(opt_output_path);
00e2e675
DG
360 if (traces_path == NULL) {
361 ret = CMD_ERROR;
362 goto error;
363 }
364
37d03ff7 365 /* Create URL string from the local file system path */
a4b92340 366 ret = asprintf(&alloc_url, "file://%s", traces_path);
00e2e675 367 if (ret < 0) {
a4b92340 368 PERROR("asprintf url path");
00e2e675
DG
369 ret = CMD_FATAL;
370 goto error;
371 }
a4b92340
DG
372 /* URL to use in the lttng_create_session() call */
373 url = alloc_url;
40b03f52 374 print_str_url = traces_path;
a4b92340
DG
375 } else if (opt_url) { /* Handling URL (-U opt) */
376 url = opt_url;
40b03f52 377 print_str_url = url;
1a241656
DG
378 } else if (opt_data_url && opt_ctrl_url) {
379 /*
380 * With both control and data, we'll be setting the consumer URL after
381 * session creation thus use no URL.
382 */
383 url = NULL;
2bba9e53 384 } else if (!opt_no_output) {
ab2e5c7c
MD
385 char *tmp_path;
386
00e2e675 387 /* Auto output path */
ab2e5c7c
MD
388 tmp_path = utils_get_home_dir();
389 if (tmp_path == NULL) {
d386b234 390 ERR("HOME path not found.\n \
00e2e675 391 Please specify an output path using -o, --output PATH");
f3ed775e
DG
392 ret = CMD_FATAL;
393 goto error;
394 }
ab2e5c7c
MD
395 alloc_path = strdup(tmp_path);
396 if (!alloc_path) {
397 PERROR("allocating alloc_path");
398 ret = CMD_FATAL;
399 goto error;
400 }
07424f16
DG
401 ret = asprintf(&alloc_url,
402 "file://%s/" DEFAULT_TRACE_DIR_NAME "/%s",
403 alloc_path, session_name_date);
58a97671 404 if (ret < 0) {
00e2e675
DG
405 PERROR("asprintf trace dir name");
406 ret = CMD_FATAL;
58a97671
DG
407 goto error;
408 }
00e2e675 409
a4b92340 410 url = alloc_url;
1a241656 411 print_str_url = alloc_url + strlen("file://");
2bba9e53 412 } else {
96fe6b8d 413 /* No output means --no-output or --snapshot mode. */
2bba9e53 414 url = NULL;
00e2e675
DG
415 }
416
1a241656 417 /* Use default live URL if NO url is/are found. */
ecc48a90 418 if ((opt_live_timer && !opt_url) && (opt_live_timer && !opt_data_url)) {
d73c5802
DG
419 ret = asprintf(&alloc_url, "net://127.0.0.1");
420 if (ret < 0) {
421 PERROR("asprintf default live URL");
422 ret = CMD_FATAL;
423 goto error;
424 }
425 url = alloc_url;
426 print_str_url = url;
ecc48a90
JD
427 }
428
429 if (opt_snapshot && opt_live_timer) {
430 ERR("Snapshot and live modes are mutually exclusive.");
431 ret = CMD_ERROR;
432 goto error;
433 }
434
16f6f820
DG
435 if (opt_snapshot) {
436 /* No output by default. */
437 const char *snapshot_url = NULL;
438
439 if (opt_url) {
440 snapshot_url = url;
441 } else if (!opt_data_url && !opt_ctrl_url) {
442 /* This is the session path that we need to use as output. */
443 snapshot_url = url;
444 }
445 ret = lttng_create_session_snapshot(session_name, snapshot_url);
ecc48a90 446 } else if (opt_live_timer) {
8960e9cd
DG
447 const char *pathname;
448
449 if (opt_relayd_path) {
450 pathname = opt_relayd_path;
451 } else {
452 pathname = INSTALL_BIN_PATH "/lttng-relayd";
453 }
2c521c63
DG
454 if (!opt_url && !opt_data_url && !check_relayd() &&
455 spawn_relayd(pathname, 0) < 0) {
8960e9cd
DG
456 goto error;
457 }
ecc48a90 458 ret = lttng_create_session_live(session_name, url, opt_live_timer);
16f6f820 459 } else {
548527ad 460 ret = _lttng_create_session_ext(session_name, url, datetime);
16f6f820 461 }
f3ed775e 462 if (ret < 0) {
ae856491 463 /* Don't set ret so lttng can interpret the sessiond error. */
42224349 464 switch (-ret) {
f73fabfd 465 case LTTNG_ERR_EXIST_SESS:
42224349
DG
466 WARN("Session %s already exists", session_name);
467 break;
60e835ca 468 default:
60e835ca 469 break;
42224349 470 }
f3ed775e
DG
471 goto error;
472 }
473
0ab61906 474 if (opt_ctrl_url && opt_data_url) {
16f6f820
DG
475 if (opt_snapshot) {
476 ret = add_snapshot_output(session_name, opt_ctrl_url,
477 opt_data_url);
478 } else {
479 /* Setting up control URI (-C or/and -D opt) */
480 ret = set_consumer_url(session_name, opt_ctrl_url, opt_data_url);
481 }
a4b92340 482 if (ret < 0) {
4f50c803
DG
483 /* Destroy created session because the URL are not valid. */
484 lttng_destroy_session(session_name);
a4b92340
DG
485 goto error;
486 }
4f50c803
DG
487 }
488
d7ba1388
MD
489 if (opt_shm_path) {
490 ret = snprintf(shm_path, sizeof(shm_path),
491 "%s/%s", opt_shm_path, session_name_date);
492 if (ret < 0) {
493 PERROR("snprintf shm_path");
494 goto error;
495 }
496
497 ret = lttng_set_session_shm_path(session_name, shm_path);
498 if (ret < 0) {
499 lttng_destroy_session(session_name);
500 goto error;
501 }
502 }
503
4f50c803 504 MSG("Session %s created.", session_name);
16f6f820 505 if (print_str_url && !opt_snapshot) {
4f50c803 506 MSG("Traces will be written in %s", print_str_url);
d73c5802
DG
507
508 if (opt_live_timer) {
509 MSG("Live timer set to %u usec", opt_live_timer);
510 }
16f6f820
DG
511 } else if (opt_snapshot) {
512 if (print_str_url) {
513 MSG("Default snapshot output set to: %s", print_str_url);
514 }
515 MSG("Snapshot mode set. Every channel enabled for that session will "
1daafe0d 516 "be set in overwrite mode and mmap output.");
a4b92340 517 }
d7ba1388
MD
518 if (opt_shm_path) {
519 MSG("Session %s set to shm_path: %s.", session_name,
520 shm_path);
521 }
a4b92340 522
37d03ff7
JRJ
523 /* Mi output */
524 if (lttng_opt_mi) {
525 ret = mi_created_session(session_name);
526 if (ret) {
527 ret = CMD_ERROR;
528 goto error;
529 }
530 }
531
58a97671
DG
532 /* Init lttng session config */
533 ret = config_init(session_name);
f3ed775e 534 if (ret < 0) {
27089920 535 ret = CMD_ERROR;
f3ed775e
DG
536 goto error;
537 }
538
f3ed775e
DG
539 ret = CMD_SUCCESS;
540
541error:
0e428499
DG
542 free(alloc_url);
543 free(traces_path);
87f222b6 544 free(alloc_path);
a4b92340
DG
545
546 if (ret < 0) {
547 ERR("%s", lttng_strerror(ret));
548 }
f3ed775e
DG
549 return ret;
550}
551
96ec43a8
JG
552/*
553 * spawn_sessiond
554 *
555 * Spawn a session daemon by forking and execv.
556 */
557static int spawn_sessiond(char *pathname)
558{
559 int ret = 0;
560 pid_t pid;
561
562 MSG("Spawning a session daemon");
96ec43a8
JG
563 pid = fork();
564 if (pid == 0) {
565 /*
550b3e4b 566 * Spawn session daemon in daemon mode.
96ec43a8 567 */
550b3e4b
PP
568 execlp(pathname, "lttng-sessiond",
569 "--daemonize", NULL);
96ec43a8
JG
570 /* execlp only returns if error happened */
571 if (errno == ENOENT) {
572 ERR("No session daemon found. Use --sessiond-path.");
573 } else {
574 PERROR("execlp");
575 }
576 kill(getppid(), SIGTERM); /* wake parent */
577 exit(EXIT_FAILURE);
578 } else if (pid > 0) {
96ec43a8 579 /*
550b3e4b
PP
580 * In daemon mode (--daemonize), sessiond only exits when
581 * it's ready to accept commands.
96ec43a8 582 */
550b3e4b 583 for (;;) {
09eea2d6
JG
584 int status;
585 pid_t wait_pid_ret = waitpid(pid, &status, 0);
586
587 if (wait_pid_ret < 0) {
588 if (errno == EINTR) {
589 continue;
590 }
591 PERROR("waitpid");
592 ret = -errno;
593 goto end;
594 }
550b3e4b
PP
595
596 if (WIFSIGNALED(status)) {
597 ERR("Session daemon was killed by signal %d",
598 WTERMSIG(status));
599 ret = -1;
600 goto end;
601 } else if (WIFEXITED(status)) {
602 DBG("Session daemon terminated normally (exit status: %d)",
603 WEXITSTATUS(status));
604
605 if (WEXITSTATUS(status) != 0) {
606 ERR("Session daemon terminated with an error (exit status: %d)",
607 WEXITSTATUS(status));
608 ret = -1;
609 goto end;
610 }
611 break;
612 }
96ec43a8 613 }
550b3e4b 614
96ec43a8
JG
615 goto end;
616 } else {
617 PERROR("fork");
618 ret = -1;
619 goto end;
620 }
621
622end:
623 return ret;
624}
625
626/*
627 * launch_sessiond
628 *
629 * Check if the session daemon is available using
630 * the liblttngctl API for the check. If not, try to
631 * spawn a daemon.
632 */
633static int launch_sessiond(void)
634{
635 int ret;
636 char *pathname = NULL;
637
638 ret = lttng_session_daemon_alive();
639 if (ret) {
640 /* Sessiond is alive, not an error */
641 ret = 0;
642 goto end;
643 }
644
645 /* Try command line option path */
646 pathname = opt_sessiond_path;
647
648 /* Try LTTNG_SESSIOND_PATH env variable */
649 if (pathname == NULL) {
650 pathname = getenv(DEFAULT_SESSIOND_PATH_ENV);
651 }
652
653 /* Try with configured path */
654 if (pathname == NULL) {
655 if (CONFIG_SESSIOND_BIN[0] != '\0') {
656 pathname = CONFIG_SESSIOND_BIN;
657 }
658 }
659
660 /* Try the default path */
661 if (pathname == NULL) {
662 pathname = INSTALL_BIN_PATH "/lttng-sessiond";
663 }
664
665 DBG("Session daemon binary path: %s", pathname);
666
667 /* Check existence and permissions */
668 ret = access(pathname, F_OK | X_OK);
669 if (ret < 0) {
670 ERR("No such file or access denied: %s", pathname);
671 goto end;
672 }
673
674 ret = spawn_sessiond(pathname);
96ec43a8 675end:
fcbf4ae7
JG
676 if (ret) {
677 ERR("Problem occurred while launching session daemon (%s)",
678 pathname);
679 }
96ec43a8
JG
680 return ret;
681}
682
f3ed775e 683/*
74cc1d0f 684 * The 'create <options>' first level command
1c8d13c8
TD
685 *
686 * Returns one of the CMD_* result constants.
f3ed775e
DG
687 */
688int cmd_create(int argc, const char **argv)
689{
37d03ff7 690 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
ecc48a90 691 char *opt_arg = NULL;
f3ed775e
DG
692 static poptContext pc;
693
694 pc = poptGetContext(NULL, argc, argv, long_options, 0);
695 poptReadDefaultConfig(pc, 0);
696
697 while ((opt = poptGetNextOpt(pc)) != -1) {
698 switch (opt) {
699 case OPT_HELP:
27089920 700 usage(stdout);
f3ed775e 701 goto end;
679b4943
SM
702 case OPT_LIST_OPTIONS:
703 list_cmd_options(stdout, long_options);
679b4943 704 goto end;
ecc48a90
JD
705 case OPT_LIVE_TIMER:
706 {
707 unsigned long v;
708
709 errno = 0;
710 opt_arg = poptGetOptArg(pc);
d73c5802
DG
711 if (!opt_arg) {
712 /* Set up default values. */
713 opt_live_timer = (uint32_t) DEFAULT_LTTNG_LIVE_TIMER;
714 DBG("Session live timer interval set to default value %d",
715 opt_live_timer);
716 break;
717 }
718
ecc48a90
JD
719 v = strtoul(opt_arg, NULL, 0);
720 if (errno != 0 || !isdigit(opt_arg[0])) {
721 ERR("Wrong value in --live parameter: %s", opt_arg);
722 ret = CMD_ERROR;
723 goto end;
724 }
725 if (v != (uint32_t) v) {
726 ERR("32-bit overflow in --live parameter: %s", opt_arg);
727 ret = CMD_ERROR;
728 goto end;
729 }
0ed9e0be
JG
730 if (v == 0) {
731 ERR("Live timer interval must be greater than zero");
732 ret = CMD_ERROR;
733 goto end;
734 }
ecc48a90
JD
735 opt_live_timer = (uint32_t) v;
736 DBG("Session live timer interval set to %d", opt_live_timer);
737 break;
738 }
f3ed775e
DG
739 default:
740 usage(stderr);
741 ret = CMD_UNDEFINED;
742 goto end;
743 }
744 }
745
785d2d0d 746 if (opt_no_consumer) {
96fe6b8d 747 MSG("The option --no-consumer is obsolete. Use --no-output now.");
785d2d0d
DG
748 ret = CMD_WARNING;
749 goto end;
750 }
751
96ec43a8
JG
752 /* Spawn a session daemon if needed */
753 if (!opt_no_sessiond) {
754 ret = launch_sessiond();
755 if (ret) {
756 ret = CMD_ERROR;
757 goto end;
758 }
759 }
760
acc09215 761 /* MI initialization */
37d03ff7
JRJ
762 if (lttng_opt_mi) {
763 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
764 if (!writer) {
765 ret = -LTTNG_ERR_NOMEM;
766 goto end;
767 }
768
769 /* Open command element */
770 ret = mi_lttng_writer_command_open(writer,
771 mi_lttng_element_command_create);
772 if (ret) {
773 ret = CMD_ERROR;
774 goto end;
775 }
776
777 /* Open output element */
778 ret = mi_lttng_writer_open_element(writer,
779 mi_lttng_element_command_output);
780 if (ret) {
781 ret = CMD_ERROR;
782 goto end;
783 }
784 }
f3ed775e
DG
785 opt_session_name = (char*) poptGetArg(pc);
786
37d03ff7
JRJ
787 command_ret = create_session();
788 if (command_ret) {
789 success = 0;
790 }
791
792 if (lttng_opt_mi) {
793 /* Close output element */
794 ret = mi_lttng_writer_close_element(writer);
795 if (ret) {
796 ret = CMD_ERROR;
797 goto end;
798 }
799
800 /* Success ? */
801 ret = mi_lttng_writer_write_element_bool(writer,
802 mi_lttng_element_command_success, success);
803 if (ret) {
804 ret = CMD_ERROR;
805 goto end;
806 }
807
808 /* Command element close */
809 ret = mi_lttng_writer_command_close(writer);
810 if (ret) {
811 ret = CMD_ERROR;
812 goto end;
813 }
814 }
f3ed775e
DG
815
816end:
37d03ff7
JRJ
817 /* Mi clean-up */
818 if (writer && mi_lttng_writer_destroy(writer)) {
819 /* Preserve original error code */
820 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
821 }
822
acc09215 823 /* Overwrite ret if an error occurred in create_session() */
37d03ff7
JRJ
824 ret = command_ret ? command_ret : ret;
825
ca1c3607 826 poptFreeContext(pc);
f3ed775e
DG
827 return ret;
828}
This page took 0.080658 seconds and 4 git commands to generate.