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