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