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