965ab94feb66f45ca5d89752a3da5661f341d415
[lttng-tools.git] / src / bin / lttng / commands / stop.c
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 ret = lttng_stop_tracing_no_wait(session_name);
83 if (ret < 0) {
84 switch (-ret) {
85 case LTTNG_ERR_TRACE_ALREADY_STOPPED:
86 WARN("Tracing already stopped for session %s", session_name);
87 break;
88 default:
89 ERR("%s", lttng_strerror(ret));
90 break;
91 }
92 goto free_name;
93 }
94
95 if (!opt_no_wait) {
96 _MSG("Waiting for data availability");
97 fflush(stdout);
98 do {
99 ret = lttng_data_pending(session_name);
100 if (ret < 0) {
101 /* Return the data available call error. */
102 goto error;
103 }
104
105 /*
106 * Data sleep time before retrying (in usec). Don't sleep if the call
107 * returned value indicates availability.
108 */
109 if (ret) {
110 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME);
111 _MSG(".");
112 fflush(stdout);
113 }
114 } while (ret != 0);
115 MSG("");
116 }
117
118 ret = CMD_SUCCESS;
119
120 MSG("Tracing stopped for session %s", session_name);
121
122 free_name:
123 if (opt_session_name == NULL) {
124 free(session_name);
125 }
126
127 error:
128 return ret;
129 }
130
131 /*
132 * cmd_stop
133 *
134 * The 'stop <options>' first level command
135 */
136 int cmd_stop(int argc, const char **argv)
137 {
138 int opt, ret = CMD_SUCCESS;
139 static poptContext pc;
140
141 pc = poptGetContext(NULL, argc, argv, long_options, 0);
142 poptReadDefaultConfig(pc, 0);
143
144 /* TODO: mi support */
145 if (lttng_opt_mi) {
146 ret = -LTTNG_ERR_MI_NOT_IMPLEMENTED;
147 ERR("mi option not supported");
148 goto end;
149 }
150
151 while ((opt = poptGetNextOpt(pc)) != -1) {
152 switch (opt) {
153 case OPT_HELP:
154 usage(stdout);
155 goto end;
156 case OPT_LIST_OPTIONS:
157 list_cmd_options(stdout, long_options);
158 goto end;
159 default:
160 usage(stderr);
161 ret = CMD_UNDEFINED;
162 goto end;
163 }
164 }
165
166 opt_session_name = (char*) poptGetArg(pc);
167
168 ret = stop_tracing();
169
170 end:
171 poptFreeContext(pc);
172 return ret;
173 }
This page took 0.048034 seconds and 3 git commands to generate.