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