| 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 "../command.hpp" |
| 10 | |
| 11 | #include <common/spawn-viewer.hpp> |
| 12 | |
| 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 | |
| 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, 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 } |
| 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 = nullptr; |
| 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, 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 = nullptr; |
| 79 | struct lttng_session *sessions = nullptr; |
| 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 = nullptr; |
| 101 | } else { |
| 102 | if (arg_session_name == nullptr) { |
| 103 | session_name = get_session_name(); |
| 104 | } else { |
| 105 | session_name = strdup(arg_session_name); |
| 106 | if (session_name == nullptr) { |
| 107 | PERROR("Failed to copy session name"); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | if (session_name == nullptr) { |
| 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.", session_name); |
| 126 | MSG("Is there a session daemon running?"); |
| 127 | ret = CMD_ERROR; |
| 128 | goto free_error; |
| 129 | } |
| 130 | |
| 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 | } |
| 137 | } |
| 138 | |
| 139 | if (!found) { |
| 140 | MSG("Session name %s not found", session_name); |
| 141 | ret = CMD_ERROR; |
| 142 | goto free_sessions; |
| 143 | } |
| 144 | |
| 145 | session_live_mode = sessions[i].live_timer_interval; |
| 146 | |
| 147 | DBG("Session live mode set to %d", session_live_mode); |
| 148 | |
| 149 | if (sessions[i].enabled && !session_live_mode) { |
| 150 | WARN("Session %s is running. Please stop it before reading it.", |
| 151 | session_name); |
| 152 | ret = CMD_ERROR; |
| 153 | goto free_sessions; |
| 154 | } |
| 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 | } |
| 163 | free_trace_path = true; |
| 164 | } else { |
| 165 | /* Get file system session path. */ |
| 166 | trace_path = sessions[i].path; |
| 167 | } |
| 168 | } else { |
| 169 | trace_path = opt_trace_path; |
| 170 | } |
| 171 | |
| 172 | MSG("Trace directory: %s\n", trace_path); |
| 173 | |
| 174 | ret = spawn_viewer(trace_path, opt_viewer, session_live_mode); |
| 175 | if (ret < 0) { |
| 176 | /* Don't set ret so lttng can interpret the sessiond error. */ |
| 177 | goto free_sessions; |
| 178 | } |
| 179 | |
| 180 | free_sessions: |
| 181 | if (session_live_mode && free_trace_path) { |
| 182 | free(trace_path); |
| 183 | } |
| 184 | free(sessions); |
| 185 | free_error: |
| 186 | free(session_name); |
| 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; |
| 198 | const char *arg_session_name = nullptr; |
| 199 | const char *leftover = nullptr; |
| 200 | |
| 201 | pc = poptGetContext(nullptr, argc, argv, long_options, 0); |
| 202 | poptReadDefaultConfig(pc, 0); |
| 203 | |
| 204 | if (lttng_opt_mi) { |
| 205 | WARN("mi does not apply to view command"); |
| 206 | } |
| 207 | |
| 208 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 209 | switch (opt) { |
| 210 | case OPT_HELP: |
| 211 | SHOW_HELP(); |
| 212 | goto end; |
| 213 | case OPT_LIST_OPTIONS: |
| 214 | list_cmd_options(stdout, long_options); |
| 215 | goto end; |
| 216 | default: |
| 217 | ret = CMD_UNDEFINED; |
| 218 | goto end; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | arg_session_name = poptGetArg(pc); |
| 223 | |
| 224 | leftover = poptGetArg(pc); |
| 225 | if (leftover) { |
| 226 | ERR("Unknown argument: %s", leftover); |
| 227 | ret = CMD_ERROR; |
| 228 | goto end; |
| 229 | } |
| 230 | |
| 231 | ret = view_trace(arg_session_name); |
| 232 | |
| 233 | end: |
| 234 | poptFreeContext(pc); |
| 235 | return ret; |
| 236 | } |