Save registration time for app
[lttng-tools.git] / src / bin / lttng-sessiond / ust-app.c
CommitLineData
91d76f53
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
bdf64013 3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
91d76f53 4 *
d14d33bf
AM
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
91d76f53
DG
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
d14d33bf
AM
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
91d76f53
DG
17 */
18
6c1c0768 19#define _LGPL_SOURCE
91d76f53 20#include <errno.h>
7972aab2 21#include <inttypes.h>
91d76f53
DG
22#include <pthread.h>
23#include <stdio.h>
24#include <stdlib.h>
099e26bd 25#include <string.h>
aba8e916
DG
26#include <sys/stat.h>
27#include <sys/types.h>
099e26bd 28#include <unistd.h>
0df502fd 29#include <urcu/compiler.h>
fb54cdbf 30#include <lttng/ust-error.h>
331744e3 31#include <signal.h>
bec39940 32
990570ed 33#include <common/common.h>
86acf0da 34#include <common/sessiond-comm/sessiond-comm.h>
1e307fab 35
7972aab2 36#include "buffer-registry.h"
86acf0da 37#include "fd-limit.h"
8782cc74 38#include "health-sessiond.h"
56fff090 39#include "ust-app.h"
48842b30 40#include "ust-consumer.h"
d80a6244 41#include "ust-ctl.h"
0b2dc8df 42#include "utils.h"
fb83fe64 43#include "session.h"
e9404c27
JG
44#include "lttng-sessiond.h"
45#include "notification-thread-commands.h"
5c408ad8 46#include "rotate.h"
d80a6244 47
c4b88406
MD
48static
49int ust_app_flush_app_session(struct ust_app *app, struct ust_app_session *ua_sess);
50
d9bf3ca4
MD
51/* Next available channel key. Access under next_channel_key_lock. */
52static uint64_t _next_channel_key;
53static pthread_mutex_t next_channel_key_lock = PTHREAD_MUTEX_INITIALIZER;
54
55/* Next available session ID. Access under next_session_id_lock. */
56static uint64_t _next_session_id;
57static pthread_mutex_t next_session_id_lock = PTHREAD_MUTEX_INITIALIZER;
ffe60014
DG
58
59/*
d9bf3ca4 60 * Return the incremented value of next_channel_key.
ffe60014 61 */
d9bf3ca4 62static uint64_t get_next_channel_key(void)
ffe60014 63{
d9bf3ca4
MD
64 uint64_t ret;
65
66 pthread_mutex_lock(&next_channel_key_lock);
67 ret = ++_next_channel_key;
68 pthread_mutex_unlock(&next_channel_key_lock);
69 return ret;
ffe60014
DG
70}
71
72/*
7972aab2 73 * Return the atomically incremented value of next_session_id.
ffe60014 74 */
d9bf3ca4 75static uint64_t get_next_session_id(void)
ffe60014 76{
d9bf3ca4
MD
77 uint64_t ret;
78
79 pthread_mutex_lock(&next_session_id_lock);
80 ret = ++_next_session_id;
81 pthread_mutex_unlock(&next_session_id_lock);
82 return ret;
ffe60014
DG
83}
84
d65d2de8
DG
85static void copy_channel_attr_to_ustctl(
86 struct ustctl_consumer_channel_attr *attr,
87 struct lttng_ust_channel_attr *uattr)
88{
89 /* Copy event attributes since the layout is different. */
90 attr->subbuf_size = uattr->subbuf_size;
91 attr->num_subbuf = uattr->num_subbuf;
92 attr->overwrite = uattr->overwrite;
93 attr->switch_timer_interval = uattr->switch_timer_interval;
94 attr->read_timer_interval = uattr->read_timer_interval;
95 attr->output = uattr->output;
491d1539 96 attr->blocking_timeout = uattr->u.s.blocking_timeout;
d65d2de8
DG
97}
98
025faf73
DG
99/*
100 * Match function for the hash table lookup.
101 *
102 * It matches an ust app event based on three attributes which are the event
103 * name, the filter bytecode and the loglevel.
104 */
18eace3b
DG
105static int ht_match_ust_app_event(struct cds_lfht_node *node, const void *_key)
106{
107 struct ust_app_event *event;
108 const struct ust_app_ht_key *key;
2106efa0 109 int ev_loglevel_value;
18eace3b
DG
110
111 assert(node);
112 assert(_key);
113
114 event = caa_container_of(node, struct ust_app_event, node.node);
115 key = _key;
2106efa0 116 ev_loglevel_value = event->attr.loglevel;
18eace3b 117
1af53eb5 118 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
18eace3b
DG
119
120 /* Event name */
121 if (strncmp(event->attr.name, key->name, sizeof(event->attr.name)) != 0) {
122 goto no_match;
123 }
124
125 /* Event loglevel. */
2106efa0 126 if (ev_loglevel_value != key->loglevel_type) {
025faf73 127 if (event->attr.loglevel_type == LTTNG_UST_LOGLEVEL_ALL
2106efa0
PP
128 && key->loglevel_type == 0 &&
129 ev_loglevel_value == -1) {
025faf73
DG
130 /*
131 * Match is accepted. This is because on event creation, the
132 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
133 * -1 are accepted for this loglevel type since 0 is the one set by
134 * the API when receiving an enable event.
135 */
136 } else {
137 goto no_match;
138 }
18eace3b
DG
139 }
140
141 /* One of the filters is NULL, fail. */
142 if ((key->filter && !event->filter) || (!key->filter && event->filter)) {
143 goto no_match;
144 }
145
025faf73
DG
146 if (key->filter && event->filter) {
147 /* Both filters exists, check length followed by the bytecode. */
148 if (event->filter->len != key->filter->len ||
149 memcmp(event->filter->data, key->filter->data,
150 event->filter->len) != 0) {
151 goto no_match;
152 }
18eace3b
DG
153 }
154
1af53eb5
JI
155 /* One of the exclusions is NULL, fail. */
156 if ((key->exclusion && !event->exclusion) || (!key->exclusion && event->exclusion)) {
157 goto no_match;
158 }
159
160 if (key->exclusion && event->exclusion) {
161 /* Both exclusions exists, check count followed by the names. */
162 if (event->exclusion->count != key->exclusion->count ||
163 memcmp(event->exclusion->names, key->exclusion->names,
164 event->exclusion->count * LTTNG_UST_SYM_NAME_LEN) != 0) {
165 goto no_match;
166 }
167 }
168
169
025faf73 170 /* Match. */
18eace3b
DG
171 return 1;
172
173no_match:
174 return 0;
18eace3b
DG
175}
176
025faf73
DG
177/*
178 * Unique add of an ust app event in the given ht. This uses the custom
179 * ht_match_ust_app_event match function and the event name as hash.
180 */
d0b96690 181static void add_unique_ust_app_event(struct ust_app_channel *ua_chan,
18eace3b
DG
182 struct ust_app_event *event)
183{
184 struct cds_lfht_node *node_ptr;
185 struct ust_app_ht_key key;
d0b96690 186 struct lttng_ht *ht;
18eace3b 187
d0b96690
DG
188 assert(ua_chan);
189 assert(ua_chan->events);
18eace3b
DG
190 assert(event);
191
d0b96690 192 ht = ua_chan->events;
18eace3b
DG
193 key.name = event->attr.name;
194 key.filter = event->filter;
2106efa0 195 key.loglevel_type = event->attr.loglevel;
91c89f23 196 key.exclusion = event->exclusion;
18eace3b
DG
197
198 node_ptr = cds_lfht_add_unique(ht->ht,
199 ht->hash_fct(event->node.key, lttng_ht_seed),
200 ht_match_ust_app_event, &key, &event->node.node);
201 assert(node_ptr == &event->node.node);
202}
203
d88aee68
DG
204/*
205 * Close the notify socket from the given RCU head object. This MUST be called
206 * through a call_rcu().
207 */
208static void close_notify_sock_rcu(struct rcu_head *head)
209{
210 int ret;
211 struct ust_app_notify_sock_obj *obj =
212 caa_container_of(head, struct ust_app_notify_sock_obj, head);
213
214 /* Must have a valid fd here. */
215 assert(obj->fd >= 0);
216
217 ret = close(obj->fd);
218 if (ret) {
219 ERR("close notify sock %d RCU", obj->fd);
220 }
221 lttng_fd_put(LTTNG_FD_APPS, 1);
222
223 free(obj);
224}
225
7972aab2
DG
226/*
227 * Return the session registry according to the buffer type of the given
228 * session.
229 *
230 * A registry per UID object MUST exists before calling this function or else
231 * it assert() if not found. RCU read side lock must be acquired.
232 */
233static struct ust_registry_session *get_session_registry(
234 struct ust_app_session *ua_sess)
235{
236 struct ust_registry_session *registry = NULL;
237
238 assert(ua_sess);
239
240 switch (ua_sess->buffer_type) {
241 case LTTNG_BUFFER_PER_PID:
242 {
243 struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id);
244 if (!reg_pid) {
245 goto error;
246 }
247 registry = reg_pid->registry->reg.ust;
248 break;
249 }
250 case LTTNG_BUFFER_PER_UID:
251 {
252 struct buffer_reg_uid *reg_uid = buffer_reg_uid_find(
470cc211
JG
253 ua_sess->tracing_id, ua_sess->bits_per_long,
254 ua_sess->real_credentials.uid);
7972aab2
DG
255 if (!reg_uid) {
256 goto error;
257 }
258 registry = reg_uid->registry->reg.ust;
259 break;
260 }
261 default:
262 assert(0);
263 };
264
265error:
266 return registry;
267}
268
55cc08a6
DG
269/*
270 * Delete ust context safely. RCU read lock must be held before calling
271 * this function.
272 */
273static
fb45065e
MD
274void delete_ust_app_ctx(int sock, struct ust_app_ctx *ua_ctx,
275 struct ust_app *app)
55cc08a6 276{
ffe60014
DG
277 int ret;
278
279 assert(ua_ctx);
280
55cc08a6 281 if (ua_ctx->obj) {
fb45065e 282 pthread_mutex_lock(&app->sock_lock);
ffe60014 283 ret = ustctl_release_object(sock, ua_ctx->obj);
fb45065e 284 pthread_mutex_unlock(&app->sock_lock);
d0b96690
DG
285 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
286 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
287 sock, ua_ctx->obj->handle, ret);
ffe60014 288 }
55cc08a6
DG
289 free(ua_ctx->obj);
290 }
291 free(ua_ctx);
292}
293
d80a6244
DG
294/*
295 * Delete ust app event safely. RCU read lock must be held before calling
296 * this function.
297 */
8b366481 298static
fb45065e
MD
299void delete_ust_app_event(int sock, struct ust_app_event *ua_event,
300 struct ust_app *app)
d80a6244 301{
ffe60014
DG
302 int ret;
303
304 assert(ua_event);
305
53a80697 306 free(ua_event->filter);
951f0b71
JI
307 if (ua_event->exclusion != NULL)
308 free(ua_event->exclusion);
edb67388 309 if (ua_event->obj != NULL) {
fb45065e 310 pthread_mutex_lock(&app->sock_lock);
ffe60014 311 ret = ustctl_release_object(sock, ua_event->obj);
fb45065e 312 pthread_mutex_unlock(&app->sock_lock);
ffe60014
DG
313 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
314 ERR("UST app sock %d release event obj failed with ret %d",
315 sock, ret);
316 }
edb67388
DG
317 free(ua_event->obj);
318 }
d80a6244
DG
319 free(ua_event);
320}
321
322/*
7972aab2
DG
323 * Release ust data object of the given stream.
324 *
325 * Return 0 on success or else a negative value.
d80a6244 326 */
fb45065e
MD
327static int release_ust_app_stream(int sock, struct ust_app_stream *stream,
328 struct ust_app *app)
d80a6244 329{
7972aab2 330 int ret = 0;
ffe60014
DG
331
332 assert(stream);
333
8b366481 334 if (stream->obj) {
fb45065e 335 pthread_mutex_lock(&app->sock_lock);
ffe60014 336 ret = ustctl_release_object(sock, stream->obj);
fb45065e 337 pthread_mutex_unlock(&app->sock_lock);
ffe60014
DG
338 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
339 ERR("UST app sock %d release stream obj failed with ret %d",
340 sock, ret);
341 }
4063050c 342 lttng_fd_put(LTTNG_FD_APPS, 2);
8b366481
DG
343 free(stream->obj);
344 }
7972aab2
DG
345
346 return ret;
347}
348
349/*
350 * Delete ust app stream safely. RCU read lock must be held before calling
351 * this function.
352 */
353static
fb45065e
MD
354void delete_ust_app_stream(int sock, struct ust_app_stream *stream,
355 struct ust_app *app)
7972aab2
DG
356{
357 assert(stream);
358
fb45065e 359 (void) release_ust_app_stream(sock, stream, app);
84cd17c6 360 free(stream);
d80a6244
DG
361}
362
36b588ed
MD
363/*
364 * We need to execute ht_destroy outside of RCU read-side critical
0b2dc8df
MD
365 * section and outside of call_rcu thread, so we postpone its execution
366 * using ht_cleanup_push. It is simpler than to change the semantic of
367 * the many callers of delete_ust_app_session().
36b588ed
MD
368 */
369static
370void delete_ust_app_channel_rcu(struct rcu_head *head)
371{
372 struct ust_app_channel *ua_chan =
373 caa_container_of(head, struct ust_app_channel, rcu_head);
374
0b2dc8df
MD
375 ht_cleanup_push(ua_chan->ctx);
376 ht_cleanup_push(ua_chan->events);
36b588ed
MD
377 free(ua_chan);
378}
379
fb83fe64
JD
380/*
381 * Extract the lost packet or discarded events counter when the channel is
382 * being deleted and store the value in the parent channel so we can
383 * access it from lttng list and at stop/destroy.
82cac6d2
JG
384 *
385 * The session list lock must be held by the caller.
fb83fe64
JD
386 */
387static
388void save_per_pid_lost_discarded_counters(struct ust_app_channel *ua_chan)
389{
390 uint64_t discarded = 0, lost = 0;
391 struct ltt_session *session;
392 struct ltt_ust_channel *uchan;
393
394 if (ua_chan->attr.type != LTTNG_UST_CHAN_PER_CPU) {
395 return;
396 }
397
398 rcu_read_lock();
399 session = session_find_by_id(ua_chan->session->tracing_id);
d68ec974
JG
400 if (!session || !session->ust_session) {
401 /*
402 * Not finding the session is not an error because there are
403 * multiple ways the channels can be torn down.
404 *
405 * 1) The session daemon can initiate the destruction of the
406 * ust app session after receiving a destroy command or
407 * during its shutdown/teardown.
408 * 2) The application, since we are in per-pid tracing, is
409 * unregistering and tearing down its ust app session.
410 *
411 * Both paths are protected by the session list lock which
412 * ensures that the accounting of lost packets and discarded
413 * events is done exactly once. The session is then unpublished
414 * from the session list, resulting in this condition.
415 */
fb83fe64
JD
416 goto end;
417 }
418
419 if (ua_chan->attr.overwrite) {
420 consumer_get_lost_packets(ua_chan->session->tracing_id,
421 ua_chan->key, session->ust_session->consumer,
422 &lost);
423 } else {
424 consumer_get_discarded_events(ua_chan->session->tracing_id,
425 ua_chan->key, session->ust_session->consumer,
426 &discarded);
427 }
428 uchan = trace_ust_find_channel_by_name(
429 session->ust_session->domain_global.channels,
430 ua_chan->name);
431 if (!uchan) {
432 ERR("Missing UST channel to store discarded counters");
433 goto end;
434 }
435
436 uchan->per_pid_closed_app_discarded += discarded;
437 uchan->per_pid_closed_app_lost += lost;
438
439end:
440 rcu_read_unlock();
e32d7f27
JG
441 if (session) {
442 session_put(session);
443 }
fb83fe64
JD
444}
445
d80a6244
DG
446/*
447 * Delete ust app channel safely. RCU read lock must be held before calling
448 * this function.
82cac6d2
JG
449 *
450 * The session list lock must be held by the caller.
d80a6244 451 */
8b366481 452static
d0b96690
DG
453void delete_ust_app_channel(int sock, struct ust_app_channel *ua_chan,
454 struct ust_app *app)
d80a6244
DG
455{
456 int ret;
bec39940 457 struct lttng_ht_iter iter;
d80a6244 458 struct ust_app_event *ua_event;
55cc08a6 459 struct ust_app_ctx *ua_ctx;
030a66fa 460 struct ust_app_stream *stream, *stmp;
7972aab2 461 struct ust_registry_session *registry;
d80a6244 462
ffe60014
DG
463 assert(ua_chan);
464
465 DBG3("UST app deleting channel %s", ua_chan->name);
466
55cc08a6 467 /* Wipe stream */
d80a6244 468 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
84cd17c6 469 cds_list_del(&stream->list);
fb45065e 470 delete_ust_app_stream(sock, stream, app);
d80a6244
DG
471 }
472
55cc08a6 473 /* Wipe context */
bec39940 474 cds_lfht_for_each_entry(ua_chan->ctx->ht, &iter.iter, ua_ctx, node.node) {
31746f93 475 cds_list_del(&ua_ctx->list);
bec39940 476 ret = lttng_ht_del(ua_chan->ctx, &iter);
55cc08a6 477 assert(!ret);
fb45065e 478 delete_ust_app_ctx(sock, ua_ctx, app);
55cc08a6 479 }
d80a6244 480
55cc08a6 481 /* Wipe events */
bec39940
DG
482 cds_lfht_for_each_entry(ua_chan->events->ht, &iter.iter, ua_event,
483 node.node) {
484 ret = lttng_ht_del(ua_chan->events, &iter);
525b0740 485 assert(!ret);
fb45065e 486 delete_ust_app_event(sock, ua_event, app);
d80a6244 487 }
edb67388 488
c8335706
MD
489 if (ua_chan->session->buffer_type == LTTNG_BUFFER_PER_PID) {
490 /* Wipe and free registry from session registry. */
491 registry = get_session_registry(ua_chan->session);
492 if (registry) {
e9404c27 493 ust_registry_channel_del_free(registry, ua_chan->key,
e38d96f9
MD
494 sock >= 0);
495 }
45798a31
JG
496 /*
497 * A negative socket can be used by the caller when
498 * cleaning-up a ua_chan in an error path. Skip the
499 * accounting in this case.
500 */
e38d96f9
MD
501 if (sock >= 0) {
502 save_per_pid_lost_discarded_counters(ua_chan);
c8335706 503 }
7972aab2 504 }
d0b96690 505
edb67388 506 if (ua_chan->obj != NULL) {
d0b96690
DG
507 /* Remove channel from application UST object descriptor. */
508 iter.iter.node = &ua_chan->ust_objd_node.node;
c6e62271
DG
509 ret = lttng_ht_del(app->ust_objd, &iter);
510 assert(!ret);
fb45065e 511 pthread_mutex_lock(&app->sock_lock);
ffe60014 512 ret = ustctl_release_object(sock, ua_chan->obj);
fb45065e 513 pthread_mutex_unlock(&app->sock_lock);
ffe60014
DG
514 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
515 ERR("UST app sock %d release channel obj failed with ret %d",
516 sock, ret);
517 }
7972aab2 518 lttng_fd_put(LTTNG_FD_APPS, 1);
edb67388
DG
519 free(ua_chan->obj);
520 }
36b588ed 521 call_rcu(&ua_chan->rcu_head, delete_ust_app_channel_rcu);
d80a6244
DG
522}
523
fb45065e
MD
524int ust_app_register_done(struct ust_app *app)
525{
526 int ret;
527
528 pthread_mutex_lock(&app->sock_lock);
529 ret = ustctl_register_done(app->sock);
530 pthread_mutex_unlock(&app->sock_lock);
531 return ret;
532}
533
534int ust_app_release_object(struct ust_app *app, struct lttng_ust_object_data *data)
535{
536 int ret, sock;
537
538 if (app) {
539 pthread_mutex_lock(&app->sock_lock);
540 sock = app->sock;
541 } else {
542 sock = -1;
543 }
544 ret = ustctl_release_object(sock, data);
545 if (app) {
546 pthread_mutex_unlock(&app->sock_lock);
547 }
548 return ret;
549}
550
331744e3 551/*
1b532a60
DG
552 * Push metadata to consumer socket.
553 *
dc2bbdae
MD
554 * RCU read-side lock must be held to guarantee existance of socket.
555 * Must be called with the ust app session lock held.
556 * Must be called with the registry lock held.
331744e3
JD
557 *
558 * On success, return the len of metadata pushed or else a negative value.
2c57e06d
MD
559 * Returning a -EPIPE return value means we could not send the metadata,
560 * but it can be caused by recoverable errors (e.g. the application has
561 * terminated concurrently).
331744e3
JD
562 */
563ssize_t ust_app_push_metadata(struct ust_registry_session *registry,
564 struct consumer_socket *socket, int send_zero_data)
565{
566 int ret;
567 char *metadata_str = NULL;
c585821b 568 size_t len, offset, new_metadata_len_sent;
331744e3 569 ssize_t ret_val;
93ec662e 570 uint64_t metadata_key, metadata_version;
331744e3
JD
571
572 assert(registry);
573 assert(socket);
1b532a60 574
c585821b
MD
575 metadata_key = registry->metadata_key;
576
ce34fcd0 577 /*
dc2bbdae
MD
578 * Means that no metadata was assigned to the session. This can
579 * happens if no start has been done previously.
ce34fcd0 580 */
c585821b 581 if (!metadata_key) {
ce34fcd0
MD
582 return 0;
583 }
584
331744e3
JD
585 offset = registry->metadata_len_sent;
586 len = registry->metadata_len - registry->metadata_len_sent;
c585821b 587 new_metadata_len_sent = registry->metadata_len;
93ec662e 588 metadata_version = registry->metadata_version;
331744e3
JD
589 if (len == 0) {
590 DBG3("No metadata to push for metadata key %" PRIu64,
591 registry->metadata_key);
592 ret_val = len;
593 if (send_zero_data) {
594 DBG("No metadata to push");
595 goto push_data;
596 }
597 goto end;
598 }
599
600 /* Allocate only what we have to send. */
601 metadata_str = zmalloc(len);
602 if (!metadata_str) {
603 PERROR("zmalloc ust app metadata string");
604 ret_val = -ENOMEM;
605 goto error;
606 }
c585821b 607 /* Copy what we haven't sent out. */
331744e3 608 memcpy(metadata_str, registry->metadata + offset, len);
331744e3
JD
609
610push_data:
c585821b
MD
611 pthread_mutex_unlock(&registry->lock);
612 /*
613 * We need to unlock the registry while we push metadata to
614 * break a circular dependency between the consumerd metadata
615 * lock and the sessiond registry lock. Indeed, pushing metadata
616 * to the consumerd awaits that it gets pushed all the way to
617 * relayd, but doing so requires grabbing the metadata lock. If
618 * a concurrent metadata request is being performed by
619 * consumerd, this can try to grab the registry lock on the
620 * sessiond while holding the metadata lock on the consumer
621 * daemon. Those push and pull schemes are performed on two
622 * different bidirectionnal communication sockets.
623 */
624 ret = consumer_push_metadata(socket, metadata_key,
93ec662e 625 metadata_str, len, offset, metadata_version);
c585821b 626 pthread_mutex_lock(&registry->lock);
331744e3 627 if (ret < 0) {
000baf6a 628 /*
dc2bbdae
MD
629 * There is an acceptable race here between the registry
630 * metadata key assignment and the creation on the
631 * consumer. The session daemon can concurrently push
632 * metadata for this registry while being created on the
633 * consumer since the metadata key of the registry is
634 * assigned *before* it is setup to avoid the consumer
635 * to ask for metadata that could possibly be not found
636 * in the session daemon.
000baf6a 637 *
dc2bbdae
MD
638 * The metadata will get pushed either by the session
639 * being stopped or the consumer requesting metadata if
640 * that race is triggered.
000baf6a
DG
641 */
642 if (ret == -LTTCOMM_CONSUMERD_CHANNEL_FAIL) {
643 ret = 0;
c585821b
MD
644 } else {
645 ERR("Error pushing metadata to consumer");
000baf6a 646 }
331744e3
JD
647 ret_val = ret;
648 goto error_push;
c585821b
MD
649 } else {
650 /*
651 * Metadata may have been concurrently pushed, since
652 * we're not holding the registry lock while pushing to
653 * consumer. This is handled by the fact that we send
654 * the metadata content, size, and the offset at which
655 * that metadata belongs. This may arrive out of order
656 * on the consumer side, and the consumer is able to
657 * deal with overlapping fragments. The consumer
658 * supports overlapping fragments, which must be
659 * contiguous starting from offset 0. We keep the
660 * largest metadata_len_sent value of the concurrent
661 * send.
662 */
663 registry->metadata_len_sent =
664 max_t(size_t, registry->metadata_len_sent,
665 new_metadata_len_sent);
331744e3 666 }
331744e3
JD
667 free(metadata_str);
668 return len;
669
670end:
671error:
ce34fcd0
MD
672 if (ret_val) {
673 /*
dc2bbdae
MD
674 * On error, flag the registry that the metadata is
675 * closed. We were unable to push anything and this
676 * means that either the consumer is not responding or
677 * the metadata cache has been destroyed on the
678 * consumer.
ce34fcd0
MD
679 */
680 registry->metadata_closed = 1;
681 }
331744e3
JD
682error_push:
683 free(metadata_str);
684 return ret_val;
685}
686
d88aee68 687/*
ce34fcd0 688 * For a given application and session, push metadata to consumer.
331744e3
JD
689 * Either sock or consumer is required : if sock is NULL, the default
690 * socket to send the metadata is retrieved from consumer, if sock
691 * is not NULL we use it to send the metadata.
ce34fcd0 692 * RCU read-side lock must be held while calling this function,
dc2bbdae
MD
693 * therefore ensuring existance of registry. It also ensures existance
694 * of socket throughout this function.
d88aee68
DG
695 *
696 * Return 0 on success else a negative error.
2c57e06d
MD
697 * Returning a -EPIPE return value means we could not send the metadata,
698 * but it can be caused by recoverable errors (e.g. the application has
699 * terminated concurrently).
d88aee68 700 */
7972aab2
DG
701static int push_metadata(struct ust_registry_session *registry,
702 struct consumer_output *consumer)
d88aee68 703{
331744e3
JD
704 int ret_val;
705 ssize_t ret;
d88aee68
DG
706 struct consumer_socket *socket;
707
7972aab2
DG
708 assert(registry);
709 assert(consumer);
710
ce34fcd0 711 pthread_mutex_lock(&registry->lock);
ce34fcd0 712 if (registry->metadata_closed) {
dc2bbdae
MD
713 ret_val = -EPIPE;
714 goto error;
d88aee68
DG
715 }
716
d88aee68 717 /* Get consumer socket to use to push the metadata.*/
7972aab2
DG
718 socket = consumer_find_socket_by_bitness(registry->bits_per_long,
719 consumer);
d88aee68 720 if (!socket) {
331744e3 721 ret_val = -1;
ce34fcd0 722 goto error;
d88aee68
DG
723 }
724
331744e3 725 ret = ust_app_push_metadata(registry, socket, 0);
d88aee68 726 if (ret < 0) {
331744e3 727 ret_val = ret;
ce34fcd0 728 goto error;
d88aee68 729 }
dc2bbdae 730 pthread_mutex_unlock(&registry->lock);
d88aee68
DG
731 return 0;
732
ce34fcd0 733error:
dc2bbdae 734 pthread_mutex_unlock(&registry->lock);
331744e3 735 return ret_val;
d88aee68
DG
736}
737
738/*
739 * Send to the consumer a close metadata command for the given session. Once
740 * done, the metadata channel is deleted and the session metadata pointer is
dc2bbdae 741 * nullified. The session lock MUST be held unless the application is
d88aee68
DG
742 * in the destroy path.
743 *
744 * Return 0 on success else a negative value.
745 */
7972aab2
DG
746static int close_metadata(struct ust_registry_session *registry,
747 struct consumer_output *consumer)
d88aee68
DG
748{
749 int ret;
750 struct consumer_socket *socket;
751
7972aab2
DG
752 assert(registry);
753 assert(consumer);
d88aee68 754
7972aab2
DG
755 rcu_read_lock();
756
ce34fcd0
MD
757 pthread_mutex_lock(&registry->lock);
758
7972aab2 759 if (!registry->metadata_key || registry->metadata_closed) {
d88aee68 760 ret = 0;
1b532a60 761 goto end;
d88aee68
DG
762 }
763
d88aee68 764 /* Get consumer socket to use to push the metadata.*/
7972aab2
DG
765 socket = consumer_find_socket_by_bitness(registry->bits_per_long,
766 consumer);
d88aee68
DG
767 if (!socket) {
768 ret = -1;
7972aab2 769 goto error;
d88aee68
DG
770 }
771
7972aab2 772 ret = consumer_close_metadata(socket, registry->metadata_key);
d88aee68 773 if (ret < 0) {
7972aab2 774 goto error;
d88aee68
DG
775 }
776
d88aee68 777error:
1b532a60
DG
778 /*
779 * Metadata closed. Even on error this means that the consumer is not
780 * responding or not found so either way a second close should NOT be emit
781 * for this registry.
782 */
783 registry->metadata_closed = 1;
784end:
ce34fcd0 785 pthread_mutex_unlock(&registry->lock);
7972aab2 786 rcu_read_unlock();
d88aee68
DG
787 return ret;
788}
789
36b588ed
MD
790/*
791 * We need to execute ht_destroy outside of RCU read-side critical
0b2dc8df
MD
792 * section and outside of call_rcu thread, so we postpone its execution
793 * using ht_cleanup_push. It is simpler than to change the semantic of
794 * the many callers of delete_ust_app_session().
36b588ed
MD
795 */
796static
797void delete_ust_app_session_rcu(struct rcu_head *head)
798{
799 struct ust_app_session *ua_sess =
800 caa_container_of(head, struct ust_app_session, rcu_head);
801
0b2dc8df 802 ht_cleanup_push(ua_sess->channels);
36b588ed
MD
803 free(ua_sess);
804}
805
d80a6244
DG
806/*
807 * Delete ust app session safely. RCU read lock must be held before calling
808 * this function.
82cac6d2
JG
809 *
810 * The session list lock must be held by the caller.
d80a6244 811 */
8b366481 812static
d0b96690
DG
813void delete_ust_app_session(int sock, struct ust_app_session *ua_sess,
814 struct ust_app *app)
d80a6244
DG
815{
816 int ret;
bec39940 817 struct lttng_ht_iter iter;
d80a6244 818 struct ust_app_channel *ua_chan;
7972aab2 819 struct ust_registry_session *registry;
d80a6244 820
d88aee68
DG
821 assert(ua_sess);
822
1b532a60
DG
823 pthread_mutex_lock(&ua_sess->lock);
824
b161602a
MD
825 assert(!ua_sess->deleted);
826 ua_sess->deleted = true;
827
7972aab2 828 registry = get_session_registry(ua_sess);
fad1ed2f 829 /* Registry can be null on error path during initialization. */
ce34fcd0 830 if (registry) {
d88aee68 831 /* Push metadata for application before freeing the application. */
7972aab2 832 (void) push_metadata(registry, ua_sess->consumer);
d88aee68 833
7972aab2
DG
834 /*
835 * Don't ask to close metadata for global per UID buffers. Close
1b532a60
DG
836 * metadata only on destroy trace session in this case. Also, the
837 * previous push metadata could have flag the metadata registry to
838 * close so don't send a close command if closed.
7972aab2 839 */
ce34fcd0 840 if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) {
7972aab2
DG
841 /* And ask to close it for this session registry. */
842 (void) close_metadata(registry, ua_sess->consumer);
843 }
d80a6244
DG
844 }
845
bec39940
DG
846 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
847 node.node) {
848 ret = lttng_ht_del(ua_sess->channels, &iter);
525b0740 849 assert(!ret);
d0b96690 850 delete_ust_app_channel(sock, ua_chan, app);
d80a6244 851 }
d80a6244 852
7972aab2
DG
853 /* In case of per PID, the registry is kept in the session. */
854 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) {
855 struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id);
856 if (reg_pid) {
fad1ed2f
JR
857 /*
858 * Registry can be null on error path during
859 * initialization.
860 */
7972aab2
DG
861 buffer_reg_pid_remove(reg_pid);
862 buffer_reg_pid_destroy(reg_pid);
863 }
864 }
d0b96690 865
aee6bafd 866 if (ua_sess->handle != -1) {
fb45065e 867 pthread_mutex_lock(&app->sock_lock);
ffe60014 868 ret = ustctl_release_handle(sock, ua_sess->handle);
fb45065e 869 pthread_mutex_unlock(&app->sock_lock);
ffe60014
DG
870 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
871 ERR("UST app sock %d release session handle failed with ret %d",
872 sock, ret);
873 }
10b56aef
MD
874 /* Remove session from application UST object descriptor. */
875 iter.iter.node = &ua_sess->ust_objd_node.node;
876 ret = lttng_ht_del(app->ust_sessions_objd, &iter);
877 assert(!ret);
aee6bafd 878 }
10b56aef 879
1b532a60
DG
880 pthread_mutex_unlock(&ua_sess->lock);
881
6addfa37
MD
882 consumer_output_put(ua_sess->consumer);
883
36b588ed 884 call_rcu(&ua_sess->rcu_head, delete_ust_app_session_rcu);
d80a6244 885}
91d76f53
DG
886
887/*
284d8f55
DG
888 * Delete a traceable application structure from the global list. Never call
889 * this function outside of a call_rcu call.
36b588ed
MD
890 *
891 * RCU read side lock should _NOT_ be held when calling this function.
91d76f53 892 */
8b366481
DG
893static
894void delete_ust_app(struct ust_app *app)
91d76f53 895{
8b366481 896 int ret, sock;
d42f20df 897 struct ust_app_session *ua_sess, *tmp_ua_sess;
44d3bd01 898
82cac6d2
JG
899 /*
900 * The session list lock must be held during this function to guarantee
901 * the existence of ua_sess.
902 */
903 session_lock_list();
d80a6244 904 /* Delete ust app sessions info */
852d0037
DG
905 sock = app->sock;
906 app->sock = -1;
d80a6244 907
8b366481 908 /* Wipe sessions */
d42f20df
DG
909 cds_list_for_each_entry_safe(ua_sess, tmp_ua_sess, &app->teardown_head,
910 teardown_node) {
911 /* Free every object in the session and the session. */
36b588ed 912 rcu_read_lock();
d0b96690 913 delete_ust_app_session(sock, ua_sess, app);
36b588ed 914 rcu_read_unlock();
d80a6244 915 }
36b588ed 916
0b2dc8df 917 ht_cleanup_push(app->sessions);
10b56aef 918 ht_cleanup_push(app->ust_sessions_objd);
0b2dc8df 919 ht_cleanup_push(app->ust_objd);
d80a6244 920
6414a713 921 /*
852d0037
DG
922 * Wait until we have deleted the application from the sock hash table
923 * before closing this socket, otherwise an application could re-use the
924 * socket ID and race with the teardown, using the same hash table entry.
925 *
926 * It's OK to leave the close in call_rcu. We want it to stay unique for
927 * all RCU readers that could run concurrently with unregister app,
928 * therefore we _need_ to only close that socket after a grace period. So
929 * it should stay in this RCU callback.
930 *
931 * This close() is a very important step of the synchronization model so
932 * every modification to this function must be carefully reviewed.
6414a713 933 */
799e2c4f
MD
934 ret = close(sock);
935 if (ret) {
936 PERROR("close");
937 }
4063050c 938 lttng_fd_put(LTTNG_FD_APPS, 1);
d80a6244 939
852d0037 940 DBG2("UST app pid %d deleted", app->pid);
284d8f55 941 free(app);
82cac6d2 942 session_unlock_list();
099e26bd
DG
943}
944
945/*
f6a9efaa 946 * URCU intermediate call to delete an UST app.
099e26bd 947 */
8b366481
DG
948static
949void delete_ust_app_rcu(struct rcu_head *head)
099e26bd 950{
bec39940
DG
951 struct lttng_ht_node_ulong *node =
952 caa_container_of(head, struct lttng_ht_node_ulong, head);
f6a9efaa 953 struct ust_app *app =
852d0037 954 caa_container_of(node, struct ust_app, pid_n);
f6a9efaa 955
852d0037 956 DBG3("Call RCU deleting app PID %d", app->pid);
f6a9efaa 957 delete_ust_app(app);
099e26bd
DG
958}
959
ffe60014
DG
960/*
961 * Delete the session from the application ht and delete the data structure by
962 * freeing every object inside and releasing them.
82cac6d2
JG
963 *
964 * The session list lock must be held by the caller.
ffe60014 965 */
d0b96690 966static void destroy_app_session(struct ust_app *app,
ffe60014
DG
967 struct ust_app_session *ua_sess)
968{
969 int ret;
970 struct lttng_ht_iter iter;
971
972 assert(app);
973 assert(ua_sess);
974
975 iter.iter.node = &ua_sess->node.node;
976 ret = lttng_ht_del(app->sessions, &iter);
977 if (ret) {
978 /* Already scheduled for teardown. */
979 goto end;
980 }
981
982 /* Once deleted, free the data structure. */
d0b96690 983 delete_ust_app_session(app->sock, ua_sess, app);
ffe60014
DG
984
985end:
986 return;
987}
988
8b366481
DG
989/*
990 * Alloc new UST app session.
991 */
992static
40bbd087 993struct ust_app_session *alloc_ust_app_session(void)
8b366481
DG
994{
995 struct ust_app_session *ua_sess;
996
997 /* Init most of the default value by allocating and zeroing */
998 ua_sess = zmalloc(sizeof(struct ust_app_session));
999 if (ua_sess == NULL) {
1000 PERROR("malloc");
ffe60014 1001 goto error_free;
8b366481
DG
1002 }
1003
1004 ua_sess->handle = -1;
bec39940 1005 ua_sess->channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
ad7a9107 1006 ua_sess->metadata_attr.type = LTTNG_UST_CHAN_METADATA;
84ad93e8 1007 pthread_mutex_init(&ua_sess->lock, NULL);
ad7a9107 1008
8b366481
DG
1009 return ua_sess;
1010
ffe60014 1011error_free:
8b366481
DG
1012 return NULL;
1013}
1014
1015/*
1016 * Alloc new UST app channel.
1017 */
1018static
1019struct ust_app_channel *alloc_ust_app_channel(char *name,
d0b96690 1020 struct ust_app_session *ua_sess,
ffe60014 1021 struct lttng_ust_channel_attr *attr)
8b366481
DG
1022{
1023 struct ust_app_channel *ua_chan;
1024
1025 /* Init most of the default value by allocating and zeroing */
1026 ua_chan = zmalloc(sizeof(struct ust_app_channel));
1027 if (ua_chan == NULL) {
1028 PERROR("malloc");
1029 goto error;
1030 }
1031
1032 /* Setup channel name */
1033 strncpy(ua_chan->name, name, sizeof(ua_chan->name));
1034 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
1035
1036 ua_chan->enabled = 1;
1037 ua_chan->handle = -1;
45893984 1038 ua_chan->session = ua_sess;
ffe60014 1039 ua_chan->key = get_next_channel_key();
bec39940
DG
1040 ua_chan->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1041 ua_chan->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
1042 lttng_ht_node_init_str(&ua_chan->node, ua_chan->name);
8b366481
DG
1043
1044 CDS_INIT_LIST_HEAD(&ua_chan->streams.head);
31746f93 1045 CDS_INIT_LIST_HEAD(&ua_chan->ctx_list);
8b366481
DG
1046
1047 /* Copy attributes */
1048 if (attr) {
ffe60014 1049 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
2fe6e7f5
DG
1050 ua_chan->attr.subbuf_size = attr->subbuf_size;
1051 ua_chan->attr.num_subbuf = attr->num_subbuf;
1052 ua_chan->attr.overwrite = attr->overwrite;
1053 ua_chan->attr.switch_timer_interval = attr->switch_timer_interval;
1054 ua_chan->attr.read_timer_interval = attr->read_timer_interval;
1055 ua_chan->attr.output = attr->output;
491d1539 1056 ua_chan->attr.blocking_timeout = attr->u.s.blocking_timeout;
8b366481 1057 }
ffe60014
DG
1058 /* By default, the channel is a per cpu channel. */
1059 ua_chan->attr.type = LTTNG_UST_CHAN_PER_CPU;
8b366481
DG
1060
1061 DBG3("UST app channel %s allocated", ua_chan->name);
1062
1063 return ua_chan;
1064
1065error:
1066 return NULL;
1067}
1068
37f1c236
DG
1069/*
1070 * Allocate and initialize a UST app stream.
1071 *
1072 * Return newly allocated stream pointer or NULL on error.
1073 */
ffe60014 1074struct ust_app_stream *ust_app_alloc_stream(void)
37f1c236
DG
1075{
1076 struct ust_app_stream *stream = NULL;
1077
1078 stream = zmalloc(sizeof(*stream));
1079 if (stream == NULL) {
1080 PERROR("zmalloc ust app stream");
1081 goto error;
1082 }
1083
1084 /* Zero could be a valid value for a handle so flag it to -1. */
1085 stream->handle = -1;
1086
1087error:
1088 return stream;
1089}
1090
8b366481
DG
1091/*
1092 * Alloc new UST app event.
1093 */
1094static
1095struct ust_app_event *alloc_ust_app_event(char *name,
1096 struct lttng_ust_event *attr)
1097{
1098 struct ust_app_event *ua_event;
1099
1100 /* Init most of the default value by allocating and zeroing */
1101 ua_event = zmalloc(sizeof(struct ust_app_event));
1102 if (ua_event == NULL) {
1103 PERROR("malloc");
1104 goto error;
1105 }
1106
1107 ua_event->enabled = 1;
1108 strncpy(ua_event->name, name, sizeof(ua_event->name));
1109 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
bec39940 1110 lttng_ht_node_init_str(&ua_event->node, ua_event->name);
8b366481
DG
1111
1112 /* Copy attributes */
1113 if (attr) {
1114 memcpy(&ua_event->attr, attr, sizeof(ua_event->attr));
1115 }
1116
1117 DBG3("UST app event %s allocated", ua_event->name);
1118
1119 return ua_event;
1120
1121error:
1122 return NULL;
1123}
1124
1125/*
1126 * Alloc new UST app context.
1127 */
1128static
bdf64013 1129struct ust_app_ctx *alloc_ust_app_ctx(struct lttng_ust_context_attr *uctx)
8b366481
DG
1130{
1131 struct ust_app_ctx *ua_ctx;
1132
1133 ua_ctx = zmalloc(sizeof(struct ust_app_ctx));
1134 if (ua_ctx == NULL) {
1135 goto error;
1136 }
1137
31746f93
DG
1138 CDS_INIT_LIST_HEAD(&ua_ctx->list);
1139
8b366481
DG
1140 if (uctx) {
1141 memcpy(&ua_ctx->ctx, uctx, sizeof(ua_ctx->ctx));
bdf64013
JG
1142 if (uctx->ctx == LTTNG_UST_CONTEXT_APP_CONTEXT) {
1143 char *provider_name = NULL, *ctx_name = NULL;
1144
1145 provider_name = strdup(uctx->u.app_ctx.provider_name);
1146 ctx_name = strdup(uctx->u.app_ctx.ctx_name);
1147 if (!provider_name || !ctx_name) {
1148 free(provider_name);
1149 free(ctx_name);
1150 goto error;
1151 }
1152
1153 ua_ctx->ctx.u.app_ctx.provider_name = provider_name;
1154 ua_ctx->ctx.u.app_ctx.ctx_name = ctx_name;
1155 }
8b366481
DG
1156 }
1157
1158 DBG3("UST app context %d allocated", ua_ctx->ctx.ctx);
8b366481 1159 return ua_ctx;
bdf64013
JG
1160error:
1161 free(ua_ctx);
1162 return NULL;
8b366481
DG
1163}
1164
025faf73
DG
1165/*
1166 * Allocate a filter and copy the given original filter.
1167 *
1168 * Return allocated filter or NULL on error.
1169 */
51755dc8
JG
1170static struct lttng_filter_bytecode *copy_filter_bytecode(
1171 struct lttng_filter_bytecode *orig_f)
025faf73 1172{
51755dc8 1173 struct lttng_filter_bytecode *filter = NULL;
025faf73
DG
1174
1175 /* Copy filter bytecode */
1176 filter = zmalloc(sizeof(*filter) + orig_f->len);
1177 if (!filter) {
51755dc8 1178 PERROR("zmalloc alloc filter bytecode");
025faf73
DG
1179 goto error;
1180 }
1181
1182 memcpy(filter, orig_f, sizeof(*filter) + orig_f->len);
1183
1184error:
1185 return filter;
1186}
1187
51755dc8
JG
1188/*
1189 * Create a liblttng-ust filter bytecode from given bytecode.
1190 *
1191 * Return allocated filter or NULL on error.
1192 */
1193static struct lttng_ust_filter_bytecode *create_ust_bytecode_from_bytecode(
1194 struct lttng_filter_bytecode *orig_f)
1195{
1196 struct lttng_ust_filter_bytecode *filter = NULL;
1197
1198 /* Copy filter bytecode */
1199 filter = zmalloc(sizeof(*filter) + orig_f->len);
1200 if (!filter) {
1201 PERROR("zmalloc alloc ust filter bytecode");
1202 goto error;
1203 }
1204
1205 assert(sizeof(struct lttng_filter_bytecode) ==
1206 sizeof(struct lttng_ust_filter_bytecode));
1207 memcpy(filter, orig_f, sizeof(*filter) + orig_f->len);
1208error:
1209 return filter;
1210}
1211
099e26bd 1212/*
421cb601
DG
1213 * Find an ust_app using the sock and return it. RCU read side lock must be
1214 * held before calling this helper function.
099e26bd 1215 */
f20baf8e 1216struct ust_app *ust_app_find_by_sock(int sock)
099e26bd 1217{
bec39940 1218 struct lttng_ht_node_ulong *node;
bec39940 1219 struct lttng_ht_iter iter;
f6a9efaa 1220
852d0037 1221 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter);
bec39940 1222 node = lttng_ht_iter_get_node_ulong(&iter);
f6a9efaa
DG
1223 if (node == NULL) {
1224 DBG2("UST app find by sock %d not found", sock);
f6a9efaa
DG
1225 goto error;
1226 }
852d0037
DG
1227
1228 return caa_container_of(node, struct ust_app, sock_n);
f6a9efaa
DG
1229
1230error:
1231 return NULL;
099e26bd
DG
1232}
1233
d0b96690
DG
1234/*
1235 * Find an ust_app using the notify sock and return it. RCU read side lock must
1236 * be held before calling this helper function.
1237 */
1238static struct ust_app *find_app_by_notify_sock(int sock)
1239{
1240 struct lttng_ht_node_ulong *node;
1241 struct lttng_ht_iter iter;
1242
1243 lttng_ht_lookup(ust_app_ht_by_notify_sock, (void *)((unsigned long) sock),
1244 &iter);
1245 node = lttng_ht_iter_get_node_ulong(&iter);
1246 if (node == NULL) {
1247 DBG2("UST app find by notify sock %d not found", sock);
1248 goto error;
1249 }
1250
1251 return caa_container_of(node, struct ust_app, notify_sock_n);
1252
1253error:
1254 return NULL;
1255}
1256
025faf73
DG
1257/*
1258 * Lookup for an ust app event based on event name, filter bytecode and the
1259 * event loglevel.
1260 *
1261 * Return an ust_app_event object or NULL on error.
1262 */
18eace3b 1263static struct ust_app_event *find_ust_app_event(struct lttng_ht *ht,
88e3c2f5 1264 const char *name, const struct lttng_filter_bytecode *filter,
2106efa0 1265 int loglevel_value,
39c5a3a7 1266 const struct lttng_event_exclusion *exclusion)
18eace3b
DG
1267{
1268 struct lttng_ht_iter iter;
1269 struct lttng_ht_node_str *node;
1270 struct ust_app_event *event = NULL;
1271 struct ust_app_ht_key key;
18eace3b
DG
1272
1273 assert(name);
1274 assert(ht);
1275
1276 /* Setup key for event lookup. */
1277 key.name = name;
1278 key.filter = filter;
2106efa0 1279 key.loglevel_type = loglevel_value;
39c5a3a7 1280 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
51755dc8 1281 key.exclusion = exclusion;
18eace3b 1282
025faf73
DG
1283 /* Lookup using the event name as hash and a custom match fct. */
1284 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
1285 ht_match_ust_app_event, &key, &iter.iter);
18eace3b
DG
1286 node = lttng_ht_iter_get_node_str(&iter);
1287 if (node == NULL) {
1288 goto end;
1289 }
1290
1291 event = caa_container_of(node, struct ust_app_event, node);
1292
1293end:
18eace3b
DG
1294 return event;
1295}
1296
55cc08a6
DG
1297/*
1298 * Create the channel context on the tracer.
d0b96690
DG
1299 *
1300 * Called with UST app session lock held.
55cc08a6
DG
1301 */
1302static
1303int create_ust_channel_context(struct ust_app_channel *ua_chan,
1304 struct ust_app_ctx *ua_ctx, struct ust_app *app)
1305{
1306 int ret;
1307
840cb59c 1308 health_code_update();
86acf0da 1309
fb45065e 1310 pthread_mutex_lock(&app->sock_lock);
852d0037 1311 ret = ustctl_add_context(app->sock, &ua_ctx->ctx,
55cc08a6 1312 ua_chan->obj, &ua_ctx->obj);
fb45065e 1313 pthread_mutex_unlock(&app->sock_lock);
55cc08a6 1314 if (ret < 0) {
ffe60014
DG
1315 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1316 ERR("UST app create channel context failed for app (pid: %d) "
1317 "with ret %d", app->pid, ret);
1318 } else {
3757b385
DG
1319 /*
1320 * This is normal behavior, an application can die during the
1321 * creation process. Don't report an error so the execution can
1322 * continue normally.
1323 */
1324 ret = 0;
88e3c2f5 1325 DBG3("UST app add context failed. Application is dead.");
ffe60014 1326 }
55cc08a6
DG
1327 goto error;
1328 }
1329
1330 ua_ctx->handle = ua_ctx->obj->handle;
1331
d0b96690
DG
1332 DBG2("UST app context handle %d created successfully for channel %s",
1333 ua_ctx->handle, ua_chan->name);
55cc08a6
DG
1334
1335error:
840cb59c 1336 health_code_update();
55cc08a6
DG
1337 return ret;
1338}
1339
53a80697
MD
1340/*
1341 * Set the filter on the tracer.
1342 */
1343static
1344int set_ust_event_filter(struct ust_app_event *ua_event,
1345 struct ust_app *app)
1346{
1347 int ret;
51755dc8 1348 struct lttng_ust_filter_bytecode *ust_bytecode = NULL;
53a80697 1349
840cb59c 1350 health_code_update();
86acf0da 1351
53a80697 1352 if (!ua_event->filter) {
86acf0da
DG
1353 ret = 0;
1354 goto error;
53a80697
MD
1355 }
1356
51755dc8
JG
1357 ust_bytecode = create_ust_bytecode_from_bytecode(ua_event->filter);
1358 if (!ust_bytecode) {
1359 ret = -LTTNG_ERR_NOMEM;
1360 goto error;
1361 }
fb45065e 1362 pthread_mutex_lock(&app->sock_lock);
51755dc8 1363 ret = ustctl_set_filter(app->sock, ust_bytecode,
53a80697 1364 ua_event->obj);
fb45065e 1365 pthread_mutex_unlock(&app->sock_lock);
53a80697 1366 if (ret < 0) {
ffe60014
DG
1367 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1368 ERR("UST app event %s filter failed for app (pid: %d) "
1369 "with ret %d", ua_event->attr.name, app->pid, ret);
1370 } else {
3757b385
DG
1371 /*
1372 * This is normal behavior, an application can die during the
1373 * creation process. Don't report an error so the execution can
1374 * continue normally.
1375 */
1376 ret = 0;
ffe60014
DG
1377 DBG3("UST app filter event failed. Application is dead.");
1378 }
53a80697
MD
1379 goto error;
1380 }
1381
1382 DBG2("UST filter set successfully for event %s", ua_event->name);
1383
1384error:
840cb59c 1385 health_code_update();
51755dc8 1386 free(ust_bytecode);
53a80697
MD
1387 return ret;
1388}
1389
51755dc8
JG
1390static
1391struct lttng_ust_event_exclusion *create_ust_exclusion_from_exclusion(
1392 struct lttng_event_exclusion *exclusion)
1393{
1394 struct lttng_ust_event_exclusion *ust_exclusion = NULL;
1395 size_t exclusion_alloc_size = sizeof(struct lttng_ust_event_exclusion) +
1396 LTTNG_UST_SYM_NAME_LEN * exclusion->count;
1397
1398 ust_exclusion = zmalloc(exclusion_alloc_size);
1399 if (!ust_exclusion) {
1400 PERROR("malloc");
1401 goto end;
1402 }
1403
1404 assert(sizeof(struct lttng_event_exclusion) ==
1405 sizeof(struct lttng_ust_event_exclusion));
1406 memcpy(ust_exclusion, exclusion, exclusion_alloc_size);
1407end:
1408 return ust_exclusion;
1409}
1410
7cc9a73c
JI
1411/*
1412 * Set event exclusions on the tracer.
1413 */
1414static
1415int set_ust_event_exclusion(struct ust_app_event *ua_event,
1416 struct ust_app *app)
1417{
1418 int ret;
51755dc8 1419 struct lttng_ust_event_exclusion *ust_exclusion = NULL;
7cc9a73c
JI
1420
1421 health_code_update();
1422
1423 if (!ua_event->exclusion || !ua_event->exclusion->count) {
1424 ret = 0;
1425 goto error;
1426 }
1427
51755dc8
JG
1428 ust_exclusion = create_ust_exclusion_from_exclusion(
1429 ua_event->exclusion);
1430 if (!ust_exclusion) {
1431 ret = -LTTNG_ERR_NOMEM;
1432 goto error;
1433 }
fb45065e 1434 pthread_mutex_lock(&app->sock_lock);
51755dc8 1435 ret = ustctl_set_exclusion(app->sock, ust_exclusion, ua_event->obj);
fb45065e 1436 pthread_mutex_unlock(&app->sock_lock);
7cc9a73c
JI
1437 if (ret < 0) {
1438 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1439 ERR("UST app event %s exclusions failed for app (pid: %d) "
1440 "with ret %d", ua_event->attr.name, app->pid, ret);
1441 } else {
1442 /*
1443 * This is normal behavior, an application can die during the
1444 * creation process. Don't report an error so the execution can
1445 * continue normally.
1446 */
1447 ret = 0;
1448 DBG3("UST app event exclusion failed. Application is dead.");
1449 }
1450 goto error;
1451 }
1452
1453 DBG2("UST exclusion set successfully for event %s", ua_event->name);
1454
1455error:
1456 health_code_update();
51755dc8 1457 free(ust_exclusion);
7cc9a73c
JI
1458 return ret;
1459}
1460
9730260e
DG
1461/*
1462 * Disable the specified event on to UST tracer for the UST session.
1463 */
1464static int disable_ust_event(struct ust_app *app,
1465 struct ust_app_session *ua_sess, struct ust_app_event *ua_event)
1466{
1467 int ret;
1468
840cb59c 1469 health_code_update();
86acf0da 1470
fb45065e 1471 pthread_mutex_lock(&app->sock_lock);
852d0037 1472 ret = ustctl_disable(app->sock, ua_event->obj);
fb45065e 1473 pthread_mutex_unlock(&app->sock_lock);
9730260e 1474 if (ret < 0) {
ffe60014
DG
1475 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1476 ERR("UST app event %s disable failed for app (pid: %d) "
1477 "and session handle %d with ret %d",
1478 ua_event->attr.name, app->pid, ua_sess->handle, ret);
1479 } else {
3757b385
DG
1480 /*
1481 * This is normal behavior, an application can die during the
1482 * creation process. Don't report an error so the execution can
1483 * continue normally.
1484 */
1485 ret = 0;
ffe60014
DG
1486 DBG3("UST app disable event failed. Application is dead.");
1487 }
9730260e
DG
1488 goto error;
1489 }
1490
1491 DBG2("UST app event %s disabled successfully for app (pid: %d)",
852d0037 1492 ua_event->attr.name, app->pid);
9730260e
DG
1493
1494error:
840cb59c 1495 health_code_update();
9730260e
DG
1496 return ret;
1497}
1498
78f0bacd
DG
1499/*
1500 * Disable the specified channel on to UST tracer for the UST session.
1501 */
1502static int disable_ust_channel(struct ust_app *app,
1503 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
1504{
1505 int ret;
1506
840cb59c 1507 health_code_update();
86acf0da 1508
fb45065e 1509 pthread_mutex_lock(&app->sock_lock);
852d0037 1510 ret = ustctl_disable(app->sock, ua_chan->obj);
fb45065e 1511 pthread_mutex_unlock(&app->sock_lock);
78f0bacd 1512 if (ret < 0) {
ffe60014
DG
1513 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1514 ERR("UST app channel %s disable failed for app (pid: %d) "
1515 "and session handle %d with ret %d",
1516 ua_chan->name, app->pid, ua_sess->handle, ret);
1517 } else {
3757b385
DG
1518 /*
1519 * This is normal behavior, an application can die during the
1520 * creation process. Don't report an error so the execution can
1521 * continue normally.
1522 */
1523 ret = 0;
ffe60014
DG
1524 DBG3("UST app disable channel failed. Application is dead.");
1525 }
78f0bacd
DG
1526 goto error;
1527 }
1528
78f0bacd 1529 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
852d0037 1530 ua_chan->name, app->pid);
78f0bacd
DG
1531
1532error:
840cb59c 1533 health_code_update();
78f0bacd
DG
1534 return ret;
1535}
1536
1537/*
1538 * Enable the specified channel on to UST tracer for the UST session.
1539 */
1540static int enable_ust_channel(struct ust_app *app,
1541 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
1542{
1543 int ret;
1544
840cb59c 1545 health_code_update();
86acf0da 1546
fb45065e 1547 pthread_mutex_lock(&app->sock_lock);
852d0037 1548 ret = ustctl_enable(app->sock, ua_chan->obj);
fb45065e 1549 pthread_mutex_unlock(&app->sock_lock);
78f0bacd 1550 if (ret < 0) {
ffe60014
DG
1551 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1552 ERR("UST app channel %s enable failed for app (pid: %d) "
1553 "and session handle %d with ret %d",
1554 ua_chan->name, app->pid, ua_sess->handle, ret);
1555 } else {
3757b385
DG
1556 /*
1557 * This is normal behavior, an application can die during the
1558 * creation process. Don't report an error so the execution can
1559 * continue normally.
1560 */
1561 ret = 0;
ffe60014
DG
1562 DBG3("UST app enable channel failed. Application is dead.");
1563 }
78f0bacd
DG
1564 goto error;
1565 }
1566
1567 ua_chan->enabled = 1;
1568
1569 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
852d0037 1570 ua_chan->name, app->pid);
78f0bacd
DG
1571
1572error:
840cb59c 1573 health_code_update();
78f0bacd
DG
1574 return ret;
1575}
1576
edb67388
DG
1577/*
1578 * Enable the specified event on to UST tracer for the UST session.
1579 */
1580static int enable_ust_event(struct ust_app *app,
1581 struct ust_app_session *ua_sess, struct ust_app_event *ua_event)
1582{
1583 int ret;
1584
840cb59c 1585 health_code_update();
86acf0da 1586
fb45065e 1587 pthread_mutex_lock(&app->sock_lock);
852d0037 1588 ret = ustctl_enable(app->sock, ua_event->obj);
fb45065e 1589 pthread_mutex_unlock(&app->sock_lock);
edb67388 1590 if (ret < 0) {
ffe60014
DG
1591 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1592 ERR("UST app event %s enable failed for app (pid: %d) "
1593 "and session handle %d with ret %d",
1594 ua_event->attr.name, app->pid, ua_sess->handle, ret);
1595 } else {
3757b385
DG
1596 /*
1597 * This is normal behavior, an application can die during the
1598 * creation process. Don't report an error so the execution can
1599 * continue normally.
1600 */
1601 ret = 0;
ffe60014
DG
1602 DBG3("UST app enable event failed. Application is dead.");
1603 }
edb67388
DG
1604 goto error;
1605 }
1606
1607 DBG2("UST app event %s enabled successfully for app (pid: %d)",
852d0037 1608 ua_event->attr.name, app->pid);
edb67388
DG
1609
1610error:
840cb59c 1611 health_code_update();
edb67388
DG
1612 return ret;
1613}
1614
099e26bd 1615/*
7972aab2 1616 * Send channel and stream buffer to application.
4f3ab6ee 1617 *
ffe60014 1618 * Return 0 on success. On error, a negative value is returned.
4f3ab6ee 1619 */
7972aab2
DG
1620static int send_channel_pid_to_ust(struct ust_app *app,
1621 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
4f3ab6ee
DG
1622{
1623 int ret;
ffe60014 1624 struct ust_app_stream *stream, *stmp;
4f3ab6ee
DG
1625
1626 assert(app);
ffe60014 1627 assert(ua_sess);
4f3ab6ee 1628 assert(ua_chan);
4f3ab6ee 1629
840cb59c 1630 health_code_update();
4f3ab6ee 1631
7972aab2
DG
1632 DBG("UST app sending channel %s to UST app sock %d", ua_chan->name,
1633 app->sock);
86acf0da 1634
ffe60014
DG
1635 /* Send channel to the application. */
1636 ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan);
a7169585
MD
1637 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1638 ret = -ENOTCONN; /* Caused by app exiting. */
1639 goto error;
1640 } else if (ret < 0) {
b551a063
DG
1641 goto error;
1642 }
1643
d88aee68
DG
1644 health_code_update();
1645
ffe60014
DG
1646 /* Send all streams to application. */
1647 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
1648 ret = ust_consumer_send_stream_to_ust(app, ua_chan, stream);
a7169585
MD
1649 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1650 ret = -ENOTCONN; /* Caused by app exiting. */
1651 goto error;
1652 } else if (ret < 0) {
ffe60014
DG
1653 goto error;
1654 }
1655 /* We don't need the stream anymore once sent to the tracer. */
1656 cds_list_del(&stream->list);
fb45065e 1657 delete_ust_app_stream(-1, stream, app);
ffe60014 1658 }
ffe60014
DG
1659 /* Flag the channel that it is sent to the application. */
1660 ua_chan->is_sent = 1;
ffe60014 1661
b551a063 1662error:
840cb59c 1663 health_code_update();
b551a063
DG
1664 return ret;
1665}
1666
91d76f53 1667/*
5b4a0ec0 1668 * Create the specified event onto the UST tracer for a UST session.
d0b96690
DG
1669 *
1670 * Should be called with session mutex held.
91d76f53 1671 */
edb67388
DG
1672static
1673int create_ust_event(struct ust_app *app, struct ust_app_session *ua_sess,
1674 struct ust_app_channel *ua_chan, struct ust_app_event *ua_event)
91d76f53 1675{
5b4a0ec0 1676 int ret = 0;
284d8f55 1677
840cb59c 1678 health_code_update();
86acf0da 1679
5b4a0ec0 1680 /* Create UST event on tracer */
fb45065e 1681 pthread_mutex_lock(&app->sock_lock);
852d0037 1682 ret = ustctl_create_event(app->sock, &ua_event->attr, ua_chan->obj,
5b4a0ec0 1683 &ua_event->obj);
fb45065e 1684 pthread_mutex_unlock(&app->sock_lock);
5b4a0ec0 1685 if (ret < 0) {
ffe60014 1686 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
88e3c2f5 1687 abort();
ffe60014
DG
1688 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1689 ua_event->attr.name, app->pid, ret);
1690 } else {
3757b385
DG
1691 /*
1692 * This is normal behavior, an application can die during the
1693 * creation process. Don't report an error so the execution can
1694 * continue normally.
1695 */
1696 ret = 0;
ffe60014
DG
1697 DBG3("UST app create event failed. Application is dead.");
1698 }
5b4a0ec0 1699 goto error;
91d76f53 1700 }
f6a9efaa 1701
5b4a0ec0 1702 ua_event->handle = ua_event->obj->handle;
284d8f55 1703
5b4a0ec0 1704 DBG2("UST app event %s created successfully for pid:%d",
852d0037 1705 ua_event->attr.name, app->pid);
f6a9efaa 1706
840cb59c 1707 health_code_update();
86acf0da 1708
025faf73
DG
1709 /* Set filter if one is present. */
1710 if (ua_event->filter) {
1711 ret = set_ust_event_filter(ua_event, app);
1712 if (ret < 0) {
1713 goto error;
1714 }
1715 }
1716
7cc9a73c
JI
1717 /* Set exclusions for the event */
1718 if (ua_event->exclusion) {
1719 ret = set_ust_event_exclusion(ua_event, app);
1720 if (ret < 0) {
1721 goto error;
1722 }
1723 }
1724
8535a6d9 1725 /* If event not enabled, disable it on the tracer */
40113787
MD
1726 if (ua_event->enabled) {
1727 /*
1728 * We now need to explicitly enable the event, since it
1729 * is now disabled at creation.
1730 */
1731 ret = enable_ust_event(app, ua_sess, ua_event);
1732 if (ret < 0) {
1733 /*
1734 * If we hit an EPERM, something is wrong with our enable call. If
1735 * we get an EEXIST, there is a problem on the tracer side since we
1736 * just created it.
1737 */
1738 switch (ret) {
1739 case -LTTNG_UST_ERR_PERM:
1740 /* Code flow problem */
1741 assert(0);
1742 case -LTTNG_UST_ERR_EXIST:
1743 /* It's OK for our use case. */
1744 ret = 0;
1745 break;
1746 default:
1747 break;
1748 }
1749 goto error;
1750 }
8535a6d9
DG
1751 }
1752
5b4a0ec0 1753error:
840cb59c 1754 health_code_update();
5b4a0ec0 1755 return ret;
91d76f53 1756}
48842b30 1757
5b4a0ec0
DG
1758/*
1759 * Copy data between an UST app event and a LTT event.
1760 */
421cb601 1761static void shadow_copy_event(struct ust_app_event *ua_event,
48842b30
DG
1762 struct ltt_ust_event *uevent)
1763{
b4ffad32
JI
1764 size_t exclusion_alloc_size;
1765
48842b30
DG
1766 strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name));
1767 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
1768
fc34caaa
DG
1769 ua_event->enabled = uevent->enabled;
1770
5b4a0ec0
DG
1771 /* Copy event attributes */
1772 memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr));
1773
53a80697
MD
1774 /* Copy filter bytecode */
1775 if (uevent->filter) {
51755dc8 1776 ua_event->filter = copy_filter_bytecode(uevent->filter);
025faf73 1777 /* Filter might be NULL here in case of ENONEM. */
53a80697 1778 }
b4ffad32
JI
1779
1780 /* Copy exclusion data */
1781 if (uevent->exclusion) {
51755dc8 1782 exclusion_alloc_size = sizeof(struct lttng_event_exclusion) +
b4ffad32
JI
1783 LTTNG_UST_SYM_NAME_LEN * uevent->exclusion->count;
1784 ua_event->exclusion = zmalloc(exclusion_alloc_size);
5f8df26c
JI
1785 if (ua_event->exclusion == NULL) {
1786 PERROR("malloc");
1787 } else {
1788 memcpy(ua_event->exclusion, uevent->exclusion,
1789 exclusion_alloc_size);
b4ffad32
JI
1790 }
1791 }
48842b30
DG
1792}
1793
5b4a0ec0
DG
1794/*
1795 * Copy data between an UST app channel and a LTT channel.
1796 */
421cb601 1797static void shadow_copy_channel(struct ust_app_channel *ua_chan,
48842b30
DG
1798 struct ltt_ust_channel *uchan)
1799{
fc34caaa 1800 DBG2("UST app shadow copy of channel %s started", ua_chan->name);
48842b30
DG
1801
1802 strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name));
1803 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
ffe60014 1804
1624d5b7
JD
1805 ua_chan->tracefile_size = uchan->tracefile_size;
1806 ua_chan->tracefile_count = uchan->tracefile_count;
1807
ffe60014
DG
1808 /* Copy event attributes since the layout is different. */
1809 ua_chan->attr.subbuf_size = uchan->attr.subbuf_size;
1810 ua_chan->attr.num_subbuf = uchan->attr.num_subbuf;
1811 ua_chan->attr.overwrite = uchan->attr.overwrite;
1812 ua_chan->attr.switch_timer_interval = uchan->attr.switch_timer_interval;
1813 ua_chan->attr.read_timer_interval = uchan->attr.read_timer_interval;
e9404c27 1814 ua_chan->monitor_timer_interval = uchan->monitor_timer_interval;
ffe60014 1815 ua_chan->attr.output = uchan->attr.output;
491d1539
MD
1816 ua_chan->attr.blocking_timeout = uchan->attr.u.s.blocking_timeout;
1817
ffe60014
DG
1818 /*
1819 * Note that the attribute channel type is not set since the channel on the
1820 * tracing registry side does not have this information.
1821 */
48842b30 1822
fc34caaa 1823 ua_chan->enabled = uchan->enabled;
7972aab2 1824 ua_chan->tracing_channel_id = uchan->id;
fc34caaa 1825
fc34caaa 1826 DBG3("UST app shadow copy of channel %s done", ua_chan->name);
48842b30
DG
1827}
1828
5b4a0ec0
DG
1829/*
1830 * Copy data between a UST app session and a regular LTT session.
1831 */
421cb601 1832static void shadow_copy_session(struct ust_app_session *ua_sess,
bec39940 1833 struct ltt_ust_session *usess, struct ust_app *app)
48842b30 1834{
477d7741
MD
1835 struct tm *timeinfo;
1836 char datetime[16];
1837 int ret;
d7ba1388 1838 char tmp_shm_path[PATH_MAX];
477d7741 1839
940c4592 1840 timeinfo = localtime(&app->registration_time);
477d7741 1841 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
48842b30 1842
421cb601 1843 DBG2("Shadow copy of session handle %d", ua_sess->handle);
48842b30 1844
7972aab2
DG
1845 ua_sess->tracing_id = usess->id;
1846 ua_sess->id = get_next_session_id();
470cc211
JG
1847 ua_sess->real_credentials.uid = app->uid;
1848 ua_sess->real_credentials.gid = app->gid;
1849 ua_sess->effective_credentials.uid = usess->uid;
1850 ua_sess->effective_credentials.gid = usess->gid;
7972aab2
DG
1851 ua_sess->buffer_type = usess->buffer_type;
1852 ua_sess->bits_per_long = app->bits_per_long;
6addfa37 1853
7972aab2 1854 /* There is only one consumer object per session possible. */
6addfa37 1855 consumer_output_get(usess->consumer);
7972aab2 1856 ua_sess->consumer = usess->consumer;
6addfa37 1857
2bba9e53 1858 ua_sess->output_traces = usess->output_traces;
ecc48a90 1859 ua_sess->live_timer_interval = usess->live_timer_interval;
84ad93e8
DG
1860 copy_channel_attr_to_ustctl(&ua_sess->metadata_attr,
1861 &usess->metadata_attr);
7972aab2
DG
1862
1863 switch (ua_sess->buffer_type) {
1864 case LTTNG_BUFFER_PER_PID:
1865 ret = snprintf(ua_sess->path, sizeof(ua_sess->path),
dec56f6c 1866 DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s", app->name, app->pid,
7972aab2
DG
1867 datetime);
1868 break;
1869 case LTTNG_BUFFER_PER_UID:
1870 ret = snprintf(ua_sess->path, sizeof(ua_sess->path),
470cc211
JG
1871 DEFAULT_UST_TRACE_UID_PATH,
1872 ua_sess->real_credentials.uid,
1873 app->bits_per_long);
7972aab2
DG
1874 break;
1875 default:
1876 assert(0);
1877 goto error;
1878 }
477d7741
MD
1879 if (ret < 0) {
1880 PERROR("asprintf UST shadow copy session");
477d7741 1881 assert(0);
7972aab2 1882 goto error;
477d7741
MD
1883 }
1884
3d071855
MD
1885 strncpy(ua_sess->root_shm_path, usess->root_shm_path,
1886 sizeof(ua_sess->root_shm_path));
1887 ua_sess->root_shm_path[sizeof(ua_sess->root_shm_path) - 1] = '\0';
d7ba1388
MD
1888 strncpy(ua_sess->shm_path, usess->shm_path,
1889 sizeof(ua_sess->shm_path));
1890 ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0';
1891 if (ua_sess->shm_path[0]) {
1892 switch (ua_sess->buffer_type) {
1893 case LTTNG_BUFFER_PER_PID:
1894 ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path),
1895 DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s",
1896 app->name, app->pid, datetime);
1897 break;
1898 case LTTNG_BUFFER_PER_UID:
1899 ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path),
1900 DEFAULT_UST_TRACE_UID_PATH,
1901 app->uid, app->bits_per_long);
1902 break;
1903 default:
1904 assert(0);
1905 goto error;
1906 }
1907 if (ret < 0) {
1908 PERROR("sprintf UST shadow copy session");
1909 assert(0);
1910 goto error;
1911 }
1912 strncat(ua_sess->shm_path, tmp_shm_path,
1913 sizeof(ua_sess->shm_path) - strlen(ua_sess->shm_path) - 1);
1914 ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0';
1915 }
6addfa37 1916 return;
7972aab2
DG
1917
1918error:
6addfa37 1919 consumer_output_put(ua_sess->consumer);
48842b30
DG
1920}
1921
78f0bacd
DG
1922/*
1923 * Lookup sesison wrapper.
1924 */
84cd17c6 1925static
fb9a95c4 1926void __lookup_session_by_app(const struct ltt_ust_session *usess,
bec39940 1927 struct ust_app *app, struct lttng_ht_iter *iter)
84cd17c6
MD
1928{
1929 /* Get right UST app session from app */
d9bf3ca4 1930 lttng_ht_lookup(app->sessions, &usess->id, iter);
84cd17c6
MD
1931}
1932
421cb601
DG
1933/*
1934 * Return ust app session from the app session hashtable using the UST session
a991f516 1935 * id.
421cb601 1936 */
48842b30 1937static struct ust_app_session *lookup_session_by_app(
fb9a95c4 1938 const struct ltt_ust_session *usess, struct ust_app *app)
48842b30 1939{
bec39940 1940 struct lttng_ht_iter iter;
d9bf3ca4 1941 struct lttng_ht_node_u64 *node;
48842b30 1942
84cd17c6 1943 __lookup_session_by_app(usess, app, &iter);
d9bf3ca4 1944 node = lttng_ht_iter_get_node_u64(&iter);
48842b30
DG
1945 if (node == NULL) {
1946 goto error;
1947 }
1948
1949 return caa_container_of(node, struct ust_app_session, node);
1950
1951error:
1952 return NULL;
1953}
1954
7972aab2
DG
1955/*
1956 * Setup buffer registry per PID for the given session and application. If none
1957 * is found, a new one is created, added to the global registry and
1958 * initialized. If regp is valid, it's set with the newly created object.
1959 *
1960 * Return 0 on success or else a negative value.
1961 */
1962static int setup_buffer_reg_pid(struct ust_app_session *ua_sess,
1963 struct ust_app *app, struct buffer_reg_pid **regp)
1964{
1965 int ret = 0;
1966 struct buffer_reg_pid *reg_pid;
1967
1968 assert(ua_sess);
1969 assert(app);
1970
1971 rcu_read_lock();
1972
1973 reg_pid = buffer_reg_pid_find(ua_sess->id);
1974 if (!reg_pid) {
1975 /*
1976 * This is the create channel path meaning that if there is NO
1977 * registry available, we have to create one for this session.
1978 */
d7ba1388 1979 ret = buffer_reg_pid_create(ua_sess->id, &reg_pid,
3d071855 1980 ua_sess->root_shm_path, ua_sess->shm_path);
7972aab2
DG
1981 if (ret < 0) {
1982 goto error;
1983 }
7972aab2
DG
1984 } else {
1985 goto end;
1986 }
1987
1988 /* Initialize registry. */
1989 ret = ust_registry_session_init(&reg_pid->registry->reg.ust, app,
1990 app->bits_per_long, app->uint8_t_alignment,
1991 app->uint16_t_alignment, app->uint32_t_alignment,
af6142cf 1992 app->uint64_t_alignment, app->long_alignment,
470cc211
JG
1993 app->byte_order, app->version.major, app->version.minor,
1994 reg_pid->root_shm_path, reg_pid->shm_path,
1995 ua_sess->effective_credentials.uid,
1996 ua_sess->effective_credentials.gid);
7972aab2 1997 if (ret < 0) {
286c991a
MD
1998 /*
1999 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2000 * destroy the buffer registry, because it is always expected
2001 * that if the buffer registry can be found, its ust registry is
2002 * non-NULL.
2003 */
2004 buffer_reg_pid_destroy(reg_pid);
7972aab2
DG
2005 goto error;
2006 }
2007
286c991a
MD
2008 buffer_reg_pid_add(reg_pid);
2009
7972aab2
DG
2010 DBG3("UST app buffer registry per PID created successfully");
2011
2012end:
2013 if (regp) {
2014 *regp = reg_pid;
2015 }
2016error:
2017 rcu_read_unlock();
2018 return ret;
2019}
2020
2021/*
2022 * Setup buffer registry per UID for the given session and application. If none
2023 * is found, a new one is created, added to the global registry and
2024 * initialized. If regp is valid, it's set with the newly created object.
2025 *
2026 * Return 0 on success or else a negative value.
2027 */
2028static int setup_buffer_reg_uid(struct ltt_ust_session *usess,
d7ba1388 2029 struct ust_app_session *ua_sess,
7972aab2
DG
2030 struct ust_app *app, struct buffer_reg_uid **regp)
2031{
2032 int ret = 0;
2033 struct buffer_reg_uid *reg_uid;
2034
2035 assert(usess);
2036 assert(app);
2037
2038 rcu_read_lock();
2039
2040 reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid);
2041 if (!reg_uid) {
2042 /*
2043 * This is the create channel path meaning that if there is NO
2044 * registry available, we have to create one for this session.
2045 */
2046 ret = buffer_reg_uid_create(usess->id, app->bits_per_long, app->uid,
3d071855
MD
2047 LTTNG_DOMAIN_UST, &reg_uid,
2048 ua_sess->root_shm_path, ua_sess->shm_path);
7972aab2
DG
2049 if (ret < 0) {
2050 goto error;
2051 }
7972aab2
DG
2052 } else {
2053 goto end;
2054 }
2055
2056 /* Initialize registry. */
af6142cf 2057 ret = ust_registry_session_init(&reg_uid->registry->reg.ust, NULL,
7972aab2
DG
2058 app->bits_per_long, app->uint8_t_alignment,
2059 app->uint16_t_alignment, app->uint32_t_alignment,
af6142cf
MD
2060 app->uint64_t_alignment, app->long_alignment,
2061 app->byte_order, app->version.major,
3d071855
MD
2062 app->version.minor, reg_uid->root_shm_path,
2063 reg_uid->shm_path, usess->uid, usess->gid);
7972aab2 2064 if (ret < 0) {
286c991a
MD
2065 /*
2066 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2067 * destroy the buffer registry, because it is always expected
2068 * that if the buffer registry can be found, its ust registry is
2069 * non-NULL.
2070 */
2071 buffer_reg_uid_destroy(reg_uid, NULL);
7972aab2
DG
2072 goto error;
2073 }
2074 /* Add node to teardown list of the session. */
2075 cds_list_add(&reg_uid->lnode, &usess->buffer_reg_uid_list);
2076
286c991a 2077 buffer_reg_uid_add(reg_uid);
7972aab2 2078
286c991a 2079 DBG3("UST app buffer registry per UID created successfully");
7972aab2
DG
2080end:
2081 if (regp) {
2082 *regp = reg_uid;
2083 }
2084error:
2085 rcu_read_unlock();
2086 return ret;
2087}
2088
421cb601 2089/*
3d8ca23b 2090 * Create a session on the tracer side for the given app.
421cb601 2091 *
3d8ca23b
DG
2092 * On success, ua_sess_ptr is populated with the session pointer or else left
2093 * untouched. If the session was created, is_created is set to 1. On error,
2094 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2095 * be NULL.
2096 *
2097 * Returns 0 on success or else a negative code which is either -ENOMEM or
2098 * -ENOTCONN which is the default code if the ustctl_create_session fails.
421cb601 2099 */
03f91eaa 2100static int find_or_create_ust_app_session(struct ltt_ust_session *usess,
3d8ca23b
DG
2101 struct ust_app *app, struct ust_app_session **ua_sess_ptr,
2102 int *is_created)
421cb601 2103{
3d8ca23b 2104 int ret, created = 0;
421cb601
DG
2105 struct ust_app_session *ua_sess;
2106
3d8ca23b
DG
2107 assert(usess);
2108 assert(app);
2109 assert(ua_sess_ptr);
2110
840cb59c 2111 health_code_update();
86acf0da 2112
421cb601
DG
2113 ua_sess = lookup_session_by_app(usess, app);
2114 if (ua_sess == NULL) {
d9bf3ca4 2115 DBG2("UST app pid: %d session id %" PRIu64 " not found, creating it",
852d0037 2116 app->pid, usess->id);
40bbd087 2117 ua_sess = alloc_ust_app_session();
421cb601
DG
2118 if (ua_sess == NULL) {
2119 /* Only malloc can failed so something is really wrong */
3d8ca23b
DG
2120 ret = -ENOMEM;
2121 goto error;
421cb601 2122 }
477d7741 2123 shadow_copy_session(ua_sess, usess, app);
3d8ca23b 2124 created = 1;
421cb601
DG
2125 }
2126
7972aab2
DG
2127 switch (usess->buffer_type) {
2128 case LTTNG_BUFFER_PER_PID:
2129 /* Init local registry. */
2130 ret = setup_buffer_reg_pid(ua_sess, app, NULL);
421cb601 2131 if (ret < 0) {
e64207cf 2132 delete_ust_app_session(-1, ua_sess, app);
7972aab2
DG
2133 goto error;
2134 }
2135 break;
2136 case LTTNG_BUFFER_PER_UID:
2137 /* Look for a global registry. If none exists, create one. */
d7ba1388 2138 ret = setup_buffer_reg_uid(usess, ua_sess, app, NULL);
7972aab2 2139 if (ret < 0) {
e64207cf 2140 delete_ust_app_session(-1, ua_sess, app);
7972aab2
DG
2141 goto error;
2142 }
2143 break;
2144 default:
2145 assert(0);
2146 ret = -EINVAL;
2147 goto error;
2148 }
2149
2150 health_code_update();
2151
2152 if (ua_sess->handle == -1) {
fb45065e 2153 pthread_mutex_lock(&app->sock_lock);
7972aab2 2154 ret = ustctl_create_session(app->sock);
fb45065e 2155 pthread_mutex_unlock(&app->sock_lock);
7972aab2
DG
2156 if (ret < 0) {
2157 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
2158 ERR("Creating session for app pid %d with ret %d",
ffe60014
DG
2159 app->pid, ret);
2160 } else {
2161 DBG("UST app creating session failed. Application is dead");
3757b385
DG
2162 /*
2163 * This is normal behavior, an application can die during the
2164 * creation process. Don't report an error so the execution can
2165 * continue normally. This will get flagged ENOTCONN and the
2166 * caller will handle it.
2167 */
2168 ret = 0;
ffe60014 2169 }
d0b96690 2170 delete_ust_app_session(-1, ua_sess, app);
3d8ca23b
DG
2171 if (ret != -ENOMEM) {
2172 /*
2173 * Tracer is probably gone or got an internal error so let's
2174 * behave like it will soon unregister or not usable.
2175 */
2176 ret = -ENOTCONN;
2177 }
2178 goto error;
421cb601
DG
2179 }
2180
7972aab2
DG
2181 ua_sess->handle = ret;
2182
2183 /* Add ust app session to app's HT */
d9bf3ca4
MD
2184 lttng_ht_node_init_u64(&ua_sess->node,
2185 ua_sess->tracing_id);
2186 lttng_ht_add_unique_u64(app->sessions, &ua_sess->node);
10b56aef
MD
2187 lttng_ht_node_init_ulong(&ua_sess->ust_objd_node, ua_sess->handle);
2188 lttng_ht_add_unique_ulong(app->ust_sessions_objd,
2189 &ua_sess->ust_objd_node);
7972aab2
DG
2190
2191 DBG2("UST app session created successfully with handle %d", ret);
2192 }
2193
2194 *ua_sess_ptr = ua_sess;
2195 if (is_created) {
2196 *is_created = created;
2197 }
2198
2199 /* Everything went well. */
2200 ret = 0;
2201
2202error:
2203 health_code_update();
2204 return ret;
2205}
2206
6a6b2068
JG
2207/*
2208 * Match function for a hash table lookup of ust_app_ctx.
2209 *
2210 * It matches an ust app context based on the context type and, in the case
2211 * of perf counters, their name.
2212 */
2213static int ht_match_ust_app_ctx(struct cds_lfht_node *node, const void *_key)
2214{
2215 struct ust_app_ctx *ctx;
bdf64013 2216 const struct lttng_ust_context_attr *key;
6a6b2068
JG
2217
2218 assert(node);
2219 assert(_key);
2220
2221 ctx = caa_container_of(node, struct ust_app_ctx, node.node);
2222 key = _key;
2223
2224 /* Context type */
2225 if (ctx->ctx.ctx != key->ctx) {
2226 goto no_match;
2227 }
2228
bdf64013
JG
2229 switch(key->ctx) {
2230 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER:
6a6b2068 2231 if (strncmp(key->u.perf_counter.name,
bdf64013
JG
2232 ctx->ctx.u.perf_counter.name,
2233 sizeof(key->u.perf_counter.name))) {
2234 goto no_match;
2235 }
2236 break;
2237 case LTTNG_UST_CONTEXT_APP_CONTEXT:
2238 if (strcmp(key->u.app_ctx.provider_name,
2239 ctx->ctx.u.app_ctx.provider_name) ||
2240 strcmp(key->u.app_ctx.ctx_name,
2241 ctx->ctx.u.app_ctx.ctx_name)) {
6a6b2068
JG
2242 goto no_match;
2243 }
bdf64013
JG
2244 break;
2245 default:
2246 break;
6a6b2068
JG
2247 }
2248
2249 /* Match. */
2250 return 1;
2251
2252no_match:
2253 return 0;
2254}
2255
2256/*
2257 * Lookup for an ust app context from an lttng_ust_context.
2258 *
be184a0f 2259 * Must be called while holding RCU read side lock.
6a6b2068
JG
2260 * Return an ust_app_ctx object or NULL on error.
2261 */
2262static
2263struct ust_app_ctx *find_ust_app_context(struct lttng_ht *ht,
bdf64013 2264 struct lttng_ust_context_attr *uctx)
6a6b2068
JG
2265{
2266 struct lttng_ht_iter iter;
2267 struct lttng_ht_node_ulong *node;
2268 struct ust_app_ctx *app_ctx = NULL;
2269
2270 assert(uctx);
2271 assert(ht);
2272
2273 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2274 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) uctx->ctx, lttng_ht_seed),
2275 ht_match_ust_app_ctx, uctx, &iter.iter);
2276 node = lttng_ht_iter_get_node_ulong(&iter);
2277 if (!node) {
2278 goto end;
2279 }
2280
2281 app_ctx = caa_container_of(node, struct ust_app_ctx, node);
2282
2283end:
2284 return app_ctx;
2285}
2286
7972aab2
DG
2287/*
2288 * Create a context for the channel on the tracer.
2289 *
2290 * Called with UST app session lock held and a RCU read side lock.
2291 */
2292static
c9edf082 2293int create_ust_app_channel_context(struct ust_app_channel *ua_chan,
bdf64013 2294 struct lttng_ust_context_attr *uctx,
7972aab2
DG
2295 struct ust_app *app)
2296{
2297 int ret = 0;
7972aab2
DG
2298 struct ust_app_ctx *ua_ctx;
2299
2300 DBG2("UST app adding context to channel %s", ua_chan->name);
2301
6a6b2068
JG
2302 ua_ctx = find_ust_app_context(ua_chan->ctx, uctx);
2303 if (ua_ctx) {
7972aab2
DG
2304 ret = -EEXIST;
2305 goto error;
2306 }
2307
2308 ua_ctx = alloc_ust_app_ctx(uctx);
2309 if (ua_ctx == NULL) {
2310 /* malloc failed */
7682f304 2311 ret = -ENOMEM;
7972aab2
DG
2312 goto error;
2313 }
2314
2315 lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx);
aa3514e9 2316 lttng_ht_add_ulong(ua_chan->ctx, &ua_ctx->node);
31746f93 2317 cds_list_add_tail(&ua_ctx->list, &ua_chan->ctx_list);
7972aab2
DG
2318
2319 ret = create_ust_channel_context(ua_chan, ua_ctx, app);
2320 if (ret < 0) {
2321 goto error;
2322 }
2323
2324error:
2325 return ret;
2326}
2327
2328/*
2329 * Enable on the tracer side a ust app event for the session and channel.
2330 *
2331 * Called with UST app session lock held.
2332 */
2333static
2334int enable_ust_app_event(struct ust_app_session *ua_sess,
2335 struct ust_app_event *ua_event, struct ust_app *app)
2336{
2337 int ret;
2338
2339 ret = enable_ust_event(app, ua_sess, ua_event);
2340 if (ret < 0) {
2341 goto error;
2342 }
2343
2344 ua_event->enabled = 1;
2345
2346error:
2347 return ret;
2348}
2349
2350/*
2351 * Disable on the tracer side a ust app event for the session and channel.
2352 */
2353static int disable_ust_app_event(struct ust_app_session *ua_sess,
2354 struct ust_app_event *ua_event, struct ust_app *app)
2355{
2356 int ret;
2357
2358 ret = disable_ust_event(app, ua_sess, ua_event);
2359 if (ret < 0) {
2360 goto error;
2361 }
2362
2363 ua_event->enabled = 0;
2364
2365error:
2366 return ret;
2367}
2368
2369/*
2370 * Lookup ust app channel for session and disable it on the tracer side.
2371 */
2372static
2373int disable_ust_app_channel(struct ust_app_session *ua_sess,
2374 struct ust_app_channel *ua_chan, struct ust_app *app)
2375{
2376 int ret;
2377
2378 ret = disable_ust_channel(app, ua_sess, ua_chan);
2379 if (ret < 0) {
2380 goto error;
2381 }
2382
2383 ua_chan->enabled = 0;
2384
2385error:
2386 return ret;
2387}
2388
2389/*
2390 * Lookup ust app channel for session and enable it on the tracer side. This
2391 * MUST be called with a RCU read side lock acquired.
2392 */
2393static int enable_ust_app_channel(struct ust_app_session *ua_sess,
2394 struct ltt_ust_channel *uchan, struct ust_app *app)
2395{
2396 int ret = 0;
2397 struct lttng_ht_iter iter;
2398 struct lttng_ht_node_str *ua_chan_node;
2399 struct ust_app_channel *ua_chan;
2400
2401 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
2402 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
2403 if (ua_chan_node == NULL) {
d9bf3ca4 2404 DBG2("Unable to find channel %s in ust session id %" PRIu64,
7972aab2
DG
2405 uchan->name, ua_sess->tracing_id);
2406 goto error;
2407 }
2408
2409 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2410
2411 ret = enable_ust_channel(app, ua_sess, ua_chan);
2412 if (ret < 0) {
2413 goto error;
2414 }
2415
2416error:
2417 return ret;
2418}
2419
2420/*
2421 * Ask the consumer to create a channel and get it if successful.
2422 *
fad1ed2f
JR
2423 * Called with UST app session lock held.
2424 *
7972aab2
DG
2425 * Return 0 on success or else a negative value.
2426 */
2427static int do_consumer_create_channel(struct ltt_ust_session *usess,
2428 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan,
e098433c
JG
2429 int bitness, struct ust_registry_session *registry,
2430 uint64_t trace_archive_id)
7972aab2
DG
2431{
2432 int ret;
2433 unsigned int nb_fd = 0;
2434 struct consumer_socket *socket;
2435
2436 assert(usess);
2437 assert(ua_sess);
2438 assert(ua_chan);
2439 assert(registry);
2440
2441 rcu_read_lock();
2442 health_code_update();
2443
2444 /* Get the right consumer socket for the application. */
2445 socket = consumer_find_socket_by_bitness(bitness, usess->consumer);
2446 if (!socket) {
2447 ret = -EINVAL;
2448 goto error;
2449 }
2450
2451 health_code_update();
2452
2453 /* Need one fd for the channel. */
2454 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
2455 if (ret < 0) {
2456 ERR("Exhausted number of available FD upon create channel");
2457 goto error;
2458 }
2459
2460 /*
2461 * Ask consumer to create channel. The consumer will return the number of
2462 * stream we have to expect.
2463 */
2464 ret = ust_consumer_ask_channel(ua_sess, ua_chan, usess->consumer, socket,
d2956687 2465 registry, usess->current_trace_chunk);
7972aab2
DG
2466 if (ret < 0) {
2467 goto error_ask;
2468 }
2469
2470 /*
2471 * Compute the number of fd needed before receiving them. It must be 2 per
2472 * stream (2 being the default value here).
2473 */
2474 nb_fd = DEFAULT_UST_STREAM_FD_NUM * ua_chan->expected_stream_count;
2475
2476 /* Reserve the amount of file descriptor we need. */
2477 ret = lttng_fd_get(LTTNG_FD_APPS, nb_fd);
2478 if (ret < 0) {
2479 ERR("Exhausted number of available FD upon create channel");
2480 goto error_fd_get_stream;
2481 }
2482
2483 health_code_update();
2484
2485 /*
2486 * Now get the channel from the consumer. This call wil populate the stream
2487 * list of that channel and set the ust objects.
2488 */
d9078d0c
DG
2489 if (usess->consumer->enabled) {
2490 ret = ust_consumer_get_channel(socket, ua_chan);
2491 if (ret < 0) {
2492 goto error_destroy;
2493 }
7972aab2
DG
2494 }
2495
2496 rcu_read_unlock();
2497 return 0;
2498
2499error_destroy:
2500 lttng_fd_put(LTTNG_FD_APPS, nb_fd);
2501error_fd_get_stream:
2502 /*
2503 * Initiate a destroy channel on the consumer since we had an error
2504 * handling it on our side. The return value is of no importance since we
2505 * already have a ret value set by the previous error that we need to
2506 * return.
2507 */
2508 (void) ust_consumer_destroy_channel(socket, ua_chan);
2509error_ask:
2510 lttng_fd_put(LTTNG_FD_APPS, 1);
2511error:
2512 health_code_update();
2513 rcu_read_unlock();
2514 return ret;
2515}
2516
2517/*
2518 * Duplicate the ust data object of the ust app stream and save it in the
2519 * buffer registry stream.
2520 *
2521 * Return 0 on success or else a negative value.
2522 */
2523static int duplicate_stream_object(struct buffer_reg_stream *reg_stream,
2524 struct ust_app_stream *stream)
2525{
2526 int ret;
2527
2528 assert(reg_stream);
2529 assert(stream);
2530
2531 /* Reserve the amount of file descriptor we need. */
2532 ret = lttng_fd_get(LTTNG_FD_APPS, 2);
2533 if (ret < 0) {
2534 ERR("Exhausted number of available FD upon duplicate stream");
2535 goto error;
2536 }
2537
2538 /* Duplicate object for stream once the original is in the registry. */
2539 ret = ustctl_duplicate_ust_object_data(&stream->obj,
2540 reg_stream->obj.ust);
2541 if (ret < 0) {
2542 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2543 reg_stream->obj.ust, stream->obj, ret);
2544 lttng_fd_put(LTTNG_FD_APPS, 2);
2545 goto error;
2546 }
2547 stream->handle = stream->obj->handle;
2548
2549error:
2550 return ret;
2551}
2552
2553/*
2554 * Duplicate the ust data object of the ust app. channel and save it in the
2555 * buffer registry channel.
2556 *
2557 * Return 0 on success or else a negative value.
2558 */
2559static int duplicate_channel_object(struct buffer_reg_channel *reg_chan,
2560 struct ust_app_channel *ua_chan)
2561{
2562 int ret;
2563
2564 assert(reg_chan);
2565 assert(ua_chan);
2566
2567 /* Need two fds for the channel. */
2568 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
2569 if (ret < 0) {
2570 ERR("Exhausted number of available FD upon duplicate channel");
2571 goto error_fd_get;
2572 }
2573
2574 /* Duplicate object for stream once the original is in the registry. */
2575 ret = ustctl_duplicate_ust_object_data(&ua_chan->obj, reg_chan->obj.ust);
2576 if (ret < 0) {
2577 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2578 reg_chan->obj.ust, ua_chan->obj, ret);
2579 goto error;
2580 }
2581 ua_chan->handle = ua_chan->obj->handle;
2582
2583 return 0;
2584
2585error:
2586 lttng_fd_put(LTTNG_FD_APPS, 1);
2587error_fd_get:
2588 return ret;
2589}
2590
2591/*
2592 * For a given channel buffer registry, setup all streams of the given ust
2593 * application channel.
2594 *
2595 * Return 0 on success or else a negative value.
2596 */
2597static int setup_buffer_reg_streams(struct buffer_reg_channel *reg_chan,
fb45065e
MD
2598 struct ust_app_channel *ua_chan,
2599 struct ust_app *app)
7972aab2
DG
2600{
2601 int ret = 0;
2602 struct ust_app_stream *stream, *stmp;
2603
2604 assert(reg_chan);
2605 assert(ua_chan);
2606
2607 DBG2("UST app setup buffer registry stream");
2608
2609 /* Send all streams to application. */
2610 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
2611 struct buffer_reg_stream *reg_stream;
2612
2613 ret = buffer_reg_stream_create(&reg_stream);
2614 if (ret < 0) {
2615 goto error;
2616 }
2617
2618 /*
2619 * Keep original pointer and nullify it in the stream so the delete
2620 * stream call does not release the object.
2621 */
2622 reg_stream->obj.ust = stream->obj;
2623 stream->obj = NULL;
2624 buffer_reg_stream_add(reg_stream, reg_chan);
421cb601 2625
7972aab2
DG
2626 /* We don't need the streams anymore. */
2627 cds_list_del(&stream->list);
fb45065e 2628 delete_ust_app_stream(-1, stream, app);
7972aab2 2629 }
421cb601 2630
7972aab2
DG
2631error:
2632 return ret;
2633}
2634
2635/*
2636 * Create a buffer registry channel for the given session registry and
2637 * application channel object. If regp pointer is valid, it's set with the
2638 * created object. Important, the created object is NOT added to the session
2639 * registry hash table.
2640 *
2641 * Return 0 on success else a negative value.
2642 */
2643static int create_buffer_reg_channel(struct buffer_reg_session *reg_sess,
2644 struct ust_app_channel *ua_chan, struct buffer_reg_channel **regp)
2645{
2646 int ret;
2647 struct buffer_reg_channel *reg_chan = NULL;
2648
2649 assert(reg_sess);
2650 assert(ua_chan);
2651
2652 DBG2("UST app creating buffer registry channel for %s", ua_chan->name);
2653
2654 /* Create buffer registry channel. */
2655 ret = buffer_reg_channel_create(ua_chan->tracing_channel_id, &reg_chan);
2656 if (ret < 0) {
2657 goto error_create;
421cb601 2658 }
7972aab2
DG
2659 assert(reg_chan);
2660 reg_chan->consumer_key = ua_chan->key;
8c924c7b 2661 reg_chan->subbuf_size = ua_chan->attr.subbuf_size;
d07ceecd 2662 reg_chan->num_subbuf = ua_chan->attr.num_subbuf;
421cb601 2663
7972aab2
DG
2664 /* Create and add a channel registry to session. */
2665 ret = ust_registry_channel_add(reg_sess->reg.ust,
2666 ua_chan->tracing_channel_id);
2667 if (ret < 0) {
2668 goto error;
d88aee68 2669 }
7972aab2 2670 buffer_reg_channel_add(reg_sess, reg_chan);
d88aee68 2671
7972aab2
DG
2672 if (regp) {
2673 *regp = reg_chan;
3d8ca23b 2674 }
d88aee68 2675
7972aab2 2676 return 0;
3d8ca23b
DG
2677
2678error:
7972aab2
DG
2679 /* Safe because the registry channel object was not added to any HT. */
2680 buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST);
2681error_create:
3d8ca23b 2682 return ret;
421cb601
DG
2683}
2684
55cc08a6 2685/*
7972aab2
DG
2686 * Setup buffer registry channel for the given session registry and application
2687 * channel object. If regp pointer is valid, it's set with the created object.
d0b96690 2688 *
7972aab2 2689 * Return 0 on success else a negative value.
55cc08a6 2690 */
7972aab2 2691static int setup_buffer_reg_channel(struct buffer_reg_session *reg_sess,
fb45065e
MD
2692 struct ust_app_channel *ua_chan, struct buffer_reg_channel *reg_chan,
2693 struct ust_app *app)
55cc08a6 2694{
7972aab2 2695 int ret;
55cc08a6 2696
7972aab2
DG
2697 assert(reg_sess);
2698 assert(reg_chan);
2699 assert(ua_chan);
2700 assert(ua_chan->obj);
55cc08a6 2701
7972aab2 2702 DBG2("UST app setup buffer registry channel for %s", ua_chan->name);
55cc08a6 2703
7972aab2 2704 /* Setup all streams for the registry. */
fb45065e 2705 ret = setup_buffer_reg_streams(reg_chan, ua_chan, app);
7972aab2 2706 if (ret < 0) {
55cc08a6
DG
2707 goto error;
2708 }
2709
7972aab2
DG
2710 reg_chan->obj.ust = ua_chan->obj;
2711 ua_chan->obj = NULL;
55cc08a6 2712
7972aab2 2713 return 0;
55cc08a6
DG
2714
2715error:
7972aab2
DG
2716 buffer_reg_channel_remove(reg_sess, reg_chan);
2717 buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST);
55cc08a6
DG
2718 return ret;
2719}
2720
edb67388 2721/*
7972aab2 2722 * Send buffer registry channel to the application.
d0b96690 2723 *
7972aab2 2724 * Return 0 on success else a negative value.
edb67388 2725 */
7972aab2
DG
2726static int send_channel_uid_to_ust(struct buffer_reg_channel *reg_chan,
2727 struct ust_app *app, struct ust_app_session *ua_sess,
2728 struct ust_app_channel *ua_chan)
edb67388
DG
2729{
2730 int ret;
7972aab2 2731 struct buffer_reg_stream *reg_stream;
edb67388 2732
7972aab2
DG
2733 assert(reg_chan);
2734 assert(app);
2735 assert(ua_sess);
2736 assert(ua_chan);
2737
2738 DBG("UST app sending buffer registry channel to ust sock %d", app->sock);
2739
2740 ret = duplicate_channel_object(reg_chan, ua_chan);
edb67388
DG
2741 if (ret < 0) {
2742 goto error;
2743 }
2744
7972aab2
DG
2745 /* Send channel to the application. */
2746 ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan);
a7169585
MD
2747 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
2748 ret = -ENOTCONN; /* Caused by app exiting. */
2749 goto error;
2750 } else if (ret < 0) {
7972aab2
DG
2751 goto error;
2752 }
2753
2754 health_code_update();
2755
2756 /* Send all streams to application. */
2757 pthread_mutex_lock(&reg_chan->stream_list_lock);
2758 cds_list_for_each_entry(reg_stream, &reg_chan->streams, lnode) {
2759 struct ust_app_stream stream;
2760
2761 ret = duplicate_stream_object(reg_stream, &stream);
2762 if (ret < 0) {
2763 goto error_stream_unlock;
2764 }
2765
2766 ret = ust_consumer_send_stream_to_ust(app, ua_chan, &stream);
2767 if (ret < 0) {
fb45065e 2768 (void) release_ust_app_stream(-1, &stream, app);
a7169585
MD
2769 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
2770 ret = -ENOTCONN; /* Caused by app exiting. */
a7169585 2771 }
7972aab2
DG
2772 goto error_stream_unlock;
2773 }
edb67388 2774
7972aab2
DG
2775 /*
2776 * The return value is not important here. This function will output an
2777 * error if needed.
2778 */
fb45065e 2779 (void) release_ust_app_stream(-1, &stream, app);
7972aab2
DG
2780 }
2781 ua_chan->is_sent = 1;
2782
2783error_stream_unlock:
2784 pthread_mutex_unlock(&reg_chan->stream_list_lock);
edb67388
DG
2785error:
2786 return ret;
2787}
2788
9730260e 2789/*
7972aab2
DG
2790 * Create and send to the application the created buffers with per UID buffers.
2791 *
9acdc1d6 2792 * This MUST be called with a RCU read side lock acquired.
71e0a100 2793 * The session list lock and the session's lock must be acquired.
9acdc1d6 2794 *
7972aab2 2795 * Return 0 on success else a negative value.
9730260e 2796 */
7972aab2
DG
2797static int create_channel_per_uid(struct ust_app *app,
2798 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
2799 struct ust_app_channel *ua_chan)
9730260e
DG
2800{
2801 int ret;
7972aab2
DG
2802 struct buffer_reg_uid *reg_uid;
2803 struct buffer_reg_channel *reg_chan;
e32d7f27 2804 struct ltt_session *session = NULL;
e098433c
JG
2805 enum lttng_error_code notification_ret;
2806 struct ust_registry_channel *chan_reg;
9730260e 2807
7972aab2
DG
2808 assert(app);
2809 assert(usess);
2810 assert(ua_sess);
2811 assert(ua_chan);
2812
2813 DBG("UST app creating channel %s with per UID buffers", ua_chan->name);
2814
2815 reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid);
2816 /*
2817 * The session creation handles the creation of this global registry
2818 * object. If none can be find, there is a code flow problem or a
2819 * teardown race.
2820 */
2821 assert(reg_uid);
2822
2823 reg_chan = buffer_reg_channel_find(ua_chan->tracing_channel_id,
2824 reg_uid);
2721f7ea
JG
2825 if (reg_chan) {
2826 goto send_channel;
2827 }
7972aab2 2828
2721f7ea
JG
2829 /* Create the buffer registry channel object. */
2830 ret = create_buffer_reg_channel(reg_uid->registry, ua_chan, &reg_chan);
2831 if (ret < 0) {
2832 ERR("Error creating the UST channel \"%s\" registry instance",
f14256d6 2833 ua_chan->name);
2721f7ea
JG
2834 goto error;
2835 }
f14256d6 2836
e098433c
JG
2837 session = session_find_by_id(ua_sess->tracing_id);
2838 assert(session);
2839 assert(pthread_mutex_trylock(&session->lock));
2840 assert(session_trylock_list());
2841
2721f7ea
JG
2842 /*
2843 * Create the buffers on the consumer side. This call populates the
2844 * ust app channel object with all streams and data object.
2845 */
2846 ret = do_consumer_create_channel(usess, ua_sess, ua_chan,
e098433c 2847 app->bits_per_long, reg_uid->registry->reg.ust,
d2956687 2848 session->most_recent_chunk_id.value);
2721f7ea
JG
2849 if (ret < 0) {
2850 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2851 ua_chan->name);
7972aab2
DG
2852
2853 /*
2721f7ea
JG
2854 * Let's remove the previously created buffer registry channel so
2855 * it's not visible anymore in the session registry.
7972aab2 2856 */
2721f7ea
JG
2857 ust_registry_channel_del_free(reg_uid->registry->reg.ust,
2858 ua_chan->tracing_channel_id, false);
2859 buffer_reg_channel_remove(reg_uid->registry, reg_chan);
2860 buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST);
2861 goto error;
7972aab2
DG
2862 }
2863
2721f7ea
JG
2864 /*
2865 * Setup the streams and add it to the session registry.
2866 */
2867 ret = setup_buffer_reg_channel(reg_uid->registry,
2868 ua_chan, reg_chan, app);
2869 if (ret < 0) {
2870 ERR("Error setting up UST channel \"%s\"", ua_chan->name);
2871 goto error;
2872 }
2873
e098433c
JG
2874 /* Notify the notification subsystem of the channel's creation. */
2875 pthread_mutex_lock(&reg_uid->registry->reg.ust->lock);
2876 chan_reg = ust_registry_channel_find(reg_uid->registry->reg.ust,
2877 ua_chan->tracing_channel_id);
2878 assert(chan_reg);
2879 chan_reg->consumer_key = ua_chan->key;
2880 chan_reg = NULL;
2881 pthread_mutex_unlock(&reg_uid->registry->reg.ust->lock);
e9404c27 2882
e098433c
JG
2883 notification_ret = notification_thread_command_add_channel(
2884 notification_thread_handle, session->name,
470cc211
JG
2885 ua_sess->effective_credentials.uid,
2886 ua_sess->effective_credentials.gid, ua_chan->name,
2887 ua_chan->key, LTTNG_DOMAIN_UST,
e098433c
JG
2888 ua_chan->attr.subbuf_size * ua_chan->attr.num_subbuf);
2889 if (notification_ret != LTTNG_OK) {
2890 ret = - (int) notification_ret;
2891 ERR("Failed to add channel to notification thread");
2892 goto error;
e9404c27
JG
2893 }
2894
2721f7ea 2895send_channel:
66ff8e3f
JG
2896 /* Send buffers to the application. */
2897 ret = send_channel_uid_to_ust(reg_chan, app, ua_sess, ua_chan);
2898 if (ret < 0) {
2899 if (ret != -ENOTCONN) {
2900 ERR("Error sending channel to application");
2901 }
2902 goto error;
2903 }
2904
9730260e 2905error:
e32d7f27
JG
2906 if (session) {
2907 session_put(session);
2908 }
9730260e
DG
2909 return ret;
2910}
2911
78f0bacd 2912/*
7972aab2
DG
2913 * Create and send to the application the created buffers with per PID buffers.
2914 *
fad1ed2f 2915 * Called with UST app session lock held.
71e0a100 2916 * The session list lock and the session's lock must be acquired.
fad1ed2f 2917 *
7972aab2 2918 * Return 0 on success else a negative value.
78f0bacd 2919 */
7972aab2
DG
2920static int create_channel_per_pid(struct ust_app *app,
2921 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
2922 struct ust_app_channel *ua_chan)
78f0bacd 2923{
8535a6d9 2924 int ret;
7972aab2 2925 struct ust_registry_session *registry;
e9404c27 2926 enum lttng_error_code cmd_ret;
e32d7f27 2927 struct ltt_session *session = NULL;
e9404c27
JG
2928 uint64_t chan_reg_key;
2929 struct ust_registry_channel *chan_reg;
78f0bacd 2930
7972aab2
DG
2931 assert(app);
2932 assert(usess);
2933 assert(ua_sess);
2934 assert(ua_chan);
2935
2936 DBG("UST app creating channel %s with per PID buffers", ua_chan->name);
2937
2938 rcu_read_lock();
2939
2940 registry = get_session_registry(ua_sess);
fad1ed2f 2941 /* The UST app session lock is held, registry shall not be null. */
7972aab2
DG
2942 assert(registry);
2943
2944 /* Create and add a new channel registry to session. */
2945 ret = ust_registry_channel_add(registry, ua_chan->key);
78f0bacd 2946 if (ret < 0) {
f14256d6
MD
2947 ERR("Error creating the UST channel \"%s\" registry instance",
2948 ua_chan->name);
78f0bacd
DG
2949 goto error;
2950 }
2951
e098433c
JG
2952 session = session_find_by_id(ua_sess->tracing_id);
2953 assert(session);
2954
2955 assert(pthread_mutex_trylock(&session->lock));
2956 assert(session_trylock_list());
2957
7972aab2
DG
2958 /* Create and get channel on the consumer side. */
2959 ret = do_consumer_create_channel(usess, ua_sess, ua_chan,
e098433c 2960 app->bits_per_long, registry,
d2956687 2961 session->most_recent_chunk_id.value);
7972aab2 2962 if (ret < 0) {
f14256d6
MD
2963 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2964 ua_chan->name);
5b951542 2965 goto error_remove_from_registry;
7972aab2
DG
2966 }
2967
2968 ret = send_channel_pid_to_ust(app, ua_sess, ua_chan);
2969 if (ret < 0) {
a7169585
MD
2970 if (ret != -ENOTCONN) {
2971 ERR("Error sending channel to application");
2972 }
5b951542 2973 goto error_remove_from_registry;
7972aab2 2974 }
8535a6d9 2975
e9404c27
JG
2976 chan_reg_key = ua_chan->key;
2977 pthread_mutex_lock(&registry->lock);
2978 chan_reg = ust_registry_channel_find(registry, chan_reg_key);
2979 assert(chan_reg);
2980 chan_reg->consumer_key = ua_chan->key;
2981 pthread_mutex_unlock(&registry->lock);
2982
2983 cmd_ret = notification_thread_command_add_channel(
2984 notification_thread_handle, session->name,
470cc211
JG
2985 ua_sess->effective_credentials.uid,
2986 ua_sess->effective_credentials.gid, ua_chan->name,
2987 ua_chan->key, LTTNG_DOMAIN_UST,
e9404c27
JG
2988 ua_chan->attr.subbuf_size * ua_chan->attr.num_subbuf);
2989 if (cmd_ret != LTTNG_OK) {
2990 ret = - (int) cmd_ret;
2991 ERR("Failed to add channel to notification thread");
5b951542 2992 goto error_remove_from_registry;
e9404c27
JG
2993 }
2994
5b951542
MD
2995error_remove_from_registry:
2996 if (ret) {
2997 ust_registry_channel_del_free(registry, ua_chan->key, false);
2998 }
78f0bacd 2999error:
7972aab2 3000 rcu_read_unlock();
e32d7f27
JG
3001 if (session) {
3002 session_put(session);
3003 }
78f0bacd
DG
3004 return ret;
3005}
3006
3007/*
7972aab2 3008 * From an already allocated ust app channel, create the channel buffers if
88e3c2f5 3009 * needed and send them to the application. This MUST be called with a RCU read
7972aab2
DG
3010 * side lock acquired.
3011 *
fad1ed2f
JR
3012 * Called with UST app session lock held.
3013 *
a7169585
MD
3014 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3015 * the application exited concurrently.
78f0bacd 3016 */
88e3c2f5 3017static int ust_app_channel_send(struct ust_app *app,
7972aab2
DG
3018 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
3019 struct ust_app_channel *ua_chan)
78f0bacd 3020{
7972aab2 3021 int ret;
78f0bacd 3022
7972aab2
DG
3023 assert(app);
3024 assert(usess);
88e3c2f5 3025 assert(usess->active);
7972aab2
DG
3026 assert(ua_sess);
3027 assert(ua_chan);
3028
3029 /* Handle buffer type before sending the channel to the application. */
3030 switch (usess->buffer_type) {
3031 case LTTNG_BUFFER_PER_UID:
3032 {
3033 ret = create_channel_per_uid(app, usess, ua_sess, ua_chan);
3034 if (ret < 0) {
3035 goto error;
3036 }
3037 break;
3038 }
3039 case LTTNG_BUFFER_PER_PID:
3040 {
3041 ret = create_channel_per_pid(app, usess, ua_sess, ua_chan);
3042 if (ret < 0) {
3043 goto error;
3044 }
3045 break;
3046 }
3047 default:
3048 assert(0);
3049 ret = -EINVAL;
78f0bacd
DG
3050 goto error;
3051 }
3052
7972aab2
DG
3053 /* Initialize ust objd object using the received handle and add it. */
3054 lttng_ht_node_init_ulong(&ua_chan->ust_objd_node, ua_chan->handle);
3055 lttng_ht_add_unique_ulong(app->ust_objd, &ua_chan->ust_objd_node);
78f0bacd 3056
7972aab2
DG
3057 /* If channel is not enabled, disable it on the tracer */
3058 if (!ua_chan->enabled) {
3059 ret = disable_ust_channel(app, ua_sess, ua_chan);
3060 if (ret < 0) {
3061 goto error;
3062 }
78f0bacd
DG
3063 }
3064
3065error:
3066 return ret;
3067}
3068
284d8f55 3069/*
88e3c2f5 3070 * Create UST app channel and return it through ua_chanp if not NULL.
d0b96690 3071 *
36b588ed 3072 * Called with UST app session lock and RCU read-side lock held.
7972aab2 3073 *
88e3c2f5 3074 * Return 0 on success or else a negative value.
284d8f55 3075 */
88e3c2f5
JG
3076static int ust_app_channel_allocate(struct ust_app_session *ua_sess,
3077 struct ltt_ust_channel *uchan,
7972aab2 3078 enum lttng_ust_chan_type type, struct ltt_ust_session *usess,
4d710ac2 3079 struct ust_app_channel **ua_chanp)
5b4a0ec0
DG
3080{
3081 int ret = 0;
bec39940
DG
3082 struct lttng_ht_iter iter;
3083 struct lttng_ht_node_str *ua_chan_node;
5b4a0ec0
DG
3084 struct ust_app_channel *ua_chan;
3085
3086 /* Lookup channel in the ust app session */
bec39940
DG
3087 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
3088 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
fc34caaa 3089 if (ua_chan_node != NULL) {
5b4a0ec0 3090 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
fc34caaa 3091 goto end;
5b4a0ec0
DG
3092 }
3093
d0b96690 3094 ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr);
fc34caaa
DG
3095 if (ua_chan == NULL) {
3096 /* Only malloc can fail here */
4d710ac2 3097 ret = -ENOMEM;
88e3c2f5 3098 goto error;
fc34caaa
DG
3099 }
3100 shadow_copy_channel(ua_chan, uchan);
3101
ffe60014
DG
3102 /* Set channel type. */
3103 ua_chan->attr.type = type;
3104
d0b96690
DG
3105 /* Only add the channel if successful on the tracer side. */
3106 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
fc34caaa 3107end:
4d710ac2
DG
3108 if (ua_chanp) {
3109 *ua_chanp = ua_chan;
3110 }
3111
3112 /* Everything went well. */
3113 return 0;
5b4a0ec0
DG
3114
3115error:
4d710ac2 3116 return ret;
5b4a0ec0
DG
3117}
3118
3119/*
3120 * Create UST app event and create it on the tracer side.
d0b96690
DG
3121 *
3122 * Called with ust app session mutex held.
5b4a0ec0 3123 */
edb67388
DG
3124static
3125int create_ust_app_event(struct ust_app_session *ua_sess,
3126 struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent,
3127 struct ust_app *app)
284d8f55 3128{
edb67388 3129 int ret = 0;
5b4a0ec0 3130 struct ust_app_event *ua_event;
284d8f55 3131
edb67388
DG
3132 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
3133 if (ua_event == NULL) {
3134 /* Only malloc can failed so something is really wrong */
3135 ret = -ENOMEM;
fc34caaa 3136 goto end;
5b4a0ec0 3137 }
edb67388 3138 shadow_copy_event(ua_event, uevent);
5b4a0ec0 3139
edb67388 3140 /* Create it on the tracer side */
5b4a0ec0 3141 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
284d8f55 3142 if (ret < 0) {
fc34caaa 3143 /* Not found previously means that it does not exist on the tracer */
76f66f63 3144 assert(ret != -LTTNG_UST_ERR_EXIST);
284d8f55
DG
3145 goto error;
3146 }
3147
d0b96690 3148 add_unique_ust_app_event(ua_chan, ua_event);
284d8f55 3149
fc34caaa 3150 DBG2("UST app create event %s for PID %d completed", ua_event->name,
852d0037 3151 app->pid);
7f79d3a1 3152
edb67388 3153end:
fc34caaa
DG
3154 return ret;
3155
5b4a0ec0 3156error:
fc34caaa 3157 /* Valid. Calling here is already in a read side lock */
fb45065e 3158 delete_ust_app_event(-1, ua_event, app);
edb67388 3159 return ret;
5b4a0ec0
DG
3160}
3161
3162/*
3163 * Create UST metadata and open it on the tracer side.
d0b96690 3164 *
7972aab2 3165 * Called with UST app session lock held and RCU read side lock.
5b4a0ec0
DG
3166 */
3167static int create_ust_app_metadata(struct ust_app_session *ua_sess,
ad7a9107 3168 struct ust_app *app, struct consumer_output *consumer)
5b4a0ec0
DG
3169{
3170 int ret = 0;
ffe60014 3171 struct ust_app_channel *metadata;
d88aee68 3172 struct consumer_socket *socket;
7972aab2 3173 struct ust_registry_session *registry;
e32d7f27 3174 struct ltt_session *session = NULL;
5b4a0ec0 3175
ffe60014
DG
3176 assert(ua_sess);
3177 assert(app);
d88aee68 3178 assert(consumer);
5b4a0ec0 3179
7972aab2 3180 registry = get_session_registry(ua_sess);
fad1ed2f 3181 /* The UST app session is held registry shall not be null. */
7972aab2
DG
3182 assert(registry);
3183
ce34fcd0
MD
3184 pthread_mutex_lock(&registry->lock);
3185
1b532a60
DG
3186 /* Metadata already exists for this registry or it was closed previously */
3187 if (registry->metadata_key || registry->metadata_closed) {
7972aab2
DG
3188 ret = 0;
3189 goto error;
5b4a0ec0
DG
3190 }
3191
ffe60014 3192 /* Allocate UST metadata */
d0b96690 3193 metadata = alloc_ust_app_channel(DEFAULT_METADATA_NAME, ua_sess, NULL);
ffe60014
DG
3194 if (!metadata) {
3195 /* malloc() failed */
3196 ret = -ENOMEM;
3197 goto error;
3198 }
5b4a0ec0 3199
ad7a9107 3200 memcpy(&metadata->attr, &ua_sess->metadata_attr, sizeof(metadata->attr));
5b4a0ec0 3201
7972aab2
DG
3202 /* Need one fd for the channel. */
3203 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
3204 if (ret < 0) {
3205 ERR("Exhausted number of available FD upon create metadata");
3206 goto error;
3207 }
3208
4dc3dfc5
DG
3209 /* Get the right consumer socket for the application. */
3210 socket = consumer_find_socket_by_bitness(app->bits_per_long, consumer);
3211 if (!socket) {
3212 ret = -EINVAL;
3213 goto error_consumer;
3214 }
3215
331744e3
JD
3216 /*
3217 * Keep metadata key so we can identify it on the consumer side. Assign it
3218 * to the registry *before* we ask the consumer so we avoid the race of the
3219 * consumer requesting the metadata and the ask_channel call on our side
3220 * did not returned yet.
3221 */
3222 registry->metadata_key = metadata->key;
3223
e098433c
JG
3224 session = session_find_by_id(ua_sess->tracing_id);
3225 assert(session);
3226