2dd69495efaf095b633247b4f24b849a8571076f
[lttng-tools.git] / src / bin / lttng-sessiond / ust-app.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #define _LGPL_SOURCE
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <pthread.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 #include <urcu/compiler.h>
30 #include <lttng/ust-error.h>
31 #include <signal.h>
32
33 #include <common/common.h>
34 #include <common/sessiond-comm/sessiond-comm.h>
35
36 #include "buffer-registry.h"
37 #include "fd-limit.h"
38 #include "health-sessiond.h"
39 #include "ust-app.h"
40 #include "ust-consumer.h"
41 #include "ust-ctl.h"
42 #include "utils.h"
43
44 static
45 int ust_app_flush_app_session(struct ust_app *app, struct ust_app_session *ua_sess);
46
47 /* Next available channel key. Access under next_channel_key_lock. */
48 static uint64_t _next_channel_key;
49 static pthread_mutex_t next_channel_key_lock = PTHREAD_MUTEX_INITIALIZER;
50
51 /* Next available session ID. Access under next_session_id_lock. */
52 static uint64_t _next_session_id;
53 static pthread_mutex_t next_session_id_lock = PTHREAD_MUTEX_INITIALIZER;
54
55 /*
56 * Return the incremented value of next_channel_key.
57 */
58 static uint64_t get_next_channel_key(void)
59 {
60 uint64_t ret;
61
62 pthread_mutex_lock(&next_channel_key_lock);
63 ret = ++_next_channel_key;
64 pthread_mutex_unlock(&next_channel_key_lock);
65 return ret;
66 }
67
68 /*
69 * Return the atomically incremented value of next_session_id.
70 */
71 static uint64_t get_next_session_id(void)
72 {
73 uint64_t ret;
74
75 pthread_mutex_lock(&next_session_id_lock);
76 ret = ++_next_session_id;
77 pthread_mutex_unlock(&next_session_id_lock);
78 return ret;
79 }
80
81 static void copy_channel_attr_to_ustctl(
82 struct ustctl_consumer_channel_attr *attr,
83 struct lttng_ust_channel_attr *uattr)
84 {
85 /* Copy event attributes since the layout is different. */
86 attr->subbuf_size = uattr->subbuf_size;
87 attr->num_subbuf = uattr->num_subbuf;
88 attr->overwrite = uattr->overwrite;
89 attr->switch_timer_interval = uattr->switch_timer_interval;
90 attr->read_timer_interval = uattr->read_timer_interval;
91 attr->output = uattr->output;
92 }
93
94 /*
95 * Match function for the hash table lookup.
96 *
97 * It matches an ust app event based on three attributes which are the event
98 * name, the filter bytecode and the loglevel.
99 */
100 static int ht_match_ust_app_event(struct cds_lfht_node *node, const void *_key)
101 {
102 struct ust_app_event *event;
103 const struct ust_app_ht_key *key;
104
105 assert(node);
106 assert(_key);
107
108 event = caa_container_of(node, struct ust_app_event, node.node);
109 key = _key;
110
111 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
112
113 /* Event name */
114 if (strncmp(event->attr.name, key->name, sizeof(event->attr.name)) != 0) {
115 goto no_match;
116 }
117
118 /* Event loglevel. */
119 if (event->attr.loglevel != key->loglevel) {
120 if (event->attr.loglevel_type == LTTNG_UST_LOGLEVEL_ALL
121 && key->loglevel == 0 && event->attr.loglevel == -1) {
122 /*
123 * Match is accepted. This is because on event creation, the
124 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
125 * -1 are accepted for this loglevel type since 0 is the one set by
126 * the API when receiving an enable event.
127 */
128 } else {
129 goto no_match;
130 }
131 }
132
133 /* One of the filters is NULL, fail. */
134 if ((key->filter && !event->filter) || (!key->filter && event->filter)) {
135 goto no_match;
136 }
137
138 if (key->filter && event->filter) {
139 /* Both filters exists, check length followed by the bytecode. */
140 if (event->filter->len != key->filter->len ||
141 memcmp(event->filter->data, key->filter->data,
142 event->filter->len) != 0) {
143 goto no_match;
144 }
145 }
146
147 /* One of the exclusions is NULL, fail. */
148 if ((key->exclusion && !event->exclusion) || (!key->exclusion && event->exclusion)) {
149 goto no_match;
150 }
151
152 if (key->exclusion && event->exclusion) {
153 /* Both exclusions exists, check count followed by the names. */
154 if (event->exclusion->count != key->exclusion->count ||
155 memcmp(event->exclusion->names, key->exclusion->names,
156 event->exclusion->count * LTTNG_UST_SYM_NAME_LEN) != 0) {
157 goto no_match;
158 }
159 }
160
161
162 /* Match. */
163 return 1;
164
165 no_match:
166 return 0;
167 }
168
169 /*
170 * Unique add of an ust app event in the given ht. This uses the custom
171 * ht_match_ust_app_event match function and the event name as hash.
172 */
173 static void add_unique_ust_app_event(struct ust_app_channel *ua_chan,
174 struct ust_app_event *event)
175 {
176 struct cds_lfht_node *node_ptr;
177 struct ust_app_ht_key key;
178 struct lttng_ht *ht;
179
180 assert(ua_chan);
181 assert(ua_chan->events);
182 assert(event);
183
184 ht = ua_chan->events;
185 key.name = event->attr.name;
186 key.filter = event->filter;
187 key.loglevel = event->attr.loglevel;
188 key.exclusion = event->exclusion;
189
190 node_ptr = cds_lfht_add_unique(ht->ht,
191 ht->hash_fct(event->node.key, lttng_ht_seed),
192 ht_match_ust_app_event, &key, &event->node.node);
193 assert(node_ptr == &event->node.node);
194 }
195
196 /*
197 * Close the notify socket from the given RCU head object. This MUST be called
198 * through a call_rcu().
199 */
200 static void close_notify_sock_rcu(struct rcu_head *head)
201 {
202 int ret;
203 struct ust_app_notify_sock_obj *obj =
204 caa_container_of(head, struct ust_app_notify_sock_obj, head);
205
206 /* Must have a valid fd here. */
207 assert(obj->fd >= 0);
208
209 ret = close(obj->fd);
210 if (ret) {
211 ERR("close notify sock %d RCU", obj->fd);
212 }
213 lttng_fd_put(LTTNG_FD_APPS, 1);
214
215 free(obj);
216 }
217
218 /*
219 * Return the session registry according to the buffer type of the given
220 * session.
221 *
222 * A registry per UID object MUST exists before calling this function or else
223 * it assert() if not found. RCU read side lock must be acquired.
224 */
225 static struct ust_registry_session *get_session_registry(
226 struct ust_app_session *ua_sess)
227 {
228 struct ust_registry_session *registry = NULL;
229
230 assert(ua_sess);
231
232 switch (ua_sess->buffer_type) {
233 case LTTNG_BUFFER_PER_PID:
234 {
235 struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id);
236 if (!reg_pid) {
237 goto error;
238 }
239 registry = reg_pid->registry->reg.ust;
240 break;
241 }
242 case LTTNG_BUFFER_PER_UID:
243 {
244 struct buffer_reg_uid *reg_uid = buffer_reg_uid_find(
245 ua_sess->tracing_id, ua_sess->bits_per_long, ua_sess->uid);
246 if (!reg_uid) {
247 goto error;
248 }
249 registry = reg_uid->registry->reg.ust;
250 break;
251 }
252 default:
253 assert(0);
254 };
255
256 error:
257 return registry;
258 }
259
260 /*
261 * Delete ust context safely. RCU read lock must be held before calling
262 * this function.
263 */
264 static
265 void delete_ust_app_ctx(int sock, struct ust_app_ctx *ua_ctx)
266 {
267 int ret;
268
269 assert(ua_ctx);
270
271 if (ua_ctx->obj) {
272 ret = ustctl_release_object(sock, ua_ctx->obj);
273 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
274 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
275 sock, ua_ctx->obj->handle, ret);
276 }
277 free(ua_ctx->obj);
278 }
279 free(ua_ctx);
280 }
281
282 /*
283 * Delete ust app event safely. RCU read lock must be held before calling
284 * this function.
285 */
286 static
287 void delete_ust_app_event(int sock, struct ust_app_event *ua_event)
288 {
289 int ret;
290
291 assert(ua_event);
292
293 free(ua_event->filter);
294 if (ua_event->exclusion != NULL)
295 free(ua_event->exclusion);
296 if (ua_event->obj != NULL) {
297 ret = ustctl_release_object(sock, ua_event->obj);
298 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
299 ERR("UST app sock %d release event obj failed with ret %d",
300 sock, ret);
301 }
302 free(ua_event->obj);
303 }
304 free(ua_event);
305 }
306
307 /*
308 * Release ust data object of the given stream.
309 *
310 * Return 0 on success or else a negative value.
311 */
312 static int release_ust_app_stream(int sock, struct ust_app_stream *stream)
313 {
314 int ret = 0;
315
316 assert(stream);
317
318 if (stream->obj) {
319 ret = ustctl_release_object(sock, stream->obj);
320 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
321 ERR("UST app sock %d release stream obj failed with ret %d",
322 sock, ret);
323 }
324 lttng_fd_put(LTTNG_FD_APPS, 2);
325 free(stream->obj);
326 }
327
328 return ret;
329 }
330
331 /*
332 * Delete ust app stream safely. RCU read lock must be held before calling
333 * this function.
334 */
335 static
336 void delete_ust_app_stream(int sock, struct ust_app_stream *stream)
337 {
338 assert(stream);
339
340 (void) release_ust_app_stream(sock, stream);
341 free(stream);
342 }
343
344 /*
345 * We need to execute ht_destroy outside of RCU read-side critical
346 * section and outside of call_rcu thread, so we postpone its execution
347 * using ht_cleanup_push. It is simpler than to change the semantic of
348 * the many callers of delete_ust_app_session().
349 */
350 static
351 void delete_ust_app_channel_rcu(struct rcu_head *head)
352 {
353 struct ust_app_channel *ua_chan =
354 caa_container_of(head, struct ust_app_channel, rcu_head);
355
356 ht_cleanup_push(ua_chan->ctx);
357 ht_cleanup_push(ua_chan->events);
358 free(ua_chan);
359 }
360
361 /*
362 * Delete ust app channel safely. RCU read lock must be held before calling
363 * this function.
364 */
365 static
366 void delete_ust_app_channel(int sock, struct ust_app_channel *ua_chan,
367 struct ust_app *app)
368 {
369 int ret;
370 struct lttng_ht_iter iter;
371 struct ust_app_event *ua_event;
372 struct ust_app_ctx *ua_ctx;
373 struct ust_app_stream *stream, *stmp;
374 struct ust_registry_session *registry;
375
376 assert(ua_chan);
377
378 DBG3("UST app deleting channel %s", ua_chan->name);
379
380 /* Wipe stream */
381 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
382 cds_list_del(&stream->list);
383 delete_ust_app_stream(sock, stream);
384 }
385
386 /* Wipe context */
387 cds_lfht_for_each_entry(ua_chan->ctx->ht, &iter.iter, ua_ctx, node.node) {
388 cds_list_del(&ua_ctx->list);
389 ret = lttng_ht_del(ua_chan->ctx, &iter);
390 assert(!ret);
391 delete_ust_app_ctx(sock, ua_ctx);
392 }
393
394 /* Wipe events */
395 cds_lfht_for_each_entry(ua_chan->events->ht, &iter.iter, ua_event,
396 node.node) {
397 ret = lttng_ht_del(ua_chan->events, &iter);
398 assert(!ret);
399 delete_ust_app_event(sock, ua_event);
400 }
401
402 if (ua_chan->session->buffer_type == LTTNG_BUFFER_PER_PID) {
403 /* Wipe and free registry from session registry. */
404 registry = get_session_registry(ua_chan->session);
405 if (registry) {
406 ust_registry_channel_del_free(registry, ua_chan->key);
407 }
408 }
409
410 if (ua_chan->obj != NULL) {
411 /* Remove channel from application UST object descriptor. */
412 iter.iter.node = &ua_chan->ust_objd_node.node;
413 ret = lttng_ht_del(app->ust_objd, &iter);
414 assert(!ret);
415 ret = ustctl_release_object(sock, ua_chan->obj);
416 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
417 ERR("UST app sock %d release channel obj failed with ret %d",
418 sock, ret);
419 }
420 lttng_fd_put(LTTNG_FD_APPS, 1);
421 free(ua_chan->obj);
422 }
423 call_rcu(&ua_chan->rcu_head, delete_ust_app_channel_rcu);
424 }
425
426 /*
427 * Push metadata to consumer socket.
428 *
429 * RCU read-side lock must be held to guarantee existance of socket.
430 * Must be called with the ust app session lock held.
431 * Must be called with the registry lock held.
432 *
433 * On success, return the len of metadata pushed or else a negative value.
434 * Returning a -EPIPE return value means we could not send the metadata,
435 * but it can be caused by recoverable errors (e.g. the application has
436 * terminated concurrently).
437 */
438 ssize_t ust_app_push_metadata(struct ust_registry_session *registry,
439 struct consumer_socket *socket, int send_zero_data)
440 {
441 int ret;
442 char *metadata_str = NULL;
443 size_t len, offset;
444 ssize_t ret_val;
445
446 assert(registry);
447 assert(socket);
448
449 /*
450 * Means that no metadata was assigned to the session. This can
451 * happens if no start has been done previously.
452 */
453 if (!registry->metadata_key) {
454 return 0;
455 }
456
457 /*
458 * On a push metadata error either the consumer is dead or the
459 * metadata channel has been destroyed because its endpoint
460 * might have died (e.g: relayd), or because the application has
461 * exited. If so, the metadata closed flag is set to 1 so we
462 * deny pushing metadata again which is not valid anymore on the
463 * consumer side.
464 */
465 if (registry->metadata_closed) {
466 return -EPIPE;
467 }
468
469 offset = registry->metadata_len_sent;
470 len = registry->metadata_len - registry->metadata_len_sent;
471 if (len == 0) {
472 DBG3("No metadata to push for metadata key %" PRIu64,
473 registry->metadata_key);
474 ret_val = len;
475 if (send_zero_data) {
476 DBG("No metadata to push");
477 goto push_data;
478 }
479 goto end;
480 }
481
482 /* Allocate only what we have to send. */
483 metadata_str = zmalloc(len);
484 if (!metadata_str) {
485 PERROR("zmalloc ust app metadata string");
486 ret_val = -ENOMEM;
487 goto error;
488 }
489 /* Copy what we haven't send out. */
490 memcpy(metadata_str, registry->metadata + offset, len);
491 registry->metadata_len_sent += len;
492
493 push_data:
494 ret = consumer_push_metadata(socket, registry->metadata_key,
495 metadata_str, len, offset);
496 if (ret < 0) {
497 /*
498 * There is an acceptable race here between the registry
499 * metadata key assignment and the creation on the
500 * consumer. The session daemon can concurrently push
501 * metadata for this registry while being created on the
502 * consumer since the metadata key of the registry is
503 * assigned *before* it is setup to avoid the consumer
504 * to ask for metadata that could possibly be not found
505 * in the session daemon.
506 *
507 * The metadata will get pushed either by the session
508 * being stopped or the consumer requesting metadata if
509 * that race is triggered.
510 */
511 if (ret == -LTTCOMM_CONSUMERD_CHANNEL_FAIL) {
512 ret = 0;
513 }
514
515 /*
516 * Update back the actual metadata len sent since it
517 * failed here.
518 */
519 registry->metadata_len_sent -= len;
520 ret_val = ret;
521 goto error_push;
522 }
523
524 free(metadata_str);
525 return len;
526
527 end:
528 error:
529 if (ret_val) {
530 /*
531 * On error, flag the registry that the metadata is
532 * closed. We were unable to push anything and this
533 * means that either the consumer is not responding or
534 * the metadata cache has been destroyed on the
535 * consumer.
536 */
537 registry->metadata_closed = 1;
538 }
539 error_push:
540 free(metadata_str);
541 return ret_val;
542 }
543
544 /*
545 * For a given application and session, push metadata to consumer.
546 * Either sock or consumer is required : if sock is NULL, the default
547 * socket to send the metadata is retrieved from consumer, if sock
548 * is not NULL we use it to send the metadata.
549 * RCU read-side lock must be held while calling this function,
550 * therefore ensuring existance of registry. It also ensures existance
551 * of socket throughout this function.
552 *
553 * Return 0 on success else a negative error.
554 * Returning a -EPIPE return value means we could not send the metadata,
555 * but it can be caused by recoverable errors (e.g. the application has
556 * terminated concurrently).
557 */
558 static int push_metadata(struct ust_registry_session *registry,
559 struct consumer_output *consumer)
560 {
561 int ret_val;
562 ssize_t ret;
563 struct consumer_socket *socket;
564
565 assert(registry);
566 assert(consumer);
567
568 pthread_mutex_lock(&registry->lock);
569 if (registry->metadata_closed) {
570 ret_val = -EPIPE;
571 goto error;
572 }
573
574 /* Get consumer socket to use to push the metadata.*/
575 socket = consumer_find_socket_by_bitness(registry->bits_per_long,
576 consumer);
577 if (!socket) {
578 ret_val = -1;
579 goto error;
580 }
581
582 ret = ust_app_push_metadata(registry, socket, 0);
583 if (ret < 0) {
584 ret_val = ret;
585 goto error;
586 }
587 pthread_mutex_unlock(&registry->lock);
588 return 0;
589
590 error:
591 pthread_mutex_unlock(&registry->lock);
592 return ret_val;
593 }
594
595 /*
596 * Send to the consumer a close metadata command for the given session. Once
597 * done, the metadata channel is deleted and the session metadata pointer is
598 * nullified. The session lock MUST be held unless the application is
599 * in the destroy path.
600 *
601 * Return 0 on success else a negative value.
602 */
603 static int close_metadata(struct ust_registry_session *registry,
604 struct consumer_output *consumer)
605 {
606 int ret;
607 struct consumer_socket *socket;
608
609 assert(registry);
610 assert(consumer);
611
612 rcu_read_lock();
613
614 pthread_mutex_lock(&registry->lock);
615
616 if (!registry->metadata_key || registry->metadata_closed) {
617 ret = 0;
618 goto end;
619 }
620
621 /* Get consumer socket to use to push the metadata.*/
622 socket = consumer_find_socket_by_bitness(registry->bits_per_long,
623 consumer);
624 if (!socket) {
625 ret = -1;
626 goto error;
627 }
628
629 ret = consumer_close_metadata(socket, registry->metadata_key);
630 if (ret < 0) {
631 goto error;
632 }
633
634 error:
635 /*
636 * Metadata closed. Even on error this means that the consumer is not
637 * responding or not found so either way a second close should NOT be emit
638 * for this registry.
639 */
640 registry->metadata_closed = 1;
641 end:
642 pthread_mutex_unlock(&registry->lock);
643 rcu_read_unlock();
644 return ret;
645 }
646
647 /*
648 * We need to execute ht_destroy outside of RCU read-side critical
649 * section and outside of call_rcu thread, so we postpone its execution
650 * using ht_cleanup_push. It is simpler than to change the semantic of
651 * the many callers of delete_ust_app_session().
652 */
653 static
654 void delete_ust_app_session_rcu(struct rcu_head *head)
655 {
656 struct ust_app_session *ua_sess =
657 caa_container_of(head, struct ust_app_session, rcu_head);
658
659 ht_cleanup_push(ua_sess->channels);
660 free(ua_sess);
661 }
662
663 /*
664 * Delete ust app session safely. RCU read lock must be held before calling
665 * this function.
666 */
667 static
668 void delete_ust_app_session(int sock, struct ust_app_session *ua_sess,
669 struct ust_app *app)
670 {
671 int ret;
672 struct lttng_ht_iter iter;
673 struct ust_app_channel *ua_chan;
674 struct ust_registry_session *registry;
675
676 assert(ua_sess);
677
678 pthread_mutex_lock(&ua_sess->lock);
679
680 assert(!ua_sess->deleted);
681 ua_sess->deleted = true;
682
683 registry = get_session_registry(ua_sess);
684 if (registry) {
685 /* Push metadata for application before freeing the application. */
686 (void) push_metadata(registry, ua_sess->consumer);
687
688 /*
689 * Don't ask to close metadata for global per UID buffers. Close
690 * metadata only on destroy trace session in this case. Also, the
691 * previous push metadata could have flag the metadata registry to
692 * close so don't send a close command if closed.
693 */
694 if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) {
695 /* And ask to close it for this session registry. */
696 (void) close_metadata(registry, ua_sess->consumer);
697 }
698 }
699
700 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
701 node.node) {
702 ret = lttng_ht_del(ua_sess->channels, &iter);
703 assert(!ret);
704 delete_ust_app_channel(sock, ua_chan, app);
705 }
706
707 /* In case of per PID, the registry is kept in the session. */
708 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) {
709 struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id);
710 if (reg_pid) {
711 buffer_reg_pid_remove(reg_pid);
712 buffer_reg_pid_destroy(reg_pid);
713 }
714 }
715
716 if (ua_sess->handle != -1) {
717 ret = ustctl_release_handle(sock, ua_sess->handle);
718 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
719 ERR("UST app sock %d release session handle failed with ret %d",
720 sock, ret);
721 }
722 }
723 pthread_mutex_unlock(&ua_sess->lock);
724
725 call_rcu(&ua_sess->rcu_head, delete_ust_app_session_rcu);
726 }
727
728 /*
729 * Delete a traceable application structure from the global list. Never call
730 * this function outside of a call_rcu call.
731 *
732 * RCU read side lock should _NOT_ be held when calling this function.
733 */
734 static
735 void delete_ust_app(struct ust_app *app)
736 {
737 int ret, sock;
738 struct ust_app_session *ua_sess, *tmp_ua_sess;
739
740 /* Delete ust app sessions info */
741 sock = app->sock;
742 app->sock = -1;
743
744 /* Wipe sessions */
745 cds_list_for_each_entry_safe(ua_sess, tmp_ua_sess, &app->teardown_head,
746 teardown_node) {
747 /* Free every object in the session and the session. */
748 rcu_read_lock();
749 delete_ust_app_session(sock, ua_sess, app);
750 rcu_read_unlock();
751 }
752
753 ht_cleanup_push(app->sessions);
754 ht_cleanup_push(app->ust_objd);
755
756 /*
757 * Wait until we have deleted the application from the sock hash table
758 * before closing this socket, otherwise an application could re-use the
759 * socket ID and race with the teardown, using the same hash table entry.
760 *
761 * It's OK to leave the close in call_rcu. We want it to stay unique for
762 * all RCU readers that could run concurrently with unregister app,
763 * therefore we _need_ to only close that socket after a grace period. So
764 * it should stay in this RCU callback.
765 *
766 * This close() is a very important step of the synchronization model so
767 * every modification to this function must be carefully reviewed.
768 */
769 ret = close(sock);
770 if (ret) {
771 PERROR("close");
772 }
773 lttng_fd_put(LTTNG_FD_APPS, 1);
774
775 DBG2("UST app pid %d deleted", app->pid);
776 free(app);
777 }
778
779 /*
780 * URCU intermediate call to delete an UST app.
781 */
782 static
783 void delete_ust_app_rcu(struct rcu_head *head)
784 {
785 struct lttng_ht_node_ulong *node =
786 caa_container_of(head, struct lttng_ht_node_ulong, head);
787 struct ust_app *app =
788 caa_container_of(node, struct ust_app, pid_n);
789
790 DBG3("Call RCU deleting app PID %d", app->pid);
791 delete_ust_app(app);
792 }
793
794 /*
795 * Delete the session from the application ht and delete the data structure by
796 * freeing every object inside and releasing them.
797 */
798 static void destroy_app_session(struct ust_app *app,
799 struct ust_app_session *ua_sess)
800 {
801 int ret;
802 struct lttng_ht_iter iter;
803
804 assert(app);
805 assert(ua_sess);
806
807 iter.iter.node = &ua_sess->node.node;
808 ret = lttng_ht_del(app->sessions, &iter);
809 if (ret) {
810 /* Already scheduled for teardown. */
811 goto end;
812 }
813
814 /* Once deleted, free the data structure. */
815 delete_ust_app_session(app->sock, ua_sess, app);
816
817 end:
818 return;
819 }
820
821 /*
822 * Alloc new UST app session.
823 */
824 static
825 struct ust_app_session *alloc_ust_app_session(struct ust_app *app)
826 {
827 struct ust_app_session *ua_sess;
828
829 /* Init most of the default value by allocating and zeroing */
830 ua_sess = zmalloc(sizeof(struct ust_app_session));
831 if (ua_sess == NULL) {
832 PERROR("malloc");
833 goto error_free;
834 }
835
836 ua_sess->handle = -1;
837 ua_sess->channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
838 ua_sess->metadata_attr.type = LTTNG_UST_CHAN_METADATA;
839 pthread_mutex_init(&ua_sess->lock, NULL);
840
841 return ua_sess;
842
843 error_free:
844 return NULL;
845 }
846
847 /*
848 * Alloc new UST app channel.
849 */
850 static
851 struct ust_app_channel *alloc_ust_app_channel(char *name,
852 struct ust_app_session *ua_sess,
853 struct lttng_ust_channel_attr *attr)
854 {
855 struct ust_app_channel *ua_chan;
856
857 /* Init most of the default value by allocating and zeroing */
858 ua_chan = zmalloc(sizeof(struct ust_app_channel));
859 if (ua_chan == NULL) {
860 PERROR("malloc");
861 goto error;
862 }
863
864 /* Setup channel name */
865 strncpy(ua_chan->name, name, sizeof(ua_chan->name));
866 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
867
868 ua_chan->enabled = 1;
869 ua_chan->handle = -1;
870 ua_chan->session = ua_sess;
871 ua_chan->key = get_next_channel_key();
872 ua_chan->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
873 ua_chan->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
874 lttng_ht_node_init_str(&ua_chan->node, ua_chan->name);
875
876 CDS_INIT_LIST_HEAD(&ua_chan->streams.head);
877 CDS_INIT_LIST_HEAD(&ua_chan->ctx_list);
878
879 /* Copy attributes */
880 if (attr) {
881 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
882 ua_chan->attr.subbuf_size = attr->subbuf_size;
883 ua_chan->attr.num_subbuf = attr->num_subbuf;
884 ua_chan->attr.overwrite = attr->overwrite;
885 ua_chan->attr.switch_timer_interval = attr->switch_timer_interval;
886 ua_chan->attr.read_timer_interval = attr->read_timer_interval;
887 ua_chan->attr.output = attr->output;
888 }
889 /* By default, the channel is a per cpu channel. */
890 ua_chan->attr.type = LTTNG_UST_CHAN_PER_CPU;
891
892 DBG3("UST app channel %s allocated", ua_chan->name);
893
894 return ua_chan;
895
896 error:
897 return NULL;
898 }
899
900 /*
901 * Allocate and initialize a UST app stream.
902 *
903 * Return newly allocated stream pointer or NULL on error.
904 */
905 struct ust_app_stream *ust_app_alloc_stream(void)
906 {
907 struct ust_app_stream *stream = NULL;
908
909 stream = zmalloc(sizeof(*stream));
910 if (stream == NULL) {
911 PERROR("zmalloc ust app stream");
912 goto error;
913 }
914
915 /* Zero could be a valid value for a handle so flag it to -1. */
916 stream->handle = -1;
917
918 error:
919 return stream;
920 }
921
922 /*
923 * Alloc new UST app event.
924 */
925 static
926 struct ust_app_event *alloc_ust_app_event(char *name,
927 struct lttng_ust_event *attr)
928 {
929 struct ust_app_event *ua_event;
930
931 /* Init most of the default value by allocating and zeroing */
932 ua_event = zmalloc(sizeof(struct ust_app_event));
933 if (ua_event == NULL) {
934 PERROR("malloc");
935 goto error;
936 }
937
938 ua_event->enabled = 1;
939 strncpy(ua_event->name, name, sizeof(ua_event->name));
940 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
941 lttng_ht_node_init_str(&ua_event->node, ua_event->name);
942
943 /* Copy attributes */
944 if (attr) {
945 memcpy(&ua_event->attr, attr, sizeof(ua_event->attr));
946 }
947
948 DBG3("UST app event %s allocated", ua_event->name);
949
950 return ua_event;
951
952 error:
953 return NULL;
954 }
955
956 /*
957 * Alloc new UST app context.
958 */
959 static
960 struct ust_app_ctx *alloc_ust_app_ctx(struct lttng_ust_context *uctx)
961 {
962 struct ust_app_ctx *ua_ctx;
963
964 ua_ctx = zmalloc(sizeof(struct ust_app_ctx));
965 if (ua_ctx == NULL) {
966 goto error;
967 }
968
969 CDS_INIT_LIST_HEAD(&ua_ctx->list);
970
971 if (uctx) {
972 memcpy(&ua_ctx->ctx, uctx, sizeof(ua_ctx->ctx));
973 }
974
975 DBG3("UST app context %d allocated", ua_ctx->ctx.ctx);
976
977 error:
978 return ua_ctx;
979 }
980
981 /*
982 * Allocate a filter and copy the given original filter.
983 *
984 * Return allocated filter or NULL on error.
985 */
986 static struct lttng_filter_bytecode *copy_filter_bytecode(
987 struct lttng_filter_bytecode *orig_f)
988 {
989 struct lttng_filter_bytecode *filter = NULL;
990
991 /* Copy filter bytecode */
992 filter = zmalloc(sizeof(*filter) + orig_f->len);
993 if (!filter) {
994 PERROR("zmalloc alloc filter bytecode");
995 goto error;
996 }
997
998 memcpy(filter, orig_f, sizeof(*filter) + orig_f->len);
999
1000 error:
1001 return filter;
1002 }
1003
1004 /*
1005 * Create a liblttng-ust filter bytecode from given bytecode.
1006 *
1007 * Return allocated filter or NULL on error.
1008 */
1009 static struct lttng_ust_filter_bytecode *create_ust_bytecode_from_bytecode(
1010 struct lttng_filter_bytecode *orig_f)
1011 {
1012 struct lttng_ust_filter_bytecode *filter = NULL;
1013
1014 /* Copy filter bytecode */
1015 filter = zmalloc(sizeof(*filter) + orig_f->len);
1016 if (!filter) {
1017 PERROR("zmalloc alloc ust filter bytecode");
1018 goto error;
1019 }
1020
1021 assert(sizeof(struct lttng_filter_bytecode) ==
1022 sizeof(struct lttng_ust_filter_bytecode));
1023 memcpy(filter, orig_f, sizeof(*filter) + orig_f->len);
1024 error:
1025 return filter;
1026 }
1027
1028 /*
1029 * Find an ust_app using the sock and return it. RCU read side lock must be
1030 * held before calling this helper function.
1031 */
1032 struct ust_app *ust_app_find_by_sock(int sock)
1033 {
1034 struct lttng_ht_node_ulong *node;
1035 struct lttng_ht_iter iter;
1036
1037 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter);
1038 node = lttng_ht_iter_get_node_ulong(&iter);
1039 if (node == NULL) {
1040 DBG2("UST app find by sock %d not found", sock);
1041 goto error;
1042 }
1043
1044 return caa_container_of(node, struct ust_app, sock_n);
1045
1046 error:
1047 return NULL;
1048 }
1049
1050 /*
1051 * Find an ust_app using the notify sock and return it. RCU read side lock must
1052 * be held before calling this helper function.
1053 */
1054 static struct ust_app *find_app_by_notify_sock(int sock)
1055 {
1056 struct lttng_ht_node_ulong *node;
1057 struct lttng_ht_iter iter;
1058
1059 lttng_ht_lookup(ust_app_ht_by_notify_sock, (void *)((unsigned long) sock),
1060 &iter);
1061 node = lttng_ht_iter_get_node_ulong(&iter);
1062 if (node == NULL) {
1063 DBG2("UST app find by notify sock %d not found", sock);
1064 goto error;
1065 }
1066
1067 return caa_container_of(node, struct ust_app, notify_sock_n);
1068
1069 error:
1070 return NULL;
1071 }
1072
1073 /*
1074 * Lookup for an ust app event based on event name, filter bytecode and the
1075 * event loglevel.
1076 *
1077 * Return an ust_app_event object or NULL on error.
1078 */
1079 static struct ust_app_event *find_ust_app_event(struct lttng_ht *ht,
1080 char *name, struct lttng_filter_bytecode *filter, int loglevel,
1081 const struct lttng_event_exclusion *exclusion)
1082 {
1083 struct lttng_ht_iter iter;
1084 struct lttng_ht_node_str *node;
1085 struct ust_app_event *event = NULL;
1086 struct ust_app_ht_key key;
1087
1088 assert(name);
1089 assert(ht);
1090
1091 /* Setup key for event lookup. */
1092 key.name = name;
1093 key.filter = filter;
1094 key.loglevel = loglevel;
1095 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1096 key.exclusion = exclusion;
1097
1098 /* Lookup using the event name as hash and a custom match fct. */
1099 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
1100 ht_match_ust_app_event, &key, &iter.iter);
1101 node = lttng_ht_iter_get_node_str(&iter);
1102 if (node == NULL) {
1103 goto end;
1104 }
1105
1106 event = caa_container_of(node, struct ust_app_event, node);
1107
1108 end:
1109 return event;
1110 }
1111
1112 /*
1113 * Create the channel context on the tracer.
1114 *
1115 * Called with UST app session lock held.
1116 */
1117 static
1118 int create_ust_channel_context(struct ust_app_channel *ua_chan,
1119 struct ust_app_ctx *ua_ctx, struct ust_app *app)
1120 {
1121 int ret;
1122
1123 health_code_update();
1124
1125 ret = ustctl_add_context(app->sock, &ua_ctx->ctx,
1126 ua_chan->obj, &ua_ctx->obj);
1127 if (ret < 0) {
1128 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1129 ERR("UST app create channel context failed for app (pid: %d) "
1130 "with ret %d", app->pid, ret);
1131 } else {
1132 /*
1133 * This is normal behavior, an application can die during the
1134 * creation process. Don't report an error so the execution can
1135 * continue normally.
1136 */
1137 ret = 0;
1138 DBG3("UST app disable event failed. Application is dead.");
1139 }
1140 goto error;
1141 }
1142
1143 ua_ctx->handle = ua_ctx->obj->handle;
1144
1145 DBG2("UST app context handle %d created successfully for channel %s",
1146 ua_ctx->handle, ua_chan->name);
1147
1148 error:
1149 health_code_update();
1150 return ret;
1151 }
1152
1153 /*
1154 * Set the filter on the tracer.
1155 */
1156 static
1157 int set_ust_event_filter(struct ust_app_event *ua_event,
1158 struct ust_app *app)
1159 {
1160 int ret;
1161 struct lttng_ust_filter_bytecode *ust_bytecode = NULL;
1162
1163 health_code_update();
1164
1165 if (!ua_event->filter) {
1166 ret = 0;
1167 goto error;
1168 }
1169
1170 ust_bytecode = create_ust_bytecode_from_bytecode(ua_event->filter);
1171 if (!ust_bytecode) {
1172 ret = -LTTNG_ERR_NOMEM;
1173 goto error;
1174 }
1175 ret = ustctl_set_filter(app->sock, ust_bytecode,
1176 ua_event->obj);
1177 if (ret < 0) {
1178 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1179 ERR("UST app event %s filter failed for app (pid: %d) "
1180 "with ret %d", ua_event->attr.name, app->pid, ret);
1181 } else {
1182 /*
1183 * This is normal behavior, an application can die during the
1184 * creation process. Don't report an error so the execution can
1185 * continue normally.
1186 */
1187 ret = 0;
1188 DBG3("UST app filter event failed. Application is dead.");
1189 }
1190 goto error;
1191 }
1192
1193 DBG2("UST filter set successfully for event %s", ua_event->name);
1194
1195 error:
1196 health_code_update();
1197 free(ust_bytecode);
1198 return ret;
1199 }
1200
1201 static
1202 struct lttng_ust_event_exclusion *create_ust_exclusion_from_exclusion(
1203 struct lttng_event_exclusion *exclusion)
1204 {
1205 struct lttng_ust_event_exclusion *ust_exclusion = NULL;
1206 size_t exclusion_alloc_size = sizeof(struct lttng_ust_event_exclusion) +
1207 LTTNG_UST_SYM_NAME_LEN * exclusion->count;
1208
1209 ust_exclusion = zmalloc(exclusion_alloc_size);
1210 if (!ust_exclusion) {
1211 PERROR("malloc");
1212 goto end;
1213 }
1214
1215 assert(sizeof(struct lttng_event_exclusion) ==
1216 sizeof(struct lttng_ust_event_exclusion));
1217 memcpy(ust_exclusion, exclusion, exclusion_alloc_size);
1218 end:
1219 return ust_exclusion;
1220 }
1221
1222 /*
1223 * Set event exclusions on the tracer.
1224 */
1225 static
1226 int set_ust_event_exclusion(struct ust_app_event *ua_event,
1227 struct ust_app *app)
1228 {
1229 int ret;
1230 struct lttng_ust_event_exclusion *ust_exclusion = NULL;
1231
1232 health_code_update();
1233
1234 if (!ua_event->exclusion || !ua_event->exclusion->count) {
1235 ret = 0;
1236 goto error;
1237 }
1238
1239 ust_exclusion = create_ust_exclusion_from_exclusion(
1240 ua_event->exclusion);
1241 if (!ust_exclusion) {
1242 ret = -LTTNG_ERR_NOMEM;
1243 goto error;
1244 }
1245 ret = ustctl_set_exclusion(app->sock, ust_exclusion, ua_event->obj);
1246 if (ret < 0) {
1247 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1248 ERR("UST app event %s exclusions failed for app (pid: %d) "
1249 "with ret %d", ua_event->attr.name, app->pid, ret);
1250 } else {
1251 /*
1252 * This is normal behavior, an application can die during the
1253 * creation process. Don't report an error so the execution can
1254 * continue normally.
1255 */
1256 ret = 0;
1257 DBG3("UST app event exclusion failed. Application is dead.");
1258 }
1259 goto error;
1260 }
1261
1262 DBG2("UST exclusion set successfully for event %s", ua_event->name);
1263
1264 error:
1265 health_code_update();
1266 free(ust_exclusion);
1267 return ret;
1268 }
1269
1270 /*
1271 * Disable the specified event on to UST tracer for the UST session.
1272 */
1273 static int disable_ust_event(struct ust_app *app,
1274 struct ust_app_session *ua_sess, struct ust_app_event *ua_event)
1275 {
1276 int ret;
1277
1278 health_code_update();
1279
1280 ret = ustctl_disable(app->sock, ua_event->obj);
1281 if (ret < 0) {
1282 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1283 ERR("UST app event %s disable failed for app (pid: %d) "
1284 "and session handle %d with ret %d",
1285 ua_event->attr.name, app->pid, ua_sess->handle, ret);
1286 } else {
1287 /*
1288 * This is normal behavior, an application can die during the
1289 * creation process. Don't report an error so the execution can
1290 * continue normally.
1291 */
1292 ret = 0;
1293 DBG3("UST app disable event failed. Application is dead.");
1294 }
1295 goto error;
1296 }
1297
1298 DBG2("UST app event %s disabled successfully for app (pid: %d)",
1299 ua_event->attr.name, app->pid);
1300
1301 error:
1302 health_code_update();
1303 return ret;
1304 }
1305
1306 /*
1307 * Disable the specified channel on to UST tracer for the UST session.
1308 */
1309 static int disable_ust_channel(struct ust_app *app,
1310 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
1311 {
1312 int ret;
1313
1314 health_code_update();
1315
1316 ret = ustctl_disable(app->sock, ua_chan->obj);
1317 if (ret < 0) {
1318 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1319 ERR("UST app channel %s disable failed for app (pid: %d) "
1320 "and session handle %d with ret %d",
1321 ua_chan->name, app->pid, ua_sess->handle, ret);
1322 } else {
1323 /*
1324 * This is normal behavior, an application can die during the
1325 * creation process. Don't report an error so the execution can
1326 * continue normally.
1327 */
1328 ret = 0;
1329 DBG3("UST app disable channel failed. Application is dead.");
1330 }
1331 goto error;
1332 }
1333
1334 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1335 ua_chan->name, app->pid);
1336
1337 error:
1338 health_code_update();
1339 return ret;
1340 }
1341
1342 /*
1343 * Enable the specified channel on to UST tracer for the UST session.
1344 */
1345 static int enable_ust_channel(struct ust_app *app,
1346 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
1347 {
1348 int ret;
1349
1350 health_code_update();
1351
1352 ret = ustctl_enable(app->sock, ua_chan->obj);
1353 if (ret < 0) {
1354 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1355 ERR("UST app channel %s enable failed for app (pid: %d) "
1356 "and session handle %d with ret %d",
1357 ua_chan->name, app->pid, ua_sess->handle, ret);
1358 } else {
1359 /*
1360 * This is normal behavior, an application can die during the
1361 * creation process. Don't report an error so the execution can
1362 * continue normally.
1363 */
1364 ret = 0;
1365 DBG3("UST app enable channel failed. Application is dead.");
1366 }
1367 goto error;
1368 }
1369
1370 ua_chan->enabled = 1;
1371
1372 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1373 ua_chan->name, app->pid);
1374
1375 error:
1376 health_code_update();
1377 return ret;
1378 }
1379
1380 /*
1381 * Enable the specified event on to UST tracer for the UST session.
1382 */
1383 static int enable_ust_event(struct ust_app *app,
1384 struct ust_app_session *ua_sess, struct ust_app_event *ua_event)
1385 {
1386 int ret;
1387
1388 health_code_update();
1389
1390 ret = ustctl_enable(app->sock, ua_event->obj);
1391 if (ret < 0) {
1392 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1393 ERR("UST app event %s enable failed for app (pid: %d) "
1394 "and session handle %d with ret %d",
1395 ua_event->attr.name, app->pid, ua_sess->handle, ret);
1396 } else {
1397 /*
1398 * This is normal behavior, an application can die during the
1399 * creation process. Don't report an error so the execution can
1400 * continue normally.
1401 */
1402 ret = 0;
1403 DBG3("UST app enable event failed. Application is dead.");
1404 }
1405 goto error;
1406 }
1407
1408 DBG2("UST app event %s enabled successfully for app (pid: %d)",
1409 ua_event->attr.name, app->pid);
1410
1411 error:
1412 health_code_update();
1413 return ret;
1414 }
1415
1416 /*
1417 * Send channel and stream buffer to application.
1418 *
1419 * Return 0 on success. On error, a negative value is returned.
1420 */
1421 static int send_channel_pid_to_ust(struct ust_app *app,
1422 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
1423 {
1424 int ret;
1425 struct ust_app_stream *stream, *stmp;
1426
1427 assert(app);
1428 assert(ua_sess);
1429 assert(ua_chan);
1430
1431 health_code_update();
1432
1433 DBG("UST app sending channel %s to UST app sock %d", ua_chan->name,
1434 app->sock);
1435
1436 /* Send channel to the application. */
1437 ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan);
1438 if (ret < 0) {
1439 goto error;
1440 }
1441
1442 health_code_update();
1443
1444 /* Send all streams to application. */
1445 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
1446 ret = ust_consumer_send_stream_to_ust(app, ua_chan, stream);
1447 if (ret < 0) {
1448 goto error;
1449 }
1450 /* We don't need the stream anymore once sent to the tracer. */
1451 cds_list_del(&stream->list);
1452 delete_ust_app_stream(-1, stream);
1453 }
1454 /* Flag the channel that it is sent to the application. */
1455 ua_chan->is_sent = 1;
1456
1457 error:
1458 health_code_update();
1459 return ret;
1460 }
1461
1462 /*
1463 * Create the specified event onto the UST tracer for a UST session.
1464 *
1465 * Should be called with session mutex held.
1466 */
1467 static
1468 int create_ust_event(struct ust_app *app, struct ust_app_session *ua_sess,
1469 struct ust_app_channel *ua_chan, struct ust_app_event *ua_event)
1470 {
1471 int ret = 0;
1472
1473 health_code_update();
1474
1475 /* Create UST event on tracer */
1476 ret = ustctl_create_event(app->sock, &ua_event->attr, ua_chan->obj,
1477 &ua_event->obj);
1478 if (ret < 0) {
1479 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1480 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1481 ua_event->attr.name, app->pid, ret);
1482 } else {
1483 /*
1484 * This is normal behavior, an application can die during the
1485 * creation process. Don't report an error so the execution can
1486 * continue normally.
1487 */
1488 ret = 0;
1489 DBG3("UST app create event failed. Application is dead.");
1490 }
1491 goto error;
1492 }
1493
1494 ua_event->handle = ua_event->obj->handle;
1495
1496 DBG2("UST app event %s created successfully for pid:%d",
1497 ua_event->attr.name, app->pid);
1498
1499 health_code_update();
1500
1501 /* Set filter if one is present. */
1502 if (ua_event->filter) {
1503 ret = set_ust_event_filter(ua_event, app);
1504 if (ret < 0) {
1505 goto error;
1506 }
1507 }
1508
1509 /* Set exclusions for the event */
1510 if (ua_event->exclusion) {
1511 ret = set_ust_event_exclusion(ua_event, app);
1512 if (ret < 0) {
1513 goto error;
1514 }
1515 }
1516
1517 /* If event not enabled, disable it on the tracer */
1518 if (ua_event->enabled) {
1519 /*
1520 * We now need to explicitly enable the event, since it
1521 * is now disabled at creation.
1522 */
1523 ret = enable_ust_event(app, ua_sess, ua_event);
1524 if (ret < 0) {
1525 /*
1526 * If we hit an EPERM, something is wrong with our enable call. If
1527 * we get an EEXIST, there is a problem on the tracer side since we
1528 * just created it.
1529 */
1530 switch (ret) {
1531 case -LTTNG_UST_ERR_PERM:
1532 /* Code flow problem */
1533 assert(0);
1534 case -LTTNG_UST_ERR_EXIST:
1535 /* It's OK for our use case. */
1536 ret = 0;
1537 break;
1538 default:
1539 break;
1540 }
1541 goto error;
1542 }
1543 }
1544
1545 error:
1546 health_code_update();
1547 return ret;
1548 }
1549
1550 /*
1551 * Copy data between an UST app event and a LTT event.
1552 */
1553 static void shadow_copy_event(struct ust_app_event *ua_event,
1554 struct ltt_ust_event *uevent)
1555 {
1556 size_t exclusion_alloc_size;
1557
1558 strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name));
1559 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
1560
1561 ua_event->enabled = uevent->enabled;
1562
1563 /* Copy event attributes */
1564 memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr));
1565
1566 /* Copy filter bytecode */
1567 if (uevent->filter) {
1568 ua_event->filter = copy_filter_bytecode(uevent->filter);
1569 /* Filter might be NULL here in case of ENONEM. */
1570 }
1571
1572 /* Copy exclusion data */
1573 if (uevent->exclusion) {
1574 exclusion_alloc_size = sizeof(struct lttng_event_exclusion) +
1575 LTTNG_UST_SYM_NAME_LEN * uevent->exclusion->count;
1576 ua_event->exclusion = zmalloc(exclusion_alloc_size);
1577 if (ua_event->exclusion == NULL) {
1578 PERROR("malloc");
1579 } else {
1580 memcpy(ua_event->exclusion, uevent->exclusion,
1581 exclusion_alloc_size);
1582 }
1583 }
1584 }
1585
1586 /*
1587 * Copy data between an UST app channel and a LTT channel.
1588 */
1589 static void shadow_copy_channel(struct ust_app_channel *ua_chan,
1590 struct ltt_ust_channel *uchan)
1591 {
1592 struct lttng_ht_iter iter;
1593 struct ltt_ust_event *uevent;
1594 struct ltt_ust_context *uctx;
1595 struct ust_app_event *ua_event;
1596 struct ust_app_ctx *ua_ctx;
1597
1598 DBG2("UST app shadow copy of channel %s started", ua_chan->name);
1599
1600 strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name));
1601 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
1602
1603 ua_chan->tracefile_size = uchan->tracefile_size;
1604 ua_chan->tracefile_count = uchan->tracefile_count;
1605
1606 /* Copy event attributes since the layout is different. */
1607 ua_chan->attr.subbuf_size = uchan->attr.subbuf_size;
1608 ua_chan->attr.num_subbuf = uchan->attr.num_subbuf;
1609 ua_chan->attr.overwrite = uchan->attr.overwrite;
1610 ua_chan->attr.switch_timer_interval = uchan->attr.switch_timer_interval;
1611 ua_chan->attr.read_timer_interval = uchan->attr.read_timer_interval;
1612 ua_chan->attr.output = uchan->attr.output;
1613 /*
1614 * Note that the attribute channel type is not set since the channel on the
1615 * tracing registry side does not have this information.
1616 */
1617
1618 ua_chan->enabled = uchan->enabled;
1619 ua_chan->tracing_channel_id = uchan->id;
1620
1621 cds_list_for_each_entry(uctx, &uchan->ctx_list, list) {
1622 ua_ctx = alloc_ust_app_ctx(&uctx->ctx);
1623 if (ua_ctx == NULL) {
1624 continue;
1625 }
1626 lttng_ht_node_init_ulong(&ua_ctx->node,
1627 (unsigned long) ua_ctx->ctx.ctx);
1628 lttng_ht_add_ulong(ua_chan->ctx, &ua_ctx->node);
1629 cds_list_add_tail(&ua_ctx->list, &ua_chan->ctx_list);
1630 }
1631
1632 /* Copy all events from ltt ust channel to ust app channel */
1633 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
1634 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
1635 uevent->filter, uevent->attr.loglevel, uevent->exclusion);
1636 if (ua_event == NULL) {
1637 DBG2("UST event %s not found on shadow copy channel",
1638 uevent->attr.name);
1639 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
1640 if (ua_event == NULL) {
1641 continue;
1642 }
1643 shadow_copy_event(ua_event, uevent);
1644 add_unique_ust_app_event(ua_chan, ua_event);
1645 }
1646 }
1647
1648 DBG3("UST app shadow copy of channel %s done", ua_chan->name);
1649 }
1650
1651 /*
1652 * Copy data between a UST app session and a regular LTT session.
1653 */
1654 static void shadow_copy_session(struct ust_app_session *ua_sess,
1655 struct ltt_ust_session *usess, struct ust_app *app)
1656 {
1657 struct lttng_ht_node_str *ua_chan_node;
1658 struct lttng_ht_iter iter;
1659 struct ltt_ust_channel *uchan;
1660 struct ust_app_channel *ua_chan;
1661 time_t rawtime;
1662 struct tm *timeinfo;
1663 char datetime[16];
1664 int ret;
1665 char tmp_shm_path[PATH_MAX];
1666
1667 /* Get date and time for unique app path */
1668 time(&rawtime);
1669 timeinfo = localtime(&rawtime);
1670 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
1671
1672 DBG2("Shadow copy of session handle %d", ua_sess->handle);
1673
1674 ua_sess->tracing_id = usess->id;
1675 ua_sess->id = get_next_session_id();
1676 ua_sess->uid = app->uid;
1677 ua_sess->gid = app->gid;
1678 ua_sess->euid = usess->uid;
1679 ua_sess->egid = usess->gid;
1680 ua_sess->buffer_type = usess->buffer_type;
1681 ua_sess->bits_per_long = app->bits_per_long;
1682 /* There is only one consumer object per session possible. */
1683 ua_sess->consumer = usess->consumer;
1684 ua_sess->output_traces = usess->output_traces;
1685 ua_sess->live_timer_interval = usess->live_timer_interval;
1686 copy_channel_attr_to_ustctl(&ua_sess->metadata_attr,
1687 &usess->metadata_attr);
1688
1689 switch (ua_sess->buffer_type) {
1690 case LTTNG_BUFFER_PER_PID:
1691 ret = snprintf(ua_sess->path, sizeof(ua_sess->path),
1692 DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s", app->name, app->pid,
1693 datetime);
1694 break;
1695 case LTTNG_BUFFER_PER_UID:
1696 ret = snprintf(ua_sess->path, sizeof(ua_sess->path),
1697 DEFAULT_UST_TRACE_UID_PATH, ua_sess->uid, app->bits_per_long);
1698 break;
1699 default:
1700 assert(0);
1701 goto error;
1702 }
1703 if (ret < 0) {
1704 PERROR("asprintf UST shadow copy session");
1705 assert(0);
1706 goto error;
1707 }
1708
1709 strncpy(ua_sess->root_shm_path, usess->root_shm_path,
1710 sizeof(ua_sess->root_shm_path));
1711 ua_sess->root_shm_path[sizeof(ua_sess->root_shm_path) - 1] = '\0';
1712 strncpy(ua_sess->shm_path, usess->shm_path,
1713 sizeof(ua_sess->shm_path));
1714 ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0';
1715 if (ua_sess->shm_path[0]) {
1716 switch (ua_sess->buffer_type) {
1717 case LTTNG_BUFFER_PER_PID:
1718 ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path),
1719 DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s",
1720 app->name, app->pid, datetime);
1721 break;
1722 case LTTNG_BUFFER_PER_UID:
1723 ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path),
1724 DEFAULT_UST_TRACE_UID_PATH,
1725 app->uid, app->bits_per_long);
1726 break;
1727 default:
1728 assert(0);
1729 goto error;
1730 }
1731 if (ret < 0) {
1732 PERROR("sprintf UST shadow copy session");
1733 assert(0);
1734 goto error;
1735 }
1736 strncat(ua_sess->shm_path, tmp_shm_path,
1737 sizeof(ua_sess->shm_path) - strlen(ua_sess->shm_path) - 1);
1738 ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0';
1739 }
1740
1741 /* Iterate over all channels in global domain. */
1742 cds_lfht_for_each_entry(usess->domain_global.channels->ht, &iter.iter,
1743 uchan, node.node) {
1744 struct lttng_ht_iter uiter;
1745
1746 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
1747 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
1748 if (ua_chan_node != NULL) {
1749 /* Session exist. Contiuing. */
1750 continue;
1751 }
1752
1753 DBG2("Channel %s not found on shadow session copy, creating it",
1754 uchan->name);
1755 ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr);
1756 if (ua_chan == NULL) {
1757 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
1758 continue;
1759 }
1760 shadow_copy_channel(ua_chan, uchan);
1761 /*
1762 * The concept of metadata channel does not exist on the tracing
1763 * registry side of the session daemon so this can only be a per CPU
1764 * channel and not metadata.
1765 */
1766 ua_chan->attr.type = LTTNG_UST_CHAN_PER_CPU;
1767
1768 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
1769 }
1770
1771 error:
1772 return;
1773 }
1774
1775 /*
1776 * Lookup sesison wrapper.
1777 */
1778 static
1779 void __lookup_session_by_app(struct ltt_ust_session *usess,
1780 struct ust_app *app, struct lttng_ht_iter *iter)
1781 {
1782 /* Get right UST app session from app */
1783 lttng_ht_lookup(app->sessions, &usess->id, iter);
1784 }
1785
1786 /*
1787 * Return ust app session from the app session hashtable using the UST session
1788 * id.
1789 */
1790 static struct ust_app_session *lookup_session_by_app(
1791 struct ltt_ust_session *usess, struct ust_app *app)
1792 {
1793 struct lttng_ht_iter iter;
1794 struct lttng_ht_node_u64 *node;
1795
1796 __lookup_session_by_app(usess, app, &iter);
1797 node = lttng_ht_iter_get_node_u64(&iter);
1798 if (node == NULL) {
1799 goto error;
1800 }
1801
1802 return caa_container_of(node, struct ust_app_session, node);
1803
1804 error:
1805 return NULL;
1806 }
1807
1808 /*
1809 * Setup buffer registry per PID for the given session and application. If none
1810 * is found, a new one is created, added to the global registry and
1811 * initialized. If regp is valid, it's set with the newly created object.
1812 *
1813 * Return 0 on success or else a negative value.
1814 */
1815 static int setup_buffer_reg_pid(struct ust_app_session *ua_sess,
1816 struct ust_app *app, struct buffer_reg_pid **regp)
1817 {
1818 int ret = 0;
1819 struct buffer_reg_pid *reg_pid;
1820
1821 assert(ua_sess);
1822 assert(app);
1823
1824 rcu_read_lock();
1825
1826 reg_pid = buffer_reg_pid_find(ua_sess->id);
1827 if (!reg_pid) {
1828 /*
1829 * This is the create channel path meaning that if there is NO
1830 * registry available, we have to create one for this session.
1831 */
1832 ret = buffer_reg_pid_create(ua_sess->id, &reg_pid,
1833 ua_sess->root_shm_path, ua_sess->shm_path);
1834 if (ret < 0) {
1835 goto error;
1836 }
1837 } else {
1838 goto end;
1839 }
1840
1841 /* Initialize registry. */
1842 ret = ust_registry_session_init(&reg_pid->registry->reg.ust, app,
1843 app->bits_per_long, app->uint8_t_alignment,
1844 app->uint16_t_alignment, app->uint32_t_alignment,
1845 app->uint64_t_alignment, app->long_alignment,
1846 app->byte_order, app->version.major,
1847 app->version.minor, reg_pid->root_shm_path,
1848 reg_pid->shm_path,
1849 ua_sess->euid, ua_sess->egid);
1850 if (ret < 0) {
1851 /*
1852 * reg_pid->registry->reg.ust is NULL upon error, so we need to
1853 * destroy the buffer registry, because it is always expected
1854 * that if the buffer registry can be found, its ust registry is
1855 * non-NULL.
1856 */
1857 buffer_reg_pid_destroy(reg_pid);
1858 goto error;
1859 }
1860
1861 buffer_reg_pid_add(reg_pid);
1862
1863 DBG3("UST app buffer registry per PID created successfully");
1864
1865 end:
1866 if (regp) {
1867 *regp = reg_pid;
1868 }
1869 error:
1870 rcu_read_unlock();
1871 return ret;
1872 }
1873
1874 /*
1875 * Setup buffer registry per UID for the given session and application. If none
1876 * is found, a new one is created, added to the global registry and
1877 * initialized. If regp is valid, it's set with the newly created object.
1878 *
1879 * Return 0 on success or else a negative value.
1880 */
1881 static int setup_buffer_reg_uid(struct ltt_ust_session *usess,
1882 struct ust_app_session *ua_sess,
1883 struct ust_app *app, struct buffer_reg_uid **regp)
1884 {
1885 int ret = 0;
1886 struct buffer_reg_uid *reg_uid;
1887
1888 assert(usess);
1889 assert(app);
1890
1891 rcu_read_lock();
1892
1893 reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid);
1894 if (!reg_uid) {
1895 /*
1896 * This is the create channel path meaning that if there is NO
1897 * registry available, we have to create one for this session.
1898 */
1899 ret = buffer_reg_uid_create(usess->id, app->bits_per_long, app->uid,
1900 LTTNG_DOMAIN_UST, &reg_uid,
1901 ua_sess->root_shm_path, ua_sess->shm_path);
1902 if (ret < 0) {
1903 goto error;
1904 }
1905 } else {
1906 goto end;
1907 }
1908
1909 /* Initialize registry. */
1910 ret = ust_registry_session_init(&reg_uid->registry->reg.ust, NULL,
1911 app->bits_per_long, app->uint8_t_alignment,
1912 app->uint16_t_alignment, app->uint32_t_alignment,
1913 app->uint64_t_alignment, app->long_alignment,
1914 app->byte_order, app->version.major,
1915 app->version.minor, reg_uid->root_shm_path,
1916 reg_uid->shm_path, usess->uid, usess->gid);
1917 if (ret < 0) {
1918 /*
1919 * reg_uid->registry->reg.ust is NULL upon error, so we need to
1920 * destroy the buffer registry, because it is always expected
1921 * that if the buffer registry can be found, its ust registry is
1922 * non-NULL.
1923 */
1924 buffer_reg_uid_destroy(reg_uid, NULL);
1925 goto error;
1926 }
1927 /* Add node to teardown list of the session. */
1928 cds_list_add(&reg_uid->lnode, &usess->buffer_reg_uid_list);
1929
1930 buffer_reg_uid_add(reg_uid);
1931
1932 DBG3("UST app buffer registry per UID created successfully");
1933 end:
1934 if (regp) {
1935 *regp = reg_uid;
1936 }
1937 error:
1938 rcu_read_unlock();
1939 return ret;
1940 }
1941
1942 /*
1943 * Create a session on the tracer side for the given app.
1944 *
1945 * On success, ua_sess_ptr is populated with the session pointer or else left
1946 * untouched. If the session was created, is_created is set to 1. On error,
1947 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
1948 * be NULL.
1949 *
1950 * Returns 0 on success or else a negative code which is either -ENOMEM or
1951 * -ENOTCONN which is the default code if the ustctl_create_session fails.
1952 */
1953 static int create_ust_app_session(struct ltt_ust_session *usess,
1954 struct ust_app *app, struct ust_app_session **ua_sess_ptr,
1955 int *is_created)
1956 {
1957 int ret, created = 0;
1958 struct ust_app_session *ua_sess;
1959
1960 assert(usess);
1961 assert(app);
1962 assert(ua_sess_ptr);
1963
1964 health_code_update();
1965
1966 ua_sess = lookup_session_by_app(usess, app);
1967 if (ua_sess == NULL) {
1968 DBG2("UST app pid: %d session id %" PRIu64 " not found, creating it",
1969 app->pid, usess->id);
1970 ua_sess = alloc_ust_app_session(app);
1971 if (ua_sess == NULL) {
1972 /* Only malloc can failed so something is really wrong */
1973 ret = -ENOMEM;
1974 goto error;
1975 }
1976 shadow_copy_session(ua_sess, usess, app);
1977 created = 1;
1978 }
1979
1980 switch (usess->buffer_type) {
1981 case LTTNG_BUFFER_PER_PID:
1982 /* Init local registry. */
1983 ret = setup_buffer_reg_pid(ua_sess, app, NULL);
1984 if (ret < 0) {
1985 delete_ust_app_session(-1, ua_sess, app);
1986 goto error;
1987 }
1988 break;
1989 case LTTNG_BUFFER_PER_UID:
1990 /* Look for a global registry. If none exists, create one. */
1991 ret = setup_buffer_reg_uid(usess, ua_sess, app, NULL);
1992 if (ret < 0) {
1993 delete_ust_app_session(-1, ua_sess, app);
1994 goto error;
1995 }
1996 break;
1997 default:
1998 assert(0);
1999 ret = -EINVAL;
2000 goto error;
2001 }
2002
2003 health_code_update();
2004
2005 if (ua_sess->handle == -1) {
2006 ret = ustctl_create_session(app->sock);
2007 if (ret < 0) {
2008 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
2009 ERR("Creating session for app pid %d with ret %d",
2010 app->pid, ret);
2011 } else {
2012 DBG("UST app creating session failed. Application is dead");
2013 /*
2014 * This is normal behavior, an application can die during the
2015 * creation process. Don't report an error so the execution can
2016 * continue normally. This will get flagged ENOTCONN and the
2017 * caller will handle it.
2018 */
2019 ret = 0;
2020 }
2021 delete_ust_app_session(-1, ua_sess, app);
2022 if (ret != -ENOMEM) {
2023 /*
2024 * Tracer is probably gone or got an internal error so let's
2025 * behave like it will soon unregister or not usable.
2026 */
2027 ret = -ENOTCONN;
2028 }
2029 goto error;
2030 }
2031
2032 ua_sess->handle = ret;
2033
2034 /* Add ust app session to app's HT */
2035 lttng_ht_node_init_u64(&ua_sess->node,
2036 ua_sess->tracing_id);
2037 lttng_ht_add_unique_u64(app->sessions, &ua_sess->node);
2038
2039 DBG2("UST app session created successfully with handle %d", ret);
2040 }
2041
2042 *ua_sess_ptr = ua_sess;
2043 if (is_created) {
2044 *is_created = created;
2045 }
2046
2047 /* Everything went well. */
2048 ret = 0;
2049
2050 error:
2051 health_code_update();
2052 return ret;
2053 }
2054
2055 /*
2056 * Match function for a hash table lookup of ust_app_ctx.
2057 *
2058 * It matches an ust app context based on the context type and, in the case
2059 * of perf counters, their name.
2060 */
2061 static int ht_match_ust_app_ctx(struct cds_lfht_node *node, const void *_key)
2062 {
2063 struct ust_app_ctx *ctx;
2064 const struct lttng_ust_context *key;
2065
2066 assert(node);
2067 assert(_key);
2068
2069 ctx = caa_container_of(node, struct ust_app_ctx, node.node);
2070 key = _key;
2071
2072 /* Context type */
2073 if (ctx->ctx.ctx != key->ctx) {
2074 goto no_match;
2075 }
2076
2077 /* Check the name in the case of perf thread counters. */
2078 if (key->ctx == LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER) {
2079 if (strncmp(key->u.perf_counter.name,
2080 ctx->ctx.u.perf_counter.name,
2081 sizeof(key->u.perf_counter.name))) {
2082 goto no_match;
2083 }
2084 }
2085
2086 /* Match. */
2087 return 1;
2088
2089 no_match:
2090 return 0;
2091 }
2092
2093 /*
2094 * Lookup for an ust app context from an lttng_ust_context.
2095 *
2096 * Must be called while holding RCU read side lock.
2097 * Return an ust_app_ctx object or NULL on error.
2098 */
2099 static
2100 struct ust_app_ctx *find_ust_app_context(struct lttng_ht *ht,
2101 struct lttng_ust_context *uctx)
2102 {
2103 struct lttng_ht_iter iter;
2104 struct lttng_ht_node_ulong *node;
2105 struct ust_app_ctx *app_ctx = NULL;
2106
2107 assert(uctx);
2108 assert(ht);
2109
2110 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2111 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) uctx->ctx, lttng_ht_seed),
2112 ht_match_ust_app_ctx, uctx, &iter.iter);
2113 node = lttng_ht_iter_get_node_ulong(&iter);
2114 if (!node) {
2115 goto end;
2116 }
2117
2118 app_ctx = caa_container_of(node, struct ust_app_ctx, node);
2119
2120 end:
2121 return app_ctx;
2122 }
2123
2124 /*
2125 * Create a context for the channel on the tracer.
2126 *
2127 * Called with UST app session lock held and a RCU read side lock.
2128 */
2129 static
2130 int create_ust_app_channel_context(struct ust_app_session *ua_sess,
2131 struct ust_app_channel *ua_chan, struct lttng_ust_context *uctx,
2132 struct ust_app *app)
2133 {
2134 int ret = 0;
2135 struct ust_app_ctx *ua_ctx;
2136
2137 DBG2("UST app adding context to channel %s", ua_chan->name);
2138
2139 ua_ctx = find_ust_app_context(ua_chan->ctx, uctx);
2140 if (ua_ctx) {
2141 ret = -EEXIST;
2142 goto error;
2143 }
2144
2145 ua_ctx = alloc_ust_app_ctx(uctx);
2146 if (ua_ctx == NULL) {
2147 /* malloc failed */
2148 ret = -1;
2149 goto error;
2150 }
2151
2152 lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx);
2153 lttng_ht_add_ulong(ua_chan->ctx, &ua_ctx->node);
2154 cds_list_add_tail(&ua_ctx->list, &ua_chan->ctx_list);
2155
2156 ret = create_ust_channel_context(ua_chan, ua_ctx, app);
2157 if (ret < 0) {
2158 goto error;
2159 }
2160
2161 error:
2162 return ret;
2163 }
2164
2165 /*
2166 * Enable on the tracer side a ust app event for the session and channel.
2167 *
2168 * Called with UST app session lock held.
2169 */
2170 static
2171 int enable_ust_app_event(struct ust_app_session *ua_sess,
2172 struct ust_app_event *ua_event, struct ust_app *app)
2173 {
2174 int ret;
2175
2176 ret = enable_ust_event(app, ua_sess, ua_event);
2177 if (ret < 0) {
2178 goto error;
2179 }
2180
2181 ua_event->enabled = 1;
2182
2183 error:
2184 return ret;
2185 }
2186
2187 /*
2188 * Disable on the tracer side a ust app event for the session and channel.
2189 */
2190 static int disable_ust_app_event(struct ust_app_session *ua_sess,
2191 struct ust_app_event *ua_event, struct ust_app *app)
2192 {
2193 int ret;
2194
2195 ret = disable_ust_event(app, ua_sess, ua_event);
2196 if (ret < 0) {
2197 goto error;
2198 }
2199
2200 ua_event->enabled = 0;
2201
2202 error:
2203 return ret;
2204 }
2205
2206 /*
2207 * Lookup ust app channel for session and disable it on the tracer side.
2208 */
2209 static
2210 int disable_ust_app_channel(struct ust_app_session *ua_sess,
2211 struct ust_app_channel *ua_chan, struct ust_app *app)
2212 {
2213 int ret;
2214
2215 ret = disable_ust_channel(app, ua_sess, ua_chan);
2216 if (ret < 0) {
2217 goto error;
2218 }
2219
2220 ua_chan->enabled = 0;
2221
2222 error:
2223 return ret;
2224 }
2225
2226 /*
2227 * Lookup ust app channel for session and enable it on the tracer side. This
2228 * MUST be called with a RCU read side lock acquired.
2229 */
2230 static int enable_ust_app_channel(struct ust_app_session *ua_sess,
2231 struct ltt_ust_channel *uchan, struct ust_app *app)
2232 {
2233 int ret = 0;
2234 struct lttng_ht_iter iter;
2235 struct lttng_ht_node_str *ua_chan_node;
2236 struct ust_app_channel *ua_chan;
2237
2238 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
2239 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
2240 if (ua_chan_node == NULL) {
2241 DBG2("Unable to find channel %s in ust session id %" PRIu64,
2242 uchan->name, ua_sess->tracing_id);
2243 goto error;
2244 }
2245
2246 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2247
2248 ret = enable_ust_channel(app, ua_sess, ua_chan);
2249 if (ret < 0) {
2250 goto error;
2251 }
2252
2253 error:
2254 return ret;
2255 }
2256
2257 /*
2258 * Ask the consumer to create a channel and get it if successful.
2259 *
2260 * Return 0 on success or else a negative value.
2261 */
2262 static int do_consumer_create_channel(struct ltt_ust_session *usess,
2263 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan,
2264 int bitness, struct ust_registry_session *registry)
2265 {
2266 int ret;
2267 unsigned int nb_fd = 0;
2268 struct consumer_socket *socket;
2269
2270 assert(usess);
2271 assert(ua_sess);
2272 assert(ua_chan);
2273 assert(registry);
2274
2275 rcu_read_lock();
2276 health_code_update();
2277
2278 /* Get the right consumer socket for the application. */
2279 socket = consumer_find_socket_by_bitness(bitness, usess->consumer);
2280 if (!socket) {
2281 ret = -EINVAL;
2282 goto error;
2283 }
2284
2285 health_code_update();
2286
2287 /* Need one fd for the channel. */
2288 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
2289 if (ret < 0) {
2290 ERR("Exhausted number of available FD upon create channel");
2291 goto error;
2292 }
2293
2294 /*
2295 * Ask consumer to create channel. The consumer will return the number of
2296 * stream we have to expect.
2297 */
2298 ret = ust_consumer_ask_channel(ua_sess, ua_chan, usess->consumer, socket,
2299 registry);
2300 if (ret < 0) {
2301 goto error_ask;
2302 }
2303
2304 /*
2305 * Compute the number of fd needed before receiving them. It must be 2 per
2306 * stream (2 being the default value here).
2307 */
2308 nb_fd = DEFAULT_UST_STREAM_FD_NUM * ua_chan->expected_stream_count;
2309
2310 /* Reserve the amount of file descriptor we need. */
2311 ret = lttng_fd_get(LTTNG_FD_APPS, nb_fd);
2312 if (ret < 0) {
2313 ERR("Exhausted number of available FD upon create channel");
2314 goto error_fd_get_stream;
2315 }
2316
2317 health_code_update();
2318
2319 /*
2320 * Now get the channel from the consumer. This call wil populate the stream
2321 * list of that channel and set the ust objects.
2322 */
2323 if (usess->consumer->enabled) {
2324 ret = ust_consumer_get_channel(socket, ua_chan);
2325 if (ret < 0) {
2326 goto error_destroy;
2327 }
2328 }
2329
2330 rcu_read_unlock();
2331 return 0;
2332
2333 error_destroy:
2334 lttng_fd_put(LTTNG_FD_APPS, nb_fd);
2335 error_fd_get_stream:
2336 /*
2337 * Initiate a destroy channel on the consumer since we had an error
2338 * handling it on our side. The return value is of no importance since we
2339 * already have a ret value set by the previous error that we need to
2340 * return.
2341 */
2342 (void) ust_consumer_destroy_channel(socket, ua_chan);
2343 error_ask:
2344 lttng_fd_put(LTTNG_FD_APPS, 1);
2345 error:
2346 health_code_update();
2347 rcu_read_unlock();
2348 return ret;
2349 }
2350
2351 /*
2352 * Duplicate the ust data object of the ust app stream and save it in the
2353 * buffer registry stream.
2354 *
2355 * Return 0 on success or else a negative value.
2356 */
2357 static int duplicate_stream_object(struct buffer_reg_stream *reg_stream,
2358 struct ust_app_stream *stream)
2359 {
2360 int ret;
2361
2362 assert(reg_stream);
2363 assert(stream);
2364
2365 /* Reserve the amount of file descriptor we need. */
2366 ret = lttng_fd_get(LTTNG_FD_APPS, 2);
2367 if (ret < 0) {
2368 ERR("Exhausted number of available FD upon duplicate stream");
2369 goto error;
2370 }
2371
2372 /* Duplicate object for stream once the original is in the registry. */
2373 ret = ustctl_duplicate_ust_object_data(&stream->obj,
2374 reg_stream->obj.ust);
2375 if (ret < 0) {
2376 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2377 reg_stream->obj.ust, stream->obj, ret);
2378 lttng_fd_put(LTTNG_FD_APPS, 2);
2379 goto error;
2380 }
2381 stream->handle = stream->obj->handle;
2382
2383 error:
2384 return ret;
2385 }
2386
2387 /*
2388 * Duplicate the ust data object of the ust app. channel and save it in the
2389 * buffer registry channel.
2390 *
2391 * Return 0 on success or else a negative value.
2392 */
2393 static int duplicate_channel_object(struct buffer_reg_channel *reg_chan,
2394 struct ust_app_channel *ua_chan)
2395 {
2396 int ret;
2397
2398 assert(reg_chan);
2399 assert(ua_chan);
2400
2401 /* Need two fds for the channel. */
2402 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
2403 if (ret < 0) {
2404 ERR("Exhausted number of available FD upon duplicate channel");
2405 goto error_fd_get;
2406 }
2407
2408 /* Duplicate object for stream once the original is in the registry. */
2409 ret = ustctl_duplicate_ust_object_data(&ua_chan->obj, reg_chan->obj.ust);
2410 if (ret < 0) {
2411 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2412 reg_chan->obj.ust, ua_chan->obj, ret);
2413 goto error;
2414 }
2415 ua_chan->handle = ua_chan->obj->handle;
2416
2417 return 0;
2418
2419 error:
2420 lttng_fd_put(LTTNG_FD_APPS, 1);
2421 error_fd_get:
2422 return ret;
2423 }
2424
2425 /*
2426 * For a given channel buffer registry, setup all streams of the given ust
2427 * application channel.
2428 *
2429 * Return 0 on success or else a negative value.
2430 */
2431 static int setup_buffer_reg_streams(struct buffer_reg_channel *reg_chan,
2432 struct ust_app_channel *ua_chan)
2433 {
2434 int ret = 0;
2435 struct ust_app_stream *stream, *stmp;
2436
2437 assert(reg_chan);
2438 assert(ua_chan);
2439
2440 DBG2("UST app setup buffer registry stream");
2441
2442 /* Send all streams to application. */
2443 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
2444 struct buffer_reg_stream *reg_stream;
2445
2446 ret = buffer_reg_stream_create(&reg_stream);
2447 if (ret < 0) {
2448 goto error;
2449 }
2450
2451 /*
2452 * Keep original pointer and nullify it in the stream so the delete
2453 * stream call does not release the object.
2454 */
2455 reg_stream->obj.ust = stream->obj;
2456 stream->obj = NULL;
2457 buffer_reg_stream_add(reg_stream, reg_chan);
2458
2459 /* We don't need the streams anymore. */
2460 cds_list_del(&stream->list);
2461 delete_ust_app_stream(-1, stream);
2462 }
2463
2464 error:
2465 return ret;
2466 }
2467
2468 /*
2469 * Create a buffer registry channel for the given session registry and
2470 * application channel object. If regp pointer is valid, it's set with the
2471 * created object. Important, the created object is NOT added to the session
2472 * registry hash table.
2473 *
2474 * Return 0 on success else a negative value.
2475 */
2476 static int create_buffer_reg_channel(struct buffer_reg_session *reg_sess,
2477 struct ust_app_channel *ua_chan, struct buffer_reg_channel **regp)
2478 {
2479 int ret;
2480 struct buffer_reg_channel *reg_chan = NULL;
2481
2482 assert(reg_sess);
2483 assert(ua_chan);
2484
2485 DBG2("UST app creating buffer registry channel for %s", ua_chan->name);
2486
2487 /* Create buffer registry channel. */
2488 ret = buffer_reg_channel_create(ua_chan->tracing_channel_id, &reg_chan);
2489 if (ret < 0) {
2490 goto error_create;
2491 }
2492 assert(reg_chan);
2493 reg_chan->consumer_key = ua_chan->key;
2494 reg_chan->subbuf_size = ua_chan->attr.subbuf_size;
2495 reg_chan->num_subbuf = ua_chan->attr.num_subbuf;
2496
2497 /* Create and add a channel registry to session. */
2498 ret = ust_registry_channel_add(reg_sess->reg.ust,
2499 ua_chan->tracing_channel_id);
2500 if (ret < 0) {
2501 goto error;
2502 }
2503 buffer_reg_channel_add(reg_sess, reg_chan);
2504
2505 if (regp) {
2506 *regp = reg_chan;
2507 }
2508
2509 return 0;
2510
2511 error:
2512 /* Safe because the registry channel object was not added to any HT. */
2513 buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST);
2514 error_create:
2515 return ret;
2516 }
2517
2518 /*
2519 * Setup buffer registry channel for the given session registry and application
2520 * channel object. If regp pointer is valid, it's set with the created object.
2521 *
2522 * Return 0 on success else a negative value.
2523 */
2524 static int setup_buffer_reg_channel(struct buffer_reg_session *reg_sess,
2525 struct ust_app_channel *ua_chan, struct buffer_reg_channel *reg_chan)
2526 {
2527 int ret;
2528
2529 assert(reg_sess);
2530 assert(reg_chan);
2531 assert(ua_chan);
2532 assert(ua_chan->obj);
2533
2534 DBG2("UST app setup buffer registry channel for %s", ua_chan->name);
2535
2536 /* Setup all streams for the registry. */
2537 ret = setup_buffer_reg_streams(reg_chan, ua_chan);
2538 if (ret < 0) {
2539 goto error;
2540 }
2541
2542 reg_chan->obj.ust = ua_chan->obj;
2543 ua_chan->obj = NULL;
2544
2545 return 0;
2546
2547 error:
2548 buffer_reg_channel_remove(reg_sess, reg_chan);
2549 buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST);
2550 return ret;
2551 }
2552
2553 /*
2554 * Send buffer registry channel to the application.
2555 *
2556 * Return 0 on success else a negative value.
2557 */
2558 static int send_channel_uid_to_ust(struct buffer_reg_channel *reg_chan,
2559 struct ust_app *app, struct ust_app_session *ua_sess,
2560 struct ust_app_channel *ua_chan)
2561 {
2562 int ret;
2563 struct buffer_reg_stream *reg_stream;
2564
2565 assert(reg_chan);
2566 assert(app);
2567 assert(ua_sess);
2568 assert(ua_chan);
2569
2570 DBG("UST app sending buffer registry channel to ust sock %d", app->sock);
2571
2572 ret = duplicate_channel_object(reg_chan, ua_chan);
2573 if (ret < 0) {
2574 goto error;
2575 }
2576
2577 /* Send channel to the application. */
2578 ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan);
2579 if (ret < 0) {
2580 goto error;
2581 }
2582
2583 health_code_update();
2584
2585 /* Send all streams to application. */
2586 pthread_mutex_lock(&reg_chan->stream_list_lock);
2587 cds_list_for_each_entry(reg_stream, &reg_chan->streams, lnode) {
2588 struct ust_app_stream stream;
2589
2590 ret = duplicate_stream_object(reg_stream, &stream);
2591 if (ret < 0) {
2592 goto error_stream_unlock;
2593 }
2594
2595 ret = ust_consumer_send_stream_to_ust(app, ua_chan, &stream);
2596 if (ret < 0) {
2597 (void) release_ust_app_stream(-1, &stream);
2598 goto error_stream_unlock;
2599 }
2600
2601 /*
2602 * The return value is not important here. This function will output an
2603 * error if needed.
2604 */
2605 (void) release_ust_app_stream(-1, &stream);
2606 }
2607 ua_chan->is_sent = 1;
2608
2609 error_stream_unlock:
2610 pthread_mutex_unlock(&reg_chan->stream_list_lock);
2611 error:
2612 return ret;
2613 }
2614
2615 /*
2616 * Create and send to the application the created buffers with per UID buffers.
2617 *
2618 * Return 0 on success else a negative value.
2619 */
2620 static int create_channel_per_uid(struct ust_app *app,
2621 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
2622 struct ust_app_channel *ua_chan)
2623 {
2624 int ret;
2625 struct buffer_reg_uid *reg_uid;
2626 struct buffer_reg_channel *reg_chan;
2627
2628 assert(app);
2629 assert(usess);
2630 assert(ua_sess);
2631 assert(ua_chan);
2632
2633 DBG("UST app creating channel %s with per UID buffers", ua_chan->name);
2634
2635 reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid);
2636 /*
2637 * The session creation handles the creation of this global registry
2638 * object. If none can be find, there is a code flow problem or a
2639 * teardown race.
2640 */
2641 assert(reg_uid);
2642
2643 reg_chan = buffer_reg_channel_find(ua_chan->tracing_channel_id,
2644 reg_uid);
2645 if (!reg_chan) {
2646 /* Create the buffer registry channel object. */
2647 ret = create_buffer_reg_channel(reg_uid->registry, ua_chan, &reg_chan);
2648 if (ret < 0) {
2649 ERR("Error creating the UST channel \"%s\" registry instance",
2650 ua_chan->name);
2651 goto error;
2652 }
2653 assert(reg_chan);
2654
2655 /*
2656 * Create the buffers on the consumer side. This call populates the
2657 * ust app channel object with all streams and data object.
2658 */
2659 ret = do_consumer_create_channel(usess, ua_sess, ua_chan,
2660 app->bits_per_long, reg_uid->registry->reg.ust);
2661 if (ret < 0) {
2662 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2663 ua_chan->name);
2664
2665 /*
2666 * Let's remove the previously created buffer registry channel so
2667 * it's not visible anymore in the session registry.
2668 */
2669 ust_registry_channel_del_free(reg_uid->registry->reg.ust,
2670 ua_chan->tracing_channel_id);
2671 buffer_reg_channel_remove(reg_uid->registry, reg_chan);
2672 buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST);
2673 goto error;
2674 }
2675
2676 /*
2677 * Setup the streams and add it to the session registry.
2678 */
2679 ret = setup_buffer_reg_channel(reg_uid->registry, ua_chan, reg_chan);
2680 if (ret < 0) {
2681 ERR("Error setting up UST channel \"%s\"",
2682 ua_chan->name);
2683 goto error;
2684 }
2685
2686 }
2687
2688 /* Send buffers to the application. */
2689 ret = send_channel_uid_to_ust(reg_chan, app, ua_sess, ua_chan);
2690 if (ret < 0) {
2691 /*
2692 * Don't report error to the console, since it may be
2693 * caused by application concurrently exiting.
2694 */
2695 goto error;
2696 }
2697
2698 error:
2699 return ret;
2700 }
2701
2702 /*
2703 * Create and send to the application the created buffers with per PID buffers.
2704 *
2705 * Return 0 on success else a negative value.
2706 */
2707 static int create_channel_per_pid(struct ust_app *app,
2708 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
2709 struct ust_app_channel *ua_chan)
2710 {
2711 int ret;
2712 struct ust_registry_session *registry;
2713
2714 assert(app);
2715 assert(usess);
2716 assert(ua_sess);
2717 assert(ua_chan);
2718
2719 DBG("UST app creating channel %s with per PID buffers", ua_chan->name);
2720
2721 rcu_read_lock();
2722
2723 registry = get_session_registry(ua_sess);
2724 assert(registry);
2725
2726 /* Create and add a new channel registry to session. */
2727 ret = ust_registry_channel_add(registry, ua_chan->key);
2728 if (ret < 0) {
2729 ERR("Error creating the UST channel \"%s\" registry instance",
2730 ua_chan->name);
2731 goto error;
2732 }
2733
2734 /* Create and get channel on the consumer side. */
2735 ret = do_consumer_create_channel(usess, ua_sess, ua_chan,
2736 app->bits_per_long, registry);
2737 if (ret < 0) {
2738 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2739 ua_chan->name);
2740 goto error;
2741 }
2742
2743 ret = send_channel_pid_to_ust(app, ua_sess, ua_chan);
2744 if (ret < 0) {
2745 /*
2746 * Don't report error to the console, since it may be
2747 * caused by application concurrently exiting.
2748 */
2749 goto error;
2750 }
2751
2752 error:
2753 rcu_read_unlock();
2754 return ret;
2755 }
2756
2757 /*
2758 * From an already allocated ust app channel, create the channel buffers if
2759 * need and send it to the application. This MUST be called with a RCU read
2760 * side lock acquired.
2761 *
2762 * Return 0 on success or else a negative value.
2763 */
2764 static int do_create_channel(struct ust_app *app,
2765 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
2766 struct ust_app_channel *ua_chan)
2767 {
2768 int ret;
2769
2770 assert(app);
2771 assert(usess);
2772 assert(ua_sess);
2773 assert(ua_chan);
2774
2775 /* Handle buffer type before sending the channel to the application. */
2776 switch (usess->buffer_type) {
2777 case LTTNG_BUFFER_PER_UID:
2778 {
2779 ret = create_channel_per_uid(app, usess, ua_sess, ua_chan);
2780 if (ret < 0) {
2781 goto error;
2782 }
2783 break;
2784 }
2785 case LTTNG_BUFFER_PER_PID:
2786 {
2787 ret = create_channel_per_pid(app, usess, ua_sess, ua_chan);
2788 if (ret < 0) {
2789 goto error;
2790 }
2791 break;
2792 }
2793 default:
2794 assert(0);
2795 ret = -EINVAL;
2796 goto error;
2797 }
2798
2799 /* Initialize ust objd object using the received handle and add it. */
2800 lttng_ht_node_init_ulong(&ua_chan->ust_objd_node, ua_chan->handle);
2801 lttng_ht_add_unique_ulong(app->ust_objd, &ua_chan->ust_objd_node);
2802
2803 /* If channel is not enabled, disable it on the tracer */
2804 if (!ua_chan->enabled) {
2805 ret = disable_ust_channel(app, ua_sess, ua_chan);
2806 if (ret < 0) {
2807 goto error;
2808 }
2809 }
2810
2811 error:
2812 return ret;
2813 }
2814
2815 /*
2816 * Create UST app channel and create it on the tracer. Set ua_chanp of the
2817 * newly created channel if not NULL.
2818 *
2819 * Called with UST app session lock and RCU read-side lock held.
2820 *
2821 * Return 0 on success or else a negative value.
2822 */
2823 static int create_ust_app_channel(struct ust_app_session *ua_sess,
2824 struct ltt_ust_channel *uchan, struct ust_app *app,
2825 enum lttng_ust_chan_type type, struct ltt_ust_session *usess,
2826 struct ust_app_channel **ua_chanp)
2827 {
2828 int ret = 0;
2829 struct lttng_ht_iter iter;
2830 struct lttng_ht_node_str *ua_chan_node;
2831 struct ust_app_channel *ua_chan;
2832
2833 /* Lookup channel in the ust app session */
2834 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
2835 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
2836 if (ua_chan_node != NULL) {
2837 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2838 goto end;
2839 }
2840
2841 ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr);
2842 if (ua_chan == NULL) {
2843 /* Only malloc can fail here */
2844 ret = -ENOMEM;
2845 goto error_alloc;
2846 }
2847 shadow_copy_channel(ua_chan, uchan);
2848
2849 /* Set channel type. */
2850 ua_chan->attr.type = type;
2851
2852 ret = do_create_channel(app, usess, ua_sess, ua_chan);
2853 if (ret < 0) {
2854 goto error;
2855 }
2856
2857 DBG2("UST app create channel %s for PID %d completed", ua_chan->name,
2858 app->pid);
2859
2860 /* Only add the channel if successful on the tracer side. */
2861 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
2862
2863 end:
2864 if (ua_chanp) {
2865 *ua_chanp = ua_chan;
2866 }
2867
2868 /* Everything went well. */
2869 return 0;
2870
2871 error:
2872 delete_ust_app_channel(ua_chan->is_sent ? app->sock : -1, ua_chan, app);
2873 error_alloc:
2874 return ret;
2875 }
2876
2877 /*
2878 * Create UST app event and create it on the tracer side.
2879 *
2880 * Called with ust app session mutex held.
2881 */
2882 static
2883 int create_ust_app_event(struct ust_app_session *ua_sess,
2884 struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent,
2885 struct ust_app *app)
2886 {
2887 int ret = 0;
2888 struct ust_app_event *ua_event;
2889
2890 /* Get event node */
2891 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
2892 uevent->filter, uevent->attr.loglevel, uevent->exclusion);
2893 if (ua_event != NULL) {
2894 ret = -EEXIST;
2895 goto end;
2896 }
2897
2898 /* Does not exist so create one */
2899 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
2900 if (ua_event == NULL) {
2901 /* Only malloc can failed so something is really wrong */
2902 ret = -ENOMEM;
2903 goto end;
2904 }
2905 shadow_copy_event(ua_event, uevent);
2906
2907 /* Create it on the tracer side */
2908 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
2909 if (ret < 0) {
2910 /* Not found previously means that it does not exist on the tracer */
2911 assert(ret != -LTTNG_UST_ERR_EXIST);
2912 goto error;
2913 }
2914
2915 add_unique_ust_app_event(ua_chan, ua_event);
2916
2917 DBG2("UST app create event %s for PID %d completed", ua_event->name,
2918 app->pid);
2919
2920 end:
2921 return ret;
2922
2923 error:
2924 /* Valid. Calling here is already in a read side lock */
2925 delete_ust_app_event(-1, ua_event);
2926 return ret;
2927 }
2928
2929 /*
2930 * Create UST metadata and open it on the tracer side.
2931 *
2932 * Called with UST app session lock held and RCU read side lock.
2933 */
2934 static int create_ust_app_metadata(struct ust_app_session *ua_sess,
2935 struct ust_app *app, struct consumer_output *consumer)
2936 {
2937 int ret = 0;
2938 struct ust_app_channel *metadata;
2939 struct consumer_socket *socket;
2940 struct ust_registry_session *registry;
2941
2942 assert(ua_sess);
2943 assert(app);
2944 assert(consumer);
2945
2946 registry = get_session_registry(ua_sess);
2947 assert(registry);
2948
2949 pthread_mutex_lock(&registry->lock);
2950
2951 /* Metadata already exists for this registry or it was closed previously */
2952 if (registry->metadata_key || registry->metadata_closed) {
2953 ret = 0;
2954 goto error;
2955 }
2956
2957 /* Allocate UST metadata */
2958 metadata = alloc_ust_app_channel(DEFAULT_METADATA_NAME, ua_sess, NULL);
2959 if (!metadata) {
2960 /* malloc() failed */
2961 ret = -ENOMEM;
2962 goto error;
2963 }
2964
2965 memcpy(&metadata->attr, &ua_sess->metadata_attr, sizeof(metadata->attr));
2966
2967 /* Need one fd for the channel. */
2968 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
2969 if (ret < 0) {
2970 ERR("Exhausted number of available FD upon create metadata");
2971 goto error;
2972 }
2973
2974 /* Get the right consumer socket for the application. */
2975 socket = consumer_find_socket_by_bitness(app->bits_per_long, consumer);
2976 if (!socket) {
2977 ret = -EINVAL;
2978 goto error_consumer;
2979 }
2980
2981 /*
2982 * Keep metadata key so we can identify it on the consumer side. Assign it
2983 * to the registry *before* we ask the consumer so we avoid the race of the
2984 * consumer requesting the metadata and the ask_channel call on our side
2985 * did not returned yet.
2986 */
2987 registry->metadata_key = metadata->key;
2988
2989 /*
2990 * Ask the metadata channel creation to the consumer. The metadata object
2991 * will be created by the consumer and kept their. However, the stream is
2992 * never added or monitored until we do a first push metadata to the
2993 * consumer.
2994 */
2995 ret = ust_consumer_ask_channel(ua_sess, metadata, consumer, socket,
2996 registry);
2997 if (ret < 0) {
2998 /* Nullify the metadata key so we don't try to close it later on. */
2999 registry->metadata_key = 0;
3000 goto error_consumer;
3001 }
3002
3003 /*
3004 * The setup command will make the metadata stream be sent to the relayd,
3005 * if applicable, and the thread managing the metadatas. This is important
3006 * because after this point, if an error occurs, the only way the stream
3007 * can be deleted is to be monitored in the consumer.
3008 */
3009 ret = consumer_setup_metadata(socket, metadata->key);
3010 if (ret < 0) {
3011 /* Nullify the metadata key so we don't try to close it later on. */
3012 registry->metadata_key = 0;
3013 goto error_consumer;
3014 }
3015
3016 DBG2("UST metadata with key %" PRIu64 " created for app pid %d",
3017 metadata->key, app->pid);
3018
3019 error_consumer:
3020 lttng_fd_put(LTTNG_FD_APPS, 1);
3021 delete_ust_app_channel(-1, metadata, app);
3022 error:
3023 pthread_mutex_unlock(&registry->lock);
3024 return ret;
3025 }
3026
3027 /*
3028 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3029 * acquired before calling this function.
3030 */
3031 struct ust_app *ust_app_find_by_pid(pid_t pid)
3032 {
3033 struct ust_app *app = NULL;
3034 struct lttng_ht_node_ulong *node;
3035 struct lttng_ht_iter iter;
3036
3037 lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) pid), &iter);
3038 node = lttng_ht_iter_get_node_ulong(&iter);
3039 if (node == NULL) {
3040 DBG2("UST app no found with pid %d", pid);
3041 goto error;
3042 }
3043
3044 DBG2("Found UST app by pid %d", pid);
3045
3046 app = caa_container_of(node, struct ust_app, pid_n);
3047
3048 error:
3049 return app;
3050 }
3051
3052 /*
3053 * Allocate and init an UST app object using the registration information and
3054 * the command socket. This is called when the command socket connects to the
3055 * session daemon.
3056 *
3057 * The object is returned on success or else NULL.
3058 */
3059 struct ust_app *ust_app_create(struct ust_register_msg *msg, int sock)
3060 {
3061 struct ust_app *lta = NULL;
3062
3063 assert(msg);
3064 assert(sock >= 0);
3065
3066 DBG3("UST app creating application for socket %d", sock);
3067
3068 if ((msg->bits_per_long == 64 &&
3069 (uatomic_read(&ust_consumerd64_fd) == -EINVAL))
3070 || (msg->bits_per_long == 32 &&
3071 (uatomic_read(&ust_consumerd32_fd) == -EINVAL))) {
3072 ERR("Registration failed: application \"%s\" (pid: %d) has "
3073 "%d-bit long, but no consumerd for this size is available.\n",
3074 msg->name, msg->pid, msg->bits_per_long);
3075 goto error;
3076 }
3077
3078 lta = zmalloc(sizeof(struct ust_app));
3079 if (lta == NULL) {
3080 PERROR("malloc");
3081 goto error;
3082 }
3083
3084 lta->ppid = msg->ppid;
3085 lta->uid = msg->uid;
3086 lta->gid = msg->gid;
3087
3088 lta->bits_per_long = msg->bits_per_long;
3089 lta->uint8_t_alignment = msg->uint8_t_alignment;
3090 lta->uint16_t_alignment = msg->uint16_t_alignment;
3091 lta->uint32_t_alignment = msg->uint32_t_alignment;
3092 lta->uint64_t_alignment = msg->uint64_t_alignment;
3093 lta->long_alignment = msg->long_alignment;
3094 lta->byte_order = msg->byte_order;
3095
3096 lta->v_major = msg->major;
3097 lta->v_minor = msg->minor;
3098 lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3099 lta->ust_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
3100 lta->notify_sock = -1;
3101
3102 /* Copy name and make sure it's NULL terminated. */
3103 strncpy(lta->name, msg->name, sizeof(lta->name));
3104 lta->name[UST_APP_PROCNAME_LEN] = '\0';
3105
3106 /*
3107 * Before this can be called, when receiving the registration information,
3108 * the application compatibility is checked. So, at this point, the
3109 * application can work with this session daemon.
3110 */
3111 lta->compatible = 1;
3112
3113 lta->pid = msg->pid;
3114 lttng_ht_node_init_ulong(&lta->pid_n, (unsigned long) lta->pid);
3115 lta->sock = sock;
3116 lttng_ht_node_init_ulong(&lta->sock_n, (unsigned long) lta->sock);
3117
3118 CDS_INIT_LIST_HEAD(&lta->teardown_head);
3119
3120 error:
3121 return lta;
3122 }
3123
3124 /*
3125 * For a given application object, add it to every hash table.
3126 */
3127 void ust_app_add(struct ust_app *app)
3128 {
3129 assert(app);
3130 assert(app->notify_sock >= 0);
3131
3132 rcu_read_lock();
3133
3134 /*
3135 * On a re-registration, we want to kick out the previous registration of
3136 * that pid
3137 */
3138 lttng_ht_add_replace_ulong(ust_app_ht, &app->pid_n);
3139
3140 /*
3141 * The socket _should_ be unique until _we_ call close. So, a add_unique
3142 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3143 * already in the table.
3144 */
3145 lttng_ht_add_unique_ulong(ust_app_ht_by_sock, &app->sock_n);
3146
3147 /* Add application to the notify socket hash table. */
3148 lttng_ht_node_init_ulong(&app->notify_sock_n, app->notify_sock);
3149 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock, &app->notify_sock_n);
3150
3151 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3152 "notify_sock:%d (version %d.%d)", app->pid, app->ppid, app->uid,
3153 app->gid, app->sock, app->name, app->notify_sock, app->v_major,
3154 app->v_minor);
3155
3156 rcu_read_unlock();
3157 }
3158
3159 /*
3160 * Set the application version into the object.
3161 *
3162 * Return 0 on success else a negative value either an errno code or a
3163 * LTTng-UST error code.
3164 */
3165 int ust_app_version(struct ust_app *app)
3166 {
3167 int ret;
3168
3169 assert(app);
3170
3171 ret = ustctl_tracer_version(app->sock, &app->version);
3172 if (ret < 0) {
3173 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
3174 ERR("UST app %d version failed with ret %d", app->sock, ret);
3175 } else {
3176 DBG3("UST app %d version failed. Application is dead", app->sock);
3177 }
3178 }
3179
3180 return ret;
3181 }
3182
3183 /*
3184 * Unregister app by removing it from the global traceable app list and freeing
3185 * the data struct.
3186 *
3187 * The socket is already closed at this point so no close to sock.
3188 */
3189 void ust_app_unregister(int sock)
3190 {
3191 struct ust_app *lta;
3192 struct lttng_ht_node_ulong *node;
3193 struct lttng_ht_iter ust_app_sock_iter;
3194 struct lttng_ht_iter iter;
3195 struct ust_app_session *ua_sess;
3196 int ret;
3197
3198 rcu_read_lock();
3199
3200 /* Get the node reference for a call_rcu */
3201 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &ust_app_sock_iter);
3202 node = lttng_ht_iter_get_node_ulong(&ust_app_sock_iter);
3203 assert(node);
3204
3205 lta = caa_container_of(node, struct ust_app, sock_n);
3206 DBG("PID %d unregistering with sock %d", lta->pid, sock);
3207
3208 /*
3209 * For per-PID buffers, perform "push metadata" and flush all
3210 * application streams before removing app from hash tables,
3211 * ensuring proper behavior of data_pending check.
3212 * Remove sessions so they are not visible during deletion.
3213 */
3214 cds_lfht_for_each_entry(lta->sessions->ht, &iter.iter, ua_sess,
3215 node.node) {
3216 struct ust_registry_session *registry;
3217
3218 ret = lttng_ht_del(lta->sessions, &iter);
3219 if (ret) {
3220 /* The session was already removed so scheduled for teardown. */
3221 continue;
3222 }
3223
3224 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) {
3225 (void) ust_app_flush_app_session(lta, ua_sess);
3226 }
3227
3228 /*
3229 * Add session to list for teardown. This is safe since at this point we
3230 * are the only one using this list.
3231 */
3232 pthread_mutex_lock(&ua_sess->lock);
3233
3234 if (ua_sess->deleted) {
3235 pthread_mutex_unlock(&ua_sess->lock);
3236 continue;
3237 }
3238
3239 /*
3240 * Normally, this is done in the delete session process which is
3241 * executed in the call rcu below. However, upon registration we can't
3242 * afford to wait for the grace period before pushing data or else the
3243 * data pending feature can race between the unregistration and stop
3244 * command where the data pending command is sent *before* the grace
3245 * period ended.
3246 *
3247 * The close metadata below nullifies the metadata pointer in the
3248 * session so the delete session will NOT push/close a second time.
3249 */
3250 registry = get_session_registry(ua_sess);
3251 if (registry) {
3252 /* Push metadata for application before freeing the application. */
3253 (void) push_metadata(registry, ua_sess->consumer);
3254
3255 /*
3256 * Don't ask to close metadata for global per UID buffers. Close
3257 * metadata only on destroy trace session in this case. Also, the
3258 * previous push metadata could have flag the metadata registry to
3259 * close so don't send a close command if closed.
3260 */
3261 if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) {
3262 /* And ask to close it for this session registry. */
3263 (void) close_metadata(registry, ua_sess->consumer);
3264 }
3265 }
3266 cds_list_add(&ua_sess->teardown_node, &lta->teardown_head);
3267
3268 pthread_mutex_unlock(&ua_sess->lock);
3269 }
3270
3271 /* Remove application from PID hash table */
3272 ret = lttng_ht_del(ust_app_ht_by_sock, &ust_app_sock_iter);
3273 assert(!ret);
3274
3275 /*
3276 * Remove application from notify hash table. The thread handling the
3277 * notify socket could have deleted the node so ignore on error because
3278 * either way it's valid. The close of that socket is handled by the other
3279 * thread.
3280 */
3281 iter.iter.node = &lta->notify_sock_n.node;
3282 (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter);
3283
3284 /*
3285 * Ignore return value since the node might have been removed before by an
3286 * add replace during app registration because the PID can be reassigned by
3287 * the OS.
3288 */
3289 iter.iter.node = &lta->pid_n.node;
3290 ret = lttng_ht_del(ust_app_ht, &iter);
3291 if (ret) {
3292 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3293 lta->pid);
3294 }
3295
3296 /* Free memory */
3297 call_rcu(&lta->pid_n.head, delete_ust_app_rcu);
3298
3299 rcu_read_unlock();
3300 return;
3301 }
3302
3303 /*
3304 * Fill events array with all events name of all registered apps.
3305 */
3306 int ust_app_list_events(struct lttng_event **events)
3307 {
3308 int ret, handle;
3309 size_t nbmem, count = 0;
3310 struct lttng_ht_iter iter;
3311 struct ust_app *app;
3312 struct lttng_event *tmp_event;
3313
3314 nbmem = UST_APP_EVENT_LIST_SIZE;
3315 tmp_event = zmalloc(nbmem * sizeof(struct lttng_event));
3316 if (tmp_event == NULL) {
3317 PERROR("zmalloc ust app events");
3318 ret = -ENOMEM;
3319 goto error;
3320 }
3321
3322 rcu_read_lock();
3323
3324 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3325 struct lttng_ust_tracepoint_iter uiter;
3326
3327 health_code_update();
3328
3329 if (!app->compatible) {
3330 /*
3331 * TODO: In time, we should notice the caller of this error by
3332 * telling him that this is a version error.
3333 */
3334 continue;
3335 }
3336 handle = ustctl_tracepoint_list(app->sock);
3337 if (handle < 0) {
3338 if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) {
3339 ERR("UST app list events getting handle failed for app pid %d",
3340 app->pid);
3341 }
3342 continue;
3343 }
3344
3345 while ((ret = ustctl_tracepoint_list_get(app->sock, handle,
3346 &uiter)) != -LTTNG_UST_ERR_NOENT) {
3347 /* Handle ustctl error. */
3348 if (ret < 0) {
3349 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
3350 ERR("UST app tp list get failed for app %d with ret %d",
3351 app->sock, ret);
3352 } else {
3353 DBG3("UST app tp list get failed. Application is dead");
3354 /*
3355 * This is normal behavior, an application can die during the
3356 * creation process. Don't report an error so the execution can
3357 * continue normally. Continue normal execution.
3358 */
3359 break;
3360 }
3361 free(tmp_event);
3362 goto rcu_error;
3363 }
3364
3365 health_code_update();
3366 if (count >= nbmem) {
3367 /* In case the realloc fails, we free the memory */
3368 struct lttng_event *new_tmp_event;
3369 size_t new_nbmem;
3370
3371 new_nbmem = nbmem << 1;
3372 DBG2("Reallocating event list from %zu to %zu entries",
3373 nbmem, new_nbmem);
3374 new_tmp_event = realloc(tmp_event,
3375 new_nbmem * sizeof(struct lttng_event));
3376 if (new_tmp_event == NULL) {
3377 PERROR("realloc ust app events");
3378 free(tmp_event);
3379 ret = -ENOMEM;
3380 goto rcu_error;
3381 }
3382 /* Zero the new memory */
3383 memset(new_tmp_event + nbmem, 0,
3384 (new_nbmem - nbmem) * sizeof(struct lttng_event));
3385 nbmem = new_nbmem;
3386 tmp_event = new_tmp_event;
3387 }
3388 memcpy(tmp_event[count].name, uiter.name, LTTNG_UST_SYM_NAME_LEN);
3389 tmp_event[count].loglevel = uiter.loglevel;
3390 tmp_event[count].type = (enum lttng_event_type) LTTNG_UST_TRACEPOINT;
3391 tmp_event[count].pid = app->pid;
3392 tmp_event[count].enabled = -1;
3393 count++;
3394 }
3395 }
3396
3397 ret = count;
3398 *events = tmp_event;
3399
3400 DBG2("UST app list events done (%zu events)", count);
3401
3402 rcu_error:
3403 rcu_read_unlock();
3404 error:
3405 health_code_update();
3406 return ret;
3407 }
3408
3409 /*
3410 * Fill events array with all events name of all registered apps.
3411 */
3412 int ust_app_list_event_fields(struct lttng_event_field **fields)
3413 {
3414 int ret, handle;
3415 size_t nbmem, count = 0;
3416 struct lttng_ht_iter iter;
3417 struct ust_app *app;
3418 struct lttng_event_field *tmp_event;
3419
3420 nbmem = UST_APP_EVENT_LIST_SIZE;
3421 tmp_event = zmalloc(nbmem * sizeof(struct lttng_event_field));
3422 if (tmp_event == NULL) {
3423 PERROR("zmalloc ust app event fields");
3424 ret = -ENOMEM;
3425 goto error;
3426 }
3427
3428 rcu_read_lock();
3429
3430 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3431 struct lttng_ust_field_iter uiter;
3432
3433 health_code_update();
3434
3435 if (!app->compatible) {
3436 /*
3437 * TODO: In time, we should notice the caller of this error by
3438 * telling him that this is a version error.
3439 */
3440 continue;
3441 }
3442 handle = ustctl_tracepoint_field_list(app->sock);
3443 if (handle < 0) {
3444 if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) {
3445 ERR("UST app list field getting handle failed for app pid %d",
3446 app->pid);
3447 }
3448 continue;
3449 }
3450
3451 while ((ret = ustctl_tracepoint_field_list_get(app->sock, handle,
3452 &uiter)) != -LTTNG_UST_ERR_NOENT) {
3453 /* Handle ustctl error. */
3454 if (ret < 0) {
3455 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
3456 ERR("UST app tp list field failed for app %d with ret %d",
3457 app->sock, ret);
3458 } else {
3459 DBG3("UST app tp list field failed. Application is dead");
3460 /*
3461 * This is normal behavior, an application can die during the
3462 * creation process. Don't report an error so the execution can
3463 * continue normally. Reset list and count for next app.
3464 */
3465 break;
3466 }
3467 free(tmp_event);
3468 goto rcu_error;
3469 }
3470
3471 health_code_update();
3472 if (count >= nbmem) {
3473 /* In case the realloc fails, we free the memory */
3474 struct lttng_event_field *new_tmp_event;
3475 size_t new_nbmem;
3476
3477 new_nbmem = nbmem << 1;
3478 DBG2("Reallocating event field list from %zu to %zu entries",
3479 nbmem, new_nbmem);
3480 new_tmp_event = realloc(tmp_event,
3481 new_nbmem * sizeof(struct lttng_event_field));
3482 if (new_tmp_event == NULL) {
3483 PERROR("realloc ust app event fields");
3484 free(tmp_event);
3485 ret = -ENOMEM;
3486 goto rcu_error;
3487 }
3488 /* Zero the new memory */
3489 memset(new_tmp_event + nbmem, 0,
3490 (new_nbmem - nbmem) * sizeof(struct lttng_event_field));
3491 nbmem = new_nbmem;
3492 tmp_event = new_tmp_event;
3493 }
3494
3495 memcpy(tmp_event[count].field_name, uiter.field_name, LTTNG_UST_SYM_NAME_LEN);
3496 /* Mapping between these enums matches 1 to 1. */
3497 tmp_event[count].type = (enum lttng_event_field_type) uiter.type;
3498 tmp_event[count].nowrite = uiter.nowrite;
3499
3500 memcpy(tmp_event[count].event.name, uiter.event_name, LTTNG_UST_SYM_NAME_LEN);
3501 tmp_event[count].event.loglevel = uiter.loglevel;
3502 tmp_event[count].event.type = LTTNG_EVENT_TRACEPOINT;
3503 tmp_event[count].event.pid = app->pid;
3504 tmp_event[count].event.enabled = -1;
3505 count++;
3506 }
3507 }
3508
3509 ret = count;
3510 *fields = tmp_event;
3511
3512 DBG2("UST app list event fields done (%zu events)", count);
3513
3514 rcu_error:
3515 rcu_read_unlock();
3516 error:
3517 health_code_update();
3518 return ret;
3519 }
3520
3521 /*
3522 * Free and clean all traceable apps of the global list.
3523 *
3524 * Should _NOT_ be called with RCU read-side lock held.
3525 */
3526 void ust_app_clean_list(void)
3527 {
3528 int ret;
3529 struct ust_app *app;
3530 struct lttng_ht_iter iter;
3531
3532 DBG2("UST app cleaning registered apps hash table");
3533
3534 rcu_read_lock();
3535
3536 if (ust_app_ht) {
3537 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3538 ret = lttng_ht_del(ust_app_ht, &iter);
3539 assert(!ret);
3540 call_rcu(&app->pid_n.head, delete_ust_app_rcu);
3541 }
3542 }
3543
3544 /* Cleanup socket hash table */
3545 if (ust_app_ht_by_sock) {
3546 cds_lfht_for_each_entry(ust_app_ht_by_sock->ht, &iter.iter, app,
3547 sock_n.node) {
3548 ret = lttng_ht_del(ust_app_ht_by_sock, &iter);
3549 assert(!ret);
3550 }
3551 }
3552
3553 /* Cleanup notify socket hash table */
3554 if (ust_app_ht_by_notify_sock) {
3555 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock->ht, &iter.iter, app,
3556 notify_sock_n.node) {
3557 ret = lttng_ht_del(ust_app_ht_by_notify_sock, &iter);
3558 assert(!ret);
3559 }
3560 }
3561 rcu_read_unlock();
3562
3563 /* Destroy is done only when the ht is empty */
3564 if (ust_app_ht) {
3565 ht_cleanup_push(ust_app_ht);
3566 }
3567 if (ust_app_ht_by_sock) {
3568 ht_cleanup_push(ust_app_ht_by_sock);
3569 }
3570 if (ust_app_ht_by_notify_sock) {
3571 ht_cleanup_push(ust_app_ht_by_notify_sock);
3572 }
3573 }
3574
3575 /*
3576 * Init UST app hash table.
3577 */
3578 int ust_app_ht_alloc(void)
3579 {
3580 ust_app_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
3581 if (!ust_app_ht) {
3582 return -1;
3583 }
3584 ust_app_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
3585 if (!ust_app_ht_by_sock) {
3586 return -1;
3587 }
3588 ust_app_ht_by_notify_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
3589 if (!ust_app_ht_by_notify_sock) {
3590 return -1;
3591 }
3592 return 0;
3593 }
3594
3595 /*
3596 * For a specific UST session, disable the channel for all registered apps.
3597 */
3598 int ust_app_disable_channel_glb(struct ltt_ust_session *usess,
3599 struct ltt_ust_channel *uchan)
3600 {
3601 int ret = 0;
3602 struct lttng_ht_iter iter;
3603 struct lttng_ht_node_str *ua_chan_node;
3604 struct ust_app *app;
3605 struct ust_app_session *ua_sess;
3606 struct ust_app_channel *ua_chan;
3607
3608 if (usess == NULL || uchan == NULL) {
3609 ERR("Disabling UST global channel with NULL values");
3610 ret = -1;
3611 goto error;
3612 }
3613
3614 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64,
3615 uchan->name, usess->id);
3616
3617 rcu_read_lock();
3618
3619 /* For every registered applications */
3620 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3621 struct lttng_ht_iter uiter;
3622 if (!app->compatible) {
3623 /*
3624 * TODO: In time, we should notice the caller of this error by
3625 * telling him that this is a version error.
3626 */
3627 continue;
3628 }
3629 ua_sess = lookup_session_by_app(usess, app);
3630 if (ua_sess == NULL) {
3631 continue;
3632 }
3633
3634 /* Get channel */
3635 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
3636 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
3637 /* If the session if found for the app, the channel must be there */
3638 assert(ua_chan_node);
3639
3640 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
3641 /* The channel must not be already disabled */
3642 assert(ua_chan->enabled == 1);
3643
3644 /* Disable channel onto application */
3645 ret = disable_ust_app_channel(ua_sess, ua_chan, app);
3646 if (ret < 0) {
3647 /* XXX: We might want to report this error at some point... */
3648 continue;
3649 }
3650 }
3651
3652 rcu_read_unlock();
3653
3654 error:
3655 return ret;
3656 }
3657
3658 /*
3659 * For a specific UST session, enable the channel for all registered apps.
3660 */
3661 int ust_app_enable_channel_glb(struct ltt_ust_session *usess,
3662 struct ltt_ust_channel *uchan)
3663 {
3664 int ret = 0;
3665 struct lttng_ht_iter iter;
3666 struct ust_app *app;
3667 struct ust_app_session *ua_sess;
3668
3669 if (usess == NULL || uchan == NULL) {
3670 ERR("Adding UST global channel to NULL values");
3671 ret = -1;
3672 goto error;
3673 }
3674
3675 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64,
3676 uchan->name, usess->id);
3677
3678 rcu_read_lock();
3679
3680 /* For every registered applications */
3681 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3682 if (!app->compatible) {
3683 /*
3684 * TODO: In time, we should notice the caller of this error by
3685 * telling him that this is a version error.
3686 */
3687 continue;
3688 }
3689 ua_sess = lookup_session_by_app(usess, app);
3690 if (ua_sess == NULL) {
3691 continue;
3692 }
3693
3694 /* Enable channel onto application */
3695 ret = enable_ust_app_channel(ua_sess, uchan, app);
3696 if (ret < 0) {
3697 /* XXX: We might want to report this error at some point... */
3698 continue;
3699 }
3700 }
3701
3702 rcu_read_unlock();
3703
3704 error:
3705 return ret;
3706 }
3707
3708 /*
3709 * Disable an event in a channel and for a specific session.
3710 */
3711 int ust_app_disable_event_glb(struct ltt_ust_session *usess,
3712 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
3713 {
3714 int ret = 0;
3715 struct lttng_ht_iter iter, uiter;
3716 struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
3717 struct ust_app *app;
3718 struct ust_app_session *ua_sess;
3719 struct ust_app_channel *ua_chan;
3720 struct ust_app_event *ua_event;
3721
3722 DBG("UST app disabling event %s for all apps in channel "
3723 "%s for session id %" PRIu64,
3724 uevent->attr.name, uchan->name, usess->id);
3725
3726 rcu_read_lock();
3727
3728 /* For all registered applications */
3729 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3730 if (!app->compatible) {
3731 /*
3732 * TODO: In time, we should notice the caller of this error by
3733 * telling him that this is a version error.
3734 */
3735 continue;
3736 }
3737 ua_sess = lookup_session_by_app(usess, app);
3738 if (ua_sess == NULL) {
3739 /* Next app */
3740 continue;
3741 }
3742
3743 /* Lookup channel in the ust app session */
3744 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
3745 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);