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