Port: Remove _GNU_SOURCE, defined in config.h
[lttng-tools.git] / src / bin / lttng / commands / set_session.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 #include <assert.h>
27
28 #include <common/mi-lttng.h>
29
30 #include "../command.h"
31
32 static char *opt_session_name;
33
34 enum {
35 OPT_HELP = 1,
36 OPT_LIST_OPTIONS,
37 };
38
39 static struct mi_writer *writer;
40
41 static struct poptOption long_options[] = {
42 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
43 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
44 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
45 {0, 0, 0, 0, 0, 0, 0}
46 };
47
48 /*
49 * usage
50 */
51 static void usage(FILE *ofp)
52 {
53 fprintf(ofp, "usage: lttng set-session NAME [OPTIONS]\n");
54 fprintf(ofp, "\n");
55 fprintf(ofp, "Options:\n");
56 fprintf(ofp, " -h, --help Show this help\n");
57 fprintf(ofp, " --list-options Simple listing of options\n");
58 fprintf(ofp, "\n");
59 }
60
61 /*
62 * Print the necessary mi for a session and name.
63 */
64 static int mi_print(char *session_name)
65 {
66 int ret;
67
68 assert(writer);
69 assert(session_name);
70
71 /*
72 * Open a sessions element
73 * This is purely for validation purpose
74 */
75 ret = mi_lttng_sessions_open(writer);
76 if (ret) {
77 goto end;
78 }
79
80 /* Open a session element */
81 ret = mi_lttng_writer_open_element(writer, config_element_session);
82 if (ret) {
83 goto end;
84 }
85
86 /* Session name */
87 ret = mi_lttng_writer_write_element_string(writer , config_element_name,
88 session_name);
89 if (ret) {
90 goto end;
91 }
92
93 /* Close session and sessions element */
94 ret = mi_lttng_close_multi_element(writer, 2);
95 if (ret) {
96 goto end;
97 }
98 end:
99 return ret;
100 }
101
102 /*
103 * set_session
104 */
105 static int set_session(void)
106 {
107 int ret = CMD_SUCCESS;
108 int count, i;
109 unsigned int session_found = 0;
110 struct lttng_session *sessions;
111
112 if (opt_session_name && strlen(opt_session_name) > NAME_MAX) {
113 ERR("Session name too long. Length must be lower or equal to %d",
114 NAME_MAX);
115 ret = CMD_ERROR;
116 goto end;
117 }
118
119 count = lttng_list_sessions(&sessions);
120 if (count < 0) {
121 ret = CMD_ERROR;
122 ERR("%s", lttng_strerror(count));
123 goto end;
124 }
125
126 for (i = 0; i < count; i++) {
127 if (strncmp(sessions[i].name, opt_session_name, NAME_MAX) == 0) {
128 session_found = 1;
129 break;
130 }
131 }
132
133 if (!session_found) {
134 ERR("Session '%s' not found", opt_session_name);
135 ret = CMD_ERROR;
136 goto error;
137 }
138
139 ret = config_init(opt_session_name);
140 if (ret < 0) {
141 ERR("Unable to set session name");
142 ret = CMD_ERROR;
143 goto error;
144 }
145
146 MSG("Session set to %s", opt_session_name);
147 if (lttng_opt_mi) {
148 ret = mi_print(opt_session_name);
149 if (ret) {
150 ret = CMD_ERROR;
151 goto error;
152 }
153 }
154
155 ret = CMD_SUCCESS;
156
157 error:
158 free(sessions);
159 end:
160 return ret;
161 }
162
163 /*
164 * cmd_set_session
165 */
166 int cmd_set_session(int argc, const char **argv)
167 {
168 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
169 static poptContext pc;
170
171 pc = poptGetContext(NULL, argc, argv, long_options, 0);
172 poptReadDefaultConfig(pc, 0);
173
174 while ((opt = poptGetNextOpt(pc)) != -1) {
175 switch (opt) {
176 case OPT_HELP:
177 usage(stdout);
178 goto end;
179 case OPT_LIST_OPTIONS:
180 list_cmd_options(stdout, long_options);
181 goto end;
182 default:
183 usage(stderr);
184 ret = CMD_UNDEFINED;
185 goto end;
186 }
187 }
188
189 opt_session_name = (char *) poptGetArg(pc);
190 if (opt_session_name == NULL) {
191 ERR("Missing session name");
192 usage(stderr);
193 ret = CMD_ERROR;
194 goto end;
195 }
196
197 /* Mi check */
198 if (lttng_opt_mi) {
199 writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
200 if (!writer) {
201 ret = -LTTNG_ERR_NOMEM;
202 goto end;
203 }
204
205 /* Open command element */
206 ret = mi_lttng_writer_command_open(writer,
207 mi_lttng_element_command_set_session);
208 if (ret) {
209 ret = CMD_ERROR;
210 goto end;
211 }
212
213 /* Open output element */
214 ret = mi_lttng_writer_open_element(writer,
215 mi_lttng_element_command_output);
216 if (ret) {
217 ret = CMD_ERROR;
218 goto end;
219 }
220 }
221
222 command_ret = set_session();
223 if (command_ret) {
224 success = 0;
225 }
226
227 /* Mi closing */
228 if (lttng_opt_mi) {
229 /* Close output element */
230 ret = mi_lttng_writer_close_element(writer);
231 if (ret) {
232 ret = CMD_ERROR;
233 goto end;
234 }
235
236 /* Success ? */
237 ret = mi_lttng_writer_write_element_bool(writer,
238 mi_lttng_element_command_success, success);
239 if (ret) {
240 ret = CMD_ERROR;
241 goto end;
242 }
243
244 /* Command element close */
245 ret = mi_lttng_writer_command_close(writer);
246 if (ret) {
247 ret = CMD_ERROR;
248 goto end;
249 }
250 }
251
252 end:
253 /* Mi clean-up */
254 if (writer && mi_lttng_writer_destroy(writer)) {
255 /* Preserve original error code */
256 ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
257 }
258
259 /* Overwrite ret if an error occured during set_session() */
260 ret = command_ret ? command_ret : ret;
261
262 poptFreeContext(pc);
263 return ret;
264 }
This page took 0.034134 seconds and 4 git commands to generate.