Remove fork() for the lttng view spawn viewer
[lttng-tools.git] / src / bin / lttng / commands / view.c
CommitLineData
0c95f5b2
DG
1/*
2 * Copyright (C) 2011 - David Goulet <dgoulet@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; only version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307, USA.
16 */
17
18#define _GNU_SOURCE
19#include <popt.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <unistd.h>
26
27#include "../command.h"
28#include <config.h>
29
30static char *opt_session_name;
31static char *opt_viewer;
32static const char *babeltrace_bin = CONFIG_BABELTRACE_BIN;
33//static const char *lttv_gui_bin = CONFIG_LTTV_GUI_BIN;
34
35enum {
36 OPT_HELP = 1,
37 OPT_LIST_OPTIONS,
38};
39
40static struct poptOption long_options[] = {
41 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
42 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
43 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
44 {"viewer", 'e', POPT_ARG_STRING, &opt_viewer, 0, 0, 0},
45 {0, 0, 0, 0, 0, 0, 0}
46};
47
48/*
49 * This is needed for each viewer since we are using execvp().
50 */
36ef520f 51static const char *babeltrace_opts[] = { "babeltrace" };
0c95f5b2
DG
52//static const char *lttv_gui_opts[] = { "lttv-gui", "-t", };
53
54/*
55 * Type is also use as the index in the viewers array. So please, make sure
56 * your enum value is in the right order in the array below.
57 */
58enum viewer_type {
59 VIEWER_BABELTRACE = 0,
60 VIEWER_LTTV_GUI = 1,
61 VIEWER_USER_DEFINED = 2,
62};
63
64/*
65 * NOTE: "lttv" is a shell command and it's not working for exec() family
66 * functions so we might think of removing this wrapper or using bash.
67 */
68static struct viewers {
69 const char *exec_name;
70 enum viewer_type type;
71} viewers[] = {
72 { "babeltrace", VIEWER_BABELTRACE },
73 { "lttv-gui", VIEWER_LTTV_GUI },
74 { NULL, VIEWER_USER_DEFINED },
75};
76
77/*
78 * usage
79 */
80static void usage(FILE *ofp)
81{
82 fprintf(ofp, "usage: lttng view [SESSION_NAME] [OPTIONS]\n");
83 fprintf(ofp, "\n");
84 fprintf(ofp, "By default, the babeltrace viewer will be used for text viewing\n");
85 fprintf(ofp, "\n");
86 fprintf(ofp, "Where SESSION_NAME is an optional session name. If not specified, lttng will\n");
87 fprintf(ofp, "get it from the configuration file (.lttngrc).\n");
88 fprintf(ofp, "\n");
89 fprintf(ofp, " -h, --help Show this help\n");
90 fprintf(ofp, " --list-options Simple listing of options\n");
91 fprintf(ofp, " -e, --viewer CMD Specify viewer and/or options to use\n");
92 fprintf(ofp, " This will completely override the default viewers so\n");
93 fprintf(ofp, " please make sure to specify the full command.\n");
94 fprintf(ofp, "\n");
95}
96
97static struct viewers *parse_options(void)
98{
99 if (opt_viewer == NULL) {
100 /* Default is babeltrace */
101 return &(viewers[VIEWER_BABELTRACE]);
102 }
103
104#if 0
105 if (strstr(opt_viewer, viewers[VIEWER_LTTV_GUI].exec_name) == 0) {
106 return &(viewers[VIEWER_LTTV_GUI]);
107 }
108#endif
109
110 /*
111 * This means that if -e, --viewers is used, we just override everything
112 * with it. For supported viewers like lttv, we could simply detect if "-t"
113 * is passed and if not, add the trace directory to it.
114 */
115 return &(viewers[VIEWER_USER_DEFINED]);
116}
117
118/*
119 * Alloc an array of string pointer from a simple string having all options
120 * seperated by spaces. Also adds the trace path to the arguments.
121 *
122 * The returning pointer is ready to be passed to execvp().
123 */
124static char **alloc_argv_from_user_opts(char *opts, const char *trace_path)
125{
126 int i = 0, ignore_space = 0;
127 unsigned int num_opts = 1;
128 char **argv, *token = opts;
129
130 /* Count number of arguments. */
131 do {
132 if (*token == ' ') {
133 /* Use to ignore consecutive spaces */
134 if (!ignore_space) {
135 num_opts++;
136 }
137 ignore_space = 1;
138 } else {
139 ignore_space = 0;
140 }
141 token++;
142 } while (*token != '\0');
143
144 /* Add two here for the NULL terminating element and trace path */
145 argv = malloc(sizeof(char *) * (num_opts + 2));
146 if (argv == NULL) {
147 goto error;
148 }
149
150 token = strtok(opts, " ");
151 while (token != NULL) {
152 argv[i] = strdup(token);
153 token = strtok(NULL, " ");
154 i++;
155 }
156
157 argv[num_opts] = (char *) trace_path;
158 argv[num_opts + 1] = NULL;
159
160 return argv;
161
162error:
163 return NULL;
164}
165
166/*
167 * Alloc an array of string pointer from an array of strings. It also adds
168 * the trace path to the argv.
169 *
170 * The returning pointer is ready to be passed to execvp().
171 */
172static char **alloc_argv_from_local_opts(const char **opts, size_t opts_len,
173 const char *trace_path)
174{
175 char **argv;
176 size_t size;
177
178 size = sizeof(char *) * opts_len;
179
180 /* Add two here for the trace_path and the NULL terminating element. */
181 argv = malloc(size + 2);
182 if (argv == NULL) {
183 goto error;
184 }
185
186 memcpy(argv, opts, size);
187
188 argv[opts_len] = (char *)trace_path;
189 argv[opts_len + 1] = NULL;
190
191error:
192 return argv;
193}
194
195/*
196 * Spawn viewer with the trace directory path.
197 */
198static int spawn_viewer(const char *trace_path)
199{
200 int ret = 0;
0c95f5b2
DG
201 struct stat status;
202 const char *viewer_bin = NULL;
203 struct viewers *viewer;
204 char **argv = NULL;
205
206 /* Check for --viewer options */
207 viewer = parse_options();
208 if (viewer == NULL) {
209 ret = CMD_ERROR;
210 goto error;
211 }
212
85a68078
DG
213 switch (viewer->type) {
214 case VIEWER_BABELTRACE:
215 if (stat(babeltrace_bin, &status) == 0) {
216 viewer_bin = babeltrace_bin;
217 } else {
218 viewer_bin = viewer->exec_name;
219 }
220 argv = alloc_argv_from_local_opts(babeltrace_opts,
221 ARRAY_SIZE(babeltrace_opts), trace_path);
222 break;
0c95f5b2 223#if 0
85a68078
DG
224 case VIEWER_LTTV_GUI:
225 if (stat(lttv_gui_bin, &status) == 0) {
226 viewer_bin = lttv_gui_bin;
227 } else {
228 viewer_bin = viewer->exec_name;
229 }
230 argv = alloc_argv_from_local_opts(lttv_gui_opts,
231 ARRAY_SIZE(lttv_gui_opts), trace_path);
232 break;
0c95f5b2 233#endif
85a68078
DG
234 case VIEWER_USER_DEFINED:
235 argv = alloc_argv_from_user_opts(opt_viewer, trace_path);
236 if (argv) {
237 viewer_bin = argv[0];
0c95f5b2 238 }
85a68078
DG
239 break;
240 default:
241 viewer_bin = viewers[VIEWER_BABELTRACE].exec_name;
242 argv = alloc_argv_from_local_opts(babeltrace_opts,
243 ARRAY_SIZE(babeltrace_opts), trace_path);
244 break;
245 }
0c95f5b2 246
85a68078
DG
247 if (argv == NULL) {
248 ret = CMD_FATAL;
249 goto error;
250 }
0c95f5b2 251
85a68078 252 DBG("Using %s viewer", viewer_bin);
0c95f5b2 253
85a68078
DG
254 ret = execvp(viewer_bin, argv);
255 if (ret) {
256 PERROR("exec: %s", viewer_bin);
257 free(argv);
0c95f5b2 258 ret = CMD_FATAL;
85a68078 259 goto error;
0c95f5b2
DG
260 }
261
262error:
263 return ret;
264}
265
266/*
267 * Exec viewer if found and use session name path.
268 */
269static int view_trace(void)
270{
271 int ret, count, i, found = 0;
272 char *session_name;
273 struct lttng_session *sessions = NULL;
274
275 /*
276 * Safety net. If lttng is suid at some point for *any* useless reasons,
277 * this prevent any bad execution of binraries.
278 */
279 if (getuid() != 0) {
280 if (getuid() != geteuid()) {
281 ERR("UID does not match effective UID.");
6e9261f4 282 ret = CMD_ERROR;
0c95f5b2
DG
283 goto error;
284 } else if (getgid() != getegid()) {
285 ERR("GID does not match effective GID.");
6e9261f4 286 ret = CMD_ERROR;
0c95f5b2
DG
287 goto error;
288 }
289 }
290
291 if (opt_session_name == NULL) {
292 session_name = get_session_name();
293 if (session_name == NULL) {
294 ret = CMD_ERROR;
295 goto error;
296 }
297 } else {
298 session_name = opt_session_name;
299 }
300
301 DBG("Viewing trace for session %s", session_name);
302
303 /* Getting all sessions */
304 count = lttng_list_sessions(&sessions);
305 if (count < 0) {
306 ERR("Unable to list sessions. Session name %s not found.",
307 session_name);
308 MSG("Is there a session daemon running?");
309 ret = CMD_ERROR;
310 goto free_error;
311 }
312
313 /* Find our session listed by the session daemon */
314 for (i = 0; i < count; i++) {
315 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
316 found = 1;
317 break;
318 }
319 }
320
321 if (!found) {
322 MSG("Session name %s not found", session_name);
e7248846 323 ret = CMD_ERROR;
0c95f5b2
DG
324 goto free_sessions;
325 }
326
327 MSG("Trace directory: %s\n", sessions[i].path);
328
329 ret = spawn_viewer(sessions[i].path);
330 if (ret < 0) {
331 /* Don't set ret so lttng can interpret the sessiond error. */
332 goto free_sessions;
333 }
334
335 ret = CMD_SUCCESS;
336
337free_sessions:
338 if (sessions) {
339 free(sessions);
340 }
341free_error:
342 if (opt_session_name == NULL) {
343 free(session_name);
344 }
345error:
346 return ret;
347}
348
349/*
350 * The 'view <options>' first level command
351 */
352int cmd_view(int argc, const char **argv)
353{
354 int opt, ret = CMD_SUCCESS;
355 static poptContext pc;
356
357 pc = poptGetContext(NULL, argc, argv, long_options, 0);
358 poptReadDefaultConfig(pc, 0);
359
360 while ((opt = poptGetNextOpt(pc)) != -1) {
361 switch (opt) {
362 case OPT_HELP:
363 usage(stdout);
364 goto end;
365 case OPT_LIST_OPTIONS:
366 list_cmd_options(stdout, long_options);
367 goto end;
368 default:
369 usage(stderr);
370 ret = CMD_UNDEFINED;
371 goto end;
372 }
373 }
374
375 opt_session_name = (char*) poptGetArg(pc);
376
377 ret = view_trace();
378
379end:
380 poptFreeContext(pc);
381 return ret;
382}
This page took 0.038718 seconds and 5 git commands to generate.