sessiond: setup event notifier group for registering app
[lttng-tools.git] / src / bin / lttng-sessiond / ust-app.c
CommitLineData
91d76f53 1/*
ab5be9fa
MJ
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
91d76f53 4 *
ab5be9fa 5 * SPDX-License-Identifier: GPL-2.0-only
91d76f53 6 *
91d76f53
DG
7 */
8
6c1c0768 9#define _LGPL_SOURCE
7972aab2 10#include <inttypes.h>
91d76f53
DG
11#include <pthread.h>
12#include <stdio.h>
13#include <stdlib.h>
099e26bd 14#include <string.h>
aba8e916
DG
15#include <sys/stat.h>
16#include <sys/types.h>
099e26bd 17#include <unistd.h>
0df502fd 18#include <urcu/compiler.h>
331744e3 19#include <signal.h>
bec39940 20
edf4b93e 21#include <common/compat/errno.h>
990570ed 22#include <common/common.h>
86acf0da 23#include <common/sessiond-comm/sessiond-comm.h>
1e307fab 24
7972aab2 25#include "buffer-registry.h"
86acf0da 26#include "fd-limit.h"
8782cc74 27#include "health-sessiond.h"
56fff090 28#include "ust-app.h"
48842b30 29#include "ust-consumer.h"
75018ab6
JG
30#include "lttng-ust-ctl.h"
31#include "lttng-ust-error.h"
0b2dc8df 32#include "utils.h"
fb83fe64 33#include "session.h"
e9404c27
JG
34#include "lttng-sessiond.h"
35#include "notification-thread-commands.h"
5c408ad8 36#include "rotate.h"
d80a6244 37
44cdb3a2
MJ
38struct lttng_ht *ust_app_ht;
39struct lttng_ht *ust_app_ht_by_sock;
40struct lttng_ht *ust_app_ht_by_notify_sock;
41
c4b88406
MD
42static
43int ust_app_flush_app_session(struct ust_app *app, struct ust_app_session *ua_sess);
44
d9bf3ca4
MD
45/* Next available channel key. Access under next_channel_key_lock. */
46static uint64_t _next_channel_key;
47static pthread_mutex_t next_channel_key_lock = PTHREAD_MUTEX_INITIALIZER;
48
49/* Next available session ID. Access under next_session_id_lock. */
50static uint64_t _next_session_id;
51static pthread_mutex_t next_session_id_lock = PTHREAD_MUTEX_INITIALIZER;
ffe60014
DG
52
53/*
d9bf3ca4 54 * Return the incremented value of next_channel_key.
ffe60014 55 */
d9bf3ca4 56static uint64_t get_next_channel_key(void)
ffe60014 57{
d9bf3ca4
MD
58 uint64_t ret;
59
60 pthread_mutex_lock(&next_channel_key_lock);
61 ret = ++_next_channel_key;
62 pthread_mutex_unlock(&next_channel_key_lock);
63 return ret;
ffe60014
DG
64}
65
66/*
7972aab2 67 * Return the atomically incremented value of next_session_id.
ffe60014 68 */
d9bf3ca4 69static uint64_t get_next_session_id(void)
ffe60014 70{
d9bf3ca4
MD
71 uint64_t ret;
72
73 pthread_mutex_lock(&next_session_id_lock);
74 ret = ++_next_session_id;
75 pthread_mutex_unlock(&next_session_id_lock);
76 return ret;
ffe60014
DG
77}
78
d65d2de8
DG
79static void copy_channel_attr_to_ustctl(
80 struct ustctl_consumer_channel_attr *attr,
81 struct lttng_ust_channel_attr *uattr)
82{
83 /* Copy event attributes since the layout is different. */
84 attr->subbuf_size = uattr->subbuf_size;
85 attr->num_subbuf = uattr->num_subbuf;
86 attr->overwrite = uattr->overwrite;
87 attr->switch_timer_interval = uattr->switch_timer_interval;
88 attr->read_timer_interval = uattr->read_timer_interval;
89 attr->output = uattr->output;
491d1539 90 attr->blocking_timeout = uattr->u.s.blocking_timeout;
d65d2de8
DG
91}
92
025faf73
DG
93/*
94 * Match function for the hash table lookup.
95 *
96 * It matches an ust app event based on three attributes which are the event
97 * name, the filter bytecode and the loglevel.
98 */
18eace3b
DG
99static int ht_match_ust_app_event(struct cds_lfht_node *node, const void *_key)
100{
101 struct ust_app_event *event;
102 const struct ust_app_ht_key *key;
2106efa0 103 int ev_loglevel_value;
18eace3b
DG
104
105 assert(node);
106 assert(_key);
107
108 event = caa_container_of(node, struct ust_app_event, node.node);
109 key = _key;
2106efa0 110 ev_loglevel_value = event->attr.loglevel;
18eace3b 111
1af53eb5 112 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
18eace3b
DG
113
114 /* Event name */
115 if (strncmp(event->attr.name, key->name, sizeof(event->attr.name)) != 0) {
116 goto no_match;
117 }
118
119 /* Event loglevel. */
2106efa0 120 if (ev_loglevel_value != key->loglevel_type) {
025faf73 121 if (event->attr.loglevel_type == LTTNG_UST_LOGLEVEL_ALL
2106efa0
PP
122 && key->loglevel_type == 0 &&
123 ev_loglevel_value == -1) {
025faf73
DG
124 /*
125 * Match is accepted. This is because on event creation, the
126 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
127 * -1 are accepted for this loglevel type since 0 is the one set by
128 * the API when receiving an enable event.
129 */
130 } else {
131 goto no_match;
132 }
18eace3b
DG
133 }
134
135 /* One of the filters is NULL, fail. */
136 if ((key->filter && !event->filter) || (!key->filter && event->filter)) {
137 goto no_match;
138 }
139
025faf73
DG
140 if (key->filter && event->filter) {
141 /* Both filters exists, check length followed by the bytecode. */
142 if (event->filter->len != key->filter->len ||
143 memcmp(event->filter->data, key->filter->data,
144 event->filter->len) != 0) {
145 goto no_match;
146 }
18eace3b
DG
147 }
148
1af53eb5
JI
149 /* One of the exclusions is NULL, fail. */
150 if ((key->exclusion && !event->exclusion) || (!key->exclusion && event->exclusion)) {
151 goto no_match;
152 }
153
154 if (key->exclusion && event->exclusion) {
155 /* Both exclusions exists, check count followed by the names. */
156 if (event->exclusion->count != key->exclusion->count ||
157 memcmp(event->exclusion->names, key->exclusion->names,
158 event->exclusion->count * LTTNG_UST_SYM_NAME_LEN) != 0) {
159 goto no_match;
160 }
161 }
162
163
025faf73 164 /* Match. */
18eace3b
DG
165 return 1;
166
167no_match:
168 return 0;
18eace3b
DG
169}
170
025faf73
DG
171/*
172 * Unique add of an ust app event in the given ht. This uses the custom
173 * ht_match_ust_app_event match function and the event name as hash.
174 */
d0b96690 175static void add_unique_ust_app_event(struct ust_app_channel *ua_chan,
18eace3b
DG
176 struct ust_app_event *event)
177{
178 struct cds_lfht_node *node_ptr;
179 struct ust_app_ht_key key;
d0b96690 180 struct lttng_ht *ht;
18eace3b 181
d0b96690
DG
182 assert(ua_chan);
183 assert(ua_chan->events);
18eace3b
DG
184 assert(event);
185
d0b96690 186 ht = ua_chan->events;
18eace3b
DG
187 key.name = event->attr.name;
188 key.filter = event->filter;
2106efa0 189 key.loglevel_type = event->attr.loglevel;
91c89f23 190 key.exclusion = event->exclusion;
18eace3b
DG
191
192 node_ptr = cds_lfht_add_unique(ht->ht,
193 ht->hash_fct(event->node.key, lttng_ht_seed),
194 ht_match_ust_app_event, &key, &event->node.node);
195 assert(node_ptr == &event->node.node);
196}
197
d88aee68
DG
198/*
199 * Close the notify socket from the given RCU head object. This MUST be called
200 * through a call_rcu().
201 */
202static void close_notify_sock_rcu(struct rcu_head *head)
203{
204 int ret;
205 struct ust_app_notify_sock_obj *obj =
206 caa_container_of(head, struct ust_app_notify_sock_obj, head);
207
208 /* Must have a valid fd here. */
209 assert(obj->fd >= 0);
210
211 ret = close(obj->fd);
212 if (ret) {
213 ERR("close notify sock %d RCU", obj->fd);
214 }
215 lttng_fd_put(LTTNG_FD_APPS, 1);
216
217 free(obj);
218}
219
7972aab2
DG
220/*
221 * Return the session registry according to the buffer type of the given
222 * session.
223 *
224 * A registry per UID object MUST exists before calling this function or else
225 * it assert() if not found. RCU read side lock must be acquired.
226 */
227static struct ust_registry_session *get_session_registry(
228 struct ust_app_session *ua_sess)
229{
230 struct ust_registry_session *registry = NULL;
231
232 assert(ua_sess);
233
234 switch (ua_sess->buffer_type) {
235 case LTTNG_BUFFER_PER_PID:
236 {
237 struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id);
238 if (!reg_pid) {
239 goto error;
240 }
241 registry = reg_pid->registry->reg.ust;
242 break;
243 }
244 case LTTNG_BUFFER_PER_UID:
245 {
246 struct buffer_reg_uid *reg_uid = buffer_reg_uid_find(
470cc211 247 ua_sess->tracing_id, ua_sess->bits_per_long,
ff588497 248 lttng_credentials_get_uid(&ua_sess->real_credentials));
7972aab2
DG
249 if (!reg_uid) {
250 goto error;
251 }
252 registry = reg_uid->registry->reg.ust;
253 break;
254 }
255 default:
256 assert(0);
257 };
258
259error:
260 return registry;
261}
262
55cc08a6
DG
263/*
264 * Delete ust context safely. RCU read lock must be held before calling
265 * this function.
266 */
267static
fb45065e
MD
268void delete_ust_app_ctx(int sock, struct ust_app_ctx *ua_ctx,
269 struct ust_app *app)
55cc08a6 270{
ffe60014
DG
271 int ret;
272
273 assert(ua_ctx);
274
55cc08a6 275 if (ua_ctx->obj) {
fb45065e 276 pthread_mutex_lock(&app->sock_lock);
ffe60014 277 ret = ustctl_release_object(sock, ua_ctx->obj);
fb45065e 278 pthread_mutex_unlock(&app->sock_lock);
d0b96690
DG
279 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
280 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
281 sock, ua_ctx->obj->handle, ret);
ffe60014 282 }
55cc08a6
DG
283 free(ua_ctx->obj);
284 }
285 free(ua_ctx);
286}
287
d80a6244
DG
288/*
289 * Delete ust app event safely. RCU read lock must be held before calling
290 * this function.
291 */
8b366481 292static
fb45065e
MD
293void delete_ust_app_event(int sock, struct ust_app_event *ua_event,
294 struct ust_app *app)
d80a6244 295{
ffe60014
DG
296 int ret;
297
298 assert(ua_event);
299
53a80697 300 free(ua_event->filter);
951f0b71
JI
301 if (ua_event->exclusion != NULL)
302 free(ua_event->exclusion);
edb67388 303 if (ua_event->obj != NULL) {
fb45065e 304 pthread_mutex_lock(&app->sock_lock);
ffe60014 305 ret = ustctl_release_object(sock, ua_event->obj);
fb45065e 306 pthread_mutex_unlock(&app->sock_lock);
ffe60014
DG
307 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
308 ERR("UST app sock %d release event obj failed with ret %d",
309 sock, ret);
310 }
edb67388
DG
311 free(ua_event->obj);
312 }
d80a6244
DG
313 free(ua_event);
314}
315
316/*
7972aab2
DG
317 * Release ust data object of the given stream.
318 *
319 * Return 0 on success or else a negative value.
d80a6244 320 */
fb45065e
MD
321static int release_ust_app_stream(int sock, struct ust_app_stream *stream,
322 struct ust_app *app)
d80a6244 323{
7972aab2 324 int ret = 0;
ffe60014
DG
325
326 assert(stream);
327
8b366481 328 if (stream->obj) {
fb45065e 329 pthread_mutex_lock(&app->sock_lock);
ffe60014 330 ret = ustctl_release_object(sock, stream->obj);
fb45065e 331 pthread_mutex_unlock(&app->sock_lock);
ffe60014
DG
332 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
333 ERR("UST app sock %d release stream obj failed with ret %d",
334 sock, ret);
335 }
4063050c 336 lttng_fd_put(LTTNG_FD_APPS, 2);
8b366481
DG
337 free(stream->obj);
338 }
7972aab2
DG
339
340 return ret;
341}
342
343/*
344 * Delete ust app stream safely. RCU read lock must be held before calling
345 * this function.
346 */
347static
fb45065e
MD
348void delete_ust_app_stream(int sock, struct ust_app_stream *stream,
349 struct ust_app *app)
7972aab2
DG
350{
351 assert(stream);
352
fb45065e 353 (void) release_ust_app_stream(sock, stream, app);
84cd17c6 354 free(stream);
d80a6244
DG
355}
356
36b588ed
MD
357/*
358 * We need to execute ht_destroy outside of RCU read-side critical
0b2dc8df
MD
359 * section and outside of call_rcu thread, so we postpone its execution
360 * using ht_cleanup_push. It is simpler than to change the semantic of
361 * the many callers of delete_ust_app_session().
36b588ed
MD
362 */
363static
364void delete_ust_app_channel_rcu(struct rcu_head *head)
365{
366 struct ust_app_channel *ua_chan =
367 caa_container_of(head, struct ust_app_channel, rcu_head);
368
0b2dc8df
MD
369 ht_cleanup_push(ua_chan->ctx);
370 ht_cleanup_push(ua_chan->events);
36b588ed
MD
371 free(ua_chan);
372}
373
fb83fe64
JD
374/*
375 * Extract the lost packet or discarded events counter when the channel is
376 * being deleted and store the value in the parent channel so we can
377 * access it from lttng list and at stop/destroy.
82cac6d2
JG
378 *
379 * The session list lock must be held by the caller.
fb83fe64
JD
380 */
381static
382void save_per_pid_lost_discarded_counters(struct ust_app_channel *ua_chan)
383{
384 uint64_t discarded = 0, lost = 0;
385 struct ltt_session *session;
386 struct ltt_ust_channel *uchan;
387
388 if (ua_chan->attr.type != LTTNG_UST_CHAN_PER_CPU) {
389 return;
390 }
391
392 rcu_read_lock();
393 session = session_find_by_id(ua_chan->session->tracing_id);
d68ec974
JG
394 if (!session || !session->ust_session) {
395 /*
396 * Not finding the session is not an error because there are
397 * multiple ways the channels can be torn down.
398 *
399 * 1) The session daemon can initiate the destruction of the
400 * ust app session after receiving a destroy command or
401 * during its shutdown/teardown.
402 * 2) The application, since we are in per-pid tracing, is
403 * unregistering and tearing down its ust app session.
404 *
405 * Both paths are protected by the session list lock which
406 * ensures that the accounting of lost packets and discarded
407 * events is done exactly once. The session is then unpublished
408 * from the session list, resulting in this condition.
409 */
fb83fe64
JD
410 goto end;
411 }
412
413 if (ua_chan->attr.overwrite) {
414 consumer_get_lost_packets(ua_chan->session->tracing_id,
415 ua_chan->key, session->ust_session->consumer,
416 &lost);
417 } else {
418 consumer_get_discarded_events(ua_chan->session->tracing_id,
419 ua_chan->key, session->ust_session->consumer,
420 &discarded);
421 }
422 uchan = trace_ust_find_channel_by_name(
423 session->ust_session->domain_global.channels,
424 ua_chan->name);
425 if (!uchan) {
426 ERR("Missing UST channel to store discarded counters");
427 goto end;
428 }
429
430 uchan->per_pid_closed_app_discarded += discarded;
431 uchan->per_pid_closed_app_lost += lost;
432
433end:
434 rcu_read_unlock();
e32d7f27
JG
435 if (session) {
436 session_put(session);
437 }
fb83fe64
JD
438}
439
d80a6244
DG
440/*
441 * Delete ust app channel safely. RCU read lock must be held before calling
442 * this function.
82cac6d2
JG
443 *
444 * The session list lock must be held by the caller.
d80a6244 445 */
8b366481 446static
d0b96690
DG
447void delete_ust_app_channel(int sock, struct ust_app_channel *ua_chan,
448 struct ust_app *app)
d80a6244
DG
449{
450 int ret;
bec39940 451 struct lttng_ht_iter iter;
d80a6244 452 struct ust_app_event *ua_event;
55cc08a6 453 struct ust_app_ctx *ua_ctx;
030a66fa 454 struct ust_app_stream *stream, *stmp;
7972aab2 455 struct ust_registry_session *registry;
d80a6244 456
ffe60014
DG
457 assert(ua_chan);
458
459 DBG3("UST app deleting channel %s", ua_chan->name);
460
55cc08a6 461 /* Wipe stream */
d80a6244 462 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
84cd17c6 463 cds_list_del(&stream->list);
fb45065e 464 delete_ust_app_stream(sock, stream, app);
d80a6244
DG
465 }
466
55cc08a6 467 /* Wipe context */
bec39940 468 cds_lfht_for_each_entry(ua_chan->ctx->ht, &iter.iter, ua_ctx, node.node) {
31746f93 469 cds_list_del(&ua_ctx->list);
bec39940 470 ret = lttng_ht_del(ua_chan->ctx, &iter);
55cc08a6 471 assert(!ret);
fb45065e 472 delete_ust_app_ctx(sock, ua_ctx, app);
55cc08a6 473 }
d80a6244 474
55cc08a6 475 /* Wipe events */
bec39940
DG
476 cds_lfht_for_each_entry(ua_chan->events->ht, &iter.iter, ua_event,
477 node.node) {
478 ret = lttng_ht_del(ua_chan->events, &iter);
525b0740 479 assert(!ret);
fb45065e 480 delete_ust_app_event(sock, ua_event, app);
d80a6244 481 }
edb67388 482
c8335706
MD
483 if (ua_chan->session->buffer_type == LTTNG_BUFFER_PER_PID) {
484 /* Wipe and free registry from session registry. */
485 registry = get_session_registry(ua_chan->session);
486 if (registry) {
e9404c27 487 ust_registry_channel_del_free(registry, ua_chan->key,
e38d96f9
MD
488 sock >= 0);
489 }
45798a31
JG
490 /*
491 * A negative socket can be used by the caller when
492 * cleaning-up a ua_chan in an error path. Skip the
493 * accounting in this case.
494 */
e38d96f9
MD
495 if (sock >= 0) {
496 save_per_pid_lost_discarded_counters(ua_chan);
c8335706 497 }
7972aab2 498 }
d0b96690 499
edb67388 500 if (ua_chan->obj != NULL) {
d0b96690
DG
501 /* Remove channel from application UST object descriptor. */
502 iter.iter.node = &ua_chan->ust_objd_node.node;
c6e62271
DG
503 ret = lttng_ht_del(app->ust_objd, &iter);
504 assert(!ret);
fb45065e 505 pthread_mutex_lock(&app->sock_lock);
ffe60014 506 ret = ustctl_release_object(sock, ua_chan->obj);
fb45065e 507 pthread_mutex_unlock(&app->sock_lock);
ffe60014
DG
508 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
509 ERR("UST app sock %d release channel obj failed with ret %d",
510 sock, ret);
511 }
7972aab2 512 lttng_fd_put(LTTNG_FD_APPS, 1);
edb67388
DG
513 free(ua_chan->obj);
514 }
36b588ed 515 call_rcu(&ua_chan->rcu_head, delete_ust_app_channel_rcu);
d80a6244
DG
516}
517
fb45065e
MD
518int ust_app_register_done(struct ust_app *app)
519{
520 int ret;
521
522 pthread_mutex_lock(&app->sock_lock);
523 ret = ustctl_register_done(app->sock);
524 pthread_mutex_unlock(&app->sock_lock);
525 return ret;
526}
527
528int ust_app_release_object(struct ust_app *app, struct lttng_ust_object_data *data)
529{
530 int ret, sock;
531
532 if (app) {
533 pthread_mutex_lock(&app->sock_lock);
534 sock = app->sock;
535 } else {
536 sock = -1;
537 }
538 ret = ustctl_release_object(sock, data);
539 if (app) {
540 pthread_mutex_unlock(&app->sock_lock);
541 }
542 return ret;
543}
544
331744e3 545/*
1b532a60
DG
546 * Push metadata to consumer socket.
547 *
dc2bbdae
MD
548 * RCU read-side lock must be held to guarantee existance of socket.
549 * Must be called with the ust app session lock held.
550 * Must be called with the registry lock held.
331744e3
JD
551 *
552 * On success, return the len of metadata pushed or else a negative value.
2c57e06d
MD
553 * Returning a -EPIPE return value means we could not send the metadata,
554 * but it can be caused by recoverable errors (e.g. the application has
555 * terminated concurrently).
331744e3
JD
556 */
557ssize_t ust_app_push_metadata(struct ust_registry_session *registry,
558 struct consumer_socket *socket, int send_zero_data)
559{
560 int ret;
561 char *metadata_str = NULL;
c585821b 562 size_t len, offset, new_metadata_len_sent;
331744e3 563 ssize_t ret_val;
93ec662e 564 uint64_t metadata_key, metadata_version;
331744e3
JD
565
566 assert(registry);
567 assert(socket);
1b532a60 568
c585821b
MD
569 metadata_key = registry->metadata_key;
570
ce34fcd0 571 /*
dc2bbdae
MD
572 * Means that no metadata was assigned to the session. This can
573 * happens if no start has been done previously.
ce34fcd0 574 */
c585821b 575 if (!metadata_key) {
ce34fcd0
MD
576 return 0;
577 }
578
331744e3
JD
579 offset = registry->metadata_len_sent;
580 len = registry->metadata_len - registry->metadata_len_sent;
c585821b 581 new_metadata_len_sent = registry->metadata_len;
93ec662e 582 metadata_version = registry->metadata_version;
331744e3
JD
583 if (len == 0) {
584 DBG3("No metadata to push for metadata key %" PRIu64,
585 registry->metadata_key);
586 ret_val = len;
587 if (send_zero_data) {
588 DBG("No metadata to push");
589 goto push_data;
590 }
591 goto end;
592 }
593
594 /* Allocate only what we have to send. */
595 metadata_str = zmalloc(len);
596 if (!metadata_str) {
597 PERROR("zmalloc ust app metadata string");
598 ret_val = -ENOMEM;
599 goto error;
600 }
c585821b 601 /* Copy what we haven't sent out. */
331744e3 602 memcpy(metadata_str, registry->metadata + offset, len);
331744e3
JD
603
604push_data:
c585821b
MD
605 pthread_mutex_unlock(&registry->lock);
606 /*
607 * We need to unlock the registry while we push metadata to
608 * break a circular dependency between the consumerd metadata
609 * lock and the sessiond registry lock. Indeed, pushing metadata
610 * to the consumerd awaits that it gets pushed all the way to
611 * relayd, but doing so requires grabbing the metadata lock. If
612 * a concurrent metadata request is being performed by
613 * consumerd, this can try to grab the registry lock on the
614 * sessiond while holding the metadata lock on the consumer
615 * daemon. Those push and pull schemes are performed on two
616 * different bidirectionnal communication sockets.
617 */
618 ret = consumer_push_metadata(socket, metadata_key,
93ec662e 619 metadata_str, len, offset, metadata_version);
c585821b 620 pthread_mutex_lock(&registry->lock);
331744e3 621 if (ret < 0) {
000baf6a 622 /*
dc2bbdae
MD
623 * There is an acceptable race here between the registry
624 * metadata key assignment and the creation on the
625 * consumer. The session daemon can concurrently push
626 * metadata for this registry while being created on the
627 * consumer since the metadata key of the registry is
628 * assigned *before* it is setup to avoid the consumer
629 * to ask for metadata that could possibly be not found
630 * in the session daemon.
000baf6a 631 *
dc2bbdae
MD
632 * The metadata will get pushed either by the session
633 * being stopped or the consumer requesting metadata if
634 * that race is triggered.
000baf6a
DG
635 */
636 if (ret == -LTTCOMM_CONSUMERD_CHANNEL_FAIL) {
637 ret = 0;
c585821b
MD
638 } else {
639 ERR("Error pushing metadata to consumer");
000baf6a 640 }
331744e3
JD
641 ret_val = ret;
642 goto error_push;
c585821b
MD
643 } else {
644 /*
645 * Metadata may have been concurrently pushed, since
646 * we're not holding the registry lock while pushing to
647 * consumer. This is handled by the fact that we send
648 * the metadata content, size, and the offset at which
649 * that metadata belongs. This may arrive out of order
650 * on the consumer side, and the consumer is able to
651 * deal with overlapping fragments. The consumer
652 * supports overlapping fragments, which must be
653 * contiguous starting from offset 0. We keep the
654 * largest metadata_len_sent value of the concurrent
655 * send.
656 */
657 registry->metadata_len_sent =
658 max_t(size_t, registry->metadata_len_sent,
659 new_metadata_len_sent);
331744e3 660 }
331744e3
JD
661 free(metadata_str);
662 return len;
663
664end:
665error:
ce34fcd0
MD
666 if (ret_val) {
667 /*
dc2bbdae
MD
668 * On error, flag the registry that the metadata is
669 * closed. We were unable to push anything and this
670 * means that either the consumer is not responding or
671 * the metadata cache has been destroyed on the
672 * consumer.
ce34fcd0
MD
673 */
674 registry->metadata_closed = 1;
675 }
331744e3
JD
676error_push:
677 free(metadata_str);
678 return ret_val;
679}
680
d88aee68 681/*
ce34fcd0 682 * For a given application and session, push metadata to consumer.
331744e3
JD
683 * Either sock or consumer is required : if sock is NULL, the default
684 * socket to send the metadata is retrieved from consumer, if sock
685 * is not NULL we use it to send the metadata.
ce34fcd0 686 * RCU read-side lock must be held while calling this function,
dc2bbdae
MD
687 * therefore ensuring existance of registry. It also ensures existance
688 * of socket throughout this function.
d88aee68
DG
689 *
690 * Return 0 on success else a negative error.
2c57e06d
MD
691 * Returning a -EPIPE return value means we could not send the metadata,
692 * but it can be caused by recoverable errors (e.g. the application has
693 * terminated concurrently).
d88aee68 694 */
7972aab2
DG
695static int push_metadata(struct ust_registry_session *registry,
696 struct consumer_output *consumer)
d88aee68 697{
331744e3
JD
698 int ret_val;
699 ssize_t ret;
d88aee68
DG
700 struct consumer_socket *socket;
701
7972aab2
DG
702 assert(registry);
703 assert(consumer);
704
ce34fcd0 705 pthread_mutex_lock(&registry->lock);
ce34fcd0 706 if (registry->metadata_closed) {
dc2bbdae
MD
707 ret_val = -EPIPE;
708 goto error;
d88aee68
DG
709 }
710
d88aee68 711 /* Get consumer socket to use to push the metadata.*/
7972aab2
DG
712 socket = consumer_find_socket_by_bitness(registry->bits_per_long,
713 consumer);
d88aee68 714 if (!socket) {
331744e3 715 ret_val = -1;
ce34fcd0 716 goto error;
d88aee68
DG
717 }
718
331744e3 719 ret = ust_app_push_metadata(registry, socket, 0);
d88aee68 720 if (ret < 0) {
331744e3 721 ret_val = ret;
ce34fcd0 722 goto error;
d88aee68 723 }
dc2bbdae 724 pthread_mutex_unlock(&registry->lock);
d88aee68
DG
725 return 0;
726
ce34fcd0 727error:
dc2bbdae 728 pthread_mutex_unlock(&registry->lock);
331744e3 729 return ret_val;
d88aee68
DG
730}
731
732/*
733 * Send to the consumer a close metadata command for the given session. Once
734 * done, the metadata channel is deleted and the session metadata pointer is
dc2bbdae 735 * nullified. The session lock MUST be held unless the application is
d88aee68
DG
736 * in the destroy path.
737 *
a70ac2f4
MD
738 * Do not hold the registry lock while communicating with the consumerd, because
739 * doing so causes inter-process deadlocks between consumerd and sessiond with
740 * the metadata request notification.
741 *
d88aee68
DG
742 * Return 0 on success else a negative value.
743 */
7972aab2
DG
744static int close_metadata(struct ust_registry_session *registry,
745 struct consumer_output *consumer)
d88aee68
DG
746{
747 int ret;
748 struct consumer_socket *socket;
a70ac2f4
MD
749 uint64_t metadata_key;
750 bool registry_was_already_closed;
d88aee68 751
7972aab2
DG
752 assert(registry);
753 assert(consumer);
d88aee68 754
7972aab2
DG
755 rcu_read_lock();
756
ce34fcd0 757 pthread_mutex_lock(&registry->lock);
a70ac2f4
MD
758 metadata_key = registry->metadata_key;
759 registry_was_already_closed = registry->metadata_closed;
760 if (metadata_key != 0) {
761 /*
762 * Metadata closed. Even on error this means that the consumer
763 * is not responding or not found so either way a second close
764 * should NOT be emit for this registry.
765 */
766 registry->metadata_closed = 1;
767 }
768 pthread_mutex_unlock(&registry->lock);
ce34fcd0 769
a70ac2f4 770 if (metadata_key == 0 || registry_was_already_closed) {
d88aee68 771 ret = 0;
1b532a60 772 goto end;
d88aee68
DG
773 }
774
d88aee68 775 /* Get consumer socket to use to push the metadata.*/
7972aab2
DG
776 socket = consumer_find_socket_by_bitness(registry->bits_per_long,
777 consumer);
d88aee68
DG
778 if (!socket) {
779 ret = -1;
a70ac2f4 780 goto end;
d88aee68
DG
781 }
782
a70ac2f4 783 ret = consumer_close_metadata(socket, metadata_key);
d88aee68 784 if (ret < 0) {
a70ac2f4 785 goto end;
d88aee68
DG
786 }
787
1b532a60 788end:
7972aab2 789 rcu_read_unlock();
d88aee68
DG
790 return ret;
791}
792
36b588ed
MD
793/*
794 * We need to execute ht_destroy outside of RCU read-side critical
0b2dc8df
MD
795 * section and outside of call_rcu thread, so we postpone its execution
796 * using ht_cleanup_push. It is simpler than to change the semantic of
797 * the many callers of delete_ust_app_session().
36b588ed
MD
798 */
799static
800void delete_ust_app_session_rcu(struct rcu_head *head)
801{
802 struct ust_app_session *ua_sess =
803 caa_container_of(head, struct ust_app_session, rcu_head);
804
0b2dc8df 805 ht_cleanup_push(ua_sess->channels);
36b588ed
MD
806 free(ua_sess);
807}
808
d80a6244
DG
809/*
810 * Delete ust app session safely. RCU read lock must be held before calling
811 * this function.
82cac6d2
JG
812 *
813 * The session list lock must be held by the caller.
d80a6244 814 */
8b366481 815static
d0b96690
DG
816void delete_ust_app_session(int sock, struct ust_app_session *ua_sess,
817 struct ust_app *app)
d80a6244
DG
818{
819 int ret;
bec39940 820 struct lttng_ht_iter iter;
d80a6244 821 struct ust_app_channel *ua_chan;
7972aab2 822 struct ust_registry_session *registry;
d80a6244 823
d88aee68
DG
824 assert(ua_sess);
825
1b532a60
DG
826 pthread_mutex_lock(&ua_sess->lock);
827
b161602a
MD
828 assert(!ua_sess->deleted);
829 ua_sess->deleted = true;
830
7972aab2 831 registry = get_session_registry(ua_sess);
fad1ed2f 832 /* Registry can be null on error path during initialization. */
ce34fcd0 833 if (registry) {
d88aee68 834 /* Push metadata for application before freeing the application. */
7972aab2 835 (void) push_metadata(registry, ua_sess->consumer);
d88aee68 836
7972aab2
DG
837 /*
838 * Don't ask to close metadata for global per UID buffers. Close
1b532a60
DG
839 * metadata only on destroy trace session in this case. Also, the
840 * previous push metadata could have flag the metadata registry to
841 * close so don't send a close command if closed.
7972aab2 842 */
ce34fcd0 843 if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) {
7972aab2
DG
844 /* And ask to close it for this session registry. */
845 (void) close_metadata(registry, ua_sess->consumer);
846 }
d80a6244
DG
847 }
848
bec39940
DG
849 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
850 node.node) {
851 ret = lttng_ht_del(ua_sess->channels, &iter);
525b0740 852 assert(!ret);
d0b96690 853 delete_ust_app_channel(sock, ua_chan, app);
d80a6244 854 }
d80a6244 855
7972aab2
DG
856 /* In case of per PID, the registry is kept in the session. */
857 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) {
858 struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id);
859 if (reg_pid) {
fad1ed2f
JR
860 /*
861 * Registry can be null on error path during
862 * initialization.
863 */
7972aab2
DG
864 buffer_reg_pid_remove(reg_pid);
865 buffer_reg_pid_destroy(reg_pid);
866 }
867 }
d0b96690 868
aee6bafd 869 if (ua_sess->handle != -1) {
fb45065e 870 pthread_mutex_lock(&app->sock_lock);
ffe60014 871 ret = ustctl_release_handle(sock, ua_sess->handle);
fb45065e 872 pthread_mutex_unlock(&app->sock_lock);
ffe60014
DG
873 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
874 ERR("UST app sock %d release session handle failed with ret %d",
875 sock, ret);
876 }
10b56aef
MD
877 /* Remove session from application UST object descriptor. */
878 iter.iter.node = &ua_sess->ust_objd_node.node;
879 ret = lttng_ht_del(app->ust_sessions_objd, &iter);
880 assert(!ret);
aee6bafd 881 }
10b56aef 882
1b532a60
DG
883 pthread_mutex_unlock(&ua_sess->lock);
884
6addfa37
MD
885 consumer_output_put(ua_sess->consumer);
886
36b588ed 887 call_rcu(&ua_sess->rcu_head, delete_ust_app_session_rcu);
d80a6244 888}
91d76f53
DG
889
890/*
284d8f55
DG
891 * Delete a traceable application structure from the global list. Never call
892 * this function outside of a call_rcu call.
36b588ed
MD
893 *
894 * RCU read side lock should _NOT_ be held when calling this function.
91d76f53 895 */
8b366481
DG
896static
897void delete_ust_app(struct ust_app *app)
91d76f53 898{
8b366481 899 int ret, sock;
d42f20df 900 struct ust_app_session *ua_sess, *tmp_ua_sess;
44d3bd01 901
82cac6d2
JG
902 /*
903 * The session list lock must be held during this function to guarantee
904 * the existence of ua_sess.
905 */
906 session_lock_list();
d80a6244 907 /* Delete ust app sessions info */
852d0037
DG
908 sock = app->sock;
909 app->sock = -1;
d80a6244 910
8b366481 911 /* Wipe sessions */
d42f20df
DG
912 cds_list_for_each_entry_safe(ua_sess, tmp_ua_sess, &app->teardown_head,
913 teardown_node) {
914 /* Free every object in the session and the session. */
36b588ed 915 rcu_read_lock();
d0b96690 916 delete_ust_app_session(sock, ua_sess, app);
36b588ed 917 rcu_read_unlock();
d80a6244 918 }
36b588ed 919
0b2dc8df 920 ht_cleanup_push(app->sessions);
10b56aef 921 ht_cleanup_push(app->ust_sessions_objd);
0b2dc8df 922 ht_cleanup_push(app->ust_objd);
d80a6244 923
da873412
JR
924 /*
925 * This could be NULL if the event notifier setup failed (e.g the app
926 * was killed or the tracer does not support this feature).
927 */
928 if (app->event_notifier_group.object) {
929 enum lttng_error_code ret_code;
930 const int event_notifier_read_fd = lttng_pipe_get_readfd(
931 app->event_notifier_group.event_pipe);
932
933 ret_code = notification_thread_command_remove_tracer_event_source(
934 notification_thread_handle,
935 event_notifier_read_fd);
936 if (ret_code != LTTNG_OK) {
937 ERR("Failed to remove application tracer event source from notification thread");
938 }
939
940 ustctl_release_object(sock, app->event_notifier_group.object);
941 free(app->event_notifier_group.object);
942 }
943
944 lttng_pipe_destroy(app->event_notifier_group.event_pipe);
945
6414a713 946 /*
852d0037
DG
947 * Wait until we have deleted the application from the sock hash table
948 * before closing this socket, otherwise an application could re-use the
949 * socket ID and race with the teardown, using the same hash table entry.
950 *
951 * It's OK to leave the close in call_rcu. We want it to stay unique for
952 * all RCU readers that could run concurrently with unregister app,
953 * therefore we _need_ to only close that socket after a grace period. So
954 * it should stay in this RCU callback.
955 *
956 * This close() is a very important step of the synchronization model so
957 * every modification to this function must be carefully reviewed.
6414a713 958 */
799e2c4f
MD
959 ret = close(sock);
960 if (ret) {
961 PERROR("close");
962 }
4063050c 963 lttng_fd_put(LTTNG_FD_APPS, 1);
d80a6244 964
852d0037 965 DBG2("UST app pid %d deleted", app->pid);
284d8f55 966 free(app);
82cac6d2 967 session_unlock_list();
099e26bd
DG
968}
969
970/*
f6a9efaa 971 * URCU intermediate call to delete an UST app.
099e26bd 972 */
8b366481
DG
973static
974void delete_ust_app_rcu(struct rcu_head *head)
099e26bd 975{
bec39940
DG
976 struct lttng_ht_node_ulong *node =
977 caa_container_of(head, struct lttng_ht_node_ulong, head);
f6a9efaa 978 struct ust_app *app =
852d0037 979 caa_container_of(node, struct ust_app, pid_n);
f6a9efaa 980
852d0037 981 DBG3("Call RCU deleting app PID %d", app->pid);
f6a9efaa 982 delete_ust_app(app);
099e26bd
DG
983}
984
ffe60014
DG
985/*
986 * Delete the session from the application ht and delete the data structure by
987 * freeing every object inside and releasing them.
82cac6d2
JG
988 *
989 * The session list lock must be held by the caller.
ffe60014 990 */
d0b96690 991static void destroy_app_session(struct ust_app *app,
ffe60014
DG
992 struct ust_app_session *ua_sess)
993{
994 int ret;
995 struct lttng_ht_iter iter;
996
997 assert(app);
998 assert(ua_sess);
999
1000 iter.iter.node = &ua_sess->node.node;
1001 ret = lttng_ht_del(app->sessions, &iter);
1002 if (ret) {
1003 /* Already scheduled for teardown. */
1004 goto end;
1005 }
1006
1007 /* Once deleted, free the data structure. */
d0b96690 1008 delete_ust_app_session(app->sock, ua_sess, app);
ffe60014
DG
1009
1010end:
1011 return;
1012}
1013
8b366481
DG
1014/*
1015 * Alloc new UST app session.
1016 */
1017static
40bbd087 1018struct ust_app_session *alloc_ust_app_session(void)
8b366481
DG
1019{
1020 struct ust_app_session *ua_sess;
1021
1022 /* Init most of the default value by allocating and zeroing */
1023 ua_sess = zmalloc(sizeof(struct ust_app_session));
1024 if (ua_sess == NULL) {
1025 PERROR("malloc");
ffe60014 1026 goto error_free;
8b366481
DG
1027 }
1028
1029 ua_sess->handle = -1;
bec39940 1030 ua_sess->channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
ad7a9107 1031 ua_sess->metadata_attr.type = LTTNG_UST_CHAN_METADATA;
84ad93e8 1032 pthread_mutex_init(&ua_sess->lock, NULL);
ad7a9107 1033
8b366481
DG
1034 return ua_sess;
1035
ffe60014 1036error_free:
8b366481
DG
1037 return NULL;
1038}
1039
1040/*
1041 * Alloc new UST app channel.
1042 */
1043static
b53d4e59 1044struct ust_app_channel *alloc_ust_app_channel(const char *name,
d0b96690 1045 struct ust_app_session *ua_sess,
ffe60014 1046 struct lttng_ust_channel_attr *attr)
8b366481
DG
1047{
1048 struct ust_app_channel *ua_chan;
1049
1050 /* Init most of the default value by allocating and zeroing */
1051 ua_chan = zmalloc(sizeof(struct ust_app_channel));
1052 if (ua_chan == NULL) {
1053 PERROR("malloc");
1054 goto error;
1055 }
1056
1057 /* Setup channel name */
1058 strncpy(ua_chan->name, name, sizeof(ua_chan->name));
1059 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
1060
1061 ua_chan->enabled = 1;
1062 ua_chan->handle = -1;
45893984 1063 ua_chan->session = ua_sess;
ffe60014 1064 ua_chan->key = get_next_channel_key();
bec39940
DG
1065 ua_chan->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1066 ua_chan->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
1067 lttng_ht_node_init_str(&ua_chan->node, ua_chan->name);
8b366481
DG
1068
1069 CDS_INIT_LIST_HEAD(&ua_chan->streams.head);
31746f93 1070 CDS_INIT_LIST_HEAD(&ua_chan->ctx_list);
8b366481
DG
1071
1072 /* Copy attributes */
1073 if (attr) {
ffe60014 1074 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
2fe6e7f5
DG
1075 ua_chan->attr.subbuf_size = attr->subbuf_size;
1076 ua_chan->attr.num_subbuf = attr->num_subbuf;
1077 ua_chan->attr.overwrite = attr->overwrite;
1078 ua_chan->attr.switch_timer_interval = attr->switch_timer_interval;
1079 ua_chan->attr.read_timer_interval = attr->read_timer_interval;
1080 ua_chan->attr.output = attr->output;
491d1539 1081 ua_chan->attr.blocking_timeout = attr->u.s.blocking_timeout;
8b366481 1082 }
ffe60014
DG
1083 /* By default, the channel is a per cpu channel. */
1084 ua_chan->attr.type = LTTNG_UST_CHAN_PER_CPU;
8b366481
DG
1085
1086 DBG3("UST app channel %s allocated", ua_chan->name);
1087
1088 return ua_chan;
1089
1090error:
1091 return NULL;
1092}
1093
37f1c236
DG
1094/*
1095 * Allocate and initialize a UST app stream.
1096 *
1097 * Return newly allocated stream pointer or NULL on error.
1098 */
ffe60014 1099struct ust_app_stream *ust_app_alloc_stream(void)
37f1c236
DG
1100{
1101 struct ust_app_stream *stream = NULL;
1102
1103 stream = zmalloc(sizeof(*stream));
1104 if (stream == NULL) {
1105 PERROR("zmalloc ust app stream");
1106 goto error;
1107 }
1108
1109 /* Zero could be a valid value for a handle so flag it to -1. */
1110 stream->handle = -1;
1111
1112error:
1113 return stream;
1114}
1115
8b366481
DG
1116/*
1117 * Alloc new UST app event.
1118 */
1119static
1120struct ust_app_event *alloc_ust_app_event(char *name,
1121 struct lttng_ust_event *attr)
1122{
1123 struct ust_app_event *ua_event;
1124
1125 /* Init most of the default value by allocating and zeroing */
1126 ua_event = zmalloc(sizeof(struct ust_app_event));
1127 if (ua_event == NULL) {
20533947 1128 PERROR("Failed to allocate ust_app_event structure");
8b366481
DG
1129 goto error;
1130 }
1131
1132 ua_event->enabled = 1;
1133 strncpy(ua_event->name, name, sizeof(ua_event->name));
1134 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
bec39940 1135 lttng_ht_node_init_str(&ua_event->node, ua_event->name);
8b366481
DG
1136
1137 /* Copy attributes */
1138 if (attr) {
1139 memcpy(&ua_event->attr, attr, sizeof(ua_event->attr));
1140 }
1141
1142 DBG3("UST app event %s allocated", ua_event->name);
1143
1144 return ua_event;
1145
1146error:
1147 return NULL;
1148}
1149
1150/*
1151 * Alloc new UST app context.
1152 */
1153static
bdf64013 1154struct ust_app_ctx *alloc_ust_app_ctx(struct lttng_ust_context_attr *uctx)
8b366481
DG
1155{
1156 struct ust_app_ctx *ua_ctx;
1157
1158 ua_ctx = zmalloc(sizeof(struct ust_app_ctx));
1159 if (ua_ctx == NULL) {
1160 goto error;
1161 }
1162
31746f93
DG
1163 CDS_INIT_LIST_HEAD(&ua_ctx->list);
1164
8b366481
DG
1165 if (uctx) {
1166 memcpy(&ua_ctx->ctx, uctx, sizeof(ua_ctx->ctx));
bdf64013 1167 if (uctx->ctx == LTTNG_UST_CONTEXT_APP_CONTEXT) {
f3db82be 1168 char *provider_name = NULL, *ctx_name = NULL;
bdf64013
JG
1169
1170 provider_name = strdup(uctx->u.app_ctx.provider_name);
1171 ctx_name = strdup(uctx->u.app_ctx.ctx_name);
1172 if (!provider_name || !ctx_name) {
1173 free(provider_name);
1174 free(ctx_name);
1175 goto error;
1176 }
1177
1178 ua_ctx->ctx.u.app_ctx.provider_name = provider_name;
1179 ua_ctx->ctx.u.app_ctx.ctx_name = ctx_name;
1180 }
8b366481
DG
1181 }
1182
1183 DBG3("UST app context %d allocated", ua_ctx->ctx.ctx);
8b366481 1184 return ua_ctx;
bdf64013
JG
1185error:
1186 free(ua_ctx);
1187 return NULL;
8b366481
DG
1188}
1189
025faf73
DG
1190/*
1191 * Allocate a filter and copy the given original filter.
1192 *
1193 * Return allocated filter or NULL on error.
1194 */
51755dc8
JG
1195static struct lttng_filter_bytecode *copy_filter_bytecode(
1196 struct lttng_filter_bytecode *orig_f)
025faf73 1197{
51755dc8 1198 struct lttng_filter_bytecode *filter = NULL;
025faf73
DG
1199
1200 /* Copy filter bytecode */
1201 filter = zmalloc(sizeof(*filter) + orig_f->len);
1202 if (!filter) {
51755dc8 1203 PERROR("zmalloc alloc filter bytecode");
025faf73
DG
1204 goto error;
1205 }
1206
1207 memcpy(filter, orig_f, sizeof(*filter) + orig_f->len);
1208
1209error:
1210 return filter;
1211}
1212
51755dc8
JG
1213/*
1214 * Create a liblttng-ust filter bytecode from given bytecode.
1215 *
1216 * Return allocated filter or NULL on error.
1217 */
1218static struct lttng_ust_filter_bytecode *create_ust_bytecode_from_bytecode(
a154c7b8 1219 const struct lttng_filter_bytecode *orig_f)
51755dc8
JG
1220{
1221 struct lttng_ust_filter_bytecode *filter = NULL;
1222
1223 /* Copy filter bytecode */
1224 filter = zmalloc(sizeof(*filter) + orig_f->len);
1225 if (!filter) {
1226 PERROR("zmalloc alloc ust filter bytecode");
1227 goto error;
1228 }
1229
1230 assert(sizeof(struct lttng_filter_bytecode) ==
1231 sizeof(struct lttng_ust_filter_bytecode));
1232 memcpy(filter, orig_f, sizeof(*filter) + orig_f->len);
1233error:
1234 return filter;
1235}
1236
099e26bd 1237/*
421cb601
DG
1238 * Find an ust_app using the sock and return it. RCU read side lock must be
1239 * held before calling this helper function.
099e26bd 1240 */
f20baf8e 1241struct ust_app *ust_app_find_by_sock(int sock)
099e26bd 1242{
bec39940 1243 struct lttng_ht_node_ulong *node;
bec39940 1244 struct lttng_ht_iter iter;
f6a9efaa 1245
852d0037 1246 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter);
bec39940 1247 node = lttng_ht_iter_get_node_ulong(&iter);
f6a9efaa
DG
1248 if (node == NULL) {
1249 DBG2("UST app find by sock %d not found", sock);
f6a9efaa
DG
1250 goto error;
1251 }
852d0037
DG
1252
1253 return caa_container_of(node, struct ust_app, sock_n);
f6a9efaa
DG
1254
1255error:
1256 return NULL;
099e26bd
DG
1257}
1258
d0b96690
DG
1259/*
1260 * Find an ust_app using the notify sock and return it. RCU read side lock must
1261 * be held before calling this helper function.
1262 */
1263static struct ust_app *find_app_by_notify_sock(int sock)
1264{
1265 struct lttng_ht_node_ulong *node;
1266 struct lttng_ht_iter iter;
1267
1268 lttng_ht_lookup(ust_app_ht_by_notify_sock, (void *)((unsigned long) sock),
1269 &iter);
1270 node = lttng_ht_iter_get_node_ulong(&iter);
1271 if (node == NULL) {
1272 DBG2("UST app find by notify sock %d not found", sock);
1273 goto error;
1274 }
1275
1276 return caa_container_of(node, struct ust_app, notify_sock_n);
1277
1278error:
1279 return NULL;
1280}
1281
025faf73
DG
1282/*
1283 * Lookup for an ust app event based on event name, filter bytecode and the
1284 * event loglevel.
1285 *
1286 * Return an ust_app_event object or NULL on error.
1287 */
18eace3b 1288static struct ust_app_event *find_ust_app_event(struct lttng_ht *ht,
88e3c2f5 1289 const char *name, const struct lttng_filter_bytecode *filter,
2106efa0 1290 int loglevel_value,
39c5a3a7 1291 const struct lttng_event_exclusion *exclusion)
18eace3b
DG
1292{
1293 struct lttng_ht_iter iter;
1294 struct lttng_ht_node_str *node;
1295 struct ust_app_event *event = NULL;
1296 struct ust_app_ht_key key;
18eace3b
DG
1297
1298 assert(name);
1299 assert(ht);
1300
1301 /* Setup key for event lookup. */
1302 key.name = name;
1303 key.filter = filter;
2106efa0 1304 key.loglevel_type = loglevel_value;
39c5a3a7 1305 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
51755dc8 1306 key.exclusion = exclusion;
18eace3b 1307
025faf73
DG
1308 /* Lookup using the event name as hash and a custom match fct. */
1309 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
1310 ht_match_ust_app_event, &key, &iter.iter);
18eace3b
DG
1311 node = lttng_ht_iter_get_node_str(&iter);
1312 if (node == NULL) {
1313 goto end;
1314 }
1315
1316 event = caa_container_of(node, struct ust_app_event, node);
1317
1318end:
18eace3b
DG
1319 return event;
1320}
1321
55cc08a6
DG
1322/*
1323 * Create the channel context on the tracer.
d0b96690
DG
1324 *
1325 * Called with UST app session lock held.
55cc08a6
DG
1326 */
1327static
1328int create_ust_channel_context(struct ust_app_channel *ua_chan,
1329 struct ust_app_ctx *ua_ctx, struct ust_app *app)
1330{
1331 int ret;
1332
840cb59c 1333 health_code_update();
86acf0da 1334
fb45065e 1335 pthread_mutex_lock(&app->sock_lock);
852d0037 1336 ret = ustctl_add_context(app->sock, &ua_ctx->ctx,
55cc08a6 1337 ua_chan->obj, &ua_ctx->obj);
fb45065e 1338 pthread_mutex_unlock(&app->sock_lock);
55cc08a6 1339 if (ret < 0) {
ffe60014
DG
1340 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1341 ERR("UST app create channel context failed for app (pid: %d) "
1342 "with ret %d", app->pid, ret);
1343 } else {
3757b385
DG
1344 /*
1345 * This is normal behavior, an application can die during the
1346 * creation process. Don't report an error so the execution can
1347 * continue normally.
1348 */
1349 ret = 0;
88e3c2f5 1350 DBG3("UST app add context failed. Application is dead.");
ffe60014 1351 }
55cc08a6
DG
1352 goto error;
1353 }
1354
1355 ua_ctx->handle = ua_ctx->obj->handle;
1356
d0b96690
DG
1357 DBG2("UST app context handle %d created successfully for channel %s",
1358 ua_ctx->handle, ua_chan->name);
55cc08a6
DG
1359
1360error:
840cb59c 1361 health_code_update();
55cc08a6
DG
1362 return ret;
1363}
1364
53a80697
MD
1365/*
1366 * Set the filter on the tracer.
1367 */
a154c7b8
JR
1368static int set_ust_object_filter(struct ust_app *app,
1369 const struct lttng_filter_bytecode *bytecode,
1370 struct lttng_ust_object_data *ust_object)
53a80697
MD
1371{
1372 int ret;
51755dc8 1373 struct lttng_ust_filter_bytecode *ust_bytecode = NULL;
53a80697 1374
840cb59c 1375 health_code_update();
86acf0da 1376
a154c7b8 1377 ust_bytecode = create_ust_bytecode_from_bytecode(bytecode);
51755dc8
JG
1378 if (!ust_bytecode) {
1379 ret = -LTTNG_ERR_NOMEM;
1380 goto error;
1381 }
fb45065e 1382 pthread_mutex_lock(&app->sock_lock);
51755dc8 1383 ret = ustctl_set_filter(app->sock, ust_bytecode,
a154c7b8 1384 ust_object);
fb45065e 1385 pthread_mutex_unlock(&app->sock_lock);
53a80697 1386 if (ret < 0) {
ffe60014 1387 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
a154c7b8
JR
1388 ERR("UST app set object filter failed for object %p of app (pid: %d) "
1389 "with ret %d", ust_object, app->pid, ret);
ffe60014 1390 } else {
3757b385
DG
1391 /*
1392 * This is normal behavior, an application can die during the
1393 * creation process. Don't report an error so the execution can
1394 * continue normally.
1395 */
1396 ret = 0;
a154c7b8 1397 DBG3("Failed to set UST app object filter. Application is dead.");
ffe60014 1398 }
53a80697
MD
1399 goto error;
1400 }
1401
a154c7b8 1402 DBG2("UST filter successfully set for object %p", ust_object);
53a80697
MD
1403
1404error:
840cb59c 1405 health_code_update();
51755dc8 1406 free(ust_bytecode);
53a80697
MD
1407 return ret;
1408}
1409
51755dc8
JG
1410static
1411struct lttng_ust_event_exclusion *create_ust_exclusion_from_exclusion(
c0901ffa 1412 const struct lttng_event_exclusion *exclusion)
51755dc8
JG
1413{
1414 struct lttng_ust_event_exclusion *ust_exclusion = NULL;
1415 size_t exclusion_alloc_size = sizeof(struct lttng_ust_event_exclusion) +
1416 LTTNG_UST_SYM_NAME_LEN * exclusion->count;
1417
1418 ust_exclusion = zmalloc(exclusion_alloc_size);
1419 if (!ust_exclusion) {
1420 PERROR("malloc");
1421 goto end;
1422 }
1423
1424 assert(sizeof(struct lttng_event_exclusion) ==
1425 sizeof(struct lttng_ust_event_exclusion));
1426 memcpy(ust_exclusion, exclusion, exclusion_alloc_size);
1427end:
1428 return ust_exclusion;
1429}
1430
7cc9a73c
JI
1431/*
1432 * Set event exclusions on the tracer.
1433 */
c0901ffa
JR
1434static int set_ust_object_exclusions(struct ust_app *app,
1435 const struct lttng_event_exclusion *exclusions,
1436 struct lttng_ust_object_data *ust_object)
7cc9a73c
JI
1437{
1438 int ret;
c0901ffa 1439 struct lttng_ust_event_exclusion *ust_exclusions = NULL;
7cc9a73c 1440
c0901ffa 1441 assert(exclusions && exclusions->count > 0);
7cc9a73c 1442
c0901ffa 1443 health_code_update();
7cc9a73c 1444
c0901ffa
JR
1445 ust_exclusions = create_ust_exclusion_from_exclusion(
1446 exclusions);
1447 if (!ust_exclusions) {
51755dc8
JG
1448 ret = -LTTNG_ERR_NOMEM;
1449 goto error;
1450 }
fb45065e 1451 pthread_mutex_lock(&app->sock_lock);
c0901ffa 1452 ret = ustctl_set_exclusion(app->sock, ust_exclusions, ust_object);
fb45065e 1453 pthread_mutex_unlock(&app->sock_lock);
7cc9a73c
JI
1454 if (ret < 0) {
1455 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
c0901ffa
JR
1456 ERR("Failed to set UST app exclusions for object %p of app (pid: %d) "
1457 "with ret %d", ust_object, app->pid, ret);
7cc9a73c
JI
1458 } else {
1459 /*
1460 * This is normal behavior, an application can die during the
1461 * creation process. Don't report an error so the execution can
1462 * continue normally.
1463 */
1464 ret = 0;
c0901ffa 1465 DBG3("Failed to set UST app object exclusions. Application is dead.");
7cc9a73c
JI
1466 }
1467 goto error;
1468 }
1469
c0901ffa 1470 DBG2("UST exclusions set successfully for object %p", ust_object);
7cc9a73c
JI
1471
1472error:
1473 health_code_update();
c0901ffa 1474 free(ust_exclusions);
7cc9a73c
JI
1475 return ret;
1476}
1477
9730260e
DG
1478/*
1479 * Disable the specified event on to UST tracer for the UST session.
1480 */
e2456d0a
JR
1481static int disable_ust_object(struct ust_app *app,
1482 struct lttng_ust_object_data *object)
9730260e
DG
1483{
1484 int ret;
1485
840cb59c 1486 health_code_update();
86acf0da 1487
fb45065e 1488 pthread_mutex_lock(&app->sock_lock);
e2456d0a 1489 ret = ustctl_disable(app->sock, object);
fb45065e 1490 pthread_mutex_unlock(&app->sock_lock);
9730260e 1491 if (ret < 0) {
ffe60014 1492 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
e2456d0a
JR
1493 ERR("Failed to disable UST app object %p app (pid: %d) with ret %d",
1494 object, app->pid, ret);
ffe60014 1495 } else {
3757b385
DG
1496 /*
1497 * This is normal behavior, an application can die during the
1498 * creation process. Don't report an error so the execution can
1499 * continue normally.
1500 */
1501 ret = 0;
e2456d0a 1502 DBG3("Failed to disable UST app object. Application is dead.");
ffe60014 1503 }
9730260e
DG
1504 goto error;
1505 }
1506
e2456d0a
JR
1507 DBG2("UST app object %p disabled successfully for app (pid: %d)",
1508 object, app->pid);
9730260e
DG
1509
1510error:
840cb59c 1511 health_code_update();
9730260e
DG
1512 return ret;
1513}
1514
78f0bacd
DG
1515/*
1516 * Disable the specified channel on to UST tracer for the UST session.
1517 */
1518static int disable_ust_channel(struct ust_app *app,
1519 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
1520{
1521 int ret;
1522
840cb59c 1523 health_code_update();
86acf0da 1524
fb45065e 1525 pthread_mutex_lock(&app->sock_lock);
852d0037 1526 ret = ustctl_disable(app->sock, ua_chan->obj);
fb45065e 1527 pthread_mutex_unlock(&app->sock_lock);
78f0bacd 1528 if (ret < 0) {
ffe60014
DG
1529 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1530 ERR("UST app channel %s disable failed for app (pid: %d) "
1531 "and session handle %d with ret %d",
1532 ua_chan->name, app->pid, ua_sess->handle, ret);
1533 } else {
3757b385
DG
1534 /*
1535 * This is normal behavior, an application can die during the
1536 * creation process. Don't report an error so the execution can
1537 * continue normally.
1538 */
1539 ret = 0;
ffe60014
DG
1540 DBG3("UST app disable channel failed. Application is dead.");
1541 }
78f0bacd
DG
1542 goto error;
1543 }
1544
78f0bacd 1545 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
852d0037 1546 ua_chan->name, app->pid);
78f0bacd
DG
1547
1548error:
840cb59c 1549 health_code_update();
78f0bacd
DG
1550 return ret;
1551}
1552
1553/*
1554 * Enable the specified channel on to UST tracer for the UST session.
1555 */
1556static int enable_ust_channel(struct ust_app *app,
1557 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
1558{
1559 int ret;
1560
840cb59c 1561 health_code_update();
86acf0da 1562
fb45065e 1563 pthread_mutex_lock(&app->sock_lock);
852d0037 1564 ret = ustctl_enable(app->sock, ua_chan->obj);
fb45065e 1565 pthread_mutex_unlock(&app->sock_lock);
78f0bacd 1566 if (ret < 0) {
ffe60014
DG
1567 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1568 ERR("UST app channel %s enable failed for app (pid: %d) "
1569 "and session handle %d with ret %d",
1570 ua_chan->name, app->pid, ua_sess->handle, ret);
1571 } else {
3757b385
DG
1572 /*
1573 * This is normal behavior, an application can die during the
1574 * creation process. Don't report an error so the execution can
1575 * continue normally.
1576 */
1577 ret = 0;
ffe60014
DG
1578 DBG3("UST app enable channel failed. Application is dead.");
1579 }
78f0bacd
DG
1580 goto error;
1581 }
1582
1583 ua_chan->enabled = 1;
1584
1585 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
852d0037 1586 ua_chan->name, app->pid);
78f0bacd
DG
1587
1588error:
840cb59c 1589 health_code_update();
78f0bacd
DG
1590 return ret;
1591}
1592
edb67388
DG
1593/*
1594 * Enable the specified event on to UST tracer for the UST session.
1595 */
3428a1b7
JR
1596static int enable_ust_object(
1597 struct ust_app *app, struct lttng_ust_object_data *ust_object)
edb67388
DG
1598{
1599 int ret;
1600
840cb59c 1601 health_code_update();
86acf0da 1602
fb45065e 1603 pthread_mutex_lock(&app->sock_lock);
3428a1b7 1604 ret = ustctl_enable(app->sock, ust_object);
fb45065e 1605 pthread_mutex_unlock(&app->sock_lock);
edb67388 1606 if (ret < 0) {
ffe60014 1607 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
3428a1b7
JR
1608 ERR("UST app enable failed for object %p app (pid: %d) with ret %d",
1609 ust_object, app->pid, ret);
ffe60014 1610 } else {
3757b385
DG
1611 /*
1612 * This is normal behavior, an application can die during the
1613 * creation process. Don't report an error so the execution can
1614 * continue normally.
1615 */
1616 ret = 0;
3428a1b7 1617 DBG3("Failed to enable UST app object. Application is dead.");
ffe60014 1618 }
edb67388
DG
1619 goto error;
1620 }
1621
3428a1b7
JR
1622 DBG2("UST app object %p enabled successfully for app (pid: %d)",
1623 ust_object, app->pid);
edb67388
DG
1624
1625error:
840cb59c 1626 health_code_update();
edb67388
DG
1627 return ret;
1628}
1629
099e26bd 1630/*
7972aab2 1631 * Send channel and stream buffer to application.
4f3ab6ee 1632 *
ffe60014 1633 * Return 0 on success. On error, a negative value is returned.
4f3ab6ee 1634 */
7972aab2
DG
1635static int send_channel_pid_to_ust(struct ust_app *app,
1636 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
4f3ab6ee
DG
1637{
1638 int ret;
ffe60014 1639 struct ust_app_stream *stream, *stmp;
4f3ab6ee
DG
1640
1641 assert(app);
ffe60014 1642 assert(ua_sess);
4f3ab6ee 1643 assert(ua_chan);
4f3ab6ee 1644
840cb59c 1645 health_code_update();
4f3ab6ee 1646
7972aab2
DG
1647 DBG("UST app sending channel %s to UST app sock %d", ua_chan->name,
1648 app->sock);
86acf0da 1649
ffe60014
DG
1650 /* Send channel to the application. */
1651 ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan);
a7169585
MD
1652 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1653 ret = -ENOTCONN; /* Caused by app exiting. */
1654 goto error;
1655 } else if (ret < 0) {
b551a063
DG
1656 goto error;
1657 }
1658
d88aee68
DG
1659 health_code_update();
1660
ffe60014
DG
1661 /* Send all streams to application. */
1662 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
1663 ret = ust_consumer_send_stream_to_ust(app, ua_chan, stream);
a7169585
MD
1664 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1665 ret = -ENOTCONN; /* Caused by app exiting. */
1666 goto error;
1667 } else if (ret < 0) {
ffe60014
DG
1668 goto error;
1669 }
1670 /* We don't need the stream anymore once sent to the tracer. */
1671 cds_list_del(&stream->list);
fb45065e 1672 delete_ust_app_stream(-1, stream, app);
ffe60014 1673 }
ffe60014
DG
1674 /* Flag the channel that it is sent to the application. */
1675 ua_chan->is_sent = 1;
ffe60014 1676
b551a063 1677error:
840cb59c 1678 health_code_update();
b551a063
DG
1679 return ret;
1680}
1681
91d76f53 1682/*
5b4a0ec0 1683 * Create the specified event onto the UST tracer for a UST session.
d0b96690
DG
1684 *
1685 * Should be called with session mutex held.
91d76f53 1686 */
edb67388
DG
1687static
1688int create_ust_event(struct ust_app *app, struct ust_app_session *ua_sess,
1689 struct ust_app_channel *ua_chan, struct ust_app_event *ua_event)
91d76f53 1690{
5b4a0ec0 1691 int ret = 0;
284d8f55 1692
840cb59c 1693 health_code_update();
86acf0da 1694
5b4a0ec0 1695 /* Create UST event on tracer */
fb45065e 1696 pthread_mutex_lock(&app->sock_lock);
852d0037 1697 ret = ustctl_create_event(app->sock, &ua_event->attr, ua_chan->obj,
5b4a0ec0 1698 &ua_event->obj);
fb45065e 1699 pthread_mutex_unlock(&app->sock_lock);
5b4a0ec0 1700 if (ret < 0) {
ffe60014 1701 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
88e3c2f5 1702 abort();
ffe60014
DG
1703 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1704 ua_event->attr.name, app->pid, ret);
1705 } else {
3757b385
DG
1706 /*
1707 * This is normal behavior, an application can die during the
1708 * creation process. Don't report an error so the execution can
1709 * continue normally.
1710 */
1711 ret = 0;
ffe60014
DG
1712 DBG3("UST app create event failed. Application is dead.");
1713 }
5b4a0ec0 1714 goto error;
91d76f53 1715 }
f6a9efaa 1716
5b4a0ec0 1717 ua_event->handle = ua_event->obj->handle;
284d8f55 1718
3428a1b7
JR
1719 DBG2("UST app event %s created successfully for pid:%d object: %p",
1720 ua_event->attr.name, app->pid, ua_event->obj);
f6a9efaa 1721
840cb59c 1722 health_code_update();
86acf0da 1723
025faf73
DG
1724 /* Set filter if one is present. */
1725 if (ua_event->filter) {
a154c7b8 1726 ret = set_ust_object_filter(app, ua_event->filter, ua_event->obj);
025faf73
DG
1727 if (ret < 0) {
1728 goto error;
1729 }
1730 }
1731
7cc9a73c
JI
1732 /* Set exclusions for the event */
1733 if (ua_event->exclusion) {
c0901ffa 1734 ret = set_ust_object_exclusions(app, ua_event->exclusion, ua_event->obj);
7cc9a73c
JI
1735 if (ret < 0) {
1736 goto error;
1737 }
1738 }
1739
8535a6d9 1740 /* If event not enabled, disable it on the tracer */
40113787
MD
1741 if (ua_event->enabled) {
1742 /*
1743 * We now need to explicitly enable the event, since it
1744 * is now disabled at creation.
1745 */
3428a1b7 1746 ret = enable_ust_object(app, ua_event->obj);
40113787
MD
1747 if (ret < 0) {
1748 /*
1749 * If we hit an EPERM, something is wrong with our enable call. If
1750 * we get an EEXIST, there is a problem on the tracer side since we
1751 * just created it.
1752 */
1753 switch (ret) {
1754 case -LTTNG_UST_ERR_PERM:
1755 /* Code flow problem */
1756 assert(0);
1757 case -LTTNG_UST_ERR_EXIST:
1758 /* It's OK for our use case. */
1759 ret = 0;
1760 break;
1761 default:
1762 break;
1763 }
1764 goto error;
1765 }
8535a6d9
DG
1766 }
1767
5b4a0ec0 1768error:
840cb59c 1769 health_code_update();
5b4a0ec0 1770 return ret;
91d76f53 1771}
48842b30 1772
5b4a0ec0
DG
1773/*
1774 * Copy data between an UST app event and a LTT event.
1775 */
421cb601 1776static void shadow_copy_event(struct ust_app_event *ua_event,
48842b30
DG
1777 struct ltt_ust_event *uevent)
1778{
b4ffad32
JI
1779 size_t exclusion_alloc_size;
1780
48842b30
DG
1781 strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name));
1782 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
1783
fc34caaa
DG
1784 ua_event->enabled = uevent->enabled;
1785
5b4a0ec0
DG
1786 /* Copy event attributes */
1787 memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr));
1788
53a80697
MD
1789 /* Copy filter bytecode */
1790 if (uevent->filter) {
51755dc8 1791 ua_event->filter = copy_filter_bytecode(uevent->filter);
025faf73 1792 /* Filter might be NULL here in case of ENONEM. */
53a80697 1793 }
b4ffad32
JI
1794
1795 /* Copy exclusion data */
1796 if (uevent->exclusion) {
51755dc8 1797 exclusion_alloc_size = sizeof(struct lttng_event_exclusion) +
b4ffad32
JI
1798 LTTNG_UST_SYM_NAME_LEN * uevent->exclusion->count;
1799 ua_event->exclusion = zmalloc(exclusion_alloc_size);
5f8df26c
JI
1800 if (ua_event->exclusion == NULL) {
1801 PERROR("malloc");
1802 } else {
1803 memcpy(ua_event->exclusion, uevent->exclusion,
1804 exclusion_alloc_size);
b4ffad32
JI
1805 }
1806 }
48842b30
DG
1807}
1808
5b4a0ec0
DG
1809/*
1810 * Copy data between an UST app channel and a LTT channel.
1811 */
421cb601 1812static void shadow_copy_channel(struct ust_app_channel *ua_chan,
48842b30
DG
1813 struct ltt_ust_channel *uchan)
1814{
fc34caaa 1815 DBG2("UST app shadow copy of channel %s started", ua_chan->name);
48842b30
DG
1816
1817 strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name));
1818 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
ffe60014 1819
1624d5b7
JD
1820 ua_chan->tracefile_size = uchan->tracefile_size;
1821 ua_chan->tracefile_count = uchan->tracefile_count;
1822
ffe60014
DG
1823 /* Copy event attributes since the layout is different. */
1824 ua_chan->attr.subbuf_size = uchan->attr.subbuf_size;
1825 ua_chan->attr.num_subbuf = uchan->attr.num_subbuf;
1826 ua_chan->attr.overwrite = uchan->attr.overwrite;
1827 ua_chan->attr.switch_timer_interval = uchan->attr.switch_timer_interval;
1828 ua_chan->attr.read_timer_interval = uchan->attr.read_timer_interval;
e9404c27 1829 ua_chan->monitor_timer_interval = uchan->monitor_timer_interval;
ffe60014 1830 ua_chan->attr.output = uchan->attr.output;
491d1539
MD
1831 ua_chan->attr.blocking_timeout = uchan->attr.u.s.blocking_timeout;
1832
ffe60014
DG
1833 /*
1834 * Note that the attribute channel type is not set since the channel on the
1835 * tracing registry side does not have this information.
1836 */
48842b30 1837
fc34caaa 1838 ua_chan->enabled = uchan->enabled;
7972aab2 1839 ua_chan->tracing_channel_id = uchan->id;
fc34caaa 1840
fc34caaa 1841 DBG3("UST app shadow copy of channel %s done", ua_chan->name);
48842b30
DG
1842}
1843
5b4a0ec0
DG
1844/*
1845 * Copy data between a UST app session and a regular LTT session.
1846 */
421cb601 1847static void shadow_copy_session(struct ust_app_session *ua_sess,
bec39940 1848 struct ltt_ust_session *usess, struct ust_app *app)
48842b30 1849{
477d7741
MD
1850 struct tm *timeinfo;
1851 char datetime[16];
1852 int ret;
d7ba1388 1853 char tmp_shm_path[PATH_MAX];
477d7741 1854
940c4592 1855 timeinfo = localtime(&app->registration_time);
477d7741 1856 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
48842b30 1857
421cb601 1858 DBG2("Shadow copy of session handle %d", ua_sess->handle);
48842b30 1859
7972aab2
DG
1860 ua_sess->tracing_id = usess->id;
1861 ua_sess->id = get_next_session_id();
ff588497
JR
1862 LTTNG_OPTIONAL_SET(&ua_sess->real_credentials.uid, app->uid);
1863 LTTNG_OPTIONAL_SET(&ua_sess->real_credentials.gid, app->gid);
1864 LTTNG_OPTIONAL_SET(&ua_sess->effective_credentials.uid, usess->uid);
1865 LTTNG_OPTIONAL_SET(&ua_sess->effective_credentials.gid, usess->gid);
7972aab2
DG
1866 ua_sess->buffer_type = usess->buffer_type;
1867 ua_sess->bits_per_long = app->bits_per_long;
6addfa37 1868
7972aab2 1869 /* There is only one consumer object per session possible. */
6addfa37 1870 consumer_output_get(usess->consumer);
7972aab2 1871 ua_sess->consumer = usess->consumer;
6addfa37 1872
2bba9e53 1873 ua_sess->output_traces = usess->output_traces;
ecc48a90 1874 ua_sess->live_timer_interval = usess->live_timer_interval;
84ad93e8
DG
1875 copy_channel_attr_to_ustctl(&ua_sess->metadata_attr,
1876 &usess->metadata_attr);
7972aab2
DG
1877
1878 switch (ua_sess->buffer_type) {
1879 case LTTNG_BUFFER_PER_PID:
1880 ret = snprintf(ua_sess->path, sizeof(ua_sess->path),
dec56f6c 1881 DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s", app->name, app->pid,
7972aab2
DG
1882 datetime);
1883 break;
1884 case LTTNG_BUFFER_PER_UID:
1885 ret = snprintf(ua_sess->path, sizeof(ua_sess->path),
470cc211 1886 DEFAULT_UST_TRACE_UID_PATH,
ff588497 1887 lttng_credentials_get_uid(&ua_sess->real_credentials),
470cc211 1888 app->bits_per_long);
7972aab2
DG
1889 break;
1890 default:
1891 assert(0);
1892 goto error;
1893 }
477d7741
MD
1894 if (ret < 0) {
1895 PERROR("asprintf UST shadow copy session");
477d7741 1896 assert(0);
7972aab2 1897 goto error;
477d7741
MD
1898 }
1899
3d071855
MD
1900 strncpy(ua_sess->root_shm_path, usess->root_shm_path,
1901 sizeof(ua_sess->root_shm_path));
1902 ua_sess->root_shm_path[sizeof(ua_sess->root_shm_path) - 1] = '\0';
d7ba1388
MD
1903 strncpy(ua_sess->shm_path, usess->shm_path,
1904 sizeof(ua_sess->shm_path));
1905 ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0';
1906 if (ua_sess->shm_path[0]) {
1907 switch (ua_sess->buffer_type) {
1908 case LTTNG_BUFFER_PER_PID:
1909 ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path),
5da88b0f 1910 "/" DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s",
d7ba1388
MD
1911 app->name, app->pid, datetime);
1912 break;
1913 case LTTNG_BUFFER_PER_UID:
1914 ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path),
5da88b0f 1915 "/" DEFAULT_UST_TRACE_UID_PATH,
d7ba1388
MD
1916 app->uid, app->bits_per_long);
1917 break;
1918 default:
1919 assert(0);
1920 goto error;
1921 }
1922 if (ret < 0) {
1923 PERROR("sprintf UST shadow copy session");
1924 assert(0);
1925 goto error;
1926 }
1927 strncat(ua_sess->shm_path, tmp_shm_path,
1928 sizeof(ua_sess->shm_path) - strlen(ua_sess->shm_path) - 1);
1929 ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0';
1930 }
6addfa37 1931 return;
7972aab2
DG
1932
1933error:
6addfa37 1934 consumer_output_put(ua_sess->consumer);
48842b30
DG
1935}
1936
78f0bacd
DG
1937/*
1938 * Lookup sesison wrapper.
1939 */
84cd17c6 1940static
fb9a95c4 1941void __lookup_session_by_app(const struct ltt_ust_session *usess,
bec39940 1942 struct ust_app *app, struct lttng_ht_iter *iter)
84cd17c6
MD
1943{
1944 /* Get right UST app session from app */
d9bf3ca4 1945 lttng_ht_lookup(app->sessions, &usess->id, iter);
84cd17c6
MD
1946}
1947
421cb601
DG
1948/*
1949 * Return ust app session from the app session hashtable using the UST session
a991f516 1950 * id.
421cb601 1951 */
48842b30 1952static struct ust_app_session *lookup_session_by_app(
fb9a95c4 1953 const struct ltt_ust_session *usess, struct ust_app *app)
48842b30 1954{
bec39940 1955 struct lttng_ht_iter iter;
d9bf3ca4 1956 struct lttng_ht_node_u64 *node;
48842b30 1957
84cd17c6 1958 __lookup_session_by_app(usess, app, &iter);
d9bf3ca4 1959 node = lttng_ht_iter_get_node_u64(&iter);
48842b30
DG
1960 if (node == NULL) {
1961 goto error;
1962 }
1963
1964 return caa_container_of(node, struct ust_app_session, node);
1965
1966error:
1967 return NULL;
1968}
1969
7972aab2
DG
1970/*
1971 * Setup buffer registry per PID for the given session and application. If none
1972 * is found, a new one is created, added to the global registry and
1973 * initialized. If regp is valid, it's set with the newly created object.
1974 *
1975 * Return 0 on success or else a negative value.
1976 */
1977static int setup_buffer_reg_pid(struct ust_app_session *ua_sess,
1978 struct ust_app *app, struct buffer_reg_pid **regp)
1979{
1980 int ret = 0;
1981 struct buffer_reg_pid *reg_pid;
1982
1983 assert(ua_sess);
1984 assert(app);
1985
1986 rcu_read_lock();
1987
1988 reg_pid = buffer_reg_pid_find(ua_sess->id);
1989 if (!reg_pid) {
1990 /*
1991 * This is the create channel path meaning that if there is NO
1992 * registry available, we have to create one for this session.
1993 */
d7ba1388 1994 ret = buffer_reg_pid_create(ua_sess->id, &reg_pid,
3d071855 1995 ua_sess->root_shm_path, ua_sess->shm_path);
7972aab2
DG
1996 if (ret < 0) {
1997 goto error;
1998 }
7972aab2
DG
1999 } else {
2000 goto end;
2001 }
2002
2003 /* Initialize registry. */
2004 ret = ust_registry_session_init(&reg_pid->registry->reg.ust, app,
2005 app->bits_per_long, app->uint8_t_alignment,
2006 app->uint16_t_alignment, app->uint32_t_alignment,
af6142cf 2007 app->uint64_t_alignment, app->long_alignment,
470cc211
JG
2008 app->byte_order, app->version.major, app->version.minor,
2009 reg_pid->root_shm_path, reg_pid->shm_path,
ff588497
JR
2010 lttng_credentials_get_uid(&ua_sess->effective_credentials),
2011 lttng_credentials_get_gid(&ua_sess->effective_credentials),
2012 ua_sess->tracing_id,
8de88061 2013 app->uid);
7972aab2 2014 if (ret < 0) {
286c991a
MD
2015 /*
2016 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2017 * destroy the buffer registry, because it is always expected
2018 * that if the buffer registry can be found, its ust registry is
2019 * non-NULL.
2020 */
2021 buffer_reg_pid_destroy(reg_pid);
7972aab2
DG
2022 goto error;
2023 }
2024
286c991a
MD
2025 buffer_reg_pid_add(reg_pid);
2026
7972aab2
DG
2027 DBG3("UST app buffer registry per PID created successfully");
2028
2029end:
2030 if (regp) {
2031 *regp = reg_pid;
2032 }
2033error:
2034 rcu_read_unlock();
2035 return ret;
2036}
2037
2038/*
2039 * Setup buffer registry per UID for the given session and application. If none
2040 * is found, a new one is created, added to the global registry and
2041 * initialized. If regp is valid, it's set with the newly created object.
2042 *
2043 * Return 0 on success or else a negative value.
2044 */
2045static int setup_buffer_reg_uid(struct ltt_ust_session *usess,
d7ba1388 2046 struct ust_app_session *ua_sess,
7972aab2
DG
2047 struct ust_app *app, struct buffer_reg_uid **regp)
2048{
2049 int ret = 0;
2050 struct buffer_reg_uid *reg_uid;
2051
2052 assert(usess);
2053 assert(app);
2054
2055 rcu_read_lock();
2056
2057 reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid);
2058 if (!reg_uid) {
2059 /*
2060 * This is the create channel path meaning that if there is NO
2061 * registry available, we have to create one for this session.
2062 */
2063 ret = buffer_reg_uid_create(usess->id, app->bits_per_long, app->uid,
3d071855
MD
2064 LTTNG_DOMAIN_UST, &reg_uid,
2065 ua_sess->root_shm_path, ua_sess->shm_path);
7972aab2
DG
2066 if (ret < 0) {
2067 goto error;
2068 }
7972aab2
DG
2069 } else {
2070 goto end;
2071 }
2072
2073 /* Initialize registry. */
af6142cf 2074 ret = ust_registry_session_init(&reg_uid->registry->reg.ust, NULL,
7972aab2
DG
2075 app->bits_per_long, app->uint8_t_alignment,
2076 app->uint16_t_alignment, app->uint32_t_alignment,
af6142cf
MD
2077 app->uint64_t_alignment, app->long_alignment,
2078 app->byte_order, app->version.major,
3d071855 2079 app->version.minor, reg_uid->root_shm_path,
8de88061
JR
2080 reg_uid->shm_path, usess->uid, usess->gid,
2081 ua_sess->tracing_id, app->uid);
7972aab2 2082 if (ret < 0) {
286c991a
MD
2083 /*
2084 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2085 * destroy the buffer registry, because it is always expected
2086 * that if the buffer registry can be found, its ust registry is
2087 * non-NULL.
2088 */
2089 buffer_reg_uid_destroy(reg_uid, NULL);
7972aab2
DG
2090 goto error;
2091 }
2092 /* Add node to teardown list of the session. */
2093 cds_list_add(&reg_uid->lnode, &usess->buffer_reg_uid_list);
2094
286c991a 2095 buffer_reg_uid_add(reg_uid);
7972aab2 2096
286c991a 2097 DBG3("UST app buffer registry per UID created successfully");
7972aab2
DG
2098end:
2099 if (regp) {
2100 *regp = reg_uid;
2101 }
2102error:
2103 rcu_read_unlock();
2104 return ret;
2105}
2106
421cb601 2107/*
3d8ca23b 2108 * Create a session on the tracer side for the given app.
421cb601 2109 *
3d8ca23b
DG
2110 * On success, ua_sess_ptr is populated with the session pointer or else left
2111 * untouched. If the session was created, is_created is set to 1. On error,
2112 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2113 * be NULL.
2114 *
2115 * Returns 0 on success or else a negative code which is either -ENOMEM or
2116 * -ENOTCONN which is the default code if the ustctl_create_session fails.
421cb601 2117 */
03f91eaa 2118static int find_or_create_ust_app_session(struct ltt_ust_session *usess,
3d8ca23b
DG
2119 struct ust_app *app, struct ust_app_session **ua_sess_ptr,
2120 int *is_created)
421cb601 2121{
3d8ca23b 2122 int ret, created = 0;
421cb601
DG
2123 struct ust_app_session *ua_sess;
2124
3d8ca23b
DG
2125 assert(usess);
2126 assert(app);
2127 assert(ua_sess_ptr);
2128
840cb59c 2129 health_code_update();
86acf0da 2130
421cb601
DG
2131 ua_sess = lookup_session_by_app(usess, app);
2132 if (ua_sess == NULL) {
d9bf3ca4 2133 DBG2("UST app pid: %d session id %" PRIu64 " not found, creating it",
852d0037 2134 app->pid, usess->id);
40bbd087 2135 ua_sess = alloc_ust_app_session();
421cb601
DG
2136 if (ua_sess == NULL) {
2137 /* Only malloc can failed so something is really wrong */
3d8ca23b
DG
2138 ret = -ENOMEM;
2139 goto error;
421cb601 2140 }
477d7741 2141 shadow_copy_session(ua_sess, usess, app);
3d8ca23b 2142 created = 1;
421cb601
DG
2143 }
2144
7972aab2
DG
2145 switch (usess->buffer_type) {
2146 case LTTNG_BUFFER_PER_PID:
2147 /* Init local registry. */
2148 ret = setup_buffer_reg_pid(ua_sess, app, NULL);
421cb601 2149 if (ret < 0) {
e64207cf 2150 delete_ust_app_session(-1, ua_sess, app);
7972aab2
DG
2151 goto error;
2152 }
2153 break;
2154 case LTTNG_BUFFER_PER_UID:
2155 /* Look for a global registry. If none exists, create one. */
d7ba1388 2156 ret = setup_buffer_reg_uid(usess, ua_sess, app, NULL);
7972aab2 2157 if (ret < 0) {
e64207cf 2158 delete_ust_app_session(-1, ua_sess, app);
7972aab2
DG
2159 goto error;
2160 }
2161 break;
2162 default:
2163 assert(0);
2164 ret = -EINVAL;
2165 goto error;
2166 }
2167
2168 health_code_update();
2169
2170 if (ua_sess->handle == -1) {
fb45065e 2171 pthread_mutex_lock(&app->sock_lock);
7972aab2 2172 ret = ustctl_create_session(app->sock);
fb45065e 2173 pthread_mutex_unlock(&app->sock_lock);
7972aab2
DG
2174 if (ret < 0) {
2175 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
2176 ERR("Creating session for app pid %d with ret %d",
ffe60014
DG
2177 app->pid, ret);
2178 } else {
2179 DBG("UST app creating session failed. Application is dead");
3757b385
DG
2180 /*
2181 * This is normal behavior, an application can die during the
2182 * creation process. Don't report an error so the execution can
2183 * continue normally. This will get flagged ENOTCONN and the
2184 * caller will handle it.
2185 */
2186 ret = 0;
ffe60014 2187 }
d0b96690 2188 delete_ust_app_session(-1, ua_sess, app);
3d8ca23b
DG
2189 if (ret != -ENOMEM) {
2190 /*
2191 * Tracer is probably gone or got an internal error so let's
2192 * behave like it will soon unregister or not usable.
2193 */
2194 ret = -ENOTCONN;
2195 }
2196 goto error;
421cb601
DG
2197 }
2198
7972aab2
DG
2199 ua_sess->handle = ret;
2200
2201 /* Add ust app session to app's HT */
d9bf3ca4
MD
2202 lttng_ht_node_init_u64(&ua_sess->node,
2203 ua_sess->tracing_id);
2204 lttng_ht_add_unique_u64(app->sessions, &ua_sess->node);
10b56aef
MD
2205 lttng_ht_node_init_ulong(&ua_sess->ust_objd_node, ua_sess->handle);
2206 lttng_ht_add_unique_ulong(app->ust_sessions_objd,
2207 &ua_sess->ust_objd_node);
7972aab2
DG
2208
2209 DBG2("UST app session created successfully with handle %d", ret);
2210 }
2211
2212 *ua_sess_ptr = ua_sess;
2213 if (is_created) {
2214 *is_created = created;
2215 }
2216
2217 /* Everything went well. */
2218 ret = 0;
2219
2220error:
2221 health_code_update();
2222 return ret;
2223}
2224
6a6b2068
JG
2225/*
2226 * Match function for a hash table lookup of ust_app_ctx.
2227 *
2228 * It matches an ust app context based on the context type and, in the case
2229 * of perf counters, their name.
2230 */
2231static int ht_match_ust_app_ctx(struct cds_lfht_node *node, const void *_key)
2232{
2233 struct ust_app_ctx *ctx;
bdf64013 2234 const struct lttng_ust_context_attr *key;
6a6b2068
JG
2235
2236 assert(node);
2237 assert(_key);
2238
2239 ctx = caa_container_of(node, struct ust_app_ctx, node.node);
2240 key = _key;
2241
2242 /* Context type */
2243 if (ctx->ctx.ctx != key->ctx) {
2244 goto no_match;
2245 }
2246
bdf64013
JG
2247 switch(key->ctx) {
2248 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER:
6a6b2068 2249 if (strncmp(key->u.perf_counter.name,
bdf64013
JG
2250 ctx->ctx.u.perf_counter.name,
2251 sizeof(key->u.perf_counter.name))) {
2252 goto no_match;
2253 }
2254 break;
2255 case LTTNG_UST_CONTEXT_APP_CONTEXT:
2256 if (strcmp(key->u.app_ctx.provider_name,
2257 ctx->ctx.u.app_ctx.provider_name) ||
2258 strcmp(key->u.app_ctx.ctx_name,
2259 ctx->ctx.u.app_ctx.ctx_name)) {
6a6b2068
JG
2260 goto no_match;
2261 }
bdf64013
JG
2262 break;
2263 default:
2264 break;
6a6b2068
JG
2265 }
2266
2267 /* Match. */
2268 return 1;
2269
2270no_match:
2271 return 0;
2272}
2273
2274/*
2275 * Lookup for an ust app context from an lttng_ust_context.
2276 *
be184a0f 2277 * Must be called while holding RCU read side lock.
6a6b2068
JG
2278 * Return an ust_app_ctx object or NULL on error.
2279 */
2280static
2281struct ust_app_ctx *find_ust_app_context(struct lttng_ht *ht,
bdf64013 2282 struct lttng_ust_context_attr *uctx)
6a6b2068
JG
2283{
2284 struct lttng_ht_iter iter;
2285 struct lttng_ht_node_ulong *node;
2286 struct ust_app_ctx *app_ctx = NULL;
2287
2288 assert(uctx);
2289 assert(ht);
2290
2291 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2292 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) uctx->ctx, lttng_ht_seed),
2293 ht_match_ust_app_ctx, uctx, &iter.iter);
2294 node = lttng_ht_iter_get_node_ulong(&iter);
2295 if (!node) {
2296 goto end;
2297 }
2298
2299 app_ctx = caa_container_of(node, struct ust_app_ctx, node);
2300
2301end:
2302 return app_ctx;
2303}
2304
7972aab2
DG
2305/*
2306 * Create a context for the channel on the tracer.
2307 *
2308 * Called with UST app session lock held and a RCU read side lock.
2309 */
2310static
c9edf082 2311int create_ust_app_channel_context(struct ust_app_channel *ua_chan,
f3db82be 2312 struct lttng_ust_context_attr *uctx,
7972aab2
DG
2313 struct ust_app *app)
2314{
2315 int ret = 0;
7972aab2
DG
2316 struct ust_app_ctx *ua_ctx;
2317
2318 DBG2("UST app adding context to channel %s", ua_chan->name);
2319
6a6b2068
JG
2320 ua_ctx = find_ust_app_context(ua_chan->ctx, uctx);
2321 if (ua_ctx) {
7972aab2
DG
2322 ret = -EEXIST;
2323 goto error;
2324 }
2325
2326 ua_ctx = alloc_ust_app_ctx(uctx);
2327 if (ua_ctx == NULL) {
2328 /* malloc failed */
7682f304 2329 ret = -ENOMEM;
7972aab2
DG
2330 goto error;
2331 }
2332
2333 lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx);
aa3514e9 2334 lttng_ht_add_ulong(ua_chan->ctx, &ua_ctx->node);
31746f93 2335 cds_list_add_tail(&ua_ctx->list, &ua_chan->ctx_list);
7972aab2
DG
2336
2337 ret = create_ust_channel_context(ua_chan, ua_ctx, app);
2338 if (ret < 0) {
2339 goto error;
2340 }
2341
2342error:
2343 return ret;
2344}
2345
2346/*
2347 * Enable on the tracer side a ust app event for the session and channel.
2348 *
2349 * Called with UST app session lock held.
2350 */
2351static
2352int enable_ust_app_event(struct ust_app_session *ua_sess,
2353 struct ust_app_event *ua_event, struct ust_app *app)
2354{
2355 int ret;
2356
3428a1b7 2357 ret = enable_ust_object(app, ua_event->obj);
7972aab2
DG
2358 if (ret < 0) {
2359 goto error;
2360 }
2361
2362 ua_event->enabled = 1;
2363
2364error:
2365 return ret;
2366}
2367
2368/*
2369 * Disable on the tracer side a ust app event for the session and channel.
2370 */
2371static int disable_ust_app_event(struct ust_app_session *ua_sess,
2372 struct ust_app_event *ua_event, struct ust_app *app)
2373{
2374 int ret;
2375
e2456d0a 2376 ret = disable_ust_object(app, ua_event->obj);
7972aab2
DG
2377 if (ret < 0) {
2378 goto error;
2379 }
2380
2381 ua_event->enabled = 0;
2382
2383error:
2384 return ret;
2385}
2386
2387/*
2388 * Lookup ust app channel for session and disable it on the tracer side.
2389 */
2390static
2391int disable_ust_app_channel(struct ust_app_session *ua_sess,
2392 struct ust_app_channel *ua_chan, struct ust_app *app)
2393{
2394 int ret;
2395
2396 ret = disable_ust_channel(app, ua_sess, ua_chan);
2397 if (ret < 0) {
2398 goto error;
2399 }
2400
2401 ua_chan->enabled = 0;
2402
2403error:
2404 return ret;
2405}
2406
2407/*
2408 * Lookup ust app channel for session and enable it on the tracer side. This
2409 * MUST be called with a RCU read side lock acquired.
2410 */
2411static int enable_ust_app_channel(struct ust_app_session *ua_sess,
2412 struct ltt_ust_channel *uchan, struct ust_app *app)
2413{
2414 int ret = 0;
2415 struct lttng_ht_iter iter;
2416 struct lttng_ht_node_str *ua_chan_node;
2417 struct ust_app_channel *ua_chan;
2418
2419 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
2420 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
2421 if (ua_chan_node == NULL) {
d9bf3ca4 2422 DBG2("Unable to find channel %s in ust session id %" PRIu64,
7972aab2
DG
2423 uchan->name, ua_sess->tracing_id);
2424 goto error;
2425 }
2426
2427 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2428
2429 ret = enable_ust_channel(app, ua_sess, ua_chan);
2430 if (ret < 0) {
2431 goto error;
2432 }
2433
2434error:
2435 return ret;
2436}
2437
2438/*
2439 * Ask the consumer to create a channel and get it if successful.
2440 *
fad1ed2f
JR
2441 * Called with UST app session lock held.
2442 *
7972aab2
DG
2443 * Return 0 on success or else a negative value.
2444 */
2445static int do_consumer_create_channel(struct ltt_ust_session *usess,
2446 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan,
e098433c
JG
2447 int bitness, struct ust_registry_session *registry,
2448 uint64_t trace_archive_id)
7972aab2
DG
2449{
2450 int ret;
2451 unsigned int nb_fd = 0;
2452 struct consumer_socket *socket;
2453
2454 assert(usess);
2455 assert(ua_sess);
2456 assert(ua_chan);
2457 assert(registry);
2458
2459 rcu_read_lock();
2460 health_code_update();
2461
2462 /* Get the right consumer socket for the application. */
2463 socket = consumer_find_socket_by_bitness(bitness, usess->consumer);
2464 if (!socket) {
2465 ret = -EINVAL;
2466 goto error;
2467 }
2468
2469 health_code_update();
2470
2471 /* Need one fd for the channel. */
2472 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
2473 if (ret < 0) {
2474 ERR("Exhausted number of available FD upon create channel");
2475 goto error;
2476 }
2477
2478 /*
2479 * Ask consumer to create channel. The consumer will return the number of
2480 * stream we have to expect.
2481 */
2482 ret = ust_consumer_ask_channel(ua_sess, ua_chan, usess->consumer, socket,
d2956687 2483 registry, usess->current_trace_chunk);
7972aab2
DG
2484 if (ret < 0) {
2485 goto error_ask;
2486 }
2487
2488 /*
2489 * Compute the number of fd needed before receiving them. It must be 2 per
2490 * stream (2 being the default value here).
2491 */
2492 nb_fd = DEFAULT_UST_STREAM_FD_NUM * ua_chan->expected_stream_count;
2493
2494 /* Reserve the amount of file descriptor we need. */
2495 ret = lttng_fd_get(LTTNG_FD_APPS, nb_fd);
2496 if (ret < 0) {
2497 ERR("Exhausted number of available FD upon create channel");
2498 goto error_fd_get_stream;
2499 }
2500
2501 health_code_update();
2502
2503 /*
2504 * Now get the channel from the consumer. This call wil populate the stream
2505 * list of that channel and set the ust objects.
2506 */
d9078d0c
DG
2507 if (usess->consumer->enabled) {
2508 ret = ust_consumer_get_channel(socket, ua_chan);
2509 if (ret < 0) {
2510 goto error_destroy;
2511 }
7972aab2
DG
2512 }
2513
2514 rcu_read_unlock();
2515 return 0;
2516
2517error_destroy:
2518 lttng_fd_put(LTTNG_FD_APPS, nb_fd);
2519error_fd_get_stream:
2520 /*
2521 * Initiate a destroy channel on the consumer since we had an error
2522 * handling it on our side. The return value is of no importance since we
2523 * already have a ret value set by the previous error that we need to
2524 * return.
2525 */
2526 (void) ust_consumer_destroy_channel(socket, ua_chan);
2527error_ask:
2528 lttng_fd_put(LTTNG_FD_APPS, 1);
2529error:
2530 health_code_update();
2531 rcu_read_unlock();
2532 return ret;
2533}
2534
2535/*
2536 * Duplicate the ust data object of the ust app stream and save it in the
2537 * buffer registry stream.
2538 *
2539 * Return 0 on success or else a negative value.
2540 */
2541static int duplicate_stream_object(struct buffer_reg_stream *reg_stream,
2542 struct ust_app_stream *stream)
2543{
2544 int ret;
2545
2546 assert(reg_stream);
2547 assert(stream);
2548
2549 /* Reserve the amount of file descriptor we need. */
2550 ret = lttng_fd_get(LTTNG_FD_APPS, 2);
2551 if (ret < 0) {
2552 ERR("Exhausted number of available FD upon duplicate stream");
2553 goto error;
2554 }
2555
2556 /* Duplicate object for stream once the original is in the registry. */
2557 ret = ustctl_duplicate_ust_object_data(&stream->obj,
2558 reg_stream->obj.ust);
2559 if (ret < 0) {
2560 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2561 reg_stream->obj.ust, stream->obj, ret);
2562 lttng_fd_put(LTTNG_FD_APPS, 2);
2563 goto error;
2564 }
2565 stream->handle = stream->obj->handle;
2566
2567error:
2568 return ret;
2569}
2570
2571/*
2572 * Duplicate the ust data object of the ust app. channel and save it in the
2573 * buffer registry channel.
2574 *
2575 * Return 0 on success or else a negative value.
2576 */
2577static int duplicate_channel_object(struct buffer_reg_channel *reg_chan,
2578 struct ust_app_channel *ua_chan)
2579{
2580 int ret;
2581
2582 assert(reg_chan);
2583 assert(ua_chan);
2584
2585 /* Need two fds for the channel. */
2586 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
2587 if (ret < 0) {
2588 ERR("Exhausted number of available FD upon duplicate channel");
2589 goto error_fd_get;
2590 }
2591
2592 /* Duplicate object for stream once the original is in the registry. */
2593 ret = ustctl_duplicate_ust_object_data(&ua_chan->obj, reg_chan->obj.ust);
2594 if (ret < 0) {
2595 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2596 reg_chan->obj.ust, ua_chan->obj, ret);
2597 goto error;
2598 }
2599 ua_chan->handle = ua_chan->obj->handle;
2600
2601 return 0;
2602
2603error:
2604 lttng_fd_put(LTTNG_FD_APPS, 1);
2605error_fd_get:
2606 return ret;
2607}
2608
2609/*
2610 * For a given channel buffer registry, setup all streams of the given ust
2611 * application channel.
2612 *
2613 * Return 0 on success or else a negative value.
2614 */
2615static int setup_buffer_reg_streams(struct buffer_reg_channel *reg_chan,
fb45065e
MD
2616 struct ust_app_channel *ua_chan,
2617 struct ust_app *app)
7972aab2
DG
2618{
2619 int ret = 0;
2620 struct ust_app_stream *stream, *stmp;
2621
2622 assert(reg_chan);
2623 assert(ua_chan);
2624
2625 DBG2("UST app setup buffer registry stream");
2626
2627 /* Send all streams to application. */
2628 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
2629 struct buffer_reg_stream *reg_stream;
2630
2631 ret = buffer_reg_stream_create(&reg_stream);
2632 if (ret < 0) {
2633 goto error;
2634 }
2635
2636 /*
2637 * Keep original pointer and nullify it in the stream so the delete
2638 * stream call does not release the object.
2639 */
2640 reg_stream->obj.ust = stream->obj;
2641 stream->obj = NULL;
2642 buffer_reg_stream_add(reg_stream, reg_chan);
421cb601 2643
7972aab2
DG
2644 /* We don't need the streams anymore. */
2645 cds_list_del(&stream->list);
fb45065e 2646 delete_ust_app_stream(-1, stream, app);
7972aab2 2647 }
421cb601 2648
7972aab2
DG
2649error:
2650 return ret;
2651}
2652
2653/*
2654 * Create a buffer registry channel for the given session registry and
2655 * application channel object. If regp pointer is valid, it's set with the
2656 * created object. Important, the created object is NOT added to the session
2657 * registry hash table.
2658 *
2659 * Return 0 on success else a negative value.
2660 */
2661static int create_buffer_reg_channel(struct buffer_reg_session *reg_sess,
2662 struct ust_app_channel *ua_chan, struct buffer_reg_channel **regp)
2663{
2664 int ret;
2665 struct buffer_reg_channel *reg_chan = NULL;
2666
2667 assert(reg_sess);
2668 assert(ua_chan);
2669
2670 DBG2("UST app creating buffer registry channel for %s", ua_chan->name);
2671
2672 /* Create buffer registry channel. */
2673 ret = buffer_reg_channel_create(ua_chan->tracing_channel_id, &reg_chan);
2674 if (ret < 0) {
2675 goto error_create;
421cb601 2676 }
7972aab2
DG
2677 assert(reg_chan);
2678 reg_chan->consumer_key = ua_chan->key;
8c924c7b 2679 reg_chan->subbuf_size = ua_chan->attr.subbuf_size;
d07ceecd 2680 reg_chan->num_subbuf = ua_chan->attr.num_subbuf;
421cb601 2681
7972aab2
DG
2682 /* Create and add a channel registry to session. */
2683 ret = ust_registry_channel_add(reg_sess->reg.ust,
2684 ua_chan->tracing_channel_id);
2685 if (ret < 0) {
2686 goto error;
d88aee68 2687 }
7972aab2 2688 buffer_reg_channel_add(reg_sess, reg_chan);
d88aee68 2689
7972aab2
DG
2690 if (regp) {
2691 *regp = reg_chan;
3d8ca23b 2692 }
d88aee68 2693
7972aab2 2694 return 0;
3d8ca23b
DG
2695
2696error:
7972aab2
DG
2697 /* Safe because the registry channel object was not added to any HT. */
2698 buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST);
2699error_create:
3d8ca23b 2700 return ret;
421cb601
DG
2701}
2702
55cc08a6 2703/*
7972aab2
DG
2704 * Setup buffer registry channel for the given session registry and application
2705 * channel object. If regp pointer is valid, it's set with the created object.
d0b96690 2706 *
7972aab2 2707 * Return 0 on success else a negative value.
55cc08a6 2708 */
7972aab2 2709static int setup_buffer_reg_channel(struct buffer_reg_session *reg_sess,
fb45065e
MD
2710 struct ust_app_channel *ua_chan, struct buffer_reg_channel *reg_chan,
2711 struct ust_app *app)
55cc08a6 2712{
7972aab2 2713 int ret;
55cc08a6 2714
7972aab2
DG
2715 assert(reg_sess);
2716 assert(reg_chan);
2717 assert(ua_chan);
2718 assert(ua_chan->obj);
55cc08a6 2719
7972aab2 2720 DBG2("UST app setup buffer registry channel for %s", ua_chan->name);
55cc08a6 2721
7972aab2 2722 /* Setup all streams for the registry. */
fb45065e 2723 ret = setup_buffer_reg_streams(reg_chan, ua_chan, app);
7972aab2 2724 if (ret < 0) {
55cc08a6
DG
2725 goto error;
2726 }
2727
7972aab2
DG
2728 reg_chan->obj.ust = ua_chan->obj;
2729 ua_chan->obj = NULL;
55cc08a6 2730
7972aab2 2731 return 0;
55cc08a6
DG
2732
2733error:
7972aab2
DG
2734 buffer_reg_channel_remove(reg_sess, reg_chan);
2735 buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST);
55cc08a6
DG
2736 return ret;
2737}
2738
edb67388 2739/*
7972aab2 2740 * Send buffer registry channel to the application.
d0b96690 2741 *
7972aab2 2742 * Return 0 on success else a negative value.
edb67388 2743 */
7972aab2
DG
2744static int send_channel_uid_to_ust(struct buffer_reg_channel *reg_chan,
2745 struct ust_app *app, struct ust_app_session *ua_sess,
2746 struct ust_app_channel *ua_chan)
edb67388
DG
2747{
2748 int ret;
7972aab2 2749 struct buffer_reg_stream *reg_stream;
edb67388 2750
7972aab2
DG
2751 assert(reg_chan);
2752 assert(app);
2753 assert(ua_sess);
2754 assert(ua_chan);
2755
2756 DBG("UST app sending buffer registry channel to ust sock %d", app->sock);
2757
2758 ret = duplicate_channel_object(reg_chan, ua_chan);
edb67388
DG
2759 if (ret < 0) {
2760 goto error;
2761 }
2762
7972aab2
DG
2763 /* Send channel to the application. */
2764 ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan);
a7169585
MD
2765 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
2766 ret = -ENOTCONN; /* Caused by app exiting. */
2767 goto error;
2768 } else if (ret < 0) {
7972aab2
DG
2769 goto error;
2770 }
2771
2772 health_code_update();
2773
2774 /* Send all streams to application. */
2775 pthread_mutex_lock(&reg_chan->stream_list_lock);
2776 cds_list_for_each_entry(reg_stream, &reg_chan->streams, lnode) {
2777 struct ust_app_stream stream;
2778
2779 ret = duplicate_stream_object(reg_stream, &stream);
2780 if (ret < 0) {
2781 goto error_stream_unlock;
2782 }
2783
2784 ret = ust_consumer_send_stream_to_ust(app, ua_chan, &stream);
2785 if (ret < 0) {
fb45065e 2786 (void) release_ust_app_stream(-1, &stream, app);
a7169585
MD
2787 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
2788 ret = -ENOTCONN; /* Caused by app exiting. */
a7169585 2789 }
7972aab2
DG
2790 goto error_stream_unlock;
2791 }
edb67388 2792
7972aab2
DG
2793 /*
2794 * The return value is not important here. This function will output an
2795 * error if needed.
2796 */
fb45065e 2797 (void) release_ust_app_stream(-1, &stream, app);
7972aab2
DG
2798 }
2799 ua_chan->is_sent = 1;
2800
2801error_stream_unlock:
2802 pthread_mutex_unlock(&reg_chan->stream_list_lock);
edb67388
DG
2803error:
2804 return ret;
2805}
2806
9730260e 2807/*
7972aab2
DG
2808 * Create and send to the application the created buffers with per UID buffers.
2809 *
9acdc1d6 2810 * This MUST be called with a RCU read side lock acquired.
71e0a100 2811 * The session list lock and the session's lock must be acquired.
9acdc1d6 2812 *
7972aab2 2813 * Return 0 on success else a negative value.
9730260e 2814 */
7972aab2
DG
2815static int create_channel_per_uid(struct ust_app *app,
2816 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
2817 struct ust_app_channel *ua_chan)
9730260e
DG
2818{
2819 int ret;
7972aab2
DG
2820 struct buffer_reg_uid *reg_uid;
2821 struct buffer_reg_channel *reg_chan;
e32d7f27 2822 struct ltt_session *session = NULL;
e098433c
JG
2823 enum lttng_error_code notification_ret;
2824 struct ust_registry_channel *chan_reg;
9730260e 2825
7972aab2
DG
2826 assert(app);
2827 assert(usess);
2828 assert(ua_sess);
2829 assert(ua_chan);
2830
2831 DBG("UST app creating channel %s with per UID buffers", ua_chan->name);
2832
2833 reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid);
2834 /*
2835 * The session creation handles the creation of this global registry
2836 * object. If none can be find, there is a code flow problem or a
2837 * teardown race.
2838 */
2839 assert(reg_uid);
2840
2841 reg_chan = buffer_reg_channel_find(ua_chan->tracing_channel_id,
2842 reg_uid);
2721f7ea
JG
2843 if (reg_chan) {
2844 goto send_channel;
2845 }
7972aab2 2846
2721f7ea
JG
2847 /* Create the buffer registry channel object. */
2848 ret = create_buffer_reg_channel(reg_uid->registry, ua_chan, &reg_chan);
2849 if (ret < 0) {
2850 ERR("Error creating the UST channel \"%s\" registry instance",
f14256d6 2851 ua_chan->name);
2721f7ea
JG
2852 goto error;
2853 }
f14256d6 2854
e098433c
JG
2855 session = session_find_by_id(ua_sess->tracing_id);
2856 assert(session);
2857 assert(pthread_mutex_trylock(&session->lock));
2858 assert(session_trylock_list());
2859
2721f7ea
JG
2860 /*
2861 * Create the buffers on the consumer side. This call populates the
2862 * ust app channel object with all streams and data object.
2863 */
2864 ret = do_consumer_create_channel(usess, ua_sess, ua_chan,
e098433c 2865 app->bits_per_long, reg_uid->registry->reg.ust,
d2956687 2866 session->most_recent_chunk_id.value);
2721f7ea
JG
2867 if (ret < 0) {
2868 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2869 ua_chan->name);
7972aab2
DG
2870
2871 /*
2721f7ea
JG
2872 * Let's remove the previously created buffer registry channel so
2873 * it's not visible anymore in the session registry.
7972aab2 2874 */
2721f7ea
JG
2875 ust_registry_channel_del_free(reg_uid->registry->reg.ust,
2876 ua_chan->tracing_channel_id, false);
2877 buffer_reg_channel_remove(reg_uid->registry, reg_chan);
2878 buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST);
2879 goto error;
7972aab2
DG
2880 }
2881
2721f7ea
JG
2882 /*
2883 * Setup the streams and add it to the session registry.
2884 */
2885 ret = setup_buffer_reg_channel(reg_uid->registry,
2886 ua_chan, reg_chan, app);
2887 if (ret < 0) {
2888 ERR("Error setting up UST channel \"%s\"", ua_chan->name);
2889 goto error;
2890 }
2891
e098433c
JG
2892 /* Notify the notification subsystem of the channel's creation. */
2893 pthread_mutex_lock(&reg_uid->registry->reg.ust->lock);
2894 chan_reg = ust_registry_channel_find(reg_uid->registry->reg.ust,
2895 ua_chan->tracing_channel_id);
2896 assert(chan_reg);
2897 chan_reg->consumer_key = ua_chan->key;
2898 chan_reg = NULL;
2899 pthread_mutex_unlock(&reg_uid->registry->reg.ust->lock);
e9404c27 2900
e098433c
JG
2901 notification_ret = notification_thread_command_add_channel(
2902 notification_thread_handle, session->name,
ff588497
JR
2903 lttng_credentials_get_uid(&ua_sess->effective_credentials),
2904 lttng_credentials_get_gid(&ua_sess->effective_credentials),
2905 ua_chan->name,
470cc211 2906 ua_chan->key, LTTNG_DOMAIN_UST,
e098433c
JG
2907 ua_chan->attr.subbuf_size * ua_chan->attr.num_subbuf);
2908 if (notification_ret != LTTNG_OK) {
2909 ret = - (int) notification_ret;
2910 ERR("Failed to add channel to notification thread");
2911 goto error;
e9404c27
JG
2912 }
2913
2721f7ea 2914send_channel:
66ff8e3f
JG
2915 /* Send buffers to the application. */
2916 ret = send_channel_uid_to_ust(reg_chan, app, ua_sess, ua_chan);
2917 if (ret < 0) {
2918 if (ret != -ENOTCONN) {
2919 ERR("Error sending channel to application");
2920 }
2921 goto error;
2922 }
2923
9730260e 2924error:
e32d7f27
JG
2925 if (session) {
2926 session_put(session);
2927 }
9730260e
DG
2928 return ret;
2929}
2930
78f0bacd 2931/*
7972aab2
DG
2932 * Create and send to the application the created buffers with per PID buffers.
2933 *
fad1ed2f 2934 * Called with UST app session lock held.
71e0a100 2935 * The session list lock and the session's lock must be acquired.
fad1ed2f 2936 *
7972aab2 2937 * Return 0 on success else a negative value.
78f0bacd 2938 */
7972aab2
DG
2939static int create_channel_per_pid(struct ust_app *app,
2940 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
2941 struct ust_app_channel *ua_chan)
78f0bacd 2942{
8535a6d9 2943 int ret;
7972aab2 2944 struct ust_registry_session *registry;
e9404c27 2945 enum lttng_error_code cmd_ret;
e32d7f27 2946 struct ltt_session *session = NULL;
e9404c27
JG
2947 uint64_t chan_reg_key;
2948 struct ust_registry_channel *chan_reg;
78f0bacd 2949
7972aab2
DG
2950 assert(app);
2951 assert(usess);
2952 assert(ua_sess);
2953 assert(ua_chan);
2954
2955 DBG("UST app creating channel %s with per PID buffers", ua_chan->name);
2956
2957 rcu_read_lock();
2958
2959 registry = get_session_registry(ua_sess);
fad1ed2f 2960 /* The UST app session lock is held, registry shall not be null. */
7972aab2
DG
2961 assert(registry);
2962
2963 /* Create and add a new channel registry to session. */
2964 ret = ust_registry_channel_add(registry, ua_chan->key);
78f0bacd 2965 if (ret < 0) {
f14256d6
MD
2966 ERR("Error creating the UST channel \"%s\" registry instance",
2967 ua_chan->name);
78f0bacd
DG
2968 goto error;
2969 }
2970
e098433c
JG
2971 session = session_find_by_id(ua_sess->tracing_id);
2972 assert(session);
2973
2974 assert(pthread_mutex_trylock(&session->lock));
2975 assert(session_trylock_list());
2976
7972aab2
DG
2977 /* Create and get channel on the consumer side. */
2978 ret = do_consumer_create_channel(usess, ua_sess, ua_chan,
e098433c 2979 app->bits_per_long, registry,
d2956687 2980 session->most_recent_chunk_id.value);
7972aab2 2981 if (ret < 0) {
f14256d6
MD
2982 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2983 ua_chan->name);
5b951542 2984 goto error_remove_from_registry;
7972aab2
DG
2985 }
2986
2987 ret = send_channel_pid_to_ust(app, ua_sess, ua_chan);
2988 if (ret < 0) {
a7169585
MD
2989 if (ret != -ENOTCONN) {
2990 ERR("Error sending channel to application");
2991 }
5b951542 2992 goto error_remove_from_registry;
7972aab2 2993 }
8535a6d9 2994
e9404c27
JG
2995 chan_reg_key = ua_chan->key;
2996 pthread_mutex_lock(&registry->lock);
2997 chan_reg = ust_registry_channel_find(registry, chan_reg_key);
2998 assert(chan_reg);
2999 chan_reg->consumer_key = ua_chan->key;
3000 pthread_mutex_unlock(&registry->lock);
3001
3002 cmd_ret = notification_thread_command_add_channel(
3003 notification_thread_handle, session->name,
ff588497
JR
3004 lttng_credentials_get_uid(&ua_sess->effective_credentials),
3005 lttng_credentials_get_gid(&ua_sess->effective_credentials),
3006 ua_chan->name,
470cc211 3007 ua_chan->key, LTTNG_DOMAIN_UST,
e9404c27
JG
3008 ua_chan->attr.subbuf_size * ua_chan->attr.num_subbuf);
3009 if (cmd_ret != LTTNG_OK) {
3010 ret = - (int) cmd_ret;
3011 ERR("Failed to add channel to notification thread");
5b951542 3012 goto error_remove_from_registry;
e9404c27
JG
3013 }
3014
5b951542
MD
3015error_remove_from_registry:
3016 if (ret) {
3017 ust_registry_channel_del_free(registry, ua_chan->key, false);
3018 }
78f0bacd 3019error:
7972aab2 3020 rcu_read_unlock();
e32d7f27
JG
3021 if (session) {
3022 session_put(session);
3023 }
78f0bacd
DG
3024 return ret;
3025}
3026
3027/*
7972aab2 3028 * From an already allocated ust app channel, create the channel buffers if
88e3c2f5 3029 * needed and send them to the application. This MUST be called with a RCU read
7972aab2
DG
3030 * side lock acquired.
3031 *
fad1ed2f
JR
3032 * Called with UST app session lock held.
3033 *
a7169585
MD
3034 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3035 * the application exited concurrently.
78f0bacd 3036 */
88e3c2f5 3037static int ust_app_channel_send(struct ust_app *app,
7972aab2
DG
3038 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
3039 struct ust_app_channel *ua_chan)
78f0bacd 3040{
7972aab2 3041 int ret;
78f0bacd 3042
7972aab2
DG
3043 assert(app);
3044 assert(usess);
88e3c2f5 3045 assert(usess->active);
7972aab2
DG
3046 assert(ua_sess);
3047 assert(ua_chan);
3048
3049 /* Handle buffer type before sending the channel to the application. */
3050 switch (usess->buffer_type) {
3051 case LTTNG_BUFFER_PER_UID:
3052 {
3053 ret = create_channel_per_uid(app, usess, ua_sess, ua_chan);
3054 if (ret < 0) {
3055 goto error;
3056 }
3057 break;
3058 }
3059 case LTTNG_BUFFER_PER_PID:
3060 {
3061 ret = create_channel_per_pid(app, usess, ua_sess, ua_chan);
3062 if (ret < 0) {
3063 goto error;
3064 }
3065 break;
3066 }
3067 default:
3068 assert(0);
3069 ret = -EINVAL;
78f0bacd
DG
3070 goto error;
3071 }
3072
7972aab2
DG
3073 /* Initialize ust objd object using the received handle and add it. */
3074 lttng_ht_node_init_ulong(&ua_chan->ust_objd_node, ua_chan->handle);
3075 lttng_ht_add_unique_ulong(app->ust_objd, &ua_chan->ust_objd_node);
78f0bacd 3076
7972aab2
DG
3077 /* If channel is not enabled, disable it on the tracer */
3078 if (!ua_chan->enabled) {
3079 ret = disable_ust_channel(app, ua_sess, ua_chan);
3080 if (ret < 0) {
3081 goto error;
3082 }
78f0bacd
DG
3083 }
3084
3085error:
3086 return ret;
3087}
3088
284d8f55 3089/*
88e3c2f5 3090 * Create UST app channel and return it through ua_chanp if not NULL.
d0b96690 3091 *
36b588ed 3092 * Called with UST app session lock and RCU read-side lock held.
7972aab2 3093 *
88e3c2f5 3094 * Return 0 on success or else a negative value.
284d8f55 3095 */
88e3c2f5
JG
3096static int ust_app_channel_allocate(struct ust_app_session *ua_sess,
3097 struct ltt_ust_channel *uchan,
7972aab2 3098 enum lttng_ust_chan_type type, struct ltt_ust_session *usess,
4d710ac2 3099 struct ust_app_channel **ua_chanp)
5b4a0ec0
DG
3100{
3101 int ret = 0;
bec39940
DG
3102 struct lttng_ht_iter iter;
3103 struct lttng_ht_node_str *ua_chan_node;
5b4a0ec0
DG
3104 struct ust_app_channel *ua_chan;
3105
3106 /* Lookup channel in the ust app session */
bec39940
DG
3107 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
3108 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
fc34caaa 3109 if (ua_chan_node != NULL) {
5b4a0ec0 3110 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
fc34caaa 3111 goto end;
5b4a0ec0
DG
3112 }
3113
d0b96690 3114 ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr);
fc34caaa
DG
3115 if (ua_chan == NULL) {
3116 /* Only malloc can fail here */
4d710ac2 3117 ret = -ENOMEM;
88e3c2f5 3118 goto error;
fc34caaa
DG
3119 }
3120 shadow_copy_channel(ua_chan, uchan);
3121
ffe60014
DG
3122 /* Set channel type. */
3123 ua_chan->attr.type = type;
3124
d0b96690
DG
3125 /* Only add the channel if successful on the tracer side. */
3126 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
fc34caaa 3127end:
4d710ac2
DG
3128 if (ua_chanp) {
3129 *ua_chanp = ua_chan;
3130 }
3131
3132 /* Everything went well. */
3133 return 0;
5b4a0ec0
DG
3134
3135error:
4d710ac2 3136 return ret;
5b4a0ec0
DG
3137}
3138
3139/*
3140 * Create UST app event and create it on the tracer side.
d0b96690
DG
3141 *
3142 * Called with ust app session mutex held.
5b4a0ec0 3143 */
edb67388
DG
3144static
3145int create_ust_app_event(struct ust_app_session *ua_sess,
3146 struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent,
3147 struct ust_app *app)
284d8f55 3148{
edb67388 3149 int ret = 0;
5b4a0ec0 3150 struct ust_app_event *ua_event;
284d8f55 3151
edb67388
DG
3152 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
3153 if (ua_event == NULL) {
20533947 3154 /* Only failure mode of alloc_ust_app_event(). */
edb67388 3155 ret = -ENOMEM;
fc34caaa 3156 goto end;
5b4a0ec0 3157 }
edb67388 3158 shadow_copy_event(ua_event, uevent);
5b4a0ec0 3159
edb67388 3160 /* Create it on the tracer side */
5b4a0ec0 3161 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
284d8f55 3162 if (ret < 0) {
e9f11505
JG
3163 /*
3164 * Not found previously means that it does not exist on the
3165 * tracer. If the application reports that the event existed,
3166 * it means there is a bug in the sessiond or lttng-ust
3167 * (or corruption, etc.)
3168 */
3169 if (ret == -LTTNG_UST_ERR_EXIST) {
3170 ERR("Tracer for application reported that an event being created already existed: "
3171 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3172 uevent->attr.name,
3173 app->pid, app->ppid, app->uid,
3174 app->gid);
3175 }
284d8f55
DG
3176 goto error;
3177 }
3178
d0b96690 3179 add_unique_ust_app_event(ua_chan, ua_event);
284d8f55 3180
fc34caaa 3181 DBG2("UST app create event %s for PID %d completed", ua_event->name,
852d0037 3182 app->pid);
7f79d3a1 3183
edb67388 3184end:
fc34caaa
DG
3185 return ret;
3186
5b4a0ec0 3187error:
fc34caaa 3188 /* Valid. Calling here is already in a read side lock */
fb45065e 3189 delete_ust_app_event(-1, ua_event, app);
edb67388 3190 return ret;
5b4a0ec0
DG
3191}
3192
3193/*
3194 * Create UST metadata and open it on the tracer side.
d0b96690 3195 *
7972aab2 3196 * Called with UST app session lock held and RCU read side lock.
5b4a0ec0
DG
3197 */
3198static int create_ust_app_metadata(struct ust_app_session *ua_sess,
ad7a9107 3199 struct ust_app *app, struct consumer_output *consumer)
5b4a0ec0
DG
3200{
3201 int ret = 0;
ffe60014 3202 struct ust_app_channel *metadata;
d88aee68 3203 struct consumer_socket *socket;
7972aab2 3204 struct ust_registry_session *registry;
e32d7f27 3205 struct ltt_session *session = NULL;
5b4a0ec0 3206
ffe60014
DG
3207 assert(ua_sess);
3208 assert(app);
d88aee68 3209 assert(consumer);
5b4a0ec0 3210
7972aab2 3211 registry = get_session_registry(ua_sess);
fad1ed2f 3212 /* The UST app session is held registry shall not be null. */
7972aab2
DG
3213 assert(registry);
3214
ce34fcd0
MD
3215 pthread_mutex_lock(&registry->lock);
3216
1b532a60
DG
3217 /* Metadata already exists for this registry or it was closed previously */
3218 if (registry->metadata_key || registry->metadata_closed) {
7972aab2
DG
3219 ret = 0;
3220 goto error;
5b4a0ec0
DG
3221 }
3222
ffe60014 3223 /* Allocate UST metadata */
d0b96690 3224 metadata = alloc_ust_app_channel(DEFAULT_METADATA_NAME, ua_sess, NULL);
ffe60014
DG
3225 if (!metadata) {
3226 /* malloc() failed */
3227 ret = -ENOMEM;
3228 goto error;
3229 }
5b4a0ec0 3230
ad7a9107 3231 memcpy(&metadata->attr, &ua_sess->metadata_attr, sizeof(metadata->attr));
5b4a0ec0 3232
7972aab2
DG
3233 /* Need one fd for the channel. */
3234 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
3235 if (ret < 0) {
3236 ERR("Exhausted number of available FD upon create metadata");
3237 goto error;
3238 }
3239
4dc3dfc5
DG
3240 /* Get the right consumer socket for the application. */
3241 socket = consumer_find_socket_by_bitness(app->bits_per_long, consumer);
3242 if (!socket) {
3243 ret = -EINVAL;
3244 goto error_consumer;
3245 }
3246
331744e3
JD
3247 /*
3248 * Keep metadata key so we can identify it on the consumer side. Assign it
3249 * to the registry *before* we ask the consumer so we avoid the race of the
3250 * consumer requesting the metadata and the ask_channel call on our side
3251 * did not returned yet.
3252 */
3253 registry->metadata_key = metadata->key;
3254
e098433c
JG
3255 session = session_find_by_id(ua_sess->tracing_id);
3256 assert(session);
3257
3258 assert(pthread_mutex_trylock(&session->lock));
3259 assert(session_trylock_list());
3260
d88aee68
DG
3261 /*
3262 * Ask the metadata channel creation to the consumer. The metadata object
3263 * will be created by the consumer and kept their. However, the stream is
3264 * never added or monitored until we do a first push metadata to the
3265 * consumer.
3266 */
7972aab2 3267 ret = ust_consumer_ask_channel(ua_sess, metadata, consumer, socket,
d2956687 3268 registry, session->current_trace_chunk);
d88aee68 3269 if (ret < 0) {
f2a444f1
DG
3270 /* Nullify the metadata key so we don't try to close it later on. */
3271 registry->metadata_key = 0;
d88aee68
DG
3272 goto error_consumer;
3273 }
3274
3275 /*
3276 * The setup command will make the metadata stream be sent to the relayd,
3277 * if applicable, and the thread managing the metadatas. This is important
3278 * because after this point, if an error occurs, the only way the stream
3279 * can be deleted is to be monitored in the consumer.
3280 */
7972aab2 3281 ret = consumer_setup_metadata(socket, metadata->key);
ffe60014 3282 if (ret < 0) {
f2a444f1
DG
3283 /* Nullify the metadata key so we don't try to close it later on. */
3284 registry->metadata_key = 0;
d88aee68 3285 goto error_consumer;
5b4a0ec0
DG
3286 }
3287
7972aab2
DG
3288 DBG2("UST metadata with key %" PRIu64 " created for app pid %d",
3289 metadata->key, app->pid);
5b4a0ec0 3290
d88aee68 3291error_consumer:
b80f0b6c 3292 lttng_fd_put(LTTNG_FD_APPS, 1);
d88aee68 3293 delete_ust_app_channel(-1, metadata, app);
5b4a0ec0 3294error:
ce34fcd0 3295 pthread_mutex_unlock(&registry->lock);
e32d7f27
JG
3296 if (session) {
3297 session_put(session);
3298 }
ffe60014 3299 return ret;
5b4a0ec0
DG
3300}
3301
5b4a0ec0 3302/*
d88aee68
DG
3303 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3304 * acquired before calling this function.
5b4a0ec0
DG
3305 */
3306struct ust_app *ust_app_find_by_pid(pid_t pid)
3307{
d88aee68 3308 struct ust_app *app = NULL;
bec39940
DG
3309 struct lttng_ht_node_ulong *node;
3310 struct lttng_ht_iter iter;
5b4a0ec0 3311
bec39940
DG
3312 lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) pid), &iter);
3313 node = lttng_ht_iter_get_node_ulong(&iter);
5b4a0ec0
DG
3314 if (node == NULL) {
3315 DBG2("UST app no found with pid %d", pid);
3316 goto error;
3317 }
5b4a0ec0
DG
3318
3319 DBG2("Found UST app by pid %d", pid);
3320
d88aee68 3321 app = caa_container_of(node, struct ust_app, pid_n);
5b4a0ec0
DG
3322
3323error:
d88aee68 3324 return app;
5b4a0ec0
DG
3325}
3326
d88aee68
DG
3327/*
3328 * Allocate and init an UST app object using the registration information and
3329 * the command socket. This is called when the command socket connects to the
3330 * session daemon.
3331 *
3332 * The object is returned on success or else NULL.
3333 */
d0b96690 3334struct ust_app *ust_app_create(struct ust_register_msg *msg, int sock)
5b4a0ec0 3335{
d0b96690 3336 struct ust_app *lta = NULL;
da873412 3337 struct lttng_pipe *event_notifier_event_source_pipe = NULL;
d0b96690
DG
3338
3339 assert(msg);
3340 assert(sock >= 0);
3341
3342 DBG3("UST app creating application for socket %d", sock);
5b4a0ec0 3343
173af62f
DG
3344 if ((msg->bits_per_long == 64 &&
3345 (uatomic_read(&ust_consumerd64_fd) == -EINVAL))
3346 || (msg->bits_per_long == 32 &&
3347 (uatomic_read(&ust_consumerd32_fd) == -EINVAL))) {
f943b0fb 3348 ERR("Registration failed: application \"%s\" (pid: %d) has "
d0b96690
DG
3349 "%d-bit long, but no consumerd for this size is available.\n",
3350 msg->name, msg->pid, msg->bits_per_long);
3351 goto error;
3f2c5fcc 3352 }
d0b96690 3353
da873412
JR
3354 event_notifier_event_source_pipe = lttng_pipe_open(FD_CLOEXEC);
3355 if (!event_notifier_event_source_pipe) {
3356 PERROR("Failed to open application event source pipe: '%s' (ppid = %d)",
3357 msg->name, msg->ppid);
3358 goto error;
3359 }
3360
5b4a0ec0
DG
3361 lta = zmalloc(sizeof(struct ust_app));
3362 if (lta == NULL) {
3363 PERROR("malloc");
da873412 3364 goto error_free_pipe;
5b4a0ec0
DG
3365 }
3366
da873412
JR
3367 lta->event_notifier_group.event_pipe = event_notifier_event_source_pipe;
3368
5b4a0ec0
DG
3369 lta->ppid = msg->ppid;
3370 lta->uid = msg->uid;
3371 lta->gid = msg->gid;
d0b96690 3372
7753dea8 3373 lta->bits_per_long = msg->bits_per_long;
d0b96690
DG
3374 lta->uint8_t_alignment = msg->uint8_t_alignment;
3375 lta->uint16_t_alignment = msg->uint16_t_alignment;
3376 lta->uint32_t_alignment = msg->uint32_t_alignment;
3377 lta->uint64_t_alignment = msg->uint64_t_alignment;
3378 lta->long_alignment = msg->long_alignment;
3379 lta->byte_order = msg->byte_order;
3380
5b4a0ec0
DG
3381 lta->v_major = msg->major;
3382 lta->v_minor = msg->minor;
d9bf3ca4 3383 lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
d0b96690 3384 lta->ust_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
10b56aef 3385 lta->ust_sessions_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
d0b96690 3386 lta->notify_sock = -1;
d88aee68
DG
3387
3388 /* Copy name and make sure it's NULL terminated. */
3389 strncpy(lta->name, msg->name, sizeof(lta->name));
3390 lta->name[UST_APP_PROCNAME_LEN] = '\0';
3391
3392 /*
3393 * Before this can be called, when receiving the registration information,
3394 * the application compatibility is checked. So, at this point, the
3395 * application can work with this session daemon.
3396 */
d0b96690 3397 lta->compatible = 1;
5b4a0ec0 3398
852d0037 3399 lta->pid = msg->pid;
d0b96690 3400 lttng_ht_node_init_ulong(&lta->pid_n, (unsigned long) lta->pid);
852d0037 3401 lta->sock = sock;
fb45065e 3402 pthread_mutex_init(&lta->sock_lock, NULL);
d0b96690 3403 lttng_ht_node_init_ulong(&lta->sock_n, (unsigned long) lta->sock);
5b4a0ec0 3404
d42f20df 3405 CDS_INIT_LIST_HEAD(&lta->teardown_head);
d0b96690 3406 return lta;
da873412
JR
3407
3408error_free_pipe:
3409 lttng_pipe_destroy(event_notifier_event_source_pipe);
3410error:
3411 return NULL;
d0b96690
DG
3412}
3413
d88aee68
DG
3414/*
3415 * For a given application object, add it to every hash table.
3416 */
d0b96690
DG
3417void ust_app_add(struct ust_app *app)
3418{
3419 assert(app);
3420 assert(app->notify_sock >= 0);
3421
940c4592
JR
3422 app->registration_time = time(NULL);
3423
5b4a0ec0 3424 rcu_read_lock();
852d0037
DG
3425
3426 /*
3427 * On a re-registration, we want to kick out the previous registration of
3428 * that pid
3429 */
d0b96690 3430 lttng_ht_add_replace_ulong(ust_app_ht, &app->pid_n);
852d0037
DG
3431
3432 /*
3433 * The socket _should_ be unique until _we_ call close. So, a add_unique
3434 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3435 * already in the table.
3436 */
d0b96690 3437 lttng_ht_add_unique_ulong(ust_app_ht_by_sock, &app->sock_n);
852d0037 3438
d0b96690
DG
3439 /* Add application to the notify socket hash table. */
3440 lttng_ht_node_init_ulong(&app->notify_sock_n, app->notify_sock);
3441 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock, &app->notify_sock_n);
5b4a0ec0 3442
d0b96690 3443 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
d88aee68
DG
3444 "notify_sock:%d (version %d.%d)", app->pid, app->ppid, app->uid,
3445 app->gid, app->sock, app->name, app->notify_sock, app->v_major,
3446 app->v_minor);
5b4a0ec0 3447
d0b96690
DG
3448 rcu_read_unlock();
3449}
3450
d88aee68
DG
3451/*
3452 * Set the application version into the object.
3453 *
3454 * Return 0 on success else a negative value either an errno code or a
3455 * LTTng-UST error code.
3456 */
d0b96690
DG
3457int ust_app_version(struct ust_app *app)
3458{
d88aee68
DG
3459 int ret;
3460
d0b96690 3461 assert(app);
d88aee68 3462
fb45065e 3463 pthread_mutex_lock(&app->sock_lock);
d88aee68 3464 ret = ustctl_tracer_version(app->sock, &app->version);
fb45065e 3465 pthread_mutex_unlock(&app->sock_lock);
d88aee68
DG
3466 if (ret < 0) {
3467 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
5368d366 3468 ERR("UST app %d version failed with ret %d", app->sock, ret);
d88aee68 3469 } else {
5368d366 3470 DBG3("UST app %d version failed. Application is dead", app->sock);
d88aee68
DG
3471 }
3472 }
3473
3474 return ret;
5b4a0ec0
DG
3475}
3476
da873412
JR
3477/*
3478 * Setup the base event notifier group.
3479 *
3480 * Return 0 on success else a negative value either an errno code or a
3481 * LTTng-UST error code.
3482 */
3483int ust_app_setup_event_notifier_group(struct ust_app *app)
3484{
3485 int ret;
3486 int event_pipe_write_fd;
3487 struct lttng_ust_object_data *event_notifier_group = NULL;
3488 enum lttng_error_code lttng_ret;
3489
3490 assert(app);
3491
3492 /* Get the write side of the pipe. */
3493 event_pipe_write_fd = lttng_pipe_get_writefd(
3494 app->event_notifier_group.event_pipe);
3495
3496 pthread_mutex_lock(&app->sock_lock);
3497 ret = ustctl_create_event_notifier_group(app->sock,
3498 event_pipe_write_fd, &event_notifier_group);
3499 pthread_mutex_unlock(&app->sock_lock);
3500 if (ret < 0) {
3501 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
3502 ERR("Failed to create application event notifier group: ret = %d, app socket fd = %d, event_pipe_write_fd = %d",
3503 ret, app->sock, event_pipe_write_fd);
3504 } else {
3505 DBG("Failed to create application event notifier group (application is dead): app socket fd = %d",
3506 app->sock);
3507 }
3508
3509 goto error;
3510 }
3511
3512 lttng_ret = notification_thread_command_add_tracer_event_source(
3513 notification_thread_handle,
3514 lttng_pipe_get_readfd(app->event_notifier_group.event_pipe),
3515 LTTNG_DOMAIN_UST);
3516 if (lttng_ret != LTTNG_OK) {
3517 ERR("Failed to add tracer event source to notification thread");
3518 ret = - 1;
3519 goto error;
3520 }
3521
3522 /* Assign handle only when the complete setup is valid. */
3523 app->event_notifier_group.object = event_notifier_group;
3524 return ret;
3525
3526error:
3527 ustctl_release_object(app->sock, app->event_notifier_group.object);
3528 free(app->event_notifier_group.object);
3529 return ret;
3530}
3531
5b4a0ec0
DG
3532/*
3533 * Unregister app by removing it from the global traceable app list and freeing
3534 * the data struct.
3535 *
3536 * The socket is already closed at this point so no close to sock.
3537 */
3538void ust_app_unregister(int sock)
3539{
3540 struct ust_app *lta;
bec39940 3541 struct lttng_ht_node_ulong *node;
c4b88406 3542 struct lttng_ht_iter ust_app_sock_iter;
bec39940 3543 struct lttng_ht_iter iter;
d42f20df 3544 struct ust_app_session *ua_sess;
525b0740 3545 int ret;
5b4a0ec0
DG
3546
3547 rcu_read_lock();
886459c6 3548
5b4a0ec0 3549 /* Get the node reference for a call_rcu */
c4b88406
MD
3550 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &ust_app_sock_iter);
3551 node = lttng_ht_iter_get_node_ulong(&ust_app_sock_iter);
d0b96690 3552 assert(node);
284d8f55 3553
852d0037 3554 lta = caa_container_of(node, struct ust_app, sock_n);
852d0037
DG
3555 DBG("PID %d unregistering with sock %d", lta->pid, sock);
3556
d88aee68 3557 /*
ce34fcd0
MD
3558 * For per-PID buffers, perform "push metadata" and flush all
3559 * application streams before removing app from hash tables,
3560 * ensuring proper behavior of data_pending check.
c4b88406 3561 * Remove sessions so they are not visible during deletion.
d88aee68 3562 */
d42f20df
DG
3563 cds_lfht_for_each_entry(lta->sessions->ht, &iter.iter, ua_sess,
3564 node.node) {
7972aab2
DG
3565 struct ust_registry_session *registry;
3566
d42f20df
DG
3567 ret = lttng_ht_del(lta->sessions, &iter);
3568 if (ret) {
3569 /* The session was already removed so scheduled for teardown. */
3570 continue;
3571 }
3572
ce34fcd0
MD
3573 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) {
3574 (void) ust_app_flush_app_session(lta, ua_sess);
3575 }
c4b88406 3576
d42f20df
DG
3577 /*
3578 * Add session to list for teardown. This is safe since at this point we
3579 * are the only one using this list.
3580 */
d88aee68
DG
3581 pthread_mutex_lock(&ua_sess->lock);
3582
b161602a
MD
3583 if (ua_sess->deleted) {
3584 pthread_mutex_unlock(&ua_sess->lock);
3585 continue;
3586 }
3587
d88aee68
DG
3588 /*
3589 * Normally, this is done in the delete session process which is
3590 * executed in the call rcu below. However, upon registration we can't
3591 * afford to wait for the grace period before pushing data or else the
3592 * data pending feature can race between the unregistration and stop
3593 * command where the data pending command is sent *before* the grace
3594 * period ended.
3595 *
3596 * The close metadata below nullifies the metadata pointer in the
3597 * session so the delete session will NOT push/close a second time.
3598 */
7972aab2 3599 registry = get_session_registry(ua_sess);
ce34fcd0 3600 if (registry) {
7972aab2
DG
3601 /* Push metadata for application before freeing the application. */
3602 (void) push_metadata(registry, ua_sess->consumer);
3603
3604 /*
3605 * Don't ask to close metadata for global per UID buffers. Close
1b532a60
DG
3606 * metadata only on destroy trace session in this case. Also, the
3607 * previous push metadata could have flag the metadata registry to
3608 * close so don't send a close command if closed.
7972aab2 3609 */
ce34fcd0 3610 if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) {
7972aab2
DG
3611 /* And ask to close it for this session registry. */
3612 (void) close_metadata(registry, ua_sess->consumer);
3613 }
3614 }
d42f20df 3615 cds_list_add(&ua_sess->teardown_node, &lta->teardown_head);
c4b88406 3616
d88aee68 3617 pthread_mutex_unlock(&ua_sess->lock);
d42f20df
DG
3618 }
3619
c4b88406
MD
3620 /* Remove application from PID hash table */
3621 ret = lttng_ht_del(ust_app_ht_by_sock, &ust_app_sock_iter);
3622 assert(!ret);
3623
3624 /*
3625 * Remove application from notify hash table. The thread handling the
3626 * notify socket could have deleted the node so ignore on error because
c48239ca
JG
3627 * either way it's valid. The close of that socket is handled by the
3628 * apps_notify_thread.
c4b88406
MD
3629 */
3630 iter.iter.node = &lta->notify_sock_n.node;
3631 (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter);
3632
3633 /*
3634 * Ignore return value since the node might have been removed before by an
3635 * add replace during app registration because the PID can be reassigned by
3636 * the OS.
3637 */
3638 iter.iter.node = &lta->pid_n.node;
3639 ret = lttng_ht_del(ust_app_ht, &iter);
3640 if (ret) {
3641 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3642 lta->pid);
3643 }
3644
852d0037
DG
3645 /* Free memory */
3646 call_rcu(&lta->pid_n.head, delete_ust_app_rcu);
3647
5b4a0ec0
DG
3648 rcu_read_unlock();
3649 return;
284d8f55
DG
3650}
3651
5b4a0ec0
DG
3652/*
3653 * Fill events array with all events name of all registered apps.
3654 */
3655int ust_app_list_events(struct lttng_event **events)
421cb601 3656{
5b4a0ec0
DG
3657 int ret, handle;
3658 size_t nbmem, count = 0;
bec39940 3659 struct lttng_ht_iter iter;
5b4a0ec0 3660 struct ust_app *app;
c617c0c6 3661 struct lttng_event *tmp_event;
421cb601 3662
5b4a0ec0 3663 nbmem = UST_APP_EVENT_LIST_SIZE;
c617c0c6
MD
3664 tmp_event = zmalloc(nbmem * sizeof(struct lttng_event));
3665 if (tmp_event == NULL) {
5b4a0ec0
DG
3666 PERROR("zmalloc ust app events");
3667 ret = -ENOMEM;
421cb601
DG
3668 goto error;
3669 }
3670
5b4a0ec0 3671 rcu_read_lock();
421cb601 3672
852d0037 3673 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
90eaa0d2 3674 struct lttng_ust_tracepoint_iter uiter;
ac3bd9c0 3675
840cb59c 3676 health_code_update();
86acf0da 3677
e0c7ec2b
DG
3678 if (!app->compatible) {
3679 /*
3680 * TODO: In time, we should notice the caller of this error by
3681 * telling him that this is a version error.
3682 */
3683 continue;
3684 }
fb45065e 3685 pthread_mutex_lock(&app->sock_lock);
852d0037 3686 handle = ustctl_tracepoint_list(app->sock);
5b4a0ec0 3687 if (handle < 0) {
ffe60014
DG
3688 if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) {
3689 ERR("UST app list events getting handle failed for app pid %d",
3690 app->pid);
3691 }
fb45065e 3692 pthread_mutex_unlock(&app->sock_lock);
5b4a0ec0
DG
3693 continue;
3694 }
421cb601 3695
852d0037 3696 while ((ret = ustctl_tracepoint_list_get(app->sock, handle,
fb54cdbf 3697 &uiter)) != -LTTNG_UST_ERR_NOENT) {
ffe60014
DG
3698 /* Handle ustctl error. */
3699 if (ret < 0) {
fb45065e
MD
3700 int release_ret;
3701
a2ba1ab0 3702 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
ffe60014
DG
3703 ERR("UST app tp list get failed for app %d with ret %d",
3704 app->sock, ret);
3705 } else {
3706 DBG3("UST app tp list get failed. Application is dead");
3757b385
DG
3707 /*
3708 * This is normal behavior, an application can die during the
3709 * creation process. Don't report an error so the execution can
3710 * continue normally. Continue normal execution.
3711 */
3712 break;
ffe60014 3713 }
98f595d4 3714 free(tmp_event);
fb45065e 3715 release_ret = ustctl_release_handle(app->sock, handle);
68313703
JG
3716 if (release_ret < 0 &&
3717 release_ret != -LTTNG_UST_ERR_EXITING &&
3718 release_ret != -EPIPE) {
fb45065e
MD
3719 ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret);
3720 }
3721 pthread_mutex_unlock(&app->sock_lock);
ffe60014
DG
3722 goto rcu_error;
3723 }
3724
840cb59c 3725 health_code_update();
815564d8 3726 if (count >= nbmem) {
d7b3776f 3727 /* In case the realloc fails, we free the memory */
53efb85a
MD
3728 struct lttng_event *new_tmp_event;
3729 size_t new_nbmem;
3730
3731 new_nbmem = nbmem << 1;
3732 DBG2("Reallocating event list from %zu to %zu entries",
3733 nbmem, new_nbmem);
3734 new_tmp_event = realloc(tmp_event,
3735 new_nbmem * sizeof(struct lttng_event));
3736 if (new_tmp_event == NULL) {
fb45065e
MD
3737 int release_ret;
3738
5b4a0ec0 3739 PERROR("realloc ust app events");
c617c0c6 3740 free(tmp_event);
5b4a0ec0 3741 ret = -ENOMEM;
fb45065e 3742 release_ret = ustctl_release_handle(app->sock, handle);
68313703
JG
3743 if (release_ret < 0 &&
3744 release_ret != -LTTNG_UST_ERR_EXITING &&
3745 release_ret != -EPIPE) {
fb45065e
MD
3746 ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret);
3747 }
3748 pthread_mutex_unlock(&app->sock_lock);
5b4a0ec0
DG
3749 goto rcu_error;
3750 }
53efb85a
MD
3751 /* Zero the new memory */
3752 memset(new_tmp_event + nbmem, 0,
3753 (new_nbmem - nbmem) * sizeof(struct lttng_event));
3754 nbmem = new_nbmem;
3755 tmp_event = new_tmp_event;
5b4a0ec0 3756 }
c617c0c6
MD
3757 memcpy(tmp_event[count].name, uiter.name, LTTNG_UST_SYM_NAME_LEN);
3758 tmp_event[count].loglevel = uiter.loglevel;
3759 tmp_event[count].type = (enum lttng_event_type) LTTNG_UST_TRACEPOINT;
3760 tmp_event[count].pid = app->pid;
3761 tmp_event[count].enabled = -1;
5b4a0ec0 3762 count++;
421cb601 3763 }
fb45065e
MD
3764 ret = ustctl_release_handle(app->sock, handle);
3765 pthread_mutex_unlock(&app->sock_lock);
68313703 3766 if (ret < 0 && ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
fb45065e
MD
3767 ERR("Error releasing app handle for app %d with ret %d", app->sock, ret);
3768 }
421cb601
DG
3769 }
3770
5b4a0ec0 3771 ret = count;
c617c0c6 3772 *events = tmp_event;
421cb601 3773
5b4a0ec0 3774 DBG2("UST app list events done (%zu events)", count);
421cb601 3775
5b4a0ec0
DG
3776rcu_error:
3777 rcu_read_unlock();
421cb601 3778error:
840cb59c 3779 health_code_update();
5b4a0ec0 3780 return ret;
421cb601
DG
3781}
3782
f37d259d
MD
3783/*
3784 * Fill events array with all events name of all registered apps.
3785 */
3786int ust_app_list_event_fields(struct lttng_event_field **fields)
3787{
3788 int ret, handle;
3789 size_t nbmem, count = 0;
3790 struct lttng_ht_iter iter;
3791 struct ust_app *app;
c617c0c6 3792 struct lttng_event_field *tmp_event;
f37d259d
MD
3793
3794 nbmem = UST_APP_EVENT_LIST_SIZE;
c617c0c6
MD
3795 tmp_event = zmalloc(nbmem * sizeof(struct lttng_event_field));
3796 if (tmp_event == NULL) {
f37d259d
MD
3797 PERROR("zmalloc ust app event fields");
3798 ret = -ENOMEM;
3799 goto error;
3800 }
3801
3802 rcu_read_lock();
3803
3804 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3805 struct lttng_ust_field_iter uiter;
3806
840cb59c 3807 health_code_update();
86acf0da 3808
f37d259d
MD
3809 if (!app->compatible) {
3810 /*
3811 * TODO: In time, we should notice the caller of this error by
3812 * telling him that this is a version error.
3813 */
3814 continue;
3815 }
fb45065e 3816 pthread_mutex_lock(&app->sock_lock);
f37d259d
MD
3817 handle = ustctl_tracepoint_field_list(app->sock);
3818 if (handle < 0) {
ffe60014
DG
3819 if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) {
3820 ERR("UST app list field getting handle failed for app pid %d",
3821 app->pid);
3822 }
fb45065e 3823 pthread_mutex_unlock(&app->sock_lock);
f37d259d
MD
3824 continue;
3825 }
3826
3827 while ((ret = ustctl_tracepoint_field_list_get(app->sock, handle,
fb54cdbf 3828 &uiter)) != -LTTNG_UST_ERR_NOENT) {
ffe60014
DG
3829 /* Handle ustctl error. */
3830 if (ret < 0) {
fb45065e
MD
3831 int release_ret;
3832
a2ba1ab0 3833 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
ffe60014
DG
3834 ERR("UST app tp list field failed for app %d with ret %d",
3835 app->sock, ret);
3836 } else {
3837 DBG3("UST app tp list field failed. Application is dead");
3757b385
DG
3838 /*
3839 * This is normal behavior, an application can die during the
3840 * creation process. Don't report an error so the execution can
98f595d4 3841 * continue normally. Reset list and count for next app.
3757b385
DG
3842 */
3843 break;
ffe60014 3844 }
98f595d4 3845 free(tmp_event);
fb45065e
MD
3846 release_ret = ustctl_release_handle(app->sock, handle);
3847 pthread_mutex_unlock(&app->sock_lock);
68313703
JG
3848 if (release_ret < 0 &&
3849 release_ret != -LTTNG_UST_ERR_EXITING &&
3850 release_ret != -EPIPE) {
fb45065e
MD
3851 ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret);
3852 }
ffe60014
DG
3853 goto rcu_error;
3854 }
3855
840cb59c 3856 health_code_update();
f37d259d 3857 if (count >= nbmem) {
d7b3776f 3858 /* In case the realloc fails, we free the memory */
53efb85a
MD
3859 struct lttng_event_field *new_tmp_event;
3860 size_t new_nbmem;
3861
3862 new_nbmem = nbmem << 1;
3863 DBG2("Reallocating event field list from %zu to %zu entries",
3864 nbmem, new_nbmem);
3865 new_tmp_event = realloc(tmp_event,
3866 new_nbmem * sizeof(struct lttng_event_field));
3867 if (new_tmp_event == NULL) {
fb45065e
MD
3868 int release_ret;
3869
f37d259d 3870 PERROR("realloc ust app event fields");
c617c0c6 3871 free(tmp_event);
f37d259d 3872 ret = -ENOMEM;
fb45065e
MD
3873 release_ret = ustctl_release_handle(app->sock, handle);
3874 pthread_mutex_unlock(&app->sock_lock);
68313703
JG
3875 if (release_ret &&
3876 release_ret != -LTTNG_UST_ERR_EXITING &&
3877 release_ret != -EPIPE) {
fb45065e
MD
3878 ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret);
3879 }
f37d259d
MD
3880 goto rcu_error;
3881 }
53efb85a
MD
3882 /* Zero the new memory */
3883 memset(new_tmp_event + nbmem, 0,
3884 (new_nbmem - nbmem) * sizeof(struct lttng_event_field));
3885 nbmem = new_nbmem;
3886 tmp_event = new_tmp_event;
f37d259d 3887 }
f37d259d 3888
c617c0c6 3889 memcpy(tmp_event[count].field_name, uiter.field_name, LTTNG_UST_SYM_NAME_LEN);
2e84128e
DG
3890 /* Mapping between these enums matches 1 to 1. */
3891 tmp_event[count].type = (enum lttng_event_field_type) uiter.type;
c617c0c6 3892 tmp_event[count].nowrite = uiter.nowrite;
f37d259d 3893
c617c0c6
MD
3894 memcpy(tmp_event[count].event.name, uiter.event_name, LTTNG_UST_SYM_NAME_LEN);
3895 tmp_event[count].event.loglevel = uiter.loglevel;
2e84128e 3896 tmp_event[count].event.type = LTTNG_EVENT_TRACEPOINT;
c617c0c6
MD
3897 tmp_event[count].event.pid = app->pid;
3898 tmp_event[count].event.enabled = -1;
f37d259d
MD
3899 count++;
3900 }
fb45065e
MD
3901 ret = ustctl_release_handle(app->sock, handle);
3902 pthread_mutex_unlock(&app->sock_lock);
68313703
JG
3903 if (ret < 0 &&
3904 ret != -LTTNG_UST_ERR_EXITING &&
3905 ret != -EPIPE) {
fb45065e
MD
3906 ERR("Error releasing app handle for app %d with ret %d", app->sock, ret);
3907 }
f37d259d
MD
3908 }
3909
3910 ret = count;
c617c0c6 3911 *fields = tmp_event;
f37d259d
MD
3912
3913 DBG2("UST app list event fields done (%zu events)", count);
3914
3915rcu_error:
3916 rcu_read_unlock();
3917error:
840cb59c 3918 health_code_update();
f37d259d
MD
3919 return ret;
3920}
3921
5b4a0ec0
DG
3922/*
3923 * Free and clean all traceable apps of the global list.
36b588ed
MD
3924 *
3925 * Should _NOT_ be called with RCU read-side lock held.
5b4a0ec0
DG
3926 */
3927void ust_app_clean_list(void)
421cb601 3928{
5b4a0ec0 3929 int ret;
659ed79f 3930 struct ust_app *app;
bec39940 3931 struct lttng_ht_iter iter;
421cb601 3932
5b4a0ec0 3933 DBG2("UST app cleaning registered apps hash table");
421cb601 3934
5b4a0ec0 3935 rcu_read_lock();
421cb601 3936
faadaa3a
JG
3937 /* Cleanup notify socket hash table */
3938 if (ust_app_ht_by_notify_sock) {
3939 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock->ht, &iter.iter, app,
3940 notify_sock_n.node) {
3941 struct cds_lfht_node *node;
3942 struct ust_app *app;
3943
3944 node = cds_lfht_iter_get_node(&iter.iter);
3945 if (!node) {
3946 continue;
3947 }
3948
3949 app = container_of(node, struct ust_app,
3950 notify_sock_n.node);
3951 ust_app_notify_sock_unregister(app->notify_sock);
3952 }
3953 }
3954
f1b711c4
MD
3955 if (ust_app_ht) {
3956 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3957 ret = lttng_ht_del(ust_app_ht, &iter);
3958 assert(!ret);
3959 call_rcu(&app->pid_n.head, delete_ust_app_rcu);
3960 }
421cb601
DG
3961 }
3962
852d0037 3963 /* Cleanup socket hash table */
f1b711c4
MD
3964 if (ust_app_ht_by_sock) {
3965 cds_lfht_for_each_entry(ust_app_ht_by_sock->ht, &iter.iter, app,
3966 sock_n.node) {
3967 ret = lttng_ht_del(ust_app_ht_by_sock, &iter);
3968 assert(!ret);
3969 }
bec39940 3970 }
852d0037 3971
36b588ed 3972 rcu_read_unlock();
d88aee68 3973
bec39940 3974 /* Destroy is done only when the ht is empty */
f1b711c4
MD
3975 if (ust_app_ht) {
3976 ht_cleanup_push(ust_app_ht);
3977 }
3978 if (ust_app_ht_by_sock) {
3979 ht_cleanup_push(ust_app_ht_by_sock);
3980 }
3981 if (ust_app_ht_by_notify_sock) {
3982 ht_cleanup_push(ust_app_ht_by_notify_sock);
3983 }
5b4a0ec0
DG
3984}
3985
3986/*
3987 * Init UST app hash table.
3988 */
57703f6e 3989int ust_app_ht_alloc(void)
5b4a0ec0 3990{
bec39940 3991 ust_app_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
57703f6e
MD
3992 if (!ust_app_ht) {
3993 return -1;
3994 }
852d0037 3995 ust_app_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
57703f6e
MD
3996 if (!ust_app_ht_by_sock) {
3997 return -1;
3998 }
d0b96690 3999 ust_app_ht_by_notify_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
57703f6e
MD
4000 if (!ust_app_ht_by_notify_sock) {
4001 return -1;
4002 }
4003 return 0;
421cb601
DG
4004}
4005
78f0bacd
DG
4006/*
4007 * For a specific UST session, disable the channel for all registered apps.
4008 */
35a9059d 4009int ust_app_disable_channel_glb(struct ltt_ust_session *usess,
78f0bacd
DG
4010 struct ltt_ust_channel *uchan)
4011{
4012 int ret = 0;
bec39940
DG
4013 struct lttng_ht_iter iter;
4014 struct lttng_ht_node_str *ua_chan_node;
78f0bacd
DG
4015 struct ust_app *app;
4016 struct ust_app_session *ua_sess;
8535a6d9 4017 struct ust_app_channel *ua_chan;
78f0bacd 4018
88e3c2f5 4019 assert(usess->active);
d9bf3ca4 4020 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64,
a991f516 4021 uchan->name, usess->id);
78f0bacd
DG
4022
4023 rcu_read_lock();
4024
4025 /* For every registered applications */
852d0037 4026 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
bec39940 4027 struct lttng_ht_iter uiter;
e0c7ec2b
DG
4028 if (!app->compatible) {
4029 /*
4030 * TODO: In time, we should notice the caller of this error by
4031 * telling him that this is a version error.
4032 */
4033 continue;
4034 }
78f0bacd
DG
4035 ua_sess = lookup_session_by_app(usess, app);
4036 if (ua_sess == NULL) {
4037 continue;
4038 }
4039
8535a6d9 4040 /* Get channel */
bec39940
DG
4041 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
4042 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
8535a6d9
DG
4043 /* If the session if found for the app, the channel must be there */
4044 assert(ua_chan_node);
4045
4046 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
4047 /* The channel must not be already disabled */
4048 assert(ua_chan->enabled == 1);
4049
4050 /* Disable channel onto application */
4051 ret = disable_ust_app_channel(ua_sess, ua_chan, app);
78f0bacd
DG
4052 if (ret < 0) {
4053 /* XXX: We might want to report this error at some point... */
4054 continue;
4055 }
4056 }
4057
4058 rcu_read_unlock();
78f0bacd
DG
4059 return ret;
4060}
4061
4062/*
4063 * For a specific UST session, enable the channel for all registered apps.
4064 */
35a9059d 4065int ust_app_enable_channel_glb(struct ltt_ust_session *usess,
78f0bacd
DG
4066 struct ltt_ust_channel *uchan)
4067{
4068 int ret = 0;
bec39940 4069 struct lttng_ht_iter iter;
78f0bacd
DG
4070 struct ust_app *app;
4071 struct ust_app_session *ua_sess;
4072
88e3c2f5 4073 assert(usess->active);
d9bf3ca4 4074 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64,
a991f516 4075 uchan->name, usess->id);
78f0bacd
DG
4076
4077 rcu_read_lock();
4078
4079 /* For every registered applications */
852d0037 4080 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
4081 if (!app->compatible) {
4082 /*
4083 * TODO: In time, we should notice the caller of this error by
4084 * telling him that this is a version error.
4085 */
4086 continue;
4087 }
78f0bacd
DG
4088 ua_sess = lookup_session_by_app(usess, app);
4089 if (ua_sess == NULL) {
4090 continue;
4091 }
4092
4093 /* Enable channel onto application */
4094 ret = enable_ust_app_channel(ua_sess, uchan, app);
4095 if (ret < 0) {
4096 /* XXX: We might want to report this error at some point... */
4097 continue;
4098 }
4099 }
4100
4101 rcu_read_unlock();
78f0bacd
DG
4102 return ret;
4103}
4104
b0a40d28
DG
4105/*
4106 * Disable an event in a channel and for a specific session.
4107 */
35a9059d
DG
4108int ust_app_disable_event_glb(struct ltt_ust_session *usess,
4109 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
b0a40d28
DG
4110{
4111 int ret = 0;
bec39940 4112 struct lttng_ht_iter iter, uiter;
700c5a9d 4113 struct lttng_ht_node_str *ua_chan_node;
b0a40d28
DG
4114 struct ust_app *app;
4115 struct ust_app_session *ua_sess;
4116 struct ust_app_channel *ua_chan;
4117 struct ust_app_event *ua_event;
4118
88e3c2f5 4119 assert(usess->active);
b0a40d28 4120 DBG("UST app disabling event %s for all apps in channel "
d9bf3ca4
MD
4121 "%s for session id %" PRIu64,
4122 uevent->attr.name, uchan->name, usess->id);
b0a40d28
DG
4123
4124 rcu_read_lock();
4125
4126 /* For all registered applications */
852d0037 4127 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
4128 if (!app->compatible) {
4129 /*
4130 * TODO: In time, we should notice the caller of this error by
4131 * telling him that this is a version error.
4132 */
4133 continue;
4134 }
b0a40d28
DG
4135 ua_sess = lookup_session_by_app(usess, app);
4136 if (ua_sess == NULL) {
4137 /* Next app */
4138 continue;
4139 }
4140
4141 /* Lookup channel in the ust app session */
bec39940
DG
4142 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
4143 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
b0a40d28 4144 if (ua_chan_node == NULL) {
d9bf3ca4 4145 DBG2("Channel %s not found in session id %" PRIu64 " for app pid %d."
852d0037 4146 "Skipping", uchan->name, usess->id, app->pid);
b0a40d28
DG
4147 continue;
4148 }
4149 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
4150
700c5a9d
JR
4151 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
4152 uevent->filter, uevent->attr.loglevel,
4153 uevent->exclusion);
4154 if (ua_event == NULL) {
b0a40d28 4155 DBG2("Event %s not found in channel %s for app pid %d."
852d0037 4156 "Skipping", uevent->attr.name, uchan->name, app->pid);
b0a40d28
DG
4157 continue;
4158 }
b0a40d28 4159
7f79d3a1 4160 ret = disable_ust_app_event(ua_sess, ua_event, app);
b0a40d28
DG
4161 if (ret < 0) {
4162 /* XXX: Report error someday... */
4163 continue;
4164 }
4165 }
4166
4167 rcu_read_unlock();
88e3c2f5
JG
4168 return ret;
4169}
4170
4171/* The ua_sess lock must be held by the caller. */
4172static
4173int ust_app_channel_create(struct ltt_ust_session *usess,
4174 struct ust_app_session *ua_sess,
4175 struct ltt_ust_channel *uchan, struct ust_app *app,
4176 struct ust_app_channel **_ua_chan)
4177{
4178 int ret = 0;
4179 struct ust_app_channel *ua_chan = NULL;
4180
4181 assert(ua_sess);
4182 ASSERT_LOCKED(ua_sess->lock);
4183
4184 if (!strncmp(uchan->name, DEFAULT_METADATA_NAME,
4185 sizeof(uchan->name))) {
4186 copy_channel_attr_to_ustctl(&ua_sess->metadata_attr,
4187 &uchan->attr);
4188 ret = 0;
4189 } else {
4190 struct ltt_ust_context *uctx = NULL;
4191
4192 /*
4193 * Create channel onto application and synchronize its
4194 * configuration.
4195 */
4196 ret = ust_app_channel_allocate(ua_sess, uchan,
4197 LTTNG_UST_CHAN_PER_CPU, usess,
4198 &ua_chan);
88ebf5a7
JR
4199 if (ret < 0) {
4200 goto error;
4201 }
4202
4203 ret = ust_app_channel_send(app, usess,
4204 ua_sess, ua_chan);
4205 if (ret) {
4206 goto error;
88e3c2f5
JG
4207 }
4208
4209 /* Add contexts. */
4210 cds_list_for_each_entry(uctx, &uchan->ctx_list, list) {
4211 ret = create_ust_app_channel_context(ua_chan,
4212 &uctx->ctx, app);
4213 if (ret) {
88ebf5a7 4214 goto error;
88e3c2f5
JG
4215 }
4216 }
4217 }
88ebf5a7
JR
4218
4219error:
88e3c2f5
JG
4220 if (ret < 0) {
4221 switch (ret) {
4222 case -ENOTCONN:
4223 /*
4224 * The application's socket is not valid. Either a bad socket
4225 * or a timeout on it. We can't inform the caller that for a
4226 * specific app, the session failed so lets continue here.
4227 */
4228 ret = 0; /* Not an error. */
4229 break;
4230 case -ENOMEM:
4231 default:
4232 break;
4233 }
4234 }
88ebf5a7 4235
88e3c2f5
JG
4236 if (ret == 0 && _ua_chan) {
4237 /*
4238 * Only return the application's channel on success. Note
4239 * that the channel can still be part of the application's
4240 * channel hashtable on error.
4241 */
4242 *_ua_chan = ua_chan;
4243 }
b0a40d28
DG
4244 return ret;
4245}
4246
5b4a0ec0 4247/*
edb67388 4248 * Enable event for a specific session and channel on the tracer.
5b4a0ec0 4249 */
35a9059d 4250int ust_app_enable_event_glb(struct ltt_ust_session *usess,
48842b30
DG
4251 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
4252{
4253 int ret = 0;
bec39940 4254 struct lttng_ht_iter iter, uiter;
18eace3b 4255 struct lttng_ht_node_str *ua_chan_node;
48842b30
DG
4256 struct ust_app *app;
4257 struct ust_app_session *ua_sess;
4258 struct ust_app_channel *ua_chan;
4259 struct ust_app_event *ua_event;
48842b30 4260
88e3c2f5 4261 assert(usess->active);
d9bf3ca4 4262 DBG("UST app enabling event %s for all apps for session id %" PRIu64,
a991f516 4263 uevent->attr.name, usess->id);
48842b30 4264
edb67388
DG
4265 /*
4266 * NOTE: At this point, this function is called only if the session and
4267 * channel passed are already created for all apps. and enabled on the
4268 * tracer also.
4269 */
4270
48842b30 4271 rcu_read_lock();
421cb601
DG
4272
4273 /* For all registered applications */
852d0037 4274 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
4275 if (!app->compatible) {
4276 /*
4277 * TODO: In time, we should notice the caller of this error by
4278 * telling him that this is a version error.
4279 */
4280 continue;
4281 }
edb67388 4282 ua_sess = lookup_session_by_app(usess, app);
c4a1715b
DG
4283 if (!ua_sess) {
4284 /* The application has problem or is probably dead. */
4285 continue;
4286 }
ba767faf 4287
d0b96690
DG
4288 pthread_mutex_lock(&ua_sess->lock);
4289
b161602a
MD
4290 if (ua_sess->deleted) {
4291 pthread_mutex_unlock(&ua_sess->lock);
4292 continue;
4293 }
4294
edb67388 4295 /* Lookup channel in the ust app session */
bec39940
DG
4296 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
4297 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
a7169585
MD
4298 /*
4299 * It is possible that the channel cannot be found is
4300 * the channel/event creation occurs concurrently with
4301 * an application exit.
4302 */
4303 if (!ua_chan_node) {
4304 pthread_mutex_unlock(&ua_sess->lock);
4305 continue;
4306 }
edb67388
DG
4307
4308 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
4309
18eace3b
DG
4310 /* Get event node */
4311 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
39c5a3a7 4312 uevent->filter, uevent->attr.loglevel, uevent->exclusion);
18eace3b 4313 if (ua_event == NULL) {
7f79d3a1 4314 DBG3("UST app enable event %s not found for app PID %d."
852d0037 4315 "Skipping app", uevent->attr.name, app->pid);
d0b96690 4316 goto next_app;
35a9059d 4317 }
35a9059d
DG
4318
4319 ret = enable_ust_app_event(ua_sess, ua_event, app);
4320 if (ret < 0) {
d0b96690 4321 pthread_mutex_unlock(&ua_sess->lock);
7f79d3a1 4322 goto error;
48842b30 4323 }
d0b96690
DG
4324 next_app:
4325 pthread_mutex_unlock(&ua_sess->lock);
edb67388
DG
4326 }
4327
7f79d3a1 4328error:
edb67388 4329 rcu_read_unlock();
edb67388
DG
4330 return ret;
4331}
4332
4333/*
4334 * For a specific existing UST session and UST channel, creates the event for
4335 * all registered apps.
4336 */
35a9059d 4337int ust_app_create_event_glb(struct ltt_ust_session *usess,
edb67388
DG
4338 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
4339{
4340 int ret = 0;
bec39940
DG
4341 struct lttng_ht_iter iter, uiter;
4342 struct lttng_ht_node_str *ua_chan_node;
edb67388
DG
4343 struct ust_app *app;
4344 struct ust_app_session *ua_sess;
4345 struct ust_app_channel *ua_chan;
4346
88e3c2f5 4347 assert(usess->active);
d9bf3ca4 4348 DBG("UST app creating event %s for all apps for session id %" PRIu64,
a991f516 4349 uevent->attr.name, usess->id);
edb67388 4350
edb67388
DG
4351 rcu_read_lock();
4352
4353 /* For all registered applications */
852d0037 4354 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
4355 if (!app->compatible) {
4356 /*
4357 * TODO: In time, we should notice the caller of this error by
4358 * telling him that this is a version error.
4359 */
4360 continue;
4361 }
edb67388 4362 ua_sess = lookup_session_by_app(usess, app);
c4a1715b
DG
4363 if (!ua_sess) {
4364 /* The application has problem or is probably dead. */
4365 continue;
4366 }
48842b30 4367
d0b96690 4368 pthread_mutex_lock(&ua_sess->lock);
b161602a
MD
4369
4370 if (ua_sess->deleted) {
4371 pthread_mutex_unlock(&ua_sess->lock);
4372 continue;
4373 }
4374
48842b30 4375 /* Lookup channel in the ust app session */
bec39940
DG
4376 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
4377 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
edb67388
DG
4378 /* If the channel is not found, there is a code flow error */
4379 assert(ua_chan_node);
4380
48842b30
DG
4381 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
4382
edb67388 4383 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
d0b96690 4384 pthread_mutex_unlock(&ua_sess->lock);
edb67388 4385 if (ret < 0) {
49c336c1 4386 if (ret != -LTTNG_UST_ERR_EXIST) {
fc34caaa
DG
4387 /* Possible value at this point: -ENOMEM. If so, we stop! */
4388 break;
4389 }
4390 DBG2("UST app event %s already exist on app PID %d",
852d0037 4391 uevent->attr.name, app->pid);
5b4a0ec0 4392 continue;
48842b30 4393 }
48842b30 4394 }
5b4a0ec0 4395
48842b30 4396 rcu_read_unlock();
48842b30
DG
4397 return ret;
4398}
4399
5b4a0ec0
DG
4400/*
4401 * Start tracing for a specific UST session and app.
fad1ed2f
JR
4402 *
4403 * Called with UST app session lock held.
4404 *
5b4a0ec0 4405 */
b34cbebf 4406static
421cb601 4407int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
48842b30
DG
4408{
4409 int ret = 0;
48842b30 4410 struct ust_app_session *ua_sess;
48842b30 4411
852d0037 4412 DBG("Starting tracing for ust app pid %d", app->pid);
5cf5d0e7 4413
509cbaf8
MD
4414 rcu_read_lock();
4415
e0c7ec2b
DG
4416 if (!app->compatible) {
4417 goto end;
4418 }
4419
421cb601
DG
4420 ua_sess = lookup_session_by_app(usess, app);
4421 if (ua_sess == NULL) {
d42f20df
DG
4422 /* The session is in teardown process. Ignore and continue. */
4423 goto end;
421cb601 4424 }
48842b30 4425
d0b96690
DG
4426 pthread_mutex_lock(&ua_sess->lock);
4427
b161602a
MD
4428 if (ua_sess->deleted) {
4429 pthread_mutex_unlock(&ua_sess->lock);
4430 goto end;
4431 }
4432
b0a1c741
JR
4433 if (ua_sess->enabled) {
4434 pthread_mutex_unlock(&ua_sess->lock);
4435 goto end;
4436 }
4437
aea829b3
DG
4438 /* Upon restart, we skip the setup, already done */
4439 if (ua_sess->started) {
8be98f9a 4440 goto skip_setup;
aea829b3 4441 }
8be98f9a 4442
840cb59c 4443 health_code_update();
86acf0da 4444
8be98f9a 4445skip_setup:
a945cdc7 4446 /* This starts the UST tracing */
fb45065e 4447 pthread_mutex_lock(&app->sock_lock);
852d0037 4448 ret = ustctl_start_session(app->sock, ua_sess->handle);
fb45065e 4449 pthread_mutex_unlock(&app->sock_lock);
421cb601 4450 if (ret < 0) {
ffe60014
DG
4451 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
4452 ERR("Error starting tracing for app pid: %d (ret: %d)",
4453 app->pid, ret);
4454 } else {
4455 DBG("UST app start session failed. Application is dead.");
3757b385
DG
4456 /*
4457 * This is normal behavior, an application can die during the
4458 * creation process. Don't report an error so the execution can
4459 * continue normally.
4460 */
4461 pthread_mutex_unlock(&ua_sess->lock);
4462 goto end;
ffe60014 4463 }
d0b96690 4464 goto error_unlock;
421cb601 4465 }
5b4a0ec0 4466
55c3953d
DG
4467 /* Indicate that the session has been started once */
4468 ua_sess->started = 1;
b0a1c741 4469 ua_sess->enabled = 1;
55c3953d 4470
d0b96690
DG
4471 pthread_mutex_unlock(&ua_sess->lock);
4472
840cb59c 4473 health_code_update();
86acf0da 4474
421cb601 4475 /* Quiescent wait after starting trace */
fb45065e 4476 pthread_mutex_lock(&app->sock_lock);
ffe60014 4477 ret = ustctl_wait_quiescent(app->sock);
fb45065e 4478 pthread_mutex_unlock(&app->sock_lock);
ffe60014
DG
4479 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
4480 ERR("UST app wait quiescent failed for app pid %d ret %d",
4481 app->pid, ret);
4482 }
48842b30 4483
e0c7ec2b
DG
4484end:
4485 rcu_read_unlock();
840cb59c 4486 health_code_update();
421cb601 4487 return 0;
48842b30 4488
d0b96690
DG
4489error_unlock:
4490 pthread_mutex_unlock(&ua_sess->lock);
509cbaf8 4491 rcu_read_unlock();
840cb59c 4492 health_code_update();
421cb601
DG
4493 return -1;
4494}
48842b30 4495
8be98f9a
MD
4496/*
4497 * Stop tracing for a specific UST session and app.
4498 */
b34cbebf 4499static
8be98f9a
MD
4500int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app)
4501{
4502 int ret = 0;
4503 struct ust_app_session *ua_sess;
7972aab2 4504 struct ust_registry_session *registry;
8be98f9a 4505
852d0037 4506 DBG("Stopping tracing for ust app pid %d", app->pid);
8be98f9a
MD
4507
4508 rcu_read_lock();
4509
e0c7ec2b 4510 if (!app->compatible) {
d88aee68 4511 goto end_no_session;
e0c7ec2b
DG
4512 }
4513
8be98f9a
MD
4514 ua_sess = lookup_session_by_app(usess, app);
4515 if (ua_sess == NULL) {
d88aee68 4516 goto end_no_session;
8be98f9a
MD
4517 }
4518
d88aee68
DG
4519 pthread_mutex_lock(&ua_sess->lock);
4520
b161602a
MD
4521 if (ua_sess->deleted) {
4522 pthread_mutex_unlock(&ua_sess->lock);
4523 goto end_no_session;
4524 }
4525
9bc07046
DG
4526 /*
4527 * If started = 0, it means that stop trace has been called for a session
c45536e1
DG
4528 * that was never started. It's possible since we can have a fail start
4529 * from either the application manager thread or the command thread. Simply
4530 * indicate that this is a stop error.
9bc07046 4531 */
f9dfc3d9 4532 if (!ua_sess->started) {
c45536e1
DG
4533 goto error_rcu_unlock;
4534 }
7db205b5 4535
840cb59c 4536 health_code_update();
86acf0da 4537
9d6c7d3f 4538 /* This inhibits UST tracing */
fb45065e 4539 pthread_mutex_lock(&app->sock_lock);
852d0037 4540 ret = ustctl_stop_session(app->sock, ua_sess->handle);
fb45065e 4541 pthread_mutex_unlock(&app->sock_lock);
9d6c7d3f 4542 if (ret < 0) {
ffe60014
DG
4543 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
4544 ERR("Error stopping tracing for app pid: %d (ret: %d)",
4545 app->pid, ret);
4546 } else {
4547 DBG("UST app stop session failed. Application is dead.");
3757b385
DG
4548 /*
4549 * This is normal behavior, an application can die during the
4550 * creation process. Don't report an error so the execution can
4551 * continue normally.
4552 */
4553 goto end_unlock;
ffe60014 4554 }
9d6c7d3f
DG
4555 goto error_rcu_unlock;
4556 }
4557
840cb59c 4558 health_code_update();
b0a1c741 4559 ua_sess->enabled = 0;
86acf0da 4560
9d6c7d3f 4561 /* Quiescent wait after stopping trace */
fb45065e 4562 pthread_mutex_lock(&app->sock_lock);
ffe60014 4563 ret = ustctl_wait_quiescent(app->sock);
fb45065e 4564 pthread_mutex_unlock(&app->sock_lock);
ffe60014
DG
4565 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
4566 ERR("UST app wait quiescent failed for app pid %d ret %d",
4567 app->pid, ret);
4568 }
9d6c7d3f 4569
840cb59c 4570 health_code_update();
86acf0da 4571
b34cbebf 4572 registry = get_session_registry(ua_sess);
fad1ed2f
JR
4573
4574 /* The UST app session is held registry shall not be null. */
b34cbebf 4575 assert(registry);
1b532a60 4576
ce34fcd0
MD
4577 /* Push metadata for application before freeing the application. */
4578 (void) push_metadata(registry, ua_sess->consumer);
b34cbebf 4579
3757b385 4580end_unlock:
b34cbebf
MD
4581 pthread_mutex_unlock(&ua_sess->lock);
4582end_no_session:
4583 rcu_read_unlock();
4584 health_code_update();
4585 return 0;
4586
4587error_rcu_unlock:
4588 pthread_mutex_unlock(&ua_sess->lock);
4589 rcu_read_unlock();
4590 health_code_update();
4591 return -1;
4592}
4593
b34cbebf 4594static
c4b88406
MD
4595int ust_app_flush_app_session(struct ust_app *app,
4596 struct ust_app_session *ua_sess)
b34cbebf 4597{
c4b88406 4598 int ret, retval = 0;
b34cbebf 4599 struct lttng_ht_iter iter;
b34cbebf 4600 struct ust_app_channel *ua_chan;
c4b88406 4601 struct consumer_socket *socket;
b34cbebf 4602
c4b88406 4603 DBG("Flushing app session buffers for ust app pid %d", app->pid);
b34cbebf
MD
4604
4605 rcu_read_lock();
4606
4607 if (!app->compatible) {
c4b88406 4608 goto end_not_compatible;
b34cbebf
MD
4609 }
4610
4611 pthread_mutex_lock(&ua_sess->lock);
4612
b161602a
MD
4613 if (ua_sess->deleted) {
4614 goto end_deleted;
4615 }
4616
b34cbebf
MD
4617 health_code_update();
4618
9d6c7d3f 4619 /* Flushing buffers */
c4b88406
MD
4620 socket = consumer_find_socket_by_bitness(app->bits_per_long,
4621 ua_sess->consumer);
ce34fcd0
MD
4622
4623 /* Flush buffers and push metadata. */
4624 switch (ua_sess->buffer_type) {
4625 case LTTNG_BUFFER_PER_PID:
4626 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
4627 node.node) {
4628 health_code_update();
ce34fcd0
MD
4629 ret = consumer_flush_channel(socket, ua_chan->key);
4630 if (ret) {
4631 ERR("Error flushing consumer channel");
4632 retval = -1;
4633 continue;
4634 }
8be98f9a 4635 }
ce34fcd0
MD
4636 break;
4637 case LTTNG_BUFFER_PER_UID:
4638 default:
4639 assert(0);
4640 break;
8be98f9a 4641 }
8be98f9a 4642
840cb59c 4643 health_code_update();
86acf0da 4644
b161602a 4645end_deleted:
d88aee68 4646 pthread_mutex_unlock(&ua_sess->lock);
ce34fcd0 4647
c4b88406
MD
4648end_not_compatible:
4649 rcu_read_unlock();
4650 health_code_update();
4651 return retval;
4652}
4653
4654/*
ce34fcd0
MD
4655 * Flush buffers for all applications for a specific UST session.
4656 * Called with UST session lock held.
c4b88406
MD
4657 */
4658static
ce34fcd0 4659int ust_app_flush_session(struct ltt_ust_session *usess)
c4b88406
MD
4660
4661{
99b1411c 4662 int ret = 0;
c4b88406 4663
ce34fcd0 4664 DBG("Flushing session buffers for all ust apps");
c4b88406
MD
4665
4666 rcu_read_lock();
4667
ce34fcd0
MD
4668 /* Flush buffers and push metadata. */
4669 switch (usess->buffer_type) {
4670 case LTTNG_BUFFER_PER_UID:
4671 {
4672 struct buffer_reg_uid *reg;
4673 struct lttng_ht_iter iter;
4674
4675 /* Flush all per UID buffers associated to that session. */
4676 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
4677 struct ust_registry_session *ust_session_reg;
4678 struct buffer_reg_channel *reg_chan;
4679 struct consumer_socket *socket;
4680
4681 /* Get consumer socket to use to push the metadata.*/
4682 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
4683 usess->consumer);
4684 if (!socket) {
4685 /* Ignore request if no consumer is found for the session. */
4686 continue;
4687 }
4688
4689 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
4690 reg_chan, node.node) {
4691 /*
4692 * The following call will print error values so the return
4693 * code is of little importance because whatever happens, we
4694 * have to try them all.
4695 */
4696 (void) consumer_flush_channel(socket, reg_chan->consumer_key);
4697 }
4698
4699 ust_session_reg = reg->registry->reg.ust;
4700 /* Push metadata. */
4701 (void) push_metadata(ust_session_reg, usess->consumer);
4702 }
ce34fcd0
MD
4703 break;
4704 }
4705 case LTTNG_BUFFER_PER_PID:
4706 {
4707 struct ust_app_session *ua_sess;
4708 struct lttng_ht_iter iter;
4709 struct ust_app *app;
4710
4711 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4712 ua_sess = lookup_session_by_app(usess, app);
4713 if (ua_sess == NULL) {
4714 continue;
4715 }
4716 (void) ust_app_flush_app_session(app, ua_sess);
4717 }
4718 break;
4719 }
4720 default:
99b1411c 4721 ret = -1;
ce34fcd0
MD
4722 assert(0);
4723 break;
c4b88406 4724 }
c4b88406 4725
7db205b5 4726 rcu_read_unlock();
840cb59c 4727 health_code_update();
c4b88406 4728 return ret;
8be98f9a
MD
4729}
4730
0dd01979
MD
4731static
4732int ust_app_clear_quiescent_app_session(struct ust_app *app,
4733 struct ust_app_session *ua_sess)
4734{
4735 int ret = 0;
4736 struct lttng_ht_iter iter;
4737 struct ust_app_channel *ua_chan;
4738 struct consumer_socket *socket;
4739
4740 DBG("Clearing stream quiescent state for ust app pid %d", app->pid);
4741
4742 rcu_read_lock();
4743
4744 if (!app->compatible) {
4745 goto end_not_compatible;
4746 }
4747
4748 pthread_mutex_lock(&ua_sess->lock);
4749
4750 if (ua_sess->deleted) {
4751 goto end_unlock;
4752 }
4753
4754 health_code_update();
4755
4756 socket = consumer_find_socket_by_bitness(app->bits_per_long,
4757 ua_sess->consumer);
4758 if (!socket) {
4759 ERR("Failed to find consumer (%" PRIu32 ") socket",
4760 app->bits_per_long);
4761 ret = -1;
4762 goto end_unlock;
4763 }
4764
4765 /* Clear quiescent state. */
4766 switch (ua_sess->buffer_type) {
4767 case LTTNG_BUFFER_PER_PID:
4768 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter,
4769 ua_chan, node.node) {
4770 health_code_update();
4771 ret = consumer_clear_quiescent_channel(socket,
4772 ua_chan->key);
4773 if (ret) {
4774 ERR("Error clearing quiescent state for consumer channel");
4775 ret = -1;
4776 continue;
4777 }
4778 }
4779 break;
4780 case LTTNG_BUFFER_PER_UID:
4781 default:
4782 assert(0);
4783 ret = -1;
4784 break;
4785 }
4786
4787 health_code_update();
4788
4789end_unlock:
4790 pthread_mutex_unlock(&ua_sess->lock);
4791
4792end_not_compatible:
4793 rcu_read_unlock();
4794 health_code_update();
4795 return ret;
4796}
4797
4798/*
4799 * Clear quiescent state in each stream for all applications for a
4800 * specific UST session.
4801 * Called with UST session lock held.
4802 */
4803static
4804int ust_app_clear_quiescent_session(struct ltt_ust_session *usess)
4805
4806{
4807 int ret = 0;
4808
4809 DBG("Clearing stream quiescent state for all ust apps");
4810
4811 rcu_read_lock();
4812
4813 switch (usess->buffer_type) {
4814 case LTTNG_BUFFER_PER_UID:
4815 {
4816 struct lttng_ht_iter iter;
4817 struct buffer_reg_uid *reg;
4818
4819 /*
4820 * Clear quiescent for all per UID buffers associated to
4821 * that session.
4822 */
4823 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
4824 struct consumer_socket *socket;
4825 struct buffer_reg_channel *reg_chan;
4826
4827 /* Get associated consumer socket.*/
4828 socket = consumer_find_socket_by_bitness(
4829 reg->bits_per_long, usess->consumer);
4830 if (!socket) {
4831 /*
4832 * Ignore request if no consumer is found for
4833 * the session.
4834 */
4835 continue;
4836 }
4837
4838 cds_lfht_for_each_entry(reg->registry->channels->ht,
4839 &iter.iter, reg_chan, node.node) {
4840 /*
4841 * The following call will print error values so
4842 * the return code is of little importance
4843 * because whatever happens, we have to try them
4844 * all.
4845 */
4846 (void) consumer_clear_quiescent_channel(socket,
4847 reg_chan->consumer_key);
4848 }
4849 }
4850 break;
4851 }
4852 case LTTNG_BUFFER_PER_PID:
4853 {
4854 struct ust_app_session *ua_sess;
4855 struct lttng_ht_iter iter;
4856 struct ust_app *app;
4857
4858 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app,
4859 pid_n.node) {
4860 ua_sess = lookup_session_by_app(usess, app);
4861 if (ua_sess == NULL) {
4862 continue;
4863 }
4864 (void) ust_app_clear_quiescent_app_session(app,
4865 ua_sess);
4866 }
4867 break;
4868 }
4869 default:
4870 ret = -1;
4871 assert(0);
4872 break;
4873 }
4874
4875 rcu_read_unlock();
4876 health_code_update();
4877 return ret;
4878}
4879
84cd17c6
MD
4880/*
4881 * Destroy a specific UST session in apps.
4882 */
3353de95 4883static int destroy_trace(struct ltt_ust_session *usess, struct ust_app *app)
84cd17c6 4884{
ffe60014 4885 int ret;
84cd17c6 4886 struct ust_app_session *ua_sess;
bec39940 4887 struct lttng_ht_iter iter;
d9bf3ca4 4888 struct lttng_ht_node_u64 *node;
84cd17c6 4889
852d0037 4890 DBG("Destroy tracing for ust app pid %d", app->pid);
84cd17c6
MD
4891
4892 rcu_read_lock();
4893
e0c7ec2b
DG
4894 if (!app->compatible) {
4895 goto end;
4896 }
4897
84cd17c6 4898 __lookup_session_by_app(usess, app, &iter);
d9bf3ca4 4899 node = lttng_ht_iter_get_node_u64(&iter);
84cd17c6 4900 if (node == NULL) {
d42f20df
DG
4901 /* Session is being or is deleted. */
4902 goto end;
84cd17c6
MD
4903 }
4904 ua_sess = caa_container_of(node, struct ust_app_session, node);
c4a1715b 4905
840cb59c 4906 health_code_update();
d0b96690 4907 destroy_app_session(app, ua_sess);
84cd17c6 4908
840cb59c 4909 health_code_update();
7db205b5 4910
84cd17c6 4911 /* Quiescent wait after stopping trace */
fb45065e 4912 pthread_mutex_lock(&app->sock_lock);
ffe60014 4913 ret = ustctl_wait_quiescent(app->sock);
fb45065e 4914 pthread_mutex_unlock(&app->sock_lock);
ffe60014
DG
4915 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
4916 ERR("UST app wait quiescent failed for app pid %d ret %d",
4917 app->pid, ret);
4918 }
e0c7ec2b
DG
4919end:
4920 rcu_read_unlock();
840cb59c 4921 health_code_update();
84cd17c6 4922 return 0;
84cd17c6
MD
4923}
4924
5b4a0ec0
DG
4925/*
4926 * Start tracing for the UST session.
4927 */
421cb601
DG
4928int ust_app_start_trace_all(struct ltt_ust_session *usess)
4929{
bec39940 4930 struct lttng_ht_iter iter;
421cb601 4931 struct ust_app *app;
48842b30 4932
421cb601
DG
4933 DBG("Starting all UST traces");
4934
bb2452c8
MD
4935 /*
4936 * Even though the start trace might fail, flag this session active so
4937 * other application coming in are started by default.
4938 */
4939 usess->active = 1;
4940
421cb601 4941 rcu_read_lock();
421cb601 4942
0dd01979
MD
4943 /*
4944 * In a start-stop-start use-case, we need to clear the quiescent state
4945 * of each channel set by the prior stop command, thus ensuring that a
4946 * following stop or destroy is sure to grab a timestamp_end near those
4947 * operations, even if the packet is empty.
4948 */
4949 (void) ust_app_clear_quiescent_session(usess);
4950
0498a00c
MD
4951 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4952 ust_app_global_update(usess, app);
4953 }
4954
48842b30
DG
4955 rcu_read_unlock();
4956
4957 return 0;
4958}
487cf67c 4959
8be98f9a
MD
4960/*
4961 * Start tracing for the UST session.
ce34fcd0 4962 * Called with UST session lock held.
8be98f9a
MD
4963 */
4964int ust_app_stop_trace_all(struct ltt_ust_session *usess)
4965{
4966 int ret = 0;
bec39940 4967 struct lttng_ht_iter iter;
8be98f9a
MD
4968 struct ust_app *app;
4969
4970 DBG("Stopping all UST traces");
4971
bb2452c8
MD
4972 /*
4973 * Even though the stop trace might fail, flag this session inactive so
4974 * other application coming in are not started by default.
4975 */
4976 usess->active = 0;
4977
8be98f9a
MD
4978 rcu_read_lock();
4979
b34cbebf
MD
4980 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4981 ret = ust_app_stop_trace(usess, app);
4982 if (ret < 0) {
4983 /* Continue to next apps even on error */
4984 continue;
4985 }
4986 }
4987
ce34fcd0 4988 (void) ust_app_flush_session(usess);
8be98f9a
MD
4989
4990 rcu_read_unlock();
4991
4992 return 0;
4993}
4994
84cd17c6
MD
4995/*
4996 * Destroy app UST session.
4997 */
4998int ust_app_destroy_trace_all(struct ltt_ust_session *usess)
4999{
5000 int ret = 0;
bec39940 5001 struct lttng_ht_iter iter;
84cd17c6
MD
5002 struct ust_app *app;
5003
5004 DBG("Destroy all UST traces");
5005
5006 rcu_read_lock();
5007
852d0037 5008 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3353de95 5009 ret = destroy_trace(usess, app);
84cd17c6
MD
5010 if (ret < 0) {
5011 /* Continue to next apps even on error */
5012 continue;
5013 }
5014 }
5015
5016 rcu_read_unlock();
5017
5018 return 0;
5019}
5020
88e3c2f5 5021/* The ua_sess lock must be held by the caller. */
a9ad0c8f 5022static
88e3c2f5
JG
5023int find_or_create_ust_app_channel(
5024 struct ltt_ust_session *usess,
5025 struct ust_app_session *ua_sess,
5026 struct ust_app *app,
5027 struct ltt_ust_channel *uchan,
5028 struct ust_app_channel **ua_chan)
487cf67c 5029{
55c54cce 5030 int ret = 0;
88e3c2f5
JG
5031 struct lttng_ht_iter iter;
5032 struct lttng_ht_node_str *ua_chan_node;
5033
5034 lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &iter);
5035 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
5036 if (ua_chan_node) {
5037 *ua_chan = caa_container_of(ua_chan_node,
5038 struct ust_app_channel, node);
5039 goto end;
5040 }
5041
5042 ret = ust_app_channel_create(usess, ua_sess, uchan, app, ua_chan);
5043 if (ret) {
5044 goto end;
5045 }
5046end:
5047 return ret;
5048}
5049
5050static
5051int ust_app_channel_synchronize_event(struct ust_app_channel *ua_chan,
5052 struct ltt_ust_event *uevent, struct ust_app_session *ua_sess,
5053 struct ust_app *app)
5054{
5055 int ret = 0;
5056 struct ust_app_event *ua_event = NULL;
5057
5058 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
5059 uevent->filter, uevent->attr.loglevel, uevent->exclusion);
5060 if (!ua_event) {
5061 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
5062 if (ret < 0) {
5063 goto end;
5064 }
5065 } else {
5066 if (ua_event->enabled != uevent->enabled) {
5067 ret = uevent->enabled ?
5068 enable_ust_app_event(ua_sess, ua_event, app) :
5069 disable_ust_app_event(ua_sess, ua_event, app);
5070 }
5071 }
5072
5073end:
5074 return ret;
5075}
5076
5077/*
5078 * The caller must ensure that the application is compatible and is tracked
2a3a1a2b 5079 * by the process attribute trackers.
88e3c2f5
JG
5080 */
5081static
5082void ust_app_synchronize(struct ltt_ust_session *usess,
5083 struct ust_app *app)
5084{
5085 int ret = 0;
5086 struct cds_lfht_iter uchan_iter;
5087 struct ltt_ust_channel *uchan;
3d8ca23b 5088 struct ust_app_session *ua_sess = NULL;
1f3580c7 5089
88e3c2f5
JG
5090 /*
5091 * The application's configuration should only be synchronized for
5092 * active sessions.
5093 */
5094 assert(usess->active);
5095
5096 ret = find_or_create_ust_app_session(usess, app, &ua_sess, NULL);
3d8ca23b
DG
5097 if (ret < 0) {
5098 /* Tracer is probably gone or ENOMEM. */
487cf67c
DG
5099 goto error;
5100 }
3d8ca23b 5101 assert(ua_sess);
487cf67c 5102
d0b96690 5103 pthread_mutex_lock(&ua_sess->lock);
b161602a
MD
5104 if (ua_sess->deleted) {
5105 pthread_mutex_unlock(&ua_sess->lock);
5106 goto end;
5107 }
5108
88e3c2f5 5109 rcu_read_lock();
ef67c072 5110
88e3c2f5
JG
5111 cds_lfht_for_each_entry(usess->domain_global.channels->ht, &uchan_iter,
5112 uchan, node.node) {
5113 struct ust_app_channel *ua_chan;
5114 struct cds_lfht_iter uevent_iter;
5115 struct ltt_ust_event *uevent;
487cf67c 5116
31746f93 5117 /*
88e3c2f5
JG
5118 * Search for a matching ust_app_channel. If none is found,
5119 * create it. Creating the channel will cause the ua_chan
5120 * structure to be allocated, the channel buffers to be
5121 * allocated (if necessary) and sent to the application, and
5122 * all enabled contexts will be added to the channel.
31746f93 5123 */
f3db82be 5124 ret = find_or_create_ust_app_channel(usess, ua_sess,
88e3c2f5
JG
5125 app, uchan, &ua_chan);
5126 if (ret) {
5127 /* Tracer is probably gone or ENOMEM. */
5128 goto error_unlock;
727d5404
DG
5129 }
5130
88e3c2f5
JG
5131 if (!ua_chan) {
5132 /* ua_chan will be NULL for the metadata channel */
5133 continue;
5134 }
727d5404 5135
88e3c2f5 5136 cds_lfht_for_each_entry(uchan->events->ht, &uevent_iter, uevent,
bec39940 5137 node.node) {
88e3c2f5
JG
5138 ret = ust_app_channel_synchronize_event(ua_chan,
5139 uevent, ua_sess, app);
5140 if (ret) {
d0b96690 5141 goto error_unlock;
487cf67c 5142 }
36dc12cc 5143 }
d0b96690 5144
88e3c2f5
JG
5145 if (ua_chan->enabled != uchan->enabled) {
5146 ret = uchan->enabled ?
5147 enable_ust_app_channel(ua_sess, uchan, app) :
5148 disable_ust_app_channel(ua_sess, ua_chan, app);
5149 if (ret) {
5150 goto error_unlock;
5151 }
5152 }
36dc12cc 5153 }
ef67c072
JG
5154
5155 /*
5156 * Create the metadata for the application. This returns gracefully if a
5157 * metadata was already set for the session.
5158 *
5159 * The metadata channel must be created after the data channels as the
5160 * consumer daemon assumes this ordering. When interacting with a relay
5161 * daemon, the consumer will use this assumption to send the
5162 * "STREAMS_SENT" message to the relay daemon.
5163 */
5164 ret = create_ust_app_metadata(ua_sess, app, usess->consumer);
5165 if (ret < 0) {
5166 goto error_unlock;
5167 }
5168
88e3c2f5 5169 rcu_read_unlock();
0498a00c 5170
a9ad0c8f 5171end:
88e3c2f5 5172 pthread_mutex_unlock(&ua_sess->lock);
ffe60014 5173 /* Everything went well at this point. */
ffe60014
DG
5174 return;
5175
d0b96690 5176error_unlock:
88e3c2f5 5177 rcu_read_unlock();
d0b96690 5178 pthread_mutex_unlock(&ua_sess->lock);
487cf67c 5179error:
ffe60014 5180 if (ua_sess) {
d0b96690 5181 destroy_app_session(app, ua_sess);
ffe60014 5182 }
487cf67c
DG
5183 return;
5184}
55cc08a6 5185
a9ad0c8f
MD
5186static
5187void ust_app_global_destroy(struct ltt_ust_session *usess, struct ust_app *app)
5188{
5189 struct ust_app_session *ua_sess;
5190
5191 ua_sess = lookup_session_by_app(usess, app);
5192 if (ua_sess == NULL) {
5193 return;
5194 }
5195 destroy_app_session(app, ua_sess);
5196}
5197
5198/*
5199 * Add channels/events from UST global domain to registered apps at sock.
5200 *
5201 * Called with session lock held.
5202 * Called with RCU read-side lock held.
5203 */
5204void ust_app_global_update(struct ltt_ust_session *usess, struct ust_app *app)
5205{
5206 assert(usess);
88e3c2f5 5207 assert(usess->active);
a9ad0c8f
MD
5208
5209 DBG2("UST app global update for app sock %d for session id %" PRIu64,
5210 app->sock, usess->id);
5211
5212 if (!app->compatible) {
5213 return;
5214 }
159b042f
JG
5215 if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID,
5216 usess, app->pid) &&
55c9e7ca 5217 trace_ust_id_tracker_lookup(
159b042f
JG
5218 LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID,
5219 usess, app->uid) &&
55c9e7ca 5220 trace_ust_id_tracker_lookup(
159b042f
JG
5221 LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID,
5222 usess, app->gid)) {
88e3c2f5
JG
5223 /*
5224 * Synchronize the application's internal tracing configuration
5225 * and start tracing.
5226 */
5227 ust_app_synchronize(usess, app);
5228 ust_app_start_trace(usess, app);
a9ad0c8f
MD
5229 } else {
5230 ust_app_global_destroy(usess, app);
5231 }
5232}
5233
5234/*
5235 * Called with session lock held.
5236 */
5237void ust_app_global_update_all(struct ltt_ust_session *usess)
5238{
5239 struct lttng_ht_iter iter;
5240 struct ust_app *app;
5241
5242 rcu_read_lock();
5243 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5244 ust_app_global_update(usess, app);
5245 }
5246 rcu_read_unlock();
5247}
5248
55cc08a6
DG
5249/*
5250 * Add context to a specific channel for global UST domain.
5251 */
5252int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess,
5253 struct ltt_ust_channel *uchan, struct ltt_ust_context *uctx)
5254{
5255 int ret = 0;
bec39940
DG
5256 struct lttng_ht_node_str *ua_chan_node;
5257 struct lttng_ht_iter iter, uiter;
55cc08a6
DG
5258 struct ust_app_channel *ua_chan = NULL;
5259 struct ust_app_session *ua_sess;
5260 struct ust_app *app;
5261
88e3c2f5 5262 assert(usess->active);
0498a00c 5263
55cc08a6 5264 rcu_read_lock();
852d0037 5265 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
5266 if (!app->compatible) {
5267 /*
5268 * TODO: In time, we should notice the caller of this error by
5269 * telling him that this is a version error.
5270 */
5271 continue;
5272 }
55cc08a6
DG
5273 ua_sess = lookup_session_by_app(usess, app);
5274 if (ua_sess == NULL) {
5275 continue;
5276 }
5277
d0b96690 5278 pthread_mutex_lock(&ua_sess->lock);
b161602a
MD
5279
5280 if (ua_sess->deleted) {
5281 pthread_mutex_unlock(&ua_sess->lock);
5282 continue;
5283 }
5284
55cc08a6 5285 /* Lookup channel in the ust app session */
bec39940
DG
5286 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
5287 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
55cc08a6 5288 if (ua_chan_node == NULL) {
d0b96690 5289 goto next_app;
55cc08a6
DG
5290 }
5291 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel,
5292 node);
c9edf082 5293 ret = create_ust_app_channel_context(ua_chan, &uctx->ctx, app);
55cc08a6 5294 if (ret < 0) {
d0b96690 5295 goto next_app;
55cc08a6 5296 }
d0b96690
DG
5297 next_app:
5298 pthread_mutex_unlock(&ua_sess->lock);
55cc08a6
DG
5299 }
5300
55cc08a6 5301 rcu_read_unlock();
76d45b40
DG
5302 return ret;
5303}
7f79d3a1 5304
d0b96690
DG
5305/*
5306 * Receive registration and populate the given msg structure.
5307 *
5308 * On success return 0 else a negative value returned by the ustctl call.
5309 */
5310int ust_app_recv_registration(int sock, struct ust_register_msg *msg)
5311{
5312 int ret;
5313 uint32_t pid, ppid, uid, gid;
5314
5315 assert(msg);
5316
5317 ret = ustctl_recv_reg_msg(sock, &msg->type, &msg->major, &msg->minor,
5318 &pid, &ppid, &uid, &gid,
5319 &msg->bits_per_long,
5320 &msg->uint8_t_alignment,
5321 &msg->uint16_t_alignment,
5322 &msg->uint32_t_alignment,
5323 &msg->uint64_t_alignment,
5324 &msg->long_alignment,
5325 &msg->byte_order,
5326 msg->name);
5327 if (ret < 0) {
5328 switch (-ret) {
5329 case EPIPE:
5330 case ECONNRESET:
5331 case LTTNG_UST_ERR_EXITING:
5332 DBG3("UST app recv reg message failed. Application died");
5333 break;
5334 case LTTNG_UST_ERR_UNSUP_MAJOR:
5335 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
5336 msg->major, msg->minor, LTTNG_UST_ABI_MAJOR_VERSION,
5337 LTTNG_UST_ABI_MINOR_VERSION);
5338 break;
5339 default:
5340 ERR("UST app recv reg message failed with ret %d", ret);
5341 break;
5342 }
5343 goto error;
5344 }
5345 msg->pid = (pid_t) pid;
5346 msg->ppid = (pid_t) ppid;
5347 msg->uid = (uid_t) uid;
5348 msg->gid = (gid_t) gid;
5349
5350error:
5351 return ret;
5352}
5353
10b56aef
MD
5354/*
5355 * Return a ust app session object using the application object and the
5356 * session object descriptor has a key. If not found, NULL is returned.
5357 * A RCU read side lock MUST be acquired when calling this function.
5358*/
5359static struct ust_app_session *find_session_by_objd(struct ust_app *app,
5360 int objd)
5361{
5362 struct lttng_ht_node_ulong *node;
5363 struct lttng_ht_iter iter;
5364 struct ust_app_session *ua_sess = NULL;
5365
5366 assert(app);
5367
5368 lttng_ht_lookup(app->ust_sessions_objd, (void *)((unsigned long) objd), &iter);
5369 node = lttng_ht_iter_get_node_ulong(&iter);
5370 if (node == NULL) {
5371 DBG2("UST app session find by objd %d not found", objd);
5372 goto error;
5373 }
5374
5375 ua_sess = caa_container_of(node, struct ust_app_session, ust_objd_node);
5376
5377error:
5378 return ua_sess;
5379}
5380
d88aee68
DG
5381/*
5382 * Return a ust app channel object using the application object and the channel
5383 * object descriptor has a key. If not found, NULL is returned. A RCU read side
5384 * lock MUST be acquired before calling this function.
5385 */
d0b96690
DG
5386static struct ust_app_channel *find_channel_by_objd(struct ust_app *app,
5387 int objd)
5388{
5389 struct lttng_ht_node_ulong *node;
5390 struct lttng_ht_iter iter;
5391 struct ust_app_channel *ua_chan = NULL;
5392
5393 assert(app);
5394
5395 lttng_ht_lookup(app->ust_objd, (void *)((unsigned long) objd), &iter);
5396 node = lttng_ht_iter_get_node_ulong(&iter);
5397 if (node == NULL) {
5398 DBG2("UST app channel find by objd %d not found", objd);
5399 goto error;
5400 }
5401
5402 ua_chan = caa_container_of(node, struct ust_app_channel, ust_objd_node);
5403
5404error:
5405 return ua_chan;
5406}
5407
d88aee68
DG
5408/*
5409 * Reply to a register channel notification from an application on the notify
5410 * socket. The channel metadata is also created.
5411 *
5412 * The session UST registry lock is acquired in this function.
5413 *
5414 * On success 0 is returned else a negative value.
5415 */
8eede835 5416static int reply_ust_register_channel(int sock, int cobjd,
d0b96690
DG
5417 size_t nr_fields, struct ustctl_field *fields)
5418{
5419 int ret, ret_code = 0;
294e218e 5420 uint32_t chan_id;
7972aab2 5421 uint64_t chan_reg_key;
d0b96690
DG
5422 enum ustctl_channel_header type;
5423 struct ust_app *app;
5424 struct ust_app_channel *ua_chan;
5425 struct ust_app_session *ua_sess;
7972aab2 5426 struct ust_registry_session *registry;
45893984 5427 struct ust_registry_channel *chan_reg;
d0b96690
DG
5428
5429 rcu_read_lock();
5430
5431 /* Lookup application. If not found, there is a code flow error. */
5432 app = find_app_by_notify_sock(sock);
d88aee68 5433 if (!app) {
fad1ed2f 5434 DBG("Application socket %d is being torn down. Abort event notify",
d88aee68
DG
5435 sock);
5436 ret = 0;
5437 goto error_rcu_unlock;
5438 }
d0b96690 5439
4950b860 5440 /* Lookup channel by UST object descriptor. */
d0b96690 5441 ua_chan = find_channel_by_objd(app, cobjd);
4950b860 5442 if (!ua_chan) {
fad1ed2f 5443 DBG("Application channel is being torn down. Abort event notify");
4950b860
MD
5444 ret = 0;
5445 goto error_rcu_unlock;
5446 }
5447
d0b96690
DG
5448 assert(ua_chan->session);
5449 ua_sess = ua_chan->session;
d0b96690 5450
7972aab2
DG
5451 /* Get right session registry depending on the session buffer type. */
5452 registry = get_session_registry(ua_sess);
fad1ed2f
JR
5453 if (!registry) {
5454 DBG("Application session is being torn down. Abort event notify");
5455 ret = 0;
5456 goto error_rcu_unlock;
5457 };
45893984 5458
7972aab2
DG
5459 /* Depending on the buffer type, a different channel key is used. */
5460 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) {
5461 chan_reg_key = ua_chan->tracing_channel_id;
d0b96690 5462 } else {
7972aab2 5463 chan_reg_key = ua_chan->key;
d0b96690
DG
5464 }
5465
7972aab2
DG
5466 pthread_mutex_lock(&registry->lock);
5467
5468 chan_reg = ust_registry_channel_find(registry, chan_reg_key);
5469 assert(chan_reg);
5470
5471 if (!chan_reg->register_done) {
294e218e
MD
5472 /*
5473 * TODO: eventually use the registry event count for
5474 * this channel to better guess header type for per-pid
5475 * buffers.
5476 */
5477 type = USTCTL_CHANNEL_HEADER_LARGE;
7972aab2
DG
5478 chan_reg->nr_ctx_fields = nr_fields;
5479 chan_reg->ctx_fields = fields;
fad1ed2f 5480 fields = NULL;
7972aab2 5481 chan_reg->header_type = type;
d0b96690 5482 } else {
7972aab2
DG
5483 /* Get current already assigned values. */
5484 type = chan_reg->header_type;
d0b96690 5485 }
7972aab2
DG
5486 /* Channel id is set during the object creation. */
5487 chan_id = chan_reg->chan_id;
d0b96690
DG
5488
5489 /* Append to metadata */
7972aab2
DG
5490 if (!chan_reg->metadata_dumped) {
5491 ret_code = ust_metadata_channel_statedump(registry, chan_reg);
d0b96690
DG
5492 if (ret_code) {
5493 ERR("Error appending channel metadata (errno = %d)", ret_code);
5494 goto reply;
5495 }
5496 }
5497
5498reply:
7972aab2
DG
5499 DBG3("UST app replying to register channel key %" PRIu64
5500 " with id %u, type: %d, ret: %d", chan_reg_key, chan_id, type,
5501 ret_code);
d0b96690
DG
5502
5503 ret = ustctl_reply_register_channel(sock, chan_id, type, ret_code);
5504 if (ret < 0) {
5505 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
5506 ERR("UST app reply channel failed with ret %d", ret);
5507 } else {
5508 DBG3("UST app reply channel failed. Application died");
5509 }
5510 goto error;
5511 }
5512
7972aab2
DG
5513 /* This channel registry registration is completed. */
5514 chan_reg->register_done = 1;
5515
d0b96690 5516error:
7972aab2 5517 pthread_mutex_unlock(&registry->lock);
d88aee68 5518error_rcu_unlock:
d0b96690 5519 rcu_read_unlock();
fad1ed2f 5520 free(fields);
d0b96690
DG
5521 return ret;
5522}
5523
d88aee68
DG
5524/*
5525 * Add event to the UST channel registry. When the event is added to the
5526 * registry, the metadata is also created. Once done, this replies to the
5527 * application with the appropriate error code.
5528 *
5529 * The session UST registry lock is acquired in the function.
5530 *
5531 * On success 0 is returned else a negative value.
5532 */
d0b96690 5533static int add_event_ust_registry(int sock, int sobjd, int cobjd, char *name,
2106efa0
PP
5534 char *sig, size_t nr_fields, struct ustctl_field *fields,
5535 int loglevel_value, char *model_emf_uri)
d0b96690
DG
5536{
5537 int ret, ret_code;
5538 uint32_t event_id = 0;
7972aab2 5539 uint64_t chan_reg_key;
d0b96690
DG
5540 struct ust_app *app;
5541 struct ust_app_channel *ua_chan;
5542 struct ust_app_session *ua_sess;
7972aab2 5543 struct ust_registry_session *registry;
d0b96690
DG
5544
5545 rcu_read_lock();
5546
5547 /* Lookup application. If not found, there is a code flow error. */
5548 app = find_app_by_notify_sock(sock);
d88aee68 5549 if (!app) {
fad1ed2f 5550 DBG("Application socket %d is being torn down. Abort event notify",
d88aee68
DG
5551 sock);
5552 ret = 0;
5553 goto error_rcu_unlock;
5554 }
d0b96690 5555
4950b860 5556 /* Lookup channel by UST object descriptor. */
d0b96690 5557 ua_chan = find_channel_by_objd(app, cobjd);
4950b860 5558 if (!ua_chan) {
fad1ed2f 5559 DBG("Application channel is being torn down. Abort event notify");
4950b860
MD
5560 ret = 0;
5561 goto error_rcu_unlock;
5562 }
5563
d0b96690
DG
5564 assert(ua_chan->session);
5565 ua_sess = ua_chan->session;
5566
7972aab2 5567 registry = get_session_registry(ua_sess);
fad1ed2f
JR
5568 if (!registry) {
5569 DBG("Application session is being torn down. Abort event notify");
5570 ret = 0;
5571 goto error_rcu_unlock;
5572 }
7972aab2
DG
5573
5574 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) {
5575 chan_reg_key = ua_chan->tracing_channel_id;
5576 } else {
5577 chan_reg_key = ua_chan->key;
5578 }
5579
5580 pthread_mutex_lock(&registry->lock);
d0b96690 5581
d5d629b5
DG
5582 /*
5583 * From this point on, this call acquires the ownership of the sig, fields
5584 * and model_emf_uri meaning any free are done inside it if needed. These
5585 * three variables MUST NOT be read/write after this.
5586 */
7972aab2 5587 ret_code = ust_registry_create_event(registry, chan_reg_key,
2106efa0
PP
5588 sobjd, cobjd, name, sig, nr_fields, fields,
5589 loglevel_value, model_emf_uri, ua_sess->buffer_type,
5590 &event_id, app);
fad1ed2f
JR
5591 sig = NULL;
5592 fields = NULL;
5593 model_emf_uri = NULL;
d0b96690
DG
5594
5595 /*
5596 * The return value is returned to ustctl so in case of an error, the
5597 * application can be notified. In case of an error, it's important not to
5598 * return a negative error or else the application will get closed.
5599 */
5600 ret = ustctl_reply_register_event(sock, event_id, ret_code);
5601 if (ret < 0) {
5602 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
5603 ERR("UST app reply event failed with ret %d", ret);
5604 } else {
5605 DBG3("UST app reply event failed. Application died");
5606 }
5607 /*
5608 * No need to wipe the create event since the application socket will
5609 * get close on error hence cleaning up everything by itself.
5610 */
5611 goto error;
5612 }
5613
7972aab2
DG
5614 DBG3("UST registry event %s with id %" PRId32 " added successfully",
5615 name, event_id);
d88aee68 5616
d0b96690 5617error:
7972aab2 5618 pthread_mutex_unlock(&registry->lock);
d88aee68 5619error_rcu_unlock:
d0b96690 5620 rcu_read_unlock();
fad1ed2f
JR
5621 free(sig);
5622 free(fields);
5623 free(model_emf_uri);
d0b96690
DG
5624 return ret;
5625}
5626
10b56aef
MD
5627/*
5628 * Add enum to the UST session registry. Once done, this replies to the
5629 * application with the appropriate error code.
5630 *
5631 * The session UST registry lock is acquired within this function.
5632 *
5633 * On success 0 is returned else a negative value.
5634 */
5635static int add_enum_ust_registry(int sock, int sobjd, char *name,
5636 struct ustctl_enum_entry *entries, size_t nr_entries)
5637{
5638 int ret = 0, ret_code;
5639 struct ust_app *app;
5640 struct ust_app_session *ua_sess;
5641 struct ust_registry_session *registry;
5642 uint64_t enum_id = -1ULL;
5643
5644 rcu_read_lock();
5645
5646 /* Lookup application. If not found, there is a code flow error. */
5647 app = find_app_by_notify_sock(sock);
5648 if (!app) {
5649 /* Return an error since this is not an error */
5650 DBG("Application socket %d is being torn down. Aborting enum registration",
5651 sock);
5652 free(entries);
5653 goto error_rcu_unlock;
5654 }
5655
5656 /* Lookup session by UST object descriptor. */
5657 ua_sess = find_session_by_objd(app, sobjd);
5658 if (!ua_sess) {
5659 /* Return an error since this is not an error */
fad1ed2f 5660 DBG("Application session is being torn down (session not found). Aborting enum registration.");
10b56aef
MD
5661 free(entries);
5662 goto error_rcu_unlock;
5663 }
5664
5665 registry = get_session_registry(ua_sess);
fad1ed2f
JR
5666 if (!registry) {
5667 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
5668 free(entries);
5669 goto error_rcu_unlock;
5670 }
10b56aef
MD
5671
5672 pthread_mutex_lock(&registry->lock);
5673
5674 /*
5675 * From this point on, the callee acquires the ownership of
5676 * entries. The variable entries MUST NOT be read/written after
5677 * call.
5678 */
5679 ret_code = ust_registry_create_or_find_enum(registry, sobjd, name,
5680 entries, nr_entries, &enum_id);
5681 entries = NULL;
5682
5683 /*
5684 * The return value is returned to ustctl so in case of an error, the
5685 * application can be notified. In case of an error, it's important not to
5686 * return a negative error or else the application will get closed.
5687 */
5688 ret = ustctl_reply_register_enum(sock, enum_id, ret_code);
5689 if (ret < 0) {
5690 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
5691 ERR("UST app reply enum failed with ret %d", ret);
5692 } else {
5693 DBG3("UST app reply enum failed. Application died");
5694 }
5695 /*
5696 * No need to wipe the create enum since the application socket will
5697 * get close on error hence cleaning up everything by itself.
5698 */
5699 goto error;
5700 }
5701
5702 DBG3("UST registry enum %s added successfully or already found", name);
5703
5704error:
5705 pthread_mutex_unlock(&registry->lock);
5706error_rcu_unlock:
5707 rcu_read_unlock();
5708 return ret;
5709}
5710
d88aee68
DG
5711/*
5712 * Handle application notification through the given notify socket.
5713 *
5714 * Return 0 on success or else a negative value.
5715 */
d0b96690
DG
5716int ust_app_recv_notify(int sock)
5717{
5718 int ret;
5719 enum ustctl_notify_cmd cmd;
5720
5721 DBG3("UST app receiving notify from sock %d", sock);
5722
5723 ret = ustctl_recv_notify(sock, &cmd);
5724 if (ret < 0) {
5725 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
5726 ERR("UST app recv notify failed with ret %d", ret);
5727 } else {
5728 DBG3("UST app recv notify failed. Application died");
5729 }
5730 goto error;
5731 }
5732
5733 switch (cmd) {
5734 case USTCTL_NOTIFY_CMD_EVENT:
5735 {
2106efa0 5736 int sobjd, cobjd, loglevel_value;
d0b96690
DG
5737 char name[LTTNG_UST_SYM_NAME_LEN], *sig, *model_emf_uri;
5738 size_t nr_fields;
5739 struct ustctl_field *fields;
5740
5741 DBG2("UST app ustctl register event received");
5742
2106efa0
PP
5743 ret = ustctl_recv_register_event(sock, &sobjd, &cobjd, name,
5744 &loglevel_value, &sig, &nr_fields, &fields,
5745 &model_emf_uri);
d0b96690
DG
5746 if (ret < 0) {
5747 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
5748 ERR("UST app recv event failed with ret %d", ret);
5749 } else {
5750 DBG3("UST app recv event failed. Application died");
5751 }
5752 goto error;
5753 }
5754
d5d629b5
DG
5755 /*
5756 * Add event to the UST registry coming from the notify socket. This
5757 * call will free if needed the sig, fields and model_emf_uri. This
5758 * code path loses the ownsership of these variables and transfer them
5759 * to the this function.
5760 */
d0b96690 5761 ret = add_event_ust_registry(sock, sobjd, cobjd, name, sig, nr_fields,
2106efa0 5762 fields, loglevel_value, model_emf_uri);
d0b96690
DG
5763 if (ret < 0) {
5764 goto error;
5765 }
5766
5767 break;
5768 }
5769 case USTCTL_NOTIFY_CMD_CHANNEL:
5770 {
5771 int sobjd, cobjd;
5772 size_t nr_fields;
5773 struct ustctl_field *fields;
5774
5775 DBG2("UST app ustctl register channel received");
5776
5777 ret = ustctl_recv_register_channel(sock, &sobjd, &cobjd, &nr_fields,
5778 &fields);
5779 if (ret < 0) {
5780 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
5781 ERR("UST app recv channel failed with ret %d", ret);
5782 } else {
5783 DBG3("UST app recv channel failed. Application died");
5784 }
5785 goto error;
5786 }
5787
d5d629b5
DG
5788 /*
5789 * The fields ownership are transfered to this function call meaning
5790 * that if needed it will be freed. After this, it's invalid to access
5791 * fields or clean it up.
5792 */
8eede835 5793 ret = reply_ust_register_channel(sock, cobjd, nr_fields,
d0b96690
DG
5794 fields);
5795 if (ret < 0) {
5796 goto error;
5797 }
5798
5799 break;
5800 }
10b56aef
MD
5801 case USTCTL_NOTIFY_CMD_ENUM:
5802 {
5803 int sobjd;
5804 char name[LTTNG_UST_SYM_NAME_LEN];
5805 size_t nr_entries;
5806 struct ustctl_enum_entry *entries;
5807
5808 DBG2("UST app ustctl register enum received");
5809
5810 ret = ustctl_recv_register_enum(sock, &sobjd, name,
5811 &entries, &nr_entries);
5812 if (ret < 0) {
5813 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
5814 ERR("UST app recv enum failed with ret %d", ret);
5815 } else {
5816 DBG3("UST app recv enum failed. Application died");
5817 }
5818 goto error;
5819 }
5820
5821 /* Callee assumes ownership of entries */
5822 ret = add_enum_ust_registry(sock, sobjd, name,
5823 entries, nr_entries);
5824 if (ret < 0) {
5825 goto error;
5826 }
5827
5828 break;
5829 }
d0b96690
DG
5830 default:
5831 /* Should NEVER happen. */
5832 assert(0);
5833 }
5834
5835error:
5836 return ret;
5837}
d88aee68
DG
5838
5839/*
5840 * Once the notify socket hangs up, this is called. First, it tries to find the
5841 * corresponding application. On failure, the call_rcu to close the socket is
5842 * executed. If an application is found, it tries to delete it from the notify
5843 * socket hash table. Whathever the result, it proceeds to the call_rcu.
5844 *
5845 * Note that an object needs to be allocated here so on ENOMEM failure, the
5846 * call RCU is not done but the rest of the cleanup is.
5847 */
5848void ust_app_notify_sock_unregister(int sock)
5849{
5850 int err_enomem = 0;
5851 struct lttng_ht_iter iter;
5852 struct ust_app *app;
5853 struct ust_app_notify_sock_obj *obj;
5854
5855 assert(sock >= 0);
5856
5857 rcu_read_lock();
5858
5859 obj = zmalloc(sizeof(*obj));
5860 if (!obj) {
5861 /*
5862 * An ENOMEM is kind of uncool. If this strikes we continue the
5863 * procedure but the call_rcu will not be called. In this case, we
5864 * accept the fd leak rather than possibly creating an unsynchronized
5865 * state between threads.
5866 *
5867 * TODO: The notify object should be created once the notify socket is
5868 * registered and stored independantely from the ust app object. The
5869 * tricky part is to synchronize the teardown of the application and
5870 * this notify object. Let's keep that in mind so we can avoid this
5871 * kind of shenanigans with ENOMEM in the teardown path.
5872 */
5873 err_enomem = 1;
5874 } else {
5875 obj->fd = sock;
5876 }
5877
5878 DBG("UST app notify socket unregister %d", sock);
5879
5880 /*
5881 * Lookup application by notify socket. If this fails, this means that the
5882 * hash table delete has already been done by the application
5883 * unregistration process so we can safely close the notify socket in a
5884 * call RCU.
5885 */
5886 app = find_app_by_notify_sock(sock);
5887 if (!app) {
5888 goto close_socket;
5889 }
5890
5891 iter.iter.node = &app->notify_sock_n.node;
5892
5893 /*
5894 * Whatever happens here either we fail or succeed, in both cases we have
5895 * to close the socket after a grace period to continue to the call RCU
5896 * here. If the deletion is successful, the application is not visible
5897 * anymore by other threads and is it fails it means that it was already
5898 * deleted from the hash table so either way we just have to close the
5899 * socket.
5900 */
5901 (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter);
5902
5903close_socket:
5904 rcu_read_unlock();
5905
5906 /*
5907 * Close socket after a grace period to avoid for the socket to be reused
5908 * before the application object is freed creating potential race between
5909 * threads trying to add unique in the global hash table.
5910 */
5911 if (!err_enomem) {
5912 call_rcu(&obj->head, close_notify_sock_rcu);
5913 }
5914}
f45e313d
DG
5915
5916/*
5917 * Destroy a ust app data structure and free its memory.
5918 */
5919void ust_app_destroy(struct ust_app *app)
5920{
5921 if (!app) {
5922 return;
5923 }
5924
5925 call_rcu(&app->pid_n.head, delete_ust_app_rcu);
5926}
6dc3064a
DG
5927
5928/*
5929 * Take a snapshot for a given UST session. The snapshot is sent to the given
5930 * output.
5931 *
9a654598 5932 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
6dc3064a 5933 */
fb9a95c4
JG
5934enum lttng_error_code ust_app_snapshot_record(
5935 const struct ltt_ust_session *usess,
348a81dc 5936 const struct consumer_output *output, int wait,
d07ceecd 5937 uint64_t nb_packets_per_stream)
6dc3064a
DG
5938{
5939 int ret = 0;
9a654598 5940 enum lttng_error_code status = LTTNG_OK;
6dc3064a
DG
5941 struct lttng_ht_iter iter;
5942 struct ust_app *app;
affce97e 5943 char *trace_path = NULL;
6dc3064a
DG
5944
5945 assert(usess);
5946 assert(output);
5947
5948 rcu_read_lock();
5949
8c924c7b
MD
5950 switch (usess->buffer_type) {
5951 case LTTNG_BUFFER_PER_UID:
5952 {
5953 struct buffer_reg_uid *reg;
6dc3064a 5954
8c924c7b
MD
5955 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
5956 struct buffer_reg_channel *reg_chan;
5957 struct consumer_socket *socket;
3b967712 5958 char pathname[PATH_MAX];
5da88b0f 5959 size_t consumer_path_offset = 0;
6dc3064a 5960
2b269489
JR
5961 if (!reg->registry->reg.ust->metadata_key) {
5962 /* Skip since no metadata is present */
5963 continue;
5964 }
5965
8c924c7b
MD
5966 /* Get consumer socket to use to push the metadata.*/
5967 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
5968 usess->consumer);
5969 if (!socket) {
9a654598 5970 status = LTTNG_ERR_INVALID;
8c924c7b
MD
5971 goto error;
5972 }
6dc3064a 5973
8c924c7b
MD
5974 memset(pathname, 0, sizeof(pathname));
5975 ret = snprintf(pathname, sizeof(pathname),
5da88b0f 5976 DEFAULT_UST_TRACE_DIR "/" DEFAULT_UST_TRACE_UID_PATH,
8c924c7b
MD
5977 reg->uid, reg->bits_per_long);
5978 if (ret < 0) {
5979 PERROR("snprintf snapshot path");
9a654598 5980 status = LTTNG_ERR_INVALID;
8c924c7b
MD
5981 goto error;
5982 }
affce97e
JG
5983 /* Free path allowed on previous iteration. */
5984 free(trace_path);
5da88b0f
MD
5985 trace_path = setup_channel_trace_path(usess->consumer, pathname,
5986 &consumer_path_offset);
3b967712
MD
5987 if (!trace_path) {
5988 status = LTTNG_ERR_INVALID;
5989 goto error;
5990 }
f3db82be 5991 /* Add the UST default trace dir to path. */
8c924c7b
MD
5992 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
5993 reg_chan, node.node) {
9a654598 5994 status = consumer_snapshot_channel(socket,
e098433c
JG
5995 reg_chan->consumer_key,
5996 output, 0, usess->uid,
5da88b0f 5997 usess->gid, &trace_path[consumer_path_offset], wait,
d2956687 5998 nb_packets_per_stream);
9a654598 5999 if (status != LTTNG_OK) {
8c924c7b
MD
6000 goto error;
6001 }
6002 }
9a654598 6003 status = consumer_snapshot_channel(socket,
68808f4e 6004 reg->registry->reg.ust->metadata_key, output, 1,
5da88b0f
MD
6005 usess->uid, usess->gid, &trace_path[consumer_path_offset],
6006 wait, 0);
9a654598 6007 if (status != LTTNG_OK) {
8c924c7b
MD
6008 goto error;
6009 }
af706bb7 6010 }
8c924c7b
MD
6011 break;
6012 }
6013 case LTTNG_BUFFER_PER_PID:
6014 {
6015 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
6016 struct consumer_socket *socket;
6017 struct lttng_ht_iter chan_iter;
6018 struct ust_app_channel *ua_chan;
6019 struct ust_app_session *ua_sess;
6020 struct ust_registry_session *registry;
3b967712 6021 char pathname[PATH_MAX];
5da88b0f 6022 size_t consumer_path_offset = 0;
8c924c7b
MD
6023
6024 ua_sess = lookup_session_by_app(usess, app);
6025 if (!ua_sess) {
6026 /* Session not associated with this app. */
6027 continue;
6028 }
af706bb7 6029
8c924c7b
MD
6030 /* Get the right consumer socket for the application. */
6031 socket = consumer_find_socket_by_bitness(app->bits_per_long,
348a81dc 6032 output);
8c924c7b 6033 if (!socket) {
9a654598 6034 status = LTTNG_ERR_INVALID;
5c786ded
JD
6035 goto error;
6036 }
6037
8c924c7b
MD
6038 /* Add the UST default trace dir to path. */
6039 memset(pathname, 0, sizeof(pathname));
5da88b0f 6040 ret = snprintf(pathname, sizeof(pathname), DEFAULT_UST_TRACE_DIR "/%s",
8c924c7b 6041 ua_sess->path);
6dc3064a 6042 if (ret < 0) {
9a654598 6043 status = LTTNG_ERR_INVALID;
8c924c7b 6044 PERROR("snprintf snapshot path");
6dc3064a
DG
6045 goto error;
6046 }
affce97e
JG
6047 /* Free path allowed on previous iteration. */
6048 free(trace_path);
5da88b0f
MD
6049 trace_path = setup_channel_trace_path(usess->consumer, pathname,
6050 &consumer_path_offset);
3b967712
MD
6051 if (!trace_path) {
6052 status = LTTNG_ERR_INVALID;
6053 goto error;
6054 }
f3db82be 6055 cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter,
8c924c7b 6056 ua_chan, node.node) {
9a654598 6057 status = consumer_snapshot_channel(socket,
470cc211 6058 ua_chan->key, output, 0,
ff588497
JR
6059 lttng_credentials_get_uid(&ua_sess->effective_credentials),
6060 lttng_credentials_get_gid(&ua_sess->effective_credentials),
5da88b0f 6061 &trace_path[consumer_path_offset], wait,
d2956687 6062 nb_packets_per_stream);
9a654598
JG
6063 switch (status) {
6064 case LTTNG_OK:
6065 break;
6066 case LTTNG_ERR_CHAN_NOT_FOUND:
6067 continue;
6068 default:
8c924c7b
MD
6069 goto error;
6070 }
6071 }
6072
6073 registry = get_session_registry(ua_sess);
fad1ed2f 6074 if (!registry) {
9bbfb88c
MD
6075 DBG("Application session is being torn down. Skip application.");
6076 continue;
fad1ed2f 6077 }
9a654598 6078 status = consumer_snapshot_channel(socket,
470cc211 6079 registry->metadata_key, output, 1,
ff588497
JR
6080 lttng_credentials_get_uid(&ua_sess->effective_credentials),
6081 lttng_credentials_get_gid(&ua_sess->effective_credentials),
5da88b0f 6082 &trace_path[consumer_path_offset], wait, 0);
9a654598
JG
6083 switch (status) {
6084 case LTTNG_OK:
6085 break;
6086 case LTTNG_ERR_CHAN_NOT_FOUND:
6087 continue;
6088 default:
8c924c7b
MD
6089 goto error;
6090 }
6091 }
6092 break;
6093 }
6094 default:
6095 assert(0);
6096 break;
6dc3064a
DG
6097 }
6098
6099error:
affce97e 6100 free(trace_path);
6dc3064a 6101 rcu_read_unlock();
9a654598 6102 return status;
6dc3064a 6103}
5c786ded
JD
6104
6105/*
d07ceecd 6106 * Return the size taken by one more packet per stream.
5c786ded 6107 */
fb9a95c4
JG
6108uint64_t ust_app_get_size_one_more_packet_per_stream(
6109 const struct ltt_ust_session *usess, uint64_t cur_nr_packets)
5c786ded 6110{
d07ceecd 6111 uint64_t tot_size = 0;
5c786ded
JD
6112 struct ust_app *app;
6113 struct lttng_ht_iter iter;
6114
6115 assert(usess);
6116
6117 switch (usess->buffer_type) {
6118 case LTTNG_BUFFER_PER_UID:
6119 {
6120 struct buffer_reg_uid *reg;
6121
6122 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
6123 struct buffer_reg_channel *reg_chan;
6124
b7064eaa 6125 rcu_read_lock();
5c786ded
JD
6126 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
6127 reg_chan, node.node) {
d07ceecd
MD
6128 if (cur_nr_packets >= reg_chan->num_subbuf) {
6129 /*
6130 * Don't take channel into account if we
6131 * already grab all its packets.
6132 */
6133 continue;
6134 }
6135 tot_size += reg_chan->subbuf_size * reg_chan->stream_count;
5c786ded 6136 }
b7064eaa 6137 rcu_read_unlock();
5c786ded
JD
6138 }
6139 break;
6140 }
6141 case LTTNG_BUFFER_PER_PID:
6142 {
6143 rcu_read_lock();
6144 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
6145 struct ust_app_channel *ua_chan;
6146 struct ust_app_session *ua_sess;
6147 struct lttng_ht_iter chan_iter;
6148
6149 ua_sess = lookup_session_by_app(usess, app);
6150 if (!ua_sess) {
6151 /* Session not associated with this app. */
6152 continue;
6153 }
6154
6155 cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter,
6156 ua_chan, node.node) {
d07ceecd
MD
6157 if (cur_nr_packets >= ua_chan->attr.num_subbuf) {
6158 /*
6159 * Don't take channel into account if we
6160 * already grab all its packets.
6161 */
6162 continue;
6163 }
6164 tot_size += ua_chan->attr.subbuf_size * ua_chan->streams.count;
5c786ded
JD
6165 }
6166 }
6167 rcu_read_unlock();
6168 break;
6169 }
6170 default:
6171 assert(0);
6172 break;
6173 }
6174
d07ceecd 6175 return tot_size;
5c786ded 6176}
fb83fe64
JD
6177
6178int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id,
6179 struct cds_list_head *buffer_reg_uid_list,
6180 struct consumer_output *consumer, uint64_t uchan_id,
6181 int overwrite, uint64_t *discarded, uint64_t *lost)
6182{
6183 int ret;
6184 uint64_t consumer_chan_key;
6185
70dd8162
MD
6186 *discarded = 0;
6187 *lost = 0;
6188
fb83fe64 6189 ret = buffer_reg_uid_consumer_channel_key(
76604852 6190 buffer_reg_uid_list, uchan_id, &consumer_chan_key);
fb83fe64 6191 if (ret < 0) {
70dd8162
MD
6192 /* Not found */
6193 ret = 0;
fb83fe64
JD
6194 goto end;
6195 }
6196
6197 if (overwrite) {
6198 ret = consumer_get_lost_packets(ust_session_id,
6199 consumer_chan_key, consumer, lost);
6200 } else {
6201 ret = consumer_get_discarded_events(ust_session_id,
6202 consumer_chan_key, consumer, discarded);
6203 }
6204
6205end:
6206 return ret;
6207}
6208
6209int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session *usess,
6210 struct ltt_ust_channel *uchan,
6211 struct consumer_output *consumer, int overwrite,
6212 uint64_t *discarded, uint64_t *lost)
6213{
6214 int ret = 0;
6215 struct lttng_ht_iter iter;
6216 struct lttng_ht_node_str *ua_chan_node;
6217 struct ust_app *app;
6218 struct ust_app_session *ua_sess;
6219 struct ust_app_channel *ua_chan;
6220
70dd8162
MD
6221 *discarded = 0;
6222 *lost = 0;
6223
fb83fe64
JD
6224 rcu_read_lock();
6225 /*
70dd8162
MD
6226 * Iterate over every registered applications. Sum counters for
6227 * all applications containing requested session and channel.
fb83fe64
JD
6228 */
6229 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
6230 struct lttng_ht_iter uiter;
6231
6232 ua_sess = lookup_session_by_app(usess, app);
6233 if (ua_sess == NULL) {
6234 continue;
6235 }
6236
6237 /* Get channel */
ee022399 6238 lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &uiter);
fb83fe64
JD
6239 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
6240 /* If the session is found for the app, the channel must be there */
6241 assert(ua_chan_node);
6242
6243 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
6244
6245 if (overwrite) {
70dd8162
MD
6246 uint64_t _lost;
6247
fb83fe64 6248 ret = consumer_get_lost_packets(usess->id, ua_chan->key,
70dd8162
MD
6249 consumer, &_lost);
6250 if (ret < 0) {
6251 break;
6252 }
6253 (*lost) += _lost;
fb83fe64 6254 } else {
70dd8162
MD
6255 uint64_t _discarded;
6256
fb83fe64 6257 ret = consumer_get_discarded_events(usess->id,
70dd8162
MD
6258 ua_chan->key, consumer, &_discarded);
6259 if (ret < 0) {
6260 break;
6261 }
6262 (*discarded) += _discarded;
fb83fe64 6263 }
fb83fe64
JD
6264 }
6265
fb83fe64
JD
6266 rcu_read_unlock();
6267 return ret;
6268}
c2561365
JD
6269
6270static
6271int ust_app_regenerate_statedump(struct ltt_ust_session *usess,
6272 struct ust_app *app)
6273{
6274 int ret = 0;
6275 struct ust_app_session *ua_sess;
6276
6277 DBG("Regenerating the metadata for ust app pid %d", app->pid);
6278
6279 rcu_read_lock();
6280
6281 ua_sess = lookup_session_by_app(usess, app);
6282 if (ua_sess == NULL) {
6283 /* The session is in teardown process. Ignore and continue. */
6284 goto end;
6285 }
6286
6287 pthread_mutex_lock(&ua_sess->lock);
6288
6289 if (ua_sess->deleted) {
6290 goto end_unlock;
6291 }
6292
6293 pthread_mutex_lock(&app->sock_lock);
6294 ret = ustctl_regenerate_statedump(app->sock, ua_sess->handle);
6295 pthread_mutex_unlock(&app->sock_lock);
6296
6297end_unlock:
6298 pthread_mutex_unlock(&ua_sess->lock);
6299
6300end:
6301 rcu_read_unlock();
6302 health_code_update();
6303 return ret;
6304}
6305
6306/*
6307 * Regenerate the statedump for each app in the session.
6308 */
6309int ust_app_regenerate_statedump_all(struct ltt_ust_session *usess)
6310{
6311 int ret = 0;
6312 struct lttng_ht_iter iter;
6313 struct ust_app *app;
6314
6315 DBG("Regenerating the metadata for all UST apps");
6316
6317 rcu_read_lock();
6318
6319 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
6320 if (!app->compatible) {
6321 continue;
6322 }
6323
6324 ret = ust_app_regenerate_statedump(usess, app);
6325 if (ret < 0) {
6326 /* Continue to the next app even on error */
6327 continue;
6328 }
6329 }
6330
6331 rcu_read_unlock();
6332
6333 return 0;
6334}
5c408ad8
JD
6335
6336/*
6337 * Rotate all the channels of a session.
6338 *
6f6d3b69 6339 * Return LTTNG_OK on success or else an LTTng error code.
5c408ad8 6340 */
6f6d3b69 6341enum lttng_error_code ust_app_rotate_session(struct ltt_session *session)
5c408ad8 6342{
6f6d3b69
MD
6343 int ret;
6344 enum lttng_error_code cmd_ret = LTTNG_OK;
5c408ad8
JD
6345 struct lttng_ht_iter iter;
6346 struct ust_app *app;
6347 struct ltt_ust_session *usess = session->ust_session;
5c408ad8
JD
6348
6349 assert(usess);
6350
6351 rcu_read_lock();
6352
6353 switch (usess->buffer_type) {
6354 case LTTNG_BUFFER_PER_UID:
6355 {
6356 struct buffer_reg_uid *reg;
6357
6358 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
6359 struct buffer_reg_channel *reg_chan;
6360 struct consumer_socket *socket;
6361
14d3fca9
JR
6362 if (!reg->registry->reg.ust->metadata_key) {
6363 /* Skip since no metadata is present */
6364 continue;
6365 }
6366
5c408ad8
JD
6367 /* Get consumer socket to use to push the metadata.*/
6368 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
6369 usess->consumer);
6370 if (!socket) {
6f6d3b69 6371 cmd_ret = LTTNG_ERR_INVALID;
5c408ad8
JD
6372 goto error;
6373 }
6374
5c408ad8
JD
6375 /* Rotate the data channels. */
6376 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
6377 reg_chan, node.node) {
5c408ad8
JD
6378 ret = consumer_rotate_channel(socket,
6379 reg_chan->consumer_key,
6380 usess->uid, usess->gid,
d2956687
JG
6381 usess->consumer,
6382 /* is_metadata_channel */ false);
5c408ad8 6383 if (ret < 0) {
6f6d3b69 6384 cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
5c408ad8
JD
6385 goto error;
6386 }
6387 }
6388
6389 (void) push_metadata(reg->registry->reg.ust, usess->consumer);
6390
6391 ret = consumer_rotate_channel(socket,
6392 reg->registry->reg.ust->metadata_key,
6393 usess->uid, usess->gid,
d2956687
JG
6394 usess->consumer,
6395 /* is_metadata_channel */ true);
5c408ad8 6396 if (ret < 0) {
6f6d3b69 6397 cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
5c408ad8
JD
6398 goto error;
6399 }
5c408ad8
JD
6400 }
6401 break;
6402 }
6403 case LTTNG_BUFFER_PER_PID:
6404 {
6405 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
6406 struct consumer_socket *socket;
6407 struct lttng_ht_iter chan_iter;
6408 struct ust_app_channel *ua_chan;
6409 struct ust_app_session *ua_sess;
6410 struct ust_registry_session *registry;
6411
6412 ua_sess = lookup_session_by_app(usess, app);
6413 if (!ua_sess) {
6414 /* Session not associated with this app. */
6415 continue;
6416 }
5c408ad8
JD
6417
6418 /* Get the right consumer socket for the application. */
6419 socket = consumer_find_socket_by_bitness(app->bits_per_long,
6420 usess->consumer);
6421 if (!socket) {
6f6d3b69 6422 cmd_ret = LTTNG_ERR_INVALID;
5c408ad8
JD
6423 goto error;
6424 }
6425
6426 registry = get_session_registry(ua_sess);
6427 if (!registry) {
6f6d3b69
MD
6428 DBG("Application session is being torn down. Skip application.");
6429 continue;
5c408ad8
JD
6430 }
6431
5c408ad8
JD
6432 /* Rotate the data channels. */
6433 cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter,
6434 ua_chan, node.node) {
470cc211
JG
6435 ret = consumer_rotate_channel(socket,
6436 ua_chan->key,
ff588497
JR
6437 lttng_credentials_get_uid(&ua_sess->effective_credentials),
6438 lttng_credentials_get_gid(&ua_sess->effective_credentials),
d2956687
JG
6439 ua_sess->consumer,
6440 /* is_metadata_channel */ false);
5c408ad8 6441 if (ret < 0) {
6f6d3b69
MD
6442 /* Per-PID buffer and application going away. */
6443 if (ret == -LTTNG_ERR_CHAN_NOT_FOUND)
6444 continue;
6445 cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
5c408ad8
JD
6446 goto error;
6447 }
6448 }
6449
6450 /* Rotate the metadata channel. */
6451 (void) push_metadata(registry, usess->consumer);
470cc211
JG
6452 ret = consumer_rotate_channel(socket,
6453 registry->metadata_key,
ff588497
JR
6454 lttng_credentials_get_uid(&ua_sess->effective_credentials),
6455 lttng_credentials_get_gid(&ua_sess->effective_credentials),
d2956687
JG
6456 ua_sess->consumer,
6457 /* is_metadata_channel */ true);
5c408ad8 6458 if (ret < 0) {
6f6d3b69
MD
6459 /* Per-PID buffer and application going away. */
6460 if (ret == -LTTNG_ERR_CHAN_NOT_FOUND)
6461 continue;
6462 cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
5c408ad8
JD
6463 goto error;
6464 }
5c408ad8
JD
6465 }
6466 break;
6467 }
6468 default:
6469 assert(0);
6470 break;
6471 }
6472
6f6d3b69 6473 cmd_ret = LTTNG_OK;
5c408ad8
JD
6474
6475error:
6476 rcu_read_unlock();
6f6d3b69 6477 return cmd_ret;
5c408ad8 6478}
d2956687
JG
6479
6480enum lttng_error_code ust_app_create_channel_subdirectories(
6481 const struct ltt_ust_session *usess)
6482{
6483 enum lttng_error_code ret = LTTNG_OK;
6484 struct lttng_ht_iter iter;
6485 enum lttng_trace_chunk_status chunk_status;
6486 char *pathname_index;
6487 int fmt_ret;
6488
6489 assert(usess->current_trace_chunk);
6490 rcu_read_lock();
6491
6492 switch (usess->buffer_type) {
6493 case LTTNG_BUFFER_PER_UID:
6494 {
6495 struct buffer_reg_uid *reg;
6496
6497 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
6498 fmt_ret = asprintf(&pathname_index,
5da88b0f 6499 DEFAULT_UST_TRACE_DIR "/" DEFAULT_UST_TRACE_UID_PATH "/" DEFAULT_INDEX_DIR,
d2956687
JG
6500 reg->uid, reg->bits_per_long);
6501 if (fmt_ret < 0) {
6502 ERR("Failed to format channel index directory");
6503 ret = LTTNG_ERR_CREATE_DIR_FAIL;
6504 goto error;
6505 }
6506
6507 /*
6508 * Create the index subdirectory which will take care
6509 * of implicitly creating the channel's path.
6510 */
6511 chunk_status = lttng_trace_chunk_create_subdirectory(
6512 usess->current_trace_chunk,
6513 pathname_index);
6514 free(pathname_index);
6515 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
6516 ret = LTTNG_ERR_CREATE_DIR_FAIL;
6517 goto error;
6518 }
6519 }
6520 break;
6521 }
6522 case LTTNG_BUFFER_PER_PID:
6523 {
6524 struct ust_app *app;
6525
495dece5
MD
6526 /*
6527 * Create the toplevel ust/ directory in case no apps are running.
6528 */
6529 chunk_status = lttng_trace_chunk_create_subdirectory(
6530 usess->current_trace_chunk,
6531 DEFAULT_UST_TRACE_DIR);
6532 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
6533 ret = LTTNG_ERR_CREATE_DIR_FAIL;
6534 goto error;
6535 }
6536
d2956687
JG
6537 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app,
6538 pid_n.node) {
6539 struct ust_app_session *ua_sess;
6540 struct ust_registry_session *registry;
6541
6542 ua_sess = lookup_session_by_app(usess, app);
6543 if (!ua_sess) {
6544 /* Session not associated with this app. */
6545 continue;
6546 }
6547
6548 registry = get_session_registry(ua_sess);
6549 if (!registry) {
6550 DBG("Application session is being torn down. Skip application.");
6551 continue;
6552 }
6553
6554 fmt_ret = asprintf(&pathname_index,
5da88b0f 6555 DEFAULT_UST_TRACE_DIR "/%s/" DEFAULT_INDEX_DIR,
d2956687
JG
6556 ua_sess->path);
6557 if (fmt_ret < 0) {
6558 ERR("Failed to format channel index directory");
6559 ret = LTTNG_ERR_CREATE_DIR_FAIL;
6560 goto error;
6561 }
6562 /*
6563 * Create the index subdirectory which will take care
6564 * of implicitly creating the channel's path.
6565 */
6566 chunk_status = lttng_trace_chunk_create_subdirectory(
6567 usess->current_trace_chunk,
6568 pathname_index);
6569 free(pathname_index);
6570 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
6571 ret = LTTNG_ERR_CREATE_DIR_FAIL;
6572 goto error;
6573 }
6574 }
6575 break;
6576 }
6577 default:
6578 abort();
6579 }
6580
6581 ret = LTTNG_OK;
6582error:
6583 rcu_read_unlock();
6584 return ret;
6585}
4a9b9759
MD
6586
6587/*
6588 * Clear all the channels of a session.
6589 *
6590 * Return LTTNG_OK on success or else an LTTng error code.
6591 */
6592enum lttng_error_code ust_app_clear_session(struct ltt_session *session)
6593{
6594 int ret;
6595 enum lttng_error_code cmd_ret = LTTNG_OK;
6596 struct lttng_ht_iter iter;
6597 struct ust_app *app;
6598 struct ltt_ust_session *usess = session->ust_session;
6599
6600 assert(usess);
6601
6602 rcu_read_lock();
6603
6604 if (usess->active) {
6605 ERR("Expecting inactive session %s (%" PRIu64 ")", session->name, session->id);
6606 cmd_ret = LTTNG_ERR_FATAL;
6607 goto end;
6608 }
6609
6610 switch (usess->buffer_type) {
6611 case LTTNG_BUFFER_PER_UID:
6612 {
6613 struct buffer_reg_uid *reg;
6614
6615 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
6616 struct buffer_reg_channel *reg_chan;
6617 struct consumer_socket *socket;
6618
6619 /* Get consumer socket to use to push the metadata.*/
6620 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
6621 usess->consumer);
6622 if (!socket) {
6623 cmd_ret = LTTNG_ERR_INVALID;
6624 goto error_socket;
6625 }
6626
6627 /* Clear the data channels. */
6628 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
6629 reg_chan, node.node) {
6630 ret = consumer_clear_channel(socket,
6631 reg_chan->consumer_key);
6632 if (ret < 0) {
6633 goto error;
6634 }
6635 }
6636
6637 (void) push_metadata(reg->registry->reg.ust, usess->consumer);
6638
6639 /*
6640 * Clear the metadata channel.
6641 * Metadata channel is not cleared per se but we still need to
6642 * perform a rotation operation on it behind the scene.
6643 */
6644 ret = consumer_clear_channel(socket,
6645 reg->registry->reg.ust->metadata_key);
6646 if (ret < 0) {
6647 goto error;
6648 }
6649 }
6650 break;
6651 }
6652 case LTTNG_BUFFER_PER_PID:
6653 {
6654 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
6655 struct consumer_socket *socket;
6656 struct lttng_ht_iter chan_iter;
6657 struct ust_app_channel *ua_chan;
6658 struct ust_app_session *ua_sess;
6659 struct ust_registry_session *registry;
6660
6661 ua_sess = lookup_session_by_app(usess, app);
6662 if (!ua_sess) {
6663 /* Session not associated with this app. */
6664 continue;
6665 }
6666
6667 /* Get the right consumer socket for the application. */
6668 socket = consumer_find_socket_by_bitness(app->bits_per_long,
6669 usess->consumer);
6670 if (!socket) {
6671 cmd_ret = LTTNG_ERR_INVALID;
6672 goto error_socket;
6673 }
6674
6675 registry = get_session_registry(ua_sess);
6676 if (!registry) {
6677 DBG("Application session is being torn down. Skip application.");
6678 continue;
6679 }
6680
6681 /* Clear the data channels. */
6682 cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter,
6683 ua_chan, node.node) {
6684 ret = consumer_clear_channel(socket, ua_chan->key);
6685 if (ret < 0) {
6686 /* Per-PID buffer and application going away. */
6687 if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) {
6688 continue;
6689 }
6690 goto error;
6691 }
6692 }
6693
6694 (void) push_metadata(registry, usess->consumer);
6695
6696 /*
6697 * Clear the metadata channel.
6698 * Metadata channel is not cleared per se but we still need to
6699 * perform rotation operation on it behind the scene.
6700 */
6701 ret = consumer_clear_channel(socket, registry->metadata_key);
6702 if (ret < 0) {
6703 /* Per-PID buffer and application going away. */
6704 if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) {
6705 continue;
6706 }
6707 goto error;
6708 }
6709 }
6710 break;
6711 }
6712 default:
6713 assert(0);
6714 break;
6715 }
6716
6717 cmd_ret = LTTNG_OK;
6718 goto end;
6719
6720error:
6721 switch (-ret) {
6722 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED:
6723 cmd_ret = LTTNG_ERR_CLEAR_RELAY_DISALLOWED;
6724 break;
6725 default:
6726 cmd_ret = LTTNG_ERR_CLEAR_FAIL_CONSUMER;
6727 }
6728
6729error_socket:
6730end:
6731 rcu_read_unlock();
6732 return cmd_ret;
6733}
04ed9e10
JG
6734
6735/*
6736 * This function skips the metadata channel as the begin/end timestamps of a
6737 * metadata packet are useless.
6738 *
6739 * Moreover, opening a packet after a "clear" will cause problems for live
6740 * sessions as it will introduce padding that was not part of the first trace
6741 * chunk. The relay daemon expects the content of the metadata stream of
6742 * successive metadata trace chunks to be strict supersets of one another.
6743 *
6744 * For example, flushing a packet at the beginning of the metadata stream of
6745 * a trace chunk resulting from a "clear" session command will cause the
6746 * size of the metadata stream of the new trace chunk to not match the size of
6747 * the metadata stream of the original chunk. This will confuse the relay
6748 * daemon as the same "offset" in a metadata stream will no longer point
6749 * to the same content.
6750 */
6751enum lttng_error_code ust_app_open_packets(struct ltt_session *session)
6752{
6753 enum lttng_error_code ret = LTTNG_OK;
6754 struct lttng_ht_iter iter;
6755 struct ltt_ust_session *usess = session->ust_session;
6756
6757 assert(usess);
6758
6759 rcu_read_lock();
6760
6761 switch (usess->buffer_type) {
6762 case LTTNG_BUFFER_PER_UID:
6763 {
6764 struct buffer_reg_uid *reg;
6765
6766 cds_list_for_each_entry (
6767 reg, &usess->buffer_reg_uid_list, lnode) {
6768 struct buffer_reg_channel *reg_chan;
6769 struct consumer_socket *socket;
6770
6771 socket = consumer_find_socket_by_bitness(
6772 reg->bits_per_long, usess->consumer);
6773 if (!socket) {
6774 ret = LTTNG_ERR_FATAL;
6775 goto error;
6776 }
6777
6778 cds_lfht_for_each_entry(reg->registry->channels->ht,
6779 &iter.iter, reg_chan, node.node) {
6780 const int open_ret =
6781 consumer_open_channel_packets(
6782 socket,
6783 reg_chan->consumer_key);
6784
6785 if (open_ret < 0) {
6786 ret = LTTNG_ERR_UNK;
6787 goto error;
6788 }
6789 }
6790 }
6791 break;
6792 }
6793 case LTTNG_BUFFER_PER_PID:
6794 {
6795 struct ust_app *app;
6796
6797 cds_lfht_for_each_entry (
6798 ust_app_ht->ht, &iter.iter, app, pid_n.node) {
6799 struct consumer_socket *socket;
6800 struct lttng_ht_iter chan_iter;
6801 struct ust_app_channel *ua_chan;
6802 struct ust_app_session *ua_sess;
6803 struct ust_registry_session *registry;
6804
6805 ua_sess = lookup_session_by_app(usess, app);
6806 if (!ua_sess) {
6807 /* Session not associated with this app. */
6808 continue;
6809 }
6810
6811 /* Get the right consumer socket for the application. */
6812 socket = consumer_find_socket_by_bitness(
6813 app->bits_per_long, usess->consumer);
6814 if (!socket) {
6815 ret = LTTNG_ERR_FATAL;
6816 goto error;
6817 }
6818
6819 registry = get_session_registry(ua_sess);
6820 if (!registry) {
6821 DBG("Application session is being torn down. Skip application.");
6822 continue;
6823 }
6824
6825 cds_lfht_for_each_entry(ua_sess->channels->ht,
6826 &chan_iter.iter, ua_chan, node.node) {
6827 const int open_ret =
6828 consumer_open_channel_packets(
6829 socket,
6830 ua_chan->key);
6831
6832 if (open_ret < 0) {
6833 /*
6834 * Per-PID buffer and application going
6835 * away.
6836 */
97a171e1 6837 if (open_ret == -LTTNG_ERR_CHAN_NOT_FOUND) {
04ed9e10
JG
6838 continue;
6839 }
6840
6841 ret = LTTNG_ERR_UNK;
6842 goto error;
6843 }
6844 }
6845 }
6846 break;
6847 }
6848 default:
6849 abort();
6850 break;
6851 }
6852
6853error:
6854 rcu_read_unlock();
6855 return ret;
6856}
This page took 0.50347 seconds and 4 git commands to generate.