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