Commit | Line | Data |
---|---|---|
00e2e675 DG |
1 | /* |
2 | * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms of the GNU General Public License, version 2 only, as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
11 | * more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along with | |
14 | * this program; if not, write to the Free Software Foundation, Inc., 51 | |
15 | * 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 | ||
35 | static int opt_kernel; | |
36 | static int opt_userspace; | |
37 | static int opt_enable; | |
38 | static char *opt_session_name; | |
39 | static char *opt_uris; | |
40 | static char *opt_ctrl_uris; | |
41 | static char *opt_data_uris; | |
42 | static char *opt_uris_arg; | |
43 | ||
44 | static struct lttng_handle *handle; | |
45 | ||
46 | enum { | |
47 | OPT_HELP = 1, | |
48 | OPT_LIST_OPTIONS, | |
49 | }; | |
50 | ||
51 | static struct poptOption long_options[] = { | |
52 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
53 | {"help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL}, | |
54 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, | |
55 | {"session", 's', POPT_ARG_STRING, &opt_session_name, 0, 0, 0}, | |
56 | {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0}, | |
57 | {"userspace", 'u', POPT_ARG_VAL, &opt_userspace, 1, 0, 0}, | |
58 | {"set-uri", 'U', POPT_ARG_STRING, &opt_uris, 0, 0, 0}, | |
59 | {"ctrl-uri", 'C', POPT_ARG_STRING, &opt_ctrl_uris, 0, 0, 0}, | |
60 | {"data-uri", 'D', POPT_ARG_STRING, &opt_data_uris, 0, 0, 0}, | |
61 | {"enable", 'e', POPT_ARG_VAL, &opt_enable, 1, 0, 0}, | |
62 | {0, 0, 0, 0, 0, 0, 0} | |
63 | }; | |
64 | ||
65 | /* | |
66 | * usage | |
67 | */ | |
68 | static void usage(FILE *ofp) | |
69 | { | |
70 | fprintf(ofp, "usage: lttng enable-consumer [-u|-k] [URI] [OPTIONS]\n"); | |
71 | fprintf(ofp, "\n"); | |
72 | fprintf(ofp, "The default behavior is to enable a consumer to the current URI.\n"); | |
73 | fprintf(ofp, "The default URI is the local filesystem at the path of the session.\n"); | |
74 | fprintf(ofp, "\n"); | |
75 | fprintf(ofp, "The enable-consumer feature supports both local and network transport.\n"); | |
76 | fprintf(ofp, "You must have a running lttng-relayd for network transmission.\n"); | |
77 | fprintf(ofp, "\n"); | |
78 | fprintf(ofp, "You can optionally specify two URIs for respectively the\n"); | |
79 | fprintf(ofp, "control and data channel. URI supported:\n"); | |
80 | fprintf(ofp, " > file://PATH\n"); | |
81 | fprintf(ofp, " Local file full system path.\n"); | |
82 | fprintf(ofp, "\n"); | |
83 | fprintf(ofp, " > net://DST[:CTRL_PORT[:DATA_PORT]] and net6://...\n"); | |
84 | fprintf(ofp, " This will use the default network transport layer which is\n"); | |
85 | fprintf(ofp, " TCP for both control and data port. The default ports are\n"); | |
86 | fprintf(ofp, " respectively 5342 and 5343.\n"); | |
87 | fprintf(ofp, " Example:\n"); | |
88 | fprintf(ofp, " # lttng enable-consumer net://192.168.1.42 -k\n"); | |
89 | fprintf(ofp, " Uses TCP and default ports for the given destination.\n"); | |
90 | fprintf(ofp, "\n"); | |
91 | fprintf(ofp, " > tcp://DST:PORT and tcp6://DST:PORT\n"); | |
92 | fprintf(ofp, "\n"); | |
93 | fprintf(ofp, "Options:\n"); | |
94 | fprintf(ofp, " -h, --help Show this help\n"); | |
95 | fprintf(ofp, " --list-options Simple listing of options\n"); | |
96 | fprintf(ofp, " -s, --session=NAME Apply to session name\n"); | |
97 | fprintf(ofp, " -k, --kernel Apply to the kernel tracer\n"); | |
98 | fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n"); | |
99 | //fprintf(ofp, " -U, --set-uri=URI1[,URI2,...]\n"); | |
100 | fprintf(ofp, "\n"); | |
101 | fprintf(ofp, "Extended Options:\n"); | |
102 | fprintf(ofp, "\n"); | |
103 | fprintf(ofp, "Using these options, each API call is controlled individually.\n"); | |
104 | fprintf(ofp, "For instance, -C does not enable the consumer automatically.\n"); | |
105 | fprintf(ofp, "\n"); | |
106 | fprintf(ofp, " -U, --set-uri=URI Set URI for the enable-consumer destination.\n"); | |
107 | fprintf(ofp, " It is persistent for the session lifetime.\n"); | |
108 | fprintf(ofp, " Redo the command to change it.\n"); | |
109 | fprintf(ofp, " This will set both data and control URI for network.\n"); | |
110 | //fprintf(ofp, " -C, --ctrl-uri=URI1[,URI2,...]\n"); | |
111 | fprintf(ofp, " -C, --ctrl-uri=URI Set control path URI.\n"); | |
112 | //fprintf(ofp, " -D, --data-uri=URI1[,URI2,...]\n"); | |
113 | fprintf(ofp, " -D, --data-uri=URI Set data path URI.\n"); | |
114 | fprintf(ofp, " -e, --enable Enable consumer\n"); | |
115 | fprintf(ofp, "\n"); | |
116 | } | |
117 | ||
118 | /* | |
119 | * Print URI message. | |
120 | */ | |
121 | static void print_uri_msg(struct lttng_uri *uri) | |
122 | { | |
123 | char *dst; | |
124 | ||
125 | switch (uri->dtype) { | |
126 | case LTTNG_DST_IPV4: | |
127 | dst = uri->dst.ipv4; | |
128 | break; | |
129 | case LTTNG_DST_IPV6: | |
130 | dst = uri->dst.ipv6; | |
131 | break; | |
132 | case LTTNG_DST_PATH: | |
133 | dst = uri->dst.path; | |
134 | MSG("Consumer destination set to %s", dst); | |
135 | goto end; | |
136 | default: | |
137 | DBG("Unknown URI destination"); | |
138 | goto end; | |
139 | } | |
140 | ||
141 | MSG("Consumer %s stream set to %s with the %s protocol on port %d", | |
142 | uri->stype == LTTNG_STREAM_CONTROL ? "control" : "data", | |
143 | dst, uri->proto == LTTNG_TCP ? "TCP" : "UNK", uri->port); | |
144 | ||
145 | end: | |
146 | return; | |
147 | } | |
148 | ||
149 | /* | |
150 | * Setting URIs taking from the command line arguments. There is some | |
151 | * manipulations and special cases using the default args. | |
152 | */ | |
153 | static int set_consumer_arg_uris(struct lttng_uri *uri, size_t size) | |
154 | { | |
155 | int ret, i; | |
156 | ||
157 | if (size == 2) { | |
158 | /* URIs are the control and data stream respectively for net:// */ | |
159 | uri[0].stype = LTTNG_STREAM_CONTROL; | |
160 | uri[1].stype = LTTNG_STREAM_DATA; | |
161 | ||
162 | for (i = 0; i < size; i++) { | |
163 | ret = lttng_set_consumer_uri(handle, &uri[i]); | |
164 | if (ret < 0) { | |
165 | ERR("Setting %s stream URI: %s", | |
166 | uri[i].stype == LTTNG_STREAM_DATA ? "data" : "control", | |
167 | lttng_strerror(ret)); | |
168 | goto error; | |
169 | } | |
170 | /* Set default port if none was given */ | |
171 | if (uri[i].port == 0) { | |
172 | if (uri[i].stype == LTTNG_STREAM_CONTROL) { | |
173 | uri[i].port = DEFAULT_NETWORK_CONTROL_PORT; | |
174 | } else { | |
175 | uri[i].port = DEFAULT_NETWORK_DATA_PORT; | |
176 | } | |
177 | } | |
178 | print_uri_msg(&uri[i]); | |
179 | } | |
180 | } else if (size == 1 && uri[0].dtype == LTTNG_DST_PATH) { | |
181 | /* Set URI if it's file:// */ | |
182 | ret = lttng_set_consumer_uri(handle, &uri[0]); | |
183 | if (ret < 0) { | |
184 | ERR("Failed to set URI %s: %s", opt_uris_arg, | |
185 | lttng_strerror(ret)); | |
186 | goto error; | |
187 | } | |
188 | print_uri_msg(&uri[0]); | |
189 | } else { | |
190 | ERR("Only net:// and file:// are supported. " | |
191 | "Use -D or -U for more fine grained control"); | |
192 | ret = CMD_ERROR; | |
193 | goto error; | |
194 | } | |
195 | ||
196 | error: | |
197 | return ret; | |
198 | } | |
199 | ||
200 | /* | |
201 | * Parse URI from string to lttng_uri object array. | |
202 | */ | |
203 | static ssize_t parse_uri_from_str(const char *str_uri, struct lttng_uri **uris) | |
204 | { | |
205 | ssize_t size; | |
206 | ||
207 | if (*uris != NULL) { | |
208 | free(*uris); | |
209 | } | |
210 | ||
211 | size = uri_parse(str_uri, uris); | |
212 | if (size < 1) { | |
213 | ERR("Bad URI %s. Either the hostname or IP is invalid", str_uri); | |
214 | size = -1; | |
215 | } | |
216 | ||
217 | return size; | |
218 | } | |
219 | ||
220 | /* | |
221 | * Enable consumer command. | |
222 | */ | |
223 | static int enable_consumer(char *session_name) | |
224 | { | |
225 | int ret = CMD_SUCCESS; | |
226 | int run_enable_cmd = 1; | |
227 | ssize_t size; | |
228 | struct lttng_domain dom; | |
229 | struct lttng_uri *uri = NULL; | |
230 | ||
231 | memset(&dom, 0, sizeof(dom)); | |
232 | ||
233 | /* Create lttng domain */ | |
234 | if (opt_kernel) { | |
235 | dom.type = LTTNG_DOMAIN_KERNEL; | |
236 | } else if (opt_userspace) { | |
237 | dom.type = LTTNG_DOMAIN_UST; | |
238 | } else { | |
239 | ERR("Please specify a tracer (-k/--kernel or -u/--userspace)"); | |
240 | ret = CMD_ERROR; | |
241 | goto error; | |
242 | } | |
243 | ||
244 | handle = lttng_create_handle(session_name, &dom); | |
245 | if (handle == NULL) { | |
246 | ret = -1; | |
247 | goto error; | |
248 | } | |
249 | ||
250 | /* Handle trailing arguments */ | |
251 | if (opt_uris_arg) { | |
252 | size = parse_uri_from_str(opt_uris_arg, &uri); | |
253 | if (size < 1) { | |
254 | ret = CMD_ERROR; | |
255 | goto error; | |
256 | } | |
257 | ||
258 | ret = set_consumer_arg_uris(uri, size); | |
259 | if (ret < 0) { | |
260 | goto free_uri; | |
261 | } | |
262 | } | |
263 | ||
264 | /* Handling URIs (-U opt) */ | |
265 | if (opt_uris) { | |
266 | size = parse_uri_from_str(opt_uris, &uri); | |
267 | if (size < 1) { | |
268 | ret = CMD_ERROR; | |
269 | goto error; | |
270 | } | |
271 | ||
272 | ret = set_consumer_arg_uris(uri, size); | |
273 | if (ret < 0) { | |
274 | goto free_uri; | |
275 | } | |
276 | ||
277 | /* opt_enable will tell us to run or not the enable_consumer cmd. */ | |
278 | run_enable_cmd = 0; | |
279 | } | |
280 | ||
281 | /* Setting up control URI (-C opt) */ | |
282 | if (opt_ctrl_uris) { | |
283 | size = parse_uri_from_str(opt_ctrl_uris, &uri); | |
284 | if (size < 1) { | |
285 | ret = CMD_ERROR; | |
286 | goto error; | |
287 | } | |
288 | ||
289 | /* Set default port if none specified */ | |
290 | if (uri[0].port == 0) { | |
291 | uri[0].port = DEFAULT_NETWORK_CONTROL_PORT; | |
292 | } | |
293 | ||
294 | uri[0].stype = LTTNG_STREAM_CONTROL; | |
295 | ||
296 | ret = lttng_set_consumer_uri(handle, &uri[0]); | |
297 | if (ret < 0) { | |
298 | ERR("Failed to set control URI %s: %s", opt_ctrl_uris, | |
299 | lttng_strerror(ret)); | |
300 | goto free_uri; | |
301 | } | |
302 | print_uri_msg(&uri[0]); | |
303 | ||
304 | /* opt_enable will tell us to run or not the enable_consumer cmd. */ | |
305 | run_enable_cmd = 0; | |
306 | } | |
307 | ||
308 | /* Setting up data URI (-D opt) */ | |
309 | if (opt_data_uris) { | |
310 | size = parse_uri_from_str(opt_data_uris, &uri); | |
311 | if (size < 1) { | |
312 | ret = CMD_ERROR; | |
313 | goto error; | |
314 | } | |
315 | ||
316 | /* Set default port if none specified */ | |
317 | if (uri[0].port == 0) { | |
318 | uri[0].port = DEFAULT_NETWORK_DATA_PORT; | |
319 | } | |
320 | ||
321 | uri[0].stype = LTTNG_STREAM_DATA; | |
322 | ||
323 | ret = lttng_set_consumer_uri(handle, &uri[0]); | |
324 | if (ret < 0) { | |
325 | ERR("Failed to set data URI %s: %s", opt_data_uris, | |
326 | lttng_strerror(ret)); | |
327 | goto free_uri; | |
328 | } | |
329 | print_uri_msg(&uri[0]); | |
330 | ||
331 | /* opt_enable will tell us to run or not the enable_consumer cmd. */ | |
332 | run_enable_cmd = 0; | |
333 | } | |
334 | ||
335 | /* Enable consumer (-e opt) */ | |
336 | if (opt_enable || run_enable_cmd) { | |
337 | ret = lttng_enable_consumer(handle); | |
338 | if (ret < 0) { | |
339 | ERR("Enabling consumer for session %s: %s", session_name, | |
340 | lttng_strerror(ret)); | |
341 | if (ret == -LTTCOMM_ENABLE_CONSUMER_FAIL) { | |
342 | ERR("Perhaps the session was previously started?"); | |
343 | } | |
344 | goto free_uri; | |
345 | } | |
346 | ||
347 | MSG("Consumer enabled successfully"); | |
348 | } | |
349 | ||
350 | free_uri: | |
351 | free(uri); | |
352 | ||
353 | error: | |
354 | lttng_destroy_handle(handle); | |
355 | return ret; | |
356 | } | |
357 | ||
358 | /* | |
359 | * The 'enable-consumer <options>' first level command | |
360 | * | |
361 | * Returns one of the CMD_* result constants. | |
362 | */ | |
363 | int cmd_enable_consumer(int argc, const char **argv) | |
364 | { | |
365 | int opt, ret = CMD_SUCCESS; | |
366 | static poptContext pc; | |
367 | char *session_name = NULL; | |
368 | ||
369 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
370 | poptReadDefaultConfig(pc, 0); | |
371 | ||
372 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
373 | switch (opt) { | |
374 | case OPT_HELP: | |
375 | usage(stdout); | |
376 | goto end; | |
377 | case OPT_LIST_OPTIONS: | |
378 | list_cmd_options(stdout, long_options); | |
379 | goto end; | |
380 | default: | |
381 | usage(stderr); | |
382 | ret = CMD_UNDEFINED; | |
383 | goto end; | |
384 | } | |
385 | } | |
386 | ||
387 | opt_uris_arg = (char *) poptGetArg(pc); | |
388 | DBG("URIs: %s", opt_uris_arg); | |
389 | ||
390 | /* Get session name */ | |
391 | if (!opt_session_name) { | |
392 | session_name = get_session_name(); | |
393 | if (session_name == NULL) { | |
394 | ret = CMD_ERROR; | |
395 | goto end; | |
396 | } | |
397 | } else { | |
398 | session_name = opt_session_name; | |
399 | } | |
400 | ||
401 | ret = enable_consumer(session_name); | |
402 | ||
403 | end: | |
404 | if (opt_session_name == NULL) { | |
405 | free(session_name); | |
406 | } | |
407 | ||
408 | poptFreeContext(pc); | |
409 | return ret; | |
410 | } |