Fix: lttng: poptGetArg doesn't provide string ownership
[lttng-tools.git] / src / bin / lttng / commands / set_session.c
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 #include <assert.h>
17
18 #include <common/mi-lttng.h>
19
20 #include "../command.h"
21
22 #ifdef LTTNG_EMBED_HELP
23 static const char help_msg[] =
24 #include <lttng-set-session.1.h>
25 ;
26 #endif
27
28 enum {
29 OPT_HELP = 1,
30 OPT_LIST_OPTIONS,
31 };
32
33 static struct mi_writer *writer;
34
35 static struct poptOption long_options[] = {
36 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
37 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
38 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
39 {0, 0, 0, 0, 0, 0, 0}
40 };
41
42 /*
43 * Print the necessary mi for a session and name.
44 */
45 static int mi_print(const char *session_name)
46 {
47 int ret;
48
49 assert(writer);
50 assert(session_name);
51
52 /*
53 * Open a sessions element
54 * This is purely for validation purpose
55 */
56 ret = mi_lttng_sessions_open(writer);
57 if (ret) {
58 goto end;
59 }
60
61 /* Open a session element */
62 ret = mi_lttng_writer_open_element(writer, config_element_session);
63 if (ret) {
64 goto end;
65 }
66
67 /* Session name */
68 ret = mi_lttng_writer_write_element_string(writer , config_element_name,
69 session_name);
70 if (ret) {
71 goto end;
72 }
73
74 /* Close session and sessions element */
75 ret = mi_lttng_close_multi_element(writer, 2);
76 if (ret) {
77 goto end;
78 }
79 end:
80 return ret;
81 }
82
83 /*
84 * set_session
85 */
86 static int set_session(const char *session_name)
87 {
88 int ret = CMD_SUCCESS;
89 int count, i;
90 unsigned int session_found = 0;
91 struct lttng_session *sessions;
92
93 if (session_name && strlen(session_name) > NAME_MAX) {
94 ERR("Session name too long. Length must be lower or equal to %d",
95 NAME_MAX);
96 ret = CMD_ERROR;
97 goto end;
98 }
99
100 count = lttng_list_sessions(&sessions);
101 if (count < 0) {
102 ret = CMD_ERROR;
103 ERR("%s", lttng_strerror(count));
104 goto end;
105 }
106
107 for (i = 0; i < count; i++) {
108 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
109 session_found = 1;
110 break;
111 }
112 }
113
114 if (!session_found) {
115 ERR("Session '%s' not found", session_name);
116 ret = CMD_ERROR;
117 goto error;
118 }
119
120 ret = config_init(session_name);
121 if (ret < 0) {
122 ERR("Unable to set session name");
123 ret = CMD_ERROR;
124 goto error;
125 }
126
127 MSG("Session set to %s", session_name);
128 if (lttng_opt_mi) {
129 ret = mi_print(session_name);
130 if (ret) {
131 ret = CMD_ERROR;
132 goto error;
133 }
134 }
135
136 ret = CMD_SUCCESS;
137
138 error:
139 free(sessions);
140 end:
141 return ret;
142 }
143
144 /*
145 * cmd_set_session
146 */
147 int cmd_set_session(int argc, const char **argv)
148 {
149 int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
150 static poptContext pc;
151 const char *arg_session_name = NULL;
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 arg_session_name = poptGetArg(pc);
171 if (arg_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(arg_session_name);
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.034612 seconds and 5 git commands to generate.