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