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