Fix: ust-app: per-PID app unregister vs tracing stop races
[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 * The socket lock MUST be acquired.
430 * The ust app session lock MUST be acquired.
431 *
432 * On success, return the len of metadata pushed or else a negative value.
433 */
434 ssize_t ust_app_push_metadata(struct ust_registry_session *registry,
435 struct consumer_socket *socket, int send_zero_data)
436 {
437 int ret;
438 char *metadata_str = NULL;
439 size_t len, offset;
440 ssize_t ret_val;
441
442 assert(registry);
443 assert(socket);
444
445 /*
446 * On a push metadata error either the consumer is dead or the metadata
447 * channel has been destroyed because its endpoint might have died (e.g:
448 * relayd). If so, the metadata closed flag is set to 1 so we deny pushing
449 * metadata again which is not valid anymore on the consumer side.
450 *
451 * The ust app session mutex locked allows us to make this check without
452 * the registry lock.
453 */
454 if (registry->metadata_closed) {
455 return -EPIPE;
456 }
457
458 pthread_mutex_lock(&registry->lock);
459
460 offset = registry->metadata_len_sent;
461 len = registry->metadata_len - registry->metadata_len_sent;
462 if (len == 0) {
463 DBG3("No metadata to push for metadata key %" PRIu64,
464 registry->metadata_key);
465 ret_val = len;
466 if (send_zero_data) {
467 DBG("No metadata to push");
468 goto push_data;
469 }
470 goto end;
471 }
472
473 /* Allocate only what we have to send. */
474 metadata_str = zmalloc(len);
475 if (!metadata_str) {
476 PERROR("zmalloc ust app metadata string");
477 ret_val = -ENOMEM;
478 goto error;
479 }
480 /* Copy what we haven't send out. */
481 memcpy(metadata_str, registry->metadata + offset, len);
482 registry->metadata_len_sent += len;
483
484 push_data:
485 pthread_mutex_unlock(&registry->lock);
486 ret = consumer_push_metadata(socket, registry->metadata_key,
487 metadata_str, len, offset);
488 if (ret < 0) {
489 /*
490 * There is an acceptable race here between the registry metadata key
491 * assignment and the creation on the consumer. The session daemon can
492 * concurrently push metadata for this registry while being created on
493 * the consumer since the metadata key of the registry is assigned
494 * *before* it is setup to avoid the consumer to ask for metadata that
495 * could possibly be not found in the session daemon.
496 *
497 * The metadata will get pushed either by the session being stopped or
498 * the consumer requesting metadata if that race is triggered.
499 */
500 if (ret == -LTTCOMM_CONSUMERD_CHANNEL_FAIL) {
501 ret = 0;
502 }
503
504 /* Update back the actual metadata len sent since it failed here. */
505 pthread_mutex_lock(&registry->lock);
506 registry->metadata_len_sent -= len;
507 pthread_mutex_unlock(&registry->lock);
508 ret_val = ret;
509 goto error_push;
510 }
511
512 free(metadata_str);
513 return len;
514
515 end:
516 error:
517 pthread_mutex_unlock(&registry->lock);
518 error_push:
519 free(metadata_str);
520 return ret_val;
521 }
522
523 /*
524 * For a given application and session, push metadata to consumer. The session
525 * lock MUST be acquired here before calling this.
526 * Either sock or consumer is required : if sock is NULL, the default
527 * socket to send the metadata is retrieved from consumer, if sock
528 * is not NULL we use it to send the metadata.
529 *
530 * Return 0 on success else a negative error.
531 */
532 static int push_metadata(struct ust_registry_session *registry,
533 struct consumer_output *consumer)
534 {
535 int ret_val;
536 ssize_t ret;
537 struct consumer_socket *socket;
538
539 assert(registry);
540 assert(consumer);
541
542 rcu_read_lock();
543
544 /*
545 * Means that no metadata was assigned to the session. This can happens if
546 * no start has been done previously.
547 */
548 if (!registry->metadata_key) {
549 ret_val = 0;
550 goto end_rcu_unlock;
551 }
552
553 /* Get consumer socket to use to push the metadata.*/
554 socket = consumer_find_socket_by_bitness(registry->bits_per_long,
555 consumer);
556 if (!socket) {
557 ret_val = -1;
558 goto error_rcu_unlock;
559 }
560
561 /*
562 * TODO: Currently, we hold the socket lock around sampling of the next
563 * metadata segment to ensure we send metadata over the consumer socket in
564 * the correct order. This makes the registry lock nest inside the socket
565 * lock.
566 *
567 * Please note that this is a temporary measure: we should move this lock
568 * back into ust_consumer_push_metadata() when the consumer gets the
569 * ability to reorder the metadata it receives.
570 */
571 pthread_mutex_lock(socket->lock);
572 ret = ust_app_push_metadata(registry, socket, 0);
573 pthread_mutex_unlock(socket->lock);
574 if (ret < 0) {
575 ret_val = ret;
576 goto error_rcu_unlock;
577 }
578
579 rcu_read_unlock();
580 return 0;
581
582 error_rcu_unlock:
583 /*
584 * On error, flag the registry that the metadata is closed. We were unable
585 * to push anything and this means that either the consumer is not
586 * responding or the metadata cache has been destroyed on the consumer.
587 */
588 registry->metadata_closed = 1;
589 end_rcu_unlock:
590 rcu_read_unlock();
591 return ret_val;
592 }
593
594 /*
595 * Send to the consumer a close metadata command for the given session. Once
596 * done, the metadata channel is deleted and the session metadata pointer is
597 * nullified. The session lock MUST be acquired here unless the application is
598 * in the destroy path.
599 *
600 * Return 0 on success else a negative value.
601 */
602 static int close_metadata(struct ust_registry_session *registry,
603 struct consumer_output *consumer)
604 {
605 int ret;
606 struct consumer_socket *socket;
607
608 assert(registry);
609 assert(consumer);
610
611 rcu_read_lock();
612
613 if (!registry->metadata_key || registry->metadata_closed) {
614 ret = 0;
615 goto end;
616 }
617
618 /* Get consumer socket to use to push the metadata.*/
619 socket = consumer_find_socket_by_bitness(registry->bits_per_long,
620 consumer);
621 if (!socket) {
622 ret = -1;
623 goto error;
624 }
625
626 ret = consumer_close_metadata(socket, registry->metadata_key);
627 if (ret < 0) {
628 goto error;
629 }
630
631 error:
632 /*
633 * Metadata closed. Even on error this means that the consumer is not
634 * responding or not found so either way a second close should NOT be emit
635 * for this registry.
636 */
637 registry->metadata_closed = 1;
638 end:
639 rcu_read_unlock();
640 return ret;
641 }
642
643 /*
644 * We need to execute ht_destroy outside of RCU read-side critical
645 * section and outside of call_rcu thread, so we postpone its execution
646 * using ht_cleanup_push. It is simpler than to change the semantic of
647 * the many callers of delete_ust_app_session().
648 */
649 static
650 void delete_ust_app_session_rcu(struct rcu_head *head)
651 {
652 struct ust_app_session *ua_sess =
653 caa_container_of(head, struct ust_app_session, rcu_head);
654
655 ht_cleanup_push(ua_sess->channels);
656 free(ua_sess);
657 }
658
659 /*
660 * Delete ust app session safely. RCU read lock must be held before calling
661 * this function.
662 */
663 static
664 void delete_ust_app_session(int sock, struct ust_app_session *ua_sess,
665 struct ust_app *app)
666 {
667 int ret;
668 struct lttng_ht_iter iter;
669 struct ust_app_channel *ua_chan;
670 struct ust_registry_session *registry;
671
672 assert(ua_sess);
673
674 pthread_mutex_lock(&ua_sess->lock);
675
676 registry = get_session_registry(ua_sess);
677 if (registry && !registry->metadata_closed) {
678 /* Push metadata for application before freeing the application. */
679 (void) push_metadata(registry, ua_sess->consumer);
680
681 /*
682 * Don't ask to close metadata for global per UID buffers. Close
683 * metadata only on destroy trace session in this case. Also, the
684 * previous push metadata could have flag the metadata registry to
685 * close so don't send a close command if closed.
686 */
687 if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID &&
688 !registry->metadata_closed) {
689 /* And ask to close it for this session registry. */
690 (void) close_metadata(registry, ua_sess->consumer);
691 }
692 }
693
694 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
695 node.node) {
696 ret = lttng_ht_del(ua_sess->channels, &iter);
697 assert(!ret);
698 delete_ust_app_channel(sock, ua_chan, app);
699 }
700
701 /* In case of per PID, the registry is kept in the session. */
702 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) {
703 struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id);
704 if (reg_pid) {
705 buffer_reg_pid_remove(reg_pid);
706 buffer_reg_pid_destroy(reg_pid);
707 }
708 }
709
710 if (ua_sess->handle != -1) {
711 ret = ustctl_release_handle(sock, ua_sess->handle);
712 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
713 ERR("UST app sock %d release session handle failed with ret %d",
714 sock, ret);
715 }
716 }
717 pthread_mutex_unlock(&ua_sess->lock);
718
719 call_rcu(&ua_sess->rcu_head, delete_ust_app_session_rcu);
720 }
721
722 /*
723 * Delete a traceable application structure from the global list. Never call
724 * this function outside of a call_rcu call.
725 *
726 * RCU read side lock should _NOT_ be held when calling this function.
727 */
728 static
729 void delete_ust_app(struct ust_app *app)
730 {
731 int ret, sock;
732 struct ust_app_session *ua_sess, *tmp_ua_sess;
733
734 /* Delete ust app sessions info */
735 sock = app->sock;
736 app->sock = -1;
737
738 /* Wipe sessions */
739 cds_list_for_each_entry_safe(ua_sess, tmp_ua_sess, &app->teardown_head,
740 teardown_node) {
741 /* Free every object in the session and the session. */
742 rcu_read_lock();
743 delete_ust_app_session(sock, ua_sess, app);
744 rcu_read_unlock();
745 }
746
747 ht_cleanup_push(app->sessions);
748 ht_cleanup_push(app->ust_objd);
749
750 /*
751 * Wait until we have deleted the application from the sock hash table
752 * before closing this socket, otherwise an application could re-use the
753 * socket ID and race with the teardown, using the same hash table entry.
754 *
755 * It's OK to leave the close in call_rcu. We want it to stay unique for
756 * all RCU readers that could run concurrently with unregister app,
757 * therefore we _need_ to only close that socket after a grace period. So
758 * it should stay in this RCU callback.
759 *
760 * This close() is a very important step of the synchronization model so
761 * every modification to this function must be carefully reviewed.
762 */
763 ret = close(sock);
764 if (ret) {
765 PERROR("close");
766 }
767 lttng_fd_put(LTTNG_FD_APPS, 1);
768
769 DBG2("UST app pid %d deleted", app->pid);
770 free(app);
771 }
772
773 /*
774 * URCU intermediate call to delete an UST app.
775 */
776 static
777 void delete_ust_app_rcu(struct rcu_head *head)
778 {
779 struct lttng_ht_node_ulong *node =
780 caa_container_of(head, struct lttng_ht_node_ulong, head);
781 struct ust_app *app =
782 caa_container_of(node, struct ust_app, pid_n);
783
784 DBG3("Call RCU deleting app PID %d", app->pid);
785 delete_ust_app(app);
786 }
787
788 /*
789 * Delete the session from the application ht and delete the data structure by
790 * freeing every object inside and releasing them.
791 */
792 static void destroy_app_session(struct ust_app *app,
793 struct ust_app_session *ua_sess)
794 {
795 int ret;
796 struct lttng_ht_iter iter;
797
798 assert(app);
799 assert(ua_sess);
800
801 iter.iter.node = &ua_sess->node.node;
802 ret = lttng_ht_del(app->sessions, &iter);
803 if (ret) {
804 /* Already scheduled for teardown. */
805 goto end;
806 }
807
808 /* Once deleted, free the data structure. */
809 delete_ust_app_session(app->sock, ua_sess, app);
810
811 end:
812 return;
813 }
814
815 /*
816 * Alloc new UST app session.
817 */
818 static
819 struct ust_app_session *alloc_ust_app_session(struct ust_app *app)
820 {
821 struct ust_app_session *ua_sess;
822
823 /* Init most of the default value by allocating and zeroing */
824 ua_sess = zmalloc(sizeof(struct ust_app_session));
825 if (ua_sess == NULL) {
826 PERROR("malloc");
827 goto error_free;
828 }
829
830 ua_sess->handle = -1;
831 ua_sess->channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
832 ua_sess->metadata_attr.type = LTTNG_UST_CHAN_METADATA;
833 pthread_mutex_init(&ua_sess->lock, NULL);
834
835 return ua_sess;
836
837 error_free:
838 return NULL;
839 }
840
841 /*
842 * Alloc new UST app channel.
843 */
844 static
845 struct ust_app_channel *alloc_ust_app_channel(char *name,
846 struct ust_app_session *ua_sess,
847 struct lttng_ust_channel_attr *attr)
848 {
849 struct ust_app_channel *ua_chan;
850
851 /* Init most of the default value by allocating and zeroing */
852 ua_chan = zmalloc(sizeof(struct ust_app_channel));
853 if (ua_chan == NULL) {
854 PERROR("malloc");
855 goto error;
856 }
857
858 /* Setup channel name */
859 strncpy(ua_chan->name, name, sizeof(ua_chan->name));
860 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
861
862 ua_chan->enabled = 1;
863 ua_chan->handle = -1;
864 ua_chan->session = ua_sess;
865 ua_chan->key = get_next_channel_key();
866 ua_chan->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
867 ua_chan->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
868 lttng_ht_node_init_str(&ua_chan->node, ua_chan->name);
869
870 CDS_INIT_LIST_HEAD(&ua_chan->streams.head);
871 CDS_INIT_LIST_HEAD(&ua_chan->ctx_list);
872
873 /* Copy attributes */
874 if (attr) {
875 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
876 ua_chan->attr.subbuf_size = attr->subbuf_size;
877 ua_chan->attr.num_subbuf = attr->num_subbuf;
878 ua_chan->attr.overwrite = attr->overwrite;
879 ua_chan->attr.switch_timer_interval = attr->switch_timer_interval;
880 ua_chan->attr.read_timer_interval = attr->read_timer_interval;
881 ua_chan->attr.output = attr->output;
882 }
883 /* By default, the channel is a per cpu channel. */
884 ua_chan->attr.type = LTTNG_UST_CHAN_PER_CPU;
885
886 DBG3("UST app channel %s allocated", ua_chan->name);
887
888 return ua_chan;
889
890 error:
891 return NULL;
892 }
893
894 /*
895 * Allocate and initialize a UST app stream.
896 *
897 * Return newly allocated stream pointer or NULL on error.
898 */
899 struct ust_app_stream *ust_app_alloc_stream(void)
900 {
901 struct ust_app_stream *stream = NULL;
902
903 stream = zmalloc(sizeof(*stream));
904 if (stream == NULL) {
905 PERROR("zmalloc ust app stream");
906 goto error;
907 }
908
909 /* Zero could be a valid value for a handle so flag it to -1. */
910 stream->handle = -1;
911
912 error:
913 return stream;
914 }
915
916 /*
917 * Alloc new UST app event.
918 */
919 static
920 struct ust_app_event *alloc_ust_app_event(char *name,
921 struct lttng_ust_event *attr)
922 {
923 struct ust_app_event *ua_event;
924
925 /* Init most of the default value by allocating and zeroing */
926 ua_event = zmalloc(sizeof(struct ust_app_event));
927 if (ua_event == NULL) {
928 PERROR("malloc");
929 goto error;
930 }
931
932 ua_event->enabled = 1;
933 strncpy(ua_event->name, name, sizeof(ua_event->name));
934 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
935 lttng_ht_node_init_str(&ua_event->node, ua_event->name);
936
937 /* Copy attributes */
938 if (attr) {
939 memcpy(&ua_event->attr, attr, sizeof(ua_event->attr));
940 }
941
942 DBG3("UST app event %s allocated", ua_event->name);
943
944 return ua_event;
945
946 error:
947 return NULL;
948 }
949
950 /*
951 * Alloc new UST app context.
952 */
953 static
954 struct ust_app_ctx *alloc_ust_app_ctx(struct lttng_ust_context *uctx)
955 {
956 struct ust_app_ctx *ua_ctx;
957
958 ua_ctx = zmalloc(sizeof(struct ust_app_ctx));
959 if (ua_ctx == NULL) {
960 goto error;
961 }
962
963 CDS_INIT_LIST_HEAD(&ua_ctx->list);
964
965 if (uctx) {
966 memcpy(&ua_ctx->ctx, uctx, sizeof(ua_ctx->ctx));
967 }
968
969 DBG3("UST app context %d allocated", ua_ctx->ctx.ctx);
970
971 error:
972 return ua_ctx;
973 }
974
975 /*
976 * Allocate a filter and copy the given original filter.
977 *
978 * Return allocated filter or NULL on error.
979 */
980 static struct lttng_ust_filter_bytecode *alloc_copy_ust_app_filter(
981 struct lttng_ust_filter_bytecode *orig_f)
982 {
983 struct lttng_ust_filter_bytecode *filter = NULL;
984
985 /* Copy filter bytecode */
986 filter = zmalloc(sizeof(*filter) + orig_f->len);
987 if (!filter) {
988 PERROR("zmalloc alloc ust app filter");
989 goto error;
990 }
991
992 memcpy(filter, orig_f, sizeof(*filter) + orig_f->len);
993
994 error:
995 return filter;
996 }
997
998 /*
999 * Find an ust_app using the sock and return it. RCU read side lock must be
1000 * held before calling this helper function.
1001 */
1002 struct ust_app *ust_app_find_by_sock(int sock)
1003 {
1004 struct lttng_ht_node_ulong *node;
1005 struct lttng_ht_iter iter;
1006
1007 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter);
1008 node = lttng_ht_iter_get_node_ulong(&iter);
1009 if (node == NULL) {
1010 DBG2("UST app find by sock %d not found", sock);
1011 goto error;
1012 }
1013
1014 return caa_container_of(node, struct ust_app, sock_n);
1015
1016 error:
1017 return NULL;
1018 }
1019
1020 /*
1021 * Find an ust_app using the notify sock and return it. RCU read side lock must
1022 * be held before calling this helper function.
1023 */
1024 static struct ust_app *find_app_by_notify_sock(int sock)
1025 {
1026 struct lttng_ht_node_ulong *node;
1027 struct lttng_ht_iter iter;
1028
1029 lttng_ht_lookup(ust_app_ht_by_notify_sock, (void *)((unsigned long) sock),
1030 &iter);
1031 node = lttng_ht_iter_get_node_ulong(&iter);
1032 if (node == NULL) {
1033 DBG2("UST app find by notify sock %d not found", sock);
1034 goto error;
1035 }
1036
1037 return caa_container_of(node, struct ust_app, notify_sock_n);
1038
1039 error:
1040 return NULL;
1041 }
1042
1043 /*
1044 * Lookup for an ust app event based on event name, filter bytecode and the
1045 * event loglevel.
1046 *
1047 * Return an ust_app_event object or NULL on error.
1048 */
1049 static struct ust_app_event *find_ust_app_event(struct lttng_ht *ht,
1050 char *name, struct lttng_ust_filter_bytecode *filter, int loglevel,
1051 const struct lttng_event_exclusion *exclusion)
1052 {
1053 struct lttng_ht_iter iter;
1054 struct lttng_ht_node_str *node;
1055 struct ust_app_event *event = NULL;
1056 struct ust_app_ht_key key;
1057
1058 assert(name);
1059 assert(ht);
1060
1061 /* Setup key for event lookup. */
1062 key.name = name;
1063 key.filter = filter;
1064 key.loglevel = loglevel;
1065 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1066 key.exclusion = (struct lttng_ust_event_exclusion *)exclusion;
1067
1068 /* Lookup using the event name as hash and a custom match fct. */
1069 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
1070 ht_match_ust_app_event, &key, &iter.iter);
1071 node = lttng_ht_iter_get_node_str(&iter);
1072 if (node == NULL) {
1073 goto end;
1074 }
1075
1076 event = caa_container_of(node, struct ust_app_event, node);
1077
1078 end:
1079 return event;
1080 }
1081
1082 /*
1083 * Create the channel context on the tracer.
1084 *
1085 * Called with UST app session lock held.
1086 */
1087 static
1088 int create_ust_channel_context(struct ust_app_channel *ua_chan,
1089 struct ust_app_ctx *ua_ctx, struct ust_app *app)
1090 {
1091 int ret;
1092
1093 health_code_update();
1094
1095 ret = ustctl_add_context(app->sock, &ua_ctx->ctx,
1096 ua_chan->obj, &ua_ctx->obj);
1097 if (ret < 0) {
1098 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1099 ERR("UST app create channel context failed for app (pid: %d) "
1100 "with ret %d", app->pid, ret);
1101 } else {
1102 /*
1103 * This is normal behavior, an application can die during the
1104 * creation process. Don't report an error so the execution can
1105 * continue normally.
1106 */
1107 ret = 0;
1108 DBG3("UST app disable event failed. Application is dead.");
1109 }
1110 goto error;
1111 }
1112
1113 ua_ctx->handle = ua_ctx->obj->handle;
1114
1115 DBG2("UST app context handle %d created successfully for channel %s",
1116 ua_ctx->handle, ua_chan->name);
1117
1118 error:
1119 health_code_update();
1120 return ret;
1121 }
1122
1123 /*
1124 * Set the filter on the tracer.
1125 */
1126 static
1127 int set_ust_event_filter(struct ust_app_event *ua_event,
1128 struct ust_app *app)
1129 {
1130 int ret;
1131
1132 health_code_update();
1133
1134 if (!ua_event->filter) {
1135 ret = 0;
1136 goto error;
1137 }
1138
1139 ret = ustctl_set_filter(app->sock, ua_event->filter,
1140 ua_event->obj);
1141 if (ret < 0) {
1142 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1143 ERR("UST app event %s filter failed for app (pid: %d) "
1144 "with ret %d", ua_event->attr.name, app->pid, ret);
1145 } else {
1146 /*
1147 * This is normal behavior, an application can die during the
1148 * creation process. Don't report an error so the execution can
1149 * continue normally.
1150 */
1151 ret = 0;
1152 DBG3("UST app filter event failed. Application is dead.");
1153 }
1154 goto error;
1155 }
1156
1157 DBG2("UST filter set successfully for event %s", ua_event->name);
1158
1159 error:
1160 health_code_update();
1161 return ret;
1162 }
1163
1164 /*
1165 * Set event exclusions on the tracer.
1166 */
1167 static
1168 int set_ust_event_exclusion(struct ust_app_event *ua_event,
1169 struct ust_app *app)
1170 {
1171 int ret;
1172
1173 health_code_update();
1174
1175 if (!ua_event->exclusion || !ua_event->exclusion->count) {
1176 ret = 0;
1177 goto error;
1178 }
1179
1180 ret = ustctl_set_exclusion(app->sock, ua_event->exclusion,
1181 ua_event->obj);
1182 if (ret < 0) {
1183 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1184 ERR("UST app event %s exclusions failed for app (pid: %d) "
1185 "with ret %d", ua_event->attr.name, app->pid, ret);
1186 } else {
1187 /*
1188 * This is normal behavior, an application can die during the
1189 * creation process. Don't report an error so the execution can
1190 * continue normally.
1191 */
1192 ret = 0;
1193 DBG3("UST app event exclusion failed. Application is dead.");
1194 }
1195 goto error;
1196 }
1197
1198 DBG2("UST exclusion set successfully for event %s", ua_event->name);
1199
1200 error:
1201 health_code_update();
1202 return ret;
1203 }
1204
1205 /*
1206 * Disable the specified event on to UST tracer for the UST session.
1207 */
1208 static int disable_ust_event(struct ust_app *app,
1209 struct ust_app_session *ua_sess, struct ust_app_event *ua_event)
1210 {
1211 int ret;
1212
1213 health_code_update();
1214
1215 ret = ustctl_disable(app->sock, ua_event->obj);
1216 if (ret < 0) {
1217 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1218 ERR("UST app event %s disable failed for app (pid: %d) "
1219 "and session handle %d with ret %d",
1220 ua_event->attr.name, app->pid, ua_sess->handle, ret);
1221 } else {
1222 /*
1223 * This is normal behavior, an application can die during the
1224 * creation process. Don't report an error so the execution can
1225 * continue normally.
1226 */
1227 ret = 0;
1228 DBG3("UST app disable event failed. Application is dead.");
1229 }
1230 goto error;
1231 }
1232
1233 DBG2("UST app event %s disabled successfully for app (pid: %d)",
1234 ua_event->attr.name, app->pid);
1235
1236 error:
1237 health_code_update();
1238 return ret;
1239 }
1240
1241 /*
1242 * Disable the specified channel on to UST tracer for the UST session.
1243 */
1244 static int disable_ust_channel(struct ust_app *app,
1245 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
1246 {
1247 int ret;
1248
1249 health_code_update();
1250
1251 ret = ustctl_disable(app->sock, ua_chan->obj);
1252 if (ret < 0) {
1253 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1254 ERR("UST app channel %s disable failed for app (pid: %d) "
1255 "and session handle %d with ret %d",
1256 ua_chan->name, app->pid, ua_sess->handle, ret);
1257 } else {
1258 /*
1259 * This is normal behavior, an application can die during the
1260 * creation process. Don't report an error so the execution can
1261 * continue normally.
1262 */
1263 ret = 0;
1264 DBG3("UST app disable channel failed. Application is dead.");
1265 }
1266 goto error;
1267 }
1268
1269 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
1270 ua_chan->name, app->pid);
1271
1272 error:
1273 health_code_update();
1274 return ret;
1275 }
1276
1277 /*
1278 * Enable the specified channel on to UST tracer for the UST session.
1279 */
1280 static int enable_ust_channel(struct ust_app *app,
1281 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
1282 {
1283 int ret;
1284
1285 health_code_update();
1286
1287 ret = ustctl_enable(app->sock, ua_chan->obj);
1288 if (ret < 0) {
1289 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1290 ERR("UST app channel %s enable failed for app (pid: %d) "
1291 "and session handle %d with ret %d",
1292 ua_chan->name, app->pid, ua_sess->handle, ret);
1293 } else {
1294 /*
1295 * This is normal behavior, an application can die during the
1296 * creation process. Don't report an error so the execution can
1297 * continue normally.
1298 */
1299 ret = 0;
1300 DBG3("UST app enable channel failed. Application is dead.");
1301 }
1302 goto error;
1303 }
1304
1305 ua_chan->enabled = 1;
1306
1307 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
1308 ua_chan->name, app->pid);
1309
1310 error:
1311 health_code_update();
1312 return ret;
1313 }
1314
1315 /*
1316 * Enable the specified event on to UST tracer for the UST session.
1317 */
1318 static int enable_ust_event(struct ust_app *app,
1319 struct ust_app_session *ua_sess, struct ust_app_event *ua_event)
1320 {
1321 int ret;
1322
1323 health_code_update();
1324
1325 ret = ustctl_enable(app->sock, ua_event->obj);
1326 if (ret < 0) {
1327 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1328 ERR("UST app event %s enable failed for app (pid: %d) "
1329 "and session handle %d with ret %d",
1330 ua_event->attr.name, app->pid, ua_sess->handle, ret);
1331 } else {
1332 /*
1333 * This is normal behavior, an application can die during the
1334 * creation process. Don't report an error so the execution can
1335 * continue normally.
1336 */
1337 ret = 0;
1338 DBG3("UST app enable event failed. Application is dead.");
1339 }
1340 goto error;
1341 }
1342
1343 DBG2("UST app event %s enabled successfully for app (pid: %d)",
1344 ua_event->attr.name, app->pid);
1345
1346 error:
1347 health_code_update();
1348 return ret;
1349 }
1350
1351 /*
1352 * Send channel and stream buffer to application.
1353 *
1354 * Return 0 on success. On error, a negative value is returned.
1355 */
1356 static int send_channel_pid_to_ust(struct ust_app *app,
1357 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
1358 {
1359 int ret;
1360 struct ust_app_stream *stream, *stmp;
1361
1362 assert(app);
1363 assert(ua_sess);
1364 assert(ua_chan);
1365
1366 health_code_update();
1367
1368 DBG("UST app sending channel %s to UST app sock %d", ua_chan->name,
1369 app->sock);
1370
1371 /* Send channel to the application. */
1372 ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan);
1373 if (ret < 0) {
1374 goto error;
1375 }
1376
1377 health_code_update();
1378
1379 /* Send all streams to application. */
1380 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
1381 ret = ust_consumer_send_stream_to_ust(app, ua_chan, stream);
1382 if (ret < 0) {
1383 goto error;
1384 }
1385 /* We don't need the stream anymore once sent to the tracer. */
1386 cds_list_del(&stream->list);
1387 delete_ust_app_stream(-1, stream);
1388 }
1389 /* Flag the channel that it is sent to the application. */
1390 ua_chan->is_sent = 1;
1391
1392 error:
1393 health_code_update();
1394 return ret;
1395 }
1396
1397 /*
1398 * Create the specified event onto the UST tracer for a UST session.
1399 *
1400 * Should be called with session mutex held.
1401 */
1402 static
1403 int create_ust_event(struct ust_app *app, struct ust_app_session *ua_sess,
1404 struct ust_app_channel *ua_chan, struct ust_app_event *ua_event)
1405 {
1406 int ret = 0;
1407
1408 health_code_update();
1409
1410 /* Create UST event on tracer */
1411 ret = ustctl_create_event(app->sock, &ua_event->attr, ua_chan->obj,
1412 &ua_event->obj);
1413 if (ret < 0) {
1414 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1415 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1416 ua_event->attr.name, app->pid, ret);
1417 } else {
1418 /*
1419 * This is normal behavior, an application can die during the
1420 * creation process. Don't report an error so the execution can
1421 * continue normally.
1422 */
1423 ret = 0;
1424 DBG3("UST app create event failed. Application is dead.");
1425 }
1426 goto error;
1427 }
1428
1429 ua_event->handle = ua_event->obj->handle;
1430
1431 DBG2("UST app event %s created successfully for pid:%d",
1432 ua_event->attr.name, app->pid);
1433
1434 health_code_update();
1435
1436 /* Set filter if one is present. */
1437 if (ua_event->filter) {
1438 ret = set_ust_event_filter(ua_event, app);
1439 if (ret < 0) {
1440 goto error;
1441 }
1442 }
1443
1444 /* Set exclusions for the event */
1445 if (ua_event->exclusion) {
1446 ret = set_ust_event_exclusion(ua_event, app);
1447 if (ret < 0) {
1448 goto error;
1449 }
1450 }
1451
1452 /* If event not enabled, disable it on the tracer */
1453 if (ua_event->enabled) {
1454 /*
1455 * We now need to explicitly enable the event, since it
1456 * is now disabled at creation.
1457 */
1458 ret = enable_ust_event(app, ua_sess, ua_event);
1459 if (ret < 0) {
1460 /*
1461 * If we hit an EPERM, something is wrong with our enable call. If
1462 * we get an EEXIST, there is a problem on the tracer side since we
1463 * just created it.
1464 */
1465 switch (ret) {
1466 case -LTTNG_UST_ERR_PERM:
1467 /* Code flow problem */
1468 assert(0);
1469 case -LTTNG_UST_ERR_EXIST:
1470 /* It's OK for our use case. */
1471 ret = 0;
1472 break;
1473 default:
1474 break;
1475 }
1476 goto error;
1477 }
1478 }
1479
1480 error:
1481 health_code_update();
1482 return ret;
1483 }
1484
1485 /*
1486 * Copy data between an UST app event and a LTT event.
1487 */
1488 static void shadow_copy_event(struct ust_app_event *ua_event,
1489 struct ltt_ust_event *uevent)
1490 {
1491 size_t exclusion_alloc_size;
1492
1493 strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name));
1494 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
1495
1496 ua_event->enabled = uevent->enabled;
1497
1498 /* Copy event attributes */
1499 memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr));
1500
1501 /* Copy filter bytecode */
1502 if (uevent->filter) {
1503 ua_event->filter = alloc_copy_ust_app_filter(uevent->filter);
1504 /* Filter might be NULL here in case of ENONEM. */
1505 }
1506
1507 /* Copy exclusion data */
1508 if (uevent->exclusion) {
1509 exclusion_alloc_size = sizeof(struct lttng_ust_event_exclusion) +
1510 LTTNG_UST_SYM_NAME_LEN * uevent->exclusion->count;
1511 ua_event->exclusion = zmalloc(exclusion_alloc_size);
1512 if (ua_event->exclusion == NULL) {
1513 PERROR("malloc");
1514 } else {
1515 memcpy(ua_event->exclusion, uevent->exclusion,
1516 exclusion_alloc_size);
1517 }
1518 }
1519 }
1520
1521 /*
1522 * Copy data between an UST app channel and a LTT channel.
1523 */
1524 static void shadow_copy_channel(struct ust_app_channel *ua_chan,
1525 struct ltt_ust_channel *uchan)
1526 {
1527 struct lttng_ht_iter iter;
1528 struct ltt_ust_event *uevent;
1529 struct ltt_ust_context *uctx;
1530 struct ust_app_event *ua_event;
1531 struct ust_app_ctx *ua_ctx;
1532
1533 DBG2("UST app shadow copy of channel %s started", ua_chan->name);
1534
1535 strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name));
1536 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
1537
1538 ua_chan->tracefile_size = uchan->tracefile_size;
1539 ua_chan->tracefile_count = uchan->tracefile_count;
1540
1541 /* Copy event attributes since the layout is different. */
1542 ua_chan->attr.subbuf_size = uchan->attr.subbuf_size;
1543 ua_chan->attr.num_subbuf = uchan->attr.num_subbuf;
1544 ua_chan->attr.overwrite = uchan->attr.overwrite;
1545 ua_chan->attr.switch_timer_interval = uchan->attr.switch_timer_interval;
1546 ua_chan->attr.read_timer_interval = uchan->attr.read_timer_interval;
1547 ua_chan->attr.output = uchan->attr.output;
1548 /*
1549 * Note that the attribute channel type is not set since the channel on the
1550 * tracing registry side does not have this information.
1551 */
1552
1553 ua_chan->enabled = uchan->enabled;
1554 ua_chan->tracing_channel_id = uchan->id;
1555
1556 cds_list_for_each_entry(uctx, &uchan->ctx_list, list) {
1557 ua_ctx = alloc_ust_app_ctx(&uctx->ctx);
1558 if (ua_ctx == NULL) {
1559 continue;
1560 }
1561 lttng_ht_node_init_ulong(&ua_ctx->node,
1562 (unsigned long) ua_ctx->ctx.ctx);
1563 lttng_ht_add_ulong(ua_chan->ctx, &ua_ctx->node);
1564 cds_list_add_tail(&ua_ctx->list, &ua_chan->ctx_list);
1565 }
1566
1567 /* Copy all events from ltt ust channel to ust app channel */
1568 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
1569 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
1570 uevent->filter, uevent->attr.loglevel, uevent->exclusion);
1571 if (ua_event == NULL) {
1572 DBG2("UST event %s not found on shadow copy channel",
1573 uevent->attr.name);
1574 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
1575 if (ua_event == NULL) {
1576 continue;
1577 }
1578 shadow_copy_event(ua_event, uevent);
1579 add_unique_ust_app_event(ua_chan, ua_event);
1580 }
1581 }
1582
1583 DBG3("UST app shadow copy of channel %s done", ua_chan->name);
1584 }
1585
1586 /*
1587 * Copy data between a UST app session and a regular LTT session.
1588 */
1589 static void shadow_copy_session(struct ust_app_session *ua_sess,
1590 struct ltt_ust_session *usess, struct ust_app *app)
1591 {
1592 struct lttng_ht_node_str *ua_chan_node;
1593 struct lttng_ht_iter iter;
1594 struct ltt_ust_channel *uchan;
1595 struct ust_app_channel *ua_chan;
1596 time_t rawtime;
1597 struct tm *timeinfo;
1598 char datetime[16];
1599 int ret;
1600
1601 /* Get date and time for unique app path */
1602 time(&rawtime);
1603 timeinfo = localtime(&rawtime);
1604 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
1605
1606 DBG2("Shadow copy of session handle %d", ua_sess->handle);
1607
1608 ua_sess->tracing_id = usess->id;
1609 ua_sess->id = get_next_session_id();
1610 ua_sess->uid = app->uid;
1611 ua_sess->gid = app->gid;
1612 ua_sess->euid = usess->uid;
1613 ua_sess->egid = usess->gid;
1614 ua_sess->buffer_type = usess->buffer_type;
1615 ua_sess->bits_per_long = app->bits_per_long;
1616 /* There is only one consumer object per session possible. */
1617 ua_sess->consumer = usess->consumer;
1618 ua_sess->output_traces = usess->output_traces;
1619 ua_sess->live_timer_interval = usess->live_timer_interval;
1620 copy_channel_attr_to_ustctl(&ua_sess->metadata_attr,
1621 &usess->metadata_attr);
1622
1623 switch (ua_sess->buffer_type) {
1624 case LTTNG_BUFFER_PER_PID:
1625 ret = snprintf(ua_sess->path, sizeof(ua_sess->path),
1626 DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s", app->name, app->pid,
1627 datetime);
1628 break;
1629 case LTTNG_BUFFER_PER_UID:
1630 ret = snprintf(ua_sess->path, sizeof(ua_sess->path),
1631 DEFAULT_UST_TRACE_UID_PATH, ua_sess->uid, app->bits_per_long);
1632 break;
1633 default:
1634 assert(0);
1635 goto error;
1636 }
1637 if (ret < 0) {
1638 PERROR("asprintf UST shadow copy session");
1639 assert(0);
1640 goto error;
1641 }
1642
1643 /* Iterate over all channels in global domain. */
1644 cds_lfht_for_each_entry(usess->domain_global.channels->ht, &iter.iter,
1645 uchan, node.node) {
1646 struct lttng_ht_iter uiter;
1647
1648 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
1649 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
1650 if (ua_chan_node != NULL) {
1651 /* Session exist. Contiuing. */
1652 continue;
1653 }
1654
1655 DBG2("Channel %s not found on shadow session copy, creating it",
1656 uchan->name);
1657 ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr);
1658 if (ua_chan == NULL) {
1659 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
1660 continue;
1661 }
1662 shadow_copy_channel(ua_chan, uchan);
1663 /*
1664 * The concept of metadata channel does not exist on the tracing
1665 * registry side of the session daemon so this can only be a per CPU
1666 * channel and not metadata.
1667 */
1668 ua_chan->attr.type = LTTNG_UST_CHAN_PER_CPU;
1669
1670 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
1671 }
1672
1673 error:
1674 return;
1675 }
1676
1677 /*
1678 * Lookup sesison wrapper.
1679 */
1680 static
1681 void __lookup_session_by_app(struct ltt_ust_session *usess,
1682 struct ust_app *app, struct lttng_ht_iter *iter)
1683 {
1684 /* Get right UST app session from app */
1685 lttng_ht_lookup(app->sessions, &usess->id, iter);
1686 }
1687
1688 /*
1689 * Return ust app session from the app session hashtable using the UST session
1690 * id.
1691 */
1692 static struct ust_app_session *lookup_session_by_app(
1693 struct ltt_ust_session *usess, struct ust_app *app)
1694 {
1695 struct lttng_ht_iter iter;
1696 struct lttng_ht_node_u64 *node;
1697
1698 __lookup_session_by_app(usess, app, &iter);
1699 node = lttng_ht_iter_get_node_u64(&iter);
1700 if (node == NULL) {
1701 goto error;
1702 }
1703
1704 return caa_container_of(node, struct ust_app_session, node);
1705
1706 error:
1707 return NULL;
1708 }
1709
1710 /*
1711 * Setup buffer registry per PID for the given session and application. If none
1712 * is found, a new one is created, added to the global registry and
1713 * initialized. If regp is valid, it's set with the newly created object.
1714 *
1715 * Return 0 on success or else a negative value.
1716 */
1717 static int setup_buffer_reg_pid(struct ust_app_session *ua_sess,
1718 struct ust_app *app, struct buffer_reg_pid **regp)
1719 {
1720 int ret = 0;
1721 struct buffer_reg_pid *reg_pid;
1722
1723 assert(ua_sess);
1724 assert(app);
1725
1726 rcu_read_lock();
1727
1728 reg_pid = buffer_reg_pid_find(ua_sess->id);
1729 if (!reg_pid) {
1730 /*
1731 * This is the create channel path meaning that if there is NO
1732 * registry available, we have to create one for this session.
1733 */
1734 ret = buffer_reg_pid_create(ua_sess->id, &reg_pid);
1735 if (ret < 0) {
1736 goto error;
1737 }
1738 } else {
1739 goto end;
1740 }
1741
1742 /* Initialize registry. */
1743 ret = ust_registry_session_init(&reg_pid->registry->reg.ust, app,
1744 app->bits_per_long, app->uint8_t_alignment,
1745 app->uint16_t_alignment, app->uint32_t_alignment,
1746 app->uint64_t_alignment, app->long_alignment,
1747 app->byte_order, app->version.major,
1748 app->version.minor);
1749 if (ret < 0) {
1750 /*
1751 * reg_pid->registry->reg.ust is NULL upon error, so we need to
1752 * destroy the buffer registry, because it is always expected
1753 * that if the buffer registry can be found, its ust registry is
1754 * non-NULL.
1755 */
1756 buffer_reg_pid_destroy(reg_pid);
1757 goto error;
1758 }
1759
1760 buffer_reg_pid_add(reg_pid);
1761
1762 DBG3("UST app buffer registry per PID created successfully");
1763
1764 end:
1765 if (regp) {
1766 *regp = reg_pid;
1767 }
1768 error:
1769 rcu_read_unlock();
1770 return ret;
1771 }
1772
1773 /*
1774 * Setup buffer registry per UID for the given session and application. If none
1775 * is found, a new one is created, added to the global registry and
1776 * initialized. If regp is valid, it's set with the newly created object.
1777 *
1778 * Return 0 on success or else a negative value.
1779 */
1780 static int setup_buffer_reg_uid(struct ltt_ust_session *usess,
1781 struct ust_app *app, struct buffer_reg_uid **regp)
1782 {
1783 int ret = 0;
1784 struct buffer_reg_uid *reg_uid;
1785
1786 assert(usess);
1787 assert(app);
1788
1789 rcu_read_lock();
1790
1791 reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid);
1792 if (!reg_uid) {
1793 /*
1794 * This is the create channel path meaning that if there is NO
1795 * registry available, we have to create one for this session.
1796 */
1797 ret = buffer_reg_uid_create(usess->id, app->bits_per_long, app->uid,
1798 LTTNG_DOMAIN_UST, &reg_uid);
1799 if (ret < 0) {
1800 goto error;
1801 }
1802 } else {
1803 goto end;
1804 }
1805
1806 /* Initialize registry. */
1807 ret = ust_registry_session_init(&reg_uid->registry->reg.ust, NULL,
1808 app->bits_per_long, app->uint8_t_alignment,
1809 app->uint16_t_alignment, app->uint32_t_alignment,
1810 app->uint64_t_alignment, app->long_alignment,
1811 app->byte_order, app->version.major,
1812 app->version.minor);
1813 if (ret < 0) {
1814 /*
1815 * reg_uid->registry->reg.ust is NULL upon error, so we need to
1816 * destroy the buffer registry, because it is always expected
1817 * that if the buffer registry can be found, its ust registry is
1818 * non-NULL.
1819 */
1820 buffer_reg_uid_destroy(reg_uid, NULL);
1821 goto error;
1822 }
1823 /* Add node to teardown list of the session. */
1824 cds_list_add(&reg_uid->lnode, &usess->buffer_reg_uid_list);
1825
1826 buffer_reg_uid_add(reg_uid);
1827
1828 DBG3("UST app buffer registry per UID created successfully");
1829 end:
1830 if (regp) {
1831 *regp = reg_uid;
1832 }
1833 error:
1834 rcu_read_unlock();
1835 return ret;
1836 }
1837
1838 /*
1839 * Create a session on the tracer side for the given app.
1840 *
1841 * On success, ua_sess_ptr is populated with the session pointer or else left
1842 * untouched. If the session was created, is_created is set to 1. On error,
1843 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
1844 * be NULL.
1845 *
1846 * Returns 0 on success or else a negative code which is either -ENOMEM or
1847 * -ENOTCONN which is the default code if the ustctl_create_session fails.
1848 */
1849 static int create_ust_app_session(struct ltt_ust_session *usess,
1850 struct ust_app *app, struct ust_app_session **ua_sess_ptr,
1851 int *is_created)
1852 {
1853 int ret, created = 0;
1854 struct ust_app_session *ua_sess;
1855
1856 assert(usess);
1857 assert(app);
1858 assert(ua_sess_ptr);
1859
1860 health_code_update();
1861
1862 ua_sess = lookup_session_by_app(usess, app);
1863 if (ua_sess == NULL) {
1864 DBG2("UST app pid: %d session id %" PRIu64 " not found, creating it",
1865 app->pid, usess->id);
1866 ua_sess = alloc_ust_app_session(app);
1867 if (ua_sess == NULL) {
1868 /* Only malloc can failed so something is really wrong */
1869 ret = -ENOMEM;
1870 goto error;
1871 }
1872 shadow_copy_session(ua_sess, usess, app);
1873 created = 1;
1874 }
1875
1876 switch (usess->buffer_type) {
1877 case LTTNG_BUFFER_PER_PID:
1878 /* Init local registry. */
1879 ret = setup_buffer_reg_pid(ua_sess, app, NULL);
1880 if (ret < 0) {
1881 delete_ust_app_session(-1, ua_sess, app);
1882 goto error;
1883 }
1884 break;
1885 case LTTNG_BUFFER_PER_UID:
1886 /* Look for a global registry. If none exists, create one. */
1887 ret = setup_buffer_reg_uid(usess, app, NULL);
1888 if (ret < 0) {
1889 delete_ust_app_session(-1, ua_sess, app);
1890 goto error;
1891 }
1892 break;
1893 default:
1894 assert(0);
1895 ret = -EINVAL;
1896 goto error;
1897 }
1898
1899 health_code_update();
1900
1901 if (ua_sess->handle == -1) {
1902 ret = ustctl_create_session(app->sock);
1903 if (ret < 0) {
1904 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1905 ERR("Creating session for app pid %d with ret %d",
1906 app->pid, ret);
1907 } else {
1908 DBG("UST app creating session failed. Application is dead");
1909 /*
1910 * This is normal behavior, an application can die during the
1911 * creation process. Don't report an error so the execution can
1912 * continue normally. This will get flagged ENOTCONN and the
1913 * caller will handle it.
1914 */
1915 ret = 0;
1916 }
1917 delete_ust_app_session(-1, ua_sess, app);
1918 if (ret != -ENOMEM) {
1919 /*
1920 * Tracer is probably gone or got an internal error so let's
1921 * behave like it will soon unregister or not usable.
1922 */
1923 ret = -ENOTCONN;
1924 }
1925 goto error;
1926 }
1927
1928 ua_sess->handle = ret;
1929
1930 /* Add ust app session to app's HT */
1931 lttng_ht_node_init_u64(&ua_sess->node,
1932 ua_sess->tracing_id);
1933 lttng_ht_add_unique_u64(app->sessions, &ua_sess->node);
1934
1935 DBG2("UST app session created successfully with handle %d", ret);
1936 }
1937
1938 *ua_sess_ptr = ua_sess;
1939 if (is_created) {
1940 *is_created = created;
1941 }
1942
1943 /* Everything went well. */
1944 ret = 0;
1945
1946 error:
1947 health_code_update();
1948 return ret;
1949 }
1950
1951 /*
1952 * Match function for a hash table lookup of ust_app_ctx.
1953 *
1954 * It matches an ust app context based on the context type and, in the case
1955 * of perf counters, their name.
1956 */
1957 static int ht_match_ust_app_ctx(struct cds_lfht_node *node, const void *_key)
1958 {
1959 struct ust_app_ctx *ctx;
1960 const struct lttng_ust_context *key;
1961
1962 assert(node);
1963 assert(_key);
1964
1965 ctx = caa_container_of(node, struct ust_app_ctx, node.node);
1966 key = _key;
1967
1968 /* Context type */
1969 if (ctx->ctx.ctx != key->ctx) {
1970 goto no_match;
1971 }
1972
1973 /* Check the name in the case of perf thread counters. */
1974 if (key->ctx == LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER) {
1975 if (strncmp(key->u.perf_counter.name,
1976 ctx->ctx.u.perf_counter.name,
1977 sizeof(key->u.perf_counter.name))) {
1978 goto no_match;
1979 }
1980 }
1981
1982 /* Match. */
1983 return 1;
1984
1985 no_match:
1986 return 0;
1987 }
1988
1989 /*
1990 * Lookup for an ust app context from an lttng_ust_context.
1991 *
1992 * Must be called while holding RCU read side lock.
1993 * Return an ust_app_ctx object or NULL on error.
1994 */
1995 static
1996 struct ust_app_ctx *find_ust_app_context(struct lttng_ht *ht,
1997 struct lttng_ust_context *uctx)
1998 {
1999 struct lttng_ht_iter iter;
2000 struct lttng_ht_node_ulong *node;
2001 struct ust_app_ctx *app_ctx = NULL;
2002
2003 assert(uctx);
2004 assert(ht);
2005
2006 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2007 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) uctx->ctx, lttng_ht_seed),
2008 ht_match_ust_app_ctx, uctx, &iter.iter);
2009 node = lttng_ht_iter_get_node_ulong(&iter);
2010 if (!node) {
2011 goto end;
2012 }
2013
2014 app_ctx = caa_container_of(node, struct ust_app_ctx, node);
2015
2016 end:
2017 return app_ctx;
2018 }
2019
2020 /*
2021 * Create a context for the channel on the tracer.
2022 *
2023 * Called with UST app session lock held and a RCU read side lock.
2024 */
2025 static
2026 int create_ust_app_channel_context(struct ust_app_session *ua_sess,
2027 struct ust_app_channel *ua_chan, struct lttng_ust_context *uctx,
2028 struct ust_app *app)
2029 {
2030 int ret = 0;
2031 struct ust_app_ctx *ua_ctx;
2032
2033 DBG2("UST app adding context to channel %s", ua_chan->name);
2034
2035 ua_ctx = find_ust_app_context(ua_chan->ctx, uctx);
2036 if (ua_ctx) {
2037 ret = -EEXIST;
2038 goto error;
2039 }
2040
2041 ua_ctx = alloc_ust_app_ctx(uctx);
2042 if (ua_ctx == NULL) {
2043 /* malloc failed */
2044 ret = -1;
2045 goto error;
2046 }
2047
2048 lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx);
2049 lttng_ht_add_ulong(ua_chan->ctx, &ua_ctx->node);
2050 cds_list_add_tail(&ua_ctx->list, &ua_chan->ctx_list);
2051
2052 ret = create_ust_channel_context(ua_chan, ua_ctx, app);
2053 if (ret < 0) {
2054 goto error;
2055 }
2056
2057 error:
2058 return ret;
2059 }
2060
2061 /*
2062 * Enable on the tracer side a ust app event for the session and channel.
2063 *
2064 * Called with UST app session lock held.
2065 */
2066 static
2067 int enable_ust_app_event(struct ust_app_session *ua_sess,
2068 struct ust_app_event *ua_event, struct ust_app *app)
2069 {
2070 int ret;
2071
2072 ret = enable_ust_event(app, ua_sess, ua_event);
2073 if (ret < 0) {
2074 goto error;
2075 }
2076
2077 ua_event->enabled = 1;
2078
2079 error:
2080 return ret;
2081 }
2082
2083 /*
2084 * Disable on the tracer side a ust app event for the session and channel.
2085 */
2086 static int disable_ust_app_event(struct ust_app_session *ua_sess,
2087 struct ust_app_event *ua_event, struct ust_app *app)
2088 {
2089 int ret;
2090
2091 ret = disable_ust_event(app, ua_sess, ua_event);
2092 if (ret < 0) {
2093 goto error;
2094 }
2095
2096 ua_event->enabled = 0;
2097
2098 error:
2099 return ret;
2100 }
2101
2102 /*
2103 * Lookup ust app channel for session and disable it on the tracer side.
2104 */
2105 static
2106 int disable_ust_app_channel(struct ust_app_session *ua_sess,
2107 struct ust_app_channel *ua_chan, struct ust_app *app)
2108 {
2109 int ret;
2110
2111 ret = disable_ust_channel(app, ua_sess, ua_chan);
2112 if (ret < 0) {
2113 goto error;
2114 }
2115
2116 ua_chan->enabled = 0;
2117
2118 error:
2119 return ret;
2120 }
2121
2122 /*
2123 * Lookup ust app channel for session and enable it on the tracer side. This
2124 * MUST be called with a RCU read side lock acquired.
2125 */
2126 static int enable_ust_app_channel(struct ust_app_session *ua_sess,
2127 struct ltt_ust_channel *uchan, struct ust_app *app)
2128 {
2129 int ret = 0;
2130 struct lttng_ht_iter iter;
2131 struct lttng_ht_node_str *ua_chan_node;
2132 struct ust_app_channel *ua_chan;
2133
2134 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
2135 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
2136 if (ua_chan_node == NULL) {
2137 DBG2("Unable to find channel %s in ust session id %" PRIu64,
2138 uchan->name, ua_sess->tracing_id);
2139 goto error;
2140 }
2141
2142 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2143
2144 ret = enable_ust_channel(app, ua_sess, ua_chan);
2145 if (ret < 0) {
2146 goto error;
2147 }
2148
2149 error:
2150 return ret;
2151 }
2152
2153 /*
2154 * Ask the consumer to create a channel and get it if successful.
2155 *
2156 * Return 0 on success or else a negative value.
2157 */
2158 static int do_consumer_create_channel(struct ltt_ust_session *usess,
2159 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan,
2160 int bitness, struct ust_registry_session *registry)
2161 {
2162 int ret;
2163 unsigned int nb_fd = 0;
2164 struct consumer_socket *socket;
2165
2166 assert(usess);
2167 assert(ua_sess);
2168 assert(ua_chan);
2169 assert(registry);
2170
2171 rcu_read_lock();
2172 health_code_update();
2173
2174 /* Get the right consumer socket for the application. */
2175 socket = consumer_find_socket_by_bitness(bitness, usess->consumer);
2176 if (!socket) {
2177 ret = -EINVAL;
2178 goto error;
2179 }
2180
2181 health_code_update();
2182
2183 /* Need one fd for the channel. */
2184 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
2185 if (ret < 0) {
2186 ERR("Exhausted number of available FD upon create channel");
2187 goto error;
2188 }
2189
2190 /*
2191 * Ask consumer to create channel. The consumer will return the number of
2192 * stream we have to expect.
2193 */
2194 ret = ust_consumer_ask_channel(ua_sess, ua_chan, usess->consumer, socket,
2195 registry);
2196 if (ret < 0) {
2197 goto error_ask;
2198 }
2199
2200 /*
2201 * Compute the number of fd needed before receiving them. It must be 2 per
2202 * stream (2 being the default value here).
2203 */
2204 nb_fd = DEFAULT_UST_STREAM_FD_NUM * ua_chan->expected_stream_count;
2205
2206 /* Reserve the amount of file descriptor we need. */
2207 ret = lttng_fd_get(LTTNG_FD_APPS, nb_fd);
2208 if (ret < 0) {
2209 ERR("Exhausted number of available FD upon create channel");
2210 goto error_fd_get_stream;
2211 }
2212
2213 health_code_update();
2214
2215 /*
2216 * Now get the channel from the consumer. This call wil populate the stream
2217 * list of that channel and set the ust objects.
2218 */
2219 if (usess->consumer->enabled) {
2220 ret = ust_consumer_get_channel(socket, ua_chan);
2221 if (ret < 0) {
2222 goto error_destroy;
2223 }
2224 }
2225
2226 rcu_read_unlock();
2227 return 0;
2228
2229 error_destroy:
2230 lttng_fd_put(LTTNG_FD_APPS, nb_fd);
2231 error_fd_get_stream:
2232 /*
2233 * Initiate a destroy channel on the consumer since we had an error
2234 * handling it on our side. The return value is of no importance since we
2235 * already have a ret value set by the previous error that we need to
2236 * return.
2237 */
2238 (void) ust_consumer_destroy_channel(socket, ua_chan);
2239 error_ask:
2240 lttng_fd_put(LTTNG_FD_APPS, 1);
2241 error:
2242 health_code_update();
2243 rcu_read_unlock();
2244 return ret;
2245 }
2246
2247 /*
2248 * Duplicate the ust data object of the ust app stream and save it in the
2249 * buffer registry stream.
2250 *
2251 * Return 0 on success or else a negative value.
2252 */
2253 static int duplicate_stream_object(struct buffer_reg_stream *reg_stream,
2254 struct ust_app_stream *stream)
2255 {
2256 int ret;
2257
2258 assert(reg_stream);
2259 assert(stream);
2260
2261 /* Reserve the amount of file descriptor we need. */
2262 ret = lttng_fd_get(LTTNG_FD_APPS, 2);
2263 if (ret < 0) {
2264 ERR("Exhausted number of available FD upon duplicate stream");
2265 goto error;
2266 }
2267
2268 /* Duplicate object for stream once the original is in the registry. */
2269 ret = ustctl_duplicate_ust_object_data(&stream->obj,
2270 reg_stream->obj.ust);
2271 if (ret < 0) {
2272 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2273 reg_stream->obj.ust, stream->obj, ret);
2274 lttng_fd_put(LTTNG_FD_APPS, 2);
2275 goto error;
2276 }
2277 stream->handle = stream->obj->handle;
2278
2279 error:
2280 return ret;
2281 }
2282
2283 /*
2284 * Duplicate the ust data object of the ust app. channel and save it in the
2285 * buffer registry channel.
2286 *
2287 * Return 0 on success or else a negative value.
2288 */
2289 static int duplicate_channel_object(struct buffer_reg_channel *reg_chan,
2290 struct ust_app_channel *ua_chan)
2291 {
2292 int ret;
2293
2294 assert(reg_chan);
2295 assert(ua_chan);
2296
2297 /* Need two fds for the channel. */
2298 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
2299 if (ret < 0) {
2300 ERR("Exhausted number of available FD upon duplicate channel");
2301 goto error_fd_get;
2302 }
2303
2304 /* Duplicate object for stream once the original is in the registry. */
2305 ret = ustctl_duplicate_ust_object_data(&ua_chan->obj, reg_chan->obj.ust);
2306 if (ret < 0) {
2307 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2308 reg_chan->obj.ust, ua_chan->obj, ret);
2309 goto error;
2310 }
2311 ua_chan->handle = ua_chan->obj->handle;
2312
2313 return 0;
2314
2315 error:
2316 lttng_fd_put(LTTNG_FD_APPS, 1);
2317 error_fd_get:
2318 return ret;
2319 }
2320
2321 /*
2322 * For a given channel buffer registry, setup all streams of the given ust
2323 * application channel.
2324 *
2325 * Return 0 on success or else a negative value.
2326 */
2327 static int setup_buffer_reg_streams(struct buffer_reg_channel *reg_chan,
2328 struct ust_app_channel *ua_chan)
2329 {
2330 int ret = 0;
2331 struct ust_app_stream *stream, *stmp;
2332
2333 assert(reg_chan);
2334 assert(ua_chan);
2335
2336 DBG2("UST app setup buffer registry stream");
2337
2338 /* Send all streams to application. */
2339 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
2340 struct buffer_reg_stream *reg_stream;
2341
2342 ret = buffer_reg_stream_create(&reg_stream);
2343 if (ret < 0) {
2344 goto error;
2345 }
2346
2347 /*
2348 * Keep original pointer and nullify it in the stream so the delete
2349 * stream call does not release the object.
2350 */
2351 reg_stream->obj.ust = stream->obj;
2352 stream->obj = NULL;
2353 buffer_reg_stream_add(reg_stream, reg_chan);
2354
2355 /* We don't need the streams anymore. */
2356 cds_list_del(&stream->list);
2357 delete_ust_app_stream(-1, stream);
2358 }
2359
2360 error:
2361 return ret;
2362 }
2363
2364 /*
2365 * Create a buffer registry channel for the given session registry and
2366 * application channel object. If regp pointer is valid, it's set with the
2367 * created object. Important, the created object is NOT added to the session
2368 * registry hash table.
2369 *
2370 * Return 0 on success else a negative value.
2371 */
2372 static int create_buffer_reg_channel(struct buffer_reg_session *reg_sess,
2373 struct ust_app_channel *ua_chan, struct buffer_reg_channel **regp)
2374 {
2375 int ret;
2376 struct buffer_reg_channel *reg_chan = NULL;
2377
2378 assert(reg_sess);
2379 assert(ua_chan);
2380
2381 DBG2("UST app creating buffer registry channel for %s", ua_chan->name);
2382
2383 /* Create buffer registry channel. */
2384 ret = buffer_reg_channel_create(ua_chan->tracing_channel_id, &reg_chan);
2385 if (ret < 0) {
2386 goto error_create;
2387 }
2388 assert(reg_chan);
2389 reg_chan->consumer_key = ua_chan->key;
2390 reg_chan->subbuf_size = ua_chan->attr.subbuf_size;
2391
2392 /* Create and add a channel registry to session. */
2393 ret = ust_registry_channel_add(reg_sess->reg.ust,
2394 ua_chan->tracing_channel_id);
2395 if (ret < 0) {
2396 goto error;
2397 }
2398 buffer_reg_channel_add(reg_sess, reg_chan);
2399
2400 if (regp) {
2401 *regp = reg_chan;
2402 }
2403
2404 return 0;
2405
2406 error:
2407 /* Safe because the registry channel object was not added to any HT. */
2408 buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST);
2409 error_create:
2410 return ret;
2411 }
2412
2413 /*
2414 * Setup buffer registry channel for the given session registry and application
2415 * channel object. If regp pointer is valid, it's set with the created object.
2416 *
2417 * Return 0 on success else a negative value.
2418 */
2419 static int setup_buffer_reg_channel(struct buffer_reg_session *reg_sess,
2420 struct ust_app_channel *ua_chan, struct buffer_reg_channel *reg_chan)
2421 {
2422 int ret;
2423
2424 assert(reg_sess);
2425 assert(reg_chan);
2426 assert(ua_chan);
2427 assert(ua_chan->obj);
2428
2429 DBG2("UST app setup buffer registry channel for %s", ua_chan->name);
2430
2431 /* Setup all streams for the registry. */
2432 ret = setup_buffer_reg_streams(reg_chan, ua_chan);
2433 if (ret < 0) {
2434 goto error;
2435 }
2436
2437 reg_chan->obj.ust = ua_chan->obj;
2438 ua_chan->obj = NULL;
2439
2440 return 0;
2441
2442 error:
2443 buffer_reg_channel_remove(reg_sess, reg_chan);
2444 buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST);
2445 return ret;
2446 }
2447
2448 /*
2449 * Send buffer registry channel to the application.
2450 *
2451 * Return 0 on success else a negative value.
2452 */
2453 static int send_channel_uid_to_ust(struct buffer_reg_channel *reg_chan,
2454 struct ust_app *app, struct ust_app_session *ua_sess,
2455 struct ust_app_channel *ua_chan)
2456 {
2457 int ret;
2458 struct buffer_reg_stream *reg_stream;
2459
2460 assert(reg_chan);
2461 assert(app);
2462 assert(ua_sess);
2463 assert(ua_chan);
2464
2465 DBG("UST app sending buffer registry channel to ust sock %d", app->sock);
2466
2467 ret = duplicate_channel_object(reg_chan, ua_chan);
2468 if (ret < 0) {
2469 goto error;
2470 }
2471
2472 /* Send channel to the application. */
2473 ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan);
2474 if (ret < 0) {
2475 goto error;
2476 }
2477
2478 health_code_update();
2479
2480 /* Send all streams to application. */
2481 pthread_mutex_lock(&reg_chan->stream_list_lock);
2482 cds_list_for_each_entry(reg_stream, &reg_chan->streams, lnode) {
2483 struct ust_app_stream stream;
2484
2485 ret = duplicate_stream_object(reg_stream, &stream);
2486 if (ret < 0) {
2487 goto error_stream_unlock;
2488 }
2489
2490 ret = ust_consumer_send_stream_to_ust(app, ua_chan, &stream);
2491 if (ret < 0) {
2492 (void) release_ust_app_stream(-1, &stream);
2493 goto error_stream_unlock;
2494 }
2495
2496 /*
2497 * The return value is not important here. This function will output an
2498 * error if needed.
2499 */
2500 (void) release_ust_app_stream(-1, &stream);
2501 }
2502 ua_chan->is_sent = 1;
2503
2504 error_stream_unlock:
2505 pthread_mutex_unlock(&reg_chan->stream_list_lock);
2506 error:
2507 return ret;
2508 }
2509
2510 /*
2511 * Create and send to the application the created buffers with per UID buffers.
2512 *
2513 * Return 0 on success else a negative value.
2514 */
2515 static int create_channel_per_uid(struct ust_app *app,
2516 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
2517 struct ust_app_channel *ua_chan)
2518 {
2519 int ret;
2520 struct buffer_reg_uid *reg_uid;
2521 struct buffer_reg_channel *reg_chan;
2522
2523 assert(app);
2524 assert(usess);
2525 assert(ua_sess);
2526 assert(ua_chan);
2527
2528 DBG("UST app creating channel %s with per UID buffers", ua_chan->name);
2529
2530 reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid);
2531 /*
2532 * The session creation handles the creation of this global registry
2533 * object. If none can be find, there is a code flow problem or a
2534 * teardown race.
2535 */
2536 assert(reg_uid);
2537
2538 reg_chan = buffer_reg_channel_find(ua_chan->tracing_channel_id,
2539 reg_uid);
2540 if (!reg_chan) {
2541 /* Create the buffer registry channel object. */
2542 ret = create_buffer_reg_channel(reg_uid->registry, ua_chan, &reg_chan);
2543 if (ret < 0) {
2544 ERR("Error creating the UST channel \"%s\" registry instance",
2545 ua_chan->name);
2546 goto error;
2547 }
2548 assert(reg_chan);
2549
2550 /*
2551 * Create the buffers on the consumer side. This call populates the
2552 * ust app channel object with all streams and data object.
2553 */
2554 ret = do_consumer_create_channel(usess, ua_sess, ua_chan,
2555 app->bits_per_long, reg_uid->registry->reg.ust);
2556 if (ret < 0) {
2557 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2558 ua_chan->name);
2559
2560 /*
2561 * Let's remove the previously created buffer registry channel so
2562 * it's not visible anymore in the session registry.
2563 */
2564 ust_registry_channel_del_free(reg_uid->registry->reg.ust,
2565 ua_chan->tracing_channel_id);
2566 buffer_reg_channel_remove(reg_uid->registry, reg_chan);
2567 buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST);
2568 goto error;
2569 }
2570
2571 /*
2572 * Setup the streams and add it to the session registry.
2573 */
2574 ret = setup_buffer_reg_channel(reg_uid->registry, ua_chan, reg_chan);
2575 if (ret < 0) {
2576 ERR("Error setting up UST channel \"%s\"",
2577 ua_chan->name);
2578 goto error;
2579 }
2580
2581 }
2582
2583 /* Send buffers to the application. */
2584 ret = send_channel_uid_to_ust(reg_chan, app, ua_sess, ua_chan);
2585 if (ret < 0) {
2586 /*
2587 * Don't report error to the console, since it may be
2588 * caused by application concurrently exiting.
2589 */
2590 goto error;
2591 }
2592
2593 error:
2594 return ret;
2595 }
2596
2597 /*
2598 * Create and send to the application the created buffers with per PID buffers.
2599 *
2600 * Return 0 on success else a negative value.
2601 */
2602 static int create_channel_per_pid(struct ust_app *app,
2603 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
2604 struct ust_app_channel *ua_chan)
2605 {
2606 int ret;
2607 struct ust_registry_session *registry;
2608
2609 assert(app);
2610 assert(usess);
2611 assert(ua_sess);
2612 assert(ua_chan);
2613
2614 DBG("UST app creating channel %s with per PID buffers", ua_chan->name);
2615
2616 rcu_read_lock();
2617
2618 registry = get_session_registry(ua_sess);
2619 assert(registry);
2620
2621 /* Create and add a new channel registry to session. */
2622 ret = ust_registry_channel_add(registry, ua_chan->key);
2623 if (ret < 0) {
2624 ERR("Error creating the UST channel \"%s\" registry instance",
2625 ua_chan->name);
2626 goto error;
2627 }
2628
2629 /* Create and get channel on the consumer side. */
2630 ret = do_consumer_create_channel(usess, ua_sess, ua_chan,
2631 app->bits_per_long, registry);
2632 if (ret < 0) {
2633 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2634 ua_chan->name);
2635 goto error;
2636 }
2637
2638 ret = send_channel_pid_to_ust(app, ua_sess, ua_chan);
2639 if (ret < 0) {
2640 /*
2641 * Don't report error to the console, since it may be
2642 * caused by application concurrently exiting.
2643 */
2644 goto error;
2645 }
2646
2647 error:
2648 rcu_read_unlock();
2649 return ret;
2650 }
2651
2652 /*
2653 * From an already allocated ust app channel, create the channel buffers if
2654 * need and send it to the application. This MUST be called with a RCU read
2655 * side lock acquired.
2656 *
2657 * Return 0 on success or else a negative value.
2658 */
2659 static int do_create_channel(struct ust_app *app,
2660 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
2661 struct ust_app_channel *ua_chan)
2662 {
2663 int ret;
2664
2665 assert(app);
2666 assert(usess);
2667 assert(ua_sess);
2668 assert(ua_chan);
2669
2670 /* Handle buffer type before sending the channel to the application. */
2671 switch (usess->buffer_type) {
2672 case LTTNG_BUFFER_PER_UID:
2673 {
2674 ret = create_channel_per_uid(app, usess, ua_sess, ua_chan);
2675 if (ret < 0) {
2676 goto error;
2677 }
2678 break;
2679 }
2680 case LTTNG_BUFFER_PER_PID:
2681 {
2682 ret = create_channel_per_pid(app, usess, ua_sess, ua_chan);
2683 if (ret < 0) {
2684 goto error;
2685 }
2686 break;
2687 }
2688 default:
2689 assert(0);
2690 ret = -EINVAL;
2691 goto error;
2692 }
2693
2694 /* Initialize ust objd object using the received handle and add it. */
2695 lttng_ht_node_init_ulong(&ua_chan->ust_objd_node, ua_chan->handle);
2696 lttng_ht_add_unique_ulong(app->ust_objd, &ua_chan->ust_objd_node);
2697
2698 /* If channel is not enabled, disable it on the tracer */
2699 if (!ua_chan->enabled) {
2700 ret = disable_ust_channel(app, ua_sess, ua_chan);
2701 if (ret < 0) {
2702 goto error;
2703 }
2704 }
2705
2706 error:
2707 return ret;
2708 }
2709
2710 /*
2711 * Create UST app channel and create it on the tracer. Set ua_chanp of the
2712 * newly created channel if not NULL.
2713 *
2714 * Called with UST app session lock and RCU read-side lock held.
2715 *
2716 * Return 0 on success or else a negative value.
2717 */
2718 static int create_ust_app_channel(struct ust_app_session *ua_sess,
2719 struct ltt_ust_channel *uchan, struct ust_app *app,
2720 enum lttng_ust_chan_type type, struct ltt_ust_session *usess,
2721 struct ust_app_channel **ua_chanp)
2722 {
2723 int ret = 0;
2724 struct lttng_ht_iter iter;
2725 struct lttng_ht_node_str *ua_chan_node;
2726 struct ust_app_channel *ua_chan;
2727
2728 /* Lookup channel in the ust app session */
2729 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
2730 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
2731 if (ua_chan_node != NULL) {
2732 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2733 goto end;
2734 }
2735
2736 ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr);
2737 if (ua_chan == NULL) {
2738 /* Only malloc can fail here */
2739 ret = -ENOMEM;
2740 goto error_alloc;
2741 }
2742 shadow_copy_channel(ua_chan, uchan);
2743
2744 /* Set channel type. */
2745 ua_chan->attr.type = type;
2746
2747 ret = do_create_channel(app, usess, ua_sess, ua_chan);
2748 if (ret < 0) {
2749 goto error;
2750 }
2751
2752 DBG2("UST app create channel %s for PID %d completed", ua_chan->name,
2753 app->pid);
2754
2755 /* Only add the channel if successful on the tracer side. */
2756 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
2757
2758 end:
2759 if (ua_chanp) {
2760 *ua_chanp = ua_chan;
2761 }
2762
2763 /* Everything went well. */
2764 return 0;
2765
2766 error:
2767 delete_ust_app_channel(ua_chan->is_sent ? app->sock : -1, ua_chan, app);
2768 error_alloc:
2769 return ret;
2770 }
2771
2772 /*
2773 * Create UST app event and create it on the tracer side.
2774 *
2775 * Called with ust app session mutex held.
2776 */
2777 static
2778 int create_ust_app_event(struct ust_app_session *ua_sess,
2779 struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent,
2780 struct ust_app *app)
2781 {
2782 int ret = 0;
2783 struct ust_app_event *ua_event;
2784
2785 /* Get event node */
2786 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
2787 uevent->filter, uevent->attr.loglevel, uevent->exclusion);
2788 if (ua_event != NULL) {
2789 ret = -EEXIST;
2790 goto end;
2791 }
2792
2793 /* Does not exist so create one */
2794 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
2795 if (ua_event == NULL) {
2796 /* Only malloc can failed so something is really wrong */
2797 ret = -ENOMEM;
2798 goto end;
2799 }
2800 shadow_copy_event(ua_event, uevent);
2801
2802 /* Create it on the tracer side */
2803 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
2804 if (ret < 0) {
2805 /* Not found previously means that it does not exist on the tracer */
2806 assert(ret != -LTTNG_UST_ERR_EXIST);
2807 goto error;
2808 }
2809
2810 add_unique_ust_app_event(ua_chan, ua_event);
2811
2812 DBG2("UST app create event %s for PID %d completed", ua_event->name,
2813 app->pid);
2814
2815 end:
2816 return ret;
2817
2818 error:
2819 /* Valid. Calling here is already in a read side lock */
2820 delete_ust_app_event(-1, ua_event);
2821 return ret;
2822 }
2823
2824 /*
2825 * Create UST metadata and open it on the tracer side.
2826 *
2827 * Called with UST app session lock held and RCU read side lock.
2828 */
2829 static int create_ust_app_metadata(struct ust_app_session *ua_sess,
2830 struct ust_app *app, struct consumer_output *consumer)
2831 {
2832 int ret = 0;
2833 struct ust_app_channel *metadata;
2834 struct consumer_socket *socket;
2835 struct ust_registry_session *registry;
2836
2837 assert(ua_sess);
2838 assert(app);
2839 assert(consumer);
2840
2841 registry = get_session_registry(ua_sess);
2842 assert(registry);
2843
2844 /* Metadata already exists for this registry or it was closed previously */
2845 if (registry->metadata_key || registry->metadata_closed) {
2846 ret = 0;
2847 goto error;
2848 }
2849
2850 /* Allocate UST metadata */
2851 metadata = alloc_ust_app_channel(DEFAULT_METADATA_NAME, ua_sess, NULL);
2852 if (!metadata) {
2853 /* malloc() failed */
2854 ret = -ENOMEM;
2855 goto error;
2856 }
2857
2858 memcpy(&metadata->attr, &ua_sess->metadata_attr, sizeof(metadata->attr));
2859
2860 /* Need one fd for the channel. */
2861 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
2862 if (ret < 0) {
2863 ERR("Exhausted number of available FD upon create metadata");
2864 goto error;
2865 }
2866
2867 /* Get the right consumer socket for the application. */
2868 socket = consumer_find_socket_by_bitness(app->bits_per_long, consumer);
2869 if (!socket) {
2870 ret = -EINVAL;
2871 goto error_consumer;
2872 }
2873
2874 /*
2875 * Keep metadata key so we can identify it on the consumer side. Assign it
2876 * to the registry *before* we ask the consumer so we avoid the race of the
2877 * consumer requesting the metadata and the ask_channel call on our side
2878 * did not returned yet.
2879 */
2880 registry->metadata_key = metadata->key;
2881
2882 /*
2883 * Ask the metadata channel creation to the consumer. The metadata object
2884 * will be created by the consumer and kept their. However, the stream is
2885 * never added or monitored until we do a first push metadata to the
2886 * consumer.
2887 */
2888 ret = ust_consumer_ask_channel(ua_sess, metadata, consumer, socket,
2889 registry);
2890 if (ret < 0) {
2891 /* Nullify the metadata key so we don't try to close it later on. */
2892 registry->metadata_key = 0;
2893 goto error_consumer;
2894 }
2895
2896 /*
2897 * The setup command will make the metadata stream be sent to the relayd,
2898 * if applicable, and the thread managing the metadatas. This is important
2899 * because after this point, if an error occurs, the only way the stream
2900 * can be deleted is to be monitored in the consumer.
2901 */
2902 ret = consumer_setup_metadata(socket, metadata->key);
2903 if (ret < 0) {
2904 /* Nullify the metadata key so we don't try to close it later on. */
2905 registry->metadata_key = 0;
2906 goto error_consumer;
2907 }
2908
2909 DBG2("UST metadata with key %" PRIu64 " created for app pid %d",
2910 metadata->key, app->pid);
2911
2912 error_consumer:
2913 lttng_fd_put(LTTNG_FD_APPS, 1);
2914 delete_ust_app_channel(-1, metadata, app);
2915 error:
2916 return ret;
2917 }
2918
2919 /*
2920 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
2921 * acquired before calling this function.
2922 */
2923 struct ust_app *ust_app_find_by_pid(pid_t pid)
2924 {
2925 struct ust_app *app = NULL;
2926 struct lttng_ht_node_ulong *node;
2927 struct lttng_ht_iter iter;
2928
2929 lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) pid), &iter);
2930 node = lttng_ht_iter_get_node_ulong(&iter);
2931 if (node == NULL) {
2932 DBG2("UST app no found with pid %d", pid);
2933 goto error;
2934 }
2935
2936 DBG2("Found UST app by pid %d", pid);
2937
2938 app = caa_container_of(node, struct ust_app, pid_n);
2939
2940 error:
2941 return app;
2942 }
2943
2944 /*
2945 * Allocate and init an UST app object using the registration information and
2946 * the command socket. This is called when the command socket connects to the
2947 * session daemon.
2948 *
2949 * The object is returned on success or else NULL.
2950 */
2951 struct ust_app *ust_app_create(struct ust_register_msg *msg, int sock)
2952 {
2953 struct ust_app *lta = NULL;
2954
2955 assert(msg);
2956 assert(sock >= 0);
2957
2958 DBG3("UST app creating application for socket %d", sock);
2959
2960 if ((msg->bits_per_long == 64 &&
2961 (uatomic_read(&ust_consumerd64_fd) == -EINVAL))
2962 || (msg->bits_per_long == 32 &&
2963 (uatomic_read(&ust_consumerd32_fd) == -EINVAL))) {
2964 ERR("Registration failed: application \"%s\" (pid: %d) has "
2965 "%d-bit long, but no consumerd for this size is available.\n",
2966 msg->name, msg->pid, msg->bits_per_long);
2967 goto error;
2968 }
2969
2970 lta = zmalloc(sizeof(struct ust_app));
2971 if (lta == NULL) {
2972 PERROR("malloc");
2973 goto error;
2974 }
2975
2976 lta->ppid = msg->ppid;
2977 lta->uid = msg->uid;
2978 lta->gid = msg->gid;
2979
2980 lta->bits_per_long = msg->bits_per_long;
2981 lta->uint8_t_alignment = msg->uint8_t_alignment;
2982 lta->uint16_t_alignment = msg->uint16_t_alignment;
2983 lta->uint32_t_alignment = msg->uint32_t_alignment;
2984 lta->uint64_t_alignment = msg->uint64_t_alignment;
2985 lta->long_alignment = msg->long_alignment;
2986 lta->byte_order = msg->byte_order;
2987
2988 lta->v_major = msg->major;
2989 lta->v_minor = msg->minor;
2990 lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
2991 lta->ust_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
2992 lta->notify_sock = -1;
2993
2994 /* Copy name and make sure it's NULL terminated. */
2995 strncpy(lta->name, msg->name, sizeof(lta->name));
2996 lta->name[UST_APP_PROCNAME_LEN] = '\0';
2997
2998 /*
2999 * Before this can be called, when receiving the registration information,
3000 * the application compatibility is checked. So, at this point, the
3001 * application can work with this session daemon.
3002 */
3003 lta->compatible = 1;
3004
3005 lta->pid = msg->pid;
3006 lttng_ht_node_init_ulong(&lta->pid_n, (unsigned long) lta->pid);
3007 lta->sock = sock;
3008 lttng_ht_node_init_ulong(&lta->sock_n, (unsigned long) lta->sock);
3009
3010 CDS_INIT_LIST_HEAD(&lta->teardown_head);
3011
3012 error:
3013 return lta;
3014 }
3015
3016 /*
3017 * For a given application object, add it to every hash table.
3018 */
3019 void ust_app_add(struct ust_app *app)
3020 {
3021 assert(app);
3022 assert(app->notify_sock >= 0);
3023
3024 rcu_read_lock();
3025
3026 /*
3027 * On a re-registration, we want to kick out the previous registration of
3028 * that pid
3029 */
3030 lttng_ht_add_replace_ulong(ust_app_ht, &app->pid_n);
3031
3032 /*
3033 * The socket _should_ be unique until _we_ call close. So, a add_unique
3034 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3035 * already in the table.
3036 */
3037 lttng_ht_add_unique_ulong(ust_app_ht_by_sock, &app->sock_n);
3038
3039 /* Add application to the notify socket hash table. */
3040 lttng_ht_node_init_ulong(&app->notify_sock_n, app->notify_sock);
3041 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock, &app->notify_sock_n);
3042
3043 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
3044 "notify_sock:%d (version %d.%d)", app->pid, app->ppid, app->uid,
3045 app->gid, app->sock, app->name, app->notify_sock, app->v_major,
3046 app->v_minor);
3047
3048 rcu_read_unlock();
3049 }
3050
3051 /*
3052 * Set the application version into the object.
3053 *
3054 * Return 0 on success else a negative value either an errno code or a
3055 * LTTng-UST error code.
3056 */
3057 int ust_app_version(struct ust_app *app)
3058 {
3059 int ret;
3060
3061 assert(app);
3062
3063 ret = ustctl_tracer_version(app->sock, &app->version);
3064 if (ret < 0) {
3065 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
3066 ERR("UST app %d version failed with ret %d", app->sock, ret);
3067 } else {
3068 DBG3("UST app %d version failed. Application is dead", app->sock);
3069 }
3070 }
3071
3072 return ret;
3073 }
3074
3075 /*
3076 * Unregister app by removing it from the global traceable app list and freeing
3077 * the data struct.
3078 *
3079 * The socket is already closed at this point so no close to sock.
3080 */
3081 void ust_app_unregister(int sock)
3082 {
3083 struct ust_app *lta;
3084 struct lttng_ht_node_ulong *node;
3085 struct lttng_ht_iter ust_app_sock_iter;
3086 struct lttng_ht_iter iter;
3087 struct ust_app_session *ua_sess;
3088 int ret;
3089
3090 rcu_read_lock();
3091
3092 /* Get the node reference for a call_rcu */
3093 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &ust_app_sock_iter);
3094 node = lttng_ht_iter_get_node_ulong(&ust_app_sock_iter);
3095 assert(node);
3096
3097 lta = caa_container_of(node, struct ust_app, sock_n);
3098 DBG("PID %d unregistering with sock %d", lta->pid, sock);
3099
3100 /*
3101 * Perform "push metadata" and flush all application streams
3102 * before removing app from hash tables, ensuring proper
3103 * behavior of data_pending check.
3104 * Remove sessions so they are not visible during deletion.
3105 */
3106 cds_lfht_for_each_entry(lta->sessions->ht, &iter.iter, ua_sess,
3107 node.node) {
3108 struct ust_registry_session *registry;
3109
3110 ret = lttng_ht_del(lta->sessions, &iter);
3111 if (ret) {
3112 /* The session was already removed so scheduled for teardown. */
3113 continue;
3114 }
3115
3116 (void) ust_app_flush_app_session(lta, ua_sess);
3117
3118 /*
3119 * Add session to list for teardown. This is safe since at this point we
3120 * are the only one using this list.
3121 */
3122 pthread_mutex_lock(&ua_sess->lock);
3123
3124 /*
3125 * Normally, this is done in the delete session process which is
3126 * executed in the call rcu below. However, upon registration we can't
3127 * afford to wait for the grace period before pushing data or else the
3128 * data pending feature can race between the unregistration and stop
3129 * command where the data pending command is sent *before* the grace
3130 * period ended.
3131 *
3132 * The close metadata below nullifies the metadata pointer in the
3133 * session so the delete session will NOT push/close a second time.
3134 */
3135 registry = get_session_registry(ua_sess);
3136 if (registry && !registry->metadata_closed) {
3137 /* Push metadata for application before freeing the application. */
3138 (void) push_metadata(registry, ua_sess->consumer);
3139
3140 /*
3141 * Don't ask to close metadata for global per UID buffers. Close
3142 * metadata only on destroy trace session in this case. Also, the
3143 * previous push metadata could have flag the metadata registry to
3144 * close so don't send a close command if closed.
3145 */
3146 if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID &&
3147 !registry->metadata_closed) {
3148 /* And ask to close it for this session registry. */
3149 (void) close_metadata(registry, ua_sess->consumer);
3150 }
3151 }
3152 cds_list_add(&ua_sess->teardown_node, &lta->teardown_head);
3153
3154 pthread_mutex_unlock(&ua_sess->lock);
3155 }
3156
3157 /* Remove application from PID hash table */
3158 ret = lttng_ht_del(ust_app_ht_by_sock, &ust_app_sock_iter);
3159 assert(!ret);
3160
3161 /*
3162 * Remove application from notify hash table. The thread handling the
3163 * notify socket could have deleted the node so ignore on error because
3164 * either way it's valid. The close of that socket is handled by the other
3165 * thread.
3166 */
3167 iter.iter.node = &lta->notify_sock_n.node;
3168 (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter);
3169
3170 /*
3171 * Ignore return value since the node might have been removed before by an
3172 * add replace during app registration because the PID can be reassigned by
3173 * the OS.
3174 */
3175 iter.iter.node = &lta->pid_n.node;
3176 ret = lttng_ht_del(ust_app_ht, &iter);
3177 if (ret) {
3178 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3179 lta->pid);
3180 }
3181
3182 /* Free memory */
3183 call_rcu(&lta->pid_n.head, delete_ust_app_rcu);
3184
3185 rcu_read_unlock();
3186 return;
3187 }
3188
3189 /*
3190 * Fill events array with all events name of all registered apps.
3191 */
3192 int ust_app_list_events(struct lttng_event **events)
3193 {
3194 int ret, handle;
3195 size_t nbmem, count = 0;
3196 struct lttng_ht_iter iter;
3197 struct ust_app *app;
3198 struct lttng_event *tmp_event;
3199
3200 nbmem = UST_APP_EVENT_LIST_SIZE;
3201 tmp_event = zmalloc(nbmem * sizeof(struct lttng_event));
3202 if (tmp_event == NULL) {
3203 PERROR("zmalloc ust app events");
3204 ret = -ENOMEM;
3205 goto error;
3206 }
3207
3208 rcu_read_lock();
3209
3210 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3211 struct lttng_ust_tracepoint_iter uiter;
3212
3213 health_code_update();
3214
3215 if (!app->compatible) {
3216 /*
3217 * TODO: In time, we should notice the caller of this error by
3218 * telling him that this is a version error.
3219 */
3220 continue;
3221 }
3222 handle = ustctl_tracepoint_list(app->sock);
3223 if (handle < 0) {
3224 if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) {
3225 ERR("UST app list events getting handle failed for app pid %d",
3226 app->pid);
3227 }
3228 continue;
3229 }
3230
3231 while ((ret = ustctl_tracepoint_list_get(app->sock, handle,
3232 &uiter)) != -LTTNG_UST_ERR_NOENT) {
3233 /* Handle ustctl error. */
3234 if (ret < 0) {
3235 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
3236 ERR("UST app tp list get failed for app %d with ret %d",
3237 app->sock, ret);
3238 } else {
3239 DBG3("UST app tp list get failed. Application is dead");
3240 /*
3241 * This is normal behavior, an application can die during the
3242 * creation process. Don't report an error so the execution can
3243 * continue normally. Continue normal execution.
3244 */
3245 break;
3246 }
3247 free(tmp_event);
3248