Commit | Line | Data |
---|---|---|
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 | ||
18 | #define _GNU_SOURCE | |
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 | |
32 | static char *opt_session_name; | |
33 | ||
34 | enum { | |
35 | OPT_HELP = 1, | |
679b4943 | 36 | OPT_LIST_OPTIONS, |
3087ef18 DG |
37 | }; |
38 | ||
ce91cd0b JRJ |
39 | static struct mi_writer *writer; |
40 | ||
3087ef18 DG |
41 | static 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 | */ | |
51 | static 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 | */ | |
64 | static 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 | } | |
98 | end: | |
99 | return ret; | |
100 | } | |
101 | ||
3087ef18 DG |
102 | /* |
103 | * set_session | |
104 | */ | |
105 | static int set_session(void) | |
106 | { | |
107 | int ret = CMD_SUCCESS; | |
3087ef18 | 108 | |
487b253b DG |
109 | if (opt_session_name && strlen(opt_session_name) > NAME_MAX) { |
110 | ERR("Session name too long. Length must be lower or equal to %d", | |
111 | NAME_MAX); | |
112 | ret = CMD_ERROR; | |
113 | goto error; | |
114 | } | |
115 | ||
fffd0547 | 116 | ret = config_init(opt_session_name); |
3087ef18 | 117 | if (ret < 0) { |
fffd0547 | 118 | ERR("Unable to set session name"); |
3087ef18 DG |
119 | ret = CMD_ERROR; |
120 | goto error; | |
121 | } | |
122 | ||
123 | MSG("Session set to %s", opt_session_name); | |
ce91cd0b JRJ |
124 | if (lttng_opt_mi) { |
125 | ret = mi_print(opt_session_name); | |
126 | if (ret) { | |
127 | ret = CMD_ERROR; | |
128 | goto error; | |
129 | } | |
130 | } | |
131 | ||
3087ef18 DG |
132 | ret = CMD_SUCCESS; |
133 | ||
134 | error: | |
135 | return ret; | |
136 | } | |
137 | ||
138 | /* | |
139 | * cmd_set_session | |
140 | */ | |
141 | int cmd_set_session(int argc, const char **argv) | |
142 | { | |
ce91cd0b | 143 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
3087ef18 DG |
144 | static poptContext pc; |
145 | ||
146 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
147 | poptReadDefaultConfig(pc, 0); | |
148 | ||
149 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
150 | switch (opt) { | |
151 | case OPT_HELP: | |
ca1c3607 | 152 | usage(stdout); |
3087ef18 | 153 | goto end; |
679b4943 SM |
154 | case OPT_LIST_OPTIONS: |
155 | list_cmd_options(stdout, long_options); | |
679b4943 | 156 | goto end; |
3087ef18 DG |
157 | default: |
158 | usage(stderr); | |
159 | ret = CMD_UNDEFINED; | |
160 | goto end; | |
161 | } | |
162 | } | |
163 | ||
164 | opt_session_name = (char *) poptGetArg(pc); | |
165 | if (opt_session_name == NULL) { | |
166 | ERR("Missing session name"); | |
167 | usage(stderr); | |
ca1c3607 | 168 | ret = CMD_ERROR; |
3087ef18 DG |
169 | goto end; |
170 | } | |
171 | ||
ce91cd0b JRJ |
172 | /* Mi check */ |
173 | if (lttng_opt_mi) { | |
174 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); | |
175 | if (!writer) { | |
176 | ret = -LTTNG_ERR_NOMEM; | |
177 | goto end; | |
178 | } | |
179 | ||
180 | /* Open command element */ | |
181 | ret = mi_lttng_writer_command_open(writer, | |
182 | mi_lttng_element_command_set_session); | |
183 | if (ret) { | |
184 | ret = CMD_ERROR; | |
185 | goto end; | |
186 | } | |
187 | ||
188 | /* Open output element */ | |
189 | ret = mi_lttng_writer_open_element(writer, | |
190 | mi_lttng_element_command_output); | |
191 | if (ret) { | |
192 | ret = CMD_ERROR; | |
193 | goto end; | |
194 | } | |
195 | } | |
196 | ||
197 | command_ret = set_session(); | |
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(writer, | |
213 | 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 | } | |
3087ef18 DG |
226 | |
227 | end: | |
ce91cd0b JRJ |
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 occured during set_session() */ | |
235 | ret = command_ret ? command_ret : ret; | |
236 | ||
ca1c3607 | 237 | poptFreeContext(pc); |
3087ef18 DG |
238 | return ret; |
239 | } |