Clean-up: sessiond: change space to tabs
[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 <errno.h>
11 #include <inttypes.h>
12 #include <pthread.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/stat.h>
17 #include <sys/types.h>
18 #include <unistd.h>
19 #include <urcu/compiler.h>
20 #include <signal.h>
21
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 ua_sess->real_credentials.uid);
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 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
1347 int set_ust_event_filter(struct ust_app_event *ua_event,
1348 struct ust_app *app)
1349 {
1350 int ret;
1351 struct lttng_ust_filter_bytecode *ust_bytecode = NULL;
1352
1353 health_code_update();
1354
1355 if (!ua_event->filter) {
1356 ret = 0;
1357 goto error;
1358 }
1359
1360 ust_bytecode = create_ust_bytecode_from_bytecode(ua_event->filter);
1361 if (!ust_bytecode) {
1362 ret = -LTTNG_ERR_NOMEM;
1363 goto error;
1364 }
1365 pthread_mutex_lock(&app->sock_lock);
1366 ret = ustctl_set_filter(app->sock, ust_bytecode,
1367 ua_event->obj);
1368 pthread_mutex_unlock(&app->sock_lock);
1369 if (ret < 0) {
1370 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1371 ERR("UST app event %s filter failed for app (pid: %d) "
1372 "with ret %d", ua_event->attr.name, app->pid, ret);
1373 } else {
1374 /*
1375 * This is normal behavior, an application can die during the
1376 * creation process. Don't report an error so the execution can
1377 * continue normally.
1378 */
1379 ret = 0;
1380 DBG3("UST app filter event failed. Application is dead.");
1381 }
1382 goto error;
1383 }
1384
1385 DBG2("UST filter set successfully for event %s", ua_event->name);
1386
1387 error:
1388 health_code_update();
1389 free(ust_bytecode);
1390 return ret;
1391 }
1392
1393 static
1394 struct lttng_ust_event_exclusion *create_ust_exclusion_from_exclusion(
1395 struct lttng_event_exclusion *exclusion)
1396 {
1397 struct lttng_ust_event_exclusion *ust_exclusion = NULL;
1398 size_t exclusion_alloc_size = sizeof(struct lttng_ust_event_exclusion) +
1399 LTTNG_UST_SYM_NAME_LEN * exclusion->count;
1400
1401 ust_exclusion = zmalloc(exclusion_alloc_size);
1402 if (!ust_exclusion) {
1403 PERROR("malloc");
1404 goto end;
1405 }
1406
1407 assert(sizeof(struct lttng_event_exclusion) ==
1408 sizeof(struct lttng_ust_event_exclusion));
1409 memcpy(ust_exclusion, exclusion, exclusion_alloc_size);
1410 end:
1411 return ust_exclusion;
1412 }
1413
1414 /*
1415 * Set event exclusions on the tracer.
1416 */
1417 static
1418 int set_ust_event_exclusion(struct ust_app_event *ua_event,
1419 struct ust_app *app)
1420 {
1421 int ret;
1422 struct lttng_ust_event_exclusion *ust_exclusion = NULL;
1423
1424 health_code_update();
1425
1426 if (!ua_event->exclusion || !ua_event->exclusion->count) {
1427 ret = 0;
1428 goto error;
1429 }
1430
1431 ust_exclusion = create_ust_exclusion_from_exclusion(
1432 ua_event->exclusion);
1433 if (!ust_exclusion) {
1434 ret = -LTTNG_ERR_NOMEM;
1435 goto error;
1436 }
1437 pthread_mutex_lock(&app->sock_lock);
1438 ret = ustctl_set_exclusion(app->sock, ust_exclusion, ua_event->obj);
1439 pthread_mutex_unlock(&app->sock_lock);
1440 if (ret < 0) {
1441 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1442 ERR("UST app event %s exclusions failed for app (pid: %d) "
1443 "with ret %d", ua_event->attr.name, app->pid, ret);
1444 } else {
1445 /*
1446 * This is normal behavior, an application can die during the
1447 * creation process. Don't report an error so the execution can
1448 * continue normally.
1449 */
1450 ret = 0;
1451 DBG3("UST app event exclusion failed. Application is dead.");
1452 }
1453 goto error;
1454 }
1455
1456 DBG2("UST exclusion set successfully for event %s", ua_event->name);
1457
1458 error:
1459 health_code_update();
1460 free(ust_exclusion);
1461 return ret;
1462 }
1463
1464 /*
1465 * Disable the specified event on to UST tracer for the UST session.
1466 */
1467 static int disable_ust_event(struct ust_app *app,
1468 struct ust_app_session *ua_sess, struct ust_app_event *ua_event)
1469 {
1470 int ret;
1471
1472 health_code_update();
1473
1474 pthread_mutex_lock(&app->sock_lock);
1475 ret = ustctl_disable(app->sock, ua_event->obj);
1476 pthread_mutex_unlock(&app->sock_lock);
1477 if (ret < 0) {
1478 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1479 ERR("UST app event %s disable failed for app (pid: %d) "
1480 "and session handle %d with ret %d",
1481 ua_event->attr.name, app->pid, ua_sess->handle, ret);
1482 } else {
1483 /*
1484 * This is normal behavior, an application can die during the
1485 * creation process. Don't report an error so the execution can
1486 * continue normally.
1487 */
1488 ret = 0;
1489 DBG3("UST app disable event failed. Application is dead.");
1490 }
1491 goto error;
1492 }
1493
1494 DBG2("UST app event %s disabled successfully for app (pid: %d)",
1495 ua_event->attr.name, app->pid);
1496
1497 error:
1498 health_code_update();
1499 return ret;
1500 }
1501
1502 /*
1503 * Disable the specified channel on to UST tracer for the UST session.
1504 */
1505 static int disable_ust_channel(struct ust_app *app,
1506 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
1507 {
1508 int ret;
1509
1510 health_code_update();
1511
1512 pthread_mutex_lock(&app->sock_lock);
1513 ret = ustctl_disable(app->sock, ua_chan->obj);
1514 pthread_mutex_unlock(&app->sock_lock);
1515 if (ret < 0) {
1516 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1517 ERR("UST app channel %s disable failed for app (pid: %d) "
1518 "and session handle %d with ret %d",
1519 ua_chan->name, app->pid, ua_sess->handle, ret);
1520 } else {
1521 /*
1522 * This is normal behavior, an application can die during the
1523 * creation process. Don't report an error so the execution can
1524 * continue normally.
1525 */
1526 ret = 0;
1527 DBG3("UST app disable channel failed. Application is dead.");
1528 }
1529 goto error;
1530 }
1531
1532 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1533 ua_chan->name, app->pid);
1534
1535 error:
1536 health_code_update();
1537 return ret;
1538 }
1539
1540 /*
1541 * Enable the specified channel on to UST tracer for the UST session.
1542 */
1543 static int enable_ust_channel(struct ust_app *app,
1544 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
1545 {
1546 int ret;
1547
1548 health_code_update();
1549
1550 pthread_mutex_lock(&app->sock_lock);
1551 ret = ustctl_enable(app->sock, ua_chan->obj);
1552 pthread_mutex_unlock(&app->sock_lock);
1553 if (ret < 0) {
1554 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1555 ERR("UST app channel %s enable failed for app (pid: %d) "
1556 "and session handle %d with ret %d",
1557 ua_chan->name, app->pid, ua_sess->handle, ret);
1558 } else {
1559 /*
1560 * This is normal behavior, an application can die during the
1561 * creation process. Don't report an error so the execution can
1562 * continue normally.
1563 */
1564 ret = 0;
1565 DBG3("UST app enable channel failed. Application is dead.");
1566 }
1567 goto error;
1568 }
1569
1570 ua_chan->enabled = 1;
1571
1572 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1573 ua_chan->name, app->pid);
1574
1575 error:
1576 health_code_update();
1577 return ret;
1578 }
1579
1580 /*
1581 * Enable the specified event on to UST tracer for the UST session.
1582 */
1583 static int enable_ust_event(struct ust_app *app,
1584 struct ust_app_session *ua_sess, struct ust_app_event *ua_event)
1585 {
1586 int ret;
1587
1588 health_code_update();
1589
1590 pthread_mutex_lock(&app->sock_lock);
1591 ret = ustctl_enable(app->sock, ua_event->obj);
1592 pthread_mutex_unlock(&app->sock_lock);
1593 if (ret < 0) {
1594 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1595 ERR("UST app event %s enable failed for app (pid: %d) "
1596 "and session handle %d with ret %d",
1597 ua_event->attr.name, app->pid, ua_sess->handle, ret);
1598 } else {
1599 /*
1600 * This is normal behavior, an application can die during the
1601 * creation process. Don't report an error so the execution can
1602 * continue normally.
1603 */
1604 ret = 0;
1605 DBG3("UST app enable event failed. Application is dead.");
1606 }
1607 goto error;
1608 }
1609
1610 DBG2("UST app event %s enabled successfully for app (pid: %d)",
1611 ua_event->attr.name, app->pid);
1612
1613 error:
1614 health_code_update();
1615 return ret;
1616 }
1617
1618 /*
1619 * Send channel and stream buffer to application.
1620 *
1621 * Return 0 on success. On error, a negative value is returned.
1622 */
1623 static int send_channel_pid_to_ust(struct ust_app *app,
1624 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
1625 {
1626 int ret;
1627 struct ust_app_stream *stream, *stmp;
1628
1629 assert(app);
1630 assert(ua_sess);
1631 assert(ua_chan);
1632
1633 health_code_update();
1634
1635 DBG("UST app sending channel %s to UST app sock %d", ua_chan->name,
1636 app->sock);
1637
1638 /* Send channel to the application. */
1639 ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan);
1640 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1641 ret = -ENOTCONN; /* Caused by app exiting. */
1642 goto error;
1643 } else if (ret < 0) {
1644 goto error;
1645 }
1646
1647 health_code_update();
1648
1649 /* Send all streams to application. */
1650 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
1651 ret = ust_consumer_send_stream_to_ust(app, ua_chan, stream);
1652 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1653 ret = -ENOTCONN; /* Caused by app exiting. */
1654 goto error;
1655 } else if (ret < 0) {
1656 goto error;
1657 }
1658 /* We don't need the stream anymore once sent to the tracer. */
1659 cds_list_del(&stream->list);
1660 delete_ust_app_stream(-1, stream, app);
1661 }
1662 /* Flag the channel that it is sent to the application. */
1663 ua_chan->is_sent = 1;
1664
1665 error:
1666 health_code_update();
1667 return ret;
1668 }
1669
1670 /*
1671 * Create the specified event onto the UST tracer for a UST session.
1672 *
1673 * Should be called with session mutex held.
1674 */
1675 static
1676 int create_ust_event(struct ust_app *app, struct ust_app_session *ua_sess,
1677 struct ust_app_channel *ua_chan, struct ust_app_event *ua_event)
1678 {
1679 int ret = 0;
1680
1681 health_code_update();
1682
1683 /* Create UST event on tracer */
1684 pthread_mutex_lock(&app->sock_lock);
1685 ret = ustctl_create_event(app->sock, &ua_event->attr, ua_chan->obj,
1686 &ua_event->obj);
1687 pthread_mutex_unlock(&app->sock_lock);
1688 if (ret < 0) {
1689 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1690 abort();
1691 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1692 ua_event->attr.name, app->pid, ret);
1693 } else {
1694 /*
1695 * This is normal behavior, an application can die during the
1696 * creation process. Don't report an error so the execution can
1697 * continue normally.
1698 */
1699 ret = 0;
1700 DBG3("UST app create event failed. Application is dead.");
1701 }
1702 goto error;
1703 }
1704
1705 ua_event->handle = ua_event->obj->handle;
1706
1707 DBG2("UST app event %s created successfully for pid:%d",
1708 ua_event->attr.name, app->pid);
1709
1710 health_code_update();
1711
1712 /* Set filter if one is present. */
1713 if (ua_event->filter) {
1714 ret = set_ust_event_filter(ua_event, app);
1715 if (ret < 0) {
1716 goto error;
1717 }
1718 }
1719
1720 /* Set exclusions for the event */
1721 if (ua_event->exclusion) {
1722 ret = set_ust_event_exclusion(ua_event, app);
1723 if (ret < 0) {
1724 goto error;
1725 }
1726 }
1727
1728 /* If event not enabled, disable it on the tracer */
1729 if (ua_event->enabled) {
1730 /*
1731 * We now need to explicitly enable the event, since it
1732 * is now disabled at creation.
1733 */
1734 ret = enable_ust_event(app, ua_sess, ua_event);
1735 if (ret < 0) {
1736 /*
1737 * If we hit an EPERM, something is wrong with our enable call. If
1738 * we get an EEXIST, there is a problem on the tracer side since we
1739 * just created it.
1740 */
1741 switch (ret) {
1742 case -LTTNG_UST_ERR_PERM:
1743 /* Code flow problem */
1744 assert(0);
1745 case -LTTNG_UST_ERR_EXIST:
1746 /* It's OK for our use case. */
1747 ret = 0;
1748 break;
1749 default:
1750 break;
1751 }
1752 goto error;
1753 }
1754 }
1755
1756 error:
1757 health_code_update();
1758 return ret;
1759 }
1760
1761 /*
1762 * Copy data between an UST app event and a LTT event.
1763 */
1764 static void shadow_copy_event(struct ust_app_event *ua_event,
1765 struct ltt_ust_event *uevent)
1766 {
1767 size_t exclusion_alloc_size;
1768
1769 strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name));
1770 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
1771
1772 ua_event->enabled = uevent->enabled;
1773
1774 /* Copy event attributes */
1775 memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr));
1776
1777 /* Copy filter bytecode */
1778 if (uevent->filter) {
1779 ua_event->filter = copy_filter_bytecode(uevent->filter);
1780 /* Filter might be NULL here in case of ENONEM. */
1781 }
1782
1783 /* Copy exclusion data */
1784 if (uevent->exclusion) {
1785 exclusion_alloc_size = sizeof(struct lttng_event_exclusion) +
1786 LTTNG_UST_SYM_NAME_LEN * uevent->exclusion->count;
1787 ua_event->exclusion = zmalloc(exclusion_alloc_size);
1788 if (ua_event->exclusion == NULL) {
1789 PERROR("malloc");
1790 } else {
1791 memcpy(ua_event->exclusion, uevent->exclusion,
1792 exclusion_alloc_size);
1793 }
1794 }
1795 }
1796
1797 /*
1798 * Copy data between an UST app channel and a LTT channel.
1799 */
1800 static void shadow_copy_channel(struct ust_app_channel *ua_chan,
1801 struct ltt_ust_channel *uchan)
1802 {
1803 DBG2("UST app shadow copy of channel %s started", ua_chan->name);
1804
1805 strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name));
1806 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
1807
1808 ua_chan->tracefile_size = uchan->tracefile_size;
1809 ua_chan->tracefile_count = uchan->tracefile_count;
1810
1811 /* Copy event attributes since the layout is different. */
1812 ua_chan->attr.subbuf_size = uchan->attr.subbuf_size;
1813 ua_chan->attr.num_subbuf = uchan->attr.num_subbuf;
1814 ua_chan->attr.overwrite = uchan->attr.overwrite;
1815 ua_chan->attr.switch_timer_interval = uchan->attr.switch_timer_interval;
1816 ua_chan->attr.read_timer_interval = uchan->attr.read_timer_interval;
1817 ua_chan->monitor_timer_interval = uchan->monitor_timer_interval;
1818 ua_chan->attr.output = uchan->attr.output;
1819 ua_chan->attr.blocking_timeout = uchan->attr.u.s.blocking_timeout;
1820
1821 /*
1822 * Note that the attribute channel type is not set since the channel on the
1823 * tracing registry side does not have this information.
1824 */
1825
1826 ua_chan->enabled = uchan->enabled;
1827 ua_chan->tracing_channel_id = uchan->id;
1828
1829 DBG3("UST app shadow copy of channel %s done", ua_chan->name);
1830 }
1831
1832 /*
1833 * Copy data between a UST app session and a regular LTT session.
1834 */
1835 static void shadow_copy_session(struct ust_app_session *ua_sess,
1836 struct ltt_ust_session *usess, struct ust_app *app)
1837 {
1838 struct tm *timeinfo;
1839 char datetime[16];
1840 int ret;
1841 char tmp_shm_path[PATH_MAX];
1842
1843 timeinfo = localtime(&app->registration_time);
1844 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
1845
1846 DBG2("Shadow copy of session handle %d", ua_sess->handle);
1847
1848 ua_sess->tracing_id = usess->id;
1849 ua_sess->id = get_next_session_id();
1850 ua_sess->real_credentials.uid = app->uid;
1851 ua_sess->real_credentials.gid = app->gid;
1852 ua_sess->effective_credentials.uid = usess->uid;
1853 ua_sess->effective_credentials.gid = usess->gid;
1854 ua_sess->buffer_type = usess->buffer_type;
1855 ua_sess->bits_per_long = app->bits_per_long;
1856
1857 /* There is only one consumer object per session possible. */
1858 consumer_output_get(usess->consumer);
1859 ua_sess->consumer = usess->consumer;
1860
1861 ua_sess->output_traces = usess->output_traces;
1862 ua_sess->live_timer_interval = usess->live_timer_interval;
1863 copy_channel_attr_to_ustctl(&ua_sess->metadata_attr,
1864 &usess->metadata_attr);
1865
1866 switch (ua_sess->buffer_type) {
1867 case LTTNG_BUFFER_PER_PID:
1868 ret = snprintf(ua_sess->path, sizeof(ua_sess->path),
1869 DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s", app->name, app->pid,
1870 datetime);
1871 break;
1872 case LTTNG_BUFFER_PER_UID:
1873 ret = snprintf(ua_sess->path, sizeof(ua_sess->path),
1874 DEFAULT_UST_TRACE_UID_PATH,
1875 ua_sess->real_credentials.uid,
1876 app->bits_per_long);
1877 break;
1878 default:
1879 assert(0);
1880 goto error;
1881 }
1882 if (ret < 0) {
1883 PERROR("asprintf UST shadow copy session");
1884 assert(0);
1885 goto error;
1886 }
1887
1888 strncpy(ua_sess->root_shm_path, usess->root_shm_path,
1889 sizeof(ua_sess->root_shm_path));
1890 ua_sess->root_shm_path[sizeof(ua_sess->root_shm_path) - 1] = '\0';
1891 strncpy(ua_sess->shm_path, usess->shm_path,
1892 sizeof(ua_sess->shm_path));
1893 ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0';
1894 if (ua_sess->shm_path[0]) {
1895 switch (ua_sess->buffer_type) {
1896 case LTTNG_BUFFER_PER_PID:
1897 ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path),
1898 "/" DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s",
1899 app->name, app->pid, datetime);
1900 break;
1901 case LTTNG_BUFFER_PER_UID:
1902 ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path),
1903 "/" DEFAULT_UST_TRACE_UID_PATH,
1904 app->uid, app->bits_per_long);
1905 break;
1906 default:
1907 assert(0);
1908 goto error;
1909 }
1910 if (ret < 0) {
1911 PERROR("sprintf UST shadow copy session");
1912 assert(0);
1913 goto error;
1914 }
1915 strncat(ua_sess->shm_path, tmp_shm_path,
1916 sizeof(ua_sess->shm_path) - strlen(ua_sess->shm_path) - 1);
1917 ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0';
1918 }
1919 return;
1920
1921 error:
1922 consumer_output_put(ua_sess->consumer);
1923 }
1924
1925 /*
1926 * Lookup sesison wrapper.
1927 */
1928 static
1929 void __lookup_session_by_app(const struct ltt_ust_session *usess,
1930 struct ust_app *app, struct lttng_ht_iter *iter)
1931 {
1932 /* Get right UST app session from app */
1933 lttng_ht_lookup(app->sessions, &usess->id, iter);
1934 }
1935
1936 /*
1937 * Return ust app session from the app session hashtable using the UST session
1938 * id.
1939 */
1940 static struct ust_app_session *lookup_session_by_app(
1941 const struct ltt_ust_session *usess, struct ust_app *app)
1942 {
1943 struct lttng_ht_iter iter;
1944 struct lttng_ht_node_u64 *node;
1945
1946 __lookup_session_by_app(usess, app, &iter);
1947 node = lttng_ht_iter_get_node_u64(&iter);
1948 if (node == NULL) {
1949 goto error;
1950 }
1951
1952 return caa_container_of(node, struct ust_app_session, node);
1953
1954 error:
1955 return NULL;
1956 }
1957
1958 /*
1959 * Setup buffer registry per PID for the given session and application. If none
1960 * is found, a new one is created, added to the global registry and
1961 * initialized. If regp is valid, it's set with the newly created object.
1962 *
1963 * Return 0 on success or else a negative value.
1964 */
1965 static int setup_buffer_reg_pid(struct ust_app_session *ua_sess,
1966 struct ust_app *app, struct buffer_reg_pid **regp)
1967 {
1968 int ret = 0;
1969 struct buffer_reg_pid *reg_pid;
1970
1971 assert(ua_sess);
1972 assert(app);
1973
1974 rcu_read_lock();
1975
1976 reg_pid = buffer_reg_pid_find(ua_sess->id);
1977 if (!reg_pid) {
1978 /*
1979 * This is the create channel path meaning that if there is NO
1980 * registry available, we have to create one for this session.
1981 */
1982 ret = buffer_reg_pid_create(ua_sess->id, &reg_pid,
1983 ua_sess->root_shm_path, ua_sess->shm_path);
1984 if (ret < 0) {
1985 goto error;
1986 }
1987 } else {
1988 goto end;
1989 }
1990
1991 /* Initialize registry. */
1992 ret = ust_registry_session_init(&reg_pid->registry->reg.ust, app,
1993 app->bits_per_long, app->uint8_t_alignment,
1994 app->uint16_t_alignment, app->uint32_t_alignment,
1995 app->uint64_t_alignment, app->long_alignment,
1996 app->byte_order, app->version.major, app->version.minor,
1997 reg_pid->root_shm_path, reg_pid->shm_path,
1998 ua_sess->effective_credentials.uid,
1999 ua_sess->effective_credentials.gid, ua_sess->tracing_id,
2000 app->uid);
2001 if (ret < 0) {
2002 /*
2003 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2004 * destroy the buffer registry, because it is always expected
2005 * that if the buffer registry can be found, its ust registry is
2006 * non-NULL.
2007 */
2008 buffer_reg_pid_destroy(reg_pid);
2009 goto error;
2010 }
2011
2012 buffer_reg_pid_add(reg_pid);
2013
2014 DBG3("UST app buffer registry per PID created successfully");
2015
2016 end:
2017 if (regp) {
2018 *regp = reg_pid;
2019 }
2020 error:
2021 rcu_read_unlock();
2022 return ret;
2023 }
2024
2025 /*
2026 * Setup buffer registry per UID for the given session and application. If none
2027 * is found, a new one is created, added to the global registry and
2028 * initialized. If regp is valid, it's set with the newly created object.
2029 *
2030 * Return 0 on success or else a negative value.
2031 */
2032 static int setup_buffer_reg_uid(struct ltt_ust_session *usess,
2033 struct ust_app_session *ua_sess,
2034 struct ust_app *app, struct buffer_reg_uid **regp)
2035 {
2036 int ret = 0;
2037 struct buffer_reg_uid *reg_uid;
2038
2039 assert(usess);
2040 assert(app);
2041
2042 rcu_read_lock();
2043
2044 reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid);
2045 if (!reg_uid) {
2046 /*
2047 * This is the create channel path meaning that if there is NO
2048 * registry available, we have to create one for this session.
2049 */
2050 ret = buffer_reg_uid_create(usess->id, app->bits_per_long, app->uid,
2051 LTTNG_DOMAIN_UST, &reg_uid,
2052 ua_sess->root_shm_path, ua_sess->shm_path);
2053 if (ret < 0) {
2054 goto error;
2055 }
2056 } else {
2057 goto end;
2058 }
2059
2060 /* Initialize registry. */
2061 ret = ust_registry_session_init(&reg_uid->registry->reg.ust, NULL,
2062 app->bits_per_long, app->uint8_t_alignment,
2063 app->uint16_t_alignment, app->uint32_t_alignment,
2064 app->uint64_t_alignment, app->long_alignment,
2065 app->byte_order, app->version.major,
2066 app->version.minor, reg_uid->root_shm_path,
2067 reg_uid->shm_path, usess->uid, usess->gid,
2068 ua_sess->tracing_id, app->uid);
2069 if (ret < 0) {
2070 /*
2071 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2072 * destroy the buffer registry, because it is always expected
2073 * that if the buffer registry can be found, its ust registry is
2074 * non-NULL.
2075 */
2076 buffer_reg_uid_destroy(reg_uid, NULL);
2077 goto error;
2078 }
2079 /* Add node to teardown list of the session. */
2080 cds_list_add(&reg_uid->lnode, &usess->buffer_reg_uid_list);
2081
2082 buffer_reg_uid_add(reg_uid);
2083
2084 DBG3("UST app buffer registry per UID created successfully");
2085 end:
2086 if (regp) {
2087 *regp = reg_uid;
2088 }
2089 error:
2090 rcu_read_unlock();
2091 return ret;
2092 }
2093
2094 /*
2095 * Create a session on the tracer side for the given app.
2096 *
2097 * On success, ua_sess_ptr is populated with the session pointer or else left
2098 * untouched. If the session was created, is_created is set to 1. On error,
2099 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2100 * be NULL.
2101 *
2102 * Returns 0 on success or else a negative code which is either -ENOMEM or
2103 * -ENOTCONN which is the default code if the ustctl_create_session fails.
2104 */
2105 static int find_or_create_ust_app_session(struct ltt_ust_session *usess,
2106 struct ust_app *app, struct ust_app_session **ua_sess_ptr,
2107 int *is_created)
2108 {
2109 int ret, created = 0;
2110 struct ust_app_session *ua_sess;
2111
2112 assert(usess);
2113 assert(app);
2114 assert(ua_sess_ptr);
2115
2116 health_code_update();
2117
2118 ua_sess = lookup_session_by_app(usess, app);
2119 if (ua_sess == NULL) {
2120 DBG2("UST app pid: %d session id %" PRIu64 " not found, creating it",
2121 app->pid, usess->id);
2122 ua_sess = alloc_ust_app_session();
2123 if (ua_sess == NULL) {
2124 /* Only malloc can failed so something is really wrong */
2125 ret = -ENOMEM;
2126 goto error;
2127 }
2128 shadow_copy_session(ua_sess, usess, app);
2129 created = 1;
2130 }
2131
2132 switch (usess->buffer_type) {
2133 case LTTNG_BUFFER_PER_PID:
2134 /* Init local registry. */
2135 ret = setup_buffer_reg_pid(ua_sess, app, NULL);
2136 if (ret < 0) {
2137 delete_ust_app_session(-1, ua_sess, app);
2138 goto error;
2139 }
2140 break;
2141 case LTTNG_BUFFER_PER_UID:
2142 /* Look for a global registry. If none exists, create one. */
2143 ret = setup_buffer_reg_uid(usess, ua_sess, app, NULL);
2144 if (ret < 0) {
2145 delete_ust_app_session(-1, ua_sess, app);
2146 goto error;
2147 }
2148 break;
2149 default:
2150 assert(0);
2151 ret = -EINVAL;
2152 goto error;
2153 }
2154
2155 health_code_update();
2156
2157 if (ua_sess->handle == -1) {
2158 pthread_mutex_lock(&app->sock_lock);
2159 ret = ustctl_create_session(app->sock);
2160 pthread_mutex_unlock(&app->sock_lock);
2161 if (ret < 0) {
2162 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
2163 ERR("Creating session for app pid %d with ret %d",
2164 app->pid, ret);
2165 } else {
2166 DBG("UST app creating session failed. Application is dead");
2167 /*
2168 * This is normal behavior, an application can die during the
2169 * creation process. Don't report an error so the execution can
2170 * continue normally. This will get flagged ENOTCONN and the
2171 * caller will handle it.
2172 */
2173 ret = 0;
2174 }
2175 delete_ust_app_session(-1, ua_sess, app);
2176 if (ret != -ENOMEM) {
2177 /*
2178 * Tracer is probably gone or got an internal error so let's
2179 * behave like it will soon unregister or not usable.
2180 */
2181 ret = -ENOTCONN;
2182 }
2183 goto error;
2184 }
2185
2186 ua_sess->handle = ret;
2187
2188 /* Add ust app session to app's HT */
2189 lttng_ht_node_init_u64(&ua_sess->node,
2190 ua_sess->tracing_id);
2191 lttng_ht_add_unique_u64(app->sessions, &ua_sess->node);
2192 lttng_ht_node_init_ulong(&ua_sess->ust_objd_node, ua_sess->handle);
2193 lttng_ht_add_unique_ulong(app->ust_sessions_objd,
2194 &ua_sess->ust_objd_node);
2195
2196 DBG2("UST app session created successfully with handle %d", ret);
2197 }
2198
2199 *ua_sess_ptr = ua_sess;
2200 if (is_created) {
2201 *is_created = created;
2202 }
2203
2204 /* Everything went well. */
2205 ret = 0;
2206
2207 error:
2208 health_code_update();
2209 return ret;
2210 }
2211
2212 /*
2213 * Match function for a hash table lookup of ust_app_ctx.
2214 *
2215 * It matches an ust app context based on the context type and, in the case
2216 * of perf counters, their name.
2217 */
2218 static int ht_match_ust_app_ctx(struct cds_lfht_node *node, const void *_key)
2219 {
2220 struct ust_app_ctx *ctx;
2221 const struct lttng_ust_context_attr *key;
2222
2223 assert(node);
2224 assert(_key);
2225
2226 ctx = caa_container_of(node, struct ust_app_ctx, node.node);
2227 key = _key;
2228
2229 /* Context type */
2230 if (ctx->ctx.ctx != key->ctx) {
2231 goto no_match;
2232 }
2233
2234 switch(key->ctx) {
2235 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER:
2236 if (strncmp(key->u.perf_counter.name,
2237 ctx->ctx.u.perf_counter.name,
2238 sizeof(key->u.perf_counter.name))) {
2239 goto no_match;
2240 }
2241 break;
2242 case LTTNG_UST_CONTEXT_APP_CONTEXT:
2243 if (strcmp(key->u.app_ctx.provider_name,
2244 ctx->ctx.u.app_ctx.provider_name) ||
2245 strcmp(key->u.app_ctx.ctx_name,
2246 ctx->ctx.u.app_ctx.ctx_name)) {
2247 goto no_match;
2248 }
2249 break;
2250 default:
2251 break;
2252 }
2253
2254 /* Match. */
2255 return 1;
2256
2257 no_match:
2258 return 0;
2259 }
2260
2261 /*
2262 * Lookup for an ust app context from an lttng_ust_context.
2263 *
2264 * Must be called while holding RCU read side lock.
2265 * Return an ust_app_ctx object or NULL on error.
2266 */
2267 static
2268 struct ust_app_ctx *find_ust_app_context(struct lttng_ht *ht,
2269 struct lttng_ust_context_attr *uctx)
2270 {
2271 struct lttng_ht_iter iter;
2272 struct lttng_ht_node_ulong *node;
2273 struct ust_app_ctx *app_ctx = NULL;
2274
2275 assert(uctx);
2276 assert(ht);
2277
2278 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2279 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) uctx->ctx, lttng_ht_seed),
2280 ht_match_ust_app_ctx, uctx, &iter.iter);
2281 node = lttng_ht_iter_get_node_ulong(&iter);
2282 if (!node) {
2283 goto end;
2284 }
2285
2286 app_ctx = caa_container_of(node, struct ust_app_ctx, node);
2287
2288 end:
2289 return app_ctx;
2290 }
2291
2292 /*
2293 * Create a context for the channel on the tracer.
2294 *
2295 * Called with UST app session lock held and a RCU read side lock.
2296 */
2297 static
2298 int create_ust_app_channel_context(struct ust_app_channel *ua_chan,
2299 struct lttng_ust_context_attr *uctx,
2300 struct ust_app *app)
2301 {
2302 int ret = 0;
2303 struct ust_app_ctx *ua_ctx;
2304
2305 DBG2("UST app adding context to channel %s", ua_chan->name);
2306
2307 ua_ctx = find_ust_app_context(ua_chan->ctx, uctx);
2308 if (ua_ctx) {
2309 ret = -EEXIST;
2310 goto error;
2311 }
2312
2313 ua_ctx = alloc_ust_app_ctx(uctx);
2314 if (ua_ctx == NULL) {
2315 /* malloc failed */
2316 ret = -ENOMEM;
2317 goto error;
2318 }
2319
2320 lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx);
2321 lttng_ht_add_ulong(ua_chan->ctx, &ua_ctx->node);
2322 cds_list_add_tail(&ua_ctx->list, &ua_chan->ctx_list);
2323
2324 ret = create_ust_channel_context(ua_chan, ua_ctx, app);
2325 if (ret < 0) {
2326 goto error;
2327 }
2328
2329 error:
2330 return ret;
2331 }
2332
2333 /*
2334 * Enable on the tracer side a ust app event for the session and channel.
2335 *
2336 * Called with UST app session lock held.
2337 */
2338 static
2339 int enable_ust_app_event(struct ust_app_session *ua_sess,
2340 struct ust_app_event *ua_event, struct ust_app *app)
2341 {
2342 int ret;
2343
2344 ret = enable_ust_event(app, ua_sess, ua_event);
2345 if (ret < 0) {
2346 goto error;
2347 }
2348
2349 ua_event->enabled = 1;
2350
2351 error:
2352 return ret;
2353 }
2354
2355 /*
2356 * Disable on the tracer side a ust app event for the session and channel.
2357 */
2358 static int disable_ust_app_event(struct ust_app_session *ua_sess,
2359 struct ust_app_event *ua_event, struct ust_app *app)
2360 {
2361 int ret;
2362
2363 ret = disable_ust_event(app, ua_sess, ua_event);
2364 if (ret < 0) {
2365 goto error;
2366 }
2367
2368 ua_event->enabled = 0;
2369
2370 error:
2371 return ret;
2372 }
2373
2374 /*
2375 * Lookup ust app channel for session and disable it on the tracer side.
2376 */
2377 static
2378 int disable_ust_app_channel(struct ust_app_session *ua_sess,
2379 struct ust_app_channel *ua_chan, struct ust_app *app)
2380 {
2381 int ret;
2382
2383 ret = disable_ust_channel(app, ua_sess, ua_chan);
2384 if (ret < 0) {
2385 goto error;
2386 }
2387
2388 ua_chan->enabled = 0;
2389
2390 error:
2391 return ret;
2392 }
2393
2394 /*
2395 * Lookup ust app channel for session and enable it on the tracer side. This
2396 * MUST be called with a RCU read side lock acquired.
2397 */
2398 static int enable_ust_app_channel(struct ust_app_session *ua_sess,
2399 struct ltt_ust_channel *uchan, struct ust_app *app)
2400 {
2401 int ret = 0;
2402 struct lttng_ht_iter iter;
2403 struct lttng_ht_node_str *ua_chan_node;
2404 struct ust_app_channel *ua_chan;
2405
2406 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
2407 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
2408 if (ua_chan_node == NULL) {
2409 DBG2("Unable to find channel %s in ust session id %" PRIu64,
2410 uchan->name, ua_sess->tracing_id);
2411 goto error;
2412 }
2413
2414 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2415
2416 ret = enable_ust_channel(app, ua_sess, ua_chan);
2417 if (ret < 0) {
2418 goto error;
2419 }
2420
2421 error:
2422 return ret;
2423 }
2424
2425 /*
2426 * Ask the consumer to create a channel and get it if successful.
2427 *
2428 * Called with UST app session lock held.
2429 *
2430 * Return 0 on success or else a negative value.
2431 */
2432 static int do_consumer_create_channel(struct ltt_ust_session *usess,
2433 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan,
2434 int bitness, struct ust_registry_session *registry,
2435 uint64_t trace_archive_id)
2436 {
2437 int ret;
2438 unsigned int nb_fd = 0;
2439 struct consumer_socket *socket;
2440
2441 assert(usess);
2442 assert(ua_sess);
2443 assert(ua_chan);
2444 assert(registry);
2445
2446 rcu_read_lock();
2447 health_code_update();
2448
2449 /* Get the right consumer socket for the application. */
2450 socket = consumer_find_socket_by_bitness(bitness, usess->consumer);
2451 if (!socket) {
2452 ret = -EINVAL;
2453 goto error;
2454 }
2455
2456 health_code_update();
2457
2458 /* Need one fd for the channel. */
2459 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
2460 if (ret < 0) {
2461 ERR("Exhausted number of available FD upon create channel");
2462 goto error;
2463 }
2464
2465 /*
2466 * Ask consumer to create channel. The consumer will return the number of
2467 * stream we have to expect.
2468 */
2469 ret = ust_consumer_ask_channel(ua_sess, ua_chan, usess->consumer, socket,
2470 registry, usess->current_trace_chunk);
2471 if (ret < 0) {
2472 goto error_ask;
2473 }
2474
2475 /*
2476 * Compute the number of fd needed before receiving them. It must be 2 per
2477 * stream (2 being the default value here).
2478 */
2479 nb_fd = DEFAULT_UST_STREAM_FD_NUM * ua_chan->expected_stream_count;
2480
2481 /* Reserve the amount of file descriptor we need. */
2482 ret = lttng_fd_get(LTTNG_FD_APPS, nb_fd);
2483 if (ret < 0) {
2484 ERR("Exhausted number of available FD upon create channel");
2485 goto error_fd_get_stream;
2486 }
2487
2488 health_code_update();
2489
2490 /*
2491 * Now get the channel from the consumer. This call wil populate the stream
2492 * list of that channel and set the ust objects.
2493 */
2494 if (usess->consumer->enabled) {
2495 ret = ust_consumer_get_channel(socket, ua_chan);
2496 if (ret < 0) {
2497 goto error_destroy;
2498 }
2499 }
2500
2501 rcu_read_unlock();
2502 return 0;
2503
2504 error_destroy:
2505 lttng_fd_put(LTTNG_FD_APPS, nb_fd);
2506 error_fd_get_stream:
2507 /*
2508 * Initiate a destroy channel on the consumer since we had an error
2509 * handling it on our side. The return value is of no importance since we
2510 * already have a ret value set by the previous error that we need to
2511 * return.
2512 */
2513 (void) ust_consumer_destroy_channel(socket, ua_chan);
2514 error_ask:
2515 lttng_fd_put(LTTNG_FD_APPS, 1);
2516 error:
2517 health_code_update();
2518 rcu_read_unlock();
2519 return ret;
2520 }
2521
2522 /*
2523 * Duplicate the ust data object of the ust app stream and save it in the
2524 * buffer registry stream.
2525 *
2526 * Return 0 on success or else a negative value.
2527 */
2528 static int duplicate_stream_object(struct buffer_reg_stream *reg_stream,
2529 struct ust_app_stream *stream)
2530 {
2531 int ret;
2532
2533 assert(reg_stream);
2534 assert(stream);
2535
2536 /* Reserve the amount of file descriptor we need. */
2537 ret = lttng_fd_get(LTTNG_FD_APPS, 2);
2538 if (ret < 0) {
2539 ERR("Exhausted number of available FD upon duplicate stream");
2540 goto error;
2541 }
2542
2543 /* Duplicate object for stream once the original is in the registry. */
2544 ret = ustctl_duplicate_ust_object_data(&stream->obj,
2545 reg_stream->obj.ust);
2546 if (ret < 0) {
2547 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2548 reg_stream->obj.ust, stream->obj, ret);
2549 lttng_fd_put(LTTNG_FD_APPS, 2);
2550 goto error;
2551 }
2552 stream->handle = stream->obj->handle;
2553
2554 error:
2555 return ret;
2556 }
2557
2558 /*
2559 * Duplicate the ust data object of the ust app. channel and save it in the
2560 * buffer registry channel.
2561 *
2562 * Return 0 on success or else a negative value.
2563 */
2564 static int duplicate_channel_object(struct buffer_reg_channel *reg_chan,
2565 struct ust_app_channel *ua_chan)
2566 {
2567 int ret;
2568
2569 assert(reg_chan);
2570 assert(ua_chan);
2571
2572 /* Need two fds for the channel. */
2573 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
2574 if (ret < 0) {
2575 ERR("Exhausted number of available FD upon duplicate channel");
2576 goto error_fd_get;
2577 }
2578
2579 /* Duplicate object for stream once the original is in the registry. */
2580 ret = ustctl_duplicate_ust_object_data(&ua_chan->obj, reg_chan->obj.ust);
2581 if (ret < 0) {
2582 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2583 reg_chan->obj.ust, ua_chan->obj, ret);
2584 goto error;
2585 }
2586 ua_chan->handle = ua_chan->obj->handle;
2587
2588 return 0;
2589
2590 error:
2591 lttng_fd_put(LTTNG_FD_APPS, 1);
2592 error_fd_get:
2593 return ret;
2594 }
2595
2596 /*
2597 * For a given channel buffer registry, setup all streams of the given ust
2598 * application channel.
2599 *
2600 * Return 0 on success or else a negative value.
2601 */
2602 static int setup_buffer_reg_streams(struct buffer_reg_channel *reg_chan,
2603 struct ust_app_channel *ua_chan,
2604 struct ust_app *app)
2605 {
2606 int ret = 0;
2607 struct ust_app_stream *stream, *stmp;
2608
2609 assert(reg_chan);
2610 assert(ua_chan);
2611
2612 DBG2("UST app setup buffer registry stream");
2613
2614 /* Send all streams to application. */
2615 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
2616 struct buffer_reg_stream *reg_stream;
2617
2618 ret = buffer_reg_stream_create(&reg_stream);
2619 if (ret < 0) {
2620 goto error;
2621 }
2622
2623 /*
2624 * Keep original pointer and nullify it in the stream so the delete
2625 * stream call does not release the object.
2626 */
2627 reg_stream->obj.ust = stream->obj;
2628 stream->obj = NULL;
2629 buffer_reg_stream_add(reg_stream, reg_chan);
2630
2631 /* We don't need the streams anymore. */
2632 cds_list_del(&stream->list);
2633 delete_ust_app_stream(-1, stream, app);
2634 }
2635
2636 error:
2637 return ret;
2638 }
2639
2640 /*
2641 * Create a buffer registry channel for the given session registry and
2642 * application channel object. If regp pointer is valid, it's set with the
2643 * created object. Important, the created object is NOT added to the session
2644 * registry hash table.
2645 *
2646 * Return 0 on success else a negative value.
2647 */
2648 static int create_buffer_reg_channel(struct buffer_reg_session *reg_sess,
2649 struct ust_app_channel *ua_chan, struct buffer_reg_channel **regp)
2650 {
2651 int ret;
2652 struct buffer_reg_channel *reg_chan = NULL;
2653
2654 assert(reg_sess);
2655 assert(ua_chan);
2656
2657 DBG2("UST app creating buffer registry channel for %s", ua_chan->name);
2658
2659 /* Create buffer registry channel. */
2660 ret = buffer_reg_channel_create(ua_chan->tracing_channel_id, &reg_chan);
2661 if (ret < 0) {
2662 goto error_create;
2663 }
2664 assert(reg_chan);
2665 reg_chan->consumer_key = ua_chan->key;
2666 reg_chan->subbuf_size = ua_chan->attr.subbuf_size;
2667 reg_chan->num_subbuf = ua_chan->attr.num_subbuf;
2668
2669 /* Create and add a channel registry to session. */
2670 ret = ust_registry_channel_add(reg_sess->reg.ust,
2671 ua_chan->tracing_channel_id);
2672 if (ret < 0) {
2673 goto error;
2674 }
2675 buffer_reg_channel_add(reg_sess, reg_chan);
2676
2677 if (regp) {
2678 *regp = reg_chan;
2679 }
2680
2681 return 0;
2682
2683 error:
2684 /* Safe because the registry channel object was not added to any HT. */
2685 buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST);
2686 error_create:
2687 return ret;
2688 }
2689
2690 /*
2691 * Setup buffer registry channel for the given session registry and application
2692 * channel object. If regp pointer is valid, it's set with the created object.
2693 *
2694 * Return 0 on success else a negative value.
2695 */
2696 static int setup_buffer_reg_channel(struct buffer_reg_session *reg_sess,
2697 struct ust_app_channel *ua_chan, struct buffer_reg_channel *reg_chan,
2698 struct ust_app *app)
2699 {
2700 int ret;
2701
2702 assert(reg_sess);
2703 assert(reg_chan);
2704 assert(ua_chan);
2705 assert(ua_chan->obj);
2706
2707 DBG2("UST app setup buffer registry channel for %s", ua_chan->name);
2708
2709 /* Setup all streams for the registry. */
2710 ret = setup_buffer_reg_streams(reg_chan, ua_chan, app);
2711 if (ret < 0) {
2712 goto error;
2713 }
2714
2715 reg_chan->obj.ust = ua_chan->obj;
2716 ua_chan->obj = NULL;
2717
2718 return 0;
2719
2720 error:
2721 buffer_reg_channel_remove(reg_sess, reg_chan);
2722 buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST);
2723 return ret;
2724 }
2725
2726 /*
2727 * Send buffer registry channel to the application.
2728 *
2729 * Return 0 on success else a negative value.
2730 */
2731 static int send_channel_uid_to_ust(struct buffer_reg_channel *reg_chan,
2732 struct ust_app *app, struct ust_app_session *ua_sess,
2733 struct ust_app_channel *ua_chan)
2734 {
2735 int ret;
2736 struct buffer_reg_stream *reg_stream;
2737
2738 assert(reg_chan);
2739 assert(app);
2740 assert(ua_sess);
2741 assert(ua_chan);
2742
2743 DBG("UST app sending buffer registry channel to ust sock %d", app->sock);
2744
2745 ret = duplicate_channel_object(reg_chan, ua_chan);
2746 if (ret < 0) {
2747 goto error;
2748 }
2749
2750 /* Send channel to the application. */
2751 ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan);
2752 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
2753 ret = -ENOTCONN; /* Caused by app exiting. */
2754 goto error;
2755 } else if (ret < 0) {
2756 goto error;
2757 }
2758
2759 health_code_update();
2760
2761 /* Send all streams to application. */
2762 pthread_mutex_lock(&reg_chan->stream_list_lock);
2763 cds_list_for_each_entry(reg_stream, &reg_chan->streams, lnode) {
2764 struct ust_app_stream stream;
2765
2766 ret = duplicate_stream_object(reg_stream, &stream);
2767 if (ret < 0) {
2768 goto error_stream_unlock;
2769 }
2770
2771 ret = ust_consumer_send_stream_to_ust(app, ua_chan, &stream);
2772 if (ret < 0) {
2773 (void) release_ust_app_stream(-1, &stream, app);
2774 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
2775 ret = -ENOTCONN; /* Caused by app exiting. */
2776 }
2777 goto error_stream_unlock;
2778 }
2779
2780 /*
2781 * The return value is not important here. This function will output an
2782 * error if needed.
2783 */
2784 (void) release_ust_app_stream(-1, &stream, app);
2785 }
2786 ua_chan->is_sent = 1;
2787
2788 error_stream_unlock:
2789 pthread_mutex_unlock(&reg_chan->stream_list_lock);
2790 error:
2791 return ret;
2792 }
2793
2794 /*
2795 * Create and send to the application the created buffers with per UID buffers.
2796 *
2797 * This MUST be called with a RCU read side lock acquired.
2798 * The session list lock and the session's lock must be acquired.
2799 *
2800 * Return 0 on success else a negative value.
2801 */
2802 static int create_channel_per_uid(struct ust_app *app,
2803 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
2804 struct ust_app_channel *ua_chan)
2805 {
2806 int ret;
2807 struct buffer_reg_uid *reg_uid;
2808 struct buffer_reg_channel *reg_chan;
2809 struct ltt_session *session = NULL;
2810 enum lttng_error_code notification_ret;
2811 struct ust_registry_channel *chan_reg;
2812
2813 assert(app);
2814 assert(usess);
2815 assert(ua_sess);
2816 assert(ua_chan);
2817
2818 DBG("UST app creating channel %s with per UID buffers", ua_chan->name);
2819
2820 reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid);
2821 /*
2822 * The session creation handles the creation of this global registry
2823 * object. If none can be find, there is a code flow problem or a
2824 * teardown race.
2825 */
2826 assert(reg_uid);
2827
2828 reg_chan = buffer_reg_channel_find(ua_chan->tracing_channel_id,
2829 reg_uid);
2830 if (reg_chan) {
2831 goto send_channel;
2832 }
2833
2834 /* Create the buffer registry channel object. */
2835 ret = create_buffer_reg_channel(reg_uid->registry, ua_chan, &reg_chan);
2836 if (ret < 0) {
2837 ERR("Error creating the UST channel \"%s\" registry instance",
2838 ua_chan->name);
2839 goto error;
2840 }
2841
2842 session = session_find_by_id(ua_sess->tracing_id);
2843 assert(session);
2844 assert(pthread_mutex_trylock(&session->lock));
2845 assert(session_trylock_list());
2846
2847 /*
2848 * Create the buffers on the consumer side. This call populates the
2849 * ust app channel object with all streams and data object.
2850 */
2851 ret = do_consumer_create_channel(usess, ua_sess, ua_chan,
2852 app->bits_per_long, reg_uid->registry->reg.ust,
2853 session->most_recent_chunk_id.value);
2854 if (ret < 0) {
2855 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2856 ua_chan->name);
2857
2858 /*
2859 * Let's remove the previously created buffer registry channel so
2860 * it's not visible anymore in the session registry.
2861 */
2862 ust_registry_channel_del_free(reg_uid->registry->reg.ust,
2863 ua_chan->tracing_channel_id, false);
2864 buffer_reg_channel_remove(reg_uid->registry, reg_chan);
2865 buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST);
2866 goto error;
2867 }
2868
2869 /*
2870 * Setup the streams and add it to the session registry.
2871 */
2872 ret = setup_buffer_reg_channel(reg_uid->registry,
2873 ua_chan, reg_chan, app);
2874 if (ret < 0) {
2875 ERR("Error setting up UST channel \"%s\"", ua_chan->name);
2876 goto error;
2877 }
2878
2879 /* Notify the notification subsystem of the channel's creation. */
2880 pthread_mutex_lock(&reg_uid->registry->reg.ust->lock);
2881 chan_reg = ust_registry_channel_find(reg_uid->registry->reg.ust,
2882 ua_chan->tracing_channel_id);
2883 assert(chan_reg);
2884 chan_reg->consumer_key = ua_chan->key;
2885 chan_reg = NULL;
2886 pthread_mutex_unlock(&reg_uid->registry->reg.ust->lock);
2887
2888 notification_ret = notification_thread_command_add_channel(
2889 notification_thread_handle, session->name,
2890 ua_sess->effective_credentials.uid,
2891 ua_sess->effective_credentials.gid, ua_chan->name,
2892 ua_chan->key, LTTNG_DOMAIN_UST,
2893 ua_chan->attr.subbuf_size * ua_chan->attr.num_subbuf);
2894 if (notification_ret != LTTNG_OK) {
2895 ret = - (int) notification_ret;
2896 ERR("Failed to add channel to notification thread");
2897 goto error;
2898 }
2899
2900 send_channel:
2901 /* Send buffers to the application. */
2902 ret = send_channel_uid_to_ust(reg_chan, app, ua_sess, ua_chan);
2903 if (ret < 0) {
2904 if (ret != -ENOTCONN) {
2905 ERR("Error sending channel to application");
2906 }
2907 goto error;
2908 }
2909
2910 error:
2911 if (session) {
2912 session_put(session);
2913 }
2914 return ret;
2915 }
2916
2917 /*
2918 * Create and send to the application the created buffers with per PID buffers.
2919 *
2920 * Called with UST app session lock held.
2921 * The session list lock and the session's lock must be acquired.
2922 *
2923 * Return 0 on success else a negative value.
2924 */
2925 static int create_channel_per_pid(struct ust_app *app,
2926 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
2927 struct ust_app_channel *ua_chan)
2928 {
2929 int ret;
2930 struct ust_registry_session *registry;
2931 enum lttng_error_code cmd_ret;
2932 struct ltt_session *session = NULL;
2933 uint64_t chan_reg_key;
2934 struct ust_registry_channel *chan_reg;
2935
2936 assert(app);
2937 assert(usess);
2938 assert(ua_sess);
2939 assert(ua_chan);
2940
2941 DBG("UST app creating channel %s with per PID buffers", ua_chan->name);
2942
2943 rcu_read_lock();
2944
2945 registry = get_session_registry(ua_sess);
2946 /* The UST app session lock is held, registry shall not be null. */
2947 assert(registry);
2948
2949 /* Create and add a new channel registry to session. */
2950 ret = ust_registry_channel_add(registry, ua_chan->key);
2951 if (ret < 0) {
2952 ERR("Error creating the UST channel \"%s\" registry instance",
2953 ua_chan->name);
2954 goto error;
2955 }
2956
2957 session = session_find_by_id(ua_sess->tracing_id);
2958 assert(session);
2959
2960 assert(pthread_mutex_trylock(&session->lock));
2961 assert(session_trylock_list());
2962
2963 /* Create and get channel on the consumer side. */
2964 ret = do_consumer_create_channel(usess, ua_sess, ua_chan,
2965 app->bits_per_long, registry,
2966 session->most_recent_chunk_id.value);
2967 if (ret < 0) {
2968 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2969 ua_chan->name);
2970 goto error_remove_from_registry;
2971 }
2972
2973 ret = send_channel_pid_to_ust(app, ua_sess, ua_chan);
2974 if (ret < 0) {
2975 if (ret != -ENOTCONN) {
2976 ERR("Error sending channel to application");
2977 }
2978 goto error_remove_from_registry;
2979 }
2980
2981 chan_reg_key = ua_chan->key;
2982 pthread_mutex_lock(&registry->lock);
2983 chan_reg = ust_registry_channel_find(registry, chan_reg_key);
2984 assert(chan_reg);
2985 chan_reg->consumer_key = ua_chan->key;
2986 pthread_mutex_unlock(&registry->lock);
2987
2988 cmd_ret = notification_thread_command_add_channel(
2989 notification_thread_handle, session->name,
2990 ua_sess->effective_credentials.uid,
2991 ua_sess->effective_credentials.gid, ua_chan->name,
2992 ua_chan->key, LTTNG_DOMAIN_UST,
2993 ua_chan->attr.subbuf_size * ua_chan->attr.num_subbuf);
2994 if (cmd_ret != LTTNG_OK) {
2995 ret = - (int) cmd_ret;
2996 ERR("Failed to add channel to notification thread");
2997 goto error_remove_from_registry;
2998 }
2999
3000 error_remove_from_registry:
3001 if (ret) {
3002 ust_registry_channel_del_free(registry, ua_chan->key, false);
3003 }
3004 error:
3005 rcu_read_unlock();
3006 if (session) {
3007 session_put(session);
3008 }
3009 return ret;
3010 }
3011
3012 /*
3013 * From an already allocated ust app channel, create the channel buffers if
3014 * needed and send them to the application. This MUST be called with a RCU read
3015 * side lock acquired.
3016 *
3017 * Called with UST app session lock held.
3018 *
3019 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3020 * the application exited concurrently.
3021 */
3022 static int ust_app_channel_send(struct ust_app *app,
3023 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
3024 struct ust_app_channel *ua_chan)
3025 {
3026 int ret;
3027
3028 assert(app);
3029 assert(usess);
3030 assert(usess->active);
3031 assert(ua_sess);
3032 assert(ua_chan);
3033
3034 /* Handle buffer type before sending the channel to the application. */
3035 switch (usess->buffer_type) {
3036 case LTTNG_BUFFER_PER_UID:
3037 {
3038 ret = create_channel_per_uid(app, usess, ua_sess, ua_chan);
3039 if (ret < 0) {
3040 goto error;
3041 }
3042 break;
3043 }
3044 case LTTNG_BUFFER_PER_PID:
3045 {
3046 ret = create_channel_per_pid(app, usess, ua_sess, ua_chan);
3047 if (ret < 0) {
3048 goto error;
3049 }
3050 break;
3051 }
3052 default:
3053 assert(0);
3054 ret = -EINVAL;
3055 goto error;
3056 }
3057
3058 /* Initialize ust objd object using the received handle and add it. */
3059 lttng_ht_node_init_ulong(&ua_chan->ust_objd_node, ua_chan->handle);
3060 lttng_ht_add_unique_ulong(app->ust_objd, &ua_chan->ust_objd_node);
3061
3062 /* If channel is not enabled, disable it on the tracer */
3063 if (!ua_chan->enabled) {
3064 ret = disable_ust_channel(app, ua_sess, ua_chan);
3065 if (ret < 0) {
3066 goto error;
3067 }
3068 }
3069
3070 error:
3071 return ret;
3072 }
3073
3074 /*
3075 * Create UST app channel and return it through ua_chanp if not NULL.
3076 *
3077 * Called with UST app session lock and RCU read-side lock held.
3078 *
3079 * Return 0 on success or else a negative value.
3080 */
3081 static int ust_app_channel_allocate(struct ust_app_session *ua_sess,
3082 struct ltt_ust_channel *uchan,
3083 enum lttng_ust_chan_type type, struct ltt_ust_session *usess,
3084 struct ust_app_channel **ua_chanp)
3085 {
3086 int ret = 0;
3087 struct lttng_ht_iter iter;
3088 struct lttng_ht_node_str *ua_chan_node;
3089 struct ust_app_channel *ua_chan;
3090
3091 /* Lookup channel in the ust app session */
3092 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
3093 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
3094 if (ua_chan_node != NULL) {
3095 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
3096 goto end;
3097 }
3098
3099 ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr);
3100 if (ua_chan == NULL) {
3101 /* Only malloc can fail here */
3102 ret = -ENOMEM;
3103 goto error;
3104 }
3105 shadow_copy_channel(ua_chan, uchan);
3106
3107 /* Set channel type. */
3108 ua_chan->attr.type = type;
3109
3110 /* Only add the channel if successful on the tracer side. */
3111 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
3112 end:
3113 if (ua_chanp) {
3114 *ua_chanp = ua_chan;
3115 }
3116
3117 /* Everything went well. */
3118 return 0;
3119
3120 error:
3121 return ret;
3122 }
3123
3124 /*
3125 * Create UST app event and create it on the tracer side.
3126 *
3127 * Called with ust app session mutex held.
3128 */
3129 static
3130 int create_ust_app_event(struct ust_app_session *ua_sess,
3131 struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent,
3132 struct ust_app *app)
3133 {
3134 int ret = 0;
3135 struct ust_app_event *ua_event;
3136
3137 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
3138 if (ua_event == NULL) {
3139 /* Only failure mode of alloc_ust_app_event(). */
3140 ret = -ENOMEM;
3141 goto end;
3142 }
3143 shadow_copy_event(ua_event, uevent);
3144
3145 /* Create it on the tracer side */
3146 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
3147 if (ret < 0) {
3148 /*
3149 * Not found previously means that it does not exist on the
3150 * tracer. If the application reports that the event existed,
3151 * it means there is a bug in the sessiond or lttng-ust
3152 * (or corruption, etc.)
3153 */
3154 if (ret == -LTTNG_UST_ERR_EXIST) {
3155 ERR("Tracer for application reported that an event being created already existed: "
3156 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3157 uevent->attr.name,
3158 app->pid, app->ppid, app->uid,
3159 app->gid);
3160 }
3161 goto error;
3162 }
3163
3164 add_unique_ust_app_event(ua_chan, ua_event);
3165
3166 DBG2("UST app create event %s for PID %d completed", ua_event->name,
3167 app->pid);
3168
3169 end:
3170 return ret;
3171
3172 error:
3173 /* Valid. Calling here is already in a read side lock */
3174 delete_ust_app_event(-1, ua_event, app);
3175 return ret;
3176 }
3177
3178 /*
3179 * Create UST metadata and open it on the tracer side.
3180 *
3181 * Called with UST app session lock held and RCU read side lock.
3182 */
3183 static int create_ust_app_metadata(struct ust_app_session *ua_sess,
3184 struct ust_app *app, struct consumer_output *consumer)
3185 {
3186 int ret = 0;
3187 struct ust_app_channel *metadata;
3188 struct consumer_socket *socket;
3189 struct ust_registry_session *registry;
3190 struct ltt_session *session = NULL;
3191
3192 assert(ua_sess);
3193 assert(app);
3194 assert(consumer);
3195
3196 registry = get_session_registry(ua_sess);
3197 /* The UST app session is held registry shall not be null. */
3198 assert(registry);
3199
3200 pthread_mutex_lock(&registry->lock);
3201
3202 /* Metadata already exists for this registry or it was closed previously */
3203 if (registry->metadata_key || registry->metadata_closed) {
3204 ret = 0;
3205 goto error;
3206 }
3207
3208 /* Allocate UST metadata */
3209 metadata = alloc_ust_app_channel(DEFAULT_METADATA_NAME, ua_sess, NULL);
3210 if (!metadata) {
3211 /* malloc() failed */
3212 ret = -ENOMEM;
3213 goto error;
3214 }
3215
3216 memcpy(&metadata->attr, &ua_sess->metadata_attr, sizeof(metadata->attr));
3217
3218 /* Need one fd for the channel. */
3219 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
3220 if (ret < 0) {
3221 ERR("Exhausted number of available FD upon create metadata");
3222 goto error;
3223 }
3224
3225 /* Get the right consumer socket for the application. */
3226 socket = consumer_find_socket_by_bitness(app->bits_per_long, consumer);
3227 if (!socket) {
3228 ret = -EINVAL;
3229 goto error_consumer;
3230 }
3231
3232 /*
3233 * Keep metadata key so we can identify it on the consumer side. Assign it
3234 * to the registry *before* we ask the consumer so we avoid the race of the
3235 * consumer requesting the metadata and the ask_channel call on our side
3236 * did not returned yet.
3237 */
3238 registry->metadata_key = metadata->key;
3239
3240 session = session_find_by_id(ua_sess->tracing_id);
3241 assert(session);
3242
3243 assert(pthread_mutex_trylock(&session->lock));
3244 assert(session_trylock_list());
3245
3246 /*
3247 * Ask the metadata channel creation to the consumer. The metadata object
3248 * will be created by the consumer and kept their. However, the stream is
3249 * never added or monitored until we do a first push metadata to the
3250 * consumer.
3251 */
3252 ret = ust_consumer_ask_channel(ua_sess, metadata, consumer, socket,
3253 registry, session->current_trace_chunk);
3254 if (ret < 0) {
3255 /* Nullify the metadata key so we don't try to close it later on. */
3256 registry->metadata_key = 0;
3257 goto error_consumer;
3258 }
3259
3260 /*
3261 * The setup command will make the metadata stream be sent to the relayd,
3262 * if applicable, and the thread managing the metadatas. This is important
3263 * because after this point, if an error occurs, the only way the stream
3264 * can be deleted is to be monitored in the consumer.
3265 */
3266 ret = consumer_setup_metadata(socket, metadata->key);
3267 if (ret < 0) {
3268 /* Nullify the metadata key so we don't try to close it later on. */
3269 registry->metadata_key = 0;
3270 goto error_consumer;
3271 }
3272
3273 DBG2("UST metadata with key %" PRIu64 " created for app pid %d",
3274 metadata->key, app->pid);
3275
3276 error_consumer:
3277 lttng_fd_put(LTTNG_FD_APPS, 1);
3278 delete_ust_app_channel(-1, metadata, app);
3279 error:
3280 pthread_mutex_unlock(&registry->lock);
3281 if (session) {
3282 session_put(session);
3283 }
3284 return ret;
3285 }
3286
3287 /*
3288 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3289 * acquired before calling this function.
3290 */
3291 struct ust_app *ust_app_find_by_pid(pid_t pid)
3292 {
3293 struct ust_app *app = NULL;
3294 struct lttng_ht_node_ulong *node;
3295 struct lttng_ht_iter iter;
3296
3297 lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) pid), &iter);
3298 node = lttng_ht_iter_get_node_ulong(&iter);
3299 if (node == NULL) {
3300 DBG2("UST app no found with pid %d", pid);
3301 goto error;
3302 }
3303
3304 DBG2("Found UST app by pid %d", pid);
3305
3306 app = caa_container_of(node, struct ust_app, pid_n);
3307
3308 error:
3309 return app;
3310 }
3311
3312 /*
3313 * Allocate and init an UST app object using the registration information and
3314 * the command socket. This is called when the command socket connects to the
3315 * session daemon.
3316 *
3317 * The object is returned on success or else NULL.
3318 */
3319 struct ust_app *ust_app_create(struct ust_register_msg *msg, int sock)
3320 {
3321 struct ust_app *lta = NULL;
3322
3323 assert(msg);
3324 assert(sock >= 0);
3325
3326 DBG3("UST app creating application for socket %d", sock);
3327
3328 if ((msg->bits_per_long == 64 &&
3329 (uatomic_read(&ust_consumerd64_fd) == -EINVAL))
3330 || (msg->bits_per_long == 32 &&
3331 (uatomic_read(&ust_consumerd32_fd) == -EINVAL))) {
3332 ERR("Registration failed: application \"%s\" (pid: %d) has "
3333 "%d-bit long, but no consumerd for this size is available.\n",
3334 msg->name, msg->pid, msg->bits_per_long);
3335 goto error;
3336 }
3337
3338 lta = zmalloc(sizeof(struct ust_app));
3339 if (lta == NULL) {
3340 PERROR("malloc");
3341 goto error;
3342 }
3343
3344 lta->ppid = msg->ppid;
3345 lta->uid = msg->uid;
3346 lta->gid = msg->gid;
3347
3348 lta->bits_per_long = msg->bits_per_long;
3349 lta->uint8_t_alignment = msg->uint8_t_alignment;
3350 lta->uint16_t_alignment = msg->uint16_t_alignment;
3351 lta->uint32_t_alignment = msg->uint32_t_alignment;
3352 lta->uint64_t_alignment = msg->uint64_t_alignment;
3353 lta->long_alignment = msg->long_alignment;
3354 lta->byte_order = msg->byte_order;
3355
3356 lta->v_major = msg->major;
3357 lta->v_minor = msg->minor;
3358 lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3359 lta->ust_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
3360 lta->ust_sessions_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
3361 lta->notify_sock = -1;
3362
3363 /* Copy name and make sure it's NULL terminated. */
3364 strncpy(lta->name, msg->name, sizeof(lta->name));
3365 lta->name[UST_APP_PROCNAME_LEN] = '\0';
3366
3367 /*
3368 * Before this can be called, when receiving the registration information,
3369 * the application compatibility is checked. So, at this point, the
3370 * application can work with this session daemon.
3371 */
3372 lta->compatible = 1;
3373
3374 lta->pid = msg->pid;
3375 lttng_ht_node_init_ulong(&lta->pid_n, (unsigned long) lta->pid);
3376 lta->sock = sock;
3377 pthread_mutex_init(&lta->sock_lock, NULL);
3378 lttng_ht_node_init_ulong(&lta->sock_n, (unsigned long) lta->sock);
3379
3380 CDS_INIT_LIST_HEAD(&lta->teardown_head);
3381 error:
3382 return lta;
3383 }
3384
3385 /*
3386 * For a given application object, add it to every hash table.
3387 */
3388 void ust_app_add(struct ust_app *app)
3389 {
3390 assert(app);
3391 assert(app->notify_sock >= 0);
3392
3393 app->registration_time = time(NULL);
3394
3395 rcu_read_lock();
3396
3397 /*
3398 * On a re-registration, we want to kick out the previous registration of
3399 * that pid
3400 */
3401 lttng_ht_add_replace_ulong(ust_app_ht, &app->pid_n);
3402
3403 /*
3404 * The socket _should_ be unique until _we_ call close. So, a add_unique
3405 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3406 * already in the table.
3407 */
3408 lttng_ht_add_unique_ulong(ust_app_ht_by_sock, &app->sock_n);
3409
3410 /* Add application to the notify socket hash table. */
3411 lttng_ht_node_init_ulong(&app->notify_sock_n, app->notify_sock);
3412 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock, &app->notify_sock_n);
3413
3414 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3415 "notify_sock:%d (version %d.%d)", app->pid, app->ppid, app->uid,
3416 app->gid, app->sock, app->name, app->notify_sock, app->v_major,
3417 app->v_minor);
3418
3419 rcu_read_unlock();
3420 }
3421
3422 /*
3423 * Set the application version into the object.
3424 *
3425 * Return 0 on success else a negative value either an errno code or a
3426 * LTTng-UST error code.
3427 */
3428 int ust_app_version(struct ust_app *app)
3429 {
3430 int ret;
3431
3432 assert(app);
3433
3434 pthread_mutex_lock(&app->sock_lock);
3435 ret = ustctl_tracer_version(app->sock, &app->version);
3436 pthread_mutex_unlock(&app->sock_lock);
3437 if (ret < 0) {
3438 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
3439 ERR("UST app %d version failed with ret %d", app->sock, ret);
3440 } else {
3441 DBG3("UST app %d version failed. Application is dead", app->sock);
3442 }
3443 }
3444
3445 return ret;
3446 }
3447
3448 /*
3449 * Unregister app by removing it from the global traceable app list and freeing
3450 * the data struct.
3451 *
3452 * The socket is already closed at this point so no close to sock.
3453 */
3454 void ust_app_unregister(int sock)
3455 {
3456 struct ust_app *lta;
3457 struct lttng_ht_node_ulong *node;
3458 struct lttng_ht_iter ust_app_sock_iter;
3459 struct lttng_ht_iter iter;
3460 struct ust_app_session *ua_sess;
3461 int ret;
3462
3463 rcu_read_lock();
3464
3465 /* Get the node reference for a call_rcu */
3466 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &ust_app_sock_iter);
3467 node = lttng_ht_iter_get_node_ulong(&ust_app_sock_iter);
3468 assert(node);
3469
3470 lta = caa_container_of(node, struct ust_app, sock_n);
3471 DBG("PID %d unregistering with sock %d", lta->pid, sock);
3472
3473 /*
3474 * For per-PID buffers, perform "push metadata" and flush all
3475 * application streams before removing app from hash tables,
3476 * ensuring proper behavior of data_pending check.
3477 * Remove sessions so they are not visible during deletion.
3478 */
3479 cds_lfht_for_each_entry(lta->sessions->ht, &iter.iter, ua_sess,
3480 node.node) {
3481 struct ust_registry_session *registry;
3482
3483 ret = lttng_ht_del(lta->sessions, &iter);
3484 if (ret) {
3485 /* The session was already removed so scheduled for teardown. */
3486 continue;
3487 }
3488
3489 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) {
3490 (void) ust_app_flush_app_session(lta, ua_sess);
3491 }
3492
3493 /*
3494 * Add session to list for teardown. This is safe since at this point we
3495 * are the only one using this list.
3496 */
3497 pthread_mutex_lock(&ua_sess->lock);
3498
3499 if (ua_sess->deleted) {
3500 pthread_mutex_unlock(&ua_sess->lock);
3501 continue;
3502 }
3503
3504 /*
3505 * Normally, this is done in the delete session process which is
3506 * executed in the call rcu below. However, upon registration we can't
3507 * afford to wait for the grace period before pushing data or else the
3508 * data pending feature can race between the unregistration and stop
3509 * command where the data pending command is sent *before* the grace
3510 * period ended.
3511 *
3512 * The close metadata below nullifies the metadata pointer in the
3513 * session so the delete session will NOT push/close a second time.
3514 */
3515 registry = get_session_registry(ua_sess);
3516 if (registry) {
3517 /* Push metadata for application before freeing the application. */
3518 (void) push_metadata(registry, ua_sess->consumer);
3519
3520 /*
3521 * Don't ask to close metadata for global per UID buffers. Close
3522 * metadata only on destroy trace session in this case. Also, the
3523 * previous push metadata could have flag the metadata registry to
3524 * close so don't send a close command if closed.
3525 */
3526 if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) {
3527 /* And ask to close it for this session registry. */
3528 (void) close_metadata(registry, ua_sess->consumer);
3529 }
3530 }
3531 cds_list_add(&ua_sess->teardown_node, &lta->teardown_head);
3532
3533 pthread_mutex_unlock(&ua_sess->lock);
3534 }
3535
3536 /* Remove application from PID hash table */
3537 ret = lttng_ht_del(ust_app_ht_by_sock, &ust_app_sock_iter);
3538 assert(!ret);
3539
3540 /*
3541 * Remove application from notify hash table. The thread handling the
3542 * notify socket could have deleted the node so ignore on error because
3543 * either way it's valid. The close of that socket is handled by the
3544 * apps_notify_thread.
3545 */
3546 iter.iter.node = &lta->notify_sock_n.node;
3547 (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter);
3548
3549 /*
3550 * Ignore return value since the node might have been removed before by an
3551 * add replace during app registration because the PID can be reassigned by
3552 * the OS.
3553 */
3554 iter.iter.node = &lta->pid_n.node;
3555 ret = lttng_ht_del(ust_app_ht, &iter);
3556 if (ret) {
3557 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3558 lta->pid);
3559 }
3560
3561 /* Free memory */
3562 call_rcu(&lta->pid_n.head, delete_ust_app_rcu);
3563
3564 rcu_read_unlock();
3565 return;
3566 }
3567
3568 /*
3569 * Fill events array with all events name of all registered apps.
3570 */
3571 int ust_app_list_events(struct lttng_event **events)
3572 {
3573 int ret, handle;
3574 size_t nbmem, count = 0;
3575 struct lttng_ht_iter iter;
3576 struct ust_app *app;
3577 struct lttng_event *tmp_event;
3578
3579 nbmem = UST_APP_EVENT_LIST_SIZE;
3580 tmp_event = zmalloc(nbmem * sizeof(struct lttng_event));
3581 if (tmp_event == NULL) {
3582 PERROR("zmalloc ust app events");
3583 ret = -ENOMEM;
3584 goto error;
3585 }
3586
3587 rcu_read_lock();
3588
3589 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3590 struct lttng_ust_tracepoint_iter uiter;
3591
3592 health_code_update();
3593
3594 if (!app->compatible) {
3595 /*
3596 * TODO: In time, we should notice the caller of this error by
3597 * telling him that this is a version error.
3598 */
3599 continue;
3600 }
3601 pthread_mutex_lock(&app->sock_lock);
3602 handle = ustctl_tracepoint_list(app->sock);
3603 if (handle < 0) {
3604 if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) {
3605 ERR("UST app list events getting handle failed for app pid %d",
3606 app->pid);
3607 }
3608 pthread_mutex_unlock(&app->sock_lock);
3609 continue;
3610 }
3611
3612 while ((ret = ustctl_tracepoint_list_get(app->sock, handle,
3613 &uiter)) != -LTTNG_UST_ERR_NOENT) {
3614 /* Handle ustctl error. */
3615 if (ret < 0) {
3616 int release_ret;
3617
3618 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
3619 ERR("UST app tp list get failed for app %d with ret %d",
3620 app->sock, ret);
3621 } else {
3622 DBG3("UST app tp list get failed. Application is dead");
3623 /*
3624 * This is normal behavior, an application can die during the
3625 * creation process. Don't report an error so the execution can
3626 * continue normally. Continue normal execution.
3627 */
3628 break;
3629 }
3630 free(tmp_event);
3631 release_ret = ustctl_release_handle(app->sock, handle);
3632 if (release_ret < 0 &&
3633 release_ret != -LTTNG_UST_ERR_EXITING &&
3634 release_ret != -EPIPE) {
3635 ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret);
3636 }
3637 pthread_mutex_unlock(&app->sock_lock);
3638 goto rcu_error;
3639 }
3640
3641 health_code_update();
3642 if (count >= nbmem) {
3643 /* In case the realloc fails, we free the memory */
3644 struct lttng_event *new_tmp_event;
3645 size_t new_nbmem;
3646
3647 new_nbmem = nbmem << 1;
3648 DBG2("Reallocating event list from %zu to %zu entries",
3649 nbmem, new_nbmem);
3650 new_tmp_event = realloc(tmp_event,
3651 new_nbmem * sizeof(struct lttng_event));
3652 if (new_tmp_event == NULL) {
3653 int release_ret;
3654
3655 PERROR("realloc ust app events");
3656 free(tmp_event);
3657 ret = -ENOMEM;
3658 release_ret = ustctl_release_handle(app->sock, handle);
3659 if (release_ret < 0 &&
3660 release_ret != -LTTNG_UST_ERR_EXITING &&
3661 release_ret != -EPIPE) {
3662 ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret);
3663 }
3664 pthread_mutex_unlock(&app->sock_lock);
3665 goto rcu_error;
3666 }
3667 /* Zero the new memory */
3668 memset(new_tmp_event + nbmem, 0,
3669 (new_nbmem - nbmem) * sizeof(struct lttng_event));
3670 nbmem = new_nbmem;
3671 tmp_event = new_tmp_event;
3672 }
3673 memcpy(tmp_event[count].name, uiter.name, LTTNG_UST_SYM_NAME_LEN);
3674 tmp_event[count].loglevel = uiter.loglevel;
3675 tmp_event[count].type = (enum lttng_event_type) LTTNG_UST_TRACEPOINT;
3676 tmp_event[count].pid = app->pid;
3677 tmp_event[count].enabled = -1;
3678 count++;
3679 }
3680 ret = ustctl_release_handle(app->sock, handle);
3681 pthread_mutex_unlock(&app->sock_lock);
3682 if (ret < 0 && ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
3683 ERR("Error releasing app handle for app %d with ret %d", app->sock, ret);
3684 }
3685 }
3686
3687 ret = count;
3688 *events = tmp_event;
3689
3690 DBG2("UST app list events done (%zu events)", count);
3691
3692 rcu_error:
3693 rcu_read_unlock();
3694 error:
3695 health_code_update();
3696 return ret;
3697 }
3698
3699 /*
3700 * Fill events array with all events name of all registered apps.
3701 */
3702 int ust_app_list_event_fields(struct lttng_event_field **fields)
3703 {
3704 int ret, handle;
3705 size_t nbmem, count = 0;
3706 struct lttng_ht_iter iter;
3707 struct ust_app *app;
3708 struct lttng_event_field *tmp_event;
3709
3710 nbmem = UST_APP_EVENT_LIST_SIZE;
3711 tmp_event = zmalloc(nbmem * sizeof(struct lttng_event_field));
3712 if (tmp_event == NULL) {
3713 PERROR("zmalloc ust app event fields");
3714 ret = -ENOMEM;
3715 goto error;
3716 }
3717
3718 rcu_read_lock();
3719
3720 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3721 struct lttng_ust_field_iter uiter;
3722
3723 health_code_update();
3724
3725 if (!app->compatible) {
3726 /*
3727 * TODO: In time, we should notice the caller of this error by
3728 * telling him that this is a version error.
3729 */
3730 continue;
3731 }
3732 pthread_mutex_lock(&app->sock_lock);
3733 handle = ustctl_tracepoint_field_list(app->sock);
3734 if (handle < 0) {
3735 if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) {
3736 ERR("UST app list field getting handle failed for app pid %d",
3737 app->pid);
3738 }
3739 pthread_mutex_unlock(&app->sock_lock);
3740 continue;
3741 }
3742
3743 while ((ret = ustctl_tracepoint_field_list_get(app->sock, handle,
3744 &uiter)) != -LTTNG_UST_ERR_NOENT) {
3745 /* Handle ustctl error. */
3746 if (ret < 0) {
3747 int release_ret;
3748
3749 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
3750 ERR("UST app tp list field failed for app %d with ret %d",
3751 app->sock, ret);
3752 } else {
3753 DBG3("UST app tp list field failed. Application is dead");
3754 /*
3755 * This is normal behavior, an application can die during the
3756 * creation process. Don't report an error so the execution can
3757 * continue normally. Reset list and count for next app.
3758 */
3759 break;
3760 }
3761 free(tmp_event);
3762 release_ret = ustctl_release_handle(app->sock, handle);
3763 pthread_mutex_unlock(&app->sock_lock);
3764 if (release_ret < 0 &&
3765 release_ret != -LTTNG_UST_ERR_EXITING &&
3766 release_ret != -EPIPE) {
3767 ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret);
3768 }
3769 goto rcu_error;
3770 }
3771
3772 health_code_update();
3773 if (count >= nbmem) {
3774 /* In case the realloc fails, we free the memory */
3775 struct lttng_event_field *new_tmp_event;
3776 size_t new_nbmem;
3777
3778 new_nbmem = nbmem << 1;
3779 DBG2("Reallocating event field list from %zu to %zu entries",
3780 nbmem, new_nbmem);
3781 new_tmp_event = realloc(tmp_event,
3782 new_nbmem * sizeof(struct lttng_event_field));
3783 if (new_tmp_event == NULL) {
3784 int release_ret;
3785
3786 PERROR("realloc ust app event fields");
3787 free(tmp_event);
3788 ret = -ENOMEM;
3789 release_ret = ustctl_release_handle(app->sock, handle);
3790 pthread_mutex_unlock(&app->sock_lock);
3791 if (release_ret &&
3792 release_ret != -LTTNG_UST_ERR_EXITING &&
3793 release_ret != -EPIPE) {
3794 ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret);
3795 }
3796 goto rcu_error;
3797 }
3798 /* Zero the new memory */
3799 memset(new_tmp_event + nbmem, 0,
3800 (new_nbmem - nbmem) * sizeof(struct lttng_event_field));
3801 nbmem = new_nbmem;
3802 tmp_event = new_tmp_event;
3803 }
3804
3805 memcpy(tmp_event[count].field_name, uiter.field_name, LTTNG_UST_SYM_NAME_LEN);
3806 /* Mapping between these enums matches 1 to 1. */
3807 tmp_event[count].type = (enum lttng_event_field_type) uiter.type;
3808 tmp_event[count].nowrite = uiter.nowrite;
3809
3810 memcpy(tmp_event[count].event.name, uiter.event_name, LTTNG_UST_SYM_NAME_LEN);
3811 tmp_event[count].event.loglevel = uiter.loglevel;
3812 tmp_event[count].event.type = LTTNG_EVENT_TRACEPOINT;
3813 tmp_event[count].event.pid = app->pid;
3814 tmp_event[count].event.enabled = -1;
3815 count++;
3816 }
3817 ret = ustctl_release_handle(app->sock, handle);
3818 pthread_mutex_unlock(&app->sock_lock);
3819 if (ret < 0 &&
3820 ret != -LTTNG_UST_ERR_EXITING &&
3821 ret != -EPIPE) {
3822 ERR("Error releasing app handle for app %d with ret %d", app->sock, ret);
3823 }
3824 }
3825
3826 ret = count;
3827 *fields = tmp_event;
3828
3829 DBG2("UST app list event fields done (%zu events)", count);
3830
3831 rcu_error:
3832 rcu_read_unlock();
3833 error:
3834 health_code_update();
3835 return ret;
3836 }
3837
3838 /*
3839 * Free and clean all traceable apps of the global list.
3840 *
3841 * Should _NOT_ be called with RCU read-side lock held.
3842 */
3843 void ust_app_clean_list(void)
3844 {
3845 int ret;
3846 struct ust_app *app;
3847 struct lttng_ht_iter iter;
3848
3849 DBG2("UST app cleaning registered apps hash table");
3850
3851 rcu_read_lock();
3852
3853 /* Cleanup notify socket hash table */
3854 if (ust_app_ht_by_notify_sock) {
3855 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock->ht, &iter.iter, app,
3856 notify_sock_n.node) {
3857 struct cds_lfht_node *node;
3858 struct ust_app *app;
3859
3860 node = cds_lfht_iter_get_node(&iter.iter);
3861 if (!node) {
3862 continue;
3863 }
3864
3865 app = container_of(node, struct ust_app,
3866 notify_sock_n.node);
3867 ust_app_notify_sock_unregister(app->notify_sock);
3868 }
3869 }
3870
3871 if (ust_app_ht) {
3872 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3873 ret = lttng_ht_del(ust_app_ht, &iter);
3874 assert(!ret);
3875 call_rcu(&app->pid_n.head, delete_ust_app_rcu);
3876 }
3877 }
3878
3879 /* Cleanup socket hash table */
3880 if (ust_app_ht_by_sock) {
3881 cds_lfht_for_each_entry(ust_app_ht_by_sock->ht, &iter.iter, app,
3882 sock_n.node) {
3883 ret = lttng_ht_del(ust_app_ht_by_sock, &iter);
3884 assert(!ret);
3885 }
3886 }
3887
3888 rcu_read_unlock();
3889
3890 /* Destroy is done only when the ht is empty */
3891 if (ust_app_ht) {
3892 ht_cleanup_push(ust_app_ht);
3893 }
3894 if (ust_app_ht_by_sock) {
3895 ht_cleanup_push(ust_app_ht_by_sock);
3896 }
3897 if (ust_app_ht_by_notify_sock) {
3898 ht_cleanup_push(ust_app_ht_by_notify_sock);
3899 }
3900 }
3901
3902 /*
3903 * Init UST app hash table.
3904 */
3905 int ust_app_ht_alloc(void)
3906 {
3907 ust_app_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
3908 if (!ust_app_ht) {
3909 return -1;
3910 }
3911 ust_app_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
3912 if (!ust_app_ht_by_sock) {
3913 return -1;
3914 }
3915 ust_app_ht_by_notify_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
3916 if (!ust_app_ht_by_notify_sock) {
3917 return -1;
3918 }
3919 return 0;
3920 }
3921
3922 /*
3923 * For a specific UST session, disable the channel for all registered apps.
3924 */
3925 int ust_app_disable_channel_glb(struct ltt_ust_session *usess,
3926 struct ltt_ust_channel *uchan)
3927 {
3928 int ret = 0;
3929 struct lttng_ht_iter iter;
3930 struct lttng_ht_node_str *ua_chan_node;
3931 struct ust_app *app;
3932 struct ust_app_session *ua_sess;
3933 struct ust_app_channel *ua_chan;
3934
3935 assert(usess->active);
3936 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64,
3937 uchan->name, usess->id);
3938
3939 rcu_read_lock();
3940
3941 /* For every registered applications */
3942 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3943 struct lttng_ht_iter uiter;
3944 if (!app->compatible) {
3945 /*
3946 * TODO: In time, we should notice the caller of this error by
3947 * telling him that this is a version error.
3948 */
3949 continue;
3950 }
3951 ua_sess = lookup_session_by_app(usess, app);
3952 if (ua_sess == NULL) {
3953 continue;
3954 }
3955
3956 /* Get channel */
3957 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
3958 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
3959 /* If the session if found for the app, the channel must be there */
3960 assert(ua_chan_node);
3961
3962 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
3963 /* The channel must not be already disabled */
3964 assert(ua_chan->enabled == 1);
3965
3966 /* Disable channel onto application */
3967 ret = disable_ust_app_channel(ua_sess, ua_chan, app);
3968 if (ret < 0) {
3969 /* XXX: We might want to report this error at some point... */
3970 continue;
3971 }
3972 }
3973
3974 rcu_read_unlock();
3975 return ret;
3976 }
3977
3978 /*
3979 * For a specific UST session, enable the channel for all registered apps.
3980 */
3981 int ust_app_enable_channel_glb(struct ltt_ust_session *usess,
3982 struct ltt_ust_channel *uchan)
3983 {
3984 int ret = 0;
3985 struct lttng_ht_iter iter;
3986 struct ust_app *app;
3987 struct ust_app_session *ua_sess;
3988
3989 assert(usess->active);
3990 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64,
3991 uchan->name, usess->id);
3992
3993 rcu_read_lock();
3994
3995 /* For every registered applications */
3996 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3997 if (!app->compatible) {
3998 /*
3999 * TODO: In time, we should notice the caller of this error by
4000 * telling him that this is a version error.
4001 */
4002 continue;
4003 }
4004 ua_sess = lookup_session_by_app(usess, app);
4005 if (ua_sess == NULL) {
4006 continue;
4007 }
4008
4009 /* Enable channel onto application */
4010 ret = enable_ust_app_channel(ua_sess, uchan, app);
4011 if (ret < 0) {
4012 /* XXX: We might want to report this error at some point... */
4013 continue;
4014 }
4015 }
4016
4017 rcu_read_unlock();
4018 return ret;
4019 }
4020
4021 /*
4022 * Disable an event in a channel and for a specific session.
4023 */
4024 int ust_app_disable_event_glb(struct ltt_ust_session *usess,
4025 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
4026 {
4027 int ret = 0;
4028 struct lttng_ht_iter iter, uiter;
4029 struct lttng_ht_node_str *ua_chan_node;
4030 struct ust_app *app;
4031 struct ust_app_session *ua_sess;
4032 struct ust_app_channel *ua_chan;
4033 struct ust_app_event *ua_event;
4034
4035 assert(usess->active);
4036 DBG("UST app disabling event %s for all apps in channel "
4037 "%s for session id %" PRIu64,
4038 uevent->attr.name, uchan->name, usess->id);
4039
4040 rcu_read_lock();
4041
4042 /* For all registered applications */
4043 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4044 if (!app->compatible) {
4045 /*
4046 * TODO: In time, we should notice the caller of this error by
4047 * telling him that this is a version error.
4048 */
4049 continue;
4050 }
4051 ua_sess = lookup_session_by_app(usess, app);
4052 if (ua_sess == NULL) {
4053 /* Next app */
4054 continue;
4055 }
4056
4057 /* Lookup channel in the ust app session */
4058 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
4059 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
4060 if (ua_chan_node == NULL) {
4061 DBG2("Channel %s not found in session id %" PRIu64 " for app pid %d."
4062 "Skipping", uchan->name, usess->id, app->pid);
4063 continue;
4064 }
4065 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
4066
4067 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
4068 uevent->filter, uevent->attr.loglevel,
4069 uevent->exclusion);
4070 if (ua_event == NULL) {
4071 DBG2("Event %s not found in channel %s for app pid %d."
4072 "Skipping", uevent->attr.name, uchan->name, app->pid);
4073 continue;
4074 }
4075
4076 ret = disable_ust_app_event(ua_sess, ua_event, app);
4077 if (ret < 0) {
4078 /* XXX: Report error someday... */
4079 continue;
4080 }
4081 }
4082
4083 rcu_read_unlock();
4084 return ret;
4085 }
4086
4087 /* The ua_sess lock must be held by the caller. */
4088 static
4089 int ust_app_channel_create(struct ltt_ust_session *usess,
4090 struct ust_app_session *ua_sess,
4091 struct ltt_ust_channel *uchan, struct ust_app *app,
4092 struct ust_app_channel **_ua_chan)
4093 {
4094 int ret = 0;
4095 struct ust_app_channel *ua_chan = NULL;
4096
4097 assert(ua_sess);
4098 ASSERT_LOCKED(ua_sess->lock);
4099
4100 if (!strncmp(uchan->name, DEFAULT_METADATA_NAME,
4101 sizeof(uchan->name))) {
4102 copy_channel_attr_to_ustctl(&ua_sess->metadata_attr,
4103 &uchan->attr);
4104 ret = 0;
4105 } else {
4106 struct ltt_ust_context *uctx = NULL;
4107
4108 /*
4109 * Create channel onto application and synchronize its
4110 * configuration.
4111 */
4112 ret = ust_app_channel_allocate(ua_sess, uchan,
4113 LTTNG_UST_CHAN_PER_CPU, usess,
4114 &ua_chan);
4115 if (ret < 0) {
4116 goto error;
4117 }
4118
4119 ret = ust_app_channel_send(app, usess,
4120 ua_sess, ua_chan);
4121 if (ret) {
4122 goto error;
4123 }
4124
4125 /* Add contexts. */
4126 cds_list_for_each_entry(uctx, &uchan->ctx_list, list) {
4127 ret = create_ust_app_channel_context(ua_chan,
4128 &uctx->ctx, app);
4129 if (ret) {
4130 goto error;
4131 }
4132 }
4133 }
4134
4135 error:
4136 if (ret < 0) {
4137 switch (ret) {
4138 case -ENOTCONN:
4139 /*
4140 * The application's socket is not valid. Either a bad socket
4141 * or a timeout on it. We can't inform the caller that for a
4142 * specific app, the session failed so lets continue here.
4143 */
4144 ret = 0; /* Not an error. */
4145 break;
4146 case -ENOMEM:
4147 default:
4148 break;
4149 }
4150 }
4151
4152 if (ret == 0 && _ua_chan) {
4153 /*
4154 * Only return the application's channel on success. Note
4155 * that the channel can still be part of the application's
4156 * channel hashtable on error.
4157 */
4158 *_ua_chan = ua_chan;
4159 }
4160 return ret;
4161 }
4162
4163 /*
4164 * Enable event for a specific session and channel on the tracer.
4165 */
4166 int ust_app_enable_event_glb(struct ltt_ust_session *usess,
4167 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
4168 {
4169 int ret = 0;
4170 struct lttng_ht_iter iter, uiter;
4171 struct lttng_ht_node_str *ua_chan_node;
4172 struct ust_app *app;
4173 struct ust_app_session *ua_sess;
4174 struct ust_app_channel *ua_chan;
4175 struct ust_app_event *ua_event;
4176
4177 assert(usess->active);
4178 DBG("UST app enabling event %s for all apps for session id %" PRIu64,
4179 uevent->attr.name, usess->id);
4180
4181 /*
4182 * NOTE: At this point, this function is called only if the session and
4183 * channel passed are already created for all apps. and enabled on the
4184 * tracer also.
4185 */
4186
4187 rcu_read_lock();
4188
4189 /* For all registered applications */
4190 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4191 if (!app->compatible) {
4192 /*
4193 * TODO: In time, we should notice the caller of this error by
4194 * telling him that this is a version error.
4195 */
4196 continue;
4197 }
4198 ua_sess = lookup_session_by_app(usess, app);
4199 if (!ua_sess) {
4200 /* The application has problem or is probably dead. */
4201 continue;
4202 }
4203
4204 pthread_mutex_lock(&ua_sess->lock);
4205
4206 if (ua_sess->deleted) {
4207 pthread_mutex_unlock(&ua_sess->lock);
4208 continue;
4209 }
4210
4211 /* Lookup channel in the ust app session */
4212 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
4213 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
4214 /*
4215 * It is possible that the channel cannot be found is
4216 * the channel/event creation occurs concurrently with
4217 * an application exit.
4218 */
4219 if (!ua_chan_node) {
4220 pthread_mutex_unlock(&ua_sess->lock);
4221 continue;
4222 }
4223
4224 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
4225
4226 /* Get event node */
4227 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
4228 uevent->filter, uevent->attr.loglevel, uevent->exclusion);
4229 if (ua_event == NULL) {
4230 DBG3("UST app enable event %s not found for app PID %d."
4231 "Skipping app", uevent->attr.name, app->pid);
4232 goto next_app;
4233 }
4234
4235 ret = enable_ust_app_event(ua_sess, ua_event, app);
4236 if (ret < 0) {
4237 pthread_mutex_unlock(&ua_sess->lock);
4238 goto error;
4239 }
4240 next_app:
4241 pthread_mutex_unlock(&ua_sess->lock);
4242 }
4243
4244 error:
4245 rcu_read_unlock();
4246 return ret;
4247 }
4248
4249 /*
4250 * For a specific existing UST session and UST channel, creates the event for
4251 * all registered apps.
4252 */
4253 int ust_app_create_event_glb(struct ltt_ust_session *usess,
4254 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
4255 {
4256 int ret = 0;
4257 struct lttng_ht_iter iter, uiter;
4258 struct lttng_ht_node_str *ua_chan_node;
4259 struct ust_app *app;
4260 struct ust_app_session *ua_sess;
4261 struct ust_app_channel *ua_chan;
4262
4263 assert(usess->active);
4264 DBG("UST app creating event %s for all apps for session id %" PRIu64,
4265 uevent->attr.name, usess->id);
4266
4267 rcu_read_lock();
4268
4269 /* For all registered applications */
4270 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4271 if (!app->compatible) {
4272 /*
4273 * TODO: In time, we should notice the caller of this error by
4274 * telling him that this is a version error.
4275 */
4276 continue;
4277 }
4278 ua_sess = lookup_session_by_app(usess, app);
4279 if (!ua_sess) {
4280 /* The application has problem or is probably dead. */
4281 continue;
4282 }
4283
4284 pthread_mutex_lock(&ua_sess->lock);
4285
4286 if (ua_sess->deleted) {
4287 pthread_mutex_unlock(&ua_sess->lock);
4288 continue;
4289 }
4290
4291 /* Lookup channel in the ust app session */
4292 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
4293 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
4294 /* If the channel is not found, there is a code flow error */
4295 assert(ua_chan_node);
4296
4297 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
4298
4299 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
4300 pthread_mutex_unlock(&ua_sess->lock);
4301 if (ret < 0) {
4302 if (ret != -LTTNG_UST_ERR_EXIST) {
4303 /* Possible value at this point: -ENOMEM. If so, we stop! */
4304 break;
4305 }
4306 DBG2("UST app event %s already exist on app PID %d",
4307 uevent->attr.name, app->pid);
4308 continue;
4309 }
4310 }
4311
4312 rcu_read_unlock();
4313 return ret;
4314 }
4315
4316 /*
4317 * Start tracing for a specific UST session and app.
4318 *
4319 * Called with UST app session lock held.
4320 *
4321 */
4322 static
4323 int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
4324 {
4325 int ret = 0;
4326 struct ust_app_session *ua_sess;
4327
4328 DBG("Starting tracing for ust app pid %d", app->pid);
4329
4330 rcu_read_lock();
4331
4332 if (!app->compatible) {
4333 goto end;
4334 }
4335
4336 ua_sess = lookup_session_by_app(usess, app);
4337 if (ua_sess == NULL) {
4338 /* The session is in teardown process. Ignore and continue. */
4339 goto end;
4340 }
4341
4342 pthread_mutex_lock(&ua_sess->lock);
4343
4344 if (ua_sess->deleted) {
4345 pthread_mutex_unlock(&ua_sess->lock);
4346 goto end;
4347 }
4348
4349 if (ua_sess->enabled) {
4350 pthread_mutex_unlock(&ua_sess->lock);
4351 goto end;
4352 }
4353
4354 /* Upon restart, we skip the setup, already done */
4355 if (ua_sess->started) {
4356 goto skip_setup;
4357 }
4358
4359 /*
4360 * Create the metadata for the application. This returns gracefully if a
4361 * metadata was already set for the session.
4362 */
4363 ret = create_ust_app_metadata(ua_sess, app, usess->consumer);
4364 if (ret < 0) {
4365 goto error_unlock;
4366 }
4367
4368 health_code_update();
4369
4370 skip_setup:
4371 /* This starts the UST tracing */
4372 pthread_mutex_lock(&app->sock_lock);
4373 ret = ustctl_start_session(app->sock, ua_sess->handle);
4374 pthread_mutex_unlock(&app->sock_lock);
4375 if (ret < 0) {
4376 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
4377 ERR("Error starting tracing for app pid: %d (ret: %d)",
4378 app->pid, ret);
4379 } else {
4380 DBG("UST app start session failed. Application is dead.");
4381 /*
4382 * This is normal behavior, an application can die during the
4383 * creation process. Don't report an error so the execution can
4384 * continue normally.
4385 */
4386 pthread_mutex_unlock(&ua_sess->lock);
4387 goto end;
4388 }
4389 goto error_unlock;
4390 }
4391
4392 /* Indicate that the session has been started once */
4393 ua_sess->started = 1;
4394 ua_sess->enabled = 1;
4395
4396 pthread_mutex_unlock(&ua_sess->lock);
4397
4398 health_code_update();
4399
4400 /* Quiescent wait after starting trace */
4401 pthread_mutex_lock(&app->sock_lock);
4402 ret = ustctl_wait_quiescent(app->sock);
4403 pthread_mutex_unlock(&app->sock_lock);
4404 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
4405 ERR("UST app wait quiescent failed for app pid %d ret %d",
4406 app->pid, ret);
4407 }
4408
4409 end:
4410 rcu_read_unlock();
4411 health_code_update();
4412 return 0;
4413
4414 error_unlock:
4415 pthread_mutex_unlock(&ua_sess->lock);
4416 rcu_read_unlock();
4417 health_code_update();
4418 return -1;
4419 }
4420
4421 /*
4422 * Stop tracing for a specific UST session and app.
4423 */
4424 static
4425 int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app)
4426 {
4427 int ret = 0;
4428 struct ust_app_session *ua_sess;
4429 struct ust_registry_session *registry;
4430
4431 DBG("Stopping tracing for ust app pid %d", app->pid);
4432
4433 rcu_read_lock();
4434
4435 if (!app->compatible) {
4436 goto end_no_session;
4437 }
4438
4439 ua_sess = lookup_session_by_app(usess, app);
4440 if (ua_sess == NULL) {
4441 goto end_no_session;
4442 }
4443
4444 pthread_mutex_lock(&ua_sess->lock);
4445
4446 if (ua_sess->deleted) {
4447 pthread_mutex_unlock(&ua_sess->lock);
4448 goto end_no_session;
4449 }
4450
4451 /*
4452 * If started = 0, it means that stop trace has been called for a session
4453 * that was never started. It's possible since we can have a fail start
4454 * from either the application manager thread or the command thread. Simply
4455 * indicate that this is a stop error.
4456 */
4457 if (!ua_sess->started) {
4458 goto error_rcu_unlock;
4459 }
4460
4461 health_code_update();
4462
4463 /* This inhibits UST tracing */
4464 pthread_mutex_lock(&app->sock_lock);
4465 ret = ustctl_stop_session(app->sock, ua_sess->handle);
4466 pthread_mutex_unlock(&app->sock_lock);
4467 if (ret < 0) {
4468 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
4469 ERR("Error stopping tracing for app pid: %d (ret: %d)",
4470 app->pid, ret);
4471 } else {
4472 DBG("UST app stop session failed. Application is dead.");
4473 /*
4474 * This is normal behavior, an application can die during the
4475 * creation process. Don't report an error so the execution can
4476 * continue normally.
4477 */
4478 goto end_unlock;
4479 }
4480 goto error_rcu_unlock;
4481 }
4482
4483 health_code_update();
4484 ua_sess->enabled = 0;
4485
4486 /* Quiescent wait after stopping trace */
4487 pthread_mutex_lock(&app->sock_lock);
4488 ret = ustctl_wait_quiescent(app->sock);
4489 pthread_mutex_unlock(&app->sock_lock);
4490 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
4491 ERR("UST app wait quiescent failed for app pid %d ret %d",
4492 app->pid, ret);
4493 }
4494
4495 health_code_update();
4496
4497 registry = get_session_registry(ua_sess);
4498
4499 /* The UST app session is held registry shall not be null. */
4500 assert(registry);
4501
4502 /* Push metadata for application before freeing the application. */
4503 (void) push_metadata(registry, ua_sess->consumer);
4504
4505 end_unlock:
4506 pthread_mutex_unlock(&ua_sess->lock);
4507 end_no_session:
4508 rcu_read_unlock();
4509 health_code_update();
4510 return 0;
4511
4512 error_rcu_unlock:
4513 pthread_mutex_unlock(&ua_sess->lock);
4514 rcu_read_unlock();
4515 health_code_update();
4516 return -1;
4517 }
4518
4519 static
4520 int ust_app_flush_app_session(struct ust_app *app,
4521 struct ust_app_session *ua_sess)
4522 {
4523 int ret, retval = 0;
4524 struct lttng_ht_iter iter;
4525 struct ust_app_channel *ua_chan;
4526 struct consumer_socket *socket;
4527
4528 DBG("Flushing app session buffers for ust app pid %d", app->pid);
4529
4530 rcu_read_lock();
4531
4532 if (!app->compatible) {
4533 goto end_not_compatible;
4534 }
4535
4536 pthread_mutex_lock(&ua_sess->lock);
4537
4538 if (ua_sess->deleted) {
4539 goto end_deleted;
4540 }
4541
4542 health_code_update();
4543
4544 /* Flushing buffers */
4545 socket = consumer_find_socket_by_bitness(app->bits_per_long,
4546 ua_sess->consumer);
4547
4548 /* Flush buffers and push metadata. */
4549 switch (ua_sess->buffer_type) {
4550 case LTTNG_BUFFER_PER_PID:
4551 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
4552 node.node) {
4553 health_code_update();
4554 ret = consumer_flush_channel(socket, ua_chan->key);
4555 if (ret) {
4556 ERR("Error flushing consumer channel");
4557 retval = -1;
4558 continue;
4559 }
4560 }
4561 break;
4562 case LTTNG_BUFFER_PER_UID:
4563 default:
4564 assert(0);
4565 break;
4566 }
4567
4568 health_code_update();
4569
4570 end_deleted:
4571 pthread_mutex_unlock(&ua_sess->lock);
4572
4573 end_not_compatible:
4574 rcu_read_unlock();
4575 health_code_update();
4576 return retval;
4577 }
4578
4579 /*
4580 * Flush buffers for all applications for a specific UST session.
4581 * Called with UST session lock held.
4582 */
4583 static
4584 int ust_app_flush_session(struct ltt_ust_session *usess)
4585
4586 {
4587 int ret = 0;
4588
4589 DBG("Flushing session buffers for all ust apps");
4590
4591 rcu_read_lock();
4592
4593 /* Flush buffers and push metadata. */
4594 switch (usess->buffer_type) {
4595 case LTTNG_BUFFER_PER_UID:
4596 {
4597 struct buffer_reg_uid *reg;
4598 struct lttng_ht_iter iter;
4599
4600 /* Flush all per UID buffers associated to that session. */
4601 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
4602 struct ust_registry_session *ust_session_reg;
4603 struct buffer_reg_channel *reg_chan;
4604 struct consumer_socket *socket;
4605
4606 /* Get consumer socket to use to push the metadata.*/
4607 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
4608 usess->consumer);
4609 if (!socket) {
4610 /* Ignore request if no consumer is found for the session. */
4611 continue;
4612 }
4613
4614 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
4615 reg_chan, node.node) {
4616 /*
4617 * The following call will print error values so the return
4618 * code is of little importance because whatever happens, we
4619 * have to try them all.
4620 */
4621 (void) consumer_flush_channel(socket, reg_chan->consumer_key);
4622 }
4623
4624 ust_session_reg = reg->registry->reg.ust;
4625 /* Push metadata. */
4626 (void) push_metadata(ust_session_reg, usess->consumer);
4627 }
4628 break;
4629 }
4630 case LTTNG_BUFFER_PER_PID:
4631 {
4632 struct ust_app_session *ua_sess;
4633 struct lttng_ht_iter iter;
4634 struct ust_app *app;
4635
4636 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4637 ua_sess = lookup_session_by_app(usess, app);
4638 if (ua_sess == NULL) {
4639 continue;
4640 }
4641 (void) ust_app_flush_app_session(app, ua_sess);
4642 }
4643 break;
4644 }
4645 default:
4646 ret = -1;
4647 assert(0);
4648 break;
4649 }
4650
4651 rcu_read_unlock();
4652 health_code_update();
4653 return ret;
4654 }
4655
4656 static
4657 int ust_app_clear_quiescent_app_session(struct ust_app *app,
4658 struct ust_app_session *ua_sess)
4659 {
4660 int ret = 0;
4661 struct lttng_ht_iter iter;
4662 struct ust_app_channel *ua_chan;
4663 struct consumer_socket *socket;
4664
4665 DBG("Clearing stream quiescent state for ust app pid %d", app->pid);
4666
4667 rcu_read_lock();
4668
4669 if (!app->compatible) {
4670 goto end_not_compatible;
4671 }
4672
4673 pthread_mutex_lock(&ua_sess->lock);
4674
4675 if (ua_sess->deleted) {
4676 goto end_unlock;
4677 }
4678
4679 health_code_update();
4680
4681 socket = consumer_find_socket_by_bitness(app->bits_per_long,
4682 ua_sess->consumer);
4683 if (!socket) {
4684 ERR("Failed to find consumer (%" PRIu32 ") socket",
4685 app->bits_per_long);
4686 ret = -1;
4687 goto end_unlock;
4688 }
4689
4690 /* Clear quiescent state. */
4691 switch (ua_sess->buffer_type) {
4692 case LTTNG_BUFFER_PER_PID:
4693 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter,
4694 ua_chan, node.node) {
4695 health_code_update();
4696 ret = consumer_clear_quiescent_channel(socket,
4697 ua_chan->key);
4698 if (ret) {
4699 ERR("Error clearing quiescent state for consumer channel");
4700 ret = -1;
4701 continue;
4702 }
4703 }
4704 break;
4705 case LTTNG_BUFFER_PER_UID:
4706 default:
4707 assert(0);
4708 ret = -1;
4709 break;
4710 }
4711
4712 health_code_update();
4713
4714 end_unlock:
4715 pthread_mutex_unlock(&ua_sess->lock);
4716
4717 end_not_compatible:
4718 rcu_read_unlock();
4719 health_code_update();
4720 return ret;
4721 }
4722
4723 /*
4724 * Clear quiescent state in each stream for all applications for a
4725 * specific UST session.
4726 * Called with UST session lock held.
4727 */
4728 static
4729 int ust_app_clear_quiescent_session(struct ltt_ust_session *usess)
4730
4731 {
4732 int ret = 0;
4733
4734 DBG("Clearing stream quiescent state for all ust apps");
4735
4736 rcu_read_lock();
4737
4738 switch (usess->buffer_type) {
4739 case LTTNG_BUFFER_PER_UID:
4740 {
4741 struct lttng_ht_iter iter;
4742 struct buffer_reg_uid *reg;
4743
4744 /*
4745 * Clear quiescent for all per UID buffers associated to
4746 * that session.
4747 */
4748 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
4749 struct consumer_socket *socket;
4750 struct buffer_reg_channel *reg_chan;
4751
4752 /* Get associated consumer socket.*/
4753 socket = consumer_find_socket_by_bitness(
4754 reg->bits_per_long, usess->consumer);
4755 if (!socket) {
4756 /*
4757 * Ignore request if no consumer is found for
4758 * the session.
4759 */
4760 continue;
4761 }
4762
4763 cds_lfht_for_each_entry(reg->registry->channels->ht,
4764 &iter.iter, reg_chan, node.node) {
4765 /*
4766 * The following call will print error values so
4767 * the return code is of little importance
4768 * because whatever happens, we have to try them
4769 * all.
4770 */
4771 (void) consumer_clear_quiescent_channel(socket,
4772 reg_chan->consumer_key);
4773 }
4774 }
4775 break;
4776 }
4777 case LTTNG_BUFFER_PER_PID:
4778 {
4779 struct ust_app_session *ua_sess;
4780 struct lttng_ht_iter iter;
4781 struct ust_app *app;
4782
4783 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app,
4784 pid_n.node) {
4785 ua_sess = lookup_session_by_app(usess, app);
4786 if (ua_sess == NULL) {
4787 continue;
4788 }
4789 (void) ust_app_clear_quiescent_app_session(app,
4790 ua_sess);
4791 }
4792 break;
4793 }
4794 default:
4795 ret = -1;
4796 assert(0);
4797 break;
4798 }
4799
4800 rcu_read_unlock();
4801 health_code_update();
4802 return ret;
4803 }
4804
4805 /*
4806 * Destroy a specific UST session in apps.
4807 */
4808 static int destroy_trace(struct ltt_ust_session *usess, struct ust_app *app)
4809 {
4810 int ret;
4811 struct ust_app_session *ua_sess;
4812 struct lttng_ht_iter iter;
4813 struct lttng_ht_node_u64 *node;
4814
4815 DBG("Destroy tracing for ust app pid %d", app->pid);
4816
4817 rcu_read_lock();
4818
4819 if (!app->compatible) {
4820 goto end;
4821 }
4822
4823 __lookup_session_by_app(usess, app, &iter);
4824 node = lttng_ht_iter_get_node_u64(&iter);
4825 if (node == NULL) {
4826 /* Session is being or is deleted. */
4827 goto end;
4828 }
4829 ua_sess = caa_container_of(node, struct ust_app_session, node);
4830
4831 health_code_update();
4832 destroy_app_session(app, ua_sess);
4833
4834 health_code_update();
4835
4836 /* Quiescent wait after stopping trace */
4837 pthread_mutex_lock(&app->sock_lock);
4838 ret = ustctl_wait_quiescent(app->sock);
4839 pthread_mutex_unlock(&app->sock_lock);
4840 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
4841 ERR("UST app wait quiescent failed for app pid %d ret %d",
4842 app->pid, ret);
4843 }
4844 end:
4845 rcu_read_unlock();
4846 health_code_update();
4847 return 0;
4848 }
4849
4850 /*
4851 * Start tracing for the UST session.
4852 */
4853 int ust_app_start_trace_all(struct ltt_ust_session *usess)
4854 {
4855 struct lttng_ht_iter iter;
4856 struct ust_app *app;
4857
4858 DBG("Starting all UST traces");
4859
4860 /*
4861 * Even though the start trace might fail, flag this session active so
4862 * other application coming in are started by default.
4863 */
4864 usess->active = 1;
4865
4866 rcu_read_lock();
4867
4868 /*
4869 * In a start-stop-start use-case, we need to clear the quiescent state
4870 * of each channel set by the prior stop command, thus ensuring that a
4871 * following stop or destroy is sure to grab a timestamp_end near those
4872 * operations, even if the packet is empty.
4873 */
4874 (void) ust_app_clear_quiescent_session(usess);
4875
4876 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4877 ust_app_global_update(usess, app);
4878 }
4879
4880 rcu_read_unlock();
4881
4882 return 0;
4883 }
4884
4885 /*
4886 * Start tracing for the UST session.
4887 * Called with UST session lock held.
4888 */
4889 int ust_app_stop_trace_all(struct ltt_ust_session *usess)
4890 {
4891 int ret = 0;
4892 struct lttng_ht_iter iter;
4893 struct ust_app *app;
4894
4895 DBG("Stopping all UST traces");
4896
4897 /*
4898 * Even though the stop trace might fail, flag this session inactive so
4899 * other application coming in are not started by default.
4900 */
4901 usess->active = 0;
4902
4903 rcu_read_lock();
4904
4905 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4906 ret = ust_app_stop_trace(usess, app);
4907 if (ret < 0) {
4908 /* Continue to next apps even on error */
4909 continue;
4910 }
4911 }
4912
4913 (void) ust_app_flush_session(usess);
4914
4915 rcu_read_unlock();
4916
4917 return 0;
4918 }
4919
4920 /*
4921 * Destroy app UST session.
4922 */
4923 int ust_app_destroy_trace_all(struct ltt_ust_session *usess)
4924 {
4925 int ret = 0;
4926 struct lttng_ht_iter iter;
4927 struct ust_app *app;
4928
4929 DBG("Destroy all UST traces");
4930
4931 rcu_read_lock();
4932
4933 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4934 ret = destroy_trace(usess, app);
4935 if (ret < 0) {
4936 /* Continue to next apps even on error */
4937 continue;
4938 }
4939 }
4940
4941 rcu_read_unlock();
4942
4943 return 0;
4944 }
4945
4946 /* The ua_sess lock must be held by the caller. */
4947 static
4948 int find_or_create_ust_app_channel(
4949 struct ltt_ust_session *usess,
4950 struct ust_app_session *ua_sess,
4951 struct ust_app *app,
4952 struct ltt_ust_channel *uchan,
4953 struct ust_app_channel **ua_chan)
4954 {
4955 int ret = 0;
4956 struct lttng_ht_iter iter;
4957 struct lttng_ht_node_str *ua_chan_node;
4958
4959 lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &iter);
4960 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
4961 if (ua_chan_node) {
4962 *ua_chan = caa_container_of(ua_chan_node,
4963 struct ust_app_channel, node);
4964 goto end;
4965 }
4966
4967 ret = ust_app_channel_create(usess, ua_sess, uchan, app, ua_chan);
4968 if (ret) {
4969 goto end;
4970 }
4971 end:
4972 return ret;
4973 }
4974
4975 static
4976 int ust_app_channel_synchronize_event(struct ust_app_channel *ua_chan,
4977 struct ltt_ust_event *uevent, struct ust_app_session *ua_sess,
4978 struct ust_app *app)
4979 {
4980 int ret = 0;
4981 struct ust_app_event *ua_event = NULL;
4982
4983 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
4984 uevent->filter, uevent->attr.loglevel, uevent->exclusion);
4985 if (!ua_event) {
4986 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
4987 if (ret < 0) {
4988 goto end;
4989 }
4990 } else {
4991 if (ua_event->enabled != uevent->enabled) {
4992 ret = uevent->enabled ?
4993 enable_ust_app_event(ua_sess, ua_event, app) :
4994 disable_ust_app_event(ua_sess, ua_event, app);
4995 }
4996 }
4997
4998 end:
4999 return ret;
5000 }
5001
5002 /*
5003 * The caller must ensure that the application is compatible and is tracked
5004 * by the process attribute trackers.
5005 */
5006 static
5007 void ust_app_synchronize(struct ltt_ust_session *usess,
5008 struct ust_app *app)
5009 {
5010 int ret = 0;
5011 struct cds_lfht_iter uchan_iter;
5012 struct ltt_ust_channel *uchan;
5013 struct ust_app_session *ua_sess = NULL;
5014
5015 /*
5016 * The application's configuration should only be synchronized for
5017 * active sessions.
5018 */
5019 assert(usess->active);
5020
5021 ret = find_or_create_ust_app_session(usess, app, &ua_sess, NULL);
5022 if (ret < 0) {
5023 /* Tracer is probably gone or ENOMEM. */
5024 goto error;
5025 }
5026 assert(ua_sess);
5027
5028 pthread_mutex_lock(&ua_sess->lock);
5029 if (ua_sess->deleted) {
5030 pthread_mutex_unlock(&ua_sess->lock);
5031 goto end;
5032 }
5033
5034 rcu_read_lock();
5035 cds_lfht_for_each_entry(usess->domain_global.channels->ht, &uchan_iter,
5036 uchan, node.node) {
5037 struct ust_app_channel *ua_chan;
5038 struct cds_lfht_iter uevent_iter;
5039 struct ltt_ust_event *uevent;
5040
5041 /*
5042 * Search for a matching ust_app_channel. If none is found,
5043 * create it. Creating the channel will cause the ua_chan
5044 * structure to be allocated, the channel buffers to be
5045 * allocated (if necessary) and sent to the application, and
5046 * all enabled contexts will be added to the channel.
5047 */
5048 ret = find_or_create_ust_app_channel(usess, ua_sess,
5049 app, uchan, &ua_chan);
5050 if (ret) {
5051 /* Tracer is probably gone or ENOMEM. */
5052 goto error_unlock;
5053 }
5054
5055 if (!ua_chan) {
5056 /* ua_chan will be NULL for the metadata channel */
5057 continue;
5058 }
5059
5060 cds_lfht_for_each_entry(uchan->events->ht, &uevent_iter, uevent,
5061 node.node) {
5062 ret = ust_app_channel_synchronize_event(ua_chan,
5063 uevent, ua_sess, app);
5064 if (ret) {
5065 goto error_unlock;
5066 }
5067 }
5068
5069 if (ua_chan->enabled != uchan->enabled) {
5070 ret = uchan->enabled ?
5071 enable_ust_app_channel(ua_sess, uchan, app) :
5072 disable_ust_app_channel(ua_sess, ua_chan, app);
5073 if (ret) {
5074 goto error_unlock;
5075 }
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 ua_sess->effective_credentials
5969 .uid,
5970 ua_sess->effective_credentials
5971 .gid,
5972 &trace_path[consumer_path_offset], wait,
5973 nb_packets_per_stream);
5974 switch (status) {
5975 case LTTNG_OK:
5976 break;
5977 case LTTNG_ERR_CHAN_NOT_FOUND:
5978 continue;
5979 default:
5980 goto error;
5981 }
5982 }
5983
5984 registry = get_session_registry(ua_sess);
5985 if (!registry) {
5986 DBG("Application session is being torn down. Skip application.");
5987 continue;
5988 }
5989 status = consumer_snapshot_channel(socket,
5990 registry->metadata_key, output, 1,
5991 ua_sess->effective_credentials.uid,
5992 ua_sess->effective_credentials.gid,
5993 &trace_path[consumer_path_offset], wait, 0);
5994 switch (status) {
5995 case LTTNG_OK:
5996 break;
5997 case LTTNG_ERR_CHAN_NOT_FOUND:
5998 continue;
5999 default:
6000 goto error;
6001 }
6002 }
6003 break;
6004 }
6005 default:
6006 assert(0);
6007 break;
6008 }
6009
6010 error:
6011 free(trace_path);
6012 rcu_read_unlock();
6013 return status;
6014 }
6015
6016 /*
6017 * Return the size taken by one more packet per stream.
6018 */
6019 uint64_t ust_app_get_size_one_more_packet_per_stream(
6020 const struct ltt_ust_session *usess, uint64_t cur_nr_packets)
6021 {
6022 uint64_t tot_size = 0;
6023 struct ust_app *app;
6024 struct lttng_ht_iter iter;
6025
6026 assert(usess);
6027
6028 switch (usess->buffer_type) {
6029 case LTTNG_BUFFER_PER_UID:
6030 {
6031 struct buffer_reg_uid *reg;
6032
6033 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
6034 struct buffer_reg_channel *reg_chan;
6035
6036 rcu_read_lock();
6037 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
6038 reg_chan, node.node) {
6039 if (cur_nr_packets >= reg_chan->num_subbuf) {
6040 /*
6041 * Don't take channel into account if we
6042 * already grab all its packets.
6043 */
6044 continue;
6045 }
6046 tot_size += reg_chan->subbuf_size * reg_chan->stream_count;
6047 }
6048 rcu_read_unlock();
6049 }
6050 break;
6051 }
6052 case LTTNG_BUFFER_PER_PID:
6053 {
6054 rcu_read_lock();
6055 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
6056 struct ust_app_channel *ua_chan;
6057 struct ust_app_session *ua_sess;
6058 struct lttng_ht_iter chan_iter;
6059
6060 ua_sess = lookup_session_by_app(usess, app);
6061 if (!ua_sess) {
6062 /* Session not associated with this app. */
6063 continue;
6064 }
6065
6066 cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter,
6067 ua_chan, node.node) {
6068 if (cur_nr_packets >= ua_chan->attr.num_subbuf) {
6069 /*
6070 * Don't take channel into account if we
6071 * already grab all its packets.
6072 */
6073 continue;
6074 }
6075 tot_size += ua_chan->attr.subbuf_size * ua_chan->streams.count;
6076 }
6077 }
6078 rcu_read_unlock();
6079 break;
6080 }
6081 default:
6082 assert(0);
6083 break;
6084 }
6085
6086 return tot_size;
6087 }
6088
6089 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id,
6090 struct cds_list_head *buffer_reg_uid_list,
6091 struct consumer_output *consumer, uint64_t uchan_id,
6092 int overwrite, uint64_t *discarded, uint64_t *lost)
6093 {
6094 int ret;
6095 uint64_t consumer_chan_key;
6096
6097 *discarded = 0;
6098 *lost = 0;
6099
6100 ret = buffer_reg_uid_consumer_channel_key(
6101 buffer_reg_uid_list, uchan_id, &consumer_chan_key);
6102 if (ret < 0) {
6103 /* Not found */
6104 ret = 0;
6105 goto end;
6106 }
6107
6108 if (overwrite) {
6109 ret = consumer_get_lost_packets(ust_session_id,
6110 consumer_chan_key, consumer, lost);
6111 } else {
6112 ret = consumer_get_discarded_events(ust_session_id,
6113 consumer_chan_key, consumer, discarded);
6114 }
6115
6116 end:
6117 return ret;
6118 }
6119
6120 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session *usess,
6121 struct ltt_ust_channel *uchan,
6122 struct consumer_output *consumer, int overwrite,
6123 uint64_t *discarded, uint64_t *lost)
6124 {
6125 int ret = 0;
6126 struct lttng_ht_iter iter;
6127 struct lttng_ht_node_str *ua_chan_node;
6128 struct ust_app *app;
6129 struct ust_app_session *ua_sess;
6130 struct ust_app_channel *ua_chan;
6131
6132 *discarded = 0;
6133 *lost = 0;
6134
6135 rcu_read_lock();
6136 /*
6137 * Iterate over every registered applications. Sum counters for
6138 * all applications containing requested session and channel.
6139 */
6140 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
6141 struct lttng_ht_iter uiter;
6142
6143 ua_sess = lookup_session_by_app(usess, app);
6144 if (ua_sess == NULL) {
6145 continue;
6146 }
6147
6148 /* Get channel */
6149 lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &uiter);
6150 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
6151 /* If the session is found for the app, the channel must be there */
6152 assert(ua_chan_node);
6153
6154 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
6155
6156 if (overwrite) {
6157 uint64_t _lost;
6158
6159 ret = consumer_get_lost_packets(usess->id, ua_chan->key,
6160 consumer, &_lost);
6161 if (ret < 0) {
6162 break;
6163 }
6164 (*lost) += _lost;
6165 } else {
6166 uint64_t _discarded;
6167
6168 ret = consumer_get_discarded_events(usess->id,
6169 ua_chan->key, consumer, &_discarded);
6170 if (ret < 0) {
6171 break;
6172 }
6173 (*discarded) += _discarded;
6174 }
6175 }
6176
6177 rcu_read_unlock();
6178 return ret;
6179 }
6180
6181 static
6182 int ust_app_regenerate_statedump(struct ltt_ust_session *usess,
6183 struct ust_app *app)
6184 {
6185 int ret = 0;
6186 struct ust_app_session *ua_sess;
6187
6188 DBG("Regenerating the metadata for ust app pid %d", app->pid);
6189
6190 rcu_read_lock();
6191
6192 ua_sess = lookup_session_by_app(usess, app);
6193 if (ua_sess == NULL) {
6194 /* The session is in teardown process. Ignore and continue. */
6195 goto end;
6196 }
6197
6198 pthread_mutex_lock(&ua_sess->lock);
6199
6200 if (ua_sess->deleted) {
6201 goto end_unlock;
6202 }
6203
6204 pthread_mutex_lock(&app->sock_lock);
6205 ret = ustctl_regenerate_statedump(app->sock, ua_sess->handle);
6206 pthread_mutex_unlock(&app->sock_lock);
6207
6208 end_unlock:
6209 pthread_mutex_unlock(&ua_sess->lock);
6210
6211 end:
6212 rcu_read_unlock();
6213 health_code_update();
6214 return ret;
6215 }
6216
6217 /*
6218 * Regenerate the statedump for each app in the session.
6219 */
6220 int ust_app_regenerate_statedump_all(struct ltt_ust_session *usess)
6221 {
6222 int ret = 0;
6223 struct lttng_ht_iter iter;
6224 struct ust_app *app;
6225
6226 DBG("Regenerating the metadata for all UST apps");
6227
6228 rcu_read_lock();
6229
6230 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
6231 if (!app->compatible) {
6232 continue;
6233 }
6234
6235 ret = ust_app_regenerate_statedump(usess, app);
6236 if (ret < 0) {
6237 /* Continue to the next app even on error */
6238 continue;
6239 }
6240 }
6241
6242 rcu_read_unlock();
6243
6244 return 0;
6245 }
6246
6247 /*
6248 * Rotate all the channels of a session.
6249 *
6250 * Return LTTNG_OK on success or else an LTTng error code.
6251 */
6252 enum lttng_error_code ust_app_rotate_session(struct ltt_session *session)
6253 {
6254 int ret;
6255 enum lttng_error_code cmd_ret = LTTNG_OK;
6256 struct lttng_ht_iter iter;
6257 struct ust_app *app;
6258 struct ltt_ust_session *usess = session->ust_session;
6259
6260 assert(usess);
6261
6262 rcu_read_lock();
6263
6264 switch (usess->buffer_type) {
6265 case LTTNG_BUFFER_PER_UID:
6266 {
6267 struct buffer_reg_uid *reg;
6268
6269 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
6270 struct buffer_reg_channel *reg_chan;
6271 struct consumer_socket *socket;
6272
6273 if (!reg->registry->reg.ust->metadata_key) {
6274 /* Skip since no metadata is present */
6275 continue;
6276 }
6277
6278 /* Get consumer socket to use to push the metadata.*/
6279 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
6280 usess->consumer);
6281 if (!socket) {
6282 cmd_ret = LTTNG_ERR_INVALID;
6283 goto error;
6284 }
6285
6286 /* Rotate the data channels. */
6287 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
6288 reg_chan, node.node) {
6289 ret = consumer_rotate_channel(socket,
6290 reg_chan->consumer_key,
6291 usess->uid, usess->gid,
6292 usess->consumer,
6293 /* is_metadata_channel */ false);
6294 if (ret < 0) {
6295 cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
6296 goto error;
6297 }
6298 }
6299
6300 (void) push_metadata(reg->registry->reg.ust, usess->consumer);
6301
6302 ret = consumer_rotate_channel(socket,
6303 reg->registry->reg.ust->metadata_key,
6304 usess->uid, usess->gid,
6305 usess->consumer,
6306 /* is_metadata_channel */ true);
6307 if (ret < 0) {
6308 cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
6309 goto error;
6310 }
6311 }
6312 break;
6313 }
6314 case LTTNG_BUFFER_PER_PID:
6315 {
6316 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
6317 struct consumer_socket *socket;
6318 struct lttng_ht_iter chan_iter;
6319 struct ust_app_channel *ua_chan;
6320 struct ust_app_session *ua_sess;
6321 struct ust_registry_session *registry;
6322
6323 ua_sess = lookup_session_by_app(usess, app);
6324 if (!ua_sess) {
6325 /* Session not associated with this app. */
6326 continue;
6327 }
6328
6329 /* Get the right consumer socket for the application. */
6330 socket = consumer_find_socket_by_bitness(app->bits_per_long,
6331 usess->consumer);
6332 if (!socket) {
6333 cmd_ret = LTTNG_ERR_INVALID;
6334 goto error;
6335 }
6336
6337 registry = get_session_registry(ua_sess);
6338 if (!registry) {
6339 DBG("Application session is being torn down. Skip application.");
6340 continue;
6341 }
6342
6343 /* Rotate the data channels. */
6344 cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter,
6345 ua_chan, node.node) {
6346 ret = consumer_rotate_channel(socket,
6347 ua_chan->key,
6348 ua_sess->effective_credentials
6349 .uid,
6350 ua_sess->effective_credentials
6351 .gid,
6352 ua_sess->consumer,
6353 /* is_metadata_channel */ false);
6354 if (ret < 0) {
6355 /* Per-PID buffer and application going away. */
6356 if (ret == -LTTNG_ERR_CHAN_NOT_FOUND)
6357 continue;
6358 cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
6359 goto error;
6360 }
6361 }
6362
6363 /* Rotate the metadata channel. */
6364 (void) push_metadata(registry, usess->consumer);
6365 ret = consumer_rotate_channel(socket,
6366 registry->metadata_key,
6367 ua_sess->effective_credentials.uid,
6368 ua_sess->effective_credentials.gid,
6369 ua_sess->consumer,
6370 /* is_metadata_channel */ true);
6371 if (ret < 0) {
6372 /* Per-PID buffer and application going away. */
6373 if (ret == -LTTNG_ERR_CHAN_NOT_FOUND)
6374 continue;
6375 cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
6376 goto error;
6377 }
6378 }
6379 break;
6380 }
6381 default:
6382 assert(0);
6383 break;
6384 }
6385
6386 cmd_ret = LTTNG_OK;
6387
6388 error:
6389 rcu_read_unlock();
6390 return cmd_ret;
6391 }
6392
6393 enum lttng_error_code ust_app_create_channel_subdirectories(
6394 const struct ltt_ust_session *usess)
6395 {
6396 enum lttng_error_code ret = LTTNG_OK;
6397 struct lttng_ht_iter iter;
6398 enum lttng_trace_chunk_status chunk_status;
6399 char *pathname_index;
6400 int fmt_ret;
6401
6402 assert(usess->current_trace_chunk);
6403 rcu_read_lock();
6404
6405 switch (usess->buffer_type) {
6406 case LTTNG_BUFFER_PER_UID:
6407 {
6408 struct buffer_reg_uid *reg;
6409
6410 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
6411 fmt_ret = asprintf(&pathname_index,
6412 DEFAULT_UST_TRACE_DIR "/" DEFAULT_UST_TRACE_UID_PATH "/" DEFAULT_INDEX_DIR,
6413 reg->uid, reg->bits_per_long);
6414 if (fmt_ret < 0) {
6415 ERR("Failed to format channel index directory");
6416 ret = LTTNG_ERR_CREATE_DIR_FAIL;
6417 goto error;
6418 }
6419
6420 /*
6421 * Create the index subdirectory which will take care
6422 * of implicitly creating the channel's path.
6423 */
6424 chunk_status = lttng_trace_chunk_create_subdirectory(
6425 usess->current_trace_chunk,
6426 pathname_index);
6427 free(pathname_index);
6428 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
6429 ret = LTTNG_ERR_CREATE_DIR_FAIL;
6430 goto error;
6431 }
6432 }
6433 break;
6434 }
6435 case LTTNG_BUFFER_PER_PID:
6436 {
6437 struct ust_app *app;
6438
6439 /*
6440 * Create the toplevel ust/ directory in case no apps are running.
6441 */
6442 chunk_status = lttng_trace_chunk_create_subdirectory(
6443 usess->current_trace_chunk,
6444 DEFAULT_UST_TRACE_DIR);
6445 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
6446 ret = LTTNG_ERR_CREATE_DIR_FAIL;
6447 goto error;
6448 }
6449
6450 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app,
6451 pid_n.node) {
6452 struct ust_app_session *ua_sess;
6453 struct ust_registry_session *registry;
6454
6455 ua_sess = lookup_session_by_app(usess, app);
6456 if (!ua_sess) {
6457 /* Session not associated with this app. */
6458 continue;
6459 }
6460
6461 registry = get_session_registry(ua_sess);
6462 if (!registry) {
6463 DBG("Application session is being torn down. Skip application.");
6464 continue;
6465 }
6466
6467 fmt_ret = asprintf(&pathname_index,
6468 DEFAULT_UST_TRACE_DIR "/%s/" DEFAULT_INDEX_DIR,
6469 ua_sess->path);
6470 if (fmt_ret < 0) {
6471 ERR("Failed to format channel index directory");
6472 ret = LTTNG_ERR_CREATE_DIR_FAIL;
6473 goto error;
6474 }
6475 /*
6476 * Create the index subdirectory which will take care
6477 * of implicitly creating the channel's path.
6478 */
6479 chunk_status = lttng_trace_chunk_create_subdirectory(
6480 usess->current_trace_chunk,
6481 pathname_index);
6482 free(pathname_index);
6483 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
6484 ret = LTTNG_ERR_CREATE_DIR_FAIL;
6485 goto error;
6486 }
6487 }
6488 break;
6489 }
6490 default:
6491 abort();
6492 }
6493
6494 ret = LTTNG_OK;
6495 error:
6496 rcu_read_unlock();
6497 return ret;
6498 }
6499
6500 /*
6501 * Clear all the channels of a session.
6502 *
6503 * Return LTTNG_OK on success or else an LTTng error code.
6504 */
6505 enum lttng_error_code ust_app_clear_session(struct ltt_session *session)
6506 {
6507 int ret;
6508 enum lttng_error_code cmd_ret = LTTNG_OK;
6509 struct lttng_ht_iter iter;
6510 struct ust_app *app;
6511 struct ltt_ust_session *usess = session->ust_session;
6512
6513 assert(usess);
6514
6515 rcu_read_lock();
6516
6517 if (usess->active) {
6518 ERR("Expecting inactive session %s (%" PRIu64 ")", session->name, session->id);
6519 cmd_ret = LTTNG_ERR_FATAL;
6520 goto end;
6521 }
6522
6523 switch (usess->buffer_type) {
6524 case LTTNG_BUFFER_PER_UID:
6525 {
6526 struct buffer_reg_uid *reg;
6527
6528 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
6529 struct buffer_reg_channel *reg_chan;
6530 struct consumer_socket *socket;
6531
6532 /* Get consumer socket to use to push the metadata.*/
6533 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
6534 usess->consumer);
6535 if (!socket) {
6536 cmd_ret = LTTNG_ERR_INVALID;
6537 goto error_socket;
6538 }
6539
6540 /* Clear the data channels. */
6541 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
6542 reg_chan, node.node) {
6543 ret = consumer_clear_channel(socket,
6544 reg_chan->consumer_key);
6545 if (ret < 0) {
6546 goto error;
6547 }
6548 }
6549
6550 (void) push_metadata(reg->registry->reg.ust, usess->consumer);
6551
6552 /*
6553 * Clear the metadata channel.
6554 * Metadata channel is not cleared per se but we still need to
6555 * perform a rotation operation on it behind the scene.
6556 */
6557 ret = consumer_clear_channel(socket,
6558 reg->registry->reg.ust->metadata_key);
6559 if (ret < 0) {
6560 goto error;
6561 }
6562 }
6563 break;
6564 }
6565 case LTTNG_BUFFER_PER_PID:
6566 {
6567 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
6568 struct consumer_socket *socket;
6569 struct lttng_ht_iter chan_iter;
6570 struct ust_app_channel *ua_chan;
6571 struct ust_app_session *ua_sess;
6572 struct ust_registry_session *registry;
6573
6574 ua_sess = lookup_session_by_app(usess, app);
6575 if (!ua_sess) {
6576 /* Session not associated with this app. */
6577 continue;
6578 }
6579
6580 /* Get the right consumer socket for the application. */
6581 socket = consumer_find_socket_by_bitness(app->bits_per_long,
6582 usess->consumer);
6583 if (!socket) {
6584 cmd_ret = LTTNG_ERR_INVALID;
6585 goto error_socket;
6586 }
6587
6588 registry = get_session_registry(ua_sess);
6589 if (!registry) {
6590 DBG("Application session is being torn down. Skip application.");
6591 continue;
6592 }
6593
6594 /* Clear the data channels. */
6595 cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter,
6596 ua_chan, node.node) {
6597 ret = consumer_clear_channel(socket, ua_chan->key);
6598 if (ret < 0) {
6599 /* Per-PID buffer and application going away. */
6600 if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) {
6601 continue;
6602 }
6603 goto error;
6604 }
6605 }
6606
6607 (void) push_metadata(registry, usess->consumer);
6608
6609 /*
6610 * Clear the metadata channel.
6611 * Metadata channel is not cleared per se but we still need to
6612 * perform rotation operation on it behind the scene.
6613 */
6614 ret = consumer_clear_channel(socket, registry->metadata_key);
6615 if (ret < 0) {
6616 /* Per-PID buffer and application going away. */
6617 if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) {
6618 continue;
6619 }
6620 goto error;
6621 }
6622 }
6623 break;
6624 }
6625 default:
6626 assert(0);
6627 break;
6628 }
6629
6630 cmd_ret = LTTNG_OK;
6631 goto end;
6632
6633 error:
6634 switch (-ret) {
6635 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED:
6636 cmd_ret = LTTNG_ERR_CLEAR_RELAY_DISALLOWED;
6637 break;
6638 default:
6639 cmd_ret = LTTNG_ERR_CLEAR_FAIL_CONSUMER;
6640 }
6641
6642 error_socket:
6643 end:
6644 rcu_read_unlock();
6645 return cmd_ret;
6646 }
6647
6648 /*
6649 * This function skips the metadata channel as the begin/end timestamps of a
6650 * metadata packet are useless.
6651 *
6652 * Moreover, opening a packet after a "clear" will cause problems for live
6653 * sessions as it will introduce padding that was not part of the first trace
6654 * chunk. The relay daemon expects the content of the metadata stream of
6655 * successive metadata trace chunks to be strict supersets of one another.
6656 *
6657 * For example, flushing a packet at the beginning of the metadata stream of
6658 * a trace chunk resulting from a "clear" session command will cause the
6659 * size of the metadata stream of the new trace chunk to not match the size of
6660 * the metadata stream of the original chunk. This will confuse the relay
6661 * daemon as the same "offset" in a metadata stream will no longer point
6662 * to the same content.
6663 */
6664 enum lttng_error_code ust_app_open_packets(struct ltt_session *session)
6665 {
6666 enum lttng_error_code ret = LTTNG_OK;
6667 struct lttng_ht_iter iter;
6668 struct ltt_ust_session *usess = session->ust_session;
6669
6670 assert(usess);
6671
6672 rcu_read_lock();
6673
6674 switch (usess->buffer_type) {
6675 case LTTNG_BUFFER_PER_UID:
6676 {
6677 struct buffer_reg_uid *reg;
6678
6679 cds_list_for_each_entry (
6680 reg, &usess->buffer_reg_uid_list, lnode) {
6681 struct buffer_reg_channel *reg_chan;
6682 struct consumer_socket *socket;
6683
6684 socket = consumer_find_socket_by_bitness(
6685 reg->bits_per_long, usess->consumer);
6686 if (!socket) {
6687 ret = LTTNG_ERR_FATAL;
6688 goto error;
6689 }
6690
6691 cds_lfht_for_each_entry(reg->registry->channels->ht,
6692 &iter.iter, reg_chan, node.node) {
6693 const int open_ret =
6694 consumer_open_channel_packets(
6695 socket,
6696 reg_chan->consumer_key);
6697
6698 if (open_ret < 0) {
6699 ret = LTTNG_ERR_UNK;
6700 goto error;
6701 }
6702 }
6703 }
6704 break;
6705 }
6706 case LTTNG_BUFFER_PER_PID:
6707 {
6708 struct ust_app *app;
6709
6710 cds_lfht_for_each_entry (
6711 ust_app_ht->ht, &iter.iter, app, pid_n.node) {
6712 struct consumer_socket *socket;
6713 struct lttng_ht_iter chan_iter;
6714 struct ust_app_channel *ua_chan;
6715 struct ust_app_session *ua_sess;
6716 struct ust_registry_session *registry;
6717
6718 ua_sess = lookup_session_by_app(usess, app);
6719 if (!ua_sess) {
6720 /* Session not associated with this app. */
6721 continue;
6722 }
6723
6724 /* Get the right consumer socket for the application. */
6725 socket = consumer_find_socket_by_bitness(
6726 app->bits_per_long, usess->consumer);
6727 if (!socket) {
6728 ret = LTTNG_ERR_FATAL;
6729 goto error;
6730 }
6731
6732 registry = get_session_registry(ua_sess);
6733 if (!registry) {
6734 DBG("Application session is being torn down. Skip application.");
6735 continue;
6736 }
6737
6738 cds_lfht_for_each_entry(ua_sess->channels->ht,
6739 &chan_iter.iter, ua_chan, node.node) {
6740 const int open_ret =
6741 consumer_open_channel_packets(
6742 socket,
6743 ua_chan->key);
6744
6745 if (open_ret < 0) {
6746 /*
6747 * Per-PID buffer and application going
6748 * away.
6749 */
6750 if (open_ret == -LTTNG_ERR_CHAN_NOT_FOUND) {
6751 continue;
6752 }
6753
6754 ret = LTTNG_ERR_UNK;
6755 goto error;
6756 }
6757 }
6758 }
6759 break;
6760 }
6761 default:
6762 abort();
6763 break;
6764 }
6765
6766 error:
6767 rcu_read_unlock();
6768 return ret;
6769 }
This page took 0.169393 seconds and 4 git commands to generate.