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