| 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, version 2 only, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | |
| 18 | #define _GNU_SOURCE |
| 19 | #include <popt.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <unistd.h> |
| 26 | |
| 27 | #include "../command.h" |
| 28 | |
| 29 | #include <common/sessiond-comm/sessiond-comm.h> |
| 30 | |
| 31 | static char *opt_session_name; |
| 32 | static int opt_no_wait; |
| 33 | |
| 34 | enum { |
| 35 | OPT_HELP = 1, |
| 36 | OPT_LIST_OPTIONS, |
| 37 | }; |
| 38 | |
| 39 | static struct poptOption long_options[] = { |
| 40 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 41 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, |
| 42 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
| 43 | {"no-wait", 'n', POPT_ARG_VAL, &opt_no_wait, 1, 0, 0}, |
| 44 | {0, 0, 0, 0, 0, 0, 0} |
| 45 | }; |
| 46 | |
| 47 | /* |
| 48 | * usage |
| 49 | */ |
| 50 | static void usage(FILE *ofp) |
| 51 | { |
| 52 | fprintf(ofp, "usage: lttng stop [NAME] [OPTIONS]\n"); |
| 53 | fprintf(ofp, "\n"); |
| 54 | fprintf(ofp, "Where NAME is an optional session name. If not specified, lttng will\n"); |
| 55 | fprintf(ofp, "get it from the configuration directory (.lttng).\n"); |
| 56 | fprintf(ofp, "\n"); |
| 57 | fprintf(ofp, "Options:\n"); |
| 58 | fprintf(ofp, " -h, --help Show this help\n"); |
| 59 | fprintf(ofp, " --list-options Simple listing of options\n"); |
| 60 | fprintf(ofp, " -n, --no-wait Don't wait for data availability\n"); |
| 61 | fprintf(ofp, "\n"); |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | * Start tracing for all trace of the session. |
| 66 | */ |
| 67 | static int stop_tracing(void) |
| 68 | { |
| 69 | int ret; |
| 70 | char *session_name; |
| 71 | |
| 72 | if (opt_session_name == NULL) { |
| 73 | session_name = get_session_name(); |
| 74 | if (session_name == NULL) { |
| 75 | ret = CMD_ERROR; |
| 76 | goto error; |
| 77 | } |
| 78 | } else { |
| 79 | session_name = opt_session_name; |
| 80 | } |
| 81 | |
| 82 | if (opt_no_wait) { |
| 83 | ret = lttng_stop_tracing_no_wait(session_name); |
| 84 | } else { |
| 85 | ret = lttng_stop_tracing(session_name); |
| 86 | } |
| 87 | if (ret < 0) { |
| 88 | switch (-ret) { |
| 89 | case LTTNG_ERR_TRACE_ALREADY_STOPPED: |
| 90 | WARN("Tracing already stopped for session %s", session_name); |
| 91 | break; |
| 92 | default: |
| 93 | ERR("%s", lttng_strerror(ret)); |
| 94 | break; |
| 95 | } |
| 96 | goto free_name; |
| 97 | } |
| 98 | |
| 99 | ret = CMD_SUCCESS; |
| 100 | |
| 101 | MSG("Tracing stopped for session %s", session_name); |
| 102 | |
| 103 | free_name: |
| 104 | if (opt_session_name == NULL) { |
| 105 | free(session_name); |
| 106 | } |
| 107 | |
| 108 | error: |
| 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * cmd_stop |
| 114 | * |
| 115 | * The 'stop <options>' first level command |
| 116 | */ |
| 117 | int cmd_stop(int argc, const char **argv) |
| 118 | { |
| 119 | int opt, ret = CMD_SUCCESS; |
| 120 | static poptContext pc; |
| 121 | |
| 122 | pc = poptGetContext(NULL, argc, argv, long_options, 0); |
| 123 | poptReadDefaultConfig(pc, 0); |
| 124 | |
| 125 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 126 | switch (opt) { |
| 127 | case OPT_HELP: |
| 128 | usage(stdout); |
| 129 | goto end; |
| 130 | case OPT_LIST_OPTIONS: |
| 131 | list_cmd_options(stdout, long_options); |
| 132 | goto end; |
| 133 | default: |
| 134 | usage(stderr); |
| 135 | ret = CMD_UNDEFINED; |
| 136 | goto end; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | opt_session_name = (char*) poptGetArg(pc); |
| 141 | |
| 142 | ret = stop_tracing(); |
| 143 | |
| 144 | end: |
| 145 | poptFreeContext(pc); |
| 146 | return ret; |
| 147 | } |