Daemonize sessiond on `lttng create`
[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>
92360082 30#include <signal.h>
bbd44cae 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,
ecc48a90 84 const char *datetime, int live_timer);
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;
218 struct lttng_domain dom;
219
220 assert(session_name);
221
222 /*
223 * Set handle with the session name and the domain set to 0. This means to
224 * the session daemon that the next action applies on the tracing session
225 * rather then the domain specific session.
226 */
227 memset(&dom, 0, sizeof(dom));
228
229 handle = lttng_create_handle(session_name, &dom);
230 if (handle == NULL) {
231 ret = CMD_FATAL;
232 goto error;
233 }
00e2e675 234
a4b92340
DG
235 ret = lttng_set_consumer_url(handle, ctrl_url, data_url);
236 if (ret < 0) {
237 goto error;
00e2e675
DG
238 }
239
a4b92340
DG
240 if (ctrl_url) {
241 MSG("Control URL %s set for session %s", ctrl_url, session_name);
00e2e675
DG
242 }
243
a4b92340
DG
244 if (data_url) {
245 MSG("Data URL %s set for session %s", data_url, session_name);
246 }
247
248error:
249 lttng_destroy_handle(handle);
250 return ret;
251}
252
16f6f820
DG
253static int add_snapshot_output(const char *session_name, const char *ctrl_url,
254 const char *data_url)
255{
256 int ret;
257 struct lttng_snapshot_output *output = NULL;
258
259 assert(session_name);
260
261 output = lttng_snapshot_output_create();
262 if (!output) {
263 ret = CMD_FATAL;
264 goto error_create;
265 }
266
267 if (ctrl_url) {
268 ret = lttng_snapshot_output_set_ctrl_url(ctrl_url, output);
269 if (ret < 0) {
270 goto error;
271 }
272 }
273
274 if (data_url) {
275 ret = lttng_snapshot_output_set_data_url(data_url, output);
276 if (ret < 0) {
277 goto error;
278 }
279 }
280
281 /* This call, if successful, populates the id of the output object. */
282 ret = lttng_snapshot_add_output(session_name, output);
283 if (ret < 0) {
284 goto error;
285 }
286
287error:
288 lttng_snapshot_output_destroy(output);
289error_create:
290 return ret;
291}
292
f3ed775e 293/*
1c8d13c8
TD
294 * Create a tracing session.
295 * If no name is specified, a default name is generated.
f3ed775e 296 *
1c8d13c8 297 * Returns one of the CMD_* result constants.
f3ed775e 298 */
a4b92340 299static int create_session(void)
f3ed775e 300{
a4b92340 301 int ret;
07424f16 302 char *session_name = NULL, *traces_path = NULL, *alloc_path = NULL;
a4b92340 303 char *alloc_url = NULL, *url = NULL, datetime[16];
487b253b 304 char session_name_date[NAME_MAX + 17], *print_str_url = NULL;
f3ed775e
DG
305 time_t rawtime;
306 struct tm *timeinfo;
d7ba1388 307 char shm_path[PATH_MAX] = "";
f3ed775e 308
d6175221
DG
309 /* Get date and time for automatic session name/path */
310 time(&rawtime);
311 timeinfo = localtime(&rawtime);
312 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
313
f3ed775e
DG
314 /* Auto session name creation */
315 if (opt_session_name == NULL) {
07424f16
DG
316 ret = snprintf(session_name_date, sizeof(session_name_date),
317 DEFAULT_SESSION_NAME "-%s", datetime);
d6175221 318 if (ret < 0) {
07424f16
DG
319 PERROR("snprintf session name");
320 goto error;
321 }
04965770 322 session_name = session_name_date;
07424f16 323 DBG("Auto session name set to %s", session_name_date);
f3ed775e 324 } else {
487b253b
DG
325 if (strlen(opt_session_name) > NAME_MAX) {
326 ERR("Session name too long. Length must be lower or equal to %d",
327 NAME_MAX);
328 ret = LTTNG_ERR_SESSION_FAIL;
329 goto error;
330 }
4b861950
DG
331 /*
332 * Check if the session name begins with "auto-" or is exactly "auto".
333 * Both are reserved for the default session name. See bug #449 to
334 * understand why we need to check both here.
335 */
336 if ((strncmp(opt_session_name, DEFAULT_SESSION_NAME "-",
337 strlen(DEFAULT_SESSION_NAME) + 1) == 0) ||
338 (strncmp(opt_session_name, DEFAULT_SESSION_NAME,
61b35a5a 339 strlen(DEFAULT_SESSION_NAME)) == 0 &&
4b861950 340 strlen(opt_session_name) == strlen(DEFAULT_SESSION_NAME))) {
61b35a5a
DG
341 ERR("%s is a reserved keyword for default session(s)",
342 DEFAULT_SESSION_NAME);
343 ret = CMD_ERROR;
344 goto error;
345 }
f3ed775e 346 session_name = opt_session_name;
07424f16
DG
347 ret = snprintf(session_name_date, sizeof(session_name_date),
348 "%s-%s", session_name, datetime);
349 if (ret < 0) {
350 PERROR("snprintf session name");
351 goto error;
352 }
f3ed775e
DG
353 }
354
1a241656
DG
355 if ((!opt_ctrl_url && opt_data_url) || (opt_ctrl_url && !opt_data_url)) {
356 ERR("You need both control and data URL.");
357 ret = CMD_ERROR;
358 goto error;
359 }
360
785d2d0d 361 if (opt_output_path != NULL) {
81b86775 362 traces_path = utils_expand_path(opt_output_path);
00e2e675
DG
363 if (traces_path == NULL) {
364 ret = CMD_ERROR;
365 goto error;
366 }
367
37d03ff7 368 /* Create URL string from the local file system path */
a4b92340 369 ret = asprintf(&alloc_url, "file://%s", traces_path);
00e2e675 370 if (ret < 0) {
a4b92340 371 PERROR("asprintf url path");
00e2e675
DG
372 ret = CMD_FATAL;
373 goto error;
374 }
a4b92340
DG
375 /* URL to use in the lttng_create_session() call */
376 url = alloc_url;
40b03f52 377 print_str_url = traces_path;
a4b92340
DG
378 } else if (opt_url) { /* Handling URL (-U opt) */
379 url = opt_url;
40b03f52 380 print_str_url = url;
1a241656
DG
381 } else if (opt_data_url && opt_ctrl_url) {
382 /*
383 * With both control and data, we'll be setting the consumer URL after
384 * session creation thus use no URL.
385 */
386 url = NULL;
2bba9e53 387 } else if (!opt_no_output) {
ab2e5c7c
MD
388 char *tmp_path;
389
00e2e675 390 /* Auto output path */
ab2e5c7c
MD
391 tmp_path = utils_get_home_dir();
392 if (tmp_path == NULL) {
d386b234 393 ERR("HOME path not found.\n \
00e2e675 394 Please specify an output path using -o, --output PATH");
f3ed775e
DG
395 ret = CMD_FATAL;
396 goto error;
397 }
ab2e5c7c
MD
398 alloc_path = strdup(tmp_path);
399 if (!alloc_path) {
400 PERROR("allocating alloc_path");
401 ret = CMD_FATAL;
402 goto error;
403 }
07424f16
DG
404 ret = asprintf(&alloc_url,
405 "file://%s/" DEFAULT_TRACE_DIR_NAME "/%s",
406 alloc_path, session_name_date);
58a97671 407 if (ret < 0) {
00e2e675
DG
408 PERROR("asprintf trace dir name");
409 ret = CMD_FATAL;
58a97671
DG
410 goto error;
411 }
00e2e675 412
a4b92340 413 url = alloc_url;
1a241656 414 print_str_url = alloc_url + strlen("file://");
2bba9e53 415 } else {
96fe6b8d 416 /* No output means --no-output or --snapshot mode. */
2bba9e53 417 url = NULL;
00e2e675
DG
418 }
419
1a241656 420 /* Use default live URL if NO url is/are found. */
ecc48a90 421 if ((opt_live_timer && !opt_url) && (opt_live_timer && !opt_data_url)) {
d73c5802
DG
422 ret = asprintf(&alloc_url, "net://127.0.0.1");
423 if (ret < 0) {
424 PERROR("asprintf default live URL");
425 ret = CMD_FATAL;
426 goto error;
427 }
428 url = alloc_url;
429 print_str_url = url;
ecc48a90
JD
430 }
431
432 if (opt_snapshot && opt_live_timer) {
433 ERR("Snapshot and live modes are mutually exclusive.");
434 ret = CMD_ERROR;
435 goto error;
436 }
437
16f6f820
DG
438 if (opt_snapshot) {
439 /* No output by default. */
440 const char *snapshot_url = NULL;
441
442 if (opt_url) {
443 snapshot_url = url;
444 } else if (!opt_data_url && !opt_ctrl_url) {
445 /* This is the session path that we need to use as output. */
446 snapshot_url = url;
447 }
448 ret = lttng_create_session_snapshot(session_name, snapshot_url);
ecc48a90 449 } else if (opt_live_timer) {
8960e9cd
DG
450 const char *pathname;
451
452 if (opt_relayd_path) {
453 pathname = opt_relayd_path;
454 } else {
455 pathname = INSTALL_BIN_PATH "/lttng-relayd";
456 }
2c521c63
DG
457 if (!opt_url && !opt_data_url && !check_relayd() &&
458 spawn_relayd(pathname, 0) < 0) {
8960e9cd
DG
459 goto error;
460 }
ecc48a90 461 ret = lttng_create_session_live(session_name, url, opt_live_timer);
16f6f820 462 } else {
ecc48a90 463 ret = _lttng_create_session_ext(session_name, url, datetime, -1);
16f6f820 464 }
f3ed775e 465 if (ret < 0) {
ae856491 466 /* Don't set ret so lttng can interpret the sessiond error. */
42224349 467 switch (-ret) {
f73fabfd 468 case LTTNG_ERR_EXIST_SESS:
42224349
DG
469 WARN("Session %s already exists", session_name);
470 break;
60e835ca 471 default:
60e835ca 472 break;
42224349 473 }
f3ed775e
DG
474 goto error;
475 }
476
0ab61906 477 if (opt_ctrl_url && opt_data_url) {
16f6f820
DG
478 if (opt_snapshot) {
479 ret = add_snapshot_output(session_name, opt_ctrl_url,
480 opt_data_url);
481 } else {
482 /* Setting up control URI (-C or/and -D opt) */
483 ret = set_consumer_url(session_name, opt_ctrl_url, opt_data_url);
484 }
a4b92340 485 if (ret < 0) {
4f50c803
DG
486 /* Destroy created session because the URL are not valid. */
487 lttng_destroy_session(session_name);
a4b92340
DG
488 goto error;
489 }
4f50c803
DG
490 }
491
d7ba1388
MD
492 if (opt_shm_path) {
493 ret = snprintf(shm_path, sizeof(shm_path),
494 "%s/%s", opt_shm_path, session_name_date);
495 if (ret < 0) {
496 PERROR("snprintf shm_path");
497 goto error;
498 }
499
500 ret = lttng_set_session_shm_path(session_name, shm_path);
501 if (ret < 0) {
502 lttng_destroy_session(session_name);
503 goto error;
504 }
505 }
506
4f50c803 507 MSG("Session %s created.", session_name);
16f6f820 508 if (print_str_url && !opt_snapshot) {
4f50c803 509 MSG("Traces will be written in %s", print_str_url);
d73c5802
DG
510
511 if (opt_live_timer) {
512 MSG("Live timer set to %u usec", opt_live_timer);
513 }
16f6f820
DG
514 } else if (opt_snapshot) {
515 if (print_str_url) {
516 MSG("Default snapshot output set to: %s", print_str_url);
517 }
518 MSG("Snapshot mode set. Every channel enabled for that session will "
1daafe0d 519 "be set in overwrite mode and mmap output.");
a4b92340 520 }
d7ba1388
MD
521 if (opt_shm_path) {
522 MSG("Session %s set to shm_path: %s.", session_name,
523 shm_path);
524 }
a4b92340 525
37d03ff7
JRJ
526 /* Mi output */
527 if (lttng_opt_mi) {
528 ret = mi_created_session(session_name);
529 if (ret) {
530 ret = CMD_ERROR;
531 goto error;
532 }
533 }
534
58a97671
DG
535 /* Init lttng session config */
536 ret = config_init(session_name);
f3ed775e 537 if (ret < 0) {
27089920 538 ret = CMD_ERROR;
f3ed775e
DG
539 goto error;
540 }
541
f3ed775e
DG
542 ret = CMD_SUCCESS;
543
544error:
0e428499
DG
545 free(alloc_url);
546 free(traces_path);
87f222b6 547 free(alloc_path);
a4b92340
DG
548
549 if (ret < 0) {
550 ERR("%s", lttng_strerror(ret));
551 }
f3ed775e
DG
552 return ret;
553}
554
92360082
JG
555/*
556 * spawn_sessiond
557 *
558 * Spawn a session daemon by forking and execv.
559 */
560static int spawn_sessiond(char *pathname)
561{
562 int ret = 0;
563 pid_t pid;
564
565 MSG("Spawning a session daemon");
92360082
JG
566 pid = fork();
567 if (pid == 0) {
568 /*
bbd44cae 569 * Spawn session daemon in daemon mode.
92360082 570 */
bbd44cae
PP
571 execlp(pathname, "lttng-sessiond",
572 "--daemonize", NULL);
92360082
JG
573 /* execlp only returns if error happened */
574 if (errno == ENOENT) {
575 ERR("No session daemon found. Use --sessiond-path.");
576 } else {
577 PERROR("execlp");
578 }
579 kill(getppid(), SIGTERM); /* wake parent */
580 exit(EXIT_FAILURE);
581 } else if (pid > 0) {
bbd44cae
PP
582 int status;
583
92360082 584 /*
bbd44cae
PP
585 * In daemon mode (--daemonize), sessiond only exits when
586 * it's ready to accept commands.
92360082 587 */
bbd44cae
PP
588 for (;;) {
589 waitpid(pid, &status, 0);
590
591 if (WIFSIGNALED(status)) {
592 ERR("Session daemon was killed by signal %d",
593 WTERMSIG(status));
594 ret = -1;
595 goto end;
596 } else if (WIFEXITED(status)) {
597 DBG("Session daemon terminated normally (exit status: %d)",
598 WEXITSTATUS(status));
599
600 if (WEXITSTATUS(status) != 0) {
601 ERR("Session daemon terminated with an error (exit status: %d)",
602 WEXITSTATUS(status));
603 ret = -1;
604 goto end;
605 }
606 break;
607 }
92360082 608 }
bbd44cae 609
92360082
JG
610 goto end;
611 } else {
612 PERROR("fork");
613 ret = -1;
614 goto end;
615 }
616
617end:
618 return ret;
619}
620
621/*
622 * launch_sessiond
623 *
624 * Check if the session daemon is available using
625 * the liblttngctl API for the check. If not, try to
626 * spawn a daemon.
627 */
628static int launch_sessiond(void)
629{
630 int ret;
631 char *pathname = NULL;
632
633 ret = lttng_session_daemon_alive();
634 if (ret) {
635 /* Sessiond is alive, not an error */
636 ret = 0;
637 goto end;
638 }
639
640 /* Try command line option path */
641 pathname = opt_sessiond_path;
642
643 /* Try LTTNG_SESSIOND_PATH env variable */
644 if (pathname == NULL) {
645 pathname = getenv(DEFAULT_SESSIOND_PATH_ENV);
646 }
647
648 /* Try with configured path */
649 if (pathname == NULL) {
650 if (CONFIG_SESSIOND_BIN[0] != '\0') {
651 pathname = CONFIG_SESSIOND_BIN;
652 }
653 }
654
655 /* Try the default path */
656 if (pathname == NULL) {
657 pathname = INSTALL_BIN_PATH "/lttng-sessiond";
658 }
659
660 DBG("Session daemon binary path: %s", pathname);
661
662 /* Check existence and permissions */
663 ret = access(pathname, F_OK | X_OK);
664 if (ret < 0) {
665 ERR("No such file or access denied: %s", pathname);
666 goto end;
667 }
668
669 ret = spawn_sessiond(pathname);
670 if (ret < 0) {
671 ERR("Problem occurred when starting %s", pathname);
672 }
673end:
674 return ret;
675}
676
f3ed775e 677/*
74cc1d0f 678 * The 'create <options>' first level command
1c8d13c8
TD
679 *
680 * Returns one of the CMD_* result constants.
f3ed775e
DG
681 */
682int cmd_create(int argc, const char **argv)
683{
37d03ff7 684 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
ecc48a90 685 char *opt_arg = NULL;
f3ed775e
DG
686 static poptContext pc;
687
688 pc = poptGetContext(NULL, argc, argv, long_options, 0);
689 poptReadDefaultConfig(pc, 0);
690
691 while ((opt = poptGetNextOpt(pc)) != -1) {
692 switch (opt) {
693 case OPT_HELP:
27089920 694 usage(stdout);
f3ed775e 695 goto end;
679b4943
SM
696 case OPT_LIST_OPTIONS:
697 list_cmd_options(stdout, long_options);
679b4943 698 goto end;
ecc48a90
JD
699 case OPT_LIVE_TIMER:
700 {
701 unsigned long v;
702
703 errno = 0;
704 opt_arg = poptGetOptArg(pc);
d73c5802
DG
705 if (!opt_arg) {
706 /* Set up default values. */
707 opt_live_timer = (uint32_t) DEFAULT_LTTNG_LIVE_TIMER;
708 DBG("Session live timer interval set to default value %d",
709 opt_live_timer);
710 break;
711 }
712
ecc48a90
JD
713 v = strtoul(opt_arg, NULL, 0);
714 if (errno != 0 || !isdigit(opt_arg[0])) {
715 ERR("Wrong value in --live parameter: %s", opt_arg);
716 ret = CMD_ERROR;
717 goto end;
718 }
719 if (v != (uint32_t) v) {
720 ERR("32-bit overflow in --live parameter: %s", opt_arg);
721 ret = CMD_ERROR;
722 goto end;
723 }
0ed9e0be
JG
724 if (v == 0) {
725 ERR("Live timer interval must be greater than zero");
726 ret = CMD_ERROR;
727 goto end;
728 }
ecc48a90
JD
729 opt_live_timer = (uint32_t) v;
730 DBG("Session live timer interval set to %d", opt_live_timer);
731 break;
732 }
f3ed775e
DG
733 default:
734 usage(stderr);
735 ret = CMD_UNDEFINED;
736 goto end;
737 }
738 }
739
785d2d0d 740 if (opt_no_consumer) {
96fe6b8d 741 MSG("The option --no-consumer is obsolete. Use --no-output now.");
785d2d0d
DG
742 ret = CMD_WARNING;
743 goto end;
744 }
745
92360082
JG
746 /* Spawn a session daemon if needed */
747 if (!opt_no_sessiond) {
748 ret = launch_sessiond();
749 if (ret) {
750 ret = CMD_ERROR;
751 goto end;
752 }
753 }
754
acc09215 755 /* MI initialization */
37d03ff7
JRJ
756 if (lttng_opt_mi) {
757 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
758 if (!writer) {
759 ret = -LTTNG_ERR_NOMEM;
760 goto end;
761 }
762
763 /* Open command element */
764 ret = mi_lttng_writer_command_open(writer,
765 mi_lttng_element_command_create);
766 if (ret) {
767 ret = CMD_ERROR;
768 goto end;
769 }
770
771 /* Open output element */
772 ret = mi_lttng_writer_open_element(writer,
773 mi_lttng_element_command_output);
774 if (ret) {
775 ret = CMD_ERROR;
776 goto end;
777 }
778 }
f3ed775e
DG
779 opt_session_name = (char*) poptGetArg(pc);
780
37d03ff7
JRJ
781 command_ret = create_session();
782 if (command_ret) {
783 success = 0;
784 }
785
786 if (lttng_opt_mi) {
787 /* Close output element */
788 ret = mi_lttng_writer_close_element(writer);
789 if (ret) {
790 ret = CMD_ERROR;
791 goto end;
792 }
793
794 /* Success ? */
795 ret = mi_lttng_writer_write_element_bool(writer,
796 mi_lttng_element_command_success, success);
797 if (ret) {
798 ret = CMD_ERROR;
799 goto end;
800 }
801
802 /* Command element close */
803 ret = mi_lttng_writer_command_close(writer);
804 if (ret) {
805 ret = CMD_ERROR;
806 goto end;
807 }
808 }
f3ed775e
DG
809
810end:
37d03ff7
JRJ
811 /* Mi clean-up */
812 if (writer && mi_lttng_writer_destroy(writer)) {
813 /* Preserve original error code */
814 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
815 }
816
acc09215 817 /* Overwrite ret if an error occurred in create_session() */
37d03ff7
JRJ
818 ret = command_ret ? command_ret : ret;
819
ca1c3607 820 poptFreeContext(pc);
f3ed775e
DG
821 return ret;
822}
This page took 0.077283 seconds and 4 git commands to generate.