Commit | Line | Data |
---|---|---|
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 | ||
6c1c0768 | 18 | #define _LGPL_SOURCE |
a4b92340 | 19 | #include <assert.h> |
ecc48a90 | 20 | #include <ctype.h> |
f3ed775e DG |
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> | |
92360082 | 29 | #include <signal.h> |
bbd44cae | 30 | #include <sys/wait.h> |
f3ed775e | 31 | |
37d03ff7 | 32 | #include <common/mi-lttng.h> |
4a102b75 | 33 | #include <common/config/session-config.h> |
37d03ff7 | 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 |
44 | static char *opt_output_path; |
45 | static char *opt_session_name; | |
a4b92340 DG |
46 | static char *opt_url; |
47 | static char *opt_ctrl_url; | |
48 | static char *opt_data_url; | |
d7ba1388 | 49 | static char *opt_shm_path; |
4a102b75 | 50 | static char *opt_template_path; |
a4b92340 | 51 | static int opt_no_consumer; |
96fe6b8d | 52 | static int opt_no_output; |
16f6f820 | 53 | static int opt_snapshot; |
ecc48a90 | 54 | static unsigned int opt_live_timer; |
e51a4200 | 55 | static bool opt_default_name; |
f99c53dc | 56 | static bool opt_default_output; |
f3ed775e DG |
57 | |
58 | enum { | |
59 | OPT_HELP = 1, | |
679b4943 | 60 | OPT_LIST_OPTIONS, |
ecc48a90 | 61 | OPT_LIVE_TIMER, |
e51a4200 | 62 | OPT_DEFAULT_NAME, |
f99c53dc | 63 | OPT_DEFAULT_OUTPUT, |
f3ed775e DG |
64 | }; |
65 | ||
05ca94c0 | 66 | enum output_type { |
101fa438 | 67 | OUTPUT_UNKNOWN = -1, |
227bc702 JR |
68 | OUTPUT_NONE, |
69 | OUTPUT_LOCAL, | |
70 | OUTPUT_NET, | |
71 | }; | |
05ca94c0 | 72 | enum session_type { |
101fa438 | 73 | SESSION_UNKNOWN = -1, |
227bc702 JR |
74 | SESSION_NORMAL, |
75 | SESSION_LIVE, | |
76 | SESSION_SNAPSHOT, | |
77 | }; | |
78 | ||
79 | ||
37d03ff7 JRJ |
80 | static struct mi_writer *writer; |
81 | ||
f3ed775e DG |
82 | static struct poptOption long_options[] = { |
83 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
679b4943 SM |
84 | {"help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL}, |
85 | {"output", 'o', POPT_ARG_STRING, &opt_output_path, 0, NULL, NULL}, | |
86 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, | |
23d14dff DG |
87 | {"set-url", 'U', POPT_ARG_STRING, &opt_url, 0, 0, 0}, |
88 | {"ctrl-url", 'C', POPT_ARG_STRING, &opt_ctrl_url, 0, 0, 0}, | |
89 | {"data-url", 'D', POPT_ARG_STRING, &opt_data_url, 0, 0, 0}, | |
96fe6b8d | 90 | {"no-output", 0, POPT_ARG_VAL, &opt_no_output, 1, 0, 0}, |
2bba9e53 | 91 | {"no-consumer", 0, POPT_ARG_VAL, &opt_no_consumer, 1, 0, 0}, |
16f6f820 | 92 | {"snapshot", 0, POPT_ARG_VAL, &opt_snapshot, 1, 0, 0}, |
d73c5802 | 93 | {"live", 0, POPT_ARG_INT | POPT_ARGFLAG_OPTIONAL, 0, OPT_LIVE_TIMER, 0, 0}, |
d7ba1388 | 94 | {"shm-path", 0, POPT_ARG_STRING, &opt_shm_path, 0, 0, 0}, |
4a102b75 | 95 | {"template-path", 0, POPT_ARG_STRING, &opt_template_path, 0, 0, 0}, |
e51a4200 | 96 | {"default-name", 0, POPT_ARG_NONE, NULL, OPT_DEFAULT_NAME, 0, 0}, |
f99c53dc | 97 | {"default-output", 0, POPT_ARG_NONE, NULL, OPT_DEFAULT_OUTPUT, 0, 0}, |
f3ed775e DG |
98 | {0, 0, 0, 0, 0, 0, 0} |
99 | }; | |
100 | ||
16de1a24 DG |
101 | /* |
102 | * Please have a look at src/lib/lttng-ctl/lttng-ctl.c for more information on | |
103 | * why this declaration exists and used ONLY in for this command. | |
104 | */ | |
07424f16 | 105 | extern int _lttng_create_session_ext(const char *name, const char *url, |
d1edd8a8 | 106 | const char *datetime); |
07424f16 | 107 | |
37d03ff7 | 108 | /* |
485ca16f | 109 | * Retrieve the created session and mi output it based on provided argument |
37d03ff7 JRJ |
110 | * This is currently a summary of what was pretty printed and is subject to |
111 | * enhancements. | |
37d03ff7 JRJ |
112 | */ |
113 | static int mi_created_session(const char *session_name) | |
114 | { | |
115 | int ret, i, count, found; | |
116 | struct lttng_session *sessions; | |
117 | ||
118 | /* session_name should not be null */ | |
119 | assert(session_name); | |
120 | assert(writer); | |
121 | ||
122 | count = lttng_list_sessions(&sessions); | |
123 | if (count < 0) { | |
124 | ret = count; | |
125 | ERR("%s", lttng_strerror(ret)); | |
126 | goto error; | |
127 | } | |
128 | ||
129 | if (count == 0) { | |
130 | ERR("Error session creation failed: session %s not found", session_name); | |
131 | ret = -LTTNG_ERR_SESS_NOT_FOUND; | |
132 | goto end; | |
133 | } | |
134 | ||
135 | found = 0; | |
136 | for (i = 0; i < count; i++) { | |
137 | if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) { | |
138 | found = 1; | |
139 | ret = mi_lttng_session(writer, &sessions[i], 0); | |
140 | if (ret) { | |
141 | goto error; | |
142 | } | |
143 | break; | |
144 | } | |
145 | } | |
146 | ||
147 | if (!found) { | |
148 | ret = -LTTNG_ERR_SESS_NOT_FOUND; | |
149 | } else { | |
150 | ret = CMD_SUCCESS; | |
151 | } | |
152 | ||
153 | error: | |
154 | free(sessions); | |
155 | end: | |
156 | return ret; | |
157 | } | |
158 | ||
00e2e675 | 159 | /* |
a4b92340 | 160 | * For a session name, set the consumer URLs. |
00e2e675 | 161 | */ |
a4b92340 DG |
162 | static int set_consumer_url(const char *session_name, const char *ctrl_url, |
163 | const char *data_url) | |
00e2e675 | 164 | { |
a4b92340 DG |
165 | int ret; |
166 | struct lttng_handle *handle; | |
a4b92340 DG |
167 | |
168 | assert(session_name); | |
169 | ||
170 | /* | |
95681498 JG |
171 | * Set handle with the session_name, but no domain. This implies that |
172 | * the actions taken with this handle apply on the tracing session | |
173 | * rather then the domain-specific session. | |
a4b92340 | 174 | */ |
95681498 | 175 | handle = lttng_create_handle(session_name, NULL); |
a4b92340 DG |
176 | if (handle == NULL) { |
177 | ret = CMD_FATAL; | |
178 | goto error; | |
179 | } | |
00e2e675 | 180 | |
a4b92340 DG |
181 | ret = lttng_set_consumer_url(handle, ctrl_url, data_url); |
182 | if (ret < 0) { | |
183 | goto error; | |
00e2e675 DG |
184 | } |
185 | ||
a4b92340 DG |
186 | error: |
187 | lttng_destroy_handle(handle); | |
188 | return ret; | |
189 | } | |
190 | ||
16f6f820 DG |
191 | static int add_snapshot_output(const char *session_name, const char *ctrl_url, |
192 | const char *data_url) | |
193 | { | |
194 | int ret; | |
195 | struct lttng_snapshot_output *output = NULL; | |
196 | ||
197 | assert(session_name); | |
198 | ||
199 | output = lttng_snapshot_output_create(); | |
200 | if (!output) { | |
201 | ret = CMD_FATAL; | |
202 | goto error_create; | |
203 | } | |
204 | ||
205 | if (ctrl_url) { | |
206 | ret = lttng_snapshot_output_set_ctrl_url(ctrl_url, output); | |
207 | if (ret < 0) { | |
208 | goto error; | |
209 | } | |
210 | } | |
211 | ||
212 | if (data_url) { | |
213 | ret = lttng_snapshot_output_set_data_url(data_url, output); | |
214 | if (ret < 0) { | |
215 | goto error; | |
216 | } | |
217 | } | |
218 | ||
219 | /* This call, if successful, populates the id of the output object. */ | |
220 | ret = lttng_snapshot_add_output(session_name, output); | |
221 | if (ret < 0) { | |
222 | goto error; | |
223 | } | |
224 | ||
225 | error: | |
226 | lttng_snapshot_output_destroy(output); | |
227 | error_create: | |
228 | return ret; | |
229 | } | |
230 | ||
227bc702 JR |
231 | /* |
232 | * Validate the combinations of passed options | |
233 | * | |
234 | * CMD_ERROR on error | |
235 | * CMD_SUCCESS on success | |
236 | */ | |
05ca94c0 | 237 | static int validate_command_options(enum session_type type) |
227bc702 JR |
238 | { |
239 | int ret = CMD_SUCCESS; | |
dd4b646b | 240 | |
227bc702 JR |
241 | if (opt_snapshot && opt_live_timer) { |
242 | ERR("Snapshot and live modes are mutually exclusive."); | |
243 | ret = CMD_ERROR; | |
244 | goto error; | |
245 | } | |
246 | ||
247 | if ((!opt_ctrl_url && opt_data_url) || (opt_ctrl_url && !opt_data_url)) { | |
dd4b646b | 248 | ERR("Control and data URLs must both be set."); |
227bc702 JR |
249 | ret = CMD_ERROR; |
250 | goto error; | |
251 | } | |
252 | ||
4a102b75 JR |
253 | if (opt_template_path) { |
254 | /* Restriction on flags exist when using template */ | |
255 | /* Session type flags are not permitted */ | |
256 | /* --live & --snapshot */ | |
05ca94c0 JR |
257 | if ((opt_live_timer && type != SESSION_LIVE) || |
258 | (opt_snapshot && type != SESSION_SNAPSHOT)) { | |
4a102b75 JR |
259 | ERR("It is not possible to change the session type of a template"); |
260 | ret = CMD_ERROR; | |
261 | goto error; | |
262 | } | |
263 | } | |
264 | ||
e51a4200 JG |
265 | if (opt_session_name && opt_default_name) { |
266 | ERR("A session name must not be provided if the --default-name option is used"); | |
267 | ret = CMD_ERROR; | |
268 | goto error; | |
269 | } | |
f99c53dc JG |
270 | |
271 | if (opt_default_output && (opt_data_url || opt_ctrl_url || opt_url || | |
272 | opt_output_path)) { | |
273 | ERR("The --default-output option may not be used with the --output/-o, --set-url/-U, --ctrl-url/-C, or --data-url/-D options"); | |
274 | ret = CMD_ERROR; | |
275 | goto error; | |
276 | } | |
227bc702 JR |
277 | error: |
278 | return ret; | |
279 | } | |
280 | ||
281 | /* | |
d49b7031 | 282 | * Create a session using direct calls to liblttng-ctl. |
227bc702 JR |
283 | * |
284 | * Return CMD_SUCCESS on success, negative value on internal lttng errors and positive | |
285 | * value on command errors. | |
286 | */ | |
287 | static int create_session_basic (const char *session_name, | |
05ca94c0 | 288 | enum session_type session_type, |
227bc702 | 289 | int live_timer, |
05ca94c0 | 290 | enum output_type output_type, |
227bc702 JR |
291 | const char* url, |
292 | const char* ctrl_url, | |
293 | const char* data_url, | |
294 | const char* shm_path, | |
295 | const char* datetime) | |
296 | { | |
297 | /* Create session based on session creation */ | |
298 | int ret = CMD_SUCCESS; | |
299 | const char *pathname; | |
300 | ||
301 | assert(datetime); | |
302 | ||
303 | if (opt_relayd_path) { | |
304 | pathname = opt_relayd_path; | |
305 | } else { | |
306 | pathname = INSTALL_BIN_PATH "/lttng-relayd"; | |
307 | } | |
308 | ||
309 | switch (session_type) { | |
310 | case SESSION_NORMAL: | |
311 | ret = _lttng_create_session_ext(session_name, url, datetime); | |
312 | break; | |
313 | case SESSION_SNAPSHOT: | |
314 | if (output_type == OUTPUT_NONE) { | |
315 | ERR("--no-output on a snapshot session is invalid"); | |
316 | ret = CMD_UNSUPPORTED; | |
317 | goto error; | |
318 | } | |
319 | ret = lttng_create_session_snapshot(session_name, url); | |
320 | break; | |
321 | case SESSION_LIVE: | |
322 | if (output_type == OUTPUT_NONE) { | |
323 | ERR("--no-output on a live session is invalid"); | |
324 | ret = CMD_UNSUPPORTED; | |
325 | goto error; | |
326 | } | |
327 | ||
328 | if (output_type == OUTPUT_LOCAL) { | |
329 | ERR("Local file output on a live session is invalid"); | |
330 | ret = CMD_UNSUPPORTED; | |
331 | goto error; | |
332 | } | |
333 | if (output_type != OUTPUT_NET && !check_relayd() && | |
334 | spawn_relayd(pathname, 0) < 0) { | |
335 | ret = CMD_FATAL; | |
336 | goto error; | |
337 | } | |
338 | ret = lttng_create_session_live(session_name, url, live_timer); | |
339 | break; | |
340 | default: | |
341 | ERR("Unknown session type"); | |
342 | ret = CMD_UNDEFINED; | |
343 | goto error; | |
344 | } | |
345 | ||
346 | if (ret < 0) { | |
8ad05258 | 347 | /* Don't set ret so the sessiond error is propagated. */ |
227bc702 JR |
348 | switch (-ret) { |
349 | case LTTNG_ERR_EXIST_SESS: | |
350 | WARN("Session %s already exists", session_name); | |
351 | break; | |
352 | default: | |
353 | break; | |
354 | } | |
355 | goto error; | |
356 | } | |
357 | ||
358 | /* Configure the session based on the output type */ | |
359 | switch (output_type) { | |
360 | case OUTPUT_LOCAL: | |
361 | break; | |
362 | case OUTPUT_NET: | |
19ed6174 JR |
363 | if (!ctrl_url || !data_url) { |
364 | break; | |
365 | } | |
366 | ||
227bc702 JR |
367 | if (session_type == SESSION_SNAPSHOT) { |
368 | ret = add_snapshot_output(session_name, ctrl_url, | |
369 | data_url); | |
19ed6174 | 370 | } else { |
227bc702 JR |
371 | /* |
372 | * Normal sessions and live sessions behave the same way | |
373 | * regarding consumer url. | |
374 | */ | |
375 | ret = set_consumer_url(session_name, ctrl_url, data_url); | |
376 | } | |
377 | if (ret < 0) { | |
378 | /* Destroy created session on errors */ | |
379 | lttng_destroy_session(session_name); | |
380 | goto error; | |
381 | } | |
382 | break; | |
383 | case OUTPUT_NONE: | |
384 | break; | |
385 | default: | |
386 | ERR("Unknown output type"); | |
387 | ret = CMD_UNDEFINED; | |
388 | goto error; | |
389 | } | |
390 | ||
391 | /* | |
392 | * Set the session shared memory path | |
393 | */ | |
394 | if (shm_path) { | |
395 | ret = lttng_set_session_shm_path(session_name, shm_path); | |
396 | if (ret < 0) { | |
397 | lttng_destroy_session(session_name); | |
398 | goto error; | |
399 | } | |
400 | } | |
401 | error: | |
402 | return ret; | |
403 | } | |
404 | ||
405 | static int generate_output(const char *session_name, | |
05ca94c0 | 406 | enum session_type session_type, |
227bc702 | 407 | int live_timer, |
05ca94c0 | 408 | enum output_type output_type, |
227bc702 JR |
409 | const char* url, |
410 | const char* ctrl_url, | |
411 | const char* data_url, | |
412 | const char* shm_path) | |
413 | { | |
414 | int ret = CMD_SUCCESS; | |
415 | ||
416 | /* | |
417 | * TODO move this to after session name | |
418 | * for now we only emulate previous behaviour. | |
419 | */ | |
420 | if (session_type != SESSION_SNAPSHOT) { | |
421 | if (ctrl_url) { | |
422 | MSG("Control URL %s set for session %s", ctrl_url, session_name); | |
423 | } | |
424 | ||
425 | if (data_url) { | |
426 | MSG("Data URL %s set for session %s", data_url, session_name); | |
427 | } | |
428 | } | |
429 | ||
430 | if (url && output_type == OUTPUT_LOCAL) { | |
431 | /* Remove the file:// */ | |
432 | if (strlen(url) > strlen("file://")){ | |
433 | url = url + strlen("file://"); | |
434 | } | |
435 | } | |
436 | ||
437 | MSG("Session %s created.", session_name); | |
438 | if (url && session_type != SESSION_SNAPSHOT) { | |
439 | MSG("Traces will be written in %s", url); | |
440 | ||
441 | if (live_timer) { | |
442 | MSG("Live timer set to %u usec", live_timer); | |
443 | } | |
444 | } else if (session_type == SESSION_SNAPSHOT) { | |
445 | if (url) { | |
446 | MSG("Default snapshot output set to: %s", url); | |
447 | } | |
448 | MSG("Snapshot mode set. Every channel enabled for that session will " | |
449 | "be set to mmap output, and default to overwrite mode."); | |
450 | } | |
451 | ||
452 | if (shm_path) { | |
453 | MSG("Session %s set to shm_path: %s.", session_name, | |
454 | shm_path); | |
455 | } | |
456 | ||
457 | /* Mi output */ | |
458 | if (lttng_opt_mi) { | |
459 | ret = mi_created_session(session_name); | |
460 | if (ret) { | |
461 | ret = CMD_ERROR; | |
462 | goto error; | |
463 | } | |
464 | } | |
465 | error: | |
466 | return ret; | |
467 | } | |
468 | ||
4a102b75 JR |
469 | static int parse_template (struct config_document *template, |
470 | char **session_name, | |
05ca94c0 | 471 | enum session_type *session_type, |
4a102b75 | 472 | int *live_timer, |
05ca94c0 | 473 | enum output_type *output_type, |
4a102b75 JR |
474 | char **url, |
475 | char **ctrl_url, | |
476 | char **data_url, | |
477 | char **shm_path) | |
478 | { | |
479 | int ret = 0; | |
b37959c7 | 480 | int printed_bytes; |
4a102b75 JR |
481 | char *raw_value = NULL; |
482 | ||
483 | assert(template); | |
484 | ||
485 | /* Session name */ | |
486 | *session_name = config_document_get_element_value(template,"/sessions/session/name"); | |
487 | ||
488 | /* Check the type of session we have in the template */ | |
489 | if(config_document_element_exist(template, "/sessions/session/attributes/snapshot_mode")) { | |
490 | *session_type = SESSION_SNAPSHOT; | |
491 | } else if (config_document_element_exist(template, "/sessions/session/attributes/live_timer_interval")) { | |
492 | *session_type = SESSION_LIVE; | |
493 | raw_value = config_document_get_element_value(template,"/sessions/session/attributes/live_timer_interval"); | |
494 | *live_timer = config_parse_value(raw_value); | |
495 | free(raw_value); | |
496 | raw_value = NULL; | |
497 | } else { | |
498 | *session_type = SESSION_NORMAL; | |
499 | } | |
500 | ||
501 | /* Output */ | |
502 | switch (*session_type) { | |
503 | case SESSION_NORMAL: | |
504 | case SESSION_LIVE: | |
505 | if (!config_document_element_exist(template, "/sessions/session/output/consumer_output/destination")){ | |
506 | break; | |
507 | } | |
508 | if (config_document_element_exist(template, "/sessions/session/output/consumer_output/destination/path")){ | |
509 | raw_value = config_document_get_element_value(template, "/sessions/session/output/consumer_output/destination/path"); | |
510 | if (!raw_value) { | |
511 | ret = -1; | |
512 | goto error; | |
513 | } | |
514 | ||
515 | if (strlen(raw_value) > 0) { | |
516 | *output_type = OUTPUT_LOCAL; | |
b37959c7 JR |
517 | printed_bytes = asprintf(url, "file://%s", raw_value); |
518 | if (printed_bytes < 0) { | |
4a102b75 JR |
519 | ret = -1; |
520 | goto error; | |
521 | } | |
522 | } else { | |
523 | *output_type = OUTPUT_NONE; | |
524 | } | |
525 | ||
526 | free(raw_value); | |
527 | raw_value = NULL; | |
528 | break; | |
529 | } else if(config_document_element_exist(template, "/sessions/session/output/consumer_output/destination/net_output")) { | |
530 | *ctrl_url = config_document_get_element_value(template, "/sessions/session/output/consumer_output/destination/net_output/control_uri"); | |
531 | *data_url = config_document_get_element_value(template, "/sessions/session/output/consumer_output/destination/net_output/data_uri"); | |
532 | if (!*ctrl_url || ! *data_url) { | |
533 | ret = -1; | |
534 | goto error; | |
535 | } | |
536 | *output_type = OUTPUT_NET; | |
537 | } else { | |
538 | /* There is no output definition */ | |
539 | } | |
540 | break; | |
541 | case SESSION_SNAPSHOT: | |
542 | if (!config_document_element_exist(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination")){ | |
543 | break; | |
544 | } | |
545 | if (config_document_element_exist(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination/path")){ | |
546 | raw_value = config_document_get_element_value(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination/path"); | |
547 | if (!raw_value) { | |
548 | ret = -1; | |
549 | goto error; | |
550 | } | |
551 | ||
552 | if (strlen(raw_value) > 0) { | |
553 | *output_type = OUTPUT_LOCAL; | |
b37959c7 JR |
554 | printed_bytes = asprintf(url, "file://%s", raw_value); |
555 | if (printed_bytes < 0) { | |
4a102b75 JR |
556 | ret = -1; |
557 | goto error; | |
558 | } | |
559 | } else { | |
560 | *output_type = OUTPUT_NONE; | |
561 | } | |
562 | ||
563 | free(raw_value); | |
564 | raw_value = NULL; | |
565 | break; | |
566 | } else if(config_document_element_exist(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination/net_output")) { | |
567 | *ctrl_url = config_document_get_element_value(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination/net_output/control_uri"); | |
568 | *data_url = config_document_get_element_value(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination/net_output/data_uri"); | |
569 | if (!*ctrl_url || ! *data_url) { | |
570 | ret = -1; | |
571 | goto error; | |
572 | } | |
573 | *output_type = OUTPUT_NET; | |
574 | } else { | |
575 | /* There is no output definition */ | |
576 | } | |
577 | break; | |
05ca94c0 JR |
578 | case SESSION_UNKNOWN: |
579 | ret = -1; | |
580 | goto error; | |
4a102b75 JR |
581 | } |
582 | ||
583 | ||
584 | /* shared memory path */ | |
585 | *shm_path = config_document_get_element_value(template,"/sessions/session/shared_memory_path"); | |
586 | ||
587 | error: | |
588 | free(raw_value); | |
589 | return ret; | |
590 | ||
591 | } | |
592 | static int create_session_from_template(struct config_document *template, | |
593 | const char *session_name, | |
05ca94c0 | 594 | enum session_type session_type, |
4a102b75 | 595 | int live_timer, |
05ca94c0 | 596 | enum output_type output_type, |
4a102b75 JR |
597 | const char *url, |
598 | const char *ctrl_url, | |
599 | const char *data_url, | |
600 | const char *shm_path, | |
601 | const char *datetime) | |
602 | { | |
603 | int ret = CMD_SUCCESS; | |
b37959c7 | 604 | int printed_bytes; |
4a102b75 JR |
605 | struct config_element *temp_element = NULL; |
606 | struct config_element *temp_element_child = NULL; | |
607 | char tmp_ctrl_uri[PATH_MAX]; | |
608 | char tmp_data_uri[PATH_MAX]; | |
609 | struct lttng_uri *uris = NULL; | |
610 | ssize_t uri_array_size = 0; | |
611 | char *tmp_string = NULL; | |
612 | ||
613 | assert(template); | |
614 | assert(session_name); | |
615 | ||
616 | memset(tmp_ctrl_uri, 0, sizeof(tmp_ctrl_uri)); | |
617 | memset(tmp_data_uri, 0, sizeof(tmp_data_uri)); | |
618 | ||
619 | /* Session name */ | |
620 | if (config_document_element_exist(template, "/sessions/session/name")) { | |
621 | /* Replace the node value */ | |
622 | config_document_replace_element_value(template, "/sessions/session/name", session_name); | |
623 | } else { | |
624 | /* insert the node */ | |
625 | temp_element = config_element_create("name", session_name); | |
626 | if (!temp_element) { | |
627 | ERR("Could not create session name node configuration"); | |
628 | ret = CMD_ERROR; | |
629 | goto error; | |
630 | } | |
631 | ret = config_document_insert_element(template, "/sessions/session", temp_element); | |
632 | if (ret) { | |
633 | ERR("Could not insert session name node configuration"); | |
634 | ret = CMD_ERROR; | |
635 | goto error; | |
636 | } | |
637 | config_element_free(temp_element); | |
638 | } | |
639 | ||
640 | /* | |
641 | * Live timer | |
642 | */ | |
643 | if (session_type == SESSION_LIVE) { | |
644 | if (config_document_element_exist(template, "/sessions/session/attributes/live_timer_interval")) { | |
b37959c7 JR |
645 | printed_bytes = asprintf(&tmp_string, "%d", live_timer); |
646 | if (printed_bytes < 0) { | |
af7f3c88 JR |
647 | ERR("Asprintf failed for live timer"); |
648 | ret = CMD_ERROR; | |
649 | goto error; | |
650 | } | |
651 | ret = config_document_replace_element_value(template, "/sessions/session/attributes/live_timer_interval", tmp_string); | |
652 | ||
653 | if (ret) { | |
654 | printf("error: %d\n", ret); | |
655 | ERR("Replacement of live_timer_interval failed"); | |
656 | ret = CMD_ERROR; | |
657 | goto error; | |
658 | } | |
659 | ||
4a102b75 JR |
660 | free(tmp_string); |
661 | tmp_string = NULL; | |
662 | } else { | |
663 | ERR("Invalid live timer template. Missing live timer node"); | |
664 | ret = CMD_ERROR; | |
665 | goto error; | |
666 | } | |
667 | ||
668 | } | |
669 | ||
670 | /* | |
671 | * Generate the output node | |
672 | */ | |
673 | ||
674 | /* Get output from urls */ | |
675 | if (url) { | |
676 | /* Get lttng uris from single url */ | |
677 | uri_array_size = uri_parse_str_urls(url, NULL, &uris); | |
678 | if (uri_array_size < 0) { | |
679 | ret = CMD_ERROR; | |
680 | goto error; | |
681 | } | |
682 | } else if (ctrl_url && data_url) { | |
683 | uri_array_size = uri_parse_str_urls(ctrl_url, data_url, &uris); | |
684 | if (uri_array_size < 0) { | |
685 | ret = CMD_ERROR; | |
686 | goto error; | |
687 | } | |
688 | } else { | |
689 | /* --no-output */ | |
690 | uri_array_size = 0; | |
691 | } | |
692 | ||
693 | /* Validate if the session output type still match the passed data */ | |
694 | if ( (uri_array_size == 0 && output_type != OUTPUT_NONE) || | |
695 | (uri_array_size == 1 && output_type != OUTPUT_LOCAL) || | |
696 | (uri_array_size == 2 && output_type != OUTPUT_NET)) { | |
697 | ERR("Overwriting value for output do not match the base output type"); | |
698 | ret = CMD_ERROR; | |
699 | goto error; | |
700 | } | |
701 | ||
702 | switch (output_type) { | |
703 | case OUTPUT_NONE: | |
704 | temp_element_child = config_element_create("path", ""); | |
705 | if (!temp_element_child) { | |
706 | ERR("Could not create empty path node configuration"); | |
707 | ret = CMD_ERROR; | |
708 | goto error; | |
709 | } | |
710 | break; | |
711 | case OUTPUT_LOCAL: | |
712 | temp_element_child = config_element_create("path", uris[0].dst.path); | |
713 | if (!temp_element_child) { | |
714 | ERR("Could not create local path node configuration"); | |
715 | ret = CMD_ERROR; | |
716 | goto error; | |
717 | } | |
718 | break; | |
719 | case OUTPUT_NET: | |
720 | uri_to_str_url(&uris[0], tmp_ctrl_uri, sizeof(tmp_ctrl_uri)); | |
721 | uri_to_str_url(&uris[1], tmp_data_uri, sizeof(tmp_data_uri)); | |
722 | ||
723 | temp_element_child = config_element_create("net_output", NULL); | |
724 | if (!temp_element_child) { | |
725 | ERR("Could not create net_output node configuration"); | |
726 | ret = CMD_ERROR; | |
727 | goto error; | |
728 | } | |
729 | ||
730 | temp_element = config_element_create("control_uri", tmp_ctrl_uri); | |
731 | if (!temp_element_child) { | |
732 | ERR("Could not create ctrl uri node configuration"); | |
733 | ret = CMD_ERROR; | |
734 | goto error; | |
735 | } | |
736 | ||
737 | ret = config_element_add_child(temp_element_child, temp_element); | |
738 | if (ret) { | |
739 | ERR("Could not append control uri to the net_output node configuration"); | |
740 | ret = CMD_ERROR; | |
741 | goto error; | |
742 | } | |
2f0e04ab | 743 | |
4a102b75 | 744 | config_element_free(temp_element); |
2f0e04ab | 745 | temp_element = NULL; |
4a102b75 JR |
746 | |
747 | temp_element = config_element_create("data_uri", tmp_data_uri); | |
748 | ||
749 | if (!temp_element_child) { | |
750 | ERR("Could not create data_uri configuration"); | |
751 | ret = CMD_ERROR; | |
752 | goto error; | |
753 | } | |
754 | ||
755 | ret = config_element_add_child(temp_element_child, temp_element); | |
756 | if (ret) { | |
757 | ERR("Could not append data uri to the net_output node configuration"); | |
758 | ret = CMD_ERROR; | |
759 | goto error; | |
760 | } | |
761 | config_element_free(temp_element); | |
2f0e04ab | 762 | temp_element = NULL; |
4a102b75 JR |
763 | break; |
764 | default: | |
765 | ret = CMD_ERROR; | |
766 | goto error; | |
767 | } | |
768 | ||
769 | temp_element = config_element_create("destination", NULL); | |
770 | if (!temp_element) { | |
771 | ERR("Could not create destination node configuration"); | |
772 | ret = CMD_ERROR; | |
773 | goto error; | |
774 | } | |
775 | ||
776 | ret = config_element_add_child(temp_element, temp_element_child); | |
777 | if (ret) { | |
778 | ERR("Could not append output data to the destination node configuration"); | |
779 | ret = CMD_ERROR; | |
780 | goto error; | |
781 | } | |
782 | ||
2f0e04ab JR |
783 | config_element_free(temp_element_child); |
784 | temp_element_child = NULL; | |
785 | ||
e0988a8c JR |
786 | /* Construct the output/consumer_output/ node*/ |
787 | temp_element_child = temp_element; | |
788 | temp_element = config_element_create("consumer_output", NULL); | |
789 | if (!temp_element) { | |
790 | ERR("Could not create consumer_output node configuration"); | |
791 | ret = CMD_ERROR; | |
792 | goto error; | |
793 | } | |
794 | ||
795 | ret = config_element_add_child(temp_element, temp_element_child); | |
796 | if (ret) { | |
797 | ERR("Could not append output data to the consumer_output node configuration"); | |
798 | ret = CMD_ERROR; | |
799 | goto error; | |
800 | } | |
801 | ||
802 | config_element_free(temp_element_child); | |
803 | temp_element_child = config_element_create("enabled", "true"); | |
804 | if (!temp_element_child) { | |
805 | ERR("Could not create enbaled node configuration"); | |
806 | ret = CMD_ERROR; | |
807 | goto error; | |
808 | } | |
809 | ||
810 | ret = config_element_add_child(temp_element, temp_element_child); | |
811 | if (ret) { | |
812 | ERR("Could not append node to the consumer_output node configuration"); | |
813 | ret = CMD_ERROR; | |
814 | goto error; | |
815 | } | |
816 | ||
817 | config_element_free(temp_element_child); | |
818 | temp_element_child = NULL; | |
819 | ||
820 | ||
821 | temp_element_child = temp_element; | |
822 | temp_element = config_element_create("output", NULL); | |
823 | if (!temp_element) { | |
824 | ERR("Could not create output node configuration"); | |
825 | ret = CMD_ERROR; | |
826 | goto error; | |
827 | } | |
828 | ||
829 | ret = config_element_add_child(temp_element, temp_element_child); | |
830 | if (ret) { | |
831 | ERR("Could not append output data to the output node configuration"); | |
832 | ret = CMD_ERROR; | |
833 | goto error; | |
834 | } | |
835 | ||
836 | config_element_free(temp_element_child); | |
837 | temp_element_child = NULL; | |
838 | ||
2f0e04ab | 839 | |
4a102b75 JR |
840 | /* |
841 | * validate and replace the destination node for each session type | |
842 | * TODO: export string as const and simply assign a base path for the | |
843 | * destination node based on the session type | |
844 | **/ | |
845 | switch (session_type) { | |
846 | case SESSION_NORMAL: | |
847 | case SESSION_LIVE: | |
e0988a8c JR |
848 | break; |
849 | case SESSION_SNAPSHOT: | |
850 | /* construct the output/snapshots_outputs/ */ | |
851 | temp_element_child = temp_element; | |
852 | temp_element = config_element_create("snapshot_outputs", NULL); | |
853 | if (!temp_element) { | |
854 | ERR("Could not create snapshot_outputs node configuration"); | |
4a102b75 JR |
855 | ret = CMD_ERROR; |
856 | goto error; | |
857 | } | |
858 | ||
e0988a8c JR |
859 | ret = config_element_add_child(temp_element, temp_element_child); |
860 | if (ret) { | |
861 | ERR("Could not append output data to the snapshot_outputs node configuration"); | |
4a102b75 JR |
862 | ret = CMD_ERROR; |
863 | goto error; | |
864 | } | |
865 | ||
e0988a8c JR |
866 | config_element_free(temp_element_child); |
867 | temp_element_child = NULL; | |
868 | ||
869 | temp_element_child = temp_element; | |
870 | temp_element = config_element_create("output", NULL); | |
871 | if (!temp_element) { | |
872 | ERR("Could not create output node configuration"); | |
873 | ret = CMD_ERROR; | |
874 | goto error; | |
875 | } | |
876 | ||
877 | ret = config_element_add_child(temp_element, temp_element_child); | |
878 | if (ret) { | |
879 | ERR("Could not append output data to the output node configuration"); | |
880 | ret = CMD_ERROR; | |
881 | goto error; | |
882 | } | |
883 | ||
884 | config_element_free(temp_element_child); | |
885 | temp_element_child = NULL; | |
4a102b75 JR |
886 | break; |
887 | default: | |
888 | ERR("Invalid session type"); | |
889 | ret = CMD_UNDEFINED; | |
890 | goto error; | |
891 | } | |
892 | ||
e0988a8c JR |
893 | if (!config_document_element_exist(template, "/sessions/session/output")) { |
894 | ret = config_document_insert_element(template, "/sessions/session", temp_element); | |
895 | } else { | |
896 | ret = config_document_replace_element(template, "/sessions/session/output", temp_element); | |
897 | } | |
898 | ||
2f0e04ab JR |
899 | config_element_free(temp_element); |
900 | temp_element = NULL; | |
4a102b75 | 901 | |
0709e00e JR |
902 | if (ret) { |
903 | ERR("%s", lttng_strerror(ret)); | |
904 | ret = CMD_ERROR; | |
905 | goto error; | |
906 | } | |
907 | ||
4a102b75 JR |
908 | |
909 | /* Shm path */ | |
910 | if (shm_path && config_document_element_exist(template, "/sessions/session/shared_memory_path")) { | |
911 | /* Replace the node value */ | |
912 | config_document_replace_element_value(template, "/sessions/session/shared_memory_path", shm_path); | |
913 | } else if (shm_path) { | |
914 | /* insert the node */ | |
915 | temp_element = config_element_create("shared_memory_path", shm_path); | |
916 | if (!temp_element) { | |
917 | ERR("Could not create shared_memory_path node configuration"); | |
918 | ret = CMD_ERROR; | |
919 | goto error; | |
920 | } | |
921 | ret = config_document_insert_element(template, "/sessions/session", temp_element); | |
922 | if (ret) { | |
923 | ERR("Could not insert shared_memory_path node configuration"); | |
924 | ret = CMD_ERROR; | |
925 | goto error; | |
926 | } | |
2f0e04ab JR |
927 | |
928 | config_element_free(temp_element); | |
929 | temp_element = NULL; | |
4a102b75 JR |
930 | } |
931 | ||
932 | ret = config_load_configuration_sessions(template, session_name, 0); | |
933 | ||
934 | ||
935 | error: | |
936 | config_element_free(temp_element); | |
937 | config_element_free(temp_element_child); | |
938 | free(tmp_string); | |
939 | free(uris); | |
940 | return ret; | |
941 | ||
942 | } | |
943 | ||
f3ed775e | 944 | /* |
1c8d13c8 TD |
945 | * Create a tracing session. |
946 | * If no name is specified, a default name is generated. | |
f3ed775e | 947 | * |
1c8d13c8 | 948 | * Returns one of the CMD_* result constants. |
f3ed775e | 949 | */ |
a4b92340 | 950 | static int create_session(void) |
f3ed775e | 951 | { |
a4b92340 | 952 | int ret; |
b37959c7 | 953 | int printed_bytes; |
227bc702 | 954 | |
4a102b75 JR |
955 | |
956 | /* Template */ | |
957 | struct config_document *template = NULL; | |
958 | ||
227bc702 | 959 | /* Base data */ |
05ca94c0 JR |
960 | enum session_type base_session_type = SESSION_UNKNOWN; |
961 | enum output_type base_output_type = OUTPUT_UNKNOWN; | |
227bc702 JR |
962 | char *base_session_name = NULL; |
963 | char *base_url = NULL; | |
964 | char *base_ctrl_url = NULL; | |
965 | char *base_data_url = NULL; | |
966 | char *base_shm_path = NULL; | |
967 | int base_live_timer = 0; | |
968 | ||
969 | /* Time data */ | |
970 | char datetime[16]; | |
f3ed775e | 971 | time_t rawtime; |
227bc702 JR |
972 | struct tm *timeinfo = NULL; |
973 | ||
974 | /* Temporary variables */ | |
975 | char *traces_path = NULL; | |
976 | char *temp_url = NULL; | |
977 | char *session_name_date = NULL; | |
978 | char *tmp_url = NULL; | |
979 | char *tmp_home_path = NULL; | |
980 | struct lttng_uri *uris = NULL; | |
981 | ssize_t uri_array_size = 0; | |
982 | ||
f3ed775e | 983 | |
d6175221 DG |
984 | /* Get date and time for automatic session name/path */ |
985 | time(&rawtime); | |
986 | timeinfo = localtime(&rawtime); | |
987 | strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo); | |
988 | ||
4a102b75 JR |
989 | if (opt_template_path) { |
990 | /* Restriction on flags exist when using template */ | |
991 | /* Session type flags are not permitted */ | |
992 | /* --live & --snapshot */ | |
993 | template = config_document_get(opt_template_path); | |
994 | if (!template) { | |
995 | ERR("Template could not be parsed"); | |
996 | ret = CMD_ERROR; | |
997 | goto error; | |
998 | } | |
999 | /* Load info from template if any */ | |
1000 | /* TODO: might want to use a struct in the end for the session... */ | |
1001 | ret = parse_template(template, &base_session_name, | |
1002 | &base_session_type, | |
1003 | &base_live_timer, | |
1004 | &base_output_type, | |
1005 | &base_url, | |
1006 | &base_ctrl_url, | |
1007 | &base_data_url, | |
1008 | &base_shm_path); | |
8e4afd43 JR |
1009 | if (ret) { |
1010 | goto error; | |
1011 | } | |
4a102b75 JR |
1012 | } |
1013 | ||
1014 | /* Option validation */ | |
1015 | if (validate_command_options(base_session_type) != CMD_SUCCESS) { | |
1016 | ret = CMD_ERROR; | |
1017 | goto error; | |
1018 | } | |
1019 | ||
227bc702 JR |
1020 | /* Find the session type based on options */ |
1021 | if(base_session_type == SESSION_UNKNOWN) { | |
1022 | if (opt_snapshot) { | |
1023 | base_session_type = SESSION_SNAPSHOT; | |
1024 | } else if (opt_live_timer) { | |
1025 | base_session_type = SESSION_LIVE; | |
1026 | } else { | |
1027 | base_session_type = SESSION_NORMAL; | |
07424f16 | 1028 | } |
227bc702 JR |
1029 | } |
1030 | ||
1031 | /* | |
1032 | * Session name handling | |
1033 | */ | |
1034 | if (opt_session_name) { | |
1035 | /* Override the session name */ | |
487b253b DG |
1036 | if (strlen(opt_session_name) > NAME_MAX) { |
1037 | ERR("Session name too long. Length must be lower or equal to %d", | |
1038 | NAME_MAX); | |
1039 | ret = LTTNG_ERR_SESSION_FAIL; | |
227bc702 | 1040 | free(session_name_date); |
487b253b DG |
1041 | goto error; |
1042 | } | |
4b861950 DG |
1043 | /* |
1044 | * Check if the session name begins with "auto-" or is exactly "auto". | |
1045 | * Both are reserved for the default session name. See bug #449 to | |
1046 | * understand why we need to check both here. | |
1047 | */ | |
1048 | if ((strncmp(opt_session_name, DEFAULT_SESSION_NAME "-", | |
227bc702 JR |
1049 | strlen(DEFAULT_SESSION_NAME) + 1) == 0) || |
1050 | (strncmp(opt_session_name, DEFAULT_SESSION_NAME, | |
1051 | strlen(DEFAULT_SESSION_NAME)) == 0 && | |
67238b20 | 1052 | strlen(opt_session_name) == strlen(DEFAULT_SESSION_NAME))) { |
61b35a5a DG |
1053 | ERR("%s is a reserved keyword for default session(s)", |
1054 | DEFAULT_SESSION_NAME); | |
227bc702 JR |
1055 | |
1056 | ret = CMD_ERROR; | |
1057 | goto error; | |
1058 | } | |
1059 | ||
1060 | base_session_name = strndup(opt_session_name, NAME_MAX); | |
1061 | if (!base_session_name) { | |
1062 | PERROR("Strdup session name"); | |
61b35a5a DG |
1063 | ret = CMD_ERROR; |
1064 | goto error; | |
1065 | } | |
227bc702 | 1066 | |
b37959c7 JR |
1067 | printed_bytes = asprintf(&session_name_date, "%s-%s", base_session_name, datetime); |
1068 | if (printed_bytes < 0) { | |
227bc702 | 1069 | PERROR("Asprintf session name"); |
af7f3c88 | 1070 | ret = CMD_ERROR; |
227bc702 JR |
1071 | goto error; |
1072 | } | |
1073 | DBG("Session name from command option set to %s", base_session_name); | |
e51a4200 | 1074 | } else if (base_session_name && !opt_default_name) { |
b37959c7 JR |
1075 | printed_bytes = asprintf(&session_name_date, "%s-%s", base_session_name, datetime); |
1076 | if (printed_bytes < 0) { | |
227bc702 | 1077 | PERROR("Asprintf session name"); |
af7f3c88 | 1078 | ret = CMD_ERROR; |
227bc702 JR |
1079 | goto error; |
1080 | } | |
1081 | } else { | |
1082 | /* Generate a name */ | |
b37959c7 JR |
1083 | printed_bytes = asprintf(&base_session_name, DEFAULT_SESSION_NAME "-%s", datetime); |
1084 | if (printed_bytes < 0) { | |
6af1d7e6 | 1085 | PERROR("Asprintf session name"); |
af7f3c88 | 1086 | ret = CMD_ERROR; |
07424f16 DG |
1087 | goto error; |
1088 | } | |
227bc702 JR |
1089 | session_name_date = strdup(base_session_name); |
1090 | DBG("Auto session name set to %s", base_session_name); | |
f3ed775e DG |
1091 | } |
1092 | ||
227bc702 JR |
1093 | |
1094 | /* | |
1095 | * Output handling | |
1096 | */ | |
1097 | ||
1098 | /* | |
1099 | * If any of those options are present clear all output related data. | |
1100 | */ | |
1101 | if (opt_output_path || opt_url || (opt_ctrl_url && opt_data_url) || opt_no_output) { | |
1102 | /* Overwrite output */ | |
1103 | free(base_url); | |
1104 | free(base_ctrl_url); | |
1105 | free(base_data_url); | |
1106 | base_url = NULL; | |
1107 | base_ctrl_url = NULL; | |
1108 | base_data_url = NULL; | |
1a241656 DG |
1109 | } |
1110 | ||
227bc702 JR |
1111 | if (opt_output_path) { |
1112 | ||
81b86775 | 1113 | traces_path = utils_expand_path(opt_output_path); |
227bc702 | 1114 | if (!traces_path) { |
00e2e675 DG |
1115 | ret = CMD_ERROR; |
1116 | goto error; | |
1117 | } | |
1118 | ||
37d03ff7 | 1119 | /* Create URL string from the local file system path */ |
b37959c7 JR |
1120 | printed_bytes = asprintf(&temp_url, "file://%s", traces_path); |
1121 | if (printed_bytes < 0) { | |
a4b92340 | 1122 | PERROR("asprintf url path"); |
00e2e675 DG |
1123 | ret = CMD_FATAL; |
1124 | goto error; | |
1125 | } | |
227bc702 JR |
1126 | |
1127 | base_url = temp_url; | |
a4b92340 | 1128 | } else if (opt_url) { /* Handling URL (-U opt) */ |
227bc702 | 1129 | base_url = strdup(opt_url); |
1a241656 DG |
1130 | } else if (opt_data_url && opt_ctrl_url) { |
1131 | /* | |
227bc702 JR |
1132 | * With both control and data, we'll be setting the consumer URL |
1133 | * after session creation thus use no URL. | |
1a241656 | 1134 | */ |
227bc702 JR |
1135 | base_ctrl_url = strdup(opt_ctrl_url); |
1136 | base_data_url = strdup(opt_data_url); | |
f99c53dc JG |
1137 | } else if (opt_default_output || !(opt_no_output || |
1138 | base_output_type == OUTPUT_NONE || base_url || | |
1139 | base_ctrl_url || base_data_url)) { | |
227bc702 JR |
1140 | /* Generate default output depending on the session type */ |
1141 | switch (base_session_type) { | |
1142 | case SESSION_NORMAL: | |
1143 | /* fallthrough */ | |
1144 | case SESSION_SNAPSHOT: | |
1145 | /* Default to a local path */ | |
1146 | tmp_home_path = utils_get_home_dir(); | |
1147 | if (tmp_home_path == NULL) { | |
1148 | ERR("HOME path not found.\n \ | |
1149 | Please specify an output path using -o, --output PATH"); | |
1150 | ret = CMD_FATAL; | |
1151 | goto error; | |
1152 | } | |
ecc48a90 | 1153 | |
b37959c7 | 1154 | printed_bytes = asprintf(&tmp_url, |
227bc702 JR |
1155 | "file://%s/" DEFAULT_TRACE_DIR_NAME "/%s", |
1156 | tmp_home_path, session_name_date); | |
16f6f820 | 1157 | |
b37959c7 | 1158 | if (printed_bytes < 0) { |
227bc702 JR |
1159 | PERROR("asprintf trace dir name"); |
1160 | ret = CMD_FATAL; | |
1161 | goto error; | |
1162 | } | |
8960e9cd | 1163 | |
227bc702 | 1164 | base_url = tmp_url ; |
42224349 | 1165 | break; |
227bc702 JR |
1166 | case SESSION_LIVE: |
1167 | /* Default to a net output */ | |
b37959c7 JR |
1168 | printed_bytes = asprintf(&tmp_url, "net://127.0.0.1"); |
1169 | if (printed_bytes < 0) { | |
227bc702 JR |
1170 | PERROR("asprintf default live URL"); |
1171 | ret = CMD_FATAL; | |
1172 | goto error; | |
1173 | } | |
1174 | base_url = tmp_url ; | |
60e835ca | 1175 | break; |
227bc702 JR |
1176 | default: |
1177 | ERR("Unknown session type"); | |
1178 | ret = CMD_FATAL; | |
a4b92340 DG |
1179 | goto error; |
1180 | } | |
4f50c803 DG |
1181 | } |
1182 | ||
227bc702 JR |
1183 | /* |
1184 | * Shared memory path handling | |
1185 | */ | |
d7ba1388 | 1186 | if (opt_shm_path) { |
af7f3c88 JR |
1187 | /* Overwrite shm_path so clear any previously defined one */ |
1188 | free(base_shm_path); | |
b37959c7 JR |
1189 | printed_bytes = asprintf(&base_shm_path, "%s/%s", opt_shm_path, session_name_date); |
1190 | if (printed_bytes < 0) { | |
227bc702 | 1191 | PERROR("asprintf shm_path"); |
af7f3c88 | 1192 | ret = CMD_FATAL; |
d7ba1388 MD |
1193 | goto error; |
1194 | } | |
1195 | } | |
1196 | ||
227bc702 JR |
1197 | /* |
1198 | * Live timer handling | |
1199 | */ | |
1200 | if (opt_live_timer) { | |
1201 | base_live_timer = opt_live_timer; | |
1202 | } | |
d73c5802 | 1203 | |
227bc702 | 1204 | /* Get output type from urls */ |
b37959c7 | 1205 | /* TODO: Find a better way of inferring output type */ |
227bc702 JR |
1206 | if (base_url) { |
1207 | /* Get lttng uris from single url */ | |
1208 | uri_array_size = uri_parse_str_urls(base_url, NULL, &uris); | |
1209 | if (uri_array_size < 0) { | |
1210 | ret = CMD_ERROR; | |
1211 | goto error; | |
d73c5802 | 1212 | } |
227bc702 JR |
1213 | } else if (base_ctrl_url && base_data_url) { |
1214 | uri_array_size = uri_parse_str_urls(base_ctrl_url, base_data_url, &uris); | |
1215 | if (uri_array_size < 0) { | |
1216 | ret = CMD_ERROR; | |
1217 | goto error; | |
16f6f820 | 1218 | } |
227bc702 JR |
1219 | } else { |
1220 | /* --no-output */ | |
1221 | uri_array_size = 0; | |
1222 | } | |
1223 | ||
1224 | switch (uri_array_size) { | |
1225 | case 0: | |
1226 | base_output_type = OUTPUT_NONE; | |
1227 | break; | |
1228 | case 1: | |
1229 | base_output_type = OUTPUT_LOCAL; | |
1230 | break; | |
1231 | case 2: | |
1232 | base_output_type = OUTPUT_NET; | |
1233 | break; | |
1234 | default: | |
1235 | ret = CMD_ERROR; | |
1236 | goto error; | |
a4b92340 | 1237 | } |
227bc702 | 1238 | |
4a102b75 JR |
1239 | if (template) { |
1240 | ret = create_session_from_template(template, | |
1241 | base_session_name, | |
1242 | base_session_type, | |
1243 | base_live_timer, | |
1244 | base_output_type, | |
1245 | base_url, | |
1246 | base_ctrl_url, | |
1247 | base_data_url, | |
1248 | base_shm_path, | |
1249 | datetime); | |
1250 | } else { | |
b37959c7 | 1251 | ret = create_session_basic(base_session_name, |
4a102b75 JR |
1252 | base_session_type, |
1253 | base_live_timer, | |
1254 | base_output_type, | |
1255 | base_url, | |
1256 | base_ctrl_url, | |
1257 | base_data_url, | |
1258 | base_shm_path, | |
1259 | datetime); | |
1260 | } | |
227bc702 JR |
1261 | if (ret) { |
1262 | goto error; | |
d7ba1388 | 1263 | } |
a4b92340 | 1264 | |
b37959c7 | 1265 | ret = generate_output(base_session_name, |
227bc702 JR |
1266 | base_session_type, |
1267 | base_live_timer, | |
1268 | base_output_type, | |
1269 | base_url, | |
1270 | base_ctrl_url, | |
1271 | base_data_url, | |
1272 | base_shm_path); | |
1273 | if (ret) { | |
1274 | goto error; | |
37d03ff7 JRJ |
1275 | } |
1276 | ||
58a97671 | 1277 | /* Init lttng session config */ |
227bc702 | 1278 | ret = config_init(base_session_name); |
f3ed775e | 1279 | if (ret < 0) { |
27089920 | 1280 | ret = CMD_ERROR; |
f3ed775e DG |
1281 | goto error; |
1282 | } | |
1283 | ||
f3ed775e DG |
1284 | ret = CMD_SUCCESS; |
1285 | ||
1286 | error: | |
227bc702 | 1287 | /* Session temp stuff */ |
2f0e04ab | 1288 | config_document_free(template); |
227bc702 | 1289 | free(session_name_date); |
227bc702 | 1290 | free(uris); |
2f0e04ab | 1291 | free(traces_path); |
a4b92340 DG |
1292 | |
1293 | if (ret < 0) { | |
1294 | ERR("%s", lttng_strerror(ret)); | |
1295 | } | |
67238b20 | 1296 | |
227bc702 JR |
1297 | free(base_session_name); |
1298 | free(base_url); | |
1299 | free(base_ctrl_url); | |
1300 | free(base_data_url); | |
1301 | free(base_shm_path); | |
f3ed775e DG |
1302 | return ret; |
1303 | } | |
1304 | ||
92360082 JG |
1305 | /* |
1306 | * spawn_sessiond | |
1307 | * | |
1308 | * Spawn a session daemon by forking and execv. | |
1309 | */ | |
1310 | static int spawn_sessiond(char *pathname) | |
1311 | { | |
1312 | int ret = 0; | |
1313 | pid_t pid; | |
1314 | ||
1315 | MSG("Spawning a session daemon"); | |
92360082 JG |
1316 | pid = fork(); |
1317 | if (pid == 0) { | |
1318 | /* | |
bbd44cae | 1319 | * Spawn session daemon in daemon mode. |
92360082 | 1320 | */ |
bbd44cae PP |
1321 | execlp(pathname, "lttng-sessiond", |
1322 | "--daemonize", NULL); | |
92360082 JG |
1323 | /* execlp only returns if error happened */ |
1324 | if (errno == ENOENT) { | |
1325 | ERR("No session daemon found. Use --sessiond-path."); | |
1326 | } else { | |
1327 | PERROR("execlp"); | |
1328 | } | |
1329 | kill(getppid(), SIGTERM); /* wake parent */ | |
1330 | exit(EXIT_FAILURE); | |
1331 | } else if (pid > 0) { | |
92360082 | 1332 | /* |
bbd44cae PP |
1333 | * In daemon mode (--daemonize), sessiond only exits when |
1334 | * it's ready to accept commands. | |
92360082 | 1335 | */ |
bbd44cae | 1336 | for (;;) { |
81527d36 JG |
1337 | int status; |
1338 | pid_t wait_pid_ret = waitpid(pid, &status, 0); | |
1339 | ||
1340 | if (wait_pid_ret < 0) { | |
1341 | if (errno == EINTR) { | |
1342 | continue; | |
1343 | } | |
1344 | PERROR("waitpid"); | |
1345 | ret = -errno; | |
1346 | goto end; | |
1347 | } | |
bbd44cae PP |
1348 | |
1349 | if (WIFSIGNALED(status)) { | |
1350 | ERR("Session daemon was killed by signal %d", | |
1351 | WTERMSIG(status)); | |
1352 | ret = -1; | |
1353 | goto end; | |
1354 | } else if (WIFEXITED(status)) { | |
1355 | DBG("Session daemon terminated normally (exit status: %d)", | |
1356 | WEXITSTATUS(status)); | |
1357 | ||
1358 | if (WEXITSTATUS(status) != 0) { | |
1359 | ERR("Session daemon terminated with an error (exit status: %d)", | |
1360 | WEXITSTATUS(status)); | |
1361 | ret = -1; | |
1362 | goto end; | |
1363 | } | |
1364 | break; | |
1365 | } | |
92360082 | 1366 | } |
bbd44cae | 1367 | |
92360082 JG |
1368 | goto end; |
1369 | } else { | |
1370 | PERROR("fork"); | |
1371 | ret = -1; | |
1372 | goto end; | |
1373 | } | |
1374 | ||
1375 | end: | |
1376 | return ret; | |
1377 | } | |
1378 | ||
1379 | /* | |
1380 | * launch_sessiond | |
1381 | * | |
1382 | * Check if the session daemon is available using | |
1383 | * the liblttngctl API for the check. If not, try to | |
1384 | * spawn a daemon. | |
1385 | */ | |
1386 | static int launch_sessiond(void) | |
1387 | { | |
1388 | int ret; | |
1389 | char *pathname = NULL; | |
1390 | ||
1391 | ret = lttng_session_daemon_alive(); | |
1392 | if (ret) { | |
1393 | /* Sessiond is alive, not an error */ | |
1394 | ret = 0; | |
1395 | goto end; | |
1396 | } | |
1397 | ||
1398 | /* Try command line option path */ | |
1399 | pathname = opt_sessiond_path; | |
1400 | ||
1401 | /* Try LTTNG_SESSIOND_PATH env variable */ | |
1402 | if (pathname == NULL) { | |
1403 | pathname = getenv(DEFAULT_SESSIOND_PATH_ENV); | |
1404 | } | |
1405 | ||
1406 | /* Try with configured path */ | |
1407 | if (pathname == NULL) { | |
1408 | if (CONFIG_SESSIOND_BIN[0] != '\0') { | |
1409 | pathname = CONFIG_SESSIOND_BIN; | |
1410 | } | |
1411 | } | |
1412 | ||
1413 | /* Try the default path */ | |
1414 | if (pathname == NULL) { | |
1415 | pathname = INSTALL_BIN_PATH "/lttng-sessiond"; | |
1416 | } | |
1417 | ||
1418 | DBG("Session daemon binary path: %s", pathname); | |
1419 | ||
1420 | /* Check existence and permissions */ | |
1421 | ret = access(pathname, F_OK | X_OK); | |
1422 | if (ret < 0) { | |
1423 | ERR("No such file or access denied: %s", pathname); | |
1424 | goto end; | |
1425 | } | |
1426 | ||
1427 | ret = spawn_sessiond(pathname); | |
92360082 | 1428 | end: |
0f4fa0d2 JG |
1429 | if (ret) { |
1430 | ERR("Problem occurred while launching session daemon (%s)", | |
1431 | pathname); | |
1432 | } | |
92360082 JG |
1433 | return ret; |
1434 | } | |
1435 | ||
f3ed775e | 1436 | /* |
74cc1d0f | 1437 | * The 'create <options>' first level command |
1c8d13c8 TD |
1438 | * |
1439 | * Returns one of the CMD_* result constants. | |
f3ed775e DG |
1440 | */ |
1441 | int cmd_create(int argc, const char **argv) | |
1442 | { | |
37d03ff7 | 1443 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
ecc48a90 | 1444 | char *opt_arg = NULL; |
f3ed775e DG |
1445 | static poptContext pc; |
1446 | ||
1447 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
1448 | poptReadDefaultConfig(pc, 0); | |
1449 | ||
1450 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
1451 | switch (opt) { | |
1452 | case OPT_HELP: | |
4ba92f18 | 1453 | SHOW_HELP(); |
f3ed775e | 1454 | goto end; |
679b4943 SM |
1455 | case OPT_LIST_OPTIONS: |
1456 | list_cmd_options(stdout, long_options); | |
679b4943 | 1457 | goto end; |
ecc48a90 JD |
1458 | case OPT_LIVE_TIMER: |
1459 | { | |
1460 | unsigned long v; | |
1461 | ||
1462 | errno = 0; | |
1463 | opt_arg = poptGetOptArg(pc); | |
d73c5802 DG |
1464 | if (!opt_arg) { |
1465 | /* Set up default values. */ | |
1466 | opt_live_timer = (uint32_t) DEFAULT_LTTNG_LIVE_TIMER; | |
1467 | DBG("Session live timer interval set to default value %d", | |
1468 | opt_live_timer); | |
1469 | break; | |
1470 | } | |
1471 | ||
ecc48a90 JD |
1472 | v = strtoul(opt_arg, NULL, 0); |
1473 | if (errno != 0 || !isdigit(opt_arg[0])) { | |
1474 | ERR("Wrong value in --live parameter: %s", opt_arg); | |
1475 | ret = CMD_ERROR; | |
1476 | goto end; | |
1477 | } | |
1478 | if (v != (uint32_t) v) { | |
1479 | ERR("32-bit overflow in --live parameter: %s", opt_arg); | |
1480 | ret = CMD_ERROR; | |
1481 | goto end; | |
1482 | } | |
0ed9e0be JG |
1483 | if (v == 0) { |
1484 | ERR("Live timer interval must be greater than zero"); | |
1485 | ret = CMD_ERROR; | |
1486 | goto end; | |
1487 | } | |
ecc48a90 JD |
1488 | opt_live_timer = (uint32_t) v; |
1489 | DBG("Session live timer interval set to %d", opt_live_timer); | |
1490 | break; | |
1491 | } | |
e51a4200 JG |
1492 | case OPT_DEFAULT_NAME: |
1493 | opt_default_name = true; | |
1494 | break; | |
f99c53dc JG |
1495 | case OPT_DEFAULT_OUTPUT: |
1496 | opt_default_output = true; | |
1497 | break; | |
f3ed775e | 1498 | default: |
f3ed775e DG |
1499 | ret = CMD_UNDEFINED; |
1500 | goto end; | |
1501 | } | |
1502 | } | |
1503 | ||
785d2d0d | 1504 | if (opt_no_consumer) { |
96fe6b8d | 1505 | MSG("The option --no-consumer is obsolete. Use --no-output now."); |
785d2d0d DG |
1506 | ret = CMD_WARNING; |
1507 | goto end; | |
1508 | } | |
1509 | ||
92360082 JG |
1510 | /* Spawn a session daemon if needed */ |
1511 | if (!opt_no_sessiond) { | |
1512 | ret = launch_sessiond(); | |
1513 | if (ret) { | |
1514 | ret = CMD_ERROR; | |
1515 | goto end; | |
1516 | } | |
1517 | } | |
1518 | ||
acc09215 | 1519 | /* MI initialization */ |
37d03ff7 JRJ |
1520 | if (lttng_opt_mi) { |
1521 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); | |
1522 | if (!writer) { | |
1523 | ret = -LTTNG_ERR_NOMEM; | |
1524 | goto end; | |
1525 | } | |
1526 | ||
1527 | /* Open command element */ | |
1528 | ret = mi_lttng_writer_command_open(writer, | |
1529 | mi_lttng_element_command_create); | |
1530 | if (ret) { | |
1531 | ret = CMD_ERROR; | |
1532 | goto end; | |
1533 | } | |
1534 | ||
1535 | /* Open output element */ | |
1536 | ret = mi_lttng_writer_open_element(writer, | |
1537 | mi_lttng_element_command_output); | |
1538 | if (ret) { | |
1539 | ret = CMD_ERROR; | |
1540 | goto end; | |
1541 | } | |
1542 | } | |
f3ed775e DG |
1543 | opt_session_name = (char*) poptGetArg(pc); |
1544 | ||
37d03ff7 | 1545 | command_ret = create_session(); |
227bc702 | 1546 | |
37d03ff7 JRJ |
1547 | if (command_ret) { |
1548 | success = 0; | |
1549 | } | |
1550 | ||
1551 | if (lttng_opt_mi) { | |
1552 | /* Close output element */ | |
1553 | ret = mi_lttng_writer_close_element(writer); | |
1554 | if (ret) { | |
1555 | ret = CMD_ERROR; | |
1556 | goto end; | |
1557 | } | |
1558 | ||
1559 | /* Success ? */ | |
1560 | ret = mi_lttng_writer_write_element_bool(writer, | |
1561 | mi_lttng_element_command_success, success); | |
1562 | if (ret) { | |
1563 | ret = CMD_ERROR; | |
1564 | goto end; | |
1565 | } | |
1566 | ||
1567 | /* Command element close */ | |
1568 | ret = mi_lttng_writer_command_close(writer); | |
1569 | if (ret) { | |
1570 | ret = CMD_ERROR; | |
1571 | goto end; | |
1572 | } | |
1573 | } | |
f3ed775e DG |
1574 | |
1575 | end: | |
37d03ff7 JRJ |
1576 | /* Mi clean-up */ |
1577 | if (writer && mi_lttng_writer_destroy(writer)) { | |
1578 | /* Preserve original error code */ | |
1579 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; | |
1580 | } | |
1581 | ||
acc09215 | 1582 | /* Overwrite ret if an error occurred in create_session() */ |
37d03ff7 JRJ |
1583 | ret = command_ret ? command_ret : ret; |
1584 | ||
ca1c3607 | 1585 | poptFreeContext(pc); |
f3ed775e DG |
1586 | return ret; |
1587 | } |