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