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 |
0c95f5b2 DG |
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 | ||
c9e313bc SM |
17 | #include <common/spawn-viewer.hpp> |
18 | #include "../command.hpp" | |
0c95f5b2 | 19 | |
0c95f5b2 | 20 | static char *opt_viewer; |
f1f887c0 | 21 | static char *opt_trace_path; |
0c95f5b2 | 22 | |
4fc83d94 PP |
23 | #ifdef LTTNG_EMBED_HELP |
24 | static const char help_msg[] = | |
25 | #include <lttng-view.1.h> | |
26 | ; | |
27 | #endif | |
28 | ||
0c95f5b2 DG |
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}, | |
f1f887c0 | 39 | {"trace-path", 't', POPT_ARG_STRING, &opt_trace_path, 0, 0, 0}, |
0c95f5b2 DG |
40 | {0, 0, 0, 0, 0, 0, 0} |
41 | }; | |
42 | ||
8960e9cd DG |
43 | /* Is the session we are trying to view is in live mode. */ |
44 | static int session_live_mode; | |
45 | ||
8960e9cd DG |
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; | |
32aef76c | 53 | char hostname[LTTNG_HOST_NAME_MAX]; |
8960e9cd DG |
54 | |
55 | ret = gethostname(hostname, sizeof(hostname)); | |
56 | if (ret < 0) { | |
6f04ed72 | 57 | PERROR("gethostname"); |
8960e9cd DG |
58 | goto error; |
59 | } | |
60 | ||
61 | ret = asprintf(&path, "net://localhost/host/%s/%s", hostname, | |
62 | session_name); | |
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; |
8960e9cd | 78 | char *session_name, *trace_path = NULL; |
0c95f5b2 | 79 | struct lttng_session *sessions = NULL; |
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) { | |
100 | session_name = NULL; | |
5b915816 MJ |
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 | ||
0c95f5b2 DG |
111 | if (session_name == NULL) { |
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) { | |
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 | } | |
0c95f5b2 | 131 | |
f1f887c0 DG |
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 | } | |
0c95f5b2 | 138 | } |
0c95f5b2 | 139 | |
f1f887c0 DG |
140 | if (!found) { |
141 | MSG("Session name %s not found", session_name); | |
142 | ret = CMD_ERROR; | |
143 | goto free_sessions; | |
144 | } | |
145 | ||
8960e9cd DG |
146 | session_live_mode = sessions[i].live_timer_interval; |
147 | ||
148 | DBG("Session live mode set to %d", session_live_mode); | |
3822545d | 149 | |
8960e9cd | 150 | if (sessions[i].enabled && !session_live_mode) { |
3822545d DG |
151 | WARN("Session %s is running. Please stop it before reading it.", |
152 | session_name); | |
153 | ret = CMD_ERROR; | |
154 | goto free_sessions; | |
155 | } | |
8960e9cd DG |
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 | } | |
f12e3556 | 164 | free_trace_path = true; |
8960e9cd DG |
165 | } else { |
166 | /* Get file system session path. */ | |
167 | trace_path = sessions[i].path; | |
168 | } | |
f1f887c0 DG |
169 | } else { |
170 | trace_path = opt_trace_path; | |
0c95f5b2 DG |
171 | } |
172 | ||
f1f887c0 | 173 | MSG("Trace directory: %s\n", trace_path); |
0c95f5b2 | 174 | |
dd392e94 | 175 | ret = spawn_viewer(trace_path, opt_viewer, session_live_mode); |
0c95f5b2 DG |
176 | if (ret < 0) { |
177 | /* Don't set ret so lttng can interpret the sessiond error. */ | |
178 | goto free_sessions; | |
179 | } | |
180 | ||
0c95f5b2 | 181 | free_sessions: |
f12e3556 | 182 | if (session_live_mode && free_trace_path) { |
8960e9cd DG |
183 | free(trace_path); |
184 | } | |
0e428499 | 185 | free(sessions); |
0c95f5b2 | 186 | free_error: |
5b915816 | 187 | free(session_name); |
0c95f5b2 DG |
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; | |
5b915816 | 199 | const char *arg_session_name = NULL; |
68c7f6e5 | 200 | const char *leftover = NULL; |
0c95f5b2 DG |
201 | |
202 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
203 | poptReadDefaultConfig(pc, 0); | |
204 | ||
c7e35b03 JR |
205 | if (lttng_opt_mi) { |
206 | WARN("mi does not apply to view command"); | |
207 | } | |
208 | ||
0c95f5b2 DG |
209 | while ((opt = poptGetNextOpt(pc)) != -1) { |
210 | switch (opt) { | |
211 | case OPT_HELP: | |
4ba92f18 | 212 | SHOW_HELP(); |
0c95f5b2 DG |
213 | goto end; |
214 | case OPT_LIST_OPTIONS: | |
215 | list_cmd_options(stdout, long_options); | |
216 | goto end; | |
217 | default: | |
0c95f5b2 DG |
218 | ret = CMD_UNDEFINED; |
219 | goto end; | |
220 | } | |
221 | } | |
222 | ||
5b915816 | 223 | arg_session_name = poptGetArg(pc); |
0c95f5b2 | 224 | |
68c7f6e5 JD |
225 | leftover = poptGetArg(pc); |
226 | if (leftover) { | |
227 | ERR("Unknown argument: %s", leftover); | |
228 | ret = CMD_ERROR; | |
229 | goto end; | |
230 | } | |
231 | ||
5b915816 | 232 | ret = view_trace(arg_session_name); |
0c95f5b2 DG |
233 | |
234 | end: | |
235 | poptFreeContext(pc); | |
236 | return ret; | |
237 | } |