Fix: bad file descriptor on close in health check
[lttng-tools.git] / src / bin / lttng / commands / create.c
... / ...
CommitLineData
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 <time.h>
26#include <unistd.h>
27
28#include "../command.h"
29#include "../utils.h"
30
31#include <common/defaults.h>
32#include <common/sessiond-comm/sessiond-comm.h>
33#include <common/uri.h>
34#include <common/utils.h>
35
36static char *opt_output_path;
37static char *opt_session_name;
38static char *opt_uris;
39static char *opt_ctrl_uris;
40static char *opt_data_uris;
41static int opt_no_consumer = 1;
42
43enum {
44 OPT_HELP = 1,
45 OPT_LIST_OPTIONS,
46};
47
48static struct poptOption long_options[] = {
49 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
50 {"help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL},
51 {"output", 'o', POPT_ARG_STRING, &opt_output_path, 0, NULL, NULL},
52 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
53 {"set-uri", 'U', POPT_ARG_STRING, &opt_uris, 0, 0, 0},
54 {"ctrl-uri", 'C', POPT_ARG_STRING, &opt_ctrl_uris, 0, 0, 0},
55 {"data-uri", 'D', POPT_ARG_STRING, &opt_data_uris, 0, 0, 0},
56 {"no-consumer", 0, POPT_ARG_NONE, &opt_no_consumer, 0, 0, 0},
57 {0, 0, 0, 0, 0, 0, 0}
58};
59
60/*
61 * usage
62 */
63static void usage(FILE *ofp)
64{
65 fprintf(ofp, "usage: lttng create [options] [NAME]\n");
66 fprintf(ofp, "\n");
67 fprintf(ofp, " The default NAME is 'auto-yyyymmdd-hhmmss'\n");
68 fprintf(ofp, " -h, --help Show this help\n");
69 fprintf(ofp, " --list-options Simple listing of options\n");
70 fprintf(ofp, " -o, --output PATH Specify output path for traces\n");
71 fprintf(ofp, " -U, --set-uri=URI Set URI for the enable-consumer destination.\n");
72 fprintf(ofp, " It is persistent for the session lifetime.\n");
73 fprintf(ofp, " Redo the command to change it.\n");
74 fprintf(ofp, " This will set both data and control URI for network.\n");
75 fprintf(ofp, " -C, --ctrl-uri=URI Set control path URI.\n");
76 fprintf(ofp, " -D, --data-uri=URI Set data path URI.\n");
77 fprintf(ofp, " --no-consumer Disable consumer for entire tracing session.\n");
78 fprintf(ofp, "\n");
79}
80
81/*
82 * Parse URI from string to lttng_uri object array.
83 */
84static ssize_t parse_uri_from_str(const char *str_uri, struct lttng_uri **uris)
85{
86 int i;
87 ssize_t size;
88 struct lttng_uri *uri;
89
90 if (*uris != NULL) {
91 free(*uris);
92 }
93
94 size = uri_parse(str_uri, uris);
95 if (size < 1) {
96 ERR("Bad URI %s. Either the hostname or IP is invalid", str_uri);
97 size = -1;
98 }
99
100 for (i = 0; i < size; i++) {
101 uri = (struct lttng_uri *) &uris[i];
102 /* Set default port if none was given */
103 if (uri->port == 0) {
104 if (uri->stype == LTTNG_STREAM_CONTROL) {
105 uri->port = DEFAULT_NETWORK_CONTROL_PORT;
106 } else if (uri->stype == LTTNG_STREAM_DATA) {
107 uri->port = DEFAULT_NETWORK_DATA_PORT;
108 }
109 }
110 }
111
112 return size;
113}
114
115/*
116 * Print URI message.
117 */
118static void print_uri_msg(struct lttng_uri *uri)
119{
120 char *dst;
121
122 switch (uri->dtype) {
123 case LTTNG_DST_IPV4:
124 dst = uri->dst.ipv4;
125 break;
126 case LTTNG_DST_IPV6:
127 dst = uri->dst.ipv6;
128 break;
129 case LTTNG_DST_PATH:
130 dst = uri->dst.path;
131 MSG("Consumer destination set to %s", dst);
132 goto end;
133 default:
134 DBG("Unknown URI destination");
135 goto end;
136 }
137
138 MSG("Consumer %s stream set to %s with the %s protocol on port %d",
139 uri->stype == LTTNG_STREAM_CONTROL ? "control" : "data",
140 dst, uri->proto == LTTNG_TCP ? "TCP" : "UNK", uri->port);
141
142end:
143 return;
144}
145
146/*
147 * Create a tracing session.
148 * If no name is specified, a default name is generated.
149 *
150 * Returns one of the CMD_* result constants.
151 */
152static int create_session()
153{
154 int ret, have_name = 0, i;
155 char datetime[16];
156 char *session_name, *traces_path = NULL, *alloc_path = NULL;
157 time_t rawtime;
158 ssize_t size;
159 struct tm *timeinfo;
160 struct lttng_uri *uris = NULL, *ctrl_uri = NULL, *data_uri = NULL;
161
162 /* Get date and time for automatic session name/path */
163 time(&rawtime);
164 timeinfo = localtime(&rawtime);
165 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
166
167 /* Auto session name creation */
168 if (opt_session_name == NULL) {
169 ret = asprintf(&session_name, "auto-%s", datetime);
170 if (ret < 0) {
171 perror("asprintf session name");
172 goto error;
173 }
174 DBG("Auto session name set to %s", session_name);
175 } else {
176 session_name = opt_session_name;
177 have_name = 1;
178 }
179
180 if (opt_output_path != NULL) {
181 traces_path = utils_expand_path(opt_output_path);
182 if (traces_path == NULL) {
183 ret = CMD_ERROR;
184 goto error;
185 }
186
187 ret = asprintf(&alloc_path, "file://%s", traces_path);
188 if (ret < 0) {
189 PERROR("asprintf expand path");
190 ret = CMD_FATAL;
191 goto error;
192 }
193
194 ret = uri_parse(alloc_path, &ctrl_uri);
195 if (ret < 1) {
196 ret = CMD_FATAL;
197 goto error;
198 }
199 } else if (opt_uris) { /* Handling URIs (-U opt) */
200 size = parse_uri_from_str(opt_uris, &uris);
201 if (size < 1) {
202 ret = CMD_ERROR;
203 goto error;
204 } else if (size == 1 && uris[0].dtype != LTTNG_DST_PATH) {
205 ERR("Only net:// and file:// are supported. "
206 "Use -C and -D for more fine grained control");
207 ret = CMD_ERROR;
208 goto error;
209 } else if (size == 2) {
210 uris[0].stype = LTTNG_STREAM_CONTROL;
211 uris[1].stype = LTTNG_STREAM_DATA;
212
213 for (i = 0; i < size; i++) {
214 /* Set default port if none was given */
215 if (uris[i].port == 0) {
216 if (uris[i].stype == LTTNG_STREAM_CONTROL) {
217 uris[i].port = DEFAULT_NETWORK_CONTROL_PORT;
218 } else {
219 uris[i].port = DEFAULT_NETWORK_DATA_PORT;
220 }
221 }
222 }
223
224 ctrl_uri = &uris[0];
225 print_uri_msg(ctrl_uri);
226 data_uri = &uris[1];
227 print_uri_msg(data_uri);
228 } else {
229 ctrl_uri = &uris[0];
230 print_uri_msg(ctrl_uri);
231 }
232 } else if (opt_ctrl_uris || opt_data_uris) {
233 /* Setting up control URI (-C opt) */
234 if (opt_ctrl_uris) {
235 size = parse_uri_from_str(opt_ctrl_uris, &uris);
236 if (size < 1) {
237 ret = CMD_ERROR;
238 goto error;
239 }
240 ctrl_uri = &uris[0];
241 ctrl_uri->stype = LTTNG_STREAM_CONTROL;
242 /* Set default port if none specified */
243 if (ctrl_uri->port == 0) {
244 ctrl_uri->port = DEFAULT_NETWORK_CONTROL_PORT;
245 }
246 print_uri_msg(ctrl_uri);
247 }
248
249 /* Setting up data URI (-D opt) */
250 if (opt_data_uris) {
251 size = parse_uri_from_str(opt_data_uris, &uris);
252 if (size < 1) {
253 ret = CMD_ERROR;
254 goto error;
255 }
256 data_uri = &uris[0];
257 data_uri->stype = LTTNG_STREAM_DATA;
258 /* Set default port if none specified */
259 if (data_uri->port == 0) {
260 data_uri->port = DEFAULT_NETWORK_DATA_PORT;
261 }
262 print_uri_msg(data_uri);
263 }
264 } else {
265 /* Auto output path */
266 alloc_path = config_get_default_path();
267 if (alloc_path == NULL) {
268 ERR("HOME path not found.\n \
269 Please specify an output path using -o, --output PATH");
270 ret = CMD_FATAL;
271 goto error;
272 }
273 alloc_path = strdup(alloc_path);
274
275 if (have_name) {
276 ret = asprintf(&traces_path, "file://%s/" DEFAULT_TRACE_DIR_NAME
277 "/%s-%s", alloc_path, session_name, datetime);
278 } else {
279 ret = asprintf(&traces_path, "file://%s/" DEFAULT_TRACE_DIR_NAME
280 "/%s", alloc_path, session_name);
281 }
282 if (ret < 0) {
283 PERROR("asprintf trace dir name");
284 ret = CMD_FATAL;
285 goto error;
286 }
287
288 ret = uri_parse(traces_path, &ctrl_uri);
289 if (ret < 1) {
290 ret = CMD_FATAL;
291 goto error;
292 }
293 }
294
295 /* If there is no subdir specified and the URI are network */
296 if (strlen(ctrl_uri->subdir) == 0) {
297 if (have_name) {
298 ret = snprintf(ctrl_uri->subdir, sizeof(ctrl_uri->subdir), "%s-%s",
299 session_name, datetime);
300 } else {
301 ret = snprintf(ctrl_uri->subdir, sizeof(ctrl_uri->subdir), "%s",
302 session_name);
303 }
304 if (ret < 0) {
305 PERROR("snprintf subdir");
306 goto error;
307 }
308 DBG("Subdir update to %s", ctrl_uri->subdir);
309 }
310
311 ret = lttng_create_session_uri(session_name, ctrl_uri, data_uri,
312 opt_no_consumer);
313 if (ret < 0) {
314 /* Don't set ret so lttng can interpret the sessiond error. */
315 switch (-ret) {
316 case LTTCOMM_EXIST_SESS:
317 WARN("Session %s already exists", session_name);
318 break;
319 }
320 goto error;
321 }
322
323 /* Init lttng session config */
324 ret = config_init(session_name);
325 if (ret < 0) {
326 ret = CMD_ERROR;
327 goto error;
328 }
329
330 MSG("Session %s created.", session_name);
331 if (ctrl_uri->dtype == LTTNG_DST_PATH) {
332 MSG("Traces will be written in %s" , ctrl_uri->dst.path);
333 }
334
335 ret = CMD_SUCCESS;
336
337error:
338 if (opt_session_name == NULL) {
339 free(session_name);
340 }
341
342 if (alloc_path) {
343 free(alloc_path);
344 }
345
346 if (traces_path) {
347 free(traces_path);
348 }
349 return ret;
350}
351
352/*
353 * The 'create <options>' first level command
354 *
355 * Returns one of the CMD_* result constants.
356 */
357int cmd_create(int argc, const char **argv)
358{
359 int opt, ret = CMD_SUCCESS;
360 static poptContext pc;
361
362 pc = poptGetContext(NULL, argc, argv, long_options, 0);
363 poptReadDefaultConfig(pc, 0);
364
365 while ((opt = poptGetNextOpt(pc)) != -1) {
366 switch (opt) {
367 case OPT_HELP:
368 usage(stdout);
369 goto end;
370 case OPT_LIST_OPTIONS:
371 list_cmd_options(stdout, long_options);
372 goto end;
373 default:
374 usage(stderr);
375 ret = CMD_UNDEFINED;
376 goto end;
377 }
378 }
379
380 opt_session_name = (char*) poptGetArg(pc);
381
382 ret = create_session();
383
384end:
385 poptFreeContext(pc);
386 return ret;
387}
This page took 0.023794 seconds and 4 git commands to generate.