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) { |
27089920 | 482 | ret = CMD_ERROR; |
f3ed775e DG |
483 | goto error; |
484 | } | |
485 | ||
f3ed775e | 486 | ret = CMD_SUCCESS; |
f3ed775e | 487 | error: |
b178f53e JG |
488 | lttng_session_descriptor_destroy(session_descriptor); |
489 | free(sessions); | |
f3ed775e DG |
490 | return ret; |
491 | } | |
492 | ||
92360082 JG |
493 | /* |
494 | * spawn_sessiond | |
495 | * | |
496 | * Spawn a session daemon by forking and execv. | |
497 | */ | |
b53d4e59 | 498 | static int spawn_sessiond(const char *pathname) |
92360082 JG |
499 | { |
500 | int ret = 0; | |
501 | pid_t pid; | |
502 | ||
503 | MSG("Spawning a session daemon"); | |
92360082 JG |
504 | pid = fork(); |
505 | if (pid == 0) { | |
506 | /* | |
bbd44cae | 507 | * Spawn session daemon in daemon mode. |
92360082 | 508 | */ |
28ab034a | 509 | execlp(pathname, "lttng-sessiond", "--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 | } | |
28ab034a | 516 | kill(getppid(), SIGTERM); /* wake parent */ |
92360082 JG |
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)) { | |
28ab034a | 537 | ERR("Session daemon was killed by signal %d", WTERMSIG(status)); |
bbd44cae | 538 | ret = -1; |
28ab034a | 539 | goto end; |
bbd44cae PP |
540 | } else if (WIFEXITED(status)) { |
541 | DBG("Session daemon terminated normally (exit status: %d)", | |
28ab034a | 542 | WEXITSTATUS(status)); |
bbd44cae PP |
543 | |
544 | if (WEXITSTATUS(status) != 0) { | |
545 | ERR("Session daemon terminated with an error (exit status: %d)", | |
28ab034a | 546 | WEXITSTATUS(status)); |
bbd44cae | 547 | ret = -1; |
28ab034a | 548 | goto end; |
bbd44cae PP |
549 | } |
550 | break; | |
551 | } | |
92360082 | 552 | } |
bbd44cae | 553 | |
92360082 JG |
554 | goto end; |
555 | } else { | |
556 | PERROR("fork"); | |
557 | ret = -1; | |
558 | goto end; | |
559 | } | |
560 | ||
561 | end: | |
562 | return ret; | |
563 | } | |
564 | ||
565 | /* | |
566 | * launch_sessiond | |
567 | * | |
568 | * Check if the session daemon is available using | |
569 | * the liblttngctl API for the check. If not, try to | |
570 | * spawn a daemon. | |
571 | */ | |
cd9adb8b | 572 | static int launch_sessiond() |
92360082 JG |
573 | { |
574 | int ret; | |
cd9adb8b | 575 | const char *pathname = nullptr; |
92360082 JG |
576 | |
577 | ret = lttng_session_daemon_alive(); | |
578 | if (ret) { | |
579 | /* Sessiond is alive, not an error */ | |
580 | ret = 0; | |
581 | goto end; | |
582 | } | |
583 | ||
584 | /* Try command line option path */ | |
585 | pathname = opt_sessiond_path; | |
586 | ||
587 | /* Try LTTNG_SESSIOND_PATH env variable */ | |
cd9adb8b | 588 | if (pathname == nullptr) { |
92360082 JG |
589 | pathname = getenv(DEFAULT_SESSIOND_PATH_ENV); |
590 | } | |
591 | ||
592 | /* Try with configured path */ | |
cd9adb8b | 593 | if (pathname == nullptr) { |
92360082 JG |
594 | if (CONFIG_SESSIOND_BIN[0] != '\0') { |
595 | pathname = CONFIG_SESSIOND_BIN; | |
596 | } | |
597 | } | |
598 | ||
599 | /* Try the default path */ | |
cd9adb8b | 600 | if (pathname == nullptr) { |
92360082 JG |
601 | pathname = INSTALL_BIN_PATH "/lttng-sessiond"; |
602 | } | |
603 | ||
604 | DBG("Session daemon binary path: %s", pathname); | |
605 | ||
606 | /* Check existence and permissions */ | |
607 | ret = access(pathname, F_OK | X_OK); | |
608 | if (ret < 0) { | |
609 | ERR("No such file or access denied: %s", pathname); | |
610 | goto end; | |
611 | } | |
612 | ||
613 | ret = spawn_sessiond(pathname); | |
92360082 | 614 | end: |
0f4fa0d2 | 615 | if (ret) { |
28ab034a | 616 | ERR("Problem occurred while launching session daemon (%s)", pathname); |
0f4fa0d2 | 617 | } |
92360082 JG |
618 | return ret; |
619 | } | |
620 | ||
cd9adb8b | 621 | static int validate_url_option_combination() |
a8d119b5 JG |
622 | { |
623 | int ret = 0; | |
624 | int used_count = 0; | |
625 | ||
626 | used_count += !!opt_url; | |
627 | used_count += !!opt_output_path; | |
628 | used_count += (opt_data_url || opt_ctrl_url); | |
629 | if (used_count > 1) { | |
630 | ERR("Only one of the --set-url, --ctrl-url/data-url, or --output options may be used at once."); | |
631 | ret = -1; | |
632 | } | |
633 | ||
634 | return ret; | |
635 | } | |
636 | ||
f3ed775e | 637 | /* |
74cc1d0f | 638 | * The 'create <options>' first level command |
1c8d13c8 TD |
639 | * |
640 | * Returns one of the CMD_* result constants. | |
f3ed775e DG |
641 | */ |
642 | int cmd_create(int argc, const char **argv) | |
643 | { | |
37d03ff7 | 644 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
cd9adb8b JG |
645 | char *opt_arg = nullptr; |
646 | const char *arg_session_name = nullptr; | |
647 | const char *leftover = nullptr; | |
f3ed775e DG |
648 | static poptContext pc; |
649 | ||
cd9adb8b | 650 | pc = poptGetContext(nullptr, argc, argv, long_options, 0); |
f3ed775e DG |
651 | poptReadDefaultConfig(pc, 0); |
652 | ||
653 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
654 | switch (opt) { | |
655 | case OPT_HELP: | |
4ba92f18 | 656 | SHOW_HELP(); |
f3ed775e | 657 | goto end; |
679b4943 SM |
658 | case OPT_LIST_OPTIONS: |
659 | list_cmd_options(stdout, long_options); | |
679b4943 | 660 | goto end; |
ecc48a90 JD |
661 | case OPT_LIVE_TIMER: |
662 | { | |
c7219617 | 663 | uint64_t v; |
ecc48a90 JD |
664 | |
665 | errno = 0; | |
e2ecf532 JG |
666 | if (opt_arg) { |
667 | free(opt_arg); | |
668 | opt_arg = nullptr; | |
669 | } | |
670 | ||
ecc48a90 | 671 | opt_arg = poptGetOptArg(pc); |
d73c5802 DG |
672 | if (!opt_arg) { |
673 | /* Set up default values. */ | |
674 | opt_live_timer = (uint32_t) DEFAULT_LTTNG_LIVE_TIMER; | |
675 | DBG("Session live timer interval set to default value %d", | |
28ab034a | 676 | opt_live_timer); |
d73c5802 DG |
677 | break; |
678 | } | |
679 | ||
c7219617 SM |
680 | if (utils_parse_time_suffix(opt_arg, &v) < 0) { |
681 | ERR("Wrong value for --live parameter: %s", opt_arg); | |
ecc48a90 JD |
682 | ret = CMD_ERROR; |
683 | goto end; | |
684 | } | |
c7219617 | 685 | |
ecc48a90 JD |
686 | if (v != (uint32_t) v) { |
687 | ERR("32-bit overflow in --live parameter: %s", opt_arg); | |
688 | ret = CMD_ERROR; | |
689 | goto end; | |
690 | } | |
c7219617 | 691 | |
0ed9e0be JG |
692 | if (v == 0) { |
693 | ERR("Live timer interval must be greater than zero"); | |
694 | ret = CMD_ERROR; | |
695 | goto end; | |
696 | } | |
c7219617 | 697 | |
ecc48a90 JD |
698 | opt_live_timer = (uint32_t) v; |
699 | DBG("Session live timer interval set to %d", opt_live_timer); | |
700 | break; | |
701 | } | |
f3ed775e | 702 | default: |
f3ed775e DG |
703 | ret = CMD_UNDEFINED; |
704 | goto end; | |
705 | } | |
706 | } | |
707 | ||
785d2d0d | 708 | if (opt_no_consumer) { |
96fe6b8d | 709 | MSG("The option --no-consumer is obsolete. Use --no-output now."); |
785d2d0d DG |
710 | ret = CMD_WARNING; |
711 | goto end; | |
712 | } | |
713 | ||
a8d119b5 JG |
714 | ret = validate_url_option_combination(); |
715 | if (ret) { | |
716 | ret = CMD_ERROR; | |
717 | goto end; | |
718 | } | |
719 | ||
92360082 JG |
720 | /* Spawn a session daemon if needed */ |
721 | if (!opt_no_sessiond) { | |
722 | ret = launch_sessiond(); | |
723 | if (ret) { | |
724 | ret = CMD_ERROR; | |
725 | goto end; | |
726 | } | |
727 | } | |
728 | ||
acc09215 | 729 | /* MI initialization */ |
37d03ff7 JRJ |
730 | if (lttng_opt_mi) { |
731 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); | |
732 | if (!writer) { | |
733 | ret = -LTTNG_ERR_NOMEM; | |
734 | goto end; | |
735 | } | |
736 | ||
737 | /* Open command element */ | |
28ab034a | 738 | ret = mi_lttng_writer_command_open(writer, mi_lttng_element_command_create); |
37d03ff7 JRJ |
739 | if (ret) { |
740 | ret = CMD_ERROR; | |
741 | goto end; | |
742 | } | |
743 | ||
744 | /* Open output element */ | |
28ab034a | 745 | ret = mi_lttng_writer_open_element(writer, mi_lttng_element_command_output); |
37d03ff7 JRJ |
746 | if (ret) { |
747 | ret = CMD_ERROR; | |
748 | goto end; | |
749 | } | |
750 | } | |
5b915816 MJ |
751 | |
752 | /* Get the optional session name argument. */ | |
753 | arg_session_name = poptGetArg(pc); | |
f3ed775e | 754 | |
68c7f6e5 JD |
755 | leftover = poptGetArg(pc); |
756 | if (leftover) { | |
757 | ERR("Unknown argument: %s", leftover); | |
758 | ret = CMD_ERROR; | |
759 | goto end; | |
760 | } | |
761 | ||
5b915816 | 762 | command_ret = create_session(arg_session_name); |
37d03ff7 JRJ |
763 | if (command_ret) { |
764 | success = 0; | |
765 | } | |
766 | ||
767 | if (lttng_opt_mi) { | |
768 | /* Close output element */ | |
769 | ret = mi_lttng_writer_close_element(writer); | |
770 | if (ret) { | |
771 | ret = CMD_ERROR; | |
772 | goto end; | |
773 | } | |
774 | ||
775 | /* Success ? */ | |
28ab034a JG |
776 | ret = mi_lttng_writer_write_element_bool( |
777 | writer, mi_lttng_element_command_success, success); | |
37d03ff7 JRJ |
778 | if (ret) { |
779 | ret = CMD_ERROR; | |
780 | goto end; | |
781 | } | |
782 | ||
783 | /* Command element close */ | |
784 | ret = mi_lttng_writer_command_close(writer); | |
785 | if (ret) { | |
786 | ret = CMD_ERROR; | |
787 | goto end; | |
788 | } | |
789 | } | |
f3ed775e DG |
790 | |
791 | end: | |
37d03ff7 JRJ |
792 | /* Mi clean-up */ |
793 | if (writer && mi_lttng_writer_destroy(writer)) { | |
794 | /* Preserve original error code */ | |
795 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; | |
796 | } | |
797 | ||
acc09215 | 798 | /* Overwrite ret if an error occurred in create_session() */ |
37d03ff7 JRJ |
799 | ret = command_ret ? command_ret : ret; |
800 | ||
e2ecf532 | 801 | free(opt_arg); |
ca1c3607 | 802 | poptFreeContext(pc); |
f3ed775e DG |
803 | return ret; |
804 | } |