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