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