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