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