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