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