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