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