Fix: enable-consumer for all domains missing dir
[lttng-tools.git] / src / bin / lttng / commands / enable_consumer.c
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_url;
40 static char *opt_ctrl_url;
41 static char *opt_data_url;
42 static char *opt_url_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_url, 0, 0, 0},
59 {"ctrl-uri", 'C', POPT_ARG_STRING, &opt_ctrl_url, 0, 0, 0},
60 {"data-uri", 'D', POPT_ARG_STRING, &opt_data_url, 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] [URL] [OPTIONS]\n");
71 fprintf(ofp, "\n");
72 fprintf(ofp, "The default behavior is to enable a consumer to the current URL.\n");
73 fprintf(ofp, "The default URL 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, "Options:\n");
79 fprintf(ofp, " -h, --help Show this help\n");
80 fprintf(ofp, " --list-options Simple listing of options\n");
81 fprintf(ofp, " -s, --session NAME Apply to session name\n");
82 fprintf(ofp, " -k, --kernel Apply to the kernel tracer\n");
83 fprintf(ofp, " -u, --userspace Apply to the user-space tracer\n");
84 fprintf(ofp, "\n");
85 fprintf(ofp, "Extended Options:\n");
86 fprintf(ofp, "\n");
87 fprintf(ofp, "Using these options, each API call can be controlled individually.\n");
88 fprintf(ofp, "For instance, -C does not enable the consumer automatically.\n");
89 fprintf(ofp, "\n");
90 fprintf(ofp, " -U, --set-uri=URL Set URL for the enable-consumer destination.\n");
91 fprintf(ofp, " It is persistent for the session lifetime.\n");
92 fprintf(ofp, " Redo the command to change it.\n");
93 fprintf(ofp, " This will set both data and control URL for network.\n");
94 fprintf(ofp, " -C, --ctrl-url=URL Set control path URL. (Must use -D also)\n");
95 fprintf(ofp, " -D, --data-url=URL Set data path URL. (Must use -C also)\n");
96 fprintf(ofp, " -e, --enable Enable consumer\n");
97 fprintf(ofp, "\n");
98 fprintf(ofp, "\n");
99 fprintf(ofp, "Please refer to the man page (lttng(1)) for more information on network\n");
100 fprintf(ofp, "streaming mechanisms and explanation of the control and data port\n");
101 fprintf(ofp, "\n");
102 fprintf(ofp, "URL format is has followed:\n");
103 fprintf(ofp, "\n");
104 fprintf(ofp, " proto://[HOST|IP][:PORT1[:PORT2]][/TRACE_PATH]\n");
105 fprintf(ofp, "\n");
106 fprintf(ofp, " Supported protocols are (proto):\n");
107 fprintf(ofp, " > file://...\n");
108 fprintf(ofp, " Local filesystem full path.\n");
109 fprintf(ofp, "\n");
110 fprintf(ofp, " > net[6]://...\n");
111 fprintf(ofp, " This will use the default network transport layer which is\n");
112 fprintf(ofp, " TCP for both control (PORT1) and data port (PORT2).\n");
113 fprintf(ofp, " The default ports are respectively 5342 and 5343.\n");
114 fprintf(ofp, "\n");
115 fprintf(ofp, " > tcp[4|6]://...\n");
116 fprintf(ofp, " Can only be used with -C and -D together\n");
117 fprintf(ofp, "\n");
118 fprintf(ofp, "NOTE: IPv6 address MUST be enclosed in brackets '[]' (rfc2732)\n");
119 fprintf(ofp, "\n");
120 fprintf(ofp, "Examples:\n");
121 fprintf(ofp, " # lttng enable-consumer -u net://192.168.1.42\n");
122 fprintf(ofp, " Uses TCP and default ports for user space tracing (-u).\n");
123 fprintf(ofp, "\n");
124 }
125
126 /*
127 * Enable consumer command.
128 */
129 static int enable_consumer(char *session_name, int domain)
130 {
131 int ret = CMD_SUCCESS;
132 int run_enable_cmd = 1;
133 struct lttng_domain dom;
134
135 memset(&dom, 0, sizeof(dom));
136
137 dom.type = domain;
138
139 handle = lttng_create_handle(session_name, &dom);
140 if (handle == NULL) {
141 ret = -1;
142 goto error;
143 }
144
145 /* Handle trailing arguments */
146 if (opt_url_arg) {
147 ret = lttng_set_consumer_url(handle, opt_url_arg, NULL);
148 if (ret < 0) {
149 ERR("%s", lttng_strerror(ret));
150 goto error;
151 }
152
153 MSG("URL %s set for %s session %s.", opt_url_arg,
154 (domain == LTTNG_DOMAIN_KERNEL) ? "kernel" : "UST",
155 session_name);
156 }
157
158 /* Handling URLs (-U opt) */
159 if (opt_url) {
160 ret = lttng_set_consumer_url(handle, opt_url, NULL);
161 if (ret < 0) {
162 ERR("%s", lttng_strerror(ret));
163 goto error;
164 }
165
166 /* opt_enable will tell us to run or not the enable_consumer cmd. */
167 run_enable_cmd = 0;
168
169 MSG("URL %s set for %s session %s.", opt_url,
170 (domain == LTTNG_DOMAIN_KERNEL) ? "kernel" : "UST",
171 session_name);
172 }
173
174 /* Setting up control URL (-C or/and -D opt) */
175 if (opt_ctrl_url || opt_data_url) {
176 ret = lttng_set_consumer_url(handle, opt_ctrl_url, opt_data_url);
177 if (ret < 0) {
178 ERR("%s", lttng_strerror(ret));
179 goto error;
180 }
181
182 /* opt_enable will tell us to run or not the enable_consumer cmd. */
183 run_enable_cmd = 0;
184
185 if (opt_ctrl_url) {
186 MSG("Control URL %s set for %s session %s.", opt_ctrl_url,
187 (domain == LTTNG_DOMAIN_KERNEL) ? "kernel" : "UST",
188 session_name);
189 }
190
191 if (opt_data_url) {
192 MSG("Data URL %s set for %s session %s.", opt_data_url,
193 (domain == LTTNG_DOMAIN_KERNEL) ? "kernel" : "UST",
194 session_name);
195 }
196 }
197
198 /* Enable consumer (-e opt) */
199 if (opt_enable || run_enable_cmd) {
200 ret = lttng_enable_consumer(handle);
201 if (ret < 0) {
202 ERR("Enabling consumer for session %s: %s", session_name,
203 lttng_strerror(ret));
204 if (ret == -LTTNG_ERR_ENABLE_CONSUMER_FAIL) {
205 ERR("Perhaps the session was previously started?");
206 }
207 goto error;
208 }
209
210 MSG("Consumer enabled successfully");
211 }
212
213 error:
214 lttng_destroy_handle(handle);
215 return ret;
216 }
217
218 /*
219 * The 'enable-consumer <options>' first level command
220 *
221 * Returns one of the CMD_* result constants.
222 */
223 int cmd_enable_consumer(int argc, const char **argv)
224 {
225 int opt, ret = CMD_SUCCESS;
226 static poptContext pc;
227 char *session_name = NULL;
228
229 pc = poptGetContext(NULL, argc, argv, long_options, 0);
230 poptReadDefaultConfig(pc, 0);
231
232 while ((opt = poptGetNextOpt(pc)) != -1) {
233 switch (opt) {
234 case OPT_HELP:
235 usage(stdout);
236 goto end;
237 case OPT_LIST_OPTIONS:
238 list_cmd_options(stdout, long_options);
239 goto end;
240 default:
241 usage(stderr);
242 ret = CMD_UNDEFINED;
243 goto end;
244 }
245 }
246
247 opt_url_arg = (char *) poptGetArg(pc);
248 DBG("URLs: %s", opt_url_arg);
249
250 /* Get session name */
251 if (!opt_session_name) {
252 session_name = get_session_name();
253 if (session_name == NULL) {
254 ret = CMD_ERROR;
255 goto end;
256 }
257 } else {
258 session_name = opt_session_name;
259 }
260
261 if (opt_kernel || (!opt_kernel && !opt_userspace)) {
262 ret = enable_consumer(session_name, LTTNG_DOMAIN_KERNEL);
263 if (ret < 0) {
264 goto end;
265 }
266 }
267
268 if (opt_userspace || (!opt_kernel && !opt_userspace)) {
269 ret = enable_consumer(session_name, LTTNG_DOMAIN_UST);
270 if (ret < 0) {
271 goto end;
272 }
273 }
274
275 end:
276 if (opt_session_name == NULL) {
277 free(session_name);
278 }
279
280 poptFreeContext(pc);
281 return ret;
282 }
This page took 0.050759 seconds and 4 git commands to generate.