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