Commit | Line | Data |
---|---|---|
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 | ||
30 | static char *opt_session_name; | |
31 | static char *opt_viewer; | |
32 | static const char *babeltrace_bin = CONFIG_BABELTRACE_BIN; | |
33 | //static const char *lttv_gui_bin = CONFIG_LTTV_GUI_BIN; | |
34 | ||
35 | enum { | |
36 | OPT_HELP = 1, | |
37 | OPT_LIST_OPTIONS, | |
38 | }; | |
39 | ||
40 | static 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 | 51 | static 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 | */ | |
58 | enum 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 | */ | |
68 | static 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 | */ | |
80 | static 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 | ||
97 | static 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 | */ | |
124 | static 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 | ||
162 | error: | |
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 | */ | |
172 | static 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 | ||
191 | error: | |
192 | return argv; | |
193 | } | |
194 | ||
195 | /* | |
196 | * Spawn viewer with the trace directory path. | |
197 | */ | |
198 | static int spawn_viewer(const char *trace_path) | |
199 | { | |
200 | int ret = 0; | |
201 | pid_t pid; | |
202 | struct stat status; | |
203 | const char *viewer_bin = NULL; | |
204 | struct viewers *viewer; | |
205 | char **argv = NULL; | |
206 | ||
207 | /* Check for --viewer options */ | |
208 | viewer = parse_options(); | |
209 | if (viewer == NULL) { | |
210 | ret = CMD_ERROR; | |
211 | goto error; | |
212 | } | |
213 | ||
214 | pid = fork(); | |
215 | if (pid == 0) { | |
216 | switch (viewer->type) { | |
217 | case VIEWER_BABELTRACE: | |
218 | if (stat(babeltrace_bin, &status) == 0) { | |
219 | viewer_bin = babeltrace_bin; | |
220 | } else { | |
221 | viewer_bin = viewer->exec_name; | |
222 | } | |
223 | argv = alloc_argv_from_local_opts(babeltrace_opts, | |
224 | ARRAY_SIZE(babeltrace_opts), trace_path); | |
225 | break; | |
226 | #if 0 | |
227 | case VIEWER_LTTV_GUI: | |
228 | if (stat(lttv_gui_bin, &status) == 0) { | |
229 | viewer_bin = lttv_gui_bin; | |
230 | } else { | |
231 | viewer_bin = viewer->exec_name; | |
232 | } | |
233 | argv = alloc_argv_from_local_opts(lttv_gui_opts, | |
234 | ARRAY_SIZE(lttv_gui_opts), trace_path); | |
235 | break; | |
236 | #endif | |
237 | case VIEWER_USER_DEFINED: | |
238 | argv = alloc_argv_from_user_opts(opt_viewer, trace_path); | |
239 | if (argv) { | |
240 | viewer_bin = argv[0]; | |
241 | } | |
242 | break; | |
243 | default: | |
244 | viewer_bin = viewers[VIEWER_BABELTRACE].exec_name; | |
245 | argv = alloc_argv_from_local_opts(babeltrace_opts, | |
246 | ARRAY_SIZE(babeltrace_opts), trace_path); | |
247 | break; | |
248 | } | |
249 | ||
250 | if (argv == NULL) { | |
251 | ret = CMD_FATAL; | |
252 | goto error; | |
253 | } | |
254 | ||
255 | DBG("Using %s viewer", viewer_bin); | |
256 | ||
257 | ret = execvp(viewer_bin, argv); | |
258 | if (ret) { | |
259 | PERROR("exec: %s", viewer_bin); | |
260 | free(argv); | |
261 | ret = CMD_FATAL; | |
262 | goto error; | |
263 | } | |
264 | } else if (pid > 0) { | |
265 | ret = CMD_SUCCESS; | |
266 | } else { | |
267 | PERROR("Fork trace viewer"); | |
268 | ret = CMD_FATAL; | |
269 | } | |
270 | ||
271 | error: | |
272 | return ret; | |
273 | } | |
274 | ||
275 | /* | |
276 | * Exec viewer if found and use session name path. | |
277 | */ | |
278 | static int view_trace(void) | |
279 | { | |
280 | int ret, count, i, found = 0; | |
281 | char *session_name; | |
282 | struct lttng_session *sessions = NULL; | |
283 | ||
284 | /* | |
285 | * Safety net. If lttng is suid at some point for *any* useless reasons, | |
286 | * this prevent any bad execution of binraries. | |
287 | */ | |
288 | if (getuid() != 0) { | |
289 | if (getuid() != geteuid()) { | |
290 | ERR("UID does not match effective UID."); | |
6e9261f4 | 291 | ret = CMD_ERROR; |
0c95f5b2 DG |
292 | goto error; |
293 | } else if (getgid() != getegid()) { | |
294 | ERR("GID does not match effective GID."); | |
6e9261f4 | 295 | ret = CMD_ERROR; |
0c95f5b2 DG |
296 | goto error; |
297 | } | |
298 | } | |
299 | ||
300 | if (opt_session_name == NULL) { | |
301 | session_name = get_session_name(); | |
302 | if (session_name == NULL) { | |
303 | ret = CMD_ERROR; | |
304 | goto error; | |
305 | } | |
306 | } else { | |
307 | session_name = opt_session_name; | |
308 | } | |
309 | ||
310 | DBG("Viewing trace for session %s", session_name); | |
311 | ||
312 | /* Getting all sessions */ | |
313 | count = lttng_list_sessions(&sessions); | |
314 | if (count < 0) { | |
315 | ERR("Unable to list sessions. Session name %s not found.", | |
316 | session_name); | |
317 | MSG("Is there a session daemon running?"); | |
318 | ret = CMD_ERROR; | |
319 | goto free_error; | |
320 | } | |
321 | ||
322 | /* Find our session listed by the session daemon */ | |
323 | for (i = 0; i < count; i++) { | |
324 | if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) { | |
325 | found = 1; | |
326 | break; | |
327 | } | |
328 | } | |
329 | ||
330 | if (!found) { | |
331 | MSG("Session name %s not found", session_name); | |
332 | goto free_sessions; | |
333 | } | |
334 | ||
335 | MSG("Trace directory: %s\n", sessions[i].path); | |
336 | ||
337 | ret = spawn_viewer(sessions[i].path); | |
338 | if (ret < 0) { | |
339 | /* Don't set ret so lttng can interpret the sessiond error. */ | |
340 | goto free_sessions; | |
341 | } | |
342 | ||
343 | ret = CMD_SUCCESS; | |
344 | ||
345 | free_sessions: | |
346 | if (sessions) { | |
347 | free(sessions); | |
348 | } | |
349 | free_error: | |
350 | if (opt_session_name == NULL) { | |
351 | free(session_name); | |
352 | } | |
353 | error: | |
354 | return ret; | |
355 | } | |
356 | ||
357 | /* | |
358 | * The 'view <options>' first level command | |
359 | */ | |
360 | int cmd_view(int argc, const char **argv) | |
361 | { | |
362 | int opt, ret = CMD_SUCCESS; | |
363 | static poptContext pc; | |
364 | ||
365 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
366 | poptReadDefaultConfig(pc, 0); | |
367 | ||
368 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
369 | switch (opt) { | |
370 | case OPT_HELP: | |
371 | usage(stdout); | |
372 | goto end; | |
373 | case OPT_LIST_OPTIONS: | |
374 | list_cmd_options(stdout, long_options); | |
375 | goto end; | |
376 | default: | |
377 | usage(stderr); | |
378 | ret = CMD_UNDEFINED; | |
379 | goto end; | |
380 | } | |
381 | } | |
382 | ||
383 | opt_session_name = (char*) poptGetArg(pc); | |
384 | ||
385 | ret = view_trace(); | |
386 | ||
387 | end: | |
388 | poptFreeContext(pc); | |
389 | return ret; | |
390 | } |