Fix: ust: app stuck on recv message during UST comm timeout scenario
[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 * Don't try to enable a channel if the session has been started at
1391 * some point in time before. The tracer does not allow it.
1392 */
8382cf6f 1393 if (session->has_been_started) {
ecc48a90
JD
1394 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
1395 goto error;
1396 }
1397
1398 /*
1399 * If the session is a live session, remove the switch timer, the
1400 * live timer does the same thing but sends also synchronisation
1401 * beacons for inactive streams.
1402 */
1403 if (session->live_timer > 0) {
df4f5a87
JG
1404 attr.attr.live_timer_interval = session->live_timer;
1405 attr.attr.switch_timer_interval = 0;
ecc48a90
JD
1406 }
1407
6e21424e
JR
1408 /* Check for feature support */
1409 switch (domain->type) {
1410 case LTTNG_DOMAIN_KERNEL:
1411 {
7d268848 1412 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
6e21424e
JR
1413 /* Sampling position of buffer is not supported */
1414 WARN("Kernel tracer does not support buffer monitoring. "
1415 "Setting the monitor interval timer to 0 "
1416 "(disabled) for channel '%s' of session '%s'",
df4f5a87
JG
1417 attr.name, session->name);
1418 lttng_channel_set_monitor_timer_interval(&attr, 0);
6e21424e
JR
1419 }
1420 break;
1421 }
1422 case LTTNG_DOMAIN_UST:
f28f9e44 1423 break;
6e21424e
JR
1424 case LTTNG_DOMAIN_JUL:
1425 case LTTNG_DOMAIN_LOG4J:
1426 case LTTNG_DOMAIN_PYTHON:
f28f9e44
JG
1427 if (!agent_tracing_is_enabled()) {
1428 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1429 ret = LTTNG_ERR_AGENT_TRACING_DISABLED;
1430 goto error;
1431 }
6e21424e
JR
1432 break;
1433 default:
1434 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1435 goto error;
1436 }
1437
7972aab2 1438 switch (domain->type) {
2f77fc4b
DG
1439 case LTTNG_DOMAIN_KERNEL:
1440 {
1441 struct ltt_kernel_channel *kchan;
1442
df4f5a87 1443 kchan = trace_kernel_get_channel_by_name(attr.name,
2f77fc4b
DG
1444 session->kernel_session);
1445 if (kchan == NULL) {
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) {
df4f5a87
JG
1505 ret = channel_ust_create(usess, &attr, domain->buf_type);
1506 if (attr.name[0] != '\0') {
85076754
MD
1507 usess->has_non_default_channel = 1;
1508 }
2f77fc4b 1509 } else {
7972aab2 1510 ret = channel_ust_enable(usess, uchan);
2f77fc4b
DG
1511 }
1512 break;
1513 }
2f77fc4b 1514 default:
f73fabfd 1515 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2f77fc4b
DG
1516 goto error;
1517 }
1518
df4f5a87 1519 if (ret == LTTNG_OK && attr.attr.output != LTTNG_EVENT_MMAP) {
54213acc
JG
1520 session->has_non_mmap_channel = true;
1521 }
2f77fc4b 1522error:
2223c96f 1523 rcu_read_unlock();
1f345e94 1524end:
2f77fc4b
DG
1525 return ret;
1526}
1527
159b042f
JG
1528enum lttng_error_code cmd_process_attr_tracker_get_tracking_policy(
1529 struct ltt_session *session,
1530 enum lttng_domain_type domain,
1531 enum lttng_process_attr process_attr,
1532 enum lttng_tracking_policy *policy)
1533{
1534 enum lttng_error_code ret_code = LTTNG_OK;
1535 const struct process_attr_tracker *tracker;
1536
1537 switch (domain) {
1538 case LTTNG_DOMAIN_KERNEL:
1539 if (!session->kernel_session) {
1540 ret_code = LTTNG_ERR_INVALID;
1541 goto end;
1542 }
1543 tracker = kernel_get_process_attr_tracker(
1544 session->kernel_session, process_attr);
1545 break;
1546 case LTTNG_DOMAIN_UST:
1547 if (!session->ust_session) {
1548 ret_code = LTTNG_ERR_INVALID;
1549 goto end;
1550 }
1551 tracker = trace_ust_get_process_attr_tracker(
1552 session->ust_session, process_attr);
1553 break;
1554 default:
1555 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1556 goto end;
1557 }
1558 if (tracker) {
1559 *policy = process_attr_tracker_get_tracking_policy(tracker);
1560 } else {
1561 ret_code = LTTNG_ERR_INVALID;
1562 }
1563end:
1564 return ret_code;
1565}
1566
1567enum lttng_error_code cmd_process_attr_tracker_set_tracking_policy(
1568 struct ltt_session *session,
1569 enum lttng_domain_type domain,
1570 enum lttng_process_attr process_attr,
1571 enum lttng_tracking_policy policy)
1572{
1573 enum lttng_error_code ret_code = LTTNG_OK;
1574
1575 switch (policy) {
1576 case LTTNG_TRACKING_POLICY_INCLUDE_SET:
1577 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL:
1578 case LTTNG_TRACKING_POLICY_INCLUDE_ALL:
1579 break;
1580 default:
1581 ret_code = LTTNG_ERR_INVALID;
1582 goto end;
1583 }
1584
1585 switch (domain) {
1586 case LTTNG_DOMAIN_KERNEL:
1587 if (!session->kernel_session) {
1588 ret_code = LTTNG_ERR_INVALID;
1589 goto end;
1590 }
1591 ret_code = kernel_process_attr_tracker_set_tracking_policy(
1592 session->kernel_session, process_attr, policy);
1593 break;
1594 case LTTNG_DOMAIN_UST:
1595 if (!session->ust_session) {
1596 ret_code = LTTNG_ERR_INVALID;
1597 goto end;
1598 }
1599 ret_code = trace_ust_process_attr_tracker_set_tracking_policy(
1600 session->ust_session, process_attr, policy);
1601 break;
1602 default:
1603 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1604 break;
1605 }
1606end:
1607 return ret_code;
1608}
1609
1610enum lttng_error_code cmd_process_attr_tracker_inclusion_set_add_value(
1611 struct ltt_session *session,
1612 enum lttng_domain_type domain,
1613 enum lttng_process_attr process_attr,
1614 const struct process_attr_value *value)
1615{
1616 enum lttng_error_code ret_code = LTTNG_OK;
1617
1618 switch (domain) {
1619 case LTTNG_DOMAIN_KERNEL:
1620 if (!session->kernel_session) {
1621 ret_code = LTTNG_ERR_INVALID;
1622 goto end;
1623 }
1624 ret_code = kernel_process_attr_tracker_inclusion_set_add_value(
1625 session->kernel_session, process_attr, value);
1626 break;
1627 case LTTNG_DOMAIN_UST:
1628 if (!session->ust_session) {
1629 ret_code = LTTNG_ERR_INVALID;
1630 goto end;
1631 }
1632 ret_code = trace_ust_process_attr_tracker_inclusion_set_add_value(
1633 session->ust_session, process_attr, value);
1634 break;
1635 default:
1636 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1637 break;
1638 }
1639end:
1640 return ret_code;
1641}
1642
1643enum lttng_error_code cmd_process_attr_tracker_inclusion_set_remove_value(
1644 struct ltt_session *session,
1645 enum lttng_domain_type domain,
1646 enum lttng_process_attr process_attr,
1647 const struct process_attr_value *value)
1648{
1649 enum lttng_error_code ret_code = LTTNG_OK;
1650
1651 switch (domain) {
1652 case LTTNG_DOMAIN_KERNEL:
1653 if (!session->kernel_session) {
1654 ret_code = LTTNG_ERR_INVALID;
1655 goto end;
1656 }
1657 ret_code = kernel_process_attr_tracker_inclusion_set_remove_value(
1658 session->kernel_session, process_attr, value);
1659 break;
1660 case LTTNG_DOMAIN_UST:
1661 if (!session->ust_session) {
1662 ret_code = LTTNG_ERR_INVALID;
1663 goto end;
1664 }
1665 ret_code = trace_ust_process_attr_tracker_inclusion_set_remove_value(
1666 session->ust_session, process_attr, value);
1667 break;
1668 default:
1669 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1670 break;
1671 }
1672end:
1673 return ret_code;
1674}
1675
1676enum lttng_error_code cmd_process_attr_tracker_get_inclusion_set(
1677 struct ltt_session *session,
1678 enum lttng_domain_type domain,
1679 enum lttng_process_attr process_attr,
1680 struct lttng_process_attr_values **values)
1681{
1682 enum lttng_error_code ret_code = LTTNG_OK;
1683 const struct process_attr_tracker *tracker;
1684 enum process_attr_tracker_status status;
1685
1686 switch (domain) {
1687 case LTTNG_DOMAIN_KERNEL:
1688 if (!session->kernel_session) {
1689 ret_code = LTTNG_ERR_INVALID;
1690 goto end;
1691 }
1692 tracker = kernel_get_process_attr_tracker(
1693 session->kernel_session, process_attr);
1694 break;
1695 case LTTNG_DOMAIN_UST:
1696 if (!session->ust_session) {
1697 ret_code = LTTNG_ERR_INVALID;
1698 goto end;
1699 }
1700 tracker = trace_ust_get_process_attr_tracker(
1701 session->ust_session, process_attr);
1702 break;
1703 default:
1704 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1705 goto end;
1706 }
1707
1708 if (!tracker) {
1709 ret_code = LTTNG_ERR_INVALID;
1710 goto end;
1711 }
1712
1713 status = process_attr_tracker_get_inclusion_set(tracker, values);
1714 switch (status) {
1715 case PROCESS_ATTR_TRACKER_STATUS_OK:
1716 ret_code = LTTNG_OK;
1717 break;
1718 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY:
1719 ret_code = LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY;
1720 break;
1721 case PROCESS_ATTR_TRACKER_STATUS_ERROR:
1722 ret_code = LTTNG_ERR_NOMEM;
1723 break;
1724 default:
1725 ret_code = LTTNG_ERR_UNK;
1726 break;
1727 }
1728
1729end:
1730 return ret_code;
1731}
1732
2f77fc4b
DG
1733/*
1734 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1735 */
56a37563 1736int cmd_disable_event(struct ltt_session *session,
df4f5a87
JG
1737 enum lttng_domain_type domain, const char *channel_name,
1738 const struct lttng_event *event)
2f77fc4b
DG
1739{
1740 int ret;
df4f5a87 1741 const char *event_name;
6e911cad 1742
18a720cd
MD
1743 DBG("Disable event command for event \'%s\'", event->name);
1744
6e911cad
MD
1745 event_name = event->name;
1746
9b7431cf
JG
1747 /* Error out on unhandled search criteria */
1748 if (event->loglevel_type || event->loglevel != -1 || event->enabled
6e911cad 1749 || event->pid || event->filter || event->exclusion) {
7076b56e
JG
1750 ret = LTTNG_ERR_UNK;
1751 goto error;
6e911cad 1752 }
2f77fc4b 1753
2223c96f
DG
1754 rcu_read_lock();
1755
2f77fc4b
DG
1756 switch (domain) {
1757 case LTTNG_DOMAIN_KERNEL:
1758 {
1759 struct ltt_kernel_channel *kchan;
1760 struct ltt_kernel_session *ksess;
1761
1762 ksess = session->kernel_session;
1763
85076754
MD
1764 /*
1765 * If a non-default channel has been created in the
1766 * session, explicitely require that -c chan_name needs
1767 * to be provided.
1768 */
1769 if (ksess->has_non_default_channel && channel_name[0] == '\0') {
1770 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
7076b56e 1771 goto error_unlock;
85076754
MD
1772 }
1773
2f77fc4b
DG
1774 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
1775 if (kchan == NULL) {
f73fabfd 1776 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
7076b56e 1777 goto error_unlock;
2f77fc4b
DG
1778 }
1779
6e911cad
MD
1780 switch (event->type) {
1781 case LTTNG_EVENT_ALL:
9550ee81 1782 case LTTNG_EVENT_TRACEPOINT:
d0ae4ea8 1783 case LTTNG_EVENT_SYSCALL:
9550ee81
JR
1784 case LTTNG_EVENT_PROBE:
1785 case LTTNG_EVENT_FUNCTION:
1786 case LTTNG_EVENT_FUNCTION_ENTRY:/* fall-through */
1787 if (event_name[0] == '\0') {
1788 ret = event_kernel_disable_event(kchan,
1789 NULL, event->type);
29c62722 1790 } else {
d0ae4ea8 1791 ret = event_kernel_disable_event(kchan,
9550ee81 1792 event_name, event->type);
29c62722 1793 }
6e911cad 1794 if (ret != LTTNG_OK) {
7076b56e 1795 goto error_unlock;
6e911cad
MD
1796 }
1797 break;
6e911cad
MD
1798 default:
1799 ret = LTTNG_ERR_UNK;
7076b56e 1800 goto error_unlock;
2f77fc4b
DG
1801 }
1802
7d268848 1803 kernel_wait_quiescent();
2f77fc4b
DG
1804 break;
1805 }
1806 case LTTNG_DOMAIN_UST:
1807 {
1808 struct ltt_ust_channel *uchan;
1809 struct ltt_ust_session *usess;
1810
1811 usess = session->ust_session;
1812
7076b56e
JG
1813 if (validate_ust_event_name(event_name)) {
1814 ret = LTTNG_ERR_INVALID_EVENT_NAME;
1815 goto error_unlock;
1816 }
1817
85076754
MD
1818 /*
1819 * If a non-default channel has been created in the
9550ee81 1820 * session, explicitly require that -c chan_name needs
85076754
MD
1821 * to be provided.
1822 */
1823 if (usess->has_non_default_channel && channel_name[0] == '\0') {
1824 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
7076b56e 1825 goto error_unlock;
85076754
MD
1826 }
1827
2f77fc4b
DG
1828 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1829 channel_name);
1830 if (uchan == NULL) {
f73fabfd 1831 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
7076b56e 1832 goto error_unlock;
2f77fc4b
DG
1833 }
1834
6e911cad
MD
1835 switch (event->type) {
1836 case LTTNG_EVENT_ALL:
b3639870
JR
1837 /*
1838 * An empty event name means that everything
1839 * should be disabled.
1840 */
1841 if (event->name[0] == '\0') {
1842 ret = event_ust_disable_all_tracepoints(usess, uchan);
77d536b2
JR
1843 } else {
1844 ret = event_ust_disable_tracepoint(usess, uchan,
1845 event_name);
1846 }
6e911cad 1847 if (ret != LTTNG_OK) {
7076b56e 1848 goto error_unlock;
6e911cad
MD
1849 }
1850 break;
1851 default:
1852 ret = LTTNG_ERR_UNK;
7076b56e 1853 goto error_unlock;
2f77fc4b
DG
1854 }
1855
1856 DBG3("Disable UST event %s in channel %s completed", event_name,
1857 channel_name);
1858 break;
1859 }
5cdb6027 1860 case LTTNG_DOMAIN_LOG4J:
f20baf8e 1861 case LTTNG_DOMAIN_JUL:
0e115563 1862 case LTTNG_DOMAIN_PYTHON:
f20baf8e 1863 {
fefd409b 1864 struct agent *agt;
f20baf8e
DG
1865 struct ltt_ust_session *usess = session->ust_session;
1866
a0377dfe 1867 LTTNG_ASSERT(usess);
f20baf8e 1868
6e911cad
MD
1869 switch (event->type) {
1870 case LTTNG_EVENT_ALL:
1871 break;
1872 default:
1873 ret = LTTNG_ERR_UNK;
7076b56e 1874 goto error_unlock;
6e911cad
MD
1875 }
1876
5cdb6027 1877 agt = trace_ust_find_agent(usess, domain);
fefd409b
DG
1878 if (!agt) {
1879 ret = -LTTNG_ERR_UST_EVENT_NOT_FOUND;
7076b56e 1880 goto error_unlock;
fefd409b 1881 }
b3639870
JR
1882 /*
1883 * An empty event name means that everything
1884 * should be disabled.
1885 */
1886 if (event->name[0] == '\0') {
18a720cd
MD
1887 ret = event_agent_disable_all(usess, agt);
1888 } else {
1889 ret = event_agent_disable(usess, agt, event_name);
1890 }
f20baf8e 1891 if (ret != LTTNG_OK) {
7076b56e 1892 goto error_unlock;
f20baf8e
DG
1893 }
1894
1895 break;
1896 }
2f77fc4b 1897 default:
f73fabfd 1898 ret = LTTNG_ERR_UND;
7076b56e 1899 goto error_unlock;
2f77fc4b
DG
1900 }
1901
f73fabfd 1902 ret = LTTNG_OK;
2f77fc4b 1903
7076b56e 1904error_unlock:
2223c96f 1905 rcu_read_unlock();
7076b56e 1906error:
2f77fc4b
DG
1907 return ret;
1908}
1909
2f77fc4b
DG
1910/*
1911 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1912 */
56a37563 1913int cmd_add_context(struct ltt_session *session, enum lttng_domain_type domain,
df4f5a87 1914 char *channel_name, const struct lttng_event_context *ctx, int kwpipe)
2f77fc4b 1915{
d5979e4a 1916 int ret, chan_kern_created = 0, chan_ust_created = 0;
bdf64013
JG
1917 char *app_ctx_provider_name = NULL, *app_ctx_name = NULL;
1918
9a699f7b
JR
1919 /*
1920 * Don't try to add a context if the session has been started at
1921 * some point in time before. The tracer does not allow it and would
1922 * result in a corrupted trace.
1923 */
1924 if (session->has_been_started) {
1925 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
1926 goto end;
1927 }
1928
bdf64013
JG
1929 if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
1930 app_ctx_provider_name = ctx->u.app_ctx.provider_name;
1931 app_ctx_name = ctx->u.app_ctx.ctx_name;
1932 }
2f77fc4b
DG
1933
1934 switch (domain) {
1935 case LTTNG_DOMAIN_KERNEL:
a0377dfe 1936 LTTNG_ASSERT(session->kernel_session);
979e618e
DG
1937
1938 if (session->kernel_session->channel_count == 0) {
1939 /* Create default channel */
1940 ret = channel_kernel_create(session->kernel_session, NULL, kwpipe);
1941 if (ret != LTTNG_OK) {
1942 goto error;
1943 }
d5979e4a 1944 chan_kern_created = 1;
979e618e 1945 }
2f77fc4b 1946 /* Add kernel context to kernel tracer */
601d5acf 1947 ret = context_kernel_add(session->kernel_session, ctx, channel_name);
f73fabfd 1948 if (ret != LTTNG_OK) {
2f77fc4b
DG
1949 goto error;
1950 }
1951 break;
bdf64013
JG
1952 case LTTNG_DOMAIN_JUL:
1953 case LTTNG_DOMAIN_LOG4J:
1954 {
1955 /*
1956 * Validate channel name.
1957 * If no channel name is given and the domain is JUL or LOG4J,
1958 * set it to the appropriate domain-specific channel name. If
1959 * a name is provided but does not match the expexted channel
1960 * name, return an error.
1961 */
1962 if (domain == LTTNG_DOMAIN_JUL && *channel_name &&
1963 strcmp(channel_name,
1964 DEFAULT_JUL_CHANNEL_NAME)) {
1965 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1966 goto error;
1967 } else if (domain == LTTNG_DOMAIN_LOG4J && *channel_name &&
1968 strcmp(channel_name,
1969 DEFAULT_LOG4J_CHANNEL_NAME)) {
1970 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1971 goto error;
1972 }
a93b3916 1973 /* break is _not_ missing here. */
bdf64013 1974 }
2f77fc4b
DG
1975 case LTTNG_DOMAIN_UST:
1976 {
1977 struct ltt_ust_session *usess = session->ust_session;
85076754
MD
1978 unsigned int chan_count;
1979
a0377dfe 1980 LTTNG_ASSERT(usess);
2f77fc4b 1981
85076754 1982 chan_count = lttng_ht_get_count(usess->domain_global.channels);
979e618e
DG
1983 if (chan_count == 0) {
1984 struct lttng_channel *attr;
1985 /* Create default channel */
0a9c6494 1986 attr = channel_new_default_attr(domain, usess->buffer_type);
979e618e
DG
1987 if (attr == NULL) {
1988 ret = LTTNG_ERR_FATAL;
1989 goto error;
1990 }
1991
7972aab2 1992 ret = channel_ust_create(usess, attr, usess->buffer_type);
979e618e
DG
1993 if (ret != LTTNG_OK) {
1994 free(attr);
1995 goto error;
1996 }
cf0bcb51 1997 channel_attr_destroy(attr);
d5979e4a 1998 chan_ust_created = 1;
979e618e
DG
1999 }
2000
601d5acf 2001 ret = context_ust_add(usess, domain, ctx, channel_name);
bdf64013
JG
2002 free(app_ctx_provider_name);
2003 free(app_ctx_name);
2004 app_ctx_name = NULL;
2005 app_ctx_provider_name = NULL;
f73fabfd 2006 if (ret != LTTNG_OK) {
2f77fc4b
DG
2007 goto error;
2008 }
2009 break;
2010 }
2f77fc4b 2011 default:
f73fabfd 2012 ret = LTTNG_ERR_UND;
2f77fc4b
DG
2013 goto error;
2014 }
2015
bdf64013
JG
2016 ret = LTTNG_OK;
2017 goto end;
2f77fc4b
DG
2018
2019error:
d5979e4a
DG
2020 if (chan_kern_created) {
2021 struct ltt_kernel_channel *kchan =
2022 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME,
2023 session->kernel_session);
2024 /* Created previously, this should NOT fail. */
a0377dfe 2025 LTTNG_ASSERT(kchan);
d5979e4a
DG
2026 kernel_destroy_channel(kchan);
2027 }
2028
2029 if (chan_ust_created) {
2030 struct ltt_ust_channel *uchan =
2031 trace_ust_find_channel_by_name(
2032 session->ust_session->domain_global.channels,
2033 DEFAULT_CHANNEL_NAME);
2034 /* Created previously, this should NOT fail. */
a0377dfe 2035 LTTNG_ASSERT(uchan);
d5979e4a
DG
2036 /* Remove from the channel list of the session. */
2037 trace_ust_delete_channel(session->ust_session->domain_global.channels,
2038 uchan);
2039 trace_ust_destroy_channel(uchan);
2040 }
bdf64013
JG
2041end:
2042 free(app_ctx_provider_name);
2043 free(app_ctx_name);
2f77fc4b
DG
2044 return ret;
2045}
2046
dac8e046
JG
2047static inline bool name_starts_with(const char *name, const char *prefix)
2048{
2049 const size_t max_cmp_len = min(strlen(prefix), LTTNG_SYMBOL_NAME_LEN);
2050
2051 return !strncmp(name, prefix, max_cmp_len);
2052}
2053
2054/* Perform userspace-specific event name validation */
2055static int validate_ust_event_name(const char *name)
2056{
2057 int ret = 0;
2058
2059 if (!name) {
2060 ret = -1;
2061 goto end;
2062 }
2063
2064 /*
2065 * Check name against all internal UST event component namespaces used
2066 * by the agents.
2067 */
2068 if (name_starts_with(name, DEFAULT_JUL_EVENT_COMPONENT) ||
2069 name_starts_with(name, DEFAULT_LOG4J_EVENT_COMPONENT) ||
2070 name_starts_with(name, DEFAULT_PYTHON_EVENT_COMPONENT)) {
2071 ret = -1;
2072 }
2073
2074end:
2075 return ret;
2076}
88f06f15 2077
2f77fc4b 2078/*
88f06f15
JG
2079 * Internal version of cmd_enable_event() with a supplemental
2080 * "internal_event" flag which is used to enable internal events which should
2081 * be hidden from clients. Such events are used in the agent implementation to
2082 * enable the events through which all "agent" events are funeled.
2f77fc4b 2083 */
88f06f15 2084static int _cmd_enable_event(struct ltt_session *session,
df4f5a87 2085 const struct lttng_domain *domain,
025faf73 2086 char *channel_name, struct lttng_event *event,
6b453b5e 2087 char *filter_expression,
2b00d462 2088 struct lttng_bytecode *filter,
db8f1377 2089 struct lttng_event_exclusion *exclusion,
88f06f15 2090 int wpipe, bool internal_event)
2f77fc4b 2091{
9f449915 2092 int ret = 0, channel_created = 0;
cfedea03 2093 struct lttng_channel *attr = NULL;
2f77fc4b 2094
a0377dfe
FD
2095 LTTNG_ASSERT(session);
2096 LTTNG_ASSERT(event);
2097 LTTNG_ASSERT(channel_name);
2f77fc4b 2098
2a385866 2099 /* If we have a filter, we must have its filter expression */
a0377dfe 2100 LTTNG_ASSERT(!(!!filter_expression ^ !!filter));
2a385866 2101
9f449915
PP
2102 /* Normalize event name as a globbing pattern */
2103 strutils_normalize_star_glob_pattern(event->name);
18a720cd 2104
9f449915
PP
2105 /* Normalize exclusion names as globbing patterns */
2106 if (exclusion) {
2107 size_t i;
f5ac4bd7 2108
9f449915
PP
2109 for (i = 0; i < exclusion->count; i++) {
2110 char *name = LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, i);
2111
2112 strutils_normalize_star_glob_pattern(name);
2113 }
930a2e99
JG
2114 }
2115
9f449915
PP
2116 DBG("Enable event command for event \'%s\'", event->name);
2117
2118 rcu_read_lock();
2119
7972aab2 2120 switch (domain->type) {
2f77fc4b
DG
2121 case LTTNG_DOMAIN_KERNEL:
2122 {
2123 struct ltt_kernel_channel *kchan;
2124
85076754
MD
2125 /*
2126 * If a non-default channel has been created in the
2127 * session, explicitely require that -c chan_name needs
2128 * to be provided.
2129 */
2130 if (session->kernel_session->has_non_default_channel
2131 && channel_name[0] == '\0') {
2132 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
2133 goto error;
2134 }
2135
2f77fc4b
DG
2136 kchan = trace_kernel_get_channel_by_name(channel_name,
2137 session->kernel_session);
2138 if (kchan == NULL) {
0a9c6494
DG
2139 attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL,
2140 LTTNG_BUFFER_GLOBAL);
2f77fc4b 2141 if (attr == NULL) {
f73fabfd 2142 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
2143 goto error;
2144 }
04c17253
MD
2145 if (lttng_strncpy(attr->name, channel_name,
2146 sizeof(attr->name))) {
2147 ret = LTTNG_ERR_INVALID;
04c17253
MD
2148 goto error;
2149 }
2f77fc4b
DG
2150
2151 ret = cmd_enable_channel(session, domain, attr, wpipe);
f73fabfd 2152 if (ret != LTTNG_OK) {
2f77fc4b
DG
2153 goto error;
2154 }
e5f5db7f 2155 channel_created = 1;
2f77fc4b
DG
2156 }
2157
2158 /* Get the newly created kernel channel pointer */
2159 kchan = trace_kernel_get_channel_by_name(channel_name,
2160 session->kernel_session);
2161 if (kchan == NULL) {
2162 /* This sould not happen... */
f73fabfd 2163 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
2164 goto error;
2165 }
2166
6e911cad
MD
2167 switch (event->type) {
2168 case LTTNG_EVENT_ALL:
29c62722 2169 {
00a62084 2170 char *filter_expression_a = NULL;
2b00d462 2171 struct lttng_bytecode *filter_a = NULL;
00a62084
MD
2172
2173 /*
2174 * We need to duplicate filter_expression and filter,
2175 * because ownership is passed to first enable
2176 * event.
2177 */
2178 if (filter_expression) {
2179 filter_expression_a = strdup(filter_expression);
2180 if (!filter_expression_a) {
2181 ret = LTTNG_ERR_FATAL;
2182 goto error;
2183 }
2184 }
2185 if (filter) {
2186 filter_a = zmalloc(sizeof(*filter_a) + filter->len);
2187 if (!filter_a) {
2188 free(filter_expression_a);
2189 ret = LTTNG_ERR_FATAL;
2190 goto error;
2191 }
2192 memcpy(filter_a, filter, sizeof(*filter_a) + filter->len);
2193 }
29c62722 2194 event->type = LTTNG_EVENT_TRACEPOINT; /* Hack */
00a62084
MD
2195 ret = event_kernel_enable_event(kchan, event,
2196 filter_expression, filter);
a969e101
MD
2197 /* We have passed ownership */
2198 filter_expression = NULL;
2199 filter = NULL;
29c62722
MD
2200 if (ret != LTTNG_OK) {
2201 if (channel_created) {
2202 /* Let's not leak a useless channel. */
2203 kernel_destroy_channel(kchan);
2204 }
00a62084
MD
2205 free(filter_expression_a);
2206 free(filter_a);
29c62722
MD
2207 goto error;
2208 }
2209 event->type = LTTNG_EVENT_SYSCALL; /* Hack */
00a62084
MD
2210 ret = event_kernel_enable_event(kchan, event,
2211 filter_expression_a, filter_a);
60d21fa2
AB
2212 /* We have passed ownership */
2213 filter_expression_a = NULL;
2214 filter_a = NULL;
29c62722
MD
2215 if (ret != LTTNG_OK) {
2216 goto error;
2217 }
2218 break;
2219 }
6e6ef3d7 2220 case LTTNG_EVENT_PROBE:
dcabc190 2221 case LTTNG_EVENT_USERSPACE_PROBE:
6e6ef3d7
DG
2222 case LTTNG_EVENT_FUNCTION:
2223 case LTTNG_EVENT_FUNCTION_ENTRY:
6e911cad 2224 case LTTNG_EVENT_TRACEPOINT:
00a62084
MD
2225 ret = event_kernel_enable_event(kchan, event,
2226 filter_expression, filter);
a969e101
MD
2227 /* We have passed ownership */
2228 filter_expression = NULL;
2229 filter = NULL;
6e911cad
MD
2230 if (ret != LTTNG_OK) {
2231 if (channel_created) {
2232 /* Let's not leak a useless channel. */
2233 kernel_destroy_channel(kchan);
2234 }
2235 goto error;
e5f5db7f 2236 }
6e911cad
MD
2237 break;
2238 case LTTNG_EVENT_SYSCALL:
00a62084
MD
2239 ret = event_kernel_enable_event(kchan, event,
2240 filter_expression, filter);
a969e101
MD
2241 /* We have passed ownership */
2242 filter_expression = NULL;
2243 filter = NULL;
e2b957af
MD
2244 if (ret != LTTNG_OK) {
2245 goto error;
2246 }
6e911cad
MD
2247 break;
2248 default:
2249 ret = LTTNG_ERR_UNK;
2f77fc4b
DG
2250 goto error;
2251 }
2252
7d268848 2253 kernel_wait_quiescent();
2f77fc4b
DG
2254 break;
2255 }
2256 case LTTNG_DOMAIN_UST:
2257 {
2258 struct ltt_ust_channel *uchan;
2259 struct ltt_ust_session *usess = session->ust_session;
2260
a0377dfe 2261 LTTNG_ASSERT(usess);
2f77fc4b 2262
85076754
MD
2263 /*
2264 * If a non-default channel has been created in the
2265 * session, explicitely require that -c chan_name needs
2266 * to be provided.
2267 */
2268 if (usess->has_non_default_channel && channel_name[0] == '\0') {
2269 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
2270 goto error;
2271 }
2272
2f77fc4b
DG
2273 /* Get channel from global UST domain */
2274 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
2275 channel_name);
2276 if (uchan == NULL) {
2277 /* Create default channel */
0a9c6494
DG
2278 attr = channel_new_default_attr(LTTNG_DOMAIN_UST,
2279 usess->buffer_type);
2f77fc4b 2280 if (attr == NULL) {
f73fabfd 2281 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
2282 goto error;
2283 }
04c17253
MD
2284 if (lttng_strncpy(attr->name, channel_name,
2285 sizeof(attr->name))) {
2286 ret = LTTNG_ERR_INVALID;
04c17253
MD
2287 goto error;
2288 }
2f77fc4b
DG
2289
2290 ret = cmd_enable_channel(session, domain, attr, wpipe);
f73fabfd 2291 if (ret != LTTNG_OK) {
2f77fc4b
DG
2292 goto error;
2293 }
2f77fc4b
DG
2294
2295 /* Get the newly created channel reference back */
2296 uchan = trace_ust_find_channel_by_name(
2297 usess->domain_global.channels, channel_name);
a0377dfe 2298 LTTNG_ASSERT(uchan);
2f77fc4b
DG
2299 }
2300
141feb8c
JG
2301 if (uchan->domain != LTTNG_DOMAIN_UST && !internal_event) {
2302 /*
2303 * Don't allow users to add UST events to channels which
2304 * are assigned to a userspace subdomain (JUL, Log4J,
2305 * Python, etc.).
2306 */
2307 ret = LTTNG_ERR_INVALID_CHANNEL_DOMAIN;
2308 goto error;
2309 }
2310
dac8e046
JG
2311 if (!internal_event) {
2312 /*
2313 * Ensure the event name is not reserved for internal
2314 * use.
2315 */
2316 ret = validate_ust_event_name(event->name);
2317 if (ret) {
0e270a1e 2318 WARN("Userspace event name %s failed validation.",
bbcab087 2319 event->name);
dac8e046
JG
2320 ret = LTTNG_ERR_INVALID_EVENT_NAME;
2321 goto error;
2322 }
2323 }
2324
2f77fc4b 2325 /* At this point, the session and channel exist on the tracer */
6b453b5e 2326 ret = event_ust_enable_tracepoint(usess, uchan, event,
88f06f15
JG
2327 filter_expression, filter, exclusion,
2328 internal_event);
49d21f93
MD
2329 /* We have passed ownership */
2330 filter_expression = NULL;
2331 filter = NULL;
2332 exclusion = NULL;
94382e15
JG
2333 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2334 goto already_enabled;
2335 } else if (ret != LTTNG_OK) {
2f77fc4b
DG
2336 goto error;
2337 }
2338 break;
2339 }
5cdb6027 2340 case LTTNG_DOMAIN_LOG4J:
f20baf8e 2341 case LTTNG_DOMAIN_JUL:
0e115563 2342 case LTTNG_DOMAIN_PYTHON:
f20baf8e 2343 {
da6c3a50 2344 const char *default_event_name, *default_chan_name;
fefd409b 2345 struct agent *agt;
f20baf8e
DG
2346 struct lttng_event uevent;
2347 struct lttng_domain tmp_dom;
2348 struct ltt_ust_session *usess = session->ust_session;
2349
a0377dfe 2350 LTTNG_ASSERT(usess);
f20baf8e 2351
f28f9e44
JG
2352 if (!agent_tracing_is_enabled()) {
2353 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2354 ret = LTTNG_ERR_AGENT_TRACING_DISABLED;
2355 goto error;
2356 }
2357
5cdb6027 2358 agt = trace_ust_find_agent(usess, domain->type);
fefd409b 2359 if (!agt) {
5cdb6027 2360 agt = agent_create(domain->type);
fefd409b 2361 if (!agt) {
e5b3c48c 2362 ret = LTTNG_ERR_NOMEM;
fefd409b
DG
2363 goto error;
2364 }
2365 agent_add(agt, usess->agents);
2366 }
2367
022d91ba 2368 /* Create the default tracepoint. */
996de3c7 2369 memset(&uevent, 0, sizeof(uevent));
f20baf8e
DG
2370 uevent.type = LTTNG_EVENT_TRACEPOINT;
2371 uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
51755dc8
JG
2372 default_event_name = event_get_default_agent_ust_name(
2373 domain->type);
da6c3a50 2374 if (!default_event_name) {
e5b3c48c 2375 ret = LTTNG_ERR_FATAL;
da6c3a50 2376 goto error;
f43f95a9 2377 }
da6c3a50 2378 strncpy(uevent.name, default_event_name, sizeof(uevent.name));
f20baf8e
DG
2379 uevent.name[sizeof(uevent.name) - 1] = '\0';
2380
2381 /*
2382 * The domain type is changed because we are about to enable the
2383 * default channel and event for the JUL domain that are hardcoded.
2384 * This happens in the UST domain.
2385 */
2386 memcpy(&tmp_dom, domain, sizeof(tmp_dom));
2387 tmp_dom.type = LTTNG_DOMAIN_UST;
2388
0e115563
DG
2389 switch (domain->type) {
2390 case LTTNG_DOMAIN_LOG4J:
da6c3a50 2391 default_chan_name = DEFAULT_LOG4J_CHANNEL_NAME;
0e115563
DG
2392 break;
2393 case LTTNG_DOMAIN_JUL:
da6c3a50 2394 default_chan_name = DEFAULT_JUL_CHANNEL_NAME;
0e115563
DG
2395 break;
2396 case LTTNG_DOMAIN_PYTHON:
2397 default_chan_name = DEFAULT_PYTHON_CHANNEL_NAME;
2398 break;
2399 default:
e98a44b0 2400 /* The switch/case we are in makes this impossible */
a0377dfe 2401 abort();
da6c3a50
DG
2402 }
2403
971da06a 2404 {
8404118c 2405 char *filter_expression_copy = NULL;
2b00d462 2406 struct lttng_bytecode *filter_copy = NULL;
971da06a
JG
2407
2408 if (filter) {
51755dc8 2409 const size_t filter_size = sizeof(
2b00d462 2410 struct lttng_bytecode)
51755dc8
JG
2411 + filter->len;
2412
2413 filter_copy = zmalloc(filter_size);
971da06a 2414 if (!filter_copy) {
018096a4 2415 ret = LTTNG_ERR_NOMEM;
b742e3e2 2416 goto error;
971da06a 2417 }
51755dc8 2418 memcpy(filter_copy, filter, filter_size);
971da06a 2419
8404118c
JG
2420 filter_expression_copy =
2421 strdup(filter_expression);
2422 if (!filter_expression) {
2423 ret = LTTNG_ERR_NOMEM;
51755dc8
JG
2424 }
2425
2426 if (!filter_expression_copy || !filter_copy) {
2427 free(filter_expression_copy);
2428 free(filter_copy);
2429 goto error;
8404118c 2430 }
971da06a
JG
2431 }
2432
88f06f15 2433 ret = cmd_enable_event_internal(session, &tmp_dom,
971da06a 2434 (char *) default_chan_name,
8404118c
JG
2435 &uevent, filter_expression_copy,
2436 filter_copy, NULL, wpipe);
971da06a
JG
2437 }
2438
94382e15
JG
2439 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2440 goto already_enabled;
2441 } else if (ret != LTTNG_OK) {
f20baf8e
DG
2442 goto error;
2443 }
2444
2445 /* The wild card * means that everything should be enabled. */
2446 if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) {
8404118c
JG
2447 ret = event_agent_enable_all(usess, agt, event, filter,
2448 filter_expression);
f20baf8e 2449 } else {
8404118c
JG
2450 ret = event_agent_enable(usess, agt, event, filter,
2451 filter_expression);
f20baf8e 2452 }
51755dc8
JG
2453 filter = NULL;
2454 filter_expression = NULL;
f20baf8e
DG
2455 if (ret != LTTNG_OK) {
2456 goto error;
2457 }
2458
2459 break;
2460 }
2f77fc4b 2461 default:
f73fabfd 2462 ret = LTTNG_ERR_UND;
2f77fc4b
DG
2463 goto error;
2464 }
2465
f73fabfd 2466 ret = LTTNG_OK;
2f77fc4b 2467
94382e15 2468already_enabled:
2f77fc4b 2469error:
49d21f93
MD
2470 free(filter_expression);
2471 free(filter);
2472 free(exclusion);
cf0bcb51 2473 channel_attr_destroy(attr);
2223c96f 2474 rcu_read_unlock();
2f77fc4b
DG
2475 return ret;
2476}
2477
88f06f15
JG
2478/*
2479 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2480 * We own filter, exclusion, and filter_expression.
2481 */
df4f5a87
JG
2482int cmd_enable_event(struct ltt_session *session,
2483 const struct lttng_domain *domain,
88f06f15
JG
2484 char *channel_name, struct lttng_event *event,
2485 char *filter_expression,
2b00d462 2486 struct lttng_bytecode *filter,
88f06f15
JG
2487 struct lttng_event_exclusion *exclusion,
2488 int wpipe)
2489{
2490 return _cmd_enable_event(session, domain, channel_name, event,
2491 filter_expression, filter, exclusion, wpipe, false);
2492}
2493
2494/*
2495 * Enable an event which is internal to LTTng. An internal should
2496 * never be made visible to clients and are immune to checks such as
2497 * reserved names.
2498 */
2499static int cmd_enable_event_internal(struct ltt_session *session,
df4f5a87 2500 const struct lttng_domain *domain,
88f06f15
JG
2501 char *channel_name, struct lttng_event *event,
2502 char *filter_expression,
2b00d462 2503 struct lttng_bytecode *filter,
88f06f15
JG
2504 struct lttng_event_exclusion *exclusion,
2505 int wpipe)
2506{
2507 return _cmd_enable_event(session, domain, channel_name, event,
2508 filter_expression, filter, exclusion, wpipe, true);
2509}
2510
2f77fc4b
DG
2511/*
2512 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2513 */
56a37563
JG
2514ssize_t cmd_list_tracepoints(enum lttng_domain_type domain,
2515 struct lttng_event **events)
2f77fc4b
DG
2516{
2517 int ret;
2518 ssize_t nb_events = 0;
2519
2520 switch (domain) {
2521 case LTTNG_DOMAIN_KERNEL:
7d268848 2522 nb_events = kernel_list_events(events);
2f77fc4b 2523 if (nb_events < 0) {
f73fabfd 2524 ret = LTTNG_ERR_KERN_LIST_FAIL;
2f77fc4b
DG
2525 goto error;
2526 }
2527 break;
2528 case LTTNG_DOMAIN_UST:
2529 nb_events = ust_app_list_events(events);
2530 if (nb_events < 0) {
f73fabfd 2531 ret = LTTNG_ERR_UST_LIST_FAIL;
2f77fc4b
DG
2532 goto error;
2533 }
2534 break;
5cdb6027 2535 case LTTNG_DOMAIN_LOG4J:
3c6a091f 2536 case LTTNG_DOMAIN_JUL:
0e115563 2537 case LTTNG_DOMAIN_PYTHON:
f60140a1 2538 nb_events = agent_list_events(events, domain);
3c6a091f
DG
2539 if (nb_events < 0) {
2540 ret = LTTNG_ERR_UST_LIST_FAIL;
2541 goto error;
2542 }
2543 break;
2f77fc4b 2544 default:
f73fabfd 2545 ret = LTTNG_ERR_UND;
2f77fc4b
DG
2546 goto error;
2547 }
2548
2549 return nb_events;
2550
2551error:
2552 /* Return negative value to differentiate return code */
2553 return -ret;
2554}
2555
2556/*
2557 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2558 */
56a37563 2559ssize_t cmd_list_tracepoint_fields(enum lttng_domain_type domain,
2f77fc4b
DG
2560 struct lttng_event_field **fields)
2561{
2562 int ret;
2563 ssize_t nb_fields = 0;
2564
2565 switch (domain) {
2566 case LTTNG_DOMAIN_UST:
2567 nb_fields = ust_app_list_event_fields(fields);
2568 if (nb_fields < 0) {
f73fabfd 2569 ret = LTTNG_ERR_UST_LIST_FAIL;
2f77fc4b
DG
2570 goto error;
2571 }
2572 break;
2573 case LTTNG_DOMAIN_KERNEL:
2574 default: /* fall-through */
f73fabfd 2575 ret = LTTNG_ERR_UND;
2f77fc4b
DG
2576 goto error;
2577 }
2578
2579 return nb_fields;
2580
2581error:
2582 /* Return negative value to differentiate return code */
2583 return -ret;
2584}
2585
834978fd
DG
2586ssize_t cmd_list_syscalls(struct lttng_event **events)
2587{
2588 return syscall_table_list(events);
2589}
2590
2f77fc4b
DG
2591/*
2592 * Command LTTNG_START_TRACE processed by the client thread.
a9ad0c8f
MD
2593 *
2594 * Called with session mutex held.
2f77fc4b
DG
2595 */
2596int cmd_start_trace(struct ltt_session *session)
2597{
82b69413 2598 enum lttng_error_code ret;
cde3e505 2599 unsigned long nb_chan = 0;
2f77fc4b
DG
2600 struct ltt_kernel_session *ksession;
2601 struct ltt_ust_session *usess;
1f496244
JG
2602 const bool session_rotated_after_last_stop =
2603 session->rotated_after_last_stop;
b02f5986
MD
2604 const bool session_cleared_after_last_stop =
2605 session->cleared_after_last_stop;
2f77fc4b 2606
a0377dfe 2607 LTTNG_ASSERT(session);
2f77fc4b
DG
2608
2609 /* Ease our life a bit ;) */
2610 ksession = session->kernel_session;
2611 usess = session->ust_session;
2612
8382cf6f
DG
2613 /* Is the session already started? */
2614 if (session->active) {
f73fabfd 2615 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
7a24ece3
JR
2616 /* Perform nothing */
2617 goto end;
2f77fc4b
DG
2618 }
2619
1f496244
JG
2620 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING &&
2621 !session->current_trace_chunk) {
2622 /*
2623 * A rotation was launched while the session was stopped and
2624 * it has not been completed yet. It is not possible to start
2625 * the session since starting the session here would require a
2626 * rotation from "NULL" to a new trace chunk. That rotation
2627 * would overlap with the ongoing rotation, which is not
2628 * supported.
2629 */
2630 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2631 session->name);
2632 ret = LTTNG_ERR_ROTATION_PENDING;
2633 goto error;
2634 }
2635
cde3e505
DG
2636 /*
2637 * Starting a session without channel is useless since after that it's not
2638 * possible to enable channel thus inform the client.
2639 */
2640 if (usess && usess->domain_global.channels) {
2641 nb_chan += lttng_ht_get_count(usess->domain_global.channels);
2642 }
2643 if (ksession) {
2644 nb_chan += ksession->channel_count;
2645 }
2646 if (!nb_chan) {
2647 ret = LTTNG_ERR_NO_CHANNEL;
2648 goto error;
2649 }
2650
1f496244
JG
2651 session->active = 1;
2652 session->rotated_after_last_stop = false;
b02f5986 2653 session->cleared_after_last_stop = false;
070b6a86 2654 if (session->output_traces && !session->current_trace_chunk) {
1f496244
JG
2655 if (!session->has_been_started) {
2656 struct lttng_trace_chunk *trace_chunk;
2657
2658 DBG("Creating initial trace chunk of session \"%s\"",
2659 session->name);
2660 trace_chunk = session_create_new_trace_chunk(
2661 session, NULL, NULL, NULL);
2662 if (!trace_chunk) {
2663 ret = LTTNG_ERR_CREATE_DIR_FAIL;
2664 goto error;
2665 }
a0377dfe 2666 LTTNG_ASSERT(!session->current_trace_chunk);
1f496244
JG
2667 ret = session_set_trace_chunk(session, trace_chunk,
2668 NULL);
2669 lttng_trace_chunk_put(trace_chunk);
2670 if (ret) {
2671 ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
2672 goto error;
2673 }
2674 } else {
2675 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2676 session->name);
2677 /*
2678 * Rotate existing streams into the new chunk.
2679 * This is a "quiet" rotation has no client has
2680 * explicitly requested this operation.
2681 *
2682 * There is also no need to wait for the rotation
2683 * to complete as it will happen immediately. No data
2684 * was produced as the session was stopped, so the
2685 * rotation should happen on reception of the command.
2686 */
343defc2
MD
2687 ret = cmd_rotate_session(session, NULL, true,
2688 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION);
1f496244
JG
2689 if (ret != LTTNG_OK) {
2690 goto error;
2691 }
5c408ad8 2692 }
c996624c
JD
2693 }
2694
2f77fc4b
DG
2695 /* Kernel tracing */
2696 if (ksession != NULL) {
c996624c 2697 DBG("Start kernel tracing session %s", session->name);
7d268848 2698 ret = start_kernel_session(ksession);
9b6c7ec5 2699 if (ret != LTTNG_OK) {
2f77fc4b
DG
2700 goto error;
2701 }
2f77fc4b
DG
2702 }
2703
2704 /* Flag session that trace should start automatically */
2705 if (usess) {
82b69413
JG
2706 int int_ret = ust_app_start_trace_all(usess);
2707
2708 if (int_ret < 0) {
f73fabfd 2709 ret = LTTNG_ERR_UST_START_FAIL;
2f77fc4b
DG
2710 goto error;
2711 }
2712 }
2713
04ed9e10
JG
2714 /*
2715 * Open a packet in every stream of the session to ensure that viewers
2716 * can correctly identify the boundaries of the periods during which
2717 * tracing was active for this session.
2718 */
2719 ret = session_open_packets(session);
2720 if (ret != LTTNG_OK) {
2721 goto error;
2722 }
2723
5c408ad8
JD
2724 /*
2725 * Clear the flag that indicates that a rotation was done while the
2726 * session was stopped.
2727 */
2728 session->rotated_after_last_stop = false;
2729
259c2674 2730 if (session->rotate_timer_period) {
82b69413
JG
2731 int int_ret = timer_session_rotation_schedule_timer_start(
2732 session, session->rotate_timer_period);
2733
2734 if (int_ret < 0) {
259c2674
JD
2735 ERR("Failed to enable rotate timer");
2736 ret = LTTNG_ERR_UNK;
2737 goto error;
2738 }
2739 }
2740
f73fabfd 2741 ret = LTTNG_OK;
2f77fc4b
DG
2742
2743error:
1f496244
JG
2744 if (ret == LTTNG_OK) {
2745 /* Flag this after a successful start. */
2746 session->has_been_started |= 1;
2747 } else {
2748 session->active = 0;
2749 /* Restore initial state on error. */
2750 session->rotated_after_last_stop =
2751 session_rotated_after_last_stop;
b02f5986
MD
2752 session->cleared_after_last_stop =
2753 session_cleared_after_last_stop;
1f496244 2754 }
7a24ece3 2755end:
2f77fc4b
DG
2756 return ret;
2757}
2758
2759/*
2760 * Command LTTNG_STOP_TRACE processed by the client thread.
2761 */
2762int cmd_stop_trace(struct ltt_session *session)
2763{
2764 int ret;
2f77fc4b
DG
2765 struct ltt_kernel_session *ksession;
2766 struct ltt_ust_session *usess;
2767
a0377dfe 2768 LTTNG_ASSERT(session);
2f77fc4b 2769
4dbe1875 2770 DBG("Begin stop session \"%s\" (id %" PRIu64 ")", session->name, session->id);
2f77fc4b
DG
2771 /* Short cut */
2772 ksession = session->kernel_session;
2773 usess = session->ust_session;
2774
8382cf6f
DG
2775 /* Session is not active. Skip everythong and inform the client. */
2776 if (!session->active) {
f73fabfd 2777 ret = LTTNG_ERR_TRACE_ALREADY_STOPPED;
2f77fc4b
DG
2778 goto error;
2779 }
2780
4dbe1875
MD
2781 ret = stop_kernel_session(ksession);
2782 if (ret != LTTNG_OK) {
2783 goto error;
2f77fc4b
DG
2784 }
2785
14fb1ebe 2786 if (usess && usess->active) {
2f77fc4b
DG
2787 ret = ust_app_stop_trace_all(usess);
2788 if (ret < 0) {
f73fabfd 2789 ret = LTTNG_ERR_UST_STOP_FAIL;
2f77fc4b
DG
2790 goto error;
2791 }
2792 }
2793
4dbe1875
MD
2794 DBG("Completed stop session \"%s\" (id %" PRIu64 ")", session->name,
2795 session->id);
8382cf6f
DG
2796 /* Flag inactive after a successful stop. */
2797 session->active = 0;
4dbe1875 2798 ret = LTTNG_OK;
2f77fc4b
DG
2799
2800error:
2801 return ret;
2802}
2803
2804/*
433f5ba9
JR
2805 * Set the base_path of the session only if subdir of a control uris is set.
2806 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2f77fc4b 2807 */
433f5ba9
JR
2808static int set_session_base_path_from_uris(struct ltt_session *session,
2809 size_t nb_uri,
bda32d56 2810 struct lttng_uri *uris)
2f77fc4b 2811{
433f5ba9
JR
2812 int ret;
2813 size_t i;
2f77fc4b 2814
e3876bf0
JR
2815 for (i = 0; i < nb_uri; i++) {
2816 if (uris[i].stype != LTTNG_STREAM_CONTROL ||
2817 uris[i].subdir[0] == '\0') {
2818 /* Not interested in these URIs */
2819 continue;
2820 }
2821
2822 if (session->base_path != NULL) {
2823 free(session->base_path);
2824 session->base_path = NULL;
2825 }
2826
2827 /* Set session base_path */
2828 session->base_path = strdup(uris[i].subdir);
2829 if (!session->base_path) {
433f5ba9
JR
2830 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2831 uris[i].subdir, session->name);
2832 ret = LTTNG_ERR_NOMEM;
e3876bf0
JR
2833 goto error;
2834 }
433f5ba9
JR
2835 DBG2("Setting base path \"%s\" for session \"%s\"",
2836 session->base_path, session->name);
2837 }
2838 ret = LTTNG_OK;
2839error:
2840 return ret;
2841}
2842
2843/*
2844 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2845 */
2846int cmd_set_consumer_uri(struct ltt_session *session, size_t nb_uri,
2847 struct lttng_uri *uris)
2848{
2849 int ret, i;
2850 struct ltt_kernel_session *ksess = session->kernel_session;
2851 struct ltt_ust_session *usess = session->ust_session;
2852
a0377dfe
FD
2853 LTTNG_ASSERT(session);
2854 LTTNG_ASSERT(uris);
2855 LTTNG_ASSERT(nb_uri > 0);
433f5ba9
JR
2856
2857 /* Can't set consumer URI if the session is active. */
2858 if (session->active) {
2859 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2860 goto error;
2861 }
2862
2863 /*
2864 * Set the session base path if any. This is done inside
2865 * cmd_set_consumer_uri to preserve backward compatibility of the
2866 * previous session creation api vs the session descriptor api.
2867 */
2868 ret = set_session_base_path_from_uris(session, nb_uri, uris);
2869 if (ret != LTTNG_OK) {
2870 goto error;
e3876bf0
JR
2871 }
2872
bda32d56 2873 /* Set the "global" consumer URIs */
2f77fc4b 2874 for (i = 0; i < nb_uri; i++) {
e3876bf0
JR
2875 ret = add_uri_to_consumer(session, session->consumer, &uris[i],
2876 LTTNG_DOMAIN_NONE);
a74934ba 2877 if (ret != LTTNG_OK) {
2f77fc4b
DG
2878 goto error;
2879 }
2f77fc4b
DG
2880 }
2881
bda32d56
JG
2882 /* Set UST session URIs */
2883 if (session->ust_session) {
2884 for (i = 0; i < nb_uri; i++) {
b178f53e 2885 ret = add_uri_to_consumer(session,
bda32d56 2886 session->ust_session->consumer,
b178f53e 2887 &uris[i], LTTNG_DOMAIN_UST);
bda32d56
JG
2888 if (ret != LTTNG_OK) {
2889 goto error;
2890 }
2891 }
2892 }
2893
2894 /* Set kernel session URIs */
2895 if (session->kernel_session) {
2896 for (i = 0; i < nb_uri; i++) {
b178f53e 2897 ret = add_uri_to_consumer(session,
bda32d56 2898 session->kernel_session->consumer,
b178f53e 2899 &uris[i], LTTNG_DOMAIN_KERNEL);
bda32d56
JG
2900 if (ret != LTTNG_OK) {
2901 goto error;
2902 }
2903 }
2904 }
2905
7ab70fe0
DG
2906 /*
2907 * Make sure to set the session in output mode after we set URI since a
2908 * session can be created without URL (thus flagged in no output mode).
2909 */
2910 session->output_traces = 1;
2911 if (ksess) {
2912 ksess->output_traces = 1;
bda32d56
JG
2913 }
2914
2915 if (usess) {
7ab70fe0
DG
2916 usess->output_traces = 1;
2917 }
2918
2f77fc4b 2919 /* All good! */
f73fabfd 2920 ret = LTTNG_OK;
2f77fc4b
DG
2921
2922error:
2923 return ret;
2924}
2925
b178f53e
JG
2926static
2927enum lttng_error_code set_session_output_from_descriptor(
2928 struct ltt_session *session,
2929 const struct lttng_session_descriptor *descriptor)
2f77fc4b
DG
2930{
2931 int ret;
b178f53e
JG
2932 enum lttng_error_code ret_code = LTTNG_OK;
2933 enum lttng_session_descriptor_type session_type =
2934 lttng_session_descriptor_get_type(descriptor);
2935 enum lttng_session_descriptor_output_type output_type =
2936 lttng_session_descriptor_get_output_type(descriptor);
2937 struct lttng_uri uris[2] = {};
2938 size_t uri_count = 0;
2939
2940 switch (output_type) {
2941 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE:
2942 goto end;
2943 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL:
2944 lttng_session_descriptor_get_local_output_uri(descriptor,
2945 &uris[0]);
2946 uri_count = 1;
2947 break;
2948 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK:
2949 lttng_session_descriptor_get_network_output_uris(descriptor,
2950 &uris[0], &uris[1]);
2951 uri_count = 2;
2952 break;
2953 default:
2954 ret_code = LTTNG_ERR_INVALID;
e32d7f27 2955 goto end;
2f77fc4b
DG
2956 }
2957
b178f53e
JG
2958 switch (session_type) {
2959 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT:
2960 {
2961 struct snapshot_output *new_output = NULL;
2962
2963 new_output = snapshot_output_alloc();
2964 if (!new_output) {
2965 ret_code = LTTNG_ERR_NOMEM;
2966 goto end;
2967 }
2968
2969 ret = snapshot_output_init_with_uri(session,
2970 DEFAULT_SNAPSHOT_MAX_SIZE,
2971 NULL, uris, uri_count, session->consumer,
2972 new_output, &session->snapshot);
2973 if (ret < 0) {
2974 ret_code = (ret == -ENOMEM) ?
2975 LTTNG_ERR_NOMEM : LTTNG_ERR_INVALID;
2976 snapshot_output_destroy(new_output);
2977 goto end;
2978 }
2979 snapshot_add_output(&session->snapshot, new_output);
2980 break;
2981 }
2982 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR:
2983 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE:
2984 {
2985 ret_code = cmd_set_consumer_uri(session, uri_count, uris);
2986 break;
2987 }
2988 default:
2989 ret_code = LTTNG_ERR_INVALID;
e32d7f27 2990 goto end;
2f77fc4b 2991 }
b178f53e
JG
2992end:
2993 return ret_code;
2994}
2995
2996static
2997enum lttng_error_code cmd_create_session_from_descriptor(
2998 struct lttng_session_descriptor *descriptor,
2999 const lttng_sock_cred *creds,
3000 const char *home_path)
3001{
3002 int ret;
3003 enum lttng_error_code ret_code;
3004 const char *session_name;
3005 struct ltt_session *new_session = NULL;
3006 enum lttng_session_descriptor_status descriptor_status;
2f77fc4b 3007
e32d7f27 3008 session_lock_list();
b178f53e
JG
3009 if (home_path) {
3010 if (*home_path != '/') {
3011 ERR("Home path provided by client is not absolute");
3012 ret_code = LTTNG_ERR_INVALID;
3013 goto end;
3014 }
3015 }
2f77fc4b 3016
b178f53e
JG
3017 descriptor_status = lttng_session_descriptor_get_session_name(
3018 descriptor, &session_name);
3019 switch (descriptor_status) {
3020 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK:
3021 break;
3022 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET:
3023 session_name = NULL;
3024 break;
3025 default:
3026 ret_code = LTTNG_ERR_INVALID;
3027 goto end;
3028 }
e3876bf0 3029
b178f53e 3030 ret_code = session_create(session_name, creds->uid, creds->gid,
e3876bf0 3031 &new_session);
b178f53e 3032 if (ret_code != LTTNG_OK) {
e32d7f27 3033 goto end;
2f77fc4b
DG
3034 }
3035
b178f53e
JG
3036 if (!session_name) {
3037 ret = lttng_session_descriptor_set_session_name(descriptor,
3038 new_session->name);
3039 if (ret) {
3040 ret_code = LTTNG_ERR_SESSION_FAIL;
3041 goto end;
3042 }
3043 }
3044
3045 if (!lttng_session_descriptor_is_output_destination_initialized(
3046 descriptor)) {
3047 /*
3048 * Only include the session's creation time in the output
3049 * destination if the name of the session itself was
3050 * not auto-generated.
3051 */
3052 ret_code = lttng_session_descriptor_set_default_output(
3053 descriptor,
3054 session_name ? &new_session->creation_time : NULL,
3055 home_path);
3056 if (ret_code != LTTNG_OK) {
e32d7f27 3057 goto end;
2bba9e53 3058 }
2bba9e53 3059 } else {
b178f53e
JG
3060 new_session->has_user_specified_directory =
3061 lttng_session_descriptor_has_output_directory(
3062 descriptor);
2f77fc4b
DG
3063 }
3064
b178f53e
JG
3065 switch (lttng_session_descriptor_get_type(descriptor)) {
3066 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT:
3067 new_session->snapshot_mode = 1;
3068 break;
3069 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE:
3070 new_session->live_timer =
3071 lttng_session_descriptor_live_get_timer_interval(
3072 descriptor);
3073 break;
3074 default:
3075 break;
3076 }
2f77fc4b 3077
b178f53e
JG
3078 ret_code = set_session_output_from_descriptor(new_session, descriptor);
3079 if (ret_code != LTTNG_OK) {
3080 goto end;
3081 }
3082 new_session->consumer->enabled = 1;
3083 ret_code = LTTNG_OK;
e32d7f27 3084end:
b178f53e
JG
3085 /* Release reference provided by the session_create function. */
3086 session_put(new_session);
3087 if (ret_code != LTTNG_OK && new_session) {
3088 /* Release the global reference on error. */
3089 session_destroy(new_session);
e32d7f27 3090 }
b178f53e
JG
3091 session_unlock_list();
3092 return ret_code;
2f77fc4b
DG
3093}
3094
b178f53e
JG
3095enum lttng_error_code cmd_create_session(struct command_ctx *cmd_ctx, int sock,
3096 struct lttng_session_descriptor **return_descriptor)
27babd3a
DG
3097{
3098 int ret;
b178f53e
JG
3099 size_t payload_size;
3100 struct lttng_dynamic_buffer payload;
3101 struct lttng_buffer_view home_dir_view;
3102 struct lttng_buffer_view session_descriptor_view;
3103 struct lttng_session_descriptor *session_descriptor = NULL;
3104 enum lttng_error_code ret_code;
3105
3106 lttng_dynamic_buffer_init(&payload);
3a91de3a 3107 if (cmd_ctx->lsm.u.create_session.home_dir_size >=
b178f53e
JG
3108 LTTNG_PATH_MAX) {
3109 ret_code = LTTNG_ERR_INVALID;
3110 goto error;
27babd3a 3111 }
3a91de3a 3112 if (cmd_ctx->lsm.u.create_session.session_descriptor_size >
b178f53e
JG
3113 LTTNG_SESSION_DESCRIPTOR_MAX_LEN) {
3114 ret_code = LTTNG_ERR_INVALID;
3115 goto error;
27babd3a
DG
3116 }
3117
3a91de3a
JG
3118 payload_size = cmd_ctx->lsm.u.create_session.home_dir_size +
3119 cmd_ctx->lsm.u.create_session.session_descriptor_size;
b178f53e
JG
3120 ret = lttng_dynamic_buffer_set_size(&payload, payload_size);
3121 if (ret) {
3122 ret_code = LTTNG_ERR_NOMEM;
3123 goto error;
27babd3a
DG
3124 }
3125
b178f53e
JG
3126 ret = lttcomm_recv_unix_sock(sock, payload.data, payload.size);
3127 if (ret <= 0) {
3128 ERR("Reception of session descriptor failed, aborting.");
3129 ret_code = LTTNG_ERR_SESSION_FAIL;
3130 goto error;
27babd3a
DG
3131 }
3132
b178f53e
JG
3133 home_dir_view = lttng_buffer_view_from_dynamic_buffer(
3134 &payload,
3135 0,
3a91de3a 3136 cmd_ctx->lsm.u.create_session.home_dir_size);
3e6e0df2
JG
3137 if (cmd_ctx->lsm.u.create_session.home_dir_size > 0 &&
3138 !lttng_buffer_view_is_valid(&home_dir_view)) {
3139 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3140 ret_code = LTTNG_ERR_INVALID_PROTOCOL;
3141 goto error;
3142 }
3143
b178f53e
JG
3144 session_descriptor_view = lttng_buffer_view_from_dynamic_buffer(
3145 &payload,
3a91de3a
JG
3146 cmd_ctx->lsm.u.create_session.home_dir_size,
3147 cmd_ctx->lsm.u.create_session.session_descriptor_size);
3e6e0df2
JG
3148 if (!lttng_buffer_view_is_valid(&session_descriptor_view)) {
3149 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3150 ret_code = LTTNG_ERR_INVALID_PROTOCOL;
3151 goto error;
3152 }
27babd3a 3153
b178f53e
JG
3154 ret = lttng_session_descriptor_create_from_buffer(
3155 &session_descriptor_view, &session_descriptor);
3156 if (ret < 0) {
3157 ERR("Failed to create session descriptor from payload of \"create session\" command");
3158 ret_code = LTTNG_ERR_INVALID;
3159 goto error;
3160 }
27babd3a 3161
b178f53e
JG
3162 /*
3163 * Sets the descriptor's auto-generated properties (name, output) if
3164 * needed.
3165 */
3166 ret_code = cmd_create_session_from_descriptor(session_descriptor,
3167 &cmd_ctx->creds,
3168 home_dir_view.size ? home_dir_view.data : NULL);
3169 if (ret_code != LTTNG_OK) {
3170 goto error;
e32d7f27 3171 }
b178f53e
JG
3172
3173 ret_code = LTTNG_OK;
3174 *return_descriptor = session_descriptor;
3175 session_descriptor = NULL;
3176error:
3177 lttng_dynamic_buffer_reset(&payload);
3178 lttng_session_descriptor_destroy(session_descriptor);
3179 return ret_code;
27babd3a
DG
3180}
3181
3e3665b8
JG
3182static
3183void cmd_destroy_session_reply(const struct ltt_session *session,
3184 void *_reply_context)
3185{
3186 int ret;
3187 ssize_t comm_ret;
3188 const struct cmd_destroy_session_reply_context *reply_context =
3189 _reply_context;
3190 struct lttng_dynamic_buffer payload;
3191 struct lttcomm_session_destroy_command_header cmd_header;
3192 struct lttng_trace_archive_location *location = NULL;
3193 struct lttcomm_lttng_msg llm = {
3194 .cmd_type = LTTNG_DESTROY_SESSION,
3285a971 3195 .ret_code = reply_context->destruction_status,
3e3665b8
JG
3196 .pid = UINT32_MAX,
3197 .cmd_header_size =
3198 sizeof(struct lttcomm_session_destroy_command_header),
3199 .data_size = 0,
3200 };
3201 size_t payload_size_before_location;
3202
3203 lttng_dynamic_buffer_init(&payload);
3204
3205 ret = lttng_dynamic_buffer_append(&payload, &llm, sizeof(llm));
0e270a1e 3206 if (ret) {
3e3665b8
JG
3207 ERR("Failed to append session destruction message");
3208 goto error;
0e270a1e 3209 }
3e3665b8
JG
3210
3211 cmd_header.rotation_state =
3212 (int32_t) (reply_context->implicit_rotation_on_destroy ?
3213 session->rotation_state :
3214 LTTNG_ROTATION_STATE_NO_ROTATION);
3215 ret = lttng_dynamic_buffer_append(&payload, &cmd_header,
3216 sizeof(cmd_header));
3217 if (ret) {
3218 ERR("Failed to append session destruction command header");
3219 goto error;
3220 }
3221
3222 if (!reply_context->implicit_rotation_on_destroy) {
3223 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3224 session->name);
3225 goto send_reply;
3226 }
3227 if (session->rotation_state != LTTNG_ROTATION_STATE_COMPLETED) {
3228 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3229 session->name);
3230 goto send_reply;
3231 }
3232
3233 location = session_get_trace_archive_location(session);
3234 if (!location) {
3235 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3236 session->name);
3237 goto error;
3238 }
3239
3240 payload_size_before_location = payload.size;
3241 comm_ret = lttng_trace_archive_location_serialize(location,
3242 &payload);
d3740619 3243 lttng_trace_archive_location_put(location);
3e3665b8
JG
3244 if (comm_ret < 0) {
3245 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3246 session->name);
3247 goto error;
3248 }
3249 /* Update the message to indicate the location's length. */
3250 ((struct lttcomm_lttng_msg *) payload.data)->data_size =
3251 payload.size - payload_size_before_location;
3252send_reply:
3253 comm_ret = lttcomm_send_unix_sock(reply_context->reply_sock_fd,
3254 payload.data, payload.size);
3255 if (comm_ret != (ssize_t) payload.size) {
3256 ERR("Failed to send result of the destruction of session \"%s\" to client",
3257 session->name);
3258 }
3259error:
3260 ret = close(reply_context->reply_sock_fd);
3261 if (ret) {
3262 PERROR("Failed to close client socket in deferred session destroy reply");
3263 }
3264 lttng_dynamic_buffer_reset(&payload);
3265 free(_reply_context);
3266}
3267
2f77fc4b
DG
3268/*
3269 * Command LTTNG_DESTROY_SESSION processed by the client thread.
a9ad0c8f
MD
3270 *
3271 * Called with session lock held.
2f77fc4b 3272 */
e32d7f27 3273int cmd_destroy_session(struct ltt_session *session,
3e3665b8
JG
3274 struct notification_thread_handle *notification_thread_handle,
3275 int *sock_fd)
2f77fc4b
DG
3276{
3277 int ret;
3285a971 3278 enum lttng_error_code destruction_last_error = LTTNG_OK;
3e3665b8
JG
3279 struct cmd_destroy_session_reply_context *reply_context = NULL;
3280
3281 if (sock_fd) {
3282 reply_context = zmalloc(sizeof(*reply_context));
3283 if (!reply_context) {
3284 ret = LTTNG_ERR_NOMEM;
3285 goto end;
3286 }
3287 reply_context->reply_sock_fd = *sock_fd;
3288 }
2f77fc4b
DG
3289
3290 /* Safety net */
a0377dfe 3291 LTTNG_ASSERT(session);
2f77fc4b 3292
3e3665b8
JG
3293 DBG("Begin destroy session %s (id %" PRIu64 ")", session->name,
3294 session->id);
3295 if (session->active) {
3296 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3297 session->name);
3298 ret = cmd_stop_trace(session);
3299 if (ret != LTTNG_OK && ret != LTTNG_ERR_TRACE_ALREADY_STOPPED) {
3300 /* Carry on with the destruction of the session. */
3301 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3302 session->name, lttng_strerror(-ret));
3285a971 3303 destruction_last_error = ret;
3e3665b8
JG
3304 }
3305 }
5c408ad8 3306
92816cc3
JG
3307 if (session->rotation_schedule_timer_enabled) {
3308 if (timer_session_rotation_schedule_timer_stop(
3309 session)) {
3310 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3311 session->name);
3285a971 3312 destruction_last_error = LTTNG_ERR_TIMER_STOP_ERROR;
92816cc3 3313 }
259c2674
JD
3314 }
3315
90936dcf
JD
3316 if (session->rotate_size) {
3317 unsubscribe_session_consumed_size_rotation(session, notification_thread_handle);
3318 session->rotate_size = 0;
3319 }
3320
a7ceb342 3321 if (session->rotated && session->current_trace_chunk && session->output_traces) {
b5893d8e
JG
3322 /*
3323 * Perform a last rotation on destruction if rotations have
3324 * occurred during the session's lifetime.
3325 */
343defc2
MD
3326 ret = cmd_rotate_session(session, NULL, false,
3327 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED);
d2956687
JG
3328 if (ret != LTTNG_OK) {
3329 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3330 session->name, lttng_strerror(-ret));
3285a971 3331 destruction_last_error = -ret;
124473a3 3332 }
0e270a1e 3333 if (reply_context) {
3e3665b8 3334 reply_context->implicit_rotation_on_destroy = true;
0e270a1e
JG
3335 }
3336 } else if (session->has_been_started && session->current_trace_chunk) {
7fdbed1c
JG
3337 /*
3338 * The user has not triggered a session rotation. However, to
3339 * ensure all data has been consumed, the session is rotated
3340 * to a 'null' trace chunk before it is destroyed.
3341 *
3342 * This is a "quiet" rotation meaning that no notification is
3343 * emitted and no renaming of the current trace chunk takes
3344 * place.
3345 */
343defc2
MD
3346 ret = cmd_rotate_session(session, NULL, true,
3347 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION);
53fb6336
JG
3348 /*
3349 * Rotation operations may not be supported by the kernel
3350 * tracer. Hence, do not consider this implicit rotation as
3351 * a session destruction error. The library has already stopped
3352 * the session and waited for pending data; there is nothing
3353 * left to do but complete the destruction of the session.
3354 */
3355 if (ret != LTTNG_OK &&
3356 ret != -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL) {
7fdbed1c 3357 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
53fb6336 3358 session->name, lttng_strerror(ret));
3285a971 3359 destruction_last_error = -ret;
7fdbed1c
JG
3360 }
3361 }
5c408ad8 3362
a503e1ef
JG
3363 if (session->shm_path[0]) {
3364 /*
3365 * When a session is created with an explicit shm_path,
3366 * the consumer daemon will create its shared memory files
3367 * at that location and will *not* unlink them. This is normal
3368 * as the intention of that feature is to make it possible
3369 * to retrieve the content of those files should a crash occur.
3370 *
3371 * To ensure the content of those files can be used, the
3372 * sessiond daemon will replicate the content of the metadata
3373 * cache in a metadata file.
3374 *
3375 * On clean-up, it is expected that the consumer daemon will
3376 * unlink the shared memory files and that the session daemon
3377 * will unlink the metadata file. Then, the session's directory
3378 * in the shm path can be removed.
3379 *
3380 * Unfortunately, a flaw in the design of the sessiond's and
3381 * consumerd's tear down of channels makes it impossible to
3382 * determine when the sessiond _and_ the consumerd have both
3383 * destroyed their representation of a channel. For one, the
3384 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3385 * callbacks in both daemons.
3386 *
3387 * However, it is also impossible for the sessiond to know when
3388 * the consumer daemon is done destroying its channel(s) since
3389 * it occurs as a reaction to the closing of the channel's file
3390 * descriptor. There is no resulting communication initiated
3391 * from the consumerd to the sessiond to confirm that the
3392 * operation is completed (and was successful).
3393 *
3394 * Until this is all fixed, the session daemon checks for the
3395 * removal of the session's shm path which makes it possible
3396 * to safely advertise a session as having been destroyed.
3397 *
3398 * Prior to this fix, it was not possible to reliably save
3399 * a session making use of the --shm-path option, destroy it,
3400 * and load it again. This is because the creation of the
3401 * session would fail upon seeing the session's shm path
3402 * already in existence.
3403 *
3404 * Note that none of the error paths in the check for the
3405 * directory's existence return an error. This is normal
3406 * as there isn't much that can be done. The session will
3407 * be destroyed properly, except that we can't offer the
3408 * guarantee that the same session can be re-created.
3409 */
3410 current_completion_handler = &destroy_completion_handler.handler;
3411 ret = lttng_strncpy(destroy_completion_handler.shm_path,
3412 session->shm_path,
3413 sizeof(destroy_completion_handler.shm_path));
a0377dfe 3414 LTTNG_ASSERT(!ret);
a503e1ef 3415 }
e32d7f27
JG
3416
3417 /*
3418 * The session is destroyed. However, note that the command context
3419 * still holds a reference to the session, thus delaying its destruction
3420 * _at least_ up to the point when that reference is released.
3421 */
3422 session_destroy(session);
3e3665b8 3423 if (reply_context) {
3285a971 3424 reply_context->destruction_status = destruction_last_error;
3e3665b8
JG
3425 ret = session_add_destroy_notifier(session,
3426 cmd_destroy_session_reply,
3427 (void *) reply_context);
3428 if (ret) {
3429 ret = LTTNG_ERR_FATAL;
3430 goto end;
3431 } else {
3432 *sock_fd = -1;
3433 }
0e270a1e
JG
3434 }
3435 ret = LTTNG_OK;
3e3665b8 3436end:
2f77fc4b
DG
3437 return ret;
3438}
3439
2f77fc4b
DG
3440/*
3441 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3442 */
56a37563
JG
3443int cmd_register_consumer(struct ltt_session *session,
3444 enum lttng_domain_type domain, const char *sock_path,
3445 struct consumer_data *cdata)
2f77fc4b
DG
3446{
3447 int ret, sock;
dd81b457 3448 struct consumer_socket *socket = NULL;
2f77fc4b 3449
a0377dfe
FD
3450 LTTNG_ASSERT(session);
3451 LTTNG_ASSERT(cdata);
3452 LTTNG_ASSERT(sock_path);
2f77fc4b
DG
3453
3454 switch (domain) {
3455 case LTTNG_DOMAIN_KERNEL:
3456 {
3457 struct ltt_kernel_session *ksess = session->kernel_session;
3458
a0377dfe 3459 LTTNG_ASSERT(ksess);
2f77fc4b
DG
3460
3461 /* Can't register a consumer if there is already one */
3462 if (ksess->consumer_fds_sent != 0) {
f73fabfd 3463 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2f77fc4b
DG
3464 goto error;
3465 }
3466
3467 sock = lttcomm_connect_unix_sock(sock_path);
3468 if (sock < 0) {
f73fabfd 3469 ret = LTTNG_ERR_CONNECT_FAIL;
2f77fc4b
DG
3470 goto error;
3471 }
4ce514c4 3472 cdata->cmd_sock = sock;
2f77fc4b 3473
4ce514c4 3474 socket = consumer_allocate_socket(&cdata->cmd_sock);
2f77fc4b 3475 if (socket == NULL) {
f66c074c
DG
3476 ret = close(sock);
3477 if (ret < 0) {
3478 PERROR("close register consumer");
3479 }
4ce514c4 3480 cdata->cmd_sock = -1;
f73fabfd 3481 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
3482 goto error;
3483 }
3484
3485 socket->lock = zmalloc(sizeof(pthread_mutex_t));
3486 if (socket->lock == NULL) {
3487 PERROR("zmalloc pthread mutex");
f73fabfd 3488 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
3489 goto error;
3490 }
3491 pthread_mutex_init(socket->lock, NULL);
3492 socket->registered = 1;
3493
3494 rcu_read_lock();
3495 consumer_add_socket(socket, ksess->consumer);
3496 rcu_read_unlock();
3497
3498 pthread_mutex_lock(&cdata->pid_mutex);
3499 cdata->pid = -1;
3500 pthread_mutex_unlock(&cdata->pid_mutex);
3501
3502 break;
3503 }
3504 default:
3505 /* TODO: Userspace tracing */
f73fabfd 3506 ret = LTTNG_ERR_UND;
2f77fc4b
DG
3507 goto error;
3508 }
3509
dd81b457 3510 return LTTNG_OK;
2f77fc4b
DG
3511
3512error:
dd81b457
DG
3513 if (socket) {
3514 consumer_destroy_socket(socket);
3515 }
2f77fc4b
DG
3516 return ret;
3517}
3518
3519/*
3520 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3521 */
3522ssize_t cmd_list_domains(struct ltt_session *session,
3523 struct lttng_domain **domains)
3524{
3525 int ret, index = 0;
3526 ssize_t nb_dom = 0;
fefd409b
DG
3527 struct agent *agt;
3528 struct lttng_ht_iter iter;
2f77fc4b
DG
3529
3530 if (session->kernel_session != NULL) {
3531 DBG3("Listing domains found kernel domain");
3532 nb_dom++;
3533 }
3534
3535 if (session->ust_session != NULL) {
3536 DBG3("Listing domains found UST global domain");
3537 nb_dom++;
3c6a091f 3538
e0a74f01 3539 rcu_read_lock();
fefd409b
DG
3540 cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter,
3541 agt, node.node) {
3542 if (agt->being_used) {
3543 nb_dom++;
3544 }
3c6a091f 3545 }
e0a74f01 3546 rcu_read_unlock();
2f77fc4b
DG
3547 }
3548
fa64dfb4
JG
3549 if (!nb_dom) {
3550 goto end;
3551 }
3552
2f77fc4b
DG
3553 *domains = zmalloc(nb_dom * sizeof(struct lttng_domain));
3554 if (*domains == NULL) {
f73fabfd 3555 ret = LTTNG_ERR_FATAL;
2f77fc4b
DG
3556 goto error;
3557 }
3558
3559 if (session->kernel_session != NULL) {
3560 (*domains)[index].type = LTTNG_DOMAIN_KERNEL;
b5edb9e8
PP
3561
3562 /* Kernel session buffer type is always GLOBAL */
3563 (*domains)[index].buf_type = LTTNG_BUFFER_GLOBAL;
3564
2f77fc4b
DG
3565 index++;
3566 }
3567
3568 if (session->ust_session != NULL) {
3569 (*domains)[index].type = LTTNG_DOMAIN_UST;
88c5f0d8 3570 (*domains)[index].buf_type = session->ust_session->buffer_type;
2f77fc4b 3571 index++;
3c6a091f 3572
e0a74f01 3573 rcu_read_lock();
fefd409b
DG
3574 cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter,
3575 agt, node.node) {
3576 if (agt->being_used) {
3577 (*domains)[index].type = agt->domain;
3578 (*domains)[index].buf_type = session->ust_session->buffer_type;
3579 index++;
3580 }
3c6a091f 3581 }
e0a74f01 3582 rcu_read_unlock();
2f77fc4b 3583 }
fa64dfb4 3584end:
2f77fc4b
DG
3585 return nb_dom;
3586
3587error:
f73fabfd
DG
3588 /* Return negative value to differentiate return code */
3589 return -ret;
2f77fc4b
DG
3590}
3591
3592
3593/*
3594 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3595 */
56a37563
JG
3596ssize_t cmd_list_channels(enum lttng_domain_type domain,
3597 struct ltt_session *session, struct lttng_channel **channels)
2f77fc4b 3598{
53e367f9 3599 ssize_t nb_chan = 0, payload_size = 0, ret;
2f77fc4b
DG
3600
3601 switch (domain) {
3602 case LTTNG_DOMAIN_KERNEL:
3603 if (session->kernel_session != NULL) {
3604 nb_chan = session->kernel_session->channel_count;
3605 }
3606 DBG3("Number of kernel channels %zd", nb_chan);
c7d620a2 3607 if (nb_chan <= 0) {
53e367f9
JG
3608 ret = -LTTNG_ERR_KERN_CHAN_NOT_FOUND;
3609 goto end;
c7d620a2 3610 }
2f77fc4b
DG
3611 break;
3612 case LTTNG_DOMAIN_UST:
3613 if (session->ust_session != NULL) {
7ffc31a2 3614 rcu_read_lock();
2f77fc4b 3615 nb_chan = lttng_ht_get_count(
7ffc31a2
JG
3616 session->ust_session->domain_global.channels);
3617 rcu_read_unlock();
2f77fc4b
DG
3618 }
3619 DBG3("Number of UST global channels %zd", nb_chan);
ade7ce52 3620 if (nb_chan < 0) {
53e367f9
JG
3621 ret = -LTTNG_ERR_UST_CHAN_NOT_FOUND;
3622 goto end;
c7d620a2 3623 }
2f77fc4b
DG
3624 break;
3625 default:
53e367f9
JG
3626 ret = -LTTNG_ERR_UND;
3627 goto end;
2f77fc4b
DG
3628 }
3629
3630 if (nb_chan > 0) {
53e367f9 3631 const size_t channel_size = sizeof(struct lttng_channel) +
cf0bcb51
JG
3632 sizeof(struct lttng_channel_extended);
3633 struct lttng_channel_extended *channel_exts;
53e367f9
JG
3634
3635 payload_size = nb_chan * channel_size;
3636 *channels = zmalloc(payload_size);
2f77fc4b 3637 if (*channels == NULL) {
53e367f9
JG
3638 ret = -LTTNG_ERR_FATAL;
3639 goto end;
2f77fc4b
DG
3640 }
3641
53e367f9
JG
3642 channel_exts = ((void *) *channels) +
3643 (nb_chan * sizeof(struct lttng_channel));
d449df4a
MD
3644 ret = list_lttng_channels(domain, session, *channels, channel_exts);
3645 if (ret != LTTNG_OK) {
3646 free(*channels);
3647 *channels = NULL;
3648 goto end;
3649 }
53e367f9
JG
3650 } else {
3651 *channels = NULL;
2f77fc4b
DG
3652 }
3653
53e367f9
JG
3654 ret = payload_size;
3655end:
3656 return ret;
2f77fc4b
DG
3657}
3658
3659/*
3660 * Command LTTNG_LIST_EVENTS processed by the client thread.
3661 */
56a37563
JG
3662ssize_t cmd_list_events(enum lttng_domain_type domain,
3663 struct ltt_session *session, char *channel_name,
e368fb43 3664 struct lttng_payload *payload)
2f77fc4b
DG
3665{
3666 int ret = 0;
e368fb43
JG
3667 ssize_t nb_events = 0;
3668 struct lttcomm_event_command_header cmd_header = {};
3669 const size_t cmd_header_offset = payload->buffer.size;
3670
3671 ret = lttng_dynamic_buffer_append(
3672 &payload->buffer, &cmd_header, sizeof(cmd_header));
3673 if (ret) {
3674 ret = LTTNG_ERR_NOMEM;
3675 goto error;
3676 }
2f77fc4b
DG
3677
3678 switch (domain) {
3679 case LTTNG_DOMAIN_KERNEL:
3680 if (session->kernel_session != NULL) {
e368fb43
JG
3681 nb_events = list_lttng_kernel_events(channel_name,
3682 session->kernel_session, payload);
2f77fc4b
DG
3683 }
3684 break;
3685 case LTTNG_DOMAIN_UST:
3686 {
3687 if (session->ust_session != NULL) {
e368fb43
JG
3688 nb_events = list_lttng_ust_global_events(channel_name,
3689 &session->ust_session->domain_global,
3690 payload);
2f77fc4b
DG
3691 }
3692 break;
3693 }
5cdb6027 3694 case LTTNG_DOMAIN_LOG4J:
3c6a091f 3695 case LTTNG_DOMAIN_JUL:
0e115563 3696 case LTTNG_DOMAIN_PYTHON:
3c6a091f 3697 if (session->ust_session) {
fefd409b
DG
3698 struct lttng_ht_iter iter;
3699 struct agent *agt;
3700
b11feea5 3701 rcu_read_lock();
fefd409b
DG
3702 cds_lfht_for_each_entry(session->ust_session->agents->ht,
3703 &iter.iter, agt, node.node) {
1dfd9906 3704 if (agt->domain == domain) {
e368fb43
JG
3705 nb_events = list_lttng_agent_events(
3706 agt, payload);
1dfd9906
JG
3707 break;
3708 }
fefd409b 3709 }
b11feea5 3710 rcu_read_unlock();
3c6a091f
DG
3711 }
3712 break;
2f77fc4b 3713 default:
f73fabfd 3714 ret = LTTNG_ERR_UND;
2f77fc4b
DG
3715 goto error;
3716 }
3717
e368fb43
JG
3718 ((struct lttcomm_event_command_header *) (payload->buffer.data +
3719 cmd_header_offset))->nb_events = (uint32_t) nb_events;
3720
3721 return nb_events;
2f77fc4b
DG
3722
3723error:
f73fabfd
DG
3724 /* Return negative value to differentiate return code */
3725 return -ret;
2f77fc4b
DG
3726}
3727
3728/*
3729 * Using the session list, filled a lttng_session array to send back to the
3730 * client for session listing.
3731 *
3732 * The session list lock MUST be acquired before calling this function. Use
3733 * session_lock_list() and session_unlock_list().
3734 */
b178f53e
JG
3735void cmd_list_lttng_sessions(struct lttng_session *sessions,
3736 size_t session_count, uid_t uid, gid_t gid)
2f77fc4b
DG
3737{
3738 int ret;
3739 unsigned int i = 0;
3740 struct ltt_session *session;
3741 struct ltt_session_list *list = session_get_list();
0e270a1e 3742 struct lttng_session_extended *extended =
b178f53e 3743 (typeof(extended)) (&sessions[session_count]);
2f77fc4b
DG
3744
3745 DBG("Getting all available session for UID %d GID %d",
3746 uid, gid);
3747 /*
3748 * Iterate over session list and append data after the control struct in
3749 * the buffer.
3750 */
3751 cds_list_for_each_entry(session, &list->head, list) {
e32d7f27
JG
3752 if (!session_get(session)) {
3753 continue;
3754 }
2f77fc4b
DG
3755 /*
3756 * Only list the sessions the user can control.
3757 */
d7b377ed 3758 if (!session_access_ok(session, uid) ||
e32d7f27
JG
3759 session->destroyed) {
3760 session_put(session);
2f77fc4b
DG
3761 continue;
3762 }
3763
3764 struct ltt_kernel_session *ksess = session->kernel_session;
3765 struct ltt_ust_session *usess = session->ust_session;
3766
3767 if (session->consumer->type == CONSUMER_DST_NET ||
3768 (ksess && ksess->consumer->type == CONSUMER_DST_NET) ||
3769 (usess && usess->consumer->type == CONSUMER_DST_NET)) {
3770 ret = build_network_session_path(sessions[i].path,
dec56f6c 3771 sizeof(sessions[i].path), session);
2f77fc4b 3772 } else {
dec56f6c 3773 ret = snprintf(sessions[i].path, sizeof(sessions[i].path), "%s",
366a9222 3774 session->consumer->dst.session_root_path);
2f77fc4b
DG
3775 }
3776 if (ret < 0) {
3777 PERROR("snprintf session path");
e32d7f27 3778 session_put(session);
2f77fc4b
DG
3779 continue;
3780 }
3781
3782 strncpy(sessions[i].name, session->name, NAME_MAX);
3783 sessions[i].name[NAME_MAX - 1] = '\0';
8382cf6f 3784 sessions[i].enabled = session->active;
2cbf8fed 3785 sessions[i].snapshot_mode = session->snapshot_mode;
8960e9cd 3786 sessions[i].live_timer_interval = session->live_timer;
b178f53e
JG
3787 extended[i].creation_time.value = (uint64_t) session->creation_time;
3788 extended[i].creation_time.is_set = 1;
2f77fc4b 3789 i++;
e32d7f27 3790 session_put(session);
2f77fc4b
DG
3791 }
3792}
3793
806e2684 3794/*
6d805429 3795 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
d3f14b8a 3796 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
806e2684 3797 */
6d805429 3798int cmd_data_pending(struct ltt_session *session)
806e2684
DG
3799{
3800 int ret;
3801 struct ltt_kernel_session *ksess = session->kernel_session;
3802 struct ltt_ust_session *usess = session->ust_session;
3803
a0377dfe 3804 LTTNG_ASSERT(session);
806e2684 3805
5c408ad8
JD
3806 DBG("Data pending for session %s", session->name);
3807
806e2684 3808 /* Session MUST be stopped to ask for data availability. */
8382cf6f 3809 if (session->active) {
806e2684
DG
3810 ret = LTTNG_ERR_SESSION_STARTED;
3811 goto error;
3a89d11a
DG
3812 } else {
3813 /*
3814 * If stopped, just make sure we've started before else the above call
3815 * will always send that there is data pending.
3816 *
3817 * The consumer assumes that when the data pending command is received,
3818 * the trace has been started before or else no output data is written
3819 * by the streams which is a condition for data pending. So, this is
3820 * *VERY* important that we don't ask the consumer before a start
3821 * trace.
3822 */
8382cf6f 3823 if (!session->has_been_started) {
3a89d11a
DG
3824 ret = 0;
3825 goto error;
3826 }
806e2684
DG
3827 }
3828
92816cc3
JG
3829 /* A rotation is still pending, we have to wait. */
3830 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
5c408ad8
JD
3831 DBG("Rotate still pending for session %s", session->name);
3832 ret = 1;
3833 goto error;
3834 }
3835
806e2684 3836 if (ksess && ksess->consumer) {
6d805429
DG
3837 ret = consumer_is_data_pending(ksess->id, ksess->consumer);
3838 if (ret == 1) {
806e2684
DG
3839 /* Data is still being extracted for the kernel. */
3840 goto error;
3841 }
3842 }
3843
3844 if (usess && usess->consumer) {
6d805429
DG
3845 ret = consumer_is_data_pending(usess->id, usess->consumer);
3846 if (ret == 1) {
806e2684
DG
3847 /* Data is still being extracted for the kernel. */
3848 goto error;
3849 }
3850 }
3851
3852 /* Data is ready to be read by a viewer */
6d805429 3853 ret = 0;
806e2684
DG
3854
3855error:
3856 return ret;
3857}
3858
6dc3064a
DG
3859/*
3860 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3861 *
3862 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3863 */
3864int cmd_snapshot_add_output(struct ltt_session *session,
df4f5a87 3865 const struct lttng_snapshot_output *output, uint32_t *id)
6dc3064a
DG
3866{
3867 int ret;
3868 struct snapshot_output *new_output;
3869
a0377dfe
FD
3870 LTTNG_ASSERT(session);
3871 LTTNG_ASSERT(output);
6dc3064a
DG
3872
3873 DBG("Cmd snapshot add output for session %s", session->name);
3874
3875 /*
903ef685 3876 * Can't create an output if the session is not set in no-output mode.
6dc3064a
DG
3877 */
3878 if (session->output_traces) {
903ef685 3879 ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
6dc3064a
DG
3880 goto error;
3881 }
3882
54213acc
JG
3883 if (session->has_non_mmap_channel) {
3884 ret = LTTNG_ERR_SNAPSHOT_UNSUPPORTED;
3885 goto error;
3886 }
3887
6dc3064a
DG
3888 /* Only one output is allowed until we have the "tee" feature. */
3889 if (session->snapshot.nb_output == 1) {
3890 ret = LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST;
3891 goto error;
3892 }
3893
3894 new_output = snapshot_output_alloc();
3895 if (!new_output) {
3896 ret = LTTNG_ERR_NOMEM;
3897 goto error;
3898 }
3899
b178f53e 3900 ret = snapshot_output_init(session, output->max_size, output->name,
6dc3064a
DG
3901 output->ctrl_url, output->data_url, session->consumer, new_output,
3902 &session->snapshot);
3903 if (ret < 0) {
3904 if (ret == -ENOMEM) {
3905 ret = LTTNG_ERR_NOMEM;
3906 } else {
3907 ret = LTTNG_ERR_INVALID;
3908 }
3909 goto free_error;
3910 }
3911
6dc3064a
DG
3912 rcu_read_lock();
3913 snapshot_add_output(&session->snapshot, new_output);
3914 if (id) {
3915 *id = new_output->id;
3916 }
3917 rcu_read_unlock();
3918
3919 return LTTNG_OK;
3920
3921free_error:
3922 snapshot_output_destroy(new_output);
3923error:
3924 return ret;
3925}
3926
3927/*
3928 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3929 *
3930 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3931 */
3932int cmd_snapshot_del_output(struct ltt_session *session,
df4f5a87 3933 const struct lttng_snapshot_output *output)
6dc3064a
DG
3934{
3935 int ret;
eb240553 3936 struct snapshot_output *sout = NULL;
6dc3064a 3937
a0377dfe
FD
3938 LTTNG_ASSERT(session);
3939 LTTNG_ASSERT(output);
6dc3064a 3940
6dc3064a
DG
3941 rcu_read_lock();
3942
3943 /*
d3f14b8a
MD
3944 * Permission denied to create an output if the session is not
3945 * set in no output mode.
6dc3064a
DG
3946 */
3947 if (session->output_traces) {
903ef685 3948 ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
6dc3064a
DG
3949 goto error;
3950 }
3951
eb240553
DG
3952 if (output->id) {
3953 DBG("Cmd snapshot del output id %" PRIu32 " for session %s", output->id,
3954 session->name);
3955 sout = snapshot_find_output_by_id(output->id, &session->snapshot);
3956 } else if (*output->name != '\0') {
3957 DBG("Cmd snapshot del output name %s for session %s", output->name,
3958 session->name);
3959 sout = snapshot_find_output_by_name(output->name, &session->snapshot);
3960 }
6dc3064a
DG
3961 if (!sout) {
3962 ret = LTTNG_ERR_INVALID;
3963 goto error;
3964 }
3965
3966 snapshot_delete_output(&session->snapshot, sout);
3967 snapshot_output_destroy(sout);
3968 ret = LTTNG_OK;
3969
3970error:
3971 rcu_read_unlock();
3972 return ret;
3973}
3974
3975/*
3976 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3977 *
3978 * If no output is available, outputs is untouched and 0 is returned.
3979 *
3980 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3981 */
3982ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
3983 struct lttng_snapshot_output **outputs)
3984{
3985 int ret, idx = 0;
b223ca94 3986 struct lttng_snapshot_output *list = NULL;
6dc3064a
DG
3987 struct lttng_ht_iter iter;
3988 struct snapshot_output *output;
3989
a0377dfe
FD
3990 LTTNG_ASSERT(session);
3991 LTTNG_ASSERT(outputs);
6dc3064a
DG
3992
3993 DBG("Cmd snapshot list outputs for session %s", session->name);
3994
3995 /*
d3f14b8a
MD
3996 * Permission denied to create an output if the session is not
3997 * set in no output mode.
6dc3064a
DG
3998 */
3999 if (session->output_traces) {
903ef685
JG
4000 ret = -LTTNG_ERR_NOT_SNAPSHOT_SESSION;
4001 goto end;
6dc3064a
DG
4002 }
4003
4004 if (session->snapshot.nb_output == 0) {
4005 ret = 0;
903ef685 4006 goto end;
6dc3064a
DG
4007 }
4008
4009 list = zmalloc(session->snapshot.nb_output * sizeof(*list));
4010 if (!list) {
b223ca94 4011 ret = -LTTNG_ERR_NOMEM;
903ef685 4012 goto end;
6dc3064a
DG
4013 }
4014
4015 /* Copy list from session to the new list object. */
b223ca94 4016 rcu_read_lock();
6dc3064a
DG
4017 cds_lfht_for_each_entry(session->snapshot.output_ht->ht, &iter.iter,
4018 output, node.node) {
a0377dfe 4019 LTTNG_ASSERT(output->consumer);
6dc3064a
DG
4020 list[idx].id = output->id;
4021 list[idx].max_size = output->max_size;
6ce22875
MD
4022 if (lttng_strncpy(list[idx].name, output->name,
4023 sizeof(list[idx].name))) {
4024 ret = -LTTNG_ERR_INVALID;
903ef685 4025 goto error;
6ce22875 4026 }
6dc3064a 4027 if (output->consumer->type == CONSUMER_DST_LOCAL) {
6ce22875 4028 if (lttng_strncpy(list[idx].ctrl_url,
366a9222 4029 output->consumer->dst.session_root_path,
6ce22875
MD
4030 sizeof(list[idx].ctrl_url))) {
4031 ret = -LTTNG_ERR_INVALID;
903ef685 4032 goto error;
6ce22875 4033 }
6dc3064a
DG
4034 } else {
4035 /* Control URI. */
4036 ret = uri_to_str_url(&output->consumer->dst.net.control,
4037 list[idx].ctrl_url, sizeof(list[idx].ctrl_url));
4038 if (ret < 0) {
b223ca94 4039 ret = -LTTNG_ERR_NOMEM;
903ef685 4040 goto error;
6dc3064a
DG
4041 }
4042
4043 /* Data URI. */
4044 ret = uri_to_str_url(&output->consumer->dst.net.data,
4045 list[idx].data_url, sizeof(list[idx].data_url));
4046 if (ret < 0) {
b223ca94 4047 ret = -LTTNG_ERR_NOMEM;
903ef685 4048 goto error;
6dc3064a
DG
4049 }
4050 }
4051 idx++;
4052 }
4053
4054 *outputs = list;
b223ca94
JG
4055 list = NULL;
4056 ret = session->snapshot.nb_output;
6dc3064a 4057error:
903ef685 4058 rcu_read_unlock();
b223ca94 4059 free(list);
903ef685 4060end:
b223ca94 4061 return ret;
6dc3064a
DG
4062}
4063
93ec662e
JD
4064/*
4065 * Check if we can regenerate the metadata for this session.
4066 * Only kernel, UST per-uid and non-live sessions are supported.
4067 *
4068 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4069 */
4070static
eded6438 4071int check_regenerate_metadata_support(struct ltt_session *session)
93ec662e
JD
4072{
4073 int ret;
4074
a0377dfe 4075 LTTNG_ASSERT(session);
93ec662e
JD
4076
4077 if (session->live_timer != 0) {
4078 ret = LTTNG_ERR_LIVE_SESSION;
4079 goto end;
4080 }
4081 if (!session->active) {
4082 ret = LTTNG_ERR_SESSION_NOT_STARTED;
4083 goto end;
4084 }
4085 if (session->ust_session) {
4086 switch (session->ust_session->buffer_type) {
4087 case LTTNG_BUFFER_PER_UID:
4088 break;
4089 case LTTNG_BUFFER_PER_PID:
4090 ret = LTTNG_ERR_PER_PID_SESSION;
4091 goto end;
4092 default:
a0377dfe 4093 abort();
93ec662e
JD
4094 ret = LTTNG_ERR_UNK;
4095 goto end;
4096 }
4097 }
4098 if (session->consumer->type == CONSUMER_DST_NET &&
4099 session->consumer->relay_minor_version < 8) {
4100 ret = LTTNG_ERR_RELAYD_VERSION_FAIL;
4101 goto end;
4102 }
4103 ret = 0;
4104
4105end:
4106 return ret;
4107}
4108
7802bae3
LL
4109static
4110int clear_metadata_file(int fd)
4111{
4112 int ret;
a5df8828 4113 off_t lseek_ret;
7802bae3 4114
a5df8828
GL
4115 lseek_ret = lseek(fd, 0, SEEK_SET);
4116 if (lseek_ret < 0) {
7802bae3 4117 PERROR("lseek");
a5df8828 4118 ret = -1;
7802bae3
LL
4119 goto end;
4120 }
4121
4122 ret = ftruncate(fd, 0);
4123 if (ret < 0) {
4124 PERROR("ftruncate");
4125 goto end;
4126 }
4127
4128end:
4129 return ret;
4130}
4131
93ec662e 4132static
eded6438 4133int ust_regenerate_metadata(struct ltt_ust_session *usess)
93ec662e
JD
4134{
4135 int ret = 0;
4136 struct buffer_reg_uid *uid_reg = NULL;
4137 struct buffer_reg_session *session_reg = NULL;
4138
4139 rcu_read_lock();
4140 cds_list_for_each_entry(uid_reg, &usess->buffer_reg_uid_list, lnode) {
4141 struct ust_registry_session *registry;
4142 struct ust_registry_channel *chan;
4143 struct lttng_ht_iter iter_chan;
4144
4145 session_reg = uid_reg->registry;
4146 registry = session_reg->reg.ust;
4147
4148 pthread_mutex_lock(&registry->lock);
4149 registry->metadata_len_sent = 0;
4150 memset(registry->metadata, 0, registry->metadata_alloc_len);
4151 registry->metadata_len = 0;
4152 registry->metadata_version++;
7802bae3
LL
4153 if (registry->metadata_fd > 0) {
4154 /* Clear the metadata file's content. */
4155 ret = clear_metadata_file(registry->metadata_fd);
4156 if (ret) {
4157 pthread_mutex_unlock(&registry->lock);
4158 goto end;
4159 }
4160 }
4161
93ec662e
JD
4162 ret = ust_metadata_session_statedump(registry, NULL,
4163 registry->major, registry->minor);
4164 if (ret) {
4165 pthread_mutex_unlock(&registry->lock);
4166 ERR("Failed to generate session metadata (err = %d)",
4167 ret);
4168 goto end;
4169 }
4170 cds_lfht_for_each_entry(registry->channels->ht, &iter_chan.iter,
4171 chan, node.node) {
4172 struct ust_registry_event *event;
4173 struct lttng_ht_iter iter_event;
4174
4175 ret = ust_metadata_channel_statedump(registry, chan);
4176 if (ret) {
4177 pthread_mutex_unlock(&registry->lock);
4178 ERR("Failed to generate channel metadata "
4179 "(err = %d)", ret);
4180 goto end;
4181 }
4182 cds_lfht_for_each_entry(chan->ht->ht, &iter_event.iter,
4183 event, node.node) {
4184 ret = ust_metadata_event_statedump(registry,
4185 chan, event);
4186 if (ret) {
4187 pthread_mutex_unlock(&registry->lock);
4188 ERR("Failed to generate event metadata "
4189 "(err = %d)", ret);
4190 goto end;
4191 }
4192 }
4193 }
4194 pthread_mutex_unlock(&registry->lock);
4195 }
4196
4197end:
4198 rcu_read_unlock();
4199 return ret;
4200}
4201
4202/*
eded6438 4203 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
93ec662e
JD
4204 *
4205 * Ask the consumer to truncate the existing metadata file(s) and
4206 * then regenerate the metadata. Live and per-pid sessions are not
4207 * supported and return an error.
4208 *
1136f41b 4209 * Return LTTNG_OK on success or else a LTTNG_ERR code.
93ec662e 4210 */
eded6438 4211int cmd_regenerate_metadata(struct ltt_session *session)
93ec662e
JD
4212{
4213 int ret;
4214
a0377dfe 4215 LTTNG_ASSERT(session);
93ec662e 4216
eded6438 4217 ret = check_regenerate_metadata_support(session);
93ec662e
JD
4218 if (ret) {
4219 goto end;
4220 }
4221
4222 if (session->kernel_session) {
eded6438 4223 ret = kernctl_session_regenerate_metadata(
93ec662e
JD
4224 session->kernel_session->fd);
4225 if (ret < 0) {
4226 ERR("Failed to regenerate the kernel metadata");
4227 goto end;
4228 }
4229 }
4230
4231 if (session->ust_session) {
eded6438 4232 ret = ust_regenerate_metadata(session->ust_session);
93ec662e
JD
4233 if (ret < 0) {
4234 ERR("Failed to regenerate the UST metadata");
4235 goto end;
4236 }
4237 }
4238 DBG("Cmd metadata regenerate for session %s", session->name);
4239 ret = LTTNG_OK;
4240
4241end:
4242 return ret;
4243}
4244
c2561365
JD
4245/*
4246 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4247 *
4248 * Ask the tracer to regenerate a new statedump.
4249 *
1136f41b 4250 * Return LTTNG_OK on success or else a LTTNG_ERR code.
c2561365
JD
4251 */
4252int cmd_regenerate_statedump(struct ltt_session *session)
4253{
4254 int ret;
4255
a0377dfe 4256 LTTNG_ASSERT(session);
c2561365
JD
4257
4258 if (!session->active) {
4259 ret = LTTNG_ERR_SESSION_NOT_STARTED;
4260 goto end;
4261 }
c2561365
JD
4262
4263 if (session->kernel_session) {
4264 ret = kernctl_session_regenerate_statedump(
4265 session->kernel_session->fd);
4266 /*
4267 * Currently, the statedump in kernel can only fail if out
4268 * of memory.
4269 */
4270 if (ret < 0) {
4271 if (ret == -ENOMEM) {
4272 ret = LTTNG_ERR_REGEN_STATEDUMP_NOMEM;
4273 } else {
4274 ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL;
4275 }
4276 ERR("Failed to regenerate the kernel statedump");
4277 goto end;
4278 }
4279 }
4280
4281 if (session->ust_session) {
4282 ret = ust_app_regenerate_statedump_all(session->ust_session);
4283 /*
4284 * Currently, the statedump in UST always returns 0.
4285 */
4286 if (ret < 0) {
4287 ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL;
4288 ERR("Failed to regenerate the UST statedump");
4289 goto end;
4290 }
4291 }
4292 DBG("Cmd regenerate statedump for session %s", session->name);
4293 ret = LTTNG_OK;
4294
4295end:
4296 return ret;
4297}
4298
989a0844
FD
4299static
4300enum lttng_error_code synchronize_tracer_notifier_register(
4301 struct notification_thread_handle *notification_thread,
4302 struct lttng_trigger *trigger, const struct lttng_credentials *cmd_creds)
70670472 4303{
989a0844 4304 enum lttng_error_code ret_code;
7c1f6da2
JG
4305 const struct lttng_condition *condition =
4306 lttng_trigger_get_const_condition(trigger);
989a0844
FD
4307 const char *trigger_name;
4308 uid_t trigger_owner;
4309 enum lttng_trigger_status trigger_status;
4310 const enum lttng_domain_type trigger_domain =
4311 lttng_trigger_get_underlying_domain_type_restriction(
4312 trigger);
70670472 4313
989a0844 4314 trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner);
a0377dfe 4315 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
989a0844 4316
a0377dfe
FD
4317 LTTNG_ASSERT(condition);
4318 LTTNG_ASSERT(lttng_condition_get_type(condition) ==
8dbb86b8 4319 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES);
989a0844
FD
4320
4321 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
4322 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ?
0efb2ad7 4323 trigger_name : "(anonymous)";
989a0844
FD
4324
4325 session_lock_list();
4326 switch (trigger_domain) {
4327 case LTTNG_DOMAIN_KERNEL:
4328 {
4329 ret_code = kernel_register_event_notifier(trigger, cmd_creds);
4330 if (ret_code != LTTNG_OK) {
4331 enum lttng_error_code notif_thread_unregister_ret;
4332
4333 notif_thread_unregister_ret =
4334 notification_thread_command_unregister_trigger(
4335 notification_thread, trigger);
4336
4337 if (notif_thread_unregister_ret != LTTNG_OK) {
4338 /* Return the original error code. */
4339 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4340 trigger_name,
4341 (int) trigger_owner,
4342 ret_code);
4343 }
4344 }
4345 break;
70670472 4346 }
989a0844
FD
4347 case LTTNG_DOMAIN_UST:
4348 ust_app_global_update_all_event_notifier_rules();
4349 break;
4350 case LTTNG_DOMAIN_JUL:
4351 case LTTNG_DOMAIN_LOG4J:
4352 case LTTNG_DOMAIN_PYTHON:
4353 {
4354 /* Agent domains. */
4355 struct agent *agt = agent_find_by_event_notifier_domain(
4356 trigger_domain);
70670472 4357
989a0844
FD
4358 if (!agt) {
4359 agt = agent_create(trigger_domain);
4360 if (!agt) {
4361 ret_code = LTTNG_ERR_NOMEM;
4362 goto end_unlock_session_list;
4363 }
4364
412d7227 4365 agent_add(agt, the_trigger_agents_ht_by_domain);
989a0844
FD
4366 }
4367
4368 ret_code = trigger_agent_enable(trigger, agt);
4369 if (ret_code != LTTNG_OK) {
4370 goto end_unlock_session_list;
4371 }
4372
4373 break;
4374 }
4375 case LTTNG_DOMAIN_NONE:
4376 default:
4377 abort();
4378 }
4379
4380 ret_code = LTTNG_OK;
4381end_unlock_session_list:
4382 session_unlock_list();
70670472
JR
4383 return ret_code;
4384}
4385
4386enum lttng_error_code cmd_register_trigger(const struct lttng_credentials *cmd_creds,
746e08d7 4387 struct lttng_trigger *trigger,
0efb2ad7 4388 bool is_trigger_anonymous,
242388e4
JR
4389 struct notification_thread_handle *notification_thread,
4390 struct lttng_trigger **return_trigger)
b0880ae5 4391{
70670472 4392 enum lttng_error_code ret_code;
70670472
JR
4393 const char *trigger_name;
4394 uid_t trigger_owner;
4395 enum lttng_trigger_status trigger_status;
4396
4397 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
ce0b1d61 4398 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ?
0efb2ad7 4399 trigger_name : "(anonymous)";
ce0b1d61 4400
70670472
JR
4401 trigger_status = lttng_trigger_get_owner_uid(
4402 trigger, &trigger_owner);
a0377dfe 4403 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
70670472
JR
4404
4405 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4406 trigger_name, (int) trigger_owner,
4407 (int) lttng_credentials_get_uid(cmd_creds));
b0880ae5 4408
64eafdf6
JR
4409 /*
4410 * Validate the trigger credentials against the command credentials.
4411 * Only the root user can register a trigger with non-matching
4412 * credentials.
4413 */
4414 if (!lttng_credentials_is_equal_uid(
4415 lttng_trigger_get_credentials(trigger),
746e08d7
JG
4416 cmd_creds)) {
4417 if (lttng_credentials_get_uid(cmd_creds) != 0) {
70670472
JR
4418 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4419 trigger_name, (int) trigger_owner,
4420 (int) lttng_credentials_get_uid(cmd_creds));
4421 ret_code = LTTNG_ERR_INVALID_TRIGGER;
64eafdf6
JR
4422 goto end;
4423 }
4424 }
3da864a9 4425
58daac01
JR
4426 /*
4427 * The bytecode generation also serves as a validation step for the
4428 * bytecode expressions.
4429 */
70670472
JR
4430 ret_code = lttng_trigger_generate_bytecode(trigger, cmd_creds);
4431 if (ret_code != LTTNG_OK) {
4432 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4433 trigger_name, (int) trigger_owner, ret_code);
58daac01
JR
4434 goto end;
4435 }
4436
242388e4
JR
4437 /*
4438 * A reference to the trigger is acquired by the notification thread.
4439 * It is safe to return the same trigger to the caller since it the
4440 * other user holds a reference.
4441 *
4442 * The trigger is modified during the execution of the
4443 * "register trigger" command. However, by the time the command returns,
4444 * it is safe to use without any locking as its properties are
4445 * immutable.
4446 */
0efb2ad7
JG
4447 ret_code = notification_thread_command_register_trigger(
4448 notification_thread, trigger, is_trigger_anonymous);
70670472 4449 if (ret_code != LTTNG_OK) {
ce0b1d61 4450 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
70670472 4451 trigger_name, (int) trigger_owner, ret_code);
44760c20 4452 goto end;
70670472
JR
4453 }
4454
ce0b1d61
JG
4455 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
4456 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ?
0efb2ad7 4457 trigger_name : "(anonymous)";
ce0b1d61 4458
70670472
JR
4459 /*
4460 * Synchronize tracers if the trigger adds an event notifier.
4461 */
989a0844
FD
4462 if (lttng_trigger_needs_tracer_notifier(trigger)) {
4463 ret_code = synchronize_tracer_notifier_register(notification_thread,
4464 trigger, cmd_creds);
4465 if (ret_code != LTTNG_OK) {
4466 ERR("Error registering tracer notifier: %s",
4467 lttng_strerror(-ret_code));
4468 goto end;
70670472
JR
4469 }
4470 }
4471
746e08d7
JG
4472 /*
4473 * Return an updated trigger to the client.
4474 *
4475 * Since a modified version of the same trigger is returned, acquire a
4476 * reference to the trigger so the caller doesn't have to care if those
4477 * are distinct instances or not.
4478 */
39b95a70
JG
4479 if (ret_code == LTTNG_OK) {
4480 lttng_trigger_get(trigger);
4481 *return_trigger = trigger;
4482 /* Ownership of trigger was transferred to caller. */
4483 trigger = NULL;
4484 }
b0880ae5 4485end:
70670472 4486 return ret_code;
989a0844
FD
4487}
4488
4489static
4490enum lttng_error_code synchronize_tracer_notifier_unregister(
4491 const struct lttng_trigger *trigger)
4492{
4493 enum lttng_error_code ret_code;
4494 const struct lttng_condition *condition =
4495 lttng_trigger_get_const_condition(trigger);
4496 const enum lttng_domain_type trigger_domain =
4497 lttng_trigger_get_underlying_domain_type_restriction(
4498 trigger);
4499
a0377dfe
FD
4500 LTTNG_ASSERT(condition);
4501 LTTNG_ASSERT(lttng_condition_get_type(condition) ==
8dbb86b8 4502 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES);
989a0844
FD
4503
4504 session_lock_list();
4505 switch (trigger_domain) {
4506 case LTTNG_DOMAIN_KERNEL:
4507 ret_code = kernel_unregister_event_notifier(trigger);
e689039f
JG
4508 if (ret_code != LTTNG_OK) {
4509 goto end_unlock_session_list;
4510 }
4511
989a0844
FD
4512 break;
4513 case LTTNG_DOMAIN_UST:
4514 ust_app_global_update_all_event_notifier_rules();
4515 break;
4516 case LTTNG_DOMAIN_JUL:
4517 case LTTNG_DOMAIN_LOG4J:
4518 case LTTNG_DOMAIN_PYTHON:
4519 {
4520 /* Agent domains. */
4521 struct agent *agt = agent_find_by_event_notifier_domain(
4522 trigger_domain);
4523
566190c4
JG
4524 /*
4525 * This trigger was never registered in the first place. Calling
4526 * this function under those circumstances is an internal error.
4527 */
a0377dfe 4528 LTTNG_ASSERT(agt);
989a0844
FD
4529 ret_code = trigger_agent_disable(trigger, agt);
4530 if (ret_code != LTTNG_OK) {
4531 goto end_unlock_session_list;
4532 }
4533
4534 break;
4535 }
4536 case LTTNG_DOMAIN_NONE:
4537 default:
4538 abort();
4539 }
4540
4541 ret_code = LTTNG_OK;
4542
9b7cbebd
JG
4543end_unlock_session_list:
4544 session_unlock_list();
4545 return ret_code;
b0880ae5
JG
4546}
4547
70670472 4548enum lttng_error_code cmd_unregister_trigger(const struct lttng_credentials *cmd_creds,
746e08d7 4549 const struct lttng_trigger *trigger,
b0880ae5
JG
4550 struct notification_thread_handle *notification_thread)
4551{
70670472 4552 enum lttng_error_code ret_code;
70670472
JR
4553 const char *trigger_name;
4554 uid_t trigger_owner;
4555 enum lttng_trigger_status trigger_status;
5c5373c3 4556 struct lttng_trigger *sessiond_trigger = NULL;
70670472
JR
4557
4558 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
0efb2ad7 4559 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)";
989a0844 4560 trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner);
a0377dfe 4561 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
70670472
JR
4562
4563 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4564 trigger_name, (int) trigger_owner,
4565 (int) lttng_credentials_get_uid(cmd_creds));
b0880ae5 4566
64eafdf6
JR
4567 /*
4568 * Validate the trigger credentials against the command credentials.
4569 * Only the root user can unregister a trigger with non-matching
4570 * credentials.
4571 */
4572 if (!lttng_credentials_is_equal_uid(
4573 lttng_trigger_get_credentials(trigger),
746e08d7
JG
4574 cmd_creds)) {
4575 if (lttng_credentials_get_uid(cmd_creds) != 0) {
70670472
JR
4576 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4577 trigger_name, (int) trigger_owner,
4578 (int) lttng_credentials_get_uid(cmd_creds));
4579 ret_code = LTTNG_ERR_INVALID_TRIGGER;
64eafdf6
JR
4580 goto end;
4581 }
4582 }
3da864a9 4583
5c5373c3
JR
4584 /* Fetch the sessiond side trigger object. */
4585 ret_code = notification_thread_command_get_trigger(
4586 notification_thread, trigger, &sessiond_trigger);
4587 if (ret_code != LTTNG_OK) {
4588 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4589 trigger_name, (int) trigger_owner, ret_code);
4590 goto end;
4591 }
4592
a0377dfe 4593 LTTNG_ASSERT(sessiond_trigger);
5c5373c3
JR
4594
4595 /*
4596 * From this point on, no matter what, consider the trigger
4597 * unregistered.
4598 *
4599 * We set the unregistered state of the sessiond side trigger object in
4600 * the client thread since we want to minimize the possibility of the
4601 * notification thread being stalled due to a long execution of an
4602 * action that required the trigger lock.
4603 */
4604 lttng_trigger_set_as_unregistered(sessiond_trigger);
4605
70670472
JR
4606 ret_code = notification_thread_command_unregister_trigger(notification_thread,
4607 trigger);
4608 if (ret_code != LTTNG_OK) {
ce0b1d61 4609 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
70670472 4610 trigger_name, (int) trigger_owner, ret_code);
13839b27 4611 goto end;
70670472
JR
4612 }
4613
4614 /*
4615 * Synchronize tracers if the trigger removes an event notifier.
44760c20
JR
4616 * Do this even if the trigger unregistration failed to at least stop
4617 * the tracers from producing notifications associated with this
4618 * event notifier.
70670472 4619 */
989a0844
FD
4620 if (lttng_trigger_needs_tracer_notifier(trigger)) {
4621 ret_code = synchronize_tracer_notifier_unregister(trigger);
4622 if (ret_code != LTTNG_OK) {
4623 ERR("Error unregistering trigger to tracer.");
4624 goto end;
70670472 4625 }
9b7cbebd 4626
70670472
JR
4627 }
4628
b0880ae5 4629end:
5c5373c3 4630 lttng_trigger_put(sessiond_trigger);
70670472 4631 return ret_code;
989a0844 4632}
b0880ae5 4633
ddd915a3 4634enum lttng_error_code cmd_list_triggers(struct command_ctx *cmd_ctx,
fbc9f37d
JR
4635 struct notification_thread_handle *notification_thread,
4636 struct lttng_triggers **return_triggers)
4637{
f2bda80e 4638 int ret;
fbc9f37d
JR
4639 enum lttng_error_code ret_code;
4640 struct lttng_triggers *triggers = NULL;
4641
4642 /* Get the set of triggers from the notification thread. */
4643 ret_code = notification_thread_command_list_triggers(
4644 notification_thread, cmd_ctx->creds.uid, &triggers);
4645 if (ret_code != LTTNG_OK) {
fbc9f37d
JR
4646 goto end;
4647 }
4648
f2bda80e
JG
4649 ret = lttng_triggers_remove_hidden_triggers(triggers);
4650 if (ret) {
4651 ret_code = LTTNG_ERR_UNK;
4652 goto end;
4653 }
4654
fbc9f37d
JR
4655 *return_triggers = triggers;
4656 triggers = NULL;
ddd915a3 4657 ret_code = LTTNG_OK;
fbc9f37d
JR
4658end:
4659 lttng_triggers_destroy(triggers);
ddd915a3 4660 return ret_code;
fbc9f37d 4661}
588c4b0d
JG
4662
4663enum lttng_error_code cmd_execute_error_query(const struct lttng_credentials *cmd_creds,
4664 const struct lttng_error_query *query,
4665 struct lttng_error_query_results **_results,
4666 struct notification_thread_handle *notification_thread)
4667{
4668 enum lttng_error_code ret_code;
4669 const struct lttng_trigger *query_target_trigger;
63dd3d7b 4670 const struct lttng_action *query_target_action = NULL;
588c4b0d
JG
4671 struct lttng_trigger *matching_trigger = NULL;
4672 const char *trigger_name;
4673 uid_t trigger_owner;
4674 enum lttng_trigger_status trigger_status;
4675 struct lttng_error_query_results *results = NULL;
4676
4677 switch (lttng_error_query_get_target_type(query)) {
4678 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER:
4679 query_target_trigger = lttng_error_query_trigger_borrow_target(query);
4680 break;
63dd3d7b
JG
4681 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION:
4682 query_target_trigger =
4683 lttng_error_query_condition_borrow_target(query);
4684 break;
588c4b0d
JG
4685 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION:
4686 query_target_trigger = lttng_error_query_action_borrow_trigger_target(
4687 query);
4688 break;
4689 default:
4690 abort();
4691 }
4692
a0377dfe 4693 LTTNG_ASSERT(query_target_trigger);
588c4b0d
JG
4694
4695 ret_code = notification_thread_command_get_trigger(notification_thread,
4696 query_target_trigger, &matching_trigger);
4697 if (ret_code != LTTNG_OK) {
4698 goto end;
4699 }
4700
4701 /* No longer needed. */
4702 query_target_trigger = NULL;
4703
4704 if (lttng_error_query_get_target_type(query) ==
4705 LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION) {
4706 /* Get the sessiond-side version of the target action. */
4707 query_target_action =
4708 lttng_error_query_action_borrow_action_target(
4709 query, matching_trigger);
4710 }
4711
4712 trigger_status = lttng_trigger_get_name(matching_trigger, &trigger_name);
4713 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ?
0efb2ad7 4714 trigger_name : "(anonymous)";
588c4b0d
JG
4715 trigger_status = lttng_trigger_get_owner_uid(matching_trigger,
4716 &trigger_owner);
a0377dfe 4717 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
588c4b0d
JG
4718
4719 results = lttng_error_query_results_create();
4720 if (!results) {
4721 ret_code = LTTNG_ERR_NOMEM;
4722 goto end;
4723 }
4724
4725 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4726 trigger_name, (int) trigger_owner,
4727 (int) lttng_credentials_get_uid(cmd_creds));
4728
4729 /*
4730 * Validate the trigger credentials against the command credentials.
4731 * Only the root user can target a trigger with non-matching
4732 * credentials.
4733 */
4734 if (!lttng_credentials_is_equal_uid(
4735 lttng_trigger_get_credentials(matching_trigger),
4736 cmd_creds)) {
4737 if (lttng_credentials_get_uid(cmd_creds) != 0) {
4738 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4739 trigger_name, (int) trigger_owner,
4740 (int) lttng_credentials_get_uid(cmd_creds));
4741 ret_code = LTTNG_ERR_INVALID_TRIGGER;
4742 goto end;
4743 }
4744 }
4745
4746 switch (lttng_error_query_get_target_type(query)) {
4747 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER:
4748 trigger_status = lttng_trigger_add_error_results(
4749 matching_trigger, results);
4750
4751 switch (trigger_status) {
4752 case LTTNG_TRIGGER_STATUS_OK:
4753 break;
4754 default:
4755 ret_code = LTTNG_ERR_UNK;
4756 goto end;
4757 }
4758
4759 break;
63dd3d7b
JG
4760 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION:
4761 {
4762 trigger_status = lttng_trigger_condition_add_error_results(
4763 matching_trigger, results);
4764
4765 switch (trigger_status) {
4766 case LTTNG_TRIGGER_STATUS_OK:
4767 break;
4768 default:
4769 ret_code = LTTNG_ERR_UNK;
4770 goto end;
4771 }
4772
4773 break;
4774 }
588c4b0d
JG
4775 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION:
4776 {
4777 const enum lttng_action_status action_status =
4778 lttng_action_add_error_query_results(
4779 query_target_action, results);
4780
4781 switch (action_status) {
4782 case LTTNG_ACTION_STATUS_OK:
4783 break;
4784 default:
4785 ret_code = LTTNG_ERR_UNK;
4786 goto end;
4787 }
4788
4789 break;
4790 }
4791 default:
ef4cf1d2 4792 abort();
588c4b0d
JG
4793 break;
4794 }
4795
4796 *_results = results;
4797 results = NULL;
4798 ret_code = LTTNG_OK;
4799end:
4800 lttng_trigger_put(matching_trigger);
4801 lttng_error_query_results_destroy(results);
4802 return ret_code;
4803}
4804
6dc3064a
DG
4805/*
4806 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4807 * snapshot output is *not* set with a remote destination.
4808 *
9a654598 4809 * Return LTTNG_OK on success or a LTTNG_ERR code.
6dc3064a 4810 */
9a654598 4811static enum lttng_error_code set_relayd_for_snapshot(
348a81dc 4812 struct consumer_output *output,
fb9a95c4 4813 const struct ltt_session *session)
6dc3064a 4814{
9a654598 4815 enum lttng_error_code status = LTTNG_OK;
6dc3064a
DG
4816 struct lttng_ht_iter iter;
4817 struct consumer_socket *socket;
1e791a74 4818 LTTNG_OPTIONAL(uint64_t) current_chunk_id = {};
6fa5fe7c 4819 const char *base_path;
6dc3064a 4820
a0377dfe
FD
4821 LTTNG_ASSERT(output);
4822 LTTNG_ASSERT(session);
6dc3064a
DG
4823
4824 DBG2("Set relayd object from snapshot output");
4825
1e791a74 4826 if (session->current_trace_chunk) {
348a81dc
JG
4827 enum lttng_trace_chunk_status chunk_status =
4828 lttng_trace_chunk_get_id(
4829 session->current_trace_chunk,
4830 &current_chunk_id.value);
1e791a74 4831
348a81dc 4832 if (chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK) {
1e791a74
JG
4833 current_chunk_id.is_set = true;
4834 } else {
4835 ERR("Failed to get current trace chunk id");
4836 status = LTTNG_ERR_UNK;
4837 goto error;
4838 }
4839 }
4840
6dc3064a 4841 /* Ignore if snapshot consumer output is not network. */
348a81dc 4842 if (output->type != CONSUMER_DST_NET) {
6dc3064a
DG
4843 goto error;
4844 }
4845
6fa5fe7c
MD
4846 /*
4847 * The snapshot record URI base path overrides the session
4848 * base path.
4849 */
4850 if (output->dst.net.control.subdir[0] != '\0') {
4851 base_path = output->dst.net.control.subdir;
4852 } else {
4853 base_path = session->base_path;
4854 }
4855
6dc3064a
DG
4856 /*
4857 * For each consumer socket, create and send the relayd object of the
4858 * snapshot output.
4859 */
4860 rcu_read_lock();
348a81dc 4861 cds_lfht_for_each_entry(output->socks->ht, &iter.iter,
5eecee74 4862 socket, node.node) {
ecd0f96d 4863 pthread_mutex_lock(socket->lock);
9a654598 4864 status = send_consumer_relayd_sockets(0, session->id,
348a81dc 4865 output, socket,
d3e2ba59 4866 session->name, session->hostname,
6fa5fe7c 4867 base_path,
1e791a74 4868 session->live_timer,
db1da059 4869 current_chunk_id.is_set ? &current_chunk_id.value : NULL,
46ef2188
MD
4870 session->creation_time,
4871 session->name_contains_creation_time);
ecd0f96d 4872 pthread_mutex_unlock(socket->lock);
9a654598 4873 if (status != LTTNG_OK) {
6dc3064a
DG
4874 rcu_read_unlock();
4875 goto error;
4876 }
4877 }
4878 rcu_read_unlock();
4879
4880error:
9a654598 4881 return status;
6dc3064a
DG
4882}
4883
4884/*
4885 * Record a kernel snapshot.
4886 *
fac41e72 4887 * Return LTTNG_OK on success or a LTTNG_ERR code.
6dc3064a 4888 */
fb9a95c4
JG
4889static enum lttng_error_code record_kernel_snapshot(
4890 struct ltt_kernel_session *ksess,
348a81dc 4891 const struct consumer_output *output,
fb9a95c4 4892 const struct ltt_session *session,
d07ceecd 4893 int wait, uint64_t nb_packets_per_stream)
6dc3064a 4894{
9a654598 4895 enum lttng_error_code status;
6dc3064a 4896
a0377dfe
FD
4897 LTTNG_ASSERT(ksess);
4898 LTTNG_ASSERT(output);
4899 LTTNG_ASSERT(session);
6dc3064a 4900
348a81dc
JG
4901 status = kernel_snapshot_record(
4902 ksess, output, wait, nb_packets_per_stream);
9a654598 4903 return status;
6dc3064a
DG
4904}
4905
4906/*
4907 * Record a UST snapshot.
4908 *
9a654598 4909 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
6dc3064a 4910 */
9a654598 4911static enum lttng_error_code record_ust_snapshot(struct ltt_ust_session *usess,
348a81dc
JG
4912 const struct consumer_output *output,
4913 const struct ltt_session *session,
4914 int wait, uint64_t nb_packets_per_stream)
6dc3064a 4915{
9a654598 4916 enum lttng_error_code status;
6dc3064a 4917
a0377dfe
FD
4918 LTTNG_ASSERT(usess);
4919 LTTNG_ASSERT(output);
4920 LTTNG_ASSERT(session);
6dc3064a 4921
348a81dc
JG
4922 status = ust_app_snapshot_record(
4923 usess, output, wait, nb_packets_per_stream);
9a654598 4924 return status;
6dc3064a
DG
4925}
4926
d07ceecd 4927static
fb9a95c4
JG
4928uint64_t get_session_size_one_more_packet_per_stream(
4929 const struct ltt_session *session, uint64_t cur_nr_packets)
68808f4e 4930{
d07ceecd 4931 uint64_t tot_size = 0;
68808f4e
DG
4932
4933 if (session->kernel_session) {
4934 struct ltt_kernel_channel *chan;
fb9a95c4
JG
4935 const struct ltt_kernel_session *ksess =
4936 session->kernel_session;
68808f4e 4937
68808f4e 4938 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
d07ceecd
MD
4939 if (cur_nr_packets >= chan->channel->attr.num_subbuf) {
4940 /*
4941 * Don't take channel into account if we
4942 * already grab all its packets.
4943 */
4944 continue;
68808f4e 4945 }
d07ceecd
MD
4946 tot_size += chan->channel->attr.subbuf_size
4947 * chan->stream_count;
68808f4e
DG
4948 }
4949 }
4950
4951 if (session->ust_session) {
fb9a95c4 4952 const struct ltt_ust_session *usess = session->ust_session;
68808f4e 4953
d07ceecd
MD
4954 tot_size += ust_app_get_size_one_more_packet_per_stream(usess,
4955 cur_nr_packets);
68808f4e
DG
4956 }
4957
d07ceecd 4958 return tot_size;
68808f4e
DG
4959}
4960
5c786ded 4961/*
d07ceecd
MD
4962 * Calculate the number of packets we can grab from each stream that
4963 * fits within the overall snapshot max size.
4964 *
4965 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
4966 * the number of packets per stream.
4967 *
4968 * TODO: this approach is not perfect: we consider the worse case
4969 * (packet filling the sub-buffers) as an upper bound, but we could do
4970 * better if we do this calculation while we actually grab the packet
4971 * content: we would know how much padding we don't actually store into
4972 * the file.
4973 *
4974 * This algorithm is currently bounded by the number of packets per
4975 * stream.
4976 *
4977 * Since we call this algorithm before actually grabbing the data, it's
4978 * an approximation: for instance, applications could appear/disappear
4979 * in between this call and actually grabbing data.
5c786ded 4980 */
d07ceecd 4981static
fb9a95c4
JG
4982int64_t get_session_nb_packets_per_stream(const struct ltt_session *session,
4983 uint64_t max_size)
5c786ded 4984{
d07ceecd
MD
4985 int64_t size_left;
4986 uint64_t cur_nb_packets = 0;
5c786ded 4987
d07ceecd
MD
4988 if (!max_size) {
4989 return 0; /* Infinite */
5c786ded
JD
4990 }
4991
d07ceecd
MD
4992 size_left = max_size;
4993 for (;;) {
4994 uint64_t one_more_packet_tot_size;
5c786ded 4995
fb9a95c4
JG
4996 one_more_packet_tot_size = get_session_size_one_more_packet_per_stream(
4997 session, cur_nb_packets);
d07ceecd
MD
4998 if (!one_more_packet_tot_size) {
4999 /* We are already grabbing all packets. */
5000 break;
5001 }
5002 size_left -= one_more_packet_tot_size;
5003 if (size_left < 0) {
5004 break;
5005 }
5006 cur_nb_packets++;
5c786ded 5007 }
aecf2da5 5008 if (!cur_nb_packets && size_left != max_size) {
d07ceecd
MD
5009 /* Not enough room to grab one packet of each stream, error. */
5010 return -1;
5011 }
5012 return cur_nb_packets;
5c786ded
JD
5013}
5014
fb9a95c4 5015static
d2956687 5016enum lttng_error_code snapshot_record(struct ltt_session *session,
fb9a95c4
JG
5017 const struct snapshot_output *snapshot_output, int wait)
5018{
5019 int64_t nb_packets_per_stream;
d2956687 5020 char snapshot_chunk_name[LTTNG_NAME_MAX];
348a81dc
JG
5021 int ret;
5022 enum lttng_error_code ret_code = LTTNG_OK;
d2956687 5023 struct lttng_trace_chunk *snapshot_trace_chunk;
348a81dc
JG
5024 struct consumer_output *original_ust_consumer_output = NULL;
5025 struct consumer_output *original_kernel_consumer_output = NULL;
5026 struct consumer_output *snapshot_ust_consumer_output = NULL;
5027 struct consumer_output *snapshot_kernel_consumer_output = NULL;
d2956687 5028
348a81dc 5029 ret = snprintf(snapshot_chunk_name, sizeof(snapshot_chunk_name),
d2956687
JG
5030 "%s-%s-%" PRIu64,
5031 snapshot_output->name,
5032 snapshot_output->datetime,
5033 snapshot_output->nb_snapshot);
348a81dc 5034 if (ret < 0 || ret >= sizeof(snapshot_chunk_name)) {
d2956687 5035 ERR("Failed to format snapshot name");
348a81dc
JG
5036 ret_code = LTTNG_ERR_INVALID;
5037 goto error;
d2956687
JG
5038 }
5039 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5040 snapshot_output->name, session->name,
5041 snapshot_chunk_name);
348a81dc
JG
5042 if (!session->kernel_session && !session->ust_session) {
5043 ERR("Failed to record snapshot as no channels exist");
5044 ret_code = LTTNG_ERR_NO_CHANNEL;
5045 goto error;
5046 }
5047
5048 if (session->kernel_session) {
5049 original_kernel_consumer_output =
5050 session->kernel_session->consumer;
5051 snapshot_kernel_consumer_output =
5052 consumer_copy_output(snapshot_output->consumer);
3b967712
MD
5053 strcpy(snapshot_kernel_consumer_output->chunk_path,
5054 snapshot_chunk_name);
bd666153
JR
5055
5056 /* Copy the original domain subdir. */
5057 strcpy(snapshot_kernel_consumer_output->domain_subdir,
5058 original_kernel_consumer_output->domain_subdir);
5059
348a81dc
JG
5060 ret = consumer_copy_sockets(snapshot_kernel_consumer_output,
5061 original_kernel_consumer_output);
5062 if (ret < 0) {
5063 ERR("Failed to copy consumer sockets from snapshot output configuration");
5064 ret_code = LTTNG_ERR_NOMEM;
5065 goto error;
5066 }
5067 ret_code = set_relayd_for_snapshot(
5068 snapshot_kernel_consumer_output, session);
5069 if (ret_code != LTTNG_OK) {
5070 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5071 goto error;
5072 }
5073 session->kernel_session->consumer =
5074 snapshot_kernel_consumer_output;
5075 }
5076 if (session->ust_session) {
5077 original_ust_consumer_output = session->ust_session->consumer;
5078 snapshot_ust_consumer_output =
5079 consumer_copy_output(snapshot_output->consumer);
3b967712
MD
5080 strcpy(snapshot_ust_consumer_output->chunk_path,
5081 snapshot_chunk_name);
bd666153
JR
5082
5083 /* Copy the original domain subdir. */
5084 strcpy(snapshot_ust_consumer_output->domain_subdir,
5085 original_ust_consumer_output->domain_subdir);
5086
348a81dc
JG
5087 ret = consumer_copy_sockets(snapshot_ust_consumer_output,
5088 original_ust_consumer_output);
5089 if (ret < 0) {
5090 ERR("Failed to copy consumer sockets from snapshot output configuration");
5091 ret_code = LTTNG_ERR_NOMEM;
5092 goto error;
5093 }
5094 ret_code = set_relayd_for_snapshot(
5095 snapshot_ust_consumer_output, session);
5096 if (ret_code != LTTNG_OK) {
5097 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5098 goto error;
5099 }
5100 session->ust_session->consumer =
5101 snapshot_ust_consumer_output;
5102 }
5103
d2956687 5104 snapshot_trace_chunk = session_create_new_trace_chunk(session,
348a81dc
JG
5105 snapshot_kernel_consumer_output ?:
5106 snapshot_ust_consumer_output,
5107 consumer_output_get_base_path(
5108 snapshot_output->consumer),
d2956687
JG
5109 snapshot_chunk_name);
5110 if (!snapshot_trace_chunk) {
348a81dc
JG
5111 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5112 session->name);
5113 ret_code = LTTNG_ERR_CREATE_DIR_FAIL;
5114 goto error;
d2956687 5115 }
a0377dfe 5116 LTTNG_ASSERT(!session->current_trace_chunk);
d2956687
JG
5117 ret = session_set_trace_chunk(session, snapshot_trace_chunk, NULL);
5118 lttng_trace_chunk_put(snapshot_trace_chunk);
5119 snapshot_trace_chunk = NULL;
5120 if (ret) {
348a81dc
JG
5121 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5122 session->name);
5123 ret_code = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
5124 goto error;
d2956687 5125 }
fb9a95c4
JG
5126
5127 nb_packets_per_stream = get_session_nb_packets_per_stream(session,
5128 snapshot_output->max_size);
5129 if (nb_packets_per_stream < 0) {
348a81dc 5130 ret_code = LTTNG_ERR_MAX_SIZE_INVALID;
5151d412 5131 goto error_close_trace_chunk;
fb9a95c4
JG
5132 }
5133
5134 if (session->kernel_session) {
348a81dc
JG
5135 ret_code = record_kernel_snapshot(session->kernel_session,
5136 snapshot_kernel_consumer_output, session,
fb9a95c4 5137 wait, nb_packets_per_stream);
348a81dc 5138 if (ret_code != LTTNG_OK) {
5151d412 5139 goto error_close_trace_chunk;
fb9a95c4
JG
5140 }
5141 }
5142
5143 if (session->ust_session) {
348a81dc
JG
5144 ret_code = record_ust_snapshot(session->ust_session,
5145 snapshot_ust_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 }
d2956687 5151
5151d412 5152error_close_trace_chunk:
dbfee52c
MD
5153 if (session_set_trace_chunk(session, NULL, &snapshot_trace_chunk)) {
5154 ERR("Failed to release the current trace chunk of session \"%s\"",
5155 session->name);
5156 ret_code = LTTNG_ERR_UNK;
5157 }
5158
5159 if (session_close_trace_chunk(session, snapshot_trace_chunk,
343defc2 5160 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION, NULL)) {
d2956687
JG
5161 /*
5162 * Don't goto end; make sure the chunk is closed for the session
5163 * to allow future snapshots.
5164 */
5165 ERR("Failed to close snapshot trace chunk of session \"%s\"",
5166 session->name);
348a81dc 5167 ret_code = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER;
d2956687 5168 }
348a81dc
JG
5169error:
5170 if (original_ust_consumer_output) {
5171 session->ust_session->consumer = original_ust_consumer_output;
5172 }
5173 if (original_kernel_consumer_output) {
5174 session->kernel_session->consumer =
5175 original_kernel_consumer_output;
5176 }
5177 consumer_output_put(snapshot_ust_consumer_output);
5178 consumer_output_put(snapshot_kernel_consumer_output);
5179 return ret_code;
fb9a95c4
JG
5180}
5181
6dc3064a
DG
5182/*
5183 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5184 *
5185 * The wait parameter is ignored so this call always wait for the snapshot to
5186 * complete before returning.
5187 *
5188 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5189 */
5190int cmd_snapshot_record(struct ltt_session *session,
df4f5a87 5191 const struct lttng_snapshot_output *output, int wait)
6dc3064a 5192{
9a654598
JG
5193 enum lttng_error_code cmd_ret = LTTNG_OK;
5194 int ret;
00e1dfc4 5195 unsigned int snapshot_success = 0;
10ba83fe 5196 char datetime[16];
2abe7969 5197 struct snapshot_output *tmp_output = NULL;
6dc3064a 5198
a0377dfe
FD
5199 LTTNG_ASSERT(session);
5200 LTTNG_ASSERT(output);
6dc3064a
DG
5201
5202 DBG("Cmd snapshot record for session %s", session->name);
5203
10ba83fe
JR
5204 /* Get the datetime for the snapshot output directory. */
5205 ret = utils_get_current_time_str("%Y%m%d-%H%M%S", datetime,
5206 sizeof(datetime));
5207 if (!ret) {
9a654598 5208 cmd_ret = LTTNG_ERR_INVALID;
10ba83fe
JR
5209 goto error;
5210 }
5211
6dc3064a 5212 /*
d3f14b8a
MD
5213 * Permission denied to create an output if the session is not
5214 * set in no output mode.
6dc3064a
DG
5215 */
5216 if (session->output_traces) {
9a654598 5217 cmd_ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
6dc3064a
DG
5218 goto error;
5219 }
5220
5221 /* The session needs to be started at least once. */
8382cf6f 5222 if (!session->has_been_started) {
9a654598 5223 cmd_ret = LTTNG_ERR_START_SESSION_ONCE;
6dc3064a
DG
5224 goto error;
5225 }
5226
5227 /* Use temporary output for the session. */
ba45d9f0 5228 if (*output->ctrl_url != '\0') {
2abe7969
JG
5229 tmp_output = snapshot_output_alloc();
5230 if (!tmp_output) {
5231 cmd_ret = LTTNG_ERR_NOMEM;
5232 goto error;
5233 }
5234
b178f53e
JG
5235 ret = snapshot_output_init(session, output->max_size,
5236 output->name,
5237 output->ctrl_url, output->data_url,
5238 session->consumer,
2abe7969 5239 tmp_output, NULL);
6dc3064a
DG
5240 if (ret < 0) {
5241 if (ret == -ENOMEM) {
9a654598 5242 cmd_ret = LTTNG_ERR_NOMEM;
6dc3064a 5243 } else {
9a654598 5244 cmd_ret = LTTNG_ERR_INVALID;
6dc3064a
DG
5245 }
5246 goto error;
5247 }
1bfe7328 5248 /* Use the global session count for the temporary snapshot. */
2abe7969 5249 tmp_output->nb_snapshot = session->snapshot.nb_snapshot;
10ba83fe
JR
5250
5251 /* Use the global datetime */
2abe7969
JG
5252 memcpy(tmp_output->datetime, datetime, sizeof(datetime));
5253 cmd_ret = snapshot_record(session, tmp_output, wait);
fb9a95c4 5254 if (cmd_ret != LTTNG_OK) {
804c90a8
JR
5255 goto error;
5256 }
804c90a8
JR
5257 snapshot_success = 1;
5258 } else {
5259 struct snapshot_output *sout;
5260 struct lttng_ht_iter iter;
68808f4e 5261
804c90a8
JR
5262 rcu_read_lock();
5263 cds_lfht_for_each_entry(session->snapshot.output_ht->ht,
5264 &iter.iter, sout, node.node) {
2abe7969
JG
5265 struct snapshot_output output_copy;
5266
804c90a8 5267 /*
2abe7969
JG
5268 * Make a local copy of the output and override output
5269 * parameters with those provided as part of the
5270 * command.
804c90a8 5271 */
2abe7969 5272 memcpy(&output_copy, sout, sizeof(output_copy));
1bfe7328 5273
804c90a8 5274 if (output->max_size != (uint64_t) -1ULL) {
2abe7969 5275 output_copy.max_size = output->max_size;
6dc3064a 5276 }
d07ceecd 5277
2abe7969
JG
5278 output_copy.nb_snapshot = session->snapshot.nb_snapshot;
5279 memcpy(output_copy.datetime, datetime,
5280 sizeof(datetime));
6dc3064a 5281
804c90a8
JR
5282 /* Use temporary name. */
5283 if (*output->name != '\0') {
2abe7969
JG
5284 if (lttng_strncpy(output_copy.name,
5285 output->name,
5286 sizeof(output_copy.name))) {
9a654598 5287 cmd_ret = LTTNG_ERR_INVALID;
cf3e357d
MD
5288 rcu_read_unlock();
5289 goto error;
5290 }
804c90a8 5291 }
e1986656 5292
2abe7969 5293 cmd_ret = snapshot_record(session, &output_copy, wait);
fb9a95c4
JG
5294 if (cmd_ret != LTTNG_OK) {
5295 rcu_read_unlock();
5296 goto error;
6dc3064a 5297 }
804c90a8 5298 snapshot_success = 1;
6dc3064a 5299 }
804c90a8 5300 rcu_read_unlock();
6dc3064a
DG
5301 }
5302
1bfe7328
DG
5303 if (snapshot_success) {
5304 session->snapshot.nb_snapshot++;
b67578cb 5305 } else {
9a654598 5306 cmd_ret = LTTNG_ERR_SNAPSHOT_FAIL;
1bfe7328
DG
5307 }
5308
6dc3064a 5309error:
2abe7969
JG
5310 if (tmp_output) {
5311 snapshot_output_destroy(tmp_output);
5312 }
9a654598 5313 return cmd_ret;
6dc3064a
DG
5314}
5315
d7ba1388
MD
5316/*
5317 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5318 */
5319int cmd_set_session_shm_path(struct ltt_session *session,
5320 const char *shm_path)
5321{
5322 /* Safety net */
a0377dfe 5323 LTTNG_ASSERT(session);
d7ba1388
MD
5324
5325 /*
5326 * Can only set shm path before session is started.
5327 */
5328 if (session->has_been_started) {
5329 return LTTNG_ERR_SESSION_STARTED;
5330 }
5331
5332 strncpy(session->shm_path, shm_path,
5333 sizeof(session->shm_path));
5334 session->shm_path[sizeof(session->shm_path) - 1] = '\0';
5335
7e397c55 5336 return LTTNG_OK;
d7ba1388
MD
5337}
5338
5c408ad8
JD
5339/*
5340 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5341 *
5342 * Ask the consumer to rotate the session output directory.
5343 * The session lock must be held.
5344 *
d5a1b7aa 5345 * Returns LTTNG_OK on success or else a negative LTTng error code.
5c408ad8
JD
5346 */
5347int cmd_rotate_session(struct ltt_session *session,
7fdbed1c 5348 struct lttng_rotate_session_return *rotate_return,
343defc2
MD
5349 bool quiet_rotation,
5350 enum lttng_trace_chunk_command_type command)
5c408ad8
JD
5351{
5352 int ret;
d2956687 5353 uint64_t ongoing_rotation_chunk_id;
d5a1b7aa 5354 enum lttng_error_code cmd_ret = LTTNG_OK;
d2956687
JG
5355 struct lttng_trace_chunk *chunk_being_archived = NULL;
5356 struct lttng_trace_chunk *new_trace_chunk = NULL;
5357 enum lttng_trace_chunk_status chunk_status;
3156892b
JG
5358 bool failed_to_rotate = false;
5359 enum lttng_error_code rotation_fail_code = LTTNG_OK;
5c408ad8 5360
a0377dfe 5361 LTTNG_ASSERT(session);
5c408ad8
JD
5362
5363 if (!session->has_been_started) {
d5a1b7aa 5364 cmd_ret = LTTNG_ERR_START_SESSION_ONCE;
d68c9a04 5365 goto end;
5c408ad8
JD
5366 }
5367
d48d65e1
MD
5368 /*
5369 * Explicit rotation is not supported for live sessions.
5370 * However, live sessions can perform a quiet rotation on
5371 * destroy.
5372 * Rotation is not supported for snapshot traces (no output).
5373 */
5374 if ((!quiet_rotation && session->live_timer) ||
5375 !session->output_traces) {
d5a1b7aa 5376 cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE;
d68c9a04 5377 goto end;
5c408ad8
JD
5378 }
5379
d2956687 5380 /* Unsupported feature in lttng-relayd before 2.11. */
070b6a86 5381 if (!quiet_rotation && session->consumer->type == CONSUMER_DST_NET &&
5c408ad8
JD
5382 (session->consumer->relay_major_version == 2 &&
5383 session->consumer->relay_minor_version < 11)) {
d5a1b7aa 5384 cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY;
d68c9a04 5385 goto end;
5c408ad8
JD
5386 }
5387
a40a503f
MD
5388 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5389 if (session->kernel_session && !kernel_supports_ring_buffer_packet_sequence_number()) {
5390 cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL;
5391 goto end;
5392 }
5393
92816cc3 5394 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
92816cc3
JG
5395 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5396 session->name);
d5a1b7aa 5397 cmd_ret = LTTNG_ERR_ROTATION_PENDING;
d68c9a04 5398 goto end;
5c408ad8
JD
5399 }
5400
5401 /*
5402 * After a stop, we only allow one rotation to occur, the other ones are
5403 * useless until a new start.
5404 */
5405 if (session->rotated_after_last_stop) {
5406 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5407 session->name);
d5a1b7aa 5408 cmd_ret = LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP;
d68c9a04 5409 goto end;
5c408ad8 5410 }
b02f5986
MD
5411
5412 /*
5413 * After a stop followed by a clear, disallow following rotations a they would
5414 * generate empty chunks.
5415 */
5416 if (session->cleared_after_last_stop) {
5417 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5418 session->name);
5419 cmd_ret = LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR;
5420 goto end;
5421 }
5422
d2956687 5423 if (session->active) {
348a81dc 5424 new_trace_chunk = session_create_new_trace_chunk(session, NULL,
d2956687
JG
5425 NULL, NULL);
5426 if (!new_trace_chunk) {
5427 cmd_ret = LTTNG_ERR_CREATE_DIR_FAIL;
5428 goto error;
5c408ad8 5429 }
0e270a1e 5430 }
2961f09e 5431
3156892b
JG
5432 /*
5433 * The current trace chunk becomes the chunk being archived.
5434 *
5435 * After this point, "chunk_being_archived" must absolutely
5436 * be closed on the consumer(s), otherwise it will never be
5437 * cleaned-up, which will result in a leak.
5438 */
d2956687
JG
5439 ret = session_set_trace_chunk(session, new_trace_chunk,
5440 &chunk_being_archived);
5441 if (ret) {
5442 cmd_ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
b178f53e
JG
5443 goto error;
5444 }
5445
5c408ad8 5446 if (session->kernel_session) {
d5a1b7aa
JG
5447 cmd_ret = kernel_rotate_session(session);
5448 if (cmd_ret != LTTNG_OK) {
3156892b
JG
5449 failed_to_rotate = true;
5450 rotation_fail_code = cmd_ret;
5c408ad8
JD
5451 }
5452 }
5453 if (session->ust_session) {
d5a1b7aa
JG
5454 cmd_ret = ust_app_rotate_session(session);
5455 if (cmd_ret != LTTNG_OK) {
3156892b
JG
5456 failed_to_rotate = true;
5457 rotation_fail_code = cmd_ret;
5c408ad8 5458 }
92816cc3 5459 }
17dd1232 5460
3b61d9ee
JG
5461 if (!session->active) {
5462 session->rotated_after_last_stop = true;
5463 }
5464
5465 if (!chunk_being_archived) {
5466 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5467 session->name);
5468 if (failed_to_rotate) {
5469 cmd_ret = rotation_fail_code;
5470 goto error;
5471 }
5472 cmd_ret = LTTNG_OK;
5473 goto end;
5474 }
5475
5476 session->rotation_state = LTTNG_ROTATION_STATE_ONGOING;
5477 chunk_status = lttng_trace_chunk_get_id(chunk_being_archived,
5478 &ongoing_rotation_chunk_id);
a0377dfe 5479 LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
3b61d9ee 5480
bbc4768c 5481 ret = session_close_trace_chunk(session, chunk_being_archived,
343defc2 5482 command, session->last_chunk_path);
d2956687
JG
5483 if (ret) {
5484 cmd_ret = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER;
5485 goto error;
5486 }
5487
3156892b
JG
5488 if (failed_to_rotate) {
5489 cmd_ret = rotation_fail_code;
5490 goto error;
5491 }
5492
7fdbed1c 5493 session->quiet_rotation = quiet_rotation;
92816cc3
JG
5494 ret = timer_session_rotation_pending_check_start(session,
5495 DEFAULT_ROTATE_PENDING_TIMER);
5496 if (ret) {
d5a1b7aa 5497 cmd_ret = LTTNG_ERR_UNK;
2961f09e 5498 goto error;
5c408ad8
JD
5499 }
5500
5c408ad8 5501 if (rotate_return) {
d2956687 5502 rotate_return->rotation_id = ongoing_rotation_chunk_id;
5c408ad8
JD
5503 }
5504
d2956687
JG
5505 session->chunk_being_archived = chunk_being_archived;
5506 chunk_being_archived = NULL;
7fdbed1c
JG
5507 if (!quiet_rotation) {
5508 ret = notification_thread_command_session_rotation_ongoing(
412d7227
SM
5509 the_notification_thread_handle, session->name,
5510 session->uid, session->gid,
7fdbed1c
JG
5511 ongoing_rotation_chunk_id);
5512 if (ret != LTTNG_OK) {
5513 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5514 session->name);
5515 cmd_ret = ret;
5516 }
2961f09e
JG
5517 }
5518
92816cc3 5519 DBG("Cmd rotate session %s, archive_id %" PRIu64 " sent",
d2956687 5520 session->name, ongoing_rotation_chunk_id);
5c408ad8 5521end:
d2956687
JG
5522 lttng_trace_chunk_put(new_trace_chunk);
5523 lttng_trace_chunk_put(chunk_being_archived);
d5a1b7aa 5524 ret = (cmd_ret == LTTNG_OK) ? cmd_ret : -((int) cmd_ret);
5c408ad8 5525 return ret;
2961f09e 5526error:
2961f09e 5527 if (session_reset_rotation_state(session,
d2956687 5528 LTTNG_ROTATION_STATE_ERROR)) {
2961f09e
JG
5529 ERR("Failed to reset rotation state of session \"%s\"",
5530 session->name);
5531 }
5532 goto end;
5c408ad8
JD
5533}
5534
5535/*
d68c9a04 5536 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5c408ad8
JD
5537 *
5538 * Check if the session has finished its rotation.
5539 *
d2956687 5540 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5c408ad8 5541 */
d68c9a04
JD
5542int cmd_rotate_get_info(struct ltt_session *session,
5543 struct lttng_rotation_get_info_return *info_return,
5544 uint64_t rotation_id)
5c408ad8 5545{
d2956687
JG
5546 enum lttng_error_code cmd_ret = LTTNG_OK;
5547 enum lttng_rotation_state rotation_state;
5c408ad8 5548
d68c9a04 5549 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64, session->name,
d2956687 5550 session->most_recent_chunk_id.value);
5c408ad8 5551
d2956687
JG
5552 if (session->chunk_being_archived) {
5553 enum lttng_trace_chunk_status chunk_status;
5554 uint64_t chunk_id;
5555
5556 chunk_status = lttng_trace_chunk_get_id(
5557 session->chunk_being_archived,
5558 &chunk_id);
a0377dfe 5559 LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
d2956687
JG
5560
5561 rotation_state = rotation_id == chunk_id ?
5562 LTTNG_ROTATION_STATE_ONGOING :
5563 LTTNG_ROTATION_STATE_EXPIRED;
5564 } else {
5565 if (session->last_archived_chunk_id.is_set &&
5566 rotation_id != session->last_archived_chunk_id.value) {
5567 rotation_state = LTTNG_ROTATION_STATE_EXPIRED;
5568 } else {
5569 rotation_state = session->rotation_state;
5570 }
5c408ad8
JD
5571 }
5572
d2956687
JG
5573 switch (rotation_state) {
5574 case LTTNG_ROTATION_STATE_NO_ROTATION:
83ed9e90 5575 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
d2956687
JG
5576 session->name);
5577 goto end;
5578 case LTTNG_ROTATION_STATE_EXPIRED:
5579 DBG("Reporting that the rotation state of rotation id %" PRIu64 " of session \"%s\" has expired",
5580 rotation_id, session->name);
5581 break;
d68c9a04 5582 case LTTNG_ROTATION_STATE_ONGOING:
d2956687 5583 DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is still pending",
d68c9a04
JD
5584 rotation_id, session->name);
5585 break;
5586 case LTTNG_ROTATION_STATE_COMPLETED:
dd73d57b 5587 {
d2956687
JG
5588 int fmt_ret;
5589 char *chunk_path;
dd73d57b
JG
5590 char *current_tracing_path_reply;
5591 size_t current_tracing_path_reply_len;
5592
d2956687
JG
5593 DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is completed",
5594 rotation_id, session->name);
5595
dd73d57b
JG
5596 switch (session_get_consumer_destination_type(session)) {
5597 case CONSUMER_DST_LOCAL:
5598 current_tracing_path_reply =
5599 info_return->location.local.absolute_path;
5600 current_tracing_path_reply_len =
5601 sizeof(info_return->location.local.absolute_path);
5602 info_return->location_type =
05f8afa9 5603 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL;
ecd1a12f
MD
5604 fmt_ret = asprintf(&chunk_path,
5605 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY "/%s",
5606 session_get_base_path(session),
5607 session->last_archived_chunk_name);
5608 if (fmt_ret == -1) {
5609 PERROR("Failed to format the path of the last archived trace chunk");
5610 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5611 cmd_ret = LTTNG_ERR_UNK;
5612 goto end;
5613 }
dd73d57b
JG
5614 break;
5615 case CONSUMER_DST_NET:
09cfbe47
JG
5616 {
5617 uint16_t ctrl_port, data_port;
5618
dd73d57b
JG
5619 current_tracing_path_reply =
5620 info_return->location.relay.relative_path;
5621 current_tracing_path_reply_len =
5622 sizeof(info_return->location.relay.relative_path);
5623 /* Currently the only supported relay protocol. */
5624 info_return->location.relay.protocol =
05f8afa9 5625 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP;
dd73d57b 5626
d2956687 5627 fmt_ret = lttng_strncpy(info_return->location.relay.host,
dd73d57b
JG
5628 session_get_net_consumer_hostname(session),
5629 sizeof(info_return->location.relay.host));
d2956687
JG
5630 if (fmt_ret) {
5631 ERR("Failed to copy host name to rotate_get_info reply");
dd73d57b 5632 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
d2956687 5633 cmd_ret = LTTNG_ERR_SET_URL;
dd73d57b
JG
5634 goto end;
5635 }
5636
09cfbe47
JG
5637 session_get_net_consumer_ports(session, &ctrl_port, &data_port);
5638 info_return->location.relay.ports.control = ctrl_port;
5639 info_return->location.relay.ports.data = data_port;
dd73d57b 5640 info_return->location_type =
05f8afa9 5641 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY;
ecd1a12f
MD
5642 chunk_path = strdup(session->last_chunk_path);
5643 if (!chunk_path) {
5644 ERR("Failed to allocate the path of the last archived trace chunk");
5645 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5646 cmd_ret = LTTNG_ERR_UNK;
5647 goto end;
5648 }
dd73d57b 5649 break;
09cfbe47 5650 }
dd73d57b
JG
5651 default:
5652 abort();
5653 }
d2956687
JG
5654
5655 fmt_ret = lttng_strncpy(current_tracing_path_reply,
5656 chunk_path, current_tracing_path_reply_len);
5657 free(chunk_path);
5658 if (fmt_ret) {
5659 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
d68c9a04 5660 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
d2956687 5661 cmd_ret = LTTNG_ERR_UNK;
5c408ad8
JD
5662 goto end;
5663 }
dd73d57b 5664
d68c9a04 5665 break;
dd73d57b 5666 }
d68c9a04 5667 case LTTNG_ROTATION_STATE_ERROR:
d2956687 5668 DBG("Reporting that an error occurred during rotation %" PRIu64 " of session \"%s\"",
d68c9a04
JD
5669 rotation_id, session->name);
5670 break;
5671 default:
5672 abort();
5c408ad8
JD
5673 }
5674
d2956687 5675 cmd_ret = LTTNG_OK;
5c408ad8 5676end:
d2956687
JG
5677 info_return->status = (int32_t) rotation_state;
5678 return cmd_ret;
5c408ad8
JD
5679}
5680
259c2674
JD
5681/*
5682 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5683 *
5684 * Configure the automatic rotation parameters.
66ea93b1
JG
5685 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5686 * 'activate' to false means deactivate the rotation schedule and validate that
5687 * 'new_value' has the same value as the currently active value.
259c2674 5688 *
1136f41b 5689 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
259c2674
JD
5690 */
5691int cmd_rotation_set_schedule(struct ltt_session *session,
66ea93b1
JG
5692 bool activate, enum lttng_rotation_schedule_type schedule_type,
5693 uint64_t new_value,
90936dcf 5694 struct notification_thread_handle *notification_thread_handle)
259c2674
JD
5695{
5696 int ret;
66ea93b1 5697 uint64_t *parameter_value;
259c2674 5698
a0377dfe 5699 LTTNG_ASSERT(session);
259c2674
JD
5700
5701 DBG("Cmd rotate set schedule session %s", session->name);
5702
92fe5ca1 5703 if (session->live_timer || !session->output_traces) {
66ea93b1 5704 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
259c2674
JD
5705 ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE;
5706 goto end;
5707 }
5708
66ea93b1
JG
5709 switch (schedule_type) {
5710 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
5711 parameter_value = &session->rotate_size;
5712 break;
5713 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
5714 parameter_value = &session->rotate_timer_period;
5715 if (new_value >= UINT_MAX) {
5716 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64 " > %u (UINT_MAX)",
5717 new_value, UINT_MAX);
5718 ret = LTTNG_ERR_INVALID;
5719 goto end;
5720 }
5721 break;
5722 default:
5723 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5724 ret = LTTNG_ERR_INVALID;
259c2674 5725 goto end;
66ea93b1
JG
5726 }
5727
5728 /* Improper use of the API. */
5729 if (new_value == -1ULL) {
5730 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5731 ret = LTTNG_ERR_INVALID;
259c2674
JD
5732 goto end;
5733 }
5734
66ea93b1
JG
5735 /*
5736 * As indicated in struct ltt_session's comments, a value of == 0 means
5737 * this schedule rotation type is not in use.
5738 *
5739 * Reject the command if we were asked to activate a schedule that was
5740 * already active.
5741 */
5742 if (activate && *parameter_value != 0) {
5743 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5744 ret = LTTNG_ERR_ROTATION_SCHEDULE_SET;
90936dcf 5745 goto end;
66ea93b1
JG
5746 }
5747
5748 /*
5749 * Reject the command if we were asked to deactivate a schedule that was
5750 * not active.
5751 */
5752 if (!activate && *parameter_value == 0) {
5753 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5754 ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET;
90936dcf
JD
5755 goto end;
5756 }
5757
66ea93b1
JG
5758 /*
5759 * Reject the command if we were asked to deactivate a schedule that
5760 * doesn't exist.
5761 */
5762 if (!activate && *parameter_value != new_value) {
5763 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5764 ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET;
5765 goto end;
5766 }
259c2674 5767
66ea93b1
JG
5768 *parameter_value = activate ? new_value : 0;
5769
5770 switch (schedule_type) {
5771 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
5772 if (activate && session->active) {
5773 /*
5774 * Only start the timer if the session is active,
5775 * otherwise it will be started when the session starts.
5776 */
92816cc3
JG
5777 ret = timer_session_rotation_schedule_timer_start(
5778 session, new_value);
259c2674 5779 if (ret) {
66ea93b1 5780 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
259c2674
JD
5781 ret = LTTNG_ERR_UNK;
5782 goto end;
5783 }
66ea93b1 5784 } else {
92816cc3
JG
5785 ret = timer_session_rotation_schedule_timer_stop(
5786 session);
66ea93b1
JG
5787 if (ret) {
5788 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5789 ret = LTTNG_ERR_UNK;
f3ce6946 5790 goto end;
66ea93b1 5791 }
259c2674 5792 }
66ea93b1
JG
5793 break;
5794 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
5795 if (activate) {
5796 ret = subscribe_session_consumed_size_rotation(session,
5797 new_value, notification_thread_handle);
90936dcf 5798 if (ret) {
66ea93b1 5799 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
90936dcf
JD
5800 ret = LTTNG_ERR_UNK;
5801 goto end;
5802 }
90936dcf 5803 } else {
66ea93b1
JG
5804 ret = unsubscribe_session_consumed_size_rotation(session,
5805 notification_thread_handle);
90936dcf 5806 if (ret) {
66ea93b1 5807 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
90936dcf
JD
5808 ret = LTTNG_ERR_UNK;
5809 goto end;
5810 }
66ea93b1 5811
90936dcf 5812 }
66ea93b1
JG
5813 break;
5814 default:
5815 /* Would have been caught before. */
5816 abort();
90936dcf
JD
5817 }
5818
259c2674
JD
5819 ret = LTTNG_OK;
5820
5821 goto end;
5822
5823end:
5824 return ret;
5825}
5826
a503e1ef
JG
5827/* Wait for a given path to be removed before continuing. */
5828static enum lttng_error_code wait_on_path(void *path_data)
5829{
5830 const char *shm_path = path_data;
5831
5832 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5833 shm_path);
5834 while (true) {
5835 int ret;
5836 struct stat st;
5837
5838 ret = stat(shm_path, &st);
5839 if (ret) {
5840 if (errno != ENOENT) {
5841 PERROR("stat() returned an error while checking for the existence of the shm path");
5842 } else {
5843 DBG("shm path no longer exists, completing the destruction of session");
5844 }
5845 break;
5846 } else {
5847 if (!S_ISDIR(st.st_mode)) {
5848 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5849 shm_path);
5850 break;
5851 }
5852 }
5853 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US);
5854 }
5855 return LTTNG_OK;
5856}
5857
5858/*
5859 * Returns a pointer to a handler to run on completion of a command.
5860 * Returns NULL if no handler has to be run for the last command executed.
5861 */
5862const struct cmd_completion_handler *cmd_pop_completion_handler(void)
5863{
5864 struct cmd_completion_handler *handler = current_completion_handler;
5865
5866 current_completion_handler = NULL;
5867 return handler;
5868}
5869
2f77fc4b
DG
5870/*
5871 * Init command subsystem.
5872 */
5873void cmd_init(void)
5874{
5875 /*
d88aee68
DG
5876 * Set network sequence index to 1 for streams to match a relayd
5877 * socket on the consumer side.
2f77fc4b 5878 */
d88aee68
DG
5879 pthread_mutex_lock(&relayd_net_seq_idx_lock);
5880 relayd_net_seq_idx = 1;
5881 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
2f77fc4b
DG
5882
5883 DBG("Command subsystem initialized");
5884}
This page took 0.44593 seconds and 4 git commands to generate.