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