Fix: lttng view.c missing strdup OOM check
[lttng-tools.git] / src / bin / lttng / commands / view.c
1 /*
2 * Copyright (C) 2011 - David Goulet <dgoulet@efficios.com>
3 *
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.
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 *
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.
16 */
17
18 #define _GNU_SOURCE
19 #define _LGPL_SOURCE
20 #include <popt.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27
28 #include "../command.h"
29 #include <config.h>
30
31 static char *opt_session_name;
32 static char *opt_viewer;
33 static char *opt_trace_path;
34 static const char *babeltrace_bin = CONFIG_BABELTRACE_BIN;
35 //static const char *lttv_gui_bin = CONFIG_LTTV_GUI_BIN;
36
37 enum {
38 OPT_HELP = 1,
39 OPT_LIST_OPTIONS,
40 };
41
42 static struct poptOption long_options[] = {
43 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
44 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
45 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
46 {"viewer", 'e', POPT_ARG_STRING, &opt_viewer, 0, 0, 0},
47 {"trace-path", 't', POPT_ARG_STRING, &opt_trace_path, 0, 0, 0},
48 {0, 0, 0, 0, 0, 0, 0}
49 };
50
51 /*
52 * This is needed for each viewer since we are using execvp().
53 */
54 static const char *babeltrace_opts[] = { "babeltrace" };
55 //static const char *lttv_gui_opts[] = { "lttv-gui", "-t", };
56
57 /*
58 * Type is also use as the index in the viewers array. So please, make sure
59 * your enum value is in the right order in the array below.
60 */
61 enum viewer_type {
62 VIEWER_BABELTRACE = 0,
63 VIEWER_LTTV_GUI = 1,
64 VIEWER_USER_DEFINED = 2,
65 };
66
67 /*
68 * NOTE: "lttv" is a shell command and it's not working for exec() family
69 * functions so we might think of removing this wrapper or using bash.
70 */
71 static struct viewers {
72 const char *exec_name;
73 enum viewer_type type;
74 } viewers[] = {
75 { "babeltrace", VIEWER_BABELTRACE },
76 { "lttv-gui", VIEWER_LTTV_GUI },
77 { NULL, VIEWER_USER_DEFINED },
78 };
79
80 /* Is the session we are trying to view is in live mode. */
81 static int session_live_mode;
82
83 /*
84 * usage
85 */
86 static void usage(FILE *ofp)
87 {
88 fprintf(ofp, "usage: lttng view [SESSION_NAME] [OPTIONS]\n");
89 fprintf(ofp, "\n");
90 fprintf(ofp, "By default, the babeltrace viewer will be used for text viewing\n");
91 fprintf(ofp, "\n");
92 fprintf(ofp, "Where SESSION_NAME is an optional session name. If not specified, lttng will\n");
93 fprintf(ofp, "get it from the configuration file (.lttngrc).\n");
94 fprintf(ofp, "\n");
95 fprintf(ofp, "Options:\n");
96 fprintf(ofp, " -h, --help Show this help\n");
97 fprintf(ofp, " --list-options Simple listing of options\n");
98 fprintf(ofp, " -t, --trace-path PATH Trace directory path for the viewer\n");
99 fprintf(ofp, " -e, --viewer CMD Specify viewer and/or options to use\n");
100 fprintf(ofp, " This will completely override the default viewers so\n");
101 fprintf(ofp, " please make sure to specify the full command. The trace\n");
102 fprintf(ofp, " directory path of the session will be appended at the end\n");
103 fprintf(ofp, " to the arguments\n");
104 fprintf(ofp, "\n");
105 }
106
107 static struct viewers *parse_options(void)
108 {
109 if (opt_viewer == NULL) {
110 /* Default is babeltrace */
111 return &(viewers[VIEWER_BABELTRACE]);
112 }
113
114 #if 0
115 if (strstr(opt_viewer, viewers[VIEWER_LTTV_GUI].exec_name) == 0) {
116 return &(viewers[VIEWER_LTTV_GUI]);
117 }
118 #endif
119
120 /*
121 * This means that if -e, --viewers is used, we just override everything
122 * with it. For supported viewers like lttv, we could simply detect if "-t"
123 * is passed and if not, add the trace directory to it.
124 */
125 return &(viewers[VIEWER_USER_DEFINED]);
126 }
127
128 /*
129 * Alloc an array of string pointer from a simple string having all options
130 * seperated by spaces. Also adds the trace path to the arguments.
131 *
132 * The returning pointer is ready to be passed to execvp().
133 */
134 static char **alloc_argv_from_user_opts(char *opts, const char *trace_path)
135 {
136 int i = 0, ignore_space = 0;
137 unsigned int num_opts = 1;
138 char **argv, *token = opts;
139
140 /* Count number of arguments. */
141 do {
142 if (*token == ' ') {
143 /* Use to ignore consecutive spaces */
144 if (!ignore_space) {
145 num_opts++;
146 }
147 ignore_space = 1;
148 } else {
149 ignore_space = 0;
150 }
151 token++;
152 } while (*token != '\0');
153
154 /* Add two here for the NULL terminating element and trace path */
155 argv = zmalloc(sizeof(char *) * (num_opts + 2));
156 if (argv == NULL) {
157 goto error;
158 }
159
160 token = strtok(opts, " ");
161 while (token != NULL) {
162 argv[i] = strdup(token);
163 if (argv[i] == NULL) {
164 goto error;
165 }
166 token = strtok(NULL, " ");
167 i++;
168 }
169
170 argv[num_opts] = (char *) trace_path;
171 argv[num_opts + 1] = NULL;
172
173 return argv;
174
175 error:
176 return NULL;
177 }
178
179 /*
180 * Alloc an array of string pointer from an array of strings. It also adds
181 * the trace path to the argv.
182 *
183 * The returning pointer is ready to be passed to execvp().
184 */
185 static char **alloc_argv_from_local_opts(const char **opts, size_t opts_len,
186 const char *trace_path)
187 {
188 char **argv;
189 size_t size, mem_len;
190
191
192 /* Add one for the NULL terminating element. */
193 mem_len = opts_len + 1;
194 if (session_live_mode) {
195 /* Add 3 option for the live mode being "-i lttng-live URL". */
196 mem_len += 3;
197 } else {
198 /* Add option for the trace path. */
199 mem_len += 1;
200 }
201
202 size = sizeof(char *) * mem_len;
203
204 /* Add two here for the trace_path and the NULL terminating element. */
205 argv = zmalloc(size);
206 if (argv == NULL) {
207 goto error;
208 }
209
210 memcpy(argv, opts, size);
211
212 if (session_live_mode) {
213 argv[opts_len] = "-i";
214 argv[opts_len + 1] = "lttng-live";
215 argv[opts_len + 2] = (char *) trace_path;
216 argv[opts_len + 3] = NULL;
217 } else {
218 argv[opts_len] = (char *) trace_path;
219 argv[opts_len + 1] = NULL;
220 }
221
222 error:
223 return argv;
224 }
225
226 /*
227 * Spawn viewer with the trace directory path.
228 */
229 static int spawn_viewer(const char *trace_path)
230 {
231 int ret = 0;
232 struct stat status;
233 const char *viewer_bin = NULL;
234 struct viewers *viewer;
235 char **argv = NULL;
236
237 /* Check for --viewer options */
238 viewer = parse_options();
239 if (viewer == NULL) {
240 ret = CMD_ERROR;
241 goto error;
242 }
243
244 switch (viewer->type) {
245 case VIEWER_BABELTRACE:
246 if (stat(babeltrace_bin, &status) == 0) {
247 viewer_bin = babeltrace_bin;
248 } else {
249 viewer_bin = viewer->exec_name;
250 }
251 argv = alloc_argv_from_local_opts(babeltrace_opts,
252 ARRAY_SIZE(babeltrace_opts), trace_path);
253 break;
254 #if 0
255 case VIEWER_LTTV_GUI:
256 if (stat(lttv_gui_bin, &status) == 0) {
257 viewer_bin = lttv_gui_bin;
258 } else {
259 viewer_bin = viewer->exec_name;
260 }
261 argv = alloc_argv_from_local_opts(lttv_gui_opts,
262 ARRAY_SIZE(lttv_gui_opts), trace_path);
263 break;
264 #endif
265 case VIEWER_USER_DEFINED:
266 argv = alloc_argv_from_user_opts(opt_viewer, trace_path);
267 if (argv) {
268 viewer_bin = argv[0];
269 }
270 break;
271 default:
272 viewer_bin = viewers[VIEWER_BABELTRACE].exec_name;
273 argv = alloc_argv_from_local_opts(babeltrace_opts,
274 ARRAY_SIZE(babeltrace_opts), trace_path);
275 break;
276 }
277
278 if (argv == NULL) {
279 ret = CMD_FATAL;
280 goto error;
281 }
282
283 DBG("Using %s viewer", viewer_bin);
284
285 ret = execvp(viewer_bin, argv);
286 if (ret) {
287 if (errno == ENOENT) {
288 ERR("%s not found on the system", viewer_bin);
289 } else {
290 PERROR("exec: %s", viewer_bin);
291 }
292 free(argv);
293 ret = CMD_FATAL;
294 goto error;
295 }
296
297 error:
298 return ret;
299 }
300
301 /*
302 * Build the live path we need for the lttng live view.
303 */
304 static char *build_live_path(char *session_name)
305 {
306 int ret;
307 char *path = NULL;
308 char hostname[HOST_NAME_MAX];
309
310 ret = gethostname(hostname, sizeof(hostname));
311 if (ret < 0) {
312 perror("gethostname");
313 goto error;
314 }
315
316 ret = asprintf(&path, "net://localhost/host/%s/%s", hostname,
317 session_name);
318 if (ret < 0) {
319 perror("asprintf live path");
320 goto error;
321 }
322
323 error:
324 return path;
325 }
326
327 /*
328 * Exec viewer if found and use session name path.
329 */
330 static int view_trace(void)
331 {
332 int ret;
333 char *session_name, *trace_path = NULL;
334 struct lttng_session *sessions = NULL;
335
336 /*
337 * Safety net. If lttng is suid at some point for *any* useless reasons,
338 * this prevent any bad execution of binaries.
339 */
340 if (getuid() != 0) {
341 if (getuid() != geteuid()) {
342 ERR("UID does not match effective UID.");
343 ret = CMD_ERROR;
344 goto error;
345 } else if (getgid() != getegid()) {
346 ERR("GID does not match effective GID.");
347 ret = CMD_ERROR;
348 goto error;
349 }
350 }
351
352 /* User define trace path override the session name */
353 if (opt_trace_path) {
354 session_name = NULL;
355 } else if(opt_session_name == NULL) {
356 session_name = get_session_name();
357 if (session_name == NULL) {
358 ret = CMD_ERROR;
359 goto error;
360 }
361 } else {
362 session_name = opt_session_name;
363 }
364
365 DBG("Viewing trace for session %s", session_name);
366
367 if (session_name) {
368 int i, count, found = 0;
369
370 /* Getting all sessions */
371 count = lttng_list_sessions(&sessions);
372 if (count < 0) {
373 ERR("Unable to list sessions. Session name %s not found.",
374 session_name);
375 MSG("Is there a session daemon running?");
376 ret = CMD_ERROR;
377 goto free_error;
378 }
379
380 /* Find our session listed by the session daemon */
381 for (i = 0; i < count; i++) {
382 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
383 found = 1;
384 break;
385 }
386 }
387
388 if (!found) {
389 MSG("Session name %s not found", session_name);
390 ret = CMD_ERROR;
391 goto free_sessions;
392 }
393
394 session_live_mode = sessions[i].live_timer_interval;
395
396 DBG("Session live mode set to %d", session_live_mode);
397
398 if (sessions[i].enabled && !session_live_mode) {
399 WARN("Session %s is running. Please stop it before reading it.",
400 session_name);
401 ret = CMD_ERROR;
402 goto free_sessions;
403 }
404
405 /* If the timer interval is set we are in live mode. */
406 if (session_live_mode) {
407 trace_path = build_live_path(session_name);
408 if (!trace_path) {
409 ret = CMD_ERROR;
410 goto free_sessions;
411 }
412 } else {
413 /* Get file system session path. */
414 trace_path = sessions[i].path;
415 }
416 } else {
417 trace_path = opt_trace_path;
418 }
419
420 MSG("Trace directory: %s\n", trace_path);
421
422 ret = spawn_viewer(trace_path);
423 if (ret < 0) {
424 /* Don't set ret so lttng can interpret the sessiond error. */
425 goto free_sessions;
426 }
427
428 free_sessions:
429 if (session_live_mode) {
430 free(trace_path);
431 }
432 free(sessions);
433 free_error:
434 if (opt_session_name == NULL) {
435 free(session_name);
436 }
437 error:
438 return ret;
439 }
440
441 /*
442 * The 'view <options>' first level command
443 */
444 int cmd_view(int argc, const char **argv)
445 {
446 int opt, ret = CMD_SUCCESS;
447 static poptContext pc;
448
449 pc = poptGetContext(NULL, argc, argv, long_options, 0);
450 poptReadDefaultConfig(pc, 0);
451
452 if (lttng_opt_mi) {
453 WARN("mi does not apply to view command");
454 }
455
456 while ((opt = poptGetNextOpt(pc)) != -1) {
457 switch (opt) {
458 case OPT_HELP:
459 usage(stdout);
460 goto end;
461 case OPT_LIST_OPTIONS:
462 list_cmd_options(stdout, long_options);
463 goto end;
464 default:
465 usage(stderr);
466 ret = CMD_UNDEFINED;
467 goto end;
468 }
469 }
470
471 opt_session_name = (char*) poptGetArg(pc);
472
473 ret = view_trace();
474
475 end:
476 poptFreeContext(pc);
477 return ret;
478 }
This page took 0.053839 seconds and 5 git commands to generate.