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 *