707f5ee7d385df79d82b327b9885724c131d68cd
[lttng-tools.git] / src / bin / lttng / commands / create.c
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 <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 enum {
61 OUTPUT_UNKNOWN = -1,
62 OUTPUT_NONE,
63 OUTPUT_LOCAL,
64 OUTPUT_NET,
65 };
66 enum {
67 SESSION_UNKNOWN = -1,
68 SESSION_NORMAL,
69 SESSION_LIVE,
70 SESSION_SNAPSHOT,
71 };
72
73
74 static struct mi_writer *writer;
75
76 static struct poptOption long_options[] = {
77 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
78 {"help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL},
79 {"output", 'o', POPT_ARG_STRING, &opt_output_path, 0, NULL, NULL},
80 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
81 {"set-url", 'U', POPT_ARG_STRING, &opt_url, 0, 0, 0},
82 {"ctrl-url", 'C', POPT_ARG_STRING, &opt_ctrl_url, 0, 0, 0},
83 {"data-url", 'D', POPT_ARG_STRING, &opt_data_url, 0, 0, 0},
84 {"no-output", 0, POPT_ARG_VAL, &opt_no_output, 1, 0, 0},
85 {"no-consumer", 0, POPT_ARG_VAL, &opt_no_consumer, 1, 0, 0},
86 {"snapshot", 0, POPT_ARG_VAL, &opt_snapshot, 1, 0, 0},
87 {"live", 0, POPT_ARG_INT | POPT_ARGFLAG_OPTIONAL, 0, OPT_LIVE_TIMER, 0, 0},
88 {"shm-path", 0, POPT_ARG_STRING, &opt_shm_path, 0, 0, 0},
89 {0, 0, 0, 0, 0, 0, 0}
90 };
91
92 /*
93 * Please have a look at src/lib/lttng-ctl/lttng-ctl.c for more information on
94 * why this declaration exists and used ONLY in for this command.
95 */
96 extern int _lttng_create_session_ext(const char *name, const char *url,
97 const char *datetime);
98
99 /*
100 * Retrieve the created session and mi output it based on provided argument
101 * This is currently a summary of what was pretty printed and is subject to
102 * enhancements.
103 */
104 static int mi_created_session(const char *session_name)
105 {
106 int ret, i, count, found;
107 struct lttng_session *sessions;
108
109 /* session_name should not be null */
110 assert(session_name);
111 assert(writer);
112
113 count = lttng_list_sessions(&sessions);
114 if (count < 0) {
115 ret = count;
116 ERR("%s", lttng_strerror(ret));
117 goto error;
118 }
119
120 if (count == 0) {
121 ERR("Error session creation failed: session %s not found", session_name);
122 ret = -LTTNG_ERR_SESS_NOT_FOUND;
123 goto end;
124 }
125
126 found = 0;
127 for (i = 0; i < count; i++) {
128 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
129 found = 1;
130 ret = mi_lttng_session(writer, &sessions[i], 0);
131 if (ret) {
132 goto error;
133 }
134 break;
135 }
136 }
137
138 if (!found) {
139 ret = -LTTNG_ERR_SESS_NOT_FOUND;
140 } else {
141 ret = CMD_SUCCESS;
142 }
143
144 error:
145 free(sessions);
146 end:
147 return ret;
148 }
149
150 /*
151 * For a session name, set the consumer URLs.
152 */
153 static int set_consumer_url(const char *session_name, const char *ctrl_url,
154 const char *data_url)
155 {
156 int ret;
157 struct lttng_handle *handle;
158
159 assert(session_name);
160
161 /*
162 * Set handle with the session_name, but no domain. This implies that
163 * the actions taken with this handle apply on the tracing session
164 * rather then the domain-specific session.
165 */
166 handle = lttng_create_handle(session_name, NULL);
167 if (handle == NULL) {
168 ret = CMD_FATAL;
169 goto error;
170 }
171
172 ret = lttng_set_consumer_url(handle, ctrl_url, data_url);
173 if (ret < 0) {
174 goto error;
175 }
176
177 error:
178 lttng_destroy_handle(handle);
179 return ret;
180 }
181
182 static int add_snapshot_output(const char *session_name, const char *ctrl_url,
183 const char *data_url)
184 {
185 int ret;
186 struct lttng_snapshot_output *output = NULL;
187
188 assert(session_name);
189
190 output = lttng_snapshot_output_create();
191 if (!output) {
192 ret = CMD_FATAL;
193 goto error_create;
194 }
195
196 if (ctrl_url) {
197 ret = lttng_snapshot_output_set_ctrl_url(ctrl_url, output);
198 if (ret < 0) {
199 goto error;
200 }
201 }
202
203 if (data_url) {
204 ret = lttng_snapshot_output_set_data_url(data_url, output);
205 if (ret < 0) {
206 goto error;
207 }
208 }
209
210 /* This call, if successful, populates the id of the output object. */
211 ret = lttng_snapshot_add_output(session_name, output);
212 if (ret < 0) {
213 goto error;
214 }
215
216 error:
217 lttng_snapshot_output_destroy(output);
218 error_create:
219 return ret;
220 }
221
222 /*
223 * Validate the combinations of passed options
224 *
225 * CMD_ERROR on error
226 * CMD_SUCCESS on success
227 */
228 static int validate_command_options(void)
229 {
230 int ret = CMD_SUCCESS;
231 if (opt_snapshot && opt_live_timer) {
232 ERR("Snapshot and live modes are mutually exclusive.");
233 ret = CMD_ERROR;
234 goto error;
235 }
236
237 if ((!opt_ctrl_url && opt_data_url) || (opt_ctrl_url && !opt_data_url)) {
238 ERR("You need both control and data URL.");
239 ret = CMD_ERROR;
240 goto error;
241 }
242
243 error:
244 return ret;
245 }
246
247 /*
248 * Create a session via direct calls to liblttng-ctl.
249 *
250 * Return CMD_SUCCESS on success, negative value on internal lttng errors and positive
251 * value on command errors.
252 */
253 static int create_session_basic (const char *session_name,
254 int session_type,
255 int live_timer,
256 int output_type,
257 const char* url,
258 const char* ctrl_url,
259 const char* data_url,
260 const char* shm_path,
261 const char* datetime)
262 {
263 /* Create session based on session creation */
264 int ret = CMD_SUCCESS;
265 const char *pathname;
266
267 assert(datetime);
268
269 if (opt_relayd_path) {
270 pathname = opt_relayd_path;
271 } else {
272 pathname = INSTALL_BIN_PATH "/lttng-relayd";
273 }
274
275 switch (session_type) {
276 case SESSION_NORMAL:
277 ret = _lttng_create_session_ext(session_name, url, datetime);
278 break;
279 case SESSION_SNAPSHOT:
280 if (output_type == OUTPUT_NONE) {
281 ERR("--no-output on a snapshot session is invalid");
282 ret = CMD_UNSUPPORTED;
283 goto error;
284 }
285 ret = lttng_create_session_snapshot(session_name, url);
286 break;
287 case SESSION_LIVE:
288 if (output_type == OUTPUT_NONE) {
289 ERR("--no-output on a live session is invalid");
290 ret = CMD_UNSUPPORTED;
291 goto error;
292 }
293
294 if (output_type == OUTPUT_LOCAL) {
295 ERR("Local file output on a live session is invalid");
296 ret = CMD_UNSUPPORTED;
297 goto error;
298 }
299 if (output_type != OUTPUT_NET && !check_relayd() &&
300 spawn_relayd(pathname, 0) < 0) {
301 ret = CMD_FATAL;
302 goto error;
303 }
304 ret = lttng_create_session_live(session_name, url, live_timer);
305 break;
306 default:
307 ERR("Unknown session type");
308 ret = CMD_UNDEFINED;
309 goto error;
310 }
311
312 if (ret < 0) {
313 /* Don't set ret so the sessiond error is propagated. */
314 switch (-ret) {
315 case LTTNG_ERR_EXIST_SESS:
316 WARN("Session %s already exists", session_name);
317 break;
318 default:
319 break;
320 }
321 goto error;
322 }
323
324 /* Configure the session based on the output type */
325 switch (output_type) {
326 case OUTPUT_LOCAL:
327 break;
328 case OUTPUT_NET:
329 if (session_type == SESSION_SNAPSHOT) {
330 ret = add_snapshot_output(session_name, ctrl_url,
331 data_url);
332 } else if (ctrl_url && data_url) {
333 /*
334 * Normal sessions and live sessions behave the same way
335 * regarding consumer url.
336 */
337 ret = set_consumer_url(session_name, ctrl_url, data_url);
338 }
339 if (ret < 0) {
340 /* Destroy created session on errors */
341 lttng_destroy_session(session_name);
342 goto error;
343 }
344 break;
345 case OUTPUT_NONE:
346 break;
347 default:
348 ERR("Unknown output type");
349 ret = CMD_UNDEFINED;
350 goto error;
351 }
352
353 /*
354 * Set the session shared memory path
355 */
356 if (shm_path) {
357 ret = lttng_set_session_shm_path(session_name, shm_path);
358 if (ret < 0) {
359 lttng_destroy_session(session_name);
360 goto error;
361 }
362 }
363 error:
364 return ret;
365 }
366
367 static int generate_output(const char *session_name,
368 int session_type,
369 int live_timer,
370 int output_type,
371 const char* url,
372 const char* ctrl_url,
373 const char* data_url,
374 const char* shm_path)
375 {
376 int ret = CMD_SUCCESS;
377
378 /*
379 * TODO move this to after session name
380 * for now we only emulate previous behaviour.
381 */
382 if (session_type != SESSION_SNAPSHOT) {
383 if (ctrl_url) {
384 MSG("Control URL %s set for session %s", ctrl_url, session_name);
385 }
386
387 if (data_url) {
388 MSG("Data URL %s set for session %s", data_url, session_name);
389 }
390 }
391
392 if (url && output_type == OUTPUT_LOCAL) {
393 /* Remove the file:// */
394 if (strlen(url) > strlen("file://")){
395 url = url + strlen("file://");
396 }
397 }
398
399 MSG("Session %s created.", session_name);
400 if (url && session_type != SESSION_SNAPSHOT) {
401 MSG("Traces will be written in %s", url);
402
403 if (live_timer) {
404 MSG("Live timer set to %u usec", live_timer);
405 }
406 } else if (session_type == SESSION_SNAPSHOT) {
407 if (url) {
408 MSG("Default snapshot output set to: %s", url);
409 }
410 MSG("Snapshot mode set. Every channel enabled for that session will "
411 "be set to mmap output, and default to overwrite mode.");
412 }
413
414 if (shm_path) {
415 MSG("Session %s set to shm_path: %s.", session_name,
416 shm_path);
417 }
418
419 /* Mi output */
420 if (lttng_opt_mi) {
421 ret = mi_created_session(session_name);
422 if (ret) {
423 ret = CMD_ERROR;
424 goto error;
425 }
426 }
427 error:
428 return ret;
429 }
430
431 /*
432 * Create a tracing session.
433 * If no name is specified, a default name is generated.
434 *
435 * Returns one of the CMD_* result constants.
436 */
437 static int create_session(void)
438 {
439 int ret;
440
441 /* Base data */
442 int base_session_type = SESSION_UNKNOWN;
443 int base_output_type = OUTPUT_UNKNOWN;
444 char *base_session_name = NULL;
445 char *base_url = NULL;
446 char *base_ctrl_url = NULL;
447 char *base_data_url = NULL;
448 char *base_shm_path = NULL;
449 int base_live_timer = 0;
450
451 /* Time data */
452 char datetime[16];
453 time_t rawtime;
454 struct tm *timeinfo = NULL;
455
456 /* Temporary variables */
457 char *traces_path = NULL;
458 char *temp_url = NULL;
459 char *session_name_date = NULL;
460 char *tmp_url = NULL;
461 char *tmp_home_path = NULL;
462 struct lttng_uri *uris = NULL;
463 ssize_t uri_array_size = 0;
464
465 /* Option validation */
466 if (validate_command_options() != CMD_SUCCESS) {
467 ret = CMD_ERROR;
468 goto error;
469 }
470
471 /* Get date and time for automatic session name/path */
472 time(&rawtime);
473 timeinfo = localtime(&rawtime);
474 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
475
476 /* Find the session type based on options */
477 if(base_session_type == SESSION_UNKNOWN) {
478 if (opt_snapshot) {
479 base_session_type = SESSION_SNAPSHOT;
480 } else if (opt_live_timer) {
481 base_session_type = SESSION_LIVE;
482 } else {
483 base_session_type = SESSION_NORMAL;
484 }
485 }
486
487 /*
488 * Session name handling
489 */
490 if (opt_session_name) {
491 /* Override the session name */
492 if (strlen(opt_session_name) > NAME_MAX) {
493 ERR("Session name too long. Length must be lower or equal to %d",
494 NAME_MAX);
495 ret = LTTNG_ERR_SESSION_FAIL;
496 free(session_name_date);
497 goto error;
498 }
499 /*
500 * Check if the session name begins with "auto-" or is exactly "auto".
501 * Both are reserved for the default session name. See bug #449 to
502 * understand why we need to check both here.
503 */
504 if ((strncmp(opt_session_name, DEFAULT_SESSION_NAME "-",
505 strlen(DEFAULT_SESSION_NAME) + 1) == 0) ||
506 (strncmp(opt_session_name, DEFAULT_SESSION_NAME,
507 strlen(DEFAULT_SESSION_NAME)) == 0 &&
508 strlen(opt_session_name) == strlen(DEFAULT_SESSION_NAME))) {
509 ERR("%s is a reserved keyword for default session(s)",
510 DEFAULT_SESSION_NAME);
511
512 ret = CMD_ERROR;
513 goto error;
514 }
515
516 base_session_name = strndup(opt_session_name, NAME_MAX);
517 if (!base_session_name) {
518 PERROR("Strdup session name");
519 ret = CMD_ERROR;
520 goto error;
521 }
522
523 ret = asprintf(&session_name_date, "%s-%s", base_session_name, datetime);
524 if (ret < 0) {
525 PERROR("Asprintf session name");
526 goto error;
527 }
528 DBG("Session name from command option set to %s", base_session_name);
529 } else if (base_session_name) {
530 ret = asprintf(&session_name_date, "%s-%s", base_session_name, datetime);
531 if (ret < 0) {
532 PERROR("Asprintf session name");
533 goto error;
534 }
535 } else {
536 /* Generate a name */
537 /* TODO: use asprint */
538 ret = asprintf(&base_session_name, DEFAULT_SESSION_NAME "-%s", datetime);
539 if (ret < 0) {
540 PERROR("Asprintf generated session name");
541 goto error;
542 }
543 session_name_date = strdup(base_session_name);
544 DBG("Auto session name set to %s", base_session_name);
545 }
546
547
548 /*
549 * Output handling
550 */
551
552 /*
553 * If any of those options are present clear all output related data.
554 */
555 if (opt_output_path || opt_url || (opt_ctrl_url && opt_data_url) || opt_no_output) {
556 /* Overwrite output */
557 free(base_url);
558 free(base_ctrl_url);
559 free(base_data_url);
560 base_url = NULL;
561 base_ctrl_url = NULL;
562 base_data_url = NULL;
563 }
564
565 if (opt_output_path) {
566
567 traces_path = utils_expand_path(opt_output_path);
568 if (!traces_path) {
569 ret = CMD_ERROR;
570 goto error;
571 }
572
573 /* Create URL string from the local file system path */
574 ret = asprintf(&temp_url, "file://%s", traces_path);
575 if (ret < 0) {
576 PERROR("asprintf url path");
577 ret = CMD_FATAL;
578 goto error;
579 }
580
581 base_url = temp_url;
582 } else if (opt_url) { /* Handling URL (-U opt) */
583 base_url = strdup(opt_url);
584 } else if (opt_data_url && opt_ctrl_url) {
585 /*
586 * With both control and data, we'll be setting the consumer URL
587 * after session creation thus use no URL.
588 */
589 base_ctrl_url = strdup(opt_ctrl_url);
590 base_data_url = strdup(opt_data_url);
591 } else if (!(opt_no_output || base_output_type == OUTPUT_NONE ||
592 base_url || base_ctrl_url || base_data_url)) {
593 /* Generate default output depending on the session type */
594 switch (base_session_type) {
595 case SESSION_NORMAL:
596 /* fallthrough */
597 case SESSION_SNAPSHOT:
598 /* Default to a local path */
599 tmp_home_path = utils_get_home_dir();
600 if (tmp_home_path == NULL) {
601 ERR("HOME path not found.\n \
602 Please specify an output path using -o, --output PATH");
603 ret = CMD_FATAL;
604 goto error;
605 }
606
607 ret = asprintf(&tmp_url,
608 "file://%s/" DEFAULT_TRACE_DIR_NAME "/%s",
609 tmp_home_path, session_name_date);
610
611 if (ret < 0) {
612 PERROR("asprintf trace dir name");
613 ret = CMD_FATAL;
614 goto error;
615 }
616
617 base_url = tmp_url ;
618 break;
619 case SESSION_LIVE:
620 /* Default to a net output */
621 ret = asprintf(&tmp_url, "net://127.0.0.1");
622 if (ret < 0) {
623 PERROR("asprintf default live URL");
624 ret = CMD_FATAL;
625 goto error;
626 }
627 base_url = tmp_url ;
628 break;
629 default:
630 ERR("Unknown session type");
631 ret = CMD_FATAL;
632 goto error;
633 }
634 }
635
636 /*
637 * Shared memory path handling
638 */
639 if (opt_shm_path) {
640 ret = asprintf(&base_shm_path, "%s/%s", opt_shm_path, session_name_date);
641 if (ret < 0) {
642 PERROR("asprintf shm_path");
643 goto error;
644 }
645 }
646
647 /*
648 * Live timer handling
649 */
650 if (opt_live_timer) {
651 base_live_timer = opt_live_timer;
652 }
653
654 /* Get output type from urls */
655 if (base_url) {
656 /* Get lttng uris from single url */
657 uri_array_size = uri_parse_str_urls(base_url, NULL, &uris);
658 if (uri_array_size < 0) {
659 ret = CMD_ERROR;
660 goto error;
661 }
662 } else if (base_ctrl_url && base_data_url) {
663 uri_array_size = uri_parse_str_urls(base_ctrl_url, base_data_url, &uris);
664 if (uri_array_size < 0) {
665 ret = CMD_ERROR;
666 goto error;
667 }
668 } else {
669 /* --no-output */
670 uri_array_size = 0;
671 }
672
673 switch (uri_array_size) {
674 case 0:
675 base_output_type = OUTPUT_NONE;
676 break;
677 case 1:
678 base_output_type = OUTPUT_LOCAL;
679 break;
680 case 2:
681 base_output_type = OUTPUT_NET;
682 break;
683 default:
684 ret = CMD_ERROR;
685 goto error;
686 }
687
688 ret = create_session_basic (base_session_name,
689 base_session_type,
690 base_live_timer,
691 base_output_type,
692 base_url,
693 base_ctrl_url,
694 base_data_url,
695 base_shm_path,
696 datetime);
697 if (ret) {
698 goto error;
699 }
700
701 ret = generate_output (base_session_name,
702 base_session_type,
703 base_live_timer,
704 base_output_type,
705 base_url,
706 base_ctrl_url,
707 base_data_url,
708 base_shm_path);
709 if (ret) {
710 goto error;
711 }
712
713 /* Init lttng session config */
714 ret = config_init(base_session_name);
715 if (ret < 0) {
716 ret = CMD_ERROR;
717 goto error;
718 }
719
720 ret = CMD_SUCCESS;
721
722 error:
723
724 /* Session temp stuff */
725 free(session_name_date);
726
727 free(uris);
728
729 if (ret < 0) {
730 ERR("%s", lttng_strerror(ret));
731 }
732 free(base_session_name);
733 free(base_url);
734 free(base_ctrl_url);
735 free(base_data_url);
736 free(base_shm_path);
737 return ret;
738 }
739
740 /*
741 * spawn_sessiond
742 *
743 * Spawn a session daemon by forking and execv.
744 */
745 static int spawn_sessiond(char *pathname)
746 {
747 int ret = 0;
748 pid_t pid;
749
750 MSG("Spawning a session daemon");
751 pid = fork();
752 if (pid == 0) {
753 /*
754 * Spawn session daemon in daemon mode.
755 */
756 execlp(pathname, "lttng-sessiond",
757 "--daemonize", NULL);
758 /* execlp only returns if error happened */
759 if (errno == ENOENT) {
760 ERR("No session daemon found. Use --sessiond-path.");
761 } else {
762 PERROR("execlp");
763 }
764 kill(getppid(), SIGTERM); /* wake parent */
765 exit(EXIT_FAILURE);
766 } else if (pid > 0) {
767 /*
768 * In daemon mode (--daemonize), sessiond only exits when
769 * it's ready to accept commands.
770 */
771 for (;;) {
772 int status;
773 pid_t wait_pid_ret = waitpid(pid, &status, 0);
774
775 if (wait_pid_ret < 0) {
776 if (errno == EINTR) {
777 continue;
778 }
779 PERROR("waitpid");
780 ret = -errno;
781 goto end;
782 }
783
784 if (WIFSIGNALED(status)) {
785 ERR("Session daemon was killed by signal %d",
786 WTERMSIG(status));
787 ret = -1;
788 goto end;
789 } else if (WIFEXITED(status)) {
790 DBG("Session daemon terminated normally (exit status: %d)",
791 WEXITSTATUS(status));
792
793 if (WEXITSTATUS(status) != 0) {
794 ERR("Session daemon terminated with an error (exit status: %d)",
795 WEXITSTATUS(status));
796 ret = -1;
797 goto end;
798 }
799 break;
800 }
801 }
802
803 goto end;
804 } else {
805 PERROR("fork");
806 ret = -1;
807 goto end;
808 }
809
810 end:
811 return ret;
812 }
813
814 /*
815 * launch_sessiond
816 *
817 * Check if the session daemon is available using
818 * the liblttngctl API for the check. If not, try to
819 * spawn a daemon.
820 */
821 static int launch_sessiond(void)
822 {
823 int ret;
824 char *pathname = NULL;
825
826 ret = lttng_session_daemon_alive();
827 if (ret) {
828 /* Sessiond is alive, not an error */
829 ret = 0;
830 goto end;
831 }
832
833 /* Try command line option path */
834 pathname = opt_sessiond_path;
835
836 /* Try LTTNG_SESSIOND_PATH env variable */
837 if (pathname == NULL) {
838 pathname = getenv(DEFAULT_SESSIOND_PATH_ENV);
839 }
840
841 /* Try with configured path */
842 if (pathname == NULL) {
843 if (CONFIG_SESSIOND_BIN[0] != '\0') {
844 pathname = CONFIG_SESSIOND_BIN;
845 }
846 }
847
848 /* Try the default path */
849 if (pathname == NULL) {
850 pathname = INSTALL_BIN_PATH "/lttng-sessiond";
851 }
852
853 DBG("Session daemon binary path: %s", pathname);
854
855 /* Check existence and permissions */
856 ret = access(pathname, F_OK | X_OK);
857 if (ret < 0) {
858 ERR("No such file or access denied: %s", pathname);
859 goto end;
860 }
861
862 ret = spawn_sessiond(pathname);
863 end:
864 if (ret) {
865 ERR("Problem occurred while launching session daemon (%s)",
866 pathname);
867 }
868 return ret;
869 }
870
871 /*
872 * The 'create <options>' first level command
873 *
874 * Returns one of the CMD_* result constants.
875 */
876 int cmd_create(int argc, const char **argv)
877 {
878 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
879 char *opt_arg = NULL;
880 static poptContext pc;
881
882 pc = poptGetContext(NULL, argc, argv, long_options, 0);
883 poptReadDefaultConfig(pc, 0);
884
885 while ((opt = poptGetNextOpt(pc)) != -1) {
886 switch (opt) {
887 case OPT_HELP:
888 SHOW_HELP();
889 goto end;
890 case OPT_LIST_OPTIONS:
891 list_cmd_options(stdout, long_options);
892 goto end;
893 case OPT_LIVE_TIMER:
894 {
895 unsigned long v;
896
897 errno = 0;
898 opt_arg = poptGetOptArg(pc);
899 if (!opt_arg) {
900 /* Set up default values. */
901 opt_live_timer = (uint32_t) DEFAULT_LTTNG_LIVE_TIMER;
902 DBG("Session live timer interval set to default value %d",
903 opt_live_timer);
904 break;
905 }
906
907 v = strtoul(opt_arg, NULL, 0);
908 if (errno != 0 || !isdigit(opt_arg[0])) {
909 ERR("Wrong value in --live parameter: %s", opt_arg);
910 ret = CMD_ERROR;
911 goto end;
912 }
913 if (v != (uint32_t) v) {
914 ERR("32-bit overflow in --live parameter: %s", opt_arg);
915 ret = CMD_ERROR;
916 goto end;
917 }
918 if (v == 0) {
919 ERR("Live timer interval must be greater than zero");
920 ret = CMD_ERROR;
921 goto end;
922 }
923 opt_live_timer = (uint32_t) v;
924 DBG("Session live timer interval set to %d", opt_live_timer);
925 break;
926 }
927 default:
928 ret = CMD_UNDEFINED;
929 goto end;
930 }
931 }
932
933 if (opt_no_consumer) {
934 MSG("The option --no-consumer is obsolete. Use --no-output now.");
935 ret = CMD_WARNING;
936 goto end;
937 }
938
939 /* Spawn a session daemon if needed */
940 if (!opt_no_sessiond) {
941 ret = launch_sessiond();
942 if (ret) {
943 ret = CMD_ERROR;
944 goto end;
945 }
946 }
947
948 /* MI initialization */
949 if (lttng_opt_mi) {
950 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
951 if (!writer) {
952 ret = -LTTNG_ERR_NOMEM;
953 goto end;
954 }
955
956 /* Open command element */
957 ret = mi_lttng_writer_command_open(writer,
958 mi_lttng_element_command_create);
959 if (ret) {
960 ret = CMD_ERROR;
961 goto end;
962 }
963
964 /* Open output element */
965 ret = mi_lttng_writer_open_element(writer,
966 mi_lttng_element_command_output);
967 if (ret) {
968 ret = CMD_ERROR;
969 goto end;
970 }
971 }
972 opt_session_name = (char*) poptGetArg(pc);
973
974 command_ret = create_session();
975
976 if (command_ret) {
977 success = 0;
978 }
979
980 if (lttng_opt_mi) {
981 /* Close output element */
982 ret = mi_lttng_writer_close_element(writer);
983 if (ret) {
984 ret = CMD_ERROR;
985 goto end;
986 }
987
988 /* Success ? */
989 ret = mi_lttng_writer_write_element_bool(writer,
990 mi_lttng_element_command_success, success);
991 if (ret) {
992 ret = CMD_ERROR;
993 goto end;
994 }
995
996 /* Command element close */
997 ret = mi_lttng_writer_command_close(writer);
998 if (ret) {
999 ret = CMD_ERROR;
1000 goto end;
1001 }
1002 }
1003
1004 end:
1005 /* Mi clean-up */
1006 if (writer && mi_lttng_writer_destroy(writer)) {
1007 /* Preserve original error code */
1008 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
1009 }
1010
1011 /* Overwrite ret if an error occurred in create_session() */
1012 ret = command_ret ? command_ret : ret;
1013
1014 poptFreeContext(pc);
1015 return ret;
1016 }
This page took 0.063648 seconds and 4 git commands to generate.