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