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