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