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 | ||
dd392e94 | 17 | #include <common/spawn-viewer.h> |
0c95f5b2 | 18 | #include "../command.h" |
0c95f5b2 DG |
19 | |
20 | static char *opt_session_name; | |
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> | |
27 | ; | |
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 */ | |
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}, | |
f1f887c0 | 40 | {"trace-path", 't', POPT_ARG_STRING, &opt_trace_path, 0, 0, 0}, |
0c95f5b2 DG |
41 | {0, 0, 0, 0, 0, 0, 0} |
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; | |
53 | char *path = NULL; | |
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 | ||
62 | ret = asprintf(&path, "net://localhost/host/%s/%s", hostname, | |
63 | session_name); | |
64 | if (ret < 0) { | |
6f04ed72 | 65 | PERROR("asprintf live path"); |
8960e9cd DG |
66 | goto error; |
67 | } | |
68 | ||
69 | error: | |
70 | return path; | |
71 | } | |
72 | ||
0c95f5b2 DG |
73 | /* |
74 | * Exec viewer if found and use session name path. | |
75 | */ | |
76 | static int view_trace(void) | |
77 | { | |
c617c0c6 | 78 | int ret; |
8960e9cd | 79 | char *session_name, *trace_path = NULL; |
0c95f5b2 | 80 | struct lttng_session *sessions = NULL; |
f12e3556 | 81 | bool free_trace_path = false; |
0c95f5b2 DG |
82 | |
83 | /* | |
84 | * Safety net. If lttng is suid at some point for *any* useless reasons, | |
f1f887c0 | 85 | * this prevent any bad execution of binaries. |
0c95f5b2 DG |
86 | */ |
87 | if (getuid() != 0) { | |
88 | if (getuid() != geteuid()) { | |
89 | ERR("UID does not match effective UID."); | |
6e9261f4 | 90 | ret = CMD_ERROR; |
0c95f5b2 DG |
91 | goto error; |
92 | } else if (getgid() != getegid()) { | |
93 | ERR("GID does not match effective GID."); | |
6e9261f4 | 94 | ret = CMD_ERROR; |
0c95f5b2 DG |
95 | goto error; |
96 | } | |
97 | } | |
98 | ||
f1f887c0 DG |
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) { | |
0c95f5b2 DG |
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 | ||
f1f887c0 | 114 | if (session_name) { |
c617c0c6 MD |
115 | int i, count, found = 0; |
116 | ||
f1f887c0 DG |
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 | } | |
0c95f5b2 | 126 | |
f1f887c0 DG |
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 | } | |
0c95f5b2 | 133 | } |
0c95f5b2 | 134 | |
f1f887c0 DG |
135 | if (!found) { |
136 | MSG("Session name %s not found", session_name); | |
137 | ret = CMD_ERROR; | |
138 | goto free_sessions; | |
139 | } | |
140 | ||
8960e9cd DG |
141 | session_live_mode = sessions[i].live_timer_interval; |
142 | ||
143 | DBG("Session live mode set to %d", session_live_mode); | |
3822545d | 144 | |
8960e9cd | 145 | if (sessions[i].enabled && !session_live_mode) { |
3822545d DG |
146 | WARN("Session %s is running. Please stop it before reading it.", |
147 | session_name); | |
148 | ret = CMD_ERROR; | |
149 | goto free_sessions; | |
150 | } | |
8960e9cd DG |
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 | } | |
f12e3556 | 159 | free_trace_path = true; |
8960e9cd DG |
160 | } else { |
161 | /* Get file system session path. */ | |
162 | trace_path = sessions[i].path; | |
163 | } | |
f1f887c0 DG |
164 | } else { |
165 | trace_path = opt_trace_path; | |
0c95f5b2 DG |
166 | } |
167 | ||
f1f887c0 | 168 | MSG("Trace directory: %s\n", trace_path); |
0c95f5b2 | 169 | |
dd392e94 | 170 | ret = spawn_viewer(trace_path, opt_viewer, session_live_mode); |
0c95f5b2 DG |
171 | if (ret < 0) { |
172 | /* Don't set ret so lttng can interpret the sessiond error. */ | |
173 | goto free_sessions; | |
174 | } | |
175 | ||
0c95f5b2 | 176 | free_sessions: |
f12e3556 | 177 | if (session_live_mode && free_trace_path) { |
8960e9cd DG |
178 | free(trace_path); |
179 | } | |
0e428499 | 180 | free(sessions); |
0c95f5b2 DG |
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; | |
68c7f6e5 | 196 | const char *leftover = NULL; |
0c95f5b2 DG |
197 | |
198 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
199 | poptReadDefaultConfig(pc, 0); | |
200 | ||
c7e35b03 JR |
201 | if (lttng_opt_mi) { |
202 | WARN("mi does not apply to view command"); | |
203 | } | |
204 | ||
0c95f5b2 DG |
205 | while ((opt = poptGetNextOpt(pc)) != -1) { |
206 | switch (opt) { | |
207 | case OPT_HELP: | |
4ba92f18 | 208 | SHOW_HELP(); |
0c95f5b2 DG |
209 | goto end; |
210 | case OPT_LIST_OPTIONS: | |
211 | list_cmd_options(stdout, long_options); | |
212 | goto end; | |
213 | default: | |
0c95f5b2 DG |
214 | ret = CMD_UNDEFINED; |
215 | goto end; | |
216 | } | |
217 | } | |
218 | ||
219 | opt_session_name = (char*) poptGetArg(pc); | |
220 | ||
68c7f6e5 JD |
221 | leftover = poptGetArg(pc); |
222 | if (leftover) { | |
223 | ERR("Unknown argument: %s", leftover); | |
224 | ret = CMD_ERROR; | |
225 | goto end; | |
226 | } | |
227 | ||
0c95f5b2 DG |
228 | ret = view_trace(); |
229 | ||
230 | end: | |
231 | poptFreeContext(pc); | |
232 | return ret; | |
233 | } |