cff6941f58b96933bd7cde48bdf257536338045a
[lttng-tools.git] / src / bin / lttng / commands / view.cpp
1 /*
2 * Copyright (C) 2011 David Goulet <dgoulet@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #define _LGPL_SOURCE
9 #include <popt.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/stat.h>
14 #include <sys/types.h>
15 #include <unistd.h>
16
17 #include <common/spawn-viewer.hpp>
18 #include "../command.hpp"
19
20 static char *opt_viewer;
21 static char *opt_trace_path;
22
23 #ifdef LTTNG_EMBED_HELP
24 static const char help_msg[] =
25 #include <lttng-view.1.h>
26 ;
27 #endif
28
29 enum {
30 OPT_HELP = 1,
31 OPT_LIST_OPTIONS,
32 };
33
34 static struct poptOption long_options[] = {
35 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
36 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
37 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
38 {"viewer", 'e', POPT_ARG_STRING, &opt_viewer, 0, 0, 0},
39 {"trace-path", 't', POPT_ARG_STRING, &opt_trace_path, 0, 0, 0},
40 {0, 0, 0, 0, 0, 0, 0}
41 };
42
43 /* Is the session we are trying to view is in live mode. */
44 static int session_live_mode;
45
46 /*
47 * Build the live path we need for the lttng live view.
48 */
49 static char *build_live_path(char *session_name)
50 {
51 int ret;
52 char *path = NULL;
53 char hostname[LTTNG_HOST_NAME_MAX];
54
55 ret = gethostname(hostname, sizeof(hostname));
56 if (ret < 0) {
57 PERROR("gethostname");
58 goto error;
59 }
60
61 ret = asprintf(&path, "net://localhost/host/%s/%s", hostname,
62 session_name);
63 if (ret < 0) {
64 PERROR("asprintf live path");
65 goto error;
66 }
67
68 error:
69 return path;
70 }
71
72 /*
73 * Exec viewer if found and use session name path.
74 */
75 static int view_trace(const char *arg_session_name)
76 {
77 int ret;
78 char *session_name, *trace_path = NULL;
79 struct lttng_session *sessions = NULL;
80 bool free_trace_path = false;
81
82 /*
83 * Safety net. If lttng is suid at some point for *any* useless reasons,
84 * this prevent any bad execution of binaries.
85 */
86 if (getuid() != 0) {
87 if (getuid() != geteuid()) {
88 ERR("UID does not match effective UID.");
89 ret = CMD_ERROR;
90 goto error;
91 } else if (getgid() != getegid()) {
92 ERR("GID does not match effective GID.");
93 ret = CMD_ERROR;
94 goto error;
95 }
96 }
97
98 /* User define trace path override the session name */
99 if (opt_trace_path) {
100 session_name = NULL;
101 } else {
102 if (arg_session_name == NULL) {
103 session_name = get_session_name();
104 } else {
105 session_name = strdup(arg_session_name);
106 if (session_name == NULL) {
107 PERROR("Failed to copy session name");
108 }
109 }
110
111 if (session_name == NULL) {
112 ret = CMD_ERROR;
113 goto error;
114 }
115 }
116
117 DBG("Viewing trace for session %s", session_name);
118
119 if (session_name) {
120 int i, count, found = 0;
121
122 /* Getting all sessions */
123 count = lttng_list_sessions(&sessions);
124 if (count < 0) {
125 ERR("Unable to list sessions. Session name %s not found.",
126 session_name);
127 MSG("Is there a session daemon running?");
128 ret = CMD_ERROR;
129 goto free_error;
130 }
131
132 /* Find our session listed by the session daemon */
133 for (i = 0; i < count; i++) {
134 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
135 found = 1;
136 break;
137 }
138 }
139
140 if (!found) {
141 MSG("Session name %s not found", session_name);
142 ret = CMD_ERROR;
143 goto free_sessions;
144 }
145
146 session_live_mode = sessions[i].live_timer_interval;
147
148 DBG("Session live mode set to %d", session_live_mode);
149
150 if (sessions[i].enabled && !session_live_mode) {
151 WARN("Session %s is running. Please stop it before reading it.",
152 session_name);
153 ret = CMD_ERROR;
154 goto free_sessions;
155 }
156
157 /* If the timer interval is set we are in live mode. */
158 if (session_live_mode) {
159 trace_path = build_live_path(session_name);
160 if (!trace_path) {
161 ret = CMD_ERROR;
162 goto free_sessions;
163 }
164 free_trace_path = true;
165 } else {
166 /* Get file system session path. */
167 trace_path = sessions[i].path;
168 }
169 } else {
170 trace_path = opt_trace_path;
171 }
172
173 MSG("Trace directory: %s\n", trace_path);
174
175 ret = spawn_viewer(trace_path, opt_viewer, session_live_mode);
176 if (ret < 0) {
177 /* Don't set ret so lttng can interpret the sessiond error. */
178 goto free_sessions;
179 }
180
181 free_sessions:
182 if (session_live_mode && free_trace_path) {
183 free(trace_path);
184 }
185 free(sessions);
186 free_error:
187 free(session_name);
188 error:
189 return ret;
190 }
191
192 /*
193 * The 'view <options>' first level command
194 */
195 int cmd_view(int argc, const char **argv)
196 {
197 int opt, ret = CMD_SUCCESS;
198 static poptContext pc;
199 const char *arg_session_name = NULL;
200 const char *leftover = NULL;
201
202 pc = poptGetContext(NULL, argc, argv, long_options, 0);
203 poptReadDefaultConfig(pc, 0);
204
205 if (lttng_opt_mi) {
206 WARN("mi does not apply to view command");
207 }
208
209 while ((opt = poptGetNextOpt(pc)) != -1) {
210 switch (opt) {
211 case OPT_HELP:
212 SHOW_HELP();
213 goto end;
214 case OPT_LIST_OPTIONS:
215 list_cmd_options(stdout, long_options);
216 goto end;
217 default:
218 ret = CMD_UNDEFINED;
219 goto end;
220 }
221 }
222
223 arg_session_name = poptGetArg(pc);
224
225 leftover = poptGetArg(pc);
226 if (leftover) {
227 ERR("Unknown argument: %s", leftover);
228 ret = CMD_ERROR;
229 goto end;
230 }
231
232 ret = view_trace(arg_session_name);
233
234 end:
235 poptFreeContext(pc);
236 return ret;
237 }
This page took 0.033617 seconds and 4 git commands to generate.