relayd: track the health thread's poll fd with fd-tracker
[lttng-tools.git] / src / bin / lttng / commands / start.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 _LGPL_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 <common/sessiond-comm/sessiond-comm.h>
28 #include <common/mi-lttng.h>
29
30 #include "../command.h"
31
32
33 static char *opt_session_name;
34 static struct mi_writer *writer;
35
36 #ifdef LTTNG_EMBED_HELP
37 static const char help_msg[] =
38 #include <lttng-start.1.h>
39 ;
40 #endif
41
42 enum {
43 OPT_HELP = 1,
44 OPT_LIST_OPTIONS,
45 };
46
47 static struct poptOption long_options[] = {
48 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
49 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
50 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
51 {0, 0, 0, 0, 0, 0, 0}
52 };
53
54 static int mi_print_session(char *session_name, int enabled)
55 {
56 int ret;
57
58 /* Open session element */
59 ret = mi_lttng_writer_open_element(writer, config_element_session);
60 if (ret) {
61 goto end;
62 }
63
64 /* Print session name element */
65 ret = mi_lttng_writer_write_element_string(writer, config_element_name,
66 session_name);
67 if (ret) {
68 goto end;
69 }
70
71 ret = mi_lttng_writer_write_element_bool(writer, config_element_enabled,
72 enabled);
73 if (ret) {
74 goto end;
75 }
76
77 /* Close session element */
78 ret = mi_lttng_writer_close_element(writer);
79
80 end:
81 return ret;
82 }
83
84 /*
85 * start_tracing
86 *
87 * Start tracing for all trace of the session.
88 */
89 static int start_tracing(void)
90 {
91 int ret;
92 char *session_name;
93
94 if (opt_session_name == NULL) {
95 session_name = get_session_name();
96 if (session_name == NULL) {
97 ret = CMD_ERROR;
98 goto error;
99 }
100 } else {
101 session_name = opt_session_name;
102 }
103
104 DBG("Starting tracing for session %s", session_name);
105
106 ret = lttng_start_tracing(session_name);
107 if (ret < 0) {
108 switch (-ret) {
109 case LTTNG_ERR_TRACE_ALREADY_STARTED:
110 WARN("Tracing already started for session %s", session_name);
111 break;
112 default:
113 ERR("%s", lttng_strerror(ret));
114 break;
115 }
116 goto free_name;
117 }
118
119 ret = CMD_SUCCESS;
120
121 MSG("Tracing started for session %s", session_name);
122 if (lttng_opt_mi) {
123 ret = mi_print_session(session_name, 1);
124 if (ret) {
125 ret = CMD_ERROR;
126 goto free_name;
127 }
128 }
129
130 free_name:
131 if (opt_session_name == NULL) {
132 free(session_name);
133 }
134 error:
135 return ret;
136 }
137
138 /*
139 * cmd_start
140 *
141 * The 'start <options>' first level command
142 */
143 int cmd_start(int argc, const char **argv)
144 {
145 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
146 static poptContext pc;
147 const char *leftover = NULL;
148
149 pc = poptGetContext(NULL, argc, argv, long_options, 0);
150 poptReadDefaultConfig(pc, 0);
151
152 while ((opt = poptGetNextOpt(pc)) != -1) {
153 switch (opt) {
154 case OPT_HELP:
155 SHOW_HELP();
156 goto end;
157 case OPT_LIST_OPTIONS:
158 list_cmd_options(stdout, long_options);
159 goto end;
160 default:
161 ret = CMD_UNDEFINED;
162 goto end;
163 }
164 }
165
166 opt_session_name = (char*) poptGetArg(pc);
167
168 leftover = poptGetArg(pc);
169 if (leftover) {
170 ERR("Unknown argument: %s", leftover);
171 ret = CMD_ERROR;
172 goto end;
173 }
174
175 /* Mi check */
176 if (lttng_opt_mi) {
177 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
178 if (!writer) {
179 ret = -LTTNG_ERR_NOMEM;
180 goto end;
181 }
182
183 /* Open command element */
184 ret = mi_lttng_writer_command_open(writer,
185 mi_lttng_element_command_start);
186 if (ret) {
187 ret = CMD_ERROR;
188 goto end;
189 }
190
191 /* Open output element */
192 ret = mi_lttng_writer_open_element(writer,
193 mi_lttng_element_command_output);
194 if (ret) {
195 ret = CMD_ERROR;
196 goto end;
197 }
198
199 /*
200 * Open sessions element
201 * For validation purpose
202 */
203 ret = mi_lttng_writer_open_element(writer,
204 config_element_sessions);
205 if (ret) {
206 ret = CMD_ERROR;
207 goto end;
208 }
209 }
210
211 command_ret = start_tracing();
212 if (command_ret) {
213 success = 0;
214 }
215
216 /* Mi closing */
217 if (lttng_opt_mi) {
218 /* Close sessions and output element */
219 ret = mi_lttng_close_multi_element(writer, 2);
220 if (ret) {
221 ret = CMD_ERROR;
222 goto end;
223 }
224
225 /* Success ? */
226 ret = mi_lttng_writer_write_element_bool(writer,
227 mi_lttng_element_command_success, success);
228 if (ret) {
229 ret = CMD_ERROR;
230 goto end;
231 }
232
233 /* Command element close */
234 ret = mi_lttng_writer_command_close(writer);
235 if (ret) {
236 ret = CMD_ERROR;
237 goto end;
238 }
239 }
240
241 end:
242 /* Mi clean-up */
243 if (writer && mi_lttng_writer_destroy(writer)) {
244 /* Preserve original error code */
245 ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL;
246 }
247
248 /* Overwrite ret if an error occurred with start_tracing */
249 ret = command_ret ? command_ret : ret;
250 poptFreeContext(pc);
251 return ret;
252 }
This page took 0.043984 seconds and 4 git commands to generate.