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