Commit | Line | Data |
---|---|---|
2f77fc4b DG |
1 | /* |
2 | * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com> | |
bdf64013 | 3 | * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
2f77fc4b DG |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License, version 2 only, as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
12 | * more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License along with | |
15 | * this program; if not, write to the Free Software Foundation, Inc., 51 | |
16 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
17 | */ | |
18 | ||
6c1c0768 | 19 | #define _LGPL_SOURCE |
2f77fc4b | 20 | #include <assert.h> |
6dc3064a | 21 | #include <inttypes.h> |
2f77fc4b DG |
22 | #include <urcu/list.h> |
23 | #include <urcu/uatomic.h> | |
a503e1ef | 24 | #include <sys/stat.h> |
e5148e25 | 25 | #include <stdio.h> |
2f77fc4b DG |
26 | |
27 | #include <common/defaults.h> | |
28 | #include <common/common.h> | |
29 | #include <common/sessiond-comm/sessiond-comm.h> | |
30 | #include <common/relayd/relayd.h> | |
10a50311 | 31 | #include <common/utils.h> |
f5436bfc | 32 | #include <common/compat/string.h> |
93ec662e | 33 | #include <common/kernel-ctl/kernel-ctl.h> |
b0880ae5 JG |
34 | #include <common/dynamic-buffer.h> |
35 | #include <common/buffer-view.h> | |
698fb2ed | 36 | #include <common/trace-chunk.h> |
823a1989 | 37 | #include <lttng/location-internal.h> |
b0880ae5 JG |
38 | #include <lttng/trigger/trigger-internal.h> |
39 | #include <lttng/condition/condition.h> | |
40 | #include <lttng/action/action.h> | |
6e21424e | 41 | #include <lttng/channel.h> |
b0880ae5 | 42 | #include <lttng/channel-internal.h> |
5c408ad8 | 43 | #include <lttng/rotate-internal.h> |
17dd1232 | 44 | #include <lttng/location-internal.h> |
5feee503 | 45 | #include <lttng/session-internal.h> |
3c02e545 | 46 | #include <lttng/userspace-probe-internal.h> |
5feee503 | 47 | #include <lttng/session-descriptor-internal.h> |
9f449915 | 48 | #include <common/string-utils/string-utils.h> |
2f77fc4b DG |
49 | |
50 | #include "channel.h" | |
51 | #include "consumer.h" | |
52 | #include "event.h" | |
8782cc74 | 53 | #include "health-sessiond.h" |
2f77fc4b DG |
54 | #include "kernel.h" |
55 | #include "kernel-consumer.h" | |
56 | #include "lttng-sessiond.h" | |
57 | #include "utils.h" | |
0dbc2034 | 58 | #include "lttng-syscall.h" |
7c1d2758 | 59 | #include "agent.h" |
93ec662e | 60 | #include "buffer-registry.h" |
b0880ae5 JG |
61 | #include "notification-thread.h" |
62 | #include "notification-thread-commands.h" | |
5c408ad8 JD |
63 | #include "rotate.h" |
64 | #include "rotation-thread.h" | |
432b38d7 | 65 | #include "timer.h" |
f28f9e44 | 66 | #include "agent-thread.h" |
2f77fc4b DG |
67 | |
68 | #include "cmd.h" | |
69 | ||
a503e1ef JG |
70 | /* Sleep for 100ms between each check for the shm path's deletion. */ |
71 | #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000 | |
72 | ||
823a1989 JG |
73 | struct cmd_destroy_session_reply_context { |
74 | int reply_sock_fd; | |
75 | bool implicit_rotation_on_destroy; | |
76 | }; | |
77 | ||
a503e1ef JG |
78 | static enum lttng_error_code wait_on_path(void *path); |
79 | ||
80 | /* | |
81 | * Command completion handler that is used by the destroy command | |
82 | * when a session that has a non-default shm_path is being destroyed. | |
83 | * | |
84 | * See comment in cmd_destroy_session() for the rationale. | |
85 | */ | |
86 | static struct destroy_completion_handler { | |
87 | struct cmd_completion_handler handler; | |
88 | char shm_path[member_sizeof(struct ltt_session, shm_path)]; | |
89 | } destroy_completion_handler = { | |
90 | .handler = { | |
91 | .run = wait_on_path, | |
92 | .data = destroy_completion_handler.shm_path | |
93 | }, | |
94 | .shm_path = { 0 }, | |
95 | }; | |
96 | ||
97 | static struct cmd_completion_handler *current_completion_handler; | |
98 | ||
2f77fc4b DG |
99 | /* |
100 | * Used to keep a unique index for each relayd socket created where this value | |
101 | * is associated with streams on the consumer so it can match the right relayd | |
d88aee68 DG |
102 | * to send to. It must be accessed with the relayd_net_seq_idx_lock |
103 | * held. | |
2f77fc4b | 104 | */ |
d88aee68 DG |
105 | static pthread_mutex_t relayd_net_seq_idx_lock = PTHREAD_MUTEX_INITIALIZER; |
106 | static uint64_t relayd_net_seq_idx; | |
2f77fc4b | 107 | |
7076b56e JG |
108 | static int validate_ust_event_name(const char *); |
109 | static int cmd_enable_event_internal(struct ltt_session *session, | |
110 | struct lttng_domain *domain, | |
111 | char *channel_name, struct lttng_event *event, | |
112 | char *filter_expression, | |
113 | struct lttng_filter_bytecode *filter, | |
114 | struct lttng_event_exclusion *exclusion, | |
115 | int wpipe); | |
116 | ||
2f77fc4b DG |
117 | /* |
118 | * Create a session path used by list_lttng_sessions for the case that the | |
119 | * session consumer is on the network. | |
120 | */ | |
121 | static int build_network_session_path(char *dst, size_t size, | |
122 | struct ltt_session *session) | |
123 | { | |
124 | int ret, kdata_port, udata_port; | |
125 | struct lttng_uri *kuri = NULL, *uuri = NULL, *uri = NULL; | |
126 | char tmp_uurl[PATH_MAX], tmp_urls[PATH_MAX]; | |
127 | ||
128 | assert(session); | |
129 | assert(dst); | |
130 | ||
131 | memset(tmp_urls, 0, sizeof(tmp_urls)); | |
132 | memset(tmp_uurl, 0, sizeof(tmp_uurl)); | |
133 | ||
134 | kdata_port = udata_port = DEFAULT_NETWORK_DATA_PORT; | |
135 | ||
136 | if (session->kernel_session && session->kernel_session->consumer) { | |
137 | kuri = &session->kernel_session->consumer->dst.net.control; | |
138 | kdata_port = session->kernel_session->consumer->dst.net.data.port; | |
139 | } | |
140 | ||
141 | if (session->ust_session && session->ust_session->consumer) { | |
142 | uuri = &session->ust_session->consumer->dst.net.control; | |
143 | udata_port = session->ust_session->consumer->dst.net.data.port; | |
144 | } | |
145 | ||
146 | if (uuri == NULL && kuri == NULL) { | |
147 | uri = &session->consumer->dst.net.control; | |
148 | kdata_port = session->consumer->dst.net.data.port; | |
149 | } else if (kuri && uuri) { | |
150 | ret = uri_compare(kuri, uuri); | |
151 | if (ret) { | |
152 | /* Not Equal */ | |
153 | uri = kuri; | |
154 | /* Build uuri URL string */ | |
155 | ret = uri_to_str_url(uuri, tmp_uurl, sizeof(tmp_uurl)); | |
156 | if (ret < 0) { | |
157 | goto error; | |
158 | } | |
159 | } else { | |
160 | uri = kuri; | |
161 | } | |
162 | } else if (kuri && uuri == NULL) { | |
163 | uri = kuri; | |
164 | } else if (uuri && kuri == NULL) { | |
165 | uri = uuri; | |
166 | } | |
167 | ||
168 | ret = uri_to_str_url(uri, tmp_urls, sizeof(tmp_urls)); | |
169 | if (ret < 0) { | |
170 | goto error; | |
171 | } | |
172 | ||
9aa9f900 DG |
173 | /* |
174 | * Do we have a UST url set. If yes, this means we have both kernel and UST | |
175 | * to print. | |
176 | */ | |
9d035200 | 177 | if (*tmp_uurl != '\0') { |
2f77fc4b DG |
178 | ret = snprintf(dst, size, "[K]: %s [data: %d] -- [U]: %s [data: %d]", |
179 | tmp_urls, kdata_port, tmp_uurl, udata_port); | |
180 | } else { | |
9aa9f900 | 181 | int dport; |
bef08707 | 182 | if (kuri || (!kuri && !uuri)) { |
9aa9f900 DG |
183 | dport = kdata_port; |
184 | } else { | |
185 | /* No kernel URI, use the UST port. */ | |
186 | dport = udata_port; | |
187 | } | |
188 | ret = snprintf(dst, size, "%s [data: %d]", tmp_urls, dport); | |
2f77fc4b DG |
189 | } |
190 | ||
191 | error: | |
192 | return ret; | |
193 | } | |
194 | ||
fb83fe64 JD |
195 | /* |
196 | * Get run-time attributes if the session has been started (discarded events, | |
197 | * lost packets). | |
198 | */ | |
199 | static int get_kernel_runtime_stats(struct ltt_session *session, | |
200 | struct ltt_kernel_channel *kchan, uint64_t *discarded_events, | |
201 | uint64_t *lost_packets) | |
202 | { | |
203 | int ret; | |
204 | ||
205 | if (!session->has_been_started) { | |
206 | ret = 0; | |
207 | *discarded_events = 0; | |
208 | *lost_packets = 0; | |
209 | goto end; | |
210 | } | |
211 | ||
e1f3997a | 212 | ret = consumer_get_discarded_events(session->id, kchan->key, |
fb83fe64 JD |
213 | session->kernel_session->consumer, |
214 | discarded_events); | |
215 | if (ret < 0) { | |
216 | goto end; | |
217 | } | |
218 | ||
e1f3997a | 219 | ret = consumer_get_lost_packets(session->id, kchan->key, |
fb83fe64 JD |
220 | session->kernel_session->consumer, |
221 | lost_packets); | |
222 | if (ret < 0) { | |
223 | goto end; | |
224 | } | |
225 | ||
226 | end: | |
227 | return ret; | |
228 | } | |
229 | ||
230 | /* | |
231 | * Get run-time attributes if the session has been started (discarded events, | |
232 | * lost packets). | |
233 | */ | |
234 | static int get_ust_runtime_stats(struct ltt_session *session, | |
235 | struct ltt_ust_channel *uchan, uint64_t *discarded_events, | |
236 | uint64_t *lost_packets) | |
237 | { | |
238 | int ret; | |
239 | struct ltt_ust_session *usess; | |
240 | ||
a91c5803 JG |
241 | if (!discarded_events || !lost_packets) { |
242 | ret = -1; | |
243 | goto end; | |
244 | } | |
245 | ||
fb83fe64 | 246 | usess = session->ust_session; |
a905c624 JG |
247 | assert(discarded_events); |
248 | assert(lost_packets); | |
fb83fe64 JD |
249 | |
250 | if (!usess || !session->has_been_started) { | |
251 | *discarded_events = 0; | |
252 | *lost_packets = 0; | |
253 | ret = 0; | |
254 | goto end; | |
255 | } | |
256 | ||
257 | if (usess->buffer_type == LTTNG_BUFFER_PER_UID) { | |
258 | ret = ust_app_uid_get_channel_runtime_stats(usess->id, | |
259 | &usess->buffer_reg_uid_list, | |
260 | usess->consumer, uchan->id, | |
261 | uchan->attr.overwrite, | |
262 | discarded_events, | |
263 | lost_packets); | |
264 | } else if (usess->buffer_type == LTTNG_BUFFER_PER_PID) { | |
265 | ret = ust_app_pid_get_channel_runtime_stats(usess, | |
266 | uchan, usess->consumer, | |
267 | uchan->attr.overwrite, | |
268 | discarded_events, | |
269 | lost_packets); | |
270 | if (ret < 0) { | |
271 | goto end; | |
272 | } | |
273 | *discarded_events += uchan->per_pid_closed_app_discarded; | |
274 | *lost_packets += uchan->per_pid_closed_app_lost; | |
275 | } else { | |
276 | ERR("Unsupported buffer type"); | |
967bc289 | 277 | assert(0); |
fb83fe64 JD |
278 | ret = -1; |
279 | goto end; | |
280 | } | |
281 | ||
282 | end: | |
283 | return ret; | |
284 | } | |
285 | ||
2f77fc4b DG |
286 | /* |
287 | * Fill lttng_channel array of all channels. | |
288 | */ | |
d449df4a | 289 | static ssize_t list_lttng_channels(enum lttng_domain_type domain, |
fb83fe64 | 290 | struct ltt_session *session, struct lttng_channel *channels, |
cf0bcb51 | 291 | struct lttng_channel_extended *chan_exts) |
2f77fc4b | 292 | { |
d449df4a | 293 | int i = 0, ret = 0; |
2f77fc4b DG |
294 | struct ltt_kernel_channel *kchan; |
295 | ||
296 | DBG("Listing channels for session %s", session->name); | |
297 | ||
298 | switch (domain) { | |
299 | case LTTNG_DOMAIN_KERNEL: | |
300 | /* Kernel channels */ | |
301 | if (session->kernel_session != NULL) { | |
302 | cds_list_for_each_entry(kchan, | |
303 | &session->kernel_session->channel_list.head, list) { | |
fb83fe64 | 304 | uint64_t discarded_events, lost_packets; |
cf0bcb51 JG |
305 | struct lttng_channel_extended *extended; |
306 | ||
307 | extended = (struct lttng_channel_extended *) | |
308 | kchan->channel->attr.extended.ptr; | |
fb83fe64 JD |
309 | |
310 | ret = get_kernel_runtime_stats(session, kchan, | |
311 | &discarded_events, &lost_packets); | |
312 | if (ret < 0) { | |
313 | goto end; | |
314 | } | |
2f77fc4b DG |
315 | /* Copy lttng_channel struct to array */ |
316 | memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel)); | |
317 | channels[i].enabled = kchan->enabled; | |
fb83fe64 JD |
318 | chan_exts[i].discarded_events = |
319 | discarded_events; | |
320 | chan_exts[i].lost_packets = lost_packets; | |
cf0bcb51 JG |
321 | chan_exts[i].monitor_timer_interval = |
322 | extended->monitor_timer_interval; | |
491d1539 | 323 | chan_exts[i].blocking_timeout = 0; |
2f77fc4b DG |
324 | i++; |
325 | } | |
326 | } | |
327 | break; | |
328 | case LTTNG_DOMAIN_UST: | |
329 | { | |
330 | struct lttng_ht_iter iter; | |
331 | struct ltt_ust_channel *uchan; | |
332 | ||
e7fe706f | 333 | rcu_read_lock(); |
2f77fc4b DG |
334 | cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht, |
335 | &iter.iter, uchan, node.node) { | |
a91c5803 | 336 | uint64_t discarded_events = 0, lost_packets = 0; |
fb83fe64 | 337 | |
8ee609c8 MD |
338 | if (lttng_strncpy(channels[i].name, uchan->name, |
339 | LTTNG_SYMBOL_NAME_LEN)) { | |
340 | break; | |
341 | } | |
2f77fc4b DG |
342 | channels[i].attr.overwrite = uchan->attr.overwrite; |
343 | channels[i].attr.subbuf_size = uchan->attr.subbuf_size; | |
344 | channels[i].attr.num_subbuf = uchan->attr.num_subbuf; | |
345 | channels[i].attr.switch_timer_interval = | |
346 | uchan->attr.switch_timer_interval; | |
347 | channels[i].attr.read_timer_interval = | |
348 | uchan->attr.read_timer_interval; | |
349 | channels[i].enabled = uchan->enabled; | |
2f785fe7 DG |
350 | channels[i].attr.tracefile_size = uchan->tracefile_size; |
351 | channels[i].attr.tracefile_count = uchan->tracefile_count; | |
5e4435a4 JG |
352 | |
353 | /* | |
354 | * Map enum lttng_ust_output to enum lttng_event_output. | |
355 | */ | |
2f77fc4b DG |
356 | switch (uchan->attr.output) { |
357 | case LTTNG_UST_MMAP: | |
2f77fc4b DG |
358 | channels[i].attr.output = LTTNG_EVENT_MMAP; |
359 | break; | |
5e4435a4 JG |
360 | default: |
361 | /* | |
362 | * LTTNG_UST_MMAP is the only supported UST | |
363 | * output mode. | |
364 | */ | |
365 | assert(0); | |
366 | break; | |
2f77fc4b | 367 | } |
fb83fe64 | 368 | |
d449df4a MD |
369 | chan_exts[i].monitor_timer_interval = |
370 | uchan->monitor_timer_interval; | |
491d1539 MD |
371 | chan_exts[i].blocking_timeout = |
372 | uchan->attr.u.s.blocking_timeout; | |
d449df4a | 373 | |
fb83fe64 JD |
374 | ret = get_ust_runtime_stats(session, uchan, |
375 | &discarded_events, &lost_packets); | |
376 | if (ret < 0) { | |
377 | break; | |
378 | } | |
379 | chan_exts[i].discarded_events = discarded_events; | |
380 | chan_exts[i].lost_packets = lost_packets; | |
2f77fc4b DG |
381 | i++; |
382 | } | |
e7fe706f | 383 | rcu_read_unlock(); |
2f77fc4b DG |
384 | break; |
385 | } | |
386 | default: | |
387 | break; | |
388 | } | |
fb83fe64 JD |
389 | |
390 | end: | |
d449df4a MD |
391 | if (ret < 0) { |
392 | return -LTTNG_ERR_FATAL; | |
393 | } else { | |
394 | return LTTNG_OK; | |
395 | } | |
2f77fc4b DG |
396 | } |
397 | ||
3c02e545 FD |
398 | static int increment_extended_len(const char *filter_expression, |
399 | struct lttng_event_exclusion *exclusion, | |
400 | const struct lttng_userspace_probe_location *probe_location, | |
401 | size_t *extended_len) | |
b4e3ceb9 | 402 | { |
3c02e545 FD |
403 | int ret = 0; |
404 | ||
b4e3ceb9 PP |
405 | *extended_len += sizeof(struct lttcomm_event_extended_header); |
406 | ||
407 | if (filter_expression) { | |
408 | *extended_len += strlen(filter_expression) + 1; | |
409 | } | |
795d57ce PP |
410 | |
411 | if (exclusion) { | |
412 | *extended_len += exclusion->count * LTTNG_SYMBOL_NAME_LEN; | |
413 | } | |
3c02e545 FD |
414 | |
415 | if (probe_location) { | |
416 | ret = lttng_userspace_probe_location_serialize(probe_location, | |
417 | NULL, NULL); | |
418 | if (ret < 0) { | |
419 | goto end; | |
420 | } | |
421 | *extended_len += ret; | |
422 | } | |
423 | ret = 0; | |
424 | end: | |
425 | return ret; | |
b4e3ceb9 PP |
426 | } |
427 | ||
3c02e545 FD |
428 | static int append_extended_info(const char *filter_expression, |
429 | struct lttng_event_exclusion *exclusion, | |
430 | struct lttng_userspace_probe_location *probe_location, | |
431 | void **extended_at) | |
b4e3ceb9 | 432 | { |
3c02e545 | 433 | int ret = 0; |
b4e3ceb9 | 434 | size_t filter_len = 0; |
795d57ce | 435 | size_t nb_exclusions = 0; |
3c02e545 FD |
436 | size_t userspace_probe_location_len = 0; |
437 | struct lttng_dynamic_buffer location_buffer; | |
438 | struct lttcomm_event_extended_header extended_header; | |
b4e3ceb9 PP |
439 | |
440 | if (filter_expression) { | |
441 | filter_len = strlen(filter_expression) + 1; | |
442 | } | |
443 | ||
795d57ce PP |
444 | if (exclusion) { |
445 | nb_exclusions = exclusion->count; | |
446 | } | |
447 | ||
3c02e545 FD |
448 | if (probe_location) { |
449 | lttng_dynamic_buffer_init(&location_buffer); | |
450 | ret = lttng_userspace_probe_location_serialize(probe_location, | |
451 | &location_buffer, NULL); | |
452 | if (ret < 0) { | |
453 | ret = -1; | |
454 | goto end; | |
455 | } | |
456 | userspace_probe_location_len = location_buffer.size; | |
457 | } | |
458 | ||
795d57ce | 459 | /* Set header fields */ |
b4e3ceb9 | 460 | extended_header.filter_len = filter_len; |
795d57ce | 461 | extended_header.nb_exclusions = nb_exclusions; |
3c02e545 | 462 | extended_header.userspace_probe_location_len = userspace_probe_location_len; |
795d57ce PP |
463 | |
464 | /* Copy header */ | |
b4e3ceb9 PP |
465 | memcpy(*extended_at, &extended_header, sizeof(extended_header)); |
466 | *extended_at += sizeof(extended_header); | |
795d57ce PP |
467 | |
468 | /* Copy filter string */ | |
469 | if (filter_expression) { | |
470 | memcpy(*extended_at, filter_expression, filter_len); | |
471 | *extended_at += filter_len; | |
472 | } | |
473 | ||
474 | /* Copy exclusion names */ | |
475 | if (exclusion) { | |
476 | size_t len = nb_exclusions * LTTNG_SYMBOL_NAME_LEN; | |
477 | ||
478 | memcpy(*extended_at, &exclusion->names, len); | |
479 | *extended_at += len; | |
480 | } | |
3c02e545 FD |
481 | |
482 | if (probe_location) { | |
483 | memcpy(*extended_at, location_buffer.data, location_buffer.size); | |
484 | *extended_at += location_buffer.size; | |
485 | lttng_dynamic_buffer_reset(&location_buffer); | |
486 | } | |
487 | ret = 0; | |
488 | end: | |
489 | return ret; | |
b4e3ceb9 PP |
490 | } |
491 | ||
3c6a091f | 492 | /* |
022d91ba | 493 | * Create a list of agent domain events. |
3c6a091f DG |
494 | * |
495 | * Return number of events in list on success or else a negative value. | |
496 | */ | |
022d91ba | 497 | static int list_lttng_agent_events(struct agent *agt, |
b4e3ceb9 | 498 | struct lttng_event **events, size_t *total_size) |
3c6a091f DG |
499 | { |
500 | int i = 0, ret = 0; | |
501 | unsigned int nb_event = 0; | |
022d91ba | 502 | struct agent_event *event; |
47e52862 | 503 | struct lttng_event *tmp_events = NULL; |
3c6a091f | 504 | struct lttng_ht_iter iter; |
b4e3ceb9 PP |
505 | size_t extended_len = 0; |
506 | void *extended_at; | |
3c6a091f | 507 | |
022d91ba | 508 | assert(agt); |
3c6a091f DG |
509 | assert(events); |
510 | ||
022d91ba | 511 | DBG3("Listing agent events"); |
3c6a091f | 512 | |
e5bbf678 | 513 | rcu_read_lock(); |
022d91ba | 514 | nb_event = lttng_ht_get_count(agt->events); |
e5bbf678 | 515 | rcu_read_unlock(); |
3c6a091f DG |
516 | if (nb_event == 0) { |
517 | ret = nb_event; | |
b4e3ceb9 | 518 | *total_size = 0; |
3c6a091f DG |
519 | goto error; |
520 | } | |
521 | ||
b4e3ceb9 PP |
522 | /* Compute required extended infos size */ |
523 | extended_len = nb_event * sizeof(struct lttcomm_event_extended_header); | |
524 | ||
525 | /* | |
526 | * This is only valid because the commands which add events are | |
527 | * processed in the same thread as the listing. | |
528 | */ | |
529 | rcu_read_lock(); | |
530 | cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) { | |
3c02e545 | 531 | ret = increment_extended_len(event->filter_expression, NULL, NULL, |
795d57ce | 532 | &extended_len); |
3c02e545 FD |
533 | if (ret) { |
534 | DBG("Error computing the length of extended info message"); | |
535 | ret = -LTTNG_ERR_FATAL; | |
536 | goto error; | |
537 | } | |
b4e3ceb9 PP |
538 | } |
539 | rcu_read_unlock(); | |
540 | ||
541 | *total_size = nb_event * sizeof(*tmp_events) + extended_len; | |
542 | tmp_events = zmalloc(*total_size); | |
3c6a091f | 543 | if (!tmp_events) { |
022d91ba | 544 | PERROR("zmalloc agent events session"); |
3c6a091f DG |
545 | ret = -LTTNG_ERR_FATAL; |
546 | goto error; | |
547 | } | |
548 | ||
b4e3ceb9 PP |
549 | extended_at = ((uint8_t *) tmp_events) + |
550 | nb_event * sizeof(struct lttng_event); | |
551 | ||
3c6a091f | 552 | rcu_read_lock(); |
022d91ba | 553 | cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) { |
3c6a091f DG |
554 | strncpy(tmp_events[i].name, event->name, sizeof(tmp_events[i].name)); |
555 | tmp_events[i].name[sizeof(tmp_events[i].name) - 1] = '\0'; | |
556 | tmp_events[i].enabled = event->enabled; | |
2106efa0 | 557 | tmp_events[i].loglevel = event->loglevel_value; |
ff94328f | 558 | tmp_events[i].loglevel_type = event->loglevel_type; |
3c6a091f | 559 | i++; |
b4e3ceb9 PP |
560 | |
561 | /* Append extended info */ | |
3c02e545 | 562 | ret = append_extended_info(event->filter_expression, NULL, NULL, |
795d57ce | 563 | &extended_at); |
3c02e545 FD |
564 | if (ret) { |
565 | DBG("Error appending extended info message"); | |
566 | ret = -LTTNG_ERR_FATAL; | |
567 | goto error; | |
568 | } | |
3c6a091f | 569 | } |
3c6a091f DG |
570 | |
571 | *events = tmp_events; | |
572 | ret = nb_event; | |
3c02e545 | 573 | assert(nb_event == i); |
3c6a091f | 574 | |
47e52862 | 575 | end: |
3c02e545 | 576 | rcu_read_unlock(); |
3c6a091f | 577 | return ret; |
47e52862 JG |
578 | error: |
579 | free(tmp_events); | |
580 | goto end; | |
3c6a091f DG |
581 | } |
582 | ||
2f77fc4b DG |
583 | /* |
584 | * Create a list of ust global domain events. | |
585 | */ | |
586 | static int list_lttng_ust_global_events(char *channel_name, | |
b4e3ceb9 PP |
587 | struct ltt_ust_domain_global *ust_global, |
588 | struct lttng_event **events, size_t *total_size) | |
2f77fc4b DG |
589 | { |
590 | int i = 0, ret = 0; | |
591 | unsigned int nb_event = 0; | |
592 | struct lttng_ht_iter iter; | |
593 | struct lttng_ht_node_str *node; | |
594 | struct ltt_ust_channel *uchan; | |
595 | struct ltt_ust_event *uevent; | |
596 | struct lttng_event *tmp; | |
b4e3ceb9 PP |
597 | size_t extended_len = 0; |
598 | void *extended_at; | |
2f77fc4b DG |
599 | |
600 | DBG("Listing UST global events for channel %s", channel_name); | |
601 | ||
602 | rcu_read_lock(); | |
603 | ||
604 | lttng_ht_lookup(ust_global->channels, (void *)channel_name, &iter); | |
605 | node = lttng_ht_iter_get_node_str(&iter); | |
606 | if (node == NULL) { | |
f73fabfd | 607 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
d31d3e8c | 608 | goto end; |
2f77fc4b DG |
609 | } |
610 | ||
611 | uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node); | |
612 | ||
3c442be3 | 613 | nb_event = lttng_ht_get_count(uchan->events); |
2f77fc4b DG |
614 | if (nb_event == 0) { |
615 | ret = nb_event; | |
b4e3ceb9 | 616 | *total_size = 0; |
d31d3e8c | 617 | goto end; |
2f77fc4b DG |
618 | } |
619 | ||
620 | DBG3("Listing UST global %d events", nb_event); | |
621 | ||
b4e3ceb9 PP |
622 | /* Compute required extended infos size */ |
623 | cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) { | |
624 | if (uevent->internal) { | |
625 | nb_event--; | |
626 | continue; | |
627 | } | |
628 | ||
3c02e545 FD |
629 | ret = increment_extended_len(uevent->filter_expression, |
630 | uevent->exclusion, NULL, &extended_len); | |
631 | if (ret) { | |
632 | DBG("Error computing the length of extended info message"); | |
633 | ret = -LTTNG_ERR_FATAL; | |
634 | goto end; | |
635 | } | |
b4e3ceb9 | 636 | } |
d31d3e8c PP |
637 | if (nb_event == 0) { |
638 | /* All events are internal, skip. */ | |
639 | ret = 0; | |
640 | *total_size = 0; | |
641 | goto end; | |
642 | } | |
b4e3ceb9 PP |
643 | |
644 | *total_size = nb_event * sizeof(struct lttng_event) + extended_len; | |
645 | tmp = zmalloc(*total_size); | |
2f77fc4b | 646 | if (tmp == NULL) { |
b12c38df JG |
647 | ret = -LTTNG_ERR_FATAL; |
648 | goto end; | |
2f77fc4b DG |
649 | } |
650 | ||
b4e3ceb9 PP |
651 | extended_at = ((uint8_t *) tmp) + nb_event * sizeof(struct lttng_event); |
652 | ||
2f77fc4b | 653 | cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) { |
3c442be3 JG |
654 | if (uevent->internal) { |
655 | /* This event should remain hidden from clients */ | |
3c442be3 JG |
656 | continue; |
657 | } | |
2f77fc4b DG |
658 | strncpy(tmp[i].name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN); |
659 | tmp[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; | |
660 | tmp[i].enabled = uevent->enabled; | |
661 | ||
662 | switch (uevent->attr.instrumentation) { | |
663 | case LTTNG_UST_TRACEPOINT: | |
664 | tmp[i].type = LTTNG_EVENT_TRACEPOINT; | |
665 | break; | |
666 | case LTTNG_UST_PROBE: | |
667 | tmp[i].type = LTTNG_EVENT_PROBE; | |
668 | break; | |
669 | case LTTNG_UST_FUNCTION: | |
670 | tmp[i].type = LTTNG_EVENT_FUNCTION; | |
671 | break; | |
672 | } | |
673 | ||
674 | tmp[i].loglevel = uevent->attr.loglevel; | |
675 | switch (uevent->attr.loglevel_type) { | |
676 | case LTTNG_UST_LOGLEVEL_ALL: | |
677 | tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; | |
678 | break; | |
679 | case LTTNG_UST_LOGLEVEL_RANGE: | |
680 | tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE; | |
681 | break; | |
682 | case LTTNG_UST_LOGLEVEL_SINGLE: | |
683 | tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE; | |
684 | break; | |
685 | } | |
686 | if (uevent->filter) { | |
687 | tmp[i].filter = 1; | |
688 | } | |
4634f12e JI |
689 | if (uevent->exclusion) { |
690 | tmp[i].exclusion = 1; | |
691 | } | |
2f77fc4b | 692 | i++; |
b4e3ceb9 PP |
693 | |
694 | /* Append extended info */ | |
3c02e545 FD |
695 | ret = append_extended_info(uevent->filter_expression, |
696 | uevent->exclusion, NULL, &extended_at); | |
697 | if (ret) { | |
698 | DBG("Error appending extended info message"); | |
699 | ret = -LTTNG_ERR_FATAL; | |
700 | goto end; | |
701 | } | |
2f77fc4b DG |
702 | } |
703 | ||
704 | ret = nb_event; | |
705 | *events = tmp; | |
d31d3e8c | 706 | end: |
2f77fc4b DG |
707 | rcu_read_unlock(); |
708 | return ret; | |
709 | } | |
710 | ||
711 | /* | |
712 | * Fill lttng_event array of all kernel events in the channel. | |
713 | */ | |
714 | static int list_lttng_kernel_events(char *channel_name, | |
b4e3ceb9 PP |
715 | struct ltt_kernel_session *kernel_session, |
716 | struct lttng_event **events, size_t *total_size) | |
2f77fc4b DG |
717 | { |
718 | int i = 0, ret; | |
719 | unsigned int nb_event; | |
720 | struct ltt_kernel_event *event; | |
721 | struct ltt_kernel_channel *kchan; | |
b4e3ceb9 PP |
722 | size_t extended_len = 0; |
723 | void *extended_at; | |
2f77fc4b DG |
724 | |
725 | kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session); | |
726 | if (kchan == NULL) { | |
f73fabfd | 727 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
2f77fc4b DG |
728 | goto error; |
729 | } | |
730 | ||
731 | nb_event = kchan->event_count; | |
732 | ||
733 | DBG("Listing events for channel %s", kchan->channel->name); | |
734 | ||
735 | if (nb_event == 0) { | |
b4e3ceb9 | 736 | *total_size = 0; |
834978fd | 737 | *events = NULL; |
db906c12 | 738 | goto end; |
2f77fc4b DG |
739 | } |
740 | ||
b4e3ceb9 PP |
741 | /* Compute required extended infos size */ |
742 | cds_list_for_each_entry(event, &kchan->events_list.head, list) { | |
3c02e545 FD |
743 | ret = increment_extended_len(event->filter_expression, NULL, |
744 | event->userspace_probe_location, | |
795d57ce | 745 | &extended_len); |
3c02e545 FD |
746 | if (ret) { |
747 | DBG("Error computing the length of extended info message"); | |
748 | ret = -LTTNG_ERR_FATAL; | |
749 | goto error; | |
750 | } | |
b4e3ceb9 PP |
751 | } |
752 | ||
753 | *total_size = nb_event * sizeof(struct lttng_event) + extended_len; | |
754 | *events = zmalloc(*total_size); | |
2f77fc4b | 755 | if (*events == NULL) { |
3c02e545 | 756 | ret = -LTTNG_ERR_FATAL; |
2f77fc4b DG |
757 | goto error; |
758 | } | |
759 | ||
ab4aa612 | 760 | extended_at = ((void *) *events) + |
b4e3ceb9 PP |
761 | nb_event * sizeof(struct lttng_event); |
762 | ||
2f77fc4b DG |
763 | /* Kernel channels */ |
764 | cds_list_for_each_entry(event, &kchan->events_list.head , list) { | |
765 | strncpy((*events)[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN); | |
766 | (*events)[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; | |
767 | (*events)[i].enabled = event->enabled; | |
00f992f2 JG |
768 | (*events)[i].filter = |
769 | (unsigned char) !!event->filter_expression; | |
2f77fc4b DG |
770 | |
771 | switch (event->event->instrumentation) { | |
772 | case LTTNG_KERNEL_TRACEPOINT: | |
773 | (*events)[i].type = LTTNG_EVENT_TRACEPOINT; | |
774 | break; | |
2f77fc4b | 775 | case LTTNG_KERNEL_KRETPROBE: |
1896972b DG |
776 | (*events)[i].type = LTTNG_EVENT_FUNCTION; |
777 | memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe, | |
778 | sizeof(struct lttng_kernel_kprobe)); | |
779 | break; | |
780 | case LTTNG_KERNEL_KPROBE: | |
2f77fc4b DG |
781 | (*events)[i].type = LTTNG_EVENT_PROBE; |
782 | memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe, | |
783 | sizeof(struct lttng_kernel_kprobe)); | |
784 | break; | |
b955b4d4 FD |
785 | case LTTNG_KERNEL_UPROBE: |
786 | (*events)[i].type = LTTNG_EVENT_USERSPACE_PROBE; | |
787 | break; | |
2f77fc4b DG |
788 | case LTTNG_KERNEL_FUNCTION: |
789 | (*events)[i].type = LTTNG_EVENT_FUNCTION; | |
790 | memcpy(&((*events)[i].attr.ftrace), &event->event->u.ftrace, | |
791 | sizeof(struct lttng_kernel_function)); | |
792 | break; | |
793 | case LTTNG_KERNEL_NOOP: | |
794 | (*events)[i].type = LTTNG_EVENT_NOOP; | |
795 | break; | |
796 | case LTTNG_KERNEL_SYSCALL: | |
797 | (*events)[i].type = LTTNG_EVENT_SYSCALL; | |
798 | break; | |
799 | case LTTNG_KERNEL_ALL: | |
1ab8c2ad FD |
800 | /* fall-through. */ |
801 | default: | |
2f77fc4b DG |
802 | assert(0); |
803 | break; | |
804 | } | |
805 | i++; | |
b4e3ceb9 PP |
806 | |
807 | /* Append extended info */ | |
3c02e545 FD |
808 | ret = append_extended_info(event->filter_expression, NULL, |
809 | event->userspace_probe_location, &extended_at); | |
810 | if (ret) { | |
811 | DBG("Error appending extended info message"); | |
812 | ret = -LTTNG_ERR_FATAL; | |
813 | goto error; | |
814 | } | |
2f77fc4b DG |
815 | } |
816 | ||
db906c12 | 817 | end: |
2f77fc4b DG |
818 | return nb_event; |
819 | ||
820 | error: | |
f73fabfd DG |
821 | /* Negate the error code to differentiate the size from an error */ |
822 | return -ret; | |
2f77fc4b DG |
823 | } |
824 | ||
825 | /* | |
826 | * Add URI so the consumer output object. Set the correct path depending on the | |
827 | * domain adding the default trace directory. | |
828 | */ | |
5feee503 JG |
829 | static enum lttng_error_code add_uri_to_consumer( |
830 | const struct ltt_session *session, | |
831 | struct consumer_output *consumer, | |
832 | struct lttng_uri *uri, enum lttng_domain_type domain) | |
2f77fc4b | 833 | { |
5feee503 JG |
834 | int ret; |
835 | enum lttng_error_code ret_code = LTTNG_OK; | |
2f77fc4b DG |
836 | |
837 | assert(uri); | |
838 | ||
839 | if (consumer == NULL) { | |
840 | DBG("No consumer detected. Don't add URI. Stopping."); | |
5feee503 | 841 | ret_code = LTTNG_ERR_NO_CONSUMER; |
2f77fc4b DG |
842 | goto error; |
843 | } | |
844 | ||
845 | switch (domain) { | |
846 | case LTTNG_DOMAIN_KERNEL: | |
5feee503 JG |
847 | ret = lttng_strncpy(consumer->domain_subdir, |
848 | DEFAULT_KERNEL_TRACE_DIR, | |
849 | sizeof(consumer->domain_subdir)); | |
2f77fc4b DG |
850 | break; |
851 | case LTTNG_DOMAIN_UST: | |
5feee503 JG |
852 | ret = lttng_strncpy(consumer->domain_subdir, |
853 | DEFAULT_UST_TRACE_DIR, | |
854 | sizeof(consumer->domain_subdir)); | |
2f77fc4b DG |
855 | break; |
856 | default: | |
857 | /* | |
5feee503 JG |
858 | * This case is possible is we try to add the URI to the global |
859 | * tracing session consumer object which in this case there is | |
860 | * no subdir. | |
2f77fc4b | 861 | */ |
5feee503 JG |
862 | memset(consumer->domain_subdir, 0, |
863 | sizeof(consumer->domain_subdir)); | |
864 | ret = 0; | |
865 | } | |
866 | if (ret) { | |
867 | ERR("Failed to initialize consumer output domain subdirectory"); | |
868 | ret_code = LTTNG_ERR_FATAL; | |
869 | goto error; | |
2f77fc4b DG |
870 | } |
871 | ||
872 | switch (uri->dtype) { | |
873 | case LTTNG_DST_IPV4: | |
874 | case LTTNG_DST_IPV6: | |
875 | DBG2("Setting network URI to consumer"); | |
876 | ||
df75acac DG |
877 | if (consumer->type == CONSUMER_DST_NET) { |
878 | if ((uri->stype == LTTNG_STREAM_CONTROL && | |
785d2d0d DG |
879 | consumer->dst.net.control_isset) || |
880 | (uri->stype == LTTNG_STREAM_DATA && | |
881 | consumer->dst.net.data_isset)) { | |
5feee503 | 882 | ret_code = LTTNG_ERR_URL_EXIST; |
df75acac DG |
883 | goto error; |
884 | } | |
885 | } else { | |
5feee503 | 886 | memset(&consumer->dst, 0, sizeof(consumer->dst)); |
785d2d0d DG |
887 | } |
888 | ||
2f77fc4b | 889 | /* Set URI into consumer output object */ |
5feee503 | 890 | ret = consumer_set_network_uri(session, consumer, uri); |
2f77fc4b | 891 | if (ret < 0) { |
5feee503 | 892 | ret_code = -ret; |
2f77fc4b DG |
893 | goto error; |
894 | } else if (ret == 1) { | |
895 | /* | |
896 | * URI was the same in the consumer so we do not append the subdir | |
897 | * again so to not duplicate output dir. | |
898 | */ | |
5feee503 | 899 | ret_code = LTTNG_OK; |
2f77fc4b DG |
900 | goto error; |
901 | } | |
2f77fc4b DG |
902 | break; |
903 | case LTTNG_DST_PATH: | |
5feee503 JG |
904 | if (*uri->dst.path != '/' || strstr(uri->dst.path, "../")) { |
905 | ret_code = LTTNG_ERR_INVALID; | |
9ac05d92 MD |
906 | goto error; |
907 | } | |
5feee503 JG |
908 | DBG2("Setting trace directory path from URI to %s", |
909 | uri->dst.path); | |
910 | memset(&consumer->dst, 0, sizeof(consumer->dst)); | |
911 | ||
912 | ret = lttng_strncpy(consumer->dst.session_root_path, | |
913 | uri->dst.path, | |
914 | sizeof(consumer->dst.session_root_path)); | |
2f77fc4b DG |
915 | consumer->type = CONSUMER_DST_LOCAL; |
916 | break; | |
917 | } | |
918 | ||
5feee503 | 919 | ret_code = LTTNG_OK; |
2f77fc4b | 920 | error: |
5feee503 | 921 | return ret_code; |
2f77fc4b DG |
922 | } |
923 | ||
924 | /* | |
925 | * Init tracing by creating trace directory and sending fds kernel consumer. | |
926 | */ | |
927 | static int init_kernel_tracing(struct ltt_kernel_session *session) | |
928 | { | |
929 | int ret = 0; | |
930 | struct lttng_ht_iter iter; | |
931 | struct consumer_socket *socket; | |
932 | ||
933 | assert(session); | |
934 | ||
e7fe706f DG |
935 | rcu_read_lock(); |
936 | ||
2f77fc4b DG |
937 | if (session->consumer_fds_sent == 0 && session->consumer != NULL) { |
938 | cds_lfht_for_each_entry(session->consumer->socks->ht, &iter.iter, | |
939 | socket, node.node) { | |
2f77fc4b | 940 | pthread_mutex_lock(socket->lock); |
f50f23d9 | 941 | ret = kernel_consumer_send_session(socket, session); |
2f77fc4b DG |
942 | pthread_mutex_unlock(socket->lock); |
943 | if (ret < 0) { | |
f73fabfd | 944 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
2f77fc4b DG |
945 | goto error; |
946 | } | |
947 | } | |
948 | } | |
949 | ||
950 | error: | |
e7fe706f | 951 | rcu_read_unlock(); |
2f77fc4b DG |
952 | return ret; |
953 | } | |
954 | ||
955 | /* | |
956 | * Create a socket to the relayd using the URI. | |
957 | * | |
958 | * On success, the relayd_sock pointer is set to the created socket. | |
8f07cd01 | 959 | * Else, it remains untouched and an LTTng error code is returned. |
2f77fc4b | 960 | */ |
8f07cd01 | 961 | static enum lttng_error_code create_connect_relayd(struct lttng_uri *uri, |
b31610f2 JD |
962 | struct lttcomm_relayd_sock **relayd_sock, |
963 | struct consumer_output *consumer) | |
2f77fc4b DG |
964 | { |
965 | int ret; | |
8f07cd01 | 966 | enum lttng_error_code status = LTTNG_OK; |
6151a90f | 967 | struct lttcomm_relayd_sock *rsock; |
2f77fc4b | 968 | |
6151a90f JD |
969 | rsock = lttcomm_alloc_relayd_sock(uri, RELAYD_VERSION_COMM_MAJOR, |
970 | RELAYD_VERSION_COMM_MINOR); | |
971 | if (!rsock) { | |
8f07cd01 | 972 | status = LTTNG_ERR_FATAL; |
2f77fc4b DG |
973 | goto error; |
974 | } | |
975 | ||
ffe60014 DG |
976 | /* |
977 | * Connect to relayd so we can proceed with a session creation. This call | |
978 | * can possibly block for an arbitrary amount of time to set the health | |
979 | * state to be in poll execution. | |
980 | */ | |
981 | health_poll_entry(); | |
6151a90f | 982 | ret = relayd_connect(rsock); |
ffe60014 | 983 | health_poll_exit(); |
2f77fc4b DG |
984 | if (ret < 0) { |
985 | ERR("Unable to reach lttng-relayd"); | |
8f07cd01 | 986 | status = LTTNG_ERR_RELAYD_CONNECT_FAIL; |
2f77fc4b DG |
987 | goto free_sock; |
988 | } | |
989 | ||
990 | /* Create socket for control stream. */ | |
991 | if (uri->stype == LTTNG_STREAM_CONTROL) { | |
992 | DBG3("Creating relayd stream socket from URI"); | |
993 | ||
994 | /* Check relayd version */ | |
6151a90f | 995 | ret = relayd_version_check(rsock); |
67d5aa28 | 996 | if (ret == LTTNG_ERR_RELAYD_VERSION_FAIL) { |
8f07cd01 | 997 | status = LTTNG_ERR_RELAYD_VERSION_FAIL; |
67d5aa28 JD |
998 | goto close_sock; |
999 | } else if (ret < 0) { | |
1000 | ERR("Unable to reach lttng-relayd"); | |
8f07cd01 | 1001 | status = LTTNG_ERR_RELAYD_CONNECT_FAIL; |
2f77fc4b DG |
1002 | goto close_sock; |
1003 | } | |
b31610f2 JD |
1004 | consumer->relay_major_version = rsock->major; |
1005 | consumer->relay_minor_version = rsock->minor; | |
2f77fc4b DG |
1006 | } else if (uri->stype == LTTNG_STREAM_DATA) { |
1007 | DBG3("Creating relayd data socket from URI"); | |
1008 | } else { | |
1009 | /* Command is not valid */ | |
1010 | ERR("Relayd invalid stream type: %d", uri->stype); | |
8f07cd01 | 1011 | status = LTTNG_ERR_INVALID; |
2f77fc4b DG |
1012 | goto close_sock; |
1013 | } | |
1014 | ||
6151a90f | 1015 | *relayd_sock = rsock; |
2f77fc4b | 1016 | |
8f07cd01 | 1017 | return status; |
2f77fc4b DG |
1018 | |
1019 | close_sock: | |
6151a90f JD |
1020 | /* The returned value is not useful since we are on an error path. */ |
1021 | (void) relayd_close(rsock); | |
2f77fc4b | 1022 | free_sock: |
6151a90f | 1023 | free(rsock); |
2f77fc4b | 1024 | error: |
8f07cd01 | 1025 | return status; |
2f77fc4b DG |
1026 | } |
1027 | ||
1028 | /* | |
1029 | * Connect to the relayd using URI and send the socket to the right consumer. | |
43fade62 JG |
1030 | * |
1031 | * The consumer socket lock must be held by the caller. | |
8f07cd01 JG |
1032 | * |
1033 | * Returns LTTNG_OK on success or an LTTng error code on failure. | |
2f77fc4b | 1034 | */ |
8f07cd01 JG |
1035 | static enum lttng_error_code send_consumer_relayd_socket( |
1036 | unsigned int session_id, | |
3044f922 | 1037 | struct lttng_uri *relayd_uri, |
56a37563 | 1038 | struct consumer_output *consumer, |
d3e2ba59 | 1039 | struct consumer_socket *consumer_sock, |
65ff8ea3 | 1040 | const char *session_name, const char *hostname, |
b2c25d00 JG |
1041 | int session_live_timer, |
1042 | const uint64_t *current_chunk_id) | |
2f77fc4b DG |
1043 | { |
1044 | int ret; | |
6151a90f | 1045 | struct lttcomm_relayd_sock *rsock = NULL; |
8f07cd01 | 1046 | enum lttng_error_code status; |
2f77fc4b | 1047 | |
ffe60014 | 1048 | /* Connect to relayd and make version check if uri is the control. */ |
8f07cd01 JG |
1049 | status = create_connect_relayd(relayd_uri, &rsock, consumer); |
1050 | if (status != LTTNG_OK) { | |
9e218353 | 1051 | goto relayd_comm_error; |
ffe60014 | 1052 | } |
6151a90f | 1053 | assert(rsock); |
ffe60014 | 1054 | |
2f77fc4b | 1055 | /* Set the network sequence index if not set. */ |
d88aee68 DG |
1056 | if (consumer->net_seq_index == (uint64_t) -1ULL) { |
1057 | pthread_mutex_lock(&relayd_net_seq_idx_lock); | |
2f77fc4b DG |
1058 | /* |
1059 | * Increment net_seq_idx because we are about to transfer the | |
1060 | * new relayd socket to the consumer. | |
d88aee68 | 1061 | * Assign unique key so the consumer can match streams. |
2f77fc4b | 1062 | */ |
d88aee68 DG |
1063 | consumer->net_seq_index = ++relayd_net_seq_idx; |
1064 | pthread_mutex_unlock(&relayd_net_seq_idx_lock); | |
2f77fc4b DG |
1065 | } |
1066 | ||
2f77fc4b | 1067 | /* Send relayd socket to consumer. */ |
6151a90f | 1068 | ret = consumer_send_relayd_socket(consumer_sock, rsock, consumer, |
d3e2ba59 | 1069 | relayd_uri->stype, session_id, |
b2c25d00 JG |
1070 | session_name, hostname, session_live_timer, |
1071 | current_chunk_id); | |
2f77fc4b | 1072 | if (ret < 0) { |
8f07cd01 | 1073 | status = LTTNG_ERR_ENABLE_CONSUMER_FAIL; |
2f77fc4b DG |
1074 | goto close_sock; |
1075 | } | |
1076 | ||
c890b720 DG |
1077 | /* Flag that the corresponding socket was sent. */ |
1078 | if (relayd_uri->stype == LTTNG_STREAM_CONTROL) { | |
ffe60014 | 1079 | consumer_sock->control_sock_sent = 1; |
c890b720 | 1080 | } else if (relayd_uri->stype == LTTNG_STREAM_DATA) { |
ffe60014 | 1081 | consumer_sock->data_sock_sent = 1; |
c890b720 DG |
1082 | } |
1083 | ||
2f77fc4b DG |
1084 | /* |
1085 | * Close socket which was dup on the consumer side. The session daemon does | |
1086 | * NOT keep track of the relayd socket(s) once transfer to the consumer. | |
1087 | */ | |
1088 | ||
1089 | close_sock: | |
8f07cd01 | 1090 | if (status != LTTNG_OK) { |
ffe60014 | 1091 | /* |
d9078d0c DG |
1092 | * The consumer output for this session should not be used anymore |
1093 | * since the relayd connection failed thus making any tracing or/and | |
1094 | * streaming not usable. | |
ffe60014 | 1095 | */ |
d9078d0c | 1096 | consumer->enabled = 0; |
ffe60014 | 1097 | } |
9e218353 JR |
1098 | (void) relayd_close(rsock); |
1099 | free(rsock); | |
1100 | ||
1101 | relayd_comm_error: | |
8f07cd01 | 1102 | return status; |
2f77fc4b DG |
1103 | } |
1104 | ||
1105 | /* | |
1106 | * Send both relayd sockets to a specific consumer and domain. This is a | |
1107 | * helper function to facilitate sending the information to the consumer for a | |
1108 | * session. | |
43fade62 JG |
1109 | * |
1110 | * The consumer socket lock must be held by the caller. | |
8f07cd01 JG |
1111 | * |
1112 | * Returns LTTNG_OK, or an LTTng error code on failure. | |
2f77fc4b | 1113 | */ |
8f07cd01 JG |
1114 | static enum lttng_error_code send_consumer_relayd_sockets( |
1115 | enum lttng_domain_type domain, | |
56a37563 | 1116 | unsigned int session_id, struct consumer_output *consumer, |
65ff8ea3 | 1117 | struct consumer_socket *sock, const char *session_name, |
b2c25d00 JG |
1118 | const char *hostname, int session_live_timer, |
1119 | const uint64_t *current_chunk_id) | |
2f77fc4b | 1120 | { |
8f07cd01 | 1121 | enum lttng_error_code status = LTTNG_OK; |
2f77fc4b | 1122 | |
2f77fc4b | 1123 | assert(consumer); |
6dc3064a | 1124 | assert(sock); |
2f77fc4b | 1125 | |
2f77fc4b | 1126 | /* Sending control relayd socket. */ |
ffe60014 | 1127 | if (!sock->control_sock_sent) { |
8f07cd01 | 1128 | status = send_consumer_relayd_socket(session_id, |
d3e2ba59 | 1129 | &consumer->dst.net.control, consumer, sock, |
b2c25d00 JG |
1130 | session_name, hostname, session_live_timer, |
1131 | current_chunk_id); | |
8f07cd01 | 1132 | if (status != LTTNG_OK) { |
c890b720 DG |
1133 | goto error; |
1134 | } | |
2f77fc4b DG |
1135 | } |
1136 | ||
1137 | /* Sending data relayd socket. */ | |
ffe60014 | 1138 | if (!sock->data_sock_sent) { |
8f07cd01 | 1139 | status = send_consumer_relayd_socket(session_id, |
d3e2ba59 | 1140 | &consumer->dst.net.data, consumer, sock, |
b2c25d00 JG |
1141 | session_name, hostname, session_live_timer, |
1142 | current_chunk_id); | |
8f07cd01 | 1143 | if (status != LTTNG_OK) { |
c890b720 DG |
1144 | goto error; |
1145 | } | |
2f77fc4b DG |
1146 | } |
1147 | ||
2f77fc4b | 1148 | error: |
8f07cd01 | 1149 | return status; |
2f77fc4b DG |
1150 | } |
1151 | ||
1152 | /* | |
1153 | * Setup relayd connections for a tracing session. First creates the socket to | |
1154 | * the relayd and send them to the right domain consumer. Consumer type MUST be | |
1155 | * network. | |
1156 | */ | |
ffe60014 | 1157 | int cmd_setup_relayd(struct ltt_session *session) |
2f77fc4b | 1158 | { |
f73fabfd | 1159 | int ret = LTTNG_OK; |
2f77fc4b DG |
1160 | struct ltt_ust_session *usess; |
1161 | struct ltt_kernel_session *ksess; | |
1162 | struct consumer_socket *socket; | |
1163 | struct lttng_ht_iter iter; | |
b2c25d00 | 1164 | LTTNG_OPTIONAL(uint64_t) current_chunk_id = {}; |
2f77fc4b | 1165 | |
b2c25d00 | 1166 | assert(session); |
2f77fc4b DG |
1167 | |
1168 | usess = session->ust_session; | |
1169 | ksess = session->kernel_session; | |
1170 | ||
785d2d0d | 1171 | DBG("Setting relayd for session %s", session->name); |
2f77fc4b | 1172 | |
b2c25d00 JG |
1173 | if (session->current_trace_chunk) { |
1174 | enum lttng_trace_chunk_status status = lttng_trace_chunk_get_id( | |
1175 | session->current_trace_chunk, ¤t_chunk_id.value); | |
1176 | ||
1177 | if (status == LTTNG_TRACE_CHUNK_STATUS_OK) { | |
1178 | current_chunk_id.is_set = true; | |
1179 | } else { | |
1180 | ERR("Failed to get current trace chunk id"); | |
1181 | ret = LTTNG_ERR_UNK; | |
1182 | goto error; | |
1183 | } | |
1184 | } | |
1185 | ||
e7fe706f DG |
1186 | rcu_read_lock(); |
1187 | ||
2f77fc4b DG |
1188 | if (usess && usess->consumer && usess->consumer->type == CONSUMER_DST_NET |
1189 | && usess->consumer->enabled) { | |
1190 | /* For each consumer socket, send relayd sockets */ | |
1191 | cds_lfht_for_each_entry(usess->consumer->socks->ht, &iter.iter, | |
1192 | socket, node.node) { | |
2f77fc4b | 1193 | pthread_mutex_lock(socket->lock); |
6dc3064a | 1194 | ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_UST, session->id, |
d3e2ba59 JD |
1195 | usess->consumer, socket, |
1196 | session->name, session->hostname, | |
b2c25d00 JG |
1197 | session->live_timer, |
1198 | current_chunk_id.is_set ? ¤t_chunk_id.value : NULL); | |
2f77fc4b | 1199 | pthread_mutex_unlock(socket->lock); |
f73fabfd | 1200 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1201 | goto error; |
1202 | } | |
6dc3064a DG |
1203 | /* Session is now ready for network streaming. */ |
1204 | session->net_handle = 1; | |
2f77fc4b | 1205 | } |
b31610f2 JD |
1206 | session->consumer->relay_major_version = |
1207 | usess->consumer->relay_major_version; | |
1208 | session->consumer->relay_minor_version = | |
1209 | usess->consumer->relay_minor_version; | |
2f77fc4b DG |
1210 | } |
1211 | ||
1212 | if (ksess && ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET | |
1213 | && ksess->consumer->enabled) { | |
1214 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter, | |
1215 | socket, node.node) { | |
2f77fc4b | 1216 | pthread_mutex_lock(socket->lock); |
6dc3064a | 1217 | ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL, session->id, |
d3e2ba59 JD |
1218 | ksess->consumer, socket, |
1219 | session->name, session->hostname, | |
b2c25d00 JG |
1220 | session->live_timer, |
1221 | current_chunk_id.is_set ? ¤t_chunk_id.value : NULL); | |
2f77fc4b | 1222 | pthread_mutex_unlock(socket->lock); |
f73fabfd | 1223 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1224 | goto error; |
1225 | } | |
6dc3064a DG |
1226 | /* Session is now ready for network streaming. */ |
1227 | session->net_handle = 1; | |
2f77fc4b | 1228 | } |
b31610f2 JD |
1229 | session->consumer->relay_major_version = |
1230 | ksess->consumer->relay_major_version; | |
1231 | session->consumer->relay_minor_version = | |
1232 | ksess->consumer->relay_minor_version; | |
2f77fc4b DG |
1233 | } |
1234 | ||
1235 | error: | |
e7fe706f | 1236 | rcu_read_unlock(); |
2f77fc4b DG |
1237 | return ret; |
1238 | } | |
1239 | ||
9b6c7ec5 DG |
1240 | /* |
1241 | * Start a kernel session by opening all necessary streams. | |
1242 | */ | |
1243 | static int start_kernel_session(struct ltt_kernel_session *ksess, int wpipe) | |
1244 | { | |
1245 | int ret; | |
1246 | struct ltt_kernel_channel *kchan; | |
1247 | ||
1248 | /* Open kernel metadata */ | |
07b86b52 | 1249 | if (ksess->metadata == NULL && ksess->output_traces) { |
9b6c7ec5 DG |
1250 | ret = kernel_open_metadata(ksess); |
1251 | if (ret < 0) { | |
1252 | ret = LTTNG_ERR_KERN_META_FAIL; | |
1253 | goto error; | |
1254 | } | |
1255 | } | |
1256 | ||
1257 | /* Open kernel metadata stream */ | |
07b86b52 | 1258 | if (ksess->metadata && ksess->metadata_stream_fd < 0) { |
9b6c7ec5 DG |
1259 | ret = kernel_open_metadata_stream(ksess); |
1260 | if (ret < 0) { | |
1261 | ERR("Kernel create metadata stream failed"); | |
1262 | ret = LTTNG_ERR_KERN_STREAM_FAIL; | |
1263 | goto error; | |
1264 | } | |
1265 | } | |
1266 | ||
1267 | /* For each channel */ | |
1268 | cds_list_for_each_entry(kchan, &ksess->channel_list.head, list) { | |
1269 | if (kchan->stream_count == 0) { | |
1270 | ret = kernel_open_channel_stream(kchan); | |
1271 | if (ret < 0) { | |
1272 | ret = LTTNG_ERR_KERN_STREAM_FAIL; | |
1273 | goto error; | |
1274 | } | |
1275 | /* Update the stream global counter */ | |
1276 | ksess->stream_count_global += ret; | |
1277 | } | |
1278 | } | |
1279 | ||
1280 | /* Setup kernel consumer socket and send fds to it */ | |
1281 | ret = init_kernel_tracing(ksess); | |
e43c41c5 | 1282 | if (ret != 0) { |
9b6c7ec5 DG |
1283 | ret = LTTNG_ERR_KERN_START_FAIL; |
1284 | goto error; | |
1285 | } | |
1286 | ||
1287 | /* This start the kernel tracing */ | |
1288 | ret = kernel_start_session(ksess); | |
1289 | if (ret < 0) { | |
1290 | ret = LTTNG_ERR_KERN_START_FAIL; | |
1291 | goto error; | |
1292 | } | |
1293 | ||
1294 | /* Quiescent wait after starting trace */ | |
462ae7ae | 1295 | kernel_wait_quiescent(wpipe); |
9b6c7ec5 | 1296 | |
14fb1ebe | 1297 | ksess->active = 1; |
9b6c7ec5 DG |
1298 | |
1299 | ret = LTTNG_OK; | |
1300 | ||
1301 | error: | |
1302 | return ret; | |
1303 | } | |
1304 | ||
2f77fc4b DG |
1305 | /* |
1306 | * Command LTTNG_DISABLE_CHANNEL processed by the client thread. | |
1307 | */ | |
56a37563 JG |
1308 | int cmd_disable_channel(struct ltt_session *session, |
1309 | enum lttng_domain_type domain, char *channel_name) | |
2f77fc4b DG |
1310 | { |
1311 | int ret; | |
1312 | struct ltt_ust_session *usess; | |
1313 | ||
1314 | usess = session->ust_session; | |
1315 | ||
2223c96f DG |
1316 | rcu_read_lock(); |
1317 | ||
2f77fc4b DG |
1318 | switch (domain) { |
1319 | case LTTNG_DOMAIN_KERNEL: | |
1320 | { | |
1321 | ret = channel_kernel_disable(session->kernel_session, | |
1322 | channel_name); | |
f73fabfd | 1323 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1324 | goto error; |
1325 | } | |
1326 | ||
1327 | kernel_wait_quiescent(kernel_tracer_fd); | |
1328 | break; | |
1329 | } | |
1330 | case LTTNG_DOMAIN_UST: | |
1331 | { | |
1332 | struct ltt_ust_channel *uchan; | |
1333 | struct lttng_ht *chan_ht; | |
1334 | ||
1335 | chan_ht = usess->domain_global.channels; | |
1336 | ||
1337 | uchan = trace_ust_find_channel_by_name(chan_ht, channel_name); | |
1338 | if (uchan == NULL) { | |
f73fabfd | 1339 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
2f77fc4b DG |
1340 | goto error; |
1341 | } | |
1342 | ||
7972aab2 | 1343 | ret = channel_ust_disable(usess, uchan); |
f73fabfd | 1344 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1345 | goto error; |
1346 | } | |
1347 | break; | |
1348 | } | |
2f77fc4b | 1349 | default: |
f73fabfd | 1350 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
2f77fc4b DG |
1351 | goto error; |
1352 | } | |
1353 | ||
f73fabfd | 1354 | ret = LTTNG_OK; |
2f77fc4b DG |
1355 | |
1356 | error: | |
2223c96f | 1357 | rcu_read_unlock(); |
2f77fc4b DG |
1358 | return ret; |
1359 | } | |
1360 | ||
ccf10263 MD |
1361 | /* |
1362 | * Command LTTNG_TRACK_PID processed by the client thread. | |
a9ad0c8f MD |
1363 | * |
1364 | * Called with session lock held. | |
ccf10263 | 1365 | */ |
56a37563 JG |
1366 | int cmd_track_pid(struct ltt_session *session, enum lttng_domain_type domain, |
1367 | int pid) | |
ccf10263 MD |
1368 | { |
1369 | int ret; | |
1370 | ||
1371 | rcu_read_lock(); | |
1372 | ||
1373 | switch (domain) { | |
1374 | case LTTNG_DOMAIN_KERNEL: | |
1375 | { | |
1376 | struct ltt_kernel_session *ksess; | |
1377 | ||
1378 | ksess = session->kernel_session; | |
1379 | ||
1380 | ret = kernel_track_pid(ksess, pid); | |
1381 | if (ret != LTTNG_OK) { | |
1382 | goto error; | |
1383 | } | |
1384 | ||
1385 | kernel_wait_quiescent(kernel_tracer_fd); | |
1386 | break; | |
1387 | } | |
ccf10263 | 1388 | case LTTNG_DOMAIN_UST: |
a9ad0c8f MD |
1389 | { |
1390 | struct ltt_ust_session *usess; | |
1391 | ||
1392 | usess = session->ust_session; | |
1393 | ||
1394 | ret = trace_ust_track_pid(usess, pid); | |
1395 | if (ret != LTTNG_OK) { | |
1396 | goto error; | |
1397 | } | |
1398 | break; | |
1399 | } | |
ccf10263 MD |
1400 | default: |
1401 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; | |
1402 | goto error; | |
1403 | } | |
1404 | ||
1405 | ret = LTTNG_OK; | |
1406 | ||
1407 | error: | |
1408 | rcu_read_unlock(); | |
1409 | return ret; | |
1410 | } | |
1411 | ||
1412 | /* | |
1413 | * Command LTTNG_UNTRACK_PID processed by the client thread. | |
a9ad0c8f MD |
1414 | * |
1415 | * Called with session lock held. | |
ccf10263 | 1416 | */ |
56a37563 JG |
1417 | int cmd_untrack_pid(struct ltt_session *session, enum lttng_domain_type domain, |
1418 | int pid) | |
ccf10263 MD |
1419 | { |
1420 | int ret; | |
1421 | ||
1422 | rcu_read_lock(); | |
1423 | ||
1424 | switch (domain) { | |
1425 | case LTTNG_DOMAIN_KERNEL: | |
1426 | { | |
1427 | struct ltt_kernel_session *ksess; | |
1428 | ||
1429 | ksess = session->kernel_session; | |
1430 | ||
1431 | ret = kernel_untrack_pid(ksess, pid); | |
1432 | if (ret != LTTNG_OK) { | |
1433 | goto error; | |
1434 | } | |
1435 | ||
1436 | kernel_wait_quiescent(kernel_tracer_fd); | |
1437 | break; | |
1438 | } | |
ccf10263 | 1439 | case LTTNG_DOMAIN_UST: |
a9ad0c8f MD |
1440 | { |
1441 | struct ltt_ust_session *usess; | |
1442 | ||
1443 | usess = session->ust_session; | |
1444 | ||
1445 | ret = trace_ust_untrack_pid(usess, pid); | |
1446 | if (ret != LTTNG_OK) { | |
1447 | goto error; | |
1448 | } | |
1449 | break; | |
1450 | } | |
ccf10263 MD |
1451 | default: |
1452 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; | |
1453 | goto error; | |
1454 | } | |
1455 | ||
1456 | ret = LTTNG_OK; | |
1457 | ||
1458 | error: | |
1459 | rcu_read_unlock(); | |
1460 | return ret; | |
1461 | } | |
1462 | ||
2f77fc4b DG |
1463 | /* |
1464 | * Command LTTNG_ENABLE_CHANNEL processed by the client thread. | |
1465 | * | |
1466 | * The wpipe arguments is used as a notifier for the kernel thread. | |
1467 | */ | |
1468 | int cmd_enable_channel(struct ltt_session *session, | |
7972aab2 | 1469 | struct lttng_domain *domain, struct lttng_channel *attr, int wpipe) |
2f77fc4b DG |
1470 | { |
1471 | int ret; | |
1472 | struct ltt_ust_session *usess = session->ust_session; | |
1473 | struct lttng_ht *chan_ht; | |
1f345e94 | 1474 | size_t len; |
2f77fc4b DG |
1475 | |
1476 | assert(session); | |
1477 | assert(attr); | |
7972aab2 | 1478 | assert(domain); |
2f77fc4b | 1479 | |
f5436bfc | 1480 | len = lttng_strnlen(attr->name, sizeof(attr->name)); |
1f345e94 PP |
1481 | |
1482 | /* Validate channel name */ | |
1483 | if (attr->name[0] == '.' || | |
1484 | memchr(attr->name, '/', len) != NULL) { | |
1485 | ret = LTTNG_ERR_INVALID_CHANNEL_NAME; | |
1486 | goto end; | |
1487 | } | |
1488 | ||
2f77fc4b DG |
1489 | DBG("Enabling channel %s for session %s", attr->name, session->name); |
1490 | ||
03b4fdcf DG |
1491 | rcu_read_lock(); |
1492 | ||
ecc48a90 JD |
1493 | /* |
1494 | * Don't try to enable a channel if the session has been started at | |
1495 | * some point in time before. The tracer does not allow it. | |
1496 | */ | |
8382cf6f | 1497 | if (session->has_been_started) { |
ecc48a90 JD |
1498 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; |
1499 | goto error; | |
1500 | } | |
1501 | ||
1502 | /* | |
1503 | * If the session is a live session, remove the switch timer, the | |
1504 | * live timer does the same thing but sends also synchronisation | |
1505 | * beacons for inactive streams. | |
1506 | */ | |
1507 | if (session->live_timer > 0) { | |
1508 | attr->attr.live_timer_interval = session->live_timer; | |
1509 | attr->attr.switch_timer_interval = 0; | |
1510 | } | |
1511 | ||
6e21424e JR |
1512 | /* Check for feature support */ |
1513 | switch (domain->type) { | |
1514 | case LTTNG_DOMAIN_KERNEL: | |
1515 | { | |
1516 | if (kernel_supports_ring_buffer_snapshot_sample_positions(kernel_tracer_fd) != 1) { | |
1517 | /* Sampling position of buffer is not supported */ | |
1518 | WARN("Kernel tracer does not support buffer monitoring. " | |
1519 | "Setting the monitor interval timer to 0 " | |
1520 | "(disabled) for channel '%s' of session '%s'", | |
1521 | attr-> name, session->name); | |
1522 | lttng_channel_set_monitor_timer_interval(attr, 0); | |
1523 | } | |
1524 | break; | |
1525 | } | |
1526 | case LTTNG_DOMAIN_UST: | |
f28f9e44 | 1527 | break; |
6e21424e JR |
1528 | case LTTNG_DOMAIN_JUL: |
1529 | case LTTNG_DOMAIN_LOG4J: | |
1530 | case LTTNG_DOMAIN_PYTHON: | |
f28f9e44 JG |
1531 | if (!agent_tracing_is_enabled()) { |
1532 | DBG("Attempted to enable a channel in an agent domain but the agent thread is not running"); | |
1533 | ret = LTTNG_ERR_AGENT_TRACING_DISABLED; | |
1534 | goto error; | |
1535 | } | |
6e21424e JR |
1536 | break; |
1537 | default: | |
1538 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; | |
1539 | goto error; | |
1540 | } | |
1541 | ||
7972aab2 | 1542 | switch (domain->type) { |
2f77fc4b DG |
1543 | case LTTNG_DOMAIN_KERNEL: |
1544 | { | |
1545 | struct ltt_kernel_channel *kchan; | |
1546 | ||
2f77fc4b DG |
1547 | kchan = trace_kernel_get_channel_by_name(attr->name, |
1548 | session->kernel_session); | |
1549 | if (kchan == NULL) { | |
8954c04a JG |
1550 | if (session->snapshot.nb_output > 0 || |
1551 | session->snapshot_mode) { | |
1552 | /* Enforce mmap output for snapshot sessions. */ | |
1553 | attr->attr.output = LTTNG_EVENT_MMAP; | |
1554 | } | |
2f77fc4b | 1555 | ret = channel_kernel_create(session->kernel_session, attr, wpipe); |
85076754 MD |
1556 | if (attr->name[0] != '\0') { |
1557 | session->kernel_session->has_non_default_channel = 1; | |
1558 | } | |
2f77fc4b DG |
1559 | } else { |
1560 | ret = channel_kernel_enable(session->kernel_session, kchan); | |
1561 | } | |
1562 | ||
f73fabfd | 1563 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1564 | goto error; |
1565 | } | |
1566 | ||
784303b0 | 1567 | kernel_wait_quiescent(kernel_tracer_fd); |
2f77fc4b DG |
1568 | break; |
1569 | } | |
1570 | case LTTNG_DOMAIN_UST: | |
9232818f JG |
1571 | case LTTNG_DOMAIN_JUL: |
1572 | case LTTNG_DOMAIN_LOG4J: | |
1573 | case LTTNG_DOMAIN_PYTHON: | |
2f77fc4b DG |
1574 | { |
1575 | struct ltt_ust_channel *uchan; | |
1576 | ||
9232818f JG |
1577 | /* |
1578 | * FIXME | |
1579 | * | |
1580 | * Current agent implementation limitations force us to allow | |
1581 | * only one channel at once in "agent" subdomains. Each | |
1582 | * subdomain has a default channel name which must be strictly | |
1583 | * adhered to. | |
1584 | */ | |
1585 | if (domain->type == LTTNG_DOMAIN_JUL) { | |
1586 | if (strncmp(attr->name, DEFAULT_JUL_CHANNEL_NAME, | |
1587 | LTTNG_SYMBOL_NAME_LEN)) { | |
1588 | ret = LTTNG_ERR_INVALID_CHANNEL_NAME; | |
1589 | goto error; | |
1590 | } | |
1591 | } else if (domain->type == LTTNG_DOMAIN_LOG4J) { | |
1592 | if (strncmp(attr->name, DEFAULT_LOG4J_CHANNEL_NAME, | |
1593 | LTTNG_SYMBOL_NAME_LEN)) { | |
1594 | ret = LTTNG_ERR_INVALID_CHANNEL_NAME; | |
1595 | goto error; | |
1596 | } | |
1597 | } else if (domain->type == LTTNG_DOMAIN_PYTHON) { | |
1598 | if (strncmp(attr->name, DEFAULT_PYTHON_CHANNEL_NAME, | |
1599 | LTTNG_SYMBOL_NAME_LEN)) { | |
1600 | ret = LTTNG_ERR_INVALID_CHANNEL_NAME; | |
1601 | goto error; | |
1602 | } | |
1603 | } | |
1604 | ||
2f77fc4b DG |
1605 | chan_ht = usess->domain_global.channels; |
1606 | ||
1607 | uchan = trace_ust_find_channel_by_name(chan_ht, attr->name); | |
1608 | if (uchan == NULL) { | |
7972aab2 | 1609 | ret = channel_ust_create(usess, attr, domain->buf_type); |
85076754 MD |
1610 | if (attr->name[0] != '\0') { |
1611 | usess->has_non_default_channel = 1; | |
1612 | } | |
2f77fc4b | 1613 | } else { |
7972aab2 | 1614 | ret = channel_ust_enable(usess, uchan); |
2f77fc4b DG |
1615 | } |
1616 | break; | |
1617 | } | |
2f77fc4b | 1618 | default: |
f73fabfd | 1619 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
2f77fc4b DG |
1620 | goto error; |
1621 | } | |
1622 | ||
8954c04a JG |
1623 | if (ret == LTTNG_OK && attr->attr.output != LTTNG_EVENT_MMAP) { |
1624 | session->has_non_mmap_channel = true; | |
1625 | } | |
2f77fc4b | 1626 | error: |
2223c96f | 1627 | rcu_read_unlock(); |
1f345e94 | 1628 | end: |
2f77fc4b DG |
1629 | return ret; |
1630 | } | |
1631 | ||
2f77fc4b DG |
1632 | /* |
1633 | * Command LTTNG_DISABLE_EVENT processed by the client thread. | |
1634 | */ | |
56a37563 JG |
1635 | int cmd_disable_event(struct ltt_session *session, |
1636 | enum lttng_domain_type domain, char *channel_name, | |
6e911cad | 1637 | struct lttng_event *event) |
2f77fc4b DG |
1638 | { |
1639 | int ret; | |
6e911cad MD |
1640 | char *event_name; |
1641 | ||
18a720cd MD |
1642 | DBG("Disable event command for event \'%s\'", event->name); |
1643 | ||
6e911cad MD |
1644 | event_name = event->name; |
1645 | ||
9b7431cf JG |
1646 | /* Error out on unhandled search criteria */ |
1647 | if (event->loglevel_type || event->loglevel != -1 || event->enabled | |
6e911cad | 1648 | || event->pid || event->filter || event->exclusion) { |
7076b56e JG |
1649 | ret = LTTNG_ERR_UNK; |
1650 | goto error; | |
6e911cad | 1651 | } |
2f77fc4b | 1652 | |
2223c96f DG |
1653 | rcu_read_lock(); |
1654 | ||
2f77fc4b DG |
1655 | switch (domain) { |
1656 | case LTTNG_DOMAIN_KERNEL: | |
1657 | { | |
1658 | struct ltt_kernel_channel *kchan; | |
1659 | struct ltt_kernel_session *ksess; | |
1660 | ||
1661 | ksess = session->kernel_session; | |
1662 | ||
85076754 MD |
1663 | /* |
1664 | * If a non-default channel has been created in the | |
1665 | * session, explicitely require that -c chan_name needs | |
1666 | * to be provided. | |
1667 | */ | |
1668 | if (ksess->has_non_default_channel && channel_name[0] == '\0') { | |
1669 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; | |
7076b56e | 1670 | goto error_unlock; |
85076754 MD |
1671 | } |
1672 | ||
2f77fc4b DG |
1673 | kchan = trace_kernel_get_channel_by_name(channel_name, ksess); |
1674 | if (kchan == NULL) { | |
f73fabfd | 1675 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
7076b56e | 1676 | goto error_unlock; |
2f77fc4b DG |
1677 | } |
1678 | ||
6e911cad MD |
1679 | switch (event->type) { |
1680 | case LTTNG_EVENT_ALL: | |
9550ee81 | 1681 | case LTTNG_EVENT_TRACEPOINT: |
d0ae4ea8 | 1682 | case LTTNG_EVENT_SYSCALL: |
9550ee81 JR |
1683 | case LTTNG_EVENT_PROBE: |
1684 | case LTTNG_EVENT_FUNCTION: | |
1685 | case LTTNG_EVENT_FUNCTION_ENTRY:/* fall-through */ | |
1686 | if (event_name[0] == '\0') { | |
1687 | ret = event_kernel_disable_event(kchan, | |
1688 | NULL, event->type); | |
29c62722 | 1689 | } else { |
d0ae4ea8 | 1690 | ret = event_kernel_disable_event(kchan, |
9550ee81 | 1691 | event_name, event->type); |
29c62722 | 1692 | } |
6e911cad | 1693 | if (ret != LTTNG_OK) { |
7076b56e | 1694 | goto error_unlock; |
6e911cad MD |
1695 | } |
1696 | break; | |
6e911cad MD |
1697 | default: |
1698 | ret = LTTNG_ERR_UNK; | |
7076b56e | 1699 | goto error_unlock; |
2f77fc4b DG |
1700 | } |
1701 | ||
1702 | kernel_wait_quiescent(kernel_tracer_fd); | |
1703 | break; | |
1704 | } | |
1705 | case LTTNG_DOMAIN_UST: | |
1706 | { | |
1707 | struct ltt_ust_channel *uchan; | |
1708 | struct ltt_ust_session *usess; | |
1709 | ||
1710 | usess = session->ust_session; | |
1711 | ||
7076b56e JG |
1712 | if (validate_ust_event_name(event_name)) { |
1713 | ret = LTTNG_ERR_INVALID_EVENT_NAME; | |
1714 | goto error_unlock; | |
1715 | } | |
1716 | ||
85076754 MD |
1717 | /* |
1718 | * If a non-default channel has been created in the | |
9550ee81 | 1719 | * session, explicitly require that -c chan_name needs |
85076754 MD |
1720 | * to be provided. |
1721 | */ | |
1722 | if (usess->has_non_default_channel && channel_name[0] == '\0') { | |
1723 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; | |
7076b56e | 1724 | goto error_unlock; |
85076754 MD |
1725 | } |
1726 | ||
2f77fc4b DG |
1727 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, |
1728 | channel_name); | |
1729 | if (uchan == NULL) { | |
f73fabfd | 1730 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
7076b56e | 1731 | goto error_unlock; |
2f77fc4b DG |
1732 | } |
1733 | ||
6e911cad MD |
1734 | switch (event->type) { |
1735 | case LTTNG_EVENT_ALL: | |
b3639870 JR |
1736 | /* |
1737 | * An empty event name means that everything | |
1738 | * should be disabled. | |
1739 | */ | |
1740 | if (event->name[0] == '\0') { | |
1741 | ret = event_ust_disable_all_tracepoints(usess, uchan); | |
77d536b2 JR |
1742 | } else { |
1743 | ret = event_ust_disable_tracepoint(usess, uchan, | |
1744 | event_name); | |
1745 | } | |
6e911cad | 1746 | if (ret != LTTNG_OK) { |
7076b56e | 1747 | goto error_unlock; |
6e911cad MD |
1748 | } |
1749 | break; | |
1750 | default: | |
1751 | ret = LTTNG_ERR_UNK; | |
7076b56e | 1752 | goto error_unlock; |
2f77fc4b DG |
1753 | } |
1754 | ||
1755 | DBG3("Disable UST event %s in channel %s completed", event_name, | |
1756 | channel_name); | |
1757 | break; | |
1758 | } | |
5cdb6027 | 1759 | case LTTNG_DOMAIN_LOG4J: |
f20baf8e | 1760 | case LTTNG_DOMAIN_JUL: |
0e115563 | 1761 | case LTTNG_DOMAIN_PYTHON: |
f20baf8e | 1762 | { |
fefd409b | 1763 | struct agent *agt; |
f20baf8e DG |
1764 | struct ltt_ust_session *usess = session->ust_session; |
1765 | ||
1766 | assert(usess); | |
1767 | ||
6e911cad MD |
1768 | switch (event->type) { |
1769 | case LTTNG_EVENT_ALL: | |
1770 | break; | |
1771 | default: | |
1772 | ret = LTTNG_ERR_UNK; | |
7076b56e | 1773 | goto error_unlock; |
6e911cad MD |
1774 | } |
1775 | ||
5cdb6027 | 1776 | agt = trace_ust_find_agent(usess, domain); |
fefd409b DG |
1777 | if (!agt) { |
1778 | ret = -LTTNG_ERR_UST_EVENT_NOT_FOUND; | |
7076b56e | 1779 | goto error_unlock; |
fefd409b | 1780 | } |
b3639870 JR |
1781 | /* |
1782 | * An empty event name means that everything | |
1783 | * should be disabled. | |
1784 | */ | |
1785 | if (event->name[0] == '\0') { | |
18a720cd MD |
1786 | ret = event_agent_disable_all(usess, agt); |
1787 | } else { | |
1788 | ret = event_agent_disable(usess, agt, event_name); | |
1789 | } | |
f20baf8e | 1790 | if (ret != LTTNG_OK) { |
7076b56e | 1791 | goto error_unlock; |
f20baf8e DG |
1792 | } |
1793 | ||
1794 | break; | |
1795 | } | |
2f77fc4b | 1796 | default: |
f73fabfd | 1797 | ret = LTTNG_ERR_UND; |
7076b56e | 1798 | goto error_unlock; |
2f77fc4b DG |
1799 | } |
1800 | ||
f73fabfd | 1801 | ret = LTTNG_OK; |
2f77fc4b | 1802 | |
7076b56e | 1803 | error_unlock: |
2223c96f | 1804 | rcu_read_unlock(); |
7076b56e | 1805 | error: |
2f77fc4b DG |
1806 | return ret; |
1807 | } | |
1808 | ||
2f77fc4b DG |
1809 | /* |
1810 | * Command LTTNG_ADD_CONTEXT processed by the client thread. | |
1811 | */ | |
56a37563 | 1812 | int cmd_add_context(struct ltt_session *session, enum lttng_domain_type domain, |
601d5acf | 1813 | char *channel_name, struct lttng_event_context *ctx, int kwpipe) |
2f77fc4b | 1814 | { |
d5979e4a | 1815 | int ret, chan_kern_created = 0, chan_ust_created = 0; |
bdf64013 JG |
1816 | char *app_ctx_provider_name = NULL, *app_ctx_name = NULL; |
1817 | ||
9a699f7b JR |
1818 | /* |
1819 | * Don't try to add a context if the session has been started at | |
1820 | * some point in time before. The tracer does not allow it and would | |
1821 | * result in a corrupted trace. | |
1822 | */ | |
1823 | if (session->has_been_started) { | |
1824 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; | |
1825 | goto end; | |
1826 | } | |
1827 | ||
bdf64013 JG |
1828 | if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) { |
1829 | app_ctx_provider_name = ctx->u.app_ctx.provider_name; | |
1830 | app_ctx_name = ctx->u.app_ctx.ctx_name; | |
1831 | } | |
2f77fc4b DG |
1832 | |
1833 | switch (domain) { | |
1834 | case LTTNG_DOMAIN_KERNEL: | |
979e618e DG |
1835 | assert(session->kernel_session); |
1836 | ||
1837 | if (session->kernel_session->channel_count == 0) { | |
1838 | /* Create default channel */ | |
1839 | ret = channel_kernel_create(session->kernel_session, NULL, kwpipe); | |
1840 | if (ret != LTTNG_OK) { | |
1841 | goto error; | |
1842 | } | |
d5979e4a | 1843 | chan_kern_created = 1; |
979e618e | 1844 | } |
2f77fc4b | 1845 | /* Add kernel context to kernel tracer */ |
601d5acf | 1846 | ret = context_kernel_add(session->kernel_session, ctx, channel_name); |
f73fabfd | 1847 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1848 | goto error; |
1849 | } | |
1850 | break; | |
bdf64013 JG |
1851 | case LTTNG_DOMAIN_JUL: |
1852 | case LTTNG_DOMAIN_LOG4J: | |
1853 | { | |
1854 | /* | |
1855 | * Validate channel name. | |
1856 | * If no channel name is given and the domain is JUL or LOG4J, | |
1857 | * set it to the appropriate domain-specific channel name. If | |
1858 | * a name is provided but does not match the expexted channel | |
1859 | * name, return an error. | |
1860 | */ | |
1861 | if (domain == LTTNG_DOMAIN_JUL && *channel_name && | |
1862 | strcmp(channel_name, | |
1863 | DEFAULT_JUL_CHANNEL_NAME)) { | |
1864 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; | |
1865 | goto error; | |
1866 | } else if (domain == LTTNG_DOMAIN_LOG4J && *channel_name && | |
1867 | strcmp(channel_name, | |
1868 | DEFAULT_LOG4J_CHANNEL_NAME)) { | |
1869 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; | |
1870 | goto error; | |
1871 | } | |
a93b3916 | 1872 | /* break is _not_ missing here. */ |
bdf64013 | 1873 | } |
2f77fc4b DG |
1874 | case LTTNG_DOMAIN_UST: |
1875 | { | |
1876 | struct ltt_ust_session *usess = session->ust_session; | |
85076754 MD |
1877 | unsigned int chan_count; |
1878 | ||
2f77fc4b DG |
1879 | assert(usess); |
1880 | ||
85076754 | 1881 | chan_count = lttng_ht_get_count(usess->domain_global.channels); |
979e618e DG |
1882 | if (chan_count == 0) { |
1883 | struct lttng_channel *attr; | |
1884 | /* Create default channel */ | |
0a9c6494 | 1885 | attr = channel_new_default_attr(domain, usess->buffer_type); |
979e618e DG |
1886 | if (attr == NULL) { |
1887 | ret = LTTNG_ERR_FATAL; | |
1888 | goto error; | |
1889 | } | |
1890 | ||
7972aab2 | 1891 | ret = channel_ust_create(usess, attr, usess->buffer_type); |
979e618e DG |
1892 | if (ret != LTTNG_OK) { |
1893 | free(attr); | |
1894 | goto error; | |
1895 | } | |
cf0bcb51 | 1896 | channel_attr_destroy(attr); |
d5979e4a | 1897 | chan_ust_created = 1; |
979e618e DG |
1898 | } |
1899 | ||
601d5acf | 1900 | ret = context_ust_add(usess, domain, ctx, channel_name); |
bdf64013 JG |
1901 | free(app_ctx_provider_name); |
1902 | free(app_ctx_name); | |
1903 | app_ctx_name = NULL; | |
1904 | app_ctx_provider_name = NULL; | |
f73fabfd | 1905 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1906 | goto error; |
1907 | } | |
1908 | break; | |
1909 | } | |
2f77fc4b | 1910 | default: |
f73fabfd | 1911 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
1912 | goto error; |
1913 | } | |
1914 | ||
bdf64013 JG |
1915 | ret = LTTNG_OK; |
1916 | goto end; | |
2f77fc4b DG |
1917 | |
1918 | error: | |
d5979e4a DG |
1919 | if (chan_kern_created) { |
1920 | struct ltt_kernel_channel *kchan = | |
1921 | trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME, | |
1922 | session->kernel_session); | |
1923 | /* Created previously, this should NOT fail. */ | |
1924 | assert(kchan); | |
1925 | kernel_destroy_channel(kchan); | |
1926 | } | |
1927 | ||
1928 | if (chan_ust_created) { | |
1929 | struct ltt_ust_channel *uchan = | |
1930 | trace_ust_find_channel_by_name( | |
1931 | session->ust_session->domain_global.channels, | |
1932 | DEFAULT_CHANNEL_NAME); | |
1933 | /* Created previously, this should NOT fail. */ | |
1934 | assert(uchan); | |
1935 | /* Remove from the channel list of the session. */ | |
1936 | trace_ust_delete_channel(session->ust_session->domain_global.channels, | |
1937 | uchan); | |
1938 | trace_ust_destroy_channel(uchan); | |
1939 | } | |
bdf64013 JG |
1940 | end: |
1941 | free(app_ctx_provider_name); | |
1942 | free(app_ctx_name); | |
2f77fc4b DG |
1943 | return ret; |
1944 | } | |
1945 | ||
dac8e046 JG |
1946 | static inline bool name_starts_with(const char *name, const char *prefix) |
1947 | { | |
1948 | const size_t max_cmp_len = min(strlen(prefix), LTTNG_SYMBOL_NAME_LEN); | |
1949 | ||
1950 | return !strncmp(name, prefix, max_cmp_len); | |
1951 | } | |
1952 | ||
1953 | /* Perform userspace-specific event name validation */ | |
1954 | static int validate_ust_event_name(const char *name) | |
1955 | { | |
1956 | int ret = 0; | |
1957 | ||
1958 | if (!name) { | |
1959 | ret = -1; | |
1960 | goto end; | |
1961 | } | |
1962 | ||
1963 | /* | |
1964 | * Check name against all internal UST event component namespaces used | |
1965 | * by the agents. | |
1966 | */ | |
1967 | if (name_starts_with(name, DEFAULT_JUL_EVENT_COMPONENT) || | |
1968 | name_starts_with(name, DEFAULT_LOG4J_EVENT_COMPONENT) || | |
1969 | name_starts_with(name, DEFAULT_PYTHON_EVENT_COMPONENT)) { | |
1970 | ret = -1; | |
1971 | } | |
1972 | ||
1973 | end: | |
1974 | return ret; | |
1975 | } | |
88f06f15 | 1976 | |
2f77fc4b | 1977 | /* |
88f06f15 JG |
1978 | * Internal version of cmd_enable_event() with a supplemental |
1979 | * "internal_event" flag which is used to enable internal events which should | |
1980 | * be hidden from clients. Such events are used in the agent implementation to | |
1981 | * enable the events through which all "agent" events are funeled. | |
2f77fc4b | 1982 | */ |
88f06f15 JG |
1983 | static int _cmd_enable_event(struct ltt_session *session, |
1984 | struct lttng_domain *domain, | |
025faf73 | 1985 | char *channel_name, struct lttng_event *event, |
6b453b5e | 1986 | char *filter_expression, |
db8f1377 JI |
1987 | struct lttng_filter_bytecode *filter, |
1988 | struct lttng_event_exclusion *exclusion, | |
88f06f15 | 1989 | int wpipe, bool internal_event) |
2f77fc4b | 1990 | { |
9f449915 | 1991 | int ret = 0, channel_created = 0; |
cfedea03 | 1992 | struct lttng_channel *attr = NULL; |
2f77fc4b DG |
1993 | |
1994 | assert(session); | |
1995 | assert(event); | |
1996 | assert(channel_name); | |
1997 | ||
2a385866 JG |
1998 | /* If we have a filter, we must have its filter expression */ |
1999 | assert(!(!!filter_expression ^ !!filter)); | |
2000 | ||
9f449915 PP |
2001 | /* Normalize event name as a globbing pattern */ |
2002 | strutils_normalize_star_glob_pattern(event->name); | |
18a720cd | 2003 | |
9f449915 PP |
2004 | /* Normalize exclusion names as globbing patterns */ |
2005 | if (exclusion) { | |
2006 | size_t i; | |
f5ac4bd7 | 2007 | |
9f449915 PP |
2008 | for (i = 0; i < exclusion->count; i++) { |
2009 | char *name = LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, i); | |
2010 | ||
2011 | strutils_normalize_star_glob_pattern(name); | |
2012 | } | |
930a2e99 JG |
2013 | } |
2014 | ||
9f449915 PP |
2015 | DBG("Enable event command for event \'%s\'", event->name); |
2016 | ||
2017 | rcu_read_lock(); | |
2018 | ||
7972aab2 | 2019 | switch (domain->type) { |
2f77fc4b DG |
2020 | case LTTNG_DOMAIN_KERNEL: |
2021 | { | |
2022 | struct ltt_kernel_channel *kchan; | |
2023 | ||
85076754 MD |
2024 | /* |
2025 | * If a non-default channel has been created in the | |
2026 | * session, explicitely require that -c chan_name needs | |
2027 | * to be provided. | |
2028 | */ | |
2029 | if (session->kernel_session->has_non_default_channel | |
2030 | && channel_name[0] == '\0') { | |
2031 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; | |
2032 | goto error; | |
2033 | } | |
2034 | ||
2f77fc4b DG |
2035 | kchan = trace_kernel_get_channel_by_name(channel_name, |
2036 | session->kernel_session); | |
2037 | if (kchan == NULL) { | |
0a9c6494 DG |
2038 | attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL, |
2039 | LTTNG_BUFFER_GLOBAL); | |
2f77fc4b | 2040 | if (attr == NULL) { |
f73fabfd | 2041 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
2042 | goto error; |
2043 | } | |
04c17253 MD |
2044 | if (lttng_strncpy(attr->name, channel_name, |
2045 | sizeof(attr->name))) { | |
2046 | ret = LTTNG_ERR_INVALID; | |
04c17253 MD |
2047 | goto error; |
2048 | } | |
2f77fc4b DG |
2049 | |
2050 | ret = cmd_enable_channel(session, domain, attr, wpipe); | |
f73fabfd | 2051 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
2052 | goto error; |
2053 | } | |
e5f5db7f | 2054 | channel_created = 1; |
2f77fc4b DG |
2055 | } |
2056 | ||
2057 | /* Get the newly created kernel channel pointer */ | |
2058 | kchan = trace_kernel_get_channel_by_name(channel_name, | |
2059 | session->kernel_session); | |
2060 | if (kchan == NULL) { | |
2061 | /* This sould not happen... */ | |
f73fabfd | 2062 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
2063 | goto error; |
2064 | } | |
2065 | ||
6e911cad MD |
2066 | switch (event->type) { |
2067 | case LTTNG_EVENT_ALL: | |
29c62722 | 2068 | { |
00a62084 MD |
2069 | char *filter_expression_a = NULL; |
2070 | struct lttng_filter_bytecode *filter_a = NULL; | |
2071 | ||
2072 | /* | |
2073 | * We need to duplicate filter_expression and filter, | |
2074 | * because ownership is passed to first enable | |
2075 | * event. | |
2076 | */ | |
2077 | if (filter_expression) { | |
2078 | filter_expression_a = strdup(filter_expression); | |
2079 | if (!filter_expression_a) { | |
2080 | ret = LTTNG_ERR_FATAL; | |
2081 | goto error; | |
2082 | } | |
2083 | } | |
2084 | if (filter) { | |
2085 | filter_a = zmalloc(sizeof(*filter_a) + filter->len); | |
2086 | if (!filter_a) { | |
2087 | free(filter_expression_a); | |
2088 | ret = LTTNG_ERR_FATAL; | |
2089 | goto error; | |
2090 | } | |
2091 | memcpy(filter_a, filter, sizeof(*filter_a) + filter->len); | |
2092 | } | |
29c62722 | 2093 | event->type = LTTNG_EVENT_TRACEPOINT; /* Hack */ |
00a62084 MD |
2094 | ret = event_kernel_enable_event(kchan, event, |
2095 | filter_expression, filter); | |
a969e101 MD |
2096 | /* We have passed ownership */ |
2097 | filter_expression = NULL; | |
2098 | filter = NULL; | |
29c62722 MD |
2099 | if (ret != LTTNG_OK) { |
2100 | if (channel_created) { | |
2101 | /* Let's not leak a useless channel. */ | |
2102 | kernel_destroy_channel(kchan); | |
2103 | } | |
00a62084 MD |
2104 | free(filter_expression_a); |
2105 | free(filter_a); | |
29c62722 MD |
2106 | goto error; |
2107 | } | |
2108 | event->type = LTTNG_EVENT_SYSCALL; /* Hack */ | |
00a62084 MD |
2109 | ret = event_kernel_enable_event(kchan, event, |
2110 | filter_expression_a, filter_a); | |
60d21fa2 AB |
2111 | /* We have passed ownership */ |
2112 | filter_expression_a = NULL; | |
2113 | filter_a = NULL; | |
29c62722 MD |
2114 | if (ret != LTTNG_OK) { |
2115 | goto error; | |
2116 | } | |
2117 | break; | |
2118 | } | |
6e6ef3d7 | 2119 | case LTTNG_EVENT_PROBE: |
dcabc190 | 2120 | case LTTNG_EVENT_USERSPACE_PROBE: |
6e6ef3d7 DG |
2121 | case LTTNG_EVENT_FUNCTION: |
2122 | case LTTNG_EVENT_FUNCTION_ENTRY: | |
6e911cad | 2123 | case LTTNG_EVENT_TRACEPOINT: |
00a62084 MD |
2124 | ret = event_kernel_enable_event(kchan, event, |
2125 | filter_expression, filter); | |
a969e101 MD |
2126 | /* We have passed ownership */ |
2127 | filter_expression = NULL; | |
2128 | filter = NULL; | |
6e911cad MD |
2129 | if (ret != LTTNG_OK) { |
2130 | if (channel_created) { | |
2131 | /* Let's not leak a useless channel. */ | |
2132 | kernel_destroy_channel(kchan); | |
2133 | } | |
2134 | goto error; | |
e5f5db7f | 2135 | } |
6e911cad MD |
2136 | break; |
2137 | case LTTNG_EVENT_SYSCALL: | |
00a62084 MD |
2138 | ret = event_kernel_enable_event(kchan, event, |
2139 | filter_expression, filter); | |
a969e101 MD |
2140 | /* We have passed ownership */ |
2141 | filter_expression = NULL; | |
2142 | filter = NULL; | |
e2b957af MD |
2143 | if (ret != LTTNG_OK) { |
2144 | goto error; | |
2145 | } | |
6e911cad MD |
2146 | break; |
2147 | default: | |
2148 | ret = LTTNG_ERR_UNK; | |
2f77fc4b DG |
2149 | goto error; |
2150 | } | |
2151 | ||
2152 | kernel_wait_quiescent(kernel_tracer_fd); | |
2153 | break; | |
2154 | } | |
2155 | case LTTNG_DOMAIN_UST: | |
2156 | { | |
2157 | struct ltt_ust_channel *uchan; | |
2158 | struct ltt_ust_session *usess = session->ust_session; | |
2159 | ||
2160 | assert(usess); | |
2161 | ||
85076754 MD |
2162 | /* |
2163 | * If a non-default channel has been created in the | |
2164 | * session, explicitely require that -c chan_name needs | |
2165 | * to be provided. | |
2166 | */ | |
2167 | if (usess->has_non_default_channel && channel_name[0] == '\0') { | |
2168 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; | |
2169 | goto error; | |
2170 | } | |
2171 | ||
2f77fc4b DG |
2172 | /* Get channel from global UST domain */ |
2173 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, | |
2174 | channel_name); | |
2175 | if (uchan == NULL) { | |
2176 | /* Create default channel */ | |
0a9c6494 DG |
2177 | attr = channel_new_default_attr(LTTNG_DOMAIN_UST, |
2178 | usess->buffer_type); | |
2f77fc4b | 2179 | if (attr == NULL) { |
f73fabfd | 2180 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
2181 | goto error; |
2182 | } | |
04c17253 MD |
2183 | if (lttng_strncpy(attr->name, channel_name, |
2184 | sizeof(attr->name))) { | |
2185 | ret = LTTNG_ERR_INVALID; | |
04c17253 MD |
2186 | goto error; |
2187 | } | |
2f77fc4b DG |
2188 | |
2189 | ret = cmd_enable_channel(session, domain, attr, wpipe); | |
f73fabfd | 2190 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
2191 | goto error; |
2192 | } | |
2f77fc4b DG |
2193 | |
2194 | /* Get the newly created channel reference back */ | |
2195 | uchan = trace_ust_find_channel_by_name( | |
2196 | usess->domain_global.channels, channel_name); | |
2197 | assert(uchan); | |
2198 | } | |
2199 | ||
141feb8c JG |
2200 | if (uchan->domain != LTTNG_DOMAIN_UST && !internal_event) { |
2201 | /* | |
2202 | * Don't allow users to add UST events to channels which | |
2203 | * are assigned to a userspace subdomain (JUL, Log4J, | |
2204 | * Python, etc.). | |
2205 | */ | |
2206 | ret = LTTNG_ERR_INVALID_CHANNEL_DOMAIN; | |
2207 | goto error; | |
2208 | } | |
2209 | ||
dac8e046 JG |
2210 | if (!internal_event) { |
2211 | /* | |
2212 | * Ensure the event name is not reserved for internal | |
2213 | * use. | |
2214 | */ | |
2215 | ret = validate_ust_event_name(event->name); | |
2216 | if (ret) { | |
2217 | WARN("Userspace event name %s failed validation.", | |
bbcab087 | 2218 | event->name); |
dac8e046 JG |
2219 | ret = LTTNG_ERR_INVALID_EVENT_NAME; |
2220 | goto error; | |
2221 | } | |
2222 | } | |
2223 | ||
2f77fc4b | 2224 | /* At this point, the session and channel exist on the tracer */ |
6b453b5e | 2225 | ret = event_ust_enable_tracepoint(usess, uchan, event, |
88f06f15 JG |
2226 | filter_expression, filter, exclusion, |
2227 | internal_event); | |
49d21f93 MD |
2228 | /* We have passed ownership */ |
2229 | filter_expression = NULL; | |
2230 | filter = NULL; | |
2231 | exclusion = NULL; | |
94382e15 JG |
2232 | if (ret == LTTNG_ERR_UST_EVENT_ENABLED) { |
2233 | goto already_enabled; | |
2234 | } else if (ret != LTTNG_OK) { | |
2f77fc4b DG |
2235 | goto error; |
2236 | } | |
2237 | break; | |
2238 | } | |
5cdb6027 | 2239 | case LTTNG_DOMAIN_LOG4J: |
f20baf8e | 2240 | case LTTNG_DOMAIN_JUL: |
0e115563 | 2241 | case LTTNG_DOMAIN_PYTHON: |
f20baf8e | 2242 | { |
da6c3a50 | 2243 | const char *default_event_name, *default_chan_name; |
fefd409b | 2244 | struct agent *agt; |
f20baf8e DG |
2245 | struct lttng_event uevent; |
2246 | struct lttng_domain tmp_dom; | |
2247 | struct ltt_ust_session *usess = session->ust_session; | |
2248 | ||
2249 | assert(usess); | |
2250 | ||
f28f9e44 JG |
2251 | if (!agent_tracing_is_enabled()) { |
2252 | DBG("Attempted to enable an event in an agent domain but the agent thread is not running"); | |
2253 | ret = LTTNG_ERR_AGENT_TRACING_DISABLED; | |
2254 | goto error; | |
2255 | } | |
2256 | ||
5cdb6027 | 2257 | agt = trace_ust_find_agent(usess, domain->type); |
fefd409b | 2258 | if (!agt) { |
5cdb6027 | 2259 | agt = agent_create(domain->type); |
fefd409b | 2260 | if (!agt) { |
e5b3c48c | 2261 | ret = LTTNG_ERR_NOMEM; |
fefd409b DG |
2262 | goto error; |
2263 | } | |
2264 | agent_add(agt, usess->agents); | |
2265 | } | |
2266 | ||
022d91ba | 2267 | /* Create the default tracepoint. */ |
996de3c7 | 2268 | memset(&uevent, 0, sizeof(uevent)); |
f20baf8e DG |
2269 | uevent.type = LTTNG_EVENT_TRACEPOINT; |
2270 | uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; | |
51755dc8 JG |
2271 | default_event_name = event_get_default_agent_ust_name( |
2272 | domain->type); | |
da6c3a50 | 2273 | if (!default_event_name) { |
e5b3c48c | 2274 | ret = LTTNG_ERR_FATAL; |
da6c3a50 | 2275 | goto error; |
f43f95a9 | 2276 | } |
da6c3a50 | 2277 | strncpy(uevent.name, default_event_name, sizeof(uevent.name)); |
f20baf8e DG |
2278 | uevent.name[sizeof(uevent.name) - 1] = '\0'; |
2279 | ||
2280 | /* | |
2281 | * The domain type is changed because we are about to enable the | |
2282 | * default channel and event for the JUL domain that are hardcoded. | |
2283 | * This happens in the UST domain. | |
2284 | */ | |
2285 | memcpy(&tmp_dom, domain, sizeof(tmp_dom)); | |
2286 | tmp_dom.type = LTTNG_DOMAIN_UST; | |
2287 | ||
0e115563 DG |
2288 | switch (domain->type) { |
2289 | case LTTNG_DOMAIN_LOG4J: | |
da6c3a50 | 2290 | default_chan_name = DEFAULT_LOG4J_CHANNEL_NAME; |
0e115563 DG |
2291 | break; |
2292 | case LTTNG_DOMAIN_JUL: | |
da6c3a50 | 2293 | default_chan_name = DEFAULT_JUL_CHANNEL_NAME; |
0e115563 DG |
2294 | break; |
2295 | case LTTNG_DOMAIN_PYTHON: | |
2296 | default_chan_name = DEFAULT_PYTHON_CHANNEL_NAME; | |
2297 | break; | |
2298 | default: | |
e98a44b0 | 2299 | /* The switch/case we are in makes this impossible */ |
0e115563 | 2300 | assert(0); |
da6c3a50 DG |
2301 | } |
2302 | ||
971da06a | 2303 | { |
8404118c | 2304 | char *filter_expression_copy = NULL; |
51755dc8 | 2305 | struct lttng_filter_bytecode *filter_copy = NULL; |
971da06a JG |
2306 | |
2307 | if (filter) { | |
51755dc8 JG |
2308 | const size_t filter_size = sizeof( |
2309 | struct lttng_filter_bytecode) | |
2310 | + filter->len; | |
2311 | ||
2312 | filter_copy = zmalloc(filter_size); | |
971da06a | 2313 | if (!filter_copy) { |
018096a4 | 2314 | ret = LTTNG_ERR_NOMEM; |
b742e3e2 | 2315 | goto error; |
971da06a | 2316 | } |
51755dc8 | 2317 | memcpy(filter_copy, filter, filter_size); |
971da06a | 2318 | |
8404118c JG |
2319 | filter_expression_copy = |
2320 | strdup(filter_expression); | |
2321 | if (!filter_expression) { | |
2322 | ret = LTTNG_ERR_NOMEM; | |
51755dc8 JG |
2323 | } |
2324 | ||
2325 | if (!filter_expression_copy || !filter_copy) { | |
2326 | free(filter_expression_copy); | |
2327 | free(filter_copy); | |
2328 | goto error; | |
8404118c | 2329 | } |
971da06a JG |
2330 | } |
2331 | ||
88f06f15 | 2332 | ret = cmd_enable_event_internal(session, &tmp_dom, |
971da06a | 2333 | (char *) default_chan_name, |
8404118c JG |
2334 | &uevent, filter_expression_copy, |
2335 | filter_copy, NULL, wpipe); | |
971da06a JG |
2336 | } |
2337 | ||
94382e15 JG |
2338 | if (ret == LTTNG_ERR_UST_EVENT_ENABLED) { |
2339 | goto already_enabled; | |
2340 | } else if (ret != LTTNG_OK) { | |
f20baf8e DG |
2341 | goto error; |
2342 | } | |
2343 | ||
2344 | /* The wild card * means that everything should be enabled. */ | |
2345 | if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) { | |
8404118c JG |
2346 | ret = event_agent_enable_all(usess, agt, event, filter, |
2347 | filter_expression); | |
f20baf8e | 2348 | } else { |
8404118c JG |
2349 | ret = event_agent_enable(usess, agt, event, filter, |
2350 | filter_expression); | |
f20baf8e | 2351 | } |
51755dc8 JG |
2352 | filter = NULL; |
2353 | filter_expression = NULL; | |
f20baf8e DG |
2354 | if (ret != LTTNG_OK) { |
2355 | goto error; | |
2356 | } | |
2357 | ||
2358 | break; | |
2359 | } | |
2f77fc4b | 2360 | default: |
f73fabfd | 2361 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
2362 | goto error; |
2363 | } | |
2364 | ||
f73fabfd | 2365 | ret = LTTNG_OK; |
2f77fc4b | 2366 | |
94382e15 | 2367 | already_enabled: |
2f77fc4b | 2368 | error: |
49d21f93 MD |
2369 | free(filter_expression); |
2370 | free(filter); | |
2371 | free(exclusion); | |
cf0bcb51 | 2372 | channel_attr_destroy(attr); |
2223c96f | 2373 | rcu_read_unlock(); |
2f77fc4b DG |
2374 | return ret; |
2375 | } | |
2376 | ||
88f06f15 JG |
2377 | /* |
2378 | * Command LTTNG_ENABLE_EVENT processed by the client thread. | |
2379 | * We own filter, exclusion, and filter_expression. | |
2380 | */ | |
2381 | int cmd_enable_event(struct ltt_session *session, struct lttng_domain *domain, | |
2382 | char *channel_name, struct lttng_event *event, | |
2383 | char *filter_expression, | |
2384 | struct lttng_filter_bytecode *filter, | |
2385 | struct lttng_event_exclusion *exclusion, | |
2386 | int wpipe) | |
2387 | { | |
2388 | return _cmd_enable_event(session, domain, channel_name, event, | |
2389 | filter_expression, filter, exclusion, wpipe, false); | |
2390 | } | |
2391 | ||
2392 | /* | |
2393 | * Enable an event which is internal to LTTng. An internal should | |
2394 | * never be made visible to clients and are immune to checks such as | |
2395 | * reserved names. | |
2396 | */ | |
2397 | static int cmd_enable_event_internal(struct ltt_session *session, | |
2398 | struct lttng_domain *domain, | |
2399 | char *channel_name, struct lttng_event *event, | |
2400 | char *filter_expression, | |
2401 | struct lttng_filter_bytecode *filter, | |
2402 | struct lttng_event_exclusion *exclusion, | |
2403 | int wpipe) | |
2404 | { | |
2405 | return _cmd_enable_event(session, domain, channel_name, event, | |
2406 | filter_expression, filter, exclusion, wpipe, true); | |
2407 | } | |
2408 | ||
2f77fc4b DG |
2409 | /* |
2410 | * Command LTTNG_LIST_TRACEPOINTS processed by the client thread. | |
2411 | */ | |
56a37563 JG |
2412 | ssize_t cmd_list_tracepoints(enum lttng_domain_type domain, |
2413 | struct lttng_event **events) | |
2f77fc4b DG |
2414 | { |
2415 | int ret; | |
2416 | ssize_t nb_events = 0; | |
2417 | ||
2418 | switch (domain) { | |
2419 | case LTTNG_DOMAIN_KERNEL: | |
2420 | nb_events = kernel_list_events(kernel_tracer_fd, events); | |
2421 | if (nb_events < 0) { | |
f73fabfd | 2422 | ret = LTTNG_ERR_KERN_LIST_FAIL; |
2f77fc4b DG |
2423 | goto error; |
2424 | } | |
2425 | break; | |
2426 | case LTTNG_DOMAIN_UST: | |
2427 | nb_events = ust_app_list_events(events); | |
2428 | if (nb_events < 0) { | |
f73fabfd | 2429 | ret = LTTNG_ERR_UST_LIST_FAIL; |
2f77fc4b DG |
2430 | goto error; |
2431 | } | |
2432 | break; | |
5cdb6027 | 2433 | case LTTNG_DOMAIN_LOG4J: |
3c6a091f | 2434 | case LTTNG_DOMAIN_JUL: |
0e115563 | 2435 | case LTTNG_DOMAIN_PYTHON: |
f60140a1 | 2436 | nb_events = agent_list_events(events, domain); |
3c6a091f DG |
2437 | if (nb_events < 0) { |
2438 | ret = LTTNG_ERR_UST_LIST_FAIL; | |
2439 | goto error; | |
2440 | } | |
2441 | break; | |
2f77fc4b | 2442 | default: |
f73fabfd | 2443 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
2444 | goto error; |
2445 | } | |
2446 | ||
2447 | return nb_events; | |
2448 | ||
2449 | error: | |
2450 | /* Return negative value to differentiate return code */ | |
2451 | return -ret; | |
2452 | } | |
2453 | ||
2454 | /* | |
2455 | * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread. | |
2456 | */ | |
56a37563 | 2457 | ssize_t cmd_list_tracepoint_fields(enum lttng_domain_type domain, |
2f77fc4b DG |
2458 | struct lttng_event_field **fields) |
2459 | { | |
2460 | int ret; | |
2461 | ssize_t nb_fields = 0; | |
2462 | ||
2463 | switch (domain) { | |
2464 | case LTTNG_DOMAIN_UST: | |
2465 | nb_fields = ust_app_list_event_fields(fields); | |
2466 | if (nb_fields < 0) { | |
f73fabfd | 2467 | ret = LTTNG_ERR_UST_LIST_FAIL; |
2f77fc4b DG |
2468 | goto error; |
2469 | } | |
2470 | break; | |
2471 | case LTTNG_DOMAIN_KERNEL: | |
2472 | default: /* fall-through */ | |
f73fabfd | 2473 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
2474 | goto error; |
2475 | } | |
2476 | ||
2477 | return nb_fields; | |
2478 | ||
2479 | error: | |
2480 | /* Return negative value to differentiate return code */ | |
2481 | return -ret; | |
2482 | } | |
2483 | ||
834978fd DG |
2484 | ssize_t cmd_list_syscalls(struct lttng_event **events) |
2485 | { | |
2486 | return syscall_table_list(events); | |
2487 | } | |
2488 | ||
a5dfbb9d MD |
2489 | /* |
2490 | * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread. | |
2491 | * | |
2492 | * Called with session lock held. | |
2493 | */ | |
2494 | ssize_t cmd_list_tracker_pids(struct ltt_session *session, | |
56a37563 | 2495 | enum lttng_domain_type domain, int32_t **pids) |
a5dfbb9d MD |
2496 | { |
2497 | int ret; | |
2498 | ssize_t nr_pids = 0; | |
2499 | ||
2500 | switch (domain) { | |
2501 | case LTTNG_DOMAIN_KERNEL: | |
2502 | { | |
2503 | struct ltt_kernel_session *ksess; | |
2504 | ||
2505 | ksess = session->kernel_session; | |
2506 | nr_pids = kernel_list_tracker_pids(ksess, pids); | |
2507 | if (nr_pids < 0) { | |
2508 | ret = LTTNG_ERR_KERN_LIST_FAIL; | |
2509 | goto error; | |
2510 | } | |
2511 | break; | |
2512 | } | |
2513 | case LTTNG_DOMAIN_UST: | |
2514 | { | |
2515 | struct ltt_ust_session *usess; | |
2516 | ||
2517 | usess = session->ust_session; | |
2518 | nr_pids = trace_ust_list_tracker_pids(usess, pids); | |
2519 | if (nr_pids < 0) { | |
2520 | ret = LTTNG_ERR_UST_LIST_FAIL; | |
2521 | goto error; | |
2522 | } | |
2523 | break; | |
2524 | } | |
2525 | case LTTNG_DOMAIN_LOG4J: | |
2526 | case LTTNG_DOMAIN_JUL: | |
2527 | case LTTNG_DOMAIN_PYTHON: | |
2528 | default: | |
2529 | ret = LTTNG_ERR_UND; | |
2530 | goto error; | |
2531 | } | |
2532 | ||
2533 | return nr_pids; | |
2534 | ||
2535 | error: | |
2536 | /* Return negative value to differentiate return code */ | |
2537 | return -ret; | |
2538 | } | |
2539 | ||
2f77fc4b DG |
2540 | /* |
2541 | * Command LTTNG_START_TRACE processed by the client thread. | |
a9ad0c8f MD |
2542 | * |
2543 | * Called with session mutex held. | |
2f77fc4b DG |
2544 | */ |
2545 | int cmd_start_trace(struct ltt_session *session) | |
2546 | { | |
698fb2ed | 2547 | enum lttng_error_code ret; |
cde3e505 | 2548 | unsigned long nb_chan = 0; |
2f77fc4b DG |
2549 | struct ltt_kernel_session *ksession; |
2550 | struct ltt_ust_session *usess; | |
2f77fc4b DG |
2551 | |
2552 | assert(session); | |
2553 | ||
2554 | /* Ease our life a bit ;) */ | |
2555 | ksession = session->kernel_session; | |
2556 | usess = session->ust_session; | |
2557 | ||
8382cf6f DG |
2558 | /* Is the session already started? */ |
2559 | if (session->active) { | |
f73fabfd | 2560 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; |
2f77fc4b DG |
2561 | goto error; |
2562 | } | |
2563 | ||
cde3e505 DG |
2564 | /* |
2565 | * Starting a session without channel is useless since after that it's not | |
2566 | * possible to enable channel thus inform the client. | |
2567 | */ | |
2568 | if (usess && usess->domain_global.channels) { | |
2569 | nb_chan += lttng_ht_get_count(usess->domain_global.channels); | |
2570 | } | |
2571 | if (ksession) { | |
2572 | nb_chan += ksession->channel_count; | |
2573 | } | |
2574 | if (!nb_chan) { | |
2575 | ret = LTTNG_ERR_NO_CHANNEL; | |
2576 | goto error; | |
2577 | } | |
2578 | ||
e5148e25 JG |
2579 | if (session->output_traces && !session->current_trace_chunk) { |
2580 | struct lttng_trace_chunk *trace_chunk; | |
2581 | ||
2582 | trace_chunk = session_create_new_trace_chunk( | |
2583 | session, NULL, NULL); | |
2584 | if (!trace_chunk) { | |
2585 | ret = LTTNG_ERR_CREATE_DIR_FAIL; | |
2586 | goto error; | |
2587 | } | |
2588 | assert(!session->current_trace_chunk); | |
2589 | ret = session_set_trace_chunk(session, trace_chunk, NULL); | |
2590 | lttng_trace_chunk_put(trace_chunk); | |
2591 | if (ret) { | |
2592 | ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER; | |
5c408ad8 JD |
2593 | goto error; |
2594 | } | |
c996624c JD |
2595 | } |
2596 | ||
2f77fc4b DG |
2597 | /* Kernel tracing */ |
2598 | if (ksession != NULL) { | |
c996624c | 2599 | DBG("Start kernel tracing session %s", session->name); |
9b6c7ec5 DG |
2600 | ret = start_kernel_session(ksession, kernel_tracer_fd); |
2601 | if (ret != LTTNG_OK) { | |
2f77fc4b DG |
2602 | goto error; |
2603 | } | |
2f77fc4b DG |
2604 | } |
2605 | ||
2606 | /* Flag session that trace should start automatically */ | |
2607 | if (usess) { | |
698fb2ed JG |
2608 | int int_ret = ust_app_start_trace_all(usess); |
2609 | ||
2610 | if (int_ret < 0) { | |
f73fabfd | 2611 | ret = LTTNG_ERR_UST_START_FAIL; |
2f77fc4b DG |
2612 | goto error; |
2613 | } | |
2614 | } | |
2615 | ||
8382cf6f DG |
2616 | /* Flag this after a successful start. */ |
2617 | session->has_been_started = 1; | |
2618 | session->active = 1; | |
9b6c7ec5 | 2619 | |
5c408ad8 JD |
2620 | /* |
2621 | * Clear the flag that indicates that a rotation was done while the | |
2622 | * session was stopped. | |
2623 | */ | |
2624 | session->rotated_after_last_stop = false; | |
2625 | ||
259c2674 | 2626 | if (session->rotate_timer_period) { |
698fb2ed JG |
2627 | int int_ret = timer_session_rotation_schedule_timer_start( |
2628 | session, session->rotate_timer_period); | |
2629 | ||
2630 | if (int_ret < 0) { | |
259c2674 JD |
2631 | ERR("Failed to enable rotate timer"); |
2632 | ret = LTTNG_ERR_UNK; | |
2633 | goto error; | |
2634 | } | |
2635 | } | |
2636 | ||
f73fabfd | 2637 | ret = LTTNG_OK; |
2f77fc4b DG |
2638 | |
2639 | error: | |
2640 | return ret; | |
2641 | } | |
2642 | ||
2643 | /* | |
2644 | * Command LTTNG_STOP_TRACE processed by the client thread. | |
2645 | */ | |
2646 | int cmd_stop_trace(struct ltt_session *session) | |
2647 | { | |
2648 | int ret; | |
2649 | struct ltt_kernel_channel *kchan; | |
2650 | struct ltt_kernel_session *ksession; | |
2651 | struct ltt_ust_session *usess; | |
e6a80fab | 2652 | bool error_occurred = false; |
2f77fc4b DG |
2653 | |
2654 | assert(session); | |
2655 | ||
5c408ad8 | 2656 | DBG("Begin stop session %s (id %" PRIu64 ")", session->name, session->id); |
2f77fc4b DG |
2657 | /* Short cut */ |
2658 | ksession = session->kernel_session; | |
2659 | usess = session->ust_session; | |
2660 | ||
8382cf6f DG |
2661 | /* Session is not active. Skip everythong and inform the client. */ |
2662 | if (!session->active) { | |
f73fabfd | 2663 | ret = LTTNG_ERR_TRACE_ALREADY_STOPPED; |
2f77fc4b DG |
2664 | goto error; |
2665 | } | |
2666 | ||
2f77fc4b | 2667 | /* Kernel tracer */ |
14fb1ebe | 2668 | if (ksession && ksession->active) { |
2f77fc4b DG |
2669 | DBG("Stop kernel tracing"); |
2670 | ||
566d8970 MD |
2671 | ret = kernel_stop_session(ksession); |
2672 | if (ret < 0) { | |
2673 | ret = LTTNG_ERR_KERN_STOP_FAIL; | |
2674 | goto error; | |
2675 | } | |
2676 | ||
2677 | kernel_wait_quiescent(kernel_tracer_fd); | |
2678 | ||
2679 | /* Flush metadata after stopping (if exists) */ | |
2f77fc4b DG |
2680 | if (ksession->metadata_stream_fd >= 0) { |
2681 | ret = kernel_metadata_flush_buffer(ksession->metadata_stream_fd); | |
2682 | if (ret < 0) { | |
2683 | ERR("Kernel metadata flush failed"); | |
2684 | } | |
2685 | } | |
2686 | ||
566d8970 | 2687 | /* Flush all buffers after stopping */ |
2f77fc4b DG |
2688 | cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) { |
2689 | ret = kernel_flush_buffer(kchan); | |
2690 | if (ret < 0) { | |
2691 | ERR("Kernel flush buffer error"); | |
2692 | } | |
2693 | } | |
2694 | ||
14fb1ebe | 2695 | ksession->active = 0; |
5c408ad8 JD |
2696 | DBG("Kernel session stopped %s (id %" PRIu64 ")", session->name, |
2697 | session->id); | |
2f77fc4b DG |
2698 | } |
2699 | ||
14fb1ebe | 2700 | if (usess && usess->active) { |
2f77fc4b DG |
2701 | ret = ust_app_stop_trace_all(usess); |
2702 | if (ret < 0) { | |
f73fabfd | 2703 | ret = LTTNG_ERR_UST_STOP_FAIL; |
2f77fc4b DG |
2704 | goto error; |
2705 | } | |
2706 | } | |
2707 | ||
8382cf6f DG |
2708 | /* Flag inactive after a successful stop. */ |
2709 | session->active = 0; | |
e6a80fab | 2710 | ret = !error_occurred ? LTTNG_OK : LTTNG_ERR_UNK; |
2f77fc4b DG |
2711 | |
2712 | error: | |
2713 | return ret; | |
2714 | } | |
2715 | ||
2716 | /* | |
2717 | * Command LTTNG_SET_CONSUMER_URI processed by the client thread. | |
2718 | */ | |
bda32d56 JG |
2719 | int cmd_set_consumer_uri(struct ltt_session *session, size_t nb_uri, |
2720 | struct lttng_uri *uris) | |
2f77fc4b DG |
2721 | { |
2722 | int ret, i; | |
2723 | struct ltt_kernel_session *ksess = session->kernel_session; | |
2724 | struct ltt_ust_session *usess = session->ust_session; | |
2f77fc4b DG |
2725 | |
2726 | assert(session); | |
2727 | assert(uris); | |
2728 | assert(nb_uri > 0); | |
2729 | ||
8382cf6f DG |
2730 | /* Can't set consumer URI if the session is active. */ |
2731 | if (session->active) { | |
f73fabfd | 2732 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; |
2f77fc4b DG |
2733 | goto error; |
2734 | } | |
2735 | ||
bda32d56 | 2736 | /* Set the "global" consumer URIs */ |
2f77fc4b | 2737 | for (i = 0; i < nb_uri; i++) { |
5feee503 JG |
2738 | ret = add_uri_to_consumer(session, |
2739 | session->consumer, | |
2740 | &uris[i], LTTNG_DOMAIN_NONE); | |
a74934ba | 2741 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
2742 | goto error; |
2743 | } | |
2f77fc4b DG |
2744 | } |
2745 | ||
bda32d56 JG |
2746 | /* Set UST session URIs */ |
2747 | if (session->ust_session) { | |
2748 | for (i = 0; i < nb_uri; i++) { | |
5feee503 | 2749 | ret = add_uri_to_consumer(session, |
bda32d56 | 2750 | session->ust_session->consumer, |
5feee503 | 2751 | &uris[i], LTTNG_DOMAIN_UST); |
bda32d56 JG |
2752 | if (ret != LTTNG_OK) { |
2753 | goto error; | |
2754 | } | |
2755 | } | |
2756 | } | |
2757 | ||
2758 | /* Set kernel session URIs */ | |
2759 | if (session->kernel_session) { | |
2760 | for (i = 0; i < nb_uri; i++) { | |
5feee503 | 2761 | ret = add_uri_to_consumer(session, |
bda32d56 | 2762 | session->kernel_session->consumer, |
5feee503 | 2763 | &uris[i], LTTNG_DOMAIN_KERNEL); |
bda32d56 JG |
2764 | if (ret != LTTNG_OK) { |
2765 | goto error; | |
2766 | } | |
2767 | } | |
2768 | } | |
2769 | ||
7ab70fe0 DG |
2770 | /* |
2771 | * Make sure to set the session in output mode after we set URI since a | |
2772 | * session can be created without URL (thus flagged in no output mode). | |
2773 | */ | |
2774 | session->output_traces = 1; | |
2775 | if (ksess) { | |
2776 | ksess->output_traces = 1; | |
bda32d56 JG |
2777 | } |
2778 | ||
2779 | if (usess) { | |
7ab70fe0 DG |
2780 | usess->output_traces = 1; |
2781 | } | |
2782 | ||
2f77fc4b | 2783 | /* All good! */ |
f73fabfd | 2784 | ret = LTTNG_OK; |
2f77fc4b DG |
2785 | |
2786 | error: | |
2787 | return ret; | |
2788 | } | |
2789 | ||
5feee503 JG |
2790 | static |
2791 | enum lttng_error_code set_session_output_from_descriptor( | |
2792 | struct ltt_session *session, | |
2793 | const struct lttng_session_descriptor *descriptor) | |
2f77fc4b DG |
2794 | { |
2795 | int ret; | |
5feee503 JG |
2796 | enum lttng_error_code ret_code = LTTNG_OK; |
2797 | enum lttng_session_descriptor_type session_type = | |
2798 | lttng_session_descriptor_get_type(descriptor); | |
2799 | enum lttng_session_descriptor_output_type output_type = | |
2800 | lttng_session_descriptor_get_output_type(descriptor); | |
2801 | struct lttng_uri uris[2] = {}; | |
2802 | size_t uri_count = 0; | |
2803 | ||
2804 | switch (output_type) { | |
2805 | case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE: | |
2806 | goto end; | |
2807 | case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL: | |
2808 | lttng_session_descriptor_get_local_output_uri(descriptor, | |
2809 | &uris[0]); | |
2810 | uri_count = 1; | |
2811 | break; | |
2812 | case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK: | |
2813 | lttng_session_descriptor_get_network_output_uris(descriptor, | |
2814 | &uris[0], &uris[1]); | |
2815 | uri_count = 2; | |
2816 | break; | |
2817 | default: | |
2818 | ret_code = LTTNG_ERR_INVALID; | |
48a86f68 | 2819 | goto end; |
2f77fc4b DG |
2820 | } |
2821 | ||
5feee503 JG |
2822 | switch (session_type) { |
2823 | case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT: | |
2824 | { | |
2825 | struct snapshot_output *new_output = NULL; | |
2826 | ||
2827 | new_output = snapshot_output_alloc(); | |
2828 | if (!new_output) { | |
2829 | ret_code = LTTNG_ERR_NOMEM; | |
2830 | goto end; | |
2831 | } | |
2832 | ||
2833 | ret = snapshot_output_init_with_uri(session, | |
2834 | DEFAULT_SNAPSHOT_MAX_SIZE, | |
2835 | NULL, uris, uri_count, session->consumer, | |
2836 | new_output, &session->snapshot); | |
2837 | if (ret < 0) { | |
2838 | ret_code = (ret == -ENOMEM) ? | |
2839 | LTTNG_ERR_NOMEM : LTTNG_ERR_INVALID; | |
2840 | snapshot_output_destroy(new_output); | |
2841 | goto end; | |
2842 | } | |
2843 | snapshot_add_output(&session->snapshot, new_output); | |
2844 | break; | |
2845 | } | |
2846 | case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR: | |
2847 | case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE: | |
2848 | { | |
2849 | ret_code = cmd_set_consumer_uri(session, uri_count, uris); | |
2850 | break; | |
2851 | } | |
2852 | default: | |
2853 | ret_code = LTTNG_ERR_INVALID; | |
48a86f68 | 2854 | goto end; |
2f77fc4b | 2855 | } |
5feee503 JG |
2856 | end: |
2857 | return ret_code; | |
2858 | } | |
2859 | ||
2860 | static | |
2861 | enum lttng_error_code cmd_create_session_from_descriptor( | |
2862 | struct lttng_session_descriptor *descriptor, | |
2863 | const lttng_sock_cred *creds, | |
2864 | const char *home_path) | |
2865 | { | |
2866 | int ret; | |
2867 | enum lttng_error_code ret_code; | |
2868 | const char *session_name; | |
2869 | struct ltt_session *new_session = NULL; | |
2870 | enum lttng_session_descriptor_status descriptor_status; | |
2f77fc4b | 2871 | |
48a86f68 | 2872 | session_lock_list(); |
5feee503 JG |
2873 | if (home_path) { |
2874 | if (*home_path != '/') { | |
2875 | ERR("Home path provided by client is not absolute"); | |
2876 | ret_code = LTTNG_ERR_INVALID; | |
2877 | goto end; | |
2878 | } | |
2879 | } | |
2f77fc4b | 2880 | |
5feee503 JG |
2881 | descriptor_status = lttng_session_descriptor_get_session_name( |
2882 | descriptor, &session_name); | |
2883 | switch (descriptor_status) { | |
2884 | case LTTNG_SESSION_DESCRIPTOR_STATUS_OK: | |
2885 | break; | |
2886 | case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET: | |
2887 | session_name = NULL; | |
2888 | break; | |
2889 | default: | |
2890 | ret_code = LTTNG_ERR_INVALID; | |
2891 | goto end; | |
2892 | } | |
2893 | ret_code = session_create(session_name, creds->uid, creds->gid, | |
2894 | &new_session); | |
2895 | if (ret_code != LTTNG_OK) { | |
48a86f68 | 2896 | goto end; |
2f77fc4b DG |
2897 | } |
2898 | ||
5feee503 JG |
2899 | if (!session_name) { |
2900 | ret = lttng_session_descriptor_set_session_name(descriptor, | |
2901 | new_session->name); | |
2902 | if (ret) { | |
2903 | ret_code = LTTNG_ERR_SESSION_FAIL; | |
2904 | goto end; | |
2905 | } | |
2906 | } | |
2907 | ||
2908 | if (!lttng_session_descriptor_is_output_destination_initialized( | |
2909 | descriptor)) { | |
2910 | /* | |
2911 | * Only include the session's creation time in the output | |
2912 | * destination if the name of the session itself was | |
2913 | * not auto-generated. | |
2914 | */ | |
2915 | ret_code = lttng_session_descriptor_set_default_output( | |
2916 | descriptor, | |
2917 | session_name ? &new_session->creation_time : NULL, | |
2918 | home_path); | |
2919 | if (ret_code != LTTNG_OK) { | |
48a86f68 | 2920 | goto end; |
2bba9e53 | 2921 | } |
2bba9e53 | 2922 | } else { |
5feee503 JG |
2923 | new_session->has_user_specified_directory = |
2924 | lttng_session_descriptor_has_output_directory( | |
2925 | descriptor); | |
2f77fc4b DG |
2926 | } |
2927 | ||
5feee503 JG |
2928 | switch (lttng_session_descriptor_get_type(descriptor)) { |
2929 | case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT: | |
2930 | new_session->snapshot_mode = 1; | |
2931 | break; | |
2932 | case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE: | |
2933 | new_session->live_timer = | |
2934 | lttng_session_descriptor_live_get_timer_interval( | |
2935 | descriptor); | |
2936 | break; | |
2937 | default: | |
2938 | break; | |
2939 | } | |
2f77fc4b | 2940 | |
5feee503 JG |
2941 | ret_code = set_session_output_from_descriptor(new_session, descriptor); |
2942 | if (ret_code != LTTNG_OK) { | |
2943 | goto end; | |
2944 | } | |
2945 | new_session->consumer->enabled = 1; | |
2946 | ret_code = LTTNG_OK; | |
48a86f68 | 2947 | end: |
5feee503 JG |
2948 | /* Release reference provided by the session_create function. */ |
2949 | session_put(new_session); | |
2950 | if (ret_code != LTTNG_OK && new_session) { | |
2951 | /* Release the global reference on error. */ | |
2952 | session_destroy(new_session); | |
48a86f68 | 2953 | } |
5feee503 JG |
2954 | session_unlock_list(); |
2955 | return ret_code; | |
2f77fc4b DG |
2956 | } |
2957 | ||
5feee503 JG |
2958 | enum lttng_error_code cmd_create_session(struct command_ctx *cmd_ctx, int sock, |
2959 | struct lttng_session_descriptor **return_descriptor) | |
27babd3a DG |
2960 | { |
2961 | int ret; | |
5feee503 JG |
2962 | size_t payload_size; |
2963 | struct lttng_dynamic_buffer payload; | |
2964 | struct lttng_buffer_view home_dir_view; | |
2965 | struct lttng_buffer_view session_descriptor_view; | |
2966 | struct lttng_session_descriptor *session_descriptor = NULL; | |
2967 | enum lttng_error_code ret_code; | |
2968 | ||
2969 | lttng_dynamic_buffer_init(&payload); | |
2970 | if (cmd_ctx->lsm->u.create_session.home_dir_size >= | |
2971 | LTTNG_PATH_MAX) { | |
2972 | ret_code = LTTNG_ERR_INVALID; | |
2973 | goto error; | |
27babd3a | 2974 | } |
5feee503 JG |
2975 | if (cmd_ctx->lsm->u.create_session.session_descriptor_size > |
2976 | LTTNG_SESSION_DESCRIPTOR_MAX_LEN) { | |
2977 | ret_code = LTTNG_ERR_INVALID; | |
2978 | goto error; | |
27babd3a DG |
2979 | } |
2980 | ||
5feee503 JG |
2981 | payload_size = cmd_ctx->lsm->u.create_session.home_dir_size + |
2982 | cmd_ctx->lsm->u.create_session.session_descriptor_size; | |
2983 | ret = lttng_dynamic_buffer_set_size(&payload, payload_size); | |
2984 | if (ret) { | |
2985 | ret_code = LTTNG_ERR_NOMEM; | |
2986 | goto error; | |
27babd3a DG |
2987 | } |
2988 | ||
5feee503 JG |
2989 | ret = lttcomm_recv_unix_sock(sock, payload.data, payload.size); |
2990 | if (ret <= 0) { | |
2991 | ERR("Reception of session descriptor failed, aborting."); | |
2992 | ret_code = LTTNG_ERR_SESSION_FAIL; | |
2993 | goto error; | |
27babd3a DG |
2994 | } |
2995 | ||
5feee503 JG |
2996 | home_dir_view = lttng_buffer_view_from_dynamic_buffer( |
2997 | &payload, | |
2998 | 0, | |
2999 | cmd_ctx->lsm->u.create_session.home_dir_size); | |
3000 | session_descriptor_view = lttng_buffer_view_from_dynamic_buffer( | |
3001 | &payload, | |
3002 | cmd_ctx->lsm->u.create_session.home_dir_size, | |
3003 | cmd_ctx->lsm->u.create_session.session_descriptor_size); | |
27babd3a | 3004 | |
5feee503 JG |
3005 | ret = lttng_session_descriptor_create_from_buffer( |
3006 | &session_descriptor_view, &session_descriptor); | |
3007 | if (ret < 0) { | |
3008 | ERR("Failed to create session descriptor from payload of \"create session\" command"); | |
3009 | ret_code = LTTNG_ERR_INVALID; | |
3010 | goto error; | |
3011 | } | |
27babd3a | 3012 | |
5feee503 JG |
3013 | /* |
3014 | * Sets the descriptor's auto-generated properties (name, output) if | |
3015 | * needed. | |
3016 | */ | |
3017 | ret_code = cmd_create_session_from_descriptor(session_descriptor, | |
3018 | &cmd_ctx->creds, | |
3019 | home_dir_view.size ? home_dir_view.data : NULL); | |
3020 | if (ret_code != LTTNG_OK) { | |
3021 | goto error; | |
48a86f68 | 3022 | } |
5feee503 JG |
3023 | |
3024 | ret_code = LTTNG_OK; | |
3025 | *return_descriptor = session_descriptor; | |
3026 | session_descriptor = NULL; | |
3027 | error: | |
3028 | lttng_dynamic_buffer_reset(&payload); | |
3029 | lttng_session_descriptor_destroy(session_descriptor); | |
3030 | return ret_code; | |
27babd3a DG |
3031 | } |
3032 | ||
823a1989 JG |
3033 | static |
3034 | void cmd_destroy_session_reply(const struct ltt_session *session, | |
3035 | void *_reply_context) | |
3036 | { | |
3037 | int ret; | |
3038 | ssize_t comm_ret; | |
3039 | const struct cmd_destroy_session_reply_context *reply_context = | |
3040 | _reply_context; | |
3041 | struct lttng_dynamic_buffer payload; | |
3042 | struct lttcomm_session_destroy_command_header cmd_header; | |
3043 | struct lttng_trace_archive_location *location = NULL; | |
3044 | struct lttcomm_lttng_msg llm = { | |
3045 | .cmd_type = LTTNG_DESTROY_SESSION, | |
3046 | .ret_code = LTTNG_OK, | |
3047 | .pid = UINT32_MAX, | |
3048 | .cmd_header_size = | |
3049 | sizeof(struct lttcomm_session_destroy_command_header), | |
3050 | .data_size = 0, | |
3051 | }; | |
3052 | size_t payload_size_before_location; | |
3053 | ||
3054 | lttng_dynamic_buffer_init(&payload); | |
3055 | ||
3056 | ret = lttng_dynamic_buffer_append(&payload, &llm, sizeof(llm)); | |
3057 | if (ret) { | |
3058 | ERR("Failed to append session destruction message"); | |
3059 | goto error; | |
3060 | } | |
3061 | ||
3062 | cmd_header.rotation_state = | |
3063 | (int32_t) (reply_context->implicit_rotation_on_destroy ? | |
3064 | session->rotation_state : | |
3065 | LTTNG_ROTATION_STATE_NO_ROTATION); | |
3066 | ret = lttng_dynamic_buffer_append(&payload, &cmd_header, | |
3067 | sizeof(cmd_header)); | |
3068 | if (ret) { | |
3069 | ERR("Failed to append session destruction command header"); | |
3070 | goto error; | |
3071 | } | |
3072 | ||
3073 | if (!reply_context->implicit_rotation_on_destroy) { | |
3074 | DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply", | |
3075 | session->name); | |
3076 | goto send_reply; | |
3077 | } | |
3078 | if (session->rotation_state != LTTNG_ROTATION_STATE_COMPLETED) { | |
3079 | DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply", | |
3080 | session->name); | |
3081 | goto send_reply; | |
3082 | } | |
3083 | ||
3084 | location = session_get_trace_archive_location(session); | |
3085 | if (!location) { | |
3086 | ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"", | |
3087 | session->name); | |
3088 | goto error; | |
3089 | } | |
3090 | ||
3091 | payload_size_before_location = payload.size; | |
3092 | comm_ret = lttng_trace_archive_location_serialize(location, | |
3093 | &payload); | |
3094 | if (comm_ret < 0) { | |
3095 | ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"", | |
3096 | session->name); | |
3097 | goto error; | |
3098 | } | |
3099 | /* Update the message to indicate the location's length. */ | |
3100 | ((struct lttcomm_lttng_msg *) payload.data)->data_size = | |
3101 | payload.size - payload_size_before_location; | |
3102 | send_reply: | |
3103 | comm_ret = lttcomm_send_unix_sock(reply_context->reply_sock_fd, | |
3104 | payload.data, payload.size); | |
3105 | if (comm_ret != (ssize_t) payload.size) { | |
3106 | ERR("Failed to send result of the destruction of session \"%s\" to client", | |
3107 | session->name); | |
3108 | } | |
3109 | error: | |
3110 | ret = close(reply_context->reply_sock_fd); | |
3111 | if (ret) { | |
3112 | PERROR("Failed to close client socket in deferred session destroy reply"); | |
3113 | } | |
3114 | lttng_dynamic_buffer_reset(&payload); | |
3115 | free(_reply_context); | |
3116 | } | |
3117 | ||
2f77fc4b DG |
3118 | /* |
3119 | * Command LTTNG_DESTROY_SESSION processed by the client thread. | |
a9ad0c8f MD |
3120 | * |
3121 | * Called with session lock held. | |
2f77fc4b | 3122 | */ |
48a86f68 | 3123 | int cmd_destroy_session(struct ltt_session *session, |
823a1989 JG |
3124 | struct notification_thread_handle *notification_thread_handle, |
3125 | int *sock_fd) | |
2f77fc4b DG |
3126 | { |
3127 | int ret; | |
823a1989 JG |
3128 | struct cmd_destroy_session_reply_context *reply_context = NULL; |
3129 | ||
3130 | if (sock_fd) { | |
3131 | reply_context = zmalloc(sizeof(*reply_context)); | |
3132 | if (!reply_context) { | |
3133 | ret = LTTNG_ERR_NOMEM; | |
3134 | goto end; | |
3135 | } | |
3136 | reply_context->reply_sock_fd = *sock_fd; | |
3137 | } | |
2f77fc4b DG |
3138 | |
3139 | /* Safety net */ | |
3140 | assert(session); | |
3141 | ||
823a1989 JG |
3142 | DBG("Begin destroy session %s (id %" PRIu64 ")", session->name, |
3143 | session->id); | |
3144 | if (session->active) { | |
3145 | DBG("Session \"%s\" is active, attempting to stop it before destroying it", | |
3146 | session->name); | |
3147 | ret = cmd_stop_trace(session); | |
3148 | if (ret != LTTNG_OK && ret != LTTNG_ERR_TRACE_ALREADY_STOPPED) { | |
3149 | /* Carry on with the destruction of the session. */ | |
3150 | ERR("Failed to stop session \"%s\" as part of its destruction: %s", | |
3151 | session->name, lttng_strerror(-ret)); | |
3152 | } | |
3153 | } | |
5c408ad8 | 3154 | |
82528808 JG |
3155 | if (session->rotation_schedule_timer_enabled) { |
3156 | if (timer_session_rotation_schedule_timer_stop( | |
3157 | session)) { | |
3158 | ERR("Failed to stop the \"rotation schedule\" timer of session %s", | |
3159 | session->name); | |
3160 | } | |
259c2674 JD |
3161 | } |
3162 | ||
90936dcf JD |
3163 | if (session->rotate_size) { |
3164 | unsubscribe_session_consumed_size_rotation(session, notification_thread_handle); | |
3165 | session->rotate_size = 0; | |
3166 | } | |
3167 | ||
e5148e25 JG |
3168 | if (session->most_recent_chunk_id.is_set && |
3169 | session->most_recent_chunk_id.value != 0 && | |
3170 | session->current_trace_chunk) { | |
3171 | ret = cmd_rotate_session(session, NULL); | |
3172 | if (ret != LTTNG_OK) { | |
3173 | ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s", | |
3174 | session->name, lttng_strerror(-ret)); | |
f859080e | 3175 | } |
823a1989 JG |
3176 | if (reply_context) { |
3177 | reply_context->implicit_rotation_on_destroy = true; | |
3178 | } | |
3179 | } | |
5c408ad8 | 3180 | |
a503e1ef JG |
3181 | if (session->shm_path[0]) { |
3182 | /* | |
3183 | * When a session is created with an explicit shm_path, | |
3184 | * the consumer daemon will create its shared memory files | |
3185 | * at that location and will *not* unlink them. This is normal | |
3186 | * as the intention of that feature is to make it possible | |
3187 | * to retrieve the content of those files should a crash occur. | |
3188 | * | |
3189 | * To ensure the content of those files can be used, the | |
3190 | * sessiond daemon will replicate the content of the metadata | |
3191 | * cache in a metadata file. | |
3192 | * | |
3193 | * On clean-up, it is expected that the consumer daemon will | |
3194 | * unlink the shared memory files and that the session daemon | |
3195 | * will unlink the metadata file. Then, the session's directory | |
3196 | * in the shm path can be removed. | |
3197 | * | |
3198 | * Unfortunately, a flaw in the design of the sessiond's and | |
3199 | * consumerd's tear down of channels makes it impossible to | |
3200 | * determine when the sessiond _and_ the consumerd have both | |
3201 | * destroyed their representation of a channel. For one, the | |
3202 | * unlinking, close, and rmdir happen in deferred 'call_rcu' | |
3203 | * callbacks in both daemons. | |
3204 | * | |
3205 | * However, it is also impossible for the sessiond to know when | |
3206 | * the consumer daemon is done destroying its channel(s) since | |
3207 | * it occurs as a reaction to the closing of the channel's file | |
3208 | * descriptor. There is no resulting communication initiated | |
3209 | * from the consumerd to the sessiond to confirm that the | |
3210 | * operation is completed (and was successful). | |
3211 | * | |
3212 | * Until this is all fixed, the session daemon checks for the | |
3213 | * removal of the session's shm path which makes it possible | |
3214 | * to safely advertise a session as having been destroyed. | |
3215 | * | |
3216 | * Prior to this fix, it was not possible to reliably save | |
3217 | * a session making use of the --shm-path option, destroy it, | |
3218 | * and load it again. This is because the creation of the | |
3219 | * session would fail upon seeing the session's shm path | |
3220 | * already in existence. | |
3221 | * | |
3222 | * Note that none of the error paths in the check for the | |
3223 | * directory's existence return an error. This is normal | |
3224 | * as there isn't much that can be done. The session will | |
3225 | * be destroyed properly, except that we can't offer the | |
3226 | * guarantee that the same session can be re-created. | |
3227 | */ | |
3228 | current_completion_handler = &destroy_completion_handler.handler; | |
3229 | ret = lttng_strncpy(destroy_completion_handler.shm_path, | |
3230 | session->shm_path, | |
3231 | sizeof(destroy_completion_handler.shm_path)); | |
3232 | assert(!ret); | |
3233 | } | |
48a86f68 JG |
3234 | |
3235 | /* | |
3236 | * The session is destroyed. However, note that the command context | |
3237 | * still holds a reference to the session, thus delaying its destruction | |
3238 | * _at least_ up to the point when that reference is released. | |
3239 | */ | |
3240 | session_destroy(session); | |
823a1989 JG |
3241 | if (reply_context) { |
3242 | ret = session_add_destroy_notifier(session, | |
3243 | cmd_destroy_session_reply, | |
3244 | (void *) reply_context); | |
3245 | if (ret) { | |
3246 | ret = LTTNG_ERR_FATAL; | |
3247 | goto end; | |
3248 | } else { | |
3249 | *sock_fd = -1; | |
3250 | } | |
3251 | } | |
3252 | ret = LTTNG_OK; | |
3253 | end: | |
2f77fc4b DG |
3254 | return ret; |
3255 | } | |
3256 | ||
2f77fc4b DG |
3257 | /* |
3258 | * Command LTTNG_REGISTER_CONSUMER processed by the client thread. | |
3259 | */ | |
56a37563 JG |
3260 | int cmd_register_consumer(struct ltt_session *session, |
3261 | enum lttng_domain_type domain, const char *sock_path, | |
3262 | struct consumer_data *cdata) | |
2f77fc4b DG |
3263 | { |
3264 | int ret, sock; | |
dd81b457 | 3265 | struct consumer_socket *socket = NULL; |
2f77fc4b DG |
3266 | |
3267 | assert(session); | |
3268 | assert(cdata); | |
3269 | assert(sock_path); | |
3270 | ||
3271 | switch (domain) { | |
3272 | case LTTNG_DOMAIN_KERNEL: | |
3273 | { | |
3274 | struct ltt_kernel_session *ksess = session->kernel_session; | |
3275 | ||
3276 | assert(ksess); | |
3277 | ||
3278 | /* Can't register a consumer if there is already one */ | |
3279 | if (ksess->consumer_fds_sent != 0) { | |
f73fabfd | 3280 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
2f77fc4b DG |
3281 | goto error; |
3282 | } | |
3283 | ||
3284 | sock = lttcomm_connect_unix_sock(sock_path); | |
3285 | if (sock < 0) { | |
f73fabfd | 3286 | ret = LTTNG_ERR_CONNECT_FAIL; |
2f77fc4b DG |
3287 | goto error; |
3288 | } | |
4ce514c4 | 3289 | cdata->cmd_sock = sock; |
2f77fc4b | 3290 | |
4ce514c4 | 3291 | socket = consumer_allocate_socket(&cdata->cmd_sock); |
2f77fc4b | 3292 | if (socket == NULL) { |
f66c074c DG |
3293 | ret = close(sock); |
3294 | if (ret < 0) { | |
3295 | PERROR("close register consumer"); | |
3296 | } | |
4ce514c4 | 3297 | cdata->cmd_sock = -1; |
f73fabfd | 3298 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
3299 | goto error; |
3300 | } | |
3301 | ||
3302 | socket->lock = zmalloc(sizeof(pthread_mutex_t)); | |
3303 | if (socket->lock == NULL) { | |
3304 | PERROR("zmalloc pthread mutex"); | |
f73fabfd | 3305 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
3306 | goto error; |
3307 | } | |
3308 | pthread_mutex_init(socket->lock, NULL); | |
3309 | socket->registered = 1; | |
3310 | ||
3311 | rcu_read_lock(); | |
3312 | consumer_add_socket(socket, ksess->consumer); | |
3313 | rcu_read_unlock(); | |
3314 | ||
3315 | pthread_mutex_lock(&cdata->pid_mutex); | |
3316 | cdata->pid = -1; | |
3317 | pthread_mutex_unlock(&cdata->pid_mutex); | |
3318 | ||
3319 | break; | |
3320 | } | |
3321 | default: | |
3322 | /* TODO: Userspace tracing */ | |
f73fabfd | 3323 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
3324 | goto error; |
3325 | } | |
3326 | ||
dd81b457 | 3327 | return LTTNG_OK; |
2f77fc4b DG |
3328 | |
3329 | error: | |
dd81b457 DG |
3330 | if (socket) { |
3331 | consumer_destroy_socket(socket); | |
3332 | } | |
2f77fc4b DG |
3333 | return ret; |
3334 | } | |
3335 | ||
3336 | /* | |
3337 | * Command LTTNG_LIST_DOMAINS processed by the client thread. | |
3338 | */ | |
3339 | ssize_t cmd_list_domains(struct ltt_session *session, | |
3340 | struct lttng_domain **domains) | |
3341 | { | |
3342 | int ret, index = 0; | |
3343 | ssize_t nb_dom = 0; | |
fefd409b DG |
3344 | struct agent *agt; |
3345 | struct lttng_ht_iter iter; | |
2f77fc4b DG |
3346 | |
3347 | if (session->kernel_session != NULL) { | |
3348 | DBG3("Listing domains found kernel domain"); | |
3349 | nb_dom++; | |
3350 | } | |
3351 | ||
3352 | if (session->ust_session != NULL) { | |
3353 | DBG3("Listing domains found UST global domain"); | |
3354 | nb_dom++; | |
3c6a091f | 3355 | |
e0a74f01 | 3356 | rcu_read_lock(); |
fefd409b DG |
3357 | cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter, |
3358 | agt, node.node) { | |
3359 | if (agt->being_used) { | |
3360 | nb_dom++; | |
3361 | } | |
3c6a091f | 3362 | } |
e0a74f01 | 3363 | rcu_read_unlock(); |
2f77fc4b DG |
3364 | } |
3365 | ||
fa64dfb4 JG |
3366 | if (!nb_dom) { |
3367 | goto end; | |
3368 | } | |
3369 | ||
2f77fc4b DG |
3370 | *domains = zmalloc(nb_dom * sizeof(struct lttng_domain)); |
3371 | if (*domains == NULL) { | |
f73fabfd | 3372 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
3373 | goto error; |
3374 | } | |
3375 | ||
3376 | if (session->kernel_session != NULL) { | |
3377 | (*domains)[index].type = LTTNG_DOMAIN_KERNEL; | |
b5edb9e8 PP |
3378 | |
3379 | /* Kernel session buffer type is always GLOBAL */ | |
3380 | (*domains)[index].buf_type = LTTNG_BUFFER_GLOBAL; | |
3381 | ||
2f77fc4b DG |
3382 | index++; |
3383 | } | |
3384 | ||
3385 | if (session->ust_session != NULL) { | |
3386 | (*domains)[index].type = LTTNG_DOMAIN_UST; | |
88c5f0d8 | 3387 | (*domains)[index].buf_type = session->ust_session->buffer_type; |
2f77fc4b | 3388 | index++; |
3c6a091f | 3389 | |
e0a74f01 | 3390 | rcu_read_lock(); |
fefd409b DG |
3391 | cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter, |
3392 | agt, node.node) { | |
3393 | if (agt->being_used) { | |
3394 | (*domains)[index].type = agt->domain; | |
3395 | (*domains)[index].buf_type = session->ust_session->buffer_type; | |
3396 | index++; | |
3397 | } | |
3c6a091f | 3398 | } |
e0a74f01 | 3399 | rcu_read_unlock(); |
2f77fc4b | 3400 | } |
fa64dfb4 | 3401 | end: |
2f77fc4b DG |
3402 | return nb_dom; |
3403 | ||
3404 | error: | |
f73fabfd DG |
3405 | /* Return negative value to differentiate return code */ |
3406 | return -ret; | |
2f77fc4b DG |
3407 | } |
3408 | ||
3409 | ||
3410 | /* | |
3411 | * Command LTTNG_LIST_CHANNELS processed by the client thread. | |
3412 | */ | |
56a37563 JG |
3413 | ssize_t cmd_list_channels(enum lttng_domain_type domain, |
3414 | struct ltt_session *session, struct lttng_channel **channels) | |
2f77fc4b | 3415 | { |
53e367f9 | 3416 | ssize_t nb_chan = 0, payload_size = 0, ret; |
2f77fc4b DG |
3417 | |
3418 | switch (domain) { | |
3419 | case LTTNG_DOMAIN_KERNEL: | |
3420 | if (session->kernel_session != NULL) { | |
3421 | nb_chan = session->kernel_session->channel_count; | |
3422 | } | |
3423 | DBG3("Number of kernel channels %zd", nb_chan); | |
c7d620a2 | 3424 | if (nb_chan <= 0) { |
53e367f9 JG |
3425 | ret = -LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
3426 | goto end; | |
c7d620a2 | 3427 | } |
2f77fc4b DG |
3428 | break; |
3429 | case LTTNG_DOMAIN_UST: | |
3430 | if (session->ust_session != NULL) { | |
7ffc31a2 | 3431 | rcu_read_lock(); |
2f77fc4b | 3432 | nb_chan = lttng_ht_get_count( |
7ffc31a2 JG |
3433 | session->ust_session->domain_global.channels); |
3434 | rcu_read_unlock(); | |
2f77fc4b DG |
3435 | } |
3436 | DBG3("Number of UST global channels %zd", nb_chan); | |
ade7ce52 | 3437 | if (nb_chan < 0) { |
53e367f9 JG |
3438 | ret = -LTTNG_ERR_UST_CHAN_NOT_FOUND; |
3439 | goto end; | |
c7d620a2 | 3440 | } |
2f77fc4b DG |
3441 | break; |
3442 | default: | |
53e367f9 JG |
3443 | ret = -LTTNG_ERR_UND; |
3444 | goto end; | |
2f77fc4b DG |
3445 | } |
3446 | ||
3447 | if (nb_chan > 0) { | |
53e367f9 | 3448 | const size_t channel_size = sizeof(struct lttng_channel) + |
cf0bcb51 JG |
3449 | sizeof(struct lttng_channel_extended); |
3450 | struct lttng_channel_extended *channel_exts; | |
53e367f9 JG |
3451 | |
3452 | payload_size = nb_chan * channel_size; | |
3453 | *channels = zmalloc(payload_size); | |
2f77fc4b | 3454 | if (*channels == NULL) { |
53e367f9 JG |
3455 | ret = -LTTNG_ERR_FATAL; |
3456 | goto end; | |
2f77fc4b DG |
3457 | } |
3458 | ||
53e367f9 JG |
3459 | channel_exts = ((void *) *channels) + |
3460 | (nb_chan * sizeof(struct lttng_channel)); | |
d449df4a MD |
3461 | ret = list_lttng_channels(domain, session, *channels, channel_exts); |
3462 | if (ret != LTTNG_OK) { | |
3463 | free(*channels); | |
3464 | *channels = NULL; | |
3465 | goto end; | |
3466 | } | |
53e367f9 JG |
3467 | } else { |
3468 | *channels = NULL; | |
2f77fc4b DG |
3469 | } |
3470 | ||
53e367f9 JG |
3471 | ret = payload_size; |
3472 | end: | |
3473 | return ret; | |
2f77fc4b DG |
3474 | } |
3475 | ||
3476 | /* | |
3477 | * Command LTTNG_LIST_EVENTS processed by the client thread. | |
3478 | */ | |
56a37563 JG |
3479 | ssize_t cmd_list_events(enum lttng_domain_type domain, |
3480 | struct ltt_session *session, char *channel_name, | |
b4e3ceb9 | 3481 | struct lttng_event **events, size_t *total_size) |
2f77fc4b DG |
3482 | { |
3483 | int ret = 0; | |
3484 | ssize_t nb_event = 0; | |
3485 | ||
3486 | switch (domain) { | |
3487 | case LTTNG_DOMAIN_KERNEL: | |
3488 | if (session->kernel_session != NULL) { | |
3489 | nb_event = list_lttng_kernel_events(channel_name, | |
b4e3ceb9 PP |
3490 | session->kernel_session, events, |
3491 | total_size); | |
2f77fc4b DG |
3492 | } |
3493 | break; | |
3494 | case LTTNG_DOMAIN_UST: | |
3495 | { | |
3496 | if (session->ust_session != NULL) { | |
3497 | nb_event = list_lttng_ust_global_events(channel_name, | |
b4e3ceb9 PP |
3498 | &session->ust_session->domain_global, events, |
3499 | total_size); | |
2f77fc4b DG |
3500 | } |
3501 | break; | |
3502 | } | |
5cdb6027 | 3503 | case LTTNG_DOMAIN_LOG4J: |
3c6a091f | 3504 | case LTTNG_DOMAIN_JUL: |
0e115563 | 3505 | case LTTNG_DOMAIN_PYTHON: |
3c6a091f | 3506 | if (session->ust_session) { |
fefd409b DG |
3507 | struct lttng_ht_iter iter; |
3508 | struct agent *agt; | |
3509 | ||
b11feea5 | 3510 | rcu_read_lock(); |
fefd409b DG |
3511 | cds_lfht_for_each_entry(session->ust_session->agents->ht, |
3512 | &iter.iter, agt, node.node) { | |
1dfd9906 JG |
3513 | if (agt->domain == domain) { |
3514 | nb_event = list_lttng_agent_events( | |
b4e3ceb9 PP |
3515 | agt, events, |
3516 | total_size); | |
1dfd9906 JG |
3517 | break; |
3518 | } | |
fefd409b | 3519 | } |
b11feea5 | 3520 | rcu_read_unlock(); |
3c6a091f DG |
3521 | } |
3522 | break; | |
2f77fc4b | 3523 | default: |
f73fabfd | 3524 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
3525 | goto error; |
3526 | } | |
3527 | ||
f73fabfd | 3528 | return nb_event; |
2f77fc4b DG |
3529 | |
3530 | error: | |
f73fabfd DG |
3531 | /* Return negative value to differentiate return code */ |
3532 | return -ret; | |
2f77fc4b DG |
3533 | } |
3534 | ||
3535 | /* | |
3536 | * Using the session list, filled a lttng_session array to send back to the | |
3537 | * client for session listing. | |
3538 | * | |
3539 | * The session list lock MUST be acquired before calling this function. Use | |
3540 | * session_lock_list() and session_unlock_list(). | |
3541 | */ | |
5feee503 JG |
3542 | void cmd_list_lttng_sessions(struct lttng_session *sessions, |
3543 | size_t session_count, uid_t uid, gid_t gid) | |
2f77fc4b DG |
3544 | { |
3545 | int ret; | |
3546 | unsigned int i = 0; | |
3547 | struct ltt_session *session; | |
3548 | struct ltt_session_list *list = session_get_list(); | |
5feee503 JG |
3549 | struct lttng_session_extended *extended = |
3550 | (typeof(extended)) (&sessions[session_count]); | |
2f77fc4b DG |
3551 | |
3552 | DBG("Getting all available session for UID %d GID %d", | |
3553 | uid, gid); | |
3554 | /* | |
3555 | * Iterate over session list and append data after the control struct in | |
3556 | * the buffer. | |
3557 | */ | |
3558 | cds_list_for_each_entry(session, &list->head, list) { | |
48a86f68 JG |
3559 | if (!session_get(session)) { |
3560 | continue; | |
3561 | } | |
2f77fc4b DG |
3562 | /* |
3563 | * Only list the sessions the user can control. | |
3564 | */ | |
48a86f68 JG |
3565 | if (!session_access_ok(session, uid, gid) || |
3566 | session->destroyed) { | |
3567 | session_put(session); | |
2f77fc4b DG |
3568 | continue; |
3569 | } | |
3570 | ||
3571 | struct ltt_kernel_session *ksess = session->kernel_session; | |
3572 | struct ltt_ust_session *usess = session->ust_session; | |
3573 | ||
3574 | if (session->consumer->type == CONSUMER_DST_NET || | |
3575 | (ksess && ksess->consumer->type == CONSUMER_DST_NET) || | |
3576 | (usess && usess->consumer->type == CONSUMER_DST_NET)) { | |
3577 | ret = build_network_session_path(sessions[i].path, | |
dec56f6c | 3578 | sizeof(sessions[i].path), session); |
2f77fc4b | 3579 | } else { |
dec56f6c | 3580 | ret = snprintf(sessions[i].path, sizeof(sessions[i].path), "%s", |
366a9222 | 3581 | session->consumer->dst.session_root_path); |
2f77fc4b DG |
3582 | } |
3583 | if (ret < 0) { | |
3584 | PERROR("snprintf session path"); | |
48a86f68 | 3585 | session_put(session); |
2f77fc4b DG |
3586 | continue; |
3587 | } | |
3588 | ||
3589 | strncpy(sessions[i].name, session->name, NAME_MAX); | |
3590 | sessions[i].name[NAME_MAX - 1] = '\0'; | |
8382cf6f | 3591 | sessions[i].enabled = session->active; |
2cbf8fed | 3592 | sessions[i].snapshot_mode = session->snapshot_mode; |
8960e9cd | 3593 | sessions[i].live_timer_interval = session->live_timer; |
5feee503 JG |
3594 | extended[i].creation_time.value = (uint64_t) session->creation_time; |
3595 | extended[i].creation_time.is_set = 1; | |
2f77fc4b | 3596 | i++; |
48a86f68 | 3597 | session_put(session); |
2f77fc4b DG |
3598 | } |
3599 | } | |
3600 | ||
806e2684 | 3601 | /* |
6d805429 | 3602 | * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning |
d3f14b8a | 3603 | * ready for trace analysis (or any kind of reader) or else 1 for pending data. |
806e2684 | 3604 | */ |
6d805429 | 3605 | int cmd_data_pending(struct ltt_session *session) |
806e2684 DG |
3606 | { |
3607 | int ret; | |
3608 | struct ltt_kernel_session *ksess = session->kernel_session; | |
3609 | struct ltt_ust_session *usess = session->ust_session; | |
3610 | ||
3611 | assert(session); | |
3612 | ||
5c408ad8 JD |
3613 | DBG("Data pending for session %s", session->name); |
3614 | ||
806e2684 | 3615 | /* Session MUST be stopped to ask for data availability. */ |
8382cf6f | 3616 | if (session->active) { |
806e2684 DG |
3617 | ret = LTTNG_ERR_SESSION_STARTED; |
3618 | goto error; | |
3a89d11a DG |
3619 | } else { |
3620 | /* | |
3621 | * If stopped, just make sure we've started before else the above call | |
3622 | * will always send that there is data pending. | |
3623 | * | |
3624 | * The consumer assumes that when the data pending command is received, | |
3625 | * the trace has been started before or else no output data is written | |
3626 | * by the streams which is a condition for data pending. So, this is | |
3627 | * *VERY* important that we don't ask the consumer before a start | |
3628 | * trace. | |
3629 | */ | |
8382cf6f | 3630 | if (!session->has_been_started) { |
3a89d11a DG |
3631 | ret = 0; |
3632 | goto error; | |
3633 | } | |
806e2684 DG |
3634 | } |
3635 | ||
82528808 JG |
3636 | /* A rotation is still pending, we have to wait. */ |
3637 | if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) { | |
5c408ad8 JD |
3638 | DBG("Rotate still pending for session %s", session->name); |
3639 | ret = 1; | |
3640 | goto error; | |
3641 | } | |
3642 | ||
806e2684 | 3643 | if (ksess && ksess->consumer) { |
6d805429 DG |
3644 | ret = consumer_is_data_pending(ksess->id, ksess->consumer); |
3645 | if (ret == 1) { | |
806e2684 DG |
3646 | /* Data is still being extracted for the kernel. */ |
3647 | goto error; | |
3648 | } | |
3649 | } | |
3650 | ||
3651 | if (usess && usess->consumer) { | |
6d805429 DG |
3652 | ret = consumer_is_data_pending(usess->id, usess->consumer); |
3653 | if (ret == 1) { | |
806e2684 DG |
3654 | /* Data is still being extracted for the kernel. */ |
3655 | goto error; | |
3656 | } | |
3657 | } | |
3658 | ||
3659 | /* Data is ready to be read by a viewer */ | |
6d805429 | 3660 | ret = 0; |
806e2684 DG |
3661 | |
3662 | error: | |
3663 | return ret; | |
3664 | } | |
3665 | ||
6dc3064a DG |
3666 | /* |
3667 | * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library. | |
3668 | * | |
3669 | * Return LTTNG_OK on success or else a LTTNG_ERR code. | |
3670 | */ | |
3671 | int cmd_snapshot_add_output(struct ltt_session *session, | |
3672 | struct lttng_snapshot_output *output, uint32_t *id) | |
3673 | { | |
3674 | int ret; | |
3675 | struct snapshot_output *new_output; | |
3676 | ||
3677 | assert(session); | |
3678 | assert(output); | |
3679 | ||
3680 | DBG("Cmd snapshot add output for session %s", session->name); | |
3681 | ||
3682 | /* | |
903ef685 | 3683 | * Can't create an output if the session is not set in no-output mode. |
6dc3064a DG |
3684 | */ |
3685 | if (session->output_traces) { | |
903ef685 | 3686 | ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION; |
6dc3064a DG |
3687 | goto error; |
3688 | } | |
3689 | ||
8954c04a JG |
3690 | if (session->has_non_mmap_channel) { |
3691 | ret = LTTNG_ERR_SNAPSHOT_UNSUPPORTED; | |
3692 | goto error; | |
3693 | } | |
3694 | ||
6dc3064a DG |
3695 | /* Only one output is allowed until we have the "tee" feature. */ |
3696 | if (session->snapshot.nb_output == 1) { | |
3697 | ret = LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST; | |
3698 | goto error; | |
3699 | } | |
3700 | ||
3701 | new_output = snapshot_output_alloc(); | |
3702 | if (!new_output) { | |
3703 | ret = LTTNG_ERR_NOMEM; | |
3704 | goto error; | |
3705 | } | |
3706 | ||
5feee503 | 3707 | ret = snapshot_output_init(session, output->max_size, output->name, |
6dc3064a DG |
3708 | output->ctrl_url, output->data_url, session->consumer, new_output, |
3709 | &session->snapshot); | |
3710 | if (ret < 0) { | |
3711 | if (ret == -ENOMEM) { | |
3712 | ret = LTTNG_ERR_NOMEM; | |
3713 | } else { | |
3714 | ret = LTTNG_ERR_INVALID; | |
3715 | } | |
3716 | goto free_error; | |
3717 | } | |
3718 | ||
6dc3064a DG |
3719 | rcu_read_lock(); |
3720 | snapshot_add_output(&session->snapshot, new_output); | |
3721 | if (id) { | |
3722 | *id = new_output->id; | |
3723 | } | |
3724 | rcu_read_unlock(); | |
3725 | ||
3726 | return LTTNG_OK; | |
3727 | ||
3728 | free_error: | |
3729 | snapshot_output_destroy(new_output); | |
3730 | error: | |
3731 | return ret; | |
3732 | } | |
3733 | ||
3734 | /* | |
3735 | * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl. | |
3736 | * | |
3737 | * Return LTTNG_OK on success or else a LTTNG_ERR code. | |
3738 | */ | |
3739 | int cmd_snapshot_del_output(struct ltt_session *session, | |
3740 | struct lttng_snapshot_output *output) | |
3741 | { | |
3742 | int ret; | |
eb240553 | 3743 | struct snapshot_output *sout = NULL; |
6dc3064a DG |
3744 | |
3745 | assert(session); | |
3746 | assert(output); | |
3747 | ||
6dc3064a DG |
3748 | rcu_read_lock(); |
3749 | ||
3750 | /* | |
d3f14b8a MD |
3751 | * Permission denied to create an output if the session is not |
3752 | * set in no output mode. | |
6dc3064a DG |
3753 | */ |
3754 | if (session->output_traces) { | |
903ef685 | 3755 | ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION; |
6dc3064a DG |
3756 | goto error; |
3757 | } | |
3758 | ||
eb240553 DG |
3759 | if (output->id) { |
3760 | DBG("Cmd snapshot del output id %" PRIu32 " for session %s", output->id, | |
3761 | session->name); | |
3762 | sout = snapshot_find_output_by_id(output->id, &session->snapshot); | |
3763 | } else if (*output->name != '\0') { | |
3764 | DBG("Cmd snapshot del output name %s for session %s", output->name, | |
3765 | session->name); | |
3766 | sout = snapshot_find_output_by_name(output->name, &session->snapshot); | |
3767 | } | |
6dc3064a DG |
3768 | if (!sout) { |
3769 | ret = LTTNG_ERR_INVALID; | |
3770 | goto error; | |
3771 | } | |
3772 | ||
3773 | snapshot_delete_output(&session->snapshot, sout); | |
3774 | snapshot_output_destroy(sout); | |
3775 | ret = LTTNG_OK; | |
3776 | ||
3777 | error: | |
3778 | rcu_read_unlock(); | |
3779 | return ret; | |
3780 | } | |
3781 | ||
3782 | /* | |
3783 | * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl. | |
3784 | * | |
3785 | * If no output is available, outputs is untouched and 0 is returned. | |
3786 | * | |
3787 | * Return the size of the newly allocated outputs or a negative LTTNG_ERR code. | |
3788 | */ | |
3789 | ssize_t cmd_snapshot_list_outputs(struct ltt_session *session, | |
3790 | struct lttng_snapshot_output **outputs) | |
3791 | { | |
3792 | int ret, idx = 0; | |
b223ca94 | 3793 | struct lttng_snapshot_output *list = NULL; |
6dc3064a DG |
3794 | struct lttng_ht_iter iter; |
3795 | struct snapshot_output *output; | |
3796 | ||
3797 | assert(session); | |
3798 | assert(outputs); | |
3799 | ||
3800 | DBG("Cmd snapshot list outputs for session %s", session->name); | |
3801 | ||
3802 | /* | |
d3f14b8a MD |
3803 | * Permission denied to create an output if the session is not |
3804 | * set in no output mode. | |
6dc3064a DG |
3805 | */ |
3806 | if (session->output_traces) { | |
903ef685 JG |
3807 | ret = -LTTNG_ERR_NOT_SNAPSHOT_SESSION; |
3808 | goto end; | |
6dc3064a DG |
3809 | } |
3810 | ||
3811 | if (session->snapshot.nb_output == 0) { | |
3812 | ret = 0; | |
903ef685 | 3813 | goto end; |
6dc3064a DG |
3814 | } |
3815 | ||
3816 | list = zmalloc(session->snapshot.nb_output * sizeof(*list)); | |
3817 | if (!list) { | |
b223ca94 | 3818 | ret = -LTTNG_ERR_NOMEM; |
903ef685 | 3819 | goto end; |
6dc3064a DG |
3820 | } |
3821 | ||
3822 | /* Copy list from session to the new list object. */ | |
b223ca94 | 3823 | rcu_read_lock(); |
6dc3064a DG |
3824 | cds_lfht_for_each_entry(session->snapshot.output_ht->ht, &iter.iter, |
3825 | output, node.node) { | |
3826 | assert(output->consumer); | |
3827 | list[idx].id = output->id; | |
3828 | list[idx].max_size = output->max_size; | |
6ce22875 MD |
3829 | if (lttng_strncpy(list[idx].name, output->name, |
3830 | sizeof(list[idx].name))) { | |
3831 | ret = -LTTNG_ERR_INVALID; | |
903ef685 | 3832 | goto error; |
6ce22875 | 3833 | } |
6dc3064a | 3834 | if (output->consumer->type == CONSUMER_DST_LOCAL) { |
6ce22875 | 3835 | if (lttng_strncpy(list[idx].ctrl_url, |
366a9222 | 3836 | output->consumer->dst.session_root_path, |
6ce22875 MD |
3837 | sizeof(list[idx].ctrl_url))) { |
3838 | ret = -LTTNG_ERR_INVALID; | |
903ef685 | 3839 | goto error; |
6ce22875 | 3840 | } |
6dc3064a DG |
3841 | } else { |
3842 | /* Control URI. */ | |
3843 | ret = uri_to_str_url(&output->consumer->dst.net.control, | |
3844 | list[idx].ctrl_url, sizeof(list[idx].ctrl_url)); | |
3845 | if (ret < 0) { | |
b223ca94 | 3846 | ret = -LTTNG_ERR_NOMEM; |
903ef685 | 3847 | goto error; |
6dc3064a DG |
3848 | } |
3849 | ||
3850 | /* Data URI. */ | |
3851 | ret = uri_to_str_url(&output->consumer->dst.net.data, | |
3852 | list[idx].data_url, sizeof(list[idx].data_url)); | |
3853 | if (ret < 0) { | |
b223ca94 | 3854 | ret = -LTTNG_ERR_NOMEM; |
903ef685 | 3855 | goto error; |
6dc3064a DG |
3856 | } |
3857 | } | |
3858 | idx++; | |
3859 | } | |
3860 | ||
3861 | *outputs = list; | |
b223ca94 JG |
3862 | list = NULL; |
3863 | ret = session->snapshot.nb_output; | |
6dc3064a | 3864 | error: |
903ef685 | 3865 | rcu_read_unlock(); |
b223ca94 | 3866 | free(list); |
903ef685 | 3867 | end: |
b223ca94 | 3868 | return ret; |
6dc3064a DG |
3869 | } |
3870 | ||
93ec662e JD |
3871 | /* |
3872 | * Check if we can regenerate the metadata for this session. | |
3873 | * Only kernel, UST per-uid and non-live sessions are supported. | |
3874 | * | |
3875 | * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise. | |
3876 | */ | |
3877 | static | |
eded6438 | 3878 | int check_regenerate_metadata_support(struct ltt_session *session) |
93ec662e JD |
3879 | { |
3880 | int ret; | |
3881 | ||
3882 | assert(session); | |
3883 | ||
3884 | if (session->live_timer != 0) { | |
3885 | ret = LTTNG_ERR_LIVE_SESSION; | |
3886 | goto end; | |
3887 | } | |
3888 | if (!session->active) { | |
3889 | ret = LTTNG_ERR_SESSION_NOT_STARTED; | |
3890 | goto end; | |
3891 | } | |
3892 | if (session->ust_session) { | |
3893 | switch (session->ust_session->buffer_type) { | |
3894 | case LTTNG_BUFFER_PER_UID: | |
3895 | break; | |
3896 | case LTTNG_BUFFER_PER_PID: | |
3897 | ret = LTTNG_ERR_PER_PID_SESSION; | |
3898 | goto end; | |
3899 | default: | |
3900 | assert(0); | |
3901 | ret = LTTNG_ERR_UNK; | |
3902 | goto end; | |
3903 | } | |
3904 | } | |
3905 | if (session->consumer->type == CONSUMER_DST_NET && | |
3906 | session->consumer->relay_minor_version < 8) { | |
3907 | ret = LTTNG_ERR_RELAYD_VERSION_FAIL; | |
3908 | goto end; | |
3909 | } | |
3910 | ret = 0; | |
3911 | ||
3912 | end: | |
3913 | return ret; | |
3914 | } | |
3915 | ||
7802bae3 LL |
3916 | static |
3917 | int clear_metadata_file(int fd) | |
3918 | { | |
3919 | int ret; | |
a5df8828 | 3920 | off_t lseek_ret; |
7802bae3 | 3921 | |
a5df8828 GL |
3922 | lseek_ret = lseek(fd, 0, SEEK_SET); |
3923 | if (lseek_ret < 0) { | |
7802bae3 | 3924 | PERROR("lseek"); |
a5df8828 | 3925 | ret = -1; |
7802bae3 LL |
3926 | goto end; |
3927 | } | |
3928 | ||
3929 | ret = ftruncate(fd, 0); | |
3930 | if (ret < 0) { | |
3931 | PERROR("ftruncate"); | |
3932 | goto end; | |
3933 | } | |
3934 | ||
3935 | end: | |
3936 | return ret; | |
3937 | } | |
3938 | ||
93ec662e | 3939 | static |
eded6438 | 3940 | int ust_regenerate_metadata(struct ltt_ust_session *usess) |
93ec662e JD |
3941 | { |
3942 | int ret = 0; | |
3943 | struct buffer_reg_uid *uid_reg = NULL; | |
3944 | struct buffer_reg_session *session_reg = NULL; | |
3945 | ||
3946 | rcu_read_lock(); | |
3947 | cds_list_for_each_entry(uid_reg, &usess->buffer_reg_uid_list, lnode) { | |
3948 | struct ust_registry_session *registry; | |
3949 | struct ust_registry_channel *chan; | |
3950 | struct lttng_ht_iter iter_chan; | |
3951 | ||
3952 | session_reg = uid_reg->registry; | |
3953 | registry = session_reg->reg.ust; | |
3954 | ||
3955 | pthread_mutex_lock(®istry->lock); | |
3956 | registry->metadata_len_sent = 0; | |
3957 | memset(registry->metadata, 0, registry->metadata_alloc_len); | |
3958 | registry->metadata_len = 0; | |
3959 | registry->metadata_version++; | |
7802bae3 LL |
3960 | if (registry->metadata_fd > 0) { |
3961 | /* Clear the metadata file's content. */ | |
3962 | ret = clear_metadata_file(registry->metadata_fd); | |
3963 | if (ret) { | |
3964 | pthread_mutex_unlock(®istry->lock); | |
3965 | goto end; | |
3966 | } | |
3967 | } | |
3968 | ||
93ec662e JD |
3969 | ret = ust_metadata_session_statedump(registry, NULL, |
3970 | registry->major, registry->minor); | |
3971 | if (ret) { | |
3972 | pthread_mutex_unlock(®istry->lock); | |
3973 | ERR("Failed to generate session metadata (err = %d)", | |
3974 | ret); | |
3975 | goto end; | |
3976 | } | |
3977 | cds_lfht_for_each_entry(registry->channels->ht, &iter_chan.iter, | |
3978 | chan, node.node) { | |
3979 | struct ust_registry_event *event; | |
3980 | struct lttng_ht_iter iter_event; | |
3981 | ||
3982 | ret = ust_metadata_channel_statedump(registry, chan); | |
3983 | if (ret) { | |
3984 | pthread_mutex_unlock(®istry->lock); | |
3985 | ERR("Failed to generate channel metadata " | |
3986 | "(err = %d)", ret); | |
3987 | goto end; | |
3988 | } | |
3989 | cds_lfht_for_each_entry(chan->ht->ht, &iter_event.iter, | |
3990 | event, node.node) { | |
3991 | ret = ust_metadata_event_statedump(registry, | |
3992 | chan, event); | |
3993 | if (ret) { | |
3994 | pthread_mutex_unlock(®istry->lock); | |
3995 | ERR("Failed to generate event metadata " | |
3996 | "(err = %d)", ret); | |
3997 | goto end; | |
3998 | } | |
3999 | } | |
4000 | } | |
4001 | pthread_mutex_unlock(®istry->lock); | |
4002 | } | |
4003 | ||
4004 | end: | |
4005 | rcu_read_unlock(); | |
4006 | return ret; | |
4007 | } | |
4008 | ||
4009 | /* | |
eded6438 | 4010 | * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library. |
93ec662e JD |
4011 | * |
4012 | * Ask the consumer to truncate the existing metadata file(s) and | |
4013 | * then regenerate the metadata. Live and per-pid sessions are not | |
4014 | * supported and return an error. | |
4015 | * | |
4016 | * Return 0 on success or else a LTTNG_ERR code. | |
4017 | */ | |
eded6438 | 4018 | int cmd_regenerate_metadata(struct ltt_session *session) |
93ec662e JD |
4019 | { |
4020 | int ret; | |
4021 | ||
4022 | assert(session); | |
4023 | ||
eded6438 | 4024 | ret = check_regenerate_metadata_support(session); |
93ec662e JD |
4025 | if (ret) { |
4026 | goto end; | |
4027 | } | |
4028 | ||
4029 | if (session->kernel_session) { | |
eded6438 | 4030 | ret = kernctl_session_regenerate_metadata( |
93ec662e JD |
4031 | session->kernel_session->fd); |
4032 | if (ret < 0) { | |
4033 | ERR("Failed to regenerate the kernel metadata"); | |
4034 | goto end; | |
4035 | } | |
4036 | } | |
4037 | ||
4038 | if (session->ust_session) { | |
eded6438 | 4039 | ret = ust_regenerate_metadata(session->ust_session); |
93ec662e JD |
4040 | if (ret < 0) { |
4041 | ERR("Failed to regenerate the UST metadata"); | |
4042 | goto end; | |
4043 | } | |
4044 | } | |
4045 | DBG("Cmd metadata regenerate for session %s", session->name); | |
4046 | ret = LTTNG_OK; | |
4047 | ||
4048 | end: | |
4049 | return ret; | |
4050 | } | |
4051 | ||
c2561365 JD |
4052 | /* |
4053 | * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library. | |
4054 | * | |
4055 | * Ask the tracer to regenerate a new statedump. | |
4056 | * | |
4057 | * Return 0 on success or else a LTTNG_ERR code. | |
4058 | */ | |
4059 | int cmd_regenerate_statedump(struct ltt_session *session) | |
4060 | { | |
4061 | int ret; | |
4062 | ||
4063 | assert(session); | |
4064 | ||
4065 | if (!session->active) { | |
4066 | ret = LTTNG_ERR_SESSION_NOT_STARTED; | |
4067 | goto end; | |
4068 | } | |
c2561365 JD |
4069 | |
4070 | if (session->kernel_session) { | |
4071 | ret = kernctl_session_regenerate_statedump( | |
4072 | session->kernel_session->fd); | |
4073 | /* | |
4074 | * Currently, the statedump in kernel can only fail if out | |
4075 | * of memory. | |
4076 | */ | |
4077 | if (ret < 0) { | |
4078 | if (ret == -ENOMEM) { | |
4079 | ret = LTTNG_ERR_REGEN_STATEDUMP_NOMEM; | |
4080 | } else { | |
4081 | ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL; | |
4082 | } | |
4083 | ERR("Failed to regenerate the kernel statedump"); | |
4084 | goto end; | |
4085 | } | |
4086 | } | |
4087 | ||
4088 | if (session->ust_session) { | |
4089 | ret = ust_app_regenerate_statedump_all(session->ust_session); | |
4090 | /* | |
4091 | * Currently, the statedump in UST always returns 0. | |
4092 | */ | |
4093 | if (ret < 0) { | |
4094 | ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL; | |
4095 | ERR("Failed to regenerate the UST statedump"); | |
4096 | goto end; | |
4097 | } | |
4098 | } | |
4099 | DBG("Cmd regenerate statedump for session %s", session->name); | |
4100 | ret = LTTNG_OK; | |
4101 | ||
4102 | end: | |
4103 | return ret; | |
4104 | } | |
4105 | ||
b0880ae5 JG |
4106 | int cmd_register_trigger(struct command_ctx *cmd_ctx, int sock, |
4107 | struct notification_thread_handle *notification_thread) | |
4108 | { | |
4109 | int ret; | |
4110 | size_t trigger_len; | |
4111 | ssize_t sock_recv_len; | |
4112 | struct lttng_trigger *trigger = NULL; | |
4113 | struct lttng_buffer_view view; | |
4114 | struct lttng_dynamic_buffer trigger_buffer; | |
4115 | ||
4116 | lttng_dynamic_buffer_init(&trigger_buffer); | |
4117 | trigger_len = (size_t) cmd_ctx->lsm->u.trigger.length; | |
4118 | ret = lttng_dynamic_buffer_set_size(&trigger_buffer, trigger_len); | |
4119 | if (ret) { | |
4120 | ret = LTTNG_ERR_NOMEM; | |
4121 | goto end; | |
4122 | } | |
4123 | ||
4124 | sock_recv_len = lttcomm_recv_unix_sock(sock, trigger_buffer.data, | |
4125 | trigger_len); | |
4126 | if (sock_recv_len < 0 || sock_recv_len != trigger_len) { | |
4127 | ERR("Failed to receive \"register trigger\" command payload"); | |
4128 | /* TODO: should this be a new error enum ? */ | |
4129 | ret = LTTNG_ERR_INVALID_TRIGGER; | |
4130 | goto end; | |
4131 | } | |
4132 | ||
4133 | view = lttng_buffer_view_from_dynamic_buffer(&trigger_buffer, 0, -1); | |
4134 | if (lttng_trigger_create_from_buffer(&view, &trigger) != | |
4135 | trigger_len) { | |
4136 | ERR("Invalid trigger payload received in \"register trigger\" command"); | |
4137 | ret = LTTNG_ERR_INVALID_TRIGGER; | |
4138 | goto end; | |
4139 | } | |
4140 | ||
4141 | ret = notification_thread_command_register_trigger(notification_thread, | |
4142 | trigger); | |
587f9fd0 JG |
4143 | /* Ownership of trigger was transferred. */ |
4144 | trigger = NULL; | |
b0880ae5 | 4145 | end: |
587f9fd0 | 4146 | lttng_trigger_destroy(trigger); |
b0880ae5 JG |
4147 | lttng_dynamic_buffer_reset(&trigger_buffer); |
4148 | return ret; | |
4149 | } | |
4150 | ||
4151 | int cmd_unregister_trigger(struct command_ctx *cmd_ctx, int sock, | |
4152 | struct notification_thread_handle *notification_thread) | |
4153 | { | |
4154 | int ret; | |
4155 | size_t trigger_len; | |
4156 | ssize_t sock_recv_len; | |
4157 | struct lttng_trigger *trigger = NULL; | |
4158 | struct lttng_buffer_view view; | |
4159 | struct lttng_dynamic_buffer trigger_buffer; | |
4160 | ||
4161 | lttng_dynamic_buffer_init(&trigger_buffer); | |
4162 | trigger_len = (size_t) cmd_ctx->lsm->u.trigger.length; | |
4163 | ret = lttng_dynamic_buffer_set_size(&trigger_buffer, trigger_len); | |
4164 | if (ret) { | |
4165 | ret = LTTNG_ERR_NOMEM; | |
4166 | goto end; | |
4167 | } | |
4168 | ||
4169 | sock_recv_len = lttcomm_recv_unix_sock(sock, trigger_buffer.data, | |
4170 | trigger_len); | |
4171 | if (sock_recv_len < 0 || sock_recv_len != trigger_len) { | |
4172 | ERR("Failed to receive \"unregister trigger\" command payload"); | |
4173 | /* TODO: should this be a new error enum ? */ | |
4174 | ret = LTTNG_ERR_INVALID_TRIGGER; | |
4175 | goto end; | |
4176 | } | |
4177 | ||
4178 | view = lttng_buffer_view_from_dynamic_buffer(&trigger_buffer, 0, -1); | |
4179 | if (lttng_trigger_create_from_buffer(&view, &trigger) != | |
4180 | trigger_len) { | |
4181 | ERR("Invalid trigger payload received in \"unregister trigger\" command"); | |
4182 | ret = LTTNG_ERR_INVALID_TRIGGER; | |
4183 | goto end; | |
4184 | } | |
4185 | ||
4186 | ret = notification_thread_command_unregister_trigger(notification_thread, | |
4187 | trigger); | |
4188 | end: | |
587f9fd0 | 4189 | lttng_trigger_destroy(trigger); |
b0880ae5 JG |
4190 | lttng_dynamic_buffer_reset(&trigger_buffer); |
4191 | return ret; | |
4192 | } | |
4193 | ||
6dc3064a DG |
4194 | /* |
4195 | * Send relayd sockets from snapshot output to consumer. Ignore request if the | |
4196 | * snapshot output is *not* set with a remote destination. | |
4197 | * | |
8f07cd01 | 4198 | * Return LTTNG_OK on success or a LTTNG_ERR code. |
6dc3064a | 4199 | */ |
8f07cd01 JG |
4200 | static enum lttng_error_code set_relayd_for_snapshot( |
4201 | struct consumer_output *consumer, | |
65ff8ea3 JG |
4202 | const struct snapshot_output *snap_output, |
4203 | const struct ltt_session *session) | |
6dc3064a | 4204 | { |
8f07cd01 | 4205 | enum lttng_error_code status = LTTNG_OK; |
6dc3064a DG |
4206 | struct lttng_ht_iter iter; |
4207 | struct consumer_socket *socket; | |
b2c25d00 | 4208 | LTTNG_OPTIONAL(uint64_t) current_chunk_id = {}; |
6dc3064a DG |
4209 | |
4210 | assert(consumer); | |
4211 | assert(snap_output); | |
4212 | assert(session); | |
4213 | ||
4214 | DBG2("Set relayd object from snapshot output"); | |
4215 | ||
b2c25d00 JG |
4216 | if (session->current_trace_chunk) { |
4217 | enum lttng_trace_chunk_status status = lttng_trace_chunk_get_id( | |
4218 | session->current_trace_chunk, ¤t_chunk_id.value); | |
4219 | ||
4220 | if (status == LTTNG_TRACE_CHUNK_STATUS_OK) { | |
4221 | current_chunk_id.is_set = true; | |
4222 | } else { | |
4223 | ERR("Failed to get current trace chunk id"); | |
4224 | status = LTTNG_ERR_UNK; | |
4225 | goto error; | |
4226 | } | |
4227 | } | |
4228 | ||
6dc3064a DG |
4229 | /* Ignore if snapshot consumer output is not network. */ |
4230 | if (snap_output->consumer->type != CONSUMER_DST_NET) { | |
4231 | goto error; | |
4232 | } | |
4233 | ||
4234 | /* | |
4235 | * For each consumer socket, create and send the relayd object of the | |
4236 | * snapshot output. | |
4237 | */ | |
4238 | rcu_read_lock(); | |
5eecee74 DG |
4239 | cds_lfht_for_each_entry(snap_output->consumer->socks->ht, &iter.iter, |
4240 | socket, node.node) { | |
ecd0f96d | 4241 | pthread_mutex_lock(socket->lock); |
8f07cd01 | 4242 | status = send_consumer_relayd_sockets(0, session->id, |
d3e2ba59 JD |
4243 | snap_output->consumer, socket, |
4244 | session->name, session->hostname, | |
b2c25d00 JG |
4245 | session->live_timer, |
4246 | current_chunk_id.is_set ? ¤t_chunk_id.value : NULL); | |
ecd0f96d | 4247 | pthread_mutex_unlock(socket->lock); |
8f07cd01 | 4248 | if (status != LTTNG_OK) { |
6dc3064a DG |
4249 | rcu_read_unlock(); |
4250 | goto error; | |
4251 | } | |
4252 | } | |
4253 | rcu_read_unlock(); | |
4254 | ||
4255 | error: | |
8f07cd01 | 4256 | return status; |
6dc3064a DG |
4257 | } |
4258 | ||
4259 | /* | |
4260 | * Record a kernel snapshot. | |
4261 | * | |
fac41e72 | 4262 | * Return LTTNG_OK on success or a LTTNG_ERR code. |
6dc3064a | 4263 | */ |
65ff8ea3 JG |
4264 | static enum lttng_error_code record_kernel_snapshot( |
4265 | struct ltt_kernel_session *ksess, | |
4266 | const struct snapshot_output *output, | |
4267 | const struct ltt_session *session, | |
d07ceecd | 4268 | int wait, uint64_t nb_packets_per_stream) |
6dc3064a DG |
4269 | { |
4270 | int ret; | |
8f07cd01 | 4271 | enum lttng_error_code status; |
6dc3064a DG |
4272 | |
4273 | assert(ksess); | |
4274 | assert(output); | |
4275 | assert(session); | |
4276 | ||
af706bb7 DG |
4277 | /* |
4278 | * Copy kernel session sockets so we can communicate with the right | |
4279 | * consumer for the snapshot record command. | |
4280 | */ | |
4281 | ret = consumer_copy_sockets(output->consumer, ksess->consumer); | |
4282 | if (ret < 0) { | |
8f07cd01 | 4283 | status = LTTNG_ERR_NOMEM; |
af706bb7 | 4284 | goto error; |
6dc3064a DG |
4285 | } |
4286 | ||
8f07cd01 JG |
4287 | status = set_relayd_for_snapshot(ksess->consumer, output, session); |
4288 | if (status != LTTNG_OK) { | |
af706bb7 | 4289 | goto error_snapshot; |
6dc3064a DG |
4290 | } |
4291 | ||
8f07cd01 JG |
4292 | status = kernel_snapshot_record(ksess, output, wait, nb_packets_per_stream); |
4293 | if (status != LTTNG_OK) { | |
af706bb7 | 4294 | goto error_snapshot; |
6dc3064a DG |
4295 | } |
4296 | ||
1371fc12 | 4297 | goto end; |
a934f4af | 4298 | |
af706bb7 DG |
4299 | error_snapshot: |
4300 | /* Clean up copied sockets so this output can use some other later on. */ | |
4301 | consumer_destroy_output_sockets(output->consumer); | |
6dc3064a | 4302 | error: |
1371fc12 | 4303 | end: |
8f07cd01 | 4304 | return status; |
6dc3064a DG |
4305 | } |
4306 | ||
4307 | /* | |
4308 | * Record a UST snapshot. | |
4309 | * | |
8f07cd01 | 4310 | * Returns LTTNG_OK on success or a LTTNG_ERR error code. |
6dc3064a | 4311 | */ |
8f07cd01 | 4312 | static enum lttng_error_code record_ust_snapshot(struct ltt_ust_session *usess, |
65ff8ea3 JG |
4313 | const struct snapshot_output *output, |
4314 | const struct ltt_session *session, int wait, | |
4315 | uint64_t nb_packets_per_stream) | |
6dc3064a DG |
4316 | { |
4317 | int ret; | |
8f07cd01 | 4318 | enum lttng_error_code status; |
6dc3064a DG |
4319 | |
4320 | assert(usess); | |
4321 | assert(output); | |
4322 | assert(session); | |
4323 | ||
af706bb7 | 4324 | /* |
d3f14b8a | 4325 | * Copy UST session sockets so we can communicate with the right |
af706bb7 DG |
4326 | * consumer for the snapshot record command. |
4327 | */ | |
4328 | ret = consumer_copy_sockets(output->consumer, usess->consumer); | |
4329 | if (ret < 0) { | |
8f07cd01 | 4330 | status = LTTNG_ERR_NOMEM; |
af706bb7 | 4331 | goto error; |
6dc3064a DG |
4332 | } |
4333 | ||
8f07cd01 JG |
4334 | status = set_relayd_for_snapshot(usess->consumer, output, session); |
4335 | if (status != LTTNG_OK) { | |
af706bb7 | 4336 | goto error_snapshot; |
6dc3064a DG |
4337 | } |
4338 | ||
65ff8ea3 JG |
4339 | status = ust_app_snapshot_record(usess, output, wait, |
4340 | nb_packets_per_stream); | |
8f07cd01 | 4341 | if (status != LTTNG_OK) { |
af706bb7 | 4342 | goto error_snapshot; |
6dc3064a DG |
4343 | } |
4344 | ||
31e66c40 JR |
4345 | goto end; |
4346 | ||
af706bb7 DG |
4347 | error_snapshot: |
4348 | /* Clean up copied sockets so this output can use some other later on. */ | |
4349 | consumer_destroy_output_sockets(output->consumer); | |
6dc3064a | 4350 | error: |
31e66c40 | 4351 | end: |
8f07cd01 | 4352 | return status; |
6dc3064a DG |
4353 | } |
4354 | ||
d07ceecd | 4355 | static |
65ff8ea3 JG |
4356 | uint64_t get_session_size_one_more_packet_per_stream( |
4357 | const struct ltt_session *session, uint64_t cur_nr_packets) | |
68808f4e | 4358 | { |
d07ceecd | 4359 | uint64_t tot_size = 0; |
68808f4e DG |
4360 | |
4361 | if (session->kernel_session) { | |
4362 | struct ltt_kernel_channel *chan; | |
65ff8ea3 JG |
4363 | const struct ltt_kernel_session *ksess = |
4364 | session->kernel_session; | |
68808f4e | 4365 | |
68808f4e | 4366 | cds_list_for_each_entry(chan, &ksess->channel_list.head, list) { |
d07ceecd MD |
4367 | if (cur_nr_packets >= chan->channel->attr.num_subbuf) { |
4368 | /* | |
4369 | * Don't take channel into account if we | |
4370 | * already grab all its packets. | |
4371 | */ | |
4372 | continue; | |
68808f4e | 4373 | } |
d07ceecd MD |
4374 | tot_size += chan->channel->attr.subbuf_size |
4375 | * chan->stream_count; | |
68808f4e DG |
4376 | } |
4377 | } | |
4378 | ||
4379 | if (session->ust_session) { | |
65ff8ea3 | 4380 | const struct ltt_ust_session *usess = session->ust_session; |
68808f4e | 4381 | |
d07ceecd MD |
4382 | tot_size += ust_app_get_size_one_more_packet_per_stream(usess, |
4383 | cur_nr_packets); | |
68808f4e DG |
4384 | } |
4385 | ||
d07ceecd | 4386 | return tot_size; |
68808f4e DG |
4387 | } |
4388 | ||
5c786ded | 4389 | /* |
d07ceecd MD |
4390 | * Calculate the number of packets we can grab from each stream that |
4391 | * fits within the overall snapshot max size. | |
4392 | * | |
4393 | * Returns -1 on error, 0 means infinite number of packets, else > 0 is | |
4394 | * the number of packets per stream. | |
4395 | * | |
4396 | * TODO: this approach is not perfect: we consider the worse case | |
4397 | * (packet filling the sub-buffers) as an upper bound, but we could do | |
4398 | * better if we do this calculation while we actually grab the packet | |
4399 | * content: we would know how much padding we don't actually store into | |
4400 | * the file. | |
4401 | * | |
4402 | * This algorithm is currently bounded by the number of packets per | |
4403 | * stream. | |
4404 | * | |
4405 | * Since we call this algorithm before actually grabbing the data, it's | |
4406 | * an approximation: for instance, applications could appear/disappear | |
4407 | * in between this call and actually grabbing data. | |
5c786ded | 4408 | */ |
d07ceecd | 4409 | static |
65ff8ea3 JG |
4410 | int64_t get_session_nb_packets_per_stream(const struct ltt_session *session, |
4411 | uint64_t max_size) | |
5c786ded | 4412 | { |
d07ceecd MD |
4413 | int64_t size_left; |
4414 | uint64_t cur_nb_packets = 0; | |
5c786ded | 4415 | |
d07ceecd MD |
4416 | if (!max_size) { |
4417 | return 0; /* Infinite */ | |
5c786ded JD |
4418 | } |
4419 | ||
d07ceecd MD |
4420 | size_left = max_size; |
4421 | for (;;) { | |
4422 | uint64_t one_more_packet_tot_size; | |
5c786ded | 4423 | |
65ff8ea3 JG |
4424 | one_more_packet_tot_size = get_session_size_one_more_packet_per_stream( |
4425 | session, cur_nb_packets); | |
d07ceecd MD |
4426 | if (!one_more_packet_tot_size) { |
4427 | /* We are already grabbing all packets. */ | |
4428 | break; | |
4429 | } | |
4430 | size_left -= one_more_packet_tot_size; | |
4431 | if (size_left < 0) { | |
4432 | break; | |
4433 | } | |
4434 | cur_nb_packets++; | |
5c786ded | 4435 | } |
d07ceecd MD |
4436 | if (!cur_nb_packets) { |
4437 | /* Not enough room to grab one packet of each stream, error. */ | |
4438 | return -1; | |
4439 | } | |
4440 | return cur_nb_packets; | |
5c786ded JD |
4441 | } |
4442 | ||
65ff8ea3 | 4443 | static |
e5148e25 | 4444 | enum lttng_error_code snapshot_record(struct ltt_session *session, |
65ff8ea3 JG |
4445 | const struct snapshot_output *snapshot_output, int wait) |
4446 | { | |
e5148e25 | 4447 | int fmt_ret; |
65ff8ea3 | 4448 | int64_t nb_packets_per_stream; |
e5148e25 | 4449 | char snapshot_chunk_name[LTTNG_NAME_MAX]; |
65ff8ea3 | 4450 | enum lttng_error_code ret = LTTNG_OK; |
e5148e25 JG |
4451 | struct lttng_trace_chunk *snapshot_trace_chunk; |
4452 | ||
4453 | fmt_ret = snprintf(snapshot_chunk_name, sizeof(snapshot_chunk_name), | |
4454 | "%s-%s-%" PRIu64, | |
4455 | snapshot_output->name, | |
4456 | snapshot_output->datetime, | |
4457 | snapshot_output->nb_snapshot); | |
4458 | if (fmt_ret < 0 || fmt_ret >= sizeof(snapshot_chunk_name)) { | |
4459 | ERR("Failed to format snapshot name"); | |
4460 | ret = LTTNG_ERR_INVALID; | |
4461 | goto end; | |
4462 | } | |
4463 | DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"", | |
4464 | snapshot_output->name, session->name, | |
4465 | snapshot_chunk_name); | |
4466 | snapshot_trace_chunk = session_create_new_trace_chunk(session, | |
4467 | snapshot_output_get_base_path(snapshot_output), | |
4468 | snapshot_chunk_name); | |
4469 | if (!snapshot_trace_chunk) { | |
4470 | ret = LTTNG_ERR_CREATE_DIR_FAIL; | |
4471 | goto end; | |
4472 | } | |
4473 | assert(!session->current_trace_chunk); | |
4474 | ret = session_set_trace_chunk(session, snapshot_trace_chunk, NULL); | |
4475 | lttng_trace_chunk_put(snapshot_trace_chunk); | |
4476 | snapshot_trace_chunk = NULL; | |
4477 | if (ret) { | |
4478 | ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER; | |
4479 | goto end; | |
4480 | } | |
65ff8ea3 JG |
4481 | |
4482 | nb_packets_per_stream = get_session_nb_packets_per_stream(session, | |
4483 | snapshot_output->max_size); | |
4484 | if (nb_packets_per_stream < 0) { | |
4485 | ret = LTTNG_ERR_MAX_SIZE_INVALID; | |
4486 | goto end; | |
4487 | } | |
4488 | ||
4489 | if (session->kernel_session) { | |
4490 | ret = record_kernel_snapshot(session->kernel_session, | |
4491 | snapshot_output, session, | |
4492 | wait, nb_packets_per_stream); | |
4493 | if (ret != LTTNG_OK) { | |
4494 | goto end; | |
4495 | } | |
4496 | } | |
4497 | ||
4498 | if (session->ust_session) { | |
4499 | ret = record_ust_snapshot(session->ust_session, | |
4500 | snapshot_output, session, | |
4501 | wait, nb_packets_per_stream); | |
4502 | if (ret != LTTNG_OK) { | |
4503 | goto end; | |
4504 | } | |
4505 | } | |
e5148e25 JG |
4506 | |
4507 | if (session_close_trace_chunk(session, session->current_trace_chunk)) { | |
4508 | /* | |
4509 | * Don't goto end; make sure the chunk is closed for the session | |
4510 | * to allow future snapshots. | |
4511 | */ | |
4512 | ERR("Failed to close snapshot trace chunk of session \"%s\"", | |
4513 | session->name); | |
4514 | ret = -1; | |
4515 | } | |
4516 | if (session_set_trace_chunk(session, NULL, NULL)) { | |
4517 | ERR("Failed to release the current trace chunk of session \"%s\"", | |
4518 | session->name); | |
4519 | ret = -1; | |
4520 | } | |
65ff8ea3 JG |
4521 | end: |
4522 | return ret; | |
4523 | } | |
4524 | ||
6dc3064a DG |
4525 | /* |
4526 | * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl. | |
4527 | * | |
4528 | * The wait parameter is ignored so this call always wait for the snapshot to | |
4529 | * complete before returning. | |
4530 | * | |
4531 | * Return LTTNG_OK on success or else a LTTNG_ERR code. | |
4532 | */ | |
4533 | int cmd_snapshot_record(struct ltt_session *session, | |
4534 | struct lttng_snapshot_output *output, int wait) | |
4535 | { | |
8f07cd01 JG |
4536 | enum lttng_error_code cmd_ret = LTTNG_OK; |
4537 | int ret; | |
e1986656 DG |
4538 | unsigned int use_tmp_output = 0; |
4539 | struct snapshot_output tmp_output; | |
00e1dfc4 | 4540 | unsigned int snapshot_success = 0; |
10ba83fe | 4541 | char datetime[16]; |
6dc3064a DG |
4542 | |
4543 | assert(session); | |
ba45d9f0 | 4544 | assert(output); |
6dc3064a DG |
4545 | |
4546 | DBG("Cmd snapshot record for session %s", session->name); | |
4547 | ||
10ba83fe JR |
4548 | /* Get the datetime for the snapshot output directory. */ |
4549 | ret = utils_get_current_time_str("%Y%m%d-%H%M%S", datetime, | |
4550 | sizeof(datetime)); | |
4551 | if (!ret) { | |
8f07cd01 | 4552 | cmd_ret = LTTNG_ERR_INVALID; |
10ba83fe JR |
4553 | goto error; |
4554 | } | |
4555 | ||
6dc3064a | 4556 | /* |
d3f14b8a MD |
4557 | * Permission denied to create an output if the session is not |
4558 | * set in no output mode. | |
6dc3064a DG |
4559 | */ |
4560 | if (session->output_traces) { | |
8f07cd01 | 4561 | cmd_ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION; |
6dc3064a DG |
4562 | goto error; |
4563 | } | |
4564 | ||
4565 | /* The session needs to be started at least once. */ | |
8382cf6f | 4566 | if (!session->has_been_started) { |
8f07cd01 | 4567 | cmd_ret = LTTNG_ERR_START_SESSION_ONCE; |
6dc3064a DG |
4568 | goto error; |
4569 | } | |
4570 | ||
4571 | /* Use temporary output for the session. */ | |
ba45d9f0 | 4572 | if (*output->ctrl_url != '\0') { |
5feee503 JG |
4573 | ret = snapshot_output_init(session, output->max_size, |
4574 | output->name, | |
4575 | output->ctrl_url, output->data_url, | |
4576 | session->consumer, | |
e1986656 | 4577 | &tmp_output, NULL); |
6dc3064a DG |
4578 | if (ret < 0) { |
4579 | if (ret == -ENOMEM) { | |
8f07cd01 | 4580 | cmd_ret = LTTNG_ERR_NOMEM; |
6dc3064a | 4581 | } else { |
8f07cd01 | 4582 | cmd_ret = LTTNG_ERR_INVALID; |
6dc3064a DG |
4583 | } |
4584 | goto error; | |
4585 | } | |
1bfe7328 DG |
4586 | /* Use the global session count for the temporary snapshot. */ |
4587 | tmp_output.nb_snapshot = session->snapshot.nb_snapshot; | |
10ba83fe JR |
4588 | |
4589 | /* Use the global datetime */ | |
4590 | memcpy(tmp_output.datetime, datetime, sizeof(datetime)); | |
e1986656 | 4591 | use_tmp_output = 1; |
6dc3064a DG |
4592 | } |
4593 | ||
804c90a8 | 4594 | if (use_tmp_output) { |
65ff8ea3 JG |
4595 | cmd_ret = snapshot_record(session, &tmp_output, wait); |
4596 | if (cmd_ret != LTTNG_OK) { | |
804c90a8 JR |
4597 | goto error; |
4598 | } | |
804c90a8 JR |
4599 | snapshot_success = 1; |
4600 | } else { | |
4601 | struct snapshot_output *sout; | |
4602 | struct lttng_ht_iter iter; | |
68808f4e | 4603 | |
804c90a8 JR |
4604 | rcu_read_lock(); |
4605 | cds_lfht_for_each_entry(session->snapshot.output_ht->ht, | |
4606 | &iter.iter, sout, node.node) { | |
804c90a8 | 4607 | /* |
65ff8ea3 JG |
4608 | * Make a local copy of the output and assign the |
4609 | * possible temporary value given by the caller. | |
804c90a8 | 4610 | */ |
804c90a8 | 4611 | memcpy(&tmp_output, sout, sizeof(tmp_output)); |
1bfe7328 | 4612 | |
804c90a8 JR |
4613 | if (output->max_size != (uint64_t) -1ULL) { |
4614 | tmp_output.max_size = output->max_size; | |
6dc3064a | 4615 | } |
d07ceecd | 4616 | |
65ff8ea3 JG |
4617 | tmp_output.nb_snapshot = session->snapshot.nb_snapshot; |
4618 | memcpy(tmp_output.datetime, datetime, sizeof(datetime)); | |
6dc3064a | 4619 | |
804c90a8 JR |
4620 | /* Use temporary name. */ |
4621 | if (*output->name != '\0') { | |
cf3e357d MD |
4622 | if (lttng_strncpy(tmp_output.name, output->name, |
4623 | sizeof(tmp_output.name))) { | |
8f07cd01 | 4624 | cmd_ret = LTTNG_ERR_INVALID; |
cf3e357d MD |
4625 | rcu_read_unlock(); |
4626 | goto error; | |
4627 | } | |
804c90a8 | 4628 | } |
e1986656 | 4629 | |
65ff8ea3 JG |
4630 | cmd_ret = snapshot_record(session, &tmp_output, wait); |
4631 | if (cmd_ret != LTTNG_OK) { | |
4632 | rcu_read_unlock(); | |
4633 | goto error; | |
6dc3064a | 4634 | } |
804c90a8 | 4635 | snapshot_success = 1; |
6dc3064a | 4636 | } |
804c90a8 | 4637 | rcu_read_unlock(); |
6dc3064a DG |
4638 | } |
4639 | ||
1bfe7328 DG |
4640 | if (snapshot_success) { |
4641 | session->snapshot.nb_snapshot++; | |
b67578cb | 4642 | } else { |
8f07cd01 | 4643 | cmd_ret = LTTNG_ERR_SNAPSHOT_FAIL; |
1bfe7328 DG |
4644 | } |
4645 | ||
6dc3064a | 4646 | error: |
8f07cd01 | 4647 | return cmd_ret; |
6dc3064a DG |
4648 | } |
4649 | ||
d7ba1388 MD |
4650 | /* |
4651 | * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread. | |
4652 | */ | |
4653 | int cmd_set_session_shm_path(struct ltt_session *session, | |
4654 | const char *shm_path) | |
4655 | { | |
4656 | /* Safety net */ | |
4657 | assert(session); | |
4658 | ||
4659 | /* | |
4660 | * Can only set shm path before session is started. | |
4661 | */ | |
4662 | if (session->has_been_started) { | |
4663 | return LTTNG_ERR_SESSION_STARTED; | |
4664 | } | |
4665 | ||
4666 | strncpy(session->shm_path, shm_path, | |
4667 | sizeof(session->shm_path)); | |
4668 | session->shm_path[sizeof(session->shm_path) - 1] = '\0'; | |
4669 | ||
4670 | return 0; | |
4671 | } | |
4672 | ||
5c408ad8 JD |
4673 | /* |
4674 | * Command LTTNG_ROTATE_SESSION from the lttng-ctl library. | |
4675 | * | |
4676 | * Ask the consumer to rotate the session output directory. | |
4677 | * The session lock must be held. | |
4678 | * | |
01d7dcc5 | 4679 | * Returns LTTNG_OK on success or else a negative LTTng error code. |
5c408ad8 JD |
4680 | */ |
4681 | int cmd_rotate_session(struct ltt_session *session, | |
4682 | struct lttng_rotate_session_return *rotate_return) | |
4683 | { | |
4684 | int ret; | |
e5148e25 | 4685 | uint64_t ongoing_rotation_chunk_id; |
01d7dcc5 | 4686 | enum lttng_error_code cmd_ret = LTTNG_OK; |
e5148e25 JG |
4687 | struct lttng_trace_chunk *chunk_being_archived = NULL; |
4688 | struct lttng_trace_chunk *new_trace_chunk = NULL; | |
4689 | enum lttng_trace_chunk_status chunk_status; | |
5c408ad8 JD |
4690 | |
4691 | assert(session); | |
4692 | ||
4693 | if (!session->has_been_started) { | |
01d7dcc5 | 4694 | cmd_ret = LTTNG_ERR_START_SESSION_ONCE; |
d68c9a04 | 4695 | goto end; |
5c408ad8 JD |
4696 | } |
4697 | ||
5268a79b | 4698 | if (session->live_timer || !session->output_traces) { |
01d7dcc5 | 4699 | cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE; |
d68c9a04 | 4700 | goto end; |
5c408ad8 JD |
4701 | } |
4702 | ||
e5148e25 | 4703 | /* Unsupported feature in lttng-relayd before 2.11. */ |
5c408ad8 JD |
4704 | if (session->consumer->type == CONSUMER_DST_NET && |
4705 | (session->consumer->relay_major_version == 2 && | |
4706 | session->consumer->relay_minor_version < 11)) { | |
01d7dcc5 | 4707 | cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY; |
d68c9a04 | 4708 | goto end; |
5c408ad8 JD |
4709 | } |
4710 | ||
82528808 | 4711 | if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) { |
82528808 JG |
4712 | DBG("Refusing to launch a rotation; a rotation is already in progress for session %s", |
4713 | session->name); | |
01d7dcc5 | 4714 | cmd_ret = LTTNG_ERR_ROTATION_PENDING; |
d68c9a04 | 4715 | goto end; |
5c408ad8 JD |
4716 | } |
4717 | ||
4718 | /* | |
4719 | * After a stop, we only allow one rotation to occur, the other ones are | |
4720 | * useless until a new start. | |
4721 | */ | |
4722 | if (session->rotated_after_last_stop) { | |
4723 | DBG("Session \"%s\" was already rotated after stop, refusing rotation", | |
4724 | session->name); | |
01d7dcc5 | 4725 | cmd_ret = LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP; |
d68c9a04 | 4726 | goto end; |
5c408ad8 JD |
4727 | } |
4728 | ||
e5148e25 | 4729 | session->rotation_state = LTTNG_ROTATION_STATE_ONGOING; |
82528808 | 4730 | |
e5148e25 JG |
4731 | if (session->active) { |
4732 | new_trace_chunk = session_create_new_trace_chunk(session, | |
4733 | NULL, NULL); | |
4734 | if (!new_trace_chunk) { | |
4735 | cmd_ret = LTTNG_ERR_CREATE_DIR_FAIL; | |
4736 | goto error; | |
5c408ad8 | 4737 | } |
e5148e25 | 4738 | } |
d70d3b5f | 4739 | |
e5148e25 JG |
4740 | /* The current trace chunk becomes the chunk being archived. */ |
4741 | ret = session_set_trace_chunk(session, new_trace_chunk, | |
4742 | &chunk_being_archived); | |
4743 | if (ret) { | |
4744 | cmd_ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER; | |
5feee503 JG |
4745 | goto error; |
4746 | } | |
4747 | ||
e5148e25 JG |
4748 | assert(chunk_being_archived); |
4749 | chunk_status = lttng_trace_chunk_get_id(chunk_being_archived, | |
4750 | &ongoing_rotation_chunk_id); | |
4751 | assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK); | |
4752 | ||
4753 | chunk_status = lttng_trace_chunk_set_close_command( | |
4754 | chunk_being_archived, | |
4755 | LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED); | |
4756 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
4757 | cmd_ret = LTTNG_ERR_FATAL; | |
5feee503 JG |
4758 | goto error; |
4759 | } | |
4760 | ||
5c408ad8 | 4761 | if (session->kernel_session) { |
01d7dcc5 JG |
4762 | cmd_ret = kernel_rotate_session(session); |
4763 | if (cmd_ret != LTTNG_OK) { | |
d70d3b5f | 4764 | goto error; |
5c408ad8 JD |
4765 | } |
4766 | } | |
4767 | if (session->ust_session) { | |
01d7dcc5 JG |
4768 | cmd_ret = ust_app_rotate_session(session); |
4769 | if (cmd_ret != LTTNG_OK) { | |
d70d3b5f | 4770 | goto error; |
5c408ad8 | 4771 | } |
82528808 | 4772 | } |
17dd1232 | 4773 | |
e5148e25 JG |
4774 | ret = session_close_trace_chunk(session, chunk_being_archived); |
4775 | if (ret) { | |
4776 | cmd_ret = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER; | |
4777 | goto error; | |
4778 | } | |
4779 | ||
82528808 JG |
4780 | ret = timer_session_rotation_pending_check_start(session, |
4781 | DEFAULT_ROTATE_PENDING_TIMER); | |
4782 | if (ret) { | |
01d7dcc5 | 4783 | cmd_ret = LTTNG_ERR_UNK; |
d70d3b5f | 4784 | goto error; |
5c408ad8 JD |
4785 | } |
4786 | ||
4787 | if (!session->active) { | |
4788 | session->rotated_after_last_stop = true; | |
4789 | } | |
4790 | ||
4791 | if (rotate_return) { | |
e5148e25 | 4792 | rotate_return->rotation_id = ongoing_rotation_chunk_id; |
5c408ad8 JD |
4793 | } |
4794 | ||
e5148e25 JG |
4795 | session->chunk_being_archived = chunk_being_archived; |
4796 | chunk_being_archived = NULL; | |
d70d3b5f JG |
4797 | ret = notification_thread_command_session_rotation_ongoing( |
4798 | notification_thread_handle, | |
4799 | session->name, session->uid, session->gid, | |
e5148e25 | 4800 | ongoing_rotation_chunk_id); |
d70d3b5f JG |
4801 | if (ret != LTTNG_OK) { |
4802 | ERR("Failed to notify notification thread that a session rotation is ongoing for session %s", | |
4803 | session->name); | |
01d7dcc5 | 4804 | cmd_ret = ret; |
d70d3b5f JG |
4805 | } |
4806 | ||
82528808 | 4807 | DBG("Cmd rotate session %s, archive_id %" PRIu64 " sent", |
e5148e25 | 4808 | session->name, ongoing_rotation_chunk_id); |
5c408ad8 | 4809 | end: |
e5148e25 JG |
4810 | lttng_trace_chunk_put(new_trace_chunk); |
4811 | lttng_trace_chunk_put(chunk_being_archived); | |
01d7dcc5 | 4812 | ret = (cmd_ret == LTTNG_OK) ? cmd_ret : -((int) cmd_ret); |
5c408ad8 | 4813 | return ret; |
d70d3b5f | 4814 | error: |
d70d3b5f | 4815 | if (session_reset_rotation_state(session, |
e5148e25 | 4816 | LTTNG_ROTATION_STATE_ERROR)) { |
d70d3b5f JG |
4817 | ERR("Failed to reset rotation state of session \"%s\"", |
4818 | session->name); | |
4819 | } | |
4820 | goto end; | |
5c408ad8 JD |
4821 | } |
4822 | ||
4823 | /* | |
d68c9a04 | 4824 | * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library. |
5c408ad8 JD |
4825 | * |
4826 | * Check if the session has finished its rotation. | |
4827 | * | |
e5148e25 | 4828 | * Return LTTNG_OK on success or else an LTTNG_ERR code. |
5c408ad8 | 4829 | */ |
d68c9a04 JD |
4830 | int cmd_rotate_get_info(struct ltt_session *session, |
4831 | struct lttng_rotation_get_info_return *info_return, | |
4832 | uint64_t rotation_id) | |
5c408ad8 | 4833 | { |
e5148e25 JG |
4834 | enum lttng_error_code cmd_ret = LTTNG_OK; |
4835 | enum lttng_rotation_state rotation_state; | |
5c408ad8 | 4836 | |
d68c9a04 | 4837 | DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64, session->name, |
e5148e25 | 4838 | session->most_recent_chunk_id.value); |
5c408ad8 | 4839 | |
e5148e25 JG |
4840 | if (session->chunk_being_archived) { |
4841 | enum lttng_trace_chunk_status chunk_status; | |
4842 | uint64_t chunk_id; | |
4843 | ||
4844 | chunk_status = lttng_trace_chunk_get_id( | |
4845 | session->chunk_being_archived, | |
4846 | &chunk_id); | |
4847 | assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK); | |
4848 | ||
4849 | rotation_state = rotation_id == chunk_id ? | |
4850 | LTTNG_ROTATION_STATE_ONGOING : | |
4851 | LTTNG_ROTATION_STATE_EXPIRED; | |
4852 | } else { | |
4853 | if (session->last_archived_chunk_id.is_set && | |
4854 | rotation_id != session->last_archived_chunk_id.value) { | |
4855 | rotation_state = LTTNG_ROTATION_STATE_EXPIRED; | |
4856 | } else { | |
4857 | rotation_state = session->rotation_state; | |
4858 | } | |
5c408ad8 JD |
4859 | } |
4860 | ||
e5148e25 JG |
4861 | switch (rotation_state) { |
4862 | case LTTNG_ROTATION_STATE_NO_ROTATION: | |
4863 | DBG("Reporting that no rotation has occured within the lifetime of session \"%s\"", | |
4864 | session->name); | |
4865 | goto end; | |
4866 | case LTTNG_ROTATION_STATE_EXPIRED: | |
4867 | DBG("Reporting that the rotation state of rotation id %" PRIu64 " of session \"%s\" has expired", | |
4868 | rotation_id, session->name); | |
4869 | break; | |
d68c9a04 | 4870 | case LTTNG_ROTATION_STATE_ONGOING: |
e5148e25 | 4871 | DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is still pending", |
d68c9a04 JD |
4872 | rotation_id, session->name); |
4873 | break; | |
4874 | case LTTNG_ROTATION_STATE_COMPLETED: | |
dd73d57b | 4875 | { |
e5148e25 JG |
4876 | int fmt_ret; |
4877 | char *chunk_path; | |
dd73d57b JG |
4878 | char *current_tracing_path_reply; |
4879 | size_t current_tracing_path_reply_len; | |
4880 | ||
e5148e25 JG |
4881 | DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is completed", |
4882 | rotation_id, session->name); | |
4883 | ||
dd73d57b JG |
4884 | switch (session_get_consumer_destination_type(session)) { |
4885 | case CONSUMER_DST_LOCAL: | |
4886 | current_tracing_path_reply = | |
4887 | info_return->location.local.absolute_path; | |
4888 | current_tracing_path_reply_len = | |
4889 | sizeof(info_return->location.local.absolute_path); | |
4890 | info_return->location_type = | |
05f8afa9 | 4891 | (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL; |
dd73d57b JG |
4892 | break; |
4893 | case CONSUMER_DST_NET: | |
4894 | current_tracing_path_reply = | |
4895 | info_return->location.relay.relative_path; | |
4896 | current_tracing_path_reply_len = | |
4897 | sizeof(info_return->location.relay.relative_path); | |
4898 | /* Currently the only supported relay protocol. */ | |
4899 | info_return->location.relay.protocol = | |
05f8afa9 | 4900 | (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP; |
dd73d57b | 4901 | |
e5148e25 | 4902 | fmt_ret = lttng_strncpy(info_return->location.relay.host, |
dd73d57b JG |
4903 | session_get_net_consumer_hostname(session), |
4904 | sizeof(info_return->location.relay.host)); | |
e5148e25 JG |
4905 | if (fmt_ret) { |
4906 | ERR("Failed to copy host name to rotate_get_info reply"); | |
dd73d57b | 4907 | info_return->status = LTTNG_ROTATION_STATUS_ERROR; |
e5148e25 | 4908 | cmd_ret = LTTNG_ERR_SET_URL; |
dd73d57b JG |
4909 | goto end; |
4910 | } | |
4911 | ||
4912 | session_get_net_consumer_ports(session, | |
4913 | &info_return->location.relay.ports.control, | |
4914 | &info_return->location.relay.ports.data); | |
4915 | info_return->location_type = | |
05f8afa9 | 4916 | (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY; |
dd73d57b JG |
4917 | break; |
4918 | default: | |
4919 | abort(); | |
4920 | } | |
e5148e25 JG |
4921 | fmt_ret = asprintf(&chunk_path, |
4922 | "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY "/%s", | |
4923 | session_get_base_path(session), | |
4924 | session->last_archived_chunk_name); | |
4925 | if (fmt_ret == -1) { | |
4926 | PERROR("Failed to format the path of the last archived trace chunk"); | |
4927 | info_return->status = LTTNG_ROTATION_STATUS_ERROR; | |
4928 | cmd_ret = LTTNG_ERR_UNK; | |
4929 | goto end; | |
4930 | } | |
4931 | ||
4932 | fmt_ret = lttng_strncpy(current_tracing_path_reply, | |
4933 | chunk_path, current_tracing_path_reply_len); | |
4934 | free(chunk_path); | |
4935 | if (fmt_ret) { | |
4936 | ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply"); | |
d68c9a04 | 4937 | info_return->status = LTTNG_ROTATION_STATUS_ERROR; |
e5148e25 | 4938 | cmd_ret = LTTNG_ERR_UNK; |
5c408ad8 JD |
4939 | goto end; |
4940 | } | |
dd73d57b | 4941 | |
d68c9a04 | 4942 | break; |
dd73d57b | 4943 | } |
d68c9a04 | 4944 | case LTTNG_ROTATION_STATE_ERROR: |
e5148e25 | 4945 | DBG("Reporting that an error occurred during rotation %" PRIu64 " of session \"%s\"", |
d68c9a04 JD |
4946 | rotation_id, session->name); |
4947 | break; | |
4948 | default: | |
4949 | abort(); | |
5c408ad8 JD |
4950 | } |
4951 | ||
e5148e25 | 4952 | cmd_ret = LTTNG_OK; |
5c408ad8 | 4953 | end: |
e5148e25 JG |
4954 | info_return->status = (int32_t) rotation_state; |
4955 | return cmd_ret; | |
5c408ad8 JD |
4956 | } |
4957 | ||
259c2674 JD |
4958 | /* |
4959 | * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library. | |
4960 | * | |
4961 | * Configure the automatic rotation parameters. | |
66ea93b1 JG |
4962 | * 'activate' to true means activate the rotation schedule type with 'new_value'. |
4963 | * 'activate' to false means deactivate the rotation schedule and validate that | |
4964 | * 'new_value' has the same value as the currently active value. | |
259c2674 | 4965 | * |
66ea93b1 | 4966 | * Return 0 on success or else a positive LTTNG_ERR code. |
259c2674 JD |
4967 | */ |
4968 | int cmd_rotation_set_schedule(struct ltt_session *session, | |
66ea93b1 JG |
4969 | bool activate, enum lttng_rotation_schedule_type schedule_type, |
4970 | uint64_t new_value, | |
90936dcf | 4971 | struct notification_thread_handle *notification_thread_handle) |
259c2674 JD |
4972 | { |
4973 | int ret; | |
66ea93b1 | 4974 | uint64_t *parameter_value; |
259c2674 JD |
4975 | |
4976 | assert(session); | |
4977 | ||
4978 | DBG("Cmd rotate set schedule session %s", session->name); | |
4979 | ||
5268a79b | 4980 | if (session->live_timer || !session->output_traces) { |
66ea93b1 | 4981 | DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session"); |
259c2674 JD |
4982 | ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE; |
4983 | goto end; | |
4984 | } | |
4985 | ||
66ea93b1 JG |
4986 | switch (schedule_type) { |
4987 | case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD: | |
4988 | parameter_value = &session->rotate_size; | |
4989 | break; | |
4990 | case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC: | |
4991 | parameter_value = &session->rotate_timer_period; | |
4992 | if (new_value >= UINT_MAX) { | |
4993 | DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64 " > %u (UINT_MAX)", | |
4994 | new_value, UINT_MAX); | |
4995 | ret = LTTNG_ERR_INVALID; | |
4996 | goto end; | |
4997 | } | |
4998 | break; | |
4999 | default: | |
5000 | WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type"); | |
5001 | ret = LTTNG_ERR_INVALID; | |
259c2674 | 5002 | goto end; |
66ea93b1 JG |
5003 | } |
5004 | ||
5005 | /* Improper use of the API. */ | |
5006 | if (new_value == -1ULL) { | |
5007 | WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1"); | |
5008 | ret = LTTNG_ERR_INVALID; | |
259c2674 JD |
5009 | goto end; |
5010 | } | |
5011 | ||
66ea93b1 JG |
5012 | /* |
5013 | * As indicated in struct ltt_session's comments, a value of == 0 means | |
5014 | * this schedule rotation type is not in use. | |
5015 | * | |
5016 | * Reject the command if we were asked to activate a schedule that was | |
5017 | * already active. | |
5018 | */ | |
5019 | if (activate && *parameter_value != 0) { | |
5020 | DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active"); | |
5021 | ret = LTTNG_ERR_ROTATION_SCHEDULE_SET; | |
90936dcf | 5022 | goto end; |
66ea93b1 JG |
5023 | } |
5024 | ||
5025 | /* | |
5026 | * Reject the command if we were asked to deactivate a schedule that was | |
5027 | * not active. | |
5028 | */ | |
5029 | if (!activate && *parameter_value == 0) { | |
5030 | DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive"); | |
5031 | ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET; | |
90936dcf JD |
5032 | goto end; |
5033 | } | |
5034 | ||
66ea93b1 JG |
5035 | /* |
5036 | * Reject the command if we were asked to deactivate a schedule that | |
5037 | * doesn't exist. | |
5038 | */ | |
5039 | if (!activate && *parameter_value != new_value) { | |
5040 | DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided"); | |
5041 | ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET; | |
5042 | goto end; | |
5043 | } | |
259c2674 | 5044 | |
66ea93b1 JG |
5045 | *parameter_value = activate ? new_value : 0; |
5046 | ||
5047 | switch (schedule_type) { | |
5048 | case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC: | |
5049 | if (activate && session->active) { | |
5050 | /* | |
5051 | * Only start the timer if the session is active, | |
5052 | * otherwise it will be started when the session starts. | |
5053 | */ | |
82528808 JG |
5054 | ret = timer_session_rotation_schedule_timer_start( |
5055 | session, new_value); | |
259c2674 | 5056 | if (ret) { |
66ea93b1 | 5057 | ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command"); |
259c2674 JD |
5058 | ret = LTTNG_ERR_UNK; |
5059 | goto end; | |
5060 | } | |
66ea93b1 | 5061 | } else { |
82528808 JG |
5062 | ret = timer_session_rotation_schedule_timer_stop( |
5063 | session); | |
66ea93b1 JG |
5064 | if (ret) { |
5065 | ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command"); | |
5066 | ret = LTTNG_ERR_UNK; | |
f3ce6946 | 5067 | goto end; |
66ea93b1 | 5068 | } |
259c2674 | 5069 | } |
66ea93b1 JG |
5070 | break; |
5071 | case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD: | |
5072 | if (activate) { | |
5073 | ret = subscribe_session_consumed_size_rotation(session, | |
5074 | new_value, notification_thread_handle); | |
90936dcf | 5075 | if (ret) { |
66ea93b1 | 5076 | ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command"); |
90936dcf JD |
5077 | ret = LTTNG_ERR_UNK; |
5078 | goto end; | |
5079 | } | |
90936dcf | 5080 | } else { |
66ea93b1 JG |
5081 | ret = unsubscribe_session_consumed_size_rotation(session, |
5082 | notification_thread_handle); | |
90936dcf | 5083 | if (ret) { |
66ea93b1 | 5084 | ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command"); |
90936dcf JD |
5085 | ret = LTTNG_ERR_UNK; |
5086 | goto end; | |
5087 | } | |
66ea93b1 | 5088 | |
90936dcf | 5089 | } |
66ea93b1 JG |
5090 | break; |
5091 | default: | |
5092 | /* Would have been caught before. */ | |
5093 | abort(); | |
90936dcf JD |
5094 | } |
5095 | ||
259c2674 JD |
5096 | ret = LTTNG_OK; |
5097 | ||
5098 | goto end; | |
5099 | ||
5100 | end: | |
5101 | return ret; | |
5102 | } | |
5103 | ||
a503e1ef JG |
5104 | /* Wait for a given path to be removed before continuing. */ |
5105 | static enum lttng_error_code wait_on_path(void *path_data) | |
5106 | { | |
5107 | const char *shm_path = path_data; | |
5108 | ||
5109 | DBG("Waiting for the shm path at %s to be removed before completing session destruction", | |
5110 | shm_path); | |
5111 | while (true) { | |
5112 | int ret; | |
5113 | struct stat st; | |
5114 | ||
5115 | ret = stat(shm_path, &st); | |
5116 | if (ret) { | |
5117 | if (errno != ENOENT) { | |
5118 | PERROR("stat() returned an error while checking for the existence of the shm path"); | |
5119 | } else { | |
5120 | DBG("shm path no longer exists, completing the destruction of session"); | |
5121 | } | |
5122 | break; | |
5123 | } else { | |
5124 | if (!S_ISDIR(st.st_mode)) { | |
5125 | ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal", | |
5126 | shm_path); | |
5127 | break; | |
5128 | } | |
5129 | } | |
5130 | usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US); | |
5131 | } | |
5132 | return LTTNG_OK; | |
5133 | } | |
5134 | ||
5135 | /* | |
5136 | * Returns a pointer to a handler to run on completion of a command. | |
5137 | * Returns NULL if no handler has to be run for the last command executed. | |
5138 | */ | |
5139 | const struct cmd_completion_handler *cmd_pop_completion_handler(void) | |
5140 | { | |
5141 | struct cmd_completion_handler *handler = current_completion_handler; | |
5142 | ||
5143 | current_completion_handler = NULL; | |
5144 | return handler; | |
5145 | } | |
5146 | ||
2f77fc4b DG |
5147 | /* |
5148 | * Init command subsystem. | |
5149 | */ | |
5150 | void cmd_init(void) | |
5151 | { | |
5152 | /* | |
d88aee68 DG |
5153 | * Set network sequence index to 1 for streams to match a relayd |
5154 | * socket on the consumer side. | |
2f77fc4b | 5155 | */ |
d88aee68 DG |
5156 | pthread_mutex_lock(&relayd_net_seq_idx_lock); |
5157 | relayd_net_seq_idx = 1; | |
5158 | pthread_mutex_unlock(&relayd_net_seq_idx_lock); | |
2f77fc4b DG |
5159 | |
5160 | DBG("Command subsystem initialized"); | |
5161 | } |