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