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