Commit | Line | Data |
---|---|---|
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 <time.h> | |
26 | #include <unistd.h> | |
27 | ||
c399183f | 28 | #include "../command.h" |
679b4943 | 29 | #include "../utils.h" |
f3ed775e | 30 | |
00e2e675 | 31 | #include <common/defaults.h> |
42224349 | 32 | #include <common/sessiond-comm/sessiond-comm.h> |
00e2e675 | 33 | #include <common/uri.h> |
81b86775 | 34 | #include <common/utils.h> |
42224349 | 35 | |
f3ed775e DG |
36 | static char *opt_output_path; |
37 | static char *opt_session_name; | |
00e2e675 DG |
38 | static char *opt_uris; |
39 | static char *opt_ctrl_uris; | |
40 | static char *opt_data_uris; | |
41 | static int opt_no_consumer = 1; | |
f3ed775e DG |
42 | |
43 | enum { | |
44 | OPT_HELP = 1, | |
679b4943 | 45 | OPT_LIST_OPTIONS, |
f3ed775e DG |
46 | }; |
47 | ||
48 | static struct poptOption long_options[] = { | |
49 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
679b4943 SM |
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}, | |
00e2e675 DG |
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}, | |
f3ed775e DG |
57 | {0, 0, 0, 0, 0, 0, 0} |
58 | }; | |
59 | ||
60 | /* | |
61 | * usage | |
62 | */ | |
63 | static void usage(FILE *ofp) | |
64 | { | |
65 | fprintf(ofp, "usage: lttng create [options] [NAME]\n"); | |
66 | fprintf(ofp, "\n"); | |
1c8d13c8 | 67 | fprintf(ofp, " The default NAME is 'auto-yyyymmdd-hhmmss'\n"); |
f3ed775e | 68 | fprintf(ofp, " -h, --help Show this help\n"); |
1c8d13c8 | 69 | fprintf(ofp, " --list-options Simple listing of options\n"); |
58a97671 | 70 | fprintf(ofp, " -o, --output PATH Specify output path for traces\n"); |
00e2e675 DG |
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"); | |
f3ed775e DG |
78 | fprintf(ofp, "\n"); |
79 | } | |
80 | ||
00e2e675 DG |
81 | /* |
82 | * Parse URI from string to lttng_uri object array. | |
83 | */ | |
84 | static 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 | */ | |
118 | static 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 | ||
142 | end: | |
143 | return; | |
144 | } | |
145 | ||
f3ed775e | 146 | /* |
1c8d13c8 TD |
147 | * Create a tracing session. |
148 | * If no name is specified, a default name is generated. | |
f3ed775e | 149 | * |
1c8d13c8 | 150 | * Returns one of the CMD_* result constants. |
f3ed775e DG |
151 | */ |
152 | static int create_session() | |
153 | { | |
00e2e675 | 154 | int ret, have_name = 0, i; |
d6175221 | 155 | char datetime[16]; |
58a97671 | 156 | char *session_name, *traces_path = NULL, *alloc_path = NULL; |
f3ed775e | 157 | time_t rawtime; |
00e2e675 | 158 | ssize_t size; |
f3ed775e | 159 | struct tm *timeinfo; |
00e2e675 | 160 | struct lttng_uri *uris = NULL, *ctrl_uri = NULL, *data_uri = NULL; |
f3ed775e | 161 | |
d6175221 DG |
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 | ||
f3ed775e DG |
167 | /* Auto session name creation */ |
168 | if (opt_session_name == NULL) { | |
ffbf37df | 169 | ret = asprintf(&session_name, "auto-%s", datetime); |
d6175221 DG |
170 | if (ret < 0) { |
171 | perror("asprintf session name"); | |
172 | goto error; | |
173 | } | |
f3ed775e DG |
174 | DBG("Auto session name set to %s", session_name); |
175 | } else { | |
176 | session_name = opt_session_name; | |
ffbf37df | 177 | have_name = 1; |
f3ed775e DG |
178 | } |
179 | ||
00e2e675 | 180 | if (opt_output_path != NULL) { |
81b86775 | 181 | traces_path = utils_expand_path(opt_output_path); |
00e2e675 DG |
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 */ | |
d386b234 | 266 | alloc_path = config_get_default_path(); |
f3ed775e | 267 | if (alloc_path == NULL) { |
d386b234 | 268 | ERR("HOME path not found.\n \ |
00e2e675 | 269 | Please specify an output path using -o, --output PATH"); |
f3ed775e DG |
270 | ret = CMD_FATAL; |
271 | goto error; | |
272 | } | |
d386b234 | 273 | alloc_path = strdup(alloc_path); |
f3ed775e | 274 | |
ffbf37df | 275 | if (have_name) { |
00e2e675 | 276 | ret = asprintf(&traces_path, "file://%s/" DEFAULT_TRACE_DIR_NAME |
ffbf37df DG |
277 | "/%s-%s", alloc_path, session_name, datetime); |
278 | } else { | |
00e2e675 | 279 | ret = asprintf(&traces_path, "file://%s/" DEFAULT_TRACE_DIR_NAME |
ffbf37df DG |
280 | "/%s", alloc_path, session_name); |
281 | } | |
58a97671 | 282 | if (ret < 0) { |
00e2e675 DG |
283 | PERROR("asprintf trace dir name"); |
284 | ret = CMD_FATAL; | |
58a97671 DG |
285 | goto error; |
286 | } | |
00e2e675 DG |
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"); | |
3badf2bf DG |
306 | goto error; |
307 | } | |
00e2e675 | 308 | DBG("Subdir update to %s", ctrl_uri->subdir); |
f3ed775e DG |
309 | } |
310 | ||
00e2e675 DG |
311 | ret = lttng_create_session_uri(session_name, ctrl_uri, data_uri, |
312 | opt_no_consumer); | |
f3ed775e | 313 | if (ret < 0) { |
ae856491 | 314 | /* Don't set ret so lttng can interpret the sessiond error. */ |
42224349 DG |
315 | switch (-ret) { |
316 | case LTTCOMM_EXIST_SESS: | |
317 | WARN("Session %s already exists", session_name); | |
318 | break; | |
319 | } | |
f3ed775e DG |
320 | goto error; |
321 | } | |
322 | ||
58a97671 DG |
323 | /* Init lttng session config */ |
324 | ret = config_init(session_name); | |
f3ed775e | 325 | if (ret < 0) { |
27089920 | 326 | ret = CMD_ERROR; |
f3ed775e DG |
327 | goto error; |
328 | } | |
329 | ||
330 | MSG("Session %s created.", session_name); | |
00e2e675 DG |
331 | if (ctrl_uri->dtype == LTTNG_DST_PATH) { |
332 | MSG("Traces will be written in %s" , ctrl_uri->dst.path); | |
333 | } | |
f3ed775e DG |
334 | |
335 | ret = CMD_SUCCESS; | |
336 | ||
337 | error: | |
54bd3caf TD |
338 | if (opt_session_name == NULL) { |
339 | free(session_name); | |
340 | } | |
341 | ||
f3ed775e DG |
342 | if (alloc_path) { |
343 | free(alloc_path); | |
344 | } | |
345 | ||
58a97671 DG |
346 | if (traces_path) { |
347 | free(traces_path); | |
f3ed775e DG |
348 | } |
349 | return ret; | |
350 | } | |
351 | ||
352 | /* | |
74cc1d0f | 353 | * The 'create <options>' first level command |
1c8d13c8 TD |
354 | * |
355 | * Returns one of the CMD_* result constants. | |
f3ed775e DG |
356 | */ |
357 | int 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: | |
27089920 | 368 | usage(stdout); |
f3ed775e | 369 | goto end; |
679b4943 SM |
370 | case OPT_LIST_OPTIONS: |
371 | list_cmd_options(stdout, long_options); | |
679b4943 | 372 | goto end; |
f3ed775e DG |
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 | ||
384 | end: | |
ca1c3607 | 385 | poptFreeContext(pc); |
f3ed775e DG |
386 | return ret; |
387 | } |