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