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