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