| 1 | /* |
| 2 | * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along |
| 15 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | */ |
| 18 | |
| 19 | #define _GNU_SOURCE |
| 20 | #include <errno.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <getopt.h> |
| 23 | #include <grp.h> |
| 24 | #include <limits.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <sys/wait.h> |
| 31 | #include <unistd.h> |
| 32 | |
| 33 | #include <lttng/liblttngctl.h> |
| 34 | |
| 35 | #include "lttng.h" |
| 36 | #include "lttngerr.h" |
| 37 | |
| 38 | /* Variables */ |
| 39 | static char *progname; |
| 40 | |
| 41 | /* Prototypes */ |
| 42 | static int process_client_opt(void); |
| 43 | static int process_opt_list_apps(void); |
| 44 | static int process_opt_list_sessions(void); |
| 45 | static int process_opt_create_session(void); |
| 46 | static void sighandler(int sig); |
| 47 | static int set_signal_handler(void); |
| 48 | |
| 49 | /* |
| 50 | * start_client |
| 51 | * |
| 52 | * Process client request from the command line |
| 53 | * options. Every tracing action is done by the |
| 54 | * liblttngctl API. |
| 55 | */ |
| 56 | static int process_client_opt(void) |
| 57 | { |
| 58 | int ret; |
| 59 | |
| 60 | /* Connect to the session daemon */ |
| 61 | ret = lttng_connect_sessiond(); |
| 62 | if (ret < 0) { |
| 63 | goto end; |
| 64 | } |
| 65 | |
| 66 | if (opt_list_apps) { |
| 67 | ret = process_opt_list_apps(); |
| 68 | if (ret < 0) { |
| 69 | goto end; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | if (opt_list_session) { |
| 74 | ret = process_opt_list_sessions(); |
| 75 | if (ret < 0) { |
| 76 | goto end; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | if (opt_create_session != NULL) { |
| 81 | ret = process_opt_create_session(); |
| 82 | if (ret < 0) { |
| 83 | goto end; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return 0; |
| 88 | |
| 89 | end: |
| 90 | ERR("%s", lttng_get_readable_code(ret)); |
| 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * process_opt_create_session |
| 96 | * |
| 97 | * Create a new session using the name pass |
| 98 | * to the command line. |
| 99 | */ |
| 100 | static int process_opt_create_session(void) |
| 101 | { |
| 102 | int ret; |
| 103 | char *session_id; |
| 104 | |
| 105 | ret = lttng_create_session(opt_create_session, &session_id); |
| 106 | if (ret < 0) { |
| 107 | goto error; |
| 108 | } |
| 109 | |
| 110 | MSG("Session created:"); |
| 111 | MSG(" %s (%s)", opt_create_session, session_id); |
| 112 | |
| 113 | error: |
| 114 | return ret; |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * process_opt_list_sessions |
| 119 | * |
| 120 | * Get the list of available sessions from |
| 121 | * the session daemon and print it to user. |
| 122 | */ |
| 123 | static int process_opt_list_sessions(void) |
| 124 | { |
| 125 | int ret, count, i; |
| 126 | struct lttng_session *sess; |
| 127 | |
| 128 | count = lttng_list_sessions(&sess); |
| 129 | if (count < 0) { |
| 130 | ret = count; |
| 131 | goto error; |
| 132 | } |
| 133 | |
| 134 | MSG("Available sessions [Name (uuid)]:"); |
| 135 | for (i = 0; i < count; i++) { |
| 136 | MSG("\tName: %s (uuid: %s)", sess[i].name, sess[i].uuid); |
| 137 | } |
| 138 | |
| 139 | free(sess); |
| 140 | MSG("\nTo select a session, use --session UUID."); |
| 141 | |
| 142 | return 0; |
| 143 | |
| 144 | error: |
| 145 | return ret; |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * process_opt_list_apps |
| 150 | * |
| 151 | * Get the UST traceable pid list and print |
| 152 | * them to the user. |
| 153 | */ |
| 154 | static int process_opt_list_apps(void) |
| 155 | { |
| 156 | int i, ret, count; |
| 157 | pid_t *pids; |
| 158 | FILE *fp; |
| 159 | char path[24]; /* Can't go bigger than /proc/65535/cmdline */ |
| 160 | char cmdline[PATH_MAX]; |
| 161 | |
| 162 | count = lttng_ust_list_apps(&pids); |
| 163 | if (count < 0) { |
| 164 | ret = count; |
| 165 | goto error; |
| 166 | } |
| 167 | |
| 168 | MSG("LTTng UST traceable application [name (pid)]:"); |
| 169 | for (i=0; i < count; i++) { |
| 170 | snprintf(path, sizeof(path), "/proc/%d/cmdline", pids[i]); |
| 171 | fp = fopen(path, "r"); |
| 172 | if (fp == NULL) { |
| 173 | MSG("\t(not running) (%d)", pids[i]); |
| 174 | continue; |
| 175 | } |
| 176 | ret = fread(cmdline, 1, sizeof(cmdline), fp); |
| 177 | MSG("\t%s (%d)", cmdline, pids[i]); |
| 178 | fclose(fp); |
| 179 | } |
| 180 | |
| 181 | /* Allocated by lttng_ust_list_apps() */ |
| 182 | free(pids); |
| 183 | |
| 184 | return 0; |
| 185 | |
| 186 | error: |
| 187 | return ret; |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * spawn_sessiond |
| 192 | * |
| 193 | * Spawn a session daemon by forking and execv. |
| 194 | */ |
| 195 | static int spawn_sessiond(char *pathname) |
| 196 | { |
| 197 | int ret = 0; |
| 198 | pid_t pid; |
| 199 | |
| 200 | MSG("Spawning session daemon"); |
| 201 | pid = fork(); |
| 202 | if (pid == 0) { |
| 203 | /* Spawn session daemon and tell |
| 204 | * it to signal us when ready. |
| 205 | */ |
| 206 | ret = execlp(pathname, "ltt-sessiond", "--sig-parent", "--quiet", NULL); |
| 207 | if (ret < 0) { |
| 208 | if (errno == ENOENT) { |
| 209 | ERR("No session daemon found. Use --sessiond-path."); |
| 210 | } else { |
| 211 | perror("execlp"); |
| 212 | } |
| 213 | kill(getppid(), SIGTERM); |
| 214 | exit(EXIT_FAILURE); |
| 215 | } |
| 216 | exit(EXIT_SUCCESS); |
| 217 | } else if (pid > 0) { |
| 218 | /* Wait for ltt-sessiond to start */ |
| 219 | pause(); |
| 220 | goto end; |
| 221 | } else { |
| 222 | perror("fork"); |
| 223 | ret = -1; |
| 224 | goto end; |
| 225 | } |
| 226 | |
| 227 | end: |
| 228 | return ret; |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * check_ltt_sessiond |
| 233 | * |
| 234 | * Check if the session daemon is available using |
| 235 | * the liblttngctl API for the check. If not, try to |
| 236 | * spawn a daemon. |
| 237 | */ |
| 238 | static int check_ltt_sessiond(void) |
| 239 | { |
| 240 | int ret; |
| 241 | char *pathname = NULL; |
| 242 | |
| 243 | ret = lttng_check_session_daemon(); |
| 244 | if (ret < 0) { |
| 245 | /* Try command line option path */ |
| 246 | if (opt_sessiond_path != NULL) { |
| 247 | ret = access(opt_sessiond_path, F_OK | X_OK); |
| 248 | if (ret < 0) { |
| 249 | ERR("No such file: %s", opt_sessiond_path); |
| 250 | goto end; |
| 251 | } |
| 252 | pathname = opt_sessiond_path; |
| 253 | } else { |
| 254 | /* Try LTTNG_SESSIOND_PATH env variable */ |
| 255 | pathname = getenv(LTTNG_SESSIOND_PATH_ENV); |
| 256 | if (pathname != NULL) { |
| 257 | /* strdup here in order to make the free() |
| 258 | * not fail later on. |
| 259 | */ |
| 260 | pathname = strdup(pathname); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | /* Let's rock and roll */ |
| 265 | if (pathname == NULL) { |
| 266 | ret = asprintf(&pathname, "ltt-sessiond"); |
| 267 | if (ret < 0) { |
| 268 | goto end; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | ret = spawn_sessiond(pathname); |
| 273 | free(pathname); |
| 274 | if (ret < 0) { |
| 275 | ERR("Problem occurs when starting %s", pathname); |
| 276 | goto end; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | end: |
| 281 | return ret; |
| 282 | } |
| 283 | |
| 284 | /* |
| 285 | * set_signal_handler |
| 286 | * |
| 287 | * Setup signal handler for SIGCHLD and SIGTERM. |
| 288 | */ |
| 289 | static int set_signal_handler(void) |
| 290 | { |
| 291 | int ret = 0; |
| 292 | struct sigaction sa; |
| 293 | sigset_t sigset; |
| 294 | |
| 295 | if ((ret = sigemptyset(&sigset)) < 0) { |
| 296 | perror("sigemptyset"); |
| 297 | goto end; |
| 298 | } |
| 299 | |
| 300 | sa.sa_handler = sighandler; |
| 301 | sa.sa_mask = sigset; |
| 302 | sa.sa_flags = 0; |
| 303 | if ((ret = sigaction(SIGCHLD, &sa, NULL)) < 0) { |
| 304 | perror("sigaction"); |
| 305 | goto end; |
| 306 | } |
| 307 | |
| 308 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { |
| 309 | perror("sigaction"); |
| 310 | goto end; |
| 311 | } |
| 312 | |
| 313 | end: |
| 314 | return ret; |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * sighandler |
| 319 | * |
| 320 | * Signal handler for the daemon |
| 321 | */ |
| 322 | static void sighandler(int sig) |
| 323 | { |
| 324 | DBG("%d received", sig); |
| 325 | switch (sig) { |
| 326 | case SIGTERM: |
| 327 | clean_exit(EXIT_FAILURE); |
| 328 | break; |
| 329 | case SIGCHLD: |
| 330 | /* Notify is done */ |
| 331 | break; |
| 332 | default: |
| 333 | break; |
| 334 | } |
| 335 | |
| 336 | return; |
| 337 | } |
| 338 | /* |
| 339 | * clean_exit |
| 340 | */ |
| 341 | void clean_exit(int code) |
| 342 | { |
| 343 | DBG("Clean exit"); |
| 344 | exit(code); |
| 345 | } |
| 346 | |
| 347 | /* |
| 348 | * main |
| 349 | */ |
| 350 | int main(int argc, char *argv[]) |
| 351 | { |
| 352 | int ret; |
| 353 | |
| 354 | progname = argv[0] ? argv[0] : "lttng"; |
| 355 | |
| 356 | /* For Mathieu Desnoyers aka Dr Tracing */ |
| 357 | if (strncmp(progname, "drtrace", 7) == 0) { |
| 358 | MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0); |
| 359 | } |
| 360 | |
| 361 | ret = parse_args(argc, (const char **) argv); |
| 362 | if (ret < 0) { |
| 363 | return EXIT_FAILURE; |
| 364 | } |
| 365 | |
| 366 | ret = set_signal_handler(); |
| 367 | if (ret < 0) { |
| 368 | return ret; |
| 369 | } |
| 370 | |
| 371 | if (opt_tracing_group != NULL) { |
| 372 | DBG("Set tracing group to '%s'", opt_tracing_group); |
| 373 | lttng_set_tracing_group(opt_tracing_group); |
| 374 | } |
| 375 | |
| 376 | /* If ask for kernel tracing, need root perms */ |
| 377 | if (opt_trace_kernel) { |
| 378 | DBG("Kernel tracing activated"); |
| 379 | if (getuid() != 0) { |
| 380 | ERR("%s must be setuid root", progname); |
| 381 | return -EPERM; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | /* Check if the lttng session daemon is running. |
| 386 | * If no, a daemon will be spawned. |
| 387 | */ |
| 388 | if (opt_no_sessiond == 0 && (check_ltt_sessiond() < 0)) { |
| 389 | return EXIT_FAILURE; |
| 390 | } |
| 391 | |
| 392 | ret = process_client_opt(); |
| 393 | if (ret < 0) { |
| 394 | return ret; |
| 395 | } |
| 396 | |
| 397 | return 0; |
| 398 | } |