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