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