2dd69495efaf095b633247b4f24b849a8571076f
[lttng-tools.git] / src / bin / lttng-sessiond / ust-app.c
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
44 static
45 int ust_app_flush_app_session(struct ust_app *app, struct ust_app_session *ua_sess);
46
47 /* Next available channel key. Access under next_channel_key_lock. */
48 static uint64_t _next_channel_key;
49 static pthread_mutex_t next_channel_key_lock = PTHREAD_MUTEX_INITIALIZER;
50
51 /* Next available session ID. Access under next_session_id_lock. */
52 static uint64_t _next_session_id;
53 static pthread_mutex_t next_session_id_lock = PTHREAD_MUTEX_INITIALIZER;
54
55 /*
56 * Return the incremented value of next_channel_key.
57 */
58 static uint64_t get_next_channel_key(void)
59 {
60 uint64_t ret;
61
62 pthread_mutex_lock(&next_channel_key_lock);
63 ret = ++_next_channel_key;
64 pthread_mutex_unlock(&next_channel_key_lock);
65 return ret;
66 }
67
68 /*
69 * Return the atomically incremented value of next_session_id.
70 */
71 static uint64_t get_next_session_id(void)
72 {
73 uint64_t ret;
74
75 pthread_mutex_lock(&next_session_id_lock);
76 ret = ++_next_session_id;
77 pthread_mutex_unlock(&next_session_id_lock);
78 return ret;
79 }
80
81 static void copy_channel_attr_to_ustctl(
82 struct ustctl_consumer_channel_attr *attr,
83 struct lttng_ust_channel_attr *uattr)
84 {
85 /* Copy event attributes since the layout is different. */
86 attr->subbuf_size = uattr->subbuf_size;
87 attr->num_subbuf = uattr->num_subbuf;
88 attr->overwrite = uattr->overwrite;
89 attr->switch_timer_interval = uattr->switch_timer_interval;
90 attr->read_timer_interval = uattr->read_timer_interval;
91 attr->output = uattr->output;
92 }
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 */
100 static 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
165 no_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 */
173 static 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 */
200 static 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 */
225 static 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
256 error:
257 return registry;
258 }
259
260 /*
261 * Delete ust context safely. RCU read lock must be held before calling
262 * this function.
263 */
264 static
265 void 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 */
286 static
287 void 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 */
312 static 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 */
335 static
336 void 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 */
350 static
351 void 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 */
365 static
366 void 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 */
438 ssize_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
493 push_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
527 end:
528 error:
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 }
539 error_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 */
558 static 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
590 error:
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 */
603 static 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
634 error:
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;
641 end:
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 */
653 static
654 void 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 */
667 static
668 void 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 */
734 static
735 void 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 */
782 static
783 void 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 */
798 static 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
817 end:
818 return;
819 }
820
821 /*
822 * Alloc new UST app session.
823 */
824 static
825 struct 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
843 error_free:
844 return NULL;
845 }
846
847 /*
848 * Alloc new UST app channel.
849 */
850 static
851 struct 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
896 error:
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 */
905 struct 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
918 error:
919 return stream;
920 }
921
922 /*
923 * Alloc new UST app event.
924 */
925 static
926 struct 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
952 error:
953 return NULL;
954 }
955
956 /*
957 * Alloc new UST app context.
958 */
959 static
960 struct 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
977 error:
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 */
986 static 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
1000 error:
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 */
1009 static 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);
1024 error:
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 */
1032 struct 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
1046 error:
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 */
1054 static 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
1069 error:
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 */
1079 static 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
1108 end:
1109 return event;
1110 }
1111
1112 /*
1113 * Create the channel context on the tracer.
1114 *
1115 * Called with UST app session lock held.
1116 */
1117 static
1118 int 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
1148 error:
1149 health_code_update();
1150 return ret;
1151 }
1152
1153 /*
1154 * Set the filter on the tracer.
1155 */
1156 static
1157 int 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
1195 error:
1196 health_code_update();
1197 free(ust_bytecode);
1198 return ret;
1199 }
1200
1201 static
1202 struct 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);
1218 end:
1219 return ust_exclusion;
1220 }
1221
1222 /*
1223 * Set event exclusions on the tracer.
1224 */
1225 static
1226 int 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
1264 error:
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 */
1273 static 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
1301 error:
1302 health_code_update();
1303 return ret;
1304 }
1305
1306 /*
1307 * Disable the specified channel on to UST tracer for the UST session.
1308 */
1309 static 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
1337 error:
1338 health_code_update();
1339 return ret;
1340 }
1341
1342 /*
1343 * Enable the specified channel on to UST tracer for the UST session.
1344 */
1345 static 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
1375 error:
1376 health_code_update();
1377 return ret;
1378 }
1379
1380 /*
1381 * Enable the specified event on to UST tracer for the UST session.
1382 */
1383 static 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
1411 error:
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 */
1421 static 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 < 0) {
1439 goto error;
1440 }
1441
1442 health_code_update();
1443
1444 /* Send all streams to application. */
1445 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
1446 ret = ust_consumer_send_stream_to_ust(app, ua_chan, stream);
1447 if (ret < 0) {
1448 goto error;
1449 }
1450 /* We don't need the stream anymore once sent to the tracer. */
1451 cds_list_del(&stream->list);
1452 delete_ust_app_stream(-1, stream);
1453 }
1454 /* Flag the channel that it is sent to the application. */
1455 ua_chan->is_sent = 1;
1456
1457 error:
1458 health_code_update();
1459 return ret;
1460 }
1461
1462 /*
1463 * Create the specified event onto the UST tracer for a UST session.
1464 *
1465 * Should be called with session mutex held.
1466 */
1467 static
1468 int create_ust_event(struct ust_app *app, struct ust_app_session *ua_sess,
1469 struct ust_app_channel *ua_chan, struct ust_app_event *ua_event)
1470 {
1471 int ret = 0;
1472
1473 health_code_update();
1474
1475 /* Create UST event on tracer */
1476 ret = ustctl_create_event(app->sock, &ua_event->attr, ua_chan->obj,
1477 &ua_event->obj);
1478 if (ret < 0) {
1479 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1480 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1481 ua_event->attr.name, app->pid, ret);
1482 } else {
1483 /*
1484 * This is normal behavior, an application can die during the
1485 * creation process. Don't report an error so the execution can
1486 * continue normally.
1487 */
1488 ret = 0;
1489 DBG3("UST app create event failed. Application is dead.");
1490 }
1491 goto error;
1492 }
1493
1494 ua_event->handle = ua_event->obj->handle;
1495
1496 DBG2("UST app event %s created successfully for pid:%d",
1497 ua_event->attr.name, app->pid);
1498
1499 health_code_update();
1500
1501 /* Set filter if one is present. */
1502 if (ua_event->filter) {
1503 ret = set_ust_event_filter(ua_event, app);
1504 if (ret < 0) {
1505 goto error;
1506 }
1507 }
1508
1509 /* Set exclusions for the event */
1510 if (ua_event->exclusion) {
1511 ret = set_ust_event_exclusion(ua_event, app);
1512 if (ret < 0) {
1513 goto error;
1514 }
1515 }
1516
1517 /* If event not enabled, disable it on the tracer */
1518 if (ua_event->enabled) {
1519 /*
1520 * We now need to explicitly enable the event, since it
1521 * is now disabled at creation.
1522 */
1523 ret = enable_ust_event(app, ua_sess, ua_event);
1524 if (ret < 0) {
1525 /*
1526 * If we hit an EPERM, something is wrong with our enable call. If
1527 * we get an EEXIST, there is a problem on the tracer side since we
1528 * just created it.
1529 */
1530 switch (ret) {
1531 case -LTTNG_UST_ERR_PERM:
1532 /* Code flow problem */
1533 assert(0);
1534 case -LTTNG_UST_ERR_EXIST:
1535 /* It's OK for our use case. */
1536 ret = 0;
1537 break;
1538 default:
1539 break;
1540 }
1541 goto error;
1542 }
1543 }
1544
1545 error:
1546 health_code_update();
1547 return ret;
1548 }
1549
1550 /*
1551 * Copy data between an UST app event and a LTT event.
1552 */
1553 static void shadow_copy_event(struct ust_app_event *ua_event,
1554 struct ltt_ust_event *uevent)
1555 {
1556 size_t exclusion_alloc_size;
1557
1558 strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name));
1559 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
1560
1561 ua_event->enabled = uevent->enabled;
1562
1563 /* Copy event attributes */
1564 memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr));
1565
1566 /* Copy filter bytecode */
1567 if (uevent->filter) {
1568 ua_event->filter = copy_filter_bytecode(uevent->filter);
1569 /* Filter might be NULL here in case of ENONEM. */
1570 }
1571
1572 /* Copy exclusion data */
1573 if (uevent->exclusion) {
1574 exclusion_alloc_size = sizeof(struct lttng_event_exclusion) +
1575 LTTNG_UST_SYM_NAME_LEN * uevent->exclusion->count;
1576 ua_event->exclusion = zmalloc(exclusion_alloc_size);
1577 if (ua_event->exclusion == NULL) {
1578 PERROR("malloc");
1579 } else {
1580 memcpy(ua_event->exclusion, uevent->exclusion,
1581 exclusion_alloc_size);
1582 }
1583 }
1584 }
1585
1586 /*
1587 * Copy data between an UST app channel and a LTT channel.
1588 */
1589 static void shadow_copy_channel(struct ust_app_channel *ua_chan,
1590 struct ltt_ust_channel *uchan)
1591 {
1592 struct lttng_ht_iter iter;
1593 struct ltt_ust_event *uevent;
1594 struct ltt_ust_context *uctx;
1595 struct ust_app_event *ua_event;
1596 struct ust_app_ctx *ua_ctx;
1597
1598 DBG2("UST app shadow copy of channel %s started", ua_chan->name);
1599
1600 strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name));
1601 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
1602
1603 ua_chan->tracefile_size = uchan->tracefile_size;
1604 ua_chan->tracefile_count = uchan->tracefile_count;
1605
1606 /* Copy event attributes since the layout is different. */
1607 ua_chan->attr.subbuf_size = uchan->attr.subbuf_size;
1608 ua_chan->attr.num_subbuf = uchan->attr.num_subbuf;
1609 ua_chan->attr.overwrite = uchan->attr.overwrite;
1610 ua_chan->attr.switch_timer_interval = uchan->attr.switch_timer_interval;
1611 ua_chan->attr.read_timer_interval = uchan->attr.read_timer_interval;
1612 ua_chan->attr.output = uchan->attr.output;
1613 /*
1614 * Note that the attribute channel type is not set since the channel on the
1615 * tracing registry side does not have this information.
1616 */
1617
1618 ua_chan->enabled = uchan->enabled;
1619 ua_chan->tracing_channel_id = uchan->id;
1620
1621 cds_list_for_each_entry(uctx, &uchan->ctx_list, list) {
1622 ua_ctx = alloc_ust_app_ctx(&uctx->ctx);
1623 if (ua_ctx == NULL) {
1624 continue;
1625 }
1626 lttng_ht_node_init_ulong(&ua_ctx->node,
1627 (unsigned long) ua_ctx->ctx.ctx);
1628 lttng_ht_add_ulong(ua_chan->ctx, &ua_ctx->node);
1629 cds_list_add_tail(&ua_ctx->list, &ua_chan->ctx_list);
1630 }
1631
1632 /* Copy all events from ltt ust channel to ust app channel */
1633 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
1634 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
1635 uevent->filter, uevent->attr.loglevel, uevent->exclusion);
1636 if (ua_event == NULL) {
1637 DBG2("UST event %s not found on shadow copy channel",
1638 uevent->attr.name);
1639 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
1640 if (ua_event == NULL) {
1641 continue;
1642 }
1643 shadow_copy_event(ua_event, uevent);
1644 add_unique_ust_app_event(ua_chan, ua_event);
1645 }
1646 }
1647
1648 DBG3("UST app shadow copy of channel %s done", ua_chan->name);
1649 }
1650
1651 /*
1652 * Copy data between a UST app session and a regular LTT session.
1653 */
1654 static void shadow_copy_session(struct ust_app_session *ua_sess,
1655 struct ltt_ust_session *usess, struct ust_app *app)
1656 {
1657 struct lttng_ht_node_str *ua_chan_node;
1658 struct lttng_ht_iter iter;
1659 struct ltt_ust_channel *uchan;
1660 struct ust_app_channel *ua_chan;
1661 time_t rawtime;
1662 struct tm *timeinfo;
1663 char datetime[16];
1664 int ret;
1665 char tmp_shm_path[PATH_MAX];
1666
1667 /* Get date and time for unique app path */
1668 time(&rawtime);
1669 timeinfo = localtime(&rawtime);
1670 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
1671
1672 DBG2("Shadow copy of session handle %d", ua_sess->handle);
1673
1674 ua_sess->tracing_id = usess->id;
1675 ua_sess->id = get_next_session_id();
1676 ua_sess->uid = app->uid;
1677 ua_sess->gid = app->gid;
1678 ua_sess->euid = usess->uid;
1679 ua_sess->egid = usess->gid;
1680 ua_sess->buffer_type = usess->buffer_type;
1681 ua_sess->bits_per_long = app->bits_per_long;
1682 /* There is only one consumer object per session possible. */
1683 ua_sess->consumer = usess->consumer;
1684 ua_sess->output_traces = usess->output_traces;
1685 ua_sess->live_timer_interval = usess->live_timer_interval;
1686 copy_channel_attr_to_ustctl(&ua_sess->metadata_attr,
1687 &usess->metadata_attr);
1688
1689 switch (ua_sess->buffer_type) {
1690 case LTTNG_BUFFER_PER_PID:
1691 ret = snprintf(ua_sess->path, sizeof(ua_sess->path),
1692 DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s", app->name, app->pid,
1693 datetime);
1694 break;
1695 case LTTNG_BUFFER_PER_UID:
1696 ret = snprintf(ua_sess->path, sizeof(ua_sess->path),
1697 DEFAULT_UST_TRACE_UID_PATH, ua_sess->uid, app->bits_per_long);
1698 break;
1699 default:
1700 assert(0);
1701 goto error;
1702 }
1703 if (ret < 0) {
1704 PERROR("asprintf UST shadow copy session");
1705 assert(0);
1706 goto error;
1707 }
1708
1709 strncpy(ua_sess->root_shm_path, usess->root_shm_path,
1710 sizeof(ua_sess->root_shm_path));
1711 ua_sess->root_shm_path[sizeof(ua_sess->root_shm_path) - 1] = '\0';
1712 strncpy(ua_sess->shm_path, usess->shm_path,
1713 sizeof(ua_sess->shm_path));
1714 ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0';
1715 if (ua_sess->shm_path[0]) {
1716 switch (ua_sess->buffer_type) {
1717 case LTTNG_BUFFER_PER_PID:
1718 ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path),
1719 DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s",
1720 app->name, app->pid, datetime);
1721 break;
1722 case LTTNG_BUFFER_PER_UID:
1723 ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path),
1724 DEFAULT_UST_TRACE_UID_PATH,
1725 app->uid, app->bits_per_long);
1726 break;
1727 default:
1728 assert(0);
1729 goto error;
1730 }
1731 if (ret < 0) {
1732 PERROR("sprintf UST shadow copy session");
1733 assert(0);
1734 goto error;
1735 }
1736 strncat(ua_sess->shm_path, tmp_shm_path,
1737 sizeof(ua_sess->shm_path) - strlen(ua_sess->shm_path) - 1);
1738 ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0';
1739 }
1740
1741 /* Iterate over all channels in global domain. */
1742 cds_lfht_for_each_entry(usess->domain_global.channels->ht, &iter.iter,
1743 uchan, node.node) {
1744 struct lttng_ht_iter uiter;
1745
1746 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
1747 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
1748 if (ua_chan_node != NULL) {
1749 /* Session exist. Contiuing. */
1750 continue;
1751 }
1752
1753 DBG2("Channel %s not found on shadow session copy, creating it",
1754 uchan->name);
1755 ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr);
1756 if (ua_chan == NULL) {
1757 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
1758 continue;
1759 }
1760 shadow_copy_channel(ua_chan, uchan);
1761 /*
1762 * The concept of metadata channel does not exist on the tracing
1763 * registry side of the session daemon so this can only be a per CPU
1764 * channel and not metadata.
1765 */
1766 ua_chan->attr.type = LTTNG_UST_CHAN_PER_CPU;
1767
1768 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
1769 }
1770
1771 error:
1772 return;
1773 }
1774
1775 /*
1776 * Lookup sesison wrapper.
1777 */
1778 static
1779 void __lookup_session_by_app(struct ltt_ust_session *usess,
1780 struct ust_app *app, struct lttng_ht_iter *iter)
1781 {
1782 /* Get right UST app session from app */
1783 lttng_ht_lookup(app->sessions, &usess->id, iter);
1784 }
1785
1786 /*
1787 * Return ust app session from the app session hashtable using the UST session
1788 * id.
1789 */
1790 static struct ust_app_session *lookup_session_by_app(
1791 struct ltt_ust_session *usess, struct ust_app *app)
1792 {
1793 struct lttng_ht_iter iter;
1794 struct lttng_ht_node_u64 *node;
1795
1796 __lookup_session_by_app(usess, app, &iter);
1797 node = lttng_ht_iter_get_node_u64(&iter);
1798 if (node == NULL) {
1799 goto error;
1800 }
1801
1802 return caa_container_of(node, struct ust_app_session, node);
1803
1804 error:
1805 return NULL;
1806 }
1807
1808 /*
1809 * Setup buffer registry per PID for the given session and application. If none
1810 * is found, a new one is created, added to the global registry and
1811 * initialized. If regp is valid, it's set with the newly created object.
1812 *
1813 * Return 0 on success or else a negative value.
1814 */
1815 static int setup_buffer_reg_pid(struct ust_app_session *ua_sess,
1816 struct ust_app *app, struct buffer_reg_pid **regp)
1817 {
1818 int ret = 0;
1819 struct buffer_reg_pid *reg_pid;
1820
1821 assert(ua_sess);
1822 assert(app);
1823
1824 rcu_read_lock();
1825
1826 reg_pid = buffer_reg_pid_find(ua_sess->id);
1827 if (!reg_pid) {
1828 /*
1829 * This is the create channel path meaning that if there is NO
1830 * registry available, we have to create one for this session.
1831 */
1832 ret = buffer_reg_pid_create(ua_sess->id, &reg_pid,
1833 ua_sess->root_shm_path, ua_sess->shm_path);
1834 if (ret < 0) {
1835 goto error;
1836 }
1837 } else {
1838 goto end;
1839 }
1840
1841 /* Initialize registry. */
1842 ret = ust_registry_session_init(&reg_pid->registry->reg.ust, app,
1843 app->bits_per_long, app->uint8_t_alignment,
1844 app->uint16_t_alignment, app->uint32_t_alignment,
1845 app->uint64_t_alignment, app->long_alignment,
1846 app->byte_order, app->version.major,
1847 app->version.minor, reg_pid->root_shm_path,
1848 reg_pid->shm_path,
1849 ua_sess->euid, ua_sess->egid);
1850 if (ret < 0) {
1851 /*
1852 * reg_pid->registry->reg.ust is NULL upon error, so we need to
1853 * destroy the buffer registry, because it is always expected
1854 * that if the buffer registry can be found, its ust registry is
1855 * non-NULL.
1856 */
1857 buffer_reg_pid_destroy(reg_pid);
1858 goto error;
1859 }
1860
1861 buffer_reg_pid_add(reg_pid);
1862
1863 DBG3("UST app buffer registry per PID created successfully");
1864
1865 end:
1866 if (regp) {
1867 *regp = reg_pid;
1868 }
1869 error:
1870 rcu_read_unlock();
1871 return ret;
1872 }
1873
1874 /*
1875 * Setup buffer registry per UID for the given session and application. If none
1876 * is found, a new one is created, added to the global registry and
1877 * initialized. If regp is valid, it's set with the newly created object.
1878 *
1879 * Return 0 on success or else a negative value.
1880 */
1881 static int setup_buffer_reg_uid(struct ltt_ust_session *usess,
1882 struct ust_app_session *ua_sess,
1883 struct ust_app *app, struct buffer_reg_uid **regp)
1884 {
1885 int ret = 0;
1886 struct buffer_reg_uid *reg_uid;
1887
1888 assert(usess);
1889 assert(app);
1890
1891 rcu_read_lock();
1892
1893 reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid);
1894 if (!reg_uid) {
1895 /*
1896 * This is the create channel path meaning that if there is NO
1897 * registry available, we have to create one for this session.
1898 */
1899 ret = buffer_reg_uid_create(usess->id, app->bits_per_long, app->uid,
1900 LTTNG_DOMAIN_UST, &reg_uid,
1901 ua_sess->root_shm_path, ua_sess->shm_path);
1902 if (ret < 0) {
1903 goto error;
1904 }
1905 } else {
1906 goto end;
1907 }
1908
1909 /* Initialize registry. */
1910 ret = ust_registry_session_init(&reg_uid->registry->reg.ust, NULL,
1911 app->bits_per_long, app->uint8_t_alignment,
1912 app->uint16_t_alignment, app->uint32_t_alignment,
1913 app->uint64_t_alignment, app->long_alignment,
1914 app->byte_order, app->version.major,
1915 app->version.minor, reg_uid->root_shm_path,
1916 reg_uid->shm_path, usess->uid, usess->gid);
1917 if (ret < 0) {
1918 /*
1919 * reg_uid->registry->reg.ust is NULL upon error, so we need to
1920 * destroy the buffer registry, because it is always expected
1921 * that if the buffer registry can be found, its ust registry is
1922 * non-NULL.
1923 */
1924 buffer_reg_uid_destroy(reg_uid, NULL);
1925 goto error;
1926 }
1927 /* Add node to teardown list of the session. */
1928 cds_list_add(&reg_uid->lnode, &usess->buffer_reg_uid_list);
1929
1930 buffer_reg_uid_add(reg_uid);
1931
1932 DBG3("UST app buffer registry per UID created successfully");
1933 end:
1934 if (regp) {
1935 *regp = reg_uid;
1936 }
1937 error:
1938 rcu_read_unlock();
1939 return ret;
1940 }
1941
1942 /*
1943 * Create a session on the tracer side for the given app.
1944 *
1945 * On success, ua_sess_ptr is populated with the session pointer or else left
1946 * untouched. If the session was created, is_created is set to 1. On error,
1947 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
1948 * be NULL.
1949 *
1950 * Returns 0 on success or else a negative code which is either -ENOMEM or
1951 * -ENOTCONN which is the default code if the ustctl_create_session fails.
1952 */
1953 static int create_ust_app_session(struct ltt_ust_session *usess,
1954 struct ust_app *app, struct ust_app_session **ua_sess_ptr,
1955 int *is_created)
1956 {
1957 int ret, created = 0;
1958 struct ust_app_session *ua_sess;
1959
1960 assert(usess);
1961 assert(app);
1962 assert(ua_sess_ptr);
1963
1964 health_code_update();
1965
1966 ua_sess = lookup_session_by_app(usess, app);
1967 if (ua_sess == NULL) {
1968 DBG2("UST app pid: %d session id %" PRIu64 " not found, creating it",
1969 app->pid, usess->id);
1970 ua_sess = alloc_ust_app_session(app);
1971 if (ua_sess == NULL) {
1972 /* Only malloc can failed so something is really wrong */
1973 ret = -ENOMEM;
1974 goto error;
1975 }
1976 shadow_copy_session(ua_sess, usess, app);
1977 created = 1;
1978 }
1979
1980 switch (usess->buffer_type) {
1981 case LTTNG_BUFFER_PER_PID:
1982 /* Init local registry. */
1983 ret = setup_buffer_reg_pid(ua_sess, app, NULL);
1984 if (ret < 0) {
1985 delete_ust_app_session(-1, ua_sess, app);
1986 goto error;
1987 }
1988 break;
1989 case LTTNG_BUFFER_PER_UID:
1990 /* Look for a global registry. If none exists, create one. */
1991 ret = setup_buffer_reg_uid(usess, ua_sess, app, NULL);
1992 if (ret < 0) {
1993 delete_ust_app_session(-1, ua_sess, app);
1994 goto error;
1995 }
1996 break;
1997 default:
1998 assert(0);
1999 ret = -EINVAL;
2000 goto error;
2001 }
2002
2003 health_code_update();
2004
2005 if (ua_sess->handle == -1) {
2006 ret = ustctl_create_session(app->sock);
2007 if (ret < 0) {
2008 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
2009 ERR("Creating session for app pid %d with ret %d",
2010 app->pid, ret);
2011 } else {
2012 DBG("UST app creating session failed. Application is dead");
2013 /*
2014 * This is normal behavior, an application can die during the
2015 * creation process. Don't report an error so the execution can
2016 * continue normally. This will get flagged ENOTCONN and the
2017 * caller will handle it.
2018 */
2019 ret = 0;
2020 }
2021 delete_ust_app_session(-1, ua_sess, app);
2022 if (ret != -ENOMEM) {
2023 /*
2024 * Tracer is probably gone or got an internal error so let's
2025 * behave like it will soon unregister or not usable.
2026 */
2027 ret = -ENOTCONN;
2028 }
2029 goto error;
2030 }
2031
2032 ua_sess->handle = ret;
2033
2034 /* Add ust app session to app's HT */
2035 lttng_ht_node_init_u64(&ua_sess->node,
2036 ua_sess->tracing_id);
2037 lttng_ht_add_unique_u64(app->sessions, &ua_sess->node);
2038
2039 DBG2("UST app session created successfully with handle %d", ret);
2040 }
2041
2042 *ua_sess_ptr = ua_sess;
2043 if (is_created) {
2044 *is_created = created;
2045 }
2046
2047 /* Everything went well. */
2048 ret = 0;
2049
2050 error:
2051 health_code_update();
2052 return ret;
2053 }
2054
2055 /*
2056 * Match function for a hash table lookup of ust_app_ctx.
2057 *
2058 * It matches an ust app context based on the context type and, in the case
2059 * of perf counters, their name.
2060 */
2061 static int ht_match_ust_app_ctx(struct cds_lfht_node *node, const void *_key)
2062 {
2063 struct ust_app_ctx *ctx;
2064 const struct lttng_ust_context *key;
2065
2066 assert(node);
2067 assert(_key);
2068
2069 ctx = caa_container_of(node, struct ust_app_ctx, node.node);
2070 key = _key;
2071
2072 /* Context type */
2073 if (ctx->ctx.ctx != key->ctx) {
2074 goto no_match;
2075 }
2076
2077 /* Check the name in the case of perf thread counters. */
2078 if (key->ctx == LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER) {
2079 if (strncmp(key->u.perf_counter.name,
2080 ctx->ctx.u.perf_counter.name,
2081 sizeof(key->u.perf_counter.name))) {
2082 goto no_match;
2083 }
2084 }
2085
2086 /* Match. */
2087 return 1;
2088
2089 no_match:
2090 return 0;
2091 }
2092
2093 /*
2094 * Lookup for an ust app context from an lttng_ust_context.
2095 *
2096 * Must be called while holding RCU read side lock.
2097 * Return an ust_app_ctx object or NULL on error.
2098 */
2099 static
2100 struct ust_app_ctx *find_ust_app_context(struct lttng_ht *ht,
2101 struct lttng_ust_context *uctx)
2102 {
2103 struct lttng_ht_iter iter;
2104 struct lttng_ht_node_ulong *node;
2105 struct ust_app_ctx *app_ctx = NULL;
2106
2107 assert(uctx);
2108 assert(ht);
2109
2110 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2111 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) uctx->ctx, lttng_ht_seed),
2112 ht_match_ust_app_ctx, uctx, &iter.iter);
2113 node = lttng_ht_iter_get_node_ulong(&iter);
2114 if (!node) {
2115 goto end;
2116 }
2117
2118 app_ctx = caa_container_of(node, struct ust_app_ctx, node);
2119
2120 end:
2121 return app_ctx;
2122 }
2123
2124 /*
2125 * Create a context for the channel on the tracer.
2126 *
2127 * Called with UST app session lock held and a RCU read side lock.
2128 */
2129 static
2130 int create_ust_app_channel_context(struct ust_app_session *ua_sess,
2131 struct ust_app_channel *ua_chan, struct lttng_ust_context *uctx,
2132 struct ust_app *app)
2133 {
2134 int ret = 0;
2135 struct ust_app_ctx *ua_ctx;
2136
2137 DBG2("UST app adding context to channel %s", ua_chan->name);
2138
2139 ua_ctx = find_ust_app_context(ua_chan->ctx, uctx);
2140 if (ua_ctx) {
2141 ret = -EEXIST;
2142 goto error;
2143 }
2144
2145 ua_ctx = alloc_ust_app_ctx(uctx);
2146 if (ua_ctx == NULL) {
2147 /* malloc failed */
2148 ret = -1;
2149 goto error;
2150 }
2151
2152 lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx);
2153 lttng_ht_add_ulong(ua_chan->ctx, &ua_ctx->node);
2154 cds_list_add_tail(&ua_ctx->list, &ua_chan->ctx_list);
2155
2156 ret = create_ust_channel_context(ua_chan, ua_ctx, app);
2157 if (ret < 0) {
2158 goto error;
2159 }
2160
2161 error:
2162 return ret;
2163 }
2164
2165 /*
2166 * Enable on the tracer side a ust app event for the session and channel.
2167 *
2168 * Called with UST app session lock held.
2169 */
2170 static
2171 int enable_ust_app_event(struct ust_app_session *ua_sess,
2172 struct ust_app_event *ua_event, struct ust_app *app)
2173 {
2174 int ret;
2175
2176 ret = enable_ust_event(app, ua_sess, ua_event);
2177 if (ret < 0) {
2178 goto error;
2179 }
2180
2181 ua_event->enabled = 1;
2182
2183 error:
2184 return ret;
2185 }
2186
2187 /*
2188 * Disable on the tracer side a ust app event for the session and channel.
2189 */
2190 static int disable_ust_app_event(struct ust_app_session *ua_sess,
2191 struct ust_app_event *ua_event, struct ust_app *app)
2192 {
2193 int ret;
2194
2195 ret = disable_ust_event(app, ua_sess, ua_event);
2196 if (ret < 0) {
2197 goto error;
2198 }
2199
2200 ua_event->enabled = 0;
2201
2202 error:
2203 return ret;
2204 }
2205
2206 /*
2207 * Lookup ust app channel for session and disable it on the tracer side.
2208 */
2209 static
2210 int disable_ust_app_channel(struct ust_app_session *ua_sess,
2211 struct ust_app_channel *ua_chan, struct ust_app *app)
2212 {
2213 int ret;
2214
2215 ret = disable_ust_channel(app, ua_sess, ua_chan);
2216 if (ret < 0) {
2217 goto error;
2218 }
2219
2220 ua_chan->enabled = 0;
2221
2222 error:
2223 return ret;
2224 }
2225
2226 /*
2227 * Lookup ust app channel for session and enable it on the tracer side. This
2228 * MUST be called with a RCU read side lock acquired.
2229 */
2230 static int enable_ust_app_channel(struct ust_app_session *ua_sess,
2231 struct ltt_ust_channel *uchan, struct ust_app *app)
2232 {
2233 int ret = 0;
2234 struct lttng_ht_iter iter;
2235 struct lttng_ht_node_str *ua_chan_node;
2236 struct ust_app_channel *ua_chan;
2237
2238 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
2239 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
2240 if (ua_chan_node == NULL) {
2241 DBG2("Unable to find channel %s in ust session id %" PRIu64,
2242 uchan->name, ua_sess->tracing_id);
2243 goto error;
2244 }
2245
2246 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2247
2248 ret = enable_ust_channel(app, ua_sess, ua_chan);
2249 if (ret < 0) {
2250 goto error;
2251 }
2252
2253 error:
2254 return ret;
2255 }
2256
2257 /*
2258 * Ask the consumer to create a channel and get it if successful.
2259 *
2260 * Return 0 on success or else a negative value.
2261 */
2262 static int do_consumer_create_channel(struct ltt_ust_session *usess,
2263 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan,
2264 int bitness, struct ust_registry_session *registry)
2265 {
2266 int ret;
2267 unsigned int nb_fd = 0;
2268 struct consumer_socket *socket;
2269
2270 assert(usess);
2271 assert(ua_sess);
2272 assert(ua_chan);
2273 assert(registry);
2274
2275 rcu_read_lock();
2276 health_code_update();
2277
2278 /* Get the right consumer socket for the application. */
2279 socket = consumer_find_socket_by_bitness(bitness, usess->consumer);
2280 if (!socket) {
2281 ret = -EINVAL;
2282 goto error;
2283 }
2284
2285 health_code_update();
2286
2287 /* Need one fd for the channel. */
2288 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
2289 if (ret < 0) {
2290 ERR("Exhausted number of available FD upon create channel");
2291 goto error;
2292 }
2293
2294 /*
2295 * Ask consumer to create channel. The consumer will return the number of
2296 * stream we have to expect.
2297 */
2298 ret = ust_consumer_ask_channel(ua_sess, ua_chan, usess->consumer, socket,
2299 registry);
2300 if (ret < 0) {
2301 goto error_ask;
2302 }
2303
2304 /*
2305 * Compute the number of fd needed before receiving them. It must be 2 per
2306 * stream (2 being the default value here).
2307 */
2308 nb_fd = DEFAULT_UST_STREAM_FD_NUM * ua_chan->expected_stream_count;
2309
2310 /* Reserve the amount of file descriptor we need. */
2311 ret = lttng_fd_get(LTTNG_FD_APPS, nb_fd);
2312 if (ret < 0) {
2313 ERR("Exhausted number of available FD upon create channel");
2314 goto error_fd_get_stream;
2315 }
2316
2317 health_code_update();
2318
2319 /*
2320 * Now get the channel from the consumer. This call wil populate the stream
2321 * list of that channel and set the ust objects.
2322 */
2323 if (usess->consumer->enabled) {
2324 ret = ust_consumer_get_channel(socket, ua_chan);
2325 if (ret < 0) {
2326 goto error_destroy;
2327 }
2328 }
2329
2330 rcu_read_unlock();
2331 return 0;
2332
2333 error_destroy:
2334 lttng_fd_put(LTTNG_FD_APPS, nb_fd);
2335 error_fd_get_stream:
2336 /*
2337 * Initiate a destroy channel on the consumer since we had an error
2338 * handling it on our side. The return value is of no importance since we
2339 * already have a ret value set by the previous error that we need to
2340 * return.
2341 */
2342 (void) ust_consumer_destroy_channel(socket, ua_chan);
2343 error_ask:
2344 lttng_fd_put(LTTNG_FD_APPS, 1);
2345 error:
2346 health_code_update();
2347 rcu_read_unlock();
2348 return ret;
2349 }
2350
2351 /*
2352 * Duplicate the ust data object of the ust app stream and save it in the
2353 * buffer registry stream.
2354 *
2355 * Return 0 on success or else a negative value.
2356 */
2357 static int duplicate_stream_object(struct buffer_reg_stream *reg_stream,
2358 struct ust_app_stream *stream)
2359 {
2360 int ret;
2361
2362 assert(reg_stream);
2363 assert(stream);
2364
2365 /* Reserve the amount of file descriptor we need. */
2366 ret = lttng_fd_get(LTTNG_FD_APPS, 2);
2367 if (ret < 0) {
2368 ERR("Exhausted number of available FD upon duplicate stream");
2369 goto error;
2370 }
2371
2372 /* Duplicate object for stream once the original is in the registry. */
2373 ret = ustctl_duplicate_ust_object_data(&stream->obj,
2374 reg_stream->obj.ust);
2375 if (ret < 0) {
2376 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2377 reg_stream->obj.ust, stream->obj, ret);
2378 lttng_fd_put(LTTNG_FD_APPS, 2);
2379 goto error;
2380 }
2381 stream->handle = stream->obj->handle;
2382
2383 error:
2384 return ret;
2385 }
2386
2387 /*
2388 * Duplicate the ust data object of the ust app. channel and save it in the
2389 * buffer registry channel.
2390 *
2391 * Return 0 on success or else a negative value.
2392 */
2393 static int duplicate_channel_object(struct buffer_reg_channel *reg_chan,
2394 struct ust_app_channel *ua_chan)
2395 {
2396 int ret;
2397
2398 assert(reg_chan);
2399 assert(ua_chan);
2400
2401 /* Need two fds for the channel. */
2402 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
2403 if (ret < 0) {
2404 ERR("Exhausted number of available FD upon duplicate channel");
2405 goto error_fd_get;
2406 }
2407
2408 /* Duplicate object for stream once the original is in the registry. */
2409 ret = ustctl_duplicate_ust_object_data(&ua_chan->obj, reg_chan->obj.ust);
2410 if (ret < 0) {
2411 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2412 reg_chan->obj.ust, ua_chan->obj, ret);
2413 goto error;
2414 }
2415 ua_chan->handle = ua_chan->obj->handle;
2416
2417 return 0;
2418
2419 error:
2420 lttng_fd_put(LTTNG_FD_APPS, 1);
2421 error_fd_get:
2422 return ret;
2423 }
2424
2425 /*
2426 * For a given channel buffer registry, setup all streams of the given ust
2427 * application channel.
2428 *
2429 * Return 0 on success or else a negative value.
2430 */
2431 static int setup_buffer_reg_streams(struct buffer_reg_channel *reg_chan,
2432 struct ust_app_channel *ua_chan)
2433 {
2434 int ret = 0;
2435 struct ust_app_stream *stream, *stmp;
2436
2437 assert(reg_chan);
2438 assert(ua_chan);
2439
2440 DBG2("UST app setup buffer registry stream");
2441
2442 /* Send all streams to application. */
2443 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
2444 struct buffer_reg_stream *reg_stream;
2445
2446 ret = buffer_reg_stream_create(&reg_stream);
2447 if (ret < 0) {
2448 goto error;
2449 }
2450
2451 /*
2452 * Keep original pointer and nullify it in the stream so the delete
2453 * stream call does not release the object.
2454 */
2455 reg_stream->obj.ust = stream->obj;
2456 stream->obj = NULL;
2457 buffer_reg_stream_add(reg_stream, reg_chan);
2458
2459 /* We don't need the streams anymore. */
2460 cds_list_del(&stream->list);
2461 delete_ust_app_stream(-1, stream);
2462 }
2463
2464 error:
2465 return ret;
2466 }
2467
2468 /*
2469 * Create a buffer registry channel for the given session registry and
2470 * application channel object. If regp pointer is valid, it's set with the
2471 * created object. Important, the created object is NOT added to the session
2472 * registry hash table.
2473 *
2474 * Return 0 on success else a negative value.
2475 */
2476 static int create_buffer_reg_channel(struct buffer_reg_session *reg_sess,
2477 struct ust_app_channel *ua_chan, struct buffer_reg_channel **regp)
2478 {
2479 int ret;
2480 struct buffer_reg_channel *reg_chan = NULL;
2481
2482 assert(reg_sess);
2483 assert(ua_chan);
2484
2485 DBG2("UST app creating buffer registry channel for %s", ua_chan->name);
2486
2487 /* Create buffer registry channel. */
2488 ret = buffer_reg_channel_create(ua_chan->tracing_channel_id, &reg_chan);
2489 if (ret < 0) {
2490 goto error_create;
2491 }
2492 assert(reg_chan);
2493 reg_chan->consumer_key = ua_chan->key;
2494 reg_chan->subbuf_size = ua_chan->attr.subbuf_size;
2495 reg_chan->num_subbuf = ua_chan->attr.num_subbuf;
2496
2497 /* Create and add a channel registry to session. */
2498 ret = ust_registry_channel_add(reg_sess->reg.ust,
2499 ua_chan->tracing_channel_id);
2500 if (ret < 0) {
2501 goto error;
2502 }
2503 buffer_reg_channel_add(reg_sess, reg_chan);
2504
2505 if (regp) {
2506 *regp = reg_chan;
2507 }
2508
2509 return 0;
2510
2511 error:
2512 /* Safe because the registry channel object was not added to any HT. */
2513 buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST);
2514 error_create:
2515 return ret;
2516 }
2517
2518 /*
2519 * Setup buffer registry channel for the given session registry and application
2520 * channel object. If regp pointer is valid, it's set with the created object.
2521 *
2522 * Return 0 on success else a negative value.
2523 */
2524 static int setup_buffer_reg_channel(struct buffer_reg_session *reg_sess,
2525 struct ust_app_channel *ua_chan, struct buffer_reg_channel *reg_chan)
2526 {
2527 int ret;
2528
2529 assert(reg_sess);
2530 assert(reg_chan);
2531 assert(ua_chan);
2532 assert(ua_chan->obj);
2533
2534 DBG2("UST app setup buffer registry channel for %s", ua_chan->name);
2535
2536 /* Setup all streams for the registry. */
2537 ret = setup_buffer_reg_streams(reg_chan, ua_chan);
2538 if (ret < 0) {
2539 goto error;
2540 }
2541
2542 reg_chan->obj.ust = ua_chan->obj;
2543 ua_chan->obj = NULL;
2544
2545 return 0;
2546
2547 error:
2548 buffer_reg_channel_remove(reg_sess, reg_chan);
2549 buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST);
2550 return ret;
2551 }
2552
2553 /*
2554 * Send buffer registry channel to the application.
2555 *
2556 * Return 0 on success else a negative value.
2557 */
2558 static int send_channel_uid_to_ust(struct buffer_reg_channel *reg_chan,
2559 struct ust_app *app, struct ust_app_session *ua_sess,
2560 struct ust_app_channel *ua_chan)
2561 {
2562 int ret;
2563 struct buffer_reg_stream *reg_stream;
2564
2565 assert(reg_chan);
2566 assert(app);
2567 assert(ua_sess);
2568 assert(ua_chan);
2569
2570 DBG("UST app sending buffer registry channel to ust sock %d", app->sock);
2571
2572 ret = duplicate_channel_object(reg_chan, ua_chan);
2573 if (ret < 0) {
2574 goto error;
2575 }
2576
2577 /* Send channel to the application. */
2578 ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan);
2579 if (ret < 0) {
2580 goto error;
2581 }
2582
2583 health_code_update();
2584
2585 /* Send all streams to application. */
2586 pthread_mutex_lock(&reg_chan->stream_list_lock);
2587 cds_list_for_each_entry(reg_stream, &reg_chan->streams, lnode) {
2588 struct ust_app_stream stream;
2589
2590 ret = duplicate_stream_object(reg_stream, &stream);
2591 if (ret < 0) {
2592 goto error_stream_unlock;
2593 }
2594
2595 ret = ust_consumer_send_stream_to_ust(app, ua_chan, &stream);
2596 if (ret < 0) {
2597 (void) release_ust_app_stream(-1, &stream);
2598 goto error_stream_unlock;
2599 }
2600
2601 /*
2602 * The return value is not important here. This function will output an
2603 * error if needed.
2604 */
2605 (void) release_ust_app_stream(-1, &stream);
2606 }
2607 ua_chan->is_sent = 1;
2608
2609 error_stream_unlock:
2610 pthread_mutex_unlock(&reg_chan->stream_list_lock);
2611 error:
2612 return ret;
2613 }
2614
2615 /*
2616 * Create and send to the application the created buffers with per UID buffers.
2617 *
2618 * Return 0 on success else a negative value.
2619 */
2620 static int create_channel_per_uid(struct ust_app *app,
2621 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
2622 struct ust_app_channel *ua_chan)
2623 {
2624 int ret;
2625 struct buffer_reg_uid *reg_uid;
2626 struct buffer_reg_channel *reg_chan;
2627
2628 assert(app);
2629 assert(usess);
2630 assert(ua_sess);
2631 assert(ua_chan);
2632
2633 DBG("UST app creating channel %s with per UID buffers", ua_chan->name);
2634
2635 reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid);
2636 /*
2637 * The session creation handles the creation of this global registry
2638 * object. If none can be find, there is a code flow problem or a
2639 * teardown race.
2640 */
2641 assert(reg_uid);
2642
2643 reg_chan = buffer_reg_channel_find(ua_chan->tracing_channel_id,
2644 reg_uid);
2645 if (!reg_chan) {
2646 /* Create the buffer registry channel object. */
2647 ret = create_buffer_reg_channel(reg_uid->registry, ua_chan, &reg_chan);
2648 if (ret < 0) {
2649 ERR("Error creating the UST channel \"%s\" registry instance",
2650 ua_chan->name);
2651 goto error;
2652 }
2653 assert(reg_chan);
2654
2655 /*
2656 * Create the buffers on the consumer side. This call populates the
2657 * ust app channel object with all streams and data object.
2658 */
2659 ret = do_consumer_create_channel(usess, ua_sess, ua_chan,
2660 app->bits_per_long, reg_uid->registry->reg.ust);
2661 if (ret < 0) {
2662 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2663 ua_chan->name);
2664
2665 /*
2666 * Let's remove the previously created buffer registry channel so
2667 * it's not visible anymore in the session registry.
2668 */
2669 ust_registry_channel_del_free(reg_uid->registry->reg.ust,
2670 ua_chan->tracing_channel_id);
2671 buffer_reg_channel_remove(reg_uid->registry, reg_chan);
2672 buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST);
2673 goto error;
2674 }
2675
2676 /*
2677 * Setup the streams and add it to the session registry.
2678 */
2679 ret = setup_buffer_reg_channel(reg_uid->registry, ua_chan, reg_chan);
2680 if (ret < 0) {
2681 ERR("Error setting up UST channel \"%s\"",
2682 ua_chan->name);
2683 goto error;
2684 }
2685
2686 }
2687
2688 /* Send buffers to the application. */
2689 ret = send_channel_uid_to_ust(reg_chan, app, ua_sess, ua_chan);
2690 if (ret < 0) {
2691 /*
2692 * Don't report error to the console, since it may be
2693 * caused by application concurrently exiting.
2694 */
2695 goto error;
2696 }
2697
2698 error:
2699 return ret;
2700 }
2701
2702 /*
2703 * Create and send to the application the created buffers with per PID buffers.
2704 *
2705 * Return 0 on success else a negative value.
2706 */
2707 static int create_channel_per_pid(struct ust_app *app,
2708 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
2709 struct ust_app_channel *ua_chan)
2710 {
2711 int ret;
2712 struct ust_registry_session *registry;
2713
2714 assert(app);
2715 assert(usess);
2716 assert(ua_sess);
2717 assert(ua_chan);
2718
2719 DBG("UST app creating channel %s with per PID buffers", ua_chan->name);
2720
2721 rcu_read_lock();
2722
2723 registry = get_session_registry(ua_sess);
2724 assert(registry);
2725
2726 /* Create and add a new channel registry to session. */
2727 ret = ust_registry_channel_add(registry, ua_chan->key);
2728 if (ret < 0) {
2729 ERR("Error creating the UST channel \"%s\" registry instance",
2730 ua_chan->name);
2731 goto error;
2732 }
2733
2734 /* Create and get channel on the consumer side. */
2735 ret = do_consumer_create_channel(usess, ua_sess, ua_chan,
2736 app->bits_per_long, registry);
2737 if (ret < 0) {
2738 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2739 ua_chan->name);
2740 goto error;
2741 }
2742
2743 ret = send_channel_pid_to_ust(app, ua_sess, ua_chan);
2744 if (ret < 0) {
2745 /*
2746 * Don't report error to the console, since it may be
2747 * caused by application concurrently exiting.
2748 */
2749 goto error;
2750 }
2751
2752 error:
2753 rcu_read_unlock();
2754 return ret;
2755 }
2756
2757 /*
2758 * From an already allocated ust app channel, create the channel buffers if
2759 * need and send it to the application. This MUST be called with a RCU read
2760 * side lock acquired.
2761 *
2762 * Return 0 on success or else a negative value.
2763 */
2764 static int do_create_channel(struct ust_app *app,
2765 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
2766 struct ust_app_channel *ua_chan)
2767 {
2768 int ret;
2769
2770 assert(app);
2771 assert(usess);
2772 assert(ua_sess);
2773 assert(ua_chan);
2774
2775 /* Handle buffer type before sending the channel to the application. */
2776 switch (usess->buffer_type) {
2777 case LTTNG_BUFFER_PER_UID:
2778 {
2779 ret = create_channel_per_uid(app, usess, ua_sess, ua_chan);
2780 if (ret < 0) {
2781 goto error;
2782 }
2783 break;
2784 }
2785 case LTTNG_BUFFER_PER_PID:
2786 {
2787 ret = create_channel_per_pid(app, usess, ua_sess, ua_chan);
2788 if (ret < 0) {
2789 goto error;
2790 }
2791 break;
2792 }
2793 default:
2794 assert(0);
2795 ret = -EINVAL;
2796 goto error;
2797 }
2798
2799 /* Initialize ust objd object using the received handle and add it. */
2800 lttng_ht_node_init_ulong(&ua_chan->ust_objd_node, ua_chan->handle);
2801 lttng_ht_add_unique_ulong(app->ust_objd, &ua_chan->ust_objd_node);
2802
2803 /* If channel is not enabled, disable it on the tracer */
2804 if (!ua_chan->enabled) {
2805 ret = disable_ust_channel(app, ua_sess, ua_chan);
2806 if (ret < 0) {
2807 goto error;
2808 }
2809 }
2810
2811 error:
2812 return ret;
2813 }
2814
2815 /*
2816 * Create UST app channel and create it on the tracer. Set ua_chanp of the
2817 * newly created channel if not NULL.
2818 *
2819 * Called with UST app session lock and RCU read-side lock held.
2820 *
2821 * Return 0 on success or else a negative value.
2822 */
2823 static int create_ust_app_channel(struct ust_app_session *ua_sess,
2824 struct ltt_ust_channel *uchan, struct ust_app *app,
2825 enum lttng_ust_chan_type type, struct ltt_ust_session *usess,
2826 struct ust_app_channel **ua_chanp)
2827 {
2828 int ret = 0;
2829 struct lttng_ht_iter iter;
2830 struct lttng_ht_node_str *ua_chan_node;
2831 struct ust_app_channel *ua_chan;
2832
2833 /* Lookup channel in the ust app session */
2834 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
2835 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
2836 if (ua_chan_node != NULL) {
2837 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2838 goto end;
2839 }
2840
2841 ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr);
2842 if (ua_chan == NULL) {
2843 /* Only malloc can fail here */
2844 ret = -ENOMEM;
2845 goto error_alloc;
2846 }
2847 shadow_copy_channel(ua_chan, uchan);
2848
2849 /* Set channel type. */
2850 ua_chan->attr.type = type;
2851
2852 ret = do_create_channel(app, usess, ua_sess, ua_chan);
2853 if (ret < 0) {
2854 goto error;
2855 }
2856
2857 DBG2("UST app create channel %s for PID %d completed", ua_chan->name,
2858 app->pid);
2859
2860 /* Only add the channel if successful on the tracer side. */
2861 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
2862
2863 end:
2864 if (ua_chanp) {
2865 *ua_chanp = ua_chan;
2866 }
2867
2868 /* Everything went well. */
2869 return 0;
2870
2871 error:
2872 delete_ust_app_channel(ua_chan->is_sent ? app->sock : -1, ua_chan, app);
2873 error_alloc:
2874 return ret;
2875 }
2876
2877 /*
2878 * Create UST app event and create it on the tracer side.
2879 *
2880 * Called with ust app session mutex held.
2881 */
2882 static
2883 int create_ust_app_event(struct ust_app_session *ua_sess,
2884 struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent,
2885 struct ust_app *app)
2886 {
2887 int ret = 0;
2888 struct ust_app_event *ua_event;
2889
2890 /* Get event node */
2891 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
2892 uevent->filter, uevent->attr.loglevel, uevent->exclusion);
2893 if (ua_event != NULL) {
2894 ret = -EEXIST;
2895 goto end;
2896 }
2897
2898 /* Does not exist so create one */
2899 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
2900 if (ua_event == NULL) {
2901 /* Only malloc can failed so something is really wrong */
2902 ret = -ENOMEM;
2903 goto end;
2904 }
2905 shadow_copy_event(ua_event, uevent);
2906
2907 /* Create it on the tracer side */
2908 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
2909 if (ret < 0) {
2910 /* Not found previously means that it does not exist on the tracer */
2911 assert(ret != -LTTNG_UST_ERR_EXIST);
2912 goto error;
2913 }
2914
2915 add_unique_ust_app_event(ua_chan, ua_event);
2916
2917 DBG2("UST app create event %s for PID %d completed", ua_event->name,
2918 app->pid);
2919
2920 end:
2921 return ret;
2922
2923 error:
2924 /* Valid. Calling here is already in a read side lock */
2925 delete_ust_app_event(-1, ua_event);
2926 return ret;
2927 }
2928
2929 /*
2930 * Create UST metadata and open it on the tracer side.
2931 *
2932 * Called with UST app session lock held and RCU read side lock.
2933 */
2934 static int create_ust_app_metadata(struct ust_app_session *ua_sess,
2935 struct ust_app *app, struct consumer_output *consumer)
2936 {
2937 int ret = 0;
2938 struct ust_app_channel *metadata;
2939 struct consumer_socket *socket;
2940 struct ust_registry_session *registry;
2941
2942 assert(ua_sess);
2943 assert(app);
2944 assert(consumer);
2945
2946 registry = get_session_registry(ua_sess);
2947 assert(registry);
2948
2949 pthread_mutex_lock(&registry->lock);
2950
2951 /* Metadata already exists for this registry or it was closed previously */
2952 if (registry->metadata_key || registry->metadata_closed) {
2953 ret = 0;
2954 goto error;
2955 }
2956
2957 /* Allocate UST metadata */
2958 metadata = alloc_ust_app_channel(DEFAULT_METADATA_NAME, ua_sess, NULL);
2959 if (!metadata) {
2960 /* malloc() failed */
2961 ret = -ENOMEM;
2962 goto error;
2963 }
2964
2965 memcpy(&metadata->attr, &ua_sess->metadata_attr, sizeof(metadata->attr));
2966
2967 /* Need one fd for the channel. */
2968 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
2969 if (ret < 0) {
2970 ERR("Exhausted number of available FD upon create metadata");
2971 goto error;
2972 }
2973
2974 /* Get the right consumer socket for the application. */
2975 socket = consumer_find_socket_by_bitness(app->bits_per_long, consumer);
2976 if (!socket) {
2977 ret = -EINVAL;
2978 goto error_consumer;
2979 }
2980
2981 /*
2982 * Keep metadata key so we can identify it on the consumer side. Assign it
2983 * to the registry *before* we ask the consumer so we avoid the race of the
2984 * consumer requesting the metadata and the ask_channel call on our side
2985 * did not returned yet.
2986 */
2987 registry->metadata_key = metadata->key;
2988
2989 /*
2990 * Ask the metadata channel creation to the consumer. The metadata object
2991 * will be created by the consumer and kept their. However, the stream is
2992 * never added or monitored until we do a first push metadata to the
2993 * consumer.
2994 */
2995 ret = ust_consumer_ask_channel(ua_sess, metadata, consumer, socket,
2996 registry);
2997 if (ret < 0) {
2998 /* Nullify the metadata key so we don't try to close it later on. */
2999 registry->metadata_key = 0;
3000 goto error_consumer;
3001 }
3002
3003 /*
3004 * The setup command will make the metadata stream be sent to the relayd,
3005 * if applicable, and the thread managing the metadatas. This is important
3006 * because after this point, if an error occurs, the only way the stream
3007 * can be deleted is to be monitored in the consumer.
3008 */
3009 ret = consumer_setup_metadata(socket, metadata->key);
3010 if (ret < 0) {
3011 /* Nullify the metadata key so we don't try to close it later on. */
3012 registry->metadata_key = 0;
3013 goto error_consumer;
3014 }
3015
3016 DBG2("UST metadata with key %" PRIu64 " created for app pid %d",
3017 metadata->key, app->pid);
3018
3019 error_consumer:
3020 lttng_fd_put(LTTNG_FD_APPS, 1);
3021 delete_ust_app_channel(-1, metadata, app);
3022 error:
3023 pthread_mutex_unlock(&registry->lock);
3024 return ret;
3025 }
3026
3027 /*
3028 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3029 * acquired before calling this function.
3030 */
3031 struct ust_app *ust_app_find_by_pid(pid_t pid)
3032 {
3033 struct ust_app *app = NULL;
3034 struct lttng_ht_node_ulong *node;
3035 struct lttng_ht_iter iter;
3036
3037 lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) pid), &iter);
3038 node = lttng_ht_iter_get_node_ulong(&iter);
3039 if (node == NULL) {
3040 DBG2("UST app no found with pid %d", pid);
3041 goto error;
3042 }
3043
3044 DBG2("Found UST app by pid %d", pid);
3045
3046 app = caa_container_of(node, struct ust_app, pid_n);
3047
3048 error:
3049 return app;
3050 }
3051
3052 /*
3053 * Allocate and init an UST app object using the registration information and
3054 * the command socket. This is called when the command socket connects to the
3055 * session daemon.
3056 *
3057 * The object is returned on success or else NULL.
3058 */
3059 struct ust_app *ust_app_create(struct ust_register_msg *msg, int sock)
3060 {
3061 struct ust_app *lta = NULL;
3062
3063 assert(msg);
3064 assert(sock >= 0);
3065
3066 DBG3("UST app creating application for socket %d", sock);
3067
3068 if ((msg->bits_per_long == 64 &&
3069 (uatomic_read(&ust_consumerd64_fd) == -EINVAL))
3070 || (msg->bits_per_long == 32 &&
3071 (uatomic_read(&ust_consumerd32_fd) == -EINVAL))) {
3072 ERR("Registration failed: application \"%s\" (pid: %d) has "
3073 "%d-bit long, but no consumerd for this size is available.\n",
3074 msg->name, msg->pid, msg->bits_per_long);
3075 goto error;
3076 }
3077
3078 lta = zmalloc(sizeof(struct ust_app));
3079 if (lta == NULL) {
3080 PERROR("malloc");
3081 goto error;
3082 }
3083
3084 lta->ppid = msg->ppid;
3085 lta->uid = msg->uid;
3086 lta->gid = msg->gid;
3087
3088 lta->bits_per_long = msg->bits_per_long;
3089 lta->uint8_t_alignment = msg->uint8_t_alignment;
3090 lta->uint16_t_alignment = msg->uint16_t_alignment;
3091 lta->uint32_t_alignment = msg->uint32_t_alignment;
3092 lta->uint64_t_alignment = msg->uint64_t_alignment;
3093 lta->long_alignment = msg->long_alignment;
3094 lta->byte_order = msg->byte_order;
3095
3096 lta->v_major = msg->major;
3097 lta->v_minor = msg->minor;
3098 lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
3099 lta->ust_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
3100 lta->notify_sock = -1;
3101
3102 /* Copy name and make sure it's NULL terminated. */
3103 strncpy(lta->name, msg->name, sizeof(lta->name));
3104 lta->name[UST_APP_PROCNAME_LEN] = '\0';
3105
3106 /*
3107 * Before this can be called, when receiving the registration information,
3108 * the application compatibility is checked. So, at this point, the
3109 * application can work with this session daemon.
3110 */
3111 lta->compatible = 1;
3112
3113 lta->pid = msg->pid;
3114 lttng_ht_node_init_ulong(&lta->pid_n, (unsigned long) lta->pid);
3115 lta->sock = sock;
3116 lttng_ht_node_init_ulong(&lta->sock_n, (unsigned long) lta->sock);
3117
3118 CDS_INIT_LIST_HEAD(&lta->teardown_head);
3119
3120 error:
3121 return lta;
3122 }
3123
3124 /*
3125 * For a given application object, add it to every hash table.
3126 */
3127 void ust_app_add(struct ust_app *app)
3128 {
3129 assert(app);
3130 assert(app->notify_sock >= 0);
3131
3132 rcu_read_lock();
3133
3134 /*
3135 * On a re-registration, we want to kick out the previous registration of
3136 * that pid
3137 */
3138 lttng_ht_add_replace_ulong(ust_app_ht, &app->pid_n);
3139
3140 /*
3141 * The socket _should_ be unique until _we_ call close. So, a add_unique
3142 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3143 * already in the table.
3144 */
3145 lttng_ht_add_unique_ulong(ust_app_ht_by_sock, &app->sock_n);
3146
3147 /* Add application to the notify socket hash table. */
3148 lttng_ht_node_init_ulong(&app->notify_sock_n, app->notify_sock);
3149 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock, &app->notify_sock_n);
3150
3151 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3152 "notify_sock:%d (version %d.%d)", app->pid, app->ppid, app->uid,
3153 app->gid, app->sock, app->name, app->notify_sock, app->v_major,
3154 app->v_minor);
3155
3156 rcu_read_unlock();
3157 }
3158
3159 /*
3160 * Set the application version into the object.
3161 *
3162 * Return 0 on success else a negative value either an errno code or a
3163 * LTTng-UST error code.
3164 */
3165 int ust_app_version(struct ust_app *app)
3166 {
3167 int ret;
3168
3169 assert(app);
3170
3171 ret = ustctl_tracer_version(app->sock, &app->version);
3172 if (ret < 0) {
3173 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
3174 ERR("UST app %d version failed with ret %d", app->sock, ret);
3175 } else {
3176 DBG3("UST app %d version failed. Application is dead", app->sock);
3177 }
3178 }
3179
3180 return ret;
3181 }
3182
3183 /*
3184 * Unregister app by removing it from the global traceable app list and freeing
3185 * the data struct.
3186 *
3187 * The socket is already closed at this point so no close to sock.
3188 */
3189 void ust_app_unregister(int sock)
3190 {
3191 struct ust_app *lta;
3192 struct lttng_ht_node_ulong *node;
3193 struct lttng_ht_iter ust_app_sock_iter;
3194 struct lttng_ht_iter iter;
3195 struct ust_app_session *ua_sess;
3196 int ret;
3197
3198 rcu_read_lock();
3199
3200 /* Get the node reference for a call_rcu */
3201 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &ust_app_sock_iter);
3202 node = lttng_ht_iter_get_node_ulong(&ust_app_sock_iter);
3203 assert(node);
3204
3205 lta = caa_container_of(node, struct ust_app, sock_n);
3206 DBG("PID %d unregistering with sock %d", lta->pid, sock);
3207
3208 /*
3209 * For per-PID buffers, perform "push metadata" and flush all
3210 * application streams before removing app from hash tables,
3211 * ensuring proper behavior of data_pending check.
3212 * Remove sessions so they are not visible during deletion.
3213 */
3214 cds_lfht_for_each_entry(lta->sessions->ht, &iter.iter, ua_sess,
3215 node.node) {
3216 struct ust_registry_session *registry;
3217
3218 ret = lttng_ht_del(lta->sessions, &iter);
3219 if (ret) {
3220 /* The session was already removed so scheduled for teardown. */
3221 continue;
3222 }
3223
3224 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) {
3225 (void) ust_app_flush_app_session(lta, ua_sess);
3226 }
3227
3228 /*
3229 * Add session to list for teardown. This is safe since at this point we
3230 * are the only one using this list.
3231 */
3232 pthread_mutex_lock(&ua_sess->lock);
3233
3234 if (ua_sess->deleted) {
3235 pthread_mutex_unlock(&ua_sess->lock);
3236 continue;
3237 }
3238
3239 /*
3240 * Normally, this is done in the delete session process which is
3241 * executed in the call rcu below. However, upon registration we can't
3242 * afford to wait for the grace period before pushing data or else the
3243 * data pending feature can race between the unregistration and stop
3244 * command where the data pending command is sent *before* the grace
3245 * period ended.
3246 *
3247 * The close metadata below nullifies the metadata pointer in the
3248 * session so the delete session will NOT push/close a second time.
3249 */
3250 registry = get_session_registry(ua_sess);
3251 if (registry) {
3252 /* Push metadata for application before freeing the application. */
3253 (void) push_metadata(registry, ua_sess->consumer);
3254
3255 /*
3256 * Don't ask to close metadata for global per UID buffers. Close
3257 * metadata only on destroy trace session in this case. Also, the
3258 * previous push metadata could have flag the metadata registry to
3259 * close so don't send a close command if closed.
3260 */
3261 if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) {
3262 /* And ask to close it for this session registry. */
3263 (void) close_metadata(registry, ua_sess->consumer);
3264 }
3265 }
3266 cds_list_add(&ua_sess->teardown_node, &lta->teardown_head);
3267
3268 pthread_mutex_unlock(&ua_sess->lock);
3269 }
3270
3271 /* Remove application from PID hash table */
3272 ret = lttng_ht_del(ust_app_ht_by_sock, &ust_app_sock_iter);
3273 assert(!ret);
3274
3275 /*
3276 * Remove application from notify hash table. The thread handling the
3277 * notify socket could have deleted the node so ignore on error because
3278 * either way it's valid. The close of that socket is handled by the other
3279 * thread.
3280 */
3281 iter.iter.node = &lta->notify_sock_n.node;
3282 (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter);
3283
3284 /*
3285 * Ignore return value since the node might have been removed before by an
3286 * add replace during app registration because the PID can be reassigned by
3287 * the OS.
3288 */
3289 iter.iter.node = &lta->pid_n.node;
3290 ret = lttng_ht_del(ust_app_ht, &iter);
3291 if (ret) {
3292 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3293 lta->pid);
3294 }
3295
3296 /* Free memory */
3297 call_rcu(&lta->pid_n.head, delete_ust_app_rcu);
3298
3299 rcu_read_unlock();
3300 return;
3301 }
3302
3303 /*
3304 * Fill events array with all events name of all registered apps.
3305 */
3306 int ust_app_list_events(struct lttng_event **events)
3307 {
3308 int ret, handle;
3309 size_t nbmem, count = 0;
3310 struct lttng_ht_iter iter;
3311 struct ust_app *app;
3312 struct lttng_event *tmp_event;
3313
3314 nbmem = UST_APP_EVENT_LIST_SIZE;
3315 tmp_event = zmalloc(nbmem * sizeof(struct lttng_event));
3316 if (tmp_event == NULL) {
3317 PERROR("zmalloc ust app events");
3318 ret = -ENOMEM;
3319 goto error;
3320 }
3321
3322 rcu_read_lock();
3323
3324 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3325 struct lttng_ust_tracepoint_iter uiter;
3326
3327 health_code_update();
3328
3329 if (!app->compatible) {
3330 /*
3331 * TODO: In time, we should notice the caller of this error by
3332 * telling him that this is a version error.
3333 */
3334 continue;
3335 }
3336 handle = ustctl_tracepoint_list(app->sock);
3337 if (handle < 0) {
3338 if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) {
3339 ERR("UST app list events getting handle failed for app pid %d",
3340 app->pid);
3341 }
3342 continue;
3343 }
3344
3345 while ((ret = ustctl_tracepoint_list_get(app->sock, handle,
3346 &uiter)) != -LTTNG_UST_ERR_NOENT) {
3347 /* Handle ustctl error. */
3348 if (ret < 0) {
3349 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
3350 ERR("UST app tp list get failed for app %d with ret %d",
3351 app->sock, ret);
3352 } else {
3353 DBG3("UST app tp list get failed. Application is dead");
3354 /*
3355 * This is normal behavior, an application can die during the
3356 * creation process. Don't report an error so the execution can
3357 * continue normally. Continue normal execution.
3358 */
3359 break;
3360 }
3361 free(tmp_event);
3362 goto rcu_error;
3363 }
3364
3365 health_code_update();
3366 if (count >= nbmem) {
3367 /* In case the realloc fails, we free the memory */
3368 struct lttng_event *new_tmp_event;
3369 size_t new_nbmem;
3370
3371 new_nbmem = nbmem << 1;
3372 DBG2("Reallocating event list from %zu to %zu entries",
3373 nbmem, new_nbmem);
3374 new_tmp_event = realloc(tmp_event,
3375 new_nbmem * sizeof(struct lttng_event));
3376 if (new_tmp_event == NULL) {
3377 PERROR("realloc ust app events");
3378 free(tmp_event);
3379 ret = -ENOMEM;
3380 goto rcu_error;
3381 }
3382 /* Zero the new memory */
3383 memset(new_tmp_event + nbmem, 0,
3384 (new_nbmem - nbmem) * sizeof(struct lttng_event));
3385 nbmem = new_nbmem;
3386 tmp_event = new_tmp_event;
3387 }
3388 memcpy(tmp_event[count].name, uiter.name, LTTNG_UST_SYM_NAME_LEN);
3389 tmp_event[count].loglevel = uiter.loglevel;
3390 tmp_event[count].type = (enum lttng_event_type) LTTNG_UST_TRACEPOINT;
3391 tmp_event[count].pid = app->pid;
3392 tmp_event[count].enabled = -1;
3393 count++;
3394 }
3395 }
3396
3397 ret = count;
3398 *events = tmp_event;
3399
3400 DBG2("UST app list events done (%zu events)", count);
3401
3402 rcu_error:
3403 rcu_read_unlock();
3404 error:
3405 health_code_update();
3406 return ret;
3407 }
3408
3409 /*
3410 * Fill events array with all events name of all registered apps.
3411 */
3412 int ust_app_list_event_fields(struct lttng_event_field **fields)
3413 {
3414 int ret, handle;
3415 size_t nbmem, count = 0;
3416 struct lttng_ht_iter iter;
3417 struct ust_app *app;
3418 struct lttng_event_field *tmp_event;
3419
3420 nbmem = UST_APP_EVENT_LIST_SIZE;
3421 tmp_event = zmalloc(nbmem * sizeof(struct lttng_event_field));
3422 if (tmp_event == NULL) {
3423 PERROR("zmalloc ust app event fields");
3424 ret = -ENOMEM;
3425 goto error;
3426 }
3427
3428 rcu_read_lock();
3429
3430 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3431 struct lttng_ust_field_iter uiter;
3432
3433 health_code_update();
3434
3435 if (!app->compatible) {
3436 /*
3437 * TODO: In time, we should notice the caller of this error by
3438 * telling him that this is a version error.
3439 */
3440 continue;
3441 }
3442 handle = ustctl_tracepoint_field_list(app->sock);
3443 if (handle < 0) {
3444 if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) {
3445 ERR("UST app list field getting handle failed for app pid %d",
3446 app->pid);
3447 }
3448 continue;
3449 }
3450
3451 while ((ret = ustctl_tracepoint_field_list_get(app->sock, handle,
3452 &uiter)) != -LTTNG_UST_ERR_NOENT) {
3453 /* Handle ustctl error. */
3454 if (ret < 0) {
3455 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
3456 ERR("UST app tp list field failed for app %d with ret %d",
3457 app->sock, ret);
3458 } else {
3459 DBG3("UST app tp list field failed. Application is dead");
3460 /*
3461 * This is normal behavior, an application can die during the
3462 * creation process. Don't report an error so the execution can
3463 * continue normally. Reset list and count for next app.
3464 */
3465 break;
3466 }
3467 free(tmp_event);
3468 goto rcu_error;
3469 }
3470
3471 health_code_update();
3472 if (count >= nbmem) {
3473 /* In case the realloc fails, we free the memory */
3474 struct lttng_event_field *new_tmp_event;
3475 size_t new_nbmem;
3476
3477 new_nbmem = nbmem << 1;
3478 DBG2("Reallocating event field list from %zu to %zu entries",
3479 nbmem, new_nbmem);
3480 new_tmp_event = realloc(tmp_event,
3481 new_nbmem * sizeof(struct lttng_event_field));
3482 if (new_tmp_event == NULL) {
3483 PERROR("realloc ust app event fields");
3484 free(tmp_event);
3485 ret = -ENOMEM;
3486 goto rcu_error;
3487 }
3488 /* Zero the new memory */
3489 memset(new_tmp_event + nbmem, 0,
3490 (new_nbmem - nbmem) * sizeof(struct lttng_event_field));
3491 nbmem = new_nbmem;
3492 tmp_event = new_tmp_event;
3493 }
3494
3495 memcpy(tmp_event[count].field_name, uiter.field_name, LTTNG_UST_SYM_NAME_LEN);
3496 /* Mapping between these enums matches 1 to 1. */
3497 tmp_event[count].type = (enum lttng_event_field_type) uiter.type;
3498 tmp_event[count].nowrite = uiter.nowrite;
3499
3500 memcpy(tmp_event[count].event.name, uiter.event_name, LTTNG_UST_SYM_NAME_LEN);
3501 tmp_event[count].event.loglevel = uiter.loglevel;
3502 tmp_event[count].event.type = LTTNG_EVENT_TRACEPOINT;
3503 tmp_event[count].event.pid = app->pid;
3504 tmp_event[count].event.enabled = -1;
3505 count++;
3506 }
3507 }
3508
3509 ret = count;
3510 *fields = tmp_event;
3511
3512 DBG2("UST app list event fields done (%zu events)", count);
3513
3514 rcu_error:
3515 rcu_read_unlock();
3516 error:
3517 health_code_update();
3518 return ret;
3519 }
3520
3521 /*
3522 * Free and clean all traceable apps of the global list.
3523 *
3524 * Should _NOT_ be called with RCU read-side lock held.
3525 */
3526 void ust_app_clean_list(void)
3527 {
3528 int ret;
3529 struct ust_app *app;
3530 struct lttng_ht_iter iter;
3531
3532 DBG2("UST app cleaning registered apps hash table");
3533
3534 rcu_read_lock();
3535
3536 if (ust_app_ht) {
3537 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3538 ret = lttng_ht_del(ust_app_ht, &iter);
3539 assert(!ret);
3540 call_rcu(&app->pid_n.head, delete_ust_app_rcu);
3541 }
3542 }
3543
3544 /* Cleanup socket hash table */
3545 if (ust_app_ht_by_sock) {
3546 cds_lfht_for_each_entry(ust_app_ht_by_sock->ht, &iter.iter, app,
3547 sock_n.node) {
3548 ret = lttng_ht_del(ust_app_ht_by_sock, &iter);
3549 assert(!ret);
3550 }
3551 }
3552
3553 /* Cleanup notify socket hash table */
3554 if (ust_app_ht_by_notify_sock) {
3555 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock->ht, &iter.iter, app,
3556 notify_sock_n.node) {
3557 ret = lttng_ht_del(ust_app_ht_by_notify_sock, &iter);
3558 assert(!ret);
3559 }
3560 }
3561 rcu_read_unlock();
3562
3563 /* Destroy is done only when the ht is empty */
3564 if (ust_app_ht) {
3565 ht_cleanup_push(ust_app_ht);
3566 }
3567 if (ust_app_ht_by_sock) {
3568 ht_cleanup_push(ust_app_ht_by_sock);
3569 }
3570 if (ust_app_ht_by_notify_sock) {
3571 ht_cleanup_push(ust_app_ht_by_notify_sock);
3572 }
3573 }
3574
3575 /*
3576 * Init UST app hash table.
3577 */
3578 int ust_app_ht_alloc(void)
3579 {
3580 ust_app_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
3581 if (!ust_app_ht) {
3582 return -1;
3583 }
3584 ust_app_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
3585 if (!ust_app_ht_by_sock) {
3586 return -1;
3587 }
3588 ust_app_ht_by_notify_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
3589 if (!ust_app_ht_by_notify_sock) {
3590 return -1;
3591 }
3592 return 0;
3593 }
3594
3595 /*
3596 * For a specific UST session, disable the channel for all registered apps.
3597 */
3598 int ust_app_disable_channel_glb(struct ltt_ust_session *usess,
3599 struct ltt_ust_channel *uchan)
3600 {
3601 int ret = 0;
3602 struct lttng_ht_iter iter;
3603 struct lttng_ht_node_str *ua_chan_node;
3604 struct ust_app *app;
3605 struct ust_app_session *ua_sess;
3606 struct ust_app_channel *ua_chan;
3607
3608 if (usess == NULL || uchan == NULL) {
3609 ERR("Disabling UST global channel with NULL values");
3610 ret = -1;
3611 goto error;
3612 }
3613
3614 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64,
3615 uchan->name, usess->id);
3616
3617 rcu_read_lock();
3618
3619 /* For every registered applications */
3620 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3621 struct lttng_ht_iter uiter;
3622 if (!app->compatible) {
3623 /*
3624 * TODO: In time, we should notice the caller of this error by
3625 * telling him that this is a version error.
3626 */
3627 continue;
3628 }
3629 ua_sess = lookup_session_by_app(usess, app);
3630 if (ua_sess == NULL) {
3631 continue;
3632 }
3633
3634 /* Get channel */
3635 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
3636 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
3637 /* If the session if found for the app, the channel must be there */
3638 assert(ua_chan_node);
3639
3640 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
3641 /* The channel must not be already disabled */
3642 assert(ua_chan->enabled == 1);
3643
3644 /* Disable channel onto application */
3645 ret = disable_ust_app_channel(ua_sess, ua_chan, app);
3646 if (ret < 0) {
3647 /* XXX: We might want to report this error at some point... */
3648 continue;
3649 }
3650 }
3651
3652 rcu_read_unlock();
3653
3654 error:
3655 return ret;
3656 }
3657
3658 /*
3659 * For a specific UST session, enable the channel for all registered apps.
3660 */
3661 int ust_app_enable_channel_glb(struct ltt_ust_session *usess,
3662 struct ltt_ust_channel *uchan)
3663 {
3664 int ret = 0;
3665 struct lttng_ht_iter iter;
3666 struct ust_app *app;
3667 struct ust_app_session *ua_sess;
3668
3669 if (usess == NULL || uchan == NULL) {
3670 ERR("Adding UST global channel to NULL values");
3671 ret = -1;
3672 goto error;
3673 }
3674
3675 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64,
3676 uchan->name, usess->id);
3677
3678 rcu_read_lock();
3679
3680 /* For every registered applications */
3681 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3682 if (!app->compatible) {
3683 /*
3684 * TODO: In time, we should notice the caller of this error by
3685 * telling him that this is a version error.
3686 */
3687 continue;
3688 }
3689 ua_sess = lookup_session_by_app(usess, app);
3690 if (ua_sess == NULL) {
3691 continue;
3692 }
3693
3694 /* Enable channel onto application */
3695 ret = enable_ust_app_channel(ua_sess, uchan, app);
3696 if (ret < 0) {
3697 /* XXX: We might want to report this error at some point... */
3698 continue;
3699 }
3700 }
3701
3702 rcu_read_unlock();
3703
3704 error:
3705 return ret;
3706 }
3707
3708 /*
3709 * Disable an event in a channel and for a specific session.
3710 */
3711 int ust_app_disable_event_glb(struct ltt_ust_session *usess,
3712 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
3713 {
3714 int ret = 0;
3715 struct lttng_ht_iter iter, uiter;
3716 struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
3717 struct ust_app *app;
3718 struct ust_app_session *ua_sess;
3719 struct ust_app_channel *ua_chan;
3720 struct ust_app_event *ua_event;
3721
3722 DBG("UST app disabling event %s for all apps in channel "
3723 "%s for session id %" PRIu64,
3724 uevent->attr.name, uchan->name, usess->id);
3725
3726 rcu_read_lock();
3727
3728 /* For all registered applications */
3729 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3730 if (!app->compatible) {
3731 /*
3732 * TODO: In time, we should notice the caller of this error by
3733 * telling him that this is a version error.
3734 */
3735 continue;
3736 }
3737 ua_sess = lookup_session_by_app(usess, app);
3738 if (ua_sess == NULL) {
3739 /* Next app */
3740 continue;
3741 }
3742
3743 /* Lookup channel in the ust app session */
3744 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
3745 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
3746 if (ua_chan_node == NULL) {
3747 DBG2("Channel %s not found in session id %" PRIu64 " for app pid %d."
3748 "Skipping", uchan->name, usess->id, app->pid);
3749 continue;
3750 }
3751 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
3752
3753 lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &uiter);
3754 ua_event_node = lttng_ht_iter_get_node_str(&uiter);
3755 if (ua_event_node == NULL) {
3756 DBG2("Event %s not found in channel %s for app pid %d."
3757 "Skipping", uevent->attr.name, uchan->name, app->pid);
3758 continue;
3759 }
3760 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
3761
3762 ret = disable_ust_app_event(ua_sess, ua_event, app);
3763 if (ret < 0) {
3764 /* XXX: Report error someday... */
3765 continue;
3766 }
3767 }
3768
3769 rcu_read_unlock();
3770
3771 return ret;
3772 }
3773
3774 /*
3775 * For a specific UST session, create the channel for all registered apps.
3776 */
3777 int ust_app_create_channel_glb(struct ltt_ust_session *usess,
3778 struct ltt_ust_channel *uchan)
3779 {
3780 int ret = 0, created;
3781 struct lttng_ht_iter iter;
3782 struct ust_app *app;
3783 struct ust_app_session *ua_sess = NULL;
3784
3785 /* Very wrong code flow */
3786 assert(usess);
3787 assert(uchan);
3788
3789 DBG2("UST app adding channel %s to UST domain for session id %" PRIu64,
3790 uchan->name, usess->id);
3791
3792 rcu_read_lock();
3793
3794 /* For every registered applications */
3795 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3796 if (!app->compatible) {
3797 /*
3798 * TODO: In time, we should notice the caller of this error by
3799 * telling him that this is a version error.
3800 */
3801 continue;
3802 }
3803 if (!trace_ust_pid_tracker_lookup(usess, app->pid)) {
3804 /* Skip. */
3805 continue;
3806 }
3807
3808 /*
3809 * Create session on the tracer side and add it to app session HT. Note
3810 * that if session exist, it will simply return a pointer to the ust
3811 * app session.
3812 */
3813 ret = create_ust_app_session(usess, app, &ua_sess, &created);
3814 if (ret < 0) {
3815 switch (ret) {
3816 case -ENOTCONN:
3817 /*
3818 * The application's socket is not valid. Either a bad socket
3819 * or a timeout on it. We can't inform the caller that for a
3820 * specific app, the session failed so lets continue here.
3821 */
3822 continue;
3823 case -ENOMEM:
3824 default:
3825 goto error_rcu_unlock;
3826 }
3827 }
3828 assert(ua_sess);
3829
3830 pthread_mutex_lock(&ua_sess->lock);
3831
3832 if (ua_sess->deleted) {
3833 pthread_mutex_unlock(&ua_sess->lock);
3834 continue;
3835 }
3836
3837 if (!strncmp(uchan->name, DEFAULT_METADATA_NAME,
3838 sizeof(uchan->name))) {
3839 copy_channel_attr_to_ustctl(&ua_sess->metadata_attr, &uchan->attr);
3840 ret = 0;
3841 } else {
3842 /* Create channel onto application. We don't need the chan ref. */
3843 ret = create_ust_app_channel(ua_sess, uchan, app,
3844 LTTNG_UST_CHAN_PER_CPU, usess, NULL);
3845 }
3846 pthread_mutex_unlock(&ua_sess->lock);
3847 if (ret < 0) {
3848 if (ret == -ENOMEM) {
3849 /* No more memory is a fatal error. Stop right now. */
3850 goto error_rcu_unlock;
3851 }
3852 /* Cleanup the created session if it's the case. */
3853 if (created) {
3854 destroy_app_session(app, ua_sess);
3855 }
3856 }
3857 }
3858
3859 error_rcu_unlock:
3860 rcu_read_unlock();
3861 return ret;
3862 }
3863
3864 /*
3865 * Enable event for a specific session and channel on the tracer.
3866 */
3867 int ust_app_enable_event_glb(struct ltt_ust_session *usess,
3868 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
3869 {
3870 int ret = 0;
3871 struct lttng_ht_iter iter, uiter;
3872 struct lttng_ht_node_str *ua_chan_node;
3873 struct ust_app *app;
3874 struct ust_app_session *ua_sess;
3875 struct ust_app_channel *ua_chan;
3876 struct ust_app_event *ua_event;
3877
3878 DBG("UST app enabling event %s for all apps for session id %" PRIu64,
3879 uevent->attr.name, usess->id);
3880
3881 /*
3882 * NOTE: At this point, this function is called only if the session and
3883 * channel passed are already created for all apps. and enabled on the
3884 * tracer also.
3885 */
3886
3887 rcu_read_lock();
3888
3889 /* For all registered applications */
3890 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3891 if (!app->compatible) {
3892 /*
3893 * TODO: In time, we should notice the caller of this error by
3894 * telling him that this is a version error.
3895 */
3896 continue;
3897 }
3898 ua_sess = lookup_session_by_app(usess, app);
3899 if (!ua_sess) {
3900 /* The application has problem or is probably dead. */
3901 continue;
3902 }
3903
3904 pthread_mutex_lock(&ua_sess->lock);
3905
3906 if (ua_sess->deleted) {
3907 pthread_mutex_unlock(&ua_sess->lock);
3908 continue;
3909 }
3910
3911 /* Lookup channel in the ust app session */
3912 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
3913 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
3914 /* If the channel is not found, there is a code flow error */
3915 assert(ua_chan_node);
3916
3917 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
3918
3919 /* Get event node */
3920 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
3921 uevent->filter, uevent->attr.loglevel, uevent->exclusion);
3922 if (ua_event == NULL) {
3923 DBG3("UST app enable event %s not found for app PID %d."
3924 "Skipping app", uevent->attr.name, app->pid);
3925 goto next_app;
3926 }
3927
3928 ret = enable_ust_app_event(ua_sess, ua_event, app);
3929 if (ret < 0) {
3930 pthread_mutex_unlock(&ua_sess->lock);
3931 goto error;
3932 }
3933 next_app:
3934 pthread_mutex_unlock(&ua_sess->lock);
3935 }
3936
3937 error:
3938 rcu_read_unlock();
3939 return ret;
3940 }
3941
3942 /*
3943 * For a specific existing UST session and UST channel, creates the event for
3944 * all registered apps.
3945 */
3946 int ust_app_create_event_glb(struct ltt_ust_session *usess,
3947 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
3948 {
3949 int ret = 0;
3950 struct lttng_ht_iter iter, uiter;
3951 struct lttng_ht_node_str *ua_chan_node;
3952 struct ust_app *app;
3953 struct ust_app_session *ua_sess;
3954 struct ust_app_channel *ua_chan;
3955
3956 DBG("UST app creating event %s for all apps for session id %" PRIu64,
3957 uevent->attr.name, usess->id);
3958
3959 rcu_read_lock();
3960
3961 /* For all registered applications */
3962 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3963 if (!app->compatible) {
3964 /*
3965 * TODO: In time, we should notice the caller of this error by
3966 * telling him that this is a version error.
3967 */
3968 continue;
3969 }
3970 ua_sess = lookup_session_by_app(usess, app);
3971 if (!ua_sess) {
3972 /* The application has problem or is probably dead. */
3973 continue;
3974 }
3975
3976 pthread_mutex_lock(&ua_sess->lock);
3977
3978 if (ua_sess->deleted) {
3979 pthread_mutex_unlock(&ua_sess->lock);
3980 continue;
3981 }
3982
3983 /* Lookup channel in the ust app session */
3984 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
3985 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
3986 /* If the channel is not found, there is a code flow error */
3987 assert(ua_chan_node);
3988
3989 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
3990
3991 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
3992 pthread_mutex_unlock(&ua_sess->lock);
3993 if (ret < 0) {
3994 if (ret != -LTTNG_UST_ERR_EXIST) {
3995 /* Possible value at this point: -ENOMEM. If so, we stop! */
3996 break;
3997 }
3998 DBG2("UST app event %s already exist on app PID %d",
3999 uevent->attr.name, app->pid);
4000 continue;
4001 }
4002 }
4003
4004 rcu_read_unlock();
4005
4006 return ret;
4007 }
4008
4009 /*
4010 * Start tracing for a specific UST session and app.
4011 */
4012 static
4013 int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
4014 {
4015 int ret = 0;
4016 struct ust_app_session *ua_sess;
4017
4018 DBG("Starting tracing for ust app pid %d", app->pid);
4019
4020 rcu_read_lock();
4021
4022 if (!app->compatible) {
4023 goto end;
4024 }
4025
4026 ua_sess = lookup_session_by_app(usess, app);
4027 if (ua_sess == NULL) {
4028 /* The session is in teardown process. Ignore and continue. */
4029 goto end;
4030 }
4031
4032 pthread_mutex_lock(&ua_sess->lock);
4033
4034 if (ua_sess->deleted) {
4035 pthread_mutex_unlock(&ua_sess->lock);
4036 goto end;
4037 }
4038
4039 /* Upon restart, we skip the setup, already done */
4040 if (ua_sess->started) {
4041 goto skip_setup;
4042 }
4043
4044 /* Create directories if consumer is LOCAL and has a path defined. */
4045 if (usess->consumer->type == CONSUMER_DST_LOCAL &&
4046 strlen(usess->consumer->dst.trace_path) > 0) {
4047 ret = run_as_mkdir_recursive(usess->consumer->dst.trace_path,
4048 S_IRWXU | S_IRWXG, ua_sess->euid, ua_sess->egid);
4049 if (ret < 0) {
4050 if (ret != -EEXIST) {
4051 ERR("Trace directory creation error");
4052 goto error_unlock;
4053 }
4054 }
4055 }
4056
4057 /*
4058 * Create the metadata for the application. This returns gracefully if a
4059 * metadata was already set for the session.
4060 */
4061 ret = create_ust_app_metadata(ua_sess, app, usess->consumer);
4062 if (ret < 0) {
4063 goto error_unlock;
4064 }
4065
4066 health_code_update();
4067
4068 skip_setup:
4069 /* This start the UST tracing */
4070 ret = ustctl_start_session(app->sock, ua_sess->handle);
4071 if (ret < 0) {
4072 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
4073 ERR("Error starting tracing for app pid: %d (ret: %d)",
4074 app->pid, ret);
4075 } else {
4076 DBG("UST app start session failed. Application is dead.");
4077 /*
4078 * This is normal behavior, an application can die during the
4079 * creation process. Don't report an error so the execution can
4080 * continue normally.
4081 */
4082 pthread_mutex_unlock(&ua_sess->lock);
4083 goto end;
4084 }
4085 goto error_unlock;
4086 }
4087
4088 /* Indicate that the session has been started once */
4089 ua_sess->started = 1;
4090
4091 pthread_mutex_unlock(&ua_sess->lock);
4092
4093 health_code_update();
4094
4095 /* Quiescent wait after starting trace */
4096 ret = ustctl_wait_quiescent(app->sock);
4097 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
4098 ERR("UST app wait quiescent failed for app pid %d ret %d",
4099 app->pid, ret);
4100 }
4101
4102 end:
4103 rcu_read_unlock();
4104 health_code_update();
4105 return 0;
4106
4107 error_unlock:
4108 pthread_mutex_unlock(&ua_sess->lock);
4109 rcu_read_unlock();
4110 health_code_update();
4111 return -1;
4112 }
4113
4114 /*
4115 * Stop tracing for a specific UST session and app.
4116 */
4117 static
4118 int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app)
4119 {
4120 int ret = 0;
4121 struct ust_app_session *ua_sess;
4122 struct ust_registry_session *registry;
4123
4124 DBG("Stopping tracing for ust app pid %d", app->pid);
4125
4126 rcu_read_lock();
4127
4128 if (!app->compatible) {
4129 goto end_no_session;
4130 }
4131
4132 ua_sess = lookup_session_by_app(usess, app);
4133 if (ua_sess == NULL) {
4134 goto end_no_session;
4135 }
4136
4137 pthread_mutex_lock(&ua_sess->lock);
4138
4139 if (ua_sess->deleted) {
4140 pthread_mutex_unlock(&ua_sess->lock);
4141 goto end_no_session;
4142 }
4143
4144 /*
4145 * If started = 0, it means that stop trace has been called for a session
4146 * that was never started. It's possible since we can have a fail start
4147 * from either the application manager thread or the command thread. Simply
4148 * indicate that this is a stop error.
4149 */
4150 if (!ua_sess->started) {
4151 goto error_rcu_unlock;
4152 }
4153
4154 health_code_update();
4155
4156 /* This inhibits UST tracing */
4157 ret = ustctl_stop_session(app->sock, ua_sess->handle);
4158 if (ret < 0) {
4159 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
4160 ERR("Error stopping tracing for app pid: %d (ret: %d)",
4161 app->pid, ret);
4162 } else {
4163 DBG("UST app stop session failed. Application is dead.");
4164 /*
4165 * This is normal behavior, an application can die during the
4166 * creation process. Don't report an error so the execution can
4167 * continue normally.
4168 */
4169 goto end_unlock;
4170 }
4171 goto error_rcu_unlock;
4172 }
4173
4174 health_code_update();
4175
4176 /* Quiescent wait after stopping trace */
4177 ret = ustctl_wait_quiescent(app->sock);
4178 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
4179 ERR("UST app wait quiescent failed for app pid %d ret %d",
4180 app->pid, ret);
4181 }
4182
4183 health_code_update();
4184
4185 registry = get_session_registry(ua_sess);
4186 assert(registry);
4187
4188 /* Push metadata for application before freeing the application. */
4189 (void) push_metadata(registry, ua_sess->consumer);
4190
4191 end_unlock:
4192 pthread_mutex_unlock(&ua_sess->lock);
4193 end_no_session:
4194 rcu_read_unlock();
4195 health_code_update();
4196 return 0;
4197
4198 error_rcu_unlock:
4199 pthread_mutex_unlock(&ua_sess->lock);
4200 rcu_read_unlock();
4201 health_code_update();
4202 return -1;
4203 }
4204
4205 static
4206 int ust_app_flush_app_session(struct ust_app *app,
4207 struct ust_app_session *ua_sess)
4208 {
4209 int ret, retval = 0;
4210 struct lttng_ht_iter iter;
4211 struct ust_app_channel *ua_chan;
4212 struct consumer_socket *socket;
4213
4214 DBG("Flushing app session buffers for ust app pid %d", app->pid);
4215
4216 rcu_read_lock();
4217
4218 if (!app->compatible) {
4219 goto end_not_compatible;
4220 }
4221
4222 pthread_mutex_lock(&ua_sess->lock);
4223
4224 if (ua_sess->deleted) {
4225 goto end_deleted;
4226 }
4227
4228 health_code_update();
4229
4230 /* Flushing buffers */
4231 socket = consumer_find_socket_by_bitness(app->bits_per_long,
4232 ua_sess->consumer);
4233
4234 /* Flush buffers and push metadata. */
4235 switch (ua_sess->buffer_type) {
4236 case LTTNG_BUFFER_PER_PID:
4237 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
4238 node.node) {
4239 health_code_update();
4240 assert(ua_chan->is_sent);
4241 ret = consumer_flush_channel(socket, ua_chan->key);
4242 if (ret) {
4243 ERR("Error flushing consumer channel");
4244 retval = -1;
4245 continue;
4246 }
4247 }
4248 break;
4249 case LTTNG_BUFFER_PER_UID:
4250 default:
4251 assert(0);
4252 break;
4253 }
4254
4255 health_code_update();
4256
4257 end_deleted:
4258 pthread_mutex_unlock(&ua_sess->lock);
4259
4260 end_not_compatible:
4261 rcu_read_unlock();
4262 health_code_update();
4263 return retval;
4264 }
4265
4266 /*
4267 * Flush buffers for all applications for a specific UST session.
4268 * Called with UST session lock held.
4269 */
4270 static
4271 int ust_app_flush_session(struct ltt_ust_session *usess)
4272
4273 {
4274 int ret = 0;
4275
4276 DBG("Flushing session buffers for all ust apps");
4277
4278 rcu_read_lock();
4279
4280 /* Flush buffers and push metadata. */
4281 switch (usess->buffer_type) {
4282 case LTTNG_BUFFER_PER_UID:
4283 {
4284 struct buffer_reg_uid *reg;
4285 struct lttng_ht_iter iter;
4286
4287 /* Flush all per UID buffers associated to that session. */
4288 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
4289 struct ust_registry_session *ust_session_reg;
4290 struct buffer_reg_channel *reg_chan;
4291 struct consumer_socket *socket;
4292
4293 /* Get consumer socket to use to push the metadata.*/
4294 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
4295 usess->consumer);
4296 if (!socket) {
4297 /* Ignore request if no consumer is found for the session. */
4298 continue;
4299 }
4300
4301 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
4302 reg_chan, node.node) {
4303 /*
4304 * The following call will print error values so the return
4305 * code is of little importance because whatever happens, we
4306 * have to try them all.
4307 */
4308 (void) consumer_flush_channel(socket, reg_chan->consumer_key);
4309 }
4310
4311 ust_session_reg = reg->registry->reg.ust;
4312 /* Push metadata. */
4313 (void) push_metadata(ust_session_reg, usess->consumer);
4314 }
4315 break;
4316 }
4317 case LTTNG_BUFFER_PER_PID:
4318 {
4319 struct ust_app_session *ua_sess;
4320 struct lttng_ht_iter iter;
4321 struct ust_app *app;
4322
4323 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4324 ua_sess = lookup_session_by_app(usess, app);
4325 if (ua_sess == NULL) {
4326 continue;
4327 }
4328 (void) ust_app_flush_app_session(app, ua_sess);
4329 }
4330 break;
4331 }
4332 default:
4333 ret = -1;
4334 assert(0);
4335 break;
4336 }
4337
4338 rcu_read_unlock();
4339 health_code_update();
4340 return ret;
4341 }
4342
4343 /*
4344 * Destroy a specific UST session in apps.
4345 */
4346 static int destroy_trace(struct ltt_ust_session *usess, struct ust_app *app)
4347 {
4348 int ret;
4349 struct ust_app_session *ua_sess;
4350 struct lttng_ht_iter iter;
4351 struct lttng_ht_node_u64 *node;
4352
4353 DBG("Destroy tracing for ust app pid %d", app->pid);
4354
4355 rcu_read_lock();
4356
4357 if (!app->compatible) {
4358 goto end;
4359 }
4360
4361 __lookup_session_by_app(usess, app, &iter);
4362 node = lttng_ht_iter_get_node_u64(&iter);
4363 if (node == NULL) {
4364 /* Session is being or is deleted. */
4365 goto end;
4366 }
4367 ua_sess = caa_container_of(node, struct ust_app_session, node);
4368
4369 health_code_update();
4370 destroy_app_session(app, ua_sess);
4371
4372 health_code_update();
4373
4374 /* Quiescent wait after stopping trace */
4375 ret = ustctl_wait_quiescent(app->sock);
4376 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
4377 ERR("UST app wait quiescent failed for app pid %d ret %d",
4378 app->pid, ret);
4379 }
4380 end:
4381 rcu_read_unlock();
4382 health_code_update();
4383 return 0;
4384 }
4385
4386 /*
4387 * Start tracing for the UST session.
4388 */
4389 int ust_app_start_trace_all(struct ltt_ust_session *usess)
4390 {
4391 int ret = 0;
4392 struct lttng_ht_iter iter;
4393 struct ust_app *app;
4394
4395 DBG("Starting all UST traces");
4396
4397 rcu_read_lock();
4398
4399 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4400 ret = ust_app_start_trace(usess, app);
4401 if (ret < 0) {
4402 /* Continue to next apps even on error */
4403 continue;
4404 }
4405 }
4406
4407 rcu_read_unlock();
4408
4409 return 0;
4410 }
4411
4412 /*
4413 * Start tracing for the UST session.
4414 * Called with UST session lock held.
4415 */
4416 int ust_app_stop_trace_all(struct ltt_ust_session *usess)
4417 {
4418 int ret = 0;
4419 struct lttng_ht_iter iter;
4420 struct ust_app *app;
4421
4422 DBG("Stopping all UST traces");
4423
4424 rcu_read_lock();
4425
4426 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4427 ret = ust_app_stop_trace(usess, app);
4428 if (ret < 0) {
4429 /* Continue to next apps even on error */
4430 continue;
4431 }
4432 }
4433
4434 (void) ust_app_flush_session(usess);
4435
4436 rcu_read_unlock();
4437
4438 return 0;
4439 }
4440
4441 /*
4442 * Destroy app UST session.
4443 */
4444 int ust_app_destroy_trace_all(struct ltt_ust_session *usess)
4445 {
4446 int ret = 0;
4447 struct lttng_ht_iter iter;
4448 struct ust_app *app;
4449
4450 DBG("Destroy all UST traces");
4451
4452 rcu_read_lock();
4453
4454 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4455 ret = destroy_trace(usess, app);
4456 if (ret < 0) {
4457 /* Continue to next apps even on error */
4458 continue;
4459 }
4460 }
4461
4462 rcu_read_unlock();
4463
4464 return 0;
4465 }
4466
4467 static
4468 void ust_app_global_create(struct ltt_ust_session *usess, struct ust_app *app)
4469 {
4470 int ret = 0;
4471 struct lttng_ht_iter iter, uiter;
4472 struct ust_app_session *ua_sess = NULL;
4473 struct ust_app_channel *ua_chan;
4474 struct ust_app_event *ua_event;
4475 struct ust_app_ctx *ua_ctx;
4476 int is_created = 0;
4477
4478 ret = create_ust_app_session(usess, app, &ua_sess, &is_created);
4479 if (ret < 0) {
4480 /* Tracer is probably gone or ENOMEM. */
4481 goto error;
4482 }
4483 if (!is_created) {
4484 /* App session already created. */
4485 goto end;
4486 }
4487 assert(ua_sess);
4488
4489 pthread_mutex_lock(&ua_sess->lock);
4490
4491 if (ua_sess->deleted) {
4492 pthread_mutex_unlock(&ua_sess->lock);
4493 goto end;
4494 }
4495
4496 /*
4497 * We can iterate safely here over all UST app session since the create ust
4498 * app session above made a shadow copy of the UST global domain from the
4499 * ltt ust session.
4500 */
4501 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
4502 node.node) {
4503 ret = do_create_channel(app, usess, ua_sess, ua_chan);
4504 if (ret < 0) {
4505 /*
4506 * Stop everything. On error, the application failed, no more
4507 * file descriptor are available or ENOMEM so stopping here is
4508 * the only thing we can do for now.
4509 */
4510 goto error_unlock;
4511 }
4512
4513 /*
4514 * Add context using the list so they are enabled in the same order the
4515 * user added them.
4516 */
4517 cds_list_for_each_entry(ua_ctx, &ua_chan->ctx_list, list) {
4518 ret = create_ust_channel_context(ua_chan, ua_ctx, app);
4519 if (ret < 0) {
4520 goto error_unlock;
4521 }
4522 }
4523
4524
4525 /* For each events */
4526 cds_lfht_for_each_entry(ua_chan->events->ht, &uiter.iter, ua_event,
4527 node.node) {
4528 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
4529 if (ret < 0) {
4530 goto error_unlock;
4531 }
4532 }
4533 }
4534
4535 pthread_mutex_unlock(&ua_sess->lock);
4536
4537 if (usess->active) {
4538 ret = ust_app_start_trace(usess, app);
4539 if (ret < 0) {
4540 goto error;
4541 }
4542
4543 DBG2("UST trace started for app pid %d", app->pid);
4544 }
4545 end:
4546 /* Everything went well at this point. */
4547 return;
4548
4549 error_unlock:
4550 pthread_mutex_unlock(&ua_sess->lock);
4551 error:
4552 if (ua_sess) {
4553 destroy_app_session(app, ua_sess);
4554 }
4555 return;
4556 }
4557
4558 static
4559 void ust_app_global_destroy(struct ltt_ust_session *usess, struct ust_app *app)
4560 {
4561 struct ust_app_session *ua_sess;
4562
4563 ua_sess = lookup_session_by_app(usess, app);
4564 if (ua_sess == NULL) {
4565 return;
4566 }
4567 destroy_app_session(app, ua_sess);
4568 }
4569
4570 /*
4571 * Add channels/events from UST global domain to registered apps at sock.
4572 *
4573 * Called with session lock held.
4574 * Called with RCU read-side lock held.
4575 */
4576 void ust_app_global_update(struct ltt_ust_session *usess, struct ust_app *app)
4577 {
4578 assert(usess);
4579
4580 DBG2("UST app global update for app sock %d for session id %" PRIu64,
4581 app->sock, usess->id);
4582
4583 if (!app->compatible) {
4584 return;
4585 }
4586
4587 if (trace_ust_pid_tracker_lookup(usess, app->pid)) {
4588 ust_app_global_create(usess, app);
4589 } else {
4590 ust_app_global_destroy(usess, app);
4591 }
4592 }
4593
4594 /*
4595 * Called with session lock held.
4596 */
4597 void ust_app_global_update_all(struct ltt_ust_session *usess)
4598 {
4599 struct lttng_ht_iter iter;
4600 struct ust_app *app;
4601
4602 rcu_read_lock();
4603 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4604 ust_app_global_update(usess, app);
4605 }
4606 rcu_read_unlock();
4607 }
4608
4609 /*
4610 * Add context to a specific channel for global UST domain.
4611 */
4612 int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess,
4613 struct ltt_ust_channel *uchan, struct ltt_ust_context *uctx)
4614 {
4615 int ret = 0;
4616 struct lttng_ht_node_str *ua_chan_node;
4617 struct lttng_ht_iter iter, uiter;
4618 struct ust_app_channel *ua_chan = NULL;
4619 struct ust_app_session *ua_sess;
4620 struct ust_app *app;
4621
4622 rcu_read_lock();
4623
4624 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4625 if (!app->compatible) {
4626 /*
4627 * TODO: In time, we should notice the caller of this error by
4628 * telling him that this is a version error.
4629 */
4630 continue;
4631 }
4632 ua_sess = lookup_session_by_app(usess, app);
4633 if (ua_sess == NULL) {
4634 continue;
4635 }
4636
4637 pthread_mutex_lock(&ua_sess->lock);
4638
4639 if (ua_sess->deleted) {
4640 pthread_mutex_unlock(&ua_sess->lock);
4641 continue;
4642 }
4643
4644 /* Lookup channel in the ust app session */
4645 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
4646 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
4647 if (ua_chan_node == NULL) {
4648 goto next_app;
4649 }
4650 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel,
4651 node);
4652 ret = create_ust_app_channel_context(ua_sess, ua_chan, &uctx->ctx, app);
4653 if (ret < 0) {
4654 goto next_app;
4655 }
4656 next_app:
4657 pthread_mutex_unlock(&ua_sess->lock);
4658 }
4659
4660 rcu_read_unlock();
4661 return ret;
4662 }
4663
4664 /*
4665 * Enable event for a channel from a UST session for a specific PID.
4666 */
4667 int ust_app_enable_event_pid(struct ltt_ust_session *usess,
4668 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent, pid_t pid)
4669 {
4670 int ret = 0;
4671 struct lttng_ht_iter iter;
4672 struct lttng_ht_node_str *ua_chan_node;
4673 struct ust_app *app;
4674 struct ust_app_session *ua_sess;
4675 struct ust_app_channel *ua_chan;
4676 struct ust_app_event *ua_event;
4677
4678 DBG("UST app enabling event %s for PID %d", uevent->attr.name, pid);
4679
4680 rcu_read_lock();
4681
4682 app = ust_app_find_by_pid(pid);
4683 if (app == NULL) {
4684 ERR("UST app enable event per PID %d not found", pid);
4685 ret = -1;
4686 goto end;
4687 }
4688
4689 if (!app->compatible) {
4690 ret = 0;
4691 goto end;
4692 }
4693
4694 ua_sess = lookup_session_by_app(usess, app);
4695 if (!ua_sess) {
4696 /* The application has problem or is probably dead. */
4697 ret = 0;
4698 goto end;
4699 }
4700
4701 pthread_mutex_lock(&ua_sess->lock);
4702
4703 if (ua_sess->deleted) {
4704 ret = 0;
4705 goto end_unlock;
4706 }
4707
4708 /* Lookup channel in the ust app session */
4709 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
4710 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
4711 /* If the channel is not found, there is a code flow error */
4712 assert(ua_chan_node);
4713
4714 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
4715
4716 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
4717 uevent->filter, uevent->attr.loglevel, uevent->exclusion);
4718 if (ua_event == NULL) {
4719 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
4720 if (ret < 0) {
4721 goto end_unlock;
4722 }
4723 } else {
4724 ret = enable_ust_app_event(ua_sess, ua_event, app);
4725 if (ret < 0) {
4726 goto end_unlock;
4727 }
4728 }
4729
4730 end_unlock:
4731 pthread_mutex_unlock(&ua_sess->lock);
4732 end:
4733 rcu_read_unlock();
4734 return ret;
4735 }
4736
4737 /*
4738 * Calibrate registered applications.
4739 */
4740 int ust_app_calibrate_glb(struct lttng_ust_calibrate *calibrate)
4741 {
4742 int ret = 0;
4743 struct lttng_ht_iter iter;
4744 struct ust_app *app;
4745
4746 rcu_read_lock();
4747
4748 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4749 if (!app->compatible) {
4750 /*
4751 * TODO: In time, we should notice the caller of this error by
4752 * telling him that this is a version error.
4753 */
4754 continue;
4755 }
4756
4757 health_code_update();
4758
4759 ret = ustctl_calibrate(app->sock, calibrate);
4760 if (ret < 0) {
4761 switch (ret) {
4762 case -ENOSYS:
4763 /* Means that it's not implemented on the tracer side. */
4764 ret = 0;
4765 break;
4766 default:
4767 DBG2("Calibrate app PID %d returned with error %d",
4768 app->pid, ret);
4769 break;
4770 }
4771 }
4772 }
4773
4774 DBG("UST app global domain calibration finished");
4775
4776 rcu_read_unlock();
4777
4778 health_code_update();
4779
4780 return ret;
4781 }
4782
4783 /*
4784 * Receive registration and populate the given msg structure.
4785 *
4786 * On success return 0 else a negative value returned by the ustctl call.
4787 */
4788 int ust_app_recv_registration(int sock, struct ust_register_msg *msg)
4789 {
4790 int ret;
4791 uint32_t pid, ppid, uid, gid;
4792
4793 assert(msg);
4794
4795 ret = ustctl_recv_reg_msg(sock, &msg->type, &msg->major, &msg->minor,
4796 &pid, &ppid, &uid, &gid,
4797 &msg->bits_per_long,
4798 &msg->uint8_t_alignment,
4799 &msg->uint16_t_alignment,
4800 &msg->uint32_t_alignment,
4801 &msg->uint64_t_alignment,
4802 &msg->long_alignment,
4803 &msg->byte_order,
4804 msg->name);
4805 if (ret < 0) {
4806 switch (-ret) {
4807 case EPIPE:
4808 case ECONNRESET:
4809 case LTTNG_UST_ERR_EXITING:
4810 DBG3("UST app recv reg message failed. Application died");
4811 break;
4812 case LTTNG_UST_ERR_UNSUP_MAJOR:
4813 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
4814 msg->major, msg->minor, LTTNG_UST_ABI_MAJOR_VERSION,
4815 LTTNG_UST_ABI_MINOR_VERSION);
4816 break;
4817 default:
4818 ERR("UST app recv reg message failed with ret %d", ret);
4819 break;
4820 }
4821 goto error;
4822 }
4823 msg->pid = (pid_t) pid;
4824 msg->ppid = (pid_t) ppid;
4825 msg->uid = (uid_t) uid;
4826 msg->gid = (gid_t) gid;
4827
4828 error:
4829 return ret;
4830 }
4831
4832 /*
4833 * Return a ust app channel object using the application object and the channel
4834 * object descriptor has a key. If not found, NULL is returned. A RCU read side
4835 * lock MUST be acquired before calling this function.
4836 */
4837 static struct ust_app_channel *find_channel_by_objd(struct ust_app *app,
4838 int objd)
4839 {
4840 struct lttng_ht_node_ulong *node;
4841 struct lttng_ht_iter iter;
4842 struct ust_app_channel *ua_chan = NULL;
4843
4844 assert(app);
4845
4846 lttng_ht_lookup(app->ust_objd, (void *)((unsigned long) objd), &iter);
4847 node = lttng_ht_iter_get_node_ulong(&iter);
4848 if (node == NULL) {
4849 DBG2("UST app channel find by objd %d not found", objd);
4850 goto error;
4851 }
4852
4853 ua_chan = caa_container_of(node, struct ust_app_channel, ust_objd_node);
4854
4855 error:
4856 return ua_chan;
4857 }
4858
4859 /*
4860 * Reply to a register channel notification from an application on the notify
4861 * socket. The channel metadata is also created.
4862 *
4863 * The session UST registry lock is acquired in this function.
4864 *
4865 * On success 0 is returned else a negative value.
4866 */
4867 static int reply_ust_register_channel(int sock, int sobjd, int cobjd,
4868 size_t nr_fields, struct ustctl_field *fields)
4869 {
4870 int ret, ret_code = 0;
4871 uint32_t chan_id, reg_count;
4872 uint64_t chan_reg_key;
4873 enum ustctl_channel_header type;
4874 struct ust_app *app;
4875 struct ust_app_channel *ua_chan;
4876 struct ust_app_session *ua_sess;
4877 struct ust_registry_session *registry;
4878 struct ust_registry_channel *chan_reg;
4879
4880 rcu_read_lock();
4881
4882 /* Lookup application. If not found, there is a code flow error. */
4883 app = find_app_by_notify_sock(sock);
4884 if (!app) {
4885 DBG("Application socket %d is being teardown. Abort event notify",
4886 sock);
4887 ret = 0;
4888 free(fields);
4889 goto error_rcu_unlock;
4890 }
4891
4892 /* Lookup channel by UST object descriptor. */
4893 ua_chan = find_channel_by_objd(app, cobjd);
4894 if (!ua_chan) {
4895 DBG("Application channel is being teardown. Abort event notify");
4896 ret = 0;
4897 free(fields);
4898 goto error_rcu_unlock;
4899 }
4900
4901 assert(ua_chan->session);
4902 ua_sess = ua_chan->session;
4903
4904 /* Get right session registry depending on the session buffer type. */
4905 registry = get_session_registry(ua_sess);
4906 assert(registry);
4907
4908 /* Depending on the buffer type, a different channel key is used. */
4909 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) {
4910 chan_reg_key = ua_chan->tracing_channel_id;
4911 } else {
4912 chan_reg_key = ua_chan->key;
4913 }
4914
4915 pthread_mutex_lock(&registry->lock);
4916
4917 chan_reg = ust_registry_channel_find(registry, chan_reg_key);
4918 assert(chan_reg);
4919
4920 if (!chan_reg->register_done) {
4921 reg_count = ust_registry_get_event_count(chan_reg);
4922 if (reg_count < 31) {
4923 type = USTCTL_CHANNEL_HEADER_COMPACT;
4924 } else {
4925 type = USTCTL_CHANNEL_HEADER_LARGE;
4926 }
4927
4928 chan_reg->nr_ctx_fields = nr_fields;
4929 chan_reg->ctx_fields = fields;
4930 chan_reg->header_type = type;
4931 } else {
4932 /* Get current already assigned values. */
4933 type = chan_reg->header_type;
4934 free(fields);
4935 /* Set to NULL so the error path does not do a double free. */
4936 fields = NULL;
4937 }
4938 /* Channel id is set during the object creation. */
4939 chan_id = chan_reg->chan_id;
4940
4941 /* Append to metadata */
4942 if (!chan_reg->metadata_dumped) {
4943 ret_code = ust_metadata_channel_statedump(registry, chan_reg);
4944 if (ret_code) {
4945 ERR("Error appending channel metadata (errno = %d)", ret_code);
4946 goto reply;
4947 }
4948 }
4949
4950 reply:
4951 DBG3("UST app replying to register channel key %" PRIu64
4952 " with id %u, type: %d, ret: %d", chan_reg_key, chan_id, type,
4953 ret_code);
4954
4955 ret = ustctl_reply_register_channel(sock, chan_id, type, ret_code);
4956 if (ret < 0) {
4957 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
4958 ERR("UST app reply channel failed with ret %d", ret);
4959 } else {
4960 DBG3("UST app reply channel failed. Application died");
4961 }
4962 goto error;
4963 }
4964
4965 /* This channel registry registration is completed. */
4966 chan_reg->register_done = 1;
4967
4968 error:
4969 pthread_mutex_unlock(&registry->lock);
4970 error_rcu_unlock:
4971 rcu_read_unlock();
4972 if (ret) {
4973 free(fields);
4974 }
4975 return ret;
4976 }
4977
4978 /*
4979 * Add event to the UST channel registry. When the event is added to the
4980 * registry, the metadata is also created. Once done, this replies to the
4981 * application with the appropriate error code.
4982 *
4983 * The session UST registry lock is acquired in the function.
4984 *
4985 * On success 0 is returned else a negative value.
4986 */
4987 static int add_event_ust_registry(int sock, int sobjd, int cobjd, char *name,
4988 char *sig, size_t nr_fields, struct ustctl_field *fields, int loglevel,
4989 char *model_emf_uri)
4990 {
4991 int ret, ret_code;
4992 uint32_t event_id = 0;
4993 uint64_t chan_reg_key;
4994 struct ust_app *app;
4995 struct ust_app_channel *ua_chan;
4996 struct ust_app_session *ua_sess;
4997 struct ust_registry_session *registry;
4998
4999 rcu_read_lock();
5000
5001 /* Lookup application. If not found, there is a code flow error. */
5002 app = find_app_by_notify_sock(sock);
5003 if (!app) {
5004 DBG("Application socket %d is being teardown. Abort event notify",
5005 sock);
5006 ret = 0;
5007 free(sig);
5008 free(fields);
5009 free(model_emf_uri);
5010 goto error_rcu_unlock;
5011 }
5012
5013 /* Lookup channel by UST object descriptor. */
5014 ua_chan = find_channel_by_objd(app, cobjd);
5015 if (!ua_chan) {
5016 DBG("Application channel is being teardown. Abort event notify");
5017 ret = 0;
5018 free(sig);
5019 free(fields);
5020 free(model_emf_uri);
5021 goto error_rcu_unlock;
5022 }
5023
5024 assert(ua_chan->session);
5025 ua_sess = ua_chan->session;
5026
5027 registry = get_session_registry(ua_sess);
5028 assert(registry);
5029
5030 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) {
5031 chan_reg_key = ua_chan->tracing_channel_id;
5032 } else {
5033 chan_reg_key = ua_chan->key;
5034 }
5035
5036 pthread_mutex_lock(&registry->lock);
5037
5038 /*
5039 * From this point on, this call acquires the ownership of the sig, fields
5040 * and model_emf_uri meaning any free are done inside it if needed. These
5041 * three variables MUST NOT be read/write after this.
5042 */
5043 ret_code = ust_registry_create_event(registry, chan_reg_key,
5044 sobjd, cobjd, name, sig, nr_fields, fields, loglevel,
5045 model_emf_uri, ua_sess->buffer_type, &event_id,
5046 app);
5047
5048 /*
5049 * The return value is returned to ustctl so in case of an error, the
5050 * application can be notified. In case of an error, it's important not to
5051 * return a negative error or else the application will get closed.
5052 */
5053 ret = ustctl_reply_register_event(sock, event_id, ret_code);
5054 if (ret < 0) {
5055 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
5056 ERR("UST app reply event failed with ret %d", ret);
5057 } else {
5058 DBG3("UST app reply event failed. Application died");
5059 }
5060 /*
5061 * No need to wipe the create event since the application socket will
5062 * get close on error hence cleaning up everything by itself.
5063 */
5064 goto error;
5065 }
5066
5067 DBG3("UST registry event %s with id %" PRId32 " added successfully",
5068 name, event_id);
5069
5070 error:
5071 pthread_mutex_unlock(&registry->lock);
5072 error_rcu_unlock:
5073 rcu_read_unlock();
5074 return ret;
5075 }
5076
5077 /*
5078 * Handle application notification through the given notify socket.
5079 *
5080 * Return 0 on success or else a negative value.
5081 */
5082 int ust_app_recv_notify(int sock)
5083 {
5084 int ret;
5085 enum ustctl_notify_cmd cmd;
5086
5087 DBG3("UST app receiving notify from sock %d", sock);
5088
5089 ret = ustctl_recv_notify(sock, &cmd);
5090 if (ret < 0) {
5091 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
5092 ERR("UST app recv notify failed with ret %d", ret);
5093 } else {
5094 DBG3("UST app recv notify failed. Application died");
5095 }
5096 goto error;
5097 }
5098
5099 switch (cmd) {
5100 case USTCTL_NOTIFY_CMD_EVENT:
5101 {
5102 int sobjd, cobjd, loglevel;
5103 char name[LTTNG_UST_SYM_NAME_LEN], *sig, *model_emf_uri;
5104 size_t nr_fields;
5105 struct ustctl_field *fields;
5106
5107 DBG2("UST app ustctl register event received");
5108
5109 ret = ustctl_recv_register_event(sock, &sobjd, &cobjd, name, &loglevel,
5110 &sig, &nr_fields, &fields, &model_emf_uri);
5111 if (ret < 0) {
5112 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
5113 ERR("UST app recv event failed with ret %d", ret);
5114 } else {
5115 DBG3("UST app recv event failed. Application died");
5116 }
5117 goto error;
5118 }
5119
5120 /*
5121 * Add event to the UST registry coming from the notify socket. This
5122 * call will free if needed the sig, fields and model_emf_uri. This
5123 * code path loses the ownsership of these variables and transfer them
5124 * to the this function.
5125 */
5126 ret = add_event_ust_registry(sock, sobjd, cobjd, name, sig, nr_fields,
5127 fields, loglevel, model_emf_uri);
5128 if (ret < 0) {
5129 goto error;
5130 }
5131
5132 break;
5133 }
5134 case USTCTL_NOTIFY_CMD_CHANNEL:
5135 {
5136 int sobjd, cobjd;
5137 size_t nr_fields;
5138 struct ustctl_field *fields;
5139
5140 DBG2("UST app ustctl register channel received");
5141
5142 ret = ustctl_recv_register_channel(sock, &sobjd, &cobjd, &nr_fields,
5143 &fields);
5144 if (ret < 0) {
5145 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
5146 ERR("UST app recv channel failed with ret %d", ret);
5147 } else {
5148 DBG3("UST app recv channel failed. Application died");
5149 }
5150 goto error;
5151 }
5152
5153 /*
5154 * The fields ownership are transfered to this function call meaning
5155 * that if needed it will be freed. After this, it's invalid to access
5156 * fields or clean it up.
5157 */
5158 ret = reply_ust_register_channel(sock, sobjd, cobjd, nr_fields,
5159 fields);
5160 if (ret < 0) {
5161 goto error;
5162 }
5163
5164 break;
5165 }
5166 default:
5167 /* Should NEVER happen. */
5168 assert(0);
5169 }
5170
5171 error:
5172 return ret;
5173 }
5174
5175 /*
5176 * Once the notify socket hangs up, this is called. First, it tries to find the
5177 * corresponding application. On failure, the call_rcu to close the socket is
5178 * executed. If an application is found, it tries to delete it from the notify
5179 * socket hash table. Whathever the result, it proceeds to the call_rcu.
5180 *
5181 * Note that an object needs to be allocated here so on ENOMEM failure, the
5182 * call RCU is not done but the rest of the cleanup is.
5183 */
5184 void ust_app_notify_sock_unregister(int sock)
5185 {
5186 int err_enomem = 0;
5187 struct lttng_ht_iter iter;
5188 struct ust_app *app;
5189 struct ust_app_notify_sock_obj *obj;
5190
5191 assert(sock >= 0);
5192
5193 rcu_read_lock();
5194
5195 obj = zmalloc(sizeof(*obj));
5196 if (!obj) {
5197 /*
5198 * An ENOMEM is kind of uncool. If this strikes we continue the
5199 * procedure but the call_rcu will not be called. In this case, we
5200 * accept the fd leak rather than possibly creating an unsynchronized
5201 * state between threads.
5202 *
5203 * TODO: The notify object should be created once the notify socket is
5204 * registered and stored independantely from the ust app object. The
5205 * tricky part is to synchronize the teardown of the application and
5206 * this notify object. Let's keep that in mind so we can avoid this
5207 * kind of shenanigans with ENOMEM in the teardown path.
5208 */
5209 err_enomem = 1;
5210 } else {
5211 obj->fd = sock;
5212 }
5213
5214 DBG("UST app notify socket unregister %d", sock);
5215
5216 /*
5217 * Lookup application by notify socket. If this fails, this means that the
5218 * hash table delete has already been done by the application
5219 * unregistration process so we can safely close the notify socket in a
5220 * call RCU.
5221 */
5222 app = find_app_by_notify_sock(sock);
5223 if (!app) {
5224 goto close_socket;
5225 }
5226
5227 iter.iter.node = &app->notify_sock_n.node;
5228
5229 /*
5230 * Whatever happens here either we fail or succeed, in both cases we have
5231 * to close the socket after a grace period to continue to the call RCU
5232 * here. If the deletion is successful, the application is not visible
5233 * anymore by other threads and is it fails it means that it was already
5234 * deleted from the hash table so either way we just have to close the
5235 * socket.
5236 */
5237 (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter);
5238
5239 close_socket:
5240 rcu_read_unlock();
5241
5242 /*
5243 * Close socket after a grace period to avoid for the socket to be reused
5244 * before the application object is freed creating potential race between
5245 * threads trying to add unique in the global hash table.
5246 */
5247 if (!err_enomem) {
5248 call_rcu(&obj->head, close_notify_sock_rcu);
5249 }
5250 }
5251
5252 /*
5253 * Destroy a ust app data structure and free its memory.
5254 */
5255 void ust_app_destroy(struct ust_app *app)
5256 {
5257 if (!app) {
5258 return;
5259 }
5260
5261 call_rcu(&app->pid_n.head, delete_ust_app_rcu);
5262 }
5263
5264 /*
5265 * Take a snapshot for a given UST session. The snapshot is sent to the given
5266 * output.
5267 *
5268 * Return 0 on success or else a negative value.
5269 */
5270 int ust_app_snapshot_record(struct ltt_ust_session *usess,
5271 struct snapshot_output *output, int wait,
5272 uint64_t nb_packets_per_stream)
5273 {
5274 int ret = 0;
5275 unsigned int snapshot_done = 0;
5276 struct lttng_ht_iter iter;
5277 struct ust_app *app;
5278 char pathname[PATH_MAX];
5279
5280 assert(usess);
5281 assert(output);
5282
5283 rcu_read_lock();
5284
5285 switch (usess->buffer_type) {
5286 case LTTNG_BUFFER_PER_UID:
5287 {
5288 struct buffer_reg_uid *reg;
5289
5290 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
5291 struct buffer_reg_channel *reg_chan;
5292 struct consumer_socket *socket;
5293
5294 /* Get consumer socket to use to push the metadata.*/
5295 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
5296 usess->consumer);
5297 if (!socket) {
5298 ret = -EINVAL;
5299 goto error;
5300 }
5301
5302 memset(pathname, 0, sizeof(pathname));
5303 ret = snprintf(pathname, sizeof(pathname),
5304 DEFAULT_UST_TRACE_DIR "/" DEFAULT_UST_TRACE_UID_PATH,
5305 reg->uid, reg->bits_per_long);
5306 if (ret < 0) {
5307 PERROR("snprintf snapshot path");
5308 goto error;
5309 }
5310
5311 /* Add the UST default trace dir to path. */
5312 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
5313 reg_chan, node.node) {
5314 ret = consumer_snapshot_channel(socket, reg_chan->consumer_key,
5315 output, 0, usess->uid, usess->gid, pathname, wait,
5316 nb_packets_per_stream);
5317 if (ret < 0) {
5318 goto error;
5319 }
5320 }
5321 ret = consumer_snapshot_channel(socket,
5322 reg->registry->reg.ust->metadata_key, output, 1,
5323 usess->uid, usess->gid, pathname, wait, 0);
5324 if (ret < 0) {
5325 goto error;
5326 }
5327 snapshot_done = 1;
5328 }
5329 break;
5330 }
5331 case LTTNG_BUFFER_PER_PID:
5332 {
5333 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5334 struct consumer_socket *socket;
5335 struct lttng_ht_iter chan_iter;
5336 struct ust_app_channel *ua_chan;
5337 struct ust_app_session *ua_sess;
5338 struct ust_registry_session *registry;
5339
5340 ua_sess = lookup_session_by_app(usess, app);
5341 if (!ua_sess) {
5342 /* Session not associated with this app. */
5343 continue;
5344 }
5345
5346 /* Get the right consumer socket for the application. */
5347 socket = consumer_find_socket_by_bitness(app->bits_per_long,
5348 output->consumer);
5349 if (!socket) {
5350 ret = -EINVAL;
5351 goto error;
5352 }
5353
5354 /* Add the UST default trace dir to path. */
5355 memset(pathname, 0, sizeof(pathname));
5356 ret = snprintf(pathname, sizeof(pathname), DEFAULT_UST_TRACE_DIR "/%s",
5357 ua_sess->path);
5358 if (ret < 0) {
5359 PERROR("snprintf snapshot path");
5360 goto error;
5361 }
5362
5363 cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter,
5364 ua_chan, node.node) {
5365 ret = consumer_snapshot_channel(socket, ua_chan->key, output,
5366 0, ua_sess->euid, ua_sess->egid, pathname, wait,
5367 nb_packets_per_stream);
5368 if (ret < 0) {
5369 goto error;
5370 }
5371 }
5372
5373 registry = get_session_registry(ua_sess);
5374 assert(registry);
5375 ret = consumer_snapshot_channel(socket, registry->metadata_key, output,
5376 1, ua_sess->euid, ua_sess->egid, pathname, wait, 0);
5377 if (ret < 0) {
5378 goto error;
5379 }
5380 snapshot_done = 1;
5381 }
5382 break;
5383 }
5384 default:
5385 assert(0);
5386 break;
5387 }
5388
5389 if (!snapshot_done) {
5390 /*
5391 * If no snapshot was made and we are not in the error path, this means
5392 * that there are no buffers thus no (prior) application to snapshot
5393 * data from so we have simply NO data.
5394 */
5395 ret = -ENODATA;
5396 }
5397
5398 error:
5399 rcu_read_unlock();
5400 return ret;
5401 }
5402
5403 /*
5404 * Return the size taken by one more packet per stream.
5405 */
5406 uint64_t ust_app_get_size_one_more_packet_per_stream(struct ltt_ust_session *usess,
5407 uint64_t cur_nr_packets)
5408 {
5409 uint64_t tot_size = 0;
5410 struct ust_app *app;
5411 struct lttng_ht_iter iter;
5412
5413 assert(usess);
5414
5415 switch (usess->buffer_type) {
5416 case LTTNG_BUFFER_PER_UID:
5417 {
5418 struct buffer_reg_uid *reg;
5419
5420 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
5421 struct buffer_reg_channel *reg_chan;
5422
5423 rcu_read_lock();
5424 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
5425 reg_chan, node.node) {
5426 if (cur_nr_packets >= reg_chan->num_subbuf) {
5427 /*
5428 * Don't take channel into account if we
5429 * already grab all its packets.
5430 */
5431 continue;
5432 }
5433 tot_size += reg_chan->subbuf_size * reg_chan->stream_count;
5434 }
5435 rcu_read_unlock();
5436 }
5437 break;
5438 }
5439 case LTTNG_BUFFER_PER_PID:
5440 {
5441 rcu_read_lock();
5442 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5443 struct ust_app_channel *ua_chan;
5444 struct ust_app_session *ua_sess;
5445 struct lttng_ht_iter chan_iter;
5446
5447 ua_sess = lookup_session_by_app(usess, app);
5448 if (!ua_sess) {
5449 /* Session not associated with this app. */
5450 continue;
5451 }
5452
5453 cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter,
5454 ua_chan, node.node) {
5455 if (cur_nr_packets >= ua_chan->attr.num_subbuf) {
5456 /*
5457 * Don't take channel into account if we
5458 * already grab all its packets.
5459 */
5460 continue;
5461 }
5462 tot_size += ua_chan->attr.subbuf_size * ua_chan->streams.count;
5463 }
5464 }
5465 rcu_read_unlock();
5466 break;
5467 }
5468 default:
5469 assert(0);
5470 break;
5471 }
5472
5473 return tot_size;
5474 }
This page took 0.177825 seconds and 3 git commands to generate.