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