Fix: sessiond vs consumerd push/get metadata deadlock
[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, new_metadata_len_sent;
444 ssize_t ret_val;
445 uint64_t metadata_key;
446
447 assert(registry);
448 assert(socket);
449
450 metadata_key = registry->metadata_key;
451
452 /*
453 * Means that no metadata was assigned to the session. This can
454 * happens if no start has been done previously.
455 */
456 if (!metadata_key) {
457 return 0;
458 }
459
460 /*
461 * On a push metadata error either the consumer is dead or the
462 * metadata channel has been destroyed because its endpoint
463 * might have died (e.g: relayd), or because the application has
464 * exited. If so, the metadata closed flag is set to 1 so we
465 * deny pushing metadata again which is not valid anymore on the
466 * consumer side.
467 */
468 if (registry->metadata_closed) {
469 return -EPIPE;
470 }
471
472 offset = registry->metadata_len_sent;
473 len = registry->metadata_len - registry->metadata_len_sent;
474 new_metadata_len_sent = registry->metadata_len;
475 if (len == 0) {
476 DBG3("No metadata to push for metadata key %" PRIu64,
477 registry->metadata_key);
478 ret_val = len;
479 if (send_zero_data) {
480 DBG("No metadata to push");
481 goto push_data;
482 }
483 goto end;
484 }
485
486 /* Allocate only what we have to send. */
487 metadata_str = zmalloc(len);
488 if (!metadata_str) {
489 PERROR("zmalloc ust app metadata string");
490 ret_val = -ENOMEM;
491 goto error;
492 }
493 /* Copy what we haven't sent out. */
494 memcpy(metadata_str, registry->metadata + offset, len);
495
496 push_data:
497 pthread_mutex_unlock(&registry->lock);
498 /*
499 * We need to unlock the registry while we push metadata to
500 * break a circular dependency between the consumerd metadata
501 * lock and the sessiond registry lock. Indeed, pushing metadata
502 * to the consumerd awaits that it gets pushed all the way to
503 * relayd, but doing so requires grabbing the metadata lock. If
504 * a concurrent metadata request is being performed by
505 * consumerd, this can try to grab the registry lock on the
506 * sessiond while holding the metadata lock on the consumer
507 * daemon. Those push and pull schemes are performed on two
508 * different bidirectionnal communication sockets.
509 */
510 ret = consumer_push_metadata(socket, metadata_key,
511 metadata_str, len, offset);
512 pthread_mutex_lock(&registry->lock);
513 if (ret < 0) {
514 /*
515 * There is an acceptable race here between the registry
516 * metadata key assignment and the creation on the
517 * consumer. The session daemon can concurrently push
518 * metadata for this registry while being created on the
519 * consumer since the metadata key of the registry is
520 * assigned *before* it is setup to avoid the consumer
521 * to ask for metadata that could possibly be not found
522 * in the session daemon.
523 *
524 * The metadata will get pushed either by the session
525 * being stopped or the consumer requesting metadata if
526 * that race is triggered.
527 */
528 if (ret == -LTTCOMM_CONSUMERD_CHANNEL_FAIL) {
529 ret = 0;
530 } else {
531 ERR("Error pushing metadata to consumer");
532 }
533 ret_val = ret;
534 goto error_push;
535 } else {
536 /*
537 * Metadata may have been concurrently pushed, since
538 * we're not holding the registry lock while pushing to
539 * consumer. This is handled by the fact that we send
540 * the metadata content, size, and the offset at which
541 * that metadata belongs. This may arrive out of order
542 * on the consumer side, and the consumer is able to
543 * deal with overlapping fragments. The consumer
544 * supports overlapping fragments, which must be
545 * contiguous starting from offset 0. We keep the
546 * largest metadata_len_sent value of the concurrent
547 * send.
548 */
549 registry->metadata_len_sent =
550 max_t(size_t, registry->metadata_len_sent,
551 new_metadata_len_sent);
552 }
553 free(metadata_str);
554 return len;
555
556 end:
557 error:
558 if (ret_val) {
559 /*
560 * On error, flag the registry that the metadata is
561 * closed. We were unable to push anything and this
562 * means that either the consumer is not responding or
563 * the metadata cache has been destroyed on the
564 * consumer.
565 */
566 registry->metadata_closed = 1;
567 }
568 error_push:
569 free(metadata_str);
570 return ret_val;
571 }
572
573 /*
574 * For a given application and session, push metadata to consumer.
575 * Either sock or consumer is required : if sock is NULL, the default
576 * socket to send the metadata is retrieved from consumer, if sock
577 * is not NULL we use it to send the metadata.
578 * RCU read-side lock must be held while calling this function,
579 * therefore ensuring existance of registry. It also ensures existance
580 * of socket throughout this function.
581 *
582 * Return 0 on success else a negative error.
583 * Returning a -EPIPE return value means we could not send the metadata,
584 * but it can be caused by recoverable errors (e.g. the application has
585 * terminated concurrently).
586 */
587 static int push_metadata(struct ust_registry_session *registry,
588 struct consumer_output *consumer)
589 {
590 int ret_val;
591 ssize_t ret;
592 struct consumer_socket *socket;
593
594 assert(registry);
595 assert(consumer);
596
597 pthread_mutex_lock(&registry->lock);
598 if (registry->metadata_closed) {
599 ret_val = -EPIPE;
600 goto error;
601 }
602
603 /* Get consumer socket to use to push the metadata.*/
604 socket = consumer_find_socket_by_bitness(registry->bits_per_long,
605 consumer);
606 if (!socket) {
607 ret_val = -1;
608 goto error;
609 }
610
611 ret = ust_app_push_metadata(registry, socket, 0);
612 if (ret < 0) {
613 ret_val = ret;
614 goto error;
615 }
616 pthread_mutex_unlock(&registry->lock);
617 return 0;
618
619 error:
620 pthread_mutex_unlock(&registry->lock);
621 return ret_val;
622 }
623
624 /*
625 * Send to the consumer a close metadata command for the given session. Once
626 * done, the metadata channel is deleted and the session metadata pointer is
627 * nullified. The session lock MUST be held unless the application is
628 * in the destroy path.
629 *
630 * Return 0 on success else a negative value.
631 */
632 static int close_metadata(struct ust_registry_session *registry,
633 struct consumer_output *consumer)
634 {
635 int ret;
636 struct consumer_socket *socket;
637
638 assert(registry);
639 assert(consumer);
640
641 rcu_read_lock();
642
643 pthread_mutex_lock(&registry->lock);
644
645 if (!registry->metadata_key || registry->metadata_closed) {
646 ret = 0;
647 goto end;
648 }
649
650 /* Get consumer socket to use to push the metadata.*/
651 socket = consumer_find_socket_by_bitness(registry->bits_per_long,
652 consumer);
653 if (!socket) {
654 ret = -1;
655 goto error;
656 }
657
658 ret = consumer_close_metadata(socket, registry->metadata_key);
659 if (ret < 0) {
660 goto error;
661 }
662
663 error:
664 /*
665 * Metadata closed. Even on error this means that the consumer is not
666 * responding or not found so either way a second close should NOT be emit
667 * for this registry.
668 */
669 registry->metadata_closed = 1;
670 end:
671 pthread_mutex_unlock(&registry->lock);
672 rcu_read_unlock();
673 return ret;
674 }
675
676 /*
677 * We need to execute ht_destroy outside of RCU read-side critical
678 * section and outside of call_rcu thread, so we postpone its execution
679 * using ht_cleanup_push. It is simpler than to change the semantic of
680 * the many callers of delete_ust_app_session().
681 */
682 static
683 void delete_ust_app_session_rcu(struct rcu_head *head)
684 {
685 struct ust_app_session *ua_sess =
686 caa_container_of(head, struct ust_app_session, rcu_head);
687
688 ht_cleanup_push(ua_sess->channels);
689 free(ua_sess);
690 }
691
692 /*
693 * Delete ust app session safely. RCU read lock must be held before calling
694 * this function.
695 */
696 static
697 void delete_ust_app_session(int sock, struct ust_app_session *ua_sess,
698 struct ust_app *app)
699 {
700 int ret;
701 struct lttng_ht_iter iter;
702 struct ust_app_channel *ua_chan;
703 struct ust_registry_session *registry;
704
705 assert(ua_sess);
706
707 pthread_mutex_lock(&ua_sess->lock);
708
709 assert(!ua_sess->deleted);
710 ua_sess->deleted = true;
711
712 registry = get_session_registry(ua_sess);
713 if (registry) {
714 /* Push metadata for application before freeing the application. */
715 (void) push_metadata(registry, ua_sess->consumer);
716
717 /*
718 * Don't ask to close metadata for global per UID buffers. Close
719 * metadata only on destroy trace session in this case. Also, the
720 * previous push metadata could have flag the metadata registry to
721 * close so don't send a close command if closed.
722 */
723 if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) {
724 /* And ask to close it for this session registry. */
725 (void) close_metadata(registry, ua_sess->consumer);
726 }
727 }
728
729 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
730 node.node) {
731 ret = lttng_ht_del(ua_sess->channels, &iter);
732 assert(!ret);
733 delete_ust_app_channel(sock, ua_chan, app);
734 }
735
736 /* In case of per PID, the registry is kept in the session. */
737 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) {
738 struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id);
739 if (reg_pid) {
740 buffer_reg_pid_remove(reg_pid);
741 buffer_reg_pid_destroy(reg_pid);
742 }
743 }
744
745 if (ua_sess->handle != -1) {
746 ret = ustctl_release_handle(sock, ua_sess->handle);
747 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
748 ERR("UST app sock %d release session handle failed with ret %d",
749 sock, ret);
750 }
751 }
752 pthread_mutex_unlock(&ua_sess->lock);
753
754 consumer_output_put(ua_sess->consumer);
755
756 call_rcu(&ua_sess->rcu_head, delete_ust_app_session_rcu);
757 }
758
759 /*
760 * Delete a traceable application structure from the global list. Never call
761 * this function outside of a call_rcu call.
762 *
763 * RCU read side lock should _NOT_ be held when calling this function.
764 */
765 static
766 void delete_ust_app(struct ust_app *app)
767 {
768 int ret, sock;
769 struct ust_app_session *ua_sess, *tmp_ua_sess;
770
771 /* Delete ust app sessions info */
772 sock = app->sock;
773 app->sock = -1;
774
775 /* Wipe sessions */
776 cds_list_for_each_entry_safe(ua_sess, tmp_ua_sess, &app->teardown_head,
777 teardown_node) {
778 /* Free every object in the session and the session. */
779 rcu_read_lock();
780 delete_ust_app_session(sock, ua_sess, app);
781 rcu_read_unlock();
782 }
783
784 ht_cleanup_push(app->sessions);
785 ht_cleanup_push(app->ust_objd);
786
787 /*
788 * Wait until we have deleted the application from the sock hash table
789 * before closing this socket, otherwise an application could re-use the
790 * socket ID and race with the teardown, using the same hash table entry.
791 *
792 * It's OK to leave the close in call_rcu. We want it to stay unique for
793 * all RCU readers that could run concurrently with unregister app,
794 * therefore we _need_ to only close that socket after a grace period. So
795 * it should stay in this RCU callback.
796 *
797 * This close() is a very important step of the synchronization model so
798 * every modification to this function must be carefully reviewed.
799 */
800 ret = close(sock);
801 if (ret) {
802 PERROR("close");
803 }
804 lttng_fd_put(LTTNG_FD_APPS, 1);
805
806 DBG2("UST app pid %d deleted", app->pid);
807 free(app);
808 }
809
810 /*
811 * URCU intermediate call to delete an UST app.
812 */
813 static
814 void delete_ust_app_rcu(struct rcu_head *head)
815 {
816 struct lttng_ht_node_ulong *node =
817 caa_container_of(head, struct lttng_ht_node_ulong, head);
818 struct ust_app *app =
819 caa_container_of(node, struct ust_app, pid_n);
820
821 DBG3("Call RCU deleting app PID %d", app->pid);
822 delete_ust_app(app);
823 }
824
825 /*
826 * Delete the session from the application ht and delete the data structure by
827 * freeing every object inside and releasing them.
828 */
829 static void destroy_app_session(struct ust_app *app,
830 struct ust_app_session *ua_sess)
831 {
832 int ret;
833 struct lttng_ht_iter iter;
834
835 assert(app);
836 assert(ua_sess);
837
838 iter.iter.node = &ua_sess->node.node;
839 ret = lttng_ht_del(app->sessions, &iter);
840 if (ret) {
841 /* Already scheduled for teardown. */
842 goto end;
843 }
844
845 /* Once deleted, free the data structure. */
846 delete_ust_app_session(app->sock, ua_sess, app);
847
848 end:
849 return;
850 }
851
852 /*
853 * Alloc new UST app session.
854 */
855 static
856 struct ust_app_session *alloc_ust_app_session(struct ust_app *app)
857 {
858 struct ust_app_session *ua_sess;
859
860 /* Init most of the default value by allocating and zeroing */
861 ua_sess = zmalloc(sizeof(struct ust_app_session));
862 if (ua_sess == NULL) {
863 PERROR("malloc");
864 goto error_free;
865 }
866
867 ua_sess->handle = -1;
868 ua_sess->channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
869 ua_sess->metadata_attr.type = LTTNG_UST_CHAN_METADATA;
870 pthread_mutex_init(&ua_sess->lock, NULL);
871
872 return ua_sess;
873
874 error_free:
875 return NULL;
876 }
877
878 /*
879 * Alloc new UST app channel.
880 */
881 static
882 struct ust_app_channel *alloc_ust_app_channel(char *name,
883 struct ust_app_session *ua_sess,
884 struct lttng_ust_channel_attr *attr)
885 {
886 struct ust_app_channel *ua_chan;
887
888 /* Init most of the default value by allocating and zeroing */
889 ua_chan = zmalloc(sizeof(struct ust_app_channel));
890 if (ua_chan == NULL) {
891 PERROR("malloc");
892 goto error;
893 }
894
895 /* Setup channel name */
896 strncpy(ua_chan->name, name, sizeof(ua_chan->name));
897 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
898
899 ua_chan->enabled = 1;
900 ua_chan->handle = -1;
901 ua_chan->session = ua_sess;
902 ua_chan->key = get_next_channel_key();
903 ua_chan->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
904 ua_chan->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
905 lttng_ht_node_init_str(&ua_chan->node, ua_chan->name);
906
907 CDS_INIT_LIST_HEAD(&ua_chan->streams.head);
908 CDS_INIT_LIST_HEAD(&ua_chan->ctx_list);
909
910 /* Copy attributes */
911 if (attr) {
912 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
913 ua_chan->attr.subbuf_size = attr->subbuf_size;
914 ua_chan->attr.num_subbuf = attr->num_subbuf;
915 ua_chan->attr.overwrite = attr->overwrite;
916 ua_chan->attr.switch_timer_interval = attr->switch_timer_interval;
917 ua_chan->attr.read_timer_interval = attr->read_timer_interval;
918 ua_chan->attr.output = attr->output;
919 }
920 /* By default, the channel is a per cpu channel. */
921 ua_chan->attr.type = LTTNG_UST_CHAN_PER_CPU;
922
923 DBG3("UST app channel %s allocated", ua_chan->name);
924
925 return ua_chan;
926
927 error:
928 return NULL;
929 }
930
931 /*
932 * Allocate and initialize a UST app stream.
933 *
934 * Return newly allocated stream pointer or NULL on error.
935 */
936 struct ust_app_stream *ust_app_alloc_stream(void)
937 {
938 struct ust_app_stream *stream = NULL;
939
940 stream = zmalloc(sizeof(*stream));
941 if (stream == NULL) {
942 PERROR("zmalloc ust app stream");
943 goto error;
944 }
945
946 /* Zero could be a valid value for a handle so flag it to -1. */
947 stream->handle = -1;
948
949 error:
950 return stream;
951 }
952
953 /*
954 * Alloc new UST app event.
955 */
956 static
957 struct ust_app_event *alloc_ust_app_event(char *name,
958 struct lttng_ust_event *attr)
959 {
960 struct ust_app_event *ua_event;
961
962 /* Init most of the default value by allocating and zeroing */
963 ua_event = zmalloc(sizeof(struct ust_app_event));
964 if (ua_event == NULL) {
965 PERROR("malloc");
966 goto error;
967 }
968
969 ua_event->enabled = 1;
970 strncpy(ua_event->name, name, sizeof(ua_event->name));
971 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
972 lttng_ht_node_init_str(&ua_event->node, ua_event->name);
973
974 /* Copy attributes */
975 if (attr) {
976 memcpy(&ua_event->attr, attr, sizeof(ua_event->attr));
977 }
978
979 DBG3("UST app event %s allocated", ua_event->name);
980
981 return ua_event;
982
983 error:
984 return NULL;
985 }
986
987 /*
988 * Alloc new UST app context.
989 */
990 static
991 struct ust_app_ctx *alloc_ust_app_ctx(struct lttng_ust_context *uctx)
992 {
993 struct ust_app_ctx *ua_ctx;
994
995 ua_ctx = zmalloc(sizeof(struct ust_app_ctx));
996 if (ua_ctx == NULL) {
997 goto error;
998 }
999
1000 CDS_INIT_LIST_HEAD(&ua_ctx->list);
1001
1002 if (uctx) {
1003 memcpy(&ua_ctx->ctx, uctx, sizeof(ua_ctx->ctx));
1004 }
1005
1006 DBG3("UST app context %d allocated", ua_ctx->ctx.ctx);
1007
1008 error:
1009 return ua_ctx;
1010 }
1011
1012 /*
1013 * Allocate a filter and copy the given original filter.
1014 *
1015 * Return allocated filter or NULL on error.
1016 */
1017 static struct lttng_filter_bytecode *copy_filter_bytecode(
1018 struct lttng_filter_bytecode *orig_f)
1019 {
1020 struct lttng_filter_bytecode *filter = NULL;
1021
1022 /* Copy filter bytecode */
1023 filter = zmalloc(sizeof(*filter) + orig_f->len);
1024 if (!filter) {
1025 PERROR("zmalloc alloc filter bytecode");
1026 goto error;
1027 }
1028
1029 memcpy(filter, orig_f, sizeof(*filter) + orig_f->len);
1030
1031 error:
1032 return filter;
1033 }
1034
1035 /*
1036 * Create a liblttng-ust filter bytecode from given bytecode.
1037 *
1038 * Return allocated filter or NULL on error.
1039 */
1040 static struct lttng_ust_filter_bytecode *create_ust_bytecode_from_bytecode(
1041 struct lttng_filter_bytecode *orig_f)
1042 {
1043 struct lttng_ust_filter_bytecode *filter = NULL;
1044
1045 /* Copy filter bytecode */
1046 filter = zmalloc(sizeof(*filter) + orig_f->len);
1047 if (!filter) {
1048 PERROR("zmalloc alloc ust filter bytecode");
1049 goto error;
1050 }
1051
1052 assert(sizeof(struct lttng_filter_bytecode) ==
1053 sizeof(struct lttng_ust_filter_bytecode));
1054 memcpy(filter, orig_f, sizeof(*filter) + orig_f->len);
1055 error:
1056 return filter;
1057 }
1058
1059 /*
1060 * Find an ust_app using the sock and return it. RCU read side lock must be
1061 * held before calling this helper function.
1062 */
1063 struct ust_app *ust_app_find_by_sock(int sock)
1064 {
1065 struct lttng_ht_node_ulong *node;
1066 struct lttng_ht_iter iter;
1067
1068 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter);
1069 node = lttng_ht_iter_get_node_ulong(&iter);
1070 if (node == NULL) {
1071 DBG2("UST app find by sock %d not found", sock);
1072 goto error;
1073 }
1074
1075 return caa_container_of(node, struct ust_app, sock_n);
1076
1077 error:
1078 return NULL;
1079 }
1080
1081 /*
1082 * Find an ust_app using the notify sock and return it. RCU read side lock must
1083 * be held before calling this helper function.
1084 */
1085 static struct ust_app *find_app_by_notify_sock(int sock)
1086 {
1087 struct lttng_ht_node_ulong *node;
1088 struct lttng_ht_iter iter;
1089
1090 lttng_ht_lookup(ust_app_ht_by_notify_sock, (void *)((unsigned long) sock),
1091 &iter);
1092 node = lttng_ht_iter_get_node_ulong(&iter);
1093 if (node == NULL) {
1094 DBG2("UST app find by notify sock %d not found", sock);
1095 goto error;
1096 }
1097
1098 return caa_container_of(node, struct ust_app, notify_sock_n);
1099
1100 error:
1101 return NULL;
1102 }
1103
1104 /*
1105 * Lookup for an ust app event based on event name, filter bytecode and the
1106 * event loglevel.
1107 *
1108 * Return an ust_app_event object or NULL on error.
1109 */
1110 static struct ust_app_event *find_ust_app_event(struct lttng_ht *ht,
1111 char *name, struct lttng_filter_bytecode *filter, int loglevel,
1112 const struct lttng_event_exclusion *exclusion)
1113 {
1114 struct lttng_ht_iter iter;
1115 struct lttng_ht_node_str *node;
1116 struct ust_app_event *event = NULL;
1117 struct ust_app_ht_key key;
1118
1119 assert(name);
1120 assert(ht);
1121
1122 /* Setup key for event lookup. */
1123 key.name = name;
1124 key.filter = filter;
1125 key.loglevel = loglevel;
1126 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1127 key.exclusion = exclusion;
1128
1129 /* Lookup using the event name as hash and a custom match fct. */
1130 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
1131 ht_match_ust_app_event, &key, &iter.iter);
1132 node = lttng_ht_iter_get_node_str(&iter);
1133 if (node == NULL) {
1134 goto end;
1135 }
1136
1137 event = caa_container_of(node, struct ust_app_event, node);
1138
1139 end:
1140 return event;
1141 }
1142
1143 /*
1144 * Create the channel context on the tracer.
1145 *
1146 * Called with UST app session lock held.
1147 */
1148 static
1149 int create_ust_channel_context(struct ust_app_channel *ua_chan,
1150 struct ust_app_ctx *ua_ctx, struct ust_app *app)
1151 {
1152 int ret;
1153
1154 health_code_update();
1155
1156 ret = ustctl_add_context(app->sock, &ua_ctx->ctx,
1157 ua_chan->obj, &ua_ctx->obj);
1158 if (ret < 0) {
1159 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1160 ERR("UST app create channel context failed for app (pid: %d) "
1161 "with ret %d", app->pid, ret);
1162 } else {
1163 /*
1164 * This is normal behavior, an application can die during the
1165 * creation process. Don't report an error so the execution can
1166 * continue normally.
1167 */
1168 ret = 0;
1169 DBG3("UST app disable event failed. Application is dead.");
1170 }
1171 goto error;
1172 }
1173
1174 ua_ctx->handle = ua_ctx->obj->handle;
1175
1176 DBG2("UST app context handle %d created successfully for channel %s",
1177 ua_ctx->handle, ua_chan->name);
1178
1179 error:
1180 health_code_update();
1181 return ret;
1182 }
1183
1184 /*
1185 * Set the filter on the tracer.
1186 */
1187 static
1188 int set_ust_event_filter(struct ust_app_event *ua_event,
1189 struct ust_app *app)
1190 {
1191 int ret;
1192 struct lttng_ust_filter_bytecode *ust_bytecode = NULL;
1193
1194 health_code_update();
1195
1196 if (!ua_event->filter) {
1197 ret = 0;
1198 goto error;
1199 }
1200
1201 ust_bytecode = create_ust_bytecode_from_bytecode(ua_event->filter);
1202 if (!ust_bytecode) {
1203 ret = -LTTNG_ERR_NOMEM;
1204 goto error;
1205 }
1206 ret = ustctl_set_filter(app->sock, ust_bytecode,
1207 ua_event->obj);
1208 if (ret < 0) {
1209 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1210 ERR("UST app event %s filter failed for app (pid: %d) "
1211 "with ret %d", ua_event->attr.name, app->pid, ret);
1212 } else {
1213 /*
1214 * This is normal behavior, an application can die during the
1215 * creation process. Don't report an error so the execution can
1216 * continue normally.
1217 */
1218 ret = 0;
1219 DBG3("UST app filter event failed. Application is dead.");
1220 }
1221 goto error;
1222 }
1223
1224 DBG2("UST filter set successfully for event %s", ua_event->name);
1225
1226 error:
1227 health_code_update();
1228 free(ust_bytecode);
1229 return ret;
1230 }
1231
1232 static
1233 struct lttng_ust_event_exclusion *create_ust_exclusion_from_exclusion(
1234 struct lttng_event_exclusion *exclusion)
1235 {
1236 struct lttng_ust_event_exclusion *ust_exclusion = NULL;
1237 size_t exclusion_alloc_size = sizeof(struct lttng_ust_event_exclusion) +
1238 LTTNG_UST_SYM_NAME_LEN * exclusion->count;
1239
1240 ust_exclusion = zmalloc(exclusion_alloc_size);
1241 if (!ust_exclusion) {
1242 PERROR("malloc");
1243 goto end;
1244 }
1245
1246 assert(sizeof(struct lttng_event_exclusion) ==
1247 sizeof(struct lttng_ust_event_exclusion));
1248 memcpy(ust_exclusion, exclusion, exclusion_alloc_size);
1249 end:
1250 return ust_exclusion;
1251 }
1252
1253 /*
1254 * Set event exclusions on the tracer.
1255 */
1256 static
1257 int set_ust_event_exclusion(struct ust_app_event *ua_event,
1258 struct ust_app *app)
1259 {
1260 int ret;
1261 struct lttng_ust_event_exclusion *ust_exclusion = NULL;
1262
1263 health_code_update();
1264
1265 if (!ua_event->exclusion || !ua_event->exclusion->count) {
1266 ret = 0;
1267 goto error;
1268 }
1269
1270 ust_exclusion = create_ust_exclusion_from_exclusion(
1271 ua_event->exclusion);
1272 if (!ust_exclusion) {
1273 ret = -LTTNG_ERR_NOMEM;
1274 goto error;
1275 }
1276 ret = ustctl_set_exclusion(app->sock, ust_exclusion, ua_event->obj);
1277 if (ret < 0) {
1278 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1279 ERR("UST app event %s exclusions failed for app (pid: %d) "
1280 "with ret %d", ua_event->attr.name, app->pid, ret);
1281 } else {
1282 /*
1283 * This is normal behavior, an application can die during the
1284 * creation process. Don't report an error so the execution can
1285 * continue normally.
1286 */
1287 ret = 0;
1288 DBG3("UST app event exclusion failed. Application is dead.");
1289 }
1290 goto error;
1291 }
1292
1293 DBG2("UST exclusion set successfully for event %s", ua_event->name);
1294
1295 error:
1296 health_code_update();
1297 free(ust_exclusion);
1298 return ret;
1299 }
1300
1301 /*
1302 * Disable the specified event on to UST tracer for the UST session.
1303 */
1304 static int disable_ust_event(struct ust_app *app,
1305 struct ust_app_session *ua_sess, struct ust_app_event *ua_event)
1306 {
1307 int ret;
1308
1309 health_code_update();
1310
1311 ret = ustctl_disable(app->sock, ua_event->obj);
1312 if (ret < 0) {
1313 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1314 ERR("UST app event %s disable failed for app (pid: %d) "
1315 "and session handle %d with ret %d",
1316 ua_event->attr.name, app->pid, ua_sess->handle, ret);
1317 } else {
1318 /*
1319 * This is normal behavior, an application can die during the
1320 * creation process. Don't report an error so the execution can
1321 * continue normally.
1322 */
1323 ret = 0;
1324 DBG3("UST app disable event failed. Application is dead.");
1325 }
1326 goto error;
1327 }
1328
1329 DBG2("UST app event %s disabled successfully for app (pid: %d)",
1330 ua_event->attr.name, app->pid);
1331
1332 error:
1333 health_code_update();
1334 return ret;
1335 }
1336
1337 /*
1338 * Disable the specified channel on to UST tracer for the UST session.
1339 */
1340 static int disable_ust_channel(struct ust_app *app,
1341 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
1342 {
1343 int ret;
1344
1345 health_code_update();
1346
1347 ret = ustctl_disable(app->sock, ua_chan->obj);
1348 if (ret < 0) {
1349 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1350 ERR("UST app channel %s disable failed for app (pid: %d) "
1351 "and session handle %d with ret %d",
1352 ua_chan->name, app->pid, ua_sess->handle, ret);
1353 } else {
1354 /*
1355 * This is normal behavior, an application can die during the
1356 * creation process. Don't report an error so the execution can
1357 * continue normally.
1358 */
1359 ret = 0;
1360 DBG3("UST app disable channel failed. Application is dead.");
1361 }
1362 goto error;
1363 }
1364
1365 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1366 ua_chan->name, app->pid);
1367
1368 error:
1369 health_code_update();
1370 return ret;
1371 }
1372
1373 /*
1374 * Enable the specified channel on to UST tracer for the UST session.
1375 */
1376 static int enable_ust_channel(struct ust_app *app,
1377 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
1378 {
1379 int ret;
1380
1381 health_code_update();
1382
1383 ret = ustctl_enable(app->sock, ua_chan->obj);
1384 if (ret < 0) {
1385 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1386 ERR("UST app channel %s enable failed for app (pid: %d) "
1387 "and session handle %d with ret %d",
1388 ua_chan->name, app->pid, ua_sess->handle, ret);
1389 } else {
1390 /*
1391 * This is normal behavior, an application can die during the
1392 * creation process. Don't report an error so the execution can
1393 * continue normally.
1394 */
1395 ret = 0;
1396 DBG3("UST app enable channel failed. Application is dead.");
1397 }
1398 goto error;
1399 }
1400
1401 ua_chan->enabled = 1;
1402
1403 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1404 ua_chan->name, app->pid);
1405
1406 error:
1407 health_code_update();
1408 return ret;
1409 }
1410
1411 /*
1412 * Enable the specified event on to UST tracer for the UST session.
1413 */
1414 static int enable_ust_event(struct ust_app *app,
1415 struct ust_app_session *ua_sess, struct ust_app_event *ua_event)
1416 {
1417 int ret;
1418
1419 health_code_update();
1420
1421 ret = ustctl_enable(app->sock, ua_event->obj);
1422 if (ret < 0) {
1423 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1424 ERR("UST app event %s enable failed for app (pid: %d) "
1425 "and session handle %d with ret %d",
1426 ua_event->attr.name, app->pid, ua_sess->handle, ret);
1427 } else {
1428 /*
1429 * This is normal behavior, an application can die during the
1430 * creation process. Don't report an error so the execution can
1431 * continue normally.
1432 */
1433 ret = 0;
1434 DBG3("UST app enable event failed. Application is dead.");
1435 }
1436 goto error;
1437 }
1438
1439 DBG2("UST app event %s enabled successfully for app (pid: %d)",
1440 ua_event->attr.name, app->pid);
1441
1442 error:
1443 health_code_update();
1444 return ret;
1445 }
1446
1447 /*
1448 * Send channel and stream buffer to application.
1449 *
1450 * Return 0 on success. On error, a negative value is returned.
1451 */
1452 static int send_channel_pid_to_ust(struct ust_app *app,
1453 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
1454 {
1455 int ret;
1456 struct ust_app_stream *stream, *stmp;
1457
1458 assert(app);
1459 assert(ua_sess);
1460 assert(ua_chan);
1461
1462 health_code_update();
1463
1464 DBG("UST app sending channel %s to UST app sock %d", ua_chan->name,
1465 app->sock);
1466
1467 /* Send channel to the application. */
1468 ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan);
1469 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1470 ret = -ENOTCONN; /* Caused by app exiting. */
1471 goto error;
1472 } else if (ret < 0) {
1473 goto error;
1474 }
1475
1476 health_code_update();
1477
1478 /* Send all streams to application. */
1479 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
1480 ret = ust_consumer_send_stream_to_ust(app, ua_chan, stream);
1481 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1482 ret = -ENOTCONN; /* Caused by app exiting. */
1483 goto error;
1484 } else if (ret < 0) {
1485 goto error;
1486 }
1487 /* We don't need the stream anymore once sent to the tracer. */
1488 cds_list_del(&stream->list);
1489 delete_ust_app_stream(-1, stream);
1490 }
1491 /* Flag the channel that it is sent to the application. */
1492 ua_chan->is_sent = 1;
1493
1494 error:
1495 health_code_update();
1496 return ret;
1497 }
1498
1499 /*
1500 * Create the specified event onto the UST tracer for a UST session.
1501 *
1502 * Should be called with session mutex held.
1503 */
1504 static
1505 int create_ust_event(struct ust_app *app, struct ust_app_session *ua_sess,
1506 struct ust_app_channel *ua_chan, struct ust_app_event *ua_event)
1507 {
1508 int ret = 0;
1509
1510 health_code_update();
1511
1512 /* Create UST event on tracer */
1513 ret = ustctl_create_event(app->sock, &ua_event->attr, ua_chan->obj,
1514 &ua_event->obj);
1515 if (ret < 0) {
1516 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1517 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1518 ua_event->attr.name, app->pid, ret);
1519 } else {
1520 /*
1521 * This is normal behavior, an application can die during the
1522 * creation process. Don't report an error so the execution can
1523 * continue normally.
1524 */
1525 ret = 0;
1526 DBG3("UST app create event failed. Application is dead.");
1527 }
1528 goto error;
1529 }
1530
1531 ua_event->handle = ua_event->obj->handle;
1532
1533 DBG2("UST app event %s created successfully for pid:%d",
1534 ua_event->attr.name, app->pid);
1535
1536 health_code_update();
1537
1538 /* Set filter if one is present. */
1539 if (ua_event->filter) {
1540 ret = set_ust_event_filter(ua_event, app);
1541 if (ret < 0) {
1542 goto error;
1543 }
1544 }
1545
1546 /* Set exclusions for the event */
1547 if (ua_event->exclusion) {
1548 ret = set_ust_event_exclusion(ua_event, app);
1549 if (ret < 0) {
1550 goto error;
1551 }
1552 }
1553
1554 /* If event not enabled, disable it on the tracer */
1555 if (ua_event->enabled) {
1556 /*
1557 * We now need to explicitly enable the event, since it
1558 * is now disabled at creation.
1559 */
1560 ret = enable_ust_event(app, ua_sess, ua_event);
1561 if (ret < 0) {
1562 /*
1563 * If we hit an EPERM, something is wrong with our enable call. If
1564 * we get an EEXIST, there is a problem on the tracer side since we
1565 * just created it.
1566 */
1567 switch (ret) {
1568 case -LTTNG_UST_ERR_PERM:
1569 /* Code flow problem */
1570 assert(0);
1571 case -LTTNG_UST_ERR_EXIST:
1572 /* It's OK for our use case. */
1573 ret = 0;
1574 break;
1575 default:
1576 break;
1577 }
1578 goto error;
1579 }
1580 }
1581
1582 error:
1583 health_code_update();
1584 return ret;
1585 }
1586
1587 /*
1588 * Copy data between an UST app event and a LTT event.
1589 */
1590 static void shadow_copy_event(struct ust_app_event *ua_event,
1591 struct ltt_ust_event *uevent)
1592 {
1593 size_t exclusion_alloc_size;
1594
1595 strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name));
1596 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
1597
1598 ua_event->enabled = uevent->enabled;
1599
1600 /* Copy event attributes */
1601 memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr));
1602
1603 /* Copy filter bytecode */
1604 if (uevent->filter) {
1605 ua_event->filter = copy_filter_bytecode(uevent->filter);
1606 /* Filter might be NULL here in case of ENONEM. */
1607 }
1608
1609 /* Copy exclusion data */
1610 if (uevent->exclusion) {
1611 exclusion_alloc_size = sizeof(struct lttng_event_exclusion) +
1612 LTTNG_UST_SYM_NAME_LEN * uevent->exclusion->count;
1613 ua_event->exclusion = zmalloc(exclusion_alloc_size);
1614 if (ua_event->exclusion == NULL) {
1615 PERROR("malloc");
1616 } else {
1617 memcpy(ua_event->exclusion, uevent->exclusion,
1618 exclusion_alloc_size);
1619 }
1620 }
1621 }
1622
1623 /*
1624 * Copy data between an UST app channel and a LTT channel.
1625 */
1626 static void shadow_copy_channel(struct ust_app_channel *ua_chan,
1627 struct ltt_ust_channel *uchan)
1628 {
1629 struct lttng_ht_iter iter;
1630 struct ltt_ust_event *uevent;
1631 struct ltt_ust_context *uctx;
1632 struct ust_app_event *ua_event;
1633 struct ust_app_ctx *ua_ctx;
1634
1635 DBG2("UST app shadow copy of channel %s started", ua_chan->name);
1636
1637 strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name));
1638 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
1639
1640 ua_chan->tracefile_size = uchan->tracefile_size;
1641 ua_chan->tracefile_count = uchan->tracefile_count;
1642
1643 /* Copy event attributes since the layout is different. */
1644 ua_chan->attr.subbuf_size = uchan->attr.subbuf_size;
1645 ua_chan->attr.num_subbuf = uchan->attr.num_subbuf;
1646 ua_chan->attr.overwrite = uchan->attr.overwrite;
1647 ua_chan->attr.switch_timer_interval = uchan->attr.switch_timer_interval;
1648 ua_chan->attr.read_timer_interval = uchan->attr.read_timer_interval;
1649 ua_chan->attr.output = uchan->attr.output;
1650 /*
1651 * Note that the attribute channel type is not set since the channel on the
1652 * tracing registry side does not have this information.
1653 */
1654
1655 ua_chan->enabled = uchan->enabled;
1656 ua_chan->tracing_channel_id = uchan->id;
1657
1658 cds_list_for_each_entry(uctx, &uchan->ctx_list, list) {
1659 ua_ctx = alloc_ust_app_ctx(&uctx->ctx);
1660 if (ua_ctx == NULL) {
1661 continue;
1662 }
1663 lttng_ht_node_init_ulong(&ua_ctx->node,
1664 (unsigned long) ua_ctx->ctx.ctx);
1665 lttng_ht_add_ulong(ua_chan->ctx, &ua_ctx->node);
1666 cds_list_add_tail(&ua_ctx->list, &ua_chan->ctx_list);
1667 }
1668
1669 /* Copy all events from ltt ust channel to ust app channel */
1670 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
1671 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
1672 uevent->filter, uevent->attr.loglevel, uevent->exclusion);
1673 if (ua_event == NULL) {
1674 DBG2("UST event %s not found on shadow copy channel",
1675 uevent->attr.name);
1676 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
1677 if (ua_event == NULL) {
1678 continue;
1679 }
1680 shadow_copy_event(ua_event, uevent);
1681 add_unique_ust_app_event(ua_chan, ua_event);
1682 }
1683 }
1684
1685 DBG3("UST app shadow copy of channel %s done", ua_chan->name);
1686 }
1687
1688 /*
1689 * Copy data between a UST app session and a regular LTT session.
1690 */
1691 static void shadow_copy_session(struct ust_app_session *ua_sess,
1692 struct ltt_ust_session *usess, struct ust_app *app)
1693 {
1694 struct lttng_ht_node_str *ua_chan_node;
1695 struct lttng_ht_iter iter;
1696 struct ltt_ust_channel *uchan;
1697 struct ust_app_channel *ua_chan;
1698 time_t rawtime;
1699 struct tm *timeinfo;
1700 char datetime[16];
1701 int ret;
1702 char tmp_shm_path[PATH_MAX];
1703
1704 /* Get date and time for unique app path */
1705 time(&rawtime);
1706 timeinfo = localtime(&rawtime);
1707 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
1708
1709 DBG2("Shadow copy of session handle %d", ua_sess->handle);
1710
1711 ua_sess->tracing_id = usess->id;
1712 ua_sess->id = get_next_session_id();
1713 ua_sess->uid = app->uid;
1714 ua_sess->gid = app->gid;
1715 ua_sess->euid = usess->uid;
1716 ua_sess->egid = usess->gid;
1717 ua_sess->buffer_type = usess->buffer_type;
1718 ua_sess->bits_per_long = app->bits_per_long;
1719
1720 /* There is only one consumer object per session possible. */
1721 consumer_output_get(usess->consumer);
1722 ua_sess->consumer = usess->consumer;
1723
1724 ua_sess->output_traces = usess->output_traces;
1725 ua_sess->live_timer_interval = usess->live_timer_interval;
1726 copy_channel_attr_to_ustctl(&ua_sess->metadata_attr,
1727 &usess->metadata_attr);
1728
1729 switch (ua_sess->buffer_type) {
1730 case LTTNG_BUFFER_PER_PID:
1731 ret = snprintf(ua_sess->path, sizeof(ua_sess->path),
1732 DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s", app->name, app->pid,
1733 datetime);
1734 break;
1735 case LTTNG_BUFFER_PER_UID:
1736 ret = snprintf(ua_sess->path, sizeof(ua_sess->path),
1737 DEFAULT_UST_TRACE_UID_PATH, ua_sess->uid, app->bits_per_long);
1738 break;
1739 default:
1740 assert(0);
1741 goto error;
1742 }
1743 if (ret < 0) {
1744 PERROR("asprintf UST shadow copy session");
1745 assert(0);
1746 goto error;
1747 }
1748
1749 strncpy(ua_sess->root_shm_path, usess->root_shm_path,
1750 sizeof(ua_sess->root_shm_path));
1751 ua_sess->root_shm_path[sizeof(ua_sess->root_shm_path) - 1] = '\0';
1752 strncpy(ua_sess->shm_path, usess->shm_path,
1753 sizeof(ua_sess->shm_path));
1754 ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0';
1755 if (ua_sess->shm_path[0]) {
1756 switch (ua_sess->buffer_type) {
1757 case LTTNG_BUFFER_PER_PID:
1758 ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path),
1759 DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s",
1760 app->name, app->pid, datetime);
1761 break;
1762 case LTTNG_BUFFER_PER_UID:
1763 ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path),
1764 DEFAULT_UST_TRACE_UID_PATH,
1765 app->uid, app->bits_per_long);
1766 break;
1767 default:
1768 assert(0);
1769 goto error;
1770 }
1771 if (ret < 0) {
1772 PERROR("sprintf UST shadow copy session");
1773 assert(0);
1774 goto error;
1775 }
1776 strncat(ua_sess->shm_path, tmp_shm_path,
1777 sizeof(ua_sess->shm_path) - strlen(ua_sess->shm_path) - 1);
1778 ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0';
1779 }
1780
1781 /* Iterate over all channels in global domain. */
1782 cds_lfht_for_each_entry(usess->domain_global.channels->ht, &iter.iter,
1783 uchan, node.node) {
1784 struct lttng_ht_iter uiter;
1785
1786 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
1787 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
1788 if (ua_chan_node != NULL) {
1789 /* Session exist. Contiuing. */
1790 continue;
1791 }
1792
1793 DBG2("Channel %s not found on shadow session copy, creating it",
1794 uchan->name);
1795 ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr);
1796 if (ua_chan == NULL) {
1797 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
1798 continue;
1799 }
1800 shadow_copy_channel(ua_chan, uchan);
1801 /*
1802 * The concept of metadata channel does not exist on the tracing
1803 * registry side of the session daemon so this can only be a per CPU
1804 * channel and not metadata.
1805 */
1806 ua_chan->attr.type = LTTNG_UST_CHAN_PER_CPU;
1807
1808 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
1809 }
1810 return;
1811
1812 error:
1813 consumer_output_put(ua_sess->consumer);
1814 }
1815
1816 /*
1817 * Lookup sesison wrapper.
1818 */
1819 static
1820 void __lookup_session_by_app(struct ltt_ust_session *usess,
1821 struct ust_app *app, struct lttng_ht_iter *iter)
1822 {
1823 /* Get right UST app session from app */
1824 lttng_ht_lookup(app->sessions, &usess->id, iter);
1825 }
1826
1827 /*
1828 * Return ust app session from the app session hashtable using the UST session
1829 * id.
1830 */
1831 static struct ust_app_session *lookup_session_by_app(
1832 struct ltt_ust_session *usess, struct ust_app *app)
1833 {
1834 struct lttng_ht_iter iter;
1835 struct lttng_ht_node_u64 *node;
1836
1837 __lookup_session_by_app(usess, app, &iter);
1838 node = lttng_ht_iter_get_node_u64(&iter);
1839 if (node == NULL) {
1840 goto error;
1841 }
1842
1843 return caa_container_of(node, struct ust_app_session, node);
1844
1845 error:
1846 return NULL;
1847 }
1848
1849 /*
1850 * Setup buffer registry per PID for the given session and application. If none
1851 * is found, a new one is created, added to the global registry and
1852 * initialized. If regp is valid, it's set with the newly created object.
1853 *
1854 * Return 0 on success or else a negative value.
1855 */
1856 static int setup_buffer_reg_pid(struct ust_app_session *ua_sess,
1857 struct ust_app *app, struct buffer_reg_pid **regp)
1858 {
1859 int ret = 0;
1860 struct buffer_reg_pid *reg_pid;
1861
1862 assert(ua_sess);
1863 assert(app);
1864
1865 rcu_read_lock();
1866
1867 reg_pid = buffer_reg_pid_find(ua_sess->id);
1868 if (!reg_pid) {
1869 /*
1870 * This is the create channel path meaning that if there is NO
1871 * registry available, we have to create one for this session.
1872 */
1873 ret = buffer_reg_pid_create(ua_sess->id, &reg_pid,
1874 ua_sess->root_shm_path, ua_sess->shm_path);
1875 if (ret < 0) {
1876 goto error;
1877 }
1878 } else {
1879 goto end;
1880 }
1881
1882 /* Initialize registry. */
1883 ret = ust_registry_session_init(&reg_pid->registry->reg.ust, app,
1884 app->bits_per_long, app->uint8_t_alignment,
1885 app->uint16_t_alignment, app->uint32_t_alignment,
1886 app->uint64_t_alignment, app->long_alignment,
1887 app->byte_order, app->version.major,
1888 app->version.minor, reg_pid->root_shm_path,
1889 reg_pid->shm_path,
1890 ua_sess->euid, ua_sess->egid);
1891 if (ret < 0) {
1892 /*
1893 * reg_pid->registry->reg.ust is NULL upon error, so we need to
1894 * destroy the buffer registry, because it is always expected
1895 * that if the buffer registry can be found, its ust registry is
1896 * non-NULL.
1897 */
1898 buffer_reg_pid_destroy(reg_pid);
1899 goto error;
1900 }
1901
1902 buffer_reg_pid_add(reg_pid);
1903
1904 DBG3("UST app buffer registry per PID created successfully");
1905
1906 end:
1907 if (regp) {
1908 *regp = reg_pid;
1909 }
1910 error:
1911 rcu_read_unlock();
1912 return ret;
1913 }
1914
1915 /*
1916 * Setup buffer registry per UID for the given session and application. If none
1917 * is found, a new one is created, added to the global registry and
1918 * initialized. If regp is valid, it's set with the newly created object.
1919 *
1920 * Return 0 on success or else a negative value.
1921 */
1922 static int setup_buffer_reg_uid(struct ltt_ust_session *usess,
1923 struct ust_app_session *ua_sess,
1924 struct ust_app *app, struct buffer_reg_uid **regp)
1925 {
1926 int ret = 0;
1927 struct buffer_reg_uid *reg_uid;
1928
1929 assert(usess);
1930 assert(app);
1931
1932 rcu_read_lock();
1933
1934 reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid);
1935 if (!reg_uid) {
1936 /*
1937 * This is the create channel path meaning that if there is NO
1938 * registry available, we have to create one for this session.
1939 */
1940 ret = buffer_reg_uid_create(usess->id, app->bits_per_long, app->uid,
1941 LTTNG_DOMAIN_UST, &reg_uid,
1942 ua_sess->root_shm_path, ua_sess->shm_path);
1943 if (ret < 0) {
1944 goto error;
1945 }
1946 } else {
1947 goto end;
1948 }
1949
1950 /* Initialize registry. */
1951 ret = ust_registry_session_init(&reg_uid->registry->reg.ust, NULL,
1952 app->bits_per_long, app->uint8_t_alignment,
1953 app->uint16_t_alignment, app->uint32_t_alignment,
1954 app->uint64_t_alignment, app->long_alignment,
1955 app->byte_order, app->version.major,
1956 app->version.minor, reg_uid->root_shm_path,
1957 reg_uid->shm_path, usess->uid, usess->gid);
1958 if (ret < 0) {
1959 /*
1960 * reg_uid->registry->reg.ust is NULL upon error, so we need to
1961 * destroy the buffer registry, because it is always expected
1962 * that if the buffer registry can be found, its ust registry is
1963 * non-NULL.
1964 */
1965 buffer_reg_uid_destroy(reg_uid, NULL);
1966 goto error;
1967 }
1968 /* Add node to teardown list of the session. */
1969 cds_list_add(&reg_uid->lnode, &usess->buffer_reg_uid_list);
1970
1971 buffer_reg_uid_add(reg_uid);
1972
1973 DBG3("UST app buffer registry per UID created successfully");
1974 end:
1975 if (regp) {
1976 *regp = reg_uid;
1977 }
1978 error:
1979 rcu_read_unlock();
1980 return ret;
1981 }
1982
1983 /*
1984 * Create a session on the tracer side for the given app.
1985 *
1986 * On success, ua_sess_ptr is populated with the session pointer or else left
1987 * untouched. If the session was created, is_created is set to 1. On error,
1988 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
1989 * be NULL.
1990 *
1991 * Returns 0 on success or else a negative code which is either -ENOMEM or
1992 * -ENOTCONN which is the default code if the ustctl_create_session fails.
1993 */
1994 static int create_ust_app_session(struct ltt_ust_session *usess,
1995 struct ust_app *app, struct ust_app_session **ua_sess_ptr,
1996 int *is_created)
1997 {
1998 int ret, created = 0;
1999 struct ust_app_session *ua_sess;
2000
2001 assert(usess);
2002 assert(app);
2003 assert(ua_sess_ptr);
2004
2005 health_code_update();
2006
2007 ua_sess = lookup_session_by_app(usess, app);
2008 if (ua_sess == NULL) {
2009 DBG2("UST app pid: %d session id %" PRIu64 " not found, creating it",
2010 app->pid, usess->id);
2011 ua_sess = alloc_ust_app_session(app);
2012 if (ua_sess == NULL) {
2013 /* Only malloc can failed so something is really wrong */
2014 ret = -ENOMEM;
2015 goto error;
2016 }
2017 shadow_copy_session(ua_sess, usess, app);
2018 created = 1;
2019 }
2020
2021 switch (usess->buffer_type) {
2022 case LTTNG_BUFFER_PER_PID:
2023 /* Init local registry. */
2024 ret = setup_buffer_reg_pid(ua_sess, app, NULL);
2025 if (ret < 0) {
2026 delete_ust_app_session(-1, ua_sess, app);
2027 goto error;
2028 }
2029 break;
2030 case LTTNG_BUFFER_PER_UID:
2031 /* Look for a global registry. If none exists, create one. */
2032 ret = setup_buffer_reg_uid(usess, ua_sess, app, NULL);
2033 if (ret < 0) {
2034 delete_ust_app_session(-1, ua_sess, app);
2035 goto error;
2036 }
2037 break;
2038 default:
2039 assert(0);
2040 ret = -EINVAL;
2041 goto error;
2042 }
2043
2044 health_code_update();
2045
2046 if (ua_sess->handle == -1) {
2047 ret = ustctl_create_session(app->sock);
2048 if (ret < 0) {
2049 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
2050 ERR("Creating session for app pid %d with ret %d",
2051 app->pid, ret);
2052 } else {
2053 DBG("UST app creating session failed. Application is dead");
2054 /*
2055 * This is normal behavior, an application can die during the
2056 * creation process. Don't report an error so the execution can
2057 * continue normally. This will get flagged ENOTCONN and the
2058 * caller will handle it.
2059 */
2060 ret = 0;
2061 }
2062 delete_ust_app_session(-1, ua_sess, app);
2063 if (ret != -ENOMEM) {
2064 /*
2065 * Tracer is probably gone or got an internal error so let's
2066 * behave like it will soon unregister or not usable.
2067 */
2068 ret = -ENOTCONN;
2069 }
2070 goto error;
2071 }
2072
2073 ua_sess->handle = ret;
2074
2075 /* Add ust app session to app's HT */
2076 lttng_ht_node_init_u64(&ua_sess->node,
2077 ua_sess->tracing_id);
2078 lttng_ht_add_unique_u64(app->sessions, &ua_sess->node);
2079
2080 DBG2("UST app session created successfully with handle %d", ret);
2081 }
2082
2083 *ua_sess_ptr = ua_sess;
2084 if (is_created) {
2085 *is_created = created;
2086 }
2087
2088 /* Everything went well. */
2089 ret = 0;
2090
2091 error:
2092 health_code_update();
2093 return ret;
2094 }
2095
2096 /*
2097 * Match function for a hash table lookup of ust_app_ctx.
2098 *
2099 * It matches an ust app context based on the context type and, in the case
2100 * of perf counters, their name.
2101 */
2102 static int ht_match_ust_app_ctx(struct cds_lfht_node *node, const void *_key)
2103 {
2104 struct ust_app_ctx *ctx;
2105 const struct lttng_ust_context *key;
2106
2107 assert(node);
2108 assert(_key);
2109
2110 ctx = caa_container_of(node, struct ust_app_ctx, node.node);
2111 key = _key;
2112
2113 /* Context type */
2114 if (ctx->ctx.ctx != key->ctx) {
2115 goto no_match;
2116 }
2117
2118 /* Check the name in the case of perf thread counters. */
2119 if (key->ctx == LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER) {
2120 if (strncmp(key->u.perf_counter.name,
2121 ctx->ctx.u.perf_counter.name,
2122 sizeof(key->u.perf_counter.name))) {
2123 goto no_match;
2124 }
2125 }
2126
2127 /* Match. */
2128 return 1;
2129
2130 no_match:
2131 return 0;
2132 }
2133
2134 /*
2135 * Lookup for an ust app context from an lttng_ust_context.
2136 *
2137 * Must be called while holding RCU read side lock.
2138 * Return an ust_app_ctx object or NULL on error.
2139 */
2140 static
2141 struct ust_app_ctx *find_ust_app_context(struct lttng_ht *ht,
2142 struct lttng_ust_context *uctx)
2143 {
2144 struct lttng_ht_iter iter;
2145 struct lttng_ht_node_ulong *node;
2146 struct ust_app_ctx *app_ctx = NULL;
2147
2148 assert(uctx);
2149 assert(ht);
2150
2151 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2152 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) uctx->ctx, lttng_ht_seed),
2153 ht_match_ust_app_ctx, uctx, &iter.iter);
2154 node = lttng_ht_iter_get_node_ulong(&iter);
2155 if (!node) {
2156 goto end;
2157 }
2158
2159 app_ctx = caa_container_of(node, struct ust_app_ctx, node);
2160
2161 end:
2162 return app_ctx;
2163 }
2164
2165 /*
2166 * Create a context for the channel on the tracer.
2167 *
2168 * Called with UST app session lock held and a RCU read side lock.
2169 */
2170 static
2171 int create_ust_app_channel_context(struct ust_app_session *ua_sess,
2172 struct ust_app_channel *ua_chan, struct lttng_ust_context *uctx,
2173 struct ust_app *app)
2174 {
2175 int ret = 0;
2176 struct ust_app_ctx *ua_ctx;
2177
2178 DBG2("UST app adding context to channel %s", ua_chan->name);
2179
2180 ua_ctx = find_ust_app_context(ua_chan->ctx, uctx);
2181 if (ua_ctx) {
2182 ret = -EEXIST;
2183 goto error;
2184 }
2185
2186 ua_ctx = alloc_ust_app_ctx(uctx);
2187 if (ua_ctx == NULL) {
2188 /* malloc failed */
2189 ret = -1;
2190 goto error;
2191 }
2192
2193 lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx);
2194 lttng_ht_add_ulong(ua_chan->ctx, &ua_ctx->node);
2195 cds_list_add_tail(&ua_ctx->list, &ua_chan->ctx_list);
2196
2197 ret = create_ust_channel_context(ua_chan, ua_ctx, app);
2198 if (ret < 0) {
2199 goto error;
2200 }
2201
2202 error:
2203 return ret;
2204 }
2205
2206 /*
2207 * Enable on the tracer side a ust app event for the session and channel.
2208 *
2209 * Called with UST app session lock held.
2210 */
2211 static
2212 int enable_ust_app_event(struct ust_app_session *ua_sess,
2213 struct ust_app_event *ua_event, struct ust_app *app)
2214 {
2215 int ret;
2216
2217 ret = enable_ust_event(app, ua_sess, ua_event);
2218 if (ret < 0) {
2219 goto error;
2220 }
2221
2222 ua_event->enabled = 1;
2223
2224 error:
2225 return ret;
2226 }
2227
2228 /*
2229 * Disable on the tracer side a ust app event for the session and channel.
2230 */
2231 static int disable_ust_app_event(struct ust_app_session *ua_sess,
2232 struct ust_app_event *ua_event, struct ust_app *app)
2233 {
2234 int ret;
2235
2236 ret = disable_ust_event(app, ua_sess, ua_event);
2237 if (ret < 0) {
2238 goto error;
2239 }
2240
2241 ua_event->enabled = 0;
2242
2243 error:
2244 return ret;
2245 }
2246
2247 /*
2248 * Lookup ust app channel for session and disable it on the tracer side.
2249 */
2250 static
2251 int disable_ust_app_channel(struct ust_app_session *ua_sess,
2252 struct ust_app_channel *ua_chan, struct ust_app *app)
2253 {
2254 int ret;
2255
2256 ret = disable_ust_channel(app, ua_sess, ua_chan);
2257 if (ret < 0) {
2258 goto error;
2259 }
2260
2261 ua_chan->enabled = 0;
2262
2263 error:
2264 return ret;
2265 }
2266
2267 /*
2268 * Lookup ust app channel for session and enable it on the tracer side. This
2269 * MUST be called with a RCU read side lock acquired.
2270 */
2271 static int enable_ust_app_channel(struct ust_app_session *ua_sess,
2272 struct ltt_ust_channel *uchan, struct ust_app *app)
2273 {
2274 int ret = 0;
2275 struct lttng_ht_iter iter;
2276 struct lttng_ht_node_str *ua_chan_node;
2277 struct ust_app_channel *ua_chan;
2278
2279 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
2280 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
2281 if (ua_chan_node == NULL) {
2282 DBG2("Unable to find channel %s in ust session id %" PRIu64,
2283 uchan->name, ua_sess->tracing_id);
2284 goto error;
2285 }
2286
2287 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2288
2289 ret = enable_ust_channel(app, ua_sess, ua_chan);
2290 if (ret < 0) {
2291 goto error;
2292 }
2293
2294 error:
2295 return ret;
2296 }
2297
2298 /*
2299 * Ask the consumer to create a channel and get it if successful.
2300 *
2301 * Return 0 on success or else a negative value.
2302 */
2303 static int do_consumer_create_channel(struct ltt_ust_session *usess,
2304 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan,
2305 int bitness, struct ust_registry_session *registry)
2306 {
2307 int ret;
2308 unsigned int nb_fd = 0;
2309 struct consumer_socket *socket;
2310
2311 assert(usess);
2312 assert(ua_sess);
2313 assert(ua_chan);
2314 assert(registry);
2315
2316 rcu_read_lock();
2317 health_code_update();
2318
2319 /* Get the right consumer socket for the application. */
2320 socket = consumer_find_socket_by_bitness(bitness, usess->consumer);
2321 if (!socket) {
2322 ret = -EINVAL;
2323 goto error;
2324 }
2325
2326 health_code_update();
2327
2328 /* Need one fd for the channel. */
2329 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
2330 if (ret < 0) {
2331 ERR("Exhausted number of available FD upon create channel");
2332 goto error;
2333 }
2334
2335 /*
2336 * Ask consumer to create channel. The consumer will return the number of
2337 * stream we have to expect.
2338 */
2339 ret = ust_consumer_ask_channel(ua_sess, ua_chan, usess->consumer, socket,
2340 registry);
2341 if (ret < 0) {
2342 goto error_ask;
2343 }
2344
2345 /*
2346 * Compute the number of fd needed before receiving them. It must be 2 per
2347 * stream (2 being the default value here).
2348 */
2349 nb_fd = DEFAULT_UST_STREAM_FD_NUM * ua_chan->expected_stream_count;
2350
2351 /* Reserve the amount of file descriptor we need. */
2352 ret = lttng_fd_get(LTTNG_FD_APPS, nb_fd);
2353 if (ret < 0) {
2354 ERR("Exhausted number of available FD upon create channel");
2355 goto error_fd_get_stream;
2356 }
2357
2358 health_code_update();
2359
2360 /*
2361 * Now get the channel from the consumer. This call wil populate the stream
2362 * list of that channel and set the ust objects.
2363 */
2364 if (usess->consumer->enabled) {
2365 ret = ust_consumer_get_channel(socket, ua_chan);
2366 if (ret < 0) {
2367 goto error_destroy;
2368 }
2369 }
2370
2371 rcu_read_unlock();
2372 return 0;
2373
2374 error_destroy:
2375 lttng_fd_put(LTTNG_FD_APPS, nb_fd);
2376 error_fd_get_stream:
2377 /*
2378 * Initiate a destroy channel on the consumer since we had an error
2379 * handling it on our side. The return value is of no importance since we
2380 * already have a ret value set by the previous error that we need to
2381 * return.
2382 */
2383 (void) ust_consumer_destroy_channel(socket, ua_chan);
2384 error_ask:
2385 lttng_fd_put(LTTNG_FD_APPS, 1);
2386 error:
2387 health_code_update();
2388 rcu_read_unlock();
2389 return ret;
2390 }
2391
2392 /*
2393 * Duplicate the ust data object of the ust app stream and save it in the
2394 * buffer registry stream.
2395 *
2396 * Return 0 on success or else a negative value.
2397 */
2398 static int duplicate_stream_object(struct buffer_reg_stream *reg_stream,
2399 struct ust_app_stream *stream)
2400 {
2401 int ret;
2402
2403 assert(reg_stream);
2404 assert(stream);
2405
2406 /* Reserve the amount of file descriptor we need. */
2407 ret = lttng_fd_get(LTTNG_FD_APPS, 2);
2408 if (ret < 0) {
2409 ERR("Exhausted number of available FD upon duplicate stream");
2410 goto error;
2411 }
2412
2413 /* Duplicate object for stream once the original is in the registry. */
2414 ret = ustctl_duplicate_ust_object_data(&stream->obj,
2415 reg_stream->obj.ust);
2416 if (ret < 0) {
2417 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2418 reg_stream->obj.ust, stream->obj, ret);
2419 lttng_fd_put(LTTNG_FD_APPS, 2);
2420 goto error;
2421 }
2422 stream->handle = stream->obj->handle;
2423
2424 error:
2425 return ret;
2426 }
2427
2428 /*
2429 * Duplicate the ust data object of the ust app. channel and save it in the
2430 * buffer registry channel.
2431 *
2432 * Return 0 on success or else a negative value.
2433 */
2434 static int duplicate_channel_object(struct buffer_reg_channel *reg_chan,
2435 struct ust_app_channel *ua_chan)
2436 {
2437 int ret;
2438
2439 assert(reg_chan);
2440 assert(ua_chan);
2441
2442 /* Need two fds for the channel. */
2443 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
2444 if (ret < 0) {
2445 ERR("Exhausted number of available FD upon duplicate channel");
2446 goto error_fd_get;
2447 }
2448
2449 /* Duplicate object for stream once the original is in the registry. */
2450 ret = ustctl_duplicate_ust_object_data(&ua_chan->obj, reg_chan->obj.ust);
2451 if (ret < 0) {
2452 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2453 reg_chan->obj.ust, ua_chan->obj, ret);
2454 goto error;
2455 }
2456 ua_chan->handle = ua_chan->obj->handle;
2457
2458 return 0;
2459
2460 error:
2461 lttng_fd_put(LTTNG_FD_APPS, 1);
2462 error_fd_get:
2463 return ret;
2464 }
2465
2466 /*
2467 * For a given channel buffer registry, setup all streams of the given ust
2468 * application channel.
2469 *
2470 * Return 0 on success or else a negative value.
2471 */
2472 static int setup_buffer_reg_streams(struct buffer_reg_channel *reg_chan,
2473 struct ust_app_channel *ua_chan)
2474 {
2475 int ret = 0;
2476 struct ust_app_stream *stream, *stmp;
2477
2478 assert(reg_chan);
2479 assert(ua_chan);
2480
2481 DBG2("UST app setup buffer registry stream");
2482
2483 /* Send all streams to application. */
2484 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
2485 struct buffer_reg_stream *reg_stream;
2486
2487 ret = buffer_reg_stream_create(&reg_stream);
2488 if (ret < 0) {
2489 goto error;
2490 }
2491
2492 /*
2493 * Keep original pointer and nullify it in the stream so the delete
2494 * stream call does not release the object.
2495 */
2496 reg_stream->obj.ust = stream->obj;
2497 stream->obj = NULL;
2498 buffer_reg_stream_add(reg_stream, reg_chan);
2499
2500 /* We don't need the streams anymore. */
2501 cds_list_del(&stream->list);
2502 delete_ust_app_stream(-1, stream);
2503 }
2504
2505 error:
2506 return ret;
2507 }
2508
2509 /*
2510 * Create a buffer registry channel for the given session registry and
2511 * application channel object. If regp pointer is valid, it's set with the
2512 * created object. Important, the created object is NOT added to the session
2513 * registry hash table.
2514 *
2515 * Return 0 on success else a negative value.
2516 */
2517 static int create_buffer_reg_channel(struct buffer_reg_session *reg_sess,
2518 struct ust_app_channel *ua_chan, struct buffer_reg_channel **regp)
2519 {
2520 int ret;
2521 struct buffer_reg_channel *reg_chan = NULL;
2522
2523 assert(reg_sess);
2524 assert(ua_chan);
2525
2526 DBG2("UST app creating buffer registry channel for %s", ua_chan->name);
2527
2528 /* Create buffer registry channel. */
2529 ret = buffer_reg_channel_create(ua_chan->tracing_channel_id, &reg_chan);
2530 if (ret < 0) {
2531 goto error_create;
2532 }
2533 assert(reg_chan);
2534 reg_chan->consumer_key = ua_chan->key;
2535 reg_chan->subbuf_size = ua_chan->attr.subbuf_size;
2536 reg_chan->num_subbuf = ua_chan->attr.num_subbuf;
2537
2538 /* Create and add a channel registry to session. */
2539 ret = ust_registry_channel_add(reg_sess->reg.ust,
2540 ua_chan->tracing_channel_id);
2541 if (ret < 0) {
2542 goto error;
2543 }
2544 buffer_reg_channel_add(reg_sess, reg_chan);
2545
2546 if (regp) {
2547 *regp = reg_chan;
2548 }
2549
2550 return 0;
2551
2552 error:
2553 /* Safe because the registry channel object was not added to any HT. */
2554 buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST);
2555 error_create:
2556 return ret;
2557 }
2558
2559 /*
2560 * Setup buffer registry channel for the given session registry and application
2561 * channel object. If regp pointer is valid, it's set with the created object.
2562 *
2563 * Return 0 on success else a negative value.
2564 */
2565 static int setup_buffer_reg_channel(struct buffer_reg_session *reg_sess,
2566 struct ust_app_channel *ua_chan, struct buffer_reg_channel *reg_chan)
2567 {
2568 int ret;
2569
2570 assert(reg_sess);
2571 assert(reg_chan);
2572 assert(ua_chan);
2573 assert(ua_chan->obj);
2574
2575 DBG2("UST app setup buffer registry channel for %s", ua_chan->name);
2576
2577 /* Setup all streams for the registry. */
2578 ret = setup_buffer_reg_streams(reg_chan, ua_chan);
2579 if (ret < 0) {
2580 goto error;
2581 }
2582
2583 reg_chan->obj.ust = ua_chan->obj;
2584 ua_chan->obj = NULL;
2585
2586 return 0;
2587
2588 error:
2589 buffer_reg_channel_remove(reg_sess, reg_chan);
2590 buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST);
2591 return ret;
2592 }
2593
2594 /*
2595 * Send buffer registry channel to the application.
2596 *
2597 * Return 0 on success else a negative value.
2598 */
2599 static int send_channel_uid_to_ust(struct buffer_reg_channel *reg_chan,
2600 struct ust_app *app, struct ust_app_session *ua_sess,
2601 struct ust_app_channel *ua_chan)
2602 {
2603 int ret;
2604 struct buffer_reg_stream *reg_stream;
2605
2606 assert(reg_chan);
2607 assert(app);
2608 assert(ua_sess);
2609 assert(ua_chan);
2610
2611 DBG("UST app sending buffer registry channel to ust sock %d", app->sock);
2612
2613 ret = duplicate_channel_object(reg_chan, ua_chan);
2614 if (ret < 0) {
2615 goto error;
2616 }
2617
2618 /* Send channel to the application. */
2619 ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan);
2620 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
2621 ret = -ENOTCONN; /* Caused by app exiting. */
2622 goto error;
2623 } else if (ret < 0) {
2624 goto error;
2625 }
2626
2627 health_code_update();
2628
2629 /* Send all streams to application. */
2630 pthread_mutex_lock(&reg_chan->stream_list_lock);
2631 cds_list_for_each_entry(reg_stream, &reg_chan->streams, lnode) {
2632 struct ust_app_stream stream;
2633
2634 ret = duplicate_stream_object(reg_stream, &stream);
2635 if (ret < 0) {
2636 goto error_stream_unlock;
2637 }
2638
2639 ret = ust_consumer_send_stream_to_ust(app, ua_chan, &stream);
2640 if (ret < 0) {
2641 (void) release_ust_app_stream(-1, &stream);
2642 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
2643 ret = -ENOTCONN; /* Caused by app exiting. */
2644 goto error_stream_unlock;
2645 } else if (ret < 0) {
2646 goto error_stream_unlock;
2647 }
2648 goto error_stream_unlock;
2649 }
2650
2651 /*
2652 * The return value is not important here. This function will output an
2653 * error if needed.
2654 */
2655 (void) release_ust_app_stream(-1, &stream);
2656 }
2657 ua_chan->is_sent = 1;
2658
2659 error_stream_unlock:
2660 pthread_mutex_unlock(&reg_chan->stream_list_lock);
2661 error:
2662 return ret;
2663 }
2664
2665 /*
2666 * Create and send to the application the created buffers with per UID buffers.
2667 *
2668 * Return 0 on success else a negative value.
2669 */
2670 static int create_channel_per_uid(struct ust_app *app,
2671 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
2672 struct ust_app_channel *ua_chan)
2673 {
2674 int ret;
2675 struct buffer_reg_uid *reg_uid;
2676 struct buffer_reg_channel *reg_chan;
2677
2678 assert(app);
2679 assert(usess);
2680 assert(ua_sess);
2681 assert(ua_chan);
2682
2683 DBG("UST app creating channel %s with per UID buffers", ua_chan->name);
2684
2685 reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid);
2686 /*
2687 * The session creation handles the creation of this global registry
2688 * object. If none can be find, there is a code flow problem or a
2689 * teardown race.
2690 */
2691 assert(reg_uid);
2692
2693 reg_chan = buffer_reg_channel_find(ua_chan->tracing_channel_id,
2694 reg_uid);
2695 if (!reg_chan) {
2696 /* Create the buffer registry channel object. */
2697 ret = create_buffer_reg_channel(reg_uid->registry, ua_chan, &reg_chan);
2698 if (ret < 0) {
2699 ERR("Error creating the UST channel \"%s\" registry instance",
2700 ua_chan->name);
2701 goto error;
2702 }
2703 assert(reg_chan);
2704
2705 /*
2706 * Create the buffers on the consumer side. This call populates the
2707 * ust app channel object with all streams and data object.
2708 */
2709 ret = do_consumer_create_channel(usess, ua_sess, ua_chan,
2710 app->bits_per_long, reg_uid->registry->reg.ust);
2711 if (ret < 0) {
2712 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2713 ua_chan->name);
2714
2715 /*
2716 * Let's remove the previously created buffer registry channel so
2717 * it's not visible anymore in the session registry.
2718 */
2719 ust_registry_channel_del_free(reg_uid->registry->reg.ust,
2720 ua_chan->tracing_channel_id);
2721 buffer_reg_channel_remove(reg_uid->registry, reg_chan);
2722 buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST);
2723 goto error;
2724 }
2725
2726 /*
2727 * Setup the streams and add it to the session registry.
2728 */
2729 ret = setup_buffer_reg_channel(reg_uid->registry, ua_chan, reg_chan);
2730 if (ret < 0) {
2731 ERR("Error setting up UST channel \"%s\"",
2732 ua_chan->name);
2733 goto error;
2734 }
2735
2736 }
2737
2738 /* Send buffers to the application. */
2739 ret = send_channel_uid_to_ust(reg_chan, app, ua_sess, ua_chan);
2740 if (ret < 0) {
2741 if (ret != -ENOTCONN) {
2742 ERR("Error sending channel to application");
2743 }
2744 goto error;
2745 }
2746
2747 error:
2748 return ret;
2749 }
2750
2751 /*
2752 * Create and send to the application the created buffers with per PID buffers.
2753 *
2754 * Return 0 on success else a negative value.
2755 */
2756 static int create_channel_per_pid(struct ust_app *app,
2757 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
2758 struct ust_app_channel *ua_chan)
2759 {
2760 int ret;
2761 struct ust_registry_session *registry;
2762
2763 assert(app);
2764 assert(usess);
2765 assert(ua_sess);
2766 assert(ua_chan);
2767
2768 DBG("UST app creating channel %s with per PID buffers", ua_chan->name);
2769
2770 rcu_read_lock();
2771
2772 registry = get_session_registry(ua_sess);
2773 assert(registry);
2774
2775 /* Create and add a new channel registry to session. */
2776 ret = ust_registry_channel_add(registry, ua_chan->key);
2777 if (ret < 0) {
2778 ERR("Error creating the UST channel \"%s\" registry instance",
2779 ua_chan->name);
2780 goto error;
2781 }
2782
2783 /* Create and get channel on the consumer side. */
2784 ret = do_consumer_create_channel(usess, ua_sess, ua_chan,
2785 app->bits_per_long, registry);
2786 if (ret < 0) {
2787 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2788 ua_chan->name);
2789 goto error;
2790 }
2791
2792 ret = send_channel_pid_to_ust(app, ua_sess, ua_chan);
2793 if (ret < 0) {
2794 if (ret != -ENOTCONN) {
2795 ERR("Error sending channel to application");
2796 }
2797 goto error;
2798 }
2799
2800 error:
2801 rcu_read_unlock();
2802 return ret;
2803 }
2804
2805 /*
2806 * From an already allocated ust app channel, create the channel buffers if
2807 * need and send it to the application. This MUST be called with a RCU read
2808 * side lock acquired.
2809 *
2810 * Return 0 on success or else a negative value. Returns -ENOTCONN if
2811 * the application exited concurrently.
2812 */
2813 static int do_create_channel(struct ust_app *app,
2814 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
2815 struct ust_app_channel *ua_chan)
2816 {
2817 int ret;
2818
2819 assert(app);
2820 assert(usess);
2821 assert(ua_sess);
2822 assert(ua_chan);
2823
2824 /* Handle buffer type before sending the channel to the application. */
2825 switch (usess->buffer_type) {
2826 case LTTNG_BUFFER_PER_UID:
2827 {
2828 ret = create_channel_per_uid(app, usess, ua_sess, ua_chan);
2829 if (ret < 0) {
2830 goto error;
2831 }
2832 break;
2833 }
2834 case LTTNG_BUFFER_PER_PID:
2835 {
2836 ret = create_channel_per_pid(app, usess, ua_sess, ua_chan);
2837 if (ret < 0) {
2838 goto error;
2839 }
2840 break;
2841 }
2842 default:
2843 assert(0);
2844 ret = -EINVAL;
2845 goto error;
2846 }
2847
2848 /* Initialize ust objd object using the received handle and add it. */
2849 lttng_ht_node_init_ulong(&ua_chan->ust_objd_node, ua_chan->handle);
2850 lttng_ht_add_unique_ulong(app->ust_objd, &ua_chan->ust_objd_node);
2851
2852 /* If channel is not enabled, disable it on the tracer */
2853 if (!ua_chan->enabled) {
2854 ret = disable_ust_channel(app, ua_sess, ua_chan);
2855 if (ret < 0) {
2856 goto error;
2857 }
2858 }
2859
2860 error:
2861 return ret;
2862 }
2863
2864 /*
2865 * Create UST app channel and create it on the tracer. Set ua_chanp of the
2866 * newly created channel if not NULL.
2867 *
2868 * Called with UST app session lock and RCU read-side lock held.
2869 *
2870 * Return 0 on success or else a negative value. Returns -ENOTCONN if
2871 * the application exited concurrently.
2872 */
2873 static int create_ust_app_channel(struct ust_app_session *ua_sess,
2874 struct ltt_ust_channel *uchan, struct ust_app *app,
2875 enum lttng_ust_chan_type type, struct ltt_ust_session *usess,
2876 struct ust_app_channel **ua_chanp)
2877 {
2878 int ret = 0;
2879 struct lttng_ht_iter iter;
2880 struct lttng_ht_node_str *ua_chan_node;
2881 struct ust_app_channel *ua_chan;
2882
2883 /* Lookup channel in the ust app session */
2884 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
2885 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
2886 if (ua_chan_node != NULL) {
2887 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2888 goto end;
2889 }
2890
2891 ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr);
2892 if (ua_chan == NULL) {
2893 /* Only malloc can fail here */
2894 ret = -ENOMEM;
2895 goto error_alloc;
2896 }
2897 shadow_copy_channel(ua_chan, uchan);
2898
2899 /* Set channel type. */
2900 ua_chan->attr.type = type;
2901
2902 ret = do_create_channel(app, usess, ua_sess, ua_chan);
2903 if (ret < 0) {
2904 goto error;
2905 }
2906
2907 DBG2("UST app create channel %s for PID %d completed", ua_chan->name,
2908 app->pid);
2909
2910 /* Only add the channel if successful on the tracer side. */
2911 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
2912
2913 end:
2914 if (ua_chanp) {
2915 *ua_chanp = ua_chan;
2916 }
2917
2918 /* Everything went well. */
2919 return 0;
2920
2921 error:
2922 delete_ust_app_channel(ua_chan->is_sent ? app->sock : -1, ua_chan, app);
2923 error_alloc:
2924 return ret;
2925 }
2926
2927 /*
2928 * Create UST app event and create it on the tracer side.
2929 *
2930 * Called with ust app session mutex held.
2931 */
2932 static
2933 int create_ust_app_event(struct ust_app_session *ua_sess,
2934 struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent,
2935 struct ust_app *app)
2936 {
2937 int ret = 0;
2938 struct ust_app_event *ua_event;
2939
2940 /* Get event node */
2941 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
2942 uevent->filter, uevent->attr.loglevel, uevent->exclusion);
2943 if (ua_event != NULL) {
2944 ret = -EEXIST;
2945 goto end;
2946 }
2947
2948 /* Does not exist so create one */
2949 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
2950 if (ua_event == NULL) {
2951 /* Only malloc can failed so something is really wrong */
2952 ret = -ENOMEM;
2953 goto end;
2954 }
2955 shadow_copy_event(ua_event, uevent);
2956
2957 /* Create it on the tracer side */
2958 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
2959 if (ret < 0) {
2960 /* Not found previously means that it does not exist on the tracer */
2961 assert(ret != -LTTNG_UST_ERR_EXIST);
2962 goto error;
2963 }
2964
2965 add_unique_ust_app_event(ua_chan, ua_event);
2966
2967 DBG2("UST app create event %s for PID %d completed", ua_event->name,
2968 app->pid);
2969
2970 end:
2971 return ret;
2972
2973 error:
2974 /* Valid. Calling here is already in a read side lock */
2975 delete_ust_app_event(-1, ua_event);
2976 return ret;
2977 }
2978
2979 /*
2980 * Create UST metadata and open it on the tracer side.
2981 *
2982 * Called with UST app session lock held and RCU read side lock.
2983 */
2984 static int create_ust_app_metadata(struct ust_app_session *ua_sess,
2985 struct ust_app *app, struct consumer_output *consumer)
2986 {
2987 int ret = 0;
2988 struct ust_app_channel *metadata;
2989 struct consumer_socket *socket;
2990 struct ust_registry_session *registry;
2991
2992 assert(ua_sess);
2993 assert(app);
2994 assert(consumer);
2995
2996 registry = get_session_registry(ua_sess);
2997 assert(registry);
2998
2999 pthread_mutex_lock(&registry->lock);
3000
3001 /* Metadata already exists for this registry or it was closed previously */
3002 if (registry->metadata_key || registry->metadata_closed) {
3003 ret = 0;
3004 goto error;
3005 }
3006
3007 /* Allocate UST metadata */
3008 metadata = alloc_ust_app_channel(DEFAULT_METADATA_NAME, ua_sess, NULL);
3009 if (!metadata) {
3010 /* malloc() failed */
3011 ret = -ENOMEM;
3012 goto error;
3013 }
3014
3015 memcpy(&metadata->attr, &ua_sess->metadata_attr, sizeof(metadata->attr));
3016
3017 /* Need one fd for the channel. */
3018 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
3019 if (ret < 0) {
3020 ERR("Exhausted number of available FD upon create metadata");
3021 goto error;
3022 }
3023
3024 /* Get the right consumer socket for the application. */
3025 socket = consumer_find_socket_by_bitness(app->bits_per_long, consumer);
3026 if (!socket) {
3027 ret = -EINVAL;
3028 goto error_consumer;
3029 }
3030
3031 /*
3032 * Keep metadata key so we can identify it on the consumer side. Assign it
3033 * to the registry *before* we ask the consumer so we avoid the race of the
3034 * consumer requesting the metadata and the ask_channel call on our side
3035 * did not returned yet.
3036 */
3037 registry->metadata_key = metadata->key;
3038
3039 /*
3040 * Ask the metadata channel creation to the consumer. The metadata object
3041 * will be created by the consumer and kept their. However, the stream is
3042 * never added or monitored until we do a first push metadata to the
3043 * consumer.
3044 */
3045 ret = ust_consumer_ask_channel(ua_sess, metadata, consumer, socket,
3046 registry);
3047 if (ret < 0) {
3048 /* Nullify the metadata key so we don't try to close it later on. */
3049 registry->metadata_key = 0;
3050 goto error_consumer;
3051 }
3052
3053 /*
3054 * The setup command will make the metadata stream be sent to the relayd,
3055 * if applicable, and the thread managing the metadatas. This is important
3056 * because after this point, if an error occurs, the only way the stream
3057 * can be deleted is to be monitored in the consumer.
3058 */
3059 ret = consumer_setup_metadata(socket, metadata->key);
3060 if (ret < 0) {
3061 /* Nullify the metadata key so we don't try to close it later on. */
3062 registry->metadata_key = 0;
3063 goto error_consumer;
3064 }
3065
3066 DBG2("UST metadata with key %" PRIu64 " created for app pid %d",
3067 metadata->key, app->pid);
3068
3069 error_consumer:
3070 lttng_fd_put(LTTNG_FD_APPS, 1);
3071 delete_ust_app_channel(-1, metadata, app);
3072 error:
3073 pthread_mutex_unlock(&registry->lock);
3074 return ret;
3075 }
3076
3077 /*
3078 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3079 * acquired before calling this function.
3080 */
3081 struct ust_app *ust_app_find_by_pid(pid_t pid)
3082 {
3083 struct ust_app *app = NULL;
3084 struct lttng_ht_node_ulong *node;
3085 struct lttng_ht_iter iter;
3086
3087 lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) pid), &iter);
3088 node = lttng_ht_iter_get_node_ulong(&iter);
3089 if (node == NULL) {
3090 DBG2("UST app no found with pid %d", pid);
3091 goto error;
3092 }
3093
3094 DBG2("Found UST app by pid %d", pid);
3095
3096 app = caa_container_of(node, struct ust_app, pid_n);
3097
3098 error:
3099 return app;
3100 }
3101
3102 /*
3103 * Allocate and init an UST app object using the registration information and
3104 * the command socket. This is called when the command socket connects to the
3105 * session daemon.
3106 *
3107 * The object is returned on success or else NULL.
3108 */
3109 struct ust_app *ust_app_create(struct ust_register_msg *msg, int sock)
3110 {
3111 struct ust_app *lta = NULL;
3112
3113 assert(msg);
3114 assert(sock >= 0);
3115
3116 DBG3("UST app creating application for socket %d", sock);
3117
3118 if ((msg->bits_per_long == 64 &&
3119 (uatomic_read(&ust_consumerd64_fd) == -EINVAL))
3120 || (msg->bits_per_long == 32 &&
3121 (uatomic_read(&ust_consumerd32_fd) == -EINVAL))) {
3122 ERR("Registration failed: application \"%s\" (pid: %d) has "
3123 "%d-bit long, but no consumerd for this size is available.\n",
3124 msg->name, msg->pid, msg->bits_per_long);
3125 goto error;
3126 }
3127
3128 lta = zmalloc(sizeof(struct ust_app));
3129 if (lta == NULL) {
3130 PERROR("malloc");
3131 goto error;
3132 }
3133
3134 lta->ppid = msg->ppid;
3135 lta->uid = msg->uid;
3136 lta->gid = msg->gid;
3137
3138 lta->bits_per_long = msg->bits_per_long;
3139 lta->uint8_t_alignment = msg->uint8_t_alignment;
3140 lta->uint16_t_alignment = msg->uint16_t_alignment;
3141 lta->uint32_t_alignment = msg->uint32_t_alignment;
3142 lta->uint64_t_alignment = msg->uint64_t_alignment;
3143 lta->long_alignment = msg->long_alignment;
3144 lta->byte_order = msg->byte_order;
3145
3146 lta->v_major = msg->major;
3147 lta->v_minor = msg->minor;
3148 lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3149 lta->ust_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
3150 lta->notify_sock = -1;
3151
3152 /* Copy name and make sure it's NULL terminated. */
3153 strncpy(lta->name, msg->name, sizeof(lta->name));
3154 lta->name[UST_APP_PROCNAME_LEN] = '\0';
3155
3156 /*
3157 * Before this can be called, when receiving the registration information,
3158 * the application compatibility is checked. So, at this point, the
3159 * application can work with this session daemon.
3160 */
3161 lta->compatible = 1;
3162
3163 lta->pid = msg->pid;
3164 lttng_ht_node_init_ulong(&lta->pid_n, (unsigned long) lta->pid);
3165 lta->sock = sock;
3166 lttng_ht_node_init_ulong(&lta->sock_n, (unsigned long) lta->sock);
3167
3168 CDS_INIT_LIST_HEAD(&lta->teardown_head);
3169
3170 error:
3171 return lta;
3172 }
3173
3174 /*
3175 * For a given application object, add it to every hash table.
3176 */
3177 void ust_app_add(struct ust_app *app)
3178 {
3179 assert(app);
3180 assert(app->notify_sock >= 0);
3181
3182 rcu_read_lock();
3183
3184 /*
3185 * On a re-registration, we want to kick out the previous registration of
3186 * that pid
3187 */
3188 lttng_ht_add_replace_ulong(ust_app_ht, &app->pid_n);
3189
3190 /*
3191 * The socket _should_ be unique until _we_ call close. So, a add_unique
3192 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3193 * already in the table.
3194 */
3195 lttng_ht_add_unique_ulong(ust_app_ht_by_sock, &app->sock_n);
3196
3197 /* Add application to the notify socket hash table. */
3198 lttng_ht_node_init_ulong(&app->notify_sock_n, app->notify_sock);
3199 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock, &app->notify_sock_n);
3200
3201 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3202 "notify_sock:%d (version %d.%d)", app->pid, app->ppid, app->uid,
3203 app->gid, app->sock, app->name, app->notify_sock, app->v_major,
3204 app->v_minor);
3205
3206 rcu_read_unlock();
3207 }
3208
3209 /*
3210 * Set the application version into the object.
3211 *
3212 * Return 0 on success else a negative value either an errno code or a
3213 * LTTng-UST error code.
3214 */
3215 int ust_app_version(struct ust_app *app)
3216 {
3217 int ret;
3218
3219 assert(app);
3220
3221 ret = ustctl_tracer_version(app->sock, &app->version);
3222 if (ret < 0) {
3223 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
3224 ERR("UST app %d version failed with ret %d", app->sock, ret);
3225 } else {
3226 DBG3("UST app %d version failed. Application is dead", app->sock);
3227 }
3228 }
3229
3230 return ret;
3231 }
3232
3233 /*
3234 * Unregister app by removing it from the global traceable app list and freeing
3235 * the data struct.
3236 *
3237 * The socket is already closed at this point so no close to sock.
3238 */
3239 void ust_app_unregister