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