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