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