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