Commit | Line | Data |
---|---|---|
f3ed775e | 1 | /* |
ab5be9fa MJ |
2 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> |
3 | * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
f3ed775e | 4 | * |
ab5be9fa | 5 | * SPDX-License-Identifier: GPL-2.0-only |
f3ed775e | 6 | * |
f3ed775e DG |
7 | */ |
8 | ||
6c1c0768 | 9 | #define _LGPL_SOURCE |
a4b92340 | 10 | #include <assert.h> |
ecc48a90 | 11 | #include <ctype.h> |
f3ed775e DG |
12 | #include <popt.h> |
13 | #include <stdio.h> | |
14 | #include <stdlib.h> | |
15 | #include <string.h> | |
16 | #include <sys/stat.h> | |
17 | #include <sys/types.h> | |
389fbf04 | 18 | #include <common/compat/time.h> |
f3ed775e | 19 | #include <unistd.h> |
92360082 | 20 | #include <signal.h> |
bbd44cae | 21 | #include <sys/wait.h> |
f3ed775e | 22 | |
37d03ff7 JRJ |
23 | #include <common/mi-lttng.h> |
24 | ||
c399183f | 25 | #include "../command.h" |
679b4943 | 26 | #include "../utils.h" |
f3ed775e | 27 | |
00e2e675 | 28 | #include <common/defaults.h> |
42224349 | 29 | #include <common/sessiond-comm/sessiond-comm.h> |
00e2e675 | 30 | #include <common/uri.h> |
81b86775 | 31 | #include <common/utils.h> |
050dd639 | 32 | #include <lttng/lttng.h> |
42224349 | 33 | |
f3ed775e DG |
34 | static char *opt_output_path; |
35 | static char *opt_session_name; | |
a4b92340 DG |
36 | static char *opt_url; |
37 | static char *opt_ctrl_url; | |
38 | static char *opt_data_url; | |
d7ba1388 | 39 | static char *opt_shm_path; |
a4b92340 | 40 | static int opt_no_consumer; |
96fe6b8d | 41 | static int opt_no_output; |
16f6f820 | 42 | static int opt_snapshot; |
c7219617 | 43 | static uint32_t opt_live_timer; |
f3ed775e | 44 | |
4fc83d94 PP |
45 | #ifdef LTTNG_EMBED_HELP |
46 | static const char help_msg[] = | |
47 | #include <lttng-create.1.h> | |
48 | ; | |
49 | #endif | |
50 | ||
f3ed775e DG |
51 | enum { |
52 | OPT_HELP = 1, | |
679b4943 | 53 | OPT_LIST_OPTIONS, |
ecc48a90 | 54 | OPT_LIVE_TIMER, |
f3ed775e DG |
55 | }; |
56 | ||
b178f53e JG |
57 | enum output_type { |
58 | OUTPUT_NONE, | |
59 | OUTPUT_LOCAL, | |
60 | OUTPUT_NETWORK, | |
61 | OUTPUT_UNSPECIFIED, | |
62 | }; | |
37d03ff7 | 63 | |
b178f53e | 64 | static struct mi_writer *writer; |
f3ed775e DG |
65 | static struct poptOption long_options[] = { |
66 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
679b4943 SM |
67 | {"help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL}, |
68 | {"output", 'o', POPT_ARG_STRING, &opt_output_path, 0, NULL, NULL}, | |
69 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, | |
23d14dff DG |
70 | {"set-url", 'U', POPT_ARG_STRING, &opt_url, 0, 0, 0}, |
71 | {"ctrl-url", 'C', POPT_ARG_STRING, &opt_ctrl_url, 0, 0, 0}, | |
72 | {"data-url", 'D', POPT_ARG_STRING, &opt_data_url, 0, 0, 0}, | |
96fe6b8d | 73 | {"no-output", 0, POPT_ARG_VAL, &opt_no_output, 1, 0, 0}, |
2bba9e53 | 74 | {"no-consumer", 0, POPT_ARG_VAL, &opt_no_consumer, 1, 0, 0}, |
16f6f820 | 75 | {"snapshot", 0, POPT_ARG_VAL, &opt_snapshot, 1, 0, 0}, |
d73c5802 | 76 | {"live", 0, POPT_ARG_INT | POPT_ARGFLAG_OPTIONAL, 0, OPT_LIVE_TIMER, 0, 0}, |
d7ba1388 | 77 | {"shm-path", 0, POPT_ARG_STRING, &opt_shm_path, 0, 0, 0}, |
f3ed775e DG |
78 | {0, 0, 0, 0, 0, 0, 0} |
79 | }; | |
80 | ||
37d03ff7 | 81 | /* |
485ca16f | 82 | * Retrieve the created session and mi output it based on provided argument |
37d03ff7 JRJ |
83 | * This is currently a summary of what was pretty printed and is subject to |
84 | * enhancements. | |
37d03ff7 JRJ |
85 | */ |
86 | static int mi_created_session(const char *session_name) | |
87 | { | |
88 | int ret, i, count, found; | |
89 | struct lttng_session *sessions; | |
90 | ||
91 | /* session_name should not be null */ | |
92 | assert(session_name); | |
93 | assert(writer); | |
94 | ||
95 | count = lttng_list_sessions(&sessions); | |
96 | if (count < 0) { | |
97 | ret = count; | |
98 | ERR("%s", lttng_strerror(ret)); | |
99 | goto error; | |
100 | } | |
101 | ||
102 | if (count == 0) { | |
103 | ERR("Error session creation failed: session %s not found", session_name); | |
104 | ret = -LTTNG_ERR_SESS_NOT_FOUND; | |
105 | goto end; | |
106 | } | |
107 | ||
108 | found = 0; | |
109 | for (i = 0; i < count; i++) { | |
110 | if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) { | |
111 | found = 1; | |
112 | ret = mi_lttng_session(writer, &sessions[i], 0); | |
113 | if (ret) { | |
114 | goto error; | |
115 | } | |
116 | break; | |
117 | } | |
118 | } | |
119 | ||
120 | if (!found) { | |
121 | ret = -LTTNG_ERR_SESS_NOT_FOUND; | |
122 | } else { | |
123 | ret = CMD_SUCCESS; | |
124 | } | |
125 | ||
126 | error: | |
127 | free(sessions); | |
128 | end: | |
129 | return ret; | |
130 | } | |
131 | ||
b178f53e JG |
132 | static |
133 | struct lttng_session_descriptor *create_session_descriptor(void) | |
16f6f820 | 134 | { |
b178f53e JG |
135 | ssize_t uri_count; |
136 | enum output_type output_type; | |
137 | struct lttng_uri *uris = NULL; | |
138 | struct lttng_session_descriptor *descriptor = NULL; | |
139 | const char *uri_str1 = NULL, *uri_str2 = NULL; | |
140 | char local_output_path[LTTNG_PATH_MAX] = {}; | |
141 | ||
142 | if (opt_no_output) { | |
143 | output_type = OUTPUT_NONE; | |
144 | } else if (opt_output_path) { | |
145 | char *expanded_output_path; | |
21a4b056 | 146 | int ret; |
b178f53e JG |
147 | |
148 | output_type = OUTPUT_LOCAL; | |
149 | expanded_output_path = utils_expand_path(opt_output_path); | |
150 | if (!expanded_output_path) { | |
151 | ERR("Failed to expand output path."); | |
152 | goto end; | |
153 | } | |
154 | ret = lttng_strncpy(local_output_path, expanded_output_path, | |
155 | sizeof(local_output_path)); | |
156 | free(expanded_output_path); | |
157 | if (ret) { | |
158 | ERR("Output path exceeds the maximal supported length (%zu bytes)", | |
159 | sizeof(local_output_path)); | |
160 | goto end; | |
161 | } | |
162 | } else if (opt_url || opt_ctrl_url) { | |
21a4b056 SM |
163 | int ret; |
164 | ||
b178f53e JG |
165 | uri_str1 = opt_ctrl_url ? opt_ctrl_url : opt_url; |
166 | uri_str2 = opt_data_url; | |
167 | ||
168 | uri_count = uri_parse_str_urls(uri_str1, uri_str2, &uris); | |
169 | if (uri_count != 1 && uri_count != 2) { | |
170 | ERR("Unrecognized URL format."); | |
171 | goto end; | |
172 | } | |
16f6f820 | 173 | |
b178f53e JG |
174 | switch (uri_count) { |
175 | case 1: | |
176 | output_type = OUTPUT_LOCAL; | |
177 | if (uris[0].dtype != LTTNG_DST_PATH) { | |
178 | ERR("Unrecognized URL format."); | |
179 | goto end; | |
180 | } | |
181 | ret = lttng_strncpy(local_output_path, uris[0].dst.path, | |
182 | sizeof(local_output_path)); | |
183 | if (ret) { | |
184 | ERR("Output path exceeds the maximal supported length (%zu bytes)", | |
185 | sizeof(local_output_path)); | |
186 | } | |
187 | break; | |
188 | case 2: | |
189 | output_type = OUTPUT_NETWORK; | |
190 | break; | |
191 | default: | |
192 | /* Already checked. */ | |
193 | abort(); | |
194 | } | |
195 | } else { | |
196 | output_type = OUTPUT_UNSPECIFIED; | |
16f6f820 DG |
197 | } |
198 | ||
b178f53e JG |
199 | if (opt_snapshot) { |
200 | /* Snapshot session. */ | |
201 | switch (output_type) { | |
202 | case OUTPUT_UNSPECIFIED: | |
203 | case OUTPUT_LOCAL: | |
204 | descriptor = lttng_session_descriptor_snapshot_local_create( | |
205 | opt_session_name, | |
206 | output_type == OUTPUT_LOCAL ? | |
207 | local_output_path : NULL); | |
208 | break; | |
209 | case OUTPUT_NONE: | |
210 | descriptor = lttng_session_descriptor_snapshot_create( | |
211 | opt_session_name); | |
212 | break; | |
213 | case OUTPUT_NETWORK: | |
214 | descriptor = lttng_session_descriptor_snapshot_network_create( | |
215 | opt_session_name, uri_str1, uri_str2); | |
216 | break; | |
217 | default: | |
218 | abort(); | |
16f6f820 | 219 | } |
b178f53e JG |
220 | } else if (opt_live_timer) { |
221 | /* Live session. */ | |
222 | if (output_type != OUTPUT_UNSPECIFIED && | |
223 | output_type != OUTPUT_NETWORK) { | |
224 | ERR("Unsupported output type specified for live session."); | |
225 | goto end; | |
226 | } | |
227 | descriptor = lttng_session_descriptor_live_network_create( | |
228 | opt_session_name, uri_str1, uri_str2, | |
229 | opt_live_timer); | |
b178f53e JG |
230 | } else { |
231 | /* Regular session. */ | |
232 | switch (output_type) { | |
233 | case OUTPUT_UNSPECIFIED: | |
234 | case OUTPUT_LOCAL: | |
235 | descriptor = lttng_session_descriptor_local_create( | |
236 | opt_session_name, | |
237 | output_type == OUTPUT_LOCAL ? | |
238 | local_output_path : NULL); | |
239 | break; | |
240 | case OUTPUT_NONE: | |
241 | descriptor = lttng_session_descriptor_create( | |
242 | opt_session_name); | |
243 | break; | |
244 | case OUTPUT_NETWORK: | |
245 | descriptor = lttng_session_descriptor_network_create( | |
246 | opt_session_name, uri_str1, uri_str2); | |
247 | break; | |
248 | default: | |
249 | abort(); | |
16f6f820 DG |
250 | } |
251 | } | |
b178f53e JG |
252 | if (!descriptor) { |
253 | ERR("Failed to initialize session creation command."); | |
973ad93e JG |
254 | } else { |
255 | /* | |
256 | * Auto-launch the relay daemon when a live session | |
257 | * is created using default URLs. | |
258 | */ | |
259 | if (!opt_url && !opt_ctrl_url && !opt_data_url && | |
260 | opt_live_timer && !check_relayd()) { | |
261 | int ret; | |
262 | const char *pathname = opt_relayd_path ? : | |
263 | INSTALL_BIN_PATH "/lttng-relayd"; | |
264 | ||
265 | ret = spawn_relayd(pathname, 0); | |
266 | if (ret < 0) { | |
267 | lttng_session_descriptor_destroy(descriptor); | |
268 | descriptor = NULL; | |
269 | } | |
270 | } | |
16f6f820 | 271 | } |
b178f53e JG |
272 | end: |
273 | free(uris); | |
274 | return descriptor; | |
16f6f820 DG |
275 | } |
276 | ||
f3ed775e | 277 | /* |
1c8d13c8 TD |
278 | * Create a tracing session. |
279 | * If no name is specified, a default name is generated. | |
f3ed775e | 280 | * |
1c8d13c8 | 281 | * Returns one of the CMD_* result constants. |
f3ed775e | 282 | */ |
a4b92340 | 283 | static int create_session(void) |
f3ed775e | 284 | { |
b178f53e JG |
285 | int ret, i; |
286 | char shm_path[LTTNG_PATH_MAX] = {}; | |
287 | struct lttng_session_descriptor *session_descriptor = NULL; | |
288 | enum lttng_session_descriptor_status descriptor_status; | |
289 | enum lttng_error_code ret_code; | |
290 | struct lttng_session *sessions = NULL; | |
291 | const struct lttng_session *created_session = NULL; | |
292 | const char *created_session_name; | |
293 | ||
294 | /* Validate options. */ | |
295 | if (opt_session_name) { | |
487b253b DG |
296 | if (strlen(opt_session_name) > NAME_MAX) { |
297 | ERR("Session name too long. Length must be lower or equal to %d", | |
298 | NAME_MAX); | |
b178f53e | 299 | ret = CMD_ERROR; |
487b253b DG |
300 | goto error; |
301 | } | |
4b861950 DG |
302 | /* |
303 | * Check if the session name begins with "auto-" or is exactly "auto". | |
304 | * Both are reserved for the default session name. See bug #449 to | |
305 | * understand why we need to check both here. | |
306 | */ | |
307 | if ((strncmp(opt_session_name, DEFAULT_SESSION_NAME "-", | |
308 | strlen(DEFAULT_SESSION_NAME) + 1) == 0) || | |
309 | (strncmp(opt_session_name, DEFAULT_SESSION_NAME, | |
61b35a5a | 310 | strlen(DEFAULT_SESSION_NAME)) == 0 && |
4b861950 | 311 | strlen(opt_session_name) == strlen(DEFAULT_SESSION_NAME))) { |
61b35a5a DG |
312 | ERR("%s is a reserved keyword for default session(s)", |
313 | DEFAULT_SESSION_NAME); | |
314 | ret = CMD_ERROR; | |
315 | goto error; | |
316 | } | |
f3ed775e DG |
317 | } |
318 | ||
b178f53e JG |
319 | if (opt_snapshot && opt_live_timer) { |
320 | ERR("Snapshot and live modes are mutually exclusive."); | |
1a241656 DG |
321 | ret = CMD_ERROR; |
322 | goto error; | |
323 | } | |
324 | ||
b178f53e JG |
325 | if ((!opt_ctrl_url && opt_data_url) || (opt_ctrl_url && !opt_data_url)) { |
326 | ERR("Both control and data URLs must be specified."); | |
327 | ret = CMD_ERROR; | |
328 | goto error; | |
00e2e675 DG |
329 | } |
330 | ||
b178f53e JG |
331 | session_descriptor = create_session_descriptor(); |
332 | if (!session_descriptor) { | |
333 | ret = CMD_ERROR; | |
334 | goto error; | |
ecc48a90 | 335 | } |
b178f53e JG |
336 | ret_code = lttng_create_session_ext(session_descriptor); |
337 | if (ret_code != LTTNG_OK) { | |
338 | ERR("%s", lttng_strerror(-ret_code)); | |
ecc48a90 JD |
339 | ret = CMD_ERROR; |
340 | goto error; | |
341 | } | |
342 | ||
b178f53e JG |
343 | descriptor_status = lttng_session_descriptor_get_session_name( |
344 | session_descriptor, &created_session_name); | |
345 | if (descriptor_status != LTTNG_SESSION_DESCRIPTOR_STATUS_OK) { | |
346 | ERR("Failed to obtain created session name"); | |
347 | ret = CMD_ERROR; | |
348 | goto error; | |
16f6f820 | 349 | } |
b178f53e JG |
350 | |
351 | ret = lttng_list_sessions(&sessions); | |
f3ed775e | 352 | if (ret < 0) { |
b178f53e JG |
353 | ERR("Failed to fetch properties of created session: %s", |
354 | lttng_strerror(ret)); | |
355 | ret = CMD_ERROR; | |
356 | goto error; | |
357 | } | |
358 | for (i = 0; i < ret; i++) { | |
359 | if (!strcmp(created_session_name, sessions[i].name)) { | |
360 | created_session = &sessions[i]; | |
60e835ca | 361 | break; |
42224349 | 362 | } |
b178f53e JG |
363 | } |
364 | if (!created_session) { | |
365 | ERR("Failed to fetch properties of created session"); | |
366 | ret = CMD_ERROR; | |
f3ed775e DG |
367 | goto error; |
368 | } | |
369 | ||
b178f53e JG |
370 | if (opt_shm_path) { |
371 | char datetime_suffix[17] = {}; | |
372 | ||
373 | /* | |
374 | * An auto-generated session name already includes the creation | |
375 | * timestamp. | |
376 | */ | |
377 | if (opt_session_name) { | |
378 | uint64_t creation_time; | |
379 | struct tm *timeinfo; | |
380 | time_t creation_time_t; | |
381 | size_t strftime_ret; | |
382 | ||
383 | ret_code = lttng_session_get_creation_time( | |
384 | created_session, | |
385 | &creation_time); | |
386 | if (ret_code != LTTNG_OK) { | |
387 | ERR("%s", lttng_strerror(-ret_code)); | |
388 | ret = CMD_ERROR; | |
389 | goto error; | |
390 | } | |
391 | creation_time_t = (time_t) creation_time; | |
392 | timeinfo = localtime(&creation_time_t); | |
393 | if (!timeinfo) { | |
394 | PERROR("Failed to interpret session creation time"); | |
395 | ret = CMD_ERROR; | |
396 | goto error; | |
397 | } | |
398 | strftime_ret = strftime(datetime_suffix, | |
399 | sizeof(datetime_suffix), | |
400 | "-%Y%m%d-%H%M%S", timeinfo); | |
401 | if (strftime_ret == 0) { | |
402 | ERR("Failed to format session creation time."); | |
403 | ret = CMD_ERROR; | |
404 | goto error; | |
405 | } | |
a4b92340 | 406 | } |
4f50c803 | 407 | |
d7ba1388 | 408 | ret = snprintf(shm_path, sizeof(shm_path), |
b178f53e JG |
409 | "%s/%s%s", opt_shm_path, created_session_name, |
410 | datetime_suffix); | |
411 | if (ret < 0 || ret >= sizeof(shm_path)) { | |
412 | ERR("Failed to format the shared memory path."); | |
413 | ret = CMD_ERROR; | |
d7ba1388 MD |
414 | goto error; |
415 | } | |
b178f53e JG |
416 | ret = lttng_set_session_shm_path(created_session_name, |
417 | shm_path); | |
d7ba1388 | 418 | if (ret < 0) { |
b178f53e JG |
419 | lttng_destroy_session(created_session_name); |
420 | ret = CMD_ERROR; | |
d7ba1388 MD |
421 | goto error; |
422 | } | |
423 | } | |
424 | ||
b178f53e JG |
425 | if (opt_snapshot) { |
426 | MSG("Snapshot session %s created.", created_session_name); | |
427 | } else if (opt_live_timer) { | |
428 | MSG("Live session %s created.", created_session_name); | |
429 | } else { | |
430 | MSG("Session %s created.", created_session_name); | |
431 | } | |
432 | ||
433 | if (*created_session->path && !opt_snapshot) { | |
434 | MSG("Traces will be output to %s", created_session->path); | |
d73c5802 DG |
435 | |
436 | if (opt_live_timer) { | |
b178f53e JG |
437 | MSG("Live timer interval set to %u %s", opt_live_timer, |
438 | USEC_UNIT); | |
d73c5802 | 439 | } |
16f6f820 | 440 | } else if (opt_snapshot) { |
b178f53e JG |
441 | struct lttng_snapshot_output_list *list; |
442 | struct lttng_snapshot_output *iter; | |
443 | char snapshot_url[LTTNG_PATH_MAX] = {}; | |
444 | ||
445 | ret = lttng_snapshot_list_output(created_session_name, &list); | |
446 | if (ret < 0) { | |
447 | ERR("Failed to list snapshot outputs."); | |
448 | ret = CMD_ERROR; | |
449 | goto error; | |
16f6f820 | 450 | } |
b178f53e JG |
451 | |
452 | while ((iter = lttng_snapshot_output_list_get_next(list))) { | |
453 | const char *url = NULL; | |
454 | ||
455 | url = lttng_snapshot_output_get_ctrl_url( | |
456 | iter); | |
457 | ret = lttng_strncpy(snapshot_url, url, | |
458 | sizeof(snapshot_url)); | |
459 | if (ret) { | |
460 | snapshot_url[0] = '\0'; | |
461 | ERR("Failed to retrieve snapshot output destination"); | |
462 | } | |
463 | break; | |
464 | } | |
465 | lttng_snapshot_output_list_destroy(list); | |
466 | ||
467 | if (*snapshot_url) { | |
468 | MSG("Default snapshot output set to %s", | |
469 | snapshot_url); | |
470 | } | |
471 | MSG("Every channel enabled for this session will be set to mmap output and default to overwrite mode."); | |
a4b92340 | 472 | } |
d7ba1388 | 473 | if (opt_shm_path) { |
b178f53e | 474 | MSG("Shared memory path set to %s", shm_path); |
d7ba1388 | 475 | } |
a4b92340 | 476 | |
37d03ff7 JRJ |
477 | /* Mi output */ |
478 | if (lttng_opt_mi) { | |
b178f53e | 479 | ret = mi_created_session(created_session_name); |
37d03ff7 JRJ |
480 | if (ret) { |
481 | ret = CMD_ERROR; | |
482 | goto error; | |
483 | } | |
484 | } | |
485 | ||
58a97671 | 486 | /* Init lttng session config */ |
b178f53e | 487 | ret = config_init(created_session_name); |
f3ed775e | 488 | if (ret < 0) { |
27089920 | 489 | ret = CMD_ERROR; |
f3ed775e DG |
490 | goto error; |
491 | } | |
492 | ||
f3ed775e | 493 | ret = CMD_SUCCESS; |
f3ed775e | 494 | error: |
b178f53e JG |
495 | lttng_session_descriptor_destroy(session_descriptor); |
496 | free(sessions); | |
f3ed775e DG |
497 | return ret; |
498 | } | |
499 | ||
92360082 JG |
500 | /* |
501 | * spawn_sessiond | |
502 | * | |
503 | * Spawn a session daemon by forking and execv. | |
504 | */ | |
b53d4e59 | 505 | static int spawn_sessiond(const char *pathname) |
92360082 JG |
506 | { |
507 | int ret = 0; | |
508 | pid_t pid; | |
509 | ||
510 | MSG("Spawning a session daemon"); | |
92360082 JG |
511 | pid = fork(); |
512 | if (pid == 0) { | |
513 | /* | |
bbd44cae | 514 | * Spawn session daemon in daemon mode. |
92360082 | 515 | */ |
bbd44cae PP |
516 | execlp(pathname, "lttng-sessiond", |
517 | "--daemonize", NULL); | |
92360082 JG |
518 | /* execlp only returns if error happened */ |
519 | if (errno == ENOENT) { | |
520 | ERR("No session daemon found. Use --sessiond-path."); | |
521 | } else { | |
522 | PERROR("execlp"); | |
523 | } | |
524 | kill(getppid(), SIGTERM); /* wake parent */ | |
525 | exit(EXIT_FAILURE); | |
526 | } else if (pid > 0) { | |
92360082 | 527 | /* |
bbd44cae PP |
528 | * In daemon mode (--daemonize), sessiond only exits when |
529 | * it's ready to accept commands. | |
92360082 | 530 | */ |
bbd44cae | 531 | for (;;) { |
81527d36 JG |
532 | int status; |
533 | pid_t wait_pid_ret = waitpid(pid, &status, 0); | |
534 | ||
535 | if (wait_pid_ret < 0) { | |
536 | if (errno == EINTR) { | |
537 | continue; | |
538 | } | |
539 | PERROR("waitpid"); | |
540 | ret = -errno; | |
541 | goto end; | |
542 | } | |
bbd44cae PP |
543 | |
544 | if (WIFSIGNALED(status)) { | |
545 | ERR("Session daemon was killed by signal %d", | |
546 | WTERMSIG(status)); | |
547 | ret = -1; | |
548 | goto end; | |
549 | } else if (WIFEXITED(status)) { | |
550 | DBG("Session daemon terminated normally (exit status: %d)", | |
551 | WEXITSTATUS(status)); | |
552 | ||
553 | if (WEXITSTATUS(status) != 0) { | |
554 | ERR("Session daemon terminated with an error (exit status: %d)", | |
555 | WEXITSTATUS(status)); | |
556 | ret = -1; | |
557 | goto end; | |
558 | } | |
559 | break; | |
560 | } | |
92360082 | 561 | } |
bbd44cae | 562 | |
92360082 JG |
563 | goto end; |
564 | } else { | |
565 | PERROR("fork"); | |
566 | ret = -1; | |
567 | goto end; | |
568 | } | |
569 | ||
570 | end: | |
571 | return ret; | |
572 | } | |
573 | ||
574 | /* | |
575 | * launch_sessiond | |
576 | * | |
577 | * Check if the session daemon is available using | |
578 | * the liblttngctl API for the check. If not, try to | |
579 | * spawn a daemon. | |
580 | */ | |
581 | static int launch_sessiond(void) | |
582 | { | |
583 | int ret; | |
b53d4e59 | 584 | const char *pathname = NULL; |
92360082 JG |
585 | |
586 | ret = lttng_session_daemon_alive(); | |
587 | if (ret) { | |
588 | /* Sessiond is alive, not an error */ | |
589 | ret = 0; | |
590 | goto end; | |
591 | } | |
592 | ||
593 | /* Try command line option path */ | |
594 | pathname = opt_sessiond_path; | |
595 | ||
596 | /* Try LTTNG_SESSIOND_PATH env variable */ | |
597 | if (pathname == NULL) { | |
598 | pathname = getenv(DEFAULT_SESSIOND_PATH_ENV); | |
599 | } | |
600 | ||
601 | /* Try with configured path */ | |
602 | if (pathname == NULL) { | |
603 | if (CONFIG_SESSIOND_BIN[0] != '\0') { | |
604 | pathname = CONFIG_SESSIOND_BIN; | |
605 | } | |
606 | } | |
607 | ||
608 | /* Try the default path */ | |
609 | if (pathname == NULL) { | |
610 | pathname = INSTALL_BIN_PATH "/lttng-sessiond"; | |
611 | } | |
612 | ||
613 | DBG("Session daemon binary path: %s", pathname); | |
614 | ||
615 | /* Check existence and permissions */ | |
616 | ret = access(pathname, F_OK | X_OK); | |
617 | if (ret < 0) { | |
618 | ERR("No such file or access denied: %s", pathname); | |
619 | goto end; | |
620 | } | |
621 | ||
622 | ret = spawn_sessiond(pathname); | |
92360082 | 623 | end: |
0f4fa0d2 JG |
624 | if (ret) { |
625 | ERR("Problem occurred while launching session daemon (%s)", | |
626 | pathname); | |
627 | } | |
92360082 JG |
628 | return ret; |
629 | } | |
630 | ||
7b3f7be2 | 631 | static |
a8d119b5 JG |
632 | int validate_url_option_combination(void) |
633 | { | |
634 | int ret = 0; | |
635 | int used_count = 0; | |
636 | ||
637 | used_count += !!opt_url; | |
638 | used_count += !!opt_output_path; | |
639 | used_count += (opt_data_url || opt_ctrl_url); | |
640 | if (used_count > 1) { | |
641 | ERR("Only one of the --set-url, --ctrl-url/data-url, or --output options may be used at once."); | |
642 | ret = -1; | |
643 | } | |
644 | ||
645 | return ret; | |
646 | } | |
647 | ||
f3ed775e | 648 | /* |
74cc1d0f | 649 | * The 'create <options>' first level command |
1c8d13c8 TD |
650 | * |
651 | * Returns one of the CMD_* result constants. | |
f3ed775e DG |
652 | */ |
653 | int cmd_create(int argc, const char **argv) | |
654 | { | |
37d03ff7 | 655 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
ecc48a90 | 656 | char *opt_arg = NULL; |
68c7f6e5 | 657 | const char *leftover = NULL; |
f3ed775e DG |
658 | static poptContext pc; |
659 | ||
660 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
661 | poptReadDefaultConfig(pc, 0); | |
662 | ||
663 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
664 | switch (opt) { | |
665 | case OPT_HELP: | |
4ba92f18 | 666 | SHOW_HELP(); |
f3ed775e | 667 | goto end; |
679b4943 SM |
668 | case OPT_LIST_OPTIONS: |
669 | list_cmd_options(stdout, long_options); | |
679b4943 | 670 | goto end; |
ecc48a90 JD |
671 | case OPT_LIVE_TIMER: |
672 | { | |
c7219617 | 673 | uint64_t v; |
ecc48a90 JD |
674 | |
675 | errno = 0; | |
676 | opt_arg = poptGetOptArg(pc); | |
d73c5802 DG |
677 | if (!opt_arg) { |
678 | /* Set up default values. */ | |
679 | opt_live_timer = (uint32_t) DEFAULT_LTTNG_LIVE_TIMER; | |
680 | DBG("Session live timer interval set to default value %d", | |
681 | opt_live_timer); | |
682 | break; | |
683 | } | |
684 | ||
c7219617 SM |
685 | if (utils_parse_time_suffix(opt_arg, &v) < 0) { |
686 | ERR("Wrong value for --live parameter: %s", opt_arg); | |
ecc48a90 JD |
687 | ret = CMD_ERROR; |
688 | goto end; | |
689 | } | |
c7219617 | 690 | |
ecc48a90 JD |
691 | if (v != (uint32_t) v) { |
692 | ERR("32-bit overflow in --live parameter: %s", opt_arg); | |
693 | ret = CMD_ERROR; | |
694 | goto end; | |
695 | } | |
c7219617 | 696 | |
0ed9e0be JG |
697 | if (v == 0) { |
698 | ERR("Live timer interval must be greater than zero"); | |
699 | ret = CMD_ERROR; | |
700 | goto end; | |
701 | } | |
c7219617 | 702 | |
ecc48a90 JD |
703 | opt_live_timer = (uint32_t) v; |
704 | DBG("Session live timer interval set to %d", opt_live_timer); | |
705 | break; | |
706 | } | |
f3ed775e | 707 | default: |
f3ed775e DG |
708 | ret = CMD_UNDEFINED; |
709 | goto end; | |
710 | } | |
711 | } | |
712 | ||
785d2d0d | 713 | if (opt_no_consumer) { |
96fe6b8d | 714 | MSG("The option --no-consumer is obsolete. Use --no-output now."); |
785d2d0d DG |
715 | ret = CMD_WARNING; |
716 | goto end; | |
717 | } | |
718 | ||
a8d119b5 JG |
719 | ret = validate_url_option_combination(); |
720 | if (ret) { | |
721 | ret = CMD_ERROR; | |
722 | goto end; | |
723 | } | |
724 | ||
92360082 JG |
725 | /* Spawn a session daemon if needed */ |
726 | if (!opt_no_sessiond) { | |
727 | ret = launch_sessiond(); | |
728 | if (ret) { | |
729 | ret = CMD_ERROR; | |
730 | goto end; | |
731 | } | |
732 | } | |
733 | ||
acc09215 | 734 | /* MI initialization */ |
37d03ff7 JRJ |
735 | if (lttng_opt_mi) { |
736 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); | |
737 | if (!writer) { | |
738 | ret = -LTTNG_ERR_NOMEM; | |
739 | goto end; | |
740 | } | |
741 | ||
742 | /* Open command element */ | |
743 | ret = mi_lttng_writer_command_open(writer, | |
744 | mi_lttng_element_command_create); | |
745 | if (ret) { | |
746 | ret = CMD_ERROR; | |
747 | goto end; | |
748 | } | |
749 | ||
750 | /* Open output element */ | |
751 | ret = mi_lttng_writer_open_element(writer, | |
752 | mi_lttng_element_command_output); | |
753 | if (ret) { | |
754 | ret = CMD_ERROR; | |
755 | goto end; | |
756 | } | |
757 | } | |
f3ed775e DG |
758 | opt_session_name = (char*) poptGetArg(pc); |
759 | ||
68c7f6e5 JD |
760 | leftover = poptGetArg(pc); |
761 | if (leftover) { | |
762 | ERR("Unknown argument: %s", leftover); | |
763 | ret = CMD_ERROR; | |
764 | goto end; | |
765 | } | |
766 | ||
37d03ff7 JRJ |
767 | command_ret = create_session(); |
768 | if (command_ret) { | |
769 | success = 0; | |
770 | } | |
771 | ||
772 | if (lttng_opt_mi) { | |
773 | /* Close output element */ | |
774 | ret = mi_lttng_writer_close_element(writer); | |
775 | if (ret) { | |
776 | ret = CMD_ERROR; | |
777 | goto end; | |
778 | } | |
779 | ||
780 | /* Success ? */ | |
781 | ret = mi_lttng_writer_write_element_bool(writer, | |
782 | mi_lttng_element_command_success, success); | |
783 | if (ret) { | |
784 | ret = CMD_ERROR; | |
785 | goto end; | |
786 | } | |
787 | ||
788 | /* Command element close */ | |
789 | ret = mi_lttng_writer_command_close(writer); | |
790 | if (ret) { | |
791 | ret = CMD_ERROR; | |
792 | goto end; | |
793 | } | |
794 | } | |
f3ed775e DG |
795 | |
796 | end: | |
37d03ff7 JRJ |
797 | /* Mi clean-up */ |
798 | if (writer && mi_lttng_writer_destroy(writer)) { | |
799 | /* Preserve original error code */ | |
800 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; | |
801 | } | |
802 | ||
acc09215 | 803 | /* Overwrite ret if an error occurred in create_session() */ |
37d03ff7 JRJ |
804 | ret = command_ret ? command_ret : ret; |
805 | ||
ca1c3607 | 806 | poptFreeContext(pc); |
f3ed775e DG |
807 | return ret; |
808 | } |