Fix: Don't output to stdout from lttng-ctl
[lttng-tools.git] / src / bin / lttng / commands / stop.c
CommitLineData
f3ed775e
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
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.
f3ed775e
DG
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 *
d14d33bf
AM
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.
f3ed775e
DG
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
c399183f 27#include "../command.h"
f3ed775e 28
42224349
DG
29#include <common/sessiond-comm/sessiond-comm.h>
30
f3ed775e 31static char *opt_session_name;
38ee087f 32static int opt_no_wait;
f3ed775e
DG
33
34enum {
35 OPT_HELP = 1,
679b4943 36 OPT_LIST_OPTIONS,
f3ed775e
DG
37};
38
39static struct poptOption long_options[] = {
40 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
41 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
679b4943 42 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
38ee087f 43 {"no-wait", 'n', POPT_ARG_VAL, &opt_no_wait, 1, 0, 0},
f3ed775e
DG
44 {0, 0, 0, 0, 0, 0, 0}
45};
46
47/*
48 * usage
49 */
50static void usage(FILE *ofp)
51{
32a6298d 52 fprintf(ofp, "usage: lttng stop [NAME] [OPTIONS]\n");
f3ed775e
DG
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");
32a6298d 57 fprintf(ofp, "Options:\n");
f3ed775e 58 fprintf(ofp, " -h, --help Show this help\n");
679b4943 59 fprintf(ofp, " --list-options Simple listing of options\n");
38ee087f 60 fprintf(ofp, " -n, --no-wait Don't wait for data availability\n");
f3ed775e
DG
61 fprintf(ofp, "\n");
62}
63
64/*
cd80958d 65 * Start tracing for all trace of the session.
f3ed775e
DG
66 */
67static int stop_tracing(void)
68{
ae856491 69 int ret;
f3ed775e
DG
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
8eb7a5e2 82 ret = lttng_stop_tracing_no_wait(session_name);
f3ed775e 83 if (ret < 0) {
42224349 84 switch (-ret) {
f73fabfd 85 case LTTNG_ERR_TRACE_ALREADY_STOPPED:
42224349
DG
86 WARN("Tracing already stopped for session %s", session_name);
87 break;
88 default:
89 ERR("%s", lttng_strerror(ret));
90 break;
91 }
f3ed775e
DG
92 goto free_name;
93 }
94
8eb7a5e2
JG
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
ae856491
DG
118 ret = CMD_SUCCESS;
119
f3ed775e
DG
120 MSG("Tracing stopped for session %s", session_name);
121
122free_name:
b73d0b29
MD
123 if (opt_session_name == NULL) {
124 free(session_name);
125 }
cd80958d 126
f3ed775e
DG
127error:
128 return ret;
129}
130
131/*
132 * cmd_stop
133 *
134 * The 'stop <options>' first level command
135 */
136int cmd_stop(int argc, const char **argv)
137{
ca1c3607 138 int opt, ret = CMD_SUCCESS;
f3ed775e
DG
139 static poptContext pc;
140
141 pc = poptGetContext(NULL, argc, argv, long_options, 0);
142 poptReadDefaultConfig(pc, 0);
143
144 while ((opt = poptGetNextOpt(pc)) != -1) {
145 switch (opt) {
146 case OPT_HELP:
ca1c3607 147 usage(stdout);
f3ed775e 148 goto end;
679b4943
SM
149 case OPT_LIST_OPTIONS:
150 list_cmd_options(stdout, long_options);
679b4943 151 goto end;
f3ed775e
DG
152 default:
153 usage(stderr);
154 ret = CMD_UNDEFINED;
155 goto end;
156 }
157 }
158
159 opt_session_name = (char*) poptGetArg(pc);
160
161 ret = stop_tracing();
162
163end:
ca1c3607 164 poptFreeContext(pc);
f3ed775e
DG
165 return ret;
166}
This page took 0.041064 seconds and 4 git commands to generate.