Commit | Line | Data |
---|---|---|
3087ef18 | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> |
3087ef18 | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
3087ef18 | 5 | * |
3087ef18 DG |
6 | */ |
7 | ||
6c1c0768 | 8 | #define _LGPL_SOURCE |
3087ef18 DG |
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> | |
ce91cd0b JRJ |
16 | #include <assert.h> |
17 | ||
18 | #include <common/mi-lttng.h> | |
3087ef18 | 19 | |
c399183f | 20 | #include "../command.h" |
3087ef18 DG |
21 | |
22 | static char *opt_session_name; | |
23 | ||
4fc83d94 PP |
24 | #ifdef LTTNG_EMBED_HELP |
25 | static const char help_msg[] = | |
26 | #include <lttng-set-session.1.h> | |
27 | ; | |
28 | #endif | |
29 | ||
3087ef18 DG |
30 | enum { |
31 | OPT_HELP = 1, | |
679b4943 | 32 | OPT_LIST_OPTIONS, |
3087ef18 DG |
33 | }; |
34 | ||
ce91cd0b JRJ |
35 | static struct mi_writer *writer; |
36 | ||
3087ef18 DG |
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}, | |
679b4943 | 40 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
3087ef18 DG |
41 | {0, 0, 0, 0, 0, 0, 0} |
42 | }; | |
43 | ||
ce91cd0b JRJ |
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 | ||
3087ef18 DG |
85 | /* |
86 | * set_session | |
87 | */ | |
88 | static int set_session(void) | |
89 | { | |
90 | int ret = CMD_SUCCESS; | |
7f0c090d PPM |
91 | int count, i; |
92 | unsigned int session_found = 0; | |
93 | struct lttng_session *sessions; | |
3087ef18 | 94 | |
487b253b DG |
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; | |
7f0c090d PPM |
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; | |
487b253b DG |
119 | goto error; |
120 | } | |
121 | ||
fffd0547 | 122 | ret = config_init(opt_session_name); |
3087ef18 | 123 | if (ret < 0) { |
fffd0547 | 124 | ERR("Unable to set session name"); |
3087ef18 DG |
125 | ret = CMD_ERROR; |
126 | goto error; | |
127 | } | |
128 | ||
129 | MSG("Session set to %s", opt_session_name); | |
ce91cd0b JRJ |
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 | ||
3087ef18 DG |
138 | ret = CMD_SUCCESS; |
139 | ||
140 | error: | |
7f0c090d PPM |
141 | free(sessions); |
142 | end: | |
3087ef18 DG |
143 | return ret; |
144 | } | |
145 | ||
146 | /* | |
147 | * cmd_set_session | |
148 | */ | |
149 | int cmd_set_session(int argc, const char **argv) | |
150 | { | |
ce91cd0b | 151 | int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1; |
3087ef18 DG |
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: | |
4ba92f18 | 160 | SHOW_HELP(); |
3087ef18 | 161 | goto end; |
679b4943 SM |
162 | case OPT_LIST_OPTIONS: |
163 | list_cmd_options(stdout, long_options); | |
679b4943 | 164 | goto end; |
3087ef18 | 165 | default: |
3087ef18 DG |
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"); | |
ca1c3607 | 174 | ret = CMD_ERROR; |
3087ef18 DG |
175 | goto end; |
176 | } | |
177 | ||
ce91cd0b JRJ |
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 | } | |
3087ef18 DG |
232 | |
233 | end: | |
ce91cd0b JRJ |
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 | ||
83f4233d | 240 | /* Overwrite ret if an error occurred during set_session() */ |
ce91cd0b JRJ |
241 | ret = command_ret ? command_ret : ret; |
242 | ||
ca1c3607 | 243 | poptFreeContext(pc); |
3087ef18 DG |
244 | return ret; |
245 | } |