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