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