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