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