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