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