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