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