Fix: check malloc return value
[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
38ee087f
DG
82 if (opt_no_wait) {
83 ret = lttng_stop_tracing_no_wait(session_name);
84 } else {
85 ret = lttng_stop_tracing(session_name);
86 }
f3ed775e 87 if (ret < 0) {
42224349 88 switch (-ret) {
f73fabfd 89 case LTTNG_ERR_TRACE_ALREADY_STOPPED:
42224349
DG
90 WARN("Tracing already stopped for session %s", session_name);
91 break;
92 default:
93 ERR("%s", lttng_strerror(ret));
94 break;
95 }
f3ed775e
DG
96 goto free_name;
97 }
98
ae856491
DG
99 ret = CMD_SUCCESS;
100
f3ed775e
DG
101 MSG("Tracing stopped for session %s", session_name);
102
103free_name:
b73d0b29
MD
104 if (opt_session_name == NULL) {
105 free(session_name);
106 }
cd80958d 107
f3ed775e
DG
108error:
109 return ret;
110}
111
112/*
113 * cmd_stop
114 *
115 * The 'stop <options>' first level command
116 */
117int cmd_stop(int argc, const char **argv)
118{
ca1c3607 119 int opt, ret = CMD_SUCCESS;
f3ed775e
DG
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:
ca1c3607 128 usage(stdout);
f3ed775e 129 goto end;
679b4943
SM
130 case OPT_LIST_OPTIONS:
131 list_cmd_options(stdout, long_options);
679b4943 132 goto end;
f3ed775e
DG
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
144end:
ca1c3607 145 poptFreeContext(pc);
f3ed775e
DG
146 return ret;
147}
This page took 0.040053 seconds and 4 git commands to generate.