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