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 | ||
227bc702 | 62 | enum { |
101fa438 | 63 | OUTPUT_UNKNOWN = -1, |
227bc702 JR |
64 | OUTPUT_NONE, |
65 | OUTPUT_LOCAL, | |
66 | OUTPUT_NET, | |
67 | }; | |
68 | enum { | |
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 | */ | |
4a102b75 | 231 | static int validate_command_options(int session_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 */ | |
251 | if ((opt_live_timer && session_type != SESSION_LIVE) || | |
252 | (opt_snapshot && session_type != SESSION_SNAPSHOT)) { | |
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, | |
270 | int session_type, | |
271 | int live_timer, | |
272 | int output_type, | |
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, | |
388 | int session_type, | |
389 | int live_timer, | |
390 | int output_type, | |
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, | |
453 | int *session_type, | |
454 | int *live_timer, | |
455 | int *output_type, | |
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; | |
559 | } | |
560 | ||
561 | ||
562 | /* shared memory path */ | |
563 | *shm_path = config_document_get_element_value(template,"/sessions/session/shared_memory_path"); | |
564 | ||
565 | error: | |
566 | free(raw_value); | |
567 | return ret; | |
568 | ||
569 | } | |
570 | static int create_session_from_template(struct config_document *template, | |
571 | const char *session_name, | |
572 | int session_type, | |
573 | int live_timer, | |
574 | int output_type, | |
575 | const char *url, | |
576 | const char *ctrl_url, | |
577 | const char *data_url, | |
578 | const char *shm_path, | |
579 | const char *datetime) | |
580 | { | |
581 | int ret = CMD_SUCCESS; | |
582 | struct config_element *temp_element = NULL; | |
583 | struct config_element *temp_element_child = NULL; | |
584 | char tmp_ctrl_uri[PATH_MAX]; | |
585 | char tmp_data_uri[PATH_MAX]; | |
586 | struct lttng_uri *uris = NULL; | |
587 | ssize_t uri_array_size = 0; | |
588 | char *tmp_string = NULL; | |
589 | ||
590 | assert(template); | |
591 | assert(session_name); | |
592 | ||
593 | memset(tmp_ctrl_uri, 0, sizeof(tmp_ctrl_uri)); | |
594 | memset(tmp_data_uri, 0, sizeof(tmp_data_uri)); | |
595 | ||
596 | /* Session name */ | |
597 | if (config_document_element_exist(template, "/sessions/session/name")) { | |
598 | /* Replace the node value */ | |
599 | config_document_replace_element_value(template, "/sessions/session/name", session_name); | |
600 | } else { | |
601 | /* insert the node */ | |
602 | temp_element = config_element_create("name", session_name); | |
603 | if (!temp_element) { | |
604 | ERR("Could not create session name node configuration"); | |
605 | ret = CMD_ERROR; | |
606 | goto error; | |
607 | } | |
608 | ret = config_document_insert_element(template, "/sessions/session", temp_element); | |
609 | if (ret) { | |
610 | ERR("Could not insert session name node configuration"); | |
611 | ret = CMD_ERROR; | |
612 | goto error; | |
613 | } | |
614 | config_element_free(temp_element); | |
615 | } | |
616 | ||
617 | /* | |
618 | * Live timer | |
619 | */ | |
620 | if (session_type == SESSION_LIVE) { | |
621 | if (config_document_element_exist(template, "/sessions/session/attributes/live_timer_interval")) { | |
622 | asprintf(&tmp_string, "%d", live_timer); | |
623 | config_document_replace_element_value(template, "/sessions/session/attributes/live_timer_interval", tmp_string); | |
624 | free(tmp_string); | |
625 | tmp_string = NULL; | |
626 | } else { | |
627 | ERR("Invalid live timer template. Missing live timer node"); | |
628 | ret = CMD_ERROR; | |
629 | goto error; | |
630 | } | |
631 | ||
632 | } | |
633 | ||
634 | /* | |
635 | * Generate the output node | |
636 | */ | |
637 | ||
638 | /* Get output from urls */ | |
639 | if (url) { | |
640 | /* Get lttng uris from single url */ | |
641 | uri_array_size = uri_parse_str_urls(url, NULL, &uris); | |
642 | if (uri_array_size < 0) { | |
643 | ret = CMD_ERROR; | |
644 | goto error; | |
645 | } | |
646 | } else if (ctrl_url && data_url) { | |
647 | uri_array_size = uri_parse_str_urls(ctrl_url, data_url, &uris); | |
648 | if (uri_array_size < 0) { | |
649 | ret = CMD_ERROR; | |
650 | goto error; | |
651 | } | |
652 | } else { | |
653 | /* --no-output */ | |
654 | uri_array_size = 0; | |
655 | } | |
656 | ||
657 | /* Validate if the session output type still match the passed data */ | |
658 | if ( (uri_array_size == 0 && output_type != OUTPUT_NONE) || | |
659 | (uri_array_size == 1 && output_type != OUTPUT_LOCAL) || | |
660 | (uri_array_size == 2 && output_type != OUTPUT_NET)) { | |
661 | ERR("Overwriting value for output do not match the base output type"); | |
662 | ret = CMD_ERROR; | |
663 | goto error; | |
664 | } | |
665 | ||
666 | switch (output_type) { | |
667 | case OUTPUT_NONE: | |
668 | temp_element_child = config_element_create("path", ""); | |
669 | if (!temp_element_child) { | |
670 | ERR("Could not create empty path node configuration"); | |
671 | ret = CMD_ERROR; | |
672 | goto error; | |
673 | } | |
674 | break; | |
675 | case OUTPUT_LOCAL: | |
676 | temp_element_child = config_element_create("path", uris[0].dst.path); | |
677 | if (!temp_element_child) { | |
678 | ERR("Could not create local path node configuration"); | |
679 | ret = CMD_ERROR; | |
680 | goto error; | |
681 | } | |
682 | break; | |
683 | case OUTPUT_NET: | |
684 | uri_to_str_url(&uris[0], tmp_ctrl_uri, sizeof(tmp_ctrl_uri)); | |
685 | uri_to_str_url(&uris[1], tmp_data_uri, sizeof(tmp_data_uri)); | |
686 | ||
687 | temp_element_child = config_element_create("net_output", NULL); | |
688 | if (!temp_element_child) { | |
689 | ERR("Could not create net_output node configuration"); | |
690 | ret = CMD_ERROR; | |
691 | goto error; | |
692 | } | |
693 | ||
694 | temp_element = config_element_create("control_uri", tmp_ctrl_uri); | |
695 | if (!temp_element_child) { | |
696 | ERR("Could not create ctrl uri node configuration"); | |
697 | ret = CMD_ERROR; | |
698 | goto error; | |
699 | } | |
700 | ||
701 | ret = config_element_add_child(temp_element_child, temp_element); | |
702 | if (ret) { | |
703 | ERR("Could not append control uri to the net_output node configuration"); | |
704 | ret = CMD_ERROR; | |
705 | goto error; | |
706 | } | |
707 | config_element_free(temp_element); | |
708 | ||
709 | temp_element = config_element_create("data_uri", tmp_data_uri); | |
710 | ||
711 | if (!temp_element_child) { | |
712 | ERR("Could not create data_uri configuration"); | |
713 | ret = CMD_ERROR; | |
714 | goto error; | |
715 | } | |
716 | ||
717 | ret = config_element_add_child(temp_element_child, temp_element); | |
718 | if (ret) { | |
719 | ERR("Could not append data uri to the net_output node configuration"); | |
720 | ret = CMD_ERROR; | |
721 | goto error; | |
722 | } | |
723 | config_element_free(temp_element); | |
724 | break; | |
725 | default: | |
726 | ret = CMD_ERROR; | |
727 | goto error; | |
728 | } | |
729 | ||
730 | temp_element = config_element_create("destination", NULL); | |
731 | if (!temp_element) { | |
732 | ERR("Could not create destination node configuration"); | |
733 | ret = CMD_ERROR; | |
734 | goto error; | |
735 | } | |
736 | ||
737 | ret = config_element_add_child(temp_element, temp_element_child); | |
738 | if (ret) { | |
739 | ERR("Could not append output data to the destination node configuration"); | |
740 | ret = CMD_ERROR; | |
741 | goto error; | |
742 | } | |
743 | ||
744 | /* | |
745 | * validate and replace the destination node for each session type | |
746 | * TODO: export string as const and simply assign a base path for the | |
747 | * destination node based on the session type | |
748 | **/ | |
749 | switch (session_type) { | |
750 | case SESSION_NORMAL: | |
751 | case SESSION_LIVE: | |
752 | if (!config_document_element_exist(template, "/sessions/session/output/consumer_output/destination")) { | |
753 | ERR("Invalid template no destination node configuration present"); | |
754 | ret = CMD_ERROR; | |
755 | goto error; | |
756 | } | |
757 | ||
758 | ret = config_document_replace_element(template, "/sessions/session/output/consumer_output/destination", temp_element); | |
759 | break; | |
760 | case SESSION_SNAPSHOT: | |
761 | if (!config_document_element_exist(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination")) { | |
762 | ERR("Invalid template no destination node configuration present"); | |
763 | ret = CMD_ERROR; | |
764 | goto error; | |
765 | } | |
766 | ||
767 | ret = config_document_replace_element(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination", temp_element); | |
768 | break; | |
769 | default: | |
770 | ERR("Invalid session type"); | |
771 | ret = CMD_UNDEFINED; | |
772 | goto error; | |
773 | } | |
774 | ||
775 | ||
0709e00e JR |
776 | if (ret) { |
777 | ERR("%s", lttng_strerror(ret)); | |
778 | ret = CMD_ERROR; | |
779 | goto error; | |
780 | } | |
781 | ||
4a102b75 JR |
782 | |
783 | /* Shm path */ | |
784 | if (shm_path && config_document_element_exist(template, "/sessions/session/shared_memory_path")) { | |
785 | /* Replace the node value */ | |
786 | config_document_replace_element_value(template, "/sessions/session/shared_memory_path", shm_path); | |
787 | } else if (shm_path) { | |
788 | /* insert the node */ | |
789 | temp_element = config_element_create("shared_memory_path", shm_path); | |
790 | if (!temp_element) { | |
791 | ERR("Could not create shared_memory_path node configuration"); | |
792 | ret = CMD_ERROR; | |
793 | goto error; | |
794 | } | |
795 | ret = config_document_insert_element(template, "/sessions/session", temp_element); | |
796 | if (ret) { | |
797 | ERR("Could not insert shared_memory_path node configuration"); | |
798 | ret = CMD_ERROR; | |
799 | goto error; | |
800 | } | |
801 | } | |
802 | ||
803 | ret = config_load_configuration_sessions(template, session_name, 0); | |
804 | ||
805 | ||
806 | error: | |
807 | config_element_free(temp_element); | |
808 | config_element_free(temp_element_child); | |
809 | free(tmp_string); | |
810 | free(uris); | |
811 | return ret; | |
812 | ||
813 | } | |
814 | ||
f3ed775e | 815 | /* |
1c8d13c8 TD |
816 | * Create a tracing session. |
817 | * If no name is specified, a default name is generated. | |
f3ed775e | 818 | * |
1c8d13c8 | 819 | * Returns one of the CMD_* result constants. |
f3ed775e | 820 | */ |
a4b92340 | 821 | static int create_session(void) |
f3ed775e | 822 | { |
a4b92340 | 823 | int ret; |
227bc702 | 824 | |
4a102b75 JR |
825 | |
826 | /* Template */ | |
827 | struct config_document *template = NULL; | |
828 | ||
227bc702 JR |
829 | /* Base data */ |
830 | int base_session_type = SESSION_UNKNOWN; | |
831 | int base_output_type = OUTPUT_UNKNOWN; | |
832 | char *base_session_name = NULL; | |
833 | char *base_url = NULL; | |
834 | char *base_ctrl_url = NULL; | |
835 | char *base_data_url = NULL; | |
836 | char *base_shm_path = NULL; | |
837 | int base_live_timer = 0; | |
838 | ||
839 | /* Time data */ | |
840 | char datetime[16]; | |
f3ed775e | 841 | time_t rawtime; |
227bc702 JR |
842 | struct tm *timeinfo = NULL; |
843 | ||
844 | /* Temporary variables */ | |
845 | char *traces_path = NULL; | |
846 | char *temp_url = NULL; | |
847 | char *session_name_date = NULL; | |
848 | char *tmp_url = NULL; | |
849 | char *tmp_home_path = NULL; | |
850 | struct lttng_uri *uris = NULL; | |
851 | ssize_t uri_array_size = 0; | |
852 | ||
f3ed775e | 853 | |
d6175221 DG |
854 | /* Get date and time for automatic session name/path */ |
855 | time(&rawtime); | |
856 | timeinfo = localtime(&rawtime); | |
857 | strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo); | |
858 | ||
4a102b75 JR |
859 | if (opt_template_path) { |
860 | /* Restriction on flags exist when using template */ | |
861 | /* Session type flags are not permitted */ | |
862 | /* --live & --snapshot */ | |
863 | template = config_document_get(opt_template_path); | |
864 | if (!template) { | |
865 | ERR("Template could not be parsed"); | |
866 | ret = CMD_ERROR; | |
867 | goto error; | |
868 | } | |
869 | /* Load info from template if any */ | |
870 | /* TODO: might want to use a struct in the end for the session... */ | |
871 | ret = parse_template(template, &base_session_name, | |
872 | &base_session_type, | |
873 | &base_live_timer, | |
874 | &base_output_type, | |
875 | &base_url, | |
876 | &base_ctrl_url, | |
877 | &base_data_url, | |
878 | &base_shm_path); | |
8e4afd43 JR |
879 | if (ret) { |
880 | goto error; | |
881 | } | |
4a102b75 JR |
882 | } |
883 | ||
884 | /* Option validation */ | |
885 | if (validate_command_options(base_session_type) != CMD_SUCCESS) { | |
886 | ret = CMD_ERROR; | |
887 | goto error; | |
888 | } | |
889 | ||
227bc702 JR |
890 | /* Find the session type based on options */ |
891 | if(base_session_type == SESSION_UNKNOWN) { | |
892 | if (opt_snapshot) { | |
893 | base_session_type = SESSION_SNAPSHOT; | |
894 | } else if (opt_live_timer) { | |
895 | base_session_type = SESSION_LIVE; | |
896 | } else { | |
897 | base_session_type = SESSION_NORMAL; | |
07424f16 | 898 | } |
227bc702 JR |
899 | } |
900 | ||
901 | /* | |
902 | * Session name handling | |
903 | */ | |
904 | if (opt_session_name) { | |
905 | /* Override the session name */ | |
487b253b DG |
906 | if (strlen(opt_session_name) > NAME_MAX) { |
907 | ERR("Session name too long. Length must be lower or equal to %d", | |
908 | NAME_MAX); | |
909 | ret = LTTNG_ERR_SESSION_FAIL; | |
227bc702 | 910 | free(session_name_date); |
487b253b DG |
911 | goto error; |
912 | } | |
4b861950 DG |
913 | /* |
914 | * Check if the session name begins with "auto-" or is exactly "auto". | |
915 | * Both are reserved for the default session name. See bug #449 to | |
916 | * understand why we need to check both here. | |
917 | */ | |
918 | if ((strncmp(opt_session_name, DEFAULT_SESSION_NAME "-", | |
227bc702 JR |
919 | strlen(DEFAULT_SESSION_NAME) + 1) == 0) || |
920 | (strncmp(opt_session_name, DEFAULT_SESSION_NAME, | |
921 | strlen(DEFAULT_SESSION_NAME)) == 0 && | |
67238b20 | 922 | strlen(opt_session_name) == strlen(DEFAULT_SESSION_NAME))) { |
61b35a5a DG |
923 | ERR("%s is a reserved keyword for default session(s)", |
924 | DEFAULT_SESSION_NAME); | |
227bc702 JR |
925 | |
926 | ret = CMD_ERROR; | |
927 | goto error; | |
928 | } | |
929 | ||
930 | base_session_name = strndup(opt_session_name, NAME_MAX); | |
931 | if (!base_session_name) { | |
932 | PERROR("Strdup session name"); | |
61b35a5a DG |
933 | ret = CMD_ERROR; |
934 | goto error; | |
935 | } | |
227bc702 JR |
936 | |
937 | ret = asprintf(&session_name_date, "%s-%s", base_session_name, datetime); | |
938 | if (ret < 0) { | |
939 | PERROR("Asprintf session name"); | |
940 | goto error; | |
941 | } | |
942 | DBG("Session name from command option set to %s", base_session_name); | |
943 | } else if (base_session_name) { | |
944 | ret = asprintf(&session_name_date, "%s-%s", base_session_name, datetime); | |
945 | if (ret < 0) { | |
946 | PERROR("Asprintf session name"); | |
947 | goto error; | |
948 | } | |
949 | } else { | |
950 | /* Generate a name */ | |
227bc702 | 951 | ret = asprintf(&base_session_name, DEFAULT_SESSION_NAME "-%s", datetime); |
07424f16 | 952 | if (ret < 0) { |
6af1d7e6 | 953 | PERROR("Asprintf session name"); |
07424f16 DG |
954 | goto error; |
955 | } | |
227bc702 JR |
956 | session_name_date = strdup(base_session_name); |
957 | DBG("Auto session name set to %s", base_session_name); | |
f3ed775e DG |
958 | } |
959 | ||
227bc702 JR |
960 | |
961 | /* | |
962 | * Output handling | |
963 | */ | |
964 | ||
965 | /* | |
966 | * If any of those options are present clear all output related data. | |
967 | */ | |
968 | if (opt_output_path || opt_url || (opt_ctrl_url && opt_data_url) || opt_no_output) { | |
969 | /* Overwrite output */ | |
970 | free(base_url); | |
971 | free(base_ctrl_url); | |
972 | free(base_data_url); | |
973 | base_url = NULL; | |
974 | base_ctrl_url = NULL; | |
975 | base_data_url = NULL; | |
1a241656 DG |
976 | } |
977 | ||
227bc702 JR |
978 | if (opt_output_path) { |
979 | ||
81b86775 | 980 | traces_path = utils_expand_path(opt_output_path); |
227bc702 | 981 | if (!traces_path) { |
00e2e675 DG |
982 | ret = CMD_ERROR; |
983 | goto error; | |
984 | } | |
985 | ||
37d03ff7 | 986 | /* Create URL string from the local file system path */ |
227bc702 | 987 | ret = asprintf(&temp_url, "file://%s", traces_path); |
00e2e675 | 988 | if (ret < 0) { |
a4b92340 | 989 | PERROR("asprintf url path"); |
00e2e675 DG |
990 | ret = CMD_FATAL; |
991 | goto error; | |
992 | } | |
227bc702 JR |
993 | |
994 | base_url = temp_url; | |
a4b92340 | 995 | } else if (opt_url) { /* Handling URL (-U opt) */ |
227bc702 | 996 | base_url = strdup(opt_url); |
1a241656 DG |
997 | } else if (opt_data_url && opt_ctrl_url) { |
998 | /* | |
227bc702 JR |
999 | * With both control and data, we'll be setting the consumer URL |
1000 | * after session creation thus use no URL. | |
1a241656 | 1001 | */ |
227bc702 JR |
1002 | base_ctrl_url = strdup(opt_ctrl_url); |
1003 | base_data_url = strdup(opt_data_url); | |
1004 | } else if (!(opt_no_output || base_output_type == OUTPUT_NONE || | |
1005 | base_url || base_ctrl_url || base_data_url)) { | |
1006 | /* Generate default output depending on the session type */ | |
1007 | switch (base_session_type) { | |
1008 | case SESSION_NORMAL: | |
1009 | /* fallthrough */ | |
1010 | case SESSION_SNAPSHOT: | |
1011 | /* Default to a local path */ | |
1012 | tmp_home_path = utils_get_home_dir(); | |
1013 | if (tmp_home_path == NULL) { | |
1014 | ERR("HOME path not found.\n \ | |
1015 | Please specify an output path using -o, --output PATH"); | |
1016 | ret = CMD_FATAL; | |
1017 | goto error; | |
1018 | } | |
ecc48a90 | 1019 | |
227bc702 JR |
1020 | ret = asprintf(&tmp_url, |
1021 | "file://%s/" DEFAULT_TRACE_DIR_NAME "/%s", | |
1022 | tmp_home_path, session_name_date); | |
16f6f820 | 1023 | |
227bc702 JR |
1024 | if (ret < 0) { |
1025 | PERROR("asprintf trace dir name"); | |
1026 | ret = CMD_FATAL; | |
1027 | goto error; | |
1028 | } | |
8960e9cd | 1029 | |
227bc702 | 1030 | base_url = tmp_url ; |
42224349 | 1031 | break; |
227bc702 JR |
1032 | case SESSION_LIVE: |
1033 | /* Default to a net output */ | |
1034 | ret = asprintf(&tmp_url, "net://127.0.0.1"); | |
1035 | if (ret < 0) { | |
1036 | PERROR("asprintf default live URL"); | |
1037 | ret = CMD_FATAL; | |
1038 | goto error; | |
1039 | } | |
1040 | base_url = tmp_url ; | |
60e835ca | 1041 | break; |
227bc702 JR |
1042 | default: |
1043 | ERR("Unknown session type"); | |
1044 | ret = CMD_FATAL; | |
a4b92340 DG |
1045 | goto error; |
1046 | } | |
4f50c803 DG |
1047 | } |
1048 | ||
227bc702 JR |
1049 | /* |
1050 | * Shared memory path handling | |
1051 | */ | |
d7ba1388 | 1052 | if (opt_shm_path) { |
227bc702 | 1053 | ret = asprintf(&base_shm_path, "%s/%s", opt_shm_path, session_name_date); |
d7ba1388 | 1054 | if (ret < 0) { |
227bc702 | 1055 | PERROR("asprintf shm_path"); |
d7ba1388 MD |
1056 | goto error; |
1057 | } | |
1058 | } | |
1059 | ||
227bc702 JR |
1060 | /* |
1061 | * Live timer handling | |
1062 | */ | |
1063 | if (opt_live_timer) { | |
1064 | base_live_timer = opt_live_timer; | |
1065 | } | |
d73c5802 | 1066 | |
227bc702 JR |
1067 | /* Get output type from urls */ |
1068 | if (base_url) { | |
1069 | /* Get lttng uris from single url */ | |
1070 | uri_array_size = uri_parse_str_urls(base_url, NULL, &uris); | |
1071 | if (uri_array_size < 0) { | |
1072 | ret = CMD_ERROR; | |
1073 | goto error; | |
d73c5802 | 1074 | } |
227bc702 JR |
1075 | } else if (base_ctrl_url && base_data_url) { |
1076 | uri_array_size = uri_parse_str_urls(base_ctrl_url, base_data_url, &uris); | |
1077 | if (uri_array_size < 0) { | |
1078 | ret = CMD_ERROR; | |
1079 | goto error; | |
16f6f820 | 1080 | } |
227bc702 JR |
1081 | } else { |
1082 | /* --no-output */ | |
1083 | uri_array_size = 0; | |
1084 | } | |
1085 | ||
1086 | switch (uri_array_size) { | |
1087 | case 0: | |
1088 | base_output_type = OUTPUT_NONE; | |
1089 | break; | |
1090 | case 1: | |
1091 | base_output_type = OUTPUT_LOCAL; | |
1092 | break; | |
1093 | case 2: | |
1094 | base_output_type = OUTPUT_NET; | |
1095 | break; | |
1096 | default: | |
1097 | ret = CMD_ERROR; | |
1098 | goto error; | |
a4b92340 | 1099 | } |
227bc702 | 1100 | |
4a102b75 JR |
1101 | if (template) { |
1102 | ret = create_session_from_template(template, | |
1103 | base_session_name, | |
1104 | base_session_type, | |
1105 | base_live_timer, | |
1106 | base_output_type, | |
1107 | base_url, | |
1108 | base_ctrl_url, | |
1109 | base_data_url, | |
1110 | base_shm_path, | |
1111 | datetime); | |
1112 | } else { | |
1113 | ret = create_session_basic (base_session_name, | |
1114 | base_session_type, | |
1115 | base_live_timer, | |
1116 | base_output_type, | |
1117 | base_url, | |
1118 | base_ctrl_url, | |
1119 | base_data_url, | |
1120 | base_shm_path, | |
1121 | datetime); | |
1122 | } | |
227bc702 JR |
1123 | if (ret) { |
1124 | goto error; | |
d7ba1388 | 1125 | } |
a4b92340 | 1126 | |
227bc702 JR |
1127 | ret = generate_output (base_session_name, |
1128 | base_session_type, | |
1129 | base_live_timer, | |
1130 | base_output_type, | |
1131 | base_url, | |
1132 | base_ctrl_url, | |
1133 | base_data_url, | |
1134 | base_shm_path); | |
1135 | if (ret) { | |
1136 | goto error; | |
37d03ff7 JRJ |
1137 | } |
1138 | ||
58a97671 | 1139 | /* Init lttng session config */ |
227bc702 | 1140 | ret = config_init(base_session_name); |
f3ed775e | 1141 | if (ret < 0) { |
27089920 | 1142 | ret = CMD_ERROR; |
f3ed775e DG |
1143 | goto error; |
1144 | } | |
1145 | ||
f3ed775e DG |
1146 | ret = CMD_SUCCESS; |
1147 | ||
1148 | error: | |
227bc702 JR |
1149 | /* Session temp stuff */ |
1150 | free(session_name_date); | |
227bc702 | 1151 | free(uris); |
a4b92340 DG |
1152 | |
1153 | if (ret < 0) { | |
1154 | ERR("%s", lttng_strerror(ret)); | |
1155 | } | |
67238b20 | 1156 | |
227bc702 JR |
1157 | free(base_session_name); |
1158 | free(base_url); | |
1159 | free(base_ctrl_url); | |
1160 | free(base_data_url); | |
1161 | free(base_shm_path); | |
f3ed775e DG |
1162 | return ret; |
1163 | } | |
1164 | ||
92360082 JG |
1165 | /* |
1166 | * spawn_sessiond | |
1167 | * | |
1168 | * Spawn a session daemon by forking and execv. | |
1169 | */ | |
1170 | static int spawn_sessiond(char *pathname) | |
1171 | { | |
1172 | int ret = 0; | |
1173 | pid_t pid; | |
1174 | ||
1175 | MSG("Spawning a session daemon"); | |
92360082 JG |
1176 | pid = fork(); |
1177 | if (pid == 0) { | |
1178 | /* | |
bbd44cae | 1179 | * Spawn session daemon in daemon mode. |
92360082 | 1180 | */ |
bbd44cae PP |
1181 | execlp(pathname, "lttng-sessiond", |
1182 | "--daemonize", NULL); | |
92360082 JG |
1183 | /* execlp only returns if error happened */ |
1184 | if (errno == ENOENT) { | |
1185 | ERR("No session daemon found. Use --sessiond-path."); | |
1186 | } else { | |
1187 | PERROR("execlp"); | |
1188 | } | |
1189 | kill(getppid(), SIGTERM); /* wake parent */ | |
1190 | exit(EXIT_FAILURE); | |
1191 | } else if (pid > 0) { | |
92360082 | 1192 | /* |
bbd44cae PP |
1193 | * In daemon mode (--daemonize), sessiond only exits when |
1194 | * it's ready to accept commands. | |
92360082 | 1195 | */ |
bbd44cae | 1196 | for (;;) { |
81527d36 JG |
1197 | int status; |
1198 | pid_t wait_pid_ret = waitpid(pid, &status, 0); | |
1199 | ||
1200 | if (wait_pid_ret < 0) { | |
1201 | if (errno == EINTR) { | |
1202 | continue; | |
1203 | } | |
1204 | PERROR("waitpid"); | |
1205 | ret = -errno; | |
1206 | goto end; | |
1207 | } | |
bbd44cae PP |
1208 | |
1209 | if (WIFSIGNALED(status)) { | |
1210 | ERR("Session daemon was killed by signal %d", | |
1211 | WTERMSIG(status)); | |
1212 | ret = -1; | |
1213 | goto end; | |
1214 | } else if (WIFEXITED(status)) { | |
1215 | DBG("Session daemon terminated normally (exit status: %d)", | |
1216 | WEXITSTATUS(status)); | |
1217 | ||
1218 | if (WEXITSTATUS(status) != 0) { | |
1219 | ERR("Session daemon terminated with an error (exit status: %d)", | |
1220 | WEXITSTATUS(status)); | |
1221 | ret = -1; | |
1222 | goto end; | |
1223 | } | |
1224 | break; | |
1225 | } | |
92360082 | 1226 | } |
bbd44cae | 1227 | |
92360082 JG |
1228 | goto end; |
1229 | } else { | |
1230 | PERROR("fork"); | |
1231 | ret = -1; | |
1232 | goto end; | |
1233 | } | |
1234 | ||
1235 | end: | |
1236 | return ret; | |
1237 | } | |
1238 | ||
1239 | /* | |
1240 | * launch_sessiond | |
1241 | * | |
1242 | * Check if the session daemon is available using | |
1243 | * the liblttngctl API for the check. If not, try to | |
1244 | * spawn a daemon. | |
1245 | */ | |
1246 | static int launch_sessiond(void) | |
1247 | { | |
1248 | int ret; | |
1249 | char *pathname = NULL; | |
1250 | ||
1251 | ret = lttng_session_daemon_alive(); | |
1252 | if (ret) { | |
1253 | /* Sessiond is alive, not an error */ | |
1254 | ret = 0; | |
1255 | goto end; | |
1256 | } | |
1257 | ||
1258 | /* Try command line option path */ | |
1259 | pathname = opt_sessiond_path; | |
1260 | ||
1261 | /* Try LTTNG_SESSIOND_PATH env variable */ | |
1262 | if (pathname == NULL) { | |
1263 | pathname = getenv(DEFAULT_SESSIOND_PATH_ENV); | |
1264 | } | |
1265 | ||
1266 | /* Try with configured path */ | |
1267 | if (pathname == NULL) { | |
1268 | if (CONFIG_SESSIOND_BIN[0] != '\0') { | |
1269 | pathname = CONFIG_SESSIOND_BIN; | |
1270 | } | |
1271 | } | |
1272 | ||
1273 | /* Try the default path */ | |
1274 | if (pathname == NULL) { | |
1275 | pathname = INSTALL_BIN_PATH "/lttng-sessiond"; | |
1276 | } | |
1277 | ||
1278 | DBG("Session daemon binary path: %s", pathname); | |
1279 | ||
1280 | /* Check existence and permissions */ | |
1281 | ret = access(pathname, F_OK | X_OK); | |
1282 | if (ret < 0) { | |
1283 | ERR("No such file or access denied: %s", pathname); | |
1284 | goto end; | |
1285 | } | |
1286 | ||
1287 | ret = spawn_sessiond(pathname); | |
92360082 | 1288 | end: |
0f4fa0d2 JG |
1289 | if (ret) { |
1290 | ERR("Problem occurred while launching session daemon (%s)", | |
1291 | pathname); | |
1292 | } | |
92360082 JG |
1293 | return ret; |
1294 | } | |
1295 | ||
f3ed775e | 1296 | /* |
74cc1d0f | 1297 | * The 'create <options>' first level command |
1c8d13c8 TD |
1298 | * |
1299 | * Returns one of the CMD_* result constants. | |
f3ed775e DG |
1300 | */ |
1301 | int cmd_create(int argc, const char **argv) | |
1302 | { | |
37d03ff7 | 1303 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
ecc48a90 | 1304 | char *opt_arg = NULL; |
f3ed775e DG |
1305 | static poptContext pc; |
1306 | ||
1307 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
1308 | poptReadDefaultConfig(pc, 0); | |
1309 | ||
1310 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
1311 | switch (opt) { | |
1312 | case OPT_HELP: | |
4ba92f18 | 1313 | SHOW_HELP(); |
f3ed775e | 1314 | goto end; |
679b4943 SM |
1315 | case OPT_LIST_OPTIONS: |
1316 | list_cmd_options(stdout, long_options); | |
679b4943 | 1317 | goto end; |
ecc48a90 JD |
1318 | case OPT_LIVE_TIMER: |
1319 | { | |
1320 | unsigned long v; | |
1321 | ||
1322 | errno = 0; | |
1323 | opt_arg = poptGetOptArg(pc); | |
d73c5802 DG |
1324 | if (!opt_arg) { |
1325 | /* Set up default values. */ | |
1326 | opt_live_timer = (uint32_t) DEFAULT_LTTNG_LIVE_TIMER; | |
1327 | DBG("Session live timer interval set to default value %d", | |
1328 | opt_live_timer); | |
1329 | break; | |
1330 | } | |
1331 | ||
ecc48a90 JD |
1332 | v = strtoul(opt_arg, NULL, 0); |
1333 | if (errno != 0 || !isdigit(opt_arg[0])) { | |
1334 | ERR("Wrong value in --live parameter: %s", opt_arg); | |
1335 | ret = CMD_ERROR; | |
1336 | goto end; | |
1337 | } | |
1338 | if (v != (uint32_t) v) { | |
1339 | ERR("32-bit overflow in --live parameter: %s", opt_arg); | |
1340 | ret = CMD_ERROR; | |
1341 | goto end; | |
1342 | } | |
0ed9e0be JG |
1343 | if (v == 0) { |
1344 | ERR("Live timer interval must be greater than zero"); | |
1345 | ret = CMD_ERROR; | |
1346 | goto end; | |
1347 | } | |
ecc48a90 JD |
1348 | opt_live_timer = (uint32_t) v; |
1349 | DBG("Session live timer interval set to %d", opt_live_timer); | |
1350 | break; | |
1351 | } | |
f3ed775e | 1352 | default: |
f3ed775e DG |
1353 | ret = CMD_UNDEFINED; |
1354 | goto end; | |
1355 | } | |
1356 | } | |
1357 | ||
785d2d0d | 1358 | if (opt_no_consumer) { |
96fe6b8d | 1359 | MSG("The option --no-consumer is obsolete. Use --no-output now."); |
785d2d0d DG |
1360 | ret = CMD_WARNING; |
1361 | goto end; | |
1362 | } | |
1363 | ||
92360082 JG |
1364 | /* Spawn a session daemon if needed */ |
1365 | if (!opt_no_sessiond) { | |
1366 | ret = launch_sessiond(); | |
1367 | if (ret) { | |
1368 | ret = CMD_ERROR; | |
1369 | goto end; | |
1370 | } | |
1371 | } | |
1372 | ||
acc09215 | 1373 | /* MI initialization */ |
37d03ff7 JRJ |
1374 | if (lttng_opt_mi) { |
1375 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); | |
1376 | if (!writer) { | |
1377 | ret = -LTTNG_ERR_NOMEM; | |
1378 | goto end; | |
1379 | } | |
1380 | ||
1381 | /* Open command element */ | |
1382 | ret = mi_lttng_writer_command_open(writer, | |
1383 | mi_lttng_element_command_create); | |
1384 | if (ret) { | |
1385 | ret = CMD_ERROR; | |
1386 | goto end; | |
1387 | } | |
1388 | ||
1389 | /* Open output element */ | |
1390 | ret = mi_lttng_writer_open_element(writer, | |
1391 | mi_lttng_element_command_output); | |
1392 | if (ret) { | |
1393 | ret = CMD_ERROR; | |
1394 | goto end; | |
1395 | } | |
1396 | } | |
f3ed775e DG |
1397 | opt_session_name = (char*) poptGetArg(pc); |
1398 | ||
37d03ff7 | 1399 | command_ret = create_session(); |
227bc702 | 1400 | |
37d03ff7 JRJ |
1401 | if (command_ret) { |
1402 | success = 0; | |
1403 | } | |
1404 | ||
1405 | if (lttng_opt_mi) { | |
1406 | /* Close output element */ | |
1407 | ret = mi_lttng_writer_close_element(writer); | |
1408 | if (ret) { | |
1409 | ret = CMD_ERROR; | |
1410 | goto end; | |
1411 | } | |
1412 | ||
1413 | /* Success ? */ | |
1414 | ret = mi_lttng_writer_write_element_bool(writer, | |
1415 | mi_lttng_element_command_success, success); | |
1416 | if (ret) { | |
1417 | ret = CMD_ERROR; | |
1418 | goto end; | |
1419 | } | |
1420 | ||
1421 | /* Command element close */ | |
1422 | ret = mi_lttng_writer_command_close(writer); | |
1423 | if (ret) { | |
1424 | ret = CMD_ERROR; | |
1425 | goto end; | |
1426 | } | |
1427 | } | |
f3ed775e DG |
1428 | |
1429 | end: | |
37d03ff7 JRJ |
1430 | /* Mi clean-up */ |
1431 | if (writer && mi_lttng_writer_destroy(writer)) { | |
1432 | /* Preserve original error code */ | |
1433 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; | |
1434 | } | |
1435 | ||
acc09215 | 1436 | /* Overwrite ret if an error occurred in create_session() */ |
37d03ff7 JRJ |
1437 | ret = command_ret ? command_ret : ret; |
1438 | ||
ca1c3607 | 1439 | poptFreeContext(pc); |
f3ed775e DG |
1440 | return ret; |
1441 | } |