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