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