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